ebus.c revision 1.4 1 /* $NetBSD: ebus.c,v 1.4 2000/04/05 12:30:42 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1999 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_bustag = pa->pa_memt;
118 sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
119 sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
120
121 node = ebus_find_node(sc, pa);
122 if (node == 0)
123 panic("could not find ebus node");
124
125 sc->sc_node = node;
126
127 /*
128 * fill in our softc with information from the prom
129 */
130 sc->sc_intmap = NULL;
131 sc->sc_range = NULL;
132 rv = getprop(node, "interrupt-map", sizeof(struct ebus_interrupt_map),
133 &sc->sc_nintmap, (void **)&sc->sc_intmap);
134 if (rv)
135 panic("could not get ebus interrupt-map");
136
137 immp = &sc->sc_intmapmask;
138 rv = getprop(node, "interrupt-map-mask",
139 sizeof(struct ebus_interrupt_map_mask), &nmapmask,
140 (void **)&immp);
141 if (rv)
142 panic("could not get ebus interrupt-map-mask");
143 if (nmapmask != 1)
144 panic("ebus interrupt-map-mask is broken");
145
146 rv = getprop(node, "ranges", sizeof(struct ebus_ranges),
147 &sc->sc_nrange, (void **)&sc->sc_range);
148 if (rv)
149 panic("could not get ebus ranges");
150
151 /*
152 * now attach all our children
153 */
154 DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
155 for (node = firstchild(node); node; node = nextsibling(node)) {
156 char *name = getpropstring(node, "name");
157
158 if (ebus_setup_attach_args(sc, node, &eba) != 0) {
159 printf("ebus_attach: %s: incomplete\n", name);
160 continue;
161 }
162 DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", eba.ea_name));
163 (void)config_found(self, &eba, ebus_print);
164 ebus_destroy_attach_args(&eba);
165 }
166 }
167
168 int
169 ebus_setup_attach_args(sc, node, ea)
170 struct ebus_softc *sc;
171 int node;
172 struct ebus_attach_args *ea;
173 {
174 int n, rv;
175
176 bzero(ea, sizeof(struct ebus_attach_args));
177 rv = getprop(node, "name", 1, &n, (void **)&ea->ea_name);
178 if (rv != 0)
179 return (rv);
180 ea->ea_name[n] = '\0';
181
182 ea->ea_node = node;
183 ea->ea_bustag = sc->sc_childbustag;
184 ea->ea_dmatag = sc->sc_dmatag;
185
186 rv = getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nregs,
187 (void **)&ea->ea_regs);
188 if (rv)
189 return (rv);
190
191 rv = getprop(node, "address", sizeof(u_int32_t), &ea->ea_naddrs,
192 (void **)&ea->ea_vaddrs);
193 if (rv != ENOENT) {
194 if (rv)
195 return (rv);
196
197 if (ea->ea_nregs != ea->ea_naddrs)
198 printf("ebus loses: device %s: %d regs and %d addrs\n",
199 ea->ea_name, ea->ea_nregs, ea->ea_naddrs);
200 } else
201 ea->ea_naddrs = 0;
202
203 if (getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintrs,
204 (void **)&ea->ea_intrs))
205 ea->ea_nintrs = 0;
206 else
207 ebus_find_ino(sc, ea);
208
209 return (0);
210 }
211
212 void
213 ebus_destroy_attach_args(ea)
214 struct ebus_attach_args *ea;
215 {
216
217 if (ea->ea_name)
218 free((void *)ea->ea_name, M_DEVBUF);
219 if (ea->ea_regs)
220 free((void *)ea->ea_regs, M_DEVBUF);
221 if (ea->ea_intrs)
222 free((void *)ea->ea_intrs, M_DEVBUF);
223 if (ea->ea_vaddrs)
224 free((void *)ea->ea_vaddrs, M_DEVBUF);
225 }
226
227 int
228 ebus_print(aux, p)
229 void *aux;
230 const char *p;
231 {
232 struct ebus_attach_args *ea = aux;
233 int i;
234
235 if (p)
236 printf("%s at %s", ea->ea_name, p);
237 for (i = 0; i < ea->ea_nregs; i++)
238 printf(" addr %x-%x", ea->ea_regs[i].lo,
239 ea->ea_regs[i].lo + ea->ea_regs[i].size - 1);
240 for (i = 0; i < ea->ea_nintrs; i++)
241 printf(" ipl %d", ea->ea_intrs[i]);
242 return (UNCONF);
243 }
244
245 /*
246 * find the INO values for each interrupt and fill them in.
247 *
248 * for each "reg" property of this device, mask it's hi and lo
249 * values with the "interrupt-map-mask"'s hi/lo values, and also
250 * mask the interrupt number with the interrupt mask. search the
251 * "interrupt-map" list for matching values of hi, lo and interrupt
252 * to give the INO for this interrupt.
253 */
254 void
255 ebus_find_ino(sc, ea)
256 struct ebus_softc *sc;
257 struct ebus_attach_args *ea;
258 {
259 u_int32_t hi, lo, intr;
260 int i, j, k;
261
262 DPRINTF(EDB_INTRMAP, ("ebus_find_ino: searching %d interrupts", ea->ea_nintrs));
263 for (j = 0; j < ea->ea_nintrs; j++) {
264 intr = ea->ea_intrs[j] & sc->sc_intmapmask.intr;
265
266 DPRINTF(EDB_INTRMAP, ("; intr %x masked to %x", ea->ea_intrs[j], intr));
267 for (i = 0; i < ea->ea_nregs; i++) {
268 hi = ea->ea_regs[i].hi & sc->sc_intmapmask.hi;
269 lo = ea->ea_regs[i].lo & sc->sc_intmapmask.lo;
270
271 DPRINTF(EDB_INTRMAP, ("; reg hi.lo %08x.08x masked to %08x.%08x", ea->ea_regs[i].hi, ea->ea_regs[i].lo, hi, lo));
272 for (k = 0; k < sc->sc_nintmap; k++) {
273 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));
274 if (hi == sc->sc_intmap[k].hi &&
275 lo == sc->sc_intmap[k].lo &&
276 intr == sc->sc_intmap[k].intr) {
277 ea->ea_intrs[j] = sc->sc_intmap[k].cintr;
278 DPRINTF(EDB_INTRMAP, ("; FOUND IT! changing to %d\n", sc->sc_intmap[k].cintr));
279 goto next_intr;
280 }
281 }
282 }
283 next_intr:
284 }
285 }
286
287 /*
288 * what is our OFW node?
289 */
290 int
291 ebus_find_node(sc, pa)
292 struct ebus_softc *sc;
293 struct pci_attach_args *pa;
294 {
295 int node = pa->pa_pc->node;
296 int n, *ap;
297 int pcibus, bus, dev, fn;
298
299 DPRINTF(EDB_PROM, ("ebus_find_node: looking at pci node %08x\n", node));
300 for (node = firstchild(node); node; node = nextsibling(node)) {
301 char *name = getpropstring(node, "name");
302
303 DPRINTF(EDB_PROM, ("ebus_find_node: looking at PCI device `%s', node = %08x\n", name, node));
304 /* must be "ebus" */
305 if (strcmp(name, "ebus") != 0)
306 continue;
307
308 /* pull the PCI bus out of the pa_tag */
309 pcibus = (pa->pa_tag >> 16) & 0xff;
310 DPRINTF(EDB_PROM, ("; pcibus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
311
312 /* get the PCI bus/device/function for this node */
313 ap = NULL;
314 if (getprop(node, "reg", sizeof(int), &n,
315 (void **)&ap))
316 continue;
317
318 bus = (ap[0] >> 16) & 0xff;
319 dev = (ap[0] >> 11) & 0x1f;
320 fn = (ap[0] >> 8) & 0x7;
321
322 DPRINTF(EDB_PROM, ("; looking for bus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
323 if (pa->pa_device != dev ||
324 pa->pa_function != fn ||
325 pcibus != bus)
326 continue;
327
328 DPRINTF(EDB_PROM, ("; found it, returning %08x\n", node));
329 /* found it! */
330 free(ap, M_DEVBUF);
331 return (node);
332 }
333
334 /* damn! */
335 return (0);
336 }
337