ebus.c revision 1.55 1 /* $NetBSD: ebus.c,v 1.55 2011/03/11 18:55:26 nakayama 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.55 2011/03/11 18:55:26 nakayama 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 <dev/ebus/ebusreg.h>
74 #include <dev/ebus/ebusvar.h>
75
76 struct ebus_softc {
77 struct device sc_dev;
78
79 int sc_node;
80
81 bus_space_tag_t sc_memtag; /* from pci */
82 bus_space_tag_t sc_iotag; /* from pci */
83 bus_space_tag_t sc_childbustag; /* pass to children */
84 bus_dma_tag_t sc_dmatag;
85
86 struct ebus_ranges *sc_range;
87 struct ebus_interrupt_map *sc_intmap;
88 struct ebus_interrupt_map_mask sc_intmapmask;
89
90 int sc_nrange; /* counters */
91 int sc_nintmap;
92 };
93
94 int ebus_match(struct device *, struct cfdata *, void *);
95 void ebus_attach(struct device *, struct device *, void *);
96
97 CFATTACH_DECL(ebus, sizeof(struct ebus_softc),
98 ebus_match, ebus_attach, NULL, NULL);
99
100 bus_space_tag_t ebus_alloc_bus_tag(struct ebus_softc *, int);
101
102 int ebus_setup_attach_args(struct ebus_softc *, int,
103 struct ebus_attach_args *);
104 void ebus_destroy_attach_args(struct ebus_attach_args *);
105 int ebus_print(void *, const char *);
106 void ebus_find_ino(struct ebus_softc *, struct ebus_attach_args *);
107
108 /*
109 * here are our bus space and bus DMA routines.
110 */
111 static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
112 static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, vaddr_t,
113 bus_space_handle_t *);
114 static void *ebus_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
115 void *, void(*)(void));
116
117 int
118 ebus_match(struct device *parent, struct cfdata *match, void *aux)
119 {
120 struct pci_attach_args *pa = aux;
121 char *name;
122 int node;
123
124 /* Only attach if there's a PROM node. */
125 node = PCITAG_NODE(pa->pa_tag);
126 if (node == -1)
127 return (0);
128
129 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
130 return (0);
131
132 /* Match a real ebus */
133 name = prom_getpropstring(node, "name");
134 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
135 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
136 strcmp(name, "ebus") == 0)
137 return (1);
138
139 /* Or a real ebus III */
140 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
141 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII &&
142 strcmp(name, "ebus") == 0)
143 return (1);
144
145 /* Or a PCI-ISA bridge XXX I hope this is on-board. */
146 if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
147 return (1);
148 }
149
150 return (0);
151 }
152
153 /*
154 * attach an ebus and all it's children. this code is modeled
155 * after the sbus code which does similar things.
156 */
157 void
158 ebus_attach(struct device *parent, struct device *self, void *aux)
159 {
160 struct ebus_softc *sc = (struct ebus_softc *)self;
161 struct pci_attach_args *pa = aux;
162 struct ebus_attach_args eba;
163 struct ebus_interrupt_map_mask *immp;
164 int node, nmapmask, error;
165 char devinfo[256];
166
167 aprint_normal("\n");
168 aprint_naive("\n");
169
170 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
171 aprint_normal_dev(self, "%s, revision 0x%02x\n", devinfo,
172 PCI_REVISION(pa->pa_class));
173
174 sc->sc_memtag = pa->pa_memt;
175 sc->sc_iotag = pa->pa_iot;
176 sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
177 sc->sc_dmatag = pa->pa_dmat;
178
179 node = PCITAG_NODE(pa->pa_tag);
180 if (node == -1)
181 panic("could not find ebus node");
182
183 sc->sc_node = node;
184
185 /*
186 * fill in our softc with information from the prom
187 */
188 sc->sc_intmap = NULL;
189 sc->sc_range = NULL;
190 sc->sc_nintmap = 0;
191 error = prom_getprop(node, "interrupt-map",
192 sizeof(struct ebus_interrupt_map),
193 &sc->sc_nintmap, &sc->sc_intmap);
194 switch (error) {
195 case 0:
196 immp = &sc->sc_intmapmask;
197 nmapmask = sizeof(*immp);
198 error = prom_getprop(node, "interrupt-map-mask",
199 sizeof(struct ebus_interrupt_map_mask), &nmapmask,
200 &immp);
201 if (error)
202 panic("could not get ebus interrupt-map-mask, error %d",
203 error);
204 if (nmapmask != 1)
205 panic("ebus interrupt-map-mask is broken");
206 break;
207 case ENOENT:
208 break;
209 default:
210 panic("ebus interrupt-map: error %d", error);
211 break;
212 }
213
214 error = prom_getprop(node, "ranges", sizeof(struct ebus_ranges),
215 &sc->sc_nrange, &sc->sc_range);
216 if (error)
217 panic("ebus ranges: error %d", error);
218
219 /*
220 * now attach all our children
221 */
222 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
223 for (node = firstchild(node); node; node = nextsibling(node)) {
224 char *name = prom_getpropstring(node, "name");
225
226 if (ebus_setup_attach_args(sc, node, &eba) != 0) {
227 aprint_error("ebus_attach: %s: incomplete\n", name);
228 continue;
229 } else {
230 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n",
231 eba.ea_name));
232 (void)config_found(self, &eba, ebus_print);
233 }
234 ebus_destroy_attach_args(&eba);
235 }
236 }
237
238 int ebus_setup_attach_args(struct ebus_softc *, int,
239 struct ebus_attach_args *);
240 int
241 ebus_setup_attach_args(struct ebus_softc *sc, int node,
242 struct ebus_attach_args *ea)
243 {
244 int n, rv;
245
246 memset(ea, 0, sizeof(struct ebus_attach_args));
247 n = 0;
248 rv = prom_getprop(node, "name", 1, &n, &ea->ea_name);
249 if (rv != 0)
250 return (rv);
251 ea->ea_name[n] = '\0';
252
253 ea->ea_node = node;
254 ea->ea_bustag = sc->sc_childbustag;
255 ea->ea_dmatag = sc->sc_dmatag;
256
257 rv = prom_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg,
258 &ea->ea_reg);
259 if (rv)
260 return (rv);
261
262 rv = prom_getprop(node, "address", sizeof(uint32_t), &ea->ea_nvaddr,
263 &ea->ea_vaddr);
264 if (rv != ENOENT) {
265 if (rv)
266 return (rv);
267
268 if (ea->ea_nreg != ea->ea_nvaddr)
269 printf("ebus loses: device %s: %d regs and %d addrs\n",
270 ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
271 } else
272 ea->ea_nvaddr = 0;
273
274 if (prom_getprop(node, "interrupts", sizeof(uint32_t), &ea->ea_nintr,
275 &ea->ea_intr))
276 ea->ea_nintr = 0;
277 else
278 ebus_find_ino(sc, ea);
279
280 return (0);
281 }
282
283 void
284 ebus_destroy_attach_args(struct ebus_attach_args *ea)
285 {
286
287 if (ea->ea_name)
288 free((void *)ea->ea_name, M_DEVBUF);
289 if (ea->ea_reg)
290 free((void *)ea->ea_reg, M_DEVBUF);
291 if (ea->ea_intr)
292 free((void *)ea->ea_intr, M_DEVBUF);
293 if (ea->ea_vaddr)
294 free((void *)ea->ea_vaddr, M_DEVBUF);
295 }
296
297 int
298 ebus_print(void *aux, const char *p)
299 {
300 struct ebus_attach_args *ea = aux;
301 int i;
302
303 if (p)
304 aprint_normal("%s at %s", ea->ea_name, p);
305 for (i = 0; i < ea->ea_nreg; i++)
306 aprint_normal("%s %x-%x", i == 0 ? " addr" : ",",
307 ea->ea_reg[i].lo,
308 ea->ea_reg[i].lo + ea->ea_reg[i].size - 1);
309 for (i = 0; i < ea->ea_nintr; i++)
310 aprint_normal(" ipl %d", ea->ea_intr[i]);
311 return (UNCONF);
312 }
313
314
315 /*
316 * find the INO values for each interrupt and fill them in.
317 *
318 * for each "reg" property of this device, mask it's hi and lo
319 * values with the "interrupt-map-mask"'s hi/lo values, and also
320 * mask the interrupt number with the interrupt mask. search the
321 * "interrupt-map" list for matching values of hi, lo and interrupt
322 * to give the INO for this interrupt.
323 */
324 void
325 ebus_find_ino(struct ebus_softc *sc, struct ebus_attach_args *ea)
326 {
327 uint32_t hi, lo, intr;
328 int i, j, k;
329
330 if (sc->sc_nintmap == 0) {
331 for (i = 0; i < ea->ea_nintr; i++) {
332 OF_mapintr(ea->ea_node, &ea->ea_intr[i],
333 sizeof(ea->ea_intr[0]),
334 sizeof(ea->ea_intr[0]));
335 }
336 return;
337 }
338
339 DPRINTF(EDB_INTRMAP,
340 ("ebus_find_ino: searching %d interrupts", ea->ea_nintr));
341
342 for (j = 0; j < ea->ea_nintr; j++) {
343
344 intr = ea->ea_intr[j] & sc->sc_intmapmask.intr;
345
346 DPRINTF(EDB_INTRMAP,
347 ("; intr %x masked to %x", ea->ea_intr[j], intr));
348 for (i = 0; i < ea->ea_nreg; i++) {
349 hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi;
350 lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo;
351
352 DPRINTF(EDB_INTRMAP,
353 ("; reg hi.lo %08x.%08x masked to %08x.%08x",
354 ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo));
355 for (k = 0; k < sc->sc_nintmap; k++) {
356 DPRINTF(EDB_INTRMAP,
357 ("; checking hi.lo %08x.%08x intr %x",
358 sc->sc_intmap[k].hi, sc->sc_intmap[k].lo,
359 sc->sc_intmap[k].intr));
360 if (hi == sc->sc_intmap[k].hi &&
361 lo == sc->sc_intmap[k].lo &&
362 intr == sc->sc_intmap[k].intr) {
363 ea->ea_intr[j] =
364 sc->sc_intmap[k].cintr;
365 DPRINTF(EDB_INTRMAP,
366 ("; FOUND IT! changing to %d\n",
367 sc->sc_intmap[k].cintr));
368 goto next_intr;
369 }
370 }
371 }
372 next_intr:;
373 }
374 }
375
376 /*
377 * bus space support. <sparc64/dev/psychoreg.h> has a discussion
378 * about PCI physical addresses, which also applies to ebus.
379 */
380 bus_space_tag_t
381 ebus_alloc_bus_tag(struct ebus_softc *sc, int type)
382 {
383 bus_space_tag_t bt;
384
385 bt = (bus_space_tag_t)
386 malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
387 if (bt == NULL)
388 panic("could not allocate ebus bus tag");
389
390 memset(bt, 0, sizeof *bt);
391 bt->cookie = sc;
392 bt->parent = sc->sc_memtag;
393 bt->type = type;
394 bt->sparc_bus_map = _ebus_bus_map;
395 bt->sparc_bus_mmap = ebus_bus_mmap;
396 bt->sparc_intr_establish = ebus_intr_establish;
397 return (bt);
398 }
399
400 static int
401 _ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
402 vaddr_t va, bus_space_handle_t *hp)
403 {
404 struct ebus_softc *sc = t->cookie;
405 paddr_t offset;
406 u_int bar;
407 int i, ss;
408
409 bar = BUS_ADDR_IOSPACE(ba);
410 offset = BUS_ADDR_PADDR(ba);
411
412 DPRINTF(EDB_BUSMAP,
413 ("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
414 (int)bar, (uint32_t)offset, (uint32_t)size,
415 flags, (void *)va));
416
417 for (i = 0; i < sc->sc_nrange; i++) {
418 bus_addr_t pciaddr;
419
420 if (bar != sc->sc_range[i].child_hi)
421 continue;
422 if (offset < sc->sc_range[i].child_lo ||
423 (offset + size) >
424 (sc->sc_range[i].child_lo + sc->sc_range[i].size))
425 continue;
426
427 /* Isolate address space and find the right tag */
428 ss = (sc->sc_range[i].phys_hi>>24)&3;
429 switch (ss) {
430 case 1: /* I/O space */
431 t = sc->sc_iotag;
432 break;
433 case 2: /* Memory space */
434 t = sc->sc_memtag;
435 break;
436 case 0: /* Config space */
437 case 3: /* 64-bit Memory space */
438 default: /* WTF? */
439 /* We don't handle these */
440 panic("_ebus_bus_map: illegal space %x", ss);
441 break;
442 }
443 pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
444 sc->sc_range[i].phys_lo;
445 pciaddr += offset;
446
447 DPRINTF(EDB_BUSMAP,
448 ("_ebus_bus_map: mapping to PCI addr %x\n",
449 (uint32_t)pciaddr));
450
451 /* pass it onto the psycho */
452 return (bus_space_map(t, pciaddr, size, flags, hp));
453 }
454 DPRINTF(EDB_BUSMAP, (": FAILED\n"));
455 return (EINVAL);
456 }
457
458 static paddr_t
459 ebus_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
460 int flags)
461 {
462 bus_addr_t offset = paddr;
463 struct ebus_softc *sc = t->cookie;
464 int i;
465
466 for (i = 0; i < sc->sc_nrange; i++) {
467 bus_addr_t paddr1 =
468 ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
469 sc->sc_range[i].child_lo;
470
471 if (offset != paddr1)
472 continue;
473
474 DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n",
475 (unsigned long long)paddr1));
476 return (bus_space_mmap(sc->sc_memtag, paddr1, off,
477 prot, flags));
478 }
479
480 return (-1);
481 }
482
483 /*
484 * install an interrupt handler for a ebus device
485 */
486 void *
487 ebus_intr_establish(bus_space_tag_t t, int pri, int level,
488 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
489 {
490
491 return (bus_intr_establish(t->parent, pri, level, handler, arg));
492 }
493