xref: /freebsd/sys/arm/xilinx/zy7_spi.c (revision 680ccae39072d6253c48cf72d2936ac99b47aefe)
13f9309e5SEmmanuel Vadot /*-
23f9309e5SEmmanuel Vadot  * Copyright (c) 2018 Thomas Skibo <thomasskibo@yahoo.com>
33f9309e5SEmmanuel Vadot  * All rights reserved.
43f9309e5SEmmanuel Vadot  *
53f9309e5SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
63f9309e5SEmmanuel Vadot  * modification, are permitted provided that the following conditions
73f9309e5SEmmanuel Vadot  * are met:
83f9309e5SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
93f9309e5SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
103f9309e5SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
113f9309e5SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
123f9309e5SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
133f9309e5SEmmanuel Vadot  *
143f9309e5SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153f9309e5SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163f9309e5SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173f9309e5SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183f9309e5SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193f9309e5SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203f9309e5SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213f9309e5SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223f9309e5SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233f9309e5SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243f9309e5SEmmanuel Vadot  * SUCH DAMAGE.
253f9309e5SEmmanuel Vadot  *
263f9309e5SEmmanuel Vadot  * $FreeBSD$
273f9309e5SEmmanuel Vadot  */
283f9309e5SEmmanuel Vadot 
293f9309e5SEmmanuel Vadot #include <sys/cdefs.h>
303f9309e5SEmmanuel Vadot __FBSDID("$FreeBSD$");
313f9309e5SEmmanuel Vadot 
323f9309e5SEmmanuel Vadot #include <sys/param.h>
333f9309e5SEmmanuel Vadot #include <sys/systm.h>
343f9309e5SEmmanuel Vadot #include <sys/conf.h>
353f9309e5SEmmanuel Vadot #include <sys/kernel.h>
363f9309e5SEmmanuel Vadot #include <sys/module.h>
373f9309e5SEmmanuel Vadot #include <sys/sysctl.h>
383f9309e5SEmmanuel Vadot #include <sys/lock.h>
393f9309e5SEmmanuel Vadot #include <sys/mutex.h>
403f9309e5SEmmanuel Vadot #include <sys/resource.h>
413f9309e5SEmmanuel Vadot #include <sys/rman.h>
423f9309e5SEmmanuel Vadot #include <sys/uio.h>
433f9309e5SEmmanuel Vadot 
443f9309e5SEmmanuel Vadot #include <machine/bus.h>
453f9309e5SEmmanuel Vadot #include <machine/resource.h>
463f9309e5SEmmanuel Vadot #include <machine/stdarg.h>
473f9309e5SEmmanuel Vadot 
483f9309e5SEmmanuel Vadot #include <dev/fdt/fdt_common.h>
493f9309e5SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
503f9309e5SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
513f9309e5SEmmanuel Vadot 
523f9309e5SEmmanuel Vadot #include <dev/spibus/spi.h>
533f9309e5SEmmanuel Vadot #include <dev/spibus/spibusvar.h>
543f9309e5SEmmanuel Vadot 
553f9309e5SEmmanuel Vadot #include "spibus_if.h"
563f9309e5SEmmanuel Vadot 
573f9309e5SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
583f9309e5SEmmanuel Vadot 	{"xlnx,zy7_spi",		1},
593f9309e5SEmmanuel Vadot 	{"xlnx,zynq-spi-1.0",		1},
603f9309e5SEmmanuel Vadot 	{"cdns,spi-r1p6",		1},
613f9309e5SEmmanuel Vadot 	{NULL,				0}
623f9309e5SEmmanuel Vadot };
633f9309e5SEmmanuel Vadot 
643f9309e5SEmmanuel Vadot struct zy7_spi_softc {
653f9309e5SEmmanuel Vadot 	device_t		dev;
663f9309e5SEmmanuel Vadot 	device_t		child;
673f9309e5SEmmanuel Vadot 	struct mtx		sc_mtx;
683f9309e5SEmmanuel Vadot 	struct resource		*mem_res;
693f9309e5SEmmanuel Vadot 	struct resource		*irq_res;
703f9309e5SEmmanuel Vadot 	void			*intrhandle;
713f9309e5SEmmanuel Vadot 
723f9309e5SEmmanuel Vadot 	uint32_t		cfg_reg_shadow;
733f9309e5SEmmanuel Vadot 	uint32_t		spi_clock;
743f9309e5SEmmanuel Vadot 	uint32_t		ref_clock;
753f9309e5SEmmanuel Vadot 	unsigned int		spi_clk_real_freq;
763f9309e5SEmmanuel Vadot 	unsigned int		rx_overflows;
773f9309e5SEmmanuel Vadot 	unsigned int		tx_underflows;
783f9309e5SEmmanuel Vadot 	unsigned int		interrupts;
793f9309e5SEmmanuel Vadot 	unsigned int		stray_ints;
803f9309e5SEmmanuel Vadot 	struct spi_command	*cmd;
813f9309e5SEmmanuel Vadot 	int			tx_bytes;	/* tx_cmd_sz + tx_data_sz */
823f9309e5SEmmanuel Vadot 	int			tx_bytes_sent;
833f9309e5SEmmanuel Vadot 	int			rx_bytes;	/* rx_cmd_sz + rx_data_sz */
843f9309e5SEmmanuel Vadot 	int			rx_bytes_rcvd;
853f9309e5SEmmanuel Vadot 	int			busy;
863f9309e5SEmmanuel Vadot };
873f9309e5SEmmanuel Vadot 
883f9309e5SEmmanuel Vadot #define ZY7_SPI_DEFAULT_SPI_CLOCK	50000000
893f9309e5SEmmanuel Vadot 
903f9309e5SEmmanuel Vadot #define SPI_SC_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
913f9309e5SEmmanuel Vadot #define	SPI_SC_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
923f9309e5SEmmanuel Vadot #define SPI_SC_LOCK_INIT(sc) \
933f9309e5SEmmanuel Vadot 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev),	NULL, MTX_DEF)
943f9309e5SEmmanuel Vadot #define SPI_SC_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
953f9309e5SEmmanuel Vadot #define SPI_SC_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
963f9309e5SEmmanuel Vadot 
973f9309e5SEmmanuel Vadot #define RD4(sc, off)		(bus_read_4((sc)->mem_res, (off)))
983f9309e5SEmmanuel Vadot #define WR4(sc, off, val)	(bus_write_4((sc)->mem_res, (off), (val)))
993f9309e5SEmmanuel Vadot 
1003f9309e5SEmmanuel Vadot /*
1013f9309e5SEmmanuel Vadot  * SPI device registers.
1023f9309e5SEmmanuel Vadot  * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual.
1033f9309e5SEmmanuel Vadot  * (v1.12.1) December 6, 2017.  Xilinx doc UG585.
1043f9309e5SEmmanuel Vadot  */
1053f9309e5SEmmanuel Vadot #define ZY7_SPI_CONFIG_REG		0x0000
1063f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_MODEFAIL_GEN_EN	(1 << 17)
1073f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_MAN_STRT		(1 << 16)
1083f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_MAN_STRT_EN		(1 << 15)
1093f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_MAN_CS			(1 << 14)
1103f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_CS_MASK		(0xf << 10)
1113f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_CS(x)			((0xf ^ (1 << (x))) << 10)
1123f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_PERI_SEL		(1 << 9)
1133f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_REF_CLK		(1 << 8)
1143f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_BAUD_RATE_DIV_MASK	(7 << 3)
1153f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_BAUD_RATE_DIV_SHIFT	3
1163f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_BAUD_RATE_DIV(x)	((x) << 3) /* divide by 2<<x */
1173f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_CLK_PH			(1 << 2)   /* clock phase */
1183f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_CLK_POL		(1 << 1)   /* clock polatiry */
1193f9309e5SEmmanuel Vadot #define   ZY7_SPI_CONFIG_MODE_SEL		(1 << 0)   /* master enable */
1203f9309e5SEmmanuel Vadot 
1213f9309e5SEmmanuel Vadot #define ZY7_SPI_INTR_STAT_REG		0x0004
1223f9309e5SEmmanuel Vadot #define ZY7_SPI_INTR_EN_REG		0x0008
1233f9309e5SEmmanuel Vadot #define ZY7_SPI_INTR_DIS_REG		0x000c
1243f9309e5SEmmanuel Vadot #define ZY7_SPI_INTR_MASK_REG		0x0010
1253f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_TX_FIFO_UNDERFLOW	(1 << 6)
1263f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_RX_FIFO_FULL		(1 << 5)
1273f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY	(1 << 4)
1283f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_TX_FIFO_FULL		(1 << 3)
1293f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_TX_FIFO_NOT_FULL		(1 << 2)
1303f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_MODE_FAULT		(1 << 1)
1313f9309e5SEmmanuel Vadot #define   ZY7_SPI_INTR_RX_OVERFLOW		(1 << 0)
1323f9309e5SEmmanuel Vadot 
1333f9309e5SEmmanuel Vadot #define ZY7_SPI_EN_REG			0x0014
1343f9309e5SEmmanuel Vadot #define   ZY7_SPI_ENABLE		(1 << 0)
1353f9309e5SEmmanuel Vadot 
1363f9309e5SEmmanuel Vadot #define ZY7_SPI_DELAY_CTRL_REG		0x0018
1373f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_BTWN_MASK		(0xff << 16)
1383f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_BTWN_SHIFT		16
1393f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_AFTER_MASK		(0xff << 8)
1403f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_AFTER_SHIFT	8
1413f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_INIT_MASK		(0xff << 0)
1423f9309e5SEmmanuel Vadot #define   ZY7_SPI_DELAY_CTRL_INIT_SHIFT		0
1433f9309e5SEmmanuel Vadot 
1443f9309e5SEmmanuel Vadot #define ZY7_SPI_TX_DATA_REG		0x001c
1453f9309e5SEmmanuel Vadot #define ZY7_SPI_RX_DATA_REG		0x0020
1463f9309e5SEmmanuel Vadot 
1473f9309e5SEmmanuel Vadot #define ZY7_SPI_SLV_IDLE_COUNT_REG	0x0024
1483f9309e5SEmmanuel Vadot 
1493f9309e5SEmmanuel Vadot #define ZY7_SPI_TX_THRESH_REG		0x0028
1503f9309e5SEmmanuel Vadot #define ZY7_SPI_RX_THRESH_REG		0x002c
1513f9309e5SEmmanuel Vadot 
1523f9309e5SEmmanuel Vadot /* Fill hardware fifo with command and data bytes. */
1533f9309e5SEmmanuel Vadot static void
1543f9309e5SEmmanuel Vadot zy7_spi_write_fifo(struct zy7_spi_softc *sc, int nbytes)
1553f9309e5SEmmanuel Vadot {
1563f9309e5SEmmanuel Vadot 	uint8_t byte;
1573f9309e5SEmmanuel Vadot 
1583f9309e5SEmmanuel Vadot 	while (nbytes > 0) {
1593f9309e5SEmmanuel Vadot 		if (sc->tx_bytes_sent < sc->cmd->tx_cmd_sz)
1603f9309e5SEmmanuel Vadot 			/* Writing command. */
1613f9309e5SEmmanuel Vadot 			byte = *((uint8_t *)sc->cmd->tx_cmd +
1623f9309e5SEmmanuel Vadot 				 sc->tx_bytes_sent);
1633f9309e5SEmmanuel Vadot 		else
1643f9309e5SEmmanuel Vadot 			/* Writing data. */
1653f9309e5SEmmanuel Vadot 			byte = *((uint8_t *)sc->cmd->tx_data +
1663f9309e5SEmmanuel Vadot 				 (sc->tx_bytes_sent - sc->cmd->tx_cmd_sz));
1673f9309e5SEmmanuel Vadot 
1683f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_TX_DATA_REG, (uint32_t)byte);
1693f9309e5SEmmanuel Vadot 
1703f9309e5SEmmanuel Vadot 		sc->tx_bytes_sent++;
1713f9309e5SEmmanuel Vadot 		nbytes--;
1723f9309e5SEmmanuel Vadot 	}
1733f9309e5SEmmanuel Vadot }
1743f9309e5SEmmanuel Vadot 
1753f9309e5SEmmanuel Vadot /* Read hardware fifo data into command response and data buffers. */
1763f9309e5SEmmanuel Vadot static void
1773f9309e5SEmmanuel Vadot zy7_spi_read_fifo(struct zy7_spi_softc *sc)
1783f9309e5SEmmanuel Vadot {
1793f9309e5SEmmanuel Vadot 	uint8_t byte;
1803f9309e5SEmmanuel Vadot 
1813f9309e5SEmmanuel Vadot 	do {
1823f9309e5SEmmanuel Vadot 		byte = RD4(sc, ZY7_SPI_RX_DATA_REG) & 0xff;
1833f9309e5SEmmanuel Vadot 
1843f9309e5SEmmanuel Vadot 		if (sc->rx_bytes_rcvd < sc->cmd->rx_cmd_sz)
1853f9309e5SEmmanuel Vadot 			/* Reading command. */
1863f9309e5SEmmanuel Vadot 			*((uint8_t *)sc->cmd->rx_cmd + sc->rx_bytes_rcvd) =
1873f9309e5SEmmanuel Vadot 			    byte;
1883f9309e5SEmmanuel Vadot 		else
1893f9309e5SEmmanuel Vadot 			/* Reading data. */
1903f9309e5SEmmanuel Vadot 			*((uint8_t *)sc->cmd->rx_data +
1913f9309e5SEmmanuel Vadot 			    (sc->rx_bytes_rcvd - sc->cmd->rx_cmd_sz)) =
1923f9309e5SEmmanuel Vadot 			    byte;
1933f9309e5SEmmanuel Vadot 
1943f9309e5SEmmanuel Vadot 		sc->rx_bytes_rcvd++;
1953f9309e5SEmmanuel Vadot 
1963f9309e5SEmmanuel Vadot 	} while (sc->rx_bytes_rcvd < sc->rx_bytes &&
1973f9309e5SEmmanuel Vadot 	    (RD4(sc, ZY7_SPI_INTR_STAT_REG) &
1983f9309e5SEmmanuel Vadot 		ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY) != 0);
1993f9309e5SEmmanuel Vadot }
2003f9309e5SEmmanuel Vadot 
2013f9309e5SEmmanuel Vadot /* End a transfer early by draining rx fifo and disabling interrupts. */
2023f9309e5SEmmanuel Vadot static void
2033f9309e5SEmmanuel Vadot zy7_spi_abort_transfer(struct zy7_spi_softc *sc)
2043f9309e5SEmmanuel Vadot {
2053f9309e5SEmmanuel Vadot 	/* Drain receive fifo. */
2063f9309e5SEmmanuel Vadot 	while ((RD4(sc, ZY7_SPI_INTR_STAT_REG) &
2073f9309e5SEmmanuel Vadot 		ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY) != 0)
2083f9309e5SEmmanuel Vadot 		(void)RD4(sc, ZY7_SPI_RX_DATA_REG);
2093f9309e5SEmmanuel Vadot 
2103f9309e5SEmmanuel Vadot 	/* Shut down interrupts. */
2113f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_INTR_DIS_REG,
2123f9309e5SEmmanuel Vadot 	    ZY7_SPI_INTR_RX_OVERFLOW |
2133f9309e5SEmmanuel Vadot 	    ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY |
2143f9309e5SEmmanuel Vadot 	    ZY7_SPI_INTR_TX_FIFO_NOT_FULL);
2153f9309e5SEmmanuel Vadot }
2163f9309e5SEmmanuel Vadot 
2173f9309e5SEmmanuel Vadot static void
2183f9309e5SEmmanuel Vadot zy7_spi_intr(void *arg)
2193f9309e5SEmmanuel Vadot {
2203f9309e5SEmmanuel Vadot 	struct zy7_spi_softc *sc = (struct zy7_spi_softc *)arg;
2213f9309e5SEmmanuel Vadot 	uint32_t istatus;
2223f9309e5SEmmanuel Vadot 
2233f9309e5SEmmanuel Vadot 	SPI_SC_LOCK(sc);
2243f9309e5SEmmanuel Vadot 
2253f9309e5SEmmanuel Vadot 	sc->interrupts++;
2263f9309e5SEmmanuel Vadot 
2273f9309e5SEmmanuel Vadot 	istatus = RD4(sc, ZY7_SPI_INTR_STAT_REG);
2283f9309e5SEmmanuel Vadot 
2293f9309e5SEmmanuel Vadot 	/* Stray interrupts can happen if a transfer gets interrupted. */
2303f9309e5SEmmanuel Vadot 	if (!sc->busy) {
2313f9309e5SEmmanuel Vadot 		sc->stray_ints++;
2323f9309e5SEmmanuel Vadot 		SPI_SC_UNLOCK(sc);
2333f9309e5SEmmanuel Vadot 		return;
2343f9309e5SEmmanuel Vadot 	}
2353f9309e5SEmmanuel Vadot 
2363f9309e5SEmmanuel Vadot 	if ((istatus & ZY7_SPI_INTR_RX_OVERFLOW) != 0) {
2373f9309e5SEmmanuel Vadot 		device_printf(sc->dev, "rx fifo overflow!\n");
2383f9309e5SEmmanuel Vadot 		sc->rx_overflows++;
2393f9309e5SEmmanuel Vadot 
2403f9309e5SEmmanuel Vadot 		/* Clear status bit. */
2413f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_INTR_STAT_REG,
2423f9309e5SEmmanuel Vadot 		    ZY7_SPI_INTR_RX_OVERFLOW);
2433f9309e5SEmmanuel Vadot 	}
2443f9309e5SEmmanuel Vadot 
2453f9309e5SEmmanuel Vadot 	/* Empty receive fifo before any more transmit data is sent. */
2463f9309e5SEmmanuel Vadot 	if (sc->rx_bytes_rcvd < sc->rx_bytes &&
2473f9309e5SEmmanuel Vadot 	    (istatus & ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY) != 0) {
2483f9309e5SEmmanuel Vadot 		zy7_spi_read_fifo(sc);
2493f9309e5SEmmanuel Vadot 		if (sc->rx_bytes_rcvd == sc->rx_bytes)
2503f9309e5SEmmanuel Vadot 			/* Disable receive interrupts. */
2513f9309e5SEmmanuel Vadot 			WR4(sc, ZY7_SPI_INTR_DIS_REG,
2523f9309e5SEmmanuel Vadot 			    ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY |
2533f9309e5SEmmanuel Vadot 			    ZY7_SPI_INTR_RX_OVERFLOW);
2543f9309e5SEmmanuel Vadot 	}
2553f9309e5SEmmanuel Vadot 
2563f9309e5SEmmanuel Vadot 	/* Count tx underflows.  They probably shouldn't happen. */
2573f9309e5SEmmanuel Vadot 	if ((istatus & ZY7_SPI_INTR_TX_FIFO_UNDERFLOW) != 0) {
2583f9309e5SEmmanuel Vadot 		sc->tx_underflows++;
2593f9309e5SEmmanuel Vadot 
2603f9309e5SEmmanuel Vadot 		/* Clear status bit. */
2613f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_INTR_STAT_REG,
2623f9309e5SEmmanuel Vadot 		    ZY7_SPI_INTR_TX_FIFO_UNDERFLOW);
2633f9309e5SEmmanuel Vadot 	}
2643f9309e5SEmmanuel Vadot 
2653f9309e5SEmmanuel Vadot 	/* Fill transmit fifo. */
2663f9309e5SEmmanuel Vadot 	if (sc->tx_bytes_sent < sc->tx_bytes &&
2673f9309e5SEmmanuel Vadot 	    (istatus & ZY7_SPI_INTR_TX_FIFO_NOT_FULL) != 0) {
2683f9309e5SEmmanuel Vadot 		zy7_spi_write_fifo(sc, MIN(96, sc->tx_bytes -
2693f9309e5SEmmanuel Vadot 			sc->tx_bytes_sent));
2703f9309e5SEmmanuel Vadot 
2713f9309e5SEmmanuel Vadot 		if (sc->tx_bytes_sent == sc->tx_bytes) {
2723f9309e5SEmmanuel Vadot 			/* Disable transmit FIFO interrupt, enable receive
2733f9309e5SEmmanuel Vadot 			 * FIFO interrupt.
2743f9309e5SEmmanuel Vadot 			 */
2753f9309e5SEmmanuel Vadot 			WR4(sc, ZY7_SPI_INTR_DIS_REG,
2763f9309e5SEmmanuel Vadot 			    ZY7_SPI_INTR_TX_FIFO_NOT_FULL);
2773f9309e5SEmmanuel Vadot 			WR4(sc, ZY7_SPI_INTR_EN_REG,
2783f9309e5SEmmanuel Vadot 			    ZY7_SPI_INTR_RX_FIFO_NOT_EMPTY);
2793f9309e5SEmmanuel Vadot 		}
2803f9309e5SEmmanuel Vadot 	}
2813f9309e5SEmmanuel Vadot 
2823f9309e5SEmmanuel Vadot 	/* Finished with transfer? */
2833f9309e5SEmmanuel Vadot 	if (sc->tx_bytes_sent == sc->tx_bytes &&
2843f9309e5SEmmanuel Vadot 	    sc->rx_bytes_rcvd == sc->rx_bytes) {
2853f9309e5SEmmanuel Vadot 		/* De-assert CS. */
2863f9309e5SEmmanuel Vadot 		sc->cfg_reg_shadow &=
2873f9309e5SEmmanuel Vadot 		    ~(ZY7_SPI_CONFIG_CLK_PH | ZY7_SPI_CONFIG_CLK_POL);
2883f9309e5SEmmanuel Vadot 		sc->cfg_reg_shadow |= ZY7_SPI_CONFIG_CS_MASK;
2893f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_CONFIG_REG, sc->cfg_reg_shadow);
2903f9309e5SEmmanuel Vadot 
2913f9309e5SEmmanuel Vadot 		wakeup(sc->dev);
2923f9309e5SEmmanuel Vadot 	}
2933f9309e5SEmmanuel Vadot 
2943f9309e5SEmmanuel Vadot 	SPI_SC_UNLOCK(sc);
2953f9309e5SEmmanuel Vadot }
2963f9309e5SEmmanuel Vadot 
2973f9309e5SEmmanuel Vadot /* Initialize hardware. */
2983f9309e5SEmmanuel Vadot static int
2993f9309e5SEmmanuel Vadot zy7_spi_init_hw(struct zy7_spi_softc *sc)
3003f9309e5SEmmanuel Vadot {
3013f9309e5SEmmanuel Vadot 	uint32_t baud_div;
3023f9309e5SEmmanuel Vadot 
3033f9309e5SEmmanuel Vadot 	/* Find best clock divider. Divide by 2 not supported. */
3043f9309e5SEmmanuel Vadot 	baud_div = 1;
3053f9309e5SEmmanuel Vadot 	while ((sc->ref_clock >> (baud_div + 1)) > sc->spi_clock &&
3063f9309e5SEmmanuel Vadot 	    baud_div < 8)
3073f9309e5SEmmanuel Vadot 		baud_div++;
3083f9309e5SEmmanuel Vadot 	if (baud_div >= 8) {
3093f9309e5SEmmanuel Vadot 		device_printf(sc->dev, "cannot configure clock divider: ref=%d"
3103f9309e5SEmmanuel Vadot 		    " spi=%d.\n", sc->ref_clock, sc->spi_clock);
3113f9309e5SEmmanuel Vadot 		return (EINVAL);
3123f9309e5SEmmanuel Vadot 	}
3133f9309e5SEmmanuel Vadot 	sc->spi_clk_real_freq = sc->ref_clock >> (baud_div + 1);
3143f9309e5SEmmanuel Vadot 
3153f9309e5SEmmanuel Vadot 	/* Set up configuration register. */
3163f9309e5SEmmanuel Vadot 	sc->cfg_reg_shadow =
3173f9309e5SEmmanuel Vadot 	    ZY7_SPI_CONFIG_MAN_CS |
3183f9309e5SEmmanuel Vadot 	    ZY7_SPI_CONFIG_CS_MASK |
3193f9309e5SEmmanuel Vadot 	    ZY7_SPI_CONFIG_BAUD_RATE_DIV(baud_div) |
3203f9309e5SEmmanuel Vadot 	    ZY7_SPI_CONFIG_MODE_SEL;
3213f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_CONFIG_REG, sc->cfg_reg_shadow);
3223f9309e5SEmmanuel Vadot 
3233f9309e5SEmmanuel Vadot 	/* Set thresholds. */
3243f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_TX_THRESH_REG, 32);
3253f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_RX_THRESH_REG, 1);
3263f9309e5SEmmanuel Vadot 
3273f9309e5SEmmanuel Vadot 	/* Clear and disable all interrupts. */
3283f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_INTR_STAT_REG, ~0);
3293f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_INTR_DIS_REG, ~0);
3303f9309e5SEmmanuel Vadot 
3313f9309e5SEmmanuel Vadot 	/* Enable SPI. */
3323f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_EN_REG, ZY7_SPI_ENABLE);
3333f9309e5SEmmanuel Vadot 
3343f9309e5SEmmanuel Vadot 	return (0);
3353f9309e5SEmmanuel Vadot }
3363f9309e5SEmmanuel Vadot 
3373f9309e5SEmmanuel Vadot static void
3383f9309e5SEmmanuel Vadot zy7_spi_add_sysctls(device_t dev)
3393f9309e5SEmmanuel Vadot {
3403f9309e5SEmmanuel Vadot 	struct zy7_spi_softc *sc = device_get_softc(dev);
3413f9309e5SEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
3423f9309e5SEmmanuel Vadot 	struct sysctl_oid_list *child;
3433f9309e5SEmmanuel Vadot 
3443f9309e5SEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
3453f9309e5SEmmanuel Vadot 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
3463f9309e5SEmmanuel Vadot 
3473f9309e5SEmmanuel Vadot 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "spi_clk_real_freq", CTLFLAG_RD,
3483f9309e5SEmmanuel Vadot 	    &sc->spi_clk_real_freq, 0, "SPI clock real frequency");
3493f9309e5SEmmanuel Vadot 
3503f9309e5SEmmanuel Vadot 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "rx_overflows", CTLFLAG_RD,
3513f9309e5SEmmanuel Vadot 	    &sc->rx_overflows, 0, "RX FIFO overflow events");
3523f9309e5SEmmanuel Vadot 
3533f9309e5SEmmanuel Vadot 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "tx_underflows", CTLFLAG_RD,
3543f9309e5SEmmanuel Vadot 	    &sc->tx_underflows, 0, "TX FIFO underflow events");
3553f9309e5SEmmanuel Vadot 
3563f9309e5SEmmanuel Vadot 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "interrupts", CTLFLAG_RD,
3573f9309e5SEmmanuel Vadot 	    &sc->interrupts, 0, "interrupt calls");
3583f9309e5SEmmanuel Vadot 
3593f9309e5SEmmanuel Vadot 	SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "stray_ints", CTLFLAG_RD,
3603f9309e5SEmmanuel Vadot 	    &sc->stray_ints, 0, "stray interrupts");
3613f9309e5SEmmanuel Vadot }
3623f9309e5SEmmanuel Vadot 
3633f9309e5SEmmanuel Vadot static int
3643f9309e5SEmmanuel Vadot zy7_spi_probe(device_t dev)
3653f9309e5SEmmanuel Vadot {
3663f9309e5SEmmanuel Vadot 
3673f9309e5SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
3683f9309e5SEmmanuel Vadot 		return (ENXIO);
3693f9309e5SEmmanuel Vadot 
3703f9309e5SEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
3713f9309e5SEmmanuel Vadot 		return (ENXIO);
3723f9309e5SEmmanuel Vadot 
3733f9309e5SEmmanuel Vadot 	device_set_desc(dev, "Zynq SPI Controller");
3743f9309e5SEmmanuel Vadot 
3753f9309e5SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
3763f9309e5SEmmanuel Vadot }
3773f9309e5SEmmanuel Vadot 
3783f9309e5SEmmanuel Vadot static int zy7_spi_detach(device_t);
3793f9309e5SEmmanuel Vadot 
3803f9309e5SEmmanuel Vadot static int
3813f9309e5SEmmanuel Vadot zy7_spi_attach(device_t dev)
3823f9309e5SEmmanuel Vadot {
3833f9309e5SEmmanuel Vadot 	struct zy7_spi_softc *sc;
3843f9309e5SEmmanuel Vadot 	int rid, err;
3853f9309e5SEmmanuel Vadot 	phandle_t node;
3863f9309e5SEmmanuel Vadot 	pcell_t cell;
3873f9309e5SEmmanuel Vadot 
3883f9309e5SEmmanuel Vadot 	sc = device_get_softc(dev);
3893f9309e5SEmmanuel Vadot 	sc->dev = dev;
3903f9309e5SEmmanuel Vadot 
3913f9309e5SEmmanuel Vadot 	SPI_SC_LOCK_INIT(sc);
3923f9309e5SEmmanuel Vadot 
3933f9309e5SEmmanuel Vadot 	/* Get ref-clock and spi-clock properties. */
3943f9309e5SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
3953f9309e5SEmmanuel Vadot 	if (OF_getprop(node, "ref-clock", &cell, sizeof(cell)) > 0)
3963f9309e5SEmmanuel Vadot 		sc->ref_clock = fdt32_to_cpu(cell);
3973f9309e5SEmmanuel Vadot 	else {
3983f9309e5SEmmanuel Vadot 		device_printf(dev, "must have ref-clock property\n");
3993f9309e5SEmmanuel Vadot 		return (ENXIO);
4003f9309e5SEmmanuel Vadot 	}
4013f9309e5SEmmanuel Vadot 	if (OF_getprop(node, "spi-clock", &cell, sizeof(cell)) > 0)
4023f9309e5SEmmanuel Vadot 		sc->spi_clock = fdt32_to_cpu(cell);
4033f9309e5SEmmanuel Vadot 	else
4043f9309e5SEmmanuel Vadot 		sc->spi_clock = ZY7_SPI_DEFAULT_SPI_CLOCK;
4053f9309e5SEmmanuel Vadot 
4063f9309e5SEmmanuel Vadot 	/* Get memory resource. */
4073f9309e5SEmmanuel Vadot 	rid = 0;
4083f9309e5SEmmanuel Vadot 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
4093f9309e5SEmmanuel Vadot 	    RF_ACTIVE);
4103f9309e5SEmmanuel Vadot 	if (sc->mem_res == NULL) {
4113f9309e5SEmmanuel Vadot 		device_printf(dev, "could not allocate memory resources.\n");
4123f9309e5SEmmanuel Vadot 		zy7_spi_detach(dev);
4133f9309e5SEmmanuel Vadot 		return (ENOMEM);
4143f9309e5SEmmanuel Vadot 	}
4153f9309e5SEmmanuel Vadot 
4163f9309e5SEmmanuel Vadot 	/* Allocate IRQ. */
4173f9309e5SEmmanuel Vadot 	rid = 0;
4183f9309e5SEmmanuel Vadot 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4193f9309e5SEmmanuel Vadot 	    RF_ACTIVE);
4203f9309e5SEmmanuel Vadot 	if (sc->irq_res == NULL) {
4213f9309e5SEmmanuel Vadot 		device_printf(dev, "could not allocate IRQ resource.\n");
4223f9309e5SEmmanuel Vadot 		zy7_spi_detach(dev);
4233f9309e5SEmmanuel Vadot 		return (ENOMEM);
4243f9309e5SEmmanuel Vadot 	}
4253f9309e5SEmmanuel Vadot 
4263f9309e5SEmmanuel Vadot 	/* Activate the interrupt. */
4273f9309e5SEmmanuel Vadot 	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
4283f9309e5SEmmanuel Vadot 	    NULL, zy7_spi_intr, sc, &sc->intrhandle);
4293f9309e5SEmmanuel Vadot 	if (err) {
4303f9309e5SEmmanuel Vadot 		device_printf(dev, "could not setup IRQ.\n");
4313f9309e5SEmmanuel Vadot 		zy7_spi_detach(dev);
4323f9309e5SEmmanuel Vadot 		return (err);
4333f9309e5SEmmanuel Vadot 	}
4343f9309e5SEmmanuel Vadot 
4353f9309e5SEmmanuel Vadot 	/* Configure the device. */
4363f9309e5SEmmanuel Vadot 	err = zy7_spi_init_hw(sc);
4373f9309e5SEmmanuel Vadot 	if (err) {
4383f9309e5SEmmanuel Vadot 		zy7_spi_detach(dev);
4393f9309e5SEmmanuel Vadot 		return (err);
4403f9309e5SEmmanuel Vadot 	}
4413f9309e5SEmmanuel Vadot 
4423f9309e5SEmmanuel Vadot 	sc->child = device_add_child(dev, "spibus", -1);
4433f9309e5SEmmanuel Vadot 
4443f9309e5SEmmanuel Vadot 	zy7_spi_add_sysctls(dev);
4453f9309e5SEmmanuel Vadot 
4463f9309e5SEmmanuel Vadot 	/* Attach spibus driver as a child later when interrupts work. */
4473f9309e5SEmmanuel Vadot 	config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
4483f9309e5SEmmanuel Vadot 
4493f9309e5SEmmanuel Vadot 	return (0);
4503f9309e5SEmmanuel Vadot }
4513f9309e5SEmmanuel Vadot 
4523f9309e5SEmmanuel Vadot static int
4533f9309e5SEmmanuel Vadot zy7_spi_detach(device_t dev)
4543f9309e5SEmmanuel Vadot {
4553f9309e5SEmmanuel Vadot 	struct zy7_spi_softc *sc = device_get_softc(dev);
4563f9309e5SEmmanuel Vadot 
4573f9309e5SEmmanuel Vadot 	if (device_is_attached(dev))
4583f9309e5SEmmanuel Vadot 		bus_generic_detach(dev);
4593f9309e5SEmmanuel Vadot 
4603f9309e5SEmmanuel Vadot 	/* Delete child bus. */
4613f9309e5SEmmanuel Vadot 	if (sc->child)
4623f9309e5SEmmanuel Vadot 		device_delete_child(dev, sc->child);
4633f9309e5SEmmanuel Vadot 
4643f9309e5SEmmanuel Vadot 	/* Disable hardware. */
4653f9309e5SEmmanuel Vadot 	if (sc->mem_res != NULL) {
4663f9309e5SEmmanuel Vadot 		/* Disable SPI. */
4673f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_EN_REG, 0);
4683f9309e5SEmmanuel Vadot 
4693f9309e5SEmmanuel Vadot 		/* Clear and disable all interrupts. */
4703f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_INTR_STAT_REG, ~0);
4713f9309e5SEmmanuel Vadot 		WR4(sc, ZY7_SPI_INTR_DIS_REG, ~0);
4723f9309e5SEmmanuel Vadot 	}
4733f9309e5SEmmanuel Vadot 
4743f9309e5SEmmanuel Vadot 	/* Teardown and release interrupt. */
4753f9309e5SEmmanuel Vadot 	if (sc->irq_res != NULL) {
4763f9309e5SEmmanuel Vadot 		if (sc->intrhandle)
4773f9309e5SEmmanuel Vadot 			bus_teardown_intr(dev, sc->irq_res, sc->intrhandle);
4783f9309e5SEmmanuel Vadot 		bus_release_resource(dev, SYS_RES_IRQ,
4793f9309e5SEmmanuel Vadot 		    rman_get_rid(sc->irq_res), sc->irq_res);
4803f9309e5SEmmanuel Vadot 	}
4813f9309e5SEmmanuel Vadot 
4823f9309e5SEmmanuel Vadot 	/* Release memory resource. */
4833f9309e5SEmmanuel Vadot 	if (sc->mem_res != NULL)
4843f9309e5SEmmanuel Vadot 		bus_release_resource(dev, SYS_RES_MEMORY,
4853f9309e5SEmmanuel Vadot 		    rman_get_rid(sc->mem_res), sc->mem_res);
4863f9309e5SEmmanuel Vadot 
4873f9309e5SEmmanuel Vadot 	SPI_SC_LOCK_DESTROY(sc);
4883f9309e5SEmmanuel Vadot 
4893f9309e5SEmmanuel Vadot 	return (0);
4903f9309e5SEmmanuel Vadot }
4913f9309e5SEmmanuel Vadot 
4923f9309e5SEmmanuel Vadot static phandle_t
4933f9309e5SEmmanuel Vadot zy7_spi_get_node(device_t bus, device_t dev)
4943f9309e5SEmmanuel Vadot {
4953f9309e5SEmmanuel Vadot 
4963f9309e5SEmmanuel Vadot 	return (ofw_bus_get_node(bus));
4973f9309e5SEmmanuel Vadot }
4983f9309e5SEmmanuel Vadot 
4993f9309e5SEmmanuel Vadot static int
5003f9309e5SEmmanuel Vadot zy7_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
5013f9309e5SEmmanuel Vadot {
5023f9309e5SEmmanuel Vadot 	struct zy7_spi_softc *sc = device_get_softc(dev);
5033f9309e5SEmmanuel Vadot 	uint32_t cs;
5043f9309e5SEmmanuel Vadot 	uint32_t mode;
5053f9309e5SEmmanuel Vadot 	int err = 0;
5063f9309e5SEmmanuel Vadot 
5073f9309e5SEmmanuel Vadot 	KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
5083f9309e5SEmmanuel Vadot 	    ("TX/RX command sizes should be equal"));
5093f9309e5SEmmanuel Vadot 	KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
5103f9309e5SEmmanuel Vadot 	    ("TX/RX data sizes should be equal"));
5113f9309e5SEmmanuel Vadot 
5123f9309e5SEmmanuel Vadot 	/* Get chip select and mode for this child. */
5133f9309e5SEmmanuel Vadot 	spibus_get_cs(child, &cs);
5143f9309e5SEmmanuel Vadot 	cs &= ~SPIBUS_CS_HIGH;
5153f9309e5SEmmanuel Vadot 	if (cs > 2) {
5163f9309e5SEmmanuel Vadot 		device_printf(dev, "Invalid chip select %d requested by %s",
5173f9309e5SEmmanuel Vadot 		    cs, device_get_nameunit(child));
5183f9309e5SEmmanuel Vadot 		return (EINVAL);
5193f9309e5SEmmanuel Vadot 	}
5203f9309e5SEmmanuel Vadot 	spibus_get_mode(child, &mode);
5213f9309e5SEmmanuel Vadot 
5223f9309e5SEmmanuel Vadot 	SPI_SC_LOCK(sc);
5233f9309e5SEmmanuel Vadot 
5243f9309e5SEmmanuel Vadot 	/* Wait for controller available. */
5253f9309e5SEmmanuel Vadot 	while (sc->busy != 0) {
5263f9309e5SEmmanuel Vadot 		err = mtx_sleep(dev, &sc->sc_mtx, 0, "zspi0", 0);
5273f9309e5SEmmanuel Vadot 		if (err) {
5283f9309e5SEmmanuel Vadot 			SPI_SC_UNLOCK(sc);
5293f9309e5SEmmanuel Vadot 			return (err);
5303f9309e5SEmmanuel Vadot 		}
5313f9309e5SEmmanuel Vadot 	}
5323f9309e5SEmmanuel Vadot 
5333f9309e5SEmmanuel Vadot 	/* Start transfer. */
5343f9309e5SEmmanuel Vadot 	sc->busy = 1;
5353f9309e5SEmmanuel Vadot 	sc->cmd = cmd;
5363f9309e5SEmmanuel Vadot 	sc->tx_bytes = sc->cmd->tx_cmd_sz + sc->cmd->tx_data_sz;
5373f9309e5SEmmanuel Vadot 	sc->tx_bytes_sent = 0;
5383f9309e5SEmmanuel Vadot 	sc->rx_bytes = sc->cmd->rx_cmd_sz + sc->cmd->rx_data_sz;
5393f9309e5SEmmanuel Vadot 	sc->rx_bytes_rcvd = 0;
5403f9309e5SEmmanuel Vadot 
5413f9309e5SEmmanuel Vadot 	/* Enable interrupts.  zy7_spi_intr() will handle transfer. */
5423f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_INTR_EN_REG,
5433f9309e5SEmmanuel Vadot 	    ZY7_SPI_INTR_TX_FIFO_NOT_FULL |
5443f9309e5SEmmanuel Vadot 	    ZY7_SPI_INTR_RX_OVERFLOW);
5453f9309e5SEmmanuel Vadot 
5463f9309e5SEmmanuel Vadot 	/* Handle polarity and phase. */
5473f9309e5SEmmanuel Vadot 	if (mode == SPIBUS_MODE_CPHA || mode == SPIBUS_MODE_CPOL_CPHA)
5483f9309e5SEmmanuel Vadot 		sc->cfg_reg_shadow |= ZY7_SPI_CONFIG_CLK_PH;
5493f9309e5SEmmanuel Vadot 	if (mode == SPIBUS_MODE_CPOL || mode == SPIBUS_MODE_CPOL_CPHA)
5503f9309e5SEmmanuel Vadot 		sc->cfg_reg_shadow |= ZY7_SPI_CONFIG_CLK_POL;
5513f9309e5SEmmanuel Vadot 
5523f9309e5SEmmanuel Vadot 	/* Assert CS. */
5533f9309e5SEmmanuel Vadot 	sc->cfg_reg_shadow &= ~ZY7_SPI_CONFIG_CS_MASK;
5543f9309e5SEmmanuel Vadot 	sc->cfg_reg_shadow |= ZY7_SPI_CONFIG_CS(cs);
5553f9309e5SEmmanuel Vadot 	WR4(sc, ZY7_SPI_CONFIG_REG, sc->cfg_reg_shadow);
5563f9309e5SEmmanuel Vadot 
5573f9309e5SEmmanuel Vadot 	/* Wait for completion. */
5583f9309e5SEmmanuel Vadot 	err = mtx_sleep(dev, &sc->sc_mtx, 0, "zspi1", hz * 2);
5593f9309e5SEmmanuel Vadot 	if (err)
5603f9309e5SEmmanuel Vadot 		zy7_spi_abort_transfer(sc);
5613f9309e5SEmmanuel Vadot 
5623f9309e5SEmmanuel Vadot 	/* Release controller. */
5633f9309e5SEmmanuel Vadot 	sc->busy = 0;
5643f9309e5SEmmanuel Vadot 	wakeup_one(dev);
5653f9309e5SEmmanuel Vadot 
5663f9309e5SEmmanuel Vadot 	SPI_SC_UNLOCK(sc);
5673f9309e5SEmmanuel Vadot 
5683f9309e5SEmmanuel Vadot 	return (err);
5693f9309e5SEmmanuel Vadot }
5703f9309e5SEmmanuel Vadot 
5713f9309e5SEmmanuel Vadot static device_method_t zy7_spi_methods[] = {
5723f9309e5SEmmanuel Vadot 	/* Device interface */
5733f9309e5SEmmanuel Vadot 	DEVMETHOD(device_probe,		zy7_spi_probe),
5743f9309e5SEmmanuel Vadot 	DEVMETHOD(device_attach,	zy7_spi_attach),
5753f9309e5SEmmanuel Vadot 	DEVMETHOD(device_detach,	zy7_spi_detach),
5763f9309e5SEmmanuel Vadot 
5773f9309e5SEmmanuel Vadot 	/* SPI interface */
5783f9309e5SEmmanuel Vadot 	DEVMETHOD(spibus_transfer,	zy7_spi_transfer),
5793f9309e5SEmmanuel Vadot 
5803f9309e5SEmmanuel Vadot 	/* ofw_bus interface */
5813f9309e5SEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	zy7_spi_get_node),
5823f9309e5SEmmanuel Vadot 
5833f9309e5SEmmanuel Vadot 	DEVMETHOD_END
5843f9309e5SEmmanuel Vadot };
5853f9309e5SEmmanuel Vadot 
5863f9309e5SEmmanuel Vadot static driver_t zy7_spi_driver = {
5873f9309e5SEmmanuel Vadot 	"zy7_spi",
5883f9309e5SEmmanuel Vadot 	zy7_spi_methods,
5893f9309e5SEmmanuel Vadot 	sizeof(struct zy7_spi_softc),
5903f9309e5SEmmanuel Vadot };
5913f9309e5SEmmanuel Vadot 
592*680ccae3SJohn Baldwin DRIVER_MODULE(zy7_spi, simplebus, zy7_spi_driver, 0, 0);
5935f31d14aSJohn Baldwin DRIVER_MODULE(ofw_spibus, zy7_spi, ofw_spibus_driver, 0, 0);
5943f9309e5SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
5953f9309e5SEmmanuel Vadot MODULE_DEPEND(zy7_spi, ofw_spibus, 1, 1, 1);
596