maple.c revision 1.11 1 /* $NetBSD: maple.c,v 1.11 2002/03/24 18:21:25 uch 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
44 #include <uvm/uvm_extern.h>
45
46 #include <machine/cpu.h>
47 #include <machine/bus.h>
48 #include <sh3/pmap.h>
49
50 #include <dreamcast/dev/maple/maple.h>
51 #include <dreamcast/dev/maple/mapleconf.h>
52 #include <dreamcast/dev/maple/maplevar.h>
53 #include <dreamcast/dev/maple/maplereg.h>
54 #include <dreamcast/dev/maple/mapleio.h>
55
56 #include "locators.h"
57
58 /* Internal macros, functions, and variables. */
59
60 #define MAPLE_CALLOUT_TICKS 2
61
62 #define MAPLEBUSUNIT(dev) (minor(dev)>>5)
63 #define MAPLEPORT(dev) ((minor(dev) & 0x18) >> 3)
64 #define MAPLESUBUNIT(dev) (minor(dev) & 0x7)
65
66 /*
67 * Function declarations.
68 */
69 static int maplematch __P((struct device *, struct cfdata *, void *));
70 static void mapleattach __P((struct device *, struct device *, void *));
71 static int mapleprint __P((void *, const char *));
72 static int maplesubmatch __P((struct device *, struct cfdata *, void *));
73 static void maple_attach_dev __P((struct maple_softc *, int, int));
74 static void maple_begin_txbuf __P((struct maple_softc *));
75 static int maple_end_txbuf __P((struct maple_softc *));
76 static void maple_write_command __P((struct maple_softc *, int, int,
77 int, int, void *));
78 static void maple_scanbus __P((struct maple_softc *));
79 static void maple_callout __P((void *));
80 static void maple_send_commands __P((struct maple_softc *));
81 static void maple_check_responses __P((struct maple_softc *));
82
83 int maple_alloc_dma __P((size_t, vaddr_t *, paddr_t *));
84 void maple_free_dma __P((paddr_t, size_t));
85
86 int mapleopen __P((dev_t, int, int, struct proc *));
87 int mapleclose __P((dev_t, int, int, struct proc *));
88 int maple_internal_ioctl __P((struct maple_softc *, int, int, u_long, caddr_t, int, struct proc *));
89 int mapleioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
90
91
92
93 /*
94 * Global variables.
95 */
96 int maple_polling = 0; /* Are we polling? (Debugger mode) */
97
98 /*
99 * Driver definition.
100 */
101 struct cfattach maple_ca = {
102 sizeof(struct maple_softc), maplematch, mapleattach
103 };
104
105 extern struct cfdriver maple_cd;
106
107 static int
108 maplematch(parent, cf, aux)
109 struct device *parent;
110 struct cfdata *cf;
111 void *aux;
112 {
113
114 if (strcmp("maple", cf->cf_driver->cd_name))
115 return (0);
116
117 return (1);
118 }
119
120 static void
121 maple_attach_dev(sc, port, subunit)
122 struct maple_softc *sc;
123 int port;
124 int subunit;
125 {
126 struct maple_attach_args ma;
127 u_int32_t func;
128 int f;
129 char oldxname[16];
130
131 ma.ma_port = port;
132 ma.ma_subunit = subunit;
133 ma.ma_devinfo = &sc->sc_unit[port][subunit].devinfo;
134 ma.ma_function = 1;
135 func = ma.ma_devinfo->di_func;
136
137 mapleprint(&ma, sc->sc_dev.dv_xname);
138 printf("\n");
139 strcpy(oldxname, sc->sc_dev.dv_xname);
140 sprintf(sc->sc_dev.dv_xname, "maple%c", port+'A');
141 if(subunit)
142 sprintf(sc->sc_dev.dv_xname+6, "%d", subunit);
143 for(f=0; f<32; f++, ma.ma_function<<=1)
144 if(func & ma.ma_function)
145 (void) config_found_sm(&sc->sc_dev, &ma,
146 NULL, maplesubmatch);
147 strcpy(sc->sc_dev.dv_xname, oldxname);
148 }
149
150 static void
151 maple_begin_txbuf(sc)
152 struct maple_softc *sc;
153 {
154 sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
155 }
156
157 static int
158 maple_end_txbuf(sc)
159 struct maple_softc *sc;
160 {
161 /* if no frame have been written, we can't mark the
162 list end, and so the DMA must not be activated */
163 if (sc->sc_txpos == sc->sc_txbuf)
164 return (0);
165
166 *sc->sc_txlink |= 0x80000000;
167
168 return (1);
169 }
170
171 static int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
172
173 static void
174 maple_write_command(sc, port, subunit, command, datalen, dataaddr)
175 struct maple_softc *sc;
176 int port;
177 int subunit;
178 int command;
179 int datalen;
180 void *dataaddr;
181 {
182 int to, from;
183 u_int32_t *p = sc->sc_txpos;
184
185 if ((port & ~(MAPLE_PORTS-1)) != 0 ||
186 subunit < 0 || subunit >= MAPLE_SUBUNITS)
187 return;
188
189 /* Compute sender and recipient address */
190 from = port << 6;
191 to = from | subunit_code[subunit];
192
193 /* Max data length = 255 longs = 1020 bytes */
194 if(datalen > 255)
195 datalen = 255;
196 else if(datalen < 0)
197 datalen = 0;
198
199 sc->sc_txlink = p;
200
201 /* Set length of packet and destination port (A-D) */
202 *p++ = datalen | (port << 16);
203
204 /* Write address to receive buffer where the response
205 frame should be put */
206 *p++ = sc->sc_rxbuf_phys[port][subunit];
207
208 /* Create the frame header. The fields are assembled "backwards"
209 because of the Maple Bus big-endianness. */
210 *p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
211
212 /* Copy parameter data, if any */
213 if (datalen > 0) {
214 u_int32_t *param = dataaddr;
215 int i;
216 for (i = 0; i < datalen; i++)
217 *p++ = *param++;
218 }
219
220 sc->sc_txpos = p;
221 }
222
223 static void
224 maple_scanbus(sc)
225 struct maple_softc *sc;
226 {
227 int p, s;
228
229 maple_polling = 1;
230
231 maple_begin_txbuf(sc);
232
233 for (p = 0; p < MAPLE_PORTS; p++) {
234 maple_write_command(sc, p, 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
235 }
236
237 if (maple_end_txbuf(sc)) {
238
239 MAPLE_DMAADDR = sc->sc_txbuf_phys;
240 MAPLE_STATE = 1;
241 while (MAPLE_STATE != 0)
242 ;
243
244 for (p = 0; p < MAPLE_PORTS; p++)
245 if ((sc->sc_rxbuf[p][0][0] & 0xff) == MAPLE_RESPONSE_DEVINFO)
246
247 sc->sc_port_units[p] = ((sc->sc_rxbuf[p][0][0]>>15)&0x3e)|1;
248
249 else
250
251 sc->sc_port_units[p] = 0;
252
253
254 maple_begin_txbuf(sc);
255
256 for (p = 0; p < MAPLE_PORTS; p++) {
257 for (s = 0; s < MAPLE_SUBUNITS; s++) {
258 if (sc->sc_port_units[p] & (1<<s))
259 maple_write_command(sc, p, s, MAPLE_COMMAND_DEVINFO, 0, NULL);
260 }
261 }
262
263 if (maple_end_txbuf(sc)) {
264
265 MAPLE_DMAADDR = sc->sc_txbuf_phys;
266 MAPLE_STATE = 1;
267 while (MAPLE_STATE != 0)
268 ;
269
270 for (p = 0; p < MAPLE_PORTS; p++)
271 for (s = 0; s < MAPLE_SUBUNITS; s++)
272 if (sc->sc_port_units[p] & (1<<s)) {
273
274 if ((sc->sc_rxbuf[p][s][0] & 0xff) ==
275 MAPLE_RESPONSE_DEVINFO) {
276
277 u_int32_t *to_swap;
278 int i;
279
280 memcpy((to_swap = &sc->sc_unit[p][s].devinfo.di_func),
281 sc->sc_rxbuf[p][s]+1, sizeof(struct maple_devinfo));
282
283 for (i = 0; i < 4; i++, to_swap++)
284 *to_swap = ntohl(*to_swap);
285
286 maple_attach_dev(sc, p, s);
287
288 } else {
289
290 printf("%s: no response from port %d subunit %d\n",
291 sc->sc_dev.dv_xname, p, s);
292
293 sc->sc_port_units[p] &= ~(1<<s);
294
295 }
296 }
297
298 }
299
300 }
301
302 maple_polling = 0;
303
304 }
305
306 static void
307 maple_send_commands(sc)
308 struct maple_softc *sc;
309 {
310 int p, s;
311
312 if (sc->maple_commands_pending || MAPLE_STATE != 0)
313 return;
314
315 maple_begin_txbuf(sc);
316
317 for (p = 0; p < MAPLE_PORTS; p++) {
318
319 for (s = 0; s < MAPLE_SUBUNITS; s++) {
320
321 if (sc->sc_unit[p][s].getcond_callback != NULL) {
322
323 u_int32_t func = ntohl(sc->sc_unit[p][s].getcond_func);
324
325 maple_write_command(sc, p, s, MAPLE_COMMAND_GETCOND, 1, &func);
326
327 }
328
329 }
330
331 }
332
333 if (!maple_end_txbuf(sc))
334 return;
335
336 MAPLE_DMAADDR = sc->sc_txbuf_phys;
337 MAPLE_STATE = 1;
338
339 sc->maple_commands_pending = 1;
340 }
341
342 static void
343 maple_check_responses(sc)
344 struct maple_softc *sc;
345 {
346 int p, s;
347
348 if (!sc->maple_commands_pending || MAPLE_STATE != 0)
349 return;
350
351 for (p = 0; p < MAPLE_PORTS; p++) {
352 for (s = 0; s < MAPLE_SUBUNITS; s++) {
353 struct maple_unit * u = &sc->sc_unit[p][s];
354 if (u->getcond_callback != NULL &&
355 (sc->sc_rxbuf[p][s][0] & 0xff) == MAPLE_RESPONSE_DATATRF &&
356 (sc->sc_rxbuf[p][s][0]>>24) >= 1 &&
357 htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
358 (*u->getcond_callback)(u->getcond_data,
359 (void *)(sc->sc_rxbuf[p][s]+2),
360 ((sc->sc_rxbuf[p][s][0]>>22)&1020)-4);
361 }
362 }
363 }
364
365 sc->maple_commands_pending = 0;
366 }
367
368 void
369 maple_run_polling(dev)
370 struct device *dev;
371 {
372 struct maple_softc *sc;
373
374 sc = (struct maple_softc *)dev;
375
376 if (MAPLE_STATE != 0)
377 return;
378
379 maple_send_commands(sc);
380
381 while (MAPLE_STATE != 0)
382 ;
383
384 maple_check_responses(sc);
385 }
386
387 void
388 maple_set_condition_callback(dev, port, subunit, func, callback, data)
389 struct device *dev;
390 int port;
391 int subunit;
392 u_int32_t func;
393 void (*callback)(void *, void *, int);
394 void *data;
395 {
396 struct maple_softc *sc;
397
398 sc = (struct maple_softc *)dev;
399
400 if ((port & ~(MAPLE_PORTS-1)) != 0 ||
401 subunit < 0 || subunit >= MAPLE_SUBUNITS)
402 return;
403
404 sc->sc_unit[port][subunit].getcond_func = func;
405 sc->sc_unit[port][subunit].getcond_callback = callback;
406 sc->sc_unit[port][subunit].getcond_data = data;
407 }
408
409 static void
410 maple_callout(ctx)
411 void *ctx;
412 {
413 struct maple_softc *sc = ctx;
414
415 if(!maple_polling) {
416
417 maple_check_responses(sc);
418
419 maple_send_commands(sc);
420
421 }
422
423 callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
424 (void *)maple_callout, sc);
425 }
426
427 int
428 maple_alloc_dma(size, vap, pap)
429 size_t size;
430 vaddr_t *vap;
431 paddr_t *pap;
432 {
433 extern paddr_t avail_start, avail_end; /* from pmap.c */
434
435 struct pglist mlist;
436 struct vm_page *m;
437 int error;
438
439 size = round_page(size);
440
441 TAILQ_INIT(&mlist);
442 error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
443 0, 0, &mlist, 1, 0);
444 if (error)
445 return (error);
446
447 m = TAILQ_FIRST(&mlist);
448 *pap = VM_PAGE_TO_PHYS(m);
449 *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
450
451 return (0);
452 }
453
454 void
455 maple_free_dma(paddr, size)
456 paddr_t paddr;
457 size_t size;
458 {
459 struct pglist mlist;
460 struct vm_page *m;
461 bus_addr_t addr;
462
463 TAILQ_INIT(&mlist);
464 for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
465 m = PHYS_TO_VM_PAGE(addr);
466 TAILQ_INSERT_TAIL(&mlist, m, pageq);
467 }
468 uvm_pglistfree(&mlist);
469 }
470
471 static void
472 mapleattach(parent, self, aux)
473 struct device *parent, *self;
474 void *aux;
475 {
476 struct maple_softc *sc;
477 vaddr_t dmabuffer;
478 paddr_t dmabuffer_phys;
479 u_int32_t *p;
480 int i, j;
481
482 sc = (struct maple_softc *)self;
483
484 printf("\n");
485
486 if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
487 printf("%s: unable to allocate DMA buffers.\n", sc->sc_dev.dv_xname);
488 return;
489 }
490
491 p = (u_int32_t *) dmabuffer;
492
493 for (i = 0; i < MAPLE_PORTS; i++)
494 for (j = 0; j < MAPLE_SUBUNITS; j++) {
495
496 sc->sc_rxbuf[i][j] = p;
497 sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
498 p += 256;
499
500 }
501
502 sc->sc_txbuf = p;
503 sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
504
505 sc->maple_commands_pending = 0;
506
507 MAPLE_RESET = RESET_MAGIC;
508 MAPLE_RESET2 = 0;
509
510 MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
511
512 MAPLE_ENABLE = 1;
513
514 maple_scanbus(sc);
515
516 memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
517
518 callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
519 (void *)maple_callout, sc);
520 }
521
522 int
523 mapleprint(aux, pnp)
524 void *aux;
525 const char *pnp;
526 {
527 struct maple_attach_args *ma = aux;
528
529 if (pnp != NULL) {
530 printf("maple%c", 'A'+ma->ma_port);
531 if (ma->ma_subunit != 0)
532 printf("%d", ma->ma_subunit);
533 printf(" at %s", pnp);
534 }
535
536 printf(" port %d", ma->ma_port);
537
538 if (ma->ma_subunit != 0)
539 printf(" subunit %d", ma->ma_subunit);
540
541 printf(": %.*s",
542 (int)sizeof(ma->ma_devinfo->di_product_name),
543 ma->ma_devinfo->di_product_name);
544
545 return (0);
546 }
547
548 static int
549 maplesubmatch(parent, match, aux)
550 struct device *parent;
551 struct cfdata *match;
552 void *aux;
553 {
554 struct maple_attach_args *ma = aux;
555
556 if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
557 match->cf_loc[MAPLECF_PORT] != ma->ma_port)
558 return (0);
559
560 if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
561 match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
562 return (0);
563
564 return ((*match->cf_attach->ca_match)(parent, match, aux));
565 }
566
567 u_int32_t
568 maple_get_function_data(devinfo, function_code)
569 struct maple_devinfo *devinfo;
570 u_int32_t function_code;
571 {
572 int i, p = 0;
573
574 for (i = 31; i >= 0; --i)
575 if (devinfo->di_func & (1U<<i)) {
576 if (function_code & (1U<<i))
577 return devinfo->di_function_data[p];
578 else
579 if (++p >= 3)
580 break;
581 }
582 return (0);
583 }
584
585 /* Generic maple device interface */
586
587 int
588 mapleopen(dev, flag, mode, p)
589 dev_t dev;
590 int flag, mode;
591 struct proc *p;
592 {
593 struct maple_softc *sc;
594
595 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
596 if (sc == NULL) /* make sure it was attached */
597 return (ENXIO);
598
599 if (MAPLEPORT(dev) >= MAPLE_PORTS)
600 return (ENXIO);
601
602 if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
603 return (ENXIO);
604
605 if(!(sc->sc_port_units[MAPLEPORT(dev)] & (1<<MAPLESUBUNIT(dev))))
606 return (ENXIO);
607
608 sc->sc_port_units_open[MAPLEPORT(dev)] |= 1<<MAPLESUBUNIT(dev);
609
610 return (0);
611 }
612
613 int
614 mapleclose(dev, flag, mode, p)
615 dev_t dev;
616 int flag, mode;
617 struct proc *p;
618 {
619 struct maple_softc *sc;
620
621 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
622
623 sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1<<MAPLESUBUNIT(dev));
624
625 return (0);
626 }
627
628 int
629 maple_internal_ioctl(sc, port, subunit, cmd, data, flag, p)
630 struct maple_softc *sc;
631 int port;
632 int subunit;
633 u_long cmd;
634 caddr_t data;
635 int flag;
636 struct proc *p;
637 {
638 struct maple_unit *u = &sc->sc_unit[port][subunit];
639
640 if(!(sc->sc_port_units[port] & (1<<subunit)))
641 return (ENXIO);
642
643 switch(cmd) {
644 case MAPLEIO_GDEVINFO:
645 memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
646 return (0);
647 default:
648 return (EINVAL);
649 }
650 }
651
652 int
653 mapleioctl(dev, cmd, data, flag, p)
654 dev_t dev;
655 u_long cmd;
656 caddr_t data;
657 int flag;
658 struct proc *p;
659 {
660 struct maple_softc *sc;
661
662 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
663
664 return maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev),
665 cmd, data, flag, p);
666 }
667