auspi.c revision 1.1.8.3 1 1.1.8.3 yamt /* $NetBSD: auspi.c,v 1.1.8.3 2007/02/26 09:07:25 yamt Exp $ */
2 1.1.8.2 yamt
3 1.1.8.2 yamt /*-
4 1.1.8.2 yamt * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 1.1.8.2 yamt * Copyright (c) 2006 Garrett D'Amore.
6 1.1.8.2 yamt * All rights reserved.
7 1.1.8.2 yamt *
8 1.1.8.2 yamt * Portions of this code were written by Garrett D'Amore for the
9 1.1.8.2 yamt * Champaign-Urbana Community Wireless Network Project.
10 1.1.8.2 yamt *
11 1.1.8.2 yamt * Redistribution and use in source and binary forms, with or
12 1.1.8.2 yamt * without modification, are permitted provided that the following
13 1.1.8.2 yamt * conditions are met:
14 1.1.8.2 yamt * 1. Redistributions of source code must retain the above copyright
15 1.1.8.2 yamt * notice, this list of conditions and the following disclaimer.
16 1.1.8.2 yamt * 2. Redistributions in binary form must reproduce the above
17 1.1.8.2 yamt * copyright notice, this list of conditions and the following
18 1.1.8.2 yamt * disclaimer in the documentation and/or other materials provided
19 1.1.8.2 yamt * with the distribution.
20 1.1.8.2 yamt * 3. All advertising materials mentioning features or use of this
21 1.1.8.2 yamt * software must display the following acknowledgements:
22 1.1.8.2 yamt * This product includes software developed by the Urbana-Champaign
23 1.1.8.2 yamt * Independent Media Center.
24 1.1.8.2 yamt * This product includes software developed by Garrett D'Amore.
25 1.1.8.2 yamt * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 1.1.8.2 yamt * D'Amore's name may not be used to endorse or promote products
27 1.1.8.2 yamt * derived from this software without specific prior written permission.
28 1.1.8.2 yamt *
29 1.1.8.2 yamt * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 1.1.8.2 yamt * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 1.1.8.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 1.1.8.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1.8.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 1.1.8.2 yamt * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1.8.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1.8.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 1.1.8.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 1.1.8.2 yamt * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1.8.2 yamt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 1.1.8.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 1.1.8.2 yamt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 1.1.8.2 yamt */
43 1.1.8.2 yamt
44 1.1.8.2 yamt #include <sys/cdefs.h>
45 1.1.8.3 yamt __KERNEL_RCSID(0, "$NetBSD: auspi.c,v 1.1.8.3 2007/02/26 09:07:25 yamt Exp $");
46 1.1.8.2 yamt
47 1.1.8.2 yamt #include "locators.h"
48 1.1.8.2 yamt
49 1.1.8.2 yamt #include <sys/param.h>
50 1.1.8.2 yamt #include <sys/systm.h>
51 1.1.8.2 yamt #include <sys/kernel.h>
52 1.1.8.2 yamt #include <sys/device.h>
53 1.1.8.2 yamt #include <sys/errno.h>
54 1.1.8.2 yamt #include <sys/proc.h>
55 1.1.8.2 yamt
56 1.1.8.2 yamt #include <machine/bus.h>
57 1.1.8.2 yamt #include <machine/cpu.h>
58 1.1.8.2 yamt
59 1.1.8.2 yamt #include <mips/alchemy/include/aubusvar.h>
60 1.1.8.2 yamt #include <mips/alchemy/include/auvar.h>
61 1.1.8.2 yamt
62 1.1.8.2 yamt #include <mips/alchemy/dev/aupscreg.h>
63 1.1.8.2 yamt #include <mips/alchemy/dev/aupscvar.h>
64 1.1.8.2 yamt #include <mips/alchemy/dev/auspireg.h>
65 1.1.8.2 yamt #include <mips/alchemy/dev/auspivar.h>
66 1.1.8.2 yamt
67 1.1.8.2 yamt #include <dev/spi/spivar.h>
68 1.1.8.2 yamt
69 1.1.8.2 yamt struct auspi_softc {
70 1.1.8.2 yamt struct device sc_dev;
71 1.1.8.2 yamt struct aupsc_controller sc_psc; /* parent controller ops */
72 1.1.8.2 yamt struct spi_controller sc_spi; /* SPI implementation ops */
73 1.1.8.2 yamt struct auspi_machdep sc_md; /* board-specific support */
74 1.1.8.2 yamt struct auspi_job *sc_job; /* current job */
75 1.1.8.2 yamt struct spi_chunk *sc_wchunk;
76 1.1.8.2 yamt struct spi_chunk *sc_rchunk;
77 1.1.8.2 yamt void *sc_ih; /* interrupt handler */
78 1.1.8.2 yamt
79 1.1.8.2 yamt struct spi_transfer *sc_transfer;
80 1.1.8.3 yamt bool sc_running; /* is it processing stuff? */
81 1.1.8.2 yamt
82 1.1.8.2 yamt SIMPLEQ_HEAD(,spi_transfer) sc_q;
83 1.1.8.2 yamt };
84 1.1.8.2 yamt
85 1.1.8.2 yamt #define auspi_select(sc, slave) \
86 1.1.8.2 yamt (sc)->sc_md.am_select((sc)->sc_md.am_cookie, (slave))
87 1.1.8.2 yamt
88 1.1.8.2 yamt #define STATIC
89 1.1.8.2 yamt
90 1.1.8.2 yamt STATIC int auspi_match(struct device *, struct cfdata *, void *);
91 1.1.8.2 yamt STATIC void auspi_attach(struct device *, struct device *, void *);
92 1.1.8.2 yamt STATIC int auspi_intr(void *);
93 1.1.8.2 yamt
94 1.1.8.2 yamt CFATTACH_DECL(auspi, sizeof(struct auspi_softc),
95 1.1.8.2 yamt auspi_match, auspi_attach, NULL, NULL);
96 1.1.8.2 yamt
97 1.1.8.2 yamt /* SPI service routines */
98 1.1.8.2 yamt STATIC int auspi_configure(void *, int, int, int);
99 1.1.8.2 yamt STATIC int auspi_transfer(void *, struct spi_transfer *);
100 1.1.8.2 yamt
101 1.1.8.2 yamt /* internal stuff */
102 1.1.8.2 yamt STATIC void auspi_done(struct auspi_softc *, int);
103 1.1.8.2 yamt STATIC void auspi_send(struct auspi_softc *);
104 1.1.8.2 yamt STATIC void auspi_recv(struct auspi_softc *);
105 1.1.8.2 yamt STATIC void auspi_sched(struct auspi_softc *);
106 1.1.8.2 yamt
107 1.1.8.2 yamt #define GETREG(sc, x) \
108 1.1.8.2 yamt bus_space_read_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x)
109 1.1.8.2 yamt #define PUTREG(sc, x, v) \
110 1.1.8.2 yamt bus_space_write_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x, v)
111 1.1.8.2 yamt
112 1.1.8.2 yamt int
113 1.1.8.2 yamt auspi_match(struct device *parent, struct cfdata *cf, void *aux)
114 1.1.8.2 yamt {
115 1.1.8.2 yamt struct aupsc_attach_args *aa = aux;
116 1.1.8.2 yamt
117 1.1.8.2 yamt if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
118 1.1.8.2 yamt return 0;
119 1.1.8.2 yamt
120 1.1.8.2 yamt return 1;
121 1.1.8.2 yamt }
122 1.1.8.2 yamt
123 1.1.8.2 yamt void
124 1.1.8.2 yamt auspi_attach(struct device *parent, struct device *self, void *aux)
125 1.1.8.2 yamt {
126 1.1.8.2 yamt struct auspi_softc *sc = device_private(self);
127 1.1.8.2 yamt struct aupsc_attach_args *aa = aux;
128 1.1.8.2 yamt struct spibus_attach_args sba;
129 1.1.8.2 yamt const struct auspi_machdep *md;
130 1.1.8.2 yamt
131 1.1.8.2 yamt if ((md = auspi_machdep(aa->aupsc_addr)) != NULL) {
132 1.1.8.2 yamt sc->sc_md = *md;
133 1.1.8.2 yamt }
134 1.1.8.2 yamt
135 1.1.8.2 yamt aprint_normal(": Alchemy PSC SPI protocol\n");
136 1.1.8.2 yamt
137 1.1.8.2 yamt sc->sc_psc = aa->aupsc_ctrl;
138 1.1.8.2 yamt
139 1.1.8.2 yamt /*
140 1.1.8.2 yamt * Initialize SPI controller
141 1.1.8.2 yamt */
142 1.1.8.2 yamt sc->sc_spi.sct_cookie = sc;
143 1.1.8.2 yamt sc->sc_spi.sct_configure = auspi_configure;
144 1.1.8.2 yamt sc->sc_spi.sct_transfer = auspi_transfer;
145 1.1.8.2 yamt
146 1.1.8.2 yamt /* fix this! */
147 1.1.8.2 yamt sc->sc_spi.sct_nslaves = sc->sc_md.am_nslaves;
148 1.1.8.2 yamt
149 1.1.8.2 yamt sba.sba_controller = &sc->sc_spi;
150 1.1.8.2 yamt
151 1.1.8.2 yamt /* enable SPI mode */
152 1.1.8.2 yamt sc->sc_psc.psc_enable(sc, AUPSC_SEL_SPI);
153 1.1.8.2 yamt
154 1.1.8.2 yamt /* initialize the queue */
155 1.1.8.2 yamt SIMPLEQ_INIT(&sc->sc_q);
156 1.1.8.2 yamt
157 1.1.8.2 yamt /* make sure interrupts disabled at the SPI */
158 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIMSK, SPIMSK_ALL);
159 1.1.8.2 yamt
160 1.1.8.2 yamt /* enable device interrupts */
161 1.1.8.2 yamt sc->sc_ih = au_intr_establish(aa->aupsc_irq, 0, IPL_SERIAL, IST_LEVEL,
162 1.1.8.2 yamt auspi_intr, sc);
163 1.1.8.2 yamt
164 1.1.8.2 yamt (void) config_found_ia(&sc->sc_dev, "spibus", &sba, spibus_print);
165 1.1.8.2 yamt }
166 1.1.8.2 yamt
167 1.1.8.2 yamt int
168 1.1.8.2 yamt auspi_configure(void *arg, int slave, int mode, int speed)
169 1.1.8.2 yamt {
170 1.1.8.2 yamt struct auspi_softc *sc = arg;
171 1.1.8.2 yamt int brg, i;
172 1.1.8.2 yamt uint32_t reg;
173 1.1.8.2 yamt
174 1.1.8.2 yamt /* setup interrupt registers */
175 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIMSK, SPIMSK_NORM);
176 1.1.8.2 yamt
177 1.1.8.2 yamt reg = GETREG(sc, AUPSC_SPICFG);
178 1.1.8.2 yamt
179 1.1.8.2 yamt reg &= ~(SPICFG_BRG_MASK); /* clear BRG */
180 1.1.8.2 yamt reg &= ~(SPICFG_DIV_MASK); /* use pscn_mainclock/2 */
181 1.1.8.2 yamt reg &= ~(SPICFG_PSE); /* disable port swap */
182 1.1.8.2 yamt reg &= ~(SPICFG_BI); /* clear bit clock invert */
183 1.1.8.2 yamt reg &= ~(SPICFG_CDE); /* clear clock phase delay */
184 1.1.8.2 yamt reg &= ~(SPICFG_CGE); /* clear clock gate enable */
185 1.1.8.2 yamt //reg |= SPICFG_MO; /* master-only mode */
186 1.1.8.2 yamt reg |= SPICFG_DE; /* device enable */
187 1.1.8.2 yamt reg |= SPICFG_DD; /* disable DMA */
188 1.1.8.2 yamt reg |= SPICFG_RT_1; /* 1 byte rx fifo threshold */
189 1.1.8.2 yamt reg |= SPICFG_TT_1; /* 1 byte tx fifo threshold */
190 1.1.8.2 yamt reg |= ((8-1) << SPICFG_LEN_SHIFT);/* always work in 8-bit chunks */
191 1.1.8.2 yamt
192 1.1.8.2 yamt /*
193 1.1.8.2 yamt * We assume a base clock of 48MHz has been established by the
194 1.1.8.2 yamt * platform code. The clock divider reduces this to 24MHz.
195 1.1.8.2 yamt * Next we have to figure out the BRG
196 1.1.8.2 yamt */
197 1.1.8.2 yamt #define BASECLK 24000000
198 1.1.8.2 yamt for (brg = 0; brg < 64; brg++) {
199 1.1.8.2 yamt if (speed >= (BASECLK / ((brg + 1) * 2))) {
200 1.1.8.2 yamt break;
201 1.1.8.2 yamt }
202 1.1.8.2 yamt }
203 1.1.8.2 yamt
204 1.1.8.2 yamt /*
205 1.1.8.2 yamt * Does the device want to go even slower? Our minimum speed without
206 1.1.8.2 yamt * changing other assumptions, and complicating the code even further,
207 1.1.8.2 yamt * is 24MHz/128, or 187.5kHz. That should be slow enough for any
208 1.1.8.2 yamt * device we're likely to encounter.
209 1.1.8.2 yamt */
210 1.1.8.2 yamt if (speed < (BASECLK / ((brg + 1) * 2))) {
211 1.1.8.2 yamt return EINVAL;
212 1.1.8.2 yamt }
213 1.1.8.2 yamt reg &= ~SPICFG_BRG_MASK;
214 1.1.8.2 yamt reg |= (brg << SPICFG_BRG_SHIFT);
215 1.1.8.2 yamt
216 1.1.8.2 yamt /*
217 1.1.8.2 yamt * I'm not entirely confident that these values are correct.
218 1.1.8.2 yamt * But at least mode 0 appears to work properly with the
219 1.1.8.2 yamt * devices I have tested. The documentation seems to suggest
220 1.1.8.2 yamt * that I have the meaning of the clock delay bit inverted.
221 1.1.8.2 yamt */
222 1.1.8.2 yamt switch (mode) {
223 1.1.8.2 yamt case SPI_MODE_0:
224 1.1.8.2 yamt reg |= 0; /* CPHA = 0, CPOL = 0 */
225 1.1.8.2 yamt break;
226 1.1.8.2 yamt case SPI_MODE_1:
227 1.1.8.2 yamt reg |= SPICFG_CDE; /* CPHA = 1, CPOL = 0 */
228 1.1.8.2 yamt break;
229 1.1.8.2 yamt case SPI_MODE_2:
230 1.1.8.2 yamt reg |= SPICFG_BI; /* CPHA = 0, CPOL = 1 */
231 1.1.8.2 yamt break;
232 1.1.8.2 yamt case SPI_MODE_3:
233 1.1.8.2 yamt reg |= SPICFG_CDE | SPICFG_BI; /* CPHA = 1, CPOL = 1 */
234 1.1.8.2 yamt break;
235 1.1.8.2 yamt default:
236 1.1.8.2 yamt return EINVAL;
237 1.1.8.2 yamt }
238 1.1.8.2 yamt
239 1.1.8.2 yamt PUTREG(sc, AUPSC_SPICFG, reg);
240 1.1.8.2 yamt
241 1.1.8.2 yamt for (i = 1000000; i; i -= 10) {
242 1.1.8.2 yamt if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
243 1.1.8.2 yamt return 0;
244 1.1.8.2 yamt }
245 1.1.8.2 yamt }
246 1.1.8.2 yamt
247 1.1.8.2 yamt return ETIMEDOUT;
248 1.1.8.2 yamt }
249 1.1.8.2 yamt
250 1.1.8.2 yamt void
251 1.1.8.2 yamt auspi_send(struct auspi_softc *sc)
252 1.1.8.2 yamt {
253 1.1.8.2 yamt uint32_t data;
254 1.1.8.2 yamt struct spi_chunk *chunk;
255 1.1.8.2 yamt
256 1.1.8.2 yamt /* fill the fifo */
257 1.1.8.2 yamt while ((chunk = sc->sc_wchunk) != NULL) {
258 1.1.8.2 yamt
259 1.1.8.2 yamt while (chunk->chunk_wresid) {
260 1.1.8.2 yamt
261 1.1.8.2 yamt /* transmit fifo full? */
262 1.1.8.2 yamt if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_TF) {
263 1.1.8.2 yamt return;
264 1.1.8.2 yamt }
265 1.1.8.2 yamt
266 1.1.8.2 yamt if (chunk->chunk_wptr) {
267 1.1.8.2 yamt data = *chunk->chunk_wptr++;
268 1.1.8.2 yamt } else {
269 1.1.8.2 yamt data = 0;
270 1.1.8.2 yamt }
271 1.1.8.2 yamt chunk->chunk_wresid--;
272 1.1.8.2 yamt
273 1.1.8.2 yamt /* if the last outbound character, mark it */
274 1.1.8.2 yamt if ((chunk->chunk_wresid == 0) &&
275 1.1.8.2 yamt (chunk->chunk_next == NULL)) {
276 1.1.8.2 yamt data |= SPITXRX_LC;
277 1.1.8.2 yamt }
278 1.1.8.2 yamt PUTREG(sc, AUPSC_SPITXRX, data);
279 1.1.8.2 yamt }
280 1.1.8.2 yamt
281 1.1.8.2 yamt /* advance to next transfer */
282 1.1.8.2 yamt sc->sc_wchunk = sc->sc_wchunk->chunk_next;
283 1.1.8.2 yamt }
284 1.1.8.2 yamt }
285 1.1.8.2 yamt
286 1.1.8.2 yamt void
287 1.1.8.2 yamt auspi_recv(struct auspi_softc *sc)
288 1.1.8.2 yamt {
289 1.1.8.2 yamt uint32_t data;
290 1.1.8.2 yamt struct spi_chunk *chunk;
291 1.1.8.2 yamt
292 1.1.8.2 yamt while ((chunk = sc->sc_rchunk) != NULL) {
293 1.1.8.2 yamt while (chunk->chunk_rresid) {
294 1.1.8.2 yamt
295 1.1.8.2 yamt /* rx fifo empty? */
296 1.1.8.2 yamt if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_RE) != 0) {
297 1.1.8.2 yamt return;
298 1.1.8.2 yamt }
299 1.1.8.2 yamt
300 1.1.8.2 yamt /* collect rx data */
301 1.1.8.2 yamt data = GETREG(sc, AUPSC_SPITXRX);
302 1.1.8.2 yamt if (chunk->chunk_rptr) {
303 1.1.8.2 yamt *chunk->chunk_rptr++ = data & 0xff;
304 1.1.8.2 yamt }
305 1.1.8.2 yamt
306 1.1.8.2 yamt chunk->chunk_rresid--;
307 1.1.8.2 yamt }
308 1.1.8.2 yamt
309 1.1.8.2 yamt /* advance next to next transfer */
310 1.1.8.2 yamt sc->sc_rchunk = sc->sc_rchunk->chunk_next;
311 1.1.8.2 yamt }
312 1.1.8.2 yamt }
313 1.1.8.2 yamt
314 1.1.8.2 yamt void
315 1.1.8.2 yamt auspi_sched(struct auspi_softc *sc)
316 1.1.8.2 yamt {
317 1.1.8.2 yamt struct spi_transfer *st;
318 1.1.8.2 yamt int err;
319 1.1.8.2 yamt
320 1.1.8.2 yamt while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
321 1.1.8.2 yamt
322 1.1.8.2 yamt /* remove the item */
323 1.1.8.2 yamt spi_transq_dequeue(&sc->sc_q);
324 1.1.8.2 yamt
325 1.1.8.2 yamt /* note that we are working on it */
326 1.1.8.2 yamt sc->sc_transfer = st;
327 1.1.8.2 yamt
328 1.1.8.2 yamt if ((err = auspi_select(sc, st->st_slave)) != 0) {
329 1.1.8.2 yamt spi_done(st, err);
330 1.1.8.2 yamt continue;
331 1.1.8.2 yamt }
332 1.1.8.2 yamt
333 1.1.8.2 yamt /* clear the fifos */
334 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
335 1.1.8.2 yamt /* setup chunks */
336 1.1.8.2 yamt sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
337 1.1.8.2 yamt auspi_send(sc);
338 1.1.8.2 yamt /* now kick the master start to get the chip running */
339 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIPCR, SPIPCR_MS);
340 1.1.8.2 yamt sc->sc_running = TRUE;
341 1.1.8.2 yamt return;
342 1.1.8.2 yamt }
343 1.1.8.2 yamt auspi_select(sc, -1);
344 1.1.8.2 yamt sc->sc_running = FALSE;
345 1.1.8.2 yamt }
346 1.1.8.2 yamt
347 1.1.8.2 yamt void
348 1.1.8.2 yamt auspi_done(struct auspi_softc *sc, int err)
349 1.1.8.2 yamt {
350 1.1.8.2 yamt struct spi_transfer *st;
351 1.1.8.2 yamt
352 1.1.8.2 yamt /* called from interrupt handler */
353 1.1.8.2 yamt if ((st = sc->sc_transfer) != NULL) {
354 1.1.8.2 yamt sc->sc_transfer = NULL;
355 1.1.8.2 yamt spi_done(st, err);
356 1.1.8.2 yamt }
357 1.1.8.2 yamt /* make sure we clear these bits out */
358 1.1.8.2 yamt sc->sc_wchunk = sc->sc_rchunk = NULL;
359 1.1.8.2 yamt auspi_sched(sc);
360 1.1.8.2 yamt }
361 1.1.8.2 yamt
362 1.1.8.2 yamt int
363 1.1.8.2 yamt auspi_intr(void *arg)
364 1.1.8.2 yamt {
365 1.1.8.2 yamt struct auspi_softc *sc = arg;
366 1.1.8.2 yamt uint32_t ev;
367 1.1.8.2 yamt int err = 0;
368 1.1.8.2 yamt
369 1.1.8.2 yamt
370 1.1.8.2 yamt if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DI) == 0) {
371 1.1.8.2 yamt return 0;
372 1.1.8.2 yamt }
373 1.1.8.2 yamt
374 1.1.8.2 yamt ev = GETREG(sc, AUPSC_SPIEVNT);
375 1.1.8.2 yamt
376 1.1.8.2 yamt if (ev & SPIMSK_MM) {
377 1.1.8.2 yamt printf("%s: multiple masters detected!\n",
378 1.1.8.2 yamt sc->sc_dev.dv_xname);
379 1.1.8.2 yamt err = EIO;
380 1.1.8.2 yamt }
381 1.1.8.2 yamt if (ev & SPIMSK_RO) {
382 1.1.8.2 yamt printf("%s: receive overflow\n", sc->sc_dev.dv_xname);
383 1.1.8.2 yamt err = EIO;
384 1.1.8.2 yamt }
385 1.1.8.2 yamt if (ev & SPIMSK_TU) {
386 1.1.8.2 yamt printf("%s: transmit underflow\n", sc->sc_dev.dv_xname);
387 1.1.8.2 yamt err = EIO;
388 1.1.8.2 yamt }
389 1.1.8.2 yamt if (err) {
390 1.1.8.2 yamt /* clear errors */
391 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIEVNT,
392 1.1.8.2 yamt ev & (SPIMSK_MM | SPIMSK_RO | SPIMSK_TU));
393 1.1.8.2 yamt /* clear the fifos */
394 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
395 1.1.8.2 yamt auspi_done(sc, err);
396 1.1.8.2 yamt
397 1.1.8.2 yamt } else {
398 1.1.8.2 yamt
399 1.1.8.2 yamt /* do all data exchanges */
400 1.1.8.2 yamt auspi_send(sc);
401 1.1.8.2 yamt auspi_recv(sc);
402 1.1.8.2 yamt
403 1.1.8.2 yamt /*
404 1.1.8.2 yamt * if the master done bit is set, make sure we do the
405 1.1.8.2 yamt * right processing.
406 1.1.8.2 yamt */
407 1.1.8.2 yamt if (ev & SPIMSK_MD) {
408 1.1.8.2 yamt if ((sc->sc_wchunk != NULL) ||
409 1.1.8.2 yamt (sc->sc_rchunk != NULL)) {
410 1.1.8.2 yamt printf("%s: partial transfer?\n",
411 1.1.8.2 yamt sc->sc_dev.dv_xname);
412 1.1.8.2 yamt err = EIO;
413 1.1.8.2 yamt }
414 1.1.8.2 yamt auspi_done(sc, err);
415 1.1.8.2 yamt }
416 1.1.8.2 yamt /* clear interrupts */
417 1.1.8.2 yamt PUTREG(sc, AUPSC_SPIEVNT,
418 1.1.8.2 yamt ev & (SPIMSK_TR | SPIMSK_RR | SPIMSK_MD));
419 1.1.8.2 yamt }
420 1.1.8.2 yamt
421 1.1.8.2 yamt return 1;
422 1.1.8.2 yamt }
423 1.1.8.2 yamt
424 1.1.8.2 yamt int
425 1.1.8.2 yamt auspi_transfer(void *arg, struct spi_transfer *st)
426 1.1.8.2 yamt {
427 1.1.8.2 yamt struct auspi_softc *sc = arg;
428 1.1.8.2 yamt int s;
429 1.1.8.2 yamt
430 1.1.8.2 yamt /* make sure we select the right chip */
431 1.1.8.2 yamt s = splserial();
432 1.1.8.2 yamt spi_transq_enqueue(&sc->sc_q, st);
433 1.1.8.2 yamt if (sc->sc_running == 0) {
434 1.1.8.2 yamt auspi_sched(sc);
435 1.1.8.2 yamt }
436 1.1.8.2 yamt splx(s);
437 1.1.8.2 yamt return 0;
438 1.1.8.2 yamt }
439 1.1.8.2 yamt
440