maple.c revision 1.16 1 /* $NetBSD: maple.c,v 1.16 2002/09/27 03:17:47 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Marcus Comstedt
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marcus Comstedt.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
38 #include <sys/poll.h>
39 #include <sys/select.h>
40 #include <sys/proc.h>
41 #include <sys/signalvar.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <machine/cpu.h>
48 #include <machine/bus.h>
49 #include <sh3/pmap.h>
50
51 #include <dreamcast/dev/maple/maple.h>
52 #include <dreamcast/dev/maple/mapleconf.h>
53 #include <dreamcast/dev/maple/maplevar.h>
54 #include <dreamcast/dev/maple/maplereg.h>
55 #include <dreamcast/dev/maple/mapleio.h>
56
57 #include "locators.h"
58
59 /* Internal macros, functions, and variables. */
60
61 #define MAPLE_CALLOUT_TICKS 2
62
63 #define MAPLEBUSUNIT(dev) (minor(dev)>>5)
64 #define MAPLEPORT(dev) ((minor(dev) & 0x18) >> 3)
65 #define MAPLESUBUNIT(dev) (minor(dev) & 0x7)
66
67 /*
68 * Function declarations.
69 */
70 static int maplematch(struct device *, struct cfdata *, void *);
71 static void mapleattach(struct device *, struct device *, void *);
72 static int mapleprint(void *, const char *);
73 static int maplesubmatch(struct device *, struct cfdata *, void *);
74 static void maple_attach_dev(struct maple_softc *, int, int);
75 static void maple_begin_txbuf(struct maple_softc *);
76 static int maple_end_txbuf(struct maple_softc *);
77 static void maple_write_command(struct maple_softc *, int, int,
78 int, int, void *);
79 static void maple_scanbus(struct maple_softc *);
80 static void maple_callout(void *);
81 static void maple_send_commands(struct maple_softc *);
82 static void maple_check_responses(struct maple_softc *);
83
84 int maple_alloc_dma(size_t, vaddr_t *, paddr_t *);
85 void maple_free_dma(paddr_t, size_t);
86
87 int maple_internal_ioctl(struct maple_softc *, int, int, u_long, caddr_t,
88 int, struct proc *);
89
90 /*
91 * Global variables.
92 */
93 int maple_polling = 0; /* Are we polling? (Debugger mode) */
94
95 /*
96 * Driver definition.
97 */
98 struct cfattach maple_ca = {
99 sizeof(struct maple_softc), maplematch, mapleattach
100 };
101
102 extern struct cfdriver maple_cd;
103
104 dev_type_open(mapleopen);
105 dev_type_close(mapleclose);
106 dev_type_ioctl(mapleioctl);
107
108 const struct cdevsw maple_cdevsw = {
109 mapleopen, mapleclose, noread, nowrite, mapleioctl,
110 nostop, notty, nopoll, nommap,
111 };
112
113 static int
114 maplematch(struct device *parent, struct cfdata *cf, void *aux)
115 {
116
117 return (1);
118 }
119
120 static void
121 maple_attach_dev(struct maple_softc *sc, int port, int subunit)
122 {
123 struct maple_attach_args ma;
124 u_int32_t func;
125 int f;
126 char oldxname[16];
127
128 ma.ma_port = port;
129 ma.ma_subunit = subunit;
130 ma.ma_devinfo = &sc->sc_unit[port][subunit].devinfo;
131 ma.ma_function = 1;
132 func = ma.ma_devinfo->di_func;
133
134 mapleprint(&ma, sc->sc_dev.dv_xname);
135 printf("\n");
136 strcpy(oldxname, sc->sc_dev.dv_xname);
137 sprintf(sc->sc_dev.dv_xname, "maple%c", port+'A');
138 if (subunit)
139 sprintf(sc->sc_dev.dv_xname+6, "%d", subunit);
140
141 for (f = 0; f < 32; f++, ma.ma_function <<= 1)
142 if (func & ma.ma_function)
143 (void)config_found_sm(&sc->sc_dev, &ma,
144 NULL, maplesubmatch);
145 strcpy(sc->sc_dev.dv_xname, oldxname);
146 }
147
148 static void
149 maple_begin_txbuf(struct maple_softc *sc)
150 {
151
152 sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
153 }
154
155 static int
156 maple_end_txbuf(struct maple_softc *sc)
157 {
158 /* if no frame have been written, we can't mark the
159 list end, and so the DMA must not be activated */
160 if (sc->sc_txpos == sc->sc_txbuf)
161 return (0);
162
163 *sc->sc_txlink |= 0x80000000;
164
165 return (1);
166 }
167
168 static int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
169
170 static void
171 maple_write_command(struct maple_softc *sc, int port, int subunit, int command,
172 int datalen, void *dataaddr)
173 {
174 int to, from;
175 u_int32_t *p = sc->sc_txpos;
176
177 if ((port & ~(MAPLE_PORTS-1)) != 0 ||
178 subunit < 0 || subunit >= MAPLE_SUBUNITS)
179 return;
180
181 /* Compute sender and recipient address */
182 from = port << 6;
183 to = from | subunit_code[subunit];
184
185 /* Max data length = 255 longs = 1020 bytes */
186 if (datalen > 255)
187 datalen = 255;
188 else if (datalen < 0)
189 datalen = 0;
190
191 sc->sc_txlink = p;
192
193 /* Set length of packet and destination port (A-D) */
194 *p++ = datalen | (port << 16);
195
196 /* Write address to receive buffer where the response
197 frame should be put */
198 *p++ = sc->sc_rxbuf_phys[port][subunit];
199
200 /* Create the frame header. The fields are assembled "backwards"
201 because of the Maple Bus big-endianness. */
202 *p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
203
204 /* Copy parameter data, if any */
205 if (datalen > 0) {
206 u_int32_t *param = dataaddr;
207 int i;
208 for (i = 0; i < datalen; i++)
209 *p++ = *param++;
210 }
211
212 sc->sc_txpos = p;
213 }
214
215 static void
216 maple_scanbus(struct maple_softc *sc)
217 {
218 int p, s;
219
220 maple_polling = 1;
221
222 maple_begin_txbuf(sc);
223
224 for (p = 0; p < MAPLE_PORTS; p++) {
225 maple_write_command(sc, p, 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
226 }
227
228 if (maple_end_txbuf(sc)) {
229
230 MAPLE_DMAADDR = sc->sc_txbuf_phys;
231 MAPLE_STATE = 1;
232 while (MAPLE_STATE != 0)
233 ;
234
235 for (p = 0; p < MAPLE_PORTS; p++)
236 if ((sc->sc_rxbuf[p][0][0] & 0xff) ==
237 MAPLE_RESPONSE_DEVINFO)
238 sc->sc_port_units[p] =
239 ((sc->sc_rxbuf[p][0][0] >> 15) & 0x3e) | 1;
240 else
241 sc->sc_port_units[p] = 0;
242
243
244 maple_begin_txbuf(sc);
245
246 for (p = 0; p < MAPLE_PORTS; p++) {
247 for (s = 0; s < MAPLE_SUBUNITS; s++) {
248 if (sc->sc_port_units[p] & (1 << s))
249 maple_write_command(sc, p, s,
250 MAPLE_COMMAND_DEVINFO, 0, NULL);
251 }
252 }
253
254 if (maple_end_txbuf(sc)) {
255 MAPLE_DMAADDR = sc->sc_txbuf_phys;
256 MAPLE_STATE = 1;
257 while (MAPLE_STATE != 0)
258 ;
259
260 for (p = 0; p < MAPLE_PORTS; p++)
261 for (s = 0; s < MAPLE_SUBUNITS; s++)
262 if (sc->sc_port_units[p] & (1 << s)) {
263
264 if ((sc->sc_rxbuf[p][s][0] &
265 0xff) ==
266 MAPLE_RESPONSE_DEVINFO) {
267
268 u_int32_t *to_swap;
269 int i;
270
271 memcpy((to_swap =
272 &sc->sc_unit[p][s].
273 devinfo.di_func),
274 sc->sc_rxbuf[p][s] +
275 1,
276 sizeof(struct
277 maple_devinfo));
278
279 for (i = 0; i < 4; i++,
280 to_swap++)
281 *to_swap =
282 ntohl
283 (*to_swap);
284 maple_attach_dev(sc, p,
285 s);
286 } else {
287
288 printf("%s: no response"
289 " from port %d "
290 " subunit %d\n",
291 sc->sc_dev.dv_xname,
292 p, s);
293 sc->sc_port_units[p] &=
294 ~(1 << s);
295 }
296 }
297
298 }
299
300 }
301
302 maple_polling = 0;
303
304 }
305
306 static void
307 maple_send_commands(struct maple_softc *sc)
308 {
309 int p, s;
310
311 if (sc->maple_commands_pending || MAPLE_STATE != 0)
312 return;
313
314 maple_begin_txbuf(sc);
315
316 for (p = 0; p < MAPLE_PORTS; p++) {
317 for (s = 0; s < MAPLE_SUBUNITS; s++) {
318 if (sc->sc_unit[p][s].getcond_callback != NULL) {
319 u_int32_t func =
320 ntohl(sc->sc_unit[p][s].getcond_func);
321
322 maple_write_command(sc, p, s,
323 MAPLE_COMMAND_GETCOND, 1, &func);
324 }
325 }
326 }
327
328 if (!maple_end_txbuf(sc))
329 return;
330
331 MAPLE_DMAADDR = sc->sc_txbuf_phys;
332 MAPLE_STATE = 1;
333
334 sc->maple_commands_pending = 1;
335 }
336
337 static void
338 maple_check_responses(struct maple_softc *sc)
339 {
340 int p, s;
341
342 if (!sc->maple_commands_pending || MAPLE_STATE != 0)
343 return;
344
345 for (p = 0; p < MAPLE_PORTS; p++) {
346 for (s = 0; s < MAPLE_SUBUNITS; s++) {
347 struct maple_unit *u = &sc->sc_unit[p][s];
348 if (u->getcond_callback != NULL &&
349 (sc->sc_rxbuf[p][s][0] & 0xff) ==
350 MAPLE_RESPONSE_DATATRF &&
351 (sc->sc_rxbuf[p][s][0] >> 24) >= 1 &&
352 htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
353 (*u->getcond_callback)(u->getcond_data,
354 (void *)(sc->sc_rxbuf[p][s] + 2),
355 ((sc->sc_rxbuf[p][s][0] >> 22) & 1020) - 4);
356 }
357 }
358 }
359
360 sc->maple_commands_pending = 0;
361 }
362
363 void
364 maple_run_polling(struct device *dev)
365 {
366 struct maple_softc *sc;
367
368 sc = (struct maple_softc *)dev;
369
370 if (MAPLE_STATE != 0)
371 return;
372
373 maple_send_commands(sc);
374
375 while (MAPLE_STATE != 0)
376 ;
377
378 maple_check_responses(sc);
379 }
380
381 void
382 maple_set_condition_callback(struct device *dev, int port, int subunit,
383 u_int32_t func, void (*callback)(void *, void *, int), void *data)
384 {
385 struct maple_softc *sc;
386
387 sc = (struct maple_softc *)dev;
388
389 if ((port & ~(MAPLE_PORTS-1)) != 0 ||
390 subunit < 0 || subunit >= MAPLE_SUBUNITS)
391 return;
392
393 sc->sc_unit[port][subunit].getcond_func = func;
394 sc->sc_unit[port][subunit].getcond_callback = callback;
395 sc->sc_unit[port][subunit].getcond_data = data;
396 }
397
398 static void
399 maple_callout(void *ctx)
400 {
401 struct maple_softc *sc = ctx;
402
403 if (!maple_polling) {
404 maple_check_responses(sc);
405 maple_send_commands(sc);
406 }
407
408 callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
409 (void *)maple_callout, sc);
410 }
411
412 int
413 maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
414 {
415 extern paddr_t avail_start, avail_end; /* from pmap.c */
416 struct pglist mlist;
417 struct vm_page *m;
418 int error;
419
420 size = round_page(size);
421
422 error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
423 0, 0, &mlist, 1, 0);
424 if (error)
425 return (error);
426
427 m = TAILQ_FIRST(&mlist);
428 *pap = VM_PAGE_TO_PHYS(m);
429 *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
430
431 return (0);
432 }
433
434 void
435 maple_free_dma(paddr_t paddr, size_t size)
436 {
437 struct pglist mlist;
438 struct vm_page *m;
439 bus_addr_t addr;
440
441 TAILQ_INIT(&mlist);
442 for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
443 m = PHYS_TO_VM_PAGE(addr);
444 TAILQ_INSERT_TAIL(&mlist, m, pageq);
445 }
446 uvm_pglistfree(&mlist);
447 }
448
449 static void
450 mapleattach(struct device *parent, struct device *self, void *aux)
451 {
452 struct maple_softc *sc;
453 vaddr_t dmabuffer;
454 paddr_t dmabuffer_phys;
455 u_int32_t *p;
456 int i, j;
457
458 sc = (struct maple_softc *)self;
459
460 printf("\n");
461
462 if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
463 printf("%s: unable to allocate DMA buffers.\n",
464 sc->sc_dev.dv_xname);
465 return;
466 }
467
468 p = (u_int32_t *)dmabuffer;
469
470 for (i = 0; i < MAPLE_PORTS; i++)
471 for (j = 0; j < MAPLE_SUBUNITS; j++) {
472 sc->sc_rxbuf[i][j] = p;
473 sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
474 p += 256;
475 }
476
477 sc->sc_txbuf = p;
478 sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
479
480 sc->maple_commands_pending = 0;
481
482 MAPLE_RESET = RESET_MAGIC;
483 MAPLE_RESET2 = 0;
484
485 MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
486
487 MAPLE_ENABLE = 1;
488
489 maple_scanbus(sc);
490
491 memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
492
493 callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
494 (void *)maple_callout, sc);
495 }
496
497 int
498 mapleprint(void *aux, const char *pnp)
499 {
500 struct maple_attach_args *ma = aux;
501
502 if (pnp != NULL) {
503 printf("maple%c", 'A'+ma->ma_port);
504 if (ma->ma_subunit != 0)
505 printf("%d", ma->ma_subunit);
506 printf(" at %s", pnp);
507 }
508
509 printf(" port %d", ma->ma_port);
510
511 if (ma->ma_subunit != 0)
512 printf(" subunit %d", ma->ma_subunit);
513
514 printf(": %.*s",
515 (int)sizeof(ma->ma_devinfo->di_product_name),
516 ma->ma_devinfo->di_product_name);
517
518 return (0);
519 }
520
521 static int
522 maplesubmatch(struct device *parent, struct cfdata *match, void *aux)
523 {
524 struct maple_attach_args *ma = aux;
525
526 if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
527 match->cf_loc[MAPLECF_PORT] != ma->ma_port)
528 return (0);
529
530 if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
531 match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
532 return (0);
533
534 return (config_match(parent, match, aux));
535 }
536
537 u_int32_t
538 maple_get_function_data(struct maple_devinfo *devinfo, u_int32_t function_code)
539 {
540 int i, p = 0;
541
542 for (i = 31; i >= 0; --i)
543 if (devinfo->di_func & (1U << i)) {
544 if (function_code & (1U << i))
545 return devinfo->di_function_data[p];
546 else
547 if (++p >= 3)
548 break;
549 }
550
551 return (0);
552 }
553
554 /* Generic maple device interface */
555
556 int
557 mapleopen(dev_t dev, int flag, int mode, struct proc *p)
558 {
559 struct maple_softc *sc;
560
561 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
562 if (sc == NULL) /* make sure it was attached */
563 return (ENXIO);
564
565 if (MAPLEPORT(dev) >= MAPLE_PORTS)
566 return (ENXIO);
567
568 if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
569 return (ENXIO);
570
571 if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
572 return (ENXIO);
573
574 sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
575
576 return (0);
577 }
578
579 int
580 mapleclose(dev_t dev, int flag, int mode, struct proc *p)
581 {
582 struct maple_softc *sc;
583
584 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
585
586 sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
587
588 return (0);
589 }
590
591 int
592 maple_internal_ioctl(struct maple_softc *sc, int port, int subunit, u_long cmd,
593 caddr_t data, int flag, struct proc *p)
594 {
595 struct maple_unit *u = &sc->sc_unit[port][subunit];
596
597 if (!(sc->sc_port_units[port] & (1 << subunit)))
598 return (ENXIO);
599
600 switch(cmd) {
601 case MAPLEIO_GDEVINFO:
602 memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
603 return (0);
604 default:
605 return (EINVAL);
606 }
607 }
608
609 int
610 mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
611 {
612 struct maple_softc *sc;
613
614 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
615
616 return (maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev),
617 cmd, data, flag, p));
618 }
619