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