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