2 * Copyright (c) 1999, 2000 Matthew R. Green
3 * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * UltraSPARC 5 and beyond EBus support
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
46 #include <machine/bus.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
54 #include <machine/ofw_bus.h>
55 #include <machine/resource.h>
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
60 #include <sparc64/pci/ofw_pci.h>
63 * The register, ranges and interrupt map properties are identical to the ISA
66 #include <sparc64/isa/ofw_isa.h>
69 struct ofw_bus_devinfo edi_obdinfo;
70 struct resource_list edi_rl;
76 struct resource *eri_res;
80 struct isa_ranges *sc_range;
81 struct ebus_rinfo *sc_rinfo;
85 struct ofw_bus_iinfo sc_iinfo;
88 static device_probe_t ebus_probe;
89 static device_attach_t ebus_attach;
90 static bus_print_child_t ebus_print_child;
91 static bus_probe_nomatch_t ebus_probe_nomatch;
92 static bus_alloc_resource_t ebus_alloc_resource;
93 static bus_release_resource_t ebus_release_resource;
94 static bus_get_resource_list_t ebus_get_resource_list;
95 static ofw_bus_get_devinfo_t ebus_get_devinfo;
97 static struct ebus_devinfo *ebus_setup_dinfo(device_t, struct ebus_softc *,
99 static void ebus_destroy_dinfo(struct ebus_devinfo *);
100 static int ebus_print_res(struct ebus_devinfo *);
102 static device_method_t ebus_methods[] = {
103 /* Device interface */
104 DEVMETHOD(device_probe, ebus_probe),
105 DEVMETHOD(device_attach, ebus_attach),
106 DEVMETHOD(device_shutdown, bus_generic_shutdown),
107 DEVMETHOD(device_suspend, bus_generic_suspend),
108 DEVMETHOD(device_resume, bus_generic_resume),
111 DEVMETHOD(bus_print_child, ebus_print_child),
112 DEVMETHOD(bus_probe_nomatch, ebus_probe_nomatch),
113 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
114 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
115 DEVMETHOD(bus_alloc_resource, ebus_alloc_resource),
116 DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
117 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
118 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
119 DEVMETHOD(bus_release_resource, ebus_release_resource),
120 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
122 /* ofw_bus interface */
123 DEVMETHOD(ofw_bus_get_devinfo, ebus_get_devinfo),
124 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
125 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
126 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
127 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
128 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
133 static driver_t ebus_driver = {
136 sizeof(struct ebus_softc),
139 static devclass_t ebus_devclass;
141 DRIVER_MODULE(ebus, pci, ebus_driver, ebus_devclass, 0, 0);
142 MODULE_DEPEND(ebus, pci, 1, 1, 1);
143 MODULE_VERSION(ebus, 1);
146 ebus_probe(device_t dev)
149 if (pci_get_class(dev) != PCIC_BRIDGE ||
150 pci_get_vendor(dev) != 0x108e ||
151 strcmp(ofw_bus_get_name(dev), "ebus") != 0)
154 if (pci_get_device(dev) == 0x1000)
155 device_set_desc(dev, "PCI-EBus2 bridge");
156 else if (pci_get_device(dev) == 0x1100)
157 device_set_desc(dev, "PCI-EBus3 bridge");
164 ebus_attach(device_t dev)
166 struct ebus_softc *sc;
167 struct ebus_devinfo *edi;
168 struct ebus_rinfo *eri;
169 struct resource *res;
174 sc = device_get_softc(dev);
176 node = ofw_bus_get_node(dev);
177 sc->sc_nrange = OF_getprop_alloc(node, "ranges",
178 sizeof(*sc->sc_range), (void **)&sc->sc_range);
179 if (sc->sc_nrange == -1) {
180 printf("ebus_attach: could not get ranges property\n");
184 sc->sc_rinfo = malloc(sizeof(*sc->sc_rinfo) * sc->sc_nrange, M_DEVBUF,
187 /* For every range, there must be a matching resource. */
188 for (rnum = 0; rnum < sc->sc_nrange; rnum++) {
189 eri = &sc->sc_rinfo[rnum];
190 eri->eri_rtype = ofw_isa_range_restype(&sc->sc_range[rnum]);
191 rid = PCIR_BAR(rnum);
192 res = bus_alloc_resource_any(dev, eri->eri_rtype, &rid,
195 printf("ebus_attach: failed to allocate range "
200 eri->eri_rman.rm_type = RMAN_ARRAY;
201 eri->eri_rman.rm_descr = "EBus range";
202 if (rman_init(&eri->eri_rman) != 0) {
203 printf("ebus_attach: failed to initialize rman!");
206 if (rman_manage_region(&eri->eri_rman, rman_get_start(res),
207 rman_get_end(res)) != 0) {
208 printf("ebus_attach: failed to register region!");
209 rman_fini(&eri->eri_rman);
214 ofw_bus_setup_iinfo(node, &sc->sc_iinfo, sizeof(ofw_isa_intr_t));
217 * Now attach our children.
219 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
220 if ((edi = ebus_setup_dinfo(dev, sc, node)) == NULL)
222 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
223 device_printf(dev, "<%s>: device_add_child failed\n",
224 edi->edi_obdinfo.obd_name);
225 ebus_destroy_dinfo(edi);
228 device_set_ivars(cdev, edi);
230 return (bus_generic_attach(dev));
233 for (i = rnum; i >= 0; i--) {
234 eri = &sc->sc_rinfo[i];
236 rman_fini(&eri->eri_rman);
237 if (eri->eri_res != 0) {
238 bus_release_resource(dev, eri->eri_rtype,
239 PCIR_BAR(rnum), eri->eri_res);
242 free(sc->sc_rinfo, M_DEVBUF);
243 free(sc->sc_range, M_OFWPROP);
248 ebus_print_child(device_t dev, device_t child)
252 retval = bus_print_child_header(dev, child);
253 retval += ebus_print_res(device_get_ivars(child));
254 retval += bus_print_child_footer(dev, child);
259 ebus_probe_nomatch(device_t dev, device_t child)
262 device_printf(dev, "<%s>", ofw_bus_get_name(child));
263 ebus_print_res(device_get_ivars(child));
264 printf(" (no driver attached)\n");
267 static struct resource *
268 ebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
269 u_long start, u_long end, u_long count, u_int flags)
271 struct ebus_softc *sc;
272 struct resource_list *rl;
273 struct resource_list_entry *rle = NULL;
274 struct resource *res;
275 struct ebus_rinfo *ri;
277 bus_space_handle_t bh;
278 int passthrough = (device_get_parent(child) != bus);
279 int isdefault = (start == 0UL && end == ~0UL);
282 sc = (struct ebus_softc *)device_get_softc(bus);
283 rl = BUS_GET_RESOURCE_LIST(bus, child);
285 * Map EBus ranges to PCI ranges. This may include changing the
290 KASSERT(!(isdefault && passthrough),
291 ("ebus_alloc_resource: passthrough of default alloc"));
293 rle = resource_list_find(rl, type, *rid);
296 KASSERT(rle->res == NULL,
297 ("ebus_alloc_resource: resource entry is busy"));
300 count = ulmax(count, rle->count);
301 end = ulmax(rle->end, start + count - 1);
305 (void)ofw_isa_range_map(sc->sc_range, sc->sc_nrange,
306 &start, &end, &ridx);
308 ri = &sc->sc_rinfo[ridx];
309 res = rman_reserve_resource(&ri->eri_rman, start, end, count,
313 rman_set_rid(res, *rid);
314 bt = rman_get_bustag(ri->eri_res);
315 rman_set_bustag(res, bt);
316 rv = bus_space_subregion(bt, rman_get_bushandle(ri->eri_res),
317 rman_get_start(res) - rman_get_start(ri->eri_res), count,
320 rman_release_resource(res);
323 rman_set_bushandle(res, bh);
328 return (resource_list_alloc(rl, bus, child, type, rid, start,
335 ebus_release_resource(device_t bus, device_t child, int type, int rid,
336 struct resource *res)
338 struct resource_list *rl;
339 struct resource_list_entry *rle;
340 int passthrough = (device_get_parent(child) != bus);
343 rl = BUS_GET_RESOURCE_LIST(bus, child);
346 if ((rv = rman_release_resource(res)) != 0)
349 rle = resource_list_find(rl, type, rid);
350 KASSERT(rle != NULL, ("ebus_release_resource: "
351 "resource entry not found!"));
352 KASSERT(rle->res != NULL, ("ebus_alloc_resource: "
353 "resource entry is not busy"));
358 return (resource_list_release(rl, bus, child, type, rid, res));
360 panic("ebus_release_resource: unsupported resource type %d",
366 static struct resource_list *
367 ebus_get_resource_list(device_t dev, device_t child)
369 struct ebus_devinfo *edi;
371 edi = device_get_ivars(child);
372 return (&edi->edi_rl);
375 static const struct ofw_bus_devinfo *
376 ebus_get_devinfo(device_t bus, device_t dev)
378 struct ebus_devinfo *edi;
380 edi = device_get_ivars(dev);
381 return (&edi->edi_obdinfo);
384 static struct ebus_devinfo *
385 ebus_setup_dinfo(device_t dev, struct ebus_softc *sc, phandle_t node)
387 struct ebus_devinfo *edi;
388 struct isa_regs *reg;
389 ofw_isa_intr_t *intrs;
390 ofw_pci_intr_t rintr;
394 edi = malloc(sizeof(*edi), M_DEVBUF, M_ZERO | M_WAITOK);
395 if (ofw_bus_gen_setup_devinfo(&edi->edi_obdinfo, node) != 0) {
399 resource_list_init(&edi->edi_rl);
400 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
402 device_printf(dev, "<%s>: incomplete\n",
403 edi->edi_obdinfo.obd_name);
406 for (i = 0; i < nreg; i++) {
407 start = ISA_REG_PHYS(reg + i);
408 resource_list_add(&edi->edi_rl, SYS_RES_MEMORY, i,
409 start, start + reg[i].size - 1, reg[i].size);
411 free(reg, M_OFWPROP);
413 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
415 for (i = 0; i < nintr; i++) {
416 rintr = ofw_isa_route_intr(dev, node, &sc->sc_iinfo, intrs[i]);
417 if (rintr == PCI_INVALID_IRQ) {
419 "<%s>: could not map EBus interrupt %d\n",
420 edi->edi_obdinfo.obd_name, intrs[i]);
421 free(intrs, M_OFWPROP);
424 resource_list_add(&edi->edi_rl, SYS_RES_IRQ, i,
427 free(intrs, M_OFWPROP);
432 ebus_destroy_dinfo(edi);
437 ebus_destroy_dinfo(struct ebus_devinfo *edi)
440 resource_list_free(&edi->edi_rl);
441 ofw_bus_gen_destroy_devinfo(&edi->edi_obdinfo);
446 ebus_print_res(struct ebus_devinfo *edi)
451 retval += resource_list_print_type(&edi->edi_rl, "addr", SYS_RES_MEMORY,
453 retval += resource_list_print_type(&edi->edi_rl, "irq", SYS_RES_IRQ,