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