ebus.c revision 1.20 1 /* $NetBSD: ebus.c,v 1.20 2005/08/12 16:28:14 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.20 2005/08/12 16:28:14 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
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 err = prom_getprop(node, "address", sizeof(u_int32_t),
340 &ea->ea_nvaddr, &ea->ea_vaddr);
341 if (err != ENOENT) {
342 if (err != 0)
343 return (err);
344
345 if (ea->ea_nreg != ea->ea_nvaddr)
346 printf("ebus loses: device %s: %d regs and %d addrs\n",
347 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
348 } else
349 ea->ea_nvaddr = 0;
350
351 /* XXX: "interrupts" hack */
352 for (n = 0; n < wiring_map_size; ++n) {
353 struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
354 if (strcmp(w->name, ea->ea_name) == 0) {
355 ea->ea_intr = malloc(sizeof(u_int32_t),
356 M_DEVBUF, M_NOWAIT);
357 ea->ea_intr[0] = w->line;
358 ea->ea_nintr = 1;
359 break;
360 }
361 }
362
363 return (0);
364 }
365
366 void
367 ebus_destroy_attach_args(ea)
368 struct ebus_attach_args *ea;
369 {
370
371 if (ea->ea_name)
372 free((void *)ea->ea_name, M_DEVBUF);
373 if (ea->ea_reg)
374 free((void *)ea->ea_reg, M_DEVBUF);
375 if (ea->ea_intr)
376 free((void *)ea->ea_intr, M_DEVBUF);
377 if (ea->ea_vaddr)
378 free((void *)ea->ea_vaddr, M_DEVBUF);
379 }
380
381 int
382 ebus_print(aux, p)
383 void *aux;
384 const char *p;
385 {
386 struct ebus_attach_args *ea = aux;
387 int i;
388
389 if (p)
390 aprint_normal("%s at %s", ea->ea_name, p);
391 for (i = 0; i < ea->ea_nreg; ++i)
392 aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",",
393 ea->ea_reg[i].hi, ea->ea_reg[i].lo);
394 for (i = 0; i < ea->ea_nintr; ++i)
395 aprint_normal(" line %d", ea->ea_intr[i]);
396 return (UNCONF);
397 }
398
399
400 /*
401 * bus space and bus DMA methods below here
402 */
403 bus_dma_tag_t
404 ebus_alloc_dma_tag(sc, pdt)
405 struct ebus_softc *sc;
406 bus_dma_tag_t pdt;
407 {
408 bus_dma_tag_t dt;
409
410 dt = (bus_dma_tag_t)
411 malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
412 if (dt == NULL)
413 panic("unable to allocate ebus DMA tag");
414
415 memset(dt, 0, sizeof *dt);
416 dt->_cookie = sc;
417 #define PCOPY(x) dt->x = pdt->x
418 PCOPY(_dmamap_create);
419 PCOPY(_dmamap_destroy);
420 PCOPY(_dmamap_load);
421 PCOPY(_dmamap_load_mbuf);
422 PCOPY(_dmamap_load_uio);
423 PCOPY(_dmamap_load_raw);
424 PCOPY(_dmamap_unload);
425 PCOPY(_dmamap_sync);
426 PCOPY(_dmamem_alloc);
427 PCOPY(_dmamem_free);
428 PCOPY(_dmamem_map);
429 PCOPY(_dmamem_unmap);
430 PCOPY(_dmamem_mmap);
431 #undef PCOPY
432 return (dt);
433 }
434
435 /*
436 * bus space support. <sparc64/dev/psychoreg.h> has a discussion
437 * about PCI physical addresses, which also applies to ebus.
438 */
439 static int
440 _ebus_bus_map(t, ba, size, flags, va, hp)
441 bus_space_tag_t t;
442 bus_addr_t ba; /* encodes bar/offset */
443 bus_size_t size;
444 int flags;
445 vaddr_t va;
446 bus_space_handle_t *hp;
447 {
448 struct ebus_softc *sc = t->cookie;
449 u_int bar;
450 paddr_t offset;
451 int i;
452
453 bar = BUS_ADDR_IOSPACE(ba);
454 offset = BUS_ADDR_PADDR(ba);
455
456 DPRINTF(EDB_BUSMAP,
457 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
458 (int)bar, (u_int32_t)offset, (u_int32_t)size,
459 flags, (void *)va));
460
461 /* EBus has only two BARs */
462 if (PCI_MAPREG_NUM(bar) > 1) {
463 DPRINTF(EDB_BUSMAP,
464 ("\n_ebus_bus_map: impossible bar\n"));
465 return (EINVAL);
466 }
467
468 /*
469 * Almost all of the interesting ebus children are mapped by
470 * BAR1, the last entry in sc_reg[], so work our way backwards.
471 */
472 for (i = sc->sc_nreg - 1; i >= 0; --i) {
473 bus_addr_t pciaddr;
474 u_int32_t ss;
475
476 /* EBus only does MEM32 */
477 ss = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
478 if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
479 continue;
480
481 if (bar != (sc->sc_reg[i].phys_hi
482 & OFW_PCI_PHYS_HI_REGISTERMASK))
483 continue;
484
485 pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
486
487 if (pciaddr + size > sc->sc_reg[i].phys_lo
488 + sc->sc_reg[i].size_lo)
489 continue;
490
491 DPRINTF(EDB_BUSMAP,
492 ("_ebus_bus_map: mapping to PCI addr %x\n",
493 (u_int32_t)pciaddr));
494
495 /* pass it onto the pci controller */
496 return (bus_space_map2(t->parent, pciaddr, size,
497 flags, va, hp));
498 }
499
500 DPRINTF(EDB_BUSMAP, (": FAILED\n"));
501 return (EINVAL);
502 }
503
504 static paddr_t
505 ebus_bus_mmap(t, ba, off, prot, flags)
506 bus_space_tag_t t;
507 bus_addr_t ba;
508 off_t off;
509 int prot;
510 int flags;
511 {
512
513 /* XXX: not implemented yet */
514 return (-1);
515 }
516
517 /*
518 * Install an interrupt handler for a EBus device.
519 */
520 void *
521 ebus_intr_establish(t, pri, level, handler, arg, fastvec)
522 bus_space_tag_t t;
523 int pri;
524 int level;
525 int (*handler)(void *);
526 void *arg;
527 void (*fastvec)(void); /* ignored */
528 {
529 return (bus_intr_establish(t->parent, pri, level, handler, arg));
530 }
531