xref: /freebsd/sys/dev/spibus/controller/rockchip/rk_spi.c (revision b196276c20b577b364372f1aa1a646b9ce34bf5c)
1994c943aSEmmanuel Vadot /*-
2994c943aSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3994c943aSEmmanuel Vadot  *
4994c943aSEmmanuel Vadot  * Copyright (c) 2019 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5994c943aSEmmanuel Vadot  *
6994c943aSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7994c943aSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8994c943aSEmmanuel Vadot  * are met:
9994c943aSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10994c943aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11994c943aSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12994c943aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13994c943aSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14994c943aSEmmanuel Vadot  *
15994c943aSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16994c943aSEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17994c943aSEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18994c943aSEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19994c943aSEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20994c943aSEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21994c943aSEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22994c943aSEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23994c943aSEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24994c943aSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25994c943aSEmmanuel Vadot  * SUCH DAMAGE.
26994c943aSEmmanuel Vadot  */
27994c943aSEmmanuel Vadot 
28994c943aSEmmanuel Vadot #include <sys/param.h>
29994c943aSEmmanuel Vadot #include <sys/systm.h>
30994c943aSEmmanuel Vadot #include <sys/bus.h>
31994c943aSEmmanuel Vadot #include <sys/kernel.h>
32994c943aSEmmanuel Vadot #include <sys/lock.h>
33994c943aSEmmanuel Vadot #include <sys/module.h>
34994c943aSEmmanuel Vadot #include <sys/mutex.h>
35994c943aSEmmanuel Vadot #include <sys/rman.h>
36994c943aSEmmanuel Vadot #include <sys/resource.h>
37994c943aSEmmanuel Vadot #include <machine/bus.h>
38994c943aSEmmanuel Vadot 
39994c943aSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
40994c943aSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
41994c943aSEmmanuel Vadot 
42994c943aSEmmanuel Vadot #include <dev/spibus/spi.h>
43994c943aSEmmanuel Vadot #include <dev/spibus/spibusvar.h>
44994c943aSEmmanuel Vadot 
45be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
461f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
47994c943aSEmmanuel Vadot 
48994c943aSEmmanuel Vadot #include "spibus_if.h"
49994c943aSEmmanuel Vadot 
50994c943aSEmmanuel Vadot #define	RK_SPI_CTRLR0		0x0000
51994c943aSEmmanuel Vadot #define		CTRLR0_OPM_MASTER	(0 << 20)
52994c943aSEmmanuel Vadot #define		CTRLR0_XFM_TR		(0 << 18)
53994c943aSEmmanuel Vadot #define		CTRLR0_FRF_MOTO		(0 << 16)
54994c943aSEmmanuel Vadot #define		CTRLR0_BHT_8BIT		(1 << 13)
55994c943aSEmmanuel Vadot #define		CTRLR0_EM_BIG		(1 << 11)
56994c943aSEmmanuel Vadot #define		CTRLR0_SSD_ONE		(1 << 10)
57994c943aSEmmanuel Vadot #define		CTRLR0_SCPOL		(1 <<  7)
58994c943aSEmmanuel Vadot #define		CTRLR0_SCPH		(1 <<  6)
59994c943aSEmmanuel Vadot #define		CTRLR0_DFS_8BIT		(1 <<  0)
60994c943aSEmmanuel Vadot #define	RK_SPI_CTRLR1		0x0004
61994c943aSEmmanuel Vadot #define	RK_SPI_ENR		0x0008
62994c943aSEmmanuel Vadot #define	RK_SPI_SER		0x000c
63994c943aSEmmanuel Vadot #define	RK_SPI_BAUDR		0x0010
64994c943aSEmmanuel Vadot #define	RK_SPI_TXFTLR		0x0014
65994c943aSEmmanuel Vadot #define	RK_SPI_RXFTLR		0x0018
66994c943aSEmmanuel Vadot #define	RK_SPI_TXFLR		0x001c
67994c943aSEmmanuel Vadot #define	RK_SPI_RXFLR		0x0020
68994c943aSEmmanuel Vadot #define	RK_SPI_SR		0x0024
69994c943aSEmmanuel Vadot #define		SR_BUSY			(1 <<  0)
70994c943aSEmmanuel Vadot #define	RK_SPI_IPR		0x0028
71994c943aSEmmanuel Vadot #define	RK_SPI_IMR		0x002c
72994c943aSEmmanuel Vadot #define		IMR_RFFIM		(1 <<  4)
73994c943aSEmmanuel Vadot #define		IMR_TFEIM		(1 <<  0)
74994c943aSEmmanuel Vadot #define	RK_SPI_ISR		0x0030
75994c943aSEmmanuel Vadot #define		ISR_RFFIS		(1 <<  4)
76994c943aSEmmanuel Vadot #define		ISR_TFEIS		(1 <<  0)
77994c943aSEmmanuel Vadot #define	RK_SPI_RISR		0x0034
78994c943aSEmmanuel Vadot #define	RK_SPI_ICR		0x0038
79994c943aSEmmanuel Vadot #define	RK_SPI_DMACR		0x003c
80994c943aSEmmanuel Vadot #define	RK_SPI_DMATDLR		0x0040
81994c943aSEmmanuel Vadot #define	RK_SPI_DMARDLR		0x0044
82994c943aSEmmanuel Vadot #define	RK_SPI_TXDR		0x0400
83994c943aSEmmanuel Vadot #define	RK_SPI_RXDR		0x0800
84994c943aSEmmanuel Vadot 
85994c943aSEmmanuel Vadot #define	CS_MAX			1
86994c943aSEmmanuel Vadot 
87994c943aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
88994c943aSEmmanuel Vadot 	{ "rockchip,rk3328-spi",		1 },
89994c943aSEmmanuel Vadot 	{ "rockchip,rk3399-spi",		1 },
90994c943aSEmmanuel Vadot 	{ "rockchip,rk3568-spi",		1 },
91994c943aSEmmanuel Vadot 	{ NULL,					0 }
92994c943aSEmmanuel Vadot };
93994c943aSEmmanuel Vadot 
94994c943aSEmmanuel Vadot static struct resource_spec rk_spi_spec[] = {
95994c943aSEmmanuel Vadot 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
96994c943aSEmmanuel Vadot 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
97994c943aSEmmanuel Vadot 	{ -1, 0 }
98994c943aSEmmanuel Vadot };
99994c943aSEmmanuel Vadot 
100994c943aSEmmanuel Vadot struct rk_spi_softc {
101994c943aSEmmanuel Vadot 	device_t	dev;
102994c943aSEmmanuel Vadot 	device_t	spibus;
103994c943aSEmmanuel Vadot 	struct resource	*res[2];
104994c943aSEmmanuel Vadot 	struct mtx	mtx;
105994c943aSEmmanuel Vadot 	clk_t		clk_apb;
106994c943aSEmmanuel Vadot 	clk_t		clk_spi;
107994c943aSEmmanuel Vadot 	void *		intrhand;
108994c943aSEmmanuel Vadot 	int		transfer;
109994c943aSEmmanuel Vadot 	uint32_t	fifo_size;
110994c943aSEmmanuel Vadot 	uint64_t	max_freq;
111994c943aSEmmanuel Vadot 
112994c943aSEmmanuel Vadot 	uint32_t	intreg;
113994c943aSEmmanuel Vadot 	uint8_t		*rxbuf;
114994c943aSEmmanuel Vadot 	uint32_t	rxidx;
115994c943aSEmmanuel Vadot 	uint8_t		*txbuf;
116994c943aSEmmanuel Vadot 	uint32_t	txidx;
117994c943aSEmmanuel Vadot 	uint32_t	txlen;
118994c943aSEmmanuel Vadot 	uint32_t	rxlen;
119994c943aSEmmanuel Vadot };
120994c943aSEmmanuel Vadot 
121994c943aSEmmanuel Vadot #define	RK_SPI_LOCK(sc)			mtx_lock(&(sc)->mtx)
122994c943aSEmmanuel Vadot #define	RK_SPI_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
123994c943aSEmmanuel Vadot #define	RK_SPI_READ_4(sc, reg)		bus_read_4((sc)->res[0], (reg))
124994c943aSEmmanuel Vadot #define	RK_SPI_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[0], (reg), (val))
125994c943aSEmmanuel Vadot 
126994c943aSEmmanuel Vadot static int rk_spi_probe(device_t dev);
127994c943aSEmmanuel Vadot static int rk_spi_attach(device_t dev);
128994c943aSEmmanuel Vadot static int rk_spi_detach(device_t dev);
129994c943aSEmmanuel Vadot static void rk_spi_intr(void *arg);
130994c943aSEmmanuel Vadot 
131994c943aSEmmanuel Vadot static void
rk_spi_enable_chip(struct rk_spi_softc * sc,int enable)132994c943aSEmmanuel Vadot rk_spi_enable_chip(struct rk_spi_softc *sc, int enable)
133994c943aSEmmanuel Vadot {
134994c943aSEmmanuel Vadot 
135994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_ENR, enable ? 1 : 0);
136994c943aSEmmanuel Vadot }
137994c943aSEmmanuel Vadot 
138994c943aSEmmanuel Vadot static int
rk_spi_set_cs(struct rk_spi_softc * sc,uint32_t cs,bool active)139994c943aSEmmanuel Vadot rk_spi_set_cs(struct rk_spi_softc *sc, uint32_t cs, bool active)
140994c943aSEmmanuel Vadot {
141994c943aSEmmanuel Vadot 	uint32_t reg;
142994c943aSEmmanuel Vadot 
143994c943aSEmmanuel Vadot 	if (cs & SPIBUS_CS_HIGH) {
144994c943aSEmmanuel Vadot 		device_printf(sc->dev, "SPIBUS_CS_HIGH is not supported\n");
145994c943aSEmmanuel Vadot 		return (EINVAL);
146994c943aSEmmanuel Vadot 	}
147994c943aSEmmanuel Vadot 
148994c943aSEmmanuel Vadot 	if (cs > CS_MAX)
149994c943aSEmmanuel Vadot 		return (EINVAL);
150994c943aSEmmanuel Vadot 
151994c943aSEmmanuel Vadot 	reg = RK_SPI_READ_4(sc, RK_SPI_SER);
152994c943aSEmmanuel Vadot 	if (active)
153994c943aSEmmanuel Vadot 		reg |= (1 << cs);
154994c943aSEmmanuel Vadot 	else
155994c943aSEmmanuel Vadot 		reg &= ~(1 << cs);
156994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_SER, reg);
157994c943aSEmmanuel Vadot 
158994c943aSEmmanuel Vadot 	return (0);
159994c943aSEmmanuel Vadot }
160994c943aSEmmanuel Vadot 
161994c943aSEmmanuel Vadot static void
rk_spi_hw_setup(struct rk_spi_softc * sc,uint32_t mode,uint32_t freq)162994c943aSEmmanuel Vadot rk_spi_hw_setup(struct rk_spi_softc *sc, uint32_t mode, uint32_t freq)
163994c943aSEmmanuel Vadot {
164994c943aSEmmanuel Vadot 	uint32_t cr0;
165994c943aSEmmanuel Vadot 	uint32_t div;
166994c943aSEmmanuel Vadot 
167994c943aSEmmanuel Vadot 	cr0 =  CTRLR0_OPM_MASTER | CTRLR0_XFM_TR | CTRLR0_FRF_MOTO |
168994c943aSEmmanuel Vadot 	    CTRLR0_BHT_8BIT | CTRLR0_EM_BIG | CTRLR0_SSD_ONE |
169994c943aSEmmanuel Vadot 	    CTRLR0_DFS_8BIT;
170994c943aSEmmanuel Vadot 
171994c943aSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPHA)
172994c943aSEmmanuel Vadot 		cr0 |= CTRLR0_SCPH;
173994c943aSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPOL)
174994c943aSEmmanuel Vadot 		cr0 |= CTRLR0_SCPOL;
175994c943aSEmmanuel Vadot 
176994c943aSEmmanuel Vadot 	/* minimum divider is 2 */
177994c943aSEmmanuel Vadot 	if (sc->max_freq < freq*2) {
178994c943aSEmmanuel Vadot 		clk_set_freq(sc->clk_spi, 2 * freq, CLK_SET_ROUND_DOWN);
179994c943aSEmmanuel Vadot 		clk_get_freq(sc->clk_spi, &sc->max_freq);
180994c943aSEmmanuel Vadot 	}
181994c943aSEmmanuel Vadot 
182994c943aSEmmanuel Vadot 	div = ((sc->max_freq + freq - 1) / freq);
183994c943aSEmmanuel Vadot 	div = (div + 1) & 0xfffe;
184994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_BAUDR, div);
185994c943aSEmmanuel Vadot 
186994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_CTRLR0, cr0);
187994c943aSEmmanuel Vadot }
188994c943aSEmmanuel Vadot 
189994c943aSEmmanuel Vadot static uint32_t
rk_spi_fifo_size(struct rk_spi_softc * sc)190994c943aSEmmanuel Vadot rk_spi_fifo_size(struct rk_spi_softc *sc)
191994c943aSEmmanuel Vadot {
192994c943aSEmmanuel Vadot 	uint32_t txftlr, reg;
193994c943aSEmmanuel Vadot 
194994c943aSEmmanuel Vadot 	for (txftlr = 2; txftlr < 32; txftlr++) {
195994c943aSEmmanuel Vadot 		RK_SPI_WRITE_4(sc, RK_SPI_TXFTLR, txftlr);
196994c943aSEmmanuel Vadot 		reg = RK_SPI_READ_4(sc, RK_SPI_TXFTLR);
197994c943aSEmmanuel Vadot 		if (reg != txftlr)
198994c943aSEmmanuel Vadot 			break;
199994c943aSEmmanuel Vadot 	}
200994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_TXFTLR, 0);
201994c943aSEmmanuel Vadot 
202994c943aSEmmanuel Vadot 	if (txftlr == 31)
203994c943aSEmmanuel Vadot 		return 0;
204994c943aSEmmanuel Vadot 
205994c943aSEmmanuel Vadot 	return txftlr;
206994c943aSEmmanuel Vadot }
207994c943aSEmmanuel Vadot 
208994c943aSEmmanuel Vadot static void
rk_spi_empty_rxfifo(struct rk_spi_softc * sc)209994c943aSEmmanuel Vadot rk_spi_empty_rxfifo(struct rk_spi_softc *sc)
210994c943aSEmmanuel Vadot {
211994c943aSEmmanuel Vadot 	uint32_t rxlevel;
212994c943aSEmmanuel Vadot 	rxlevel = RK_SPI_READ_4(sc, RK_SPI_RXFLR);
213994c943aSEmmanuel Vadot 	while (sc->rxidx < sc->rxlen &&
214994c943aSEmmanuel Vadot 	    (rxlevel-- > 0)) {
215994c943aSEmmanuel Vadot 		sc->rxbuf[sc->rxidx++] = (uint8_t)RK_SPI_READ_4(sc, RK_SPI_RXDR);
216994c943aSEmmanuel Vadot 	}
217994c943aSEmmanuel Vadot }
218994c943aSEmmanuel Vadot 
219994c943aSEmmanuel Vadot static void
rk_spi_fill_txfifo(struct rk_spi_softc * sc)220994c943aSEmmanuel Vadot rk_spi_fill_txfifo(struct rk_spi_softc *sc)
221994c943aSEmmanuel Vadot {
222994c943aSEmmanuel Vadot 	uint32_t txlevel;
223994c943aSEmmanuel Vadot 	txlevel = RK_SPI_READ_4(sc, RK_SPI_TXFLR);
224994c943aSEmmanuel Vadot 
225994c943aSEmmanuel Vadot 	while (sc->txidx < sc->txlen && txlevel < sc->fifo_size) {
226994c943aSEmmanuel Vadot 		RK_SPI_WRITE_4(sc, RK_SPI_TXDR, sc->txbuf[sc->txidx++]);
227994c943aSEmmanuel Vadot 		txlevel++;
228994c943aSEmmanuel Vadot 	}
229994c943aSEmmanuel Vadot 
230994c943aSEmmanuel Vadot 	if (sc->txidx != sc->txlen)
231994c943aSEmmanuel Vadot 		sc->intreg |= (IMR_TFEIM  | IMR_RFFIM);
232994c943aSEmmanuel Vadot }
233994c943aSEmmanuel Vadot 
234994c943aSEmmanuel Vadot static int
rk_spi_xfer_buf(struct rk_spi_softc * sc,void * rxbuf,void * txbuf,uint32_t len)235994c943aSEmmanuel Vadot rk_spi_xfer_buf(struct rk_spi_softc *sc, void *rxbuf, void *txbuf, uint32_t len)
236994c943aSEmmanuel Vadot {
237994c943aSEmmanuel Vadot 	int err;
238994c943aSEmmanuel Vadot 
239994c943aSEmmanuel Vadot 	if (len == 0)
240994c943aSEmmanuel Vadot 		return (0);
241994c943aSEmmanuel Vadot 
242994c943aSEmmanuel Vadot 	sc->rxbuf = rxbuf;
243994c943aSEmmanuel Vadot 	sc->rxlen = len;
244994c943aSEmmanuel Vadot 	sc->rxidx = 0;
245994c943aSEmmanuel Vadot 	sc->txbuf = txbuf;
246994c943aSEmmanuel Vadot 	sc->txlen = len;
247994c943aSEmmanuel Vadot 	sc->txidx = 0;
248994c943aSEmmanuel Vadot 	sc->intreg = 0;
249994c943aSEmmanuel Vadot 	rk_spi_fill_txfifo(sc);
250994c943aSEmmanuel Vadot 
251994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_IMR, sc->intreg);
252994c943aSEmmanuel Vadot 
253994c943aSEmmanuel Vadot 	err = 0;
254994c943aSEmmanuel Vadot 	while (err == 0 && sc->intreg != 0)
255994c943aSEmmanuel Vadot 		err = msleep(sc, &sc->mtx, 0, "rk_spi", 10 * hz);
256994c943aSEmmanuel Vadot 
257994c943aSEmmanuel Vadot 	while (err == 0 && sc->rxidx != sc->txidx) {
258994c943aSEmmanuel Vadot 		/* read residual data from RX fifo */
259994c943aSEmmanuel Vadot 		rk_spi_empty_rxfifo(sc);
260994c943aSEmmanuel Vadot 	}
261994c943aSEmmanuel Vadot 
262994c943aSEmmanuel Vadot 	if (sc->rxidx != sc->rxlen || sc->txidx != sc->txlen)
263994c943aSEmmanuel Vadot 		err = EIO;
264994c943aSEmmanuel Vadot 
265994c943aSEmmanuel Vadot 	return (err);
266994c943aSEmmanuel Vadot }
267994c943aSEmmanuel Vadot 
268994c943aSEmmanuel Vadot static int
rk_spi_probe(device_t dev)269994c943aSEmmanuel Vadot rk_spi_probe(device_t dev)
270994c943aSEmmanuel Vadot {
271994c943aSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
272994c943aSEmmanuel Vadot 		return (ENXIO);
273994c943aSEmmanuel Vadot 
274994c943aSEmmanuel Vadot 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
275994c943aSEmmanuel Vadot 		return (ENXIO);
276994c943aSEmmanuel Vadot 
277994c943aSEmmanuel Vadot 	device_set_desc(dev, "Rockchip SPI");
278994c943aSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
279994c943aSEmmanuel Vadot }
280994c943aSEmmanuel Vadot 
281994c943aSEmmanuel Vadot static int
rk_spi_attach(device_t dev)282994c943aSEmmanuel Vadot rk_spi_attach(device_t dev)
283994c943aSEmmanuel Vadot {
284994c943aSEmmanuel Vadot 	struct rk_spi_softc *sc;
285994c943aSEmmanuel Vadot 	int error;
286994c943aSEmmanuel Vadot 
287994c943aSEmmanuel Vadot 	sc = device_get_softc(dev);
288994c943aSEmmanuel Vadot 	sc->dev = dev;
289994c943aSEmmanuel Vadot 
290994c943aSEmmanuel Vadot 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
291994c943aSEmmanuel Vadot 
292994c943aSEmmanuel Vadot 	if (bus_alloc_resources(dev, rk_spi_spec, sc->res) != 0) {
293994c943aSEmmanuel Vadot 		device_printf(dev, "cannot allocate resources for device\n");
294994c943aSEmmanuel Vadot 		error = ENXIO;
295994c943aSEmmanuel Vadot 		goto fail;
296994c943aSEmmanuel Vadot 	}
297994c943aSEmmanuel Vadot 
298994c943aSEmmanuel Vadot 	if (bus_setup_intr(dev, sc->res[1],
299994c943aSEmmanuel Vadot 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, rk_spi_intr, sc,
300994c943aSEmmanuel Vadot 	    &sc->intrhand)) {
301994c943aSEmmanuel Vadot 		bus_release_resources(dev, rk_spi_spec, sc->res);
302994c943aSEmmanuel Vadot 		device_printf(dev, "cannot setup interrupt handler\n");
303994c943aSEmmanuel Vadot 		return (ENXIO);
304994c943aSEmmanuel Vadot 	}
305994c943aSEmmanuel Vadot 
306994c943aSEmmanuel Vadot 	/* Activate the module clock. */
307994c943aSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "apb_pclk", &sc->clk_apb);
308994c943aSEmmanuel Vadot 	if (error != 0) {
309994c943aSEmmanuel Vadot 		device_printf(dev, "cannot get apb_pclk clock\n");
310994c943aSEmmanuel Vadot 		goto fail;
311994c943aSEmmanuel Vadot 	}
312994c943aSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "spiclk", &sc->clk_spi);
313994c943aSEmmanuel Vadot 	if (error != 0) {
314994c943aSEmmanuel Vadot 		device_printf(dev, "cannot get spiclk clock\n");
315994c943aSEmmanuel Vadot 		goto fail;
316994c943aSEmmanuel Vadot 	}
317994c943aSEmmanuel Vadot 	error = clk_enable(sc->clk_apb);
318994c943aSEmmanuel Vadot 	if (error != 0) {
319994c943aSEmmanuel Vadot 		device_printf(dev, "cannot enable ahb clock\n");
320994c943aSEmmanuel Vadot 		goto fail;
321994c943aSEmmanuel Vadot 	}
322994c943aSEmmanuel Vadot 	error = clk_enable(sc->clk_spi);
323994c943aSEmmanuel Vadot 	if (error != 0) {
324994c943aSEmmanuel Vadot 		device_printf(dev, "cannot enable spiclk clock\n");
325994c943aSEmmanuel Vadot 		goto fail;
326994c943aSEmmanuel Vadot 	}
327994c943aSEmmanuel Vadot 	clk_get_freq(sc->clk_spi, &sc->max_freq);
328994c943aSEmmanuel Vadot 
329994c943aSEmmanuel Vadot 	sc->fifo_size = rk_spi_fifo_size(sc);
330994c943aSEmmanuel Vadot 	if (sc->fifo_size == 0) {
331994c943aSEmmanuel Vadot 		device_printf(dev, "failed to get fifo size\n");
332994c943aSEmmanuel Vadot 		goto fail;
333994c943aSEmmanuel Vadot 	}
334994c943aSEmmanuel Vadot 
3355b56413dSWarner Losh 	sc->spibus = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
336994c943aSEmmanuel Vadot 
337994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_IMR, 0);
338994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_TXFTLR, sc->fifo_size/2 - 1);
339994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_RXFTLR, sc->fifo_size/2 - 1);
340994c943aSEmmanuel Vadot 
341*18250ec6SJohn Baldwin 	bus_attach_children(dev);
342*18250ec6SJohn Baldwin 	return (0);
343994c943aSEmmanuel Vadot 
344994c943aSEmmanuel Vadot fail:
345994c943aSEmmanuel Vadot 	rk_spi_detach(dev);
346994c943aSEmmanuel Vadot 	return (error);
347994c943aSEmmanuel Vadot }
348994c943aSEmmanuel Vadot 
349994c943aSEmmanuel Vadot static int
rk_spi_detach(device_t dev)350994c943aSEmmanuel Vadot rk_spi_detach(device_t dev)
351994c943aSEmmanuel Vadot {
352994c943aSEmmanuel Vadot 	struct rk_spi_softc *sc;
353994c943aSEmmanuel Vadot 
354994c943aSEmmanuel Vadot 	sc = device_get_softc(dev);
355994c943aSEmmanuel Vadot 
356994c943aSEmmanuel Vadot 	bus_generic_detach(sc->dev);
357994c943aSEmmanuel Vadot 
358994c943aSEmmanuel Vadot 	if (sc->clk_spi != NULL)
359994c943aSEmmanuel Vadot 		clk_release(sc->clk_spi);
360994c943aSEmmanuel Vadot 	if (sc->clk_apb)
361994c943aSEmmanuel Vadot 		clk_release(sc->clk_apb);
362994c943aSEmmanuel Vadot 
363994c943aSEmmanuel Vadot 	if (sc->intrhand != NULL)
364994c943aSEmmanuel Vadot 		bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand);
365994c943aSEmmanuel Vadot 
366994c943aSEmmanuel Vadot 	bus_release_resources(dev, rk_spi_spec, sc->res);
367994c943aSEmmanuel Vadot 	mtx_destroy(&sc->mtx);
368994c943aSEmmanuel Vadot 
369994c943aSEmmanuel Vadot 	return (0);
370994c943aSEmmanuel Vadot }
371994c943aSEmmanuel Vadot 
372994c943aSEmmanuel Vadot static void
rk_spi_intr(void * arg)373994c943aSEmmanuel Vadot rk_spi_intr(void *arg)
374994c943aSEmmanuel Vadot {
375994c943aSEmmanuel Vadot 	struct rk_spi_softc *sc;
376994c943aSEmmanuel Vadot 	uint32_t intreg, isr;
377994c943aSEmmanuel Vadot 
378994c943aSEmmanuel Vadot 	sc = arg;
379994c943aSEmmanuel Vadot 
380994c943aSEmmanuel Vadot 	RK_SPI_LOCK(sc);
381994c943aSEmmanuel Vadot 	intreg = RK_SPI_READ_4(sc, RK_SPI_IMR);
382994c943aSEmmanuel Vadot 	isr = RK_SPI_READ_4(sc, RK_SPI_ISR);
383994c943aSEmmanuel Vadot 	RK_SPI_WRITE_4(sc, RK_SPI_ICR, isr);
384994c943aSEmmanuel Vadot 
385994c943aSEmmanuel Vadot 	if (isr & ISR_RFFIS)
386994c943aSEmmanuel Vadot 		rk_spi_empty_rxfifo(sc);
387994c943aSEmmanuel Vadot 
388994c943aSEmmanuel Vadot 	if (isr & ISR_TFEIS)
389994c943aSEmmanuel Vadot 		rk_spi_fill_txfifo(sc);
390994c943aSEmmanuel Vadot 
391994c943aSEmmanuel Vadot 	/* no bytes left, disable interrupt */
392994c943aSEmmanuel Vadot 	if (sc->txidx == sc->txlen) {
393994c943aSEmmanuel Vadot 		sc->intreg = 0;
394994c943aSEmmanuel Vadot 		wakeup(sc);
395994c943aSEmmanuel Vadot 	}
396994c943aSEmmanuel Vadot 
397994c943aSEmmanuel Vadot 	if (sc->intreg != intreg) {
398994c943aSEmmanuel Vadot 		(void)RK_SPI_WRITE_4(sc, RK_SPI_IMR, sc->intreg);
399994c943aSEmmanuel Vadot 		(void)RK_SPI_READ_4(sc, RK_SPI_IMR);
400994c943aSEmmanuel Vadot 	}
401994c943aSEmmanuel Vadot 
402994c943aSEmmanuel Vadot 	RK_SPI_UNLOCK(sc);
403994c943aSEmmanuel Vadot }
404994c943aSEmmanuel Vadot 
405994c943aSEmmanuel Vadot static phandle_t
rk_spi_get_node(device_t bus,device_t dev)406994c943aSEmmanuel Vadot rk_spi_get_node(device_t bus, device_t dev)
407994c943aSEmmanuel Vadot {
408994c943aSEmmanuel Vadot 
409994c943aSEmmanuel Vadot 	return ofw_bus_get_node(bus);
410994c943aSEmmanuel Vadot }
411994c943aSEmmanuel Vadot 
412994c943aSEmmanuel Vadot static int
rk_spi_transfer(device_t dev,device_t child,struct spi_command * cmd)413994c943aSEmmanuel Vadot rk_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
414994c943aSEmmanuel Vadot {
415994c943aSEmmanuel Vadot 	struct rk_spi_softc *sc;
416994c943aSEmmanuel Vadot 	uint32_t cs, mode, clock;
417994c943aSEmmanuel Vadot 	int err = 0;
418994c943aSEmmanuel Vadot 
419994c943aSEmmanuel Vadot 	sc = device_get_softc(dev);
420994c943aSEmmanuel Vadot 
421994c943aSEmmanuel Vadot 	spibus_get_cs(child, &cs);
422994c943aSEmmanuel Vadot 	spibus_get_clock(child, &clock);
423994c943aSEmmanuel Vadot 	spibus_get_mode(child, &mode);
424994c943aSEmmanuel Vadot 
425994c943aSEmmanuel Vadot 	RK_SPI_LOCK(sc);
426994c943aSEmmanuel Vadot 	rk_spi_hw_setup(sc, mode, clock);
427994c943aSEmmanuel Vadot 	rk_spi_enable_chip(sc, 1);
428994c943aSEmmanuel Vadot 	err = rk_spi_set_cs(sc, cs, true);
429994c943aSEmmanuel Vadot 	if (err != 0) {
430994c943aSEmmanuel Vadot 		rk_spi_enable_chip(sc, 0);
431994c943aSEmmanuel Vadot 		RK_SPI_UNLOCK(sc);
432994c943aSEmmanuel Vadot 		return (err);
433994c943aSEmmanuel Vadot 	}
434994c943aSEmmanuel Vadot 
435994c943aSEmmanuel Vadot 	/* Transfer command then data bytes. */
436994c943aSEmmanuel Vadot 	err = 0;
437994c943aSEmmanuel Vadot 	if (cmd->tx_cmd_sz > 0)
438994c943aSEmmanuel Vadot 		err = rk_spi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd,
439994c943aSEmmanuel Vadot 		    cmd->tx_cmd_sz);
440994c943aSEmmanuel Vadot 	if (cmd->tx_data_sz > 0 && err == 0)
441994c943aSEmmanuel Vadot 		err = rk_spi_xfer_buf(sc, cmd->rx_data, cmd->tx_data,
442994c943aSEmmanuel Vadot 		    cmd->tx_data_sz);
443994c943aSEmmanuel Vadot 
444994c943aSEmmanuel Vadot 	rk_spi_set_cs(sc, cs, false);
445994c943aSEmmanuel Vadot 	rk_spi_enable_chip(sc, 0);
446994c943aSEmmanuel Vadot 	RK_SPI_UNLOCK(sc);
447994c943aSEmmanuel Vadot 
448994c943aSEmmanuel Vadot 	return (err);
449994c943aSEmmanuel Vadot }
450994c943aSEmmanuel Vadot 
451994c943aSEmmanuel Vadot static device_method_t rk_spi_methods[] = {
452994c943aSEmmanuel Vadot 	/* Device interface */
453994c943aSEmmanuel Vadot 	DEVMETHOD(device_probe,		rk_spi_probe),
454994c943aSEmmanuel Vadot 	DEVMETHOD(device_attach,	rk_spi_attach),
455994c943aSEmmanuel Vadot 	DEVMETHOD(device_detach,	rk_spi_detach),
456994c943aSEmmanuel Vadot 
457994c943aSEmmanuel Vadot         /* spibus_if  */
458994c943aSEmmanuel Vadot 	DEVMETHOD(spibus_transfer,	rk_spi_transfer),
459994c943aSEmmanuel Vadot 
460994c943aSEmmanuel Vadot         /* ofw_bus_if */
461994c943aSEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	rk_spi_get_node),
462994c943aSEmmanuel Vadot 
463994c943aSEmmanuel Vadot 	DEVMETHOD_END
464994c943aSEmmanuel Vadot };
465994c943aSEmmanuel Vadot 
466994c943aSEmmanuel Vadot static driver_t rk_spi_driver = {
467994c943aSEmmanuel Vadot 	"spi",
468994c943aSEmmanuel Vadot 	rk_spi_methods,
469994c943aSEmmanuel Vadot 	sizeof(struct rk_spi_softc),
470994c943aSEmmanuel Vadot };
471994c943aSEmmanuel Vadot 
472994c943aSEmmanuel Vadot DRIVER_MODULE(rk_spi, simplebus, rk_spi_driver, 0, 0);
473994c943aSEmmanuel Vadot DRIVER_MODULE(ofw_spibus, rk_spi, ofw_spibus_driver, 0, 0);
474994c943aSEmmanuel Vadot MODULE_DEPEND(rk_spi, ofw_spibus, 1, 1, 1);
475994c943aSEmmanuel Vadot OFWBUS_PNP_INFO(compat_data);
476