2 * Copyright (c) 2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #include <sys/kernel.h>
36 #include <dev/ofw/openfirm.h>
37 #include <dev/ofw/ofw_pci.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
44 #include <machine/bus.h>
45 #include <machine/intr_machdep.h>
46 #include <machine/md_var.h>
47 #include <machine/pio.h>
48 #include <machine/resource.h>
55 #include <powerpc/ofw/ofw_pci.h>
62 static int ofw_pci_read_ivar(device_t, device_t, int,
64 static struct resource * ofw_pci_alloc_resource(device_t bus,
65 device_t child, int type, int *rid, u_long start,
66 u_long end, u_long count, u_int flags);
67 static int ofw_pci_release_resource(device_t bus, device_t child,
68 int type, int rid, struct resource *res);
69 static int ofw_pci_activate_resource(device_t bus, device_t child,
70 int type, int rid, struct resource *res);
71 static int ofw_pci_deactivate_resource(device_t bus,
72 device_t child, int type, int rid,
73 struct resource *res);
74 static int ofw_pci_adjust_resource(device_t bus, device_t child,
75 int type, struct resource *res, u_long start,
81 static int ofw_pci_maxslots(device_t);
82 static int ofw_pci_route_interrupt(device_t, device_t, int);
87 static phandle_t ofw_pci_get_node(device_t bus, device_t dev);
93 static int ofw_pci_nranges(phandle_t node);
94 static int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges);
99 static device_method_t ofw_pci_methods[] = {
100 /* Device interface */
101 DEVMETHOD(device_attach, ofw_pci_attach),
104 DEVMETHOD(bus_print_child, bus_generic_print_child),
105 DEVMETHOD(bus_read_ivar, ofw_pci_read_ivar),
106 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
107 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
108 DEVMETHOD(bus_alloc_resource, ofw_pci_alloc_resource),
109 DEVMETHOD(bus_release_resource, ofw_pci_release_resource),
110 DEVMETHOD(bus_activate_resource, ofw_pci_activate_resource),
111 DEVMETHOD(bus_deactivate_resource, ofw_pci_deactivate_resource),
112 DEVMETHOD(bus_adjust_resource, ofw_pci_adjust_resource),
115 DEVMETHOD(pcib_maxslots, ofw_pci_maxslots),
116 DEVMETHOD(pcib_route_interrupt, ofw_pci_route_interrupt),
118 /* ofw_bus interface */
119 DEVMETHOD(ofw_bus_get_node, ofw_pci_get_node),
124 DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
127 ofw_pci_attach(device_t dev)
129 struct ofw_pci_softc *sc;
131 u_int32_t busrange[2];
132 struct ofw_pci_range *rp;
135 node = ofw_bus_get_node(dev);
136 sc = device_get_softc(dev);
138 if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1)
141 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
146 sc->sc_bus = busrange[0];
148 if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
153 for (c = OF_child(node); c != 0; c = OF_peer(c)) {
154 n = ofw_pci_nranges(c);
158 if (sc->sc_nrange == 0)
160 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
163 for (c = OF_child(node); c != 0; c = OF_peer(c)) {
164 n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
168 KASSERT(i == sc->sc_nrange, ("range count mismatch"));
170 sc->sc_nrange = ofw_pci_nranges(node);
171 if (sc->sc_nrange <= 0) {
172 device_printf(dev, "could not get ranges\n");
175 sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
177 ofw_pci_fill_ranges(node, sc->sc_range);
180 sc->sc_io_rman.rm_type = RMAN_ARRAY;
181 sc->sc_io_rman.rm_descr = "PCI I/O Ports";
182 error = rman_init(&sc->sc_io_rman);
184 device_printf(dev, "rman_init() failed. error = %d\n", error);
188 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
189 sc->sc_mem_rman.rm_descr = "PCI Memory";
190 error = rman_init(&sc->sc_mem_rman);
192 device_printf(dev, "rman_init() failed. error = %d\n", error);
196 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
197 rp->pci_hi != 0; rp++) {
200 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
201 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
203 case OFW_PCI_PHYS_HI_SPACE_IO:
204 error = rman_manage_region(&sc->sc_io_rman, rp->pci,
205 rp->pci + rp->size - 1);
207 case OFW_PCI_PHYS_HI_SPACE_MEM32:
208 case OFW_PCI_PHYS_HI_SPACE_MEM64:
209 error = rman_manage_region(&sc->sc_mem_rman, rp->pci,
210 rp->pci + rp->size - 1);
216 "rman_manage_region(%x, %#jx, %#jx) failed. "
217 "error = %d\n", rp->pci_hi &
218 OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
219 rp->pci + rp->size - 1, error);
225 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
227 device_add_child(dev, "pci", device_get_unit(dev));
228 return (bus_generic_attach(dev));
232 ofw_pci_maxslots(device_t dev)
235 return (PCI_SLOTMAX);
239 ofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
241 struct ofw_pci_softc *sc;
242 struct ofw_pci_register reg;
243 uint32_t pintr, mintr;
245 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
247 sc = device_get_softc(bus);
249 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®,
250 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
252 return (MAP_IRQ(iparent, mintr));
254 /* Maybe it's a real interrupt, not an intpin */
258 device_printf(bus, "could not route pin %d for device %d.%d\n",
259 pin, pci_get_slot(dev), pci_get_function(dev));
260 return (PCI_INVALID_IRQ);
264 ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
266 struct ofw_pci_softc *sc;
268 sc = device_get_softc(dev);
271 case PCIB_IVAR_DOMAIN:
272 *result = device_get_unit(dev);
275 *result = sc->sc_bus;
282 static struct resource *
283 ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
284 u_long start, u_long end, u_long count, u_int flags)
286 struct ofw_pci_softc *sc;
291 needactivate = flags & RF_ACTIVE;
294 sc = device_get_softc(bus);
298 rm = &sc->sc_mem_rman;
302 rm = &sc->sc_io_rman;
306 return (bus_alloc_resource(bus, type, rid, start, end, count,
310 device_printf(bus, "unknown resource request from %s\n",
311 device_get_nameunit(child));
315 rv = rman_reserve_resource(rm, start, end, count, flags, child);
317 device_printf(bus, "failed to reserve resource for %s\n",
318 device_get_nameunit(child));
322 rman_set_rid(rv, *rid);
325 if (bus_activate_resource(child, type, *rid, rv) != 0) {
327 "failed to activate resource for %s\n",
328 device_get_nameunit(child));
329 rman_release_resource(rv);
338 ofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
339 struct resource *res)
341 if (rman_get_flags(res) & RF_ACTIVE) {
342 int error = bus_deactivate_resource(child, type, rid, res);
347 return (rman_release_resource(res));
351 ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
352 struct resource *res)
354 struct ofw_pci_softc *sc;
357 sc = device_get_softc(bus);
359 if (type == SYS_RES_IRQ) {
360 return (bus_activate_resource(bus, type, rid, res));
362 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
363 struct ofw_pci_range *rp;
367 start = (vm_offset_t)rman_get_start(res);
370 * Map this through the ranges list
372 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
373 rp->pci_hi != 0; rp++) {
374 if (start < rp->pci || start >= rp->pci + rp->size)
377 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
378 case OFW_PCI_PHYS_HI_SPACE_IO:
379 space = SYS_RES_IOPORT;
381 case OFW_PCI_PHYS_HI_SPACE_MEM32:
382 case OFW_PCI_PHYS_HI_SPACE_MEM64:
383 space = SYS_RES_MEMORY;
390 start += (rp->host - rp->pci);
396 printf("ofw_pci mapdev: start %zx, len %ld\n", start,
399 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
403 rman_set_virtual(res, p);
404 rman_set_bustag(res, &bs_le_tag);
405 rman_set_bushandle(res, (u_long)p);
408 return (rman_activate_resource(res));
412 ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
413 struct resource *res)
416 * If this is a memory resource, unmap it.
418 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
421 psize = rman_get_size(res);
422 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
425 return (rman_deactivate_resource(res));
429 ofw_pci_adjust_resource(device_t bus, device_t child, int type,
430 struct resource *res, u_long start, u_long end)
432 struct rman *rm = NULL;
433 struct ofw_pci_softc *sc = device_get_softc(bus);
435 KASSERT(!(rman_get_flags(res) & RF_ACTIVE),
436 ("active resources cannot be adjusted"));
437 if (rman_get_flags(res) & RF_ACTIVE)
442 rm = &sc->sc_mem_rman;
445 rm = &sc->sc_io_rman;
451 if (!rman_is_region_manager(res, rm))
454 return (rman_adjust_resource(res, start, end));
459 ofw_pci_get_node(device_t bus, device_t dev)
461 struct ofw_pci_softc *sc;
463 sc = device_get_softc(bus);
464 /* We only have one child, the PCI bus, which needs our own node. */
466 return (sc->sc_node);
470 ofw_pci_nranges(phandle_t node)
472 int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
473 ssize_t nbase_ranges;
475 OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
476 sizeof(host_address_cells));
477 OF_getprop(node, "#address-cells", &pci_address_cells,
478 sizeof(pci_address_cells));
479 OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
481 nbase_ranges = OF_getproplen(node, "ranges");
482 if (nbase_ranges <= 0)
485 return (nbase_ranges / sizeof(cell_t) /
486 (pci_address_cells + host_address_cells + size_cells));
490 ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
492 int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
494 ssize_t nbase_ranges;
498 OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
499 sizeof(host_address_cells));
500 OF_getprop(node, "#address-cells", &pci_address_cells,
501 sizeof(pci_address_cells));
502 OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
504 nbase_ranges = OF_getproplen(node, "ranges");
505 if (nbase_ranges <= 0)
507 nranges = nbase_ranges / sizeof(cell_t) /
508 (pci_address_cells + host_address_cells + size_cells);
510 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
511 OF_getprop(node, "ranges", base_ranges, nbase_ranges);
513 for (i = 0, j = 0; i < nranges; i++) {
514 ranges[i].pci_hi = base_ranges[j++];
516 for (k = 0; k < pci_address_cells - 1; k++) {
517 ranges[i].pci <<= 32;
518 ranges[i].pci |= base_ranges[j++];
521 for (k = 0; k < host_address_cells; k++) {
522 ranges[i].host <<= 32;
523 ranges[i].host |= base_ranges[j++];
526 for (k = 0; k < size_cells; k++) {
527 ranges[i].size <<= 32;
528 ranges[i].size |= base_ranges[j++];
532 free(base_ranges, M_DEVBUF);