rk_spi.c revision 1.5 1 1.5 ryo /* $NetBSD: rk_spi.c,v 1.5 2021/01/15 18:42:41 ryo Exp $ */
2 1.1 tnn
3 1.1 tnn /*
4 1.1 tnn * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 tnn * All rights reserved.
6 1.1 tnn *
7 1.1 tnn * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tnn * by Tobias Nygren.
9 1.1 tnn *
10 1.1 tnn * Redistribution and use in source and binary forms, with or without
11 1.1 tnn * modification, are permitted provided that the following conditions
12 1.1 tnn * are met:
13 1.1 tnn * 1. Redistributions of source code must retain the above copyright
14 1.1 tnn * notice, this list of conditions and the following disclaimer.
15 1.1 tnn * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tnn * notice, this list of conditions and the following disclaimer in the
17 1.1 tnn * documentation and/or other materials provided with the distribution.
18 1.1 tnn *
19 1.1 tnn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tnn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tnn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tnn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tnn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tnn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tnn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tnn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tnn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tnn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tnn * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tnn */
31 1.1 tnn
32 1.1 tnn #include <sys/cdefs.h>
33 1.5 ryo __KERNEL_RCSID(0, "$NetBSD: rk_spi.c,v 1.5 2021/01/15 18:42:41 ryo Exp $");
34 1.1 tnn
35 1.1 tnn #include <sys/param.h>
36 1.1 tnn #include <sys/device.h>
37 1.1 tnn #include <sys/systm.h>
38 1.1 tnn #include <sys/bus.h>
39 1.1 tnn #include <sys/intr.h>
40 1.1 tnn #include <sys/kernel.h>
41 1.1 tnn #include <sys/bitops.h>
42 1.1 tnn #include <dev/spi/spivar.h>
43 1.1 tnn #include <dev/fdt/fdtvar.h>
44 1.1 tnn #include <arm/fdt/arm_fdtvar.h>
45 1.1 tnn
46 1.1 tnn #define SPI_CTRLR0 0x00
47 1.1 tnn #define SPI_CTRLR0_MTM __BIT(21)
48 1.1 tnn #define SPI_CTRLR0_OPM __BIT(20)
49 1.1 tnn #define SPI_CTRLR0_XFM __BITS(19, 18)
50 1.1 tnn #define SPI_CTRLR0_FRF __BITS(17, 16)
51 1.1 tnn #define SPI_CTRLR0_RSD __BITS(15, 14)
52 1.1 tnn #define SPI_CTRLR0_BHT __BIT(13)
53 1.1 tnn #define SPI_CTRLR0_FBM __BIT(12)
54 1.1 tnn #define SPI_CTRLR0_EM __BIT(11)
55 1.1 tnn #define SPI_CTRLR0_RW __BIT(10)
56 1.1 tnn #define SPI_CTRLR0_CSM __BITS(9, 8)
57 1.1 tnn #define SPI_CTRLR0_SCPOL __BIT(7)
58 1.1 tnn #define SPI_CTRLR0_SCPH __BIT(6)
59 1.1 tnn #define SPI_CTRLR0_CFS __BITS(5, 2)
60 1.1 tnn #define SPI_CTRLR0_DFS __BITS(1, 0)
61 1.1 tnn #define SPI_CTRLR0_DFS_4BIT 0x0
62 1.1 tnn #define SPI_CTRLR0_DFS_8BIT 0x1
63 1.1 tnn #define SPI_CTRLR0_DFS_16BIT 0x2
64 1.1 tnn
65 1.1 tnn #define SPI_CTRLR1 0x04
66 1.1 tnn #define SPI_CTRLR1_NDM __BITS(15, 0)
67 1.1 tnn
68 1.1 tnn #define SPI_ENR 0x08
69 1.1 tnn #define SPI_ENR_ENR __BIT(0)
70 1.1 tnn
71 1.1 tnn #define SPI_SER 0x0c
72 1.1 tnn #define SPI_SER_SER1 __BIT(1)
73 1.1 tnn #define SPI_SER_SER0 __BIT(0)
74 1.1 tnn
75 1.1 tnn #define SPI_BAUDR 0x10
76 1.1 tnn #define SPI_BAUDR_BAUDR __BITS(15, 0)
77 1.1 tnn
78 1.1 tnn #define SPI_TXFTLR 0x14
79 1.1 tnn #define SPI_TXFTLR_TXFLTR __BITS(4, 0)
80 1.1 tnn
81 1.1 tnn #define SPI_RXFTLR 0x18
82 1.1 tnn #define SPI_RXFLTR_RXFLTR __BITS(4, 0)
83 1.1 tnn
84 1.1 tnn #define SPI_TXFLR 0x1c
85 1.1 tnn #define SPI_TXFLR_TXFLR __BITS(5, 0)
86 1.1 tnn
87 1.1 tnn #define SPI_RXFLR 0x20
88 1.1 tnn #define SPI_RXFLR_RXFLR __BITS(5, 0)
89 1.1 tnn
90 1.1 tnn #define SPI_SR 0x24
91 1.1 tnn #define SPI_SR_RFF __BIT(4)
92 1.1 tnn #define SPI_SR_RFE __BIT(3)
93 1.1 tnn #define SPI_SR_TFE __BIT(2)
94 1.1 tnn #define SPI_SR_TFF __BIT(1)
95 1.1 tnn #define SPI_SR_BSF __BIT(0)
96 1.1 tnn
97 1.1 tnn #define SPI_IPR 0x28
98 1.1 tnn #define SPI_IPR_IPR __BIT(0)
99 1.1 tnn
100 1.1 tnn #define SPI_IMR 0x2c
101 1.1 tnn #define SPI_IMR_RFFIM __BIT(4)
102 1.1 tnn #define SPI_IMR_RFOIM __BIT(3)
103 1.1 tnn #define SPI_IMR_RFUIM __BIT(2)
104 1.1 tnn #define SPI_IMR_TFOIM __BIT(1)
105 1.1 tnn #define SPI_IMR_TFEIM __BIT(0)
106 1.1 tnn
107 1.1 tnn #define SPI_ISR 0x30
108 1.1 tnn #define SPI_ISR_RFFIS __BIT(4)
109 1.1 tnn #define SPI_ISR_RFOIS __BIT(3)
110 1.1 tnn #define SPI_ISR_RFUIS __BIT(2)
111 1.1 tnn #define SPI_ISR_TFOIS __BIT(1)
112 1.1 tnn #define SPI_ISR_TFEIS __BIT(0)
113 1.1 tnn
114 1.1 tnn #define SPI_RISR 0x34
115 1.1 tnn #define SPI_RISR_RFFRIS __BIT(4)
116 1.1 tnn #define SPI_RISR_RFORIS __BIT(3)
117 1.1 tnn #define SPI_RISR_RFURIS __BIT(2)
118 1.1 tnn #define SPI_RISR_TFORIS __BIT(1)
119 1.1 tnn #define SPI_RISR_TFERIS __BIT(0)
120 1.1 tnn
121 1.1 tnn #define SPI_ICR 0x38
122 1.1 tnn #define SPI_ICR_CTFOI __BIT(3)
123 1.1 tnn #define SPI_ICR_CRFOI __BIT(2)
124 1.1 tnn #define SPI_ICR_CRFUI __BIT(1)
125 1.1 tnn #define SPI_ICR_CCI __BIT(0)
126 1.1 tnn #define SPI_ICR_ALL __BITS(3, 0)
127 1.1 tnn
128 1.1 tnn #define SPI_DMACR 0x3c
129 1.1 tnn #define SPI_DMACR_TDE __BIT(1)
130 1.1 tnn #define SPI_DMACR_RDE __BIT(0)
131 1.1 tnn
132 1.1 tnn #define SPI_DMATDLR 0x40
133 1.1 tnn #define SPI_DMATDLR_TDL __BITS(4, 0)
134 1.1 tnn
135 1.1 tnn #define SPI_DMARDLR 0x44
136 1.1 tnn #define SPI_DMARDLR_RDL __BITS(4, 0)
137 1.1 tnn
138 1.1 tnn #define SPI_TXDR 0x400
139 1.1 tnn #define SPI_TXDR_TXDR __BITS(15, 0)
140 1.1 tnn
141 1.1 tnn #define SPI_RXDR 0x800
142 1.1 tnn #define SPI_RXDR_RXDR __BITS(15, 0)
143 1.1 tnn
144 1.1 tnn #define SPI_FIFOLEN 32
145 1.1 tnn
146 1.1 tnn static const char * const compatible[] = {
147 1.1 tnn #if 0 /* should work on RK3328 but untested */
148 1.1 tnn "rockchip,rk3066-spi",
149 1.1 tnn "rockchip,rk3328-spi",
150 1.1 tnn #endif
151 1.1 tnn "rockchip,rk3399-spi",
152 1.1 tnn NULL
153 1.1 tnn };
154 1.1 tnn
155 1.1 tnn struct rk_spi_softc {
156 1.1 tnn device_t sc_dev;
157 1.1 tnn bus_space_tag_t sc_bst;
158 1.1 tnn bus_space_handle_t sc_bsh;
159 1.1 tnn void *sc_ih;
160 1.1 tnn u_int sc_spi_freq;
161 1.1 tnn struct spi_controller sc_spi;
162 1.1 tnn SIMPLEQ_HEAD(,spi_transfer) sc_q;
163 1.1 tnn struct spi_transfer *sc_transfer;
164 1.1 tnn struct spi_chunk *sc_rchunk, *sc_wchunk;
165 1.1 tnn volatile bool sc_running;
166 1.1 tnn };
167 1.1 tnn
168 1.1 tnn #define SPIREG_READ(sc, reg) \
169 1.1 tnn bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
170 1.1 tnn #define SPIREG_WRITE(sc, reg, val) \
171 1.1 tnn bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
172 1.1 tnn
173 1.3 tnn static struct spi_controller *rk_spi_get_controller(device_t);
174 1.1 tnn static int rk_spi_match(device_t, cfdata_t, void *);
175 1.1 tnn static void rk_spi_attach(device_t, device_t, void *);
176 1.1 tnn
177 1.1 tnn static int rk_spi_configure(void *, int, int, int);
178 1.1 tnn static int rk_spi_transfer(void *, struct spi_transfer *);
179 1.1 tnn
180 1.1 tnn static void rk_spi_txfifo_fill(struct rk_spi_softc * const, size_t);
181 1.1 tnn static void rk_spi_rxfifo_drain(struct rk_spi_softc * const, size_t);
182 1.1 tnn static void rk_spi_rxtx(struct rk_spi_softc * const);
183 1.1 tnn static void rk_spi_set_interrupt_mask(struct rk_spi_softc * const);
184 1.1 tnn static void rk_spi_start(struct rk_spi_softc * const);
185 1.1 tnn static int rk_spi_intr(void *);
186 1.1 tnn
187 1.1 tnn CFATTACH_DECL_NEW(rk_spi, sizeof(struct rk_spi_softc),
188 1.1 tnn rk_spi_match, rk_spi_attach, NULL, NULL);
189 1.1 tnn
190 1.3 tnn static const struct fdtbus_spi_controller_func rk_spi_funcs = {
191 1.3 tnn .get_controller = rk_spi_get_controller
192 1.3 tnn };
193 1.3 tnn
194 1.3 tnn static struct spi_controller *
195 1.3 tnn rk_spi_get_controller(device_t dev)
196 1.3 tnn {
197 1.3 tnn struct rk_spi_softc * const sc = device_private(dev);
198 1.3 tnn
199 1.3 tnn return &sc->sc_spi;
200 1.3 tnn }
201 1.3 tnn
202 1.1 tnn static int
203 1.1 tnn rk_spi_match(device_t parent, cfdata_t cf, void *aux)
204 1.1 tnn {
205 1.1 tnn struct fdt_attach_args * const faa = aux;
206 1.1 tnn
207 1.1 tnn return of_match_compatible(faa->faa_phandle, compatible);
208 1.1 tnn }
209 1.1 tnn
210 1.1 tnn static void
211 1.1 tnn rk_spi_attach(device_t parent, device_t self, void *aux)
212 1.1 tnn {
213 1.1 tnn struct rk_spi_softc * const sc = device_private(self);
214 1.1 tnn struct fdt_attach_args * const faa = aux;
215 1.1 tnn const int phandle = faa->faa_phandle;
216 1.1 tnn bus_addr_t addr;
217 1.1 tnn bus_size_t size;
218 1.1 tnn struct clk *sclk, *pclk;
219 1.1 tnn char intrstr[128];
220 1.3 tnn
221 1.1 tnn sc->sc_dev = self;
222 1.1 tnn sc->sc_bst = faa->faa_bst;
223 1.1 tnn SIMPLEQ_INIT(&sc->sc_q);
224 1.1 tnn
225 1.1 tnn if ((sclk = fdtbus_clock_get(phandle, "spiclk")) == NULL
226 1.1 tnn || clk_enable(sclk) != 0) {
227 1.1 tnn aprint_error(": couldn't enable sclk\n");
228 1.1 tnn return;
229 1.1 tnn }
230 1.1 tnn
231 1.1 tnn if ((pclk = fdtbus_clock_get(phandle, "apb_pclk")) == NULL
232 1.1 tnn || clk_enable(pclk) != 0) {
233 1.1 tnn aprint_error(": couldn't enable pclk\n");
234 1.1 tnn return;
235 1.1 tnn }
236 1.1 tnn
237 1.1 tnn sc->sc_spi_freq = clk_get_rate(sclk);
238 1.1 tnn
239 1.1 tnn if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
240 1.1 tnn || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
241 1.1 tnn aprint_error(": couldn't map registers\n");
242 1.1 tnn return;
243 1.1 tnn }
244 1.1 tnn
245 1.1 tnn SPIREG_WRITE(sc, SPI_ENR, 0);
246 1.1 tnn SPIREG_WRITE(sc, SPI_IMR, 0);
247 1.1 tnn
248 1.1 tnn if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
249 1.1 tnn aprint_error(": failed to decode interrupt\n");
250 1.1 tnn return;
251 1.1 tnn }
252 1.1 tnn
253 1.5 ryo sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
254 1.5 ryo rk_spi_intr, sc, device_xname(self));
255 1.1 tnn if (sc->sc_ih == NULL) {
256 1.1 tnn aprint_error(": unable to establish interrupt\n");
257 1.1 tnn return;
258 1.1 tnn }
259 1.1 tnn
260 1.1 tnn aprint_naive("\n");
261 1.1 tnn aprint_normal(": SPI\n");
262 1.1 tnn aprint_normal_dev(self, "interrupting on %s\n", intrstr);
263 1.1 tnn
264 1.1 tnn sc->sc_spi.sct_cookie = sc;
265 1.1 tnn sc->sc_spi.sct_configure = rk_spi_configure;
266 1.1 tnn sc->sc_spi.sct_transfer = rk_spi_transfer;
267 1.1 tnn sc->sc_spi.sct_nslaves = 2;
268 1.1 tnn
269 1.3 tnn fdtbus_register_spi_controller(self, phandle, &rk_spi_funcs);
270 1.3 tnn (void) fdtbus_attach_spibus(self, phandle, spibus_print);
271 1.1 tnn }
272 1.1 tnn
273 1.1 tnn static int
274 1.1 tnn rk_spi_configure(void *cookie, int slave, int mode, int speed)
275 1.1 tnn {
276 1.1 tnn struct rk_spi_softc * const sc = cookie;
277 1.1 tnn uint32_t ctrlr0;
278 1.1 tnn uint16_t divider;
279 1.1 tnn
280 1.1 tnn divider = (sc->sc_spi_freq / speed) & ~1;
281 1.4 tnn if (divider < 2) {
282 1.4 tnn aprint_error_dev(sc->sc_dev,
283 1.4 tnn "spi_clk %u is too low for speed %u, using speed %u\n",
284 1.4 tnn sc->sc_spi_freq, speed, sc->sc_spi_freq / 2);
285 1.4 tnn divider = 2;
286 1.4 tnn }
287 1.1 tnn
288 1.1 tnn if (slave >= sc->sc_spi.sct_nslaves)
289 1.1 tnn return EINVAL;
290 1.1 tnn
291 1.1 tnn ctrlr0 = SPI_CTRLR0_BHT | __SHIFTIN(SPI_CTRLR0_DFS_8BIT, SPI_CTRLR0_DFS);
292 1.1 tnn
293 1.1 tnn switch (mode) {
294 1.1 tnn case SPI_MODE_0:
295 1.1 tnn ctrlr0 |= 0;
296 1.1 tnn break;
297 1.1 tnn case SPI_MODE_1:
298 1.1 tnn ctrlr0 |= SPI_CTRLR0_SCPH;
299 1.1 tnn break;
300 1.1 tnn case SPI_MODE_2:
301 1.1 tnn ctrlr0 |= SPI_CTRLR0_SCPOL;
302 1.1 tnn break;
303 1.1 tnn case SPI_MODE_3:
304 1.1 tnn ctrlr0 |= SPI_CTRLR0_SCPH | SPI_CTRLR0_SCPOL;
305 1.1 tnn break;
306 1.1 tnn default:
307 1.1 tnn return EINVAL;
308 1.1 tnn }
309 1.1 tnn
310 1.1 tnn SPIREG_WRITE(sc, SPI_ENR, 0);
311 1.1 tnn SPIREG_WRITE(sc, SPI_SER, 0);
312 1.1 tnn SPIREG_WRITE(sc, SPI_CTRLR0, ctrlr0);
313 1.1 tnn SPIREG_WRITE(sc, SPI_BAUDR, divider);
314 1.1 tnn
315 1.1 tnn SPIREG_WRITE(sc, SPI_DMACR, 0);
316 1.1 tnn SPIREG_WRITE(sc, SPI_DMATDLR, 0);
317 1.1 tnn SPIREG_WRITE(sc, SPI_DMARDLR, 0);
318 1.1 tnn
319 1.1 tnn SPIREG_WRITE(sc, SPI_IPR, 0);
320 1.1 tnn SPIREG_WRITE(sc, SPI_IMR, 0);
321 1.1 tnn SPIREG_WRITE(sc, SPI_ICR, SPI_ICR_ALL);
322 1.1 tnn
323 1.1 tnn SPIREG_WRITE(sc, SPI_ENR, 1);
324 1.1 tnn
325 1.1 tnn return 0;
326 1.1 tnn }
327 1.1 tnn
328 1.1 tnn static int
329 1.1 tnn rk_spi_transfer(void *cookie, struct spi_transfer *st)
330 1.1 tnn {
331 1.1 tnn struct rk_spi_softc * const sc = cookie;
332 1.1 tnn int s;
333 1.1 tnn
334 1.1 tnn s = splbio();
335 1.1 tnn spi_transq_enqueue(&sc->sc_q, st);
336 1.1 tnn if (sc->sc_running == false) {
337 1.1 tnn rk_spi_start(sc);
338 1.1 tnn }
339 1.1 tnn splx(s);
340 1.1 tnn
341 1.1 tnn return 0;
342 1.1 tnn }
343 1.1 tnn
344 1.1 tnn static void
345 1.1 tnn rk_spi_txfifo_fill(struct rk_spi_softc * const sc, size_t maxlen)
346 1.1 tnn {
347 1.1 tnn struct spi_chunk *chunk = sc->sc_wchunk;
348 1.1 tnn size_t len;
349 1.1 tnn uint8_t b;
350 1.1 tnn
351 1.1 tnn if (chunk == NULL)
352 1.1 tnn return;
353 1.1 tnn
354 1.1 tnn len = MIN(maxlen, chunk->chunk_wresid);
355 1.1 tnn chunk->chunk_wresid -= len;
356 1.1 tnn while (len--) {
357 1.1 tnn if (chunk->chunk_wptr) {
358 1.1 tnn b = *chunk->chunk_wptr++;
359 1.1 tnn } else {
360 1.1 tnn b = 0;
361 1.1 tnn }
362 1.1 tnn bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDR, b);
363 1.1 tnn }
364 1.1 tnn if (sc->sc_wchunk->chunk_wresid == 0) {
365 1.1 tnn sc->sc_wchunk = sc->sc_wchunk->chunk_next;
366 1.1 tnn }
367 1.1 tnn }
368 1.1 tnn
369 1.1 tnn static void
370 1.1 tnn rk_spi_rxfifo_drain(struct rk_spi_softc * const sc, size_t maxlen)
371 1.1 tnn {
372 1.1 tnn struct spi_chunk *chunk = sc->sc_rchunk;
373 1.1 tnn size_t len;
374 1.1 tnn uint8_t b;
375 1.1 tnn
376 1.1 tnn if (chunk == NULL)
377 1.1 tnn return;
378 1.1 tnn
379 1.1 tnn len = MIN(maxlen, chunk->chunk_rresid);
380 1.1 tnn chunk->chunk_rresid -= len;
381 1.1 tnn
382 1.1 tnn while (len--) {
383 1.1 tnn b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDR);
384 1.1 tnn if (chunk->chunk_rptr) {
385 1.1 tnn *chunk->chunk_rptr++ = b;
386 1.1 tnn }
387 1.1 tnn }
388 1.1 tnn if (sc->sc_rchunk->chunk_rresid == 0) {
389 1.1 tnn sc->sc_rchunk = sc->sc_rchunk->chunk_next;
390 1.1 tnn }
391 1.1 tnn }
392 1.1 tnn
393 1.1 tnn static void
394 1.1 tnn rk_spi_rxtx(struct rk_spi_softc * const sc)
395 1.1 tnn {
396 1.1 tnn bool again;
397 1.1 tnn uint32_t reg;
398 1.1 tnn size_t avail;
399 1.1 tnn
400 1.1 tnn /* Service both FIFOs until no more progress can be made. */
401 1.1 tnn again = true;
402 1.1 tnn while (again) {
403 1.1 tnn again = false;
404 1.1 tnn reg = SPIREG_READ(sc, SPI_RXFLR);
405 1.1 tnn avail = __SHIFTOUT(reg, SPI_RXFLR_RXFLR);
406 1.1 tnn if (avail > 0) {
407 1.1 tnn KASSERT(sc->sc_rchunk != NULL);
408 1.1 tnn rk_spi_rxfifo_drain(sc, avail);
409 1.1 tnn again = true;
410 1.1 tnn }
411 1.1 tnn reg = SPIREG_READ(sc, SPI_TXFLR);
412 1.1 tnn avail = SPI_FIFOLEN - __SHIFTOUT(reg, SPI_TXFLR_TXFLR);
413 1.1 tnn if (avail > 0 && sc->sc_wchunk != NULL) {
414 1.1 tnn rk_spi_txfifo_fill(sc, avail);
415 1.1 tnn again = true;
416 1.1 tnn }
417 1.1 tnn }
418 1.1 tnn }
419 1.1 tnn
420 1.1 tnn static void
421 1.1 tnn rk_spi_set_interrupt_mask(struct rk_spi_softc * const sc)
422 1.1 tnn {
423 1.1 tnn uint32_t imr = SPI_IMR_RFOIM | SPI_IMR_RFUIM | SPI_IMR_TFOIM;
424 1.1 tnn int len;
425 1.1 tnn
426 1.1 tnn /*
427 1.1 tnn * Delay rx interrupts until the FIFO has the # of bytes we'd
428 1.1 tnn * ideally like to receive, or FIFO is half full.
429 1.1 tnn */
430 1.1 tnn len = sc->sc_rchunk != NULL
431 1.1 tnn ? MIN(sc->sc_rchunk->chunk_rresid, SPI_FIFOLEN / 2) : 0;
432 1.1 tnn if (len > 0) {
433 1.1 tnn SPIREG_WRITE(sc, SPI_RXFTLR, len - 1);
434 1.1 tnn imr |= SPI_IMR_RFFIM;
435 1.1 tnn }
436 1.1 tnn
437 1.1 tnn /*
438 1.1 tnn * Delay tx interrupts until the FIFO can accept the # of bytes we'd
439 1.1 tnn * ideally like to transmit, or the FIFO is half empty.
440 1.1 tnn */
441 1.1 tnn len = sc->sc_wchunk != NULL
442 1.1 tnn ? MIN(sc->sc_wchunk->chunk_wresid, SPI_FIFOLEN / 2) : 0;
443 1.1 tnn if (len > 0) {
444 1.1 tnn SPIREG_WRITE(sc, SPI_TXFTLR, SPI_FIFOLEN - len);
445 1.1 tnn imr |= SPI_IMR_TFEIM;
446 1.1 tnn }
447 1.1 tnn
448 1.1 tnn /* If xfer is done, then interrupt as soon as the tx fifo is empty. */
449 1.1 tnn if (!ISSET(imr, (SPI_IMR_RFFIM | SPI_IMR_TFEIM))) {
450 1.1 tnn SPIREG_WRITE(sc, SPI_TXFTLR, 0);
451 1.1 tnn imr |= SPI_IMR_TFEIM;
452 1.1 tnn }
453 1.1 tnn
454 1.1 tnn SPIREG_WRITE(sc, SPI_IMR, imr);
455 1.1 tnn }
456 1.1 tnn
457 1.1 tnn static void
458 1.1 tnn rk_spi_start(struct rk_spi_softc * const sc)
459 1.1 tnn {
460 1.1 tnn struct spi_transfer *st;
461 1.1 tnn
462 1.1 tnn while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
463 1.1 tnn spi_transq_dequeue(&sc->sc_q);
464 1.1 tnn KASSERT(sc->sc_transfer == NULL);
465 1.1 tnn sc->sc_transfer = st;
466 1.1 tnn sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
467 1.1 tnn sc->sc_running = true;
468 1.1 tnn
469 1.1 tnn KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
470 1.1 tnn SPIREG_WRITE(sc, SPI_SER, 1 << st->st_slave);
471 1.1 tnn
472 1.1 tnn rk_spi_rxtx(sc);
473 1.1 tnn rk_spi_set_interrupt_mask(sc);
474 1.1 tnn
475 1.1 tnn if (!cold)
476 1.1 tnn return;
477 1.1 tnn
478 1.1 tnn for (;;) {
479 1.1 tnn (void) rk_spi_intr(sc);
480 1.1 tnn if (ISSET(st->st_flags, SPI_F_DONE))
481 1.1 tnn break;
482 1.1 tnn }
483 1.1 tnn }
484 1.1 tnn sc->sc_running = false;
485 1.1 tnn }
486 1.1 tnn
487 1.1 tnn static int
488 1.1 tnn rk_spi_intr(void *cookie)
489 1.1 tnn {
490 1.1 tnn struct rk_spi_softc * const sc = cookie;
491 1.1 tnn struct spi_transfer *st;
492 1.1 tnn uint32_t isr;
493 1.1 tnn uint32_t sr;
494 1.1 tnn uint32_t icr = SPI_ICR_CCI;
495 1.1 tnn
496 1.1 tnn isr = SPIREG_READ(sc, SPI_ISR);
497 1.1 tnn if (!isr)
498 1.1 tnn return 0;
499 1.1 tnn
500 1.1 tnn if (ISSET(isr, SPI_ISR_RFOIS)) {
501 1.1 tnn device_printf(sc->sc_dev, "RXFIFO overflow\n");
502 1.1 tnn icr |= SPI_ICR_CRFOI;
503 1.1 tnn }
504 1.1 tnn if (ISSET(isr, SPI_ISR_RFUIS)) {
505 1.1 tnn device_printf(sc->sc_dev, "RXFIFO underflow\n");
506 1.1 tnn icr |= SPI_ICR_CRFUI;
507 1.1 tnn }
508 1.1 tnn if (ISSET(isr, SPI_ISR_TFOIS)) {
509 1.1 tnn device_printf(sc->sc_dev, "TXFIFO overflow\n");
510 1.1 tnn icr |= SPI_ICR_CTFOI;
511 1.1 tnn }
512 1.1 tnn
513 1.1 tnn rk_spi_rxtx(sc);
514 1.1 tnn
515 1.1 tnn if (sc->sc_rchunk == NULL && sc->sc_wchunk == NULL) {
516 1.1 tnn do {
517 1.1 tnn sr = SPIREG_READ(sc, SPI_SR);
518 1.1 tnn } while (ISSET(sr, SPI_SR_BSF));
519 1.1 tnn SPIREG_WRITE(sc, SPI_IMR, 0);
520 1.1 tnn SPIREG_WRITE(sc, SPI_SER, 0);
521 1.1 tnn st = sc->sc_transfer;
522 1.1 tnn sc->sc_transfer = NULL;
523 1.1 tnn KASSERT(st != NULL);
524 1.1 tnn spi_done(st, 0);
525 1.1 tnn sc->sc_running = false;
526 1.1 tnn } else {
527 1.1 tnn rk_spi_set_interrupt_mask(sc);
528 1.1 tnn }
529 1.1 tnn
530 1.1 tnn SPIREG_WRITE(sc, SPI_ICR, icr);
531 1.1 tnn
532 1.1 tnn return 1;
533 1.1 tnn }
534