sun4i_spi.c revision 1.2 1 1.1 tnn /* $NetBSD: sun4i_spi.c,v 1.2 2019/08/03 19:56:42 tnn 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.2 tnn __KERNEL_RCSID(0, "$NetBSD: sun4i_spi.c,v 1.2 2019/08/03 19:56:42 tnn 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.1 tnn static const char * const compatible[] = {
45 1.1 tnn "allwinner,sun4i-a10-spi",
46 1.1 tnn NULL
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.1 tnn return of_match_compatible(faa->faa_phandle, compatible);
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 struct spibus_attach_args sba;
103 1.1 tnn
104 1.1 tnn sc->sc_dev = self;
105 1.1 tnn sc->sc_bst = faa->faa_bst;
106 1.1 tnn SIMPLEQ_INIT(&sc->sc_q);
107 1.1 tnn
108 1.1 tnn if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL
109 1.1 tnn || clk_enable(clk) != 0) {
110 1.1 tnn aprint_error(": couldn't enable clock\n");
111 1.1 tnn return;
112 1.1 tnn }
113 1.1 tnn
114 1.1 tnn if ((modclk = fdtbus_clock_get(phandle, "mod")) == NULL
115 1.1 tnn || clk_set_rate(modclk, clk_get_rate(clk)) != 0
116 1.1 tnn || clk_enable(modclk) != 0) {
117 1.1 tnn aprint_error(": couldn't enable module clock\n");
118 1.1 tnn return;
119 1.1 tnn }
120 1.1 tnn sc->sc_modclkrate = clk_get_rate(modclk);
121 1.1 tnn
122 1.1 tnn if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
123 1.1 tnn || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
124 1.1 tnn aprint_error(": couldn't map registers\n");
125 1.1 tnn return;
126 1.1 tnn }
127 1.1 tnn
128 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, SPI_CTL_SSPOL | SPI_CTL_RF_RST
129 1.1 tnn | SPI_CTL_TF_RST | SPI_CTL_MODE);
130 1.1 tnn SPIREG_WRITE(sc, SPI_DMACTL, 0);
131 1.1 tnn SPIREG_WRITE(sc, SPI_WAIT, 0);
132 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, 0);
133 1.1 tnn SPIREG_WRITE(sc, SPI_INT_STA, ~0);
134 1.1 tnn
135 1.1 tnn if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
136 1.1 tnn aprint_error(": failed to decode interrupt\n");
137 1.1 tnn return;
138 1.1 tnn }
139 1.1 tnn
140 1.1 tnn sc->sc_intrh = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, sun4ispi_intr, sc);
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.1 tnn sba.sba_controller = &sc->sc_spi;
155 1.1 tnn
156 1.1 tnn (void) config_found_ia(self, "spibus", &sba, spibus_print);
157 1.1 tnn }
158 1.1 tnn
159 1.1 tnn static int
160 1.1 tnn sun4ispi_configure(void *cookie, int slave, int mode, int speed)
161 1.1 tnn {
162 1.1 tnn struct sun4ispi_softc * const sc = cookie;
163 1.1 tnn uint32_t ctl, cctl;
164 1.1 tnn uint32_t minfreq, maxfreq;
165 1.1 tnn
166 1.1 tnn minfreq = sc->sc_modclkrate >> 16;
167 1.1 tnn maxfreq = sc->sc_modclkrate >> 1;
168 1.1 tnn
169 1.1 tnn if (speed <= 0 || speed < minfreq || speed > maxfreq)
170 1.1 tnn return EINVAL;
171 1.1 tnn
172 1.1 tnn if (slave >= sc->sc_spi.sct_nslaves)
173 1.1 tnn return EINVAL;
174 1.1 tnn
175 1.1 tnn ctl = SPI_CTL_SDM | SPI_CTL_TP_EN | SPI_CTL_SSPOL | SPI_CTL_MODE | SPI_CTL_EN;
176 1.1 tnn
177 1.1 tnn switch (mode) {
178 1.1 tnn case SPI_MODE_0:
179 1.1 tnn ctl |= 0;
180 1.1 tnn break;
181 1.1 tnn case SPI_MODE_1:
182 1.1 tnn ctl |= SPI_CTL_PHA;
183 1.1 tnn break;
184 1.1 tnn case SPI_MODE_2:
185 1.1 tnn ctl |= SPI_CTL_POL;
186 1.1 tnn break;
187 1.1 tnn case SPI_MODE_3:
188 1.1 tnn ctl |= SPI_CTL_PHA | SPI_CTL_POL;
189 1.1 tnn break;
190 1.1 tnn default:
191 1.1 tnn return EINVAL;
192 1.1 tnn }
193 1.1 tnn
194 1.1 tnn if (speed < sc->sc_modclkrate / 512) {
195 1.1 tnn for (cctl = 0; cctl <= __SHIFTOUT_MASK(SPI_CCTL_CDR1); cctl++) {
196 1.1 tnn if ((sc->sc_modclkrate / (1 << cctl)) <= speed)
197 1.1 tnn goto cdr1_found;
198 1.1 tnn }
199 1.1 tnn return EINVAL;
200 1.1 tnn cdr1_found:
201 1.1 tnn cctl = __SHIFTIN(cctl, SPI_CCTL_CDR1);
202 1.1 tnn } else {
203 1.1 tnn cctl = howmany(sc->sc_modclkrate, 2 * speed) - 1;
204 1.1 tnn cctl = SPI_CCTL_DRS|__SHIFTIN(cctl, SPI_CCTL_CDR2);
205 1.1 tnn }
206 1.1 tnn
207 1.1 tnn device_printf(sc->sc_dev, "ctl 0x%x, cctl 0x%x, CLK %uHz, SCLK %uHz\n",
208 1.1 tnn ctl, cctl, sc->sc_modclkrate,
209 1.1 tnn (cctl & SPI_CCTL_DRS)
210 1.1 tnn ? (sc->sc_modclkrate / (u_int)(2 * (__SHIFTOUT(cctl, SPI_CCTL_CDR2) + 1)))
211 1.1 tnn : (sc->sc_modclkrate >> (__SHIFTOUT(cctl, SPI_CCTL_CDR1) + 1))
212 1.1 tnn );
213 1.1 tnn
214 1.1 tnn sc->sc_CTL = ctl;
215 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, (ctl | SPI_CTL_RF_RST | SPI_CTL_TF_RST) & ~SPI_CTL_EN);
216 1.1 tnn SPIREG_WRITE(sc, SPI_CCTL, cctl);
217 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, ctl);
218 1.1 tnn
219 1.1 tnn return 0;
220 1.1 tnn }
221 1.1 tnn
222 1.1 tnn static int
223 1.1 tnn sun4ispi_transfer(void *cookie, struct spi_transfer *st)
224 1.1 tnn {
225 1.1 tnn struct sun4ispi_softc * const sc = cookie;
226 1.1 tnn int s;
227 1.1 tnn
228 1.1 tnn s = splbio();
229 1.1 tnn spi_transq_enqueue(&sc->sc_q, st);
230 1.1 tnn if (sc->sc_running == false) {
231 1.1 tnn sun4ispi_start(sc);
232 1.1 tnn }
233 1.1 tnn splx(s);
234 1.1 tnn
235 1.1 tnn return 0;
236 1.1 tnn }
237 1.1 tnn
238 1.1 tnn static void
239 1.1 tnn sun4ispi_txfifo_fill(struct sun4ispi_softc * const sc, size_t maxlen)
240 1.1 tnn {
241 1.1 tnn struct spi_chunk *chunk = sc->sc_wchunk;
242 1.1 tnn size_t len;
243 1.1 tnn uint8_t b;
244 1.1 tnn
245 1.1 tnn if (chunk == NULL)
246 1.1 tnn return;
247 1.1 tnn
248 1.1 tnn len = MIN(maxlen, chunk->chunk_wresid);
249 1.1 tnn chunk->chunk_wresid -= len;
250 1.1 tnn while (len--) {
251 1.1 tnn if (chunk->chunk_wptr) {
252 1.1 tnn b = *chunk->chunk_wptr++;
253 1.1 tnn } else {
254 1.1 tnn b = 0;
255 1.1 tnn }
256 1.1 tnn bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDATA, b);
257 1.1 tnn }
258 1.1 tnn if (sc->sc_wchunk->chunk_wresid == 0) {
259 1.1 tnn sc->sc_wchunk = sc->sc_wchunk->chunk_next;
260 1.1 tnn }
261 1.1 tnn }
262 1.1 tnn
263 1.1 tnn static void
264 1.1 tnn sun4ispi_rxfifo_drain(struct sun4ispi_softc * const sc, size_t maxlen)
265 1.1 tnn {
266 1.1 tnn struct spi_chunk *chunk = sc->sc_rchunk;
267 1.1 tnn size_t len;
268 1.1 tnn uint8_t b;
269 1.1 tnn
270 1.1 tnn if (chunk == NULL)
271 1.1 tnn return;
272 1.1 tnn
273 1.1 tnn len = MIN(maxlen, chunk->chunk_rresid);
274 1.1 tnn chunk->chunk_rresid -= len;
275 1.1 tnn
276 1.1 tnn while (len--) {
277 1.1 tnn b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDATA);
278 1.1 tnn if (chunk->chunk_rptr) {
279 1.1 tnn *chunk->chunk_rptr++ = b;
280 1.1 tnn }
281 1.1 tnn }
282 1.1 tnn if (sc->sc_rchunk->chunk_rresid == 0) {
283 1.1 tnn sc->sc_rchunk = sc->sc_rchunk->chunk_next;
284 1.1 tnn }
285 1.1 tnn }
286 1.1 tnn
287 1.1 tnn static void
288 1.1 tnn sun4ispi_rxtx(struct sun4ispi_softc * const sc)
289 1.1 tnn {
290 1.1 tnn bool again;
291 1.1 tnn size_t rxavail, txavail;
292 1.1 tnn uint32_t fsr;
293 1.1 tnn
294 1.1 tnn /* service both FIFOs until no more progress can be made */
295 1.1 tnn again = true;
296 1.1 tnn while (again) {
297 1.1 tnn again = false;
298 1.1 tnn fsr = SPIREG_READ(sc, SPI_FIFO_STA);
299 1.1 tnn rxavail = __SHIFTOUT(fsr, SPI_FIFO_STA_RF_CNT);
300 1.1 tnn txavail = 64 - __SHIFTOUT(fsr, SPI_FIFO_STA_TF_CNT);
301 1.1 tnn if (rxavail > 0) {
302 1.1 tnn KASSERT(sc->sc_rchunk != NULL);
303 1.1 tnn sun4ispi_rxfifo_drain(sc, rxavail);
304 1.1 tnn again = true;
305 1.1 tnn }
306 1.1 tnn if (txavail > 0 && sc->sc_wchunk != NULL) {
307 1.1 tnn sun4ispi_txfifo_fill(sc, txavail);
308 1.1 tnn again = true;
309 1.1 tnn }
310 1.1 tnn }
311 1.1 tnn }
312 1.1 tnn
313 1.1 tnn static void
314 1.1 tnn sun4ispi_set_interrupt_mask(struct sun4ispi_softc * const sc)
315 1.1 tnn {
316 1.1 tnn uint32_t intctl;
317 1.1 tnn
318 1.1 tnn intctl = SPI_INTCTL_TX_INT_EN;
319 1.1 tnn intctl |= SPI_INTCTL_RF_OF_INT_EN;
320 1.1 tnn intctl |= SPI_INTCTL_TF_UR_INT_EN;
321 1.1 tnn
322 1.1 tnn if (sc->sc_rchunk) {
323 1.1 tnn if (sc->sc_rchunk->chunk_rresid >= 32) {
324 1.1 tnn intctl |= SPI_INTCTL_RF_HALF_FU_INT_EN;
325 1.1 tnn } else {
326 1.1 tnn intctl |= SPI_INTCTL_RF_RDY_INT_EN;
327 1.1 tnn }
328 1.1 tnn }
329 1.1 tnn if (sc->sc_wchunk) {
330 1.1 tnn intctl |= SPI_INTCTL_TF_HALF_EMP_INT_EN;
331 1.1 tnn }
332 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, intctl);
333 1.1 tnn }
334 1.1 tnn
335 1.1 tnn static void
336 1.1 tnn sun4ispi_start(struct sun4ispi_softc * const sc)
337 1.1 tnn {
338 1.1 tnn struct spi_transfer *st;
339 1.1 tnn uint32_t ctl;
340 1.1 tnn int s;
341 1.1 tnn struct spi_chunk *chunk;
342 1.1 tnn size_t burstcount;
343 1.1 tnn
344 1.1 tnn while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
345 1.1 tnn
346 1.1 tnn spi_transq_dequeue(&sc->sc_q);
347 1.1 tnn
348 1.1 tnn KASSERT(sc->sc_transfer == NULL);
349 1.1 tnn sc->sc_transfer = st;
350 1.1 tnn sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
351 1.1 tnn sc->sc_running = true;
352 1.1 tnn
353 1.1 tnn burstcount = 0;
354 1.1 tnn for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
355 1.1 tnn burstcount += chunk->chunk_count;
356 1.1 tnn }
357 1.1 tnn KASSERT(burstcount <= SPI_BC_BC);
358 1.1 tnn SPIREG_WRITE(sc, SPI_BC, __SHIFTIN(burstcount, SPI_BC_BC));
359 1.1 tnn SPIREG_WRITE(sc, SPI_TC, __SHIFTIN(burstcount, SPI_TC_WTC));
360 1.1 tnn
361 1.1 tnn sun4ispi_rxtx(sc);
362 1.1 tnn sun4ispi_set_interrupt_mask(sc);
363 1.1 tnn
364 1.1 tnn KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
365 1.1 tnn ctl = sc->sc_CTL | __SHIFTIN(st->st_slave, SPI_CTL_SS) | SPI_CTL_XCH;
366 1.1 tnn SPIREG_WRITE(sc, SPI_CTL, ctl);
367 1.1 tnn
368 1.1 tnn if (!cold)
369 1.1 tnn return;
370 1.1 tnn
371 1.1 tnn s = splbio();
372 1.1 tnn for (;;) {
373 1.1 tnn (void) sun4ispi_intr(sc);
374 1.1 tnn if (ISSET(st->st_flags, SPI_F_DONE))
375 1.1 tnn break;
376 1.1 tnn }
377 1.1 tnn splx(s);
378 1.1 tnn }
379 1.1 tnn sc->sc_running = false;
380 1.1 tnn }
381 1.1 tnn
382 1.1 tnn static int
383 1.1 tnn sun4ispi_intr(void *cookie)
384 1.1 tnn {
385 1.1 tnn struct sun4ispi_softc * const sc = cookie;
386 1.1 tnn struct spi_transfer *st;
387 1.1 tnn uint32_t isr;
388 1.1 tnn
389 1.1 tnn isr = SPIREG_READ(sc, SPI_INT_STA);
390 1.1 tnn if (!isr)
391 1.1 tnn return 0;
392 1.1 tnn
393 1.1 tnn if (ISSET(isr, SPI_INT_STA_RO)) {
394 1.1 tnn device_printf(sc->sc_dev, "RXFIFO overflow\n");
395 1.1 tnn }
396 1.1 tnn if (ISSET(isr, SPI_INT_STA_TU)) {
397 1.1 tnn device_printf(sc->sc_dev, "TXFIFO underrun\n");
398 1.1 tnn }
399 1.1 tnn
400 1.1 tnn sun4ispi_rxtx(sc);
401 1.1 tnn
402 1.1 tnn if (ISSET(isr, SPI_INT_STA_TC)) {
403 1.1 tnn SPIREG_WRITE(sc, SPI_INTCTL, 0);
404 1.1 tnn KASSERT(sc->sc_rchunk == NULL);
405 1.1 tnn KASSERT(sc->sc_wchunk == NULL);
406 1.1 tnn st = sc->sc_transfer;
407 1.1 tnn sc->sc_transfer = NULL;
408 1.1 tnn KASSERT(st != NULL);
409 1.1 tnn spi_done(st, 0);
410 1.1 tnn sc->sc_running = false;
411 1.1 tnn } else {
412 1.1 tnn sun4ispi_set_interrupt_mask(sc);
413 1.1 tnn }
414 1.1 tnn SPIREG_WRITE(sc, SPI_INT_STA, isr);
415 1.1 tnn
416 1.1 tnn return 1;
417 1.1 tnn }
418