ebus.c revision 1.6 1 1.6 mrg /* $NetBSD: ebus.c,v 1.6 2000/04/13 14:39:34 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.5 mrg * Copyright (c) 1999, 2000 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * UltraSPARC 5 and beyond ebus support.
33 1.1 mrg */
34 1.1 mrg
35 1.1 mrg #undef DEBUG
36 1.1 mrg #define DEBUG
37 1.1 mrg
38 1.1 mrg #ifdef DEBUG
39 1.1 mrg #define EDB_PROM 0x1
40 1.1 mrg #define EDB_CHILD 0x2
41 1.3 mrg #define EDB_INTRMAP 0x4
42 1.1 mrg int ebus_debug = 0;
43 1.1 mrg #define DPRINTF(l, s) do { if (ebus_debug & l) printf s; } while (0)
44 1.1 mrg #else
45 1.1 mrg #define DPRINTF(l, s)
46 1.1 mrg #endif
47 1.1 mrg
48 1.1 mrg #include <sys/param.h>
49 1.1 mrg #include <sys/conf.h>
50 1.1 mrg #include <sys/device.h>
51 1.1 mrg #include <sys/malloc.h>
52 1.1 mrg #include <sys/systm.h>
53 1.1 mrg
54 1.1 mrg #define _SPARC_BUS_DMA_PRIVATE
55 1.1 mrg #include <machine/bus.h>
56 1.1 mrg #include <machine/autoconf.h>
57 1.1 mrg
58 1.1 mrg #include <dev/pci/pcivar.h>
59 1.1 mrg #include <dev/pci/pcireg.h>
60 1.1 mrg #include <dev/pci/pcidevs.h>
61 1.1 mrg
62 1.1 mrg #include <sparc64/dev/ebusreg.h>
63 1.1 mrg #include <sparc64/dev/ebusvar.h>
64 1.1 mrg
65 1.1 mrg int ebus_match __P((struct device *, struct cfdata *, void *));
66 1.1 mrg void ebus_attach __P((struct device *, struct device *, void *));
67 1.1 mrg
68 1.1 mrg struct cfattach ebus_ca = {
69 1.1 mrg sizeof(struct device), ebus_match, ebus_attach
70 1.1 mrg };
71 1.1 mrg
72 1.1 mrg int ebus_setup_attach_args __P((struct ebus_softc *, int,
73 1.1 mrg struct ebus_attach_args *));
74 1.1 mrg void ebus_destroy_attach_args __P((struct ebus_attach_args *));
75 1.1 mrg int ebus_print __P((void *, const char *));
76 1.3 mrg void ebus_find_ino __P((struct ebus_softc *, struct ebus_attach_args *));
77 1.1 mrg int ebus_find_node __P((struct ebus_softc *, struct pci_attach_args *));
78 1.1 mrg
79 1.1 mrg int
80 1.1 mrg ebus_match(parent, match, aux)
81 1.1 mrg struct device *parent;
82 1.1 mrg struct cfdata *match;
83 1.1 mrg void *aux;
84 1.1 mrg {
85 1.1 mrg struct pci_attach_args *pa = aux;
86 1.1 mrg
87 1.1 mrg if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
88 1.1 mrg PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
89 1.1 mrg PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS)
90 1.1 mrg return (1);
91 1.1 mrg
92 1.1 mrg return (0);
93 1.1 mrg }
94 1.1 mrg
95 1.1 mrg /*
96 1.1 mrg * attach an ebus and all it's children. this code is modeled
97 1.1 mrg * after the sbus code which does similar things.
98 1.1 mrg */
99 1.1 mrg void
100 1.1 mrg ebus_attach(parent, self, aux)
101 1.1 mrg struct device *parent, *self;
102 1.1 mrg void *aux;
103 1.1 mrg {
104 1.1 mrg struct ebus_softc *sc = (struct ebus_softc *)self;
105 1.1 mrg struct pci_attach_args *pa = aux;
106 1.1 mrg struct ebus_attach_args eba;
107 1.1 mrg struct ebus_interrupt_map_mask *immp;
108 1.1 mrg int node, nmapmask, rv;
109 1.1 mrg char devinfo[256];
110 1.1 mrg
111 1.1 mrg printf("\n");
112 1.1 mrg
113 1.1 mrg pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
114 1.1 mrg printf("%s: %s, revision 0x%02x\n", self->dv_xname, devinfo,
115 1.1 mrg PCI_REVISION(pa->pa_class));
116 1.1 mrg
117 1.5 mrg sc->sc_parent = (struct psycho_softc *)parent;
118 1.1 mrg sc->sc_bustag = pa->pa_memt;
119 1.1 mrg sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
120 1.1 mrg sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
121 1.1 mrg
122 1.1 mrg node = ebus_find_node(sc, pa);
123 1.1 mrg if (node == 0)
124 1.1 mrg panic("could not find ebus node");
125 1.1 mrg
126 1.1 mrg sc->sc_node = node;
127 1.1 mrg
128 1.1 mrg /*
129 1.1 mrg * fill in our softc with information from the prom
130 1.1 mrg */
131 1.3 mrg sc->sc_intmap = NULL;
132 1.3 mrg sc->sc_range = NULL;
133 1.1 mrg rv = getprop(node, "interrupt-map", sizeof(struct ebus_interrupt_map),
134 1.1 mrg &sc->sc_nintmap, (void **)&sc->sc_intmap);
135 1.1 mrg if (rv)
136 1.1 mrg panic("could not get ebus interrupt-map");
137 1.1 mrg
138 1.1 mrg immp = &sc->sc_intmapmask;
139 1.1 mrg rv = getprop(node, "interrupt-map-mask",
140 1.1 mrg sizeof(struct ebus_interrupt_map_mask), &nmapmask,
141 1.1 mrg (void **)&immp);
142 1.1 mrg if (rv)
143 1.1 mrg panic("could not get ebus interrupt-map-mask");
144 1.1 mrg if (nmapmask != 1)
145 1.1 mrg panic("ebus interrupt-map-mask is broken");
146 1.1 mrg
147 1.1 mrg rv = getprop(node, "ranges", sizeof(struct ebus_ranges),
148 1.1 mrg &sc->sc_nrange, (void **)&sc->sc_range);
149 1.1 mrg if (rv)
150 1.1 mrg panic("could not get ebus ranges");
151 1.1 mrg
152 1.1 mrg /*
153 1.1 mrg * now attach all our children
154 1.1 mrg */
155 1.1 mrg DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
156 1.1 mrg for (node = firstchild(node); node; node = nextsibling(node)) {
157 1.1 mrg char *name = getpropstring(node, "name");
158 1.1 mrg
159 1.1 mrg if (ebus_setup_attach_args(sc, node, &eba) != 0) {
160 1.1 mrg printf("ebus_attach: %s: incomplete\n", name);
161 1.1 mrg continue;
162 1.6 mrg } else {
163 1.6 mrg DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n", eba.ea_name));
164 1.6 mrg (void)config_found(self, &eba, ebus_print);
165 1.1 mrg }
166 1.1 mrg ebus_destroy_attach_args(&eba);
167 1.1 mrg }
168 1.1 mrg }
169 1.1 mrg
170 1.1 mrg int
171 1.1 mrg ebus_setup_attach_args(sc, node, ea)
172 1.1 mrg struct ebus_softc *sc;
173 1.1 mrg int node;
174 1.1 mrg struct ebus_attach_args *ea;
175 1.1 mrg {
176 1.1 mrg int n, rv;
177 1.1 mrg
178 1.1 mrg bzero(ea, sizeof(struct ebus_attach_args));
179 1.1 mrg rv = getprop(node, "name", 1, &n, (void **)&ea->ea_name);
180 1.1 mrg if (rv != 0)
181 1.1 mrg return (rv);
182 1.1 mrg ea->ea_name[n] = '\0';
183 1.1 mrg
184 1.1 mrg ea->ea_node = node;
185 1.1 mrg ea->ea_bustag = sc->sc_childbustag;
186 1.1 mrg ea->ea_dmatag = sc->sc_dmatag;
187 1.1 mrg
188 1.1 mrg rv = getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nregs,
189 1.1 mrg (void **)&ea->ea_regs);
190 1.1 mrg if (rv)
191 1.1 mrg return (rv);
192 1.1 mrg
193 1.1 mrg rv = getprop(node, "address", sizeof(u_int32_t), &ea->ea_naddrs,
194 1.1 mrg (void **)&ea->ea_vaddrs);
195 1.1 mrg if (rv != ENOENT) {
196 1.1 mrg if (rv)
197 1.1 mrg return (rv);
198 1.1 mrg
199 1.1 mrg if (ea->ea_nregs != ea->ea_naddrs)
200 1.1 mrg printf("ebus loses: device %s: %d regs and %d addrs\n",
201 1.1 mrg ea->ea_name, ea->ea_nregs, ea->ea_naddrs);
202 1.3 mrg } else
203 1.3 mrg ea->ea_naddrs = 0;
204 1.1 mrg
205 1.3 mrg if (getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintrs,
206 1.3 mrg (void **)&ea->ea_intrs))
207 1.3 mrg ea->ea_nintrs = 0;
208 1.3 mrg else
209 1.3 mrg ebus_find_ino(sc, ea);
210 1.1 mrg
211 1.1 mrg return (0);
212 1.1 mrg }
213 1.1 mrg
214 1.1 mrg void
215 1.1 mrg ebus_destroy_attach_args(ea)
216 1.1 mrg struct ebus_attach_args *ea;
217 1.1 mrg {
218 1.1 mrg
219 1.1 mrg if (ea->ea_name)
220 1.1 mrg free((void *)ea->ea_name, M_DEVBUF);
221 1.1 mrg if (ea->ea_regs)
222 1.1 mrg free((void *)ea->ea_regs, M_DEVBUF);
223 1.1 mrg if (ea->ea_intrs)
224 1.1 mrg free((void *)ea->ea_intrs, M_DEVBUF);
225 1.1 mrg if (ea->ea_vaddrs)
226 1.1 mrg free((void *)ea->ea_vaddrs, M_DEVBUF);
227 1.1 mrg }
228 1.1 mrg
229 1.1 mrg int
230 1.1 mrg ebus_print(aux, p)
231 1.1 mrg void *aux;
232 1.1 mrg const char *p;
233 1.1 mrg {
234 1.1 mrg struct ebus_attach_args *ea = aux;
235 1.4 mrg int i;
236 1.1 mrg
237 1.1 mrg if (p)
238 1.1 mrg printf("%s at %s", ea->ea_name, p);
239 1.4 mrg for (i = 0; i < ea->ea_nregs; i++)
240 1.4 mrg printf(" addr %x-%x", ea->ea_regs[i].lo,
241 1.4 mrg ea->ea_regs[i].lo + ea->ea_regs[i].size - 1);
242 1.4 mrg for (i = 0; i < ea->ea_nintrs; i++)
243 1.4 mrg printf(" ipl %d", ea->ea_intrs[i]);
244 1.1 mrg return (UNCONF);
245 1.3 mrg }
246 1.3 mrg
247 1.3 mrg /*
248 1.3 mrg * find the INO values for each interrupt and fill them in.
249 1.3 mrg *
250 1.3 mrg * for each "reg" property of this device, mask it's hi and lo
251 1.3 mrg * values with the "interrupt-map-mask"'s hi/lo values, and also
252 1.3 mrg * mask the interrupt number with the interrupt mask. search the
253 1.3 mrg * "interrupt-map" list for matching values of hi, lo and interrupt
254 1.3 mrg * to give the INO for this interrupt.
255 1.3 mrg */
256 1.3 mrg void
257 1.3 mrg ebus_find_ino(sc, ea)
258 1.3 mrg struct ebus_softc *sc;
259 1.3 mrg struct ebus_attach_args *ea;
260 1.3 mrg {
261 1.3 mrg u_int32_t hi, lo, intr;
262 1.3 mrg int i, j, k;
263 1.3 mrg
264 1.3 mrg DPRINTF(EDB_INTRMAP, ("ebus_find_ino: searching %d interrupts", ea->ea_nintrs));
265 1.3 mrg for (j = 0; j < ea->ea_nintrs; j++) {
266 1.3 mrg intr = ea->ea_intrs[j] & sc->sc_intmapmask.intr;
267 1.3 mrg
268 1.3 mrg DPRINTF(EDB_INTRMAP, ("; intr %x masked to %x", ea->ea_intrs[j], intr));
269 1.3 mrg for (i = 0; i < ea->ea_nregs; i++) {
270 1.3 mrg hi = ea->ea_regs[i].hi & sc->sc_intmapmask.hi;
271 1.3 mrg lo = ea->ea_regs[i].lo & sc->sc_intmapmask.lo;
272 1.3 mrg
273 1.3 mrg 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 1.3 mrg for (k = 0; k < sc->sc_nintmap; k++) {
275 1.3 mrg 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 1.3 mrg if (hi == sc->sc_intmap[k].hi &&
277 1.3 mrg lo == sc->sc_intmap[k].lo &&
278 1.3 mrg intr == sc->sc_intmap[k].intr) {
279 1.3 mrg ea->ea_intrs[j] = sc->sc_intmap[k].cintr;
280 1.3 mrg DPRINTF(EDB_INTRMAP, ("; FOUND IT! changing to %d\n", sc->sc_intmap[k].cintr));
281 1.3 mrg goto next_intr;
282 1.3 mrg }
283 1.3 mrg }
284 1.3 mrg }
285 1.3 mrg next_intr:
286 1.3 mrg }
287 1.1 mrg }
288 1.1 mrg
289 1.1 mrg /*
290 1.1 mrg * what is our OFW node?
291 1.1 mrg */
292 1.1 mrg int
293 1.1 mrg ebus_find_node(sc, pa)
294 1.1 mrg struct ebus_softc *sc;
295 1.1 mrg struct pci_attach_args *pa;
296 1.1 mrg {
297 1.1 mrg int node = pa->pa_pc->node;
298 1.1 mrg int n, *ap;
299 1.1 mrg int pcibus, bus, dev, fn;
300 1.1 mrg
301 1.1 mrg DPRINTF(EDB_PROM, ("ebus_find_node: looking at pci node %08x\n", node));
302 1.1 mrg for (node = firstchild(node); node; node = nextsibling(node)) {
303 1.1 mrg char *name = getpropstring(node, "name");
304 1.1 mrg
305 1.1 mrg DPRINTF(EDB_PROM, ("ebus_find_node: looking at PCI device `%s', node = %08x\n", name, node));
306 1.1 mrg /* must be "ebus" */
307 1.1 mrg if (strcmp(name, "ebus") != 0)
308 1.1 mrg continue;
309 1.1 mrg
310 1.1 mrg /* pull the PCI bus out of the pa_tag */
311 1.1 mrg pcibus = (pa->pa_tag >> 16) & 0xff;
312 1.1 mrg DPRINTF(EDB_PROM, ("; pcibus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
313 1.1 mrg
314 1.1 mrg /* get the PCI bus/device/function for this node */
315 1.2 mrg ap = NULL;
316 1.1 mrg if (getprop(node, "reg", sizeof(int), &n,
317 1.1 mrg (void **)&ap))
318 1.1 mrg continue;
319 1.1 mrg
320 1.1 mrg bus = (ap[0] >> 16) & 0xff;
321 1.1 mrg dev = (ap[0] >> 11) & 0x1f;
322 1.1 mrg fn = (ap[0] >> 8) & 0x7;
323 1.1 mrg
324 1.1 mrg DPRINTF(EDB_PROM, ("; looking for bus %d dev %d fn %d\n", pcibus, pa->pa_device, pa->pa_function));
325 1.1 mrg if (pa->pa_device != dev ||
326 1.1 mrg pa->pa_function != fn ||
327 1.1 mrg pcibus != bus)
328 1.1 mrg continue;
329 1.1 mrg
330 1.1 mrg DPRINTF(EDB_PROM, ("; found it, returning %08x\n", node));
331 1.1 mrg /* found it! */
332 1.1 mrg free(ap, M_DEVBUF);
333 1.1 mrg return (node);
334 1.1 mrg }
335 1.1 mrg
336 1.1 mrg /* damn! */
337 1.1 mrg return (0);
338 1.1 mrg }
339