xref: /freebsd/sys/arm/ti/ti_spi.c (revision b88cc53d4ddda4486683ee2121f131b10ed21c30)
1 /*-
2  * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/rman.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <machine/intr.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <dev/spibus/spi.h>
49 #include <dev/spibus/spibusvar.h>
50 
51 #include <arm/ti/ti_prcm.h>
52 #include <arm/ti/ti_hwmods.h>
53 #include <arm/ti/ti_spireg.h>
54 #include <arm/ti/ti_spivar.h>
55 
56 #include "spibus_if.h"
57 
58 static void ti_spi_intr(void *);
59 static int ti_spi_detach(device_t);
60 
61 #undef TI_SPI_DEBUG
62 #ifdef TI_SPI_DEBUG
63 #define	IRQSTATUSBITS							\
64 	"\020\1TX0_EMPTY\2TX0_UNDERFLOW\3RX0_FULL\4RX0_OVERFLOW"	\
65 	"\5TX1_EMPTY\6TX1_UNDERFLOW\7RX1_FULL\11TX2_EMPTY"		\
66 	"\12TX1_UNDERFLOW\13RX2_FULL\15TX3_EMPTY\16TX3_UNDERFLOW"	\
67 	"\17RX3_FULL\22EOW"
68 #define	CONFBITS							\
69 	"\020\1PHA\2POL\7EPOL\17DMAW\20DMAR\21DPE0\22DPE1\23IS"		\
70 	"\24TURBO\25FORCE\30SBE\31SBPOL\34FFEW\35FFER\36CLKG"
71 #define	STATBITS							\
72 	"\020\1RXS\2TXS\3EOT\4TXFFE\5TXFFF\6RXFFE\7RXFFFF"
73 #define	MODULCTRLBITS							\
74 	"\020\1SINGLE\2NOSPIEN\3SLAVE\4SYST\10MOA\11FDAA"
75 #define	CTRLBITS							\
76 	"\020\1ENABLED"
77 
78 static void
79 ti_spi_printr(device_t dev)
80 {
81 	int clk, conf, ctrl, div, i, j, wl;
82 	struct ti_spi_softc *sc;
83 	uint32_t reg;
84 
85 	sc = device_get_softc(dev);
86 	reg = TI_SPI_READ(sc, MCSPI_SYSCONFIG);
87 	device_printf(dev, "SYSCONFIG: %#x\n", reg);
88 	reg = TI_SPI_READ(sc, MCSPI_SYSSTATUS);
89 	device_printf(dev, "SYSSTATUS: %#x\n", reg);
90 	reg = TI_SPI_READ(sc, MCSPI_IRQSTATUS);
91 	device_printf(dev, "IRQSTATUS: 0x%b\n", reg, IRQSTATUSBITS);
92 	reg = TI_SPI_READ(sc, MCSPI_IRQENABLE);
93 	device_printf(dev, "IRQENABLE: 0x%b\n", reg, IRQSTATUSBITS);
94 	reg = TI_SPI_READ(sc, MCSPI_MODULCTRL);
95 	device_printf(dev, "MODULCTRL: 0x%b\n", reg, MODULCTRLBITS);
96 	for (i = 0; i < sc->sc_numcs; i++) {
97 		ctrl = TI_SPI_READ(sc, MCSPI_CTRL_CH(i));
98 		conf = TI_SPI_READ(sc, MCSPI_CONF_CH(i));
99 		device_printf(dev, "CH%dCONF: 0x%b\n", i, conf, CONFBITS);
100 		if (conf & MCSPI_CONF_CLKG) {
101 			div = (conf >> MCSPI_CONF_CLK_SHIFT) & MCSPI_CONF_CLK_MSK;
102 			div |= ((ctrl >> MCSPI_CTRL_EXTCLK_SHIFT) & MCSPI_CTRL_EXTCLK_MSK) << 4;
103 		} else {
104 			div = 1;
105 			j = (conf >> MCSPI_CONF_CLK_SHIFT) & MCSPI_CONF_CLK_MSK;
106 			while (j-- > 0)
107 				div <<= 1;
108 		}
109 		clk = TI_SPI_GCLK / div;
110 		wl = ((conf >> MCSPI_CONF_WL_SHIFT) & MCSPI_CONF_WL_MSK) + 1;
111 		device_printf(dev, "wordlen: %-2d clock: %d\n", wl, clk);
112 		reg = TI_SPI_READ(sc, MCSPI_STAT_CH(i));
113 		device_printf(dev, "CH%dSTAT: 0x%b\n", i, reg, STATBITS);
114 		device_printf(dev, "CH%dCTRL: 0x%b\n", i, ctrl, CTRLBITS);
115 	}
116 	reg = TI_SPI_READ(sc, MCSPI_XFERLEVEL);
117 	device_printf(dev, "XFERLEVEL: %#x\n", reg);
118 }
119 #endif
120 
121 static void
122 ti_spi_set_clock(struct ti_spi_softc *sc, int ch, int freq)
123 {
124 	uint32_t clkdiv, conf, div, extclk, reg;
125 
126 	clkdiv = TI_SPI_GCLK / freq;
127 	if (clkdiv > MCSPI_EXTCLK_MSK) {
128 		extclk = 0;
129 		clkdiv = 0;
130 		div = 1;
131 		while (TI_SPI_GCLK / div > freq && clkdiv <= 0xf) {
132 			clkdiv++;
133 			div <<= 1;
134 		}
135 		conf = clkdiv << MCSPI_CONF_CLK_SHIFT;
136 	} else {
137 		extclk = clkdiv >> 4;
138 		clkdiv &= MCSPI_CONF_CLK_MSK;
139 		conf = MCSPI_CONF_CLKG | clkdiv << MCSPI_CONF_CLK_SHIFT;
140 	}
141 
142 	reg = TI_SPI_READ(sc, MCSPI_CTRL_CH(ch));
143 	reg &= ~(MCSPI_CTRL_EXTCLK_MSK << MCSPI_CTRL_EXTCLK_SHIFT);
144 	reg |= extclk << MCSPI_CTRL_EXTCLK_SHIFT;
145 	TI_SPI_WRITE(sc, MCSPI_CTRL_CH(ch), reg);
146 
147 	reg = TI_SPI_READ(sc, MCSPI_CONF_CH(ch));
148 	reg &= ~(MCSPI_CONF_CLKG | MCSPI_CONF_CLK_MSK << MCSPI_CONF_CLK_SHIFT);
149 	TI_SPI_WRITE(sc, MCSPI_CONF_CH(ch), reg | conf);
150 }
151 
152 static int
153 ti_spi_probe(device_t dev)
154 {
155 
156 	if (!ofw_bus_status_okay(dev))
157 		return (ENXIO);
158 	if (!ofw_bus_is_compatible(dev, "ti,omap4-mcspi"))
159 		return (ENXIO);
160 
161 	device_set_desc(dev, "TI McSPI controller");
162 
163 	return (BUS_PROBE_DEFAULT);
164 }
165 
166 static int
167 ti_spi_attach(device_t dev)
168 {
169 	int clk_id, err, i, rid, timeout;
170 	struct ti_spi_softc *sc;
171 	uint32_t rev;
172 
173 	sc = device_get_softc(dev);
174 	sc->sc_dev = dev;
175 
176 	/*
177 	 * Get the MMCHS device id from FDT.  If it's not there use the newbus
178 	 * unit number (which will work as long as the devices are in order and
179 	 * none are skipped in the fdt).  Note that this is a property we made
180 	 * up and added in freebsd, it doesn't exist in the published bindings.
181 	 */
182 	clk_id = ti_hwmods_get_clock(dev);
183 	if (clk_id == INVALID_CLK_IDENT) {
184 		device_printf(dev,
185 		    "failed to get clock based on hwmods property\n");
186 		return (EINVAL);
187 	}
188 
189 	/* Activate the McSPI module. */
190 	err = ti_prcm_clk_enable(clk_id);
191 	if (err) {
192 		device_printf(dev, "Error: failed to activate source clock\n");
193 		return (err);
194 	}
195 
196 	/* Get the number of available channels. */
197 	if ((OF_getencprop(ofw_bus_get_node(dev), "ti,spi-num-cs",
198 	    &sc->sc_numcs, sizeof(sc->sc_numcs))) <= 0) {
199 		sc->sc_numcs = 2;
200 	}
201 
202 	rid = 0;
203 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
204 	    RF_ACTIVE);
205 	if (!sc->sc_mem_res) {
206 		device_printf(dev, "cannot allocate memory window\n");
207 		return (ENXIO);
208 	}
209 
210 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
211 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
212 
213 	rid = 0;
214 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
215 	    RF_ACTIVE);
216 	if (!sc->sc_irq_res) {
217 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
218 		device_printf(dev, "cannot allocate interrupt\n");
219 		return (ENXIO);
220 	}
221 
222 	/* Hook up our interrupt handler. */
223 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
224 	    NULL, ti_spi_intr, sc, &sc->sc_intrhand)) {
225 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
226 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
227 		device_printf(dev, "cannot setup the interrupt handler\n");
228 		return (ENXIO);
229 	}
230 
231 	mtx_init(&sc->sc_mtx, "ti_spi", NULL, MTX_DEF);
232 
233 	/* Issue a softreset to the controller */
234 	TI_SPI_WRITE(sc, MCSPI_SYSCONFIG, MCSPI_SYSCONFIG_SOFTRESET);
235 	timeout = 1000;
236 	while (!(TI_SPI_READ(sc, MCSPI_SYSSTATUS) &
237 	    MCSPI_SYSSTATUS_RESETDONE)) {
238 		if (--timeout == 0) {
239 			device_printf(dev,
240 			    "Error: Controller reset operation timed out\n");
241 			ti_spi_detach(dev);
242 			return (ENXIO);
243 		}
244 		DELAY(100);
245 	}
246 
247 	/* Print the McSPI module revision. */
248 	rev = TI_SPI_READ(sc, MCSPI_REVISION);
249 	device_printf(dev,
250 	    "scheme: %#x func: %#x rtl: %d rev: %d.%d custom rev: %d\n",
251 	    (rev >> MCSPI_REVISION_SCHEME_SHIFT) & MCSPI_REVISION_SCHEME_MSK,
252 	    (rev >> MCSPI_REVISION_FUNC_SHIFT) & MCSPI_REVISION_FUNC_MSK,
253 	    (rev >> MCSPI_REVISION_RTL_SHIFT) & MCSPI_REVISION_RTL_MSK,
254 	    (rev >> MCSPI_REVISION_MAJOR_SHIFT) & MCSPI_REVISION_MAJOR_MSK,
255 	    (rev >> MCSPI_REVISION_MINOR_SHIFT) & MCSPI_REVISION_MINOR_MSK,
256 	    (rev >> MCSPI_REVISION_CUSTOM_SHIFT) & MCSPI_REVISION_CUSTOM_MSK);
257 
258 	/* Set Master mode, single channel. */
259 	TI_SPI_WRITE(sc, MCSPI_MODULCTRL, MCSPI_MODULCTRL_SINGLE);
260 
261 	/* Clear pending interrupts and disable interrupts. */
262 	TI_SPI_WRITE(sc, MCSPI_IRQENABLE, 0x0);
263 	TI_SPI_WRITE(sc, MCSPI_IRQSTATUS, 0xffff);
264 
265 	for (i = 0; i < sc->sc_numcs; i++) {
266 		/*
267 		 * Default to SPI mode 0, CS active low, 8 bits word length and
268 		 * 500kHz clock.
269 		 */
270 		TI_SPI_WRITE(sc, MCSPI_CONF_CH(i),
271 		    MCSPI_CONF_DPE0 | MCSPI_CONF_EPOL |
272 		    (8 - 1) << MCSPI_CONF_WL_SHIFT);
273 		/* Set initial clock - 500kHz. */
274 		ti_spi_set_clock(sc, i, 500000);
275 	}
276 
277 #ifdef	TI_SPI_DEBUG
278 	ti_spi_printr(dev);
279 #endif
280 
281 	device_add_child(dev, "spibus", -1);
282 
283 	return (bus_generic_attach(dev));
284 }
285 
286 static int
287 ti_spi_detach(device_t dev)
288 {
289 	struct ti_spi_softc *sc;
290 
291 	sc = device_get_softc(dev);
292 
293 	/* Clear pending interrupts and disable interrupts. */
294 	TI_SPI_WRITE(sc, MCSPI_IRQENABLE, 0);
295 	TI_SPI_WRITE(sc, MCSPI_IRQSTATUS, 0xffff);
296 
297 	/* Reset controller. */
298 	TI_SPI_WRITE(sc, MCSPI_SYSCONFIG, MCSPI_SYSCONFIG_SOFTRESET);
299 
300 	bus_generic_detach(dev);
301 
302 	mtx_destroy(&sc->sc_mtx);
303 	if (sc->sc_intrhand)
304 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
305 	if (sc->sc_irq_res)
306 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
307 	if (sc->sc_mem_res)
308 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
309 
310 	return (0);
311 }
312 
313 static int
314 ti_spi_fill_fifo(struct ti_spi_softc *sc)
315 {
316 	int bytes, timeout;
317 	struct spi_command *cmd;
318 	uint32_t written;
319 	uint8_t *data;
320 
321 	cmd = sc->sc_cmd;
322 	bytes = min(sc->sc_len - sc->sc_written, sc->sc_fifolvl);
323 	while (bytes-- > 0) {
324 		data = (uint8_t *)cmd->tx_cmd;
325 		written = sc->sc_written++;
326 		if (written >= cmd->tx_cmd_sz) {
327 			data = (uint8_t *)cmd->tx_data;
328 			written -= cmd->tx_cmd_sz;
329 		}
330 		if (sc->sc_fifolvl == 1) {
331 			/* FIFO disabled. */
332 			timeout = 1000;
333 			while (--timeout > 0 && (TI_SPI_READ(sc,
334 			    MCSPI_STAT_CH(sc->sc_cs)) & MCSPI_STAT_TXS) == 0) {
335 				DELAY(100);
336 			}
337 			if (timeout == 0)
338 				return (-1);
339 		}
340 		TI_SPI_WRITE(sc, MCSPI_TX_CH(sc->sc_cs), data[written]);
341 	}
342 
343 	return (0);
344 }
345 
346 static int
347 ti_spi_drain_fifo(struct ti_spi_softc *sc)
348 {
349 	int bytes, timeout;
350 	struct spi_command *cmd;
351 	uint32_t read;
352 	uint8_t *data;
353 
354 	cmd = sc->sc_cmd;
355 	bytes = min(sc->sc_len - sc->sc_read, sc->sc_fifolvl);
356 	while (bytes-- > 0) {
357 		data = (uint8_t *)cmd->rx_cmd;
358 		read = sc->sc_read++;
359 		if (read >= cmd->rx_cmd_sz) {
360 			data = (uint8_t *)cmd->rx_data;
361 			read -= cmd->rx_cmd_sz;
362 		}
363 		if (sc->sc_fifolvl == 1) {
364 			/* FIFO disabled. */
365 			timeout = 1000;
366 			while (--timeout > 0 && (TI_SPI_READ(sc,
367 			    MCSPI_STAT_CH(sc->sc_cs)) & MCSPI_STAT_RXS) == 0) {
368 				DELAY(100);
369 			}
370 			if (timeout == 0)
371 				return (-1);
372 		}
373 		data[read] = TI_SPI_READ(sc, MCSPI_RX_CH(sc->sc_cs));
374 	}
375 
376 	return (0);
377 }
378 
379 static void
380 ti_spi_intr(void *arg)
381 {
382 	int eow;
383 	struct ti_spi_softc *sc;
384 	uint32_t status;
385 
386 	eow = 0;
387 	sc = (struct ti_spi_softc *)arg;
388 	TI_SPI_LOCK(sc);
389 	status = TI_SPI_READ(sc, MCSPI_IRQSTATUS);
390 
391 	/*
392 	 * No new TX_empty or RX_full event will be asserted while the CPU has
393 	 * not performed the number of writes or reads defined by
394 	 * MCSPI_XFERLEVEL[AEL] and MCSPI_XFERLEVEL[AFL].  It is responsibility
395 	 * of CPU perform the right number of writes and reads.
396 	 */
397 	if (status & MCSPI_IRQ_TX0_EMPTY)
398 		ti_spi_fill_fifo(sc);
399 	if (status & MCSPI_IRQ_RX0_FULL)
400 		ti_spi_drain_fifo(sc);
401 
402 	if (status & MCSPI_IRQ_EOW)
403 		eow = 1;
404 
405 	/* Clear interrupt status. */
406 	TI_SPI_WRITE(sc, MCSPI_IRQSTATUS, status);
407 
408 	/* Check for end of transfer. */
409 	if (sc->sc_written == sc->sc_len && sc->sc_read == sc->sc_len) {
410 		sc->sc_flags |= TI_SPI_DONE;
411 		wakeup(sc->sc_dev);
412 	}
413 
414 	TI_SPI_UNLOCK(sc);
415 }
416 
417 static int
418 ti_spi_pio_transfer(struct ti_spi_softc *sc)
419 {
420 
421 	while (sc->sc_len - sc->sc_written > 0) {
422 		if (ti_spi_fill_fifo(sc) == -1)
423 			return (EIO);
424 		if (ti_spi_drain_fifo(sc) == -1)
425 			return (EIO);
426 	}
427 
428 	return (0);
429 }
430 
431 static int
432 ti_spi_gcd(int a, int b)
433 {
434 	int m;
435 
436 	while ((m = a % b) != 0) {
437 		a = b;
438 		b = m;
439 	}
440 
441 	return (b);
442 }
443 
444 static int
445 ti_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
446 {
447 	int err;
448 	struct ti_spi_softc *sc;
449 	uint32_t clockhz, cs, mode, reg;
450 
451 	sc = device_get_softc(dev);
452 
453 	KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
454 	    ("TX/RX command sizes should be equal"));
455 	KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
456 	    ("TX/RX data sizes should be equal"));
457 
458 	/* Get the proper chip select for this child. */
459 	spibus_get_cs(child, &cs);
460 	spibus_get_clock(child, &clockhz);
461 	spibus_get_mode(child, &mode);
462 
463 	cs &= ~SPIBUS_CS_HIGH;
464 
465 	if (cs > sc->sc_numcs) {
466 		device_printf(dev, "Invalid chip select %d requested by %s\n",
467 		    cs, device_get_nameunit(child));
468 		return (EINVAL);
469 	}
470 
471 	if (mode > 3)
472 	{
473 	    device_printf(dev, "Invalid mode %d requested by %s\n", mode,
474 		    device_get_nameunit(child));
475 	    return (EINVAL);
476 	}
477 
478 	TI_SPI_LOCK(sc);
479 
480 	/* If the controller is in use wait until it is available. */
481 	while (sc->sc_flags & TI_SPI_BUSY)
482 		mtx_sleep(dev, &sc->sc_mtx, 0, "ti_spi", 0);
483 
484 	/* Now we have control over SPI controller. */
485 	sc->sc_flags = TI_SPI_BUSY;
486 
487 	/* Save the SPI command data. */
488 	sc->sc_cs = cs;
489 	sc->sc_cmd = cmd;
490 	sc->sc_read = 0;
491 	sc->sc_written = 0;
492 	sc->sc_len = cmd->tx_cmd_sz + cmd->tx_data_sz;
493 	sc->sc_fifolvl = ti_spi_gcd(sc->sc_len, TI_SPI_FIFOSZ);
494 	if (sc->sc_fifolvl < 2 || sc->sc_len > 0xffff)
495 		sc->sc_fifolvl = 1;	/* FIFO disabled. */
496 	/* Disable FIFO for now. */
497 	sc->sc_fifolvl = 1;
498 
499 	/* Set the bus frequency. */
500 	ti_spi_set_clock(sc, sc->sc_cs, clockhz);
501 
502 	/* Disable the FIFO. */
503 	TI_SPI_WRITE(sc, MCSPI_XFERLEVEL, 0);
504 
505 	/* 8 bits word, d0 miso, d1 mosi, mode 0 and CS active low. */
506 	reg = TI_SPI_READ(sc, MCSPI_CONF_CH(sc->sc_cs));
507 	reg &= ~(MCSPI_CONF_FFER | MCSPI_CONF_FFEW | MCSPI_CONF_SBPOL |
508 	    MCSPI_CONF_SBE | MCSPI_CONF_TURBO | MCSPI_CONF_IS |
509 	    MCSPI_CONF_DPE1 | MCSPI_CONF_DPE0 | MCSPI_CONF_DMAR |
510 	    MCSPI_CONF_DMAW | MCSPI_CONF_EPOL);
511 	reg |= MCSPI_CONF_DPE0 | MCSPI_CONF_EPOL | MCSPI_CONF_WL8BITS;
512 	reg |= mode; /* POL and PHA are the low bits, we can just OR-in mode */
513 	TI_SPI_WRITE(sc, MCSPI_CONF_CH(sc->sc_cs), reg);
514 
515 #if 0
516 	/* Enable channel interrupts. */
517 	reg = TI_SPI_READ(sc, MCSPI_IRQENABLE);
518 	reg |= 0xf;
519 	TI_SPI_WRITE(sc, MCSPI_IRQENABLE, reg);
520 #endif
521 
522 	/* Start the transfer. */
523 	reg = TI_SPI_READ(sc, MCSPI_CTRL_CH(sc->sc_cs));
524 	TI_SPI_WRITE(sc, MCSPI_CTRL_CH(sc->sc_cs), reg | MCSPI_CTRL_ENABLE);
525 
526 	/* Force CS on. */
527 	reg = TI_SPI_READ(sc, MCSPI_CONF_CH(sc->sc_cs));
528 	TI_SPI_WRITE(sc, MCSPI_CONF_CH(sc->sc_cs), reg |= MCSPI_CONF_FORCE);
529 
530 	err = 0;
531 	if (sc->sc_fifolvl == 1)
532 		err = ti_spi_pio_transfer(sc);
533 
534 	/* Force CS off. */
535 	reg = TI_SPI_READ(sc, MCSPI_CONF_CH(sc->sc_cs));
536 	reg &= ~MCSPI_CONF_FORCE;
537 	TI_SPI_WRITE(sc, MCSPI_CONF_CH(sc->sc_cs), reg);
538 
539 	/* Disable IRQs. */
540 	reg = TI_SPI_READ(sc, MCSPI_IRQENABLE);
541 	reg &= ~0xf;
542 	TI_SPI_WRITE(sc, MCSPI_IRQENABLE, reg);
543 	TI_SPI_WRITE(sc, MCSPI_IRQSTATUS, 0xf);
544 
545 	/* Disable the SPI channel. */
546 	reg = TI_SPI_READ(sc, MCSPI_CTRL_CH(sc->sc_cs));
547 	reg &= ~MCSPI_CTRL_ENABLE;
548 	TI_SPI_WRITE(sc, MCSPI_CTRL_CH(sc->sc_cs), reg);
549 
550 	/* Disable FIFO. */
551 	reg = TI_SPI_READ(sc, MCSPI_CONF_CH(sc->sc_cs));
552 	reg &= ~(MCSPI_CONF_FFER | MCSPI_CONF_FFEW);
553 	TI_SPI_WRITE(sc, MCSPI_CONF_CH(sc->sc_cs), reg);
554 
555 	/* Release the controller and wakeup the next thread waiting for it. */
556 	sc->sc_flags = 0;
557 	wakeup_one(dev);
558 	TI_SPI_UNLOCK(sc);
559 
560 	return (err);
561 }
562 
563 static phandle_t
564 ti_spi_get_node(device_t bus, device_t dev)
565 {
566 
567 	/* Share controller node with spibus. */
568 	return (ofw_bus_get_node(bus));
569 }
570 
571 static device_method_t ti_spi_methods[] = {
572 	/* Device interface */
573 	DEVMETHOD(device_probe,		ti_spi_probe),
574 	DEVMETHOD(device_attach,	ti_spi_attach),
575 	DEVMETHOD(device_detach,	ti_spi_detach),
576 
577 	/* SPI interface */
578 	DEVMETHOD(spibus_transfer,	ti_spi_transfer),
579 
580 	/* ofw_bus interface */
581 	DEVMETHOD(ofw_bus_get_node,	ti_spi_get_node),
582 
583 	DEVMETHOD_END
584 };
585 
586 static devclass_t ti_spi_devclass;
587 
588 static driver_t ti_spi_driver = {
589 	"spi",
590 	ti_spi_methods,
591 	sizeof(struct ti_spi_softc),
592 };
593 
594 DRIVER_MODULE(ti_spi, simplebus, ti_spi_driver, ti_spi_devclass, 0, 0);
595