ebus.c revision 1.51.4.1 1 /* $NetBSD: ebus.c,v 1.51.4.1 2009/05/04 08:11:57 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000, 2001 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.51.4.1 2009/05/04 08:11:57 yamt Exp $");
31
32 #include "opt_ddb.h"
33
34 /*
35 * UltraSPARC 5 and beyond ebus support.
36 *
37 * note that this driver is not complete:
38 * - interrupt establish is written and appears to work
39 * - bus map code is written and appears to work
40 * - ebus2 DMA code is completely unwritten, we just punt to
41 * the iommu.
42 */
43
44 #ifdef DEBUG
45 #define EDB_PROM 0x01
46 #define EDB_CHILD 0x02
47 #define EDB_INTRMAP 0x04
48 #define EDB_BUSMAP 0x08
49 int ebus_debug = 0;
50 #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0)
51 #else
52 #define DPRINTF(l, s)
53 #endif
54
55 #include <sys/param.h>
56 #include <sys/conf.h>
57 #include <sys/device.h>
58 #include <sys/errno.h>
59 #include <sys/extent.h>
60 #include <sys/malloc.h>
61 #include <sys/systm.h>
62 #include <sys/time.h>
63
64 #define _SPARC_BUS_DMA_PRIVATE
65 #include <machine/bus.h>
66 #include <machine/autoconf.h>
67 #include <machine/openfirm.h>
68
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcidevs.h>
72
73 #include <sparc64/dev/iommureg.h>
74 #include <sparc64/dev/iommuvar.h>
75 #include <sparc64/dev/psychoreg.h>
76 #include <sparc64/dev/psychovar.h>
77 #include <dev/ebus/ebusreg.h>
78 #include <dev/ebus/ebusvar.h>
79
80 struct ebus_softc {
81 struct device sc_dev;
82
83 int sc_node;
84
85 bus_space_tag_t sc_memtag; /* from pci */
86 bus_space_tag_t sc_iotag; /* from pci */
87 bus_space_tag_t sc_childbustag; /* pass to children */
88 bus_dma_tag_t sc_dmatag;
89
90 struct ebus_ranges *sc_range;
91 struct ebus_interrupt_map *sc_intmap;
92 struct ebus_interrupt_map_mask sc_intmapmask;
93
94 int sc_nrange; /* counters */
95 int sc_nintmap;
96 };
97
98 int ebus_match(struct device *, struct cfdata *, void *);
99 void ebus_attach(struct device *, struct device *, void *);
100
101 CFATTACH_DECL(ebus, sizeof(struct ebus_softc),
102 ebus_match, ebus_attach, NULL, NULL);
103
104 bus_space_tag_t ebus_alloc_bus_tag(struct ebus_softc *, int);
105
106 int ebus_setup_attach_args(struct ebus_softc *, int,
107 struct ebus_attach_args *);
108 void ebus_destroy_attach_args(struct ebus_attach_args *);
109 int ebus_print(void *, const char *);
110 void ebus_find_ino(struct ebus_softc *, struct ebus_attach_args *);
111
112 /*
113 * here are our bus space and bus DMA routines.
114 */
115 static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
116 static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, vaddr_t,
117 bus_space_handle_t *);
118 static void *ebus_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
119 void *, void(*)(void));
120
121 int
122 ebus_match(struct device *parent, struct cfdata *match, void *aux)
123 {
124 struct pci_attach_args *pa = aux;
125 char *name;
126 int node;
127
128 /* Only attach if there's a PROM node. */
129 node = PCITAG_NODE(pa->pa_tag);
130 if (node == -1)
131 return (0);
132
133 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
134 return (0);
135
136 /* Match a real ebus */
137 name = prom_getpropstring(node, "name");
138 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
139 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
140 strcmp(name, "ebus") == 0)
141 return (1);
142
143 /* Or a real ebus III */
144 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
145 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII &&
146 strcmp(name, "ebus") == 0)
147 return (1);
148
149 /* Or a PCI-ISA bridge XXX I hope this is on-board. */
150 if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
151 return (1);
152 }
153
154 return (0);
155 }
156
157 /*
158 * attach an ebus and all it's children. this code is modeled
159 * after the sbus code which does similar things.
160 */
161 void
162 ebus_attach(struct device *parent, struct device *self, void *aux)
163 {
164 struct ebus_softc *sc = (struct ebus_softc *)self;
165 struct pci_attach_args *pa = aux;
166 struct ebus_attach_args eba;
167 struct ebus_interrupt_map_mask *immp;
168 int node, nmapmask, error;
169 char devinfo[256];
170
171 printf("\n");
172
173 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
174 printf("%s: %s, revision 0x%02x\n", device_xname(self), devinfo,
175 PCI_REVISION(pa->pa_class));
176
177 sc->sc_memtag = pa->pa_memt;
178 sc->sc_iotag = pa->pa_iot;
179 sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
180 sc->sc_dmatag = pa->pa_dmat;
181
182 node = PCITAG_NODE(pa->pa_tag);
183 if (node == -1)
184 panic("could not find ebus node");
185
186 sc->sc_node = node;
187
188 /*
189 * fill in our softc with information from the prom
190 */
191 sc->sc_intmap = NULL;
192 sc->sc_range = NULL;
193 sc->sc_nintmap = 0;
194 error = prom_getprop(node, "interrupt-map",
195 sizeof(struct ebus_interrupt_map),
196 &sc->sc_nintmap, &sc->sc_intmap);
197 switch (error) {
198 case 0:
199 immp = &sc->sc_intmapmask;
200 nmapmask = sizeof(*immp);
201 error = prom_getprop(node, "interrupt-map-mask",
202 sizeof(struct ebus_interrupt_map_mask), &nmapmask,
203 &immp);
204 if (error)
205 panic("could not get ebus interrupt-map-mask, error %d",
206 error);
207 if (nmapmask != 1)
208 panic("ebus interrupt-map-mask is broken");
209 break;
210 case ENOENT:
211 break;
212 default:
213 panic("ebus interrupt-map: error %d", error);
214 break;
215 }
216
217 error = prom_getprop(node, "ranges", sizeof(struct ebus_ranges),
218 &sc->sc_nrange, &sc->sc_range);
219 if (error)
220 panic("ebus ranges: error %d", error);
221
222 /*
223 * now attach all our children
224 */
225 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
226 for (node = firstchild(node); node; node = nextsibling(node)) {
227 char *name = prom_getpropstring(node, "name");
228
229 if (ebus_setup_attach_args(sc, node, &eba) != 0) {
230 printf("ebus_attach: %s: incomplete\n", name);
231 continue;
232 } else {
233 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n",
234 eba.ea_name));
235 (void)config_found(self, &eba, ebus_print);
236 }
237 ebus_destroy_attach_args(&eba);
238 }
239 }
240
241 int ebus_setup_attach_args(struct ebus_softc *, int,
242 struct ebus_attach_args *);
243 int
244 ebus_setup_attach_args(struct ebus_softc *sc, int node,
245 struct ebus_attach_args *ea)
246 {
247 int n, rv;
248
249 memset(ea, 0, sizeof(struct ebus_attach_args));
250 n = 0;
251 rv = prom_getprop(node, "name", 1, &n, &ea->ea_name);
252 if (rv != 0)
253 return (rv);
254 ea->ea_name[n] = '\0';
255
256 ea->ea_node = node;
257 ea->ea_bustag = sc->sc_childbustag;
258 ea->ea_dmatag = sc->sc_dmatag;
259
260 rv = prom_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg,
261 &ea->ea_reg);
262 if (rv)
263 return (rv);
264
265 rv = prom_getprop(node, "address", sizeof(uint32_t), &ea->ea_nvaddr,
266 &ea->ea_vaddr);
267 if (rv != ENOENT) {
268 if (rv)
269 return (rv);
270
271 if (ea->ea_nreg != ea->ea_nvaddr)
272 printf("ebus loses: device %s: %d regs and %d addrs\n",
273 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
274 } else
275 ea->ea_nvaddr = 0;
276
277 if (prom_getprop(node, "interrupts", sizeof(uint32_t), &ea->ea_nintr,
278 &ea->ea_intr))
279 ea->ea_nintr = 0;
280 else
281 ebus_find_ino(sc, ea);
282
283 return (0);
284 }
285
286 void
287 ebus_destroy_attach_args(struct ebus_attach_args *ea)
288 {
289
290 if (ea->ea_name)
291 free((void *)ea->ea_name, M_DEVBUF);
292 if (ea->ea_reg)
293 free((void *)ea->ea_reg, M_DEVBUF);
294 if (ea->ea_intr)
295 free((void *)ea->ea_intr, M_DEVBUF);
296 if (ea->ea_vaddr)
297 free((void *)ea->ea_vaddr, M_DEVBUF);
298 }
299
300 int
301 ebus_print(void *aux, const char *p)
302 {
303 struct ebus_attach_args *ea = aux;
304 int i;
305
306 if (p)
307 aprint_normal("%s at %s", ea->ea_name, p);
308 for (i = 0; i < ea->ea_nreg; i++)
309 aprint_normal("%s %x-%x", i == 0 ? " addr" : ",",
310 ea->ea_reg[i].lo,
311 ea->ea_reg[i].lo + ea->ea_reg[i].size - 1);
312 for (i = 0; i < ea->ea_nintr; i++)
313 aprint_normal(" ipl %d", ea->ea_intr[i]);
314 return (UNCONF);
315 }
316
317
318 /*
319 * find the INO values for each interrupt and fill them in.
320 *
321 * for each "reg" property of this device, mask it's hi and lo
322 * values with the "interrupt-map-mask"'s hi/lo values, and also
323 * mask the interrupt number with the interrupt mask. search the
324 * "interrupt-map" list for matching values of hi, lo and interrupt
325 * to give the INO for this interrupt.
326 */
327 void
328 ebus_find_ino(struct ebus_softc *sc, struct ebus_attach_args *ea)
329 {
330 uint32_t hi, lo, intr;
331 int i, j, k;
332
333 if (sc->sc_nintmap == 0) {
334 for (i = 0; i < ea->ea_nintr; i++) {
335 OF_mapintr(ea->ea_node, &ea->ea_intr[i],
336 sizeof(ea->ea_intr[0]),
337 sizeof(ea->ea_intr[0]));
338 }
339 return;
340 }
341
342 DPRINTF(EDB_INTRMAP,
343 ("ebus_find_ino: searching %d interrupts", ea->ea_nintr));
344
345 for (j = 0; j < ea->ea_nintr; j++) {
346
347 intr = ea->ea_intr[j] & sc->sc_intmapmask.intr;
348
349 DPRINTF(EDB_INTRMAP,
350 ("; intr %x masked to %x", ea->ea_intr[j], intr));
351 for (i = 0; i < ea->ea_nreg; i++) {
352 hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi;
353 lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo;
354
355 DPRINTF(EDB_INTRMAP,
356 ("; reg hi.lo %08x.%08x masked to %08x.%08x",
357 ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo));
358 for (k = 0; k < sc->sc_nintmap; k++) {
359 DPRINTF(EDB_INTRMAP,
360 ("; checking hi.lo %08x.%08x intr %x",
361 sc->sc_intmap[k].hi, sc->sc_intmap[k].lo,
362 sc->sc_intmap[k].intr));
363 if (hi == sc->sc_intmap[k].hi &&
364 lo == sc->sc_intmap[k].lo &&
365 intr == sc->sc_intmap[k].intr) {
366 ea->ea_intr[j] =
367 sc->sc_intmap[k].cintr;
368 DPRINTF(EDB_INTRMAP,
369 ("; FOUND IT! changing to %d\n",
370 sc->sc_intmap[k].cintr));
371 goto next_intr;
372 }
373 }
374 }
375 next_intr:;
376 }
377 }
378
379 /*
380 * bus space support. <sparc64/dev/psychoreg.h> has a discussion
381 * about PCI physical addresses, which also applies to ebus.
382 */
383 bus_space_tag_t
384 ebus_alloc_bus_tag(struct ebus_softc *sc, int type)
385 {
386 bus_space_tag_t bt;
387
388 bt = (bus_space_tag_t)
389 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
390 if (bt == NULL)
391 panic("could not allocate ebus bus tag");
392
393 memset(bt, 0, sizeof *bt);
394 bt->cookie = sc;
395 bt->parent = sc->sc_memtag;
396 bt->type = type;
397 bt->sparc_bus_map = _ebus_bus_map;
398 bt->sparc_bus_mmap = ebus_bus_mmap;
399 bt->sparc_intr_establish = ebus_intr_establish;
400 return (bt);
401 }
402
403 static int
404 _ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
405 vaddr_t va, bus_space_handle_t *hp)
406 {
407 struct ebus_softc *sc = t->cookie;
408 paddr_t offset;
409 u_int bar;
410 int i, ss;
411
412 bar = BUS_ADDR_IOSPACE(ba);
413 offset = BUS_ADDR_PADDR(ba);
414
415 DPRINTF(EDB_BUSMAP,
416 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
417 (int)bar, (uint32_t)offset, (uint32_t)size,
418 flags, (void *)va));
419
420 for (i = 0; i < sc->sc_nrange; i++) {
421 bus_addr_t pciaddr;
422
423 if (bar != sc->sc_range[i].child_hi)
424 continue;
425 if (offset < sc->sc_range[i].child_lo ||
426 (offset + size) >
427 (sc->sc_range[i].child_lo + sc->sc_range[i].size))
428 continue;
429
430 /* Isolate address space and find the right tag */
431 ss = (sc->sc_range[i].phys_hi>>24)&3;
432 switch (ss) {
433 case 1: /* I/O space */
434 t = sc->sc_iotag;
435 break;
436 case 2: /* Memory space */
437 t = sc->sc_memtag;
438 break;
439 case 0: /* Config space */
440 case 3: /* 64-bit Memory space */
441 default: /* WTF? */
442 /* We don't handle these */
443 panic("_ebus_bus_map: illegal space %x", ss);
444 break;
445 }
446 pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
447 sc->sc_range[i].phys_lo;
448 pciaddr += offset;
449
450 DPRINTF(EDB_BUSMAP,
451 ("_ebus_bus_map: mapping to PCI addr %x\n",
452 (uint32_t)pciaddr));
453
454 /* pass it onto the psycho */
455 return (bus_space_map(t, pciaddr, size, flags, hp));
456 }
457 DPRINTF(EDB_BUSMAP, (": FAILED\n"));
458 return (EINVAL);
459 }
460
461 static paddr_t
462 ebus_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
463 int flags)
464 {
465 bus_addr_t offset = paddr;
466 struct ebus_softc *sc = t->cookie;
467 int i;
468
469 for (i = 0; i < sc->sc_nrange; i++) {
470 bus_addr_t paddr1 =
471 ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
472 sc->sc_range[i].child_lo;
473
474 if (offset != paddr1)
475 continue;
476
477 DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n",
478 (unsigned long long)paddr1));
479 return (bus_space_mmap(sc->sc_memtag, paddr1, off,
480 prot, flags));
481 }
482
483 return (-1);
484 }
485
486 /*
487 * install an interrupt handler for a ebus device
488 */
489 void *
490 ebus_intr_establish(bus_space_tag_t t, int pri, int level,
491 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
492 {
493
494 return (bus_intr_establish(t->parent, pri, level, handler, arg));
495 }
496