ebus.c revision 1.19 1 /* $NetBSD: ebus.c,v 1.19 2005/08/12 12:46:17 macallan 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.19 2005/08/12 12:46:17 macallan 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 #ifdef MSIIEP
247 /*
248 * some Krups OF doesn't have a node for the audio chip, so we
249 * just check if there's a 'sound' node and if not we fill in the
250 * ebus_attach_args by hand and hope nobody cares about the invalid
251 * node number
252 */
253 int found_sound=0;
254 #endif
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 sc->sc_node = node;
268 sc->sc_parent = parent; /* XXX: unused so far */
269 sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */
270
271 if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL)
272 panic("unable to allocate ebus bus tag");
273
274 sbt->sparc_bus_map = _ebus_bus_map;
275 sbt->sparc_bus_mmap = ebus_bus_mmap;
276 sbt->sparc_intr_establish = ebus_intr_establish;
277
278 dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
279
280 /*
281 * Setup ranges. The interesting thing is that we use "reg"
282 * not "ranges", since "reg" on ebus has exactly the data we'd
283 * get by processing "ranges".
284 *
285 */
286 error = prom_getprop(node, "reg", sizeof(struct ofw_pci_register),
287 &sc->sc_nreg, &sc->sc_reg);
288 if (error)
289 panic("%s: unable to read ebus registers (error %d)",
290 self->dv_xname, error);
291
292 /*
293 * now attach all our children
294 */
295 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
296 for (node = firstchild(node); node; node = nextsibling(node)) {
297 char *name = prom_getpropstring(node, "name");
298
299 #ifdef MSIIEP
300 if (strcmp(name, "sound") == 0)
301 found_sound = 1;
302 #endif
303 if (ebus_setup_attach_args(sc, sbt, dmatag, node, &ea) != 0) {
304 printf("ebus_attach: %s: incomplete\n", name);
305 continue;
306 }
307 DPRINTF(EDB_CHILD,
308 ("- found child `%s', attaching\n", ea.ea_name));
309 (void)config_found(self, &ea, ebus_print);
310 ebus_destroy_attach_args(&ea);
311 }
312 #ifdef MSIIEP
313 if (found_sound == 0) {
314 /* no sound node - make up an attachment */
315
316 char name[8] = "sound";
317 struct ebus_regs reg;
318 uint32_t vaddr;
319 uint32_t intr;
320
321 memset(&ea, 0, sizeof(struct ebus_attach_args));
322 ea.ea_name = name;
323 ea.ea_node = ~0;
324 ea.ea_bustag = sbt;
325 ea.ea_dmatag = dmatag;
326 ea.ea_reg = ®
327 ea.ea_nreg = 1;
328 ea.ea_intr = &intr;
329 ea.ea_nintr = 1;
330 ea.ea_vaddr = &vaddr;
331 ea.ea_nvaddr = 0;
332 /* we Just Know(tm) thesa values ;) */
333 ea.ea_reg[0].hi = 0x14;
334 ea.ea_reg[0].lo = 0x200000;
335 ea.ea_reg[0].size = 0x1000; /* XXX */
336 ea.ea_intr[0] = 3;
337 config_found(self, &ea, ebus_print);
338 }
339 #endif
340 }
341
342 int
343 ebus_setup_attach_args(sc, bustag, dmatag, node, ea)
344 struct ebus_softc *sc;
345 bus_space_tag_t bustag;
346 bus_dma_tag_t dmatag;
347 int node;
348 struct ebus_attach_args *ea;
349 {
350 int n, err;
351
352 memset(ea, 0, sizeof(struct ebus_attach_args));
353
354 err = prom_getprop(node, "name", 1, &n, &ea->ea_name);
355 if (err != 0)
356 return (err);
357 ea->ea_name[n] = '\0';
358
359 ea->ea_node = node;
360 ea->ea_bustag = bustag;
361 ea->ea_dmatag = dmatag;
362
363 err = prom_getprop(node, "reg", sizeof(struct ebus_regs),
364 &ea->ea_nreg, &ea->ea_reg);
365 if (err != 0)
366 return (err);
367
368 /*
369 * On Ultra the bar is the _offset_ of the BAR in PCI config
370 * space but in (some?) ms-IIep systems (e.g. Krups) it's the
371 * _number_ of the BAR - e.g. BAR1 is represented by 1 in
372 * Krups PROM, while on Ultra it's 0x14. Fix it here.
373 */
374 for (n = 0; n < ea->ea_nreg; ++n) {
375 if (ea->ea_reg[n].hi < PCI_MAPREG_START) {
376 ea->ea_reg[n].hi = PCI_MAPREG_START
377 + ea->ea_reg[n].hi * sizeof(pcireg_t);
378 }
379 }
380
381 err = prom_getprop(node, "address", sizeof(u_int32_t),
382 &ea->ea_nvaddr, &ea->ea_vaddr);
383 if (err != ENOENT) {
384 if (err != 0)
385 return (err);
386
387 if (ea->ea_nreg != ea->ea_nvaddr)
388 printf("ebus loses: device %s: %d regs and %d addrs\n",
389 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
390 } else
391 ea->ea_nvaddr = 0;
392
393 /* XXX: "interrupts" hack */
394 for (n = 0; n < wiring_map_size; ++n) {
395 struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
396 if (strcmp(w->name, ea->ea_name) == 0) {
397 ea->ea_intr = malloc(sizeof(u_int32_t),
398 M_DEVBUF, M_NOWAIT);
399 ea->ea_intr[0] = w->line;
400 ea->ea_nintr = 1;
401 break;
402 }
403 }
404
405 return (0);
406 }
407
408 void
409 ebus_destroy_attach_args(ea)
410 struct ebus_attach_args *ea;
411 {
412
413 if (ea->ea_name)
414 free((void *)ea->ea_name, M_DEVBUF);
415 if (ea->ea_reg)
416 free((void *)ea->ea_reg, M_DEVBUF);
417 if (ea->ea_intr)
418 free((void *)ea->ea_intr, M_DEVBUF);
419 if (ea->ea_vaddr)
420 free((void *)ea->ea_vaddr, M_DEVBUF);
421 }
422
423 int
424 ebus_print(aux, p)
425 void *aux;
426 const char *p;
427 {
428 struct ebus_attach_args *ea = aux;
429 int i;
430
431 if (p)
432 aprint_normal("%s at %s", ea->ea_name, p);
433 for (i = 0; i < ea->ea_nreg; ++i)
434 aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",",
435 ea->ea_reg[i].hi, ea->ea_reg[i].lo);
436 for (i = 0; i < ea->ea_nintr; ++i)
437 aprint_normal(" line %d", ea->ea_intr[i]);
438 return (UNCONF);
439 }
440
441
442 /*
443 * bus space and bus DMA methods below here
444 */
445 bus_dma_tag_t
446 ebus_alloc_dma_tag(sc, pdt)
447 struct ebus_softc *sc;
448 bus_dma_tag_t pdt;
449 {
450 bus_dma_tag_t dt;
451
452 dt = (bus_dma_tag_t)
453 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
454 if (dt == NULL)
455 panic("unable to allocate ebus DMA tag");
456
457 memset(dt, 0, sizeof *dt);
458 dt->_cookie = sc;
459 #define PCOPY(x) dt->x = pdt->x
460 PCOPY(_dmamap_create);
461 PCOPY(_dmamap_destroy);
462 PCOPY(_dmamap_load);
463 PCOPY(_dmamap_load_mbuf);
464 PCOPY(_dmamap_load_uio);
465 PCOPY(_dmamap_load_raw);
466 PCOPY(_dmamap_unload);
467 PCOPY(_dmamap_sync);
468 PCOPY(_dmamem_alloc);
469 PCOPY(_dmamem_free);
470 PCOPY(_dmamem_map);
471 PCOPY(_dmamem_unmap);
472 PCOPY(_dmamem_mmap);
473 #undef PCOPY
474 return (dt);
475 }
476
477 /*
478 * bus space support. <sparc64/dev/psychoreg.h> has a discussion
479 * about PCI physical addresses, which also applies to ebus.
480 */
481 static int
482 _ebus_bus_map(t, ba, size, flags, va, hp)
483 bus_space_tag_t t;
484 bus_addr_t ba; /* encodes bar/offset */
485 bus_size_t size;
486 int flags;
487 vaddr_t va;
488 bus_space_handle_t *hp;
489 {
490 struct ebus_softc *sc = t->cookie;
491 u_int bar;
492 paddr_t offset;
493 int i;
494
495 bar = BUS_ADDR_IOSPACE(ba);
496 offset = BUS_ADDR_PADDR(ba);
497
498 DPRINTF(EDB_BUSMAP,
499 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
500 (int)bar, (u_int32_t)offset, (u_int32_t)size,
501 flags, (void *)va));
502
503 /* EBus has only two BARs */
504 if (PCI_MAPREG_NUM(bar) > 1) {
505 DPRINTF(EDB_BUSMAP,
506 ("\n_ebus_bus_map: impossible bar\n"));
507 return (EINVAL);
508 }
509
510 /*
511 * Almost all of the interesting ebus children are mapped by
512 * BAR1, the last entry in sc_reg[], so work our way backwards.
513 */
514 for (i = sc->sc_nreg - 1; i >= 0; --i) {
515 bus_addr_t pciaddr;
516 u_int32_t ss;
517
518 /* EBus only does MEM32 */
519 ss = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
520 if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
521 continue;
522
523 if (bar != (sc->sc_reg[i].phys_hi
524 & OFW_PCI_PHYS_HI_REGISTERMASK))
525 continue;
526
527 pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
528
529 if (pciaddr + size > sc->sc_reg[i].phys_lo
530 + sc->sc_reg[i].size_lo)
531 continue;
532
533 DPRINTF(EDB_BUSMAP,
534 ("_ebus_bus_map: mapping to PCI addr %x\n",
535 (u_int32_t)pciaddr));
536
537 /* pass it onto the pci controller */
538 return (bus_space_map2(t->parent, pciaddr, size,
539 flags, va, hp));
540 }
541
542 DPRINTF(EDB_BUSMAP, (": FAILED\n"));
543 return (EINVAL);
544 }
545
546 static paddr_t
547 ebus_bus_mmap(t, ba, off, prot, flags)
548 bus_space_tag_t t;
549 bus_addr_t ba;
550 off_t off;
551 int prot;
552 int flags;
553 {
554
555 /* XXX: not implemented yet */
556 return (-1);
557 }
558
559 /*
560 * Install an interrupt handler for a EBus device.
561 */
562 void *
563 ebus_intr_establish(t, pri, level, handler, arg, fastvec)
564 bus_space_tag_t t;
565 int pri;
566 int level;
567 int (*handler)(void *);
568 void *arg;
569 void (*fastvec)(void); /* ignored */
570 {
571 return (bus_intr_establish(t->parent, pri, level, handler, arg));
572 }
573