1 /* $NetBSD: maple.c,v 1.13 2002/06/02 14:44:44 drochner 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 error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE, 419 0, 0, &mlist, 1, 0); 420 if (error) 421 return (error); 422 423 m = TAILQ_FIRST(&mlist); 424 *pap = VM_PAGE_TO_PHYS(m); 425 *vap = SH3_PHYS_TO_P2SEG(VM_PAGE_TO_PHYS(m)); 426 427 return (0); 428 } 429 430 void 431 maple_free_dma(paddr_t paddr, size_t size) 432 { 433 struct pglist mlist; 434 struct vm_page *m; 435 bus_addr_t addr; 436 437 TAILQ_INIT(&mlist); 438 for (addr = paddr; addr < paddr + size; addr += PAGE_SIZE) { 439 m = PHYS_TO_VM_PAGE(addr); 440 TAILQ_INSERT_TAIL(&mlist, m, pageq); 441 } 442 uvm_pglistfree(&mlist); 443 } 444 445 static void 446 mapleattach(struct device *parent, struct device *self, void *aux) 447 { 448 struct maple_softc *sc; 449 vaddr_t dmabuffer; 450 paddr_t dmabuffer_phys; 451 u_int32_t *p; 452 int i, j; 453 454 sc = (struct maple_softc *)self; 455 456 printf("\n"); 457 458 if (maple_alloc_dma(MAPLE_DMABUF_SIZE, &dmabuffer, &dmabuffer_phys)) { 459 printf("%s: unable to allocate DMA buffers.\n", 460 sc->sc_dev.dv_xname); 461 return; 462 } 463 464 p = (u_int32_t *)dmabuffer; 465 466 for (i = 0; i < MAPLE_PORTS; i++) 467 for (j = 0; j < MAPLE_SUBUNITS; j++) { 468 sc->sc_rxbuf[i][j] = p; 469 sc->sc_rxbuf_phys[i][j] = SH3_P2SEG_TO_PHYS(p); 470 p += 256; 471 } 472 473 sc->sc_txbuf = p; 474 sc->sc_txbuf_phys = SH3_P2SEG_TO_PHYS(p); 475 476 sc->maple_commands_pending = 0; 477 478 MAPLE_RESET = RESET_MAGIC; 479 MAPLE_RESET2 = 0; 480 481 MAPLE_SPEED = SPEED_2MBPS | TIMEOUT(50000); 482 483 MAPLE_ENABLE = 1; 484 485 maple_scanbus(sc); 486 487 memset(&sc->maple_callout_ch, 0, sizeof(sc->maple_callout_ch)); 488 489 callout_reset(&sc->maple_callout_ch, MAPLE_CALLOUT_TICKS, 490 (void *)maple_callout, sc); 491 } 492 493 int 494 mapleprint(void *aux, const char *pnp) 495 { 496 struct maple_attach_args *ma = aux; 497 498 if (pnp != NULL) { 499 printf("maple%c", 'A'+ma->ma_port); 500 if (ma->ma_subunit != 0) 501 printf("%d", ma->ma_subunit); 502 printf(" at %s", pnp); 503 } 504 505 printf(" port %d", ma->ma_port); 506 507 if (ma->ma_subunit != 0) 508 printf(" subunit %d", ma->ma_subunit); 509 510 printf(": %.*s", 511 (int)sizeof(ma->ma_devinfo->di_product_name), 512 ma->ma_devinfo->di_product_name); 513 514 return (0); 515 } 516 517 static int 518 maplesubmatch(struct device *parent, struct cfdata *match, void *aux) 519 { 520 struct maple_attach_args *ma = aux; 521 522 if (match->cf_loc[MAPLECF_PORT] != MAPLECF_PORT_DEFAULT && 523 match->cf_loc[MAPLECF_PORT] != ma->ma_port) 524 return (0); 525 526 if (match->cf_loc[MAPLECF_SUBUNIT] != MAPLECF_SUBUNIT_DEFAULT && 527 match->cf_loc[MAPLECF_SUBUNIT] != ma->ma_subunit) 528 return (0); 529 530 return ((*match->cf_attach->ca_match)(parent, match, aux)); 531 } 532 533 u_int32_t 534 maple_get_function_data(struct maple_devinfo *devinfo, u_int32_t function_code) 535 { 536 int i, p = 0; 537 538 for (i = 31; i >= 0; --i) 539 if (devinfo->di_func & (1U << i)) { 540 if (function_code & (1U << i)) 541 return devinfo->di_function_data[p]; 542 else 543 if (++p >= 3) 544 break; 545 } 546 547 return (0); 548 } 549 550 /* Generic maple device interface */ 551 552 int 553 mapleopen(dev_t dev, int flag, int mode, struct proc *p) 554 { 555 struct maple_softc *sc; 556 557 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev)); 558 if (sc == NULL) /* make sure it was attached */ 559 return (ENXIO); 560 561 if (MAPLEPORT(dev) >= MAPLE_PORTS) 562 return (ENXIO); 563 564 if (MAPLESUBUNIT(dev) >= MAPLE_SUBUNITS) 565 return (ENXIO); 566 567 if (!(sc->sc_port_units[MAPLEPORT(dev)] & (1 << MAPLESUBUNIT(dev)))) 568 return (ENXIO); 569 570 sc->sc_port_units_open[MAPLEPORT(dev)] |= 1 << MAPLESUBUNIT(dev); 571 572 return (0); 573 } 574 575 int 576 mapleclose(dev_t dev, int flag, int mode, struct proc *p) 577 { 578 struct maple_softc *sc; 579 580 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev)); 581 582 sc->sc_port_units_open[MAPLEPORT(dev)] &= ~(1 << MAPLESUBUNIT(dev)); 583 584 return (0); 585 } 586 587 int 588 maple_internal_ioctl(struct maple_softc *sc, int port, int subunit, u_long cmd, 589 caddr_t data, int flag, struct proc *p) 590 { 591 struct maple_unit *u = &sc->sc_unit[port][subunit]; 592 593 if (!(sc->sc_port_units[port] & (1 << subunit))) 594 return (ENXIO); 595 596 switch(cmd) { 597 case MAPLEIO_GDEVINFO: 598 memcpy(data, &u->devinfo, sizeof(struct maple_devinfo)); 599 return (0); 600 default: 601 return (EINVAL); 602 } 603 } 604 605 int 606 mapleioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 607 { 608 struct maple_softc *sc; 609 610 sc = device_lookup(&maple_cd, MAPLEBUSUNIT(dev)); 611 612 return (maple_internal_ioctl(sc, MAPLEPORT(dev), MAPLESUBUNIT(dev), 613 cmd, data, flag, p)); 614 } 615