xref: /freebsd/sys/dev/spibus/controller/allwinner/aw_spi.c (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
1ec2b0ccdSEmmanuel Vadot /*-
2ec2b0ccdSEmmanuel Vadot  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
3ec2b0ccdSEmmanuel Vadot  *
4ec2b0ccdSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
5ec2b0ccdSEmmanuel Vadot  * modification, are permitted provided that the following conditions
6ec2b0ccdSEmmanuel Vadot  * are met:
7ec2b0ccdSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
8ec2b0ccdSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
9ec2b0ccdSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
10ec2b0ccdSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
11ec2b0ccdSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
12ec2b0ccdSEmmanuel Vadot  *
13ec2b0ccdSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14ec2b0ccdSEmmanuel Vadot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15ec2b0ccdSEmmanuel Vadot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16ec2b0ccdSEmmanuel Vadot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17ec2b0ccdSEmmanuel Vadot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18ec2b0ccdSEmmanuel Vadot  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19ec2b0ccdSEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20ec2b0ccdSEmmanuel Vadot  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21ec2b0ccdSEmmanuel Vadot  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ec2b0ccdSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ec2b0ccdSEmmanuel Vadot  * SUCH DAMAGE.
24ec2b0ccdSEmmanuel Vadot  */
25ec2b0ccdSEmmanuel Vadot 
26ec2b0ccdSEmmanuel Vadot #include <sys/param.h>
27ec2b0ccdSEmmanuel Vadot #include <sys/systm.h>
28ec2b0ccdSEmmanuel Vadot #include <sys/bus.h>
29ec2b0ccdSEmmanuel Vadot #include <sys/kernel.h>
30ec2b0ccdSEmmanuel Vadot #include <sys/lock.h>
31ec2b0ccdSEmmanuel Vadot #include <sys/module.h>
32ec2b0ccdSEmmanuel Vadot #include <sys/mutex.h>
33ec2b0ccdSEmmanuel Vadot #include <sys/rman.h>
34ec2b0ccdSEmmanuel Vadot #include <sys/resource.h>
35ec2b0ccdSEmmanuel Vadot #include <machine/bus.h>
36ec2b0ccdSEmmanuel Vadot 
37ec2b0ccdSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
38ec2b0ccdSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
39ec2b0ccdSEmmanuel Vadot 
40ec2b0ccdSEmmanuel Vadot #include <dev/spibus/spi.h>
41ec2b0ccdSEmmanuel Vadot #include <dev/spibus/spibusvar.h>
42ec2b0ccdSEmmanuel Vadot 
43be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
441f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
45ec2b0ccdSEmmanuel Vadot 
46ec2b0ccdSEmmanuel Vadot #include "spibus_if.h"
47ec2b0ccdSEmmanuel Vadot 
48ec2b0ccdSEmmanuel Vadot #define	AW_SPI_GCR		0x04		/* Global Control Register */
49ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_GCR_EN		(1 << 0)	/* ENable */
50ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_GCR_MODE_MASTER	(1 << 1)	/* 1 = Master, 0 = Slave */
51ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_GCR_TP_EN	(1 << 7)	/* 1 = Stop transmit when FIFO is full */
52ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_GCR_SRST	(1 << 31)	/* Soft Reset */
53ec2b0ccdSEmmanuel Vadot 
54ec2b0ccdSEmmanuel Vadot #define	AW_SPI_TCR		0x08		/* Transfer Control register */
55ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_XCH		(1 << 31)	/* Initiate transfer */
56ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SDDM	(1 << 14)	/* Sending Delay Data Mode */
57ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SDM		(1 << 13)	/* Master Sample Data Mode */
58ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_FBS		(1 << 12)	/* First Transmit Bit Select (1 == LSB) */
59ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SDC		(1 << 11)	/* Master Sample Data Control */
60ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_RPSM	(1 << 10)	/* Rapid Mode Select */
61ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_DDB		(1 << 9)	/* Dummy Burst Type */
62ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SSSEL_MASK	0x30		/* Chip select */
63ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SSSEL_SHIFT	4
64ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SS_LEVEL	(1 << 7)	/* 1 == CS High */
65ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SS_OWNER	(1 << 6)	/* 1 == Software controlled */
66ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_SPOL	(1 << 2)	/* 1 == Active low */
67ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_CPOL	(1 << 1)	/* 1 == Active low */
68ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_TCR_CPHA	(1 << 0)	/* 1 == Phase 1 */
69ec2b0ccdSEmmanuel Vadot 
70ec2b0ccdSEmmanuel Vadot #define	AW_SPI_IER		0x10		/* Interrupt Control Register */
71ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_SS		(1 << 13)	/* Chip select went from valid to invalid */
72ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TC		(1 << 12)	/* Transfer complete */
73ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TF_UDR	(1 << 11)	/* TXFIFO underrun */
74ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TF_OVF	(1 << 10)	/* TXFIFO overrun */
75ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_RF_UDR	(1 << 9)	/* RXFIFO underrun */
76ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_RF_OVF	(1 << 8)	/* RXFIFO overrun */
77ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TF_FULL	(1 << 6)	/* TXFIFO Full */
78ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TF_EMP	(1 << 5)	/* TXFIFO Empty */
79ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_TF_ERQ	(1 << 4)	/* TXFIFO Empty Request */
80ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_RF_FULL	(1 << 2)	/* RXFIFO Full */
81ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_RF_EMP	(1 << 1)	/* RXFIFO Empty */
82ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_IER_RF_RDY	(1 << 0)	/* RXFIFO Ready Request */
83ec2b0ccdSEmmanuel Vadot 
84ec2b0ccdSEmmanuel Vadot #define	AW_SPI_ISR		0x14		/* Interrupt Status Register */
85ec2b0ccdSEmmanuel Vadot 
86ec2b0ccdSEmmanuel Vadot #define	AW_SPI_FCR			0x18		/* FIFO Control Register */
87ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_TX_RST		(1 << 31)	/* Reset TX FIFO */
88ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_TX_TRIG_MASK	0xFF0000	/* TX FIFO Trigger level */
89ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_TX_TRIG_SHIFT	16
90ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_RX_RST	(1 << 15)		/* Reset RX FIFO */
91ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_RX_TRIG_MASK	0xFF		/* RX FIFO Trigger level */
92ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FCR_RX_TRIG_SHIFT	0
93ec2b0ccdSEmmanuel Vadot 
94ec2b0ccdSEmmanuel Vadot #define	AW_SPI_FSR	0x1C			/* FIFO Status Register */
95ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_TB_WR		(1 << 31)
96ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_TB_CNT_MASK		0x70000000
97ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_TB_CNT_SHIFT	28
98ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_TF_CNT_MASK		0xFF0000
99ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_TF_CNT_SHIFT	16
100ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_RB_WR		(1 << 15)
101ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_RB_CNT_MASK		0x7000
102ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_RB_CNT_SHIFT	12
103ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_RF_CNT_MASK		0xFF
104ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_FSR_RF_CNT_SHIFT	0
105ec2b0ccdSEmmanuel Vadot 
106ec2b0ccdSEmmanuel Vadot #define	AW_SPI_WCR	0x20	/* Wait Clock Counter Register */
107ec2b0ccdSEmmanuel Vadot 
108ec2b0ccdSEmmanuel Vadot #define	AW_SPI_CCR	0x24		/* Clock Rate Control Register */
109ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_CCR_DRS	(1 << 12)	/* Clock divider select */
110ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_CCR_CDR1_MASK	0xF00
111ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_CCR_CDR1_SHIFT	8
112ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_CCR_CDR2_MASK	0xFF
113ec2b0ccdSEmmanuel Vadot #define	 AW_SPI_CCR_CDR2_SHIFT	0
114ec2b0ccdSEmmanuel Vadot 
115ec2b0ccdSEmmanuel Vadot #define	AW_SPI_MBC	0x30	/* Burst Counter Register */
116ec2b0ccdSEmmanuel Vadot #define	AW_SPI_MTC	0x34	/* Transmit Counter Register */
117ec2b0ccdSEmmanuel Vadot #define	AW_SPI_BCC	0x38	/* Burst Control Register */
118ec2b0ccdSEmmanuel Vadot #define	AW_SPI_MDMA_CTL	0x88	/* Normal DMA Control Register */
119ec2b0ccdSEmmanuel Vadot #define	AW_SPI_TXD	0x200	/* TX Data Register */
120ec2b0ccdSEmmanuel Vadot #define	AW_SPI_RDX	0x300	/* RX Data Register */
121ec2b0ccdSEmmanuel Vadot 
122ec2b0ccdSEmmanuel Vadot #define	AW_SPI_MAX_CS		4
123ec2b0ccdSEmmanuel Vadot #define	AW_SPI_FIFO_SIZE	64
124ec2b0ccdSEmmanuel Vadot 
125ec2b0ccdSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
126ec2b0ccdSEmmanuel Vadot 	{ "allwinner,sun8i-h3-spi",		1 },
127ec2b0ccdSEmmanuel Vadot 	{ NULL,					0 }
128ec2b0ccdSEmmanuel Vadot };
129ec2b0ccdSEmmanuel Vadot 
130ec2b0ccdSEmmanuel Vadot static struct resource_spec aw_spi_spec[] = {
131ec2b0ccdSEmmanuel Vadot 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
132ec2b0ccdSEmmanuel Vadot 	{ SYS_RES_IRQ,		0,	RF_ACTIVE | RF_SHAREABLE },
133ec2b0ccdSEmmanuel Vadot 	{ -1, 0 }
134ec2b0ccdSEmmanuel Vadot };
135ec2b0ccdSEmmanuel Vadot 
136ec2b0ccdSEmmanuel Vadot struct aw_spi_softc {
137ec2b0ccdSEmmanuel Vadot 	device_t	dev;
138ec2b0ccdSEmmanuel Vadot 	device_t	spibus;
139ec2b0ccdSEmmanuel Vadot 	struct resource	*res[2];
140ec2b0ccdSEmmanuel Vadot 	struct mtx	mtx;
141ec2b0ccdSEmmanuel Vadot 	clk_t		clk_ahb;
142ec2b0ccdSEmmanuel Vadot 	clk_t		clk_mod;
143ec2b0ccdSEmmanuel Vadot 	uint64_t	mod_freq;
144ec2b0ccdSEmmanuel Vadot 	hwreset_t	rst_ahb;
145ec2b0ccdSEmmanuel Vadot 	void *		intrhand;
146ec2b0ccdSEmmanuel Vadot 	int		transfer;
147ec2b0ccdSEmmanuel Vadot 
148ec2b0ccdSEmmanuel Vadot 	uint8_t		*rxbuf;
149ec2b0ccdSEmmanuel Vadot 	uint32_t	rxcnt;
150ec2b0ccdSEmmanuel Vadot 	uint8_t		*txbuf;
151ec2b0ccdSEmmanuel Vadot 	uint32_t	txcnt;
152ec2b0ccdSEmmanuel Vadot 	uint32_t	txlen;
153ec2b0ccdSEmmanuel Vadot 	uint32_t	rxlen;
154ec2b0ccdSEmmanuel Vadot };
155ec2b0ccdSEmmanuel Vadot 
156ec2b0ccdSEmmanuel Vadot #define	AW_SPI_LOCK(sc)			mtx_lock(&(sc)->mtx)
157ec2b0ccdSEmmanuel Vadot #define	AW_SPI_UNLOCK(sc)		mtx_unlock(&(sc)->mtx)
158ec2b0ccdSEmmanuel Vadot #define	AW_SPI_ASSERT_LOCKED(sc)	mtx_assert(&(sc)->mtx, MA_OWNED)
159ec2b0ccdSEmmanuel Vadot #define	AW_SPI_READ_1(sc, reg)		bus_read_1((sc)->res[0], (reg))
160ec2b0ccdSEmmanuel Vadot #define	AW_SPI_WRITE_1(sc, reg, val)	bus_write_1((sc)->res[0], (reg), (val))
161ec2b0ccdSEmmanuel Vadot #define	AW_SPI_READ_4(sc, reg)		bus_read_4((sc)->res[0], (reg))
162ec2b0ccdSEmmanuel Vadot #define	AW_SPI_WRITE_4(sc, reg, val)	bus_write_4((sc)->res[0], (reg), (val))
163ec2b0ccdSEmmanuel Vadot 
164ec2b0ccdSEmmanuel Vadot static int aw_spi_probe(device_t dev);
165ec2b0ccdSEmmanuel Vadot static int aw_spi_attach(device_t dev);
166ec2b0ccdSEmmanuel Vadot static int aw_spi_detach(device_t dev);
167ec2b0ccdSEmmanuel Vadot static int aw_spi_intr(void *arg);
168ec2b0ccdSEmmanuel Vadot 
169ec2b0ccdSEmmanuel Vadot static int
170ec2b0ccdSEmmanuel Vadot aw_spi_probe(device_t dev)
171ec2b0ccdSEmmanuel Vadot {
172ec2b0ccdSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
173ec2b0ccdSEmmanuel Vadot 		return (ENXIO);
174ec2b0ccdSEmmanuel Vadot 
175ec2b0ccdSEmmanuel Vadot 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
176ec2b0ccdSEmmanuel Vadot 		return (ENXIO);
177ec2b0ccdSEmmanuel Vadot 
178ec2b0ccdSEmmanuel Vadot 	device_set_desc(dev, "Allwinner SPI");
179ec2b0ccdSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
180ec2b0ccdSEmmanuel Vadot }
181ec2b0ccdSEmmanuel Vadot 
182ec2b0ccdSEmmanuel Vadot static int
183ec2b0ccdSEmmanuel Vadot aw_spi_attach(device_t dev)
184ec2b0ccdSEmmanuel Vadot {
185ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
186ec2b0ccdSEmmanuel Vadot 	int error;
187ec2b0ccdSEmmanuel Vadot 
188ec2b0ccdSEmmanuel Vadot 	sc = device_get_softc(dev);
189ec2b0ccdSEmmanuel Vadot 	sc->dev = dev;
190ec2b0ccdSEmmanuel Vadot 
191ec2b0ccdSEmmanuel Vadot 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
192ec2b0ccdSEmmanuel Vadot 
193ec2b0ccdSEmmanuel Vadot 	if (bus_alloc_resources(dev, aw_spi_spec, sc->res) != 0) {
194ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot allocate resources for device\n");
195ec2b0ccdSEmmanuel Vadot 		error = ENXIO;
196ec2b0ccdSEmmanuel Vadot 		goto fail;
197ec2b0ccdSEmmanuel Vadot 	}
198ec2b0ccdSEmmanuel Vadot 
199ec2b0ccdSEmmanuel Vadot 	if (bus_setup_intr(dev, sc->res[1],
200ec2b0ccdSEmmanuel Vadot 	    INTR_TYPE_MISC | INTR_MPSAFE, aw_spi_intr, NULL, sc,
201ec2b0ccdSEmmanuel Vadot 	    &sc->intrhand)) {
202ec2b0ccdSEmmanuel Vadot 		bus_release_resources(dev, aw_spi_spec, sc->res);
203ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot setup interrupt handler\n");
204ec2b0ccdSEmmanuel Vadot 		return (ENXIO);
205ec2b0ccdSEmmanuel Vadot 	}
206ec2b0ccdSEmmanuel Vadot 
207ec2b0ccdSEmmanuel Vadot 	/* De-assert reset */
208ec2b0ccdSEmmanuel Vadot 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &sc->rst_ahb) == 0) {
209ec2b0ccdSEmmanuel Vadot 		error = hwreset_deassert(sc->rst_ahb);
210ec2b0ccdSEmmanuel Vadot 		if (error != 0) {
211ec2b0ccdSEmmanuel Vadot 			device_printf(dev, "cannot de-assert reset\n");
212ec2b0ccdSEmmanuel Vadot 			goto fail;
213ec2b0ccdSEmmanuel Vadot 		}
214ec2b0ccdSEmmanuel Vadot 	}
215ec2b0ccdSEmmanuel Vadot 
216ec2b0ccdSEmmanuel Vadot 	/* Activate the module clock. */
217ec2b0ccdSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->clk_ahb);
218ec2b0ccdSEmmanuel Vadot 	if (error != 0) {
219ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot get ahb clock\n");
220ec2b0ccdSEmmanuel Vadot 		goto fail;
221ec2b0ccdSEmmanuel Vadot 	}
222ec2b0ccdSEmmanuel Vadot 	error = clk_get_by_ofw_name(dev, 0, "mod", &sc->clk_mod);
223ec2b0ccdSEmmanuel Vadot 	if (error != 0) {
224ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot get mod clock\n");
225ec2b0ccdSEmmanuel Vadot 		goto fail;
226ec2b0ccdSEmmanuel Vadot 	}
227ec2b0ccdSEmmanuel Vadot 	error = clk_enable(sc->clk_ahb);
228ec2b0ccdSEmmanuel Vadot 	if (error != 0) {
229ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot enable ahb clock\n");
230ec2b0ccdSEmmanuel Vadot 		goto fail;
231ec2b0ccdSEmmanuel Vadot 	}
232ec2b0ccdSEmmanuel Vadot 	error = clk_enable(sc->clk_mod);
233ec2b0ccdSEmmanuel Vadot 	if (error != 0) {
234ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "cannot enable mod clock\n");
235ec2b0ccdSEmmanuel Vadot 		goto fail;
236ec2b0ccdSEmmanuel Vadot 	}
237ec2b0ccdSEmmanuel Vadot 
238*5b56413dSWarner Losh 	sc->spibus = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
239ec2b0ccdSEmmanuel Vadot 
240ec2b0ccdSEmmanuel Vadot 	return (bus_generic_attach(dev));
241ec2b0ccdSEmmanuel Vadot 
242ec2b0ccdSEmmanuel Vadot fail:
243ec2b0ccdSEmmanuel Vadot 	aw_spi_detach(dev);
244ec2b0ccdSEmmanuel Vadot 	return (error);
245ec2b0ccdSEmmanuel Vadot }
246ec2b0ccdSEmmanuel Vadot 
247ec2b0ccdSEmmanuel Vadot static int
248ec2b0ccdSEmmanuel Vadot aw_spi_detach(device_t dev)
249ec2b0ccdSEmmanuel Vadot {
250ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
251ec2b0ccdSEmmanuel Vadot 
252ec2b0ccdSEmmanuel Vadot 	sc = device_get_softc(dev);
253ec2b0ccdSEmmanuel Vadot 
254ec2b0ccdSEmmanuel Vadot 	bus_generic_detach(sc->dev);
255ec2b0ccdSEmmanuel Vadot 	if (sc->spibus != NULL)
256ec2b0ccdSEmmanuel Vadot 		device_delete_child(dev, sc->spibus);
257ec2b0ccdSEmmanuel Vadot 
258ec2b0ccdSEmmanuel Vadot 	if (sc->clk_mod != NULL)
259ec2b0ccdSEmmanuel Vadot 		clk_release(sc->clk_mod);
260ec2b0ccdSEmmanuel Vadot 	if (sc->clk_ahb)
261ec2b0ccdSEmmanuel Vadot 		clk_release(sc->clk_ahb);
262ec2b0ccdSEmmanuel Vadot 	if (sc->rst_ahb)
263ec2b0ccdSEmmanuel Vadot 		hwreset_assert(sc->rst_ahb);
264ec2b0ccdSEmmanuel Vadot 
265ec2b0ccdSEmmanuel Vadot 	if (sc->intrhand != NULL)
266ec2b0ccdSEmmanuel Vadot 		bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand);
267ec2b0ccdSEmmanuel Vadot 
268ec2b0ccdSEmmanuel Vadot 	bus_release_resources(dev, aw_spi_spec, sc->res);
269ec2b0ccdSEmmanuel Vadot 	mtx_destroy(&sc->mtx);
270ec2b0ccdSEmmanuel Vadot 
271ec2b0ccdSEmmanuel Vadot 	return (0);
272ec2b0ccdSEmmanuel Vadot }
273ec2b0ccdSEmmanuel Vadot 
274ec2b0ccdSEmmanuel Vadot static phandle_t
275ec2b0ccdSEmmanuel Vadot aw_spi_get_node(device_t bus, device_t dev)
276ec2b0ccdSEmmanuel Vadot {
277ec2b0ccdSEmmanuel Vadot 
278ec2b0ccdSEmmanuel Vadot 	return ofw_bus_get_node(bus);
279ec2b0ccdSEmmanuel Vadot }
280ec2b0ccdSEmmanuel Vadot 
281ec2b0ccdSEmmanuel Vadot static void
282ec2b0ccdSEmmanuel Vadot aw_spi_setup_mode(struct aw_spi_softc *sc, uint32_t mode)
283ec2b0ccdSEmmanuel Vadot {
284ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
285ec2b0ccdSEmmanuel Vadot 
286ec2b0ccdSEmmanuel Vadot 	/* We only support master mode */
287ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
288ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_GCR_MODE_MASTER;
289ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
290ec2b0ccdSEmmanuel Vadot 
291ec2b0ccdSEmmanuel Vadot 	/* Setup the modes */
292ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
293ec2b0ccdSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPHA)
294ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_CPHA;
295ec2b0ccdSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPOL)
296ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_CPOL;
297ec2b0ccdSEmmanuel Vadot 
298ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
299ec2b0ccdSEmmanuel Vadot }
300ec2b0ccdSEmmanuel Vadot 
301ec2b0ccdSEmmanuel Vadot static void
302ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(struct aw_spi_softc *sc, uint32_t cs, bool low)
303ec2b0ccdSEmmanuel Vadot {
304ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
305ec2b0ccdSEmmanuel Vadot 
306ec2b0ccdSEmmanuel Vadot 	/* Setup CS */
307ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
308ec2b0ccdSEmmanuel Vadot 	reg &= ~(AW_SPI_TCR_SSSEL_MASK);
309ec2b0ccdSEmmanuel Vadot 	reg |= cs << AW_SPI_TCR_SSSEL_SHIFT;
310ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_TCR_SS_OWNER;
311ec2b0ccdSEmmanuel Vadot 	if (low)
312ec2b0ccdSEmmanuel Vadot 		reg &= ~(AW_SPI_TCR_SS_LEVEL);
313ec2b0ccdSEmmanuel Vadot 	else
314ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_SS_LEVEL;
315ec2b0ccdSEmmanuel Vadot 
316ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
317ec2b0ccdSEmmanuel Vadot }
318ec2b0ccdSEmmanuel Vadot 
319ec2b0ccdSEmmanuel Vadot static uint64_t
320ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr1(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr)
321ec2b0ccdSEmmanuel Vadot {
322ec2b0ccdSEmmanuel Vadot 	uint64_t cur, best = 0;
323ec2b0ccdSEmmanuel Vadot 	int i, max, best_div;
324ec2b0ccdSEmmanuel Vadot 
325ec2b0ccdSEmmanuel Vadot 	max = AW_SPI_CCR_CDR1_MASK >> AW_SPI_CCR_CDR1_SHIFT;
326ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < max; i++) {
327ec2b0ccdSEmmanuel Vadot 		cur = sc->mod_freq / (1 << i);
328ec2b0ccdSEmmanuel Vadot 		if ((clock - cur) < (clock - best)) {
329ec2b0ccdSEmmanuel Vadot 			best = cur;
330ec2b0ccdSEmmanuel Vadot 			best_div = i;
331ec2b0ccdSEmmanuel Vadot 		}
332ec2b0ccdSEmmanuel Vadot 	}
333ec2b0ccdSEmmanuel Vadot 
334ec2b0ccdSEmmanuel Vadot 	*ccr = (best_div << AW_SPI_CCR_CDR1_SHIFT);
335ec2b0ccdSEmmanuel Vadot 	return (best);
336ec2b0ccdSEmmanuel Vadot }
337ec2b0ccdSEmmanuel Vadot 
338ec2b0ccdSEmmanuel Vadot static uint64_t
339ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr2(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr)
340ec2b0ccdSEmmanuel Vadot {
341ec2b0ccdSEmmanuel Vadot 	uint64_t cur, best = 0;
342ec2b0ccdSEmmanuel Vadot 	int i, max, best_div;
343ec2b0ccdSEmmanuel Vadot 
344ec2b0ccdSEmmanuel Vadot 	max = ((AW_SPI_CCR_CDR2_MASK) >> AW_SPI_CCR_CDR2_SHIFT);
345ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < max; i++) {
346ec2b0ccdSEmmanuel Vadot 		cur = sc->mod_freq / (2 * i + 1);
347ec2b0ccdSEmmanuel Vadot 		if ((clock - cur) < (clock - best)) {
348ec2b0ccdSEmmanuel Vadot 			best = cur;
349ec2b0ccdSEmmanuel Vadot 			best_div = i;
350ec2b0ccdSEmmanuel Vadot 		}
351ec2b0ccdSEmmanuel Vadot 	}
352ec2b0ccdSEmmanuel Vadot 
353ec2b0ccdSEmmanuel Vadot 	*ccr = AW_SPI_CCR_DRS | (best_div << AW_SPI_CCR_CDR2_SHIFT);
354ec2b0ccdSEmmanuel Vadot 	return (best);
355ec2b0ccdSEmmanuel Vadot }
356ec2b0ccdSEmmanuel Vadot 
357ec2b0ccdSEmmanuel Vadot static void
358ec2b0ccdSEmmanuel Vadot aw_spi_setup_clock(struct aw_spi_softc *sc, uint64_t clock)
359ec2b0ccdSEmmanuel Vadot {
360ec2b0ccdSEmmanuel Vadot 	uint64_t best_ccr1, best_ccr2;
361ec2b0ccdSEmmanuel Vadot 	uint32_t ccr, ccr1, ccr2;
362ec2b0ccdSEmmanuel Vadot 
363ec2b0ccdSEmmanuel Vadot 	best_ccr1 = aw_spi_clock_test_cdr1(sc, clock, &ccr1);
364ec2b0ccdSEmmanuel Vadot 	best_ccr2 = aw_spi_clock_test_cdr2(sc, clock, &ccr2);
365ec2b0ccdSEmmanuel Vadot 
366ec2b0ccdSEmmanuel Vadot 	if (best_ccr1 == clock) {
367ec2b0ccdSEmmanuel Vadot 		ccr = ccr1;
368ec2b0ccdSEmmanuel Vadot 	} else if (best_ccr2 == clock) {
369ec2b0ccdSEmmanuel Vadot 		ccr = ccr2;
370ec2b0ccdSEmmanuel Vadot 	} else {
371ec2b0ccdSEmmanuel Vadot 		if ((clock - best_ccr1) < (clock - best_ccr2))
372ec2b0ccdSEmmanuel Vadot 			ccr = ccr1;
373ec2b0ccdSEmmanuel Vadot 		else
374ec2b0ccdSEmmanuel Vadot 			ccr = ccr2;
375ec2b0ccdSEmmanuel Vadot 	}
376ec2b0ccdSEmmanuel Vadot 
377ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_CCR, ccr);
378ec2b0ccdSEmmanuel Vadot }
379ec2b0ccdSEmmanuel Vadot 
380ec2b0ccdSEmmanuel Vadot static inline void
381ec2b0ccdSEmmanuel Vadot aw_spi_fill_txfifo(struct aw_spi_softc *sc)
382ec2b0ccdSEmmanuel Vadot {
383ec2b0ccdSEmmanuel Vadot 	uint32_t reg, txcnt;
384ec2b0ccdSEmmanuel Vadot 	int i;
385ec2b0ccdSEmmanuel Vadot 
386ec2b0ccdSEmmanuel Vadot 	if (sc->txcnt == sc->txlen)
387ec2b0ccdSEmmanuel Vadot 		return;
388ec2b0ccdSEmmanuel Vadot 
389ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_FSR);
390ec2b0ccdSEmmanuel Vadot 	reg &= AW_SPI_FSR_TF_CNT_MASK;
391ec2b0ccdSEmmanuel Vadot 	txcnt = reg >> AW_SPI_FSR_TF_CNT_SHIFT;
392ec2b0ccdSEmmanuel Vadot 
393ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < (AW_SPI_FIFO_SIZE - txcnt); i++) {
394ec2b0ccdSEmmanuel Vadot 		AW_SPI_WRITE_1(sc, AW_SPI_TXD, sc->txbuf[sc->txcnt++]);
395ec2b0ccdSEmmanuel Vadot 		if (sc->txcnt == sc->txlen)
396ec2b0ccdSEmmanuel Vadot 			break;
397ec2b0ccdSEmmanuel Vadot 	}
398ec2b0ccdSEmmanuel Vadot 
399ec2b0ccdSEmmanuel Vadot 	return;
400ec2b0ccdSEmmanuel Vadot }
401ec2b0ccdSEmmanuel Vadot 
402ec2b0ccdSEmmanuel Vadot static inline void
403ec2b0ccdSEmmanuel Vadot aw_spi_read_rxfifo(struct aw_spi_softc *sc)
404ec2b0ccdSEmmanuel Vadot {
405ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
406ec2b0ccdSEmmanuel Vadot 	uint8_t val;
407ec2b0ccdSEmmanuel Vadot 	int i;
408ec2b0ccdSEmmanuel Vadot 
409ec2b0ccdSEmmanuel Vadot 	if (sc->rxcnt == sc->rxlen)
410ec2b0ccdSEmmanuel Vadot 		return;
411ec2b0ccdSEmmanuel Vadot 
412ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_FSR);
413ec2b0ccdSEmmanuel Vadot 	reg = (reg & AW_SPI_FSR_RF_CNT_MASK) >> AW_SPI_FSR_RF_CNT_SHIFT;
414ec2b0ccdSEmmanuel Vadot 
415ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < reg; i++) {
416ec2b0ccdSEmmanuel Vadot 		val = AW_SPI_READ_1(sc, AW_SPI_RDX);
417ec2b0ccdSEmmanuel Vadot 		if (sc->rxcnt < sc->rxlen)
418ec2b0ccdSEmmanuel Vadot 			sc->rxbuf[sc->rxcnt++] = val;
419ec2b0ccdSEmmanuel Vadot 	}
420ec2b0ccdSEmmanuel Vadot }
421ec2b0ccdSEmmanuel Vadot 
422ec2b0ccdSEmmanuel Vadot static int
423ec2b0ccdSEmmanuel Vadot aw_spi_intr(void *arg)
424ec2b0ccdSEmmanuel Vadot {
425ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
426ec2b0ccdSEmmanuel Vadot 	uint32_t intr;
427ec2b0ccdSEmmanuel Vadot 
428ec2b0ccdSEmmanuel Vadot 	sc = (struct aw_spi_softc *)arg;
429ec2b0ccdSEmmanuel Vadot 
430ec2b0ccdSEmmanuel Vadot 	intr = AW_SPI_READ_4(sc, AW_SPI_ISR);
431ec2b0ccdSEmmanuel Vadot 
432ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_RF_RDY)
433ec2b0ccdSEmmanuel Vadot 		aw_spi_read_rxfifo(sc);
434ec2b0ccdSEmmanuel Vadot 
435ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_TF_ERQ) {
436ec2b0ccdSEmmanuel Vadot 		aw_spi_fill_txfifo(sc);
437ec2b0ccdSEmmanuel Vadot 
438ec2b0ccdSEmmanuel Vadot 		/*
439ec2b0ccdSEmmanuel Vadot 		 * If we don't have anything else to write
440ec2b0ccdSEmmanuel Vadot 		 * disable TXFifo interrupts
441ec2b0ccdSEmmanuel Vadot 		 */
442ec2b0ccdSEmmanuel Vadot 		if (sc->txcnt == sc->txlen)
443ec2b0ccdSEmmanuel Vadot 			AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC |
444ec2b0ccdSEmmanuel Vadot 			    AW_SPI_IER_RF_RDY);
445ec2b0ccdSEmmanuel Vadot 	}
446ec2b0ccdSEmmanuel Vadot 
447ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_TC) {
448ec2b0ccdSEmmanuel Vadot 		/* read the rest of the data from the fifo */
449ec2b0ccdSEmmanuel Vadot 		aw_spi_read_rxfifo(sc);
450ec2b0ccdSEmmanuel Vadot 
451ec2b0ccdSEmmanuel Vadot 		/* Disable the interrupts */
452ec2b0ccdSEmmanuel Vadot 		AW_SPI_WRITE_4(sc, AW_SPI_IER, 0);
453ec2b0ccdSEmmanuel Vadot 		sc->transfer = 0;
454ec2b0ccdSEmmanuel Vadot 		wakeup(sc);
455ec2b0ccdSEmmanuel Vadot 	}
456ec2b0ccdSEmmanuel Vadot 
457ec2b0ccdSEmmanuel Vadot 	/* Clear Interrupts */
458ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_ISR, intr);
459ec2b0ccdSEmmanuel Vadot 	return (intr != 0 ? FILTER_HANDLED : FILTER_STRAY);
460ec2b0ccdSEmmanuel Vadot }
461ec2b0ccdSEmmanuel Vadot 
462ec2b0ccdSEmmanuel Vadot static int
463ec2b0ccdSEmmanuel Vadot aw_spi_xfer(struct aw_spi_softc *sc, void *rxbuf, void *txbuf, uint32_t txlen, uint32_t rxlen)
464ec2b0ccdSEmmanuel Vadot {
465ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
466ec2b0ccdSEmmanuel Vadot 	int error = 0, timeout;
467ec2b0ccdSEmmanuel Vadot 
468ec2b0ccdSEmmanuel Vadot 	sc->rxbuf = rxbuf;
469ec2b0ccdSEmmanuel Vadot 	sc->rxcnt = 0;
470ec2b0ccdSEmmanuel Vadot 	sc->txbuf = txbuf;
471ec2b0ccdSEmmanuel Vadot 	sc->txcnt = 0;
472ec2b0ccdSEmmanuel Vadot 	sc->txlen = txlen;
473ec2b0ccdSEmmanuel Vadot 	sc->rxlen = rxlen;
474ec2b0ccdSEmmanuel Vadot 
475ec2b0ccdSEmmanuel Vadot 	/* Reset the FIFOs */
476ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_FCR, AW_SPI_FCR_TX_RST | AW_SPI_FCR_RX_RST);
477ec2b0ccdSEmmanuel Vadot 
478ec2b0ccdSEmmanuel Vadot 	for (timeout = 1000; timeout > 0; timeout--) {
479ec2b0ccdSEmmanuel Vadot 		reg = AW_SPI_READ_4(sc, AW_SPI_FCR);
480ec2b0ccdSEmmanuel Vadot 		if (reg == 0)
481ec2b0ccdSEmmanuel Vadot 			break;
482ec2b0ccdSEmmanuel Vadot 	}
483ec2b0ccdSEmmanuel Vadot 	if (timeout == 0) {
484ec2b0ccdSEmmanuel Vadot 		device_printf(sc->dev, "Cannot reset the FIFOs\n");
485ec2b0ccdSEmmanuel Vadot 		return (EIO);
486ec2b0ccdSEmmanuel Vadot 	}
487ec2b0ccdSEmmanuel Vadot 
488ec2b0ccdSEmmanuel Vadot 	/*
489ec2b0ccdSEmmanuel Vadot 	 * Set the TX FIFO threshold to 3/4-th the size and
490ec2b0ccdSEmmanuel Vadot 	 * the RX FIFO one to 1/4-th.
491ec2b0ccdSEmmanuel Vadot 	 */
492ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_FCR,
493ec2b0ccdSEmmanuel Vadot 	    ((3 * AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_TX_TRIG_SHIFT) |
494ec2b0ccdSEmmanuel Vadot 	    ((AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_RX_TRIG_SHIFT));
495ec2b0ccdSEmmanuel Vadot 
496ec2b0ccdSEmmanuel Vadot 	/* Write the counters */
497ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_MBC, txlen);
498ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_MTC, txlen);
499ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_BCC, txlen);
500ec2b0ccdSEmmanuel Vadot 
501ec2b0ccdSEmmanuel Vadot 	/* First fill */
502ec2b0ccdSEmmanuel Vadot 	aw_spi_fill_txfifo(sc);
503ec2b0ccdSEmmanuel Vadot 
504ec2b0ccdSEmmanuel Vadot 	/* Start transmit */
505ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
506ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_TCR_XCH;
507ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
508ec2b0ccdSEmmanuel Vadot 
509ec2b0ccdSEmmanuel Vadot 	/*
510ec2b0ccdSEmmanuel Vadot 	 * Enable interrupts for :
511ec2b0ccdSEmmanuel Vadot 	 * Transmit complete
512ec2b0ccdSEmmanuel Vadot 	 * TX Fifo is below its trigger threshold
513ec2b0ccdSEmmanuel Vadot 	 * RX Fifo is above its trigger threshold
514ec2b0ccdSEmmanuel Vadot 	 */
515ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC |
516ec2b0ccdSEmmanuel Vadot 	    AW_SPI_IER_TF_ERQ | AW_SPI_IER_RF_RDY);
517ec2b0ccdSEmmanuel Vadot 
518ec2b0ccdSEmmanuel Vadot 	sc->transfer = 1;
519ec2b0ccdSEmmanuel Vadot 
520ec2b0ccdSEmmanuel Vadot 	while (error == 0 && sc->transfer != 0)
521ec2b0ccdSEmmanuel Vadot 		error = msleep(sc, &sc->mtx, 0, "aw_spi", 10 * hz);
522ec2b0ccdSEmmanuel Vadot 
523ec2b0ccdSEmmanuel Vadot 	return (0);
524ec2b0ccdSEmmanuel Vadot }
525ec2b0ccdSEmmanuel Vadot 
526ec2b0ccdSEmmanuel Vadot static int
527ec2b0ccdSEmmanuel Vadot aw_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
528ec2b0ccdSEmmanuel Vadot {
529ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
530ec2b0ccdSEmmanuel Vadot 	uint32_t cs, mode, clock, reg;
531ec2b0ccdSEmmanuel Vadot 	int err = 0;
532ec2b0ccdSEmmanuel Vadot 
533ec2b0ccdSEmmanuel Vadot 	sc = device_get_softc(dev);
534ec2b0ccdSEmmanuel Vadot 
535ec2b0ccdSEmmanuel Vadot 	spibus_get_cs(child, &cs);
536ec2b0ccdSEmmanuel Vadot 	spibus_get_clock(child, &clock);
537ec2b0ccdSEmmanuel Vadot 	spibus_get_mode(child, &mode);
538ec2b0ccdSEmmanuel Vadot 
539ec2b0ccdSEmmanuel Vadot 	/* The minimum divider is 2 so set the clock at twice the needed speed */
540ec2b0ccdSEmmanuel Vadot 	clk_set_freq(sc->clk_mod, 2 * clock, CLK_SET_ROUND_DOWN);
541ec2b0ccdSEmmanuel Vadot 	clk_get_freq(sc->clk_mod, &sc->mod_freq);
542ec2b0ccdSEmmanuel Vadot 	if (cs >= AW_SPI_MAX_CS) {
543ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "Invalid cs %d\n", cs);
544ec2b0ccdSEmmanuel Vadot 		return (EINVAL);
545ec2b0ccdSEmmanuel Vadot 	}
546ec2b0ccdSEmmanuel Vadot 
547ec2b0ccdSEmmanuel Vadot 	mtx_lock(&sc->mtx);
548ec2b0ccdSEmmanuel Vadot 
549ec2b0ccdSEmmanuel Vadot 	/* Enable and reset the module */
550ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
551ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_GCR_EN | AW_SPI_GCR_SRST;
552ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
553ec2b0ccdSEmmanuel Vadot 
554ec2b0ccdSEmmanuel Vadot 	/* Setup clock, CS and mode */
555ec2b0ccdSEmmanuel Vadot 	aw_spi_setup_clock(sc, clock);
556ec2b0ccdSEmmanuel Vadot 	aw_spi_setup_mode(sc, mode);
557ec2b0ccdSEmmanuel Vadot 	if (cs & SPIBUS_CS_HIGH)
558ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, false);
559ec2b0ccdSEmmanuel Vadot 	else
560ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, true);
561ec2b0ccdSEmmanuel Vadot 
562ec2b0ccdSEmmanuel Vadot 	/* xfer */
563ec2b0ccdSEmmanuel Vadot 	err = 0;
564ec2b0ccdSEmmanuel Vadot 	if (cmd->tx_cmd_sz > 0)
565ec2b0ccdSEmmanuel Vadot 		err = aw_spi_xfer(sc, cmd->rx_cmd, cmd->tx_cmd,
566ec2b0ccdSEmmanuel Vadot 		    cmd->tx_cmd_sz, cmd->rx_cmd_sz);
567ec2b0ccdSEmmanuel Vadot 	if (cmd->tx_data_sz > 0 && err == 0)
568ec2b0ccdSEmmanuel Vadot 		err = aw_spi_xfer(sc, cmd->rx_data, cmd->tx_data,
569ec2b0ccdSEmmanuel Vadot 		    cmd->tx_data_sz, cmd->rx_data_sz);
570ec2b0ccdSEmmanuel Vadot 
571ec2b0ccdSEmmanuel Vadot 	if (cs & SPIBUS_CS_HIGH)
572ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, true);
573ec2b0ccdSEmmanuel Vadot 	else
574ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, false);
575ec2b0ccdSEmmanuel Vadot 
576ec2b0ccdSEmmanuel Vadot 	/* Disable the module */
577ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
578ec2b0ccdSEmmanuel Vadot 	reg &= ~AW_SPI_GCR_EN;
579ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
580ec2b0ccdSEmmanuel Vadot 
581ec2b0ccdSEmmanuel Vadot 	mtx_unlock(&sc->mtx);
582ec2b0ccdSEmmanuel Vadot 
583ec2b0ccdSEmmanuel Vadot 	return (err);
584ec2b0ccdSEmmanuel Vadot }
585ec2b0ccdSEmmanuel Vadot 
586ec2b0ccdSEmmanuel Vadot static device_method_t aw_spi_methods[] = {
587ec2b0ccdSEmmanuel Vadot 	/* Device interface */
588ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_spi_probe),
589ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_spi_attach),
590ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_detach,	aw_spi_detach),
591ec2b0ccdSEmmanuel Vadot 
592ec2b0ccdSEmmanuel Vadot         /* spibus_if  */
593ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(spibus_transfer,	aw_spi_transfer),
594ec2b0ccdSEmmanuel Vadot 
595ec2b0ccdSEmmanuel Vadot         /* ofw_bus_if */
596ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	aw_spi_get_node),
597ec2b0ccdSEmmanuel Vadot 
598ec2b0ccdSEmmanuel Vadot 	DEVMETHOD_END
599ec2b0ccdSEmmanuel Vadot };
600ec2b0ccdSEmmanuel Vadot 
601ec2b0ccdSEmmanuel Vadot static driver_t aw_spi_driver = {
602ec2b0ccdSEmmanuel Vadot 	"aw_spi",
603ec2b0ccdSEmmanuel Vadot 	aw_spi_methods,
604ec2b0ccdSEmmanuel Vadot 	sizeof(struct aw_spi_softc),
605ec2b0ccdSEmmanuel Vadot };
606ec2b0ccdSEmmanuel Vadot 
607ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(aw_spi, simplebus, aw_spi_driver, 0, 0);
608ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(ofw_spibus, aw_spi, ofw_spibus_driver, 0, 0);
609ec2b0ccdSEmmanuel Vadot MODULE_DEPEND(aw_spi, ofw_spibus, 1, 1, 1);
610ec2b0ccdSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
611