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