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