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 u_long, u_long, u_long, u_int);
76 static int unin_chip_activate_resource(device_t, device_t, int, int,
78 static int unin_chip_deactivate_resource(device_t, device_t, int, int,
80 static int unin_chip_release_resource(device_t, device_t, int, int,
82 static struct resource_list *unin_chip_get_resource_list (device_t, device_t);
88 static ofw_bus_get_devinfo_t unin_chip_get_devinfo;
94 static void unin_enable_gmac(device_t dev);
95 static void unin_enable_mpic(device_t dev);
100 static device_method_t unin_chip_methods[] = {
102 /* Device interface */
103 DEVMETHOD(device_probe, unin_chip_probe),
104 DEVMETHOD(device_attach, unin_chip_attach),
107 DEVMETHOD(bus_print_child, unin_chip_print_child),
108 DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch),
109 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
110 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
112 DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource),
113 DEVMETHOD(bus_release_resource, unin_chip_release_resource),
114 DEVMETHOD(bus_activate_resource, unin_chip_activate_resource),
115 DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource),
116 DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list),
118 /* ofw_bus interface */
119 DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo),
120 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
121 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
122 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
123 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
124 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
129 static driver_t unin_chip_driver = {
132 sizeof(struct unin_chip_softc)
135 static devclass_t unin_chip_devclass;
137 DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0);
140 * Add an interrupt to the dev's resource list if present
143 unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo)
150 if (dinfo->udi_ninterrupts >= 6) {
151 printf("unin: device has more than 6 interrupts\n");
155 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
158 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
159 sizeof(*intr), (void **)&intr);
167 if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
169 panic("Interrupt but no interrupt parent!\n");
171 if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells))
175 for (i = 0; i < nintr; i+=icells) {
176 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
177 dinfo->udi_ninterrupts, intr[i], intr[i], 1);
179 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = intr[i];
180 dinfo->udi_ninterrupts++;
185 unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo)
187 struct unin_chip_reg *reg;
190 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®);
194 for (i = 0; i < nreg; i++) {
195 resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i,
197 reg[i].mr_base + reg[i].mr_size,
203 unin_enable_gmac(device_t dev)
205 volatile u_int *clkreg;
206 struct unin_chip_softc *sc;
209 sc = device_get_softc(dev);
210 clkreg = (void *)(sc->sc_addr + UNIN_CLOCKCNTL);
212 tmpl |= UNIN_CLOCKCNTL_GMAC;
217 unin_enable_mpic(device_t dev)
219 volatile u_int *toggle;
220 struct unin_chip_softc *sc;
223 sc = device_get_softc(dev);
224 toggle = (void *)(sc->sc_addr + UNIN_TOGGLE_REG);
226 tmpl |= UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE;
231 unin_chip_probe(device_t dev)
235 name = ofw_bus_get_name(dev);
240 if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0
241 && strcmp(name, "u4") != 0)
244 device_set_desc(dev, "Apple UniNorth System Controller");
249 unin_chip_attach(device_t dev)
251 struct unin_chip_softc *sc;
252 struct unin_chip_devinfo *dinfo;
260 sc = device_get_softc(dev);
261 root = ofw_bus_get_node(dev);
263 if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8)
266 if (strcmp(ofw_bus_get_name(dev), "u3") == 0
267 || strcmp(ofw_bus_get_name(dev), "u4") == 0)
268 i = 1; /* #address-cells lies */
270 sc->sc_physaddr = reg[i];
271 sc->sc_size = reg[i+1];
273 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
274 sc->sc_mem_rman.rm_descr = "UniNorth Device Memory";
276 error = rman_init(&sc->sc_mem_rman);
279 device_printf(dev, "rman_init() failed. error = %d\n", error);
283 error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr,
284 sc->sc_physaddr + sc->sc_size - 1);
287 "rman_manage_region() failed. error = %d\n",
293 * Iterate through the sub-devices
295 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
296 dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO);
297 if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child)
304 resource_list_init(&dinfo->udi_resources);
305 dinfo->udi_ninterrupts = 0;
306 unin_chip_add_intr(child, dinfo);
308 unin_chip_add_reg(child, dinfo);
310 cdev = device_add_child(dev, NULL, -1);
312 device_printf(dev, "<%s>: device_add_child failed\n",
313 dinfo->udi_obdinfo.obd_name);
314 resource_list_free(&dinfo->udi_resources);
315 ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
320 device_set_ivars(cdev, dinfo);
324 * Only map the first page, since that is where the registers
327 sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE);
329 sc->sc_version = *(u_int *)sc->sc_addr;
330 device_printf(dev, "Version %d\n", sc->sc_version);
333 * Enable the GMAC Ethernet cell and the integrated OpenPIC
334 * if Open Firmware says they are used.
336 for (child = OF_child(root); child; child = OF_peer(child)) {
337 memset(compat, 0, sizeof(compat));
338 OF_getprop(child, "compatible", compat, sizeof(compat));
339 if (strcmp(compat, "gmac") == 0)
340 unin_enable_gmac(dev);
341 if (strcmp(compat, "chrp,open-pic") == 0)
342 unin_enable_mpic(dev);
346 * GMAC lives under the PCI bus, so just check if enet is gmac.
348 child = OF_finddevice("enet");
349 memset(compat, 0, sizeof(compat));
350 OF_getprop(child, "compatible", compat, sizeof(compat));
351 if (strcmp(compat, "gmac") == 0)
352 unin_enable_gmac(dev);
354 return (bus_generic_attach(dev));
358 unin_chip_print_child(device_t dev, device_t child)
360 struct unin_chip_devinfo *dinfo;
361 struct resource_list *rl;
364 dinfo = device_get_ivars(child);
365 rl = &dinfo->udi_resources;
367 retval += bus_print_child_header(dev, child);
369 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
370 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
372 retval += bus_print_child_footer(dev, child);
378 unin_chip_probe_nomatch(device_t dev, device_t child)
380 struct unin_chip_devinfo *dinfo;
381 struct resource_list *rl;
385 dinfo = device_get_ivars(child);
386 rl = &dinfo->udi_resources;
388 if ((type = ofw_bus_get_type(child)) == NULL)
390 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
391 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
392 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
393 printf(" (no driver attached)\n");
398 static struct resource *
399 unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid,
400 u_long start, u_long end, u_long count, u_int flags)
402 struct unin_chip_softc *sc;
406 u_long adjstart, adjend, adjcount;
407 struct unin_chip_devinfo *dinfo;
408 struct resource_list_entry *rle;
410 sc = device_get_softc(bus);
411 dinfo = device_get_ivars(child);
413 needactivate = flags & RF_ACTIVE;
419 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY,
422 device_printf(bus, "no rle for %s memory %d\n",
423 device_get_nameunit(child), *rid);
427 rle->end = rle->end - 1; /* Hack? */
429 if (start < rle->start)
430 adjstart = rle->start;
431 else if (start > rle->end)
436 if (end < rle->start)
438 else if (end > rle->end)
443 adjcount = adjend - adjstart;
445 rm = &sc->sc_mem_rman;
449 /* Check for passthrough from subattachments. */
450 if (device_get_parent(child) != bus)
451 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
452 type, rid, start, end, count,
455 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ,
458 if (dinfo->udi_ninterrupts >= 6) {
460 "%s has more than 6 interrupts\n",
461 device_get_nameunit(child));
464 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ,
465 dinfo->udi_ninterrupts, start, start,
468 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start;
469 dinfo->udi_ninterrupts++;
472 return (resource_list_alloc(&dinfo->udi_resources, bus, child,
473 type, rid, start, end, count,
476 device_printf(bus, "unknown resource request from %s\n",
477 device_get_nameunit(child));
481 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
485 "failed to reserve resource %#lx - %#lx (%#lx)"
486 " for %s\n", adjstart, adjend, adjcount,
487 device_get_nameunit(child));
491 rman_set_rid(rv, *rid);
494 if (bus_activate_resource(child, type, *rid, rv) != 0) {
496 "failed to activate resource for %s\n",
497 device_get_nameunit(child));
498 rman_release_resource(rv);
507 unin_chip_release_resource(device_t bus, device_t child, int type, int rid,
508 struct resource *res)
510 if (rman_get_flags(res) & RF_ACTIVE) {
511 int error = bus_deactivate_resource(child, type, rid, res);
516 return (rman_release_resource(res));
520 unin_chip_activate_resource(device_t bus, device_t child, int type, int rid,
521 struct resource *res)
523 struct unin_chip_softc *sc;
526 sc = device_get_softc(bus);
528 if (type == SYS_RES_IRQ)
529 return (bus_activate_resource(bus, type, rid, res));
531 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
534 start = (vm_offset_t) rman_get_start(res);
537 printf("unin mapdev: start %zx, len %ld\n", start,
540 p = pmap_mapdev(start, (vm_size_t) rman_get_size(res));
543 rman_set_virtual(res, p);
544 rman_set_bustag(res, &bs_be_tag);
545 rman_set_bushandle(res, (u_long)p);
548 return (rman_activate_resource(res));
553 unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid,
554 struct resource *res)
557 * If this is a memory resource, unmap it.
559 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
562 psize = rman_get_size(res);
563 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
566 return (rman_deactivate_resource(res));
570 static struct resource_list *
571 unin_chip_get_resource_list (device_t dev, device_t child)
573 struct unin_chip_devinfo *dinfo;
575 dinfo = device_get_ivars(child);
576 return (&dinfo->udi_resources);
579 static const struct ofw_bus_devinfo *
580 unin_chip_get_devinfo(device_t dev, device_t child)
582 struct unin_chip_devinfo *dinfo;
584 dinfo = device_get_ivars(child);
585 return (&dinfo->udi_obdinfo);