1 /* $NetBSD: ebus.c,v 1.18 2004/07/10 20:37:07 pk Exp $ */ 2 3 /* 4 * Copyright (c) 1999, 2000 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 /* 32 * EBus support for PCI based SPARC systems (ms-IIep, Ultra). 33 * EBus is documented in PCIO manual (Sun Part#: 802-7837-01). 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.18 2004/07/10 20:37:07 pk Exp $"); 38 39 #if defined(DEBUG) && !defined(EBUS_DEBUG) 40 #define EBUS_DEBUG 41 #endif 42 43 #ifdef EBUS_DEBUG 44 #define EDB_PROM 0x01 45 #define EDB_CHILD 0x02 46 #define EDB_INTRMAP 0x04 47 #define EDB_BUSMAP 0x08 48 #define EDB_BUSDMA 0x10 49 #define EDB_INTR 0x20 50 int ebus_debug = 0; 51 #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0) 52 #else 53 #define DPRINTF(l, s) 54 #endif 55 56 #include <sys/param.h> 57 #include <sys/conf.h> 58 #include <sys/device.h> 59 #include <sys/errno.h> 60 #include <sys/extent.h> 61 #include <sys/malloc.h> 62 #include <sys/systm.h> 63 #include <sys/time.h> 64 65 #define _SPARC_BUS_DMA_PRIVATE 66 #include <machine/bus.h> 67 #include <machine/autoconf.h> 68 69 #include <dev/pci/pcivar.h> 70 #include <dev/pci/pcireg.h> 71 #include <dev/pci/pcidevs.h> 72 73 #include <dev/ofw/ofw_pci.h> 74 75 #include <dev/ebus/ebusreg.h> 76 #include <dev/ebus/ebusvar.h> 77 78 79 struct ebus_softc { 80 struct device sc_dev; 81 struct device *sc_parent; /* PCI bus */ 82 83 int sc_node; /* PROM node */ 84 85 bus_space_tag_t sc_bustag; /* mem tag from pci */ 86 87 /* 88 * "reg" contains exactly the info we'd get by processing 89 * "ranges", so don't bother with "ranges" and use "reg" directly. 90 */ 91 struct ofw_pci_register *sc_reg; 92 int sc_nreg; 93 }; 94 95 int ebus_match(struct device *, struct cfdata *, void *); 96 void ebus_attach(struct device *, struct device *, void *); 97 98 CFATTACH_DECL(ebus, sizeof(struct ebus_softc), 99 ebus_match, ebus_attach, NULL, NULL); 100 101 int ebus_setup_attach_args(struct ebus_softc *, bus_space_tag_t, 102 bus_dma_tag_t, int, struct ebus_attach_args *); 103 void ebus_destroy_attach_args(struct ebus_attach_args *); 104 int ebus_print(void *, const char *); 105 106 /* 107 * here are our bus space and bus DMA routines. 108 */ 109 static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int); 110 static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, 111 bus_size_t, int, vaddr_t, bus_space_handle_t *); 112 static void *ebus_intr_establish(bus_space_tag_t, int, int, 113 int (*)(void *), void *, void (*)(void)); 114 115 static bus_dma_tag_t ebus_alloc_dma_tag(struct ebus_softc *, bus_dma_tag_t); 116 117 118 /* 119 * Working around PROM bogosity. 120 * 121 * EBus doesn't have official OFW binding. sparc64 has a de-facto 122 * standard but patching it in in prompatch.c and then decoding it 123 * here would be an overkill for ms-IIep. 124 * 125 * So we assume that all ms-IIep based systems use PCIO chip only in 126 * "motherboard mode" with interrupt lines wired directly to ms-IIep 127 * interrupt inputs. 128 * 129 * Note that this is ineligible for prompatch.c, as we are not 130 * correcting PROM to conform to some established standard, this hack 131 * is tied to this version of ebus driver and as such it's better stay 132 * private to the driver. 133 */ 134 135 struct msiiep_ebus_intr_wiring { 136 const char *name; /* PROM node */ 137 int line; /* ms-IIep interrupt input */ 138 }; 139 140 static struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring[] = { 141 { "su", 0 }, { "8042", 0 }, { "sound", 3 } 142 }; 143 144 145 struct msiiep_known_ebus_wiring { 146 const char *model; 147 struct msiiep_ebus_intr_wiring *map; 148 int mapsize; 149 }; 150 151 #define MSIIEP_MODEL_WIRING(name, map) \ 152 { name, map, sizeof(map)/sizeof(map[0]) } 153 154 static struct msiiep_known_ebus_wiring known_models[] = { 155 MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring), 156 { NULL, NULL, 0} 157 }; 158 159 160 /* 161 * XXX: This assumes single EBus. However I don't think any ms-IIep 162 * system ever used more than one. In any case, without looking at a 163 * system with multiple PCIO chips I don't know how to correctly 164 * program the driver to handle PROM glitches in them, so for the time 165 * being just use globals. 166 */ 167 static struct msiiep_ebus_intr_wiring *wiring_map; 168 static int wiring_map_size; 169 170 static int ebus_init_wiring_table(struct ebus_softc *); 171 172 173 int 174 ebus_match(parent, match, aux) 175 struct device *parent; 176 struct cfdata *match; 177 void *aux; 178 { 179 struct pci_attach_args *pa = aux; 180 char name[10]; 181 int node; 182 183 /* Only attach if there's a PROM node. */ 184 node = PCITAG_NODE(pa->pa_tag); 185 if (node == -1) 186 return (0); 187 188 prom_getpropstringA(node, "name", name, sizeof name); 189 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE 190 && PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN 191 && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS 192 && strcmp(name, "ebus") == 0) 193 return (1); 194 195 return (0); 196 } 197 198 199 static int 200 ebus_init_wiring_table(sc) 201 struct ebus_softc *sc; 202 { 203 struct msiiep_known_ebus_wiring *p; 204 char buf[32]; 205 char *model; 206 207 if (wiring_map != NULL) { 208 printf("%s: global ebus wiring map already initalized\n", 209 sc->sc_dev.dv_xname); 210 return (0); 211 } 212 213 model = prom_getpropstringA(prom_findroot(), "model", 214 buf, sizeof(buf)); 215 if (model == NULL) 216 panic("ebus_init_wiring_table: no \"model\" property"); 217 218 for (p = known_models; p->model != NULL; ++p) 219 if (strcmp(model, p->model) == 0) { 220 wiring_map = p->map; 221 wiring_map_size = p->mapsize; 222 return (1); 223 } 224 225 /* not found? we should have failed in pci_attach_hook then. */ 226 panic("ebus_init_wiring_table: unknown model %s", model); 227 } 228 229 230 /* 231 * attach an ebus and all it's children. this code is modeled 232 * after the sbus code which does similar things. 233 */ 234 void 235 ebus_attach(parent, self, aux) 236 struct device *parent, *self; 237 void *aux; 238 { 239 struct ebus_softc *sc = (struct ebus_softc *)self; 240 struct pci_attach_args *pa = aux; 241 struct ebus_attach_args ea; 242 bus_space_tag_t sbt; 243 bus_dma_tag_t dmatag; 244 int node, error; 245 char devinfo[256]; 246 247 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 248 printf(": %s, revision 0x%02x\n", 249 devinfo, PCI_REVISION(pa->pa_class)); 250 251 node = PCITAG_NODE(pa->pa_tag); 252 if (node == -1) 253 panic("%s: unable to find ebus node", self->dv_xname); 254 255 if (ebus_init_wiring_table(sc) == 0) 256 return; 257 258 sc->sc_node = node; 259 sc->sc_parent = parent; /* XXX: unused so far */ 260 sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */ 261 262 if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL) 263 panic("unable to allocate ebus bus tag"); 264 265 sbt->sparc_bus_map = _ebus_bus_map; 266 sbt->sparc_bus_mmap = ebus_bus_mmap; 267 sbt->sparc_intr_establish = ebus_intr_establish; 268 269 dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat); 270 271 /* 272 * Setup ranges. The interesting thing is that we use "reg" 273 * not "ranges", since "reg" on ebus has exactly the data we'd 274 * get by processing "ranges". 275 * 276 */ 277 error = prom_getprop(node, "reg", sizeof(struct ofw_pci_register), 278 &sc->sc_nreg, &sc->sc_reg); 279 if (error) 280 panic("%s: unable to read ebus registers (error %d)", 281 self->dv_xname, error); 282 283 /* 284 * now attach all our children 285 */ 286 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node)); 287 for (node = firstchild(node); node; node = nextsibling(node)) { 288 char *name = prom_getpropstring(node, "name"); 289 290 if (ebus_setup_attach_args(sc, sbt, dmatag, node, &ea) != 0) { 291 printf("ebus_attach: %s: incomplete\n", name); 292 continue; 293 } 294 DPRINTF(EDB_CHILD, 295 ("- found child `%s', attaching\n", ea.ea_name)); 296 (void)config_found(self, &ea, ebus_print); 297 ebus_destroy_attach_args(&ea); 298 } 299 } 300 301 int 302 ebus_setup_attach_args(sc, bustag, dmatag, node, ea) 303 struct ebus_softc *sc; 304 bus_space_tag_t bustag; 305 bus_dma_tag_t dmatag; 306 int node; 307 struct ebus_attach_args *ea; 308 { 309 int n, err; 310 311 memset(ea, 0, sizeof(struct ebus_attach_args)); 312 313 err = prom_getprop(node, "name", 1, &n, &ea->ea_name); 314 if (err != 0) 315 return (err); 316 ea->ea_name[n] = '\0'; 317 318 ea->ea_node = node; 319 ea->ea_bustag = bustag; 320 ea->ea_dmatag = dmatag; 321 322 err = prom_getprop(node, "reg", sizeof(struct ebus_regs), 323 &ea->ea_nreg, &ea->ea_reg); 324 if (err != 0) 325 return (err); 326 327 /* 328 * On Ultra the bar is the _offset_ of the BAR in PCI config 329 * space but in (some?) ms-IIep systems (e.g. Krups) it's the 330 * _number_ of the BAR - e.g. BAR1 is represented by 1 in 331 * Krups PROM, while on Ultra it's 0x14. Fix it here. 332 */ 333 for (n = 0; n < ea->ea_nreg; ++n) 334 if (ea->ea_reg[n].hi < PCI_MAPREG_START) { 335 ea->ea_reg[n].hi = PCI_MAPREG_START 336 + ea->ea_reg[n].hi * sizeof(pcireg_t); 337 } 338 339 340 err = prom_getprop(node, "address", sizeof(u_int32_t), 341 &ea->ea_nvaddr, &ea->ea_vaddr); 342 if (err != ENOENT) { 343 if (err != 0) 344 return (err); 345 346 if (ea->ea_nreg != ea->ea_nvaddr) 347 printf("ebus loses: device %s: %d regs and %d addrs\n", 348 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr); 349 } else 350 ea->ea_nvaddr = 0; 351 352 /* XXX: "interrupts" hack */ 353 for (n = 0; n < wiring_map_size; ++n) { 354 struct msiiep_ebus_intr_wiring *w = &wiring_map[n]; 355 if (strcmp(w->name, ea->ea_name) == 0) { 356 ea->ea_intr = malloc(sizeof(u_int32_t), 357 M_DEVBUF, M_NOWAIT); 358 ea->ea_intr[0] = w->line; 359 ea->ea_nintr = 1; 360 break; 361 } 362 } 363 364 return (0); 365 } 366 367 void 368 ebus_destroy_attach_args(ea) 369 struct ebus_attach_args *ea; 370 { 371 372 if (ea->ea_name) 373 free((void *)ea->ea_name, M_DEVBUF); 374 if (ea->ea_reg) 375 free((void *)ea->ea_reg, M_DEVBUF); 376 if (ea->ea_intr) 377 free((void *)ea->ea_intr, M_DEVBUF); 378 if (ea->ea_vaddr) 379 free((void *)ea->ea_vaddr, M_DEVBUF); 380 } 381 382 int 383 ebus_print(aux, p) 384 void *aux; 385 const char *p; 386 { 387 struct ebus_attach_args *ea = aux; 388 int i; 389 390 if (p) 391 aprint_normal("%s at %s", ea->ea_name, p); 392 for (i = 0; i < ea->ea_nreg; ++i) 393 aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",", 394 ea->ea_reg[i].hi, ea->ea_reg[i].lo); 395 for (i = 0; i < ea->ea_nintr; ++i) 396 aprint_normal(" line %d", ea->ea_intr[i]); 397 return (UNCONF); 398 } 399 400 401 /* 402 * bus space and bus DMA methods below here 403 */ 404 bus_dma_tag_t 405 ebus_alloc_dma_tag(sc, pdt) 406 struct ebus_softc *sc; 407 bus_dma_tag_t pdt; 408 { 409 bus_dma_tag_t dt; 410 411 dt = (bus_dma_tag_t) 412 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT); 413 if (dt == NULL) 414 panic("unable to allocate ebus DMA tag"); 415 416 memset(dt, 0, sizeof *dt); 417 dt->_cookie = sc; 418 #define PCOPY(x) dt->x = pdt->x 419 PCOPY(_dmamap_create); 420 PCOPY(_dmamap_destroy); 421 PCOPY(_dmamap_load); 422 PCOPY(_dmamap_load_mbuf); 423 PCOPY(_dmamap_load_uio); 424 PCOPY(_dmamap_load_raw); 425 PCOPY(_dmamap_unload); 426 PCOPY(_dmamap_sync); 427 PCOPY(_dmamem_alloc); 428 PCOPY(_dmamem_free); 429 PCOPY(_dmamem_map); 430 PCOPY(_dmamem_unmap); 431 PCOPY(_dmamem_mmap); 432 #undef PCOPY 433 return (dt); 434 } 435 436 /* 437 * bus space support. <sparc64/dev/psychoreg.h> has a discussion 438 * about PCI physical addresses, which also applies to ebus. 439 */ 440 static int 441 _ebus_bus_map(t, ba, size, flags, va, hp) 442 bus_space_tag_t t; 443 bus_addr_t ba; /* encodes bar/offset */ 444 bus_size_t size; 445 int flags; 446 vaddr_t va; 447 bus_space_handle_t *hp; 448 { 449 struct ebus_softc *sc = t->cookie; 450 u_int bar; 451 paddr_t offset; 452 int i; 453 454 bar = BUS_ADDR_IOSPACE(ba); 455 offset = BUS_ADDR_PADDR(ba); 456 457 DPRINTF(EDB_BUSMAP, 458 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n", 459 (int)bar, (u_int32_t)offset, (u_int32_t)size, 460 flags, (void *)va)); 461 462 /* EBus has only two BARs */ 463 if (PCI_MAPREG_NUM(bar) > 1) { 464 DPRINTF(EDB_BUSMAP, 465 ("\n_ebus_bus_map: impossible bar\n")); 466 return (EINVAL); 467 } 468 469 /* 470 * Almost all of the interesting ebus children are mapped by 471 * BAR1, the last entry in sc_reg[], so work our way backwards. 472 */ 473 for (i = sc->sc_nreg - 1; i >= 0; --i) { 474 bus_addr_t pciaddr; 475 u_int32_t ss; 476 477 /* EBus only does MEM32 */ 478 ss = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK; 479 if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32) 480 continue; 481 482 if (bar != (sc->sc_reg[i].phys_hi 483 & OFW_PCI_PHYS_HI_REGISTERMASK)) 484 continue; 485 486 pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset; 487 488 if (pciaddr + size > sc->sc_reg[i].phys_lo 489 + sc->sc_reg[i].size_lo) 490 continue; 491 492 DPRINTF(EDB_BUSMAP, 493 ("_ebus_bus_map: mapping to PCI addr %x\n", 494 (u_int32_t)pciaddr)); 495 496 /* pass it onto the pci controller */ 497 return (bus_space_map2(t->parent, pciaddr, size, 498 flags, va, hp)); 499 } 500 501 DPRINTF(EDB_BUSMAP, (": FAILED\n")); 502 return (EINVAL); 503 } 504 505 static paddr_t 506 ebus_bus_mmap(t, ba, off, prot, flags) 507 bus_space_tag_t t; 508 bus_addr_t ba; 509 off_t off; 510 int prot; 511 int flags; 512 { 513 514 /* XXX: not implemented yet */ 515 return (-1); 516 } 517 518 /* 519 * Install an interrupt handler for a EBus device. 520 */ 521 void * 522 ebus_intr_establish(t, pri, level, handler, arg, fastvec) 523 bus_space_tag_t t; 524 int pri; 525 int level; 526 int (*handler)(void *); 527 void *arg; 528 void (*fastvec)(void); /* ignored */ 529 { 530 return (bus_intr_establish(t->parent, pri, level, handler, arg)); 531 } 532