sun4i_spi.c revision 1.11 1 1.11 thorpej /* $NetBSD: sun4i_spi.c,v 1.11 2025/09/10 04:17:19 thorpej Exp $ */
2 1.1 tnn
3 1.1 tnn /*
4 1.1 tnn * Copyright (c) 2019 Tobias Nygren
5 1.1 tnn * Copyright (c) 2018 Jonathan A. Kollasch
6 1.1 tnn * All rights reserved.
7 1.1 tnn *
8 1.1 tnn * Redistribution and use in source and binary forms, with or without
9 1.1 tnn * modification, are permitted provided that the following conditions
10 1.1 tnn * are met:
11 1.1 tnn * 1. Redistributions of source code must retain the above copyright
12 1.1 tnn * notice, this list of conditions and the following disclaimer.
13 1.1 tnn * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 tnn * notice, this list of conditions and the following disclaimer in the
15 1.1 tnn * documentation and/or other materials provided with the distribution.
16 1.1 tnn *
17 1.1 tnn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 1.1 tnn * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 tnn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 tnn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 1.1 tnn * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 1.1 tnn * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 1.1 tnn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 1.1 tnn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 1.1 tnn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 tnn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 1.1 tnn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 tnn */
29 1.1 tnn
30 1.1 tnn #include <sys/cdefs.h>
31 1.11 thorpej __KERNEL_RCSID(0, "$NetBSD: sun4i_spi.c,v 1.11 2025/09/10 04:17:19 thorpej Exp $");
32 1.1 tnn
33 1.1 tnn #include <sys/param.h>
34 1.1 tnn #include <sys/device.h>
35 1.1 tnn #include <sys/systm.h>
36 1.1 tnn #include <sys/bus.h>
37 1.1 tnn #include <sys/intr.h>
38 1.1 tnn #include <sys/kernel.h>
39 1.1 tnn #include <sys/bitops.h>
40 1.1 tnn #include <dev/spi/spivar.h>
41 1.1 tnn #include <arm/sunxi/sun4i_spireg.h>
42 1.1 tnn #include <dev/fdt/fdtvar.h>
43 1.1 tnn
44 1.7 thorpej static const struct device_compatible_entry compat_data[] = {
45 1.7 thorpej { .compat = "allwinner,sun4i-a10-spi" },
46 1.7 thorpej DEVICE_COMPAT_EOL
47 1.1 tnn };
48 1.1 tnn
49 1.1 tnn struct sun4ispi_softc {
50 1.1 tnn device_t sc_dev;
51 1.1 tnn bus_space_tag_t sc_bst;
52 1.1 tnn bus_space_handle_t sc_bsh;
53 1.1 tnn void *sc_intrh;
54 1.1 tnn struct spi_controller sc_spi;
55 1.1 tnn SIMPLEQ_HEAD(,spi_transfer) sc_q;
56 1.1 tnn struct spi_transfer *sc_transfer;
57 1.1 tnn struct spi_chunk *sc_rchunk, *sc_wchunk;
58 1.1 tnn uint32_t sc_CTL;
59 1.1 tnn u_int sc_modclkrate;
60 1.1 tnn volatile bool sc_running;
61 1.1 tnn };
62 1.1 tnn
63 1.1 tnn #define SPIREG_READ(sc, reg) \
64 1.1 tnn bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
65 1.1 tnn #define SPIREG_WRITE(sc, reg, val) \
66 1.1 tnn bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
67 1.1 tnn
68 1.1 tnn static int sun4ispi_match(device_t, cfdata_t, void *);
69 1.1 tnn static void sun4ispi_attach(device_t, device_t, void *);
70 1.1 tnn
71 1.1 tnn static int sun4ispi_configure(void *, int, int, int);
72 1.1 tnn static int sun4ispi_transfer(void *, struct spi_transfer *);
73 1.1 tnn
74 1.1 tnn static void sun4ispi_txfifo_fill(struct sun4ispi_softc * const, size_t);
75 1.1 tnn static void sun4ispi_rxfifo_drain(struct sun4ispi_softc * const, size_t);
76 1.2 tnn static void sun4ispi_rxtx(struct sun4ispi_softc * const);
77 1.1 tnn static void sun4ispi_set_interrupt_mask(struct sun4ispi_softc * const);
78 1.1 tnn static void sun4ispi_start(struct sun4ispi_softc * const);
79 1.1 tnn static int sun4ispi_intr(void *);
80 1.1 tnn
81 1.1 tnn CFATTACH_DECL_NEW(sun4i_spi, sizeof(struct sun4ispi_softc),
82 1.1 tnn sun4ispi_match, sun4ispi_attach, NULL, NULL);
83 1.1 tnn
84 1.1 tnn static int
85 1.1 tnn sun4ispi_match(device_t parent, cfdata_t cf, void *aux)
86 1.1 tnn {
87 1.1 tnn struct fdt_attach_args * const faa = aux;
88 1.1 tnn
89 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
90 1.1 tnn }
91 1.1 tnn
92 1.1 tnn static void
93 1.1 tnn sun4ispi_attach(device_t parent, device_t self, void *aux)
94 1.1 tnn {
95 1.1 tnn struct sun4ispi_softc * const sc = device_private(self);
96 1.1 tnn struct fdt_attach_args * const faa = aux;
97 1.1 tnn const int phandle = faa->faa_phandle;
98 1.1 tnn bus_addr_t addr;
99 1.1 tnn bus_size_t size;
100 1.1 tnn struct clk *clk, *modclk;
101 1.1 tnn char intrstr[128];
102 1.1 tnn
103 1.1 tnn sc->sc_dev = self;
104 1.1 tnn sc->sc_bst = faa->faa_bst;
105 1.1 tnn SIMPLEQ_INIT(&sc->sc_q);
106 1.1 tnn
107 1.1 tnn if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL
108 1.1 tnn || clk_enable(clk) != 0) {
109 1.1 tnn aprint_error(": couldn't enable clock\n");
110 1.1 tnn return;
111 1.1 tnn }
112 1.1 tnn
113 1.1 tnn if ((modclk = fdtbus_clock_get(phandle, "mod")) == NULL
114 1.1 tnn || clk_set_rate(modclk, clk_get_rate(clk)) != 0
115 1.1 tnn || clk_enable(modclk) != 0) {
116 1.1 tnn aprint_error(": couldn't enable module clock\n");
117 1.1 tnn return;
118 1.1 tnn }
119 1.1 tnn sc->sc_modclkrate = clk_get_rate(modclk);
120 1.1 tnn
121 1.1 tnn if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
122 1.1 tnn || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
123 1.1 tnn aprint_error(": couldn't map registers\n");
124 1.1 tnn return;
125 1.1 tnn }
126 1.1 tnn
127 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, SPI_CTL_SSPOL | SPI_CTL_RF_RST
128 1.1 tnn | SPI_CTL_TF_RST | SPI_CTL_MODE);
129 1.1 tnn SPIREG_WRITE(sc, SPI_DMACTL, 0);
130 1.1 tnn SPIREG_WRITE(sc, SPI_WAIT, 0);
131 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, 0);
132 1.1 tnn SPIREG_WRITE(sc, SPI_INT_STA, ~0);
133 1.1 tnn
134 1.1 tnn if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
135 1.1 tnn aprint_error(": failed to decode interrupt\n");
136 1.1 tnn return;
137 1.1 tnn }
138 1.1 tnn
139 1.6 jmcneill sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
140 1.6 jmcneill sun4ispi_intr, sc, device_xname(self));
141 1.1 tnn if (sc->sc_intrh == NULL) {
142 1.2 tnn aprint_error(": unable to establish interrupt\n");
143 1.1 tnn return;
144 1.1 tnn }
145 1.1 tnn
146 1.1 tnn aprint_naive("\n");
147 1.1 tnn aprint_normal(": SPI\n");
148 1.1 tnn aprint_normal_dev(self, "interrupting on %s\n", intrstr);
149 1.1 tnn
150 1.1 tnn sc->sc_spi.sct_cookie = sc;
151 1.1 tnn sc->sc_spi.sct_configure = sun4ispi_configure;
152 1.1 tnn sc->sc_spi.sct_transfer = sun4ispi_transfer;
153 1.1 tnn (void) of_getprop_uint32(phandle, "num-cs", &sc->sc_spi.sct_nslaves);
154 1.11 thorpej
155 1.11 thorpej spibus_attach(self, &sc->sc_spi);
156 1.1 tnn }
157 1.1 tnn
158 1.1 tnn static int
159 1.1 tnn sun4ispi_configure(void *cookie, int slave, int mode, int speed)
160 1.1 tnn {
161 1.1 tnn struct sun4ispi_softc * const sc = cookie;
162 1.1 tnn uint32_t ctl, cctl;
163 1.1 tnn uint32_t minfreq, maxfreq;
164 1.1 tnn
165 1.1 tnn minfreq = sc->sc_modclkrate >> 16;
166 1.1 tnn maxfreq = sc->sc_modclkrate >> 1;
167 1.1 tnn
168 1.1 tnn if (speed <= 0 || speed < minfreq || speed > maxfreq)
169 1.1 tnn return EINVAL;
170 1.1 tnn
171 1.1 tnn if (slave >= sc->sc_spi.sct_nslaves)
172 1.1 tnn return EINVAL;
173 1.1 tnn
174 1.1 tnn ctl = SPI_CTL_SDM | SPI_CTL_TP_EN | SPI_CTL_SSPOL | SPI_CTL_MODE | SPI_CTL_EN;
175 1.1 tnn
176 1.1 tnn switch (mode) {
177 1.1 tnn case SPI_MODE_0:
178 1.1 tnn ctl |= 0;
179 1.1 tnn break;
180 1.1 tnn case SPI_MODE_1:
181 1.1 tnn ctl |= SPI_CTL_PHA;
182 1.1 tnn break;
183 1.1 tnn case SPI_MODE_2:
184 1.1 tnn ctl |= SPI_CTL_POL;
185 1.1 tnn break;
186 1.1 tnn case SPI_MODE_3:
187 1.1 tnn ctl |= SPI_CTL_PHA | SPI_CTL_POL;
188 1.1 tnn break;
189 1.1 tnn default:
190 1.1 tnn return EINVAL;
191 1.1 tnn }
192 1.1 tnn
193 1.1 tnn if (speed < sc->sc_modclkrate / 512) {
194 1.1 tnn for (cctl = 0; cctl <= __SHIFTOUT_MASK(SPI_CCTL_CDR1); cctl++) {
195 1.1 tnn if ((sc->sc_modclkrate / (1 << cctl)) <= speed)
196 1.1 tnn goto cdr1_found;
197 1.1 tnn }
198 1.1 tnn return EINVAL;
199 1.1 tnn cdr1_found:
200 1.1 tnn cctl = __SHIFTIN(cctl, SPI_CCTL_CDR1);
201 1.1 tnn } else {
202 1.1 tnn cctl = howmany(sc->sc_modclkrate, 2 * speed) - 1;
203 1.1 tnn cctl = SPI_CCTL_DRS|__SHIFTIN(cctl, SPI_CCTL_CDR2);
204 1.1 tnn }
205 1.1 tnn
206 1.1 tnn device_printf(sc->sc_dev, "ctl 0x%x, cctl 0x%x, CLK %uHz, SCLK %uHz\n",
207 1.1 tnn ctl, cctl, sc->sc_modclkrate,
208 1.1 tnn (cctl & SPI_CCTL_DRS)
209 1.1 tnn ? (sc->sc_modclkrate / (u_int)(2 * (__SHIFTOUT(cctl, SPI_CCTL_CDR2) + 1)))
210 1.1 tnn : (sc->sc_modclkrate >> (__SHIFTOUT(cctl, SPI_CCTL_CDR1) + 1))
211 1.1 tnn );
212 1.1 tnn
213 1.1 tnn sc->sc_CTL = ctl;
214 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, (ctl | SPI_CTL_RF_RST | SPI_CTL_TF_RST) & ~SPI_CTL_EN);
215 1.1 tnn SPIREG_WRITE(sc, SPI_CCTL, cctl);
216 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, ctl);
217 1.1 tnn
218 1.1 tnn return 0;
219 1.1 tnn }
220 1.1 tnn
221 1.1 tnn static int
222 1.1 tnn sun4ispi_transfer(void *cookie, struct spi_transfer *st)
223 1.1 tnn {
224 1.1 tnn struct sun4ispi_softc * const sc = cookie;
225 1.1 tnn int s;
226 1.1 tnn
227 1.1 tnn s = splbio();
228 1.1 tnn spi_transq_enqueue(&sc->sc_q, st);
229 1.1 tnn if (sc->sc_running == false) {
230 1.1 tnn sun4ispi_start(sc);
231 1.1 tnn }
232 1.1 tnn splx(s);
233 1.1 tnn
234 1.1 tnn return 0;
235 1.1 tnn }
236 1.1 tnn
237 1.1 tnn static void
238 1.1 tnn sun4ispi_txfifo_fill(struct sun4ispi_softc * const sc, size_t maxlen)
239 1.1 tnn {
240 1.1 tnn struct spi_chunk *chunk = sc->sc_wchunk;
241 1.1 tnn size_t len;
242 1.1 tnn uint8_t b;
243 1.1 tnn
244 1.1 tnn if (chunk == NULL)
245 1.1 tnn return;
246 1.1 tnn
247 1.1 tnn len = MIN(maxlen, chunk->chunk_wresid);
248 1.1 tnn chunk->chunk_wresid -= len;
249 1.1 tnn while (len--) {
250 1.1 tnn if (chunk->chunk_wptr) {
251 1.1 tnn b = *chunk->chunk_wptr++;
252 1.1 tnn } else {
253 1.1 tnn b = 0;
254 1.1 tnn }
255 1.1 tnn bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDATA, b);
256 1.1 tnn }
257 1.1 tnn if (sc->sc_wchunk->chunk_wresid == 0) {
258 1.1 tnn sc->sc_wchunk = sc->sc_wchunk->chunk_next;
259 1.1 tnn }
260 1.1 tnn }
261 1.1 tnn
262 1.1 tnn static void
263 1.1 tnn sun4ispi_rxfifo_drain(struct sun4ispi_softc * const sc, size_t maxlen)
264 1.1 tnn {
265 1.1 tnn struct spi_chunk *chunk = sc->sc_rchunk;
266 1.1 tnn size_t len;
267 1.1 tnn uint8_t b;
268 1.1 tnn
269 1.1 tnn if (chunk == NULL)
270 1.1 tnn return;
271 1.1 tnn
272 1.1 tnn len = MIN(maxlen, chunk->chunk_rresid);
273 1.1 tnn chunk->chunk_rresid -= len;
274 1.1 tnn
275 1.1 tnn while (len--) {
276 1.1 tnn b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDATA);
277 1.1 tnn if (chunk->chunk_rptr) {
278 1.1 tnn *chunk->chunk_rptr++ = b;
279 1.1 tnn }
280 1.1 tnn }
281 1.1 tnn if (sc->sc_rchunk->chunk_rresid == 0) {
282 1.1 tnn sc->sc_rchunk = sc->sc_rchunk->chunk_next;
283 1.1 tnn }
284 1.1 tnn }
285 1.1 tnn
286 1.1 tnn static void
287 1.1 tnn sun4ispi_rxtx(struct sun4ispi_softc * const sc)
288 1.1 tnn {
289 1.1 tnn bool again;
290 1.1 tnn size_t rxavail, txavail;
291 1.1 tnn uint32_t fsr;
292 1.1 tnn
293 1.1 tnn /* service both FIFOs until no more progress can be made */
294 1.1 tnn again = true;
295 1.1 tnn while (again) {
296 1.1 tnn again = false;
297 1.1 tnn fsr = SPIREG_READ(sc, SPI_FIFO_STA);
298 1.1 tnn rxavail = __SHIFTOUT(fsr, SPI_FIFO_STA_RF_CNT);
299 1.1 tnn txavail = 64 - __SHIFTOUT(fsr, SPI_FIFO_STA_TF_CNT);
300 1.1 tnn if (rxavail > 0) {
301 1.1 tnn KASSERT(sc->sc_rchunk != NULL);
302 1.1 tnn sun4ispi_rxfifo_drain(sc, rxavail);
303 1.1 tnn again = true;
304 1.1 tnn }
305 1.1 tnn if (txavail > 0 && sc->sc_wchunk != NULL) {
306 1.1 tnn sun4ispi_txfifo_fill(sc, txavail);
307 1.1 tnn again = true;
308 1.1 tnn }
309 1.1 tnn }
310 1.1 tnn }
311 1.1 tnn
312 1.1 tnn static void
313 1.1 tnn sun4ispi_set_interrupt_mask(struct sun4ispi_softc * const sc)
314 1.1 tnn {
315 1.1 tnn uint32_t intctl;
316 1.1 tnn
317 1.1 tnn intctl = SPI_INTCTL_TX_INT_EN;
318 1.1 tnn intctl |= SPI_INTCTL_RF_OF_INT_EN;
319 1.1 tnn intctl |= SPI_INTCTL_TF_UR_INT_EN;
320 1.1 tnn
321 1.1 tnn if (sc->sc_rchunk) {
322 1.1 tnn if (sc->sc_rchunk->chunk_rresid >= 32) {
323 1.1 tnn intctl |= SPI_INTCTL_RF_HALF_FU_INT_EN;
324 1.1 tnn } else {
325 1.1 tnn intctl |= SPI_INTCTL_RF_RDY_INT_EN;
326 1.1 tnn }
327 1.1 tnn }
328 1.1 tnn if (sc->sc_wchunk) {
329 1.1 tnn intctl |= SPI_INTCTL_TF_HALF_EMP_INT_EN;
330 1.1 tnn }
331 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, intctl);
332 1.1 tnn }
333 1.1 tnn
334 1.1 tnn static void
335 1.1 tnn sun4ispi_start(struct sun4ispi_softc * const sc)
336 1.1 tnn {
337 1.1 tnn struct spi_transfer *st;
338 1.1 tnn uint32_t ctl;
339 1.1 tnn struct spi_chunk *chunk;
340 1.1 tnn size_t burstcount;
341 1.1 tnn
342 1.1 tnn while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
343 1.1 tnn
344 1.1 tnn spi_transq_dequeue(&sc->sc_q);
345 1.1 tnn
346 1.1 tnn KASSERT(sc->sc_transfer == NULL);
347 1.1 tnn sc->sc_transfer = st;
348 1.1 tnn sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
349 1.1 tnn sc->sc_running = true;
350 1.1 tnn
351 1.1 tnn burstcount = 0;
352 1.1 tnn for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
353 1.1 tnn burstcount += chunk->chunk_count;
354 1.1 tnn }
355 1.1 tnn KASSERT(burstcount <= SPI_BC_BC);
356 1.1 tnn SPIREG_WRITE(sc, SPI_BC, __SHIFTIN(burstcount, SPI_BC_BC));
357 1.1 tnn SPIREG_WRITE(sc, SPI_TC, __SHIFTIN(burstcount, SPI_TC_WTC));
358 1.1 tnn
359 1.1 tnn sun4ispi_rxtx(sc);
360 1.1 tnn sun4ispi_set_interrupt_mask(sc);
361 1.1 tnn
362 1.1 tnn KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
363 1.1 tnn ctl = sc->sc_CTL | __SHIFTIN(st->st_slave, SPI_CTL_SS) | SPI_CTL_XCH;
364 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, ctl);
365 1.1 tnn
366 1.1 tnn if (!cold)
367 1.1 tnn return;
368 1.1 tnn
369 1.1 tnn for (;;) {
370 1.1 tnn (void) sun4ispi_intr(sc);
371 1.1 tnn if (ISSET(st->st_flags, SPI_F_DONE))
372 1.1 tnn break;
373 1.1 tnn }
374 1.1 tnn }
375 1.1 tnn sc->sc_running = false;
376 1.1 tnn }
377 1.1 tnn
378 1.1 tnn static int
379 1.1 tnn sun4ispi_intr(void *cookie)
380 1.1 tnn {
381 1.1 tnn struct sun4ispi_softc * const sc = cookie;
382 1.1 tnn struct spi_transfer *st;
383 1.1 tnn uint32_t isr;
384 1.1 tnn
385 1.1 tnn isr = SPIREG_READ(sc, SPI_INT_STA);
386 1.1 tnn if (!isr)
387 1.1 tnn return 0;
388 1.1 tnn
389 1.1 tnn if (ISSET(isr, SPI_INT_STA_RO)) {
390 1.1 tnn device_printf(sc->sc_dev, "RXFIFO overflow\n");
391 1.1 tnn }
392 1.1 tnn if (ISSET(isr, SPI_INT_STA_TU)) {
393 1.1 tnn device_printf(sc->sc_dev, "TXFIFO underrun\n");
394 1.1 tnn }
395 1.1 tnn
396 1.1 tnn sun4ispi_rxtx(sc);
397 1.1 tnn
398 1.1 tnn if (ISSET(isr, SPI_INT_STA_TC)) {
399 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, 0);
400 1.1 tnn KASSERT(sc->sc_rchunk == NULL);
401 1.1 tnn KASSERT(sc->sc_wchunk == NULL);
402 1.1 tnn st = sc->sc_transfer;
403 1.1 tnn sc->sc_transfer = NULL;
404 1.1 tnn KASSERT(st != NULL);
405 1.1 tnn spi_done(st, 0);
406 1.1 tnn sc->sc_running = false;
407 1.1 tnn } else {
408 1.1 tnn sun4ispi_set_interrupt_mask(sc);
409 1.1 tnn }
410 1.1 tnn SPIREG_WRITE(sc, SPI_INT_STA, isr);
411 1.1 tnn
412 1.1 tnn return 1;
413 1.1 tnn }
414