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