xref: /freebsd/sys/dev/spibus/controller/allwinner/aw_spi.c (revision b196276c20b577b364372f1aa1a646b9ce34bf5c)
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
aw_spi_probe(device_t dev)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
aw_spi_attach(device_t dev)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 
2385b56413dSWarner Losh 	sc->spibus = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
239ec2b0ccdSEmmanuel Vadot 
240*18250ec6SJohn Baldwin 	bus_attach_children(dev);
241*18250ec6SJohn Baldwin 	return (0);
242ec2b0ccdSEmmanuel Vadot 
243ec2b0ccdSEmmanuel Vadot fail:
244ec2b0ccdSEmmanuel Vadot 	aw_spi_detach(dev);
245ec2b0ccdSEmmanuel Vadot 	return (error);
246ec2b0ccdSEmmanuel Vadot }
247ec2b0ccdSEmmanuel Vadot 
248ec2b0ccdSEmmanuel Vadot static int
aw_spi_detach(device_t dev)249ec2b0ccdSEmmanuel Vadot aw_spi_detach(device_t dev)
250ec2b0ccdSEmmanuel Vadot {
251ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
252ec2b0ccdSEmmanuel Vadot 
253ec2b0ccdSEmmanuel Vadot 	sc = device_get_softc(dev);
254ec2b0ccdSEmmanuel Vadot 
255ec2b0ccdSEmmanuel Vadot 	bus_generic_detach(sc->dev);
256ec2b0ccdSEmmanuel Vadot 
257ec2b0ccdSEmmanuel Vadot 	if (sc->clk_mod != NULL)
258ec2b0ccdSEmmanuel Vadot 		clk_release(sc->clk_mod);
259ec2b0ccdSEmmanuel Vadot 	if (sc->clk_ahb)
260ec2b0ccdSEmmanuel Vadot 		clk_release(sc->clk_ahb);
261ec2b0ccdSEmmanuel Vadot 	if (sc->rst_ahb)
262ec2b0ccdSEmmanuel Vadot 		hwreset_assert(sc->rst_ahb);
263ec2b0ccdSEmmanuel Vadot 
264ec2b0ccdSEmmanuel Vadot 	if (sc->intrhand != NULL)
265ec2b0ccdSEmmanuel Vadot 		bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand);
266ec2b0ccdSEmmanuel Vadot 
267ec2b0ccdSEmmanuel Vadot 	bus_release_resources(dev, aw_spi_spec, sc->res);
268ec2b0ccdSEmmanuel Vadot 	mtx_destroy(&sc->mtx);
269ec2b0ccdSEmmanuel Vadot 
270ec2b0ccdSEmmanuel Vadot 	return (0);
271ec2b0ccdSEmmanuel Vadot }
272ec2b0ccdSEmmanuel Vadot 
273ec2b0ccdSEmmanuel Vadot static phandle_t
aw_spi_get_node(device_t bus,device_t dev)274ec2b0ccdSEmmanuel Vadot aw_spi_get_node(device_t bus, device_t dev)
275ec2b0ccdSEmmanuel Vadot {
276ec2b0ccdSEmmanuel Vadot 
277ec2b0ccdSEmmanuel Vadot 	return ofw_bus_get_node(bus);
278ec2b0ccdSEmmanuel Vadot }
279ec2b0ccdSEmmanuel Vadot 
280ec2b0ccdSEmmanuel Vadot static void
aw_spi_setup_mode(struct aw_spi_softc * sc,uint32_t mode)281ec2b0ccdSEmmanuel Vadot aw_spi_setup_mode(struct aw_spi_softc *sc, uint32_t mode)
282ec2b0ccdSEmmanuel Vadot {
283ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
284ec2b0ccdSEmmanuel Vadot 
285ec2b0ccdSEmmanuel Vadot 	/* We only support master mode */
286ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
287ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_GCR_MODE_MASTER;
288ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
289ec2b0ccdSEmmanuel Vadot 
290ec2b0ccdSEmmanuel Vadot 	/* Setup the modes */
291ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
292ec2b0ccdSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPHA)
293ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_CPHA;
294ec2b0ccdSEmmanuel Vadot 	if (mode & SPIBUS_MODE_CPOL)
295ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_CPOL;
296ec2b0ccdSEmmanuel Vadot 
297ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
298ec2b0ccdSEmmanuel Vadot }
299ec2b0ccdSEmmanuel Vadot 
300ec2b0ccdSEmmanuel Vadot static void
aw_spi_setup_cs(struct aw_spi_softc * sc,uint32_t cs,bool low)301ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(struct aw_spi_softc *sc, uint32_t cs, bool low)
302ec2b0ccdSEmmanuel Vadot {
303ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
304ec2b0ccdSEmmanuel Vadot 
305ec2b0ccdSEmmanuel Vadot 	/* Setup CS */
306ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
307ec2b0ccdSEmmanuel Vadot 	reg &= ~(AW_SPI_TCR_SSSEL_MASK);
308ec2b0ccdSEmmanuel Vadot 	reg |= cs << AW_SPI_TCR_SSSEL_SHIFT;
309ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_TCR_SS_OWNER;
310ec2b0ccdSEmmanuel Vadot 	if (low)
311ec2b0ccdSEmmanuel Vadot 		reg &= ~(AW_SPI_TCR_SS_LEVEL);
312ec2b0ccdSEmmanuel Vadot 	else
313ec2b0ccdSEmmanuel Vadot 		reg |= AW_SPI_TCR_SS_LEVEL;
314ec2b0ccdSEmmanuel Vadot 
315ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
316ec2b0ccdSEmmanuel Vadot }
317ec2b0ccdSEmmanuel Vadot 
318ec2b0ccdSEmmanuel Vadot static uint64_t
aw_spi_clock_test_cdr1(struct aw_spi_softc * sc,uint64_t clock,uint32_t * ccr)319ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr1(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr)
320ec2b0ccdSEmmanuel Vadot {
321ec2b0ccdSEmmanuel Vadot 	uint64_t cur, best = 0;
322ec2b0ccdSEmmanuel Vadot 	int i, max, best_div;
323ec2b0ccdSEmmanuel Vadot 
324ec2b0ccdSEmmanuel Vadot 	max = AW_SPI_CCR_CDR1_MASK >> AW_SPI_CCR_CDR1_SHIFT;
325ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < max; i++) {
326ec2b0ccdSEmmanuel Vadot 		cur = sc->mod_freq / (1 << i);
327ec2b0ccdSEmmanuel Vadot 		if ((clock - cur) < (clock - best)) {
328ec2b0ccdSEmmanuel Vadot 			best = cur;
329ec2b0ccdSEmmanuel Vadot 			best_div = i;
330ec2b0ccdSEmmanuel Vadot 		}
331ec2b0ccdSEmmanuel Vadot 	}
332ec2b0ccdSEmmanuel Vadot 
333ec2b0ccdSEmmanuel Vadot 	*ccr = (best_div << AW_SPI_CCR_CDR1_SHIFT);
334ec2b0ccdSEmmanuel Vadot 	return (best);
335ec2b0ccdSEmmanuel Vadot }
336ec2b0ccdSEmmanuel Vadot 
337ec2b0ccdSEmmanuel Vadot static uint64_t
aw_spi_clock_test_cdr2(struct aw_spi_softc * sc,uint64_t clock,uint32_t * ccr)338ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr2(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr)
339ec2b0ccdSEmmanuel Vadot {
340ec2b0ccdSEmmanuel Vadot 	uint64_t cur, best = 0;
341ec2b0ccdSEmmanuel Vadot 	int i, max, best_div;
342ec2b0ccdSEmmanuel Vadot 
343ec2b0ccdSEmmanuel Vadot 	max = ((AW_SPI_CCR_CDR2_MASK) >> AW_SPI_CCR_CDR2_SHIFT);
344ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < max; i++) {
345ec2b0ccdSEmmanuel Vadot 		cur = sc->mod_freq / (2 * i + 1);
346ec2b0ccdSEmmanuel Vadot 		if ((clock - cur) < (clock - best)) {
347ec2b0ccdSEmmanuel Vadot 			best = cur;
348ec2b0ccdSEmmanuel Vadot 			best_div = i;
349ec2b0ccdSEmmanuel Vadot 		}
350ec2b0ccdSEmmanuel Vadot 	}
351ec2b0ccdSEmmanuel Vadot 
352ec2b0ccdSEmmanuel Vadot 	*ccr = AW_SPI_CCR_DRS | (best_div << AW_SPI_CCR_CDR2_SHIFT);
353ec2b0ccdSEmmanuel Vadot 	return (best);
354ec2b0ccdSEmmanuel Vadot }
355ec2b0ccdSEmmanuel Vadot 
356ec2b0ccdSEmmanuel Vadot static void
aw_spi_setup_clock(struct aw_spi_softc * sc,uint64_t clock)357ec2b0ccdSEmmanuel Vadot aw_spi_setup_clock(struct aw_spi_softc *sc, uint64_t clock)
358ec2b0ccdSEmmanuel Vadot {
359ec2b0ccdSEmmanuel Vadot 	uint64_t best_ccr1, best_ccr2;
360ec2b0ccdSEmmanuel Vadot 	uint32_t ccr, ccr1, ccr2;
361ec2b0ccdSEmmanuel Vadot 
362ec2b0ccdSEmmanuel Vadot 	best_ccr1 = aw_spi_clock_test_cdr1(sc, clock, &ccr1);
363ec2b0ccdSEmmanuel Vadot 	best_ccr2 = aw_spi_clock_test_cdr2(sc, clock, &ccr2);
364ec2b0ccdSEmmanuel Vadot 
365ec2b0ccdSEmmanuel Vadot 	if (best_ccr1 == clock) {
366ec2b0ccdSEmmanuel Vadot 		ccr = ccr1;
367ec2b0ccdSEmmanuel Vadot 	} else if (best_ccr2 == clock) {
368ec2b0ccdSEmmanuel Vadot 		ccr = ccr2;
369ec2b0ccdSEmmanuel Vadot 	} else {
370ec2b0ccdSEmmanuel Vadot 		if ((clock - best_ccr1) < (clock - best_ccr2))
371ec2b0ccdSEmmanuel Vadot 			ccr = ccr1;
372ec2b0ccdSEmmanuel Vadot 		else
373ec2b0ccdSEmmanuel Vadot 			ccr = ccr2;
374ec2b0ccdSEmmanuel Vadot 	}
375ec2b0ccdSEmmanuel Vadot 
376ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_CCR, ccr);
377ec2b0ccdSEmmanuel Vadot }
378ec2b0ccdSEmmanuel Vadot 
379ec2b0ccdSEmmanuel Vadot static inline void
aw_spi_fill_txfifo(struct aw_spi_softc * sc)380ec2b0ccdSEmmanuel Vadot aw_spi_fill_txfifo(struct aw_spi_softc *sc)
381ec2b0ccdSEmmanuel Vadot {
382ec2b0ccdSEmmanuel Vadot 	uint32_t reg, txcnt;
383ec2b0ccdSEmmanuel Vadot 	int i;
384ec2b0ccdSEmmanuel Vadot 
385ec2b0ccdSEmmanuel Vadot 	if (sc->txcnt == sc->txlen)
386ec2b0ccdSEmmanuel Vadot 		return;
387ec2b0ccdSEmmanuel Vadot 
388ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_FSR);
389ec2b0ccdSEmmanuel Vadot 	reg &= AW_SPI_FSR_TF_CNT_MASK;
390ec2b0ccdSEmmanuel Vadot 	txcnt = reg >> AW_SPI_FSR_TF_CNT_SHIFT;
391ec2b0ccdSEmmanuel Vadot 
392ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < (AW_SPI_FIFO_SIZE - txcnt); i++) {
393ec2b0ccdSEmmanuel Vadot 		AW_SPI_WRITE_1(sc, AW_SPI_TXD, sc->txbuf[sc->txcnt++]);
394ec2b0ccdSEmmanuel Vadot 		if (sc->txcnt == sc->txlen)
395ec2b0ccdSEmmanuel Vadot 			break;
396ec2b0ccdSEmmanuel Vadot 	}
397ec2b0ccdSEmmanuel Vadot 
398ec2b0ccdSEmmanuel Vadot 	return;
399ec2b0ccdSEmmanuel Vadot }
400ec2b0ccdSEmmanuel Vadot 
401ec2b0ccdSEmmanuel Vadot static inline void
aw_spi_read_rxfifo(struct aw_spi_softc * sc)402ec2b0ccdSEmmanuel Vadot aw_spi_read_rxfifo(struct aw_spi_softc *sc)
403ec2b0ccdSEmmanuel Vadot {
404ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
405ec2b0ccdSEmmanuel Vadot 	uint8_t val;
406ec2b0ccdSEmmanuel Vadot 	int i;
407ec2b0ccdSEmmanuel Vadot 
408ec2b0ccdSEmmanuel Vadot 	if (sc->rxcnt == sc->rxlen)
409ec2b0ccdSEmmanuel Vadot 		return;
410ec2b0ccdSEmmanuel Vadot 
411ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_FSR);
412ec2b0ccdSEmmanuel Vadot 	reg = (reg & AW_SPI_FSR_RF_CNT_MASK) >> AW_SPI_FSR_RF_CNT_SHIFT;
413ec2b0ccdSEmmanuel Vadot 
414ec2b0ccdSEmmanuel Vadot 	for (i = 0; i < reg; i++) {
415ec2b0ccdSEmmanuel Vadot 		val = AW_SPI_READ_1(sc, AW_SPI_RDX);
416ec2b0ccdSEmmanuel Vadot 		if (sc->rxcnt < sc->rxlen)
417ec2b0ccdSEmmanuel Vadot 			sc->rxbuf[sc->rxcnt++] = val;
418ec2b0ccdSEmmanuel Vadot 	}
419ec2b0ccdSEmmanuel Vadot }
420ec2b0ccdSEmmanuel Vadot 
421ec2b0ccdSEmmanuel Vadot static int
aw_spi_intr(void * arg)422ec2b0ccdSEmmanuel Vadot aw_spi_intr(void *arg)
423ec2b0ccdSEmmanuel Vadot {
424ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
425ec2b0ccdSEmmanuel Vadot 	uint32_t intr;
426ec2b0ccdSEmmanuel Vadot 
427ec2b0ccdSEmmanuel Vadot 	sc = (struct aw_spi_softc *)arg;
428ec2b0ccdSEmmanuel Vadot 
429ec2b0ccdSEmmanuel Vadot 	intr = AW_SPI_READ_4(sc, AW_SPI_ISR);
430ec2b0ccdSEmmanuel Vadot 
431ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_RF_RDY)
432ec2b0ccdSEmmanuel Vadot 		aw_spi_read_rxfifo(sc);
433ec2b0ccdSEmmanuel Vadot 
434ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_TF_ERQ) {
435ec2b0ccdSEmmanuel Vadot 		aw_spi_fill_txfifo(sc);
436ec2b0ccdSEmmanuel Vadot 
437ec2b0ccdSEmmanuel Vadot 		/*
438ec2b0ccdSEmmanuel Vadot 		 * If we don't have anything else to write
439ec2b0ccdSEmmanuel Vadot 		 * disable TXFifo interrupts
440ec2b0ccdSEmmanuel Vadot 		 */
441ec2b0ccdSEmmanuel Vadot 		if (sc->txcnt == sc->txlen)
442ec2b0ccdSEmmanuel Vadot 			AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC |
443ec2b0ccdSEmmanuel Vadot 			    AW_SPI_IER_RF_RDY);
444ec2b0ccdSEmmanuel Vadot 	}
445ec2b0ccdSEmmanuel Vadot 
446ec2b0ccdSEmmanuel Vadot 	if (intr & AW_SPI_IER_TC) {
447ec2b0ccdSEmmanuel Vadot 		/* read the rest of the data from the fifo */
448ec2b0ccdSEmmanuel Vadot 		aw_spi_read_rxfifo(sc);
449ec2b0ccdSEmmanuel Vadot 
450ec2b0ccdSEmmanuel Vadot 		/* Disable the interrupts */
451ec2b0ccdSEmmanuel Vadot 		AW_SPI_WRITE_4(sc, AW_SPI_IER, 0);
452ec2b0ccdSEmmanuel Vadot 		sc->transfer = 0;
453ec2b0ccdSEmmanuel Vadot 		wakeup(sc);
454ec2b0ccdSEmmanuel Vadot 	}
455ec2b0ccdSEmmanuel Vadot 
456ec2b0ccdSEmmanuel Vadot 	/* Clear Interrupts */
457ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_ISR, intr);
458ec2b0ccdSEmmanuel Vadot 	return (intr != 0 ? FILTER_HANDLED : FILTER_STRAY);
459ec2b0ccdSEmmanuel Vadot }
460ec2b0ccdSEmmanuel Vadot 
461ec2b0ccdSEmmanuel Vadot static int
aw_spi_xfer(struct aw_spi_softc * sc,void * rxbuf,void * txbuf,uint32_t txlen,uint32_t rxlen)462ec2b0ccdSEmmanuel Vadot aw_spi_xfer(struct aw_spi_softc *sc, void *rxbuf, void *txbuf, uint32_t txlen, uint32_t rxlen)
463ec2b0ccdSEmmanuel Vadot {
464ec2b0ccdSEmmanuel Vadot 	uint32_t reg;
465ec2b0ccdSEmmanuel Vadot 	int error = 0, timeout;
466ec2b0ccdSEmmanuel Vadot 
467ec2b0ccdSEmmanuel Vadot 	sc->rxbuf = rxbuf;
468ec2b0ccdSEmmanuel Vadot 	sc->rxcnt = 0;
469ec2b0ccdSEmmanuel Vadot 	sc->txbuf = txbuf;
470ec2b0ccdSEmmanuel Vadot 	sc->txcnt = 0;
471ec2b0ccdSEmmanuel Vadot 	sc->txlen = txlen;
472ec2b0ccdSEmmanuel Vadot 	sc->rxlen = rxlen;
473ec2b0ccdSEmmanuel Vadot 
474ec2b0ccdSEmmanuel Vadot 	/* Reset the FIFOs */
475ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_FCR, AW_SPI_FCR_TX_RST | AW_SPI_FCR_RX_RST);
476ec2b0ccdSEmmanuel Vadot 
477ec2b0ccdSEmmanuel Vadot 	for (timeout = 1000; timeout > 0; timeout--) {
478ec2b0ccdSEmmanuel Vadot 		reg = AW_SPI_READ_4(sc, AW_SPI_FCR);
479ec2b0ccdSEmmanuel Vadot 		if (reg == 0)
480ec2b0ccdSEmmanuel Vadot 			break;
481ec2b0ccdSEmmanuel Vadot 	}
482ec2b0ccdSEmmanuel Vadot 	if (timeout == 0) {
483ec2b0ccdSEmmanuel Vadot 		device_printf(sc->dev, "Cannot reset the FIFOs\n");
484ec2b0ccdSEmmanuel Vadot 		return (EIO);
485ec2b0ccdSEmmanuel Vadot 	}
486ec2b0ccdSEmmanuel Vadot 
487ec2b0ccdSEmmanuel Vadot 	/*
488ec2b0ccdSEmmanuel Vadot 	 * Set the TX FIFO threshold to 3/4-th the size and
489ec2b0ccdSEmmanuel Vadot 	 * the RX FIFO one to 1/4-th.
490ec2b0ccdSEmmanuel Vadot 	 */
491ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_FCR,
492ec2b0ccdSEmmanuel Vadot 	    ((3 * AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_TX_TRIG_SHIFT) |
493ec2b0ccdSEmmanuel Vadot 	    ((AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_RX_TRIG_SHIFT));
494ec2b0ccdSEmmanuel Vadot 
495ec2b0ccdSEmmanuel Vadot 	/* Write the counters */
496ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_MBC, txlen);
497ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_MTC, txlen);
498ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_BCC, txlen);
499ec2b0ccdSEmmanuel Vadot 
500ec2b0ccdSEmmanuel Vadot 	/* First fill */
501ec2b0ccdSEmmanuel Vadot 	aw_spi_fill_txfifo(sc);
502ec2b0ccdSEmmanuel Vadot 
503ec2b0ccdSEmmanuel Vadot 	/* Start transmit */
504ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_TCR);
505ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_TCR_XCH;
506ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg);
507ec2b0ccdSEmmanuel Vadot 
508ec2b0ccdSEmmanuel Vadot 	/*
509ec2b0ccdSEmmanuel Vadot 	 * Enable interrupts for :
510ec2b0ccdSEmmanuel Vadot 	 * Transmit complete
511ec2b0ccdSEmmanuel Vadot 	 * TX Fifo is below its trigger threshold
512ec2b0ccdSEmmanuel Vadot 	 * RX Fifo is above its trigger threshold
513ec2b0ccdSEmmanuel Vadot 	 */
514ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC |
515ec2b0ccdSEmmanuel Vadot 	    AW_SPI_IER_TF_ERQ | AW_SPI_IER_RF_RDY);
516ec2b0ccdSEmmanuel Vadot 
517ec2b0ccdSEmmanuel Vadot 	sc->transfer = 1;
518ec2b0ccdSEmmanuel Vadot 
519ec2b0ccdSEmmanuel Vadot 	while (error == 0 && sc->transfer != 0)
520ec2b0ccdSEmmanuel Vadot 		error = msleep(sc, &sc->mtx, 0, "aw_spi", 10 * hz);
521ec2b0ccdSEmmanuel Vadot 
522ec2b0ccdSEmmanuel Vadot 	return (0);
523ec2b0ccdSEmmanuel Vadot }
524ec2b0ccdSEmmanuel Vadot 
525ec2b0ccdSEmmanuel Vadot static int
aw_spi_transfer(device_t dev,device_t child,struct spi_command * cmd)526ec2b0ccdSEmmanuel Vadot aw_spi_transfer(device_t dev, device_t child, struct spi_command *cmd)
527ec2b0ccdSEmmanuel Vadot {
528ec2b0ccdSEmmanuel Vadot 	struct aw_spi_softc *sc;
529ec2b0ccdSEmmanuel Vadot 	uint32_t cs, mode, clock, reg;
530ec2b0ccdSEmmanuel Vadot 	int err = 0;
531ec2b0ccdSEmmanuel Vadot 
532ec2b0ccdSEmmanuel Vadot 	sc = device_get_softc(dev);
533ec2b0ccdSEmmanuel Vadot 
534ec2b0ccdSEmmanuel Vadot 	spibus_get_cs(child, &cs);
535ec2b0ccdSEmmanuel Vadot 	spibus_get_clock(child, &clock);
536ec2b0ccdSEmmanuel Vadot 	spibus_get_mode(child, &mode);
537ec2b0ccdSEmmanuel Vadot 
538ec2b0ccdSEmmanuel Vadot 	/* The minimum divider is 2 so set the clock at twice the needed speed */
539ec2b0ccdSEmmanuel Vadot 	clk_set_freq(sc->clk_mod, 2 * clock, CLK_SET_ROUND_DOWN);
540ec2b0ccdSEmmanuel Vadot 	clk_get_freq(sc->clk_mod, &sc->mod_freq);
541ec2b0ccdSEmmanuel Vadot 	if (cs >= AW_SPI_MAX_CS) {
542ec2b0ccdSEmmanuel Vadot 		device_printf(dev, "Invalid cs %d\n", cs);
543ec2b0ccdSEmmanuel Vadot 		return (EINVAL);
544ec2b0ccdSEmmanuel Vadot 	}
545ec2b0ccdSEmmanuel Vadot 
546ec2b0ccdSEmmanuel Vadot 	mtx_lock(&sc->mtx);
547ec2b0ccdSEmmanuel Vadot 
548ec2b0ccdSEmmanuel Vadot 	/* Enable and reset the module */
549ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
550ec2b0ccdSEmmanuel Vadot 	reg |= AW_SPI_GCR_EN | AW_SPI_GCR_SRST;
551ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
552ec2b0ccdSEmmanuel Vadot 
553ec2b0ccdSEmmanuel Vadot 	/* Setup clock, CS and mode */
554ec2b0ccdSEmmanuel Vadot 	aw_spi_setup_clock(sc, clock);
555ec2b0ccdSEmmanuel Vadot 	aw_spi_setup_mode(sc, mode);
556ec2b0ccdSEmmanuel Vadot 	if (cs & SPIBUS_CS_HIGH)
557ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, false);
558ec2b0ccdSEmmanuel Vadot 	else
559ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, true);
560ec2b0ccdSEmmanuel Vadot 
561ec2b0ccdSEmmanuel Vadot 	/* xfer */
562ec2b0ccdSEmmanuel Vadot 	err = 0;
563ec2b0ccdSEmmanuel Vadot 	if (cmd->tx_cmd_sz > 0)
564ec2b0ccdSEmmanuel Vadot 		err = aw_spi_xfer(sc, cmd->rx_cmd, cmd->tx_cmd,
565ec2b0ccdSEmmanuel Vadot 		    cmd->tx_cmd_sz, cmd->rx_cmd_sz);
566ec2b0ccdSEmmanuel Vadot 	if (cmd->tx_data_sz > 0 && err == 0)
567ec2b0ccdSEmmanuel Vadot 		err = aw_spi_xfer(sc, cmd->rx_data, cmd->tx_data,
568ec2b0ccdSEmmanuel Vadot 		    cmd->tx_data_sz, cmd->rx_data_sz);
569ec2b0ccdSEmmanuel Vadot 
570ec2b0ccdSEmmanuel Vadot 	if (cs & SPIBUS_CS_HIGH)
571ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, true);
572ec2b0ccdSEmmanuel Vadot 	else
573ec2b0ccdSEmmanuel Vadot 		aw_spi_setup_cs(sc, cs, false);
574ec2b0ccdSEmmanuel Vadot 
575ec2b0ccdSEmmanuel Vadot 	/* Disable the module */
576ec2b0ccdSEmmanuel Vadot 	reg = AW_SPI_READ_4(sc, AW_SPI_GCR);
577ec2b0ccdSEmmanuel Vadot 	reg &= ~AW_SPI_GCR_EN;
578ec2b0ccdSEmmanuel Vadot 	AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg);
579ec2b0ccdSEmmanuel Vadot 
580ec2b0ccdSEmmanuel Vadot 	mtx_unlock(&sc->mtx);
581ec2b0ccdSEmmanuel Vadot 
582ec2b0ccdSEmmanuel Vadot 	return (err);
583ec2b0ccdSEmmanuel Vadot }
584ec2b0ccdSEmmanuel Vadot 
585ec2b0ccdSEmmanuel Vadot static device_method_t aw_spi_methods[] = {
586ec2b0ccdSEmmanuel Vadot 	/* Device interface */
587ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_spi_probe),
588ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_spi_attach),
589ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(device_detach,	aw_spi_detach),
590ec2b0ccdSEmmanuel Vadot 
591ec2b0ccdSEmmanuel Vadot         /* spibus_if  */
592ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(spibus_transfer,	aw_spi_transfer),
593ec2b0ccdSEmmanuel Vadot 
594ec2b0ccdSEmmanuel Vadot         /* ofw_bus_if */
595ec2b0ccdSEmmanuel Vadot 	DEVMETHOD(ofw_bus_get_node,	aw_spi_get_node),
596ec2b0ccdSEmmanuel Vadot 
597ec2b0ccdSEmmanuel Vadot 	DEVMETHOD_END
598ec2b0ccdSEmmanuel Vadot };
599ec2b0ccdSEmmanuel Vadot 
600ec2b0ccdSEmmanuel Vadot static driver_t aw_spi_driver = {
601ec2b0ccdSEmmanuel Vadot 	"aw_spi",
602ec2b0ccdSEmmanuel Vadot 	aw_spi_methods,
603ec2b0ccdSEmmanuel Vadot 	sizeof(struct aw_spi_softc),
604ec2b0ccdSEmmanuel Vadot };
605ec2b0ccdSEmmanuel Vadot 
606ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(aw_spi, simplebus, aw_spi_driver, 0, 0);
607ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(ofw_spibus, aw_spi, ofw_spibus_driver, 0, 0);
608ec2b0ccdSEmmanuel Vadot MODULE_DEPEND(aw_spi, ofw_spibus, 1, 1, 1);
609ec2b0ccdSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
610