xref: /freebsd/sys/arm/freescale/imx/imx_spi.c (revision a0fd233964cfa62aa20b62263dffa31349443d15)
1*a0fd2339SIan Lepore /*-
2*a0fd2339SIan Lepore  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*a0fd2339SIan Lepore  *
4*a0fd2339SIan Lepore  * Copyright (c) 2018 Ian Lepore <ian@freebsd.org>
5*a0fd2339SIan Lepore  * All rights reserved.
6*a0fd2339SIan Lepore  *
7*a0fd2339SIan Lepore  * Redistribution and use in source and binary forms, with or without
8*a0fd2339SIan Lepore  * modification, are permitted provided that the following conditions
9*a0fd2339SIan Lepore  * are met:
10*a0fd2339SIan Lepore  * 1. Redistributions of source code must retain the above copyright
11*a0fd2339SIan Lepore  *    notice, this list of conditions and the following disclaimer.
12*a0fd2339SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
13*a0fd2339SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
14*a0fd2339SIan Lepore  *    documentation and/or other materials provided with the distribution.
15*a0fd2339SIan Lepore  *
16*a0fd2339SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*a0fd2339SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*a0fd2339SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*a0fd2339SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*a0fd2339SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*a0fd2339SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*a0fd2339SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*a0fd2339SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*a0fd2339SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*a0fd2339SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*a0fd2339SIan Lepore  * SUCH DAMAGE.
27*a0fd2339SIan Lepore  */
28*a0fd2339SIan Lepore 
29*a0fd2339SIan Lepore #include <sys/cdefs.h>
30*a0fd2339SIan Lepore __FBSDID("$FreeBSD$");
31*a0fd2339SIan Lepore 
32*a0fd2339SIan Lepore /*
33*a0fd2339SIan Lepore  * Driver for imx Enhanced Configurable SPI; master-mode only.
34*a0fd2339SIan Lepore  */
35*a0fd2339SIan Lepore 
36*a0fd2339SIan Lepore #include <sys/param.h>
37*a0fd2339SIan Lepore #include <sys/systm.h>
38*a0fd2339SIan Lepore #include <sys/bus.h>
39*a0fd2339SIan Lepore #include <sys/gpio.h>
40*a0fd2339SIan Lepore #include <sys/kernel.h>
41*a0fd2339SIan Lepore #include <sys/lock.h>
42*a0fd2339SIan Lepore #include <sys/module.h>
43*a0fd2339SIan Lepore #include <sys/mutex.h>
44*a0fd2339SIan Lepore #include <sys/rman.h>
45*a0fd2339SIan Lepore #include <sys/sysctl.h>
46*a0fd2339SIan Lepore #include <machine/bus.h>
47*a0fd2339SIan Lepore #include <machine/cpu.h>
48*a0fd2339SIan Lepore #include <machine/intr.h>
49*a0fd2339SIan Lepore 
50*a0fd2339SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
51*a0fd2339SIan Lepore 
52*a0fd2339SIan Lepore #include <dev/gpio/gpiobusvar.h>
53*a0fd2339SIan Lepore #include <dev/ofw/ofw_bus.h>
54*a0fd2339SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
55*a0fd2339SIan Lepore #include <dev/ofw/openfirm.h>
56*a0fd2339SIan Lepore #include <dev/spibus/spi.h>
57*a0fd2339SIan Lepore #include <dev/spibus/spibusvar.h>
58*a0fd2339SIan Lepore 
59*a0fd2339SIan Lepore #include "spibus_if.h"
60*a0fd2339SIan Lepore 
61*a0fd2339SIan Lepore #define	ECSPI_RXDATA            0x00
62*a0fd2339SIan Lepore #define	ECSPI_TXDATA            0x04
63*a0fd2339SIan Lepore #define	ECSPI_CTLREG            0x08
64*a0fd2339SIan Lepore #define	  CTLREG_BLEN_SHIFT	  20
65*a0fd2339SIan Lepore #define	  CTLREG_BLEN_MASK	  0x0fff
66*a0fd2339SIan Lepore #define	  CTLREG_CSEL_SHIFT	  18
67*a0fd2339SIan Lepore #define	  CTLREG_CSEL_MASK	  0x03
68*a0fd2339SIan Lepore #define	  CTLREG_DRCTL_SHIFT	  16
69*a0fd2339SIan Lepore #define	  CTLREG_DRCTL_MASK	  0x03
70*a0fd2339SIan Lepore #define	  CTLREG_PREDIV_SHIFT	  12
71*a0fd2339SIan Lepore #define	  CTLREG_PREDIV_MASK	  0x0f
72*a0fd2339SIan Lepore #define	  CTLREG_POSTDIV_SHIFT	  8
73*a0fd2339SIan Lepore #define	  CTLREG_POSTDIV_MASK	  0x0f
74*a0fd2339SIan Lepore #define	  CTLREG_CMODE_SHIFT	  4
75*a0fd2339SIan Lepore #define	  CTLREG_CMODE_MASK	  0x0f
76*a0fd2339SIan Lepore #define	  CTLREG_CMODES_MASTER	  (CTLREG_CMODE_MASK << CTLREG_CMODE_SHIFT)
77*a0fd2339SIan Lepore #define	  CTLREG_SMC		  (1u << 3)
78*a0fd2339SIan Lepore #define	  CTLREG_XCH		  (1u << 2)
79*a0fd2339SIan Lepore #define	  CTLREG_HT		  (1u << 1)
80*a0fd2339SIan Lepore #define	  CTLREG_EN		  (1u << 0)
81*a0fd2339SIan Lepore #define	ECSPI_CFGREG		0x0c
82*a0fd2339SIan Lepore #define	  CFGREG_HTLEN_SHIFT	  24
83*a0fd2339SIan Lepore #define	  CFGREG_SCLKCTL_SHIFT	  20
84*a0fd2339SIan Lepore #define	  CFGREG_DATACTL_SHIFT	  16
85*a0fd2339SIan Lepore #define	  CFGREG_SSPOL_SHIFT	  12
86*a0fd2339SIan Lepore #define	  CFGREG_SSCTL_SHIFT	   8
87*a0fd2339SIan Lepore #define	  CFGREG_SCLKPOL_SHIFT	   4
88*a0fd2339SIan Lepore #define	  CFGREG_SCLKPHA_SHIFT	   0
89*a0fd2339SIan Lepore #define	  CFGREG_MASK		   0x0f /* all CFGREG fields are 4 bits */
90*a0fd2339SIan Lepore #define	ECSPI_INTREG            0x10
91*a0fd2339SIan Lepore #define	  INTREG_TCEN		  (1u << 7)
92*a0fd2339SIan Lepore #define	  INTREG_ROEN		  (1u << 6)
93*a0fd2339SIan Lepore #define	  INTREG_RFEN		  (1u << 5)
94*a0fd2339SIan Lepore #define	  INTREG_RDREN		  (1u << 4)
95*a0fd2339SIan Lepore #define	  INTREG_RREN		  (1u << 3)
96*a0fd2339SIan Lepore #define	  INTREG_TFEN		  (1u << 2)
97*a0fd2339SIan Lepore #define	  INTREG_TDREN		  (1u << 1)
98*a0fd2339SIan Lepore #define	  INTREG_TEEN		  (1u << 0)
99*a0fd2339SIan Lepore #define	ECSPI_DMAREG            0x14
100*a0fd2339SIan Lepore #define	  DMA_RX_THRESH_SHIFT	  16
101*a0fd2339SIan Lepore #define	  DMA_RX_THRESH_MASK	  0x3f
102*a0fd2339SIan Lepore #define	  DMA_TX_THRESH_SHIFT	  0
103*a0fd2339SIan Lepore #define	  DMA_TX_THRESH_MASK	  0x3f
104*a0fd2339SIan Lepore #define	ECSPI_STATREG           0x18
105*a0fd2339SIan Lepore #define	  SREG_TC		  (1u << 7)
106*a0fd2339SIan Lepore #define	  SREG_RO		  (1u << 6)
107*a0fd2339SIan Lepore #define	  SREG_RF		  (1u << 5)
108*a0fd2339SIan Lepore #define	  SREG_RDR		  (1u << 4)
109*a0fd2339SIan Lepore #define	  SREG_RR		  (1u << 3)
110*a0fd2339SIan Lepore #define	  SREG_TF		  (1u << 2)
111*a0fd2339SIan Lepore #define	  SREG_TDR		  (1u << 1)
112*a0fd2339SIan Lepore #define	  SREG_TE		  (1u << 0)
113*a0fd2339SIan Lepore #define	ECSPI_PERIODREG         0x1c
114*a0fd2339SIan Lepore #define	ECSPI_TESTREG           0x20
115*a0fd2339SIan Lepore 
116*a0fd2339SIan Lepore #define	CS_MAX		4	/* Max number of chip selects. */
117*a0fd2339SIan Lepore #define	CS_MASK		0x03	/* Mask flag bits out of chipsel. */
118*a0fd2339SIan Lepore 
119*a0fd2339SIan Lepore #define	FIFO_SIZE	64
120*a0fd2339SIan Lepore #define	FIFO_RXTHRESH	32
121*a0fd2339SIan Lepore #define	FIFO_TXTHRESH	32
122*a0fd2339SIan Lepore 
123*a0fd2339SIan Lepore struct spi_softc {
124*a0fd2339SIan Lepore 	device_t 		dev;
125*a0fd2339SIan Lepore 	device_t		spibus;
126*a0fd2339SIan Lepore 	struct mtx		mtx;
127*a0fd2339SIan Lepore 	struct resource		*memres;
128*a0fd2339SIan Lepore 	struct resource		*intres;
129*a0fd2339SIan Lepore 	void			*inthandle;
130*a0fd2339SIan Lepore 	gpio_pin_t		cspins[CS_MAX];
131*a0fd2339SIan Lepore 	u_int			debug;
132*a0fd2339SIan Lepore 	u_int			basefreq;
133*a0fd2339SIan Lepore 	uint32_t		ctlreg;
134*a0fd2339SIan Lepore 	uint32_t		intreg;
135*a0fd2339SIan Lepore 	uint32_t		fifocnt;
136*a0fd2339SIan Lepore 	uint8_t			*rxbuf;
137*a0fd2339SIan Lepore 	uint32_t		rxidx;
138*a0fd2339SIan Lepore 	uint32_t		rxlen;
139*a0fd2339SIan Lepore 	uint8_t			*txbuf;
140*a0fd2339SIan Lepore 	uint32_t		txidx;
141*a0fd2339SIan Lepore 	uint32_t		txlen;
142*a0fd2339SIan Lepore };
143*a0fd2339SIan Lepore 
144*a0fd2339SIan Lepore static struct ofw_compat_data compat_data[] = {
145*a0fd2339SIan Lepore 	{"fsl,imx51-ecspi",  true},
146*a0fd2339SIan Lepore 	{"fsl,imx53-ecspi",  true},
147*a0fd2339SIan Lepore 	{"fsl,imx6dl-ecspi", true},
148*a0fd2339SIan Lepore 	{"fsl,imx6q-ecspi",  true},
149*a0fd2339SIan Lepore 	{"fsl,imx6sx-ecspi", true},
150*a0fd2339SIan Lepore 	{"fsl,imx6ul-ecspi", true},
151*a0fd2339SIan Lepore 	{NULL,               false}
152*a0fd2339SIan Lepore };
153*a0fd2339SIan Lepore 
154*a0fd2339SIan Lepore static inline uint32_t
155*a0fd2339SIan Lepore RD4(struct spi_softc *sc, bus_size_t offset)
156*a0fd2339SIan Lepore {
157*a0fd2339SIan Lepore 
158*a0fd2339SIan Lepore 	return (bus_read_4(sc->memres, offset));
159*a0fd2339SIan Lepore }
160*a0fd2339SIan Lepore 
161*a0fd2339SIan Lepore static inline void
162*a0fd2339SIan Lepore WR4(struct spi_softc *sc, bus_size_t offset, uint32_t value)
163*a0fd2339SIan Lepore {
164*a0fd2339SIan Lepore 
165*a0fd2339SIan Lepore 	bus_write_4(sc->memres, offset, value);
166*a0fd2339SIan Lepore }
167*a0fd2339SIan Lepore 
168*a0fd2339SIan Lepore static u_int
169*a0fd2339SIan Lepore spi_calc_clockdiv(struct spi_softc *sc, u_int busfreq)
170*a0fd2339SIan Lepore {
171*a0fd2339SIan Lepore 	u_int post, pre;
172*a0fd2339SIan Lepore 
173*a0fd2339SIan Lepore 	/* Returning 0 effectively sets both dividers to 1. */
174*a0fd2339SIan Lepore 	if (sc->basefreq <= busfreq)
175*a0fd2339SIan Lepore 		return (0);
176*a0fd2339SIan Lepore 
177*a0fd2339SIan Lepore 	/*
178*a0fd2339SIan Lepore 	 * Brute-force this; all real-world bus speeds are going to be found on
179*a0fd2339SIan Lepore 	 * the 1st or 2nd time through this loop.
180*a0fd2339SIan Lepore 	 */
181*a0fd2339SIan Lepore 	for (post = 0; post < 16; ++post) {
182*a0fd2339SIan Lepore 		pre = ((sc->basefreq >> post) / busfreq) - 1;
183*a0fd2339SIan Lepore 		if (pre < 16)
184*a0fd2339SIan Lepore 			break;
185*a0fd2339SIan Lepore 	}
186*a0fd2339SIan Lepore 	if (post == 16) {
187*a0fd2339SIan Lepore 		/* The lowest we can go is ~115 Hz. */
188*a0fd2339SIan Lepore 		pre = 15;
189*a0fd2339SIan Lepore 		post = 15;
190*a0fd2339SIan Lepore 	}
191*a0fd2339SIan Lepore 
192*a0fd2339SIan Lepore 	if (sc->debug >= 2) {
193*a0fd2339SIan Lepore 		device_printf(sc->dev,
194*a0fd2339SIan Lepore 		    "base %u bus %u; pre %u, post %u; actual busfreq %u\n",
195*a0fd2339SIan Lepore 		    sc->basefreq, busfreq, pre, post,
196*a0fd2339SIan Lepore 		    (sc->basefreq / (pre + 1)) / (1 << post));
197*a0fd2339SIan Lepore 	}
198*a0fd2339SIan Lepore 
199*a0fd2339SIan Lepore 	return (pre << CTLREG_PREDIV_SHIFT) | (post << CTLREG_POSTDIV_SHIFT);
200*a0fd2339SIan Lepore }
201*a0fd2339SIan Lepore 
202*a0fd2339SIan Lepore static void
203*a0fd2339SIan Lepore spi_set_chipsel(struct spi_softc *sc, u_int cs, bool active)
204*a0fd2339SIan Lepore {
205*a0fd2339SIan Lepore 	bool pinactive;
206*a0fd2339SIan Lepore 
207*a0fd2339SIan Lepore 	/*
208*a0fd2339SIan Lepore 	 * This is kinda crazy... the gpio pins for chipsel are defined as
209*a0fd2339SIan Lepore 	 * active-high in the dts, but are supposed to be treated as active-low
210*a0fd2339SIan Lepore 	 * by this driver.  So to turn on chipsel we have to invert the value
211*a0fd2339SIan Lepore 	 * passed to gpio_pin_set_active().  Then, to make it more fun, any
212*a0fd2339SIan Lepore 	 * slave can say its chipsel is active-high, so if that option is
213*a0fd2339SIan Lepore 	 * on, we have to invert the value again.
214*a0fd2339SIan Lepore 	 */
215*a0fd2339SIan Lepore 	pinactive = !active ^ (bool)(cs & SPIBUS_CS_HIGH);
216*a0fd2339SIan Lepore 
217*a0fd2339SIan Lepore 	if (sc->debug >= 2) {
218*a0fd2339SIan Lepore 		device_printf(sc->dev, "chipsel %u changed to %u\n",
219*a0fd2339SIan Lepore 		    (cs & ~SPIBUS_CS_HIGH), pinactive);
220*a0fd2339SIan Lepore 	}
221*a0fd2339SIan Lepore 
222*a0fd2339SIan Lepore 	/*
223*a0fd2339SIan Lepore 	 * Change the pin, then do a dummy read of its current state to ensure
224*a0fd2339SIan Lepore 	 * that the state change reaches the hardware before proceeding.
225*a0fd2339SIan Lepore 	 */
226*a0fd2339SIan Lepore 	gpio_pin_set_active(sc->cspins[cs & ~SPIBUS_CS_HIGH], pinactive);
227*a0fd2339SIan Lepore 	gpio_pin_is_active(sc->cspins[cs & ~SPIBUS_CS_HIGH], &pinactive);
228*a0fd2339SIan Lepore }
229*a0fd2339SIan Lepore 
230*a0fd2339SIan Lepore static void
231*a0fd2339SIan Lepore spi_hw_setup(struct spi_softc *sc, u_int cs, u_int mode, u_int freq)
232*a0fd2339SIan Lepore {
233*a0fd2339SIan Lepore 	uint32_t reg;
234*a0fd2339SIan Lepore 
235*a0fd2339SIan Lepore 	/*
236*a0fd2339SIan Lepore 	 * Set up control register, and write it first to bring the device out
237*a0fd2339SIan Lepore 	 * of reset.
238*a0fd2339SIan Lepore 	 */
239*a0fd2339SIan Lepore 	sc->ctlreg  = CTLREG_EN | CTLREG_CMODES_MASTER | CTLREG_SMC;
240*a0fd2339SIan Lepore 	sc->ctlreg |= spi_calc_clockdiv(sc, freq);
241*a0fd2339SIan Lepore 	sc->ctlreg |= 7 << CTLREG_BLEN_SHIFT; /* XXX byte at a time */
242*a0fd2339SIan Lepore 	WR4(sc, ECSPI_CTLREG, sc->ctlreg);
243*a0fd2339SIan Lepore 
244*a0fd2339SIan Lepore 	/*
245*a0fd2339SIan Lepore 	 * Set up the config register.  Note that we do all transfers with the
246*a0fd2339SIan Lepore 	 * SPI hardware's chip-select set to zero.  The actual chip select is
247*a0fd2339SIan Lepore 	 * handled with a gpio pin.
248*a0fd2339SIan Lepore 	 */
249*a0fd2339SIan Lepore 	reg = 0;
250*a0fd2339SIan Lepore 	if (cs & SPIBUS_CS_HIGH)
251*a0fd2339SIan Lepore 		reg |= 1u << CFGREG_SSPOL_SHIFT;
252*a0fd2339SIan Lepore 	if (mode & SPIBUS_MODE_CPHA)
253*a0fd2339SIan Lepore 		reg |= 1u << CFGREG_SCLKPHA_SHIFT;
254*a0fd2339SIan Lepore 	if (mode & SPIBUS_MODE_CPOL) {
255*a0fd2339SIan Lepore 		reg |= 1u << CFGREG_SCLKPOL_SHIFT;
256*a0fd2339SIan Lepore 		reg |= 1u << CFGREG_SCLKCTL_SHIFT;
257*a0fd2339SIan Lepore 	}
258*a0fd2339SIan Lepore 	WR4(sc, ECSPI_CFGREG, reg);
259*a0fd2339SIan Lepore 
260*a0fd2339SIan Lepore 	/*
261*a0fd2339SIan Lepore 	 * Set up the rx/tx FIFO interrupt thresholds.
262*a0fd2339SIan Lepore 	 */
263*a0fd2339SIan Lepore 	reg  = (FIFO_RXTHRESH << DMA_RX_THRESH_SHIFT);
264*a0fd2339SIan Lepore 	reg |= (FIFO_TXTHRESH << DMA_TX_THRESH_SHIFT);
265*a0fd2339SIan Lepore 	WR4(sc, ECSPI_DMAREG, reg);
266*a0fd2339SIan Lepore 
267*a0fd2339SIan Lepore 	/*
268*a0fd2339SIan Lepore 	 * Do a dummy read, to make sure the preceding writes reach the spi
269*a0fd2339SIan Lepore 	 * hardware before we assert any gpio chip select.
270*a0fd2339SIan Lepore 	 */
271*a0fd2339SIan Lepore 	(void)RD4(sc, ECSPI_CFGREG);
272*a0fd2339SIan Lepore }
273*a0fd2339SIan Lepore 
274*a0fd2339SIan Lepore static void
275*a0fd2339SIan Lepore spi_empty_rxfifo(struct spi_softc *sc)
276*a0fd2339SIan Lepore {
277*a0fd2339SIan Lepore 
278*a0fd2339SIan Lepore 	while (sc->rxidx < sc->rxlen && (RD4(sc, ECSPI_STATREG) & SREG_RR)) {
279*a0fd2339SIan Lepore 		sc->rxbuf[sc->rxidx++] = (uint8_t)RD4(sc, ECSPI_RXDATA);
280*a0fd2339SIan Lepore 		--sc->fifocnt;
281*a0fd2339SIan Lepore 	}
282*a0fd2339SIan Lepore }
283*a0fd2339SIan Lepore 
284*a0fd2339SIan Lepore static void
285*a0fd2339SIan Lepore spi_fill_txfifo(struct spi_softc *sc)
286*a0fd2339SIan Lepore {
287*a0fd2339SIan Lepore 
288*a0fd2339SIan Lepore 	while (sc->txidx < sc->txlen && sc->fifocnt < FIFO_SIZE) {
289*a0fd2339SIan Lepore 		WR4(sc, ECSPI_TXDATA, sc->txbuf[sc->txidx++]);
290*a0fd2339SIan Lepore 		++sc->fifocnt;
291*a0fd2339SIan Lepore 	}
292*a0fd2339SIan Lepore 
293*a0fd2339SIan Lepore 	/*
294*a0fd2339SIan Lepore 	 * If we're out of data, disable tx data ready (threshold) interrupts,
295*a0fd2339SIan Lepore 	 * and enable tx fifo empty interrupts.
296*a0fd2339SIan Lepore 	 */
297*a0fd2339SIan Lepore 	if (sc->txidx == sc->txlen)
298*a0fd2339SIan Lepore 		sc->intreg = (sc->intreg & ~INTREG_TDREN) | INTREG_TEEN;
299*a0fd2339SIan Lepore }
300*a0fd2339SIan Lepore 
301*a0fd2339SIan Lepore static void
302*a0fd2339SIan Lepore spi_intr(void *arg)
303*a0fd2339SIan Lepore {
304*a0fd2339SIan Lepore 	struct spi_softc *sc = arg;
305*a0fd2339SIan Lepore 	uint32_t intreg, status;
306*a0fd2339SIan Lepore 
307*a0fd2339SIan Lepore 	mtx_lock(&sc->mtx);
308*a0fd2339SIan Lepore 
309*a0fd2339SIan Lepore 	sc = arg;
310*a0fd2339SIan Lepore 	intreg = sc->intreg;
311*a0fd2339SIan Lepore 	status = RD4(sc, ECSPI_STATREG);
312*a0fd2339SIan Lepore 	WR4(sc, ECSPI_STATREG, status); /* Clear w1c bits. */
313*a0fd2339SIan Lepore 
314*a0fd2339SIan Lepore 	/*
315*a0fd2339SIan Lepore 	 * If we get an overflow error, just signal that the transfer is done
316*a0fd2339SIan Lepore 	 * and wakeup the waiting thread, which will see that txidx != txlen and
317*a0fd2339SIan Lepore 	 * return an IO error to the caller.
318*a0fd2339SIan Lepore 	 */
319*a0fd2339SIan Lepore 	if (__predict_false(status & SREG_RO)) {
320*a0fd2339SIan Lepore 		if (sc->debug || bootverbose) {
321*a0fd2339SIan Lepore 			device_printf(sc->dev, "rxoverflow rxidx %u txidx %u\n",
322*a0fd2339SIan Lepore 			    sc->rxidx, sc->txidx);
323*a0fd2339SIan Lepore 		}
324*a0fd2339SIan Lepore 		sc->intreg = 0;
325*a0fd2339SIan Lepore 		wakeup(sc);
326*a0fd2339SIan Lepore 		mtx_unlock(&sc->mtx);
327*a0fd2339SIan Lepore 		return;
328*a0fd2339SIan Lepore 	}
329*a0fd2339SIan Lepore 
330*a0fd2339SIan Lepore 	if (status & SREG_RR)
331*a0fd2339SIan Lepore 		spi_empty_rxfifo(sc);
332*a0fd2339SIan Lepore 
333*a0fd2339SIan Lepore 	if (status & SREG_TDR)
334*a0fd2339SIan Lepore 		spi_fill_txfifo(sc);
335*a0fd2339SIan Lepore 
336*a0fd2339SIan Lepore 	/*
337*a0fd2339SIan Lepore 	 * If we're out of bytes to send...
338*a0fd2339SIan Lepore 	 *  - If Transfer Complete is set (shift register is empty) and we've
339*a0fd2339SIan Lepore 	 *    received everything we expect, we're all done.
340*a0fd2339SIan Lepore 	 *  - Else if Tx Fifo Empty is set, we need to stop waiting for that and
341*a0fd2339SIan Lepore 	 *    switch to waiting for Transfer Complete (wait for shift register
342*a0fd2339SIan Lepore 	 *    to empty out), and also for Receive Ready (last of incoming data).
343*a0fd2339SIan Lepore 	 */
344*a0fd2339SIan Lepore 	if (sc->txidx == sc->txlen) {
345*a0fd2339SIan Lepore 		if ((status & SREG_TC) && sc->fifocnt == 0) {
346*a0fd2339SIan Lepore 			sc->intreg = 0;
347*a0fd2339SIan Lepore 			wakeup(sc);
348*a0fd2339SIan Lepore 		} else if (status & SREG_TE) {
349*a0fd2339SIan Lepore 			sc->intreg &= ~(sc->intreg & ~INTREG_TEEN);
350*a0fd2339SIan Lepore 			sc->intreg |= INTREG_TCEN | INTREG_RREN;
351*a0fd2339SIan Lepore 		}
352*a0fd2339SIan Lepore 	}
353*a0fd2339SIan Lepore 
354*a0fd2339SIan Lepore 	/*
355*a0fd2339SIan Lepore 	 * If interrupt flags changed, write the new flags to the hardware and
356*a0fd2339SIan Lepore 	 * do a dummy readback to ensure the changes reach the hardware before
357*a0fd2339SIan Lepore 	 * we exit the isr.
358*a0fd2339SIan Lepore 	 */
359*a0fd2339SIan Lepore 	if (sc->intreg != intreg) {
360*a0fd2339SIan Lepore 		WR4(sc, ECSPI_INTREG, sc->intreg);
361*a0fd2339SIan Lepore 		(void)RD4(sc, ECSPI_INTREG);
362*a0fd2339SIan Lepore 	}
363*a0fd2339SIan Lepore 
364*a0fd2339SIan Lepore 	if (sc->debug >= 3) {
365*a0fd2339SIan Lepore 		device_printf(sc->dev,
366*a0fd2339SIan Lepore 		    "spi_intr, sreg 0x%08x intreg was 0x%08x now 0x%08x\n",
367*a0fd2339SIan Lepore 		    status, intreg, sc->intreg);
368*a0fd2339SIan Lepore 	}
369*a0fd2339SIan Lepore 
370*a0fd2339SIan Lepore 	mtx_unlock(&sc->mtx);
371*a0fd2339SIan Lepore }
372*a0fd2339SIan Lepore 
373*a0fd2339SIan Lepore static int
374*a0fd2339SIan Lepore spi_xfer_buf(struct spi_softc *sc, void *rxbuf, void *txbuf, uint32_t len)
375*a0fd2339SIan Lepore {
376*a0fd2339SIan Lepore 	int err;
377*a0fd2339SIan Lepore 
378*a0fd2339SIan Lepore 	if (sc->debug >= 1) {
379*a0fd2339SIan Lepore 		device_printf(sc->dev,
380*a0fd2339SIan Lepore 		    "spi_xfer_buf, rxbuf %p txbuf %p len %u\n",
381*a0fd2339SIan Lepore 		    rxbuf, txbuf, len);
382*a0fd2339SIan Lepore 	}
383*a0fd2339SIan Lepore 
384*a0fd2339SIan Lepore 	if (len == 0)
385*a0fd2339SIan Lepore 		return (0);
386*a0fd2339SIan Lepore 
387*a0fd2339SIan Lepore 	sc->rxbuf = rxbuf;
388*a0fd2339SIan Lepore 	sc->rxlen = len;
389*a0fd2339SIan Lepore 	sc->rxidx = 0;
390*a0fd2339SIan Lepore 	sc->txbuf = txbuf;
391*a0fd2339SIan Lepore 	sc->txlen = len;
392*a0fd2339SIan Lepore 	sc->txidx = 0;
393*a0fd2339SIan Lepore 	sc->intreg = INTREG_RDREN | INTREG_TDREN;
394*a0fd2339SIan Lepore 	spi_fill_txfifo(sc);
395*a0fd2339SIan Lepore 
396*a0fd2339SIan Lepore 	/* Enable interrupts last; spi_fill_txfifo() can change sc->intreg */
397*a0fd2339SIan Lepore 	WR4(sc, ECSPI_INTREG, sc->intreg);
398*a0fd2339SIan Lepore 
399*a0fd2339SIan Lepore 	err = 0;
400*a0fd2339SIan Lepore 	while (err == 0 && sc->intreg != 0)
401*a0fd2339SIan Lepore 		err = msleep(sc, &sc->mtx, 0, "imxspi", 10 * hz);
402*a0fd2339SIan Lepore 
403*a0fd2339SIan Lepore 	if (sc->rxidx != sc->rxlen || sc->txidx != sc->txlen)
404*a0fd2339SIan Lepore 		err = EIO;
405*a0fd2339SIan Lepore 
406*a0fd2339SIan Lepore 	return (err);
407*a0fd2339SIan Lepore }
408*a0fd2339SIan Lepore 
409*a0fd2339SIan Lepore static int
410*a0fd2339SIan Lepore spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
411*a0fd2339SIan Lepore {
412*a0fd2339SIan Lepore 	struct spi_softc *sc = device_get_softc(dev);
413*a0fd2339SIan Lepore 	uint32_t cs, mode, clock;
414*a0fd2339SIan Lepore 	int err;
415*a0fd2339SIan Lepore 
416*a0fd2339SIan Lepore 	spibus_get_cs(child, &cs);
417*a0fd2339SIan Lepore 	spibus_get_clock(child, &clock);
418*a0fd2339SIan Lepore 	spibus_get_mode(child, &mode);
419*a0fd2339SIan Lepore 
420*a0fd2339SIan Lepore 	if (cs > CS_MAX || sc->cspins[cs] == NULL) {
421*a0fd2339SIan Lepore 		if (sc->debug || bootverbose)
422*a0fd2339SIan Lepore 			device_printf(sc->dev, "Invalid chip select %u\n", cs);
423*a0fd2339SIan Lepore 		return (EINVAL);
424*a0fd2339SIan Lepore 	}
425*a0fd2339SIan Lepore 
426*a0fd2339SIan Lepore 	mtx_lock(&sc->mtx);
427*a0fd2339SIan Lepore 
428*a0fd2339SIan Lepore 	if (sc->debug >= 1) {
429*a0fd2339SIan Lepore 		device_printf(sc->dev,
430*a0fd2339SIan Lepore 		    "spi_transfer, cs 0x%x clock %u mode %u\n",
431*a0fd2339SIan Lepore 		    cs, clock, mode);
432*a0fd2339SIan Lepore 	}
433*a0fd2339SIan Lepore 
434*a0fd2339SIan Lepore 	/* Set up the hardware and select the device. */
435*a0fd2339SIan Lepore 	spi_hw_setup(sc, cs, mode, clock);
436*a0fd2339SIan Lepore 	spi_set_chipsel(sc, cs, true);
437*a0fd2339SIan Lepore 
438*a0fd2339SIan Lepore 	/* Transfer command then data bytes. */
439*a0fd2339SIan Lepore 	err = 0;
440*a0fd2339SIan Lepore 	if (cmd->tx_cmd_sz > 0)
441*a0fd2339SIan Lepore 		err = spi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd,
442*a0fd2339SIan Lepore 		    cmd->tx_cmd_sz);
443*a0fd2339SIan Lepore 	if (cmd->tx_data_sz > 0 && err == 0)
444*a0fd2339SIan Lepore 		err = spi_xfer_buf(sc, cmd->rx_data, cmd->tx_data,
445*a0fd2339SIan Lepore 		    cmd->tx_data_sz);
446*a0fd2339SIan Lepore 
447*a0fd2339SIan Lepore 	/* Deselect the device, turn off (and reset) hardware. */
448*a0fd2339SIan Lepore 	spi_set_chipsel(sc, cs, false);
449*a0fd2339SIan Lepore 	WR4(sc, ECSPI_CTLREG, 0);
450*a0fd2339SIan Lepore 
451*a0fd2339SIan Lepore 	mtx_unlock(&sc->mtx);
452*a0fd2339SIan Lepore 
453*a0fd2339SIan Lepore 	return (err);
454*a0fd2339SIan Lepore }
455*a0fd2339SIan Lepore 
456*a0fd2339SIan Lepore static phandle_t
457*a0fd2339SIan Lepore spi_get_node(device_t bus, device_t dev)
458*a0fd2339SIan Lepore {
459*a0fd2339SIan Lepore 
460*a0fd2339SIan Lepore 	/*
461*a0fd2339SIan Lepore 	 * Share our controller node with our spibus child; it instantiates
462*a0fd2339SIan Lepore 	 * devices by walking the children contained within our node.
463*a0fd2339SIan Lepore 	 */
464*a0fd2339SIan Lepore 	return ofw_bus_get_node(bus);
465*a0fd2339SIan Lepore }
466*a0fd2339SIan Lepore 
467*a0fd2339SIan Lepore static int
468*a0fd2339SIan Lepore spi_detach(device_t dev)
469*a0fd2339SIan Lepore {
470*a0fd2339SIan Lepore 	struct spi_softc *sc = device_get_softc(dev);
471*a0fd2339SIan Lepore 	int idx;
472*a0fd2339SIan Lepore 
473*a0fd2339SIan Lepore 	mtx_lock(&sc->mtx);
474*a0fd2339SIan Lepore 
475*a0fd2339SIan Lepore 	bus_generic_detach(sc->dev);
476*a0fd2339SIan Lepore 	if (sc->spibus != NULL)
477*a0fd2339SIan Lepore 		device_delete_child(dev, sc->spibus);
478*a0fd2339SIan Lepore 
479*a0fd2339SIan Lepore 	for (idx = 0; idx < nitems(sc->cspins); ++idx) {
480*a0fd2339SIan Lepore 		if (sc->cspins[idx] != NULL)
481*a0fd2339SIan Lepore 			gpio_pin_release(sc->cspins[idx]);
482*a0fd2339SIan Lepore 	}
483*a0fd2339SIan Lepore 
484*a0fd2339SIan Lepore 	if (sc->inthandle != NULL)
485*a0fd2339SIan Lepore 		bus_teardown_intr(sc->dev, sc->intres, sc->inthandle);
486*a0fd2339SIan Lepore 	if (sc->intres != NULL)
487*a0fd2339SIan Lepore 		bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->intres);
488*a0fd2339SIan Lepore 	if (sc->memres != NULL)
489*a0fd2339SIan Lepore 		bus_release_resource(sc->dev, SYS_RES_MEMORY, 0, sc->memres);
490*a0fd2339SIan Lepore 
491*a0fd2339SIan Lepore 	mtx_unlock(&sc->mtx);
492*a0fd2339SIan Lepore 	mtx_destroy(&sc->mtx);
493*a0fd2339SIan Lepore 
494*a0fd2339SIan Lepore 	return (0);
495*a0fd2339SIan Lepore }
496*a0fd2339SIan Lepore 
497*a0fd2339SIan Lepore static int
498*a0fd2339SIan Lepore spi_attach(device_t dev)
499*a0fd2339SIan Lepore {
500*a0fd2339SIan Lepore 	struct spi_softc *sc = device_get_softc(dev);
501*a0fd2339SIan Lepore 	phandle_t node;
502*a0fd2339SIan Lepore 	int err, idx, rid;
503*a0fd2339SIan Lepore 
504*a0fd2339SIan Lepore 	sc->dev = dev;
505*a0fd2339SIan Lepore 	sc->basefreq = imx_ccm_ecspi_hz();
506*a0fd2339SIan Lepore 
507*a0fd2339SIan Lepore 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
508*a0fd2339SIan Lepore 
509*a0fd2339SIan Lepore 	/* Set up debug-enable sysctl. */
510*a0fd2339SIan Lepore 	SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
511*a0fd2339SIan Lepore 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
512*a0fd2339SIan Lepore 	    OID_AUTO, "debug", CTLFLAG_RWTUN, &sc->debug, 0,
513*a0fd2339SIan Lepore 	    "Enable debug, higher values = more info");
514*a0fd2339SIan Lepore 
515*a0fd2339SIan Lepore 	/* Allocate mmio register access resources. */
516*a0fd2339SIan Lepore 	rid = 0;
517*a0fd2339SIan Lepore 	sc->memres = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY, &rid,
518*a0fd2339SIan Lepore 	    RF_ACTIVE);
519*a0fd2339SIan Lepore 	if (sc->memres == NULL) {
520*a0fd2339SIan Lepore 		device_printf(sc->dev, "could not allocate registers\n");
521*a0fd2339SIan Lepore 		spi_detach(sc->dev);
522*a0fd2339SIan Lepore 		return (ENXIO);
523*a0fd2339SIan Lepore 	}
524*a0fd2339SIan Lepore 
525*a0fd2339SIan Lepore 	/* Allocate interrupt resources and set up handler. */
526*a0fd2339SIan Lepore 	rid = 0;
527*a0fd2339SIan Lepore 	sc->intres = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &rid,
528*a0fd2339SIan Lepore 	    RF_ACTIVE);
529*a0fd2339SIan Lepore 	if (sc->intres == NULL) {
530*a0fd2339SIan Lepore 		device_printf(sc->dev, "could not allocate interrupt\n");
531*a0fd2339SIan Lepore 		device_detach(sc->dev);
532*a0fd2339SIan Lepore 		return (ENXIO);
533*a0fd2339SIan Lepore 	}
534*a0fd2339SIan Lepore 	err = bus_setup_intr(sc->dev, sc->intres, INTR_TYPE_MISC | INTR_MPSAFE,
535*a0fd2339SIan Lepore 	    NULL, spi_intr, sc, &sc->inthandle);
536*a0fd2339SIan Lepore 	if (err != 0) {
537*a0fd2339SIan Lepore 		device_printf(sc->dev, "could not setup interrupt handler");
538*a0fd2339SIan Lepore 		device_detach(sc->dev);
539*a0fd2339SIan Lepore 		return (ENXIO);
540*a0fd2339SIan Lepore 	}
541*a0fd2339SIan Lepore 
542*a0fd2339SIan Lepore 	/* Allocate gpio pins for configured chip selects. */
543*a0fd2339SIan Lepore 	node = ofw_bus_get_node(sc->dev);
544*a0fd2339SIan Lepore 	for (err = 0, idx = 0; err == 0 && idx < nitems(sc->cspins); ++idx) {
545*a0fd2339SIan Lepore 		err = gpio_pin_get_by_ofw_propidx(sc->dev, node, "cs-gpios",
546*a0fd2339SIan Lepore 		    idx, &sc->cspins[idx]);
547*a0fd2339SIan Lepore 		if (err == 0) {
548*a0fd2339SIan Lepore 			gpio_pin_setflags(sc->cspins[idx], GPIO_PIN_OUTPUT);
549*a0fd2339SIan Lepore 		} else if (sc->debug >= 2) {
550*a0fd2339SIan Lepore 			device_printf(sc->dev,
551*a0fd2339SIan Lepore 			    "cannot configure gpio for chip select %u\n", idx);
552*a0fd2339SIan Lepore 		}
553*a0fd2339SIan Lepore 	}
554*a0fd2339SIan Lepore 
555*a0fd2339SIan Lepore 	/*
556*a0fd2339SIan Lepore 	 * Hardware init: put all channels into Master mode, turn off the enable
557*a0fd2339SIan Lepore 	 * bit (gates off clocks); we only enable the hardware while xfers run.
558*a0fd2339SIan Lepore 	 */
559*a0fd2339SIan Lepore 	WR4(sc, ECSPI_CTLREG, CTLREG_CMODES_MASTER);
560*a0fd2339SIan Lepore 
561*a0fd2339SIan Lepore 	/* Attach the bus driver. */
562*a0fd2339SIan Lepore 	sc->spibus = device_add_child(dev, "spibus", -1);
563*a0fd2339SIan Lepore 	return (bus_generic_attach(sc->dev));
564*a0fd2339SIan Lepore }
565*a0fd2339SIan Lepore 
566*a0fd2339SIan Lepore static int
567*a0fd2339SIan Lepore spi_probe(device_t dev)
568*a0fd2339SIan Lepore {
569*a0fd2339SIan Lepore 
570*a0fd2339SIan Lepore 	if (!ofw_bus_status_okay(dev))
571*a0fd2339SIan Lepore 		return (ENXIO);
572*a0fd2339SIan Lepore 
573*a0fd2339SIan Lepore 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
574*a0fd2339SIan Lepore 		return (ENXIO);
575*a0fd2339SIan Lepore 
576*a0fd2339SIan Lepore 	device_set_desc(dev, "i.MX ECSPI Master");
577*a0fd2339SIan Lepore 	return (BUS_PROBE_DEFAULT);
578*a0fd2339SIan Lepore }
579*a0fd2339SIan Lepore 
580*a0fd2339SIan Lepore static device_method_t spi_methods[] = {
581*a0fd2339SIan Lepore 	DEVMETHOD(device_probe,		spi_probe),
582*a0fd2339SIan Lepore 	DEVMETHOD(device_attach,	spi_attach),
583*a0fd2339SIan Lepore 	DEVMETHOD(device_detach,	spi_detach),
584*a0fd2339SIan Lepore 
585*a0fd2339SIan Lepore         /* spibus_if  */
586*a0fd2339SIan Lepore 	DEVMETHOD(spibus_transfer,	spi_transfer),
587*a0fd2339SIan Lepore 
588*a0fd2339SIan Lepore         /* ofw_bus_if */
589*a0fd2339SIan Lepore 	DEVMETHOD(ofw_bus_get_node,	spi_get_node),
590*a0fd2339SIan Lepore 
591*a0fd2339SIan Lepore 	DEVMETHOD_END
592*a0fd2339SIan Lepore };
593*a0fd2339SIan Lepore 
594*a0fd2339SIan Lepore static driver_t spi_driver = {
595*a0fd2339SIan Lepore 	"imx_spi",
596*a0fd2339SIan Lepore 	spi_methods,
597*a0fd2339SIan Lepore 	sizeof(struct spi_softc),
598*a0fd2339SIan Lepore };
599*a0fd2339SIan Lepore 
600*a0fd2339SIan Lepore static devclass_t spi_devclass;
601*a0fd2339SIan Lepore 
602*a0fd2339SIan Lepore DRIVER_MODULE(imx_spi, simplebus, spi_driver, spi_devclass, 0, 0);
603*a0fd2339SIan Lepore DRIVER_MODULE(ofw_spibus, imx_spi, ofw_spibus_driver, ofw_spibus_devclass, 0, 0);
604*a0fd2339SIan Lepore MODULE_DEPEND(imx_spi, ofw_spibus, 1, 1, 1);
605