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