1 /* $OpenBSD: ebus.c,v 1.22 2008/06/11 05:15:43 kettenis Exp $ */ 2 /* $NetBSD: ebus.c,v 1.24 2001/07/25 03:49:54 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1999, 2000 Matthew R. Green 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * UltraSPARC 5 and beyond ebus support. 34 * 35 * note that this driver is not complete: 36 * - ebus2 dma code is completely unwritten 37 * - interrupt establish is written and appears to work 38 * - bus map code is written and appears to work 39 */ 40 41 #ifdef DEBUG 42 #define EDB_PROM 0x01 43 #define EDB_CHILD 0x02 44 #define EDB_INTRMAP 0x04 45 #define EDB_BUSMAP 0x08 46 #define EDB_BUSDMA 0x10 47 #define EDB_INTR 0x20 48 int ebus_debug = 0x0; 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 <sparc64/dev/ebusreg.h> 77 #include <sparc64/dev/ebusvar.h> 78 #include <sparc64/sparc64/cache.h> 79 80 int ebus_match(struct device *, void *, void *); 81 void ebus_attach(struct device *, struct device *, void *); 82 83 struct cfattach ebus_ca = { 84 sizeof(struct ebus_softc), ebus_match, ebus_attach 85 }; 86 87 struct cfdriver ebus_cd = { 88 NULL, "ebus", DV_DULL 89 }; 90 91 92 void ebus_find_ino(struct ebus_softc *, struct ebus_attach_args *); 93 int ebus_find_node(struct pci_attach_args *); 94 95 /* 96 * here are our bus space and bus dma routines. 97 */ 98 static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_space_tag_t, bus_addr_t, 99 off_t, int, int); 100 static int _ebus_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t, 101 bus_size_t, int, bus_space_handle_t *); 102 bus_space_tag_t ebus_alloc_mem_tag(struct ebus_softc *, bus_space_tag_t); 103 bus_space_tag_t ebus_alloc_io_tag(struct ebus_softc *, bus_space_tag_t); 104 bus_space_tag_t _ebus_alloc_bus_tag(struct ebus_softc *sc, const char *, 105 bus_space_tag_t, int); 106 107 108 int 109 ebus_match(struct device *parent, void *match, void *aux) 110 { 111 struct pci_attach_args *pa = aux; 112 char name[10]; 113 int node; 114 115 /* Only attach if there's a PROM node. */ 116 node = PCITAG_NODE(pa->pa_tag); 117 if (node == -1) return (0); 118 119 /* Match a real ebus */ 120 OF_getprop(node, "name", &name, sizeof(name)); 121 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 122 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 123 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS && 124 strcmp(name, "ebus") == 0) 125 return (1); 126 127 /* Or a real RIO ebus */ 128 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 129 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 130 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_RIO_EBUS && 131 strcmp(name, "ebus") == 0) 132 return (1); 133 134 /* Or a fake ebus */ 135 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 136 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTERA && 137 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTERA_EBUS && 138 strcmp(name, "ebus") == 0) 139 return (1); 140 141 /* Or a PCI-ISA bridge XXX I hope this is on-board. */ 142 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE && 143 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) { 144 return (1); 145 } 146 147 return (0); 148 } 149 150 /* 151 * attach an ebus and all its children. this code is modeled 152 * after the sbus code which does similar things. 153 */ 154 void 155 ebus_attach(struct device *parent, struct device *self, void *aux) 156 { 157 struct ebus_softc *sc = (struct ebus_softc *)self; 158 struct pci_attach_args *pa = aux; 159 struct ebus_attach_args eba; 160 struct ebus_interrupt_map_mask *immp; 161 int node, nmapmask, error; 162 163 printf("\n"); 164 165 sc->sc_memtag = ebus_alloc_mem_tag(sc, pa->pa_memt); 166 sc->sc_iotag = ebus_alloc_io_tag(sc, pa->pa_iot); 167 sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat); 168 169 node = PCITAG_NODE(pa->pa_tag); 170 if (node == -1) 171 panic("could not find ebus node"); 172 173 sc->sc_node = node; 174 175 /* 176 * fill in our softc with information from the prom 177 */ 178 sc->sc_intmap = NULL; 179 sc->sc_range = NULL; 180 error = getprop(node, "interrupt-map", 181 sizeof(struct ebus_interrupt_map), 182 &sc->sc_nintmap, (void **)&sc->sc_intmap); 183 switch (error) { 184 case 0: 185 immp = &sc->sc_intmapmask; 186 error = getprop(node, "interrupt-map-mask", 187 sizeof(struct ebus_interrupt_map_mask), &nmapmask, 188 (void **)&immp); 189 if (error) 190 panic("could not get ebus interrupt-map-mask"); 191 if (nmapmask != 1) 192 panic("ebus interrupt-map-mask is broken"); 193 break; 194 case ENOENT: 195 break; 196 default: 197 panic("ebus interrupt-map: error %d", error); 198 break; 199 } 200 201 error = getprop(node, "ranges", sizeof(struct ebus_ranges), 202 &sc->sc_nrange, &sc->sc_range); 203 if (error) 204 panic("ebus ranges: error %d", error); 205 206 /* 207 * now attach all our children 208 */ 209 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node)); 210 for (node = firstchild(node); node; node = nextsibling(node)) { 211 if (!checkstatus(node)) 212 continue; 213 214 if (ebus_setup_attach_args(sc, node, &eba) != 0) { 215 DPRINTF(EDB_CHILD, 216 ("ebus_attach: %s: incomplete\n", 217 getpropstring(node, "name"))); 218 continue; 219 } else { 220 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", 221 eba.ea_name)); 222 (void)config_found(self, &eba, ebus_print); 223 } 224 ebus_destroy_attach_args(&eba); 225 } 226 } 227 228 int 229 ebus_setup_attach_args(struct ebus_softc *sc, int node, 230 struct ebus_attach_args *ea) 231 { 232 int n, rv; 233 234 bzero(ea, sizeof(struct ebus_attach_args)); 235 rv = getprop(node, "name", 1, &n, (void **)&ea->ea_name); 236 if (rv != 0) 237 return (rv); 238 ea->ea_name[n] = '\0'; 239 240 ea->ea_node = node; 241 ea->ea_memtag = sc->sc_memtag; 242 ea->ea_iotag = sc->sc_iotag; 243 ea->ea_dmatag = sc->sc_dmatag; 244 245 rv = getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nregs, 246 (void **)&ea->ea_regs); 247 if (rv) 248 return (rv); 249 250 rv = getprop(node, "address", sizeof(u_int32_t), &ea->ea_nvaddrs, 251 (void **)&ea->ea_vaddrs); 252 if (rv != ENOENT) { 253 if (rv) 254 return (rv); 255 256 if (ea->ea_nregs != ea->ea_nvaddrs) 257 printf("ebus loses: device %s: %d regs and %d addrs\n", 258 ea->ea_name, ea->ea_nregs, ea->ea_nvaddrs); 259 } else 260 ea->ea_nvaddrs = 0; 261 262 if (getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintrs, 263 (void **)&ea->ea_intrs)) 264 ea->ea_nintrs = 0; 265 else 266 ebus_find_ino(sc, ea); 267 268 return (0); 269 } 270 271 void 272 ebus_destroy_attach_args(struct ebus_attach_args *ea) 273 { 274 if (ea->ea_name) 275 free((void *)ea->ea_name, M_DEVBUF); 276 if (ea->ea_regs) 277 free((void *)ea->ea_regs, M_DEVBUF); 278 if (ea->ea_intrs) 279 free((void *)ea->ea_intrs, M_DEVBUF); 280 if (ea->ea_vaddrs) 281 free((void *)ea->ea_vaddrs, M_DEVBUF); 282 } 283 284 int 285 ebus_print(void *aux, const char *p) 286 { 287 struct ebus_attach_args *ea = aux; 288 int i; 289 290 if (p) 291 printf("\"%s\" at %s", ea->ea_name, p); 292 for (i = 0; i < ea->ea_nregs; i++) 293 printf("%s %x-%x", i == 0 ? " addr" : ",", 294 ea->ea_regs[i].lo, 295 ea->ea_regs[i].lo + ea->ea_regs[i].size - 1); 296 for (i = 0; i < ea->ea_nintrs; i++) 297 printf(" ivec 0x%x", ea->ea_intrs[i]); 298 return (UNCONF); 299 } 300 301 302 /* 303 * find the INO values for each interrupt and fill them in. 304 * 305 * for each "reg" property of this device, mask its hi and lo 306 * values with the "interrupt-map-mask"'s hi/lo values, and also 307 * mask the interrupt number with the interrupt mask. search the 308 * "interrupt-map" list for matching values of hi, lo and interrupt 309 * to give the INO for this interrupt. 310 */ 311 void 312 ebus_find_ino(struct ebus_softc *sc, struct ebus_attach_args *ea) 313 { 314 u_int32_t hi, lo, intr; 315 int i, j, k; 316 317 if (sc->sc_nintmap == 0) { 318 for (i = 0; i < ea->ea_nintrs; i++) { 319 OF_mapintr(ea->ea_node, &ea->ea_intrs[i], 320 sizeof(ea->ea_intrs[0]), 321 sizeof(ea->ea_intrs[0])); 322 } 323 return; 324 } 325 326 DPRINTF(EDB_INTRMAP, 327 ("ebus_find_ino: searching %d interrupts", ea->ea_nintrs)); 328 329 for (j = 0; j < ea->ea_nintrs; j++) { 330 331 intr = ea->ea_intrs[j] & sc->sc_intmapmask.intr; 332 333 DPRINTF(EDB_INTRMAP, 334 ("; intr %x masked to %x", ea->ea_intrs[j], intr)); 335 for (i = 0; i < ea->ea_nregs; i++) { 336 hi = ea->ea_regs[i].hi & sc->sc_intmapmask.hi; 337 lo = ea->ea_regs[i].lo & sc->sc_intmapmask.lo; 338 339 DPRINTF(EDB_INTRMAP, 340 ("; reg hi.lo %08x.%08x masked to %08x.%08x", 341 ea->ea_regs[i].hi, ea->ea_regs[i].lo, hi, lo)); 342 for (k = 0; k < sc->sc_nintmap; k++) { 343 DPRINTF(EDB_INTRMAP, 344 ("; checking hi.lo %08x.%08x intr %x", 345 sc->sc_intmap[k].hi, sc->sc_intmap[k].lo, 346 sc->sc_intmap[k].intr)); 347 if (hi == sc->sc_intmap[k].hi && 348 lo == sc->sc_intmap[k].lo && 349 intr == sc->sc_intmap[k].intr) { 350 ea->ea_intrs[j] = 351 sc->sc_intmap[k].cintr; 352 DPRINTF(EDB_INTRMAP, 353 ("; FOUND IT! changing to %d\n", 354 sc->sc_intmap[k].cintr)); 355 goto next_intr; 356 } 357 } 358 } 359 next_intr:; 360 } 361 } 362 363 bus_space_tag_t 364 ebus_alloc_mem_tag(struct ebus_softc *sc, bus_space_tag_t parent) 365 { 366 return (_ebus_alloc_bus_tag(sc, "mem", parent, 367 0x02)); /* 32-bit mem space (where's the #define???) */ 368 } 369 370 bus_space_tag_t 371 ebus_alloc_io_tag(struct ebus_softc *sc, bus_space_tag_t parent) 372 { 373 return (_ebus_alloc_bus_tag(sc, "io", parent, 374 0x01)); /* IO space (where's the #define???) */ 375 } 376 377 /* 378 * bus space and bus dma below here 379 */ 380 bus_space_tag_t 381 _ebus_alloc_bus_tag(struct ebus_softc *sc, const char *name, 382 bus_space_tag_t parent, int ss) 383 { 384 struct sparc_bus_space_tag *bt; 385 386 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 387 if (bt == NULL) 388 panic("could not allocate ebus bus tag"); 389 390 snprintf(bt->name, sizeof(bt->name), "%s_%s", 391 sc->sc_dev.dv_xname, name); 392 bt->cookie = sc; 393 bt->parent = parent; 394 bt->default_type = ss; 395 bt->asi = parent->asi; 396 bt->sasi = parent->sasi; 397 bt->sparc_bus_map = _ebus_bus_map; 398 bt->sparc_bus_mmap = ebus_bus_mmap; 399 400 return (bt); 401 } 402 403 bus_dma_tag_t 404 ebus_alloc_dma_tag(struct ebus_softc *sc, bus_dma_tag_t pdt) 405 { 406 bus_dma_tag_t dt; 407 408 dt = malloc(sizeof(*dt), M_DEVBUF, M_NOWAIT | M_ZERO); 409 if (dt == NULL) 410 panic("could not allocate ebus dma tag"); 411 412 dt->_cookie = sc; 413 dt->_parent = pdt; 414 sc->sc_dmatag = dt; 415 return (dt); 416 } 417 418 /* 419 * bus space support. <sparc64/dev/psychoreg.h> has a discussion 420 * about PCI physical addresses, which also applies to ebus. 421 */ 422 static int 423 _ebus_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t offset, 424 bus_size_t size, int flags, bus_space_handle_t *hp) 425 { 426 struct ebus_softc *sc = t->cookie; 427 struct ebus_ranges *range = sc->sc_range; 428 bus_addr_t hi, lo; 429 int i; 430 431 DPRINTF(EDB_BUSMAP, 432 ("\n_ebus_bus_map: type %d off %016llx sz %x flags %d", 433 (int)t->default_type, (unsigned long long)offset, (int)size, 434 (int)flags)); 435 436 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 437 printf("\n_ebus_bus_map: invalid parent"); 438 return (EINVAL); 439 } 440 441 t = t->parent; 442 443 if (flags & BUS_SPACE_MAP_PROMADDRESS) { 444 return ((*t->sparc_bus_map) 445 (t, t0, offset, size, flags, hp)); 446 } 447 448 hi = offset >> 32UL; 449 lo = offset & 0xffffffff; 450 451 DPRINTF(EDB_BUSMAP, (" (hi %08x lo %08x)", (u_int)hi, (u_int)lo)); 452 for (i = 0; i < sc->sc_nrange; i++) { 453 bus_addr_t pciaddr; 454 455 if (hi != range[i].child_hi) 456 continue; 457 if (lo < range[i].child_lo || 458 (lo + size) > (range[i].child_lo + range[i].size)) 459 continue; 460 461 if(((range[i].phys_hi >> 24) & 3) != t->default_type) 462 continue; 463 464 pciaddr = ((bus_addr_t)range[i].phys_mid << 32UL) | 465 range[i].phys_lo; 466 pciaddr += lo; 467 DPRINTF(EDB_BUSMAP, 468 ("\n_ebus_bus_map: mapping space %x paddr offset %qx " 469 "pciaddr %qx\n", (int)t->default_type, 470 (unsigned long long)offset, (unsigned long long)pciaddr)); 471 return ((*t->sparc_bus_map)(t, t0, pciaddr, size, flags, hp)); 472 } 473 DPRINTF(EDB_BUSMAP, (": FAILED\n")); 474 return (EINVAL); 475 } 476 477 static paddr_t 478 ebus_bus_mmap(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t paddr, 479 off_t off, int prot, int flags) 480 { 481 bus_addr_t offset = paddr; 482 struct ebus_softc *sc = t->cookie; 483 struct ebus_ranges *range = sc->sc_range; 484 int i; 485 486 if (t->parent == 0 || t->parent->sparc_bus_mmap == 0) { 487 printf("\nebus_bus_mmap: invalid parent"); 488 return (-1); 489 } 490 491 t = t->parent; 492 493 for (i = 0; i < sc->sc_nrange; i++) { 494 bus_addr_t paddr = ((bus_addr_t)range[i].child_hi << 32) | 495 range[i].child_lo; 496 497 if (offset != paddr) 498 continue; 499 500 DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n", 501 (unsigned long long)paddr)); 502 return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags)); 503 } 504 505 return (-1); 506 } 507