2 * Copyright 2003 by Peter Grehan. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
35 #include <sys/kernel.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
48 #include <machine/pio.h>
49 #include <machine/resource.h>
53 #include <powerpc/powermac/gracklevar.h>
60 int badaddr(void *, size_t); /* XXX */
65 static int grackle_probe(device_t);
66 static int grackle_attach(device_t);
71 static int grackle_read_ivar(device_t, device_t, int,
73 static struct resource * grackle_alloc_resource(device_t bus,
74 device_t child, int type, int *rid, u_long start,
75 u_long end, u_long count, u_int flags);
76 static int grackle_release_resource(device_t bus, device_t child,
77 int type, int rid, struct resource *res);
78 static int grackle_activate_resource(device_t bus, device_t child,
79 int type, int rid, struct resource *res);
80 static int grackle_deactivate_resource(device_t bus,
81 device_t child, int type, int rid,
82 struct resource *res);
88 static int grackle_maxslots(device_t);
89 static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int,
91 static void grackle_write_config(device_t, u_int, u_int, u_int,
92 u_int, u_int32_t, int);
93 static int grackle_route_interrupt(device_t, device_t, int);
98 static phandle_t grackle_get_node(device_t bus, device_t dev);
103 static int grackle_enable_config(struct grackle_softc *, u_int,
104 u_int, u_int, u_int);
105 static void grackle_disable_config(struct grackle_softc *);
110 static device_method_t grackle_methods[] = {
111 /* Device interface */
112 DEVMETHOD(device_probe, grackle_probe),
113 DEVMETHOD(device_attach, grackle_attach),
116 DEVMETHOD(bus_print_child, bus_generic_print_child),
117 DEVMETHOD(bus_read_ivar, grackle_read_ivar),
118 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
119 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
120 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource),
121 DEVMETHOD(bus_release_resource, grackle_release_resource),
122 DEVMETHOD(bus_activate_resource, grackle_activate_resource),
123 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource),
126 DEVMETHOD(pcib_maxslots, grackle_maxslots),
127 DEVMETHOD(pcib_read_config, grackle_read_config),
128 DEVMETHOD(pcib_write_config, grackle_write_config),
129 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt),
131 /* ofw_bus interface */
132 DEVMETHOD(ofw_bus_get_node, grackle_get_node),
137 static driver_t grackle_driver = {
140 sizeof(struct grackle_softc)
143 static devclass_t grackle_devclass;
145 DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0);
148 grackle_probe(device_t dev)
150 const char *type, *compatible;
152 type = ofw_bus_get_type(dev);
153 compatible = ofw_bus_get_compat(dev);
155 if (type == NULL || compatible == NULL)
158 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
161 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
166 grackle_attach(device_t dev)
168 struct grackle_softc *sc;
170 u_int32_t busrange[2];
171 struct grackle_range *rp, *io, *mem[2];
174 node = ofw_bus_get_node(dev);
175 sc = device_get_softc(dev);
177 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
182 sc->sc_bus = busrange[0];
185 * The Grackle PCI config addr/data registers are actually in
186 * PCI space, but since they are needed to actually probe the
187 * PCI bus, use the fact that they are also available directly
188 * on the processor bus and map them
190 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
191 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
193 bzero(sc->sc_range, sizeof(sc->sc_range));
194 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
195 sizeof(sc->sc_range));
197 if (sc->sc_nrange == -1) {
198 device_printf(dev, "could not get ranges\n");
202 sc->sc_nrange /= sizeof(sc->sc_range[0]);
204 sc->sc_range[6].pci_hi = 0;
208 for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
209 rp->pci_hi != 0; rp++) {
210 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
211 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
213 case OFW_PCI_PHYS_HI_SPACE_IO:
216 case OFW_PCI_PHYS_HI_SPACE_MEM32:
220 case OFW_PCI_PHYS_HI_SPACE_MEM64:
226 device_printf(dev, "can't find io range\n");
229 sc->sc_io_rman.rm_type = RMAN_ARRAY;
230 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports";
231 sc->sc_iostart = io->pci_iospace;
232 if (rman_init(&sc->sc_io_rman) != 0 ||
233 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
234 io->pci_lo + io->size_lo) != 0) {
235 panic("grackle_attach: failed to set up I/O rman");
239 device_printf(dev, "can't find mem ranges\n");
242 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
243 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory";
244 error = rman_init(&sc->sc_mem_rman);
246 device_printf(dev, "rman_init() failed. error = %d\n", error);
249 for (i = 0; i < nmem; i++) {
250 error = rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
251 mem[i]->pci_lo + mem[i]->size_lo);
254 "rman_manage_region() failed. error = %d\n", error);
259 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
261 device_add_child(dev, "pci", device_get_unit(dev));
262 return (bus_generic_attach(dev));
266 grackle_maxslots(device_t dev)
269 return (PCI_SLOTMAX);
273 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
276 struct grackle_softc *sc;
278 u_int32_t retval = 0xffffffff;
280 sc = device_get_softc(dev);
281 caoff = sc->sc_data + (reg & 0x03);
283 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
286 * Config probes to non-existent devices on the
287 * secondary bus generates machine checks. Be sure
291 if (badaddr((void *)sc->sc_data, 4)) {
298 retval = (in8rb(caoff));
301 retval = (in16rb(caoff));
304 retval = (in32rb(caoff));
308 grackle_disable_config(sc);
314 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
315 u_int reg, u_int32_t val, int width)
317 struct grackle_softc *sc;
320 sc = device_get_softc(dev);
321 caoff = sc->sc_data + (reg & 0x03);
323 if (grackle_enable_config(sc, bus, slot, func, reg)) {
339 grackle_disable_config(sc);
343 grackle_route_interrupt(device_t bus, device_t dev, int pin)
345 struct grackle_softc *sc;
346 struct ofw_pci_register reg;
347 uint32_t pintr, mintr;
349 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
351 sc = device_get_softc(bus);
353 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®,
354 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
356 return (MAP_IRQ(iparent, mintr));
358 /* Maybe it's a real interrupt, not an intpin */
362 device_printf(bus, "could not route pin %d for device %d.%d\n",
363 pin, pci_get_slot(dev), pci_get_function(dev));
364 return (PCI_INVALID_IRQ);
368 grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
370 struct grackle_softc *sc;
372 sc = device_get_softc(dev);
375 case PCIB_IVAR_DOMAIN:
379 *result = sc->sc_bus;
386 static struct resource *
387 grackle_alloc_resource(device_t bus, device_t child, int type, int *rid,
388 u_long start, u_long end, u_long count, u_int flags)
390 struct grackle_softc *sc;
395 needactivate = flags & RF_ACTIVE;
398 sc = device_get_softc(bus);
402 rm = &sc->sc_mem_rman;
406 rm = &sc->sc_io_rman;
410 return (bus_alloc_resource(bus, type, rid, start, end, count,
414 device_printf(bus, "unknown resource request from %s\n",
415 device_get_nameunit(child));
419 rv = rman_reserve_resource(rm, start, end, count, flags, child);
421 device_printf(bus, "failed to reserve resource for %s\n",
422 device_get_nameunit(child));
426 rman_set_rid(rv, *rid);
429 if (bus_activate_resource(child, type, *rid, rv) != 0) {
431 "failed to activate resource for %s\n",
432 device_get_nameunit(child));
433 rman_release_resource(rv);
442 grackle_release_resource(device_t bus, device_t child, int type, int rid,
443 struct resource *res)
445 if (rman_get_flags(res) & RF_ACTIVE) {
446 int error = bus_deactivate_resource(child, type, rid, res);
451 return (rman_release_resource(res));
455 grackle_activate_resource(device_t bus, device_t child, int type, int rid,
456 struct resource *res)
458 struct grackle_softc *sc;
461 sc = device_get_softc(bus);
463 if (type == SYS_RES_IRQ) {
464 return (bus_activate_resource(bus, type, rid, res));
466 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
469 start = (vm_offset_t)rman_get_start(res);
471 * For i/o-ports, convert the start address to the
472 * MPC106 PCI i/o window
474 if (type == SYS_RES_IOPORT)
475 start += sc->sc_iostart;
478 printf("grackle mapdev: start %zx, len %ld\n", start,
481 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
485 rman_set_virtual(res, p);
486 rman_set_bustag(res, &bs_le_tag);
487 rman_set_bushandle(res, (u_long)p);
490 return (rman_activate_resource(res));
494 grackle_deactivate_resource(device_t bus, device_t child, int type, int rid,
495 struct resource *res)
498 * If this is a memory resource, unmap it.
500 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
503 psize = rman_get_size(res);
504 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
507 return (rman_deactivate_resource(res));
512 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
513 u_int func, u_int reg)
518 * Unlike UniNorth, the format of the config word is the same
519 * for local (0) and remote busses.
521 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
522 | GRACKLE_CFG_ENABLE;
524 out32rb(sc->sc_addr, cfgval);
525 (void) in32rb(sc->sc_addr);
531 grackle_disable_config(struct grackle_softc *sc)
534 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
535 * accesses from causing config cycles
537 out32rb(sc->sc_addr, 0);
541 grackle_get_node(device_t bus, device_t dev)
543 struct grackle_softc *sc;
545 sc = device_get_softc(bus);
546 /* We only have one child, the PCI bus, which needs our own node. */
552 * Driver to swallow Grackle host bridges from the PCI bus side.
555 grackle_hb_probe(device_t dev)
558 if (pci_get_devid(dev) == 0x00021057) {
559 device_set_desc(dev, "Grackle Host to PCI bridge");
568 grackle_hb_attach(device_t dev)
574 static device_method_t grackle_hb_methods[] = {
575 /* Device interface */
576 DEVMETHOD(device_probe, grackle_hb_probe),
577 DEVMETHOD(device_attach, grackle_hb_attach),
582 static driver_t grackle_hb_driver = {
587 static devclass_t grackle_hb_devclass;
589 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);