imxspi.c revision 1.2.6.2 1 1.2.6.2 yamt /* $NetBSD: imxspi.c,v 1.2.6.2 2014/05/22 11:39:32 yamt Exp $ */
2 1.2.6.2 yamt
3 1.2.6.2 yamt /*-
4 1.2.6.2 yamt * Copyright (c) 2014 Genetec Corporation. All rights reserved.
5 1.2.6.2 yamt * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.2.6.2 yamt *
7 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 yamt * modification, are permitted provided that the following conditions
9 1.2.6.2 yamt * are met:
10 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
15 1.2.6.2 yamt *
16 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17 1.2.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
20 1.2.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
27 1.2.6.2 yamt */
28 1.2.6.2 yamt
29 1.2.6.2 yamt /*
30 1.2.6.2 yamt * this module support CSPI and eCSPI.
31 1.2.6.2 yamt * i.MX51 have 2 eCSPI and 1 CSPI modules.
32 1.2.6.2 yamt */
33 1.2.6.2 yamt
34 1.2.6.2 yamt #include <sys/cdefs.h>
35 1.2.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: imxspi.c,v 1.2.6.2 2014/05/22 11:39:32 yamt Exp $");
36 1.2.6.2 yamt
37 1.2.6.2 yamt #include "opt_imx.h"
38 1.2.6.2 yamt #include "opt_imxspi.h"
39 1.2.6.2 yamt
40 1.2.6.2 yamt #include <sys/param.h>
41 1.2.6.2 yamt #include <sys/systm.h>
42 1.2.6.2 yamt #include <sys/kernel.h>
43 1.2.6.2 yamt #include <sys/device.h>
44 1.2.6.2 yamt #include <sys/errno.h>
45 1.2.6.2 yamt #include <sys/proc.h>
46 1.2.6.2 yamt #include <sys/intr.h>
47 1.2.6.2 yamt
48 1.2.6.2 yamt #include <sys/bus.h>
49 1.2.6.2 yamt #include <machine/cpu.h>
50 1.2.6.2 yamt #include <machine/intr.h>
51 1.2.6.2 yamt
52 1.2.6.2 yamt #include <arm/imx/imxspivar.h>
53 1.2.6.2 yamt #include <arm/imx/imxspireg.h>
54 1.2.6.2 yamt
55 1.2.6.2 yamt /* SPI service routines */
56 1.2.6.2 yamt static int imxspi_configure_enhanced(void *, int, int, int);
57 1.2.6.2 yamt static int imxspi_configure(void *, int, int, int);
58 1.2.6.2 yamt static int imxspi_transfer(void *, struct spi_transfer *);
59 1.2.6.2 yamt static int imxspi_intr(void *);
60 1.2.6.2 yamt
61 1.2.6.2 yamt /* internal stuff */
62 1.2.6.2 yamt void imxspi_done(struct imxspi_softc *, int);
63 1.2.6.2 yamt void imxspi_send(struct imxspi_softc *);
64 1.2.6.2 yamt void imxspi_recv(struct imxspi_softc *);
65 1.2.6.2 yamt void imxspi_sched(struct imxspi_softc *);
66 1.2.6.2 yamt
67 1.2.6.2 yamt #define IMXSPI(x) \
68 1.2.6.2 yamt ((sc->sc_enhanced) ? __CONCAT(ECSPI_, x) : __CONCAT(CSPI_, x))
69 1.2.6.2 yamt #define READ_REG(sc, x) \
70 1.2.6.2 yamt bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXSPI(x))
71 1.2.6.2 yamt #define WRITE_REG(sc, x, v) \
72 1.2.6.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXSPI(x), (v))
73 1.2.6.2 yamt
74 1.2.6.2 yamt #ifdef IMXSPI_DEBUG
75 1.2.6.2 yamt int imxspi_debug = IMXSPI_DEBUG;
76 1.2.6.2 yamt #define DPRINTFN(n,x) if (imxspi_debug>(n)) printf x;
77 1.2.6.2 yamt #else
78 1.2.6.2 yamt #define DPRINTFN(n,x)
79 1.2.6.2 yamt #endif
80 1.2.6.2 yamt
81 1.2.6.2 yamt int
82 1.2.6.2 yamt imxspi_attach_common(device_t parent, struct imxspi_softc *sc, void *aux)
83 1.2.6.2 yamt {
84 1.2.6.2 yamt struct imxspi_attach_args *saa = aux;
85 1.2.6.2 yamt struct spibus_attach_args sba;
86 1.2.6.2 yamt bus_addr_t addr = saa->saa_addr;
87 1.2.6.2 yamt bus_size_t size = saa->saa_size;
88 1.2.6.2 yamt
89 1.2.6.2 yamt sc->sc_iot = saa->saa_iot;
90 1.2.6.2 yamt sc->sc_freq = saa->saa_freq;
91 1.2.6.2 yamt sc->sc_tag = saa->saa_tag;
92 1.2.6.2 yamt sc->sc_enhanced = saa->saa_enhanced;
93 1.2.6.2 yamt if (size <= 0)
94 1.2.6.2 yamt size = SPI_SIZE;
95 1.2.6.2 yamt
96 1.2.6.2 yamt if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
97 1.2.6.2 yamt aprint_error_dev(sc->sc_dev, "couldn't map registers\n");
98 1.2.6.2 yamt return 1;
99 1.2.6.2 yamt }
100 1.2.6.2 yamt
101 1.2.6.2 yamt aprint_normal(": i.MX %sCSPI Controller (clock %ld Hz)\n",
102 1.2.6.2 yamt ((sc->sc_enhanced) ? "e" : ""), sc->sc_freq);
103 1.2.6.2 yamt
104 1.2.6.2 yamt /* Initialize SPI controller */
105 1.2.6.2 yamt sc->sc_spi.sct_cookie = sc;
106 1.2.6.2 yamt if (sc->sc_enhanced)
107 1.2.6.2 yamt sc->sc_spi.sct_configure = imxspi_configure_enhanced;
108 1.2.6.2 yamt else
109 1.2.6.2 yamt sc->sc_spi.sct_configure = imxspi_configure;
110 1.2.6.2 yamt sc->sc_spi.sct_transfer = imxspi_transfer;
111 1.2.6.2 yamt
112 1.2.6.2 yamt /* sc->sc_spi.sct_nslaves must have been initialized by machdep code */
113 1.2.6.2 yamt sc->sc_spi.sct_nslaves = saa->saa_nslaves;
114 1.2.6.2 yamt if (!sc->sc_spi.sct_nslaves)
115 1.2.6.2 yamt aprint_error_dev(sc->sc_dev, "no slaves!\n");
116 1.2.6.2 yamt
117 1.2.6.2 yamt sba.sba_controller = &sc->sc_spi;
118 1.2.6.2 yamt
119 1.2.6.2 yamt /* initialize the queue */
120 1.2.6.2 yamt SIMPLEQ_INIT(&sc->sc_q);
121 1.2.6.2 yamt
122 1.2.6.2 yamt /* configure SPI */
123 1.2.6.2 yamt /* Setup Control Register */
124 1.2.6.2 yamt WRITE_REG(sc, CONREG, __SHIFTIN(0, IMXSPI(CON_DRCTL)) |
125 1.2.6.2 yamt __SHIFTIN(8 - 1, IMXSPI(CON_BITCOUNT)) |
126 1.2.6.2 yamt __SHIFTIN(0xf, IMXSPI(CON_MODE)) | IMXSPI(CON_ENABLE));
127 1.2.6.2 yamt
128 1.2.6.2 yamt /* TC and RR interruption */
129 1.2.6.2 yamt WRITE_REG(sc, INTREG, (IMXSPI(INTR_TC_EN) | IMXSPI(INTR_RR_EN)));
130 1.2.6.2 yamt WRITE_REG(sc, STATREG, IMXSPI(STAT_CLR));
131 1.2.6.2 yamt
132 1.2.6.2 yamt WRITE_REG(sc, PERIODREG, 0x0);
133 1.2.6.2 yamt
134 1.2.6.2 yamt /* enable device interrupts */
135 1.2.6.2 yamt sc->sc_ih = intr_establish(saa->saa_irq, IPL_BIO, IST_LEVEL,
136 1.2.6.2 yamt imxspi_intr, sc);
137 1.2.6.2 yamt
138 1.2.6.2 yamt /* attach slave devices */
139 1.2.6.2 yamt (void)config_found_ia(sc->sc_dev, "spibus", &sba, spibus_print);
140 1.2.6.2 yamt
141 1.2.6.2 yamt return 0;
142 1.2.6.2 yamt }
143 1.2.6.2 yamt
144 1.2.6.2 yamt static int
145 1.2.6.2 yamt imxspi_configure(void *arg, int slave, int mode, int speed)
146 1.2.6.2 yamt {
147 1.2.6.2 yamt struct imxspi_softc *sc = arg;
148 1.2.6.2 yamt uint32_t div_cnt = 0;
149 1.2.6.2 yamt uint32_t div;
150 1.2.6.2 yamt uint32_t contrl = 0;
151 1.2.6.2 yamt
152 1.2.6.2 yamt div = (sc->sc_freq + (speed - 1)) / speed;
153 1.2.6.2 yamt div = div - 1;
154 1.2.6.2 yamt for (div_cnt = 0; div > 0; div_cnt++)
155 1.2.6.2 yamt div >>= 1;
156 1.2.6.2 yamt
157 1.2.6.2 yamt div_cnt = div_cnt - 2;
158 1.2.6.2 yamt if (div_cnt >= 7)
159 1.2.6.2 yamt div_cnt = 7;
160 1.2.6.2 yamt
161 1.2.6.2 yamt contrl = READ_REG(sc, CONREG);
162 1.2.6.2 yamt contrl &= ~CSPI_CON_DIV;
163 1.2.6.2 yamt contrl |= __SHIFTIN(div_cnt, CSPI_CON_DIV);
164 1.2.6.2 yamt
165 1.2.6.2 yamt contrl &= ~(CSPI_CON_POL | CSPI_CON_PHA);
166 1.2.6.2 yamt switch (mode) {
167 1.2.6.2 yamt case SPI_MODE_0:
168 1.2.6.2 yamt /* CPHA = 0, CPOL = 0 */
169 1.2.6.2 yamt break;
170 1.2.6.2 yamt case SPI_MODE_1:
171 1.2.6.2 yamt /* CPHA = 1, CPOL = 0 */
172 1.2.6.2 yamt contrl |= CSPI_CON_PHA;
173 1.2.6.2 yamt break;
174 1.2.6.2 yamt case SPI_MODE_2:
175 1.2.6.2 yamt /* CPHA = 0, CPOL = 1 */
176 1.2.6.2 yamt contrl |= CSPI_CON_POL;
177 1.2.6.2 yamt break;
178 1.2.6.2 yamt case SPI_MODE_3:
179 1.2.6.2 yamt /* CPHA = 1, CPOL = 1 */
180 1.2.6.2 yamt contrl |= CSPI_CON_POL;
181 1.2.6.2 yamt contrl |= CSPI_CON_PHA;
182 1.2.6.2 yamt break;
183 1.2.6.2 yamt default:
184 1.2.6.2 yamt return EINVAL;
185 1.2.6.2 yamt }
186 1.2.6.2 yamt WRITE_REG(sc, CONREG, contrl);
187 1.2.6.2 yamt
188 1.2.6.2 yamt DPRINTFN(3, ("%s: slave %d mode %d speed %d\n",
189 1.2.6.2 yamt __func__, slave, mode, speed));
190 1.2.6.2 yamt
191 1.2.6.2 yamt return 0;
192 1.2.6.2 yamt }
193 1.2.6.2 yamt
194 1.2.6.2 yamt static int
195 1.2.6.2 yamt imxspi_configure_enhanced(void *arg, int slave, int mode, int speed)
196 1.2.6.2 yamt {
197 1.2.6.2 yamt struct imxspi_softc *sc = arg;
198 1.2.6.2 yamt uint32_t div_cnt = 0;
199 1.2.6.2 yamt uint32_t div;
200 1.2.6.2 yamt uint32_t contrl = 0;
201 1.2.6.2 yamt uint32_t config = 0;
202 1.2.6.2 yamt
203 1.2.6.2 yamt div = (sc->sc_freq + (speed - 1)) / speed;
204 1.2.6.2 yamt for (div_cnt = 0; div > 0; div_cnt++)
205 1.2.6.2 yamt div >>= 1;
206 1.2.6.2 yamt
207 1.2.6.2 yamt if (div_cnt >= 15)
208 1.2.6.2 yamt div_cnt = 15;
209 1.2.6.2 yamt
210 1.2.6.2 yamt contrl = READ_REG(sc, CONREG);
211 1.2.6.2 yamt contrl |= __SHIFTIN(div_cnt, ECSPI_CON_DIV);
212 1.2.6.2 yamt contrl |= __SHIFTIN(slave, ECSPI_CON_CS);
213 1.2.6.2 yamt contrl |= __SHIFTIN(__BIT(slave), ECSPI_CON_MODE);
214 1.2.6.2 yamt WRITE_REG(sc, CONREG, contrl);
215 1.2.6.2 yamt
216 1.2.6.2 yamt config = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ECSPI_CONFIGREG);
217 1.2.6.2 yamt config &= ~(__SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_POL) |
218 1.2.6.2 yamt __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_PHA));
219 1.2.6.2 yamt switch (mode) {
220 1.2.6.2 yamt case SPI_MODE_0:
221 1.2.6.2 yamt /* CPHA = 0, CPOL = 0 */
222 1.2.6.2 yamt break;
223 1.2.6.2 yamt case SPI_MODE_1:
224 1.2.6.2 yamt /* CPHA = 1, CPOL = 0 */
225 1.2.6.2 yamt config |= __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_PHA);
226 1.2.6.2 yamt break;
227 1.2.6.2 yamt case SPI_MODE_2:
228 1.2.6.2 yamt /* CPHA = 0, CPOL = 1 */
229 1.2.6.2 yamt config |= __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_POL);
230 1.2.6.2 yamt break;
231 1.2.6.2 yamt case SPI_MODE_3:
232 1.2.6.2 yamt /* CPHA = 1, CPOL = 1 */
233 1.2.6.2 yamt config |= __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_PHA);
234 1.2.6.2 yamt config |= __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SCLK_POL);
235 1.2.6.2 yamt break;
236 1.2.6.2 yamt default:
237 1.2.6.2 yamt return EINVAL;
238 1.2.6.2 yamt }
239 1.2.6.2 yamt config |= __SHIFTIN(__BIT(slave), ECSPI_CONFIG_SSB_CTL);
240 1.2.6.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, ECSPI_CONFIGREG, config);
241 1.2.6.2 yamt
242 1.2.6.2 yamt DPRINTFN(3, ("%s: slave %d mode %d speed %d\n",
243 1.2.6.2 yamt __func__, slave, mode, speed));
244 1.2.6.2 yamt
245 1.2.6.2 yamt return 0;
246 1.2.6.2 yamt }
247 1.2.6.2 yamt
248 1.2.6.2 yamt void
249 1.2.6.2 yamt imxspi_send(struct imxspi_softc *sc)
250 1.2.6.2 yamt {
251 1.2.6.2 yamt uint32_t data;
252 1.2.6.2 yamt struct spi_chunk *chunk;
253 1.2.6.2 yamt
254 1.2.6.2 yamt /* fill the fifo */
255 1.2.6.2 yamt while ((chunk = sc->sc_wchunk) != NULL) {
256 1.2.6.2 yamt while (chunk->chunk_wresid) {
257 1.2.6.2 yamt /* transmit fifo full? */
258 1.2.6.2 yamt if (READ_REG(sc, STATREG) & IMXSPI(STAT_TF))
259 1.2.6.2 yamt return;
260 1.2.6.2 yamt
261 1.2.6.2 yamt if (chunk->chunk_wptr) {
262 1.2.6.2 yamt data = *chunk->chunk_wptr;
263 1.2.6.2 yamt chunk->chunk_wptr++;
264 1.2.6.2 yamt } else {
265 1.2.6.2 yamt data = 0xff;
266 1.2.6.2 yamt }
267 1.2.6.2 yamt chunk->chunk_wresid--;
268 1.2.6.2 yamt
269 1.2.6.2 yamt WRITE_REG(sc, TXDATA, data);
270 1.2.6.2 yamt }
271 1.2.6.2 yamt /* advance to next transfer */
272 1.2.6.2 yamt sc->sc_wchunk = sc->sc_wchunk->chunk_next;
273 1.2.6.2 yamt }
274 1.2.6.2 yamt
275 1.2.6.2 yamt if (!(READ_REG(sc, STATREG) & IMXSPI(INTR_TE_EN)))
276 1.2.6.2 yamt WRITE_REG(sc, CONREG, READ_REG(sc, CONREG) | IMXSPI(CON_XCH));
277 1.2.6.2 yamt }
278 1.2.6.2 yamt
279 1.2.6.2 yamt void
280 1.2.6.2 yamt imxspi_recv(struct imxspi_softc *sc)
281 1.2.6.2 yamt {
282 1.2.6.2 yamt uint32_t data;
283 1.2.6.2 yamt struct spi_chunk *chunk;
284 1.2.6.2 yamt
285 1.2.6.2 yamt while ((chunk = sc->sc_rchunk) != NULL) {
286 1.2.6.2 yamt while (chunk->chunk_rresid) {
287 1.2.6.2 yamt /* rx fifo empty? */
288 1.2.6.2 yamt if ((!(READ_REG(sc, STATREG) & IMXSPI(STAT_RR))))
289 1.2.6.2 yamt return;
290 1.2.6.2 yamt
291 1.2.6.2 yamt /* collect rx data */
292 1.2.6.2 yamt data = READ_REG(sc, RXDATA);
293 1.2.6.2 yamt if (chunk->chunk_rptr) {
294 1.2.6.2 yamt *chunk->chunk_rptr = data & 0xff;
295 1.2.6.2 yamt chunk->chunk_rptr++;
296 1.2.6.2 yamt }
297 1.2.6.2 yamt
298 1.2.6.2 yamt chunk->chunk_rresid--;
299 1.2.6.2 yamt }
300 1.2.6.2 yamt /* advance next to next transfer */
301 1.2.6.2 yamt sc->sc_rchunk = sc->sc_rchunk->chunk_next;
302 1.2.6.2 yamt }
303 1.2.6.2 yamt }
304 1.2.6.2 yamt
305 1.2.6.2 yamt
306 1.2.6.2 yamt void
307 1.2.6.2 yamt imxspi_sched(struct imxspi_softc *sc)
308 1.2.6.2 yamt {
309 1.2.6.2 yamt struct spi_transfer *st;
310 1.2.6.2 yamt uint32_t chipselect;
311 1.2.6.2 yamt
312 1.2.6.2 yamt while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
313 1.2.6.2 yamt /* remove the item */
314 1.2.6.2 yamt spi_transq_dequeue(&sc->sc_q);
315 1.2.6.2 yamt
316 1.2.6.2 yamt /* note that we are working on it */
317 1.2.6.2 yamt sc->sc_transfer = st;
318 1.2.6.2 yamt
319 1.2.6.2 yamt /* chip slect */
320 1.2.6.2 yamt if (sc->sc_tag->spi_cs_enable != NULL)
321 1.2.6.2 yamt sc->sc_tag->spi_cs_enable(sc->sc_tag->cookie,
322 1.2.6.2 yamt st->st_slave);
323 1.2.6.2 yamt
324 1.2.6.2 yamt /*chip slect*/
325 1.2.6.2 yamt chipselect = READ_REG(sc, CONREG);
326 1.2.6.2 yamt chipselect &= ~IMXSPI(CON_CS);
327 1.2.6.2 yamt chipselect |= __SHIFTIN(st->st_slave, IMXSPI(CON_CS));
328 1.2.6.2 yamt WRITE_REG(sc, CONREG, chipselect);
329 1.2.6.2 yamt
330 1.2.6.2 yamt delay(1);
331 1.2.6.2 yamt
332 1.2.6.2 yamt /* setup chunks */
333 1.2.6.2 yamt sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
334 1.2.6.2 yamt
335 1.2.6.2 yamt /* now kick the master start to get the chip running */
336 1.2.6.2 yamt imxspi_send(sc);
337 1.2.6.2 yamt
338 1.2.6.2 yamt sc->sc_running = TRUE;
339 1.2.6.2 yamt return;
340 1.2.6.2 yamt }
341 1.2.6.2 yamt
342 1.2.6.2 yamt DPRINTFN(2, ("%s: nothing to do anymore\n", __func__));
343 1.2.6.2 yamt sc->sc_running = FALSE;
344 1.2.6.2 yamt }
345 1.2.6.2 yamt
346 1.2.6.2 yamt void
347 1.2.6.2 yamt imxspi_done(struct imxspi_softc *sc, int err)
348 1.2.6.2 yamt {
349 1.2.6.2 yamt struct spi_transfer *st;
350 1.2.6.2 yamt
351 1.2.6.2 yamt /* called from interrupt handler */
352 1.2.6.2 yamt if ((st = sc->sc_transfer) != NULL) {
353 1.2.6.2 yamt if (sc->sc_tag->spi_cs_disable != NULL)
354 1.2.6.2 yamt sc->sc_tag->spi_cs_disable(sc->sc_tag->cookie,
355 1.2.6.2 yamt st->st_slave);
356 1.2.6.2 yamt
357 1.2.6.2 yamt sc->sc_transfer = NULL;
358 1.2.6.2 yamt spi_done(st, err);
359 1.2.6.2 yamt }
360 1.2.6.2 yamt /* make sure we clear these bits out */
361 1.2.6.2 yamt sc->sc_wchunk = sc->sc_rchunk = NULL;
362 1.2.6.2 yamt imxspi_sched(sc);
363 1.2.6.2 yamt }
364 1.2.6.2 yamt
365 1.2.6.2 yamt static int
366 1.2.6.2 yamt imxspi_intr(void *arg)
367 1.2.6.2 yamt {
368 1.2.6.2 yamt struct imxspi_softc *sc = arg;
369 1.2.6.2 yamt uint32_t intr, sr;
370 1.2.6.2 yamt int err = 0;
371 1.2.6.2 yamt
372 1.2.6.2 yamt if ((intr = READ_REG(sc, INTREG)) == 0) {
373 1.2.6.2 yamt /* interrupts are not enabled, get out */
374 1.2.6.2 yamt DPRINTFN(4, ("%s: interrupts are not enabled\n", __func__));
375 1.2.6.2 yamt return 0;
376 1.2.6.2 yamt }
377 1.2.6.2 yamt
378 1.2.6.2 yamt sr = READ_REG(sc, STATREG);
379 1.2.6.2 yamt if (!(sr & intr)) {
380 1.2.6.2 yamt /* interrupt did not happen, get out */
381 1.2.6.2 yamt DPRINTFN(3, ("%s: interrupts did not happen\n", __func__));
382 1.2.6.2 yamt return 0;
383 1.2.6.2 yamt }
384 1.2.6.2 yamt
385 1.2.6.2 yamt /* Transfer Conplete? */
386 1.2.6.2 yamt if (sr & IMXSPI(INTR_TC_EN)) {
387 1.2.6.2 yamt /* complete TX */
388 1.2.6.2 yamt imxspi_send(sc);
389 1.2.6.2 yamt }
390 1.2.6.2 yamt
391 1.2.6.2 yamt /* RXFIFO ready */
392 1.2.6.2 yamt if (sr & IMXSPI(INTR_RR_EN)) {
393 1.2.6.2 yamt imxspi_recv(sc);
394 1.2.6.2 yamt if (sc->sc_wchunk == NULL && sc->sc_rchunk == NULL)
395 1.2.6.2 yamt imxspi_done(sc, err);
396 1.2.6.2 yamt }
397 1.2.6.2 yamt
398 1.2.6.2 yamt /* status register clear */
399 1.2.6.2 yamt WRITE_REG(sc, STATREG, sr);
400 1.2.6.2 yamt
401 1.2.6.2 yamt return 1;
402 1.2.6.2 yamt }
403 1.2.6.2 yamt
404 1.2.6.2 yamt int
405 1.2.6.2 yamt imxspi_transfer(void *arg, struct spi_transfer *st)
406 1.2.6.2 yamt {
407 1.2.6.2 yamt struct imxspi_softc *sc = arg;
408 1.2.6.2 yamt int s;
409 1.2.6.2 yamt
410 1.2.6.2 yamt /* make sure we select the right chip */
411 1.2.6.2 yamt s = splbio();
412 1.2.6.2 yamt spi_transq_enqueue(&sc->sc_q, st);
413 1.2.6.2 yamt if (sc->sc_running == FALSE)
414 1.2.6.2 yamt imxspi_sched(sc);
415 1.2.6.2 yamt splx(s);
416 1.2.6.2 yamt
417 1.2.6.2 yamt return 0;
418 1.2.6.2 yamt }
419 1.2.6.2 yamt
420