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/md_var.h>
47 #include <machine/pio.h>
48 #include <machine/resource.h>
52 #include <powerpc/powermac/gracklevar.h>
59 int badaddr(void *, size_t); /* XXX */
64 static int grackle_probe(device_t);
65 static int grackle_attach(device_t);
70 static int grackle_read_ivar(device_t, device_t, int,
72 static struct resource * grackle_alloc_resource(device_t bus,
73 device_t child, int type, int *rid, u_long start,
74 u_long end, u_long count, u_int flags);
75 static int grackle_release_resource(device_t bus, device_t child,
76 int type, int rid, struct resource *res);
77 static int grackle_activate_resource(device_t bus, device_t child,
78 int type, int rid, struct resource *res);
79 static int grackle_deactivate_resource(device_t bus,
80 device_t child, int type, int rid,
81 struct resource *res);
87 static int grackle_maxslots(device_t);
88 static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int,
90 static void grackle_write_config(device_t, u_int, u_int, u_int,
91 u_int, u_int32_t, int);
92 static int grackle_route_interrupt(device_t, device_t, int);
97 static phandle_t grackle_get_node(device_t bus, device_t dev);
102 static int grackle_enable_config(struct grackle_softc *, u_int,
103 u_int, u_int, u_int);
104 static void grackle_disable_config(struct grackle_softc *);
109 static device_method_t grackle_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, grackle_probe),
112 DEVMETHOD(device_attach, grackle_attach),
115 DEVMETHOD(bus_print_child, bus_generic_print_child),
116 DEVMETHOD(bus_read_ivar, grackle_read_ivar),
117 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
118 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
119 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource),
120 DEVMETHOD(bus_release_resource, grackle_release_resource),
121 DEVMETHOD(bus_activate_resource, grackle_activate_resource),
122 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource),
125 DEVMETHOD(pcib_maxslots, grackle_maxslots),
126 DEVMETHOD(pcib_read_config, grackle_read_config),
127 DEVMETHOD(pcib_write_config, grackle_write_config),
128 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt),
130 /* ofw_bus interface */
131 DEVMETHOD(ofw_bus_get_node, grackle_get_node),
136 static driver_t grackle_driver = {
139 sizeof(struct grackle_softc)
142 static devclass_t grackle_devclass;
144 DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0);
147 grackle_probe(device_t dev)
149 const char *type, *compatible;
151 type = ofw_bus_get_type(dev);
152 compatible = ofw_bus_get_compat(dev);
154 if (type == NULL || compatible == NULL)
157 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0)
160 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge");
165 grackle_attach(device_t dev)
167 struct grackle_softc *sc;
169 u_int32_t busrange[2];
170 struct grackle_range *rp, *io, *mem[2];
173 node = ofw_bus_get_node(dev);
174 sc = device_get_softc(dev);
176 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
181 sc->sc_bus = busrange[0];
184 * The Grackle PCI config addr/data registers are actually in
185 * PCI space, but since they are needed to actually probe the
186 * PCI bus, use the fact that they are also available directly
187 * on the processor bus and map them
189 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE);
190 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE);
192 bzero(sc->sc_range, sizeof(sc->sc_range));
193 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
194 sizeof(sc->sc_range));
196 if (sc->sc_nrange == -1) {
197 device_printf(dev, "could not get ranges\n");
201 sc->sc_range[6].pci_hi = 0;
205 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
206 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
207 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
209 case OFW_PCI_PHYS_HI_SPACE_IO:
212 case OFW_PCI_PHYS_HI_SPACE_MEM32:
216 case OFW_PCI_PHYS_HI_SPACE_MEM64:
222 device_printf(dev, "can't find io range\n");
225 sc->sc_io_rman.rm_type = RMAN_ARRAY;
226 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports";
227 sc->sc_iostart = io->pci_iospace;
228 if (rman_init(&sc->sc_io_rman) != 0 ||
229 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
230 io->pci_lo + io->size_lo) != 0) {
231 panic("grackle_attach: failed to set up I/O rman");
235 device_printf(dev, "can't find mem ranges\n");
238 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
239 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory";
240 error = rman_init(&sc->sc_mem_rman);
242 device_printf(dev, "rman_init() failed. error = %d\n", error);
245 for (i = 0; i < nmem; i++) {
246 error = rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
247 mem[i]->pci_lo + mem[i]->size_lo);
250 "rman_manage_region() failed. error = %d\n", error);
255 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
257 device_add_child(dev, "pci", device_get_unit(dev));
258 return (bus_generic_attach(dev));
262 grackle_maxslots(device_t dev)
265 return (PCI_SLOTMAX);
269 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
272 struct grackle_softc *sc;
274 u_int32_t retval = 0xffffffff;
276 sc = device_get_softc(dev);
277 caoff = sc->sc_data + (reg & 0x03);
279 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) {
282 * Config probes to non-existent devices on the
283 * secondary bus generates machine checks. Be sure
287 if (badaddr((void *)sc->sc_data, 4)) {
294 retval = (in8rb(caoff));
297 retval = (in16rb(caoff));
300 retval = (in32rb(caoff));
304 grackle_disable_config(sc);
310 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func,
311 u_int reg, u_int32_t val, int width)
313 struct grackle_softc *sc;
316 sc = device_get_softc(dev);
317 caoff = sc->sc_data + (reg & 0x03);
319 if (grackle_enable_config(sc, bus, slot, func, reg)) {
335 grackle_disable_config(sc);
339 grackle_route_interrupt(device_t bus, device_t dev, int pin)
341 struct grackle_softc *sc;
342 struct ofw_pci_register reg;
343 uint32_t pintr, mintr;
344 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
346 sc = device_get_softc(bus);
348 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®,
349 sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr), maskbuf))
352 /* Maybe it's a real interrupt, not an intpin */
356 device_printf(bus, "could not route pin %d for device %d.%d\n",
357 pin, pci_get_slot(dev), pci_get_function(dev));
358 return (PCI_INVALID_IRQ);
362 grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
364 struct grackle_softc *sc;
366 sc = device_get_softc(dev);
369 case PCIB_IVAR_DOMAIN:
373 *result = sc->sc_bus;
380 static struct resource *
381 grackle_alloc_resource(device_t bus, device_t child, int type, int *rid,
382 u_long start, u_long end, u_long count, u_int flags)
384 struct grackle_softc *sc;
389 needactivate = flags & RF_ACTIVE;
392 sc = device_get_softc(bus);
396 rm = &sc->sc_mem_rman;
400 rm = &sc->sc_io_rman;
404 return (bus_alloc_resource(bus, type, rid, start, end, count,
408 device_printf(bus, "unknown resource request from %s\n",
409 device_get_nameunit(child));
413 rv = rman_reserve_resource(rm, start, end, count, flags, child);
415 device_printf(bus, "failed to reserve resource for %s\n",
416 device_get_nameunit(child));
420 rman_set_rid(rv, *rid);
423 if (bus_activate_resource(child, type, *rid, rv) != 0) {
425 "failed to activate resource for %s\n",
426 device_get_nameunit(child));
427 rman_release_resource(rv);
436 grackle_release_resource(device_t bus, device_t child, int type, int rid,
437 struct resource *res)
439 if (rman_get_flags(res) & RF_ACTIVE) {
440 int error = bus_deactivate_resource(child, type, rid, res);
445 return (rman_release_resource(res));
449 grackle_activate_resource(device_t bus, device_t child, int type, int rid,
450 struct resource *res)
452 struct grackle_softc *sc;
455 sc = device_get_softc(bus);
457 if (type == SYS_RES_IRQ) {
458 return (bus_activate_resource(bus, type, rid, res));
460 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
463 start = (vm_offset_t)rman_get_start(res);
465 * For i/o-ports, convert the start address to the
466 * MPC106 PCI i/o window
468 if (type == SYS_RES_IOPORT)
469 start += sc->sc_iostart;
472 printf("grackle mapdev: start %x, len %ld\n", start,
475 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
479 rman_set_virtual(res, p);
480 rman_set_bustag(res, &bs_le_tag);
481 rman_set_bushandle(res, (u_long)p);
484 return (rman_activate_resource(res));
488 grackle_deactivate_resource(device_t bus, device_t child, int type, int rid,
489 struct resource *res)
492 * If this is a memory resource, unmap it.
494 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
497 psize = rman_get_size(res);
498 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
501 return (rman_deactivate_resource(res));
506 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot,
507 u_int func, u_int reg)
512 * Unlike UniNorth, the format of the config word is the same
513 * for local (0) and remote busses.
515 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC)
516 | GRACKLE_CFG_ENABLE;
518 out32rb(sc->sc_addr, cfgval);
519 (void) in32rb(sc->sc_addr);
525 grackle_disable_config(struct grackle_softc *sc)
528 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray
529 * accesses from causing config cycles
531 out32rb(sc->sc_addr, 0);
535 grackle_get_node(device_t bus, device_t dev)
537 struct grackle_softc *sc;
539 sc = device_get_softc(bus);
540 /* We only have one child, the PCI bus, which needs our own node. */
546 * Driver to swallow Grackle host bridges from the PCI bus side.
549 grackle_hb_probe(device_t dev)
552 if (pci_get_devid(dev) == 0x00021057) {
553 device_set_desc(dev, "Grackle Host to PCI bridge");
562 grackle_hb_attach(device_t dev)
568 static device_method_t grackle_hb_methods[] = {
569 /* Device interface */
570 DEVMETHOD(device_probe, grackle_hb_probe),
571 DEVMETHOD(device_attach, grackle_hb_attach),
576 static driver_t grackle_hb_driver = {
581 static devclass_t grackle_hb_devclass;
583 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0);