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