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