xref: /freebsd/sys/riscv/sifive/sifive_spi.c (revision 5201decc8b431a8f4f53efb8db5fc452557de68b)
190a089cfSJessica Clarke /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
390a089cfSJessica Clarke  *
490a089cfSJessica Clarke  * Copyright (c) 2019 Axiado Corporation
590a089cfSJessica Clarke  * All rights reserved.
690a089cfSJessica Clarke  *
790a089cfSJessica Clarke  * This software was developed in part by Philip Paeps and Kristof Provost
890a089cfSJessica Clarke  * under contract for Axiado Corporation.
990a089cfSJessica Clarke  *
1090a089cfSJessica Clarke  * Redistribution and use in source and binary forms, with or without
1190a089cfSJessica Clarke  * modification, are permitted provided that the following conditions
1290a089cfSJessica Clarke  * are met:
1390a089cfSJessica Clarke  * 1. Redistributions of source code must retain the above copyright
1490a089cfSJessica Clarke  *    notice, this list of conditions and the following disclaimer.
1590a089cfSJessica Clarke  * 2. Redistributions in binary form must reproduce the above copyright
1690a089cfSJessica Clarke  *    notice, this list of conditions and the following disclaimer in the
1790a089cfSJessica Clarke  *    documentation and/or other materials provided with the distribution.
1890a089cfSJessica Clarke  *
1990a089cfSJessica Clarke  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2090a089cfSJessica Clarke  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2190a089cfSJessica Clarke  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2290a089cfSJessica Clarke  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2390a089cfSJessica Clarke  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2490a089cfSJessica Clarke  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2590a089cfSJessica Clarke  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2690a089cfSJessica Clarke  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2790a089cfSJessica Clarke  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2890a089cfSJessica Clarke  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2990a089cfSJessica Clarke  * SUCH DAMAGE.
3090a089cfSJessica Clarke  */
3190a089cfSJessica Clarke 
3290a089cfSJessica Clarke #include <sys/param.h>
3390a089cfSJessica Clarke #include <sys/systm.h>
3490a089cfSJessica Clarke #include <sys/bus.h>
3590a089cfSJessica Clarke #include <sys/kernel.h>
3690a089cfSJessica Clarke #include <sys/lock.h>
3790a089cfSJessica Clarke #include <sys/module.h>
3890a089cfSJessica Clarke #include <sys/mutex.h>
3990a089cfSJessica Clarke #include <sys/rman.h>
4090a089cfSJessica Clarke 
4190a089cfSJessica Clarke #include <machine/bus.h>
4290a089cfSJessica Clarke #include <machine/cpu.h>
4390a089cfSJessica Clarke 
44be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
4590a089cfSJessica Clarke 
4690a089cfSJessica Clarke #include <dev/ofw/ofw_bus.h>
4790a089cfSJessica Clarke #include <dev/ofw/ofw_bus_subr.h>
4890a089cfSJessica Clarke #include <dev/ofw/openfirm.h>
4990a089cfSJessica Clarke 
5090a089cfSJessica Clarke #include <dev/spibus/spi.h>
5190a089cfSJessica Clarke #include <dev/spibus/spibusvar.h>
5290a089cfSJessica Clarke 
5390a089cfSJessica Clarke #include "spibus_if.h"
5490a089cfSJessica Clarke 
5590a089cfSJessica Clarke #if 1
5690a089cfSJessica Clarke #define	DBGPRINT(dev, fmt, args...) \
5790a089cfSJessica Clarke 	device_printf(dev, "%s: " fmt "\n", __func__, ## args)
5890a089cfSJessica Clarke #else
5990a089cfSJessica Clarke #define	DBGPRINT(dev, fmt, args...)
6090a089cfSJessica Clarke #endif
6190a089cfSJessica Clarke 
6290a089cfSJessica Clarke static struct resource_spec sfspi_spec[] = {
6390a089cfSJessica Clarke 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
6490a089cfSJessica Clarke 	RESOURCE_SPEC_END
6590a089cfSJessica Clarke };
6690a089cfSJessica Clarke 
6790a089cfSJessica Clarke struct sfspi_softc {
6890a089cfSJessica Clarke 	device_t		dev;
6990a089cfSJessica Clarke 	device_t		parent;
7090a089cfSJessica Clarke 
7190a089cfSJessica Clarke 	struct mtx		mtx;
7290a089cfSJessica Clarke 
7390a089cfSJessica Clarke 	struct resource		*res;
7490a089cfSJessica Clarke 	bus_space_tag_t		bst;
7590a089cfSJessica Clarke 	bus_space_handle_t	bsh;
7690a089cfSJessica Clarke 
7790a089cfSJessica Clarke 	void			*ih;
7890a089cfSJessica Clarke 
7990a089cfSJessica Clarke 	clk_t			clk;
8090a089cfSJessica Clarke 	uint64_t		freq;
8190a089cfSJessica Clarke 	uint32_t		cs_max;
8290a089cfSJessica Clarke };
8390a089cfSJessica Clarke 
8490a089cfSJessica Clarke #define	SFSPI_LOCK(sc)			mtx_lock(&(sc)->mtx)
8590a089cfSJessica Clarke #define	SFSPI_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
8690a089cfSJessica Clarke #define	SFSPI_ASSERT_LOCKED(sc)		mtx_assert(&(sc)->mtx, MA_OWNED);
8790a089cfSJessica Clarke #define	SFSPI_ASSERT_UNLOCKED(sc)	mtx_assert(&(sc)->mtx, MA_NOTOWNED);
8890a089cfSJessica Clarke 
8990a089cfSJessica Clarke /*
9090a089cfSJessica Clarke  * Register offsets.
9190a089cfSJessica Clarke  * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 101.
9290a089cfSJessica Clarke  */
9390a089cfSJessica Clarke #define	SFSPI_REG_SCKDIV	0x00 /* Serial clock divisor */
9490a089cfSJessica Clarke #define	SFSPI_REG_SCKMODE	0x04 /* Serial clock mode */
9590a089cfSJessica Clarke #define	SFSPI_REG_CSID		0x10 /* Chip select ID */
9690a089cfSJessica Clarke #define	SFSPI_REG_CSDEF		0x14 /* Chip select default */
9790a089cfSJessica Clarke #define	SFSPI_REG_CSMODE	0x18 /* Chip select mode */
9890a089cfSJessica Clarke #define	SFSPI_REG_DELAY0	0x28 /* Delay control 0 */
9990a089cfSJessica Clarke #define	SFSPI_REG_DELAY1	0x2C /* Delay control 1 */
10090a089cfSJessica Clarke #define	SFSPI_REG_FMT		0x40 /* Frame format */
10190a089cfSJessica Clarke #define	SFSPI_REG_TXDATA	0x48 /* Tx FIFO data */
10290a089cfSJessica Clarke #define	SFSPI_REG_RXDATA	0x4C /* Rx FIFO data */
10390a089cfSJessica Clarke #define	SFSPI_REG_TXMARK	0x50 /* Tx FIFO watermark */
10490a089cfSJessica Clarke #define	SFSPI_REG_RXMARK	0x54 /* Rx FIFO watermark */
10590a089cfSJessica Clarke #define	SFSPI_REG_FCTRL		0x60 /* SPI flash interface control* */
10690a089cfSJessica Clarke #define	SFSPI_REG_FFMT		0x64 /* SPI flash instruction format* */
10790a089cfSJessica Clarke #define	SFSPI_REG_IE		0x70 /* SPI interrupt enable */
10890a089cfSJessica Clarke #define	SFSPI_REG_IP		0x74 /* SPI interrupt pending */
10990a089cfSJessica Clarke 
11090a089cfSJessica Clarke #define	SFSPI_SCKDIV_MASK	0xfff
11190a089cfSJessica Clarke 
11290a089cfSJessica Clarke #define	SFSPI_CSDEF_ALL		((1 << sc->cs_max)-1)
11390a089cfSJessica Clarke 
11490a089cfSJessica Clarke #define	SFSPI_CSMODE_AUTO	0x0U
11590a089cfSJessica Clarke #define	SFSPI_CSMODE_HOLD	0x2U
11690a089cfSJessica Clarke #define	SFSPI_CSMODE_OFF	0x3U
11790a089cfSJessica Clarke 
11890a089cfSJessica Clarke #define	SFSPI_TXDATA_DATA_MASK	0xff
11990a089cfSJessica Clarke #define	SFSPI_TXDATA_FULL	(1 << 31)
12090a089cfSJessica Clarke 
12190a089cfSJessica Clarke #define	SFSPI_RXDATA_DATA_MASK	0xff
12290a089cfSJessica Clarke #define	SFSPI_RXDATA_EMPTY	(1 << 31)
12390a089cfSJessica Clarke 
12490a089cfSJessica Clarke #define	SFSPI_SCKMODE_PHA	(1 << 0)
12590a089cfSJessica Clarke #define	SFSPI_SCKMODE_POL	(1 << 1)
12690a089cfSJessica Clarke 
12790a089cfSJessica Clarke #define	SFSPI_FMT_PROTO_SINGLE	0x0U
12890a089cfSJessica Clarke #define	SFSPI_FMT_PROTO_DUAL	0x1U
12990a089cfSJessica Clarke #define	SFSPI_FMT_PROTO_QUAD	0x2U
13090a089cfSJessica Clarke #define	SFSPI_FMT_PROTO_MASK	0x3U
13190a089cfSJessica Clarke #define	SFSPI_FMT_ENDIAN	(1 << 2)
13290a089cfSJessica Clarke #define	SFSPI_FMT_DIR		(1 << 3)
13390a089cfSJessica Clarke #define	SFSPI_FMT_LEN(x)	((uint32_t)(x) << 16)
13490a089cfSJessica Clarke #define	SFSPI_FMT_LEN_MASK	(0xfU << 16)
13590a089cfSJessica Clarke 
13690a089cfSJessica Clarke #define	SFSPI_FIFO_DEPTH	8
13790a089cfSJessica Clarke 
13890a089cfSJessica Clarke #define	SFSPI_READ(_sc, _reg)		\
13990a089cfSJessica Clarke     bus_space_read_4((_sc)->bst, (_sc)->bsh, (_reg))
14090a089cfSJessica Clarke #define	SFSPI_WRITE(_sc, _reg, _val)	\
14190a089cfSJessica Clarke     bus_space_write_4((_sc)->bst, (_sc)->bsh, (_reg), (_val))
14290a089cfSJessica Clarke 
14390a089cfSJessica Clarke static void
sfspi_tx(struct sfspi_softc * sc,uint8_t * buf,uint32_t bufsiz)14490a089cfSJessica Clarke sfspi_tx(struct sfspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
14590a089cfSJessica Clarke {
14690a089cfSJessica Clarke 	uint32_t val;
14790a089cfSJessica Clarke 	uint8_t *p, *end;
14890a089cfSJessica Clarke 
14990a089cfSJessica Clarke 	KASSERT(buf != NULL, ("TX buffer cannot be NULL"));
15090a089cfSJessica Clarke 
15190a089cfSJessica Clarke 	end = buf + bufsiz;
15290a089cfSJessica Clarke 	for (p = buf; p < end; p++) {
15390a089cfSJessica Clarke 		do {
15490a089cfSJessica Clarke 			val = SFSPI_READ(sc, SFSPI_REG_TXDATA);
15590a089cfSJessica Clarke 		} while (val & SFSPI_TXDATA_FULL);
15690a089cfSJessica Clarke 		val = *p;
15790a089cfSJessica Clarke 		SFSPI_WRITE(sc, SFSPI_REG_TXDATA, val);
15890a089cfSJessica Clarke 	}
15990a089cfSJessica Clarke }
16090a089cfSJessica Clarke 
16190a089cfSJessica Clarke static void
sfspi_rx(struct sfspi_softc * sc,uint8_t * buf,uint32_t bufsiz)16290a089cfSJessica Clarke sfspi_rx(struct sfspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
16390a089cfSJessica Clarke {
16490a089cfSJessica Clarke 	uint32_t val;
16590a089cfSJessica Clarke 	uint8_t *p, *end;
16690a089cfSJessica Clarke 
16790a089cfSJessica Clarke 	KASSERT(buf != NULL, ("RX buffer cannot be NULL"));
16890a089cfSJessica Clarke 	KASSERT(bufsiz <= SFSPI_FIFO_DEPTH,
16990a089cfSJessica Clarke 	    ("Cannot receive more than %d bytes at a time\n",
17090a089cfSJessica Clarke 	    SFSPI_FIFO_DEPTH));
17190a089cfSJessica Clarke 
17290a089cfSJessica Clarke 	end = buf + bufsiz;
17390a089cfSJessica Clarke 	for (p = buf; p < end; p++) {
17490a089cfSJessica Clarke 		do {
17590a089cfSJessica Clarke 			val = SFSPI_READ(sc, SFSPI_REG_RXDATA);
17690a089cfSJessica Clarke 		} while (val & SFSPI_RXDATA_EMPTY);
17790a089cfSJessica Clarke 		*p = val & SFSPI_RXDATA_DATA_MASK;
17890a089cfSJessica Clarke 	};
17990a089cfSJessica Clarke }
18090a089cfSJessica Clarke 
18190a089cfSJessica Clarke static int
sfspi_xfer_buf(struct sfspi_softc * sc,uint8_t * rxbuf,uint8_t * txbuf,uint32_t txlen,uint32_t rxlen)18290a089cfSJessica Clarke sfspi_xfer_buf(struct sfspi_softc *sc, uint8_t *rxbuf, uint8_t *txbuf,
18390a089cfSJessica Clarke     uint32_t txlen, uint32_t rxlen)
18490a089cfSJessica Clarke {
18590a089cfSJessica Clarke 	uint32_t bytes;
18690a089cfSJessica Clarke 
18790a089cfSJessica Clarke 	KASSERT(txlen == rxlen, ("TX and RX lengths must be equal"));
18890a089cfSJessica Clarke 	KASSERT(rxbuf != NULL, ("RX buffer cannot be NULL"));
18990a089cfSJessica Clarke 	KASSERT(txbuf != NULL, ("TX buffer cannot be NULL"));
19090a089cfSJessica Clarke 
19190a089cfSJessica Clarke 	while (txlen) {
19290a089cfSJessica Clarke 		bytes = (txlen > SFSPI_FIFO_DEPTH) ? SFSPI_FIFO_DEPTH : txlen;
19390a089cfSJessica Clarke 		sfspi_tx(sc, txbuf, bytes);
19490a089cfSJessica Clarke 		txbuf += bytes;
19590a089cfSJessica Clarke 		sfspi_rx(sc, rxbuf, bytes);
19690a089cfSJessica Clarke 		rxbuf += bytes;
19790a089cfSJessica Clarke 		txlen -= bytes;
19890a089cfSJessica Clarke 	}
19990a089cfSJessica Clarke 
20090a089cfSJessica Clarke 	return (0);
20190a089cfSJessica Clarke }
20290a089cfSJessica Clarke 
20390a089cfSJessica Clarke static int
sfspi_setup(struct sfspi_softc * sc,uint32_t cs,uint32_t mode,uint32_t freq)20490a089cfSJessica Clarke sfspi_setup(struct sfspi_softc *sc, uint32_t cs, uint32_t mode,
20590a089cfSJessica Clarke     uint32_t freq)
20690a089cfSJessica Clarke {
20790a089cfSJessica Clarke 	uint32_t csmode, fmt, sckdiv, sckmode;
20890a089cfSJessica Clarke 
20990a089cfSJessica Clarke 	SFSPI_ASSERT_LOCKED(sc);
21090a089cfSJessica Clarke 
21190a089cfSJessica Clarke 	/*
21290a089cfSJessica Clarke 	 * Fsck = Fin / 2 * (div + 1)
21390a089cfSJessica Clarke 	 * -> div = Fin / (2 * Fsck) - 1
21490a089cfSJessica Clarke 	 */
21590a089cfSJessica Clarke 	sckdiv = (howmany(sc->freq >> 1, freq) - 1) & SFSPI_SCKDIV_MASK;
21690a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_SCKDIV, sckdiv);
21790a089cfSJessica Clarke 
21890a089cfSJessica Clarke 	switch (mode) {
219f5d78beaSThomas Skibo 	case SPIBUS_MODE_NONE:
220f5d78beaSThomas Skibo 		sckmode = 0;
221f5d78beaSThomas Skibo 		break;
22290a089cfSJessica Clarke 	case SPIBUS_MODE_CPHA:
22390a089cfSJessica Clarke 		sckmode = SFSPI_SCKMODE_PHA;
22490a089cfSJessica Clarke 		break;
22590a089cfSJessica Clarke 	case SPIBUS_MODE_CPOL:
22690a089cfSJessica Clarke 		sckmode = SFSPI_SCKMODE_POL;
22790a089cfSJessica Clarke 		break;
22890a089cfSJessica Clarke 	case SPIBUS_MODE_CPOL_CPHA:
22990a089cfSJessica Clarke 		sckmode = SFSPI_SCKMODE_PHA | SFSPI_SCKMODE_POL;
23090a089cfSJessica Clarke 		break;
231f5d78beaSThomas Skibo 	default:
232f5d78beaSThomas Skibo 		return (EINVAL);
23390a089cfSJessica Clarke 	}
23490a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_SCKMODE, sckmode);
23590a089cfSJessica Clarke 
23690a089cfSJessica Clarke 	csmode = SFSPI_CSMODE_HOLD;
23790a089cfSJessica Clarke 	if (cs & SPIBUS_CS_HIGH)
23890a089cfSJessica Clarke 		csmode = SFSPI_CSMODE_AUTO;
23990a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_CSMODE, csmode);
24090a089cfSJessica Clarke 
24190a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_CSID, cs & ~SPIBUS_CS_HIGH);
24290a089cfSJessica Clarke 
24390a089cfSJessica Clarke 	fmt = SFSPI_FMT_PROTO_SINGLE | SFSPI_FMT_LEN(8);
24490a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_FMT, fmt);
24590a089cfSJessica Clarke 
24690a089cfSJessica Clarke 	return (0);
24790a089cfSJessica Clarke }
24890a089cfSJessica Clarke 
24990a089cfSJessica Clarke static int
sfspi_transfer(device_t dev,device_t child,struct spi_command * cmd)25090a089cfSJessica Clarke sfspi_transfer(device_t dev, device_t child, struct spi_command *cmd)
25190a089cfSJessica Clarke {
25290a089cfSJessica Clarke 	struct sfspi_softc *sc;
25390a089cfSJessica Clarke 	uint32_t clock, cs, csdef, mode;
25490a089cfSJessica Clarke 	int err;
25590a089cfSJessica Clarke 
25690a089cfSJessica Clarke 	KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
25790a089cfSJessica Clarke 	    ("TX and RX command sizes must be equal"));
25890a089cfSJessica Clarke 	KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
25990a089cfSJessica Clarke 	    ("TX and RX data sizes must be equal"));
26090a089cfSJessica Clarke 
26190a089cfSJessica Clarke 	sc = device_get_softc(dev);
26290a089cfSJessica Clarke 	spibus_get_cs(child, &cs);
26390a089cfSJessica Clarke 	spibus_get_clock(child, &clock);
26490a089cfSJessica Clarke 	spibus_get_mode(child, &mode);
26590a089cfSJessica Clarke 
26690a089cfSJessica Clarke 	if (cs > sc->cs_max) {
26790a089cfSJessica Clarke 		device_printf(sc->dev, "Invalid chip select %u\n", cs);
26890a089cfSJessica Clarke 		return (EINVAL);
26990a089cfSJessica Clarke 	}
27090a089cfSJessica Clarke 
27190a089cfSJessica Clarke 	SFSPI_LOCK(sc);
27290a089cfSJessica Clarke 	device_busy(sc->dev);
27390a089cfSJessica Clarke 
27490a089cfSJessica Clarke 	err = sfspi_setup(sc, cs, mode, clock);
27590a089cfSJessica Clarke 	if (err != 0) {
27690a089cfSJessica Clarke 		SFSPI_UNLOCK(sc);
27790a089cfSJessica Clarke 		return (err);
27890a089cfSJessica Clarke 	}
27990a089cfSJessica Clarke 
28090a089cfSJessica Clarke 	err = 0;
28190a089cfSJessica Clarke 	if (cmd->tx_cmd_sz > 0)
28290a089cfSJessica Clarke 		err = sfspi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd,
28390a089cfSJessica Clarke 		    cmd->tx_cmd_sz, cmd->rx_cmd_sz);
28490a089cfSJessica Clarke 	if (cmd->tx_data_sz > 0 && err == 0)
28590a089cfSJessica Clarke 		err = sfspi_xfer_buf(sc, cmd->rx_data, cmd->tx_data,
28690a089cfSJessica Clarke 		    cmd->tx_data_sz, cmd->rx_data_sz);
28790a089cfSJessica Clarke 
28890a089cfSJessica Clarke 	/* Deassert chip select. */
28990a089cfSJessica Clarke 	csdef = SFSPI_CSDEF_ALL & ~(1 << cs);
29090a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_CSDEF, csdef);
29190a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_CSDEF, SFSPI_CSDEF_ALL);
29290a089cfSJessica Clarke 
29390a089cfSJessica Clarke 	device_unbusy(sc->dev);
29490a089cfSJessica Clarke 	SFSPI_UNLOCK(sc);
29590a089cfSJessica Clarke 
29690a089cfSJessica Clarke 	return (err);
29790a089cfSJessica Clarke }
29890a089cfSJessica Clarke 
29990a089cfSJessica Clarke static int
sfspi_attach(device_t dev)30090a089cfSJessica Clarke sfspi_attach(device_t dev)
30190a089cfSJessica Clarke {
30290a089cfSJessica Clarke 	struct sfspi_softc *sc;
30390a089cfSJessica Clarke 	int error;
30490a089cfSJessica Clarke 
30590a089cfSJessica Clarke 	sc = device_get_softc(dev);
30690a089cfSJessica Clarke 	sc->dev = dev;
30790a089cfSJessica Clarke 
30890a089cfSJessica Clarke 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
30990a089cfSJessica Clarke 
31090a089cfSJessica Clarke 	error = bus_alloc_resources(dev, sfspi_spec, &sc->res);
31190a089cfSJessica Clarke 	if (error) {
31290a089cfSJessica Clarke 		device_printf(dev, "Couldn't allocate resources\n");
31390a089cfSJessica Clarke 		goto fail;
31490a089cfSJessica Clarke 	}
31590a089cfSJessica Clarke 	sc->bst = rman_get_bustag(sc->res);
31690a089cfSJessica Clarke 	sc->bsh = rman_get_bushandle(sc->res);
31790a089cfSJessica Clarke 
31890a089cfSJessica Clarke 	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
31990a089cfSJessica Clarke 	if (error) {
32090a089cfSJessica Clarke 		device_printf(dev, "Couldn't allocate clock: %d\n", error);
32190a089cfSJessica Clarke 		goto fail;
32290a089cfSJessica Clarke 	}
32390a089cfSJessica Clarke 	error = clk_enable(sc->clk);
32490a089cfSJessica Clarke 	if (error) {
32590a089cfSJessica Clarke 		device_printf(dev, "Couldn't enable clock: %d\n", error);
32690a089cfSJessica Clarke 		goto fail;
32790a089cfSJessica Clarke 	}
32890a089cfSJessica Clarke 
32990a089cfSJessica Clarke 	error = clk_get_freq(sc->clk, &sc->freq);
33090a089cfSJessica Clarke 	if (error) {
33190a089cfSJessica Clarke 		device_printf(sc->dev, "Couldn't get frequency: %d\n", error);
33290a089cfSJessica Clarke 		goto fail;
33390a089cfSJessica Clarke 	}
33490a089cfSJessica Clarke 
33590a089cfSJessica Clarke 	/*
33690a089cfSJessica Clarke 	 * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 103:
33790a089cfSJessica Clarke 	 * csdef is cs_width bits wide and all ones on reset.
33890a089cfSJessica Clarke 	 */
33990a089cfSJessica Clarke 	sc->cs_max = SFSPI_READ(sc, SFSPI_REG_CSDEF);
34090a089cfSJessica Clarke 
34190a089cfSJessica Clarke 	/*
34290a089cfSJessica Clarke 	 * We don't support the direct-mapped flash interface.
34390a089cfSJessica Clarke 	 * Disable it.
34490a089cfSJessica Clarke 	 */
34590a089cfSJessica Clarke 	SFSPI_WRITE(sc, SFSPI_REG_FCTRL, 0x0);
34690a089cfSJessica Clarke 
34790a089cfSJessica Clarke 	/* Probe and attach the spibus when interrupts are available. */
3485b56413dSWarner Losh 	sc->parent = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
349*5201deccSJohn Baldwin 	bus_delayed_attach_children(dev);
35090a089cfSJessica Clarke 
35190a089cfSJessica Clarke 	return (0);
35290a089cfSJessica Clarke 
35390a089cfSJessica Clarke fail:
35490a089cfSJessica Clarke 	bus_release_resources(dev, sfspi_spec, &sc->res);
35590a089cfSJessica Clarke 	mtx_destroy(&sc->mtx);
35690a089cfSJessica Clarke 	return (error);
35790a089cfSJessica Clarke }
35890a089cfSJessica Clarke 
35990a089cfSJessica Clarke static int
sfspi_probe(device_t dev)36090a089cfSJessica Clarke sfspi_probe(device_t dev)
36190a089cfSJessica Clarke {
36290a089cfSJessica Clarke 
36390a089cfSJessica Clarke 	if (!ofw_bus_status_okay(dev))
36490a089cfSJessica Clarke 		return (ENXIO);
36590a089cfSJessica Clarke 
36690a089cfSJessica Clarke 	if (!ofw_bus_is_compatible(dev, "sifive,spi0"))
36790a089cfSJessica Clarke 		return (ENXIO);
36890a089cfSJessica Clarke 
36990a089cfSJessica Clarke 	device_set_desc(dev, "SiFive SPI controller");
37090a089cfSJessica Clarke 
37190a089cfSJessica Clarke 	return (BUS_PROBE_DEFAULT);
37290a089cfSJessica Clarke }
37390a089cfSJessica Clarke 
37490a089cfSJessica Clarke static phandle_t
sfspi_get_node(device_t bus,device_t dev)37590a089cfSJessica Clarke sfspi_get_node(device_t bus, device_t dev)
37690a089cfSJessica Clarke {
37790a089cfSJessica Clarke 
37890a089cfSJessica Clarke 	return (ofw_bus_get_node(bus));
37990a089cfSJessica Clarke }
38090a089cfSJessica Clarke 
38190a089cfSJessica Clarke static device_method_t sfspi_methods[] = {
38290a089cfSJessica Clarke 	DEVMETHOD(device_probe,		sfspi_probe),
38390a089cfSJessica Clarke 	DEVMETHOD(device_attach,	sfspi_attach),
38490a089cfSJessica Clarke 
38590a089cfSJessica Clarke 	DEVMETHOD(spibus_transfer,	sfspi_transfer),
38690a089cfSJessica Clarke 
38790a089cfSJessica Clarke 	DEVMETHOD(ofw_bus_get_node,	sfspi_get_node),
38890a089cfSJessica Clarke 
38990a089cfSJessica Clarke 	DEVMETHOD_END
39090a089cfSJessica Clarke };
39190a089cfSJessica Clarke 
39290a089cfSJessica Clarke static driver_t sfspi_driver = {
39390a089cfSJessica Clarke 	"sifive_spi",
39490a089cfSJessica Clarke 	sfspi_methods,
39590a089cfSJessica Clarke 	sizeof(struct sfspi_softc)
39690a089cfSJessica Clarke };
39790a089cfSJessica Clarke 
398bb32809bSJohn Baldwin DRIVER_MODULE(sifive_spi, simplebus, sfspi_driver, 0, 0);
3995f31d14aSJohn Baldwin DRIVER_MODULE(ofw_spibus, sifive_spi, ofw_spibus_driver, 0, 0);
40090a089cfSJessica Clarke MODULE_DEPEND(sifive_spi, ofw_spibus, 1, 1, 1);
401