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