2 * Copyright (c) 1999, 2000 Matthew R. Green
3 * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
4 * Copyright (c) 2009 by Marius Strobl <marius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Driver for JBus to EBus and PCI to EBus bridges
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
53 #include <machine/bus.h>
55 #include <machine/bus_common.h>
57 #include <machine/intr_machdep.h>
58 #include <machine/resource.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
63 #include <sparc64/pci/ofw_pci.h>
66 * The register, interrupt map and for the PCI variant also the ranges
67 * properties are identical to the ISA ones.
69 #include <sparc64/isa/ofw_isa.h>
71 struct ebus_nexus_ranges {
80 struct ofw_bus_devinfo edi_obdinfo;
81 struct resource_list edi_rl;
87 struct resource *eri_res;
92 struct ebus_rinfo *sc_rinfo;
95 #define EBUS_PCI (1 << 0)
99 struct ofw_bus_iinfo sc_iinfo;
106 static device_probe_t ebus_nexus_probe;
107 static device_attach_t ebus_nexus_attach;
108 static device_probe_t ebus_pci_probe;
109 static device_attach_t ebus_pci_attach;
110 static bus_print_child_t ebus_print_child;
111 static bus_probe_nomatch_t ebus_probe_nomatch;
112 static bus_alloc_resource_t ebus_alloc_resource;
113 static bus_release_resource_t ebus_release_resource;
114 static bus_setup_intr_t ebus_setup_intr;
115 static bus_get_resource_list_t ebus_get_resource_list;
116 static ofw_bus_get_devinfo_t ebus_get_devinfo;
118 static int ebus_attach(device_t dev, struct ebus_softc *sc, phandle_t node);
119 static struct ebus_devinfo *ebus_setup_dinfo(device_t dev,
120 struct ebus_softc *sc, phandle_t node);
121 static void ebus_destroy_dinfo(struct ebus_devinfo *edi);
122 static int ebus_print_res(struct ebus_devinfo *edi);
124 static devclass_t ebus_devclass;
126 static device_method_t ebus_nexus_methods[] = {
127 /* Device interface */
128 DEVMETHOD(device_probe, ebus_nexus_probe),
129 DEVMETHOD(device_attach, ebus_nexus_attach),
130 DEVMETHOD(device_shutdown, bus_generic_shutdown),
131 DEVMETHOD(device_suspend, bus_generic_suspend),
132 DEVMETHOD(device_resume, bus_generic_resume),
135 DEVMETHOD(bus_print_child, ebus_print_child),
136 DEVMETHOD(bus_probe_nomatch, ebus_probe_nomatch),
137 DEVMETHOD(bus_alloc_resource, ebus_alloc_resource),
138 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
139 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
140 DEVMETHOD(bus_release_resource, ebus_release_resource),
141 DEVMETHOD(bus_setup_intr, ebus_setup_intr),
142 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
143 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
144 DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
145 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
147 /* ofw_bus interface */
148 DEVMETHOD(ofw_bus_get_devinfo, ebus_get_devinfo),
149 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
150 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
151 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
152 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
153 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
158 static driver_t ebus_nexus_driver = {
161 sizeof(struct ebus_softc),
165 * NB: we rely on the interrupt controllers of the accompanying PCI-Express
166 * bridge to be registered as the nexus variant of the EBus bridges doesn't
167 * employ its own one.
169 EARLY_DRIVER_MODULE(ebus, nexus, ebus_nexus_driver, ebus_devclass, 0, 0,
171 MODULE_DEPEND(ebus, nexus, 1, 1, 1);
173 static device_method_t ebus_pci_methods[] = {
174 /* Device interface */
175 DEVMETHOD(device_probe, ebus_pci_probe),
176 DEVMETHOD(device_attach, ebus_pci_attach),
177 DEVMETHOD(device_shutdown, bus_generic_shutdown),
178 DEVMETHOD(device_suspend, bus_generic_suspend),
179 DEVMETHOD(device_resume, bus_generic_resume),
182 DEVMETHOD(bus_print_child, ebus_print_child),
183 DEVMETHOD(bus_probe_nomatch, ebus_probe_nomatch),
184 DEVMETHOD(bus_alloc_resource, ebus_alloc_resource),
185 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
186 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
187 DEVMETHOD(bus_release_resource, ebus_release_resource),
188 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
189 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
190 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
191 DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
192 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
194 /* ofw_bus interface */
195 DEVMETHOD(ofw_bus_get_devinfo, ebus_get_devinfo),
196 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
197 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
198 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
199 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
200 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
205 static driver_t ebus_pci_driver = {
208 sizeof(struct ebus_softc),
211 EARLY_DRIVER_MODULE(ebus, pci, ebus_pci_driver, ebus_devclass, 0, 0,
213 MODULE_DEPEND(ebus, pci, 1, 1, 1);
214 MODULE_VERSION(ebus, 1);
217 ebus_nexus_probe(device_t dev)
221 compat = ofw_bus_get_compat(dev);
222 if (compat != NULL && strcmp(ofw_bus_get_name(dev), "ebus") == 0 &&
223 strcmp(compat, "jbus-ebus") == 0) {
224 device_set_desc(dev, "JBus-EBus bridge");
225 return (BUS_PROBE_GENERIC);
231 ebus_pci_probe(device_t dev)
234 if (pci_get_class(dev) != PCIC_BRIDGE ||
235 pci_get_vendor(dev) != 0x108e ||
236 strcmp(ofw_bus_get_name(dev), "ebus") != 0)
239 if (pci_get_device(dev) == 0x1000)
240 device_set_desc(dev, "PCI-EBus2 bridge");
241 else if (pci_get_device(dev) == 0x1100)
242 device_set_desc(dev, "PCI-EBus3 bridge");
245 return (BUS_PROBE_GENERIC);
249 ebus_nexus_attach(device_t dev)
251 struct ebus_softc *sc;
254 sc = device_get_softc(dev);
255 node = ofw_bus_get_node(dev);
258 if (OF_getprop(node, "portid", &sc->sc_ign,
259 sizeof(sc->sc_ign)) == -1) {
260 device_printf(dev, "could not determine IGN");
265 sc->sc_nrange = OF_getprop_alloc(node, "ranges",
266 sizeof(struct ebus_nexus_ranges), &sc->sc_range);
267 if (sc->sc_nrange == -1) {
268 printf("%s: could not get ranges property\n", __func__);
271 return (ebus_attach(dev, sc, node));
275 ebus_pci_attach(device_t dev)
277 struct ebus_softc *sc;
278 struct ebus_rinfo *eri;
279 struct resource *res;
283 sc = device_get_softc(dev);
284 sc->sc_flags |= EBUS_PCI;
286 pci_write_config(dev, PCIR_COMMAND,
287 pci_read_config(dev, PCIR_COMMAND, 2) | PCIM_CMD_SERRESPEN |
288 PCIM_CMD_PERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN, 2);
289 pci_write_config(dev, PCIR_CACHELNSZ, 16 /* 64 bytes */, 1);
290 pci_write_config(dev, PCIR_LATTIMER, 64 /* 64 PCI cycles */, 1);
292 node = ofw_bus_get_node(dev);
293 sc->sc_nrange = OF_getprop_alloc(node, "ranges",
294 sizeof(struct isa_ranges), &sc->sc_range);
295 if (sc->sc_nrange == -1) {
296 printf("%s: could not get ranges property\n", __func__);
300 sc->sc_rinfo = malloc(sizeof(*sc->sc_rinfo) * sc->sc_nrange, M_DEVBUF,
303 /* For every range, there must be a matching resource. */
304 for (rnum = 0; rnum < sc->sc_nrange; rnum++) {
305 eri = &sc->sc_rinfo[rnum];
306 eri->eri_rtype = ofw_isa_range_restype(
307 &((struct isa_ranges *)sc->sc_range)[rnum]);
308 rid = PCIR_BAR(rnum);
309 res = bus_alloc_resource_any(dev, eri->eri_rtype, &rid,
312 printf("%s: failed to allocate range resource!\n",
317 eri->eri_rman.rm_type = RMAN_ARRAY;
318 eri->eri_rman.rm_descr = "EBus range";
319 if (rman_init(&eri->eri_rman) != 0) {
320 printf("%s: failed to initialize rman!", __func__);
323 if (rman_manage_region(&eri->eri_rman, rman_get_start(res),
324 rman_get_end(res)) != 0) {
325 printf("%s: failed to register region!", __func__);
326 rman_fini(&eri->eri_rman);
330 return (ebus_attach(dev, sc, node));
333 for (i = rnum; i >= 0; i--) {
334 eri = &sc->sc_rinfo[i];
336 rman_fini(&eri->eri_rman);
337 if (eri->eri_res != 0) {
338 bus_release_resource(dev, eri->eri_rtype,
339 PCIR_BAR(rnum), eri->eri_res);
342 free(sc->sc_rinfo, M_DEVBUF);
343 free(sc->sc_range, M_OFWPROP);
348 ebus_attach(device_t dev, struct ebus_softc *sc, phandle_t node)
350 struct ebus_devinfo *edi;
353 ofw_bus_setup_iinfo(node, &sc->sc_iinfo, sizeof(ofw_isa_intr_t));
356 * Now attach our children.
358 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
359 if ((edi = ebus_setup_dinfo(dev, sc, node)) == NULL)
361 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
362 device_printf(dev, "<%s>: device_add_child failed\n",
363 edi->edi_obdinfo.obd_name);
364 ebus_destroy_dinfo(edi);
367 device_set_ivars(cdev, edi);
369 return (bus_generic_attach(dev));
373 ebus_print_child(device_t dev, device_t child)
377 retval = bus_print_child_header(dev, child);
378 retval += ebus_print_res(device_get_ivars(child));
379 retval += bus_print_child_footer(dev, child);
384 ebus_probe_nomatch(device_t dev, device_t child)
387 device_printf(dev, "<%s>", ofw_bus_get_name(child));
388 ebus_print_res(device_get_ivars(child));
389 printf(" (no driver attached)\n");
392 static struct resource *
393 ebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
394 u_long start, u_long end, u_long count, u_int flags)
396 struct ebus_softc *sc;
397 struct resource_list *rl;
398 struct resource_list_entry *rle = NULL;
399 struct resource *res;
400 struct ebus_rinfo *ri;
401 struct ebus_nexus_ranges *enr;
403 bus_space_handle_t bh;
404 uint64_t cend, cstart, offset;
405 int i, isdefault, passthrough, ridx, rv;
407 isdefault = (start == 0UL && end == ~0UL);
408 passthrough = (device_get_parent(child) != bus);
409 sc = device_get_softc(bus);
410 rl = BUS_GET_RESOURCE_LIST(bus, child);
413 KASSERT(!(isdefault && passthrough),
414 ("%s: passthrough of default allocation", __func__));
416 rle = resource_list_find(rl, type, *rid);
419 KASSERT(rle->res == NULL,
420 ("%s: resource entry is busy", __func__));
423 count = ulmax(count, rle->count);
424 end = ulmax(rle->end, start + count - 1);
429 if ((sc->sc_flags & EBUS_PCI) != 0) {
431 * Map EBus ranges to PCI ranges. This may include
432 * changing the allocation type.
434 (void)ofw_isa_range_map(sc->sc_range, sc->sc_nrange,
435 &start, &end, &ridx);
436 ri = &sc->sc_rinfo[ridx];
437 res = rman_reserve_resource(&ri->eri_rman, start, end,
438 count, flags, child);
441 rman_set_rid(res, *rid);
442 bt = rman_get_bustag(ri->eri_res);
443 rman_set_bustag(res, bt);
444 rv = bus_space_subregion(bt,
445 rman_get_bushandle(ri->eri_res),
446 rman_get_start(res) - rman_get_start(ri->eri_res),
449 rman_release_resource(res);
452 rman_set_bushandle(res, bh);
454 /* Map EBus ranges to nexus ranges. */
455 for (i = 0; i < sc->sc_nrange; i++) {
456 enr = &((struct ebus_nexus_ranges *)
458 cstart = (((uint64_t)enr->child_hi) << 32) |
460 cend = cstart + enr->size - 1;
461 if (start >= cstart && end <= cend) {
463 (((uint64_t)enr->phys_hi) << 32) |
465 start += offset - cstart;
466 end += offset - cstart;
467 res = bus_generic_alloc_resource(bus,
468 child, type, rid, start, end,
479 return (resource_list_alloc(rl, bus, child, type, rid, start,
486 ebus_release_resource(device_t bus, device_t child, int type, int rid,
487 struct resource *res)
489 struct ebus_softc *sc;
490 struct resource_list *rl;
491 struct resource_list_entry *rle;
494 passthrough = (device_get_parent(child) != bus);
495 rl = BUS_GET_RESOURCE_LIST(bus, child);
498 sc = device_get_softc(bus);
499 if ((sc->sc_flags & EBUS_PCI) == 0)
500 return (resource_list_release(rl, bus, child, type,
502 if ((rv = rman_release_resource(res)) != 0)
505 rle = resource_list_find(rl, type, rid);
507 ("%s: resource entry not found!", __func__));
508 KASSERT(rle->res != NULL,
509 ("%s: resource entry is not busy", __func__));
514 return (resource_list_release(rl, bus, child, type, rid, res));
516 panic("%s: unsupported resource type %d", __func__, type);
522 ebus_setup_intr(device_t dev, device_t child, struct resource *ires,
523 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
527 struct ebus_softc *sc;
530 sc = device_get_softc(dev);
531 if ((sc->sc_flags & EBUS_PCI) == 0) {
533 * Make sure the vector is fully specified. This isn't
534 * necessarily the case with the PCI variant.
536 vec = rman_get_start(ires);
537 if (INTIGN(vec) != sc->sc_ign) {
539 "invalid interrupt vector 0x%lx\n", vec);
544 * As we rely on the interrupt controllers of the
545 * accompanying PCI-Express bridge ensure at least
546 * something is registered for this vector.
548 if (intr_vectors[vec].iv_ic == NULL) {
550 "invalid interrupt controller for vector 0x%lx\n",
556 return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
560 static struct resource_list *
561 ebus_get_resource_list(device_t dev, device_t child)
563 struct ebus_devinfo *edi;
565 edi = device_get_ivars(child);
566 return (&edi->edi_rl);
569 static const struct ofw_bus_devinfo *
570 ebus_get_devinfo(device_t bus, device_t dev)
572 struct ebus_devinfo *edi;
574 edi = device_get_ivars(dev);
575 return (&edi->edi_obdinfo);
578 static struct ebus_devinfo *
579 ebus_setup_dinfo(device_t dev, struct ebus_softc *sc, phandle_t node)
581 struct isa_regs reg, *regs;
582 ofw_isa_intr_t intr, *intrs;
583 struct ebus_devinfo *edi;
586 int i, nintr, nreg, rv;
587 uint8_t maskbuf[sizeof(reg) + sizeof(intr)];
589 edi = malloc(sizeof(*edi), M_DEVBUF, M_ZERO | M_WAITOK);
590 if (ofw_bus_gen_setup_devinfo(&edi->edi_obdinfo, node) != 0) {
594 resource_list_init(&edi->edi_rl);
595 nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), (void **)®s);
597 device_printf(dev, "<%s>: incomplete\n",
598 edi->edi_obdinfo.obd_name);
599 ebus_destroy_dinfo(edi);
602 for (i = 0; i < nreg; i++) {
603 start = ISA_REG_PHYS(regs + i);
604 (void)resource_list_add(&edi->edi_rl, SYS_RES_MEMORY, i,
605 start, start + regs[i].size - 1, regs[i].size);
607 free(regs, M_OFWPROP);
609 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intrs),
613 for (i = 0; i < nintr; i++) {
615 if ((sc->sc_flags & EBUS_PCI) != 0) {
616 rintr = ofw_isa_route_intr(dev, node, &sc->sc_iinfo,
620 rv = ofw_bus_lookup_imap(node, &sc->sc_iinfo, ®,
621 sizeof(reg), &intr, sizeof(intr), &rintr,
622 sizeof(rintr), maskbuf);
625 rintr = INTMAP_VEC(sc->sc_ign, rintr);
628 if ((sc->sc_flags & EBUS_PCI) == 0 ? rv == 0 :
629 rintr == PCI_INVALID_IRQ) {
631 "<%s>: could not map EBus interrupt %d\n",
632 edi->edi_obdinfo.obd_name, intrs[i]);
635 (void)resource_list_add(&edi->edi_rl, SYS_RES_IRQ, i, rintr,
638 free(intrs, M_OFWPROP);
643 ebus_destroy_dinfo(struct ebus_devinfo *edi)
646 resource_list_free(&edi->edi_rl);
647 ofw_bus_gen_destroy_devinfo(&edi->edi_obdinfo);
652 ebus_print_res(struct ebus_devinfo *edi)
657 retval += resource_list_print_type(&edi->edi_rl, "addr", SYS_RES_MEMORY,
659 retval += resource_list_print_type(&edi->edi_rl, "irq", SYS_RES_IRQ,