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>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
43 #include <machine/bus.h>
44 #include <machine/intr_machdep.h>
45 #include <machine/md_var.h>
46 #include <machine/pio.h>
47 #include <machine/resource.h>
51 #include <powerpc/powermac/uninorthvar.h>
57 * Driver for the Uninorth chip itself.
60 static MALLOC_DEFINE(M_UNIN, "unin", "unin device information");
66 static int unin_chip_probe(device_t);
67 static int unin_chip_attach(device_t);
72 static int unin_chip_print_child(device_t dev, device_t child);
73 static void unin_chip_probe_nomatch(device_t, device_t);
74 static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *,
75 rman_res_t, rman_res_t,
77 static int unin_chip_activate_resource(device_t, device_t, int, int,
79 static int unin_chip_deactivate_resource(device_t, device_t, int, int,
81 static int unin_chip_release_resource(device_t, device_t, int, int,
83 static struct resource_list *unin_chip_get_resource_list (device_t, device_t);
89 static ofw_bus_get_devinfo_t unin_chip_get_devinfo;
95 static void unin_enable_gmac(device_t dev);
96 static void unin_enable_mpic(device_t dev);
101 static device_method_t unin_chip_methods[] = {
103 /* Device interface */
104 DEVMETHOD(device_probe, unin_chip_probe),
105 DEVMETHOD(device_attach, unin_chip_attach),
108 DEVMETHOD(bus_print_child, unin_chip_print_child),
109 DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch),
110 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
111 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
113 DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource),
114 DEVMETHOD(bus_release_resource, unin_chip_release_resource),
115 DEVMETHOD(bus_activate_resource, unin_chip_activate_resource),
116 DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource),
117 DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list),
119 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
121 /* ofw_bus interface */
122 DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo),
123 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
124 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
125 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
126 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
127 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
132 static driver_t unin_chip_driver = {
135 sizeof(struct unin_chip_softc)
138 static devclass_t unin_chip_devclass;
141 * Assume there is only one unin chip in a PowerMac, so that pmu.c functions can
142 * suspend the chip after the whole rest of the device tree is suspended, not
145 static device_t unin_chip;
147 DRIVER_MODULE(unin, ofwbus, unin_chip_driver, unin_chip_devclass, 0, 0);
150 * Add an interrupt to the dev's resource list if present
153 unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo)
160 if (dinfo->udi_ninterrupts >= 6) {
161 printf("unin: device has more than 6 interrupts\n");
165 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
168 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
169 sizeof(*intr), (void **)&intr);
177 if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
179 panic("Interrupt but no interrupt parent!\n");
181 if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells))
185 for (i = 0; i < nintr; i+=icells) {
186 u_int irq = MAP_IRQ(iparent, intr[i]);
188 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
189 dinfo->udi_ninterrupts, irq, irq, 1);
192 powerpc_config_intr(irq,
193 (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL :
194 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
197 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq;
198 dinfo->udi_ninterrupts++;
203 unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo)
205 struct unin_chip_reg *reg;
208 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®);
212 for (i = 0; i < nreg; i++) {
213 resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i,
215 reg[i].mr_base + reg[i].mr_size,
221 unin_update_reg(device_t dev, uint32_t regoff, uint32_t set, uint32_t clr)
224 struct unin_chip_softc *sc;
227 sc = device_get_softc(dev);
228 reg = (void *)(sc->sc_addr + regoff);
236 unin_enable_gmac(device_t dev)
238 unin_update_reg(dev, UNIN_CLOCKCNTL, UNIN_CLOCKCNTL_GMAC, 0);
242 unin_enable_mpic(device_t dev)
244 unin_update_reg(dev, UNIN_TOGGLE_REG, UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE, 0);
248 unin_chip_probe(device_t dev)
252 name = ofw_bus_get_name(dev);
257 if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0
258 && strcmp(name, "u4") != 0)
261 device_set_desc(dev, "Apple UniNorth System Controller");
266 unin_chip_attach(device_t dev)
268 struct unin_chip_softc *sc;
269 struct unin_chip_devinfo *dinfo;
274 cell_t acells, scells;
280 sc = device_get_softc(dev);
281 root = ofw_bus_get_node(dev);
283 if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
287 OF_getprop(OF_parent(root), "#address-cells", &acells, sizeof(acells));
288 OF_getprop(OF_parent(root), "#size-cells", &scells, sizeof(scells));
291 sc->sc_physaddr = reg[i++];
293 sc->sc_physaddr <<= 32;
294 sc->sc_physaddr |= reg[i++];
296 sc->sc_size = reg[i++];
299 sc->sc_size |= reg[i++];
302 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
303 sc->sc_mem_rman.rm_descr = "UniNorth Device Memory";
305 error = rman_init(&sc->sc_mem_rman);
308 device_printf(dev, "rman_init() failed. error = %d\n", error);
312 error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr,
313 sc->sc_physaddr + sc->sc_size - 1);
316 "rman_manage_region() failed. error = %d\n",
321 if (unin_chip == NULL)
325 * Iterate through the sub-devices
327 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
328 dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO);
329 if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child)
336 resource_list_init(&dinfo->udi_resources);
337 dinfo->udi_ninterrupts = 0;
338 unin_chip_add_intr(child, dinfo);
341 * Some Apple machines do have a bug in OF, they miss
342 * the interrupt entries on the U3 I2C node. That means they
343 * do not have an entry with number of interrupts nor the
344 * entry of the interrupt parent handle.
345 * We define an interrupt and hardwire it to the /u3/mpic
349 if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
350 device_printf(dev, "device has no name!\n");
351 if (dinfo->udi_ninterrupts == 0 &&
352 (strcmp(name, "i2c-bus") == 0 ||
353 strcmp(name, "i2c") == 0)) {
354 if (OF_getprop(child, "interrupt-parent", &iparent,
355 sizeof(iparent)) <= 0) {
356 iparent = OF_finddevice("/u3/mpic");
357 device_printf(dev, "Set /u3/mpic as iparent!\n");
359 /* Add an interrupt number 0 to the parent. */
360 irq = MAP_IRQ(iparent, 0);
361 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
362 dinfo->udi_ninterrupts, irq, irq, 1);
363 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq;
364 dinfo->udi_ninterrupts++;
367 unin_chip_add_reg(child, dinfo);
369 cdev = device_add_child(dev, NULL, -1);
371 device_printf(dev, "<%s>: device_add_child failed\n",
372 dinfo->udi_obdinfo.obd_name);
373 resource_list_free(&dinfo->udi_resources);
374 ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
379 device_set_ivars(cdev, dinfo);
383 * Only map the first page, since that is where the registers
386 sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE);
388 sc->sc_version = *(u_int *)sc->sc_addr;
389 device_printf(dev, "Version %d\n", sc->sc_version);
392 * Enable the GMAC Ethernet cell and the integrated OpenPIC
393 * if Open Firmware says they are used.
395 for (child = OF_child(root); child; child = OF_peer(child)) {
396 memset(compat, 0, sizeof(compat));
397 OF_getprop(child, "compatible", compat, sizeof(compat));
398 if (strcmp(compat, "gmac") == 0)
399 unin_enable_gmac(dev);
400 if (strcmp(compat, "chrp,open-pic") == 0)
401 unin_enable_mpic(dev);
405 * GMAC lives under the PCI bus, so just check if enet is gmac.
407 child = OF_finddevice("enet");
408 memset(compat, 0, sizeof(compat));
409 OF_getprop(child, "compatible", compat, sizeof(compat));
410 if (strcmp(compat, "gmac") == 0)
411 unin_enable_gmac(dev);
413 return (bus_generic_attach(dev));
417 unin_chip_print_child(device_t dev, device_t child)
419 struct unin_chip_devinfo *dinfo;
420 struct resource_list *rl;
423 dinfo = device_get_ivars(child);
424 rl = &dinfo->udi_resources;
426 retval += bus_print_child_header(dev, child);
428 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
429 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
431 retval += bus_print_child_footer(dev, child);
437 unin_chip_probe_nomatch(device_t dev, device_t child)
439 struct unin_chip_devinfo *dinfo;
440 struct resource_list *rl;
444 dinfo = device_get_ivars(child);
445 rl = &dinfo->udi_resources;
447 if ((type = ofw_bus_get_type(child)) == NULL)
449 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
450 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
451 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
452 printf(" (no driver attached)\n");
457 static struct resource *
458 unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid,
459 rman_res_t start, rman_res_t end, rman_res_t count,
462 struct unin_chip_softc *sc;
466 u_long adjstart, adjend, adjcount;
467 struct unin_chip_devinfo *dinfo;
468 struct resource_list_entry *rle;
470 sc = device_get_softc(bus);
471 dinfo = device_get_ivars(child);
473 needactivate = flags & RF_ACTIVE;
479 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY,
482 device_printf(bus, "no rle for %s memory %d\n",
483 device_get_nameunit(child), *rid);
487 rle->end = rle->end - 1; /* Hack? */
489 if (start < rle->start)
490 adjstart = rle->start;
491 else if (start > rle->end)
496 if (end < rle->start)
498 else if (end > rle->end)
503 adjcount = adjend - adjstart;
505 rm = &sc->sc_mem_rman;
509 /* Check for passthrough from subattachments. */
510 if (device_get_parent(child) != bus)
511 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
512 type, rid, start, end, count,
515 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ,
518 if (dinfo->udi_ninterrupts >= 6) {
520 "%s has more than 6 interrupts\n",
521 device_get_nameunit(child));
524 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
525 dinfo->udi_ninterrupts, start, start,
528 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start;
529 dinfo->udi_ninterrupts++;
532 return (resource_list_alloc(&dinfo->udi_resources, bus, child,
533 type, rid, start, end, count,
536 device_printf(bus, "unknown resource request from %s\n",
537 device_get_nameunit(child));
541 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
545 "failed to reserve resource %#lx - %#lx (%#lx)"
546 " for %s\n", adjstart, adjend, adjcount,
547 device_get_nameunit(child));
551 rman_set_rid(rv, *rid);
554 if (bus_activate_resource(child, type, *rid, rv) != 0) {
556 "failed to activate resource for %s\n",
557 device_get_nameunit(child));
558 rman_release_resource(rv);
567 unin_chip_release_resource(device_t bus, device_t child, int type, int rid,
568 struct resource *res)
570 if (rman_get_flags(res) & RF_ACTIVE) {
571 int error = bus_deactivate_resource(child, type, rid, res);
576 return (rman_release_resource(res));
580 unin_chip_activate_resource(device_t bus, device_t child, int type, int rid,
581 struct resource *res)
585 if (type == SYS_RES_IRQ)
586 return (bus_activate_resource(bus, type, rid, res));
588 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
591 start = (vm_offset_t) rman_get_start(res);
594 printf("unin mapdev: start %zx, len %jd\n", start,
597 p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
600 rman_set_virtual(res, p);
601 rman_set_bustag(res, &bs_be_tag);
602 rman_set_bushandle(res, (u_long)p);
605 return (rman_activate_resource(res));
610 unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid,
611 struct resource *res)
614 * If this is a memory resource, unmap it.
616 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
619 psize = rman_get_size(res);
620 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
623 return (rman_deactivate_resource(res));
627 static struct resource_list *
628 unin_chip_get_resource_list (device_t dev, device_t child)
630 struct unin_chip_devinfo *dinfo;
632 dinfo = device_get_ivars(child);
633 return (&dinfo->udi_resources);
636 static const struct ofw_bus_devinfo *
637 unin_chip_get_devinfo(device_t dev, device_t child)
639 struct unin_chip_devinfo *dinfo;
641 dinfo = device_get_ivars(child);
642 return (&dinfo->udi_obdinfo);
646 unin_chip_wake(device_t dev)
651 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_NORMAL, UNIN_PWR_MASK);
653 unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_RUNNING, 0);
660 unin_chip_sleep(device_t dev, int idle)
665 unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_SLEEPING, 0);
668 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_IDLE2, UNIN_PWR_MASK);
670 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_SLEEP, UNIN_PWR_MASK);