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