at91spi.c revision 1.1.2.1 1 1.1.2.1 matt /* $Id: at91spi.c,v 1.1.2.1 2007/11/10 02:56:36 matt Exp $ */
2 1.1.2.1 matt /* $NetBSD: at91spi.c,v 1.1.2.1 2007/11/10 02:56:36 matt Exp $ */
3 1.1.2.1 matt
4 1.1.2.1 matt /*-
5 1.1.2.1 matt * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
6 1.1.2.1 matt *
7 1.1.2.1 matt * Based on arch/mips/alchemy/dev/auspi.c,
8 1.1.2.1 matt * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
9 1.1.2.1 matt * Copyright (c) 2006 Garrett D'Amore.
10 1.1.2.1 matt * All rights reserved.
11 1.1.2.1 matt *
12 1.1.2.1 matt * Portions of this code were written by Garrett D'Amore for the
13 1.1.2.1 matt * Champaign-Urbana Community Wireless Network Project.
14 1.1.2.1 matt *
15 1.1.2.1 matt * Redistribution and use in source and binary forms, with or
16 1.1.2.1 matt * without modification, are permitted provided that the following
17 1.1.2.1 matt * conditions are met:
18 1.1.2.1 matt * 1. Redistributions of source code must retain the above copyright
19 1.1.2.1 matt * notice, this list of conditions and the following disclaimer.
20 1.1.2.1 matt * 2. Redistributions in binary form must reproduce the above
21 1.1.2.1 matt * copyright notice, this list of conditions and the following
22 1.1.2.1 matt * disclaimer in the documentation and/or other materials provided
23 1.1.2.1 matt * with the distribution.
24 1.1.2.1 matt * 3. All advertising materials mentioning features or use of this
25 1.1.2.1 matt * software must display the following acknowledgements:
26 1.1.2.1 matt * This product includes software developed by the Urbana-Champaign
27 1.1.2.1 matt * Independent Media Center.
28 1.1.2.1 matt * This product includes software developed by Garrett D'Amore.
29 1.1.2.1 matt * 4. Urbana-Champaign Independent Media Center's name and Garrett
30 1.1.2.1 matt * D'Amore's name may not be used to endorse or promote products
31 1.1.2.1 matt * derived from this software without specific prior written permission.
32 1.1.2.1 matt *
33 1.1.2.1 matt * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1.2.1 matt * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
35 1.1.2.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 1.1.2.1 matt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 1.1.2.1 matt * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
38 1.1.2.1 matt * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
39 1.1.2.1 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 1.1.2.1 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 1.1.2.1 matt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 1.1.2.1 matt * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 1.1.2.1 matt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 1.1.2.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 1.1.2.1 matt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 1.1.2.1 matt */
47 1.1.2.1 matt
48 1.1.2.1 matt #include <sys/cdefs.h>
49 1.1.2.1 matt __KERNEL_RCSID(0, "$NetBSD: at91spi.c,v 1.1.2.1 2007/11/10 02:56:36 matt Exp $");
50 1.1.2.1 matt
51 1.1.2.1 matt #include "locators.h"
52 1.1.2.1 matt
53 1.1.2.1 matt #include <sys/param.h>
54 1.1.2.1 matt #include <sys/systm.h>
55 1.1.2.1 matt #include <sys/kernel.h>
56 1.1.2.1 matt #include <sys/device.h>
57 1.1.2.1 matt #include <sys/errno.h>
58 1.1.2.1 matt #include <sys/proc.h>
59 1.1.2.1 matt
60 1.1.2.1 matt #include <machine/bus.h>
61 1.1.2.1 matt #include <machine/cpu.h>
62 1.1.2.1 matt #include <machine/vmparam.h>
63 1.1.2.1 matt #include <sys/inttypes.h>
64 1.1.2.1 matt
65 1.1.2.1 matt #include <arm/at91/at91var.h>
66 1.1.2.1 matt #include <arm/at91/at91reg.h>
67 1.1.2.1 matt #include <arm/at91/at91spivar.h>
68 1.1.2.1 matt #include <arm/at91/at91spireg.h>
69 1.1.2.1 matt
70 1.1.2.1 matt #define at91spi_select(sc, slave) \
71 1.1.2.1 matt (sc)->sc_md->select_slave((sc), (slave))
72 1.1.2.1 matt
73 1.1.2.1 matt #define STATIC
74 1.1.2.1 matt
75 1.1.2.1 matt //#define AT91SPI_DEBUG 4
76 1.1.2.1 matt
77 1.1.2.1 matt #ifdef AT91SPI_DEBUG
78 1.1.2.1 matt int at91spi_debug = AT91SPI_DEBUG;
79 1.1.2.1 matt #define DPRINTFN(n,x) if (at91spi_debug>(n)) printf x;
80 1.1.2.1 matt #else
81 1.1.2.1 matt #define DPRINTFN(n,x)
82 1.1.2.1 matt #endif
83 1.1.2.1 matt
84 1.1.2.1 matt STATIC int at91spi_intr(void *);
85 1.1.2.1 matt
86 1.1.2.1 matt /* SPI service routines */
87 1.1.2.1 matt STATIC int at91spi_configure(void *, int, int, int);
88 1.1.2.1 matt STATIC int at91spi_transfer(void *, struct spi_transfer *);
89 1.1.2.1 matt STATIC void at91spi_xfer(struct at91spi_softc *sc, int start);
90 1.1.2.1 matt
91 1.1.2.1 matt /* internal stuff */
92 1.1.2.1 matt STATIC void at91spi_done(struct at91spi_softc *, int);
93 1.1.2.1 matt STATIC void at91spi_send(struct at91spi_softc *);
94 1.1.2.1 matt STATIC void at91spi_recv(struct at91spi_softc *);
95 1.1.2.1 matt STATIC void at91spi_sched(struct at91spi_softc *);
96 1.1.2.1 matt
97 1.1.2.1 matt #define GETREG(sc, x) \
98 1.1.2.1 matt bus_space_read_4(sc->sc_iot, sc->sc_ioh, x)
99 1.1.2.1 matt #define PUTREG(sc, x, v) \
100 1.1.2.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, x, v)
101 1.1.2.1 matt
102 1.1.2.1 matt void
103 1.1.2.1 matt at91spi_attach_common(struct device *parent, struct device *self, void *aux,
104 1.1.2.1 matt at91spi_machdep_tag_t md)
105 1.1.2.1 matt {
106 1.1.2.1 matt struct at91spi_softc *sc = device_private(self);
107 1.1.2.1 matt struct at91bus_attach_args *sa = aux;
108 1.1.2.1 matt struct spibus_attach_args sba;
109 1.1.2.1 matt bus_dma_segment_t segs;
110 1.1.2.1 matt int rsegs, err;
111 1.1.2.1 matt
112 1.1.2.1 matt aprint_normal(": AT91 SPI Controller\n");
113 1.1.2.1 matt
114 1.1.2.1 matt sc->sc_iot = sa->sa_iot;
115 1.1.2.1 matt sc->sc_pid = sa->sa_pid;
116 1.1.2.1 matt sc->sc_dmat = sa->sa_dmat;
117 1.1.2.1 matt sc->sc_md = md;
118 1.1.2.1 matt
119 1.1.2.1 matt if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
120 1.1.2.1 matt panic("%s: Cannot map registers", self->dv_xname);
121 1.1.2.1 matt
122 1.1.2.1 matt /* we want to use dma, so allocate dma memory: */
123 1.1.2.1 matt err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
124 1.1.2.1 matt &segs, 1, &rsegs, BUS_DMA_WAITOK);
125 1.1.2.1 matt if (err == 0) {
126 1.1.2.1 matt err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
127 1.1.2.1 matt &sc->sc_dmapage,
128 1.1.2.1 matt BUS_DMA_WAITOK);
129 1.1.2.1 matt }
130 1.1.2.1 matt if (err == 0) {
131 1.1.2.1 matt err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
132 1.1.2.1 matt PAGE_SIZE, 0, BUS_DMA_WAITOK,
133 1.1.2.1 matt &sc->sc_dmamap);
134 1.1.2.1 matt }
135 1.1.2.1 matt if (err == 0) {
136 1.1.2.1 matt err = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
137 1.1.2.1 matt sc->sc_dmapage, PAGE_SIZE, NULL,
138 1.1.2.1 matt BUS_DMA_WAITOK);
139 1.1.2.1 matt }
140 1.1.2.1 matt if (err != 0) {
141 1.1.2.1 matt panic("%s: Cannot get DMA memory", sc->sc_dev.dv_xname);
142 1.1.2.1 matt }
143 1.1.2.1 matt sc->sc_dmaaddr = sc->sc_dmamap->dm_segs[0].ds_addr;
144 1.1.2.1 matt
145 1.1.2.1 matt /*
146 1.1.2.1 matt * Initialize SPI controller
147 1.1.2.1 matt */
148 1.1.2.1 matt sc->sc_spi.sct_cookie = sc;
149 1.1.2.1 matt sc->sc_spi.sct_configure = at91spi_configure;
150 1.1.2.1 matt sc->sc_spi.sct_transfer = at91spi_transfer;
151 1.1.2.1 matt
152 1.1.2.1 matt //sc->sc_spi.sct_nslaves must have been initialized by machdep code
153 1.1.2.1 matt if (!sc->sc_spi.sct_nslaves) {
154 1.1.2.1 matt aprint_error("%s: no slaves!\n", sc->sc_dev.dv_xname);
155 1.1.2.1 matt }
156 1.1.2.1 matt
157 1.1.2.1 matt sba.sba_controller = &sc->sc_spi;
158 1.1.2.1 matt
159 1.1.2.1 matt /* initialize the queue */
160 1.1.2.1 matt SIMPLEQ_INIT(&sc->sc_q);
161 1.1.2.1 matt
162 1.1.2.1 matt /* reset the SPI */
163 1.1.2.1 matt at91_peripheral_clock(sc->sc_pid, 1);
164 1.1.2.1 matt PUTREG(sc, SPI_CR, SPI_CR_SWRST);
165 1.1.2.1 matt delay(100);
166 1.1.2.1 matt
167 1.1.2.1 matt /* be paranoid and make sure the PDC is dead */
168 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
169 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_RNCR, 0);
170 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_RCR, 0);
171 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_TNCR, 0);
172 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_TCR, 0);
173 1.1.2.1 matt
174 1.1.2.1 matt // configure SPI:
175 1.1.2.1 matt PUTREG(sc, SPI_IDR, -1);
176 1.1.2.1 matt PUTREG(sc, SPI_CSR(0), SPI_CSR_SCBR | SPI_CSR_BITS_8);
177 1.1.2.1 matt PUTREG(sc, SPI_CSR(1), SPI_CSR_SCBR | SPI_CSR_BITS_8);
178 1.1.2.1 matt PUTREG(sc, SPI_CSR(2), SPI_CSR_SCBR | SPI_CSR_BITS_8);
179 1.1.2.1 matt PUTREG(sc, SPI_CSR(3), SPI_CSR_SCBR | SPI_CSR_BITS_8);
180 1.1.2.1 matt PUTREG(sc, SPI_MR, SPI_MR_MODFDIS/* <- machdep? */ | SPI_MR_MSTR);
181 1.1.2.1 matt
182 1.1.2.1 matt /* enable device interrupts */
183 1.1.2.1 matt sc->sc_ih = at91_intr_establish(sc->sc_pid, IPL_BIO, INTR_HIGH_LEVEL,
184 1.1.2.1 matt at91spi_intr, sc);
185 1.1.2.1 matt
186 1.1.2.1 matt /* enable SPI */
187 1.1.2.1 matt PUTREG(sc, SPI_CR, SPI_CR_SPIEN);
188 1.1.2.1 matt if (GETREG(sc, SPI_SR) & SPI_SR_RDRF)
189 1.1.2.1 matt (void)GETREG(sc, SPI_RDR);
190 1.1.2.1 matt
191 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_PTCR, PDC_PTCR_TXTEN | PDC_PTCR_RXTEN);
192 1.1.2.1 matt
193 1.1.2.1 matt /* attach slave devices */
194 1.1.2.1 matt (void) config_found_ia(&sc->sc_dev, "spibus", &sba, spibus_print);
195 1.1.2.1 matt }
196 1.1.2.1 matt
197 1.1.2.1 matt int
198 1.1.2.1 matt at91spi_configure(void *arg, int slave, int mode, int speed)
199 1.1.2.1 matt {
200 1.1.2.1 matt struct at91spi_softc *sc = arg;
201 1.1.2.1 matt uint scbr;
202 1.1.2.1 matt uint32_t csr;
203 1.1.2.1 matt
204 1.1.2.1 matt /* setup interrupt registers */
205 1.1.2.1 matt PUTREG(sc, SPI_IDR, -1); /* disable interrupts for now */
206 1.1.2.1 matt
207 1.1.2.1 matt csr = GETREG(sc, SPI_CSR(0)); /* read register */
208 1.1.2.1 matt csr &= SPI_CSR_RESERVED; /* keep reserved bits */
209 1.1.2.1 matt csr |= SPI_CSR_BITS_8; /* assume 8 bit transfers */
210 1.1.2.1 matt
211 1.1.2.1 matt /*
212 1.1.2.1 matt * Calculate clock divider
213 1.1.2.1 matt */
214 1.1.2.1 matt scbr = speed ? ((AT91_MSTCLK + speed - 1) / speed + 1) & ~1 : -1;
215 1.1.2.1 matt if (scbr > 0xFF) {
216 1.1.2.1 matt aprint_error("%s: speed %d not supported\n", sc->sc_dev.dv_xname, speed);
217 1.1.2.1 matt return EINVAL;
218 1.1.2.1 matt }
219 1.1.2.1 matt csr |= scbr << SPI_CSR_SCBR_SHIFT;
220 1.1.2.1 matt
221 1.1.2.1 matt /*
222 1.1.2.1 matt * I'm not entirely confident that these values are correct.
223 1.1.2.1 matt * But at least mode 0 appears to work properly with the
224 1.1.2.1 matt * devices I have tested. The documentation seems to suggest
225 1.1.2.1 matt * that I have the meaning of the clock delay bit inverted.
226 1.1.2.1 matt */
227 1.1.2.1 matt switch (mode) {
228 1.1.2.1 matt case SPI_MODE_0:
229 1.1.2.1 matt csr |= SPI_CSR_NCPHA; /* CPHA = 0, CPOL = 0 */
230 1.1.2.1 matt break;
231 1.1.2.1 matt case SPI_MODE_1:
232 1.1.2.1 matt csr |= 0; /* CPHA = 1, CPOL = 0 */
233 1.1.2.1 matt break;
234 1.1.2.1 matt case SPI_MODE_2:
235 1.1.2.1 matt csr |= SPI_CSR_NCPHA /* CPHA = 0, CPOL = 1 */
236 1.1.2.1 matt | SPI_CSR_CPOL;
237 1.1.2.1 matt break;
238 1.1.2.1 matt case SPI_MODE_3:
239 1.1.2.1 matt csr |= SPI_CSR_CPOL; /* CPHA = 1, CPOL = 1 */
240 1.1.2.1 matt break;
241 1.1.2.1 matt default:
242 1.1.2.1 matt return EINVAL;
243 1.1.2.1 matt }
244 1.1.2.1 matt
245 1.1.2.1 matt PUTREG(sc, SPI_CSR(0), csr);
246 1.1.2.1 matt
247 1.1.2.1 matt DPRINTFN(3, ("%s: slave %d mode %d speed %d, csr=0x%08"PRIX32"\n",
248 1.1.2.1 matt __FUNCTION__, slave, mode, speed, csr));
249 1.1.2.1 matt
250 1.1.2.1 matt #if 0
251 1.1.2.1 matt // wait until ready!?
252 1.1.2.1 matt for (i = 1000000; i; i -= 10) {
253 1.1.2.1 matt if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
254 1.1.2.1 matt return 0;
255 1.1.2.1 matt }
256 1.1.2.1 matt }
257 1.1.2.1 matt
258 1.1.2.1 matt return ETIMEDOUT;
259 1.1.2.1 matt #else
260 1.1.2.1 matt return 0;
261 1.1.2.1 matt #endif
262 1.1.2.1 matt }
263 1.1.2.1 matt
264 1.1.2.1 matt #define HALF_BUF_SIZE (PAGE_SIZE / 2)
265 1.1.2.1 matt
266 1.1.2.1 matt void
267 1.1.2.1 matt at91spi_xfer(struct at91spi_softc *sc, int start)
268 1.1.2.1 matt {
269 1.1.2.1 matt struct spi_chunk *chunk;
270 1.1.2.1 matt int len;
271 1.1.2.1 matt uint32_t sr;
272 1.1.2.1 matt
273 1.1.2.1 matt DPRINTFN(3, ("%s: sc=%p start=%d\n", __FUNCTION__, sc, start));
274 1.1.2.1 matt
275 1.1.2.1 matt /* so ready to transmit more / anything received? */
276 1.1.2.1 matt if (((sr = GETREG(sc, SPI_SR)) & (SPI_SR_ENDTX | SPI_SR_ENDRX)) != (SPI_SR_ENDTX | SPI_SR_ENDRX)) {
277 1.1.2.1 matt /* not ready, get out */
278 1.1.2.1 matt DPRINTFN(3, ("%s: sc=%p start=%d sr=%"PRIX32"\n", __FUNCTION__, sc, start, sr));
279 1.1.2.1 matt return;
280 1.1.2.1 matt }
281 1.1.2.1 matt
282 1.1.2.1 matt DPRINTFN(3, ("%s: sr=%"PRIX32"\n", __FUNCTION__, sr));
283 1.1.2.1 matt
284 1.1.2.1 matt if (!start) {
285 1.1.2.1 matt // ok, something has been transfered, synchronize..
286 1.1.2.1 matt int offs = sc->sc_dmaoffs ^ HALF_BUF_SIZE;
287 1.1.2.1 matt bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, offs, HALF_BUF_SIZE,
288 1.1.2.1 matt BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
289 1.1.2.1 matt
290 1.1.2.1 matt if ((chunk = sc->sc_rchunk) != NULL) {
291 1.1.2.1 matt if ((len = chunk->chunk_rresid) > HALF_BUF_SIZE)
292 1.1.2.1 matt len = HALF_BUF_SIZE;
293 1.1.2.1 matt if (chunk->chunk_rptr && len > 0) {
294 1.1.2.1 matt memcpy(chunk->chunk_rptr, sc->sc_dmapage + offs, len);
295 1.1.2.1 matt chunk->chunk_rptr += len;
296 1.1.2.1 matt }
297 1.1.2.1 matt if ((chunk->chunk_rresid -= len) <= 0) {
298 1.1.2.1 matt // done with this chunk, get next
299 1.1.2.1 matt sc->sc_rchunk = chunk->chunk_next;
300 1.1.2.1 matt }
301 1.1.2.1 matt }
302 1.1.2.1 matt }
303 1.1.2.1 matt
304 1.1.2.1 matt /* start transmitting next chunk: */
305 1.1.2.1 matt if ((chunk = sc->sc_wchunk) != NULL) {
306 1.1.2.1 matt
307 1.1.2.1 matt /* make sure we transmit just half buffer at a time */
308 1.1.2.1 matt len = MIN(chunk->chunk_wresid, HALF_BUF_SIZE);
309 1.1.2.1 matt
310 1.1.2.1 matt // setup outgoing data
311 1.1.2.1 matt if (chunk->chunk_wptr && len > 0) {
312 1.1.2.1 matt memcpy(sc->sc_dmapage + sc->sc_dmaoffs, chunk->chunk_wptr, len);
313 1.1.2.1 matt chunk->chunk_wptr += len;
314 1.1.2.1 matt } else {
315 1.1.2.1 matt memset(sc->sc_dmapage + sc->sc_dmaoffs, 0, len);
316 1.1.2.1 matt }
317 1.1.2.1 matt
318 1.1.2.1 matt /* advance to next transfer if it's time to */
319 1.1.2.1 matt if ((chunk->chunk_wresid -= len) <= 0) {
320 1.1.2.1 matt sc->sc_wchunk = sc->sc_wchunk->chunk_next;
321 1.1.2.1 matt }
322 1.1.2.1 matt
323 1.1.2.1 matt /* determine which interrupt to get */
324 1.1.2.1 matt if (sc->sc_wchunk) {
325 1.1.2.1 matt /* just wait for next buffer to free */
326 1.1.2.1 matt PUTREG(sc, SPI_IER, SPI_SR_ENDRX);
327 1.1.2.1 matt } else {
328 1.1.2.1 matt /* must wait until transfer has completed */
329 1.1.2.1 matt PUTREG(sc, SPI_IDR, SPI_SR_ENDRX);
330 1.1.2.1 matt PUTREG(sc, SPI_IER, SPI_SR_RXBUFF);
331 1.1.2.1 matt }
332 1.1.2.1 matt
333 1.1.2.1 matt DPRINTFN(3, ("%s: dmaoffs=%d len=%d wchunk=%p (%p:%d) rchunk=%p (%p:%d) mr=%"PRIX32" sr=%"PRIX32" imr=%"PRIX32" csr0=%"PRIX32"\n",
334 1.1.2.1 matt __FUNCTION__, sc->sc_dmaoffs, len, sc->sc_wchunk,
335 1.1.2.1 matt sc->sc_wchunk ? sc->sc_wchunk->chunk_wptr : NULL,
336 1.1.2.1 matt sc->sc_wchunk ? sc->sc_wchunk->chunk_wresid : -1,
337 1.1.2.1 matt sc->sc_rchunk,
338 1.1.2.1 matt sc->sc_rchunk ? sc->sc_rchunk->chunk_rptr : NULL,
339 1.1.2.1 matt sc->sc_rchunk ? sc->sc_rchunk->chunk_rresid : -1,
340 1.1.2.1 matt GETREG(sc, SPI_MR), GETREG(sc, SPI_SR),
341 1.1.2.1 matt GETREG(sc, SPI_IMR), GETREG(sc, SPI_CSR(0))));
342 1.1.2.1 matt
343 1.1.2.1 matt // prepare DMA
344 1.1.2.1 matt bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_dmaoffs, len,
345 1.1.2.1 matt BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
346 1.1.2.1 matt
347 1.1.2.1 matt // and start transmitting / receiving
348 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_RNPR, sc->sc_dmaaddr + sc->sc_dmaoffs);
349 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_RNCR, len);
350 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_TNPR, sc->sc_dmaaddr + sc->sc_dmaoffs);
351 1.1.2.1 matt PUTREG(sc, SPI_PDC_BASE + PDC_TNCR, len);
352 1.1.2.1 matt
353 1.1.2.1 matt // swap buffer
354 1.1.2.1 matt sc->sc_dmaoffs ^= HALF_BUF_SIZE;
355 1.1.2.1 matt
356 1.1.2.1 matt // get out
357 1.1.2.1 matt return;
358 1.1.2.1 matt } else {
359 1.1.2.1 matt DPRINTFN(3, ("%s: nothing to write anymore\n", __FUNCTION__));
360 1.1.2.1 matt return;
361 1.1.2.1 matt }
362 1.1.2.1 matt }
363 1.1.2.1 matt
364 1.1.2.1 matt void
365 1.1.2.1 matt at91spi_sched(struct at91spi_softc *sc)
366 1.1.2.1 matt {
367 1.1.2.1 matt struct spi_transfer *st;
368 1.1.2.1 matt int err;
369 1.1.2.1 matt
370 1.1.2.1 matt while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
371 1.1.2.1 matt
372 1.1.2.1 matt DPRINTFN(2, ("%s: st=%p\n", __FUNCTION__, st));
373 1.1.2.1 matt
374 1.1.2.1 matt /* remove the item */
375 1.1.2.1 matt spi_transq_dequeue(&sc->sc_q);
376 1.1.2.1 matt
377 1.1.2.1 matt /* note that we are working on it */
378 1.1.2.1 matt sc->sc_transfer = st;
379 1.1.2.1 matt
380 1.1.2.1 matt if ((err = at91spi_select(sc, st->st_slave)) != 0) {
381 1.1.2.1 matt spi_done(st, err);
382 1.1.2.1 matt continue;
383 1.1.2.1 matt }
384 1.1.2.1 matt
385 1.1.2.1 matt /* setup chunks */
386 1.1.2.1 matt sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
387 1.1.2.1 matt
388 1.1.2.1 matt /* now kick the master start to get the chip running */
389 1.1.2.1 matt at91spi_xfer(sc, TRUE);
390 1.1.2.1 matt
391 1.1.2.1 matt /* enable error interrupts too: */
392 1.1.2.1 matt PUTREG(sc, SPI_IER, SPI_SR_MODF | SPI_SR_OVRES);
393 1.1.2.1 matt
394 1.1.2.1 matt sc->sc_running = TRUE;
395 1.1.2.1 matt return;
396 1.1.2.1 matt }
397 1.1.2.1 matt DPRINTFN(2, ("%s: nothing to do anymore\n", __FUNCTION__));
398 1.1.2.1 matt PUTREG(sc, SPI_IDR, -1); /* disable interrupts */
399 1.1.2.1 matt at91spi_select(sc, -1);
400 1.1.2.1 matt sc->sc_running = FALSE;
401 1.1.2.1 matt }
402 1.1.2.1 matt
403 1.1.2.1 matt void
404 1.1.2.1 matt at91spi_done(struct at91spi_softc *sc, int err)
405 1.1.2.1 matt {
406 1.1.2.1 matt struct spi_transfer *st;
407 1.1.2.1 matt
408 1.1.2.1 matt /* called from interrupt handler */
409 1.1.2.1 matt if ((st = sc->sc_transfer) != NULL) {
410 1.1.2.1 matt sc->sc_transfer = NULL;
411 1.1.2.1 matt DPRINTFN(2, ("%s: st %p finished with error code %d\n", __FUNCTION__, st, err));
412 1.1.2.1 matt spi_done(st, err);
413 1.1.2.1 matt }
414 1.1.2.1 matt /* make sure we clear these bits out */
415 1.1.2.1 matt sc->sc_wchunk = sc->sc_rchunk = NULL;
416 1.1.2.1 matt at91spi_sched(sc);
417 1.1.2.1 matt }
418 1.1.2.1 matt
419 1.1.2.1 matt int
420 1.1.2.1 matt at91spi_intr(void *arg)
421 1.1.2.1 matt {
422 1.1.2.1 matt struct at91spi_softc *sc = arg;
423 1.1.2.1 matt uint32_t imr, sr;
424 1.1.2.1 matt int err = 0;
425 1.1.2.1 matt
426 1.1.2.1 matt if ((imr = GETREG(sc, SPI_IMR)) == 0) {
427 1.1.2.1 matt /* interrupts are not enabled, get out */
428 1.1.2.1 matt DPRINTFN(4, ("%s: interrupts are not enabled\n", __FUNCTION__));
429 1.1.2.1 matt return 0;
430 1.1.2.1 matt }
431 1.1.2.1 matt
432 1.1.2.1 matt sr = GETREG(sc, SPI_SR);
433 1.1.2.1 matt if (!(sr & imr)) {
434 1.1.2.1 matt /* interrupt did not happen, get out */
435 1.1.2.1 matt DPRINTFN(3, ("%s: interrupts are not enabled, sr=%08"PRIX32" imr=%08"PRIX32"\n",
436 1.1.2.1 matt __FUNCTION__, sr, imr));
437 1.1.2.1 matt return 0;
438 1.1.2.1 matt }
439 1.1.2.1 matt
440 1.1.2.1 matt DPRINTFN(3, ("%s: sr=%08"PRIX32" imr=%08"PRIX32"\n",
441 1.1.2.1 matt __FUNCTION__, sr, imr));
442 1.1.2.1 matt
443 1.1.2.1 matt if (sr & imr & SPI_SR_MODF) {
444 1.1.2.1 matt printf("%s: mode fault!\n", sc->sc_dev.dv_xname);
445 1.1.2.1 matt err = EIO;
446 1.1.2.1 matt }
447 1.1.2.1 matt
448 1.1.2.1 matt if (sr & imr & SPI_SR_OVRES) {
449 1.1.2.1 matt printf("%s: overrun error!\n", sc->sc_dev.dv_xname);
450 1.1.2.1 matt err = EIO;
451 1.1.2.1 matt }
452 1.1.2.1 matt if (err) {
453 1.1.2.1 matt /* clear errors */
454 1.1.2.1 matt /* complete transfer */
455 1.1.2.1 matt at91spi_done(sc, err);
456 1.1.2.1 matt } else {
457 1.1.2.1 matt /* do all data exchanges */
458 1.1.2.1 matt at91spi_xfer(sc, FALSE);
459 1.1.2.1 matt
460 1.1.2.1 matt /*
461 1.1.2.1 matt * if the master done bit is set, make sure we do the
462 1.1.2.1 matt * right processing.
463 1.1.2.1 matt */
464 1.1.2.1 matt if (sr & imr & SPI_SR_RXBUFF) {
465 1.1.2.1 matt if ((sc->sc_wchunk != NULL) ||
466 1.1.2.1 matt (sc->sc_rchunk != NULL)) {
467 1.1.2.1 matt printf("%s: partial transfer?\n",
468 1.1.2.1 matt sc->sc_dev.dv_xname);
469 1.1.2.1 matt err = EIO;
470 1.1.2.1 matt }
471 1.1.2.1 matt at91spi_done(sc, err);
472 1.1.2.1 matt }
473 1.1.2.1 matt
474 1.1.2.1 matt }
475 1.1.2.1 matt
476 1.1.2.1 matt return 1;
477 1.1.2.1 matt }
478 1.1.2.1 matt
479 1.1.2.1 matt int
480 1.1.2.1 matt at91spi_transfer(void *arg, struct spi_transfer *st)
481 1.1.2.1 matt {
482 1.1.2.1 matt struct at91spi_softc *sc = arg;
483 1.1.2.1 matt int s;
484 1.1.2.1 matt
485 1.1.2.1 matt /* make sure we select the right chip */
486 1.1.2.1 matt s = splbio();
487 1.1.2.1 matt spi_transq_enqueue(&sc->sc_q, st);
488 1.1.2.1 matt if (sc->sc_running == 0) {
489 1.1.2.1 matt at91spi_sched(sc);
490 1.1.2.1 matt }
491 1.1.2.1 matt splx(s);
492 1.1.2.1 matt return 0;
493 1.1.2.1 matt }
494 1.1.2.1 matt
495