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