xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_spi.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * Copyright (c) 2013 Luiz Otavio O Souza <loos@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/intr.h>
45 
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <dev/spibus/spi.h>
50 #include <dev/spibus/spibusvar.h>
51 
52 #include <arm/broadcom/bcm2835/bcm2835_spireg.h>
53 #include <arm/broadcom/bcm2835/bcm2835_spivar.h>
54 
55 #include "spibus_if.h"
56 
57 static struct ofw_compat_data compat_data[] = {
58 	{"broadcom,bcm2835-spi",	1},
59 	{"brcm,bcm2835-spi",		1},
60 	{NULL,				0}
61 };
62 
63 static void bcm_spi_intr(void *);
64 
65 #ifdef	BCM_SPI_DEBUG
66 static void
67 bcm_spi_printr(device_t dev)
68 {
69 	struct bcm_spi_softc *sc;
70 	uint32_t reg;
71 
72 	sc = device_get_softc(dev);
73 	reg = BCM_SPI_READ(sc, SPI_CS);
74 	device_printf(dev, "CS=%b\n", reg,
75 	    "\20\1CS0\2CS1\3CPHA\4CPOL\7CSPOL"
76 	    "\10TA\11DMAEN\12INTD\13INTR\14ADCS\15REN\16LEN"
77 	    "\21DONE\22RXD\23TXD\24RXR\25RXF\26CSPOL0\27CSPOL1"
78 	    "\30CSPOL2\31DMA_LEN\32LEN_LONG");
79 	reg = BCM_SPI_READ(sc, SPI_CLK) & SPI_CLK_MASK;
80 	if (reg % 2)
81 		reg--;
82 	if (reg == 0)
83 		reg = 65536;
84 	device_printf(dev, "CLK=%uMhz/%d=%luhz\n",
85 	    SPI_CORE_CLK / 1000000, reg, SPI_CORE_CLK / reg);
86 	reg = BCM_SPI_READ(sc, SPI_DLEN) & SPI_DLEN_MASK;
87 	device_printf(dev, "DLEN=%d\n", reg);
88 	reg = BCM_SPI_READ(sc, SPI_LTOH) & SPI_LTOH_MASK;
89 	device_printf(dev, "LTOH=%d\n", reg);
90 	reg = BCM_SPI_READ(sc, SPI_DC);
91 	device_printf(dev, "DC=RPANIC=%#x RDREQ=%#x TPANIC=%#x TDREQ=%#x\n",
92 	    (reg & SPI_DC_RPANIC_MASK) >> SPI_DC_RPANIC_SHIFT,
93 	    (reg & SPI_DC_RDREQ_MASK) >> SPI_DC_RDREQ_SHIFT,
94 	    (reg & SPI_DC_TPANIC_MASK) >> SPI_DC_TPANIC_SHIFT,
95 	    (reg & SPI_DC_TDREQ_MASK) >> SPI_DC_TDREQ_SHIFT);
96 }
97 #endif
98 
99 static void
100 bcm_spi_modifyreg(struct bcm_spi_softc *sc, uint32_t off, uint32_t mask,
101 	uint32_t value)
102 {
103 	uint32_t reg;
104 
105 	mtx_assert(&sc->sc_mtx, MA_OWNED);
106 	reg = BCM_SPI_READ(sc, off);
107 	reg &= ~mask;
108 	reg |= value;
109 	BCM_SPI_WRITE(sc, off, reg);
110 }
111 
112 static int
113 bcm_spi_clock_proc(SYSCTL_HANDLER_ARGS)
114 {
115 	struct bcm_spi_softc *sc;
116 	uint32_t clk;
117 	int error;
118 
119 	sc = (struct bcm_spi_softc *)arg1;
120 
121 	BCM_SPI_LOCK(sc);
122 	clk = BCM_SPI_READ(sc, SPI_CLK);
123 	BCM_SPI_UNLOCK(sc);
124 	clk &= 0xffff;
125 	if (clk == 0)
126 		clk = 65536;
127 	clk = SPI_CORE_CLK / clk;
128 
129 	error = sysctl_handle_int(oidp, &clk, sizeof(clk), req);
130 	if (error != 0 || req->newptr == NULL)
131 		return (error);
132 
133 	return (0);
134 }
135 
136 static int
137 bcm_spi_cs_bit_proc(SYSCTL_HANDLER_ARGS, uint32_t bit)
138 {
139 	struct bcm_spi_softc *sc;
140 	uint32_t reg;
141 	int error;
142 
143 	sc = (struct bcm_spi_softc *)arg1;
144 	BCM_SPI_LOCK(sc);
145 	reg = BCM_SPI_READ(sc, SPI_CS);
146 	BCM_SPI_UNLOCK(sc);
147 	reg = (reg & bit) ? 1 : 0;
148 
149 	error = sysctl_handle_int(oidp, &reg, sizeof(reg), req);
150 	if (error != 0 || req->newptr == NULL)
151 		return (error);
152 
153 	return (0);
154 }
155 
156 static int
157 bcm_spi_cpol_proc(SYSCTL_HANDLER_ARGS)
158 {
159 
160 	return (bcm_spi_cs_bit_proc(oidp, arg1, arg2, req, SPI_CS_CPOL));
161 }
162 
163 static int
164 bcm_spi_cpha_proc(SYSCTL_HANDLER_ARGS)
165 {
166 
167 	return (bcm_spi_cs_bit_proc(oidp, arg1, arg2, req, SPI_CS_CPHA));
168 }
169 
170 static int
171 bcm_spi_cspol0_proc(SYSCTL_HANDLER_ARGS)
172 {
173 
174 	return (bcm_spi_cs_bit_proc(oidp, arg1, arg2, req, SPI_CS_CSPOL0));
175 }
176 
177 static int
178 bcm_spi_cspol1_proc(SYSCTL_HANDLER_ARGS)
179 {
180 
181 	return (bcm_spi_cs_bit_proc(oidp, arg1, arg2, req, SPI_CS_CSPOL1));
182 }
183 
184 static int
185 bcm_spi_cspol2_proc(SYSCTL_HANDLER_ARGS)
186 {
187 
188 	return (bcm_spi_cs_bit_proc(oidp, arg1, arg2, req, SPI_CS_CSPOL2));
189 }
190 
191 static void
192 bcm_spi_sysctl_init(struct bcm_spi_softc *sc)
193 {
194 	struct sysctl_ctx_list *ctx;
195 	struct sysctl_oid *tree_node;
196 	struct sysctl_oid_list *tree;
197 
198 	/*
199 	 * Add system sysctl tree/handlers.
200 	 */
201 	ctx = device_get_sysctl_ctx(sc->sc_dev);
202 	tree_node = device_get_sysctl_tree(sc->sc_dev);
203 	tree = SYSCTL_CHILDREN(tree_node);
204 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "clock",
205 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
206 	    bcm_spi_clock_proc, "IU", "SPI BUS clock frequency");
207 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "cpol",
208 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
209 	    bcm_spi_cpol_proc, "IU", "SPI BUS clock polarity");
210 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "cpha",
211 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
212 	    bcm_spi_cpha_proc, "IU", "SPI BUS clock phase");
213 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "cspol0",
214 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
215 	    bcm_spi_cspol0_proc, "IU", "SPI BUS chip select 0 polarity");
216 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "cspol1",
217 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
218 	    bcm_spi_cspol1_proc, "IU", "SPI BUS chip select 1 polarity");
219 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "cspol2",
220 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
221 	    bcm_spi_cspol2_proc, "IU", "SPI BUS chip select 2 polarity");
222 }
223 
224 static int
225 bcm_spi_probe(device_t dev)
226 {
227 
228 	if (!ofw_bus_status_okay(dev))
229 		return (ENXIO);
230 
231 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
232 		return (ENXIO);
233 
234 	device_set_desc(dev, "BCM2708/2835 SPI controller");
235 
236 	return (BUS_PROBE_DEFAULT);
237 }
238 
239 static int
240 bcm_spi_attach(device_t dev)
241 {
242 	struct bcm_spi_softc *sc;
243 	int rid;
244 
245 	if (device_get_unit(dev) != 0) {
246 		device_printf(dev, "only one SPI controller supported\n");
247 		return (ENXIO);
248 	}
249 
250 	sc = device_get_softc(dev);
251 	sc->sc_dev = dev;
252 
253 	rid = 0;
254 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
255 	    RF_ACTIVE);
256 	if (!sc->sc_mem_res) {
257 		device_printf(dev, "cannot allocate memory window\n");
258 		return (ENXIO);
259 	}
260 
261 	sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
262 	sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
263 
264 	rid = 0;
265 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
266 	    RF_ACTIVE);
267 	if (!sc->sc_irq_res) {
268 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
269 		device_printf(dev, "cannot allocate interrupt\n");
270 		return (ENXIO);
271 	}
272 
273 	/* Hook up our interrupt handler. */
274 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
275 	    NULL, bcm_spi_intr, sc, &sc->sc_intrhand)) {
276 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
277 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
278 		device_printf(dev, "cannot setup the interrupt handler\n");
279 		return (ENXIO);
280 	}
281 
282 	mtx_init(&sc->sc_mtx, "bcm_spi", NULL, MTX_DEF);
283 
284 	/* Add sysctl nodes. */
285 	bcm_spi_sysctl_init(sc);
286 
287 #ifdef	BCM_SPI_DEBUG
288 	bcm_spi_printr(dev);
289 #endif
290 
291 	/*
292 	 * Enable the SPI controller.  Clear the rx and tx FIFO.
293 	 * Defaults to SPI mode 0.
294 	 */
295 	BCM_SPI_WRITE(sc, SPI_CS, SPI_CS_CLEAR_RXFIFO | SPI_CS_CLEAR_TXFIFO);
296 
297 #ifdef	BCM_SPI_DEBUG
298 	bcm_spi_printr(dev);
299 #endif
300 
301 	device_add_child(dev, "spibus", -1);
302 
303 	return (bus_generic_attach(dev));
304 }
305 
306 static int
307 bcm_spi_detach(device_t dev)
308 {
309 	struct bcm_spi_softc *sc;
310 
311 	bus_generic_detach(dev);
312 
313 	sc = device_get_softc(dev);
314 	mtx_destroy(&sc->sc_mtx);
315 	if (sc->sc_intrhand)
316 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intrhand);
317 	if (sc->sc_irq_res)
318 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
319 	if (sc->sc_mem_res)
320 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
321 
322 	return (0);
323 }
324 
325 static void
326 bcm_spi_fill_fifo(struct bcm_spi_softc *sc)
327 {
328 	struct spi_command *cmd;
329 	uint32_t cs, written;
330 	uint8_t *data;
331 
332 	cmd = sc->sc_cmd;
333 	cs = BCM_SPI_READ(sc, SPI_CS) & (SPI_CS_TA | SPI_CS_TXD);
334 	while (sc->sc_written < sc->sc_len &&
335 	    cs == (SPI_CS_TA | SPI_CS_TXD)) {
336 		data = (uint8_t *)cmd->tx_cmd;
337 		written = sc->sc_written++;
338 		if (written >= cmd->tx_cmd_sz) {
339 			data = (uint8_t *)cmd->tx_data;
340 			written -= cmd->tx_cmd_sz;
341 		}
342 		BCM_SPI_WRITE(sc, SPI_FIFO, data[written]);
343 		cs = BCM_SPI_READ(sc, SPI_CS) & (SPI_CS_TA | SPI_CS_TXD);
344 	}
345 }
346 
347 static void
348 bcm_spi_drain_fifo(struct bcm_spi_softc *sc)
349 {
350 	struct spi_command *cmd;
351 	uint32_t cs, read;
352 	uint8_t *data;
353 
354 	cmd = sc->sc_cmd;
355 	cs = BCM_SPI_READ(sc, SPI_CS) & SPI_CS_RXD;
356 	while (sc->sc_read < sc->sc_len && cs == SPI_CS_RXD) {
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 		data[read] = BCM_SPI_READ(sc, SPI_FIFO) & 0xff;
364 		cs = BCM_SPI_READ(sc, SPI_CS) & SPI_CS_RXD;
365 	}
366 }
367 
368 static void
369 bcm_spi_intr(void *arg)
370 {
371 	struct bcm_spi_softc *sc;
372 
373 	sc = (struct bcm_spi_softc *)arg;
374 	BCM_SPI_LOCK(sc);
375 
376 	/* Filter stray interrupts. */
377 	if ((sc->sc_flags & BCM_SPI_BUSY) == 0) {
378 		BCM_SPI_UNLOCK(sc);
379 		return;
380 	}
381 
382 	/* TX - Fill up the FIFO. */
383 	bcm_spi_fill_fifo(sc);
384 
385 	/* RX - Drain the FIFO. */
386 	bcm_spi_drain_fifo(sc);
387 
388 	/* Check for end of transfer. */
389 	if (sc->sc_written == sc->sc_len && sc->sc_read == sc->sc_len) {
390 		/* Disable interrupts and the SPI engine. */
391 		if ((sc->sc_flags & BCM_SPI_KEEP_CS) == 0) {
392 			bcm_spi_modifyreg(sc, SPI_CS,
393 			    SPI_CS_TA | SPI_CS_INTR | SPI_CS_INTD, 0);
394 		}
395 		wakeup(sc->sc_dev);
396 	}
397 
398 	BCM_SPI_UNLOCK(sc);
399 }
400 
401 static int
402 bcm_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
403 {
404 	struct bcm_spi_softc *sc;
405 	uint32_t cs, mode, clock;
406 	int err;
407 
408 	sc = device_get_softc(dev);
409 
410 	KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
411 	    ("TX/RX command sizes should be equal"));
412 	KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
413 	    ("TX/RX data sizes should be equal"));
414 
415 	/* Get the bus speed, mode, and chip select for this child. */
416 
417 	spibus_get_cs(child, &cs);
418 	if ((cs & (~SPIBUS_CS_HIGH)) > 2) {
419 		device_printf(dev,
420 		    "Invalid chip select %u requested by %s\n", cs,
421 		    device_get_nameunit(child));
422 		return (EINVAL);
423 	}
424 
425 	spibus_get_clock(child, &clock);
426 	if (clock == 0) {
427 		device_printf(dev,
428 		    "Invalid clock %uHz requested by %s\n", clock,
429 		    device_get_nameunit(child));
430 		return (EINVAL);
431 	}
432 
433 	spibus_get_mode(child, &mode);
434 	if (mode > 3) {
435 		device_printf(dev,
436 		    "Invalid mode %u requested by %s\n", mode,
437 		    device_get_nameunit(child));
438 		return (EINVAL);
439 	}
440 
441 	/* If the controller is in use wait until it is available. */
442 	BCM_SPI_LOCK(sc);
443 	if (sc->sc_thread != curthread)
444 		while (sc->sc_flags & BCM_SPI_BUSY)
445 			mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_spi", 0);
446 
447 	/* Now we have control over SPI controller. */
448 	sc->sc_flags = BCM_SPI_BUSY;
449 
450 	if ((cmd->flags & SPI_FLAG_KEEP_CS) != 0)
451 		sc->sc_flags |= BCM_SPI_KEEP_CS;
452 
453 	/* Clear the FIFO. */
454 	if (sc->sc_thread != curthread)
455 		bcm_spi_modifyreg(sc, SPI_CS,
456 		    SPI_CS_CLEAR_RXFIFO | SPI_CS_CLEAR_TXFIFO,
457 		    SPI_CS_CLEAR_RXFIFO | SPI_CS_CLEAR_TXFIFO);
458 
459 	sc->sc_thread = curthread;
460 
461 	/* Save a pointer to the SPI command. */
462 	sc->sc_cmd = cmd;
463 	sc->sc_read = 0;
464 	sc->sc_written = 0;
465 	sc->sc_len = cmd->tx_cmd_sz + cmd->tx_data_sz;
466 
467 #ifdef	BCM2835_SPI_USE_CS_HIGH /* TODO: for when behavior is correct */
468 	/*
469 	 * Assign CS polarity first, while the CS indicates 'inactive'.
470 	 * This will need to set the correct polarity bit based on the 'cs', and
471 	 * the polarity bit will remain in this state, even after the transaction
472 	 * is complete.
473 	 */
474 	if((cs & ~SPIBUS_CS_HIGH) == 0) {
475 		bcm_spi_modifyreg(sc, SPI_CS,
476 		    SPI_CS_CSPOL0,
477 		    ((cs & (SPIBUS_CS_HIGH)) ? SPI_CS_CSPOL0 : 0));
478 	}
479 	else if((cs & ~SPIBUS_CS_HIGH) == 1) {
480 		bcm_spi_modifyreg(sc, SPI_CS,
481 		    SPI_CS_CSPOL1,
482 		    ((cs & (SPIBUS_CS_HIGH)) ? SPI_CS_CSPOL1 : 0));
483 	}
484 	else if((cs & ~SPIBUS_CS_HIGH) == 2) {
485 		bcm_spi_modifyreg(sc, SPI_CS,
486 		    SPI_CS_CSPOL2,
487 		    ((cs & (SPIBUS_CS_HIGH)) ? SPI_CS_CSPOL2 : 0));
488 	}
489 #endif
490 
491 	/*
492 	 * Set the mode in 'SPI_CS' (clock phase and polarity bits).
493 	 * This must happen before CS output pin is active.
494 	 * Otherwise, you might glitch and drop the first bit.
495 	 */
496 	bcm_spi_modifyreg(sc, SPI_CS,
497 	    SPI_CS_CPOL | SPI_CS_CPHA,
498 	    ((mode & SPIBUS_MODE_CPHA) ? SPI_CS_CPHA : 0) |
499 	    ((mode & SPIBUS_MODE_CPOL) ? SPI_CS_CPOL : 0));
500 
501 	/*
502 	 * Set the clock divider in 'SPI_CLK - see 'bcm_spi_clock_proc()'.
503 	 */
504 
505 	/* calculate 'clock' as a divider value from freq */
506 	clock = SPI_CORE_CLK / clock;
507 	if (clock <= 1)
508 		clock = 2;
509 	else if (clock % 2)
510 		clock--;
511 	if (clock > 0xffff)
512 		clock = 0;
513 
514 	BCM_SPI_WRITE(sc, SPI_CLK, clock);
515 
516 	/*
517 	 * Set the CS for this transaction, enable interrupts and announce
518 	 * we're ready to tx.  This will kick off the first interrupt.
519 	 */
520 	bcm_spi_modifyreg(sc, SPI_CS,
521 	    SPI_CS_MASK | SPI_CS_TA | SPI_CS_INTR | SPI_CS_INTD,
522 	    (cs & (~SPIBUS_CS_HIGH)) | /* cs is the lower 2 bits of the reg */
523 	    SPI_CS_TA | SPI_CS_INTR | SPI_CS_INTD);
524 
525 	/* Wait for the transaction to complete. */
526 	err = mtx_sleep(dev, &sc->sc_mtx, 0, "bcm_spi", hz * 2);
527 
528 	/* Make sure the SPI engine and interrupts are disabled. */
529 	if (!(cmd->flags & SPI_FLAG_KEEP_CS)) {
530 		bcm_spi_modifyreg(sc,
531 		    SPI_CS, SPI_CS_TA | SPI_CS_INTR | SPI_CS_INTD, 0);
532 		sc->sc_thread = 0;
533 	}
534 
535 	wakeup_one(dev);
536 	sc->sc_flags &= ~BCM_SPI_BUSY;
537 	/* Release the controller and wakeup the next thread waiting for it. */
538 	BCM_SPI_UNLOCK(sc);
539 
540 	/*
541 	 * Check for transfer timeout.  The SPI controller doesn't
542 	 * return errors.
543 	 */
544 	if (err == EWOULDBLOCK) {
545 		device_printf(sc->sc_dev, "SPI error (timeout)\n");
546 		err = EIO;
547 	}
548 
549 	return (err);
550 }
551 
552 static phandle_t
553 bcm_spi_get_node(device_t bus, device_t dev)
554 {
555 
556 	/* We only have one child, the SPI bus, which needs our own node. */
557 	return (ofw_bus_get_node(bus));
558 }
559 
560 static device_method_t bcm_spi_methods[] = {
561 	/* Device interface */
562 	DEVMETHOD(device_probe,		bcm_spi_probe),
563 	DEVMETHOD(device_attach,	bcm_spi_attach),
564 	DEVMETHOD(device_detach,	bcm_spi_detach),
565 
566 	/* SPI interface */
567 	DEVMETHOD(spibus_transfer,	bcm_spi_transfer),
568 
569 	/* ofw_bus interface */
570 	DEVMETHOD(ofw_bus_get_node,	bcm_spi_get_node),
571 
572 	DEVMETHOD_END
573 };
574 
575 static driver_t bcm_spi_driver = {
576 	"spi",
577 	bcm_spi_methods,
578 	sizeof(struct bcm_spi_softc),
579 };
580 
581 DRIVER_MODULE(bcm2835_spi, simplebus, bcm_spi_driver, 0, 0);
582