160727d8bSWarner Losh /*- 2*71e3c308SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*71e3c308SPedro F. Giffuni * 498f8e6c0SBenno Rice * Copyright (C) 2002 Benno Rice. 598f8e6c0SBenno Rice * All rights reserved. 698f8e6c0SBenno Rice * 798f8e6c0SBenno Rice * Redistribution and use in source and binary forms, with or without 898f8e6c0SBenno Rice * modification, are permitted provided that the following conditions 998f8e6c0SBenno Rice * are met: 1098f8e6c0SBenno Rice * 1. Redistributions of source code must retain the above copyright 1198f8e6c0SBenno Rice * notice, this list of conditions and the following disclaimer. 1298f8e6c0SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 1398f8e6c0SBenno Rice * notice, this list of conditions and the following disclaimer in the 1498f8e6c0SBenno Rice * documentation and/or other materials provided with the distribution. 1598f8e6c0SBenno Rice * 1698f8e6c0SBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 1798f8e6c0SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1898f8e6c0SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1998f8e6c0SBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2098f8e6c0SBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2198f8e6c0SBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2298f8e6c0SBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2398f8e6c0SBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2498f8e6c0SBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2598f8e6c0SBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2698f8e6c0SBenno Rice * 2798f8e6c0SBenno Rice * $FreeBSD$ 2898f8e6c0SBenno Rice */ 2998f8e6c0SBenno Rice 3098f8e6c0SBenno Rice #include <sys/param.h> 3198f8e6c0SBenno Rice #include <sys/systm.h> 3240cdee9dSPeter Grehan #include <sys/module.h> 3398f8e6c0SBenno Rice #include <sys/bus.h> 3498f8e6c0SBenno Rice #include <sys/conf.h> 3598f8e6c0SBenno Rice #include <sys/kernel.h> 3698f8e6c0SBenno Rice 3798f8e6c0SBenno Rice #include <dev/ofw/openfirm.h> 3898f8e6c0SBenno Rice #include <dev/ofw/ofw_pci.h> 3951d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 4094b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h> 4198f8e6c0SBenno Rice 4298f8e6c0SBenno Rice #include <dev/pci/pcivar.h> 4398f8e6c0SBenno Rice #include <dev/pci/pcireg.h> 4498f8e6c0SBenno Rice 4598f8e6c0SBenno Rice #include <machine/bus.h> 46ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h> 4798f8e6c0SBenno Rice #include <machine/md_var.h> 48de2fa7b8SMarcel Moolenaar #include <machine/pio.h> 4998f8e6c0SBenno Rice #include <machine/resource.h> 5098f8e6c0SBenno Rice 5198f8e6c0SBenno Rice #include <sys/rman.h> 5298f8e6c0SBenno Rice 5398f8e6c0SBenno Rice #include <powerpc/powermac/uninorthvar.h> 5498f8e6c0SBenno Rice 5598f8e6c0SBenno Rice #include <vm/vm.h> 5698f8e6c0SBenno Rice #include <vm/pmap.h> 5798f8e6c0SBenno Rice 58ca2c1931SNathan Whitehorn /* 59ca2c1931SNathan Whitehorn * Driver for the Uninorth chip itself. 60ca2c1931SNathan Whitehorn */ 6198f8e6c0SBenno Rice 62ca2c1931SNathan Whitehorn static MALLOC_DEFINE(M_UNIN, "unin", "unin device information"); 6398f8e6c0SBenno Rice 6498f8e6c0SBenno Rice /* 6598f8e6c0SBenno Rice * Device interface. 6698f8e6c0SBenno Rice */ 67ca2c1931SNathan Whitehorn 68ca2c1931SNathan Whitehorn static int unin_chip_probe(device_t); 69ca2c1931SNathan Whitehorn static int unin_chip_attach(device_t); 7098f8e6c0SBenno Rice 7198f8e6c0SBenno Rice /* 7298f8e6c0SBenno Rice * Bus interface. 7398f8e6c0SBenno Rice */ 74ca2c1931SNathan Whitehorn static int unin_chip_print_child(device_t dev, device_t child); 75ca2c1931SNathan Whitehorn static void unin_chip_probe_nomatch(device_t, device_t); 76ca2c1931SNathan Whitehorn static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *, 772dd1bdf1SJustin Hibbits rman_res_t, rman_res_t, 782dd1bdf1SJustin Hibbits rman_res_t, u_int); 79ca2c1931SNathan Whitehorn static int unin_chip_activate_resource(device_t, device_t, int, int, 80ca2c1931SNathan Whitehorn struct resource *); 81ca2c1931SNathan Whitehorn static int unin_chip_deactivate_resource(device_t, device_t, int, int, 82ca2c1931SNathan Whitehorn struct resource *); 83ca2c1931SNathan Whitehorn static int unin_chip_release_resource(device_t, device_t, int, int, 84ca2c1931SNathan Whitehorn struct resource *); 85ca2c1931SNathan Whitehorn static struct resource_list *unin_chip_get_resource_list (device_t, device_t); 8698f8e6c0SBenno Rice 8798f8e6c0SBenno Rice /* 8851d163d3SNathan Whitehorn * OFW Bus interface 8951d163d3SNathan Whitehorn */ 9051d163d3SNathan Whitehorn 91ca2c1931SNathan Whitehorn static ofw_bus_get_devinfo_t unin_chip_get_devinfo; 9251d163d3SNathan Whitehorn 9351d163d3SNathan Whitehorn /* 94ca2c1931SNathan Whitehorn * Local routines 9598f8e6c0SBenno Rice */ 96ca2c1931SNathan Whitehorn 97ca2c1931SNathan Whitehorn static void unin_enable_gmac(device_t dev); 98ca2c1931SNathan Whitehorn static void unin_enable_mpic(device_t dev); 9998f8e6c0SBenno Rice 10098f8e6c0SBenno Rice /* 10198f8e6c0SBenno Rice * Driver methods. 10298f8e6c0SBenno Rice */ 103ca2c1931SNathan Whitehorn static device_method_t unin_chip_methods[] = { 104ca2c1931SNathan Whitehorn 10598f8e6c0SBenno Rice /* Device interface */ 106ca2c1931SNathan Whitehorn DEVMETHOD(device_probe, unin_chip_probe), 107ca2c1931SNathan Whitehorn DEVMETHOD(device_attach, unin_chip_attach), 10898f8e6c0SBenno Rice 10998f8e6c0SBenno Rice /* Bus interface */ 110ca2c1931SNathan Whitehorn DEVMETHOD(bus_print_child, unin_chip_print_child), 111ca2c1931SNathan Whitehorn DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch), 11298f8e6c0SBenno Rice DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 11398f8e6c0SBenno Rice DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 11498f8e6c0SBenno Rice 115ca2c1931SNathan Whitehorn DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource), 116ca2c1931SNathan Whitehorn DEVMETHOD(bus_release_resource, unin_chip_release_resource), 117ca2c1931SNathan Whitehorn DEVMETHOD(bus_activate_resource, unin_chip_activate_resource), 118ca2c1931SNathan Whitehorn DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource), 119ca2c1931SNathan Whitehorn DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list), 12098f8e6c0SBenno Rice 1214fe8025dSNathan Whitehorn DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 1224fe8025dSNathan Whitehorn 12351d163d3SNathan Whitehorn /* ofw_bus interface */ 124ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo), 125ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 126ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 127ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 128ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 129ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 13051d163d3SNathan Whitehorn 13198f8e6c0SBenno Rice { 0, 0 } 13298f8e6c0SBenno Rice }; 13398f8e6c0SBenno Rice 134ca2c1931SNathan Whitehorn static driver_t unin_chip_driver = { 135ca2c1931SNathan Whitehorn "unin", 136ca2c1931SNathan Whitehorn unin_chip_methods, 137ca2c1931SNathan Whitehorn sizeof(struct unin_chip_softc) 13898f8e6c0SBenno Rice }; 13998f8e6c0SBenno Rice 140ca2c1931SNathan Whitehorn static devclass_t unin_chip_devclass; 14198f8e6c0SBenno Rice 1424702d987SJustin Hibbits /* 1434702d987SJustin Hibbits * Assume there is only one unin chip in a PowerMac, so that pmu.c functions can 1444702d987SJustin Hibbits * suspend the chip after the whole rest of the device tree is suspended, not 1454702d987SJustin Hibbits * earlier. 1464702d987SJustin Hibbits */ 1474702d987SJustin Hibbits static device_t unin_chip; 1484702d987SJustin Hibbits 14965d08437SNathan Whitehorn DRIVER_MODULE(unin, ofwbus, unin_chip_driver, unin_chip_devclass, 0, 0); 15098f8e6c0SBenno Rice 15151d163d3SNathan Whitehorn /* 152ca2c1931SNathan Whitehorn * Add an interrupt to the dev's resource list if present 15351d163d3SNathan Whitehorn */ 154ca2c1931SNathan Whitehorn static void 155ca2c1931SNathan Whitehorn unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo) 156ca2c1931SNathan Whitehorn { 157eaef5f0aSNathan Whitehorn phandle_t iparent; 158ca2c1931SNathan Whitehorn int *intr; 159ca2c1931SNathan Whitehorn int i, nintr; 160ca2c1931SNathan Whitehorn int icells; 16151d163d3SNathan Whitehorn 162ca2c1931SNathan Whitehorn if (dinfo->udi_ninterrupts >= 6) { 163ca2c1931SNathan Whitehorn printf("unin: device has more than 6 interrupts\n"); 164ca2c1931SNathan Whitehorn return; 16551d163d3SNathan Whitehorn } 16698f8e6c0SBenno Rice 167ca2c1931SNathan Whitehorn nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr), 168ca2c1931SNathan Whitehorn (void **)&intr); 169ca2c1931SNathan Whitehorn if (nintr == -1) { 170ca2c1931SNathan Whitehorn nintr = OF_getprop_alloc(devnode, "AAPL,interrupts", 171ca2c1931SNathan Whitehorn sizeof(*intr), (void **)&intr); 172ca2c1931SNathan Whitehorn if (nintr == -1) 173ca2c1931SNathan Whitehorn return; 174ca2c1931SNathan Whitehorn } 175ca2c1931SNathan Whitehorn 176ca2c1931SNathan Whitehorn if (intr[0] == -1) 177ca2c1931SNathan Whitehorn return; 178ca2c1931SNathan Whitehorn 179ca2c1931SNathan Whitehorn if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) 180ca2c1931SNathan Whitehorn <= 0) 181ca2c1931SNathan Whitehorn panic("Interrupt but no interrupt parent!\n"); 182ca2c1931SNathan Whitehorn 183ca2c1931SNathan Whitehorn if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells)) 184ca2c1931SNathan Whitehorn <= 0) 185ca2c1931SNathan Whitehorn icells = 1; 186ca2c1931SNathan Whitehorn 187ca2c1931SNathan Whitehorn for (i = 0; i < nintr; i+=icells) { 188607ebaafSMarcel Moolenaar u_int irq = MAP_IRQ(iparent, intr[i]); 189607ebaafSMarcel Moolenaar 190ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 191607ebaafSMarcel Moolenaar dinfo->udi_ninterrupts, irq, irq, 1); 192ca2c1931SNathan Whitehorn 193eaef5f0aSNathan Whitehorn if (icells > 1) { 194607ebaafSMarcel Moolenaar powerpc_config_intr(irq, 195eaef5f0aSNathan Whitehorn (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL : 196a7a32004SNathan Whitehorn INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 197eaef5f0aSNathan Whitehorn } 198eaef5f0aSNathan Whitehorn 199607ebaafSMarcel Moolenaar dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 200ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts++; 201ca2c1931SNathan Whitehorn } 202ca2c1931SNathan Whitehorn } 203ca2c1931SNathan Whitehorn 204ca2c1931SNathan Whitehorn static void 205ca2c1931SNathan Whitehorn unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo) 206ca2c1931SNathan Whitehorn { 207ca2c1931SNathan Whitehorn struct unin_chip_reg *reg; 208ca2c1931SNathan Whitehorn int i, nreg; 209ca2c1931SNathan Whitehorn 210ca2c1931SNathan Whitehorn nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®); 211ca2c1931SNathan Whitehorn if (nreg == -1) 212ca2c1931SNathan Whitehorn return; 213ca2c1931SNathan Whitehorn 214ca2c1931SNathan Whitehorn for (i = 0; i < nreg; i++) { 215ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i, 216ca2c1931SNathan Whitehorn reg[i].mr_base, 217ca2c1931SNathan Whitehorn reg[i].mr_base + reg[i].mr_size, 218ca2c1931SNathan Whitehorn reg[i].mr_size); 219ca2c1931SNathan Whitehorn } 220ca2c1931SNathan Whitehorn } 221ca2c1931SNathan Whitehorn 222ca2c1931SNathan Whitehorn static void 2234702d987SJustin Hibbits unin_update_reg(device_t dev, uint32_t regoff, uint32_t set, uint32_t clr) 224ca2c1931SNathan Whitehorn { 2254702d987SJustin Hibbits volatile u_int *reg; 226ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 227ca2c1931SNathan Whitehorn u_int32_t tmpl; 228ca2c1931SNathan Whitehorn 229ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 2304702d987SJustin Hibbits reg = (void *)(sc->sc_addr + regoff); 2314702d987SJustin Hibbits tmpl = inl(reg); 2324702d987SJustin Hibbits tmpl &= ~clr; 2334702d987SJustin Hibbits tmpl |= set; 2344702d987SJustin Hibbits outl(reg, tmpl); 2354702d987SJustin Hibbits } 2364702d987SJustin Hibbits 2374702d987SJustin Hibbits static void 2384702d987SJustin Hibbits unin_enable_gmac(device_t dev) 2394702d987SJustin Hibbits { 2404702d987SJustin Hibbits unin_update_reg(dev, UNIN_CLOCKCNTL, UNIN_CLOCKCNTL_GMAC, 0); 241ca2c1931SNathan Whitehorn } 242ca2c1931SNathan Whitehorn 243ca2c1931SNathan Whitehorn static void 244ca2c1931SNathan Whitehorn unin_enable_mpic(device_t dev) 245ca2c1931SNathan Whitehorn { 2464702d987SJustin Hibbits unin_update_reg(dev, UNIN_TOGGLE_REG, UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE, 0); 247ca2c1931SNathan Whitehorn } 248ca2c1931SNathan Whitehorn 249ca2c1931SNathan Whitehorn static int 250ca2c1931SNathan Whitehorn unin_chip_probe(device_t dev) 251ca2c1931SNathan Whitehorn { 252ca2c1931SNathan Whitehorn const char *name; 253ca2c1931SNathan Whitehorn 254ca2c1931SNathan Whitehorn name = ofw_bus_get_name(dev); 255ca2c1931SNathan Whitehorn 256ca2c1931SNathan Whitehorn if (name == NULL) 25798f8e6c0SBenno Rice return (ENXIO); 25898f8e6c0SBenno Rice 259ca2c1931SNathan Whitehorn if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0 260ca2c1931SNathan Whitehorn && strcmp(name, "u4") != 0) 26198f8e6c0SBenno Rice return (ENXIO); 262ca2c1931SNathan Whitehorn 263ca2c1931SNathan Whitehorn device_set_desc(dev, "Apple UniNorth System Controller"); 264ca2c1931SNathan Whitehorn return (0); 26598f8e6c0SBenno Rice } 26698f8e6c0SBenno Rice 267ca2c1931SNathan Whitehorn static int 268ca2c1931SNathan Whitehorn unin_chip_attach(device_t dev) 269ca2c1931SNathan Whitehorn { 270ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 271ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 272ca2c1931SNathan Whitehorn phandle_t root; 273ca2c1931SNathan Whitehorn phandle_t child; 27443cbf1a6SAndreas Tobler phandle_t iparent; 275ca2c1931SNathan Whitehorn device_t cdev; 2764ee63553SNathan Whitehorn cell_t acells, scells; 277ca2c1931SNathan Whitehorn char compat[32]; 27843cbf1a6SAndreas Tobler char name[32]; 27943cbf1a6SAndreas Tobler u_int irq, reg[3]; 280ca2c1931SNathan Whitehorn int error, i = 0; 281ca2c1931SNathan Whitehorn 282ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 283ca2c1931SNathan Whitehorn root = ofw_bus_get_node(dev); 284ca2c1931SNathan Whitehorn 285ca2c1931SNathan Whitehorn if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8) 28698f8e6c0SBenno Rice return (ENXIO); 287ca2c1931SNathan Whitehorn 2884ee63553SNathan Whitehorn acells = scells = 1; 2894ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#address-cells", &acells, sizeof(acells)); 2904ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#size-cells", &scells, sizeof(scells)); 291ca2c1931SNathan Whitehorn 2924ee63553SNathan Whitehorn i = 0; 2934ee63553SNathan Whitehorn sc->sc_physaddr = reg[i++]; 2944ee63553SNathan Whitehorn if (acells == 2) { 2954ee63553SNathan Whitehorn sc->sc_physaddr <<= 32; 2964ee63553SNathan Whitehorn sc->sc_physaddr |= reg[i++]; 2974ee63553SNathan Whitehorn } 2984ee63553SNathan Whitehorn sc->sc_size = reg[i++]; 2994ee63553SNathan Whitehorn if (scells == 2) { 3004ee63553SNathan Whitehorn sc->sc_size <<= 32; 3014ee63553SNathan Whitehorn sc->sc_size |= reg[i++]; 3024ee63553SNathan Whitehorn } 303ca2c1931SNathan Whitehorn 30498f8e6c0SBenno Rice sc->sc_mem_rman.rm_type = RMAN_ARRAY; 305ca2c1931SNathan Whitehorn sc->sc_mem_rman.rm_descr = "UniNorth Device Memory"; 306ca2c1931SNathan Whitehorn 307bda386dbSKevin Lo error = rman_init(&sc->sc_mem_rman); 308ca2c1931SNathan Whitehorn 309bda386dbSKevin Lo if (error) { 310bda386dbSKevin Lo device_printf(dev, "rman_init() failed. error = %d\n", error); 311bda386dbSKevin Lo return (error); 31298f8e6c0SBenno Rice } 313ca2c1931SNathan Whitehorn 314ca2c1931SNathan Whitehorn error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr, 315ca2c1931SNathan Whitehorn sc->sc_physaddr + sc->sc_size - 1); 316bda386dbSKevin Lo if (error) { 31798f8e6c0SBenno Rice device_printf(dev, 318ca2c1931SNathan Whitehorn "rman_manage_region() failed. error = %d\n", 319ca2c1931SNathan Whitehorn error); 320bda386dbSKevin Lo return (error); 32198f8e6c0SBenno Rice } 32298f8e6c0SBenno Rice 3234702d987SJustin Hibbits if (unin_chip == NULL) 3244702d987SJustin Hibbits unin_chip = dev; 3254702d987SJustin Hibbits 326d48bbef3SPeter Grehan /* 327ca2c1931SNathan Whitehorn * Iterate through the sub-devices 328d48bbef3SPeter Grehan */ 329ca2c1931SNathan Whitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 330ca2c1931SNathan Whitehorn dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO); 331ca2c1931SNathan Whitehorn if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child) 332ca2c1931SNathan Whitehorn != 0) 333ca2c1931SNathan Whitehorn { 334ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 335ca2c1931SNathan Whitehorn continue; 336ca2c1931SNathan Whitehorn } 337d48bbef3SPeter Grehan 338ca2c1931SNathan Whitehorn resource_list_init(&dinfo->udi_resources); 339ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts = 0; 340ca2c1931SNathan Whitehorn unin_chip_add_intr(child, dinfo); 341ca2c1931SNathan Whitehorn 34243cbf1a6SAndreas Tobler /* 34343cbf1a6SAndreas Tobler * Some Apple machines do have a bug in OF, they miss 34443cbf1a6SAndreas Tobler * the interrupt entries on the U3 I2C node. That means they 34543cbf1a6SAndreas Tobler * do not have an entry with number of interrupts nor the 34643cbf1a6SAndreas Tobler * entry of the interrupt parent handle. 34743cbf1a6SAndreas Tobler * We define an interrupt and hardwire it to the /u3/mpic 34843cbf1a6SAndreas Tobler * handle. 34943cbf1a6SAndreas Tobler */ 35043cbf1a6SAndreas Tobler 35143cbf1a6SAndreas Tobler if (OF_getprop(child, "name", name, sizeof(name)) <= 0) 35243cbf1a6SAndreas Tobler device_printf(dev, "device has no name!\n"); 35343cbf1a6SAndreas Tobler if (dinfo->udi_ninterrupts == 0 && 35443cbf1a6SAndreas Tobler (strcmp(name, "i2c-bus") == 0 || 35543cbf1a6SAndreas Tobler strcmp(name, "i2c") == 0)) { 35643cbf1a6SAndreas Tobler if (OF_getprop(child, "interrupt-parent", &iparent, 35743cbf1a6SAndreas Tobler sizeof(iparent)) <= 0) { 35843cbf1a6SAndreas Tobler iparent = OF_finddevice("/u3/mpic"); 35943cbf1a6SAndreas Tobler device_printf(dev, "Set /u3/mpic as iparent!\n"); 36043cbf1a6SAndreas Tobler } 36143cbf1a6SAndreas Tobler /* Add an interrupt number 0 to the parent. */ 36243cbf1a6SAndreas Tobler irq = MAP_IRQ(iparent, 0); 36343cbf1a6SAndreas Tobler resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 36443cbf1a6SAndreas Tobler dinfo->udi_ninterrupts, irq, irq, 1); 36543cbf1a6SAndreas Tobler dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 36643cbf1a6SAndreas Tobler dinfo->udi_ninterrupts++; 36743cbf1a6SAndreas Tobler } 36843cbf1a6SAndreas Tobler 369ca2c1931SNathan Whitehorn unin_chip_add_reg(child, dinfo); 370ca2c1931SNathan Whitehorn 371ca2c1931SNathan Whitehorn cdev = device_add_child(dev, NULL, -1); 372ca2c1931SNathan Whitehorn if (cdev == NULL) { 373ca2c1931SNathan Whitehorn device_printf(dev, "<%s>: device_add_child failed\n", 374ca2c1931SNathan Whitehorn dinfo->udi_obdinfo.obd_name); 375ca2c1931SNathan Whitehorn resource_list_free(&dinfo->udi_resources); 376ca2c1931SNathan Whitehorn ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo); 377ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 378ca2c1931SNathan Whitehorn continue; 379ca2c1931SNathan Whitehorn } 380ca2c1931SNathan Whitehorn 381ca2c1931SNathan Whitehorn device_set_ivars(cdev, dinfo); 382ca2c1931SNathan Whitehorn } 383ca2c1931SNathan Whitehorn 384ca2c1931SNathan Whitehorn /* 385ca2c1931SNathan Whitehorn * Only map the first page, since that is where the registers 386ca2c1931SNathan Whitehorn * of interest lie. 387ca2c1931SNathan Whitehorn */ 388ca2c1931SNathan Whitehorn sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE); 389ca2c1931SNathan Whitehorn 390ca2c1931SNathan Whitehorn sc->sc_version = *(u_int *)sc->sc_addr; 391ca2c1931SNathan Whitehorn device_printf(dev, "Version %d\n", sc->sc_version); 392ca2c1931SNathan Whitehorn 393ca2c1931SNathan Whitehorn /* 394ca2c1931SNathan Whitehorn * Enable the GMAC Ethernet cell and the integrated OpenPIC 395ca2c1931SNathan Whitehorn * if Open Firmware says they are used. 396ca2c1931SNathan Whitehorn */ 397ca2c1931SNathan Whitehorn for (child = OF_child(root); child; child = OF_peer(child)) { 398d48bbef3SPeter Grehan memset(compat, 0, sizeof(compat)); 399d48bbef3SPeter Grehan OF_getprop(child, "compatible", compat, sizeof(compat)); 400ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 401ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 402ca2c1931SNathan Whitehorn if (strcmp(compat, "chrp,open-pic") == 0) 403ca2c1931SNathan Whitehorn unin_enable_mpic(dev); 404d48bbef3SPeter Grehan } 405d48bbef3SPeter Grehan 406ca2c1931SNathan Whitehorn /* 407ca2c1931SNathan Whitehorn * GMAC lives under the PCI bus, so just check if enet is gmac. 408ca2c1931SNathan Whitehorn */ 409ca2c1931SNathan Whitehorn child = OF_finddevice("enet"); 410ca2c1931SNathan Whitehorn memset(compat, 0, sizeof(compat)); 411ca2c1931SNathan Whitehorn OF_getprop(child, "compatible", compat, sizeof(compat)); 412ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 413ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 41494b4a038SNathan Whitehorn 41598f8e6c0SBenno Rice return (bus_generic_attach(dev)); 41698f8e6c0SBenno Rice } 41798f8e6c0SBenno Rice 41898f8e6c0SBenno Rice static int 419ca2c1931SNathan Whitehorn unin_chip_print_child(device_t dev, device_t child) 42098f8e6c0SBenno Rice { 421ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 422ca2c1931SNathan Whitehorn struct resource_list *rl; 423ca2c1931SNathan Whitehorn int retval = 0; 42498f8e6c0SBenno Rice 425ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 426ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 42798f8e6c0SBenno Rice 428ca2c1931SNathan Whitehorn retval += bus_print_child_header(dev, child); 42998f8e6c0SBenno Rice 430f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx"); 431f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 43298f8e6c0SBenno Rice 433ca2c1931SNathan Whitehorn retval += bus_print_child_footer(dev, child); 43498f8e6c0SBenno Rice 435ca2c1931SNathan Whitehorn return (retval); 43698f8e6c0SBenno Rice } 43798f8e6c0SBenno Rice 43898f8e6c0SBenno Rice static void 439ca2c1931SNathan Whitehorn unin_chip_probe_nomatch(device_t dev, device_t child) 44098f8e6c0SBenno Rice { 441ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 442ca2c1931SNathan Whitehorn struct resource_list *rl; 443ca2c1931SNathan Whitehorn const char *type; 44498f8e6c0SBenno Rice 445ca2c1931SNathan Whitehorn if (bootverbose) { 446ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 447ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 44898f8e6c0SBenno Rice 449ca2c1931SNathan Whitehorn if ((type = ofw_bus_get_type(child)) == NULL) 450ca2c1931SNathan Whitehorn type = "(unknown)"; 451ca2c1931SNathan Whitehorn device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 452f8fd3fb5SJustin Hibbits resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx"); 453f8fd3fb5SJustin Hibbits resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 454ca2c1931SNathan Whitehorn printf(" (no driver attached)\n"); 45598f8e6c0SBenno Rice } 45698f8e6c0SBenno Rice } 45798f8e6c0SBenno Rice 45898f8e6c0SBenno Rice 45998f8e6c0SBenno Rice static struct resource * 460ca2c1931SNathan Whitehorn unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid, 4612dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, 4622dd1bdf1SJustin Hibbits u_int flags) 46398f8e6c0SBenno Rice { 464ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 465ca2c1931SNathan Whitehorn int needactivate; 46698f8e6c0SBenno Rice struct resource *rv; 46798f8e6c0SBenno Rice struct rman *rm; 468ca2c1931SNathan Whitehorn u_long adjstart, adjend, adjcount; 469ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 470ca2c1931SNathan Whitehorn struct resource_list_entry *rle; 471ca2c1931SNathan Whitehorn 472ca2c1931SNathan Whitehorn sc = device_get_softc(bus); 473ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 47498f8e6c0SBenno Rice 47598f8e6c0SBenno Rice needactivate = flags & RF_ACTIVE; 47698f8e6c0SBenno Rice flags &= ~RF_ACTIVE; 47798f8e6c0SBenno Rice 47898f8e6c0SBenno Rice switch (type) { 47998f8e6c0SBenno Rice case SYS_RES_MEMORY: 480ca2c1931SNathan Whitehorn case SYS_RES_IOPORT: 481ca2c1931SNathan Whitehorn rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY, 482ca2c1931SNathan Whitehorn *rid); 483ca2c1931SNathan Whitehorn if (rle == NULL) { 484ca2c1931SNathan Whitehorn device_printf(bus, "no rle for %s memory %d\n", 485ca2c1931SNathan Whitehorn device_get_nameunit(child), *rid); 486ca2c1931SNathan Whitehorn return (NULL); 487ca2c1931SNathan Whitehorn } 488ca2c1931SNathan Whitehorn 489ca2c1931SNathan Whitehorn rle->end = rle->end - 1; /* Hack? */ 490ca2c1931SNathan Whitehorn 491ca2c1931SNathan Whitehorn if (start < rle->start) 492ca2c1931SNathan Whitehorn adjstart = rle->start; 493ca2c1931SNathan Whitehorn else if (start > rle->end) 494ca2c1931SNathan Whitehorn adjstart = rle->end; 495ca2c1931SNathan Whitehorn else 496ca2c1931SNathan Whitehorn adjstart = start; 497ca2c1931SNathan Whitehorn 498ca2c1931SNathan Whitehorn if (end < rle->start) 499ca2c1931SNathan Whitehorn adjend = rle->start; 500ca2c1931SNathan Whitehorn else if (end > rle->end) 501ca2c1931SNathan Whitehorn adjend = rle->end; 502ca2c1931SNathan Whitehorn else 503ca2c1931SNathan Whitehorn adjend = end; 504ca2c1931SNathan Whitehorn 505ca2c1931SNathan Whitehorn adjcount = adjend - adjstart; 506ca2c1931SNathan Whitehorn 50798f8e6c0SBenno Rice rm = &sc->sc_mem_rman; 50898f8e6c0SBenno Rice break; 509714aa5b9SAndrew Gallatin 51098f8e6c0SBenno Rice case SYS_RES_IRQ: 511ca2c1931SNathan Whitehorn /* Check for passthrough from subattachments. */ 512ca2c1931SNathan Whitehorn if (device_get_parent(child) != bus) 513ca2c1931SNathan Whitehorn return BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 514ca2c1931SNathan Whitehorn type, rid, start, end, count, 515ca2c1931SNathan Whitehorn flags); 516de2fa7b8SMarcel Moolenaar 517ca2c1931SNathan Whitehorn rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ, 518ca2c1931SNathan Whitehorn *rid); 519ca2c1931SNathan Whitehorn if (rle == NULL) { 520ca2c1931SNathan Whitehorn if (dinfo->udi_ninterrupts >= 6) { 521ca2c1931SNathan Whitehorn device_printf(bus, 522ca2c1931SNathan Whitehorn "%s has more than 6 interrupts\n", 523ca2c1931SNathan Whitehorn device_get_nameunit(child)); 524ca2c1931SNathan Whitehorn return (NULL); 525ca2c1931SNathan Whitehorn } 526ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 527ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts, start, start, 528ca2c1931SNathan Whitehorn 1); 529ca2c1931SNathan Whitehorn 530ca2c1931SNathan Whitehorn dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start; 531ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts++; 532ca2c1931SNathan Whitehorn } 533ca2c1931SNathan Whitehorn 534ca2c1931SNathan Whitehorn return (resource_list_alloc(&dinfo->udi_resources, bus, child, 535ca2c1931SNathan Whitehorn type, rid, start, end, count, 536ca2c1931SNathan Whitehorn flags)); 53798f8e6c0SBenno Rice default: 53898f8e6c0SBenno Rice device_printf(bus, "unknown resource request from %s\n", 53998f8e6c0SBenno Rice device_get_nameunit(child)); 54098f8e6c0SBenno Rice return (NULL); 54198f8e6c0SBenno Rice } 54298f8e6c0SBenno Rice 543ca2c1931SNathan Whitehorn rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags, 544ca2c1931SNathan Whitehorn child); 54598f8e6c0SBenno Rice if (rv == NULL) { 546ca2c1931SNathan Whitehorn device_printf(bus, 547ca2c1931SNathan Whitehorn "failed to reserve resource %#lx - %#lx (%#lx)" 548ca2c1931SNathan Whitehorn " for %s\n", adjstart, adjend, adjcount, 54998f8e6c0SBenno Rice device_get_nameunit(child)); 55098f8e6c0SBenno Rice return (NULL); 55198f8e6c0SBenno Rice } 55298f8e6c0SBenno Rice 553bdfb66f8SWarner Losh rman_set_rid(rv, *rid); 55498f8e6c0SBenno Rice 55598f8e6c0SBenno Rice if (needactivate) { 55698f8e6c0SBenno Rice if (bus_activate_resource(child, type, *rid, rv) != 0) { 55798f8e6c0SBenno Rice device_printf(bus, 55898f8e6c0SBenno Rice "failed to activate resource for %s\n", 55998f8e6c0SBenno Rice device_get_nameunit(child)); 56098f8e6c0SBenno Rice rman_release_resource(rv); 56198f8e6c0SBenno Rice return (NULL); 56298f8e6c0SBenno Rice } 56398f8e6c0SBenno Rice } 56498f8e6c0SBenno Rice 56598f8e6c0SBenno Rice return (rv); 56698f8e6c0SBenno Rice } 56798f8e6c0SBenno Rice 56898f8e6c0SBenno Rice static int 569ca2c1931SNathan Whitehorn unin_chip_release_resource(device_t bus, device_t child, int type, int rid, 57098f8e6c0SBenno Rice struct resource *res) 57198f8e6c0SBenno Rice { 572ca2c1931SNathan Whitehorn if (rman_get_flags(res) & RF_ACTIVE) { 573ca2c1931SNathan Whitehorn int error = bus_deactivate_resource(child, type, rid, res); 574ca2c1931SNathan Whitehorn if (error) 575ca2c1931SNathan Whitehorn return error; 576ca2c1931SNathan Whitehorn } 577ca2c1931SNathan Whitehorn 578ca2c1931SNathan Whitehorn return (rman_release_resource(res)); 579ca2c1931SNathan Whitehorn } 580ca2c1931SNathan Whitehorn 581ca2c1931SNathan Whitehorn static int 582ca2c1931SNathan Whitehorn unin_chip_activate_resource(device_t bus, device_t child, int type, int rid, 583ca2c1931SNathan Whitehorn struct resource *res) 584ca2c1931SNathan Whitehorn { 58598f8e6c0SBenno Rice void *p; 586714aa5b9SAndrew Gallatin 58798f8e6c0SBenno Rice if (type == SYS_RES_IRQ) 58898f8e6c0SBenno Rice return (bus_activate_resource(bus, type, rid, res)); 58998f8e6c0SBenno Rice 590ca2c1931SNathan Whitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 591714aa5b9SAndrew Gallatin vm_offset_t start; 592714aa5b9SAndrew Gallatin 593714aa5b9SAndrew Gallatin start = (vm_offset_t) rman_get_start(res); 594714aa5b9SAndrew Gallatin 595714aa5b9SAndrew Gallatin if (bootverbose) 596da1b038aSJustin Hibbits printf("unin mapdev: start %zx, len %jd\n", start, 597714aa5b9SAndrew Gallatin rman_get_size(res)); 598714aa5b9SAndrew Gallatin 599714aa5b9SAndrew Gallatin p = pmap_mapdev(start, (vm_size_t) rman_get_size(res)); 60098f8e6c0SBenno Rice if (p == NULL) 60198f8e6c0SBenno Rice return (ENOMEM); 60298f8e6c0SBenno Rice rman_set_virtual(res, p); 603ca2c1931SNathan Whitehorn rman_set_bustag(res, &bs_be_tag); 60498f8e6c0SBenno Rice rman_set_bushandle(res, (u_long)p); 60598f8e6c0SBenno Rice } 60698f8e6c0SBenno Rice 60798f8e6c0SBenno Rice return (rman_activate_resource(res)); 60898f8e6c0SBenno Rice } 60998f8e6c0SBenno Rice 610ca2c1931SNathan Whitehorn 61198f8e6c0SBenno Rice static int 612ca2c1931SNathan Whitehorn unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid, 613ca2c1931SNathan Whitehorn struct resource *res) 61498f8e6c0SBenno Rice { 61598f8e6c0SBenno Rice /* 616ca2c1931SNathan Whitehorn * If this is a memory resource, unmap it. 61798f8e6c0SBenno Rice */ 618ca2c1931SNathan Whitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 619ca2c1931SNathan Whitehorn u_int32_t psize; 62098f8e6c0SBenno Rice 621ca2c1931SNathan Whitehorn psize = rman_get_size(res); 622ca2c1931SNathan Whitehorn pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 62398f8e6c0SBenno Rice } 62498f8e6c0SBenno Rice 625ca2c1931SNathan Whitehorn return (rman_deactivate_resource(res)); 62698f8e6c0SBenno Rice } 62798f8e6c0SBenno Rice 628ca2c1931SNathan Whitehorn 629ca2c1931SNathan Whitehorn static struct resource_list * 630ca2c1931SNathan Whitehorn unin_chip_get_resource_list (device_t dev, device_t child) 63151d163d3SNathan Whitehorn { 632ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 63351d163d3SNathan Whitehorn 634ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 635ca2c1931SNathan Whitehorn return (&dinfo->udi_resources); 63651d163d3SNathan Whitehorn } 63751d163d3SNathan Whitehorn 638ca2c1931SNathan Whitehorn static const struct ofw_bus_devinfo * 639ca2c1931SNathan Whitehorn unin_chip_get_devinfo(device_t dev, device_t child) 64098f8e6c0SBenno Rice { 641ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 64298f8e6c0SBenno Rice 643ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 644ca2c1931SNathan Whitehorn return (&dinfo->udi_obdinfo); 64598f8e6c0SBenno Rice } 64698f8e6c0SBenno Rice 6474702d987SJustin Hibbits int 6484702d987SJustin Hibbits unin_chip_wake(device_t dev) 6494702d987SJustin Hibbits { 6504702d987SJustin Hibbits 6514702d987SJustin Hibbits if (dev == NULL) 6524702d987SJustin Hibbits dev = unin_chip; 6534702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_NORMAL, UNIN_PWR_MASK); 6544702d987SJustin Hibbits DELAY(10); 6554702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_RUNNING, 0); 6564702d987SJustin Hibbits DELAY(100); 6574702d987SJustin Hibbits 6584702d987SJustin Hibbits return (0); 6594702d987SJustin Hibbits } 6604702d987SJustin Hibbits 6614702d987SJustin Hibbits int 6624702d987SJustin Hibbits unin_chip_sleep(device_t dev, int idle) 6634702d987SJustin Hibbits { 6644702d987SJustin Hibbits if (dev == NULL) 6654702d987SJustin Hibbits dev = unin_chip; 6664702d987SJustin Hibbits 6674702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_SLEEPING, 0); 6684702d987SJustin Hibbits DELAY(10); 6694702d987SJustin Hibbits if (idle) 6704702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_IDLE2, UNIN_PWR_MASK); 6714702d987SJustin Hibbits else 6724702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_SLEEP, UNIN_PWR_MASK); 6734702d987SJustin Hibbits DELAY(10); 6744702d987SJustin Hibbits 6754702d987SJustin Hibbits return (0); 6764702d987SJustin Hibbits } 677