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 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
112 /* ofw_bus interface */
113 DEVMETHOD(ofw_bus_get_devinfo, macio_get_devinfo),
114 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
115 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
116 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
117 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
118 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
123 static driver_t macio_pci_driver = {
126 sizeof(struct macio_softc)
129 devclass_t macio_devclass;
131 DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
134 * PCI ID search table
136 static struct macio_pci_dev {
139 } macio_pci_devlist[] = {
140 { 0x0017106b, "Paddington I/O Controller" },
141 { 0x0022106b, "KeyLargo I/O Controller" },
142 { 0x0025106b, "Pangea I/O Controller" },
143 { 0x003e106b, "Intrepid I/O Controller" },
144 { 0x0041106b, "K2 KeyLargo I/O Controller" },
145 { 0x004f106b, "Shasta I/O Controller" },
150 * Devices to exclude from the probe
151 * XXX some of these may be required in the future...
153 #define MACIO_QUIRK_IGNORE 0x00000001
154 #define MACIO_QUIRK_CHILD_HAS_INTR 0x00000002
155 #define MACIO_QUIRK_USE_CHILD_REG 0x00000004
157 struct macio_quirk_entry {
162 static struct macio_quirk_entry macio_quirks[] = {
163 { "escc-legacy", MACIO_QUIRK_IGNORE },
164 { "timer", MACIO_QUIRK_IGNORE },
165 { "escc", MACIO_QUIRK_CHILD_HAS_INTR },
166 { "i2s", MACIO_QUIRK_CHILD_HAS_INTR |
167 MACIO_QUIRK_USE_CHILD_REG },
172 macio_get_quirks(const char *name)
174 struct macio_quirk_entry *mqe;
176 for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
177 if (strcmp(name, mqe->mq_name) == 0)
178 return (mqe->mq_quirks);
184 * Add an interrupt to the dev's resource list if present
187 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
193 if (dinfo->mdi_ninterrupts >= 6) {
194 printf("macio: device has more than 6 interrupts\n");
198 if (OF_searchprop(devnode, "#interrupt-cells", &icells, sizeof(icells))
202 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
205 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
206 sizeof(*intr), (void **)&intr);
214 for (i = 0; i < nintr; i+=icells) {
215 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
216 dinfo->mdi_ninterrupts, intr[i], intr[i], 1);
218 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr[i];
219 dinfo->mdi_ninterrupts++;
225 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
227 struct macio_reg *reg;
230 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®);
234 for (i = 0; i < nreg; i++) {
235 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
236 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
245 macio_probe(device_t dev)
250 devid = pci_get_devid(dev);
251 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
252 if (devid == macio_pci_devlist[i].mpd_devid) {
253 device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
262 * PCI attach: scan Open Firmware child nodes, and attach these as children
266 macio_attach(device_t dev)
268 struct macio_softc *sc;
269 struct macio_devinfo *dinfo;
277 sc = device_get_softc(dev);
278 root = sc->sc_node = ofw_bus_get_node(dev);
281 * Locate the device node and it's base address
283 if (OF_getprop(root, "assigned-addresses",
284 reg, sizeof(reg)) < sizeof(reg)) {
288 sc->sc_base = reg[2];
289 sc->sc_size = MACIO_REG_SIZE;
291 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
292 sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
293 error = rman_init(&sc->sc_mem_rman);
295 device_printf(dev, "rman_init() failed. error = %d\n", error);
298 error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
301 "rman_manage_region() failed. error = %d\n", error);
306 * Iterate through the sub-devices
308 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
309 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
310 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
312 free(dinfo, M_MACIO);
315 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
316 if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
317 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
318 free(dinfo, M_MACIO);
321 resource_list_init(&dinfo->mdi_resources);
322 dinfo->mdi_ninterrupts = 0;
323 macio_add_intr(child, dinfo);
324 if ((quirks & MACIO_QUIRK_USE_CHILD_REG) != 0)
325 macio_add_reg(OF_child(child), dinfo);
327 macio_add_reg(child, dinfo);
328 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
329 for (subchild = OF_child(child); subchild != 0;
330 subchild = OF_peer(subchild))
331 macio_add_intr(subchild, dinfo);
332 cdev = device_add_child(dev, NULL, -1);
334 device_printf(dev, "<%s>: device_add_child failed\n",
335 dinfo->mdi_obdinfo.obd_name);
336 resource_list_free(&dinfo->mdi_resources);
337 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
338 free(dinfo, M_MACIO);
341 device_set_ivars(cdev, dinfo);
344 return (bus_generic_attach(dev));
349 macio_print_child(device_t dev, device_t child)
351 struct macio_devinfo *dinfo;
352 struct resource_list *rl;
355 dinfo = device_get_ivars(child);
356 rl = &dinfo->mdi_resources;
358 retval += bus_print_child_header(dev, child);
360 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
361 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
363 retval += bus_print_child_footer(dev, child);
370 macio_probe_nomatch(device_t dev, device_t child)
372 struct macio_devinfo *dinfo;
373 struct resource_list *rl;
377 dinfo = device_get_ivars(child);
378 rl = &dinfo->mdi_resources;
380 if ((type = ofw_bus_get_type(child)) == NULL)
382 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
383 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
384 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
385 printf(" (no driver attached)\n");
390 static struct resource *
391 macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
392 u_long start, u_long end, u_long count, u_int flags)
394 struct macio_softc *sc;
398 u_long adjstart, adjend, adjcount;
399 struct macio_devinfo *dinfo;
400 struct resource_list_entry *rle;
402 sc = device_get_softc(bus);
403 dinfo = device_get_ivars(child);
405 needactivate = flags & RF_ACTIVE;
411 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
414 device_printf(bus, "no rle for %s memory %d\n",
415 device_get_nameunit(child), *rid);
419 if (start < rle->start)
420 adjstart = rle->start;
421 else if (start > rle->end)
426 if (end < rle->start)
428 else if (end > rle->end)
433 adjcount = adjend - adjstart;
435 rm = &sc->sc_mem_rman;
439 /* Check for passthrough from subattachments like macgpio */
440 if (device_get_parent(child) != bus)
441 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
442 type, rid, start, end, count, flags);
444 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
447 if (dinfo->mdi_ninterrupts >= 6) {
449 "%s has more than 6 interrupts\n",
450 device_get_nameunit(child));
453 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
454 dinfo->mdi_ninterrupts, start, start, 1);
456 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
457 dinfo->mdi_ninterrupts++;
460 return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
461 type, rid, start, end, count, flags));
464 device_printf(bus, "unknown resource request from %s\n",
465 device_get_nameunit(child));
469 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
473 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
474 adjstart, adjend, adjcount, device_get_nameunit(child));
478 rman_set_rid(rv, *rid);
481 if (bus_activate_resource(child, type, *rid, rv) != 0) {
483 "failed to activate resource for %s\n",
484 device_get_nameunit(child));
485 rman_release_resource(rv);
495 macio_release_resource(device_t bus, device_t child, int type, int rid,
496 struct resource *res)
498 if (rman_get_flags(res) & RF_ACTIVE) {
499 int error = bus_deactivate_resource(child, type, rid, res);
504 return (rman_release_resource(res));
509 macio_activate_resource(device_t bus, device_t child, int type, int rid,
510 struct resource *res)
512 struct macio_softc *sc;
515 sc = device_get_softc(bus);
517 if (type == SYS_RES_IRQ)
518 return (bus_activate_resource(bus, type, rid, res));
520 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
521 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
522 (vm_size_t)rman_get_size(res));
525 rman_set_virtual(res, p);
526 rman_set_bustag(res, &bs_le_tag);
527 rman_set_bushandle(res, (u_long)p);
530 return (rman_activate_resource(res));
535 macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
536 struct resource *res)
539 * If this is a memory resource, unmap it.
541 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
544 psize = rman_get_size(res);
545 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
548 return (rman_deactivate_resource(res));
552 static struct resource_list *
553 macio_get_resource_list (device_t dev, device_t child)
555 struct macio_devinfo *dinfo;
557 dinfo = device_get_ivars(child);
558 return (&dinfo->mdi_resources);
561 static const struct ofw_bus_devinfo *
562 macio_get_devinfo(device_t dev, device_t child)
564 struct macio_devinfo *dinfo;
566 dinfo = device_get_ivars(child);
567 return (&dinfo->mdi_obdinfo);