2 * Copyright (C) 2002 Benno Rice.
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 Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/module.h>
33 #include <sys/kernel.h>
35 #include <dev/ofw/openfirm.h>
36 #include <dev/ofw/ofw_pci.h>
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
41 #include <machine/bus.h>
42 #include <machine/md_var.h>
43 #include <machine/nexusvar.h>
44 #include <machine/resource.h>
48 #include <powerpc/ofw/ofw_pci.h>
49 #include <powerpc/powermac/uninorthvar.h>
56 #define UNINORTH_DEBUG 0
61 static int uninorth_probe(device_t);
62 static int uninorth_attach(device_t);
67 static int uninorth_read_ivar(device_t, device_t, int,
69 static struct resource * uninorth_alloc_resource(device_t bus,
70 device_t child, int type, int *rid, u_long start,
71 u_long end, u_long count, u_int flags);
72 static int uninorth_activate_resource(device_t bus, device_t child,
73 int type, int rid, struct resource *res);
78 static int uninorth_maxslots(device_t);
79 static u_int32_t uninorth_read_config(device_t, u_int, u_int, u_int,
81 static void uninorth_write_config(device_t, u_int, u_int, u_int,
82 u_int, u_int32_t, int);
83 static int uninorth_route_interrupt(device_t, device_t, int);
88 static int uninorth_enable_config(struct uninorth_softc *, u_int,
90 static void unin_enable_gmac(void);
95 static device_method_t uninorth_methods[] = {
96 /* Device interface */
97 DEVMETHOD(device_probe, uninorth_probe),
98 DEVMETHOD(device_attach, uninorth_attach),
101 DEVMETHOD(bus_print_child, bus_generic_print_child),
102 DEVMETHOD(bus_read_ivar, uninorth_read_ivar),
103 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
104 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
105 DEVMETHOD(bus_alloc_resource, uninorth_alloc_resource),
106 DEVMETHOD(bus_activate_resource, uninorth_activate_resource),
109 DEVMETHOD(pcib_maxslots, uninorth_maxslots),
110 DEVMETHOD(pcib_read_config, uninorth_read_config),
111 DEVMETHOD(pcib_write_config, uninorth_write_config),
112 DEVMETHOD(pcib_route_interrupt, uninorth_route_interrupt),
117 static driver_t uninorth_driver = {
120 sizeof(struct uninorth_softc)
123 static devclass_t uninorth_devclass;
125 DRIVER_MODULE(uninorth, nexus, uninorth_driver, uninorth_devclass, 0, 0);
128 uninorth_probe(device_t dev)
130 char *type, *compatible;
132 type = nexus_get_device_type(dev);
133 compatible = nexus_get_compatible(dev);
135 if (type == NULL || compatible == NULL)
138 if (strcmp(type, "pci") != 0 || strcmp(compatible, "uni-north") != 0)
141 device_set_desc(dev, "Apple UniNorth Host-PCI bridge");
146 uninorth_attach(device_t dev)
148 struct uninorth_softc *sc;
151 u_int32_t reg[2], busrange[2];
152 struct uninorth_range *rp, *io, *mem[2];
155 node = nexus_get_node(dev);
156 sc = device_get_softc(dev);
158 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
161 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
166 sc->sc_addr = (vm_offset_t)pmap_mapdev(reg[0] + 0x800000, PAGE_SIZE);
167 sc->sc_data = (vm_offset_t)pmap_mapdev(reg[0] + 0xc00000, PAGE_SIZE);
168 sc->sc_bus = busrange[0];
170 bzero(sc->sc_range, sizeof(sc->sc_range));
171 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range,
172 sizeof(sc->sc_range));
174 if (sc->sc_nrange == -1) {
175 device_printf(dev, "could not get ranges\n");
179 sc->sc_range[6].pci_hi = 0;
183 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) {
184 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
185 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
187 case OFW_PCI_PHYS_HI_SPACE_IO:
190 case OFW_PCI_PHYS_HI_SPACE_MEM32:
194 case OFW_PCI_PHYS_HI_SPACE_MEM64:
200 device_printf(dev, "can't find io range\n");
203 sc->sc_io_rman.rm_type = RMAN_ARRAY;
204 sc->sc_io_rman.rm_descr = "UniNorth PCI I/O Ports";
205 sc->sc_iostart = io->host;
206 if (rman_init(&sc->sc_io_rman) != 0 ||
207 rman_manage_region(&sc->sc_io_rman, io->pci_lo,
208 io->pci_lo + io->size_lo - 1) != 0) {
209 device_printf(dev, "failed to set up io range management\n");
214 device_printf(dev, "can't find mem ranges\n");
217 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
218 sc->sc_mem_rman.rm_descr = "UniNorth PCI Memory";
219 if (rman_init(&sc->sc_mem_rman) != 0) {
221 "failed to init mem range resources\n");
224 for (i = 0; i < nmem; i++) {
225 if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo,
226 mem[i]->pci_lo + mem[i]->size_lo - 1) != 0) {
228 "failed to set up memory range management\n");
234 * Enable the GMAC Ethernet cell if Open Firmware says it is
237 for (child = OF_child(node); child; child = OF_peer(child)) {
240 memset(compat, 0, sizeof(compat));
241 OF_getprop(child, "compatible", compat, sizeof(compat));
242 if (strcmp(compat, "gmac") == 0) {
248 * Write out the correct PIC interrupt values to config space
249 * of all devices on the bus. This has to be done after the GEM
250 * cell is enabled above.
252 ofw_pci_fixup(dev, sc->sc_bus, node);
254 device_add_child(dev, "pci", device_get_unit(dev));
255 return (bus_generic_attach(dev));
259 uninorth_maxslots(device_t dev)
262 return (PCI_SLOTMAX);
266 uninorth_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
269 struct uninorth_softc *sc;
272 sc = device_get_softc(dev);
273 caoff = sc->sc_data + (reg & 0x07);
275 if (uninorth_enable_config(sc, bus, slot, func, reg) != 0) {
278 return (in8rb(caoff));
281 return (in16rb(caoff));
284 return (in32rb(caoff));
293 uninorth_write_config(device_t dev, u_int bus, u_int slot, u_int func,
294 u_int reg, u_int32_t val, int width)
296 struct uninorth_softc *sc;
299 sc = device_get_softc(dev);
300 caoff = sc->sc_data + (reg & 0x07);
302 if (uninorth_enable_config(sc, bus, slot, func, reg)) {
318 uninorth_route_interrupt(device_t bus, device_t dev, int pin)
325 uninorth_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
327 struct uninorth_softc *sc;
329 sc = device_get_softc(dev);
332 case PCIB_IVAR_DOMAIN:
333 *result = device_get_unit(dev);
336 *result = sc->sc_bus;
343 static struct resource *
344 uninorth_alloc_resource(device_t bus, device_t child, int type, int *rid,
345 u_long start, u_long end, u_long count, u_int flags)
347 struct uninorth_softc *sc;
353 needactivate = flags & RF_ACTIVE;
356 sc = device_get_softc(bus);
360 rm = &sc->sc_mem_rman;
361 bt = PPC_BUS_SPACE_MEM;
365 rm = &sc->sc_io_rman;
366 bt = PPC_BUS_SPACE_IO;
370 return (bus_alloc_resource(bus, type, rid, start, end, count,
374 device_printf(bus, "unknown resource request from %s\n",
375 device_get_nameunit(child));
379 rv = rman_reserve_resource(rm, start, end, count, flags, child);
381 device_printf(bus, "failed to reserve resource for %s\n",
382 device_get_nameunit(child));
386 rman_set_rid(rv, *rid);
387 rman_set_bustag(rv, bt);
388 rman_set_bushandle(rv, rman_get_start(rv));
391 if (bus_activate_resource(child, type, *rid, rv) != 0) {
393 "failed to activate resource for %s\n",
394 device_get_nameunit(child));
395 rman_release_resource(rv);
404 uninorth_activate_resource(device_t bus, device_t child, int type, int rid,
405 struct resource *res)
408 struct uninorth_softc *sc;
410 sc = device_get_softc(bus);
412 if (type == SYS_RES_IRQ)
413 return (bus_activate_resource(bus, type, rid, res));
415 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
418 start = (vm_offset_t)rman_get_start(res);
420 * For i/o-ports, convert the start address to the
421 * uninorth PCI i/o window
423 if (type == SYS_RES_IOPORT)
424 start += sc->sc_iostart;
427 printf("uninorth mapdev: start %x, len %ld\n", start,
430 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
433 rman_set_virtual(res, p);
434 rman_set_bushandle(res, (u_long)p);
437 return (rman_activate_resource(res));
441 uninorth_enable_config(struct uninorth_softc *sc, u_int bus, u_int slot,
442 u_int func, u_int reg)
447 if (resource_int_value(device_get_name(sc->sc_dev),
448 device_get_unit(sc->sc_dev), "skipslot", &pass) == 0) {
453 if (sc->sc_bus == bus) {
455 * No slots less than 11 on the primary bus
460 cfgval = (1 << slot) | (func << 8) | (reg & 0xfc);
462 cfgval = (bus << 16) | (slot << 11) | (func << 8) |
467 out32rb(sc->sc_addr, cfgval);
468 } while (in32rb(sc->sc_addr) != cfgval);
474 * Driver to swallow UniNorth host bridges from the PCI bus side.
477 unhb_probe(device_t dev)
480 if (pci_get_class(dev) == PCIC_BRIDGE &&
481 pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
482 device_set_desc(dev, "Host to PCI bridge");
491 unhb_attach(device_t dev)
497 static device_method_t unhb_methods[] = {
498 /* Device interface */
499 DEVMETHOD(device_probe, unhb_probe),
500 DEVMETHOD(device_attach, unhb_attach),
505 static driver_t unhb_driver = {
510 static devclass_t unhb_devclass;
512 DRIVER_MODULE(unhb, pci, unhb_driver, unhb_devclass, 0, 0);
516 * Small stub driver for the Uninorth chip itself, to allow setting
517 * of various parameters and cell enables
519 static struct unin_chip_softc *uncsc;
522 unin_enable_gmac(void)
524 volatile u_int *clkreg;
528 panic("unin_enable_gmac: device not found");
530 clkreg = (void *)(uncsc->sc_addr + UNIN_CLOCKCNTL);
532 tmpl |= UNIN_CLOCKCNTL_GMAC;
537 unin_chip_probe(device_t dev)
541 name = nexus_get_name(dev);
546 if (strcmp(name, "uni-n") != 0)
549 device_set_desc(dev, "Apple UniNorth System Controller");
554 unin_chip_attach(device_t dev)
559 uncsc = device_get_softc(dev);
560 node = nexus_get_node(dev);
562 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
565 uncsc->sc_physaddr = reg[0];
566 uncsc->sc_size = reg[1];
569 * Only map the first page, since that is where the registers
572 uncsc->sc_addr = (vm_offset_t) pmap_mapdev(reg[0], PAGE_SIZE);
574 uncsc->sc_version = *(u_int *)uncsc->sc_addr;
575 device_printf(dev, "Version %d\n", uncsc->sc_version);
580 static device_method_t unin_chip_methods[] = {
581 /* Device interface */
582 DEVMETHOD(device_probe, unin_chip_probe),
583 DEVMETHOD(device_attach, unin_chip_attach),
588 static driver_t unin_chip_driver = {
591 sizeof(struct unin_chip_softc)
594 static devclass_t unin_chip_devclass;
596 DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);