maple.c revision 1.27 1 /* $NetBSD: maple.c,v 1.27 2005/02/19 15:40:15 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by ITOH Yasufumi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 2001 Marcus Comstedt
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Marcus Comstedt.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: maple.c,v 1.27 2005/02/19 15:40:15 tsutsui Exp $");
73
74 #include <sys/param.h>
75 #include <sys/device.h>
76 #include <sys/fcntl.h>
77 #include <sys/kernel.h>
78 #include <sys/kthread.h>
79 #include <sys/poll.h>
80 #include <sys/select.h>
81 #include <sys/proc.h>
82 #include <sys/signalvar.h>
83 #include <sys/systm.h>
84 #include <sys/conf.h>
85
86 #include <uvm/uvm_extern.h>
87
88 #include <machine/cpu.h>
89 #include <machine/bus.h>
90 #include <machine/sysasicvar.h>
91 #include <sh3/pmap.h>
92
93 #include <dreamcast/dev/maple/maple.h>
94 #include <dreamcast/dev/maple/mapleconf.h>
95 #include <dreamcast/dev/maple/maplevar.h>
96 #include <dreamcast/dev/maple/maplereg.h>
97 #include <dreamcast/dev/maple/mapleio.h>
98
99 #include "locators.h"
100
101 /* Internal macros, functions, and variables. */
102
103 #define MAPLE_CALLOUT_TICKS 2
104
105 #define MAPLEBUSUNIT(dev) (minor(dev)>>5)
106 #define MAPLEPORT(dev) ((minor(dev) & 0x18) >> 3)
107 #define MAPLESUBUNIT(dev) (minor(dev) & 0x7)
108
109 /* interrupt priority level */
110 #define IPL_MAPLE IPL_BIO
111 #define splmaple() splbio()
112
113 /*
114 * Function declarations.
115 */
116 static int maplematch(struct device *, struct cfdata *, void *);
117 static void mapleattach(struct device *, struct device *, void *);
118 static void maple_create_event_thread(void *);
119 static void maple_scanbus(struct maple_softc *);
120 static char * maple_unit_name(char *, int port, int subunit);
121 static void maple_begin_txbuf(struct maple_softc *);
122 static int maple_end_txbuf(struct maple_softc *);
123 static void maple_queue_command(struct maple_softc *, struct maple_unit *,
124 int command, int datalen, const void *dataaddr);
125 static void maple_write_command(struct maple_softc *, struct maple_unit *,
126 int, int, const void *);
127 static void maple_start(struct maple_softc *sc);
128 static void maple_start_poll(struct maple_softc *);
129 static void maple_check_subunit_change(struct maple_softc *,
130 struct maple_unit *);
131 static void maple_check_unit_change(struct maple_softc *,
132 struct maple_unit *);
133 static void maple_print_unit(void *, const char *);
134 static int maplesubmatch(struct device *, struct cfdata *, void *);
135 static int mapleprint(void *, const char *);
136 static void maple_attach_unit(struct maple_softc *, struct maple_unit *);
137 static void maple_detach_unit_nofix(struct maple_softc *,
138 struct maple_unit *);
139 static void maple_detach_unit(struct maple_softc *, struct maple_unit *);
140 static void maple_queue_cmds(struct maple_softc *,
141 struct maple_cmdq_head *);
142 static void maple_unit_probe(struct maple_softc *);
143 static void maple_unit_ping(struct maple_softc *);
144 static int maple_send_defered_periodic(struct maple_softc *);
145 static void maple_send_periodic(struct maple_softc *);
146 static void maple_remove_from_queues(struct maple_softc *,
147 struct maple_unit *);
148 static int maple_retry(struct maple_softc *, struct maple_unit *,
149 enum maple_dma_stat);
150 static void maple_queue_retry(struct maple_softc *);
151 static void maple_check_responses(struct maple_softc *);
152 static void maple_event_thread(void *);
153 static int maple_intr(void *);
154 static void maple_callout(void *);
155
156 int maple_alloc_dma(size_t, vaddr_t *, paddr_t *);
157 #if 0
158 void maple_free_dma(paddr_t, size_t);
159 #endif
160
161 /*
162 * Global variables.
163 */
164 int maple_polling; /* Are we polling? (Debugger mode) */
165
166 CFATTACH_DECL(maple, sizeof(struct maple_softc),
167 maplematch, mapleattach, NULL, NULL);
168
169 extern struct cfdriver maple_cd;
170
171 dev_type_open(mapleopen);
172 dev_type_close(mapleclose);
173 dev_type_ioctl(mapleioctl);
174
175 const struct cdevsw maple_cdevsw = {
176 mapleopen, mapleclose, noread, nowrite, mapleioctl,
177 nostop, notty, nopoll, nommap, nokqfilter,
178 };
179
180 static int
181 maplematch(struct device *parent, struct cfdata *cf, void *aux)
182 {
183
184 return 1;
185 }
186
187 static void
188 mapleattach(struct device *parent, struct device *self, void *aux)
189 {
190 struct maple_softc *sc;
191 struct maple_unit *u;
192 vaddr_t dmabuffer;
193 paddr_t dmabuffer_phys;
194 uint32_t *p;
195 int port, subunit, f;
196
197 sc = (struct maple_softc *)self;
198
199 printf(": %s\n", sysasic_intr_string(IPL_MAPLE));
200
201 if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
202 printf("%s: unable to allocate DMA buffers.\n",
203 sc->sc_dev.dv_xname);
204 return;
205 }
206
207 p = (uint32_t *)dmabuffer;
208
209 for (port = 0; port < MAPLE_PORTS; port++) {
210 for (subunit = 0; subunit < MAPLE_SUBUNITS; subunit++) {
211 u = &sc->sc_unit[port][subunit];
212 u->port = port;
213 u->subunit = subunit;
214 u->u_dma_stat = MAPLE_DMA_IDLE;
215 u->u_rxbuf = p;
216 u->u_rxbuf_phys = SH3_P2SEG_TO_PHYS(p);
217 p += 256;
218
219 for (f = 0; f < MAPLE_NFUNC; f++) {
220 u->u_func[f].f_funcno = f;
221 u->u_func[f].f_unit = u;
222 }
223 }
224 }
225
226 sc->sc_txbuf = p;
227 sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
228
229 SIMPLEQ_INIT(&sc->sc_retryq);
230 TAILQ_INIT(&sc->sc_probeq);
231 TAILQ_INIT(&sc->sc_pingq);
232 TAILQ_INIT(&sc->sc_periodicq);
233 TAILQ_INIT(&sc->sc_periodicdeferq);
234 TAILQ_INIT(&sc->sc_acmdq);
235 TAILQ_INIT(&sc->sc_pcmdq);
236
237 MAPLE_RESET = RESET_MAGIC;
238 MAPLE_RESET2 = 0;
239
240 MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
241
242 MAPLE_ENABLE = 1;
243
244 maple_polling = 1;
245 maple_scanbus(sc);
246
247 callout_init(&sc->maple_callout_ch);
248
249 sc->sc_intrhand = sysasic_intr_establish(SYSASIC_EVENT_MAPLE_DMADONE,
250 IPL_MAPLE, maple_intr, sc);
251
252 config_pending_incr(); /* create thread before mounting root */
253 kthread_create(maple_create_event_thread, sc);
254 }
255
256 static void
257 maple_create_event_thread(void *arg)
258 {
259 struct maple_softc *sc = arg;
260
261 if (kthread_create1(maple_event_thread, sc, &sc->event_thread,
262 "%s", sc->sc_dev.dv_xname) == 0)
263 return;
264
265 panic("%s: unable to create event thread", sc->sc_dev.dv_xname);
266 }
267
268 /*
269 * initial device attach
270 */
271 static void
272 maple_scanbus(struct maple_softc *sc)
273 {
274 struct maple_unit *u;
275 int port;
276 int last_port, last_subunit;
277 int i;
278
279 KASSERT(cold && maple_polling);
280
281 /* probe all ports */
282 for (port = 0; port < MAPLE_PORTS; port++) {
283 u = &sc->sc_unit[port][0];
284 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
285 {
286 char buf[16];
287 printf("%s: queued to probe 1\n",
288 maple_unit_name(buf, u->port, u->subunit));
289 }
290 #endif
291 TAILQ_INSERT_TAIL(&sc->sc_probeq, u, u_q);
292 u->u_queuestat = MAPLE_QUEUE_PROBE;
293 }
294
295 last_port = last_subunit = -1;
296 maple_begin_txbuf(sc);
297 while ((u = TAILQ_FIRST(&sc->sc_probeq)) != NULL) {
298 /*
299 * Check wrap condition
300 */
301 if (u->port < last_port || u->subunit <= last_subunit)
302 break;
303 last_port = u->port;
304 if (u->port == MAPLE_PORTS - 1)
305 last_subunit = u->subunit;
306
307 maple_unit_probe(sc);
308 for (i = 10 /* just not forever */; maple_end_txbuf(sc); i--) {
309 maple_start_poll(sc);
310 maple_check_responses(sc);
311 if (i == 0)
312 break;
313 /* attach may issue cmds */
314 maple_queue_cmds(sc, &sc->sc_acmdq);
315 }
316 }
317 }
318
319 void
320 maple_run_polling(struct device *dev)
321 {
322 struct maple_softc *sc;
323 int port, subunit;
324 int i;
325
326 sc = (struct maple_softc *)dev;
327
328 /*
329 * first, make sure polling works
330 */
331 while (MAPLE_STATE != 0) /* XXX may lost a DMA cycle */
332 ;
333
334 /* XXX this will break internal state */
335 for (port = 0; port < MAPLE_PORTS; port++)
336 for (subunit = 0; subunit < MAPLE_SUBUNITS; subunit++)
337 sc->sc_unit[port][subunit].u_dma_stat = MAPLE_DMA_IDLE;
338 SIMPLEQ_INIT(&sc->sc_retryq); /* XXX discard current retrys */
339
340 /*
341 * do polling (periodic status check only)
342 */
343 maple_begin_txbuf(sc);
344 maple_send_defered_periodic(sc);
345 maple_send_periodic(sc);
346 for (i = 10 /* just not forever */; maple_end_txbuf(sc); i--) {
347 maple_start_poll(sc);
348 maple_check_responses(sc);
349 if (i == 0)
350 break;
351
352 /* maple_check_responses() has executed maple_begin_txbuf() */
353 maple_queue_retry(sc);
354 maple_send_defered_periodic(sc);
355 }
356 }
357
358 static char *
359 maple_unit_name(char *buf, int port, int subunit)
360 {
361
362 sprintf(buf, "maple%c", port + 'A');
363 if (subunit)
364 sprintf(buf+6, "%d", subunit);
365
366 return buf;
367 }
368
369 int
370 maple_alloc_dma(size_t size, vaddr_t *vap, paddr_t *pap)
371 {
372 extern paddr_t avail_start, avail_end; /* from pmap.c */
373 struct pglist mlist;
374 struct vm_page *m;
375 int error;
376
377 size = round_page(size);
378
379 error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
380 0, 0, &mlist, 1, 0);
381 if (error)
382 return error;
383
384 m = TAILQ_FIRST(&mlist);
385 *pap = VM_PAGE_TO_PHYS(m);
386 *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
387
388 return 0;
389 }
390
391 #if 0 /* currently unused */
392 void
393 maple_free_dma(paddr_t paddr, size_t size)
394 {
395 struct pglist mlist;
396 struct vm_page *m;
397 bus_addr_t addr;
398
399 TAILQ_INIT(&mlist);
400 for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
401 m = PHYS_TO_VM_PAGE(addr);
402 TAILQ_INSERT_TAIL(&mlist, m, pageq);
403 }
404 uvm_pglistfree(&mlist);
405 }
406 #endif
407
408 static void
409 maple_begin_txbuf(struct maple_softc *sc)
410 {
411
412 sc->sc_txlink = sc->sc_txpos = sc->sc_txbuf;
413 SIMPLEQ_INIT(&sc->sc_dmaq);
414 }
415
416 static int
417 maple_end_txbuf(struct maple_softc *sc)
418 {
419
420 /* if no frame have been written, we can't mark the
421 list end, and so the DMA must not be activated */
422 if (sc->sc_txpos == sc->sc_txbuf)
423 return 0;
424
425 *sc->sc_txlink |= 0x80000000;
426
427 return 1;
428 }
429
430 static const int8_t subunit_code[] = { 0x20, 0x01, 0x02, 0x04, 0x08, 0x10 };
431
432 static void
433 maple_queue_command(struct maple_softc *sc, struct maple_unit *u,
434 int command, int datalen, const void *dataaddr)
435 {
436 int to, from;
437 uint32_t *p = sc->sc_txpos;
438
439 /* Max data length = 255 longs = 1020 bytes */
440 KASSERT(datalen >= 0 && datalen <= 255);
441
442 /* Compute sender and recipient address */
443 from = u->port << 6;
444 to = from | subunit_code[u->subunit];
445
446 sc->sc_txlink = p;
447
448 /* Set length of packet and destination port (A-D) */
449 *p++ = datalen | (u->port << 16);
450
451 /* Write address to receive buffer where the response
452 frame should be put */
453 *p++ = u->u_rxbuf_phys;
454
455 /* Create the frame header. The fields are assembled "backwards"
456 because of the Maple Bus big-endianness. */
457 *p++ = (command & 0xff) | (to << 8) | (from << 16) | (datalen << 24);
458
459 /* Copy parameter data, if any */
460 if (datalen > 0) {
461 const uint32_t *param = dataaddr;
462 int i;
463 for (i = 0; i < datalen; i++)
464 *p++ = *param++;
465 }
466
467 sc->sc_txpos = p;
468
469 SIMPLEQ_INSERT_TAIL(&sc->sc_dmaq, u, u_dmaq);
470 }
471
472 static void
473 maple_write_command(struct maple_softc *sc, struct maple_unit *u, int command,
474 int datalen, const void *dataaddr)
475 {
476 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
477 char buf[16];
478
479 if (u->u_retrycnt)
480 printf("%s: retrycnt %d\n",
481 maple_unit_name(buf, u->port, u->subunit), u->u_retrycnt);
482 #endif
483 u->u_retrycnt = 0;
484 u->u_command = command;
485 u->u_datalen = datalen;
486 u->u_dataaddr = dataaddr;
487
488 maple_queue_command(sc, u, command, datalen, dataaddr);
489 }
490
491 /* start DMA */
492 static void
493 maple_start(struct maple_softc *sc)
494 {
495
496 MAPLE_DMAADDR = sc->sc_txbuf_phys;
497 MAPLE_STATE = 1;
498 }
499
500 /* start DMA -- wait until DMA done */
501 static void
502 maple_start_poll(struct maple_softc *sc)
503 {
504
505 MAPLE_DMAADDR = sc->sc_txbuf_phys;
506 MAPLE_STATE = 1;
507 while (MAPLE_STATE != 0)
508 ;
509 }
510
511 static void
512 maple_check_subunit_change(struct maple_softc *sc, struct maple_unit *u)
513 {
514 struct maple_unit *u1;
515 int port;
516 int8_t unit_map;
517 int units, un;
518 int i;
519
520 KASSERT(u->subunit == 0);
521
522 port = u->port;
523 unit_map = ((int8_t *) u->u_rxbuf)[2];
524 if (sc->sc_port_unit_map[port] == unit_map)
525 return;
526
527 units = ((unit_map & 0x1f) << 1) | 1;
528 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
529 {
530 char buf[16];
531 printf("%s: unit_map 0x%x -> 0x%x (units 0x%x)\n",
532 maple_unit_name(buf, u->port, u->subunit),
533 sc->sc_port_unit_map[port], unit_map, units);
534 }
535 #endif
536 #if 0 /* this detects unit removal rapidly but is not reliable */
537 /* check for unit change */
538 un = sc->sc_port_units[port] & ~units;
539
540 /* detach removed devices */
541 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
542 if (un & (1 << i))
543 maple_detach_unit_nofix(sc, &sc->sc_unit[port][i]);
544 #endif
545
546 sc->sc_port_unit_map[port] = unit_map;
547
548 /* schedule scanning child devices */
549 un = units & ~sc->sc_port_units[port];
550 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
551 if (un & (1 << i)) {
552 u1 = &sc->sc_unit[port][i];
553 maple_remove_from_queues(sc, u1);
554 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
555 {
556 char buf[16];
557 printf("%s: queued to probe 2\n",
558 maple_unit_name(buf, u1->port, u1->subunit));
559 }
560 #endif
561 TAILQ_INSERT_HEAD(&sc->sc_probeq, u1, u_q);
562 u1->u_queuestat = MAPLE_QUEUE_PROBE;
563 u1->u_proberetry = 0;
564 }
565 }
566
567 static void
568 maple_check_unit_change(struct maple_softc *sc, struct maple_unit *u)
569 {
570 struct maple_devinfo *newinfo = (void *) (u->u_rxbuf + 1);
571 int port, subunit;
572
573 port = u->port;
574 subunit = u->subunit;
575 if (memcmp(&u->devinfo, newinfo, sizeof(struct maple_devinfo)) == 0)
576 goto out; /* no change */
577
578 /* unit inserted */
579
580 /* attach this device */
581 u->devinfo = *newinfo;
582 maple_attach_unit(sc, u);
583
584 out:
585 maple_remove_from_queues(sc, u);
586 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
587 {
588 char buf[16];
589 printf("%s: queued to ping\n",
590 maple_unit_name(buf, u->port, u->subunit));
591 }
592 #endif
593 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
594 u->u_queuestat = MAPLE_QUEUE_PING;
595 }
596
597 static void
598 maple_print_unit(void *aux, const char *pnp)
599 {
600 struct maple_attach_args *ma = aux;
601 int port, subunit;
602 char buf[16];
603 char *prod, *p, oc;
604
605 port = ma->ma_unit->port;
606 subunit = ma->ma_unit->subunit;
607
608 if (pnp != NULL)
609 printf("%s at %s", maple_unit_name(buf, port, subunit), pnp);
610
611 printf(" port %d", port);
612
613 if (subunit != 0)
614 printf(" subunit %d", subunit);
615
616 #ifdef MAPLE_DEBUG
617 printf(": a %#x c %#x fn %#x d %#x,%#x,%#x",
618 ma->ma_devinfo->di_area_code,
619 ma->ma_devinfo->di_connector_direction,
620 be32toh(ma->ma_devinfo->di_func),
621 be32toh(ma->ma_devinfo->di_function_data[0]),
622 be32toh(ma->ma_devinfo->di_function_data[1]),
623 be32toh(ma->ma_devinfo->di_function_data[2]));
624 #endif
625
626 /* nul termination */
627 prod = ma->ma_devinfo->di_product_name;
628 for (p = prod + sizeof ma->ma_devinfo->di_product_name; p >= prod; p--)
629 if (p[-1] != '\0' && p[-1] != ' ')
630 break;
631 oc = *p;
632 *p = '\0';
633
634 printf(": %s", prod);
635
636 *p = oc; /* restore */
637 }
638
639 static int
640 maplesubmatch(struct device *parent, struct cfdata *match, void *aux)
641 {
642 struct maple_attach_args *ma = aux;
643
644 if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
645 match->cf_loc[MAPLECF_PORT] != ma->ma_unit->port)
646 return 0;
647
648 if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
649 match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_unit->subunit)
650 return 0;
651
652 return config_match(parent, match, aux);
653 }
654
655 static int
656 mapleprint(void *aux, const char *str)
657 {
658 struct maple_attach_args *ma = aux;
659
660 #ifdef MAPLE_DEBUG
661 if (str)
662 aprint_normal("%s", str);
663 aprint_normal(" function %d", ma->ma_function);
664
665 return UNCONF;
666 #else /* quiet */
667 if (!str)
668 aprint_normal(" function %d", ma->ma_function);
669
670 return QUIET;
671 #endif
672 }
673
674 static void
675 maple_attach_unit(struct maple_softc *sc, struct maple_unit *u)
676 {
677 struct maple_attach_args ma;
678 uint32_t func;
679 int f;
680 char oldxname[16];
681
682 ma.ma_unit = u;
683 ma.ma_devinfo = &u->devinfo;
684 ma.ma_basedevinfo = &sc->sc_unit[u->port][0].devinfo;
685 func = be32toh(ma.ma_devinfo->di_func);
686
687 maple_print_unit(&ma, sc->sc_dev.dv_xname);
688 printf("\n");
689 strcpy(oldxname, sc->sc_dev.dv_xname);
690 maple_unit_name(sc->sc_dev.dv_xname, u->port, u->subunit);
691
692 for (f = 0; f < MAPLE_NFUNC; f++) {
693 u->u_func[f].f_callback = NULL;
694 u->u_func[f].f_arg = NULL;
695 u->u_func[f].f_cmdstat = MAPLE_CMDSTAT_NONE;
696 u->u_func[f].f_dev = NULL;
697 if (func & MAPLE_FUNC(f)) {
698 ma.ma_function = f;
699 u->u_func[f].f_dev = config_found_sm(&sc->sc_dev, &ma,
700 mapleprint, maplesubmatch);
701 u->u_ping_func = f; /* XXX using largest func */
702 }
703 }
704 #ifdef MAPLE_MEMCARD_PING_HACK
705 /*
706 * Some 3rd party memory card pretend to be Visual Memory,
707 * but need special handling for ping.
708 */
709 if (func == (MAPLE_FUNC(MAPLE_FN_MEMCARD) | MAPLE_FUNC(MAPLE_FN_LCD) |
710 MAPLE_FUNC(MAPLE_FN_CLOCK))) {
711 u->u_ping_func = MAPLE_FN_MEMCARD;
712 u->u_ping_stat = MAPLE_PING_MEMCARD;
713 } else {
714 u->u_ping_stat = MAPLE_PING_NORMAL;
715 }
716 #endif
717 strcpy(sc->sc_dev.dv_xname, oldxname);
718
719 sc->sc_port_units[u->port] |= 1 << u->subunit;
720 }
721
722 static void
723 maple_detach_unit_nofix(struct maple_softc *sc, struct maple_unit *u)
724 {
725 struct maple_func *fn;
726 struct device *dev;
727 struct maple_unit *u1;
728 int port;
729 int error;
730 int i;
731 char buf[16];
732
733 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
734 printf("%s: remove\n", maple_unit_name(buf, u->port, u->subunit));
735 #endif
736 maple_remove_from_queues(sc, u);
737 port = u->port;
738 sc->sc_port_units[port] &= ~(1 << u->subunit);
739
740 if (u->subunit == 0) {
741 for (i = MAPLE_SUBUNITS - 1; i > 0; i--)
742 maple_detach_unit_nofix(sc, &sc->sc_unit[port][i]);
743 }
744
745 for (fn = u->u_func; fn < &u->u_func[MAPLE_NFUNC]; fn++) {
746 if ((dev = fn->f_dev) != NULL) {
747 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
748 printf("%s: detaching func %d\n",
749 maple_unit_name(buf, port, u->subunit),
750 fn->f_funcno);
751 #endif
752
753 /*
754 * Remove functions from command queue.
755 */
756 switch (fn->f_cmdstat) {
757 case MAPLE_CMDSTAT_ASYNC:
758 case MAPLE_CMDSTAT_PERIODIC_DEFERED:
759 TAILQ_REMOVE(&sc->sc_acmdq, fn, f_cmdq);
760 break;
761 case MAPLE_CMDSTAT_ASYNC_PERIODICQ:
762 case MAPLE_CMDSTAT_PERIODIC:
763 TAILQ_REMOVE(&sc->sc_pcmdq, fn, f_cmdq);
764 break;
765 default:
766 break;
767 }
768
769 /*
770 * Detach devices.
771 */
772 if ((error = config_detach(fn->f_dev, DETACH_FORCE))) {
773 printf("%s: failed to detach %s (func %d), errno %d\n",
774 maple_unit_name(buf, port, u->subunit),
775 fn->f_dev->dv_xname, fn->f_funcno, error);
776 }
777 }
778
779 maple_enable_periodic(&sc->sc_dev, u, fn->f_funcno, 0);
780
781 fn->f_dev = NULL;
782 fn->f_callback = NULL;
783 fn->f_arg = NULL;
784 fn->f_cmdstat = MAPLE_CMDSTAT_NONE;
785 }
786 if (u->u_dma_stat == MAPLE_DMA_RETRY) {
787 /* XXX expensive? */
788 SIMPLEQ_FOREACH(u1, &sc->sc_retryq, u_dmaq) {
789 if (u1 == u) {
790 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
791 printf("%s: abort retry\n",
792 maple_unit_name(buf, port, u->subunit));
793 #endif
794 SIMPLEQ_REMOVE(&sc->sc_retryq, u, maple_unit,
795 u_dmaq);
796 break;
797 }
798 }
799 }
800 u->u_dma_stat = MAPLE_DMA_IDLE;
801 u->u_noping = 0;
802 /* u->u_dma_func = uninitialized; */
803 KASSERT(u->getcond_func_set == 0);
804 memset(&u->devinfo, 0, sizeof(struct maple_devinfo));
805
806 if (u->subunit == 0) {
807 sc->sc_port_unit_map[port] = 0;
808 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
809 {
810 char buf[16];
811 printf("%s: queued to probe 3\n",
812 maple_unit_name(buf, port, u->subunit));
813 }
814 #endif
815 TAILQ_INSERT_TAIL(&sc->sc_probeq, u, u_q);
816 u->u_queuestat = MAPLE_QUEUE_PROBE;
817 }
818 }
819
820 static void
821 maple_detach_unit(struct maple_softc *sc, struct maple_unit *u)
822 {
823
824 maple_detach_unit_nofix(sc, u);
825 if (u->subunit != 0)
826 sc->sc_port_unit_map[u->port] &= ~(1 << (u->subunit - 1));
827 }
828
829 /*
830 * Send a command (called by drivers)
831 *
832 * The "cataaddr" must not point at temporary storage like stack.
833 * Only one command (per function) is valid at a time.
834 */
835 void
836 maple_command(struct device *dev, struct maple_unit *u, int func,
837 int command, int datalen, const void *dataaddr, int flags)
838 {
839 struct maple_softc *sc = (void *) dev;
840 struct maple_func *fn;
841 int s;
842
843 KASSERT(func >= 0 && func < 32);
844 KASSERT(command);
845 KASSERT((flags & ~MAPLE_FLAG_CMD_PERIODIC_TIMING) == 0);
846
847 s = splsoftclock();
848
849 fn = &u->u_func[func];
850 #if 1 /*def DIAGNOSTIC*/
851 {char buf[16];
852 if (fn->f_cmdstat != MAPLE_CMDSTAT_NONE)
853 panic("maple_command: %s func %d: requesting more than one commands",
854 maple_unit_name(buf, u->port, u->subunit), func);
855 }
856 #endif
857 fn->f_command = command;
858 fn->f_datalen = datalen;
859 fn->f_dataaddr = dataaddr;
860 if (flags & MAPLE_FLAG_CMD_PERIODIC_TIMING) {
861 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC;
862 TAILQ_INSERT_TAIL(&sc->sc_pcmdq, fn, f_cmdq);
863 } else {
864 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC;
865 TAILQ_INSERT_TAIL(&sc->sc_acmdq, fn, f_cmdq);
866 wakeup(&sc->sc_event); /* wake for async event */
867 }
868 splx(s);
869 }
870
871 static void
872 maple_queue_cmds(struct maple_softc *sc,
873 struct maple_cmdq_head *head)
874 {
875 struct maple_func *fn, *nextfn;
876 struct maple_unit *u;
877
878 /*
879 * Note: since the queue element may be queued immediately,
880 * we can't use TAILQ_FOREACH.
881 */
882 fn = TAILQ_FIRST(head);
883 TAILQ_INIT(head);
884 for ( ; fn; fn = nextfn) {
885 nextfn = TAILQ_NEXT(fn, f_cmdq);
886
887 KASSERT(fn->f_cmdstat != MAPLE_CMDSTAT_NONE);
888 u = fn->f_unit;
889 if (u->u_dma_stat == MAPLE_DMA_IDLE) {
890 maple_write_command(sc, u,
891 fn->f_command, fn->f_datalen, fn->f_dataaddr);
892 u->u_dma_stat = (fn->f_cmdstat == MAPLE_CMDSTAT_ASYNC ||
893 fn->f_cmdstat == MAPLE_CMDSTAT_ASYNC_PERIODICQ) ?
894 MAPLE_DMA_ACMD : MAPLE_DMA_PCMD;
895 u->u_dma_func = fn->f_funcno;
896 fn->f_cmdstat = MAPLE_CMDSTAT_NONE;
897 } else if (u->u_dma_stat == MAPLE_DMA_RETRY) {
898 /* unit is busy --- try again */
899 /*
900 * always add to periodic command queue
901 * (wait until the next periodic timing),
902 * since the unit will never be freed until the
903 * next periodic timing.
904 */
905 switch (fn->f_cmdstat) {
906 case MAPLE_CMDSTAT_ASYNC:
907 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC_PERIODICQ;
908 break;
909 case MAPLE_CMDSTAT_PERIODIC_DEFERED:
910 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC;
911 break;
912 default:
913 break;
914 }
915 TAILQ_INSERT_TAIL(&sc->sc_pcmdq, fn, f_cmdq);
916 } else {
917 /* unit is busy --- try again */
918 /*
919 * always add to async command queue
920 * (process immediately)
921 */
922 switch (fn->f_cmdstat) {
923 case MAPLE_CMDSTAT_ASYNC_PERIODICQ:
924 fn->f_cmdstat = MAPLE_CMDSTAT_ASYNC;
925 break;
926 case MAPLE_CMDSTAT_PERIODIC:
927 fn->f_cmdstat = MAPLE_CMDSTAT_PERIODIC_DEFERED;
928 break;
929 default:
930 break;
931 }
932 TAILQ_INSERT_TAIL(&sc->sc_acmdq, fn, f_cmdq);
933 }
934 }
935 }
936
937 /* schedule probing a device */
938 static void
939 maple_unit_probe(struct maple_softc *sc)
940 {
941 struct maple_unit *u;
942
943 if ((u = TAILQ_FIRST(&sc->sc_probeq)) != NULL) {
944 KASSERT(u->u_dma_stat == MAPLE_DMA_IDLE);
945 KASSERT(u->u_queuestat == MAPLE_QUEUE_PROBE);
946 maple_remove_from_queues(sc, u);
947 maple_write_command(sc, u, MAPLE_COMMAND_DEVINFO, 0, NULL);
948 u->u_dma_stat = MAPLE_DMA_PROBE;
949 /* u->u_dma_func = ignored; */
950 }
951 }
952
953 /*
954 * Enable/disable unit pinging (called by drivers)
955 */
956 /* ARGSUSED */
957 void
958 maple_enable_unit_ping(struct device *dev, struct maple_unit *u,
959 int func, int enable)
960 {
961 #if 0 /* currently unused */
962 struct maple_softc *sc = (void *) dev;
963 #endif
964
965 if (enable)
966 u->u_noping &= ~MAPLE_FUNC(func);
967 else
968 u->u_noping |= MAPLE_FUNC(func);
969 }
970
971 /* schedule pinging a device */
972 static void
973 maple_unit_ping(struct maple_softc *sc)
974 {
975 struct maple_unit *u;
976 struct maple_func *fn;
977 #ifdef MAPLE_MEMCARD_PING_HACK
978 static const uint32_t memcard_ping_arg[2] = {
979 0x02000000, /* htobe32(MAPLE_FUNC(MAPLE_FN_MEMCARD)) */
980 0 /* pt (1 byte) and unused 3 bytes */
981 };
982 #endif
983
984 if ((u = TAILQ_FIRST(&sc->sc_pingq)) != NULL) {
985 KASSERT(u->u_queuestat == MAPLE_QUEUE_PING);
986 maple_remove_from_queues(sc, u);
987 if (u->u_dma_stat == MAPLE_DMA_IDLE && u->u_noping == 0) {
988 #ifdef MAPLE_MEMCARD_PING_HACK
989 if (u->u_ping_stat == MAPLE_PING_MINFO) {
990 /* use MINFO for some memory cards */
991 maple_write_command(sc, u,
992 MAPLE_COMMAND_GETMINFO,
993 2, memcard_ping_arg);
994 } else
995 #endif
996 {
997 fn = &u->u_func[u->u_ping_func];
998 fn->f_work = htobe32(MAPLE_FUNC(u->u_ping_func));
999 maple_write_command(sc, u,
1000 MAPLE_COMMAND_GETCOND,
1001 1, &fn->f_work);
1002 }
1003 u->u_dma_stat = MAPLE_DMA_PING;
1004 /* u->u_dma_func = XXX; */
1005 } else {
1006 /* no need if periodic */
1007 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
1008 u->u_queuestat = MAPLE_QUEUE_PING;
1009 }
1010 }
1011 }
1012
1013 /*
1014 * Enable/disable periodic GETCOND (called by drivers)
1015 */
1016 void
1017 maple_enable_periodic(struct device *dev, struct maple_unit *u,
1018 int func, int on)
1019 {
1020 struct maple_softc *sc = (void *) dev;
1021 struct maple_func *fn;
1022
1023 KASSERT(func >= 0 && func < 32);
1024
1025 fn = &u->u_func[func];
1026
1027 if (on) {
1028 if (fn->f_periodic_stat == MAPLE_PERIODIC_NONE) {
1029 TAILQ_INSERT_TAIL(&sc->sc_periodicq, fn, f_periodicq);
1030 fn->f_periodic_stat = MAPLE_PERIODIC_INQ;
1031 u->getcond_func_set |= MAPLE_FUNC(func);
1032 }
1033 } else {
1034 if (fn->f_periodic_stat == MAPLE_PERIODIC_INQ)
1035 TAILQ_REMOVE(&sc->sc_periodicq, fn, f_periodicq);
1036 else if (fn->f_periodic_stat == MAPLE_PERIODIC_DEFERED)
1037 TAILQ_REMOVE(&sc->sc_periodicdeferq, fn, f_periodicq);
1038 fn->f_periodic_stat = MAPLE_PERIODIC_NONE;
1039 u->getcond_func_set &= ~MAPLE_FUNC(func);
1040 }
1041 }
1042
1043 /*
1044 * queue periodic GETCOND
1045 */
1046 static int
1047 maple_send_defered_periodic(struct maple_softc *sc)
1048 {
1049 struct maple_unit *u;
1050 struct maple_func *fn, *nextfn;
1051 int defer_remain = 0;
1052
1053 for (fn = TAILQ_FIRST(&sc->sc_periodicdeferq); fn; fn = nextfn) {
1054 KASSERT(fn->f_periodic_stat == MAPLE_PERIODIC_DEFERED);
1055
1056 nextfn = TAILQ_NEXT(fn, f_periodicq);
1057
1058 u = fn->f_unit;
1059 if (u->u_dma_stat == MAPLE_DMA_IDLE ||
1060 u->u_dma_stat == MAPLE_DMA_RETRY) {
1061 /*
1062 * if IDLE -> queue this request
1063 * if RETRY -> the unit never be freed until the next
1064 * periodic timing, so just restore to
1065 * the normal periodic queue.
1066 */
1067 TAILQ_REMOVE(&sc->sc_periodicdeferq, fn, f_periodicq);
1068 TAILQ_INSERT_TAIL(&sc->sc_periodicq, fn, f_periodicq);
1069 fn->f_periodic_stat = MAPLE_PERIODIC_INQ;
1070
1071 if (u->u_dma_stat == MAPLE_DMA_IDLE) {
1072 /*
1073 * queue periodic command
1074 */
1075 fn->f_work = htobe32(MAPLE_FUNC(fn->f_funcno));
1076 maple_write_command(sc, u,
1077 MAPLE_COMMAND_GETCOND, 1, &fn->f_work);
1078 u->u_dma_stat = MAPLE_DMA_PERIODIC;
1079 u->u_dma_func = fn->f_funcno;
1080 }
1081 } else {
1082 defer_remain = 1;
1083 }
1084 }
1085
1086 return defer_remain;
1087 }
1088
1089 static void
1090 maple_send_periodic(struct maple_softc *sc)
1091 {
1092 struct maple_unit *u;
1093 struct maple_func *fn, *nextfn;
1094
1095 for (fn = TAILQ_FIRST(&sc->sc_periodicq); fn; fn = nextfn) {
1096 KASSERT(fn->f_periodic_stat == MAPLE_PERIODIC_INQ);
1097
1098 nextfn = TAILQ_NEXT(fn, f_periodicq);
1099
1100 u = fn->f_unit;
1101 if (u->u_dma_stat != MAPLE_DMA_IDLE) {
1102 if (u->u_dma_stat != MAPLE_DMA_RETRY) {
1103 /*
1104 * can't be queued --- move to defered queue
1105 */
1106 TAILQ_REMOVE(&sc->sc_periodicq, fn,
1107 f_periodicq);
1108 TAILQ_INSERT_TAIL(&sc->sc_periodicdeferq, fn,
1109 f_periodicq);
1110 fn->f_periodic_stat = MAPLE_PERIODIC_DEFERED;
1111 }
1112 } else {
1113 /*
1114 * queue periodic command
1115 */
1116 fn->f_work = htobe32(MAPLE_FUNC(fn->f_funcno));
1117 maple_write_command(sc, u, MAPLE_COMMAND_GETCOND,
1118 1, &fn->f_work);
1119 u->u_dma_stat = MAPLE_DMA_PERIODIC;
1120 u->u_dma_func = fn->f_funcno;
1121 }
1122 }
1123 }
1124
1125 static void
1126 maple_remove_from_queues(struct maple_softc *sc, struct maple_unit *u)
1127 {
1128
1129 /* remove from queues */
1130 if (u->u_queuestat == MAPLE_QUEUE_PROBE)
1131 TAILQ_REMOVE(&sc->sc_probeq, u, u_q);
1132 else if (u->u_queuestat == MAPLE_QUEUE_PING)
1133 TAILQ_REMOVE(&sc->sc_pingq, u, u_q);
1134 #ifdef DIAGNOSTIC
1135 else if (u->u_queuestat != MAPLE_QUEUE_NONE)
1136 panic("maple_remove_from_queues: queuestat %d", u->u_queuestat);
1137 #endif
1138 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1139 if (u->u_queuestat != MAPLE_QUEUE_NONE) {
1140 char buf[16];
1141 printf("%s: dequeued\n",
1142 maple_unit_name(buf, u->port, u->subunit));
1143 }
1144 #endif
1145
1146 u->u_queuestat = MAPLE_QUEUE_NONE;
1147 }
1148
1149 /*
1150 * retry current command at next periodic timing
1151 */
1152 static int
1153 maple_retry(struct maple_softc *sc, struct maple_unit *u,
1154 enum maple_dma_stat st)
1155 {
1156
1157 KASSERT(st != MAPLE_DMA_IDLE && st != MAPLE_DMA_RETRY);
1158
1159 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1160 if (u->u_retrycnt == 0) {
1161 char buf[16];
1162 printf("%s: retrying: %#x, %#x, %p\n",
1163 maple_unit_name(buf, u->port, u->subunit),
1164 u->u_command, u->u_datalen, u->u_dataaddr);
1165 }
1166 #endif
1167 if (u->u_retrycnt >= MAPLE_RETRY_MAX)
1168 return 1;
1169
1170 u->u_retrycnt++;
1171
1172 u->u_saved_dma_stat = st;
1173 u->u_dma_stat = MAPLE_DMA_RETRY; /* no new command before retry done */
1174 SIMPLEQ_INSERT_TAIL(&sc->sc_retryq, u, u_dmaq);
1175
1176 return 0;
1177 }
1178
1179 static void
1180 maple_queue_retry(struct maple_softc *sc)
1181 {
1182 struct maple_unit *u, *nextu;
1183
1184 /*
1185 * Note: since the queue element is queued immediately
1186 * in maple_queue_command, we can't use SIMPLEQ_FOREACH.
1187 */
1188 for (u = SIMPLEQ_FIRST(&sc->sc_retryq); u; u = nextu) {
1189 nextu = SIMPLEQ_NEXT(u, u_dmaq);
1190
1191 /*
1192 * Retrying is in the highest priority, and the unit shall
1193 * always be free.
1194 */
1195 KASSERT(u->u_dma_stat == MAPLE_DMA_RETRY);
1196 maple_queue_command(sc, u, u->u_command, u->u_datalen,
1197 u->u_dataaddr);
1198 u->u_dma_stat = u->u_saved_dma_stat;
1199
1200 #ifdef DIAGNOSTIC
1201 KASSERT(u->u_saved_dma_stat != MAPLE_DMA_IDLE);
1202 u->u_saved_dma_stat = MAPLE_DMA_IDLE;
1203 #endif
1204 }
1205 SIMPLEQ_INIT(&sc->sc_retryq);
1206 }
1207
1208 /*
1209 * Process DMA results.
1210 * Requires kernel context.
1211 */
1212 static void
1213 maple_check_responses(struct maple_softc *sc)
1214 {
1215 struct maple_unit *u, *nextu;
1216 struct maple_func *fn;
1217 maple_response_t response;
1218 int func_code, len;
1219 int flags;
1220 char buf[16];
1221
1222 /*
1223 * Note: since the queue element may be queued immediately,
1224 * we can't use SIMPLEQ_FOREACH.
1225 */
1226 for (u = SIMPLEQ_FIRST(&sc->sc_dmaq), maple_begin_txbuf(sc);
1227 u; u = nextu) {
1228 nextu = SIMPLEQ_NEXT(u, u_dmaq);
1229
1230 if (u->u_dma_stat == MAPLE_DMA_IDLE)
1231 continue; /* just detached or DDB was active */
1232
1233 /*
1234 * check for retransmission
1235 */
1236 if ((response = u->u_rxbuf[0]) == MAPLE_RESPONSE_AGAIN) {
1237 if (maple_retry(sc, u, u->u_dma_stat) == 0)
1238 continue;
1239 /* else pass error to upper layer */
1240 }
1241
1242 len = (u->u_rxbuf[0] >> 24); /* length in long */
1243 len <<= 2; /* length in byte */
1244
1245 /*
1246 * call handler
1247 */
1248 if (u->u_dma_stat == MAPLE_DMA_PERIODIC) {
1249 /*
1250 * periodic GETCOND
1251 */
1252 u->u_dma_stat = MAPLE_DMA_IDLE;
1253 func_code = u->u_dma_func;
1254 if (response == MAPLE_RESPONSE_DATATRF && len > 0 &&
1255 be32toh(u->u_rxbuf[1]) == MAPLE_FUNC(func_code)) {
1256 fn = &u->u_func[func_code];
1257 if (fn->f_dev)
1258 (*fn->f_callback)(fn->f_arg,
1259 (void *)u->u_rxbuf, len,
1260 MAPLE_FLAG_PERIODIC);
1261 } else if (response == MAPLE_RESPONSE_NONE) {
1262 /* XXX OK? */
1263 /* detach */
1264 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1265 printf("%s: func: %d: periodic response %d\n",
1266 maple_unit_name(buf, u->port, u->subunit),
1267 u->u_dma_func,
1268 response);
1269 #endif
1270 /*
1271 * Some 3rd party devices sometimes
1272 * do not respond.
1273 */
1274 if (maple_retry(sc, u, MAPLE_DMA_PERIODIC))
1275 maple_detach_unit(sc, u);
1276 }
1277 /* XXX check unexpected conditions? */
1278
1279 } else if (u->u_dma_stat == MAPLE_DMA_PROBE) {
1280 KASSERT(u->u_queuestat == MAPLE_QUEUE_NONE);
1281 u->u_dma_stat = MAPLE_DMA_IDLE;
1282 switch (response) {
1283 default:
1284 case MAPLE_RESPONSE_NONE:
1285 /*
1286 * Do not use maple_retry(), which conflicts
1287 * with probe structure.
1288 */
1289 if (u->subunit != 0 &&
1290 ++u->u_proberetry > MAPLE_PROBERETRY_MAX) {
1291 printf("%s: no response\n",
1292 maple_unit_name(buf,
1293 u->port, u->subunit));
1294 } else {
1295 /* probe again */
1296 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 2
1297 printf("%s: queued to probe 4\n",
1298 maple_unit_name(buf, u->port, u->subunit));
1299 #endif
1300 TAILQ_INSERT_TAIL(&sc->sc_probeq, u,
1301 u_q);
1302 u->u_queuestat = MAPLE_QUEUE_PROBE;
1303 }
1304 break;
1305 case MAPLE_RESPONSE_DEVINFO:
1306 /* check if the unit is changed */
1307 maple_check_unit_change(sc, u);
1308 break;
1309 }
1310
1311 } else if (u->u_dma_stat == MAPLE_DMA_PING) {
1312 KASSERT(u->u_queuestat == MAPLE_QUEUE_NONE);
1313 u->u_dma_stat = MAPLE_DMA_IDLE;
1314 switch (response) {
1315 default:
1316 case MAPLE_RESPONSE_NONE:
1317 /*
1318 * Some 3rd party devices sometimes
1319 * do not respond.
1320 */
1321 if (maple_retry(sc, u, MAPLE_DMA_PING)) {
1322 /* detach */
1323 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1324 printf("%s: ping response %d\n",
1325 maple_unit_name(buf, u->port,
1326 u->subunit),
1327 response);
1328 #endif
1329 #ifdef MAPLE_MEMCARD_PING_HACK
1330 if (u->u_ping_stat
1331 == MAPLE_PING_MEMCARD) {
1332 /*
1333 * The unit claims itself to be
1334 * a Visual Memory, and has
1335 * never responded to GETCOND.
1336 * Try again using MINFO, in
1337 * case it is a poorly
1338 * implemented 3rd party card.
1339 */
1340 #ifdef MAPLE_DEBUG
1341 printf("%s: switching ping method\n",
1342 maple_unit_name(buf,
1343 u->port, u->subunit));
1344 #endif
1345 u->u_ping_stat
1346 = MAPLE_PING_MINFO;
1347 TAILQ_INSERT_TAIL(&sc->sc_pingq,
1348 u, u_q);
1349 u->u_queuestat
1350 = MAPLE_QUEUE_PING;
1351 } else
1352 #endif /* MAPLE_MEMCARD_PING_HACK */
1353 maple_detach_unit(sc, u);
1354 }
1355 break;
1356 case MAPLE_RESPONSE_BADCMD:
1357 case MAPLE_RESPONSE_BADFUNC:
1358 case MAPLE_RESPONSE_DATATRF:
1359 TAILQ_INSERT_TAIL(&sc->sc_pingq, u, u_q);
1360 u->u_queuestat = MAPLE_QUEUE_PING;
1361 #ifdef MAPLE_MEMCARD_PING_HACK
1362 /*
1363 * If the unit responds to GETCOND, it is a
1364 * normal implementation.
1365 */
1366 if (u->u_ping_stat == MAPLE_PING_MEMCARD)
1367 u->u_ping_stat = MAPLE_PING_NORMAL;
1368 #endif
1369 break;
1370 }
1371
1372 } else {
1373 /*
1374 * Note: Do not rely on the consistency of responses.
1375 */
1376
1377 if (response == MAPLE_RESPONSE_NONE) {
1378 if (maple_retry(sc, u, u->u_dma_stat)) {
1379 /* detach */
1380 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1381 printf("%s: command response %d\n",
1382 maple_unit_name(buf, u->port,
1383 u->subunit),
1384 response);
1385 #endif
1386 maple_detach_unit(sc, u);
1387 }
1388 continue;
1389 }
1390
1391 flags = (u->u_dma_stat == MAPLE_DMA_PCMD) ?
1392 MAPLE_FLAG_CMD_PERIODIC_TIMING : 0;
1393 u->u_dma_stat = MAPLE_DMA_IDLE;
1394
1395 func_code = u->u_dma_func;
1396 fn = &u->u_func[func_code];
1397 if (fn->f_dev == NULL) {
1398 /* detached right now */
1399 #ifdef MAPLE_DEBUG
1400 printf("%s: unknown function: function %d, response %d\n",
1401 maple_unit_name(buf, u->port, u->subunit),
1402 func_code, response);
1403 #endif
1404 continue;
1405 }
1406 if (fn->f_callback != NULL) {
1407 (*fn->f_callback)(fn->f_arg,
1408 (void *)u->u_rxbuf, len, flags);
1409 }
1410 }
1411
1412 /*
1413 * check for subunit change and schedule probing subunits
1414 */
1415 if (u->subunit == 0 && response != MAPLE_RESPONSE_NONE &&
1416 response != MAPLE_RESPONSE_AGAIN &&
1417 ((int8_t *) u->u_rxbuf)[2] != sc->sc_port_unit_map[u->port])
1418 maple_check_subunit_change(sc, u);
1419 }
1420 }
1421
1422 /*
1423 * Main Maple Bus thread
1424 */
1425 static void
1426 maple_event_thread(void *arg)
1427 {
1428 struct maple_softc *sc = arg;
1429 unsigned cnt = 1; /* timing counter */
1430 int s;
1431 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1432 int noreq = 0;
1433 #endif
1434
1435 #ifdef MAPLE_DEBUG
1436 printf("%s: forked event thread, pid %d\n",
1437 sc->sc_dev.dv_xname, sc->event_thread->p_pid);
1438 #endif
1439
1440 /* begin first DMA cycle */
1441 maple_begin_txbuf(sc);
1442
1443 sc->sc_event = 1;
1444
1445 /* OK, continue booting system */
1446 maple_polling = 0;
1447 config_pending_decr();
1448
1449 for (;;) {
1450 /*
1451 * queue requests
1452 */
1453
1454 /* queue async commands */
1455 if (!TAILQ_EMPTY(&sc->sc_acmdq))
1456 maple_queue_cmds(sc, &sc->sc_acmdq);
1457
1458 /* send defered periodic command */
1459 if (!TAILQ_EMPTY(&sc->sc_periodicdeferq))
1460 maple_send_defered_periodic(sc);
1461
1462 /* queue periodic commands */
1463 if (sc->sc_event) {
1464 /* queue commands on periodic timing */
1465 if (!TAILQ_EMPTY(&sc->sc_pcmdq))
1466 maple_queue_cmds(sc, &sc->sc_pcmdq);
1467
1468 /* retry */
1469 if (!SIMPLEQ_EMPTY(&sc->sc_retryq))
1470 maple_queue_retry(sc);
1471
1472 if ((cnt & 31) == 0) /* XXX */
1473 maple_unit_probe(sc);
1474 cnt++;
1475
1476 maple_send_periodic(sc);
1477 if ((cnt & 7) == 0) /* XXX */
1478 maple_unit_ping(sc);
1479
1480 /*
1481 * schedule periodic event
1482 */
1483 sc->sc_event = 0;
1484 callout_reset(&sc->maple_callout_ch,
1485 MAPLE_CALLOUT_TICKS, maple_callout, sc);
1486 }
1487
1488 if (maple_end_txbuf(sc)) {
1489
1490 /*
1491 * start DMA
1492 */
1493 s = splmaple();
1494 maple_start(sc);
1495
1496 /*
1497 * wait until DMA done
1498 */
1499 if (tsleep(&sc->sc_dmadone, PWAIT, "mdma", hz)
1500 == EWOULDBLOCK) {
1501 /* was DDB active? */
1502 printf("%s: timed out\n", sc->sc_dev.dv_xname);
1503 }
1504 splx(s);
1505
1506 /*
1507 * call handlers
1508 */
1509 maple_check_responses(sc);
1510 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1511 noreq = 0;
1512 #endif
1513 }
1514 #if defined(MAPLE_DEBUG) && MAPLE_DEBUG > 1
1515 else {
1516 /* weird if occurs in succession */
1517 #if MAPLE_DEBUG <= 2
1518 if (noreq) /* ignore first time */
1519 #endif
1520 printf("%s: no request %d\n",
1521 sc->sc_dev.dv_xname, noreq);
1522 noreq++;
1523 }
1524 #endif
1525
1526 /*
1527 * wait for an event
1528 */
1529 s = splsoftclock();
1530 if (TAILQ_EMPTY(&sc->sc_acmdq) && sc->sc_event == 0 &&
1531 TAILQ_EMPTY(&sc->sc_periodicdeferq)) {
1532 if (tsleep(&sc->sc_event, PWAIT, "mslp", hz)
1533 == EWOULDBLOCK) {
1534 printf("%s: event timed out\n",
1535 sc->sc_dev.dv_xname);
1536 }
1537
1538 }
1539 splx(s);
1540
1541 }
1542
1543 #if 0 /* maple root device can't be detached */
1544 kthread_exit(0);
1545 /* NOTREACHED */
1546 #endif
1547 }
1548
1549 static int
1550 maple_intr(void *arg)
1551 {
1552 struct maple_softc *sc = arg;
1553
1554 wakeup(&sc->sc_dmadone);
1555
1556 return 1;
1557 }
1558
1559 static void
1560 maple_callout(void *ctx)
1561 {
1562 struct maple_softc *sc = ctx;
1563
1564 sc->sc_event = 1; /* mark as periodic event */
1565 wakeup(&sc->sc_event);
1566 }
1567
1568 /*
1569 * Install callback handler (called by drivers)
1570 */
1571 /* ARGSUSED */
1572 void
1573 maple_set_callback(struct device *dev, struct maple_unit *u, int func,
1574 void (*callback)(void *, struct maple_response *, int, int), void *arg)
1575 {
1576 #if 0 /* currently unused */
1577 struct maple_softc *sc = (void *) dev;
1578 #endif
1579 struct maple_func *fn;
1580
1581 KASSERT(func >= 0 && func < MAPLE_NFUNC);
1582
1583 fn = &u->u_func[func];
1584
1585 fn->f_callback = callback;
1586 fn->f_arg = arg;
1587 }
1588
1589 /*
1590 * Return function definition data (called by drivers)
1591 */
1592 uint32_t
1593 maple_get_function_data(struct maple_devinfo *devinfo, int function_code)
1594 {
1595 int i, p = 0;
1596 uint32_t func;
1597
1598 func = be32toh(devinfo->di_func);
1599 for (i = 31; i >= 0; --i)
1600 if (func & MAPLE_FUNC(i)) {
1601 if (function_code == i)
1602 return be32toh(devinfo->di_function_data[p]);
1603 else
1604 if (++p >= 3)
1605 break;
1606 }
1607
1608 return 0;
1609 }
1610
1611 /* Generic maple device interface */
1612
1613 int
1614 mapleopen(dev_t dev, int flag, int mode, struct proc *p)
1615 {
1616 struct maple_softc *sc;
1617
1618 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
1619 if (sc == NULL) /* make sure it was attached */
1620 return ENXIO;
1621
1622 if (MAPLEPORT(dev) >= MAPLE_PORTS)
1623 return ENXIO;
1624
1625 if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS)
1626 return ENXIO;
1627
1628 if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev))))
1629 return ENXIO;
1630
1631 sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev);
1632
1633 return 0;
1634 }
1635
1636 int
1637 mapleclose(dev_t dev, int flag, int mode, struct proc *p)
1638 {
1639 struct maple_softc *sc;
1640
1641 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
1642
1643 sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev));
1644
1645 return 0;
1646 }
1647
1648 int
1649 maple_unit_ioctl(struct device *dev, struct maple_unit *u, u_long cmd,
1650 caddr_t data, int flag, struct proc *p)
1651 {
1652 struct maple_softc *sc = (struct maple_softc *)dev;
1653
1654 if (!(sc->sc_port_units[u->port] & (1 << u->subunit)))
1655 return ENXIO;
1656
1657 switch(cmd) {
1658 case MAPLEIO_GDEVINFO:
1659 memcpy(data, &u->devinfo, sizeof(struct maple_devinfo));
1660 break;
1661 default:
1662 return EPASSTHROUGH;
1663 }
1664
1665 return 0;
1666 }
1667
1668 int
1669 mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1670 {
1671 struct maple_softc *sc;
1672 struct maple_unit *u;
1673
1674 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev));
1675 u = &sc->sc_unit[MAPLEPORT(dev)][MAPLESUBUNIT(dev)];
1676
1677 return maple_unit_ioctl(&sc->sc_dev, u, cmd, data, flag, p);
1678 }
1679