1 /* $NetBSD: ebus.c,v 1.34 2002/10/02 16:02:18 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000, 2001 Matthew R. Green 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. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include "opt_ddb.h" 32 33 /* 34 * UltraSPARC 5 and beyond ebus support. 35 * 36 * note that this driver is not complete: 37 * - interrupt establish is written and appears to work 38 * - bus map code is written and appears to work 39 * - ebus2 dma code is completely unwritten, we just punt to 40 * the iommu. 41 */ 42 43 #ifdef DEBUG 44 #define EDB_PROM 0x01 45 #define EDB_CHILD 0x02 46 #define EDB_INTRMAP 0x04 47 #define EDB_BUSMAP 0x08 48 int ebus_debug = 0; 49 #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0) 50 #else 51 #define DPRINTF(l, s) 52 #endif 53 54 #include <sys/param.h> 55 #include <sys/conf.h> 56 #include <sys/device.h> 57 #include <sys/errno.h> 58 #include <sys/extent.h> 59 #include <sys/malloc.h> 60 #include <sys/systm.h> 61 #include <sys/time.h> 62 63 #define _SPARC_BUS_DMA_PRIVATE 64 #include <machine/bus.h> 65 #include <machine/autoconf.h> 66 #include <machine/openfirm.h> 67 68 #include <dev/pci/pcivar.h> 69 #include <dev/pci/pcireg.h> 70 #include <dev/pci/pcidevs.h> 71 72 #include <sparc64/dev/iommureg.h> 73 #include <sparc64/dev/iommuvar.h> 74 #include <sparc64/dev/psychoreg.h> 75 #include <sparc64/dev/psychovar.h> 76 #include <dev/ebus/ebusreg.h> 77 #include <dev/ebus/ebusvar.h> 78 #include <sparc64/sparc64/cache.h> 79 80 struct ebus_softc { 81 struct device sc_dev; 82 83 int sc_node; 84 85 bus_space_tag_t sc_memtag; /* from pci */ 86 bus_space_tag_t sc_iotag; /* from pci */ 87 bus_space_tag_t sc_childbustag; /* pass to children */ 88 bus_dma_tag_t sc_dmatag; 89 90 struct ebus_ranges *sc_range; 91 struct ebus_interrupt_map *sc_intmap; 92 struct ebus_interrupt_map_mask sc_intmapmask; 93 94 int sc_nrange; /* counters */ 95 int sc_nintmap; 96 }; 97 98 int ebus_match __P((struct device *, struct cfdata *, void *)); 99 void ebus_attach __P((struct device *, struct device *, void *)); 100 101 CFATTACH_DECL(ebus, sizeof(struct ebus_softc), 102 ebus_match, ebus_attach, NULL, NULL); 103 104 bus_space_tag_t ebus_alloc_bus_tag __P((struct ebus_softc *, int)); 105 106 int ebus_setup_attach_args __P((struct ebus_softc *, int, 107 struct ebus_attach_args *)); 108 void ebus_destroy_attach_args __P((struct ebus_attach_args *)); 109 int ebus_print __P((void *, const char *)); 110 void ebus_find_ino __P((struct ebus_softc *, struct ebus_attach_args *)); 111 int ebus_find_node __P((struct pci_attach_args *)); 112 113 /* 114 * here are our bus space and bus dma routines. 115 */ 116 static paddr_t ebus_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, int, int)); 117 static int _ebus_bus_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int, 118 vaddr_t, bus_space_handle_t *)); 119 static void *ebus_intr_establish __P((bus_space_tag_t, int, int, int, 120 int (*) __P((void *)), void *)); 121 122 int 123 ebus_match(parent, match, aux) 124 struct device *parent; 125 struct cfdata *match; 126 void *aux; 127 { 128 struct pci_attach_args *pa = aux; 129 char name[10]; 130 int node; 131 132 /* Only attach if there's a PROM node. */ 133 node = PCITAG_NODE(pa->pa_tag); 134 if (node == -1) return (0); 135 136 /* Match a real ebus */ 137 OF_getprop(node, "name", &name, sizeof(name)); 138 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 139 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 140 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS && 141 strcmp(name, "ebus") == 0) 142 return (1); 143 144 /* Or a real ebus III */ 145 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 146 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 147 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII && 148 strcmp(name, "ebus") == 0) 149 return (1); 150 151 /* Or a PCI-ISA bridge XXX I hope this is on-board. */ 152 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 153 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) { 154 return (1); 155 } 156 157 return (0); 158 } 159 160 /* 161 * attach an ebus and all it's children. this code is modeled 162 * after the sbus code which does similar things. 163 */ 164 void 165 ebus_attach(parent, self, aux) 166 struct device *parent, *self; 167 void *aux; 168 { 169 struct ebus_softc *sc = (struct ebus_softc *)self; 170 struct pci_attach_args *pa = aux; 171 struct ebus_attach_args eba; 172 struct ebus_interrupt_map_mask *immp; 173 int node, nmapmask, error; 174 char devinfo[256]; 175 176 printf("\n"); 177 178 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo); 179 printf("%s: %s, revision 0x%02x\n", self->dv_xname, devinfo, 180 PCI_REVISION(pa->pa_class)); 181 182 sc->sc_memtag = pa->pa_memt; 183 sc->sc_iotag = pa->pa_iot; 184 sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE); 185 sc->sc_dmatag = pa->pa_dmat; 186 187 node = PCITAG_NODE(pa->pa_tag); 188 if (node == -1) 189 panic("could not find ebus node"); 190 191 sc->sc_node = node; 192 193 /* 194 * fill in our softc with information from the prom 195 */ 196 sc->sc_intmap = NULL; 197 sc->sc_range = NULL; 198 error = PROM_getprop(node, "interrupt-map", 199 sizeof(struct ebus_interrupt_map), 200 &sc->sc_nintmap, (void **)&sc->sc_intmap); 201 switch (error) { 202 case 0: 203 immp = &sc->sc_intmapmask; 204 error = PROM_getprop(node, "interrupt-map-mask", 205 sizeof(struct ebus_interrupt_map_mask), &nmapmask, 206 (void **)&immp); 207 if (error) 208 panic("could not get ebus interrupt-map-mask"); 209 if (nmapmask != 1) 210 panic("ebus interrupt-map-mask is broken"); 211 break; 212 case ENOENT: 213 break; 214 default: 215 panic("ebus interrupt-map: error %d", error); 216 break; 217 } 218 219 error = PROM_getprop(node, "ranges", sizeof(struct ebus_ranges), 220 &sc->sc_nrange, (void **)&sc->sc_range); 221 if (error) 222 panic("ebus ranges: error %d", error); 223 224 /* 225 * now attach all our children 226 */ 227 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node)); 228 for (node = firstchild(node); node; node = nextsibling(node)) { 229 char *name = PROM_getpropstring(node, "name"); 230 231 if (ebus_setup_attach_args(sc, node, &eba) != 0) { 232 printf("ebus_attach: %s: incomplete\n", name); 233 continue; 234 } else { 235 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", 236 eba.ea_name)); 237 (void)config_found(self, &eba, ebus_print); 238 } 239 ebus_destroy_attach_args(&eba); 240 } 241 } 242 243 int 244 ebus_setup_attach_args(sc, node, ea) 245 struct ebus_softc *sc; 246 int node; 247 struct ebus_attach_args *ea; 248 { 249 int n, rv; 250 251 bzero(ea, sizeof(struct ebus_attach_args)); 252 rv = PROM_getprop(node, "name", 1, &n, (void **)&ea->ea_name); 253 if (rv != 0) 254 return (rv); 255 ea->ea_name[n] = '\0'; 256 257 ea->ea_node = node; 258 ea->ea_bustag = sc->sc_childbustag; 259 ea->ea_dmatag = sc->sc_dmatag; 260 261 rv = PROM_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg, 262 (void **)&ea->ea_reg); 263 if (rv) 264 return (rv); 265 266 rv = PROM_getprop(node, "address", sizeof(u_int32_t), &ea->ea_nvaddr, 267 (void **)&ea->ea_vaddr); 268 if (rv != ENOENT) { 269 if (rv) 270 return (rv); 271 272 if (ea->ea_nreg != ea->ea_nvaddr) 273 printf("ebus loses: device %s: %d regs and %d addrs\n", 274 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr); 275 } else 276 ea->ea_nvaddr = 0; 277 278 if (PROM_getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintr, 279 (void **)&ea->ea_intr)) 280 ea->ea_nintr = 0; 281 else 282 ebus_find_ino(sc, ea); 283 284 return (0); 285 } 286 287 void 288 ebus_destroy_attach_args(ea) 289 struct ebus_attach_args *ea; 290 { 291 292 if (ea->ea_name) 293 free((void *)ea->ea_name, M_DEVBUF); 294 if (ea->ea_reg) 295 free((void *)ea->ea_reg, M_DEVBUF); 296 if (ea->ea_intr) 297 free((void *)ea->ea_intr, M_DEVBUF); 298 if (ea->ea_vaddr) 299 free((void *)ea->ea_vaddr, M_DEVBUF); 300 } 301 302 int 303 ebus_print(aux, p) 304 void *aux; 305 const char *p; 306 { 307 struct ebus_attach_args *ea = aux; 308 int i; 309 310 if (p) 311 printf("%s at %s", ea->ea_name, p); 312 for (i = 0; i < ea->ea_nreg; i++) 313 printf("%s %x-%x", i == 0 ? " addr" : ",", 314 ea->ea_reg[i].lo, 315 ea->ea_reg[i].lo + ea->ea_reg[i].size - 1); 316 for (i = 0; i < ea->ea_nintr; i++) 317 printf(" ipl %d", ea->ea_intr[i]); 318 return (UNCONF); 319 } 320 321 322 /* 323 * find the INO values for each interrupt and fill them in. 324 * 325 * for each "reg" property of this device, mask it's hi and lo 326 * values with the "interrupt-map-mask"'s hi/lo values, and also 327 * mask the interrupt number with the interrupt mask. search the 328 * "interrupt-map" list for matching values of hi, lo and interrupt 329 * to give the INO for this interrupt. 330 */ 331 void 332 ebus_find_ino(sc, ea) 333 struct ebus_softc *sc; 334 struct ebus_attach_args *ea; 335 { 336 u_int32_t hi, lo, intr; 337 int i, j, k; 338 339 if (sc->sc_nintmap == 0) { 340 for (i = 0; i < ea->ea_nintr; i++) { 341 OF_mapintr(ea->ea_node, &ea->ea_intr[i], 342 sizeof(ea->ea_intr[0]), 343 sizeof(ea->ea_intr[0])); 344 } 345 return; 346 } 347 348 DPRINTF(EDB_INTRMAP, 349 ("ebus_find_ino: searching %d interrupts", ea->ea_nintr)); 350 351 for (j = 0; j < ea->ea_nintr; j++) { 352 353 intr = ea->ea_intr[j] & sc->sc_intmapmask.intr; 354 355 DPRINTF(EDB_INTRMAP, 356 ("; intr %x masked to %x", ea->ea_intr[j], intr)); 357 for (i = 0; i < ea->ea_nreg; i++) { 358 hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi; 359 lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo; 360 361 DPRINTF(EDB_INTRMAP, 362 ("; reg hi.lo %08x.%08x masked to %08x.%08x", 363 ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo)); 364 for (k = 0; k < sc->sc_nintmap; k++) { 365 DPRINTF(EDB_INTRMAP, 366 ("; checking hi.lo %08x.%08x intr %x", 367 sc->sc_intmap[k].hi, sc->sc_intmap[k].lo, 368 sc->sc_intmap[k].intr)); 369 if (hi == sc->sc_intmap[k].hi && 370 lo == sc->sc_intmap[k].lo && 371 intr == sc->sc_intmap[k].intr) { 372 ea->ea_intr[j] = 373 sc->sc_intmap[k].cintr; 374 DPRINTF(EDB_INTRMAP, 375 ("; FOUND IT! changing to %d\n", 376 sc->sc_intmap[k].cintr)); 377 goto next_intr; 378 } 379 } 380 } 381 next_intr:; 382 } 383 } 384 385 /* 386 * bus space support. <sparc64/dev/psychoreg.h> has a discussion 387 * about PCI physical addresses, which also applies to ebus. 388 */ 389 bus_space_tag_t 390 ebus_alloc_bus_tag(sc, type) 391 struct ebus_softc *sc; 392 int type; 393 { 394 bus_space_tag_t bt; 395 396 bt = (bus_space_tag_t) 397 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT); 398 if (bt == NULL) 399 panic("could not allocate ebus bus tag"); 400 401 bzero(bt, sizeof *bt); 402 bt->cookie = sc; 403 bt->parent = sc->sc_memtag; 404 bt->type = type; 405 bt->sparc_bus_map = _ebus_bus_map; 406 bt->sparc_bus_mmap = ebus_bus_mmap; 407 bt->sparc_intr_establish = ebus_intr_establish; 408 return (bt); 409 } 410 411 static int 412 _ebus_bus_map(t, ba, size, flags, va, hp) 413 bus_space_tag_t t; 414 bus_addr_t ba; 415 bus_size_t size; 416 int flags; 417 vaddr_t va; 418 bus_space_handle_t *hp; 419 { 420 struct ebus_softc *sc = t->cookie; 421 paddr_t offset; 422 u_int bar; 423 int i, ss; 424 425 bar = BUS_ADDR_IOSPACE(ba); 426 offset = BUS_ADDR_PADDR(ba); 427 428 DPRINTF(EDB_BUSMAP, 429 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n", 430 (int)bar, (u_int32_t)offset, (u_int32_t)size, 431 flags, (void *)va)); 432 433 for (i = 0; i < sc->sc_nrange; i++) { 434 bus_addr_t pciaddr; 435 436 if (bar != sc->sc_range[i].child_hi) 437 continue; 438 if (offset < sc->sc_range[i].child_lo || 439 (offset + size) > 440 (sc->sc_range[i].child_lo + sc->sc_range[i].size)) 441 continue; 442 443 /* Isolate address space and find the right tag */ 444 ss = (sc->sc_range[i].phys_hi>>24)&3; 445 switch (ss) { 446 case 1: /* I/O space */ 447 t = sc->sc_iotag; 448 break; 449 case 2: /* Memory space */ 450 t = sc->sc_memtag; 451 break; 452 case 0: /* Config space */ 453 case 3: /* 64-bit Memory space */ 454 default: /* WTF? */ 455 /* We don't handle these */ 456 panic("_ebus_bus_map: illegal space %x", ss); 457 break; 458 } 459 pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) | 460 sc->sc_range[i].phys_lo; 461 pciaddr += offset; 462 463 DPRINTF(EDB_BUSMAP, 464 ("_ebus_bus_map: mapping to PCI addr %x\n", 465 (u_int32_t)pciaddr)); 466 467 /* pass it onto the psycho */ 468 return (bus_space_map(t, pciaddr, size, flags, hp)); 469 } 470 DPRINTF(EDB_BUSMAP, (": FAILED\n")); 471 return (EINVAL); 472 } 473 474 static paddr_t 475 ebus_bus_mmap(t, paddr, off, prot, flags) 476 bus_space_tag_t t; 477 bus_addr_t paddr; 478 off_t off; 479 int prot; 480 int flags; 481 { 482 bus_addr_t offset = paddr; 483 struct ebus_softc *sc = t->cookie; 484 int i; 485 486 for (i = 0; i < sc->sc_nrange; i++) { 487 bus_addr_t paddr = ((bus_addr_t)sc->sc_range[i].child_hi << 32) | 488 sc->sc_range[i].child_lo; 489 490 if (offset != paddr) 491 continue; 492 493 DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n", 494 (unsigned long long)paddr)); 495 return (bus_space_mmap(sc->sc_memtag, paddr, off, 496 prot, flags)); 497 } 498 499 return (-1); 500 } 501 502 /* 503 * install an interrupt handler for a ebus device 504 */ 505 void * 506 ebus_intr_establish(t, pri, level, flags, handler, arg) 507 bus_space_tag_t t; 508 int pri; 509 int level; 510 int flags; 511 int (*handler) __P((void *)); 512 void *arg; 513 { 514 515 return (bus_intr_establish(t->parent, pri, level, flags, handler, arg)); 516 } 517