ebus.c revision 1.6 1 /* $NetBSD: ebus.c,v 1.6 2000/04/13 14:39:34 mrg 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 * UltraSPARC 5 and beyond ebus support.
33 */
34
35 #undef DEBUG
36 #define DEBUG
37
38 #ifdef DEBUG
39 #define EDB_PROM 0x1
40 #define EDB_CHILD 0x2
41 #define EDB_INTRMAP 0x4
42 int ebus_debug = 0;
43 #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0)
44 #else
45 #define DPRINTF(l, s)
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/conf.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52 #include <sys/systm.h>
53
54 #define _SPARC_BUS_DMA_PRIVATE
55 #include <machine/bus.h>
56 #include <machine/autoconf.h>
57
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcidevs.h>
61
62 #include <sparc64/dev/ebusreg.h>
63 #include <sparc64/dev/ebusvar.h>
64
65 int ebus_match __P((struct device *, struct cfdata *, void *));
66 void ebus_attach __P((struct device *, struct device *, void *));
67
68 struct cfattach ebus_ca = {
69 sizeof(struct device), ebus_match, ebus_attach
70 };
71
72 int ebus_setup_attach_args __P((struct ebus_softc *, int,
73 struct ebus_attach_args *));
74 void ebus_destroy_attach_args __P((struct ebus_attach_args *));
75 int ebus_print __P((void *, const char *));
76 void ebus_find_ino __P((struct ebus_softc *, struct ebus_attach_args *));
77 int ebus_find_node __P((struct ebus_softc *, struct pci_attach_args *));
78
79 int
80 ebus_match(parent, match, aux)
81 struct device *parent;
82 struct cfdata *match;
83 void *aux;
84 {
85 struct pci_attach_args *pa = aux;
86
87 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
88 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
89 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS)
90 return (1);
91
92 return (0);
93 }
94
95 /*
96 * attach an ebus and all it's children. this code is modeled
97 * after the sbus code which does similar things.
98 */
99 void
100 ebus_attach(parent, self, aux)
101 struct device *parent, *self;
102 void *aux;
103 {
104 struct ebus_softc *sc = (struct ebus_softc *)self;
105 struct pci_attach_args *pa = aux;
106 struct ebus_attach_args eba;
107 struct ebus_interrupt_map_mask *immp;
108 int node, nmapmask, rv;
109 char devinfo[256];
110
111 printf("\n");
112
113 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
114 printf("%s: %s, revision 0x%02x\n", self->dv_xname, devinfo,
115 PCI_REVISION(pa->pa_class));
116
117 sc->sc_parent = (struct psycho_softc *)parent;
118 sc->sc_bustag = pa->pa_memt;
119 sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
120 sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
121
122 node = ebus_find_node(sc, pa);
123 if (node == 0)
124 panic("could not find ebus node");
125
126 sc->sc_node = node;
127
128 /*
129 * fill in our softc with information from the prom
130 */
131 sc->sc_intmap = NULL;
132 sc->sc_range = NULL;
133 rv = getprop(node, "interrupt-map", sizeof(struct ebus_interrupt_map),
134 &sc->sc_nintmap, (void **)&sc->sc_intmap);
135 if (rv)
136 panic("could not get ebus interrupt-map");
137
138 immp = &sc->sc_intmapmask;
139 rv = getprop(node, "interrupt-map-mask",
140 sizeof(struct ebus_interrupt_map_mask), &nmapmask,
141 (void **)&immp);
142 if (rv)
143 panic("could not get ebus interrupt-map-mask");
144 if (nmapmask != 1)
145 panic("ebus interrupt-map-mask is broken");
146
147 rv = getprop(node, "ranges", sizeof(struct ebus_ranges),
148 &sc->sc_nrange, (void **)&sc->sc_range);
149 if (rv)
150 panic("could not get ebus ranges");
151
152 /*
153 * now attach all our children
154 */
155 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
156 for (node = firstchild(node); node; node = nextsibling(node)) {
157 char *name = getpropstring(node, "name");
158
159 if (ebus_setup_attach_args(sc, node, &eba) != 0) {
160 printf("ebus_attach: %s: incomplete\n", name);
161 continue;
162 } else {
163 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", eba.ea_name));
164 (void)config_found(self, &eba, ebus_print);
165 }
166 ebus_destroy_attach_args(&eba);
167 }
168 }
169
170 int
171 ebus_setup_attach_args(sc, node, ea)
172 struct ebus_softc *sc;
173 int node;
174 struct ebus_attach_args *ea;
175 {
176 int n, rv;
177
178 bzero(ea, sizeof(struct ebus_attach_args));
179 rv = getprop(node, "name", 1, &n, (void **)&ea->ea_name);
180 if (rv != 0)
181 return (rv);
182 ea->ea_name[n] = '\0';
183
184 ea->ea_node = node;
185 ea->ea_bustag = sc->sc_childbustag;
186 ea->ea_dmatag = sc->sc_dmatag;
187
188 rv = getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nregs,
189 (void **)&ea->ea_regs);
190 if (rv)
191 return (rv);
192
193 rv = getprop(node, "address", sizeof(u_int32_t), &ea->ea_naddrs,
194 (void **)&ea->ea_vaddrs);
195 if (rv != ENOENT) {
196 if (rv)
197 return (rv);
198
199 if (ea->ea_nregs != ea->ea_naddrs)
200 printf("ebus loses: device %s: %d regs and %d addrs\n",
201 ea->ea_name, ea->ea_nregs, ea->ea_naddrs);
202 } else
203 ea->ea_naddrs = 0;
204
205 if (getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintrs,
206 (void **)&ea->ea_intrs))
207 ea->ea_nintrs = 0;
208 else
209 ebus_find_ino(sc, ea);
210
211 return (0);
212 }
213
214 void
215 ebus_destroy_attach_args(ea)
216 struct ebus_attach_args *ea;
217 {
218
219 if (ea->ea_name)
220 free((void *)ea->ea_name, M_DEVBUF);
221 if (ea->ea_regs)
222 free((void *)ea->ea_regs, M_DEVBUF);
223 if (ea->ea_intrs)
224 free((void *)ea->ea_intrs, M_DEVBUF);
225 if (ea->ea_vaddrs)
226 free((void *)ea->ea_vaddrs, M_DEVBUF);
227 }
228
229 int
230 ebus_print(aux, p)
231 void *aux;
232 const char *p;
233 {
234 struct ebus_attach_args *ea = aux;
235 int i;
236
237 if (p)
238 printf("%s at %s", ea->ea_name, p);
239 for (i = 0; i < ea->ea_nregs; i++)
240 printf(" addr %x-%x", ea->ea_regs[i].lo,
241 ea->ea_regs[i].lo + ea->ea_regs[i].size - 1);
242 for (i = 0; i < ea->ea_nintrs; i++)
243 printf(" ipl %d", ea->ea_intrs[i]);
244 return (UNCONF);
245 }
246
247 /*
248 * find the INO values for each interrupt and fill them in.
249 *
250 * for each "reg" property of this device, mask it's hi and lo
251 * values with the "interrupt-map-mask"'s hi/lo values, and also
252 * mask the interrupt number with the interrupt mask. search the
253 * "interrupt-map" list for matching values of hi, lo and interrupt
254 * to give the INO for this interrupt.
255 */
256 void
257 ebus_find_ino(sc, ea)
258 struct ebus_softc *sc;
259 struct ebus_attach_args *ea;
260 {
261 u_int32_t hi, lo, intr;
262 int i, j, k;
263
264 DPRINTF(EDB_INTRMAP, ("ebus_find_ino: searching %d interrupts", ea->ea_nintrs));
265 for (j = 0; j < ea->ea_nintrs; j++) {
266 intr = ea->ea_intrs[j] & sc->sc_intmapmask.intr;
267
268 DPRINTF(EDB_INTRMAP, ("; intr %x masked to %x", ea->ea_intrs[j], intr));
269 for (i = 0; i < ea->ea_nregs; i++) {
270 hi = ea->ea_regs[i].hi & sc->sc_intmapmask.hi;
271 lo = ea->ea_regs[i].lo & sc->sc_intmapmask.lo;
272
273 DPRINTF(EDB_INTRMAP, ("; reg hi.lo %08x.08x masked to %08x.%08x", ea->ea_regs[i].hi, ea->ea_regs[i].lo, hi, lo));
274 for (k = 0; k < sc->sc_nintmap; k++) {
275 DPRINTF(EDB_INTRMAP, ("; checking hi.lo %08x.%08x intr %x", sc->sc_intmap[k].hi, sc->sc_intmap[k].lo, sc->sc_intmap[k].intr));
276 if (hi == sc->sc_intmap[k].hi &&
277 lo == sc->sc_intmap[k].lo &&
278 intr == sc->sc_intmap[k].intr) {
279 ea->ea_intrs[j] = sc->sc_intmap[k].cintr;
280 DPRINTF(EDB_INTRMAP, ("; FOUND IT! changing to %d\n", sc->sc_intmap[k].cintr));
281 goto next_intr;
282 }
283 }
284 }
285 next_intr:
286 }
287 }
288
289 /*
290 * what is our OFW node?
291 */
292 int
293 ebus_find_node(sc, pa)
294 struct ebus_softc *sc;
295 struct pci_attach_args *pa;
296 {
297 int node = pa->pa_pc->node;
298 int n, *ap;
299 int pcibus, bus, dev, fn;
300
301 DPRINTF(EDB_PROM, ("ebus_find_node: looking at pci node %08x\n", node));
302 for (node = firstchild(node); node; node = nextsibling(node)) {
303 char *name = getpropstring(node, "name");
304
305 DPRINTF(EDB_PROM, ("ebus_find_node: looking at PCI device `%s', node = %08x\n", name, node));
306 /* must be "ebus" */
307 if (strcmp(name, "ebus") != 0)
308 continue;
309
310 /* pull the PCI bus out of the pa_tag */
311 pcibus = (pa->pa_tag >> 16) & 0xff;
312 DPRINTF(EDB_PROM, ("; pcibus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
313
314 /* get the PCI bus/device/function for this node */
315 ap = NULL;
316 if (getprop(node, "reg", sizeof(int), &n,
317 (void **)&ap))
318 continue;
319
320 bus = (ap[0] >> 16) & 0xff;
321 dev = (ap[0] >> 11) & 0x1f;
322 fn = (ap[0] >> 8) & 0x7;
323
324 DPRINTF(EDB_PROM, ("; looking for bus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
325 if (pa->pa_device != dev ||
326 pa->pa_function != fn ||
327 pcibus != bus)
328 continue;
329
330 DPRINTF(EDB_PROM, ("; found it, returning %08x\n", node));
331 /* found it! */
332 free(ap, M_DEVBUF);
333 return (node);
334 }
335
336 /* damn! */
337 return (0);
338 }
339