2 * Copyright 2002 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
31 * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <machine/bus.h>
43 #include <machine/vmparam.h>
46 #include <machine/pmap.h>
48 #include <machine/resource.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
54 #include <powerpc/powermac/maciovar.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
66 struct rman sc_mem_rman;
69 static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
71 static int macio_probe(device_t);
72 static int macio_attach(device_t);
73 static int macio_print_child(device_t dev, device_t child);
74 static void macio_probe_nomatch(device_t, device_t);
75 static struct resource *macio_alloc_resource(device_t, device_t, int, int *,
76 u_long, u_long, u_long, u_int);
77 static int macio_activate_resource(device_t, device_t, int, int,
79 static int macio_deactivate_resource(device_t, device_t, int, int,
81 static int macio_release_resource(device_t, device_t, int, int,
83 static struct resource_list *macio_get_resource_list (device_t, device_t);
84 static ofw_bus_get_devinfo_t macio_get_devinfo;
87 * Bus interface definition
89 static device_method_t macio_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, macio_probe),
92 DEVMETHOD(device_attach, macio_attach),
93 DEVMETHOD(device_detach, bus_generic_detach),
94 DEVMETHOD(device_shutdown, bus_generic_shutdown),
95 DEVMETHOD(device_suspend, bus_generic_suspend),
96 DEVMETHOD(device_resume, bus_generic_resume),
99 DEVMETHOD(bus_print_child, macio_print_child),
100 DEVMETHOD(bus_probe_nomatch, macio_probe_nomatch),
101 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
102 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
104 DEVMETHOD(bus_alloc_resource, macio_alloc_resource),
105 DEVMETHOD(bus_release_resource, macio_release_resource),
106 DEVMETHOD(bus_activate_resource, macio_activate_resource),
107 DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource),
108 DEVMETHOD(bus_get_resource_list, macio_get_resource_list),
110 /* ofw_bus interface */
111 DEVMETHOD(ofw_bus_get_devinfo, macio_get_devinfo),
112 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
113 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
114 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
115 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
116 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
121 static driver_t macio_pci_driver = {
124 sizeof(struct macio_softc)
127 devclass_t macio_devclass;
129 DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
132 * PCI ID search table
134 static struct macio_pci_dev {
137 } macio_pci_devlist[] = {
138 { 0x0017106b, "Paddington I/O Controller" },
139 { 0x0022106b, "KeyLargo I/O Controller" },
140 { 0x0025106b, "Pangea I/O Controller" },
141 { 0x003e106b, "Intrepid I/O Controller" },
142 { 0x0041106b, "K2 KeyLargo I/O Controller" },
143 { 0x004f106b, "Shasta I/O Controller" },
148 * Devices to exclude from the probe
149 * XXX some of these may be required in the future...
151 #define MACIO_QUIRK_IGNORE 0x00000001
152 #define MACIO_QUIRK_CHILD_HAS_INTR 0x00000002
154 struct macio_quirk_entry {
159 static struct macio_quirk_entry macio_quirks[] = {
160 { "escc-legacy", MACIO_QUIRK_IGNORE },
161 { "timer", MACIO_QUIRK_IGNORE },
162 { "escc", MACIO_QUIRK_CHILD_HAS_INTR },
167 macio_get_quirks(const char *name)
169 struct macio_quirk_entry *mqe;
171 for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
172 if (strcmp(name, mqe->mq_name) == 0)
173 return (mqe->mq_quirks);
179 * Add an interrupt to the dev's resource list if present
182 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
189 if (dinfo->mdi_ninterrupts >= 6) {
190 printf("macio: device has more than 6 interrupts\n");
196 if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent))
197 OF_getprop(iparent, "#interrupt-cells", &icells, sizeof(icells));
199 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
202 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
203 sizeof(*intr), (void **)&intr);
211 for (i = 0; i < nintr; i+=icells) {
212 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
213 dinfo->mdi_ninterrupts, intr[i], intr[i], 1);
215 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr[i];
216 dinfo->mdi_ninterrupts++;
222 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
224 struct macio_reg *reg;
227 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®);
231 for (i = 0; i < nreg; i++) {
232 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
233 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
242 macio_probe(device_t dev)
247 devid = pci_get_devid(dev);
248 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
249 if (devid == macio_pci_devlist[i].mpd_devid) {
250 device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
259 * PCI attach: scan Open Firmware child nodes, and attach these as children
263 macio_attach(device_t dev)
265 struct macio_softc *sc;
266 struct macio_devinfo *dinfo;
274 sc = device_get_softc(dev);
275 root = sc->sc_node = ofw_bus_get_node(dev);
278 * Locate the device node and it's base address
280 if (OF_getprop(root, "assigned-addresses",
281 reg, sizeof(reg)) < sizeof(reg)) {
285 sc->sc_base = reg[2];
286 sc->sc_size = MACIO_REG_SIZE;
288 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
289 sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
290 error = rman_init(&sc->sc_mem_rman);
292 device_printf(dev, "rman_init() failed. error = %d\n", error);
295 error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
298 "rman_manage_region() failed. error = %d\n", error);
303 * Iterate through the sub-devices
305 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
306 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
307 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
309 free(dinfo, M_MACIO);
312 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
313 if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
314 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
315 free(dinfo, M_MACIO);
318 resource_list_init(&dinfo->mdi_resources);
319 dinfo->mdi_ninterrupts = 0;
320 macio_add_intr(child, dinfo);
321 macio_add_reg(child, dinfo);
322 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
323 for (subchild = OF_child(child); subchild != 0;
324 subchild = OF_peer(subchild))
325 macio_add_intr(subchild, dinfo);
326 cdev = device_add_child(dev, NULL, -1);
328 device_printf(dev, "<%s>: device_add_child failed\n",
329 dinfo->mdi_obdinfo.obd_name);
330 resource_list_free(&dinfo->mdi_resources);
331 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
332 free(dinfo, M_MACIO);
335 device_set_ivars(cdev, dinfo);
338 return (bus_generic_attach(dev));
343 macio_print_child(device_t dev, device_t child)
345 struct macio_devinfo *dinfo;
346 struct resource_list *rl;
349 dinfo = device_get_ivars(child);
350 rl = &dinfo->mdi_resources;
352 retval += bus_print_child_header(dev, child);
354 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
355 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
357 retval += bus_print_child_footer(dev, child);
364 macio_probe_nomatch(device_t dev, device_t child)
366 struct macio_devinfo *dinfo;
367 struct resource_list *rl;
371 dinfo = device_get_ivars(child);
372 rl = &dinfo->mdi_resources;
374 if ((type = ofw_bus_get_type(child)) == NULL)
376 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
377 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
378 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
379 printf(" (no driver attached)\n");
384 static struct resource *
385 macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
386 u_long start, u_long end, u_long count, u_int flags)
388 struct macio_softc *sc;
392 u_long adjstart, adjend, adjcount;
393 struct macio_devinfo *dinfo;
394 struct resource_list_entry *rle;
396 sc = device_get_softc(bus);
397 dinfo = device_get_ivars(child);
399 needactivate = flags & RF_ACTIVE;
405 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
408 device_printf(bus, "no rle for %s memory %d\n",
409 device_get_nameunit(child), *rid);
413 if (start < rle->start)
414 adjstart = rle->start;
415 else if (start > rle->end)
420 if (end < rle->start)
422 else if (end > rle->end)
427 adjcount = adjend - adjstart;
429 rm = &sc->sc_mem_rman;
433 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
436 if (dinfo->mdi_ninterrupts >= 6) {
438 "%s has more than 6 interrupts\n",
439 device_get_nameunit(child));
442 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
443 dinfo->mdi_ninterrupts, start, start, 1);
445 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
446 dinfo->mdi_ninterrupts++;
449 return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
450 type, rid, start, end, count, flags));
453 device_printf(bus, "unknown resource request from %s\n",
454 device_get_nameunit(child));
458 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
462 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
463 adjstart, adjend, adjcount, device_get_nameunit(child));
467 rman_set_rid(rv, *rid);
470 if (bus_activate_resource(child, type, *rid, rv) != 0) {
472 "failed to activate resource for %s\n",
473 device_get_nameunit(child));
474 rman_release_resource(rv);
484 macio_release_resource(device_t bus, device_t child, int type, int rid,
485 struct resource *res)
487 if (rman_get_flags(res) & RF_ACTIVE) {
488 int error = bus_deactivate_resource(child, type, rid, res);
493 return (rman_release_resource(res));
498 macio_activate_resource(device_t bus, device_t child, int type, int rid,
499 struct resource *res)
501 struct macio_softc *sc;
504 sc = device_get_softc(bus);
506 if (type == SYS_RES_IRQ)
507 return (bus_activate_resource(bus, type, rid, res));
509 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
510 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
511 (vm_size_t)rman_get_size(res));
514 rman_set_virtual(res, p);
515 rman_set_bustag(res, &bs_le_tag);
516 rman_set_bushandle(res, (u_long)p);
519 return (rman_activate_resource(res));
524 macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
525 struct resource *res)
528 * If this is a memory resource, unmap it.
530 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
533 psize = rman_get_size(res);
534 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
537 return (rman_deactivate_resource(res));
541 static struct resource_list *
542 macio_get_resource_list (device_t dev, device_t child)
544 struct macio_devinfo *dinfo;
546 dinfo = device_get_ivars(child);
547 return (&dinfo->mdi_resources);
550 static const struct ofw_bus_devinfo *
551 macio_get_devinfo(device_t dev, device_t child)
553 struct macio_devinfo *dinfo;
555 dinfo = device_get_ivars(child);
556 return (&dinfo->mdi_obdinfo);