xref: /netbsd-src/sys/arch/dreamcast/dev/maple/maple.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: maple.c,v 1.10 2001/12/04 15:27:35 atatat 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 		    memcpy((to_swap = &sc->sc_unit[p][s].devinfo.di_func),
284 			   sc->sc_rxbuf[p][s]+1, sizeof(struct maple_devinfo));
285 
286 		    for (i = 0; i < 4; i++, to_swap++)
287 		      *to_swap = ntohl(*to_swap);
288 
289 		    maple_attach_dev(sc, p, s);
290 
291 		  } else {
292 
293 		    printf("%s: no response from port %d subunit %d\n",
294 			   sc->sc_dev.dv_xname, p, s);
295 
296 		    sc->sc_port_units[p] &= ~(1<<s);
297 
298 		  }
299 		}
300 
301 	  }
302 
303 	}
304 
305 	maple_polling = 0;
306 
307 }
308 
309 static void
310 maple_send_commands(sc)
311 	struct maple_softc *sc;
312 {
313 	int p, s;
314 
315 	if (sc->maple_commands_pending || MAPLE_STATE != 0)
316 	  return;
317 
318 	maple_begin_txbuf(sc);
319 
320 	for (p = 0; p < MAPLE_PORTS; p++) {
321 
322 	  for (s = 0; s < MAPLE_SUBUNITS; s++) {
323 
324 	    if (sc->sc_unit[p][s].getcond_callback != NULL) {
325 
326 	      u_int32_t func = ntohl(sc->sc_unit[p][s].getcond_func);
327 
328 	      maple_write_command(sc, p, s, MAPLE_COMMAND_GETCOND, 1, &func);
329 
330 	    }
331 
332 	  }
333 
334 	}
335 
336 	if (!maple_end_txbuf(sc))
337 	  return;
338 
339 	MAPLE_DMAADDR = sc->sc_txbuf_phys;
340 	MAPLE_STATE = 1;
341 
342 	sc->maple_commands_pending = 1;
343 }
344 
345 static void
346 maple_check_responses(sc)
347 	struct maple_softc *sc;
348 {
349 	int p, s;
350 
351 	if (!sc->maple_commands_pending || MAPLE_STATE != 0)
352 	  return;
353 
354 	for (p = 0; p < MAPLE_PORTS; p++) {
355 	  for (s = 0; s < MAPLE_SUBUNITS; s++) {
356 	    struct maple_unit * u = &sc->sc_unit[p][s];
357 	    if (u->getcond_callback != NULL &&
358 		(sc->sc_rxbuf[p][s][0] & 0xff) == MAPLE_RESPONSE_DATATRF &&
359 		(sc->sc_rxbuf[p][s][0]>>24) >= 1 &&
360 		htonl(sc->sc_rxbuf[p][s][1]) == u->getcond_func) {
361 	      (*u->getcond_callback)(u->getcond_data,
362 				     (void *)(sc->sc_rxbuf[p][s]+2),
363 				     ((sc->sc_rxbuf[p][s][0]>>22)&1020)-4);
364 	    }
365 	  }
366 	}
367 
368 	sc->maple_commands_pending = 0;
369 }
370 
371 void
372 maple_run_polling(dev)
373 	struct device *dev;
374 {
375 	struct maple_softc *sc;
376 
377 	sc = (struct maple_softc *)dev;
378 
379 	if (MAPLE_STATE != 0)
380 	  return;
381 
382 	maple_send_commands(sc);
383 
384 	while (MAPLE_STATE != 0)
385 	  ;
386 
387 	maple_check_responses(sc);
388 }
389 
390 void
391 maple_set_condition_callback(dev, port, subunit, func, callback, data)
392 	struct device *dev;
393 	int port;
394 	int subunit;
395 	u_int32_t func;
396 	void (*callback)(void *, void *, int);
397 	void *data;
398 {
399 	struct maple_softc *sc;
400 
401 	sc = (struct maple_softc *)dev;
402 
403 	if ((port & ~(MAPLE_PORTS-1)) != 0 ||
404 	    subunit < 0 || subunit >= MAPLE_SUBUNITS)
405 	  return;
406 
407 	sc->sc_unit[port][subunit].getcond_func = func;
408 	sc->sc_unit[port][subunit].getcond_callback = callback;
409 	sc->sc_unit[port][subunit].getcond_data = data;
410 }
411 
412 static void
413 maple_callout(ctx)
414 	void *ctx;
415 {
416 	struct maple_softc *sc = ctx;
417 
418 	if(!maple_polling) {
419 
420 	  maple_check_responses(sc);
421 
422 	  maple_send_commands(sc);
423 
424 	}
425 
426 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
427 		      (void *)maple_callout, sc);
428 }
429 
430 int
431 maple_alloc_dma(size, vap, pap)
432 	size_t size;
433 	vaddr_t *vap;
434 	paddr_t *pap;
435 {
436 	extern paddr_t avail_start, avail_end;	/* from pmap.c */
437 
438 	struct pglist mlist;
439 	struct vm_page *m;
440 	int error;
441 
442 	size = round_page(size);
443 
444 	TAILQ_INIT(&mlist);
445 	error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
446 	    0, 0, &mlist, 1, 0);
447 	if (error)
448 		return (error);
449 
450 	m = TAILQ_FIRST(&mlist);
451 	*pap = VM_PAGE_TO_PHYS(m);
452 	*vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m));
453 
454 	return (0);
455 }
456 
457 void
458 maple_free_dma(paddr, size)
459 	paddr_t paddr;
460 	size_t size;
461 {
462 	struct pglist mlist;
463 	struct vm_page *m;
464 	bus_addr_t addr;
465 
466 	TAILQ_INIT(&mlist);
467 	for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) {
468 		m = PHYS_TO_VM_PAGE(addr);
469 		TAILQ_INSERT_TAIL(&mlist, m, pageq);
470 	}
471 	uvm_pglistfree(&mlist);
472 }
473 
474 static void
475 mapleattach(parent, self, aux)
476 	struct device *parent, *self;
477 	void *aux;
478 {
479 	struct maple_softc *sc;
480 	vaddr_t dmabuffer;
481 	paddr_t dmabuffer_phys;
482 	u_int32_t *p;
483 	int i, j;
484 
485 	sc = (struct maple_softc *)self;
486 
487 	printf("\n");
488 
489 	if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) {
490 	  printf("%s: unable to allocate DMA buffers.\n", sc->sc_dev.dv_xname);
491 	  return;
492 	}
493 
494 	p = (u_int32_t *) dmabuffer;
495 
496 	for (i = 0; i < MAPLE_PORTS; i++)
497 	  for (j = 0; j < MAPLE_SUBUNITS; j++) {
498 
499 	    sc->sc_rxbuf[i][j] = p;
500 	    sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p);
501 	    p += 256;
502 
503 	  }
504 
505 	sc->sc_txbuf = p;
506 	sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p);
507 
508 	sc->maple_commands_pending = 0;
509 
510 	MAPLE_RESET = RESET_MAGIC;
511 	MAPLE_RESET2 = 0;
512 
513 	MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000);
514 
515 	MAPLE_ENABLE = 1;
516 
517 	maple_scanbus(sc);
518 
519 	memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch));
520 
521 	callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS,
522 		      (void *)maple_callout, sc);
523 }
524 
525 int
526 mapleprint(aux, pnp)
527 	void *aux;
528 	const char *pnp;
529 {
530 	struct maple_attach_args *ma = aux;
531 
532 	if (pnp != NULL) {
533 		printf("maple%c", 'A'+ma->ma_port);
534 		if (ma->ma_subunit != 0)
535 			printf("%d", ma->ma_subunit);
536 		printf(" at %s", pnp);
537 	}
538 
539 	printf(" port %d", ma->ma_port);
540 
541 	if (ma->ma_subunit != 0)
542 		printf(" subunit %d", ma->ma_subunit);
543 
544 	printf(": %.*s",
545 	       (int)sizeof(ma->ma_devinfo->di_product_name),
546 	       ma->ma_devinfo->di_product_name);
547 
548 	return (0);
549 }
550 
551 static int
552 maplesubmatch(parent, match, aux)
553 	struct device *parent;
554 	struct cfdata *match;
555 	void *aux;
556 {
557 	struct maple_attach_args *ma = aux;
558 
559 	if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT &&
560 	    match->cf_loc[MAPLECF_PORT] != ma->ma_port)
561 		return (0);
562 
563 	if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT &&
564 	    match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit)
565 		return (0);
566 
567 	return ((*match->cf_attach->ca_match)(parent, match, aux));
568 }
569 
570 u_int32_t
571 maple_get_function_data(devinfo, function_code)
572 	struct maple_devinfo *devinfo;
573 	u_int32_t function_code;
574 {
575 	int i, p = 0;
576 
577 	for (i = 31; i >= 0; --i)
578 	  if (devinfo->di_func & (1U<<i)) {
579 	    if (function_code & (1U<<i))
580 	      return devinfo->di_function_data[p];
581 	    else
582 	      if (++p >= 3)
583 		break;
584 	  }
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 		memcpy(data, &u->devinfo, 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