160727d8bSWarner Losh /*- 298f8e6c0SBenno Rice * Copyright (C) 2002 Benno Rice. 398f8e6c0SBenno Rice * All rights reserved. 498f8e6c0SBenno Rice * 598f8e6c0SBenno Rice * Redistribution and use in source and binary forms, with or without 698f8e6c0SBenno Rice * modification, are permitted provided that the following conditions 798f8e6c0SBenno Rice * are met: 898f8e6c0SBenno Rice * 1. Redistributions of source code must retain the above copyright 998f8e6c0SBenno Rice * notice, this list of conditions and the following disclaimer. 1098f8e6c0SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 1198f8e6c0SBenno Rice * notice, this list of conditions and the following disclaimer in the 1298f8e6c0SBenno Rice * documentation and/or other materials provided with the distribution. 1398f8e6c0SBenno Rice * 1498f8e6c0SBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 1598f8e6c0SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1698f8e6c0SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1798f8e6c0SBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1898f8e6c0SBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1998f8e6c0SBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2098f8e6c0SBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2198f8e6c0SBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2298f8e6c0SBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2398f8e6c0SBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2498f8e6c0SBenno Rice * 2598f8e6c0SBenno Rice * $FreeBSD$ 2698f8e6c0SBenno Rice */ 2798f8e6c0SBenno Rice 2898f8e6c0SBenno Rice #include <sys/param.h> 2998f8e6c0SBenno Rice #include <sys/systm.h> 3040cdee9dSPeter Grehan #include <sys/module.h> 3198f8e6c0SBenno Rice #include <sys/bus.h> 3298f8e6c0SBenno Rice #include <sys/conf.h> 3398f8e6c0SBenno Rice #include <sys/kernel.h> 3498f8e6c0SBenno Rice 3598f8e6c0SBenno Rice #include <dev/ofw/openfirm.h> 3698f8e6c0SBenno Rice #include <dev/ofw/ofw_pci.h> 3751d163d3SNathan Whitehorn #include <dev/ofw/ofw_bus.h> 3894b4a038SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h> 3998f8e6c0SBenno Rice 4098f8e6c0SBenno Rice #include <dev/pci/pcivar.h> 4198f8e6c0SBenno Rice #include <dev/pci/pcireg.h> 4298f8e6c0SBenno Rice 4398f8e6c0SBenno Rice #include <machine/bus.h> 44ca2c1931SNathan Whitehorn #include <machine/intr_machdep.h> 4598f8e6c0SBenno Rice #include <machine/md_var.h> 46de2fa7b8SMarcel Moolenaar #include <machine/pio.h> 4798f8e6c0SBenno Rice #include <machine/resource.h> 4898f8e6c0SBenno Rice 4998f8e6c0SBenno Rice #include <sys/rman.h> 5098f8e6c0SBenno Rice 5198f8e6c0SBenno Rice #include <powerpc/powermac/uninorthvar.h> 5298f8e6c0SBenno Rice 5398f8e6c0SBenno Rice #include <vm/vm.h> 5498f8e6c0SBenno Rice #include <vm/pmap.h> 5598f8e6c0SBenno Rice 56ca2c1931SNathan Whitehorn /* 57ca2c1931SNathan Whitehorn * Driver for the Uninorth chip itself. 58ca2c1931SNathan Whitehorn */ 5998f8e6c0SBenno Rice 60ca2c1931SNathan Whitehorn static MALLOC_DEFINE(M_UNIN, "unin", "unin device information"); 6198f8e6c0SBenno Rice 6298f8e6c0SBenno Rice /* 6398f8e6c0SBenno Rice * Device interface. 6498f8e6c0SBenno Rice */ 65ca2c1931SNathan Whitehorn 66ca2c1931SNathan Whitehorn static int unin_chip_probe(device_t); 67ca2c1931SNathan Whitehorn static int unin_chip_attach(device_t); 6898f8e6c0SBenno Rice 6998f8e6c0SBenno Rice /* 7098f8e6c0SBenno Rice * Bus interface. 7198f8e6c0SBenno Rice */ 72ca2c1931SNathan Whitehorn static int unin_chip_print_child(device_t dev, device_t child); 73ca2c1931SNathan Whitehorn static void unin_chip_probe_nomatch(device_t, device_t); 74ca2c1931SNathan Whitehorn static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *, 752dd1bdf1SJustin Hibbits rman_res_t, rman_res_t, 762dd1bdf1SJustin Hibbits rman_res_t, u_int); 77ca2c1931SNathan Whitehorn static int unin_chip_activate_resource(device_t, device_t, int, int, 78ca2c1931SNathan Whitehorn struct resource *); 79ca2c1931SNathan Whitehorn static int unin_chip_deactivate_resource(device_t, device_t, int, int, 80ca2c1931SNathan Whitehorn struct resource *); 81ca2c1931SNathan Whitehorn static int unin_chip_release_resource(device_t, device_t, int, int, 82ca2c1931SNathan Whitehorn struct resource *); 83ca2c1931SNathan Whitehorn static struct resource_list *unin_chip_get_resource_list (device_t, device_t); 8498f8e6c0SBenno Rice 8598f8e6c0SBenno Rice /* 8651d163d3SNathan Whitehorn * OFW Bus interface 8751d163d3SNathan Whitehorn */ 8851d163d3SNathan Whitehorn 89ca2c1931SNathan Whitehorn static ofw_bus_get_devinfo_t unin_chip_get_devinfo; 9051d163d3SNathan Whitehorn 9151d163d3SNathan Whitehorn /* 92ca2c1931SNathan Whitehorn * Local routines 9398f8e6c0SBenno Rice */ 94ca2c1931SNathan Whitehorn 95ca2c1931SNathan Whitehorn static void unin_enable_gmac(device_t dev); 96ca2c1931SNathan Whitehorn static void unin_enable_mpic(device_t dev); 9798f8e6c0SBenno Rice 9898f8e6c0SBenno Rice /* 9998f8e6c0SBenno Rice * Driver methods. 10098f8e6c0SBenno Rice */ 101ca2c1931SNathan Whitehorn static device_method_t unin_chip_methods[] = { 102ca2c1931SNathan Whitehorn 10398f8e6c0SBenno Rice /* Device interface */ 104ca2c1931SNathan Whitehorn DEVMETHOD(device_probe, unin_chip_probe), 105ca2c1931SNathan Whitehorn DEVMETHOD(device_attach, unin_chip_attach), 10698f8e6c0SBenno Rice 10798f8e6c0SBenno Rice /* Bus interface */ 108ca2c1931SNathan Whitehorn DEVMETHOD(bus_print_child, unin_chip_print_child), 109ca2c1931SNathan Whitehorn DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch), 11098f8e6c0SBenno Rice DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 11198f8e6c0SBenno Rice DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 11298f8e6c0SBenno Rice 113ca2c1931SNathan Whitehorn DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource), 114ca2c1931SNathan Whitehorn DEVMETHOD(bus_release_resource, unin_chip_release_resource), 115ca2c1931SNathan Whitehorn DEVMETHOD(bus_activate_resource, unin_chip_activate_resource), 116ca2c1931SNathan Whitehorn DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource), 117ca2c1931SNathan Whitehorn DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list), 11898f8e6c0SBenno Rice 1194fe8025dSNathan Whitehorn DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 1204fe8025dSNathan Whitehorn 12151d163d3SNathan Whitehorn /* ofw_bus interface */ 122ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo), 123ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 124ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 125ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 126ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 127ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 12851d163d3SNathan Whitehorn 12998f8e6c0SBenno Rice { 0, 0 } 13098f8e6c0SBenno Rice }; 13198f8e6c0SBenno Rice 132ca2c1931SNathan Whitehorn static driver_t unin_chip_driver = { 133ca2c1931SNathan Whitehorn "unin", 134ca2c1931SNathan Whitehorn unin_chip_methods, 135ca2c1931SNathan Whitehorn sizeof(struct unin_chip_softc) 13698f8e6c0SBenno Rice }; 13798f8e6c0SBenno Rice 138ca2c1931SNathan Whitehorn static devclass_t unin_chip_devclass; 13998f8e6c0SBenno Rice 1404702d987SJustin Hibbits /* 1414702d987SJustin Hibbits * Assume there is only one unin chip in a PowerMac, so that pmu.c functions can 1424702d987SJustin Hibbits * suspend the chip after the whole rest of the device tree is suspended, not 1434702d987SJustin Hibbits * earlier. 1444702d987SJustin Hibbits */ 1454702d987SJustin Hibbits static device_t unin_chip; 1464702d987SJustin Hibbits 14765d08437SNathan Whitehorn DRIVER_MODULE(unin, ofwbus, unin_chip_driver, unin_chip_devclass, 0, 0); 14898f8e6c0SBenno Rice 14951d163d3SNathan Whitehorn /* 150ca2c1931SNathan Whitehorn * Add an interrupt to the dev's resource list if present 15151d163d3SNathan Whitehorn */ 152ca2c1931SNathan Whitehorn static void 153ca2c1931SNathan Whitehorn unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo) 154ca2c1931SNathan Whitehorn { 155eaef5f0aSNathan Whitehorn phandle_t iparent; 156ca2c1931SNathan Whitehorn int *intr; 157ca2c1931SNathan Whitehorn int i, nintr; 158ca2c1931SNathan Whitehorn int icells; 15951d163d3SNathan Whitehorn 160ca2c1931SNathan Whitehorn if (dinfo->udi_ninterrupts >= 6) { 161ca2c1931SNathan Whitehorn printf("unin: device has more than 6 interrupts\n"); 162ca2c1931SNathan Whitehorn return; 16351d163d3SNathan Whitehorn } 16498f8e6c0SBenno Rice 165ca2c1931SNathan Whitehorn nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr), 166ca2c1931SNathan Whitehorn (void **)&intr); 167ca2c1931SNathan Whitehorn if (nintr == -1) { 168ca2c1931SNathan Whitehorn nintr = OF_getprop_alloc(devnode, "AAPL,interrupts", 169ca2c1931SNathan Whitehorn sizeof(*intr), (void **)&intr); 170ca2c1931SNathan Whitehorn if (nintr == -1) 171ca2c1931SNathan Whitehorn return; 172ca2c1931SNathan Whitehorn } 173ca2c1931SNathan Whitehorn 174ca2c1931SNathan Whitehorn if (intr[0] == -1) 175ca2c1931SNathan Whitehorn return; 176ca2c1931SNathan Whitehorn 177ca2c1931SNathan Whitehorn if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) 178ca2c1931SNathan Whitehorn <= 0) 179ca2c1931SNathan Whitehorn panic("Interrupt but no interrupt parent!\n"); 180ca2c1931SNathan Whitehorn 181ca2c1931SNathan Whitehorn if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells)) 182ca2c1931SNathan Whitehorn <= 0) 183ca2c1931SNathan Whitehorn icells = 1; 184ca2c1931SNathan Whitehorn 185ca2c1931SNathan Whitehorn for (i = 0; i < nintr; i+=icells) { 186607ebaafSMarcel Moolenaar u_int irq = MAP_IRQ(iparent, intr[i]); 187607ebaafSMarcel Moolenaar 188ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 189607ebaafSMarcel Moolenaar dinfo->udi_ninterrupts, irq, irq, 1); 190ca2c1931SNathan Whitehorn 191eaef5f0aSNathan Whitehorn if (icells > 1) { 192607ebaafSMarcel Moolenaar powerpc_config_intr(irq, 193eaef5f0aSNathan Whitehorn (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL : 194a7a32004SNathan Whitehorn INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 195eaef5f0aSNathan Whitehorn } 196eaef5f0aSNathan Whitehorn 197607ebaafSMarcel Moolenaar dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 198ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts++; 199ca2c1931SNathan Whitehorn } 200ca2c1931SNathan Whitehorn } 201ca2c1931SNathan Whitehorn 202ca2c1931SNathan Whitehorn static void 203ca2c1931SNathan Whitehorn unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo) 204ca2c1931SNathan Whitehorn { 205ca2c1931SNathan Whitehorn struct unin_chip_reg *reg; 206ca2c1931SNathan Whitehorn int i, nreg; 207ca2c1931SNathan Whitehorn 208ca2c1931SNathan Whitehorn nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®); 209ca2c1931SNathan Whitehorn if (nreg == -1) 210ca2c1931SNathan Whitehorn return; 211ca2c1931SNathan Whitehorn 212ca2c1931SNathan Whitehorn for (i = 0; i < nreg; i++) { 213ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i, 214ca2c1931SNathan Whitehorn reg[i].mr_base, 215ca2c1931SNathan Whitehorn reg[i].mr_base + reg[i].mr_size, 216ca2c1931SNathan Whitehorn reg[i].mr_size); 217ca2c1931SNathan Whitehorn } 218ca2c1931SNathan Whitehorn } 219ca2c1931SNathan Whitehorn 220ca2c1931SNathan Whitehorn static void 2214702d987SJustin Hibbits unin_update_reg(device_t dev, uint32_t regoff, uint32_t set, uint32_t clr) 222ca2c1931SNathan Whitehorn { 2234702d987SJustin Hibbits volatile u_int *reg; 224ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 225ca2c1931SNathan Whitehorn u_int32_t tmpl; 226ca2c1931SNathan Whitehorn 227ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 2284702d987SJustin Hibbits reg = (void *)(sc->sc_addr + regoff); 2294702d987SJustin Hibbits tmpl = inl(reg); 2304702d987SJustin Hibbits tmpl &= ~clr; 2314702d987SJustin Hibbits tmpl |= set; 2324702d987SJustin Hibbits outl(reg, tmpl); 2334702d987SJustin Hibbits } 2344702d987SJustin Hibbits 2354702d987SJustin Hibbits static void 2364702d987SJustin Hibbits unin_enable_gmac(device_t dev) 2374702d987SJustin Hibbits { 2384702d987SJustin Hibbits unin_update_reg(dev, UNIN_CLOCKCNTL, UNIN_CLOCKCNTL_GMAC, 0); 239ca2c1931SNathan Whitehorn } 240ca2c1931SNathan Whitehorn 241ca2c1931SNathan Whitehorn static void 242ca2c1931SNathan Whitehorn unin_enable_mpic(device_t dev) 243ca2c1931SNathan Whitehorn { 2444702d987SJustin Hibbits unin_update_reg(dev, UNIN_TOGGLE_REG, UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE, 0); 245ca2c1931SNathan Whitehorn } 246ca2c1931SNathan Whitehorn 247ca2c1931SNathan Whitehorn static int 248ca2c1931SNathan Whitehorn unin_chip_probe(device_t dev) 249ca2c1931SNathan Whitehorn { 250ca2c1931SNathan Whitehorn const char *name; 251ca2c1931SNathan Whitehorn 252ca2c1931SNathan Whitehorn name = ofw_bus_get_name(dev); 253ca2c1931SNathan Whitehorn 254ca2c1931SNathan Whitehorn if (name == NULL) 25598f8e6c0SBenno Rice return (ENXIO); 25698f8e6c0SBenno Rice 257ca2c1931SNathan Whitehorn if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0 258ca2c1931SNathan Whitehorn && strcmp(name, "u4") != 0) 25998f8e6c0SBenno Rice return (ENXIO); 260ca2c1931SNathan Whitehorn 261ca2c1931SNathan Whitehorn device_set_desc(dev, "Apple UniNorth System Controller"); 262ca2c1931SNathan Whitehorn return (0); 26398f8e6c0SBenno Rice } 26498f8e6c0SBenno Rice 265ca2c1931SNathan Whitehorn static int 266ca2c1931SNathan Whitehorn unin_chip_attach(device_t dev) 267ca2c1931SNathan Whitehorn { 268ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 269ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 270ca2c1931SNathan Whitehorn phandle_t root; 271ca2c1931SNathan Whitehorn phandle_t child; 27243cbf1a6SAndreas Tobler phandle_t iparent; 273ca2c1931SNathan Whitehorn device_t cdev; 2744ee63553SNathan Whitehorn cell_t acells, scells; 275ca2c1931SNathan Whitehorn char compat[32]; 27643cbf1a6SAndreas Tobler char name[32]; 27743cbf1a6SAndreas Tobler u_int irq, reg[3]; 278ca2c1931SNathan Whitehorn int error, i = 0; 279ca2c1931SNathan Whitehorn 280ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 281ca2c1931SNathan Whitehorn root = ofw_bus_get_node(dev); 282ca2c1931SNathan Whitehorn 283ca2c1931SNathan Whitehorn if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8) 28498f8e6c0SBenno Rice return (ENXIO); 285ca2c1931SNathan Whitehorn 2864ee63553SNathan Whitehorn acells = scells = 1; 2874ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#address-cells", &acells, sizeof(acells)); 2884ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#size-cells", &scells, sizeof(scells)); 289ca2c1931SNathan Whitehorn 2904ee63553SNathan Whitehorn i = 0; 2914ee63553SNathan Whitehorn sc->sc_physaddr = reg[i++]; 2924ee63553SNathan Whitehorn if (acells == 2) { 2934ee63553SNathan Whitehorn sc->sc_physaddr <<= 32; 2944ee63553SNathan Whitehorn sc->sc_physaddr |= reg[i++]; 2954ee63553SNathan Whitehorn } 2964ee63553SNathan Whitehorn sc->sc_size = reg[i++]; 2974ee63553SNathan Whitehorn if (scells == 2) { 2984ee63553SNathan Whitehorn sc->sc_size <<= 32; 2994ee63553SNathan Whitehorn sc->sc_size |= reg[i++]; 3004ee63553SNathan Whitehorn } 301ca2c1931SNathan Whitehorn 30298f8e6c0SBenno Rice sc->sc_mem_rman.rm_type = RMAN_ARRAY; 303ca2c1931SNathan Whitehorn sc->sc_mem_rman.rm_descr = "UniNorth Device Memory"; 304ca2c1931SNathan Whitehorn 305bda386dbSKevin Lo error = rman_init(&sc->sc_mem_rman); 306ca2c1931SNathan Whitehorn 307bda386dbSKevin Lo if (error) { 308bda386dbSKevin Lo device_printf(dev, "rman_init() failed. error = %d\n", error); 309bda386dbSKevin Lo return (error); 31098f8e6c0SBenno Rice } 311ca2c1931SNathan Whitehorn 312ca2c1931SNathan Whitehorn error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr, 313ca2c1931SNathan Whitehorn sc->sc_physaddr + sc->sc_size - 1); 314bda386dbSKevin Lo if (error) { 31598f8e6c0SBenno Rice device_printf(dev, 316ca2c1931SNathan Whitehorn "rman_manage_region() failed. error = %d\n", 317ca2c1931SNathan Whitehorn error); 318bda386dbSKevin Lo return (error); 31998f8e6c0SBenno Rice } 32098f8e6c0SBenno Rice 3214702d987SJustin Hibbits if (unin_chip == NULL) 3224702d987SJustin Hibbits unin_chip = dev; 3234702d987SJustin Hibbits 324d48bbef3SPeter Grehan /* 325ca2c1931SNathan Whitehorn * Iterate through the sub-devices 326d48bbef3SPeter Grehan */ 327ca2c1931SNathan Whitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 328ca2c1931SNathan Whitehorn dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO); 329ca2c1931SNathan Whitehorn if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child) 330ca2c1931SNathan Whitehorn != 0) 331ca2c1931SNathan Whitehorn { 332ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 333ca2c1931SNathan Whitehorn continue; 334ca2c1931SNathan Whitehorn } 335d48bbef3SPeter Grehan 336ca2c1931SNathan Whitehorn resource_list_init(&dinfo->udi_resources); 337ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts = 0; 338ca2c1931SNathan Whitehorn unin_chip_add_intr(child, dinfo); 339ca2c1931SNathan Whitehorn 34043cbf1a6SAndreas Tobler /* 34143cbf1a6SAndreas Tobler * Some Apple machines do have a bug in OF, they miss 34243cbf1a6SAndreas Tobler * the interrupt entries on the U3 I2C node. That means they 34343cbf1a6SAndreas Tobler * do not have an entry with number of interrupts nor the 34443cbf1a6SAndreas Tobler * entry of the interrupt parent handle. 34543cbf1a6SAndreas Tobler * We define an interrupt and hardwire it to the /u3/mpic 34643cbf1a6SAndreas Tobler * handle. 34743cbf1a6SAndreas Tobler */ 34843cbf1a6SAndreas Tobler 34943cbf1a6SAndreas Tobler if (OF_getprop(child, "name", name, sizeof(name)) <= 0) 35043cbf1a6SAndreas Tobler device_printf(dev, "device has no name!\n"); 35143cbf1a6SAndreas Tobler if (dinfo->udi_ninterrupts == 0 && 35243cbf1a6SAndreas Tobler (strcmp(name, "i2c-bus") == 0 || 35343cbf1a6SAndreas Tobler strcmp(name, "i2c") == 0)) { 35443cbf1a6SAndreas Tobler if (OF_getprop(child, "interrupt-parent", &iparent, 35543cbf1a6SAndreas Tobler sizeof(iparent)) <= 0) { 35643cbf1a6SAndreas Tobler iparent = OF_finddevice("/u3/mpic"); 35743cbf1a6SAndreas Tobler device_printf(dev, "Set /u3/mpic as iparent!\n"); 35843cbf1a6SAndreas Tobler } 35943cbf1a6SAndreas Tobler /* Add an interrupt number 0 to the parent. */ 36043cbf1a6SAndreas Tobler irq = MAP_IRQ(iparent, 0); 36143cbf1a6SAndreas Tobler resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 36243cbf1a6SAndreas Tobler dinfo->udi_ninterrupts, irq, irq, 1); 36343cbf1a6SAndreas Tobler dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 36443cbf1a6SAndreas Tobler dinfo->udi_ninterrupts++; 36543cbf1a6SAndreas Tobler } 36643cbf1a6SAndreas Tobler 367ca2c1931SNathan Whitehorn unin_chip_add_reg(child, dinfo); 368ca2c1931SNathan Whitehorn 369ca2c1931SNathan Whitehorn cdev = device_add_child(dev, NULL, -1); 370ca2c1931SNathan Whitehorn if (cdev == NULL) { 371ca2c1931SNathan Whitehorn device_printf(dev, "<%s>: device_add_child failed\n", 372ca2c1931SNathan Whitehorn dinfo->udi_obdinfo.obd_name); 373ca2c1931SNathan Whitehorn resource_list_free(&dinfo->udi_resources); 374ca2c1931SNathan Whitehorn ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo); 375ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 376ca2c1931SNathan Whitehorn continue; 377ca2c1931SNathan Whitehorn } 378ca2c1931SNathan Whitehorn 379ca2c1931SNathan Whitehorn device_set_ivars(cdev, dinfo); 380ca2c1931SNathan Whitehorn } 381ca2c1931SNathan Whitehorn 382ca2c1931SNathan Whitehorn /* 383ca2c1931SNathan Whitehorn * Only map the first page, since that is where the registers 384ca2c1931SNathan Whitehorn * of interest lie. 385ca2c1931SNathan Whitehorn */ 386ca2c1931SNathan Whitehorn sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE); 387ca2c1931SNathan Whitehorn 388ca2c1931SNathan Whitehorn sc->sc_version = *(u_int *)sc->sc_addr; 389ca2c1931SNathan Whitehorn device_printf(dev, "Version %d\n", sc->sc_version); 390ca2c1931SNathan Whitehorn 391ca2c1931SNathan Whitehorn /* 392ca2c1931SNathan Whitehorn * Enable the GMAC Ethernet cell and the integrated OpenPIC 393ca2c1931SNathan Whitehorn * if Open Firmware says they are used. 394ca2c1931SNathan Whitehorn */ 395ca2c1931SNathan Whitehorn for (child = OF_child(root); child; child = OF_peer(child)) { 396d48bbef3SPeter Grehan memset(compat, 0, sizeof(compat)); 397d48bbef3SPeter Grehan OF_getprop(child, "compatible", compat, sizeof(compat)); 398ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 399ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 400ca2c1931SNathan Whitehorn if (strcmp(compat, "chrp,open-pic") == 0) 401ca2c1931SNathan Whitehorn unin_enable_mpic(dev); 402d48bbef3SPeter Grehan } 403d48bbef3SPeter Grehan 404ca2c1931SNathan Whitehorn /* 405ca2c1931SNathan Whitehorn * GMAC lives under the PCI bus, so just check if enet is gmac. 406ca2c1931SNathan Whitehorn */ 407ca2c1931SNathan Whitehorn child = OF_finddevice("enet"); 408ca2c1931SNathan Whitehorn memset(compat, 0, sizeof(compat)); 409ca2c1931SNathan Whitehorn OF_getprop(child, "compatible", compat, sizeof(compat)); 410ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 411ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 41294b4a038SNathan Whitehorn 41398f8e6c0SBenno Rice return (bus_generic_attach(dev)); 41498f8e6c0SBenno Rice } 41598f8e6c0SBenno Rice 41698f8e6c0SBenno Rice static int 417ca2c1931SNathan Whitehorn unin_chip_print_child(device_t dev, device_t child) 41898f8e6c0SBenno Rice { 419ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 420ca2c1931SNathan Whitehorn struct resource_list *rl; 421ca2c1931SNathan Whitehorn int retval = 0; 42298f8e6c0SBenno Rice 423ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 424ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 42598f8e6c0SBenno Rice 426ca2c1931SNathan Whitehorn retval += bus_print_child_header(dev, child); 42798f8e6c0SBenno Rice 428ca2c1931SNathan Whitehorn retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 429ca2c1931SNathan Whitehorn retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 43098f8e6c0SBenno Rice 431ca2c1931SNathan Whitehorn retval += bus_print_child_footer(dev, child); 43298f8e6c0SBenno Rice 433ca2c1931SNathan Whitehorn return (retval); 43498f8e6c0SBenno Rice } 43598f8e6c0SBenno Rice 43698f8e6c0SBenno Rice static void 437ca2c1931SNathan Whitehorn unin_chip_probe_nomatch(device_t dev, device_t child) 43898f8e6c0SBenno Rice { 439ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 440ca2c1931SNathan Whitehorn struct resource_list *rl; 441ca2c1931SNathan Whitehorn const char *type; 44298f8e6c0SBenno Rice 443ca2c1931SNathan Whitehorn if (bootverbose) { 444ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 445ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 44698f8e6c0SBenno Rice 447ca2c1931SNathan Whitehorn if ((type = ofw_bus_get_type(child)) == NULL) 448ca2c1931SNathan Whitehorn type = "(unknown)"; 449ca2c1931SNathan Whitehorn device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 450ca2c1931SNathan Whitehorn resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 451ca2c1931SNathan Whitehorn resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 452ca2c1931SNathan Whitehorn printf(" (no driver attached)\n"); 45398f8e6c0SBenno Rice } 45498f8e6c0SBenno Rice } 45598f8e6c0SBenno Rice 45698f8e6c0SBenno Rice 45798f8e6c0SBenno Rice static struct resource * 458ca2c1931SNathan Whitehorn unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid, 4592dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, 4602dd1bdf1SJustin Hibbits u_int flags) 46198f8e6c0SBenno Rice { 462ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 463ca2c1931SNathan Whitehorn int needactivate; 46498f8e6c0SBenno Rice struct resource *rv; 46598f8e6c0SBenno Rice struct rman *rm; 466ca2c1931SNathan Whitehorn u_long adjstart, adjend, adjcount; 467ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 468ca2c1931SNathan Whitehorn struct resource_list_entry *rle; 469ca2c1931SNathan Whitehorn 470ca2c1931SNathan Whitehorn sc = device_get_softc(bus); 471ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 47298f8e6c0SBenno Rice 47398f8e6c0SBenno Rice needactivate = flags & RF_ACTIVE; 47498f8e6c0SBenno Rice flags &= ~RF_ACTIVE; 47598f8e6c0SBenno Rice 47698f8e6c0SBenno Rice switch (type) { 47798f8e6c0SBenno Rice case SYS_RES_MEMORY: 478ca2c1931SNathan Whitehorn case SYS_RES_IOPORT: 479ca2c1931SNathan Whitehorn rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY, 480ca2c1931SNathan Whitehorn *rid); 481ca2c1931SNathan Whitehorn if (rle == NULL) { 482ca2c1931SNathan Whitehorn device_printf(bus, "no rle for %s memory %d\n", 483ca2c1931SNathan Whitehorn device_get_nameunit(child), *rid); 484ca2c1931SNathan Whitehorn return (NULL); 485ca2c1931SNathan Whitehorn } 486ca2c1931SNathan Whitehorn 487ca2c1931SNathan Whitehorn rle->end = rle->end - 1; /* Hack? */ 488ca2c1931SNathan Whitehorn 489ca2c1931SNathan Whitehorn if (start < rle->start) 490ca2c1931SNathan Whitehorn adjstart = rle->start; 491ca2c1931SNathan Whitehorn else if (start > rle->end) 492ca2c1931SNathan Whitehorn adjstart = rle->end; 493ca2c1931SNathan Whitehorn else 494ca2c1931SNathan Whitehorn adjstart = start; 495ca2c1931SNathan Whitehorn 496ca2c1931SNathan Whitehorn if (end < rle->start) 497ca2c1931SNathan Whitehorn adjend = rle->start; 498ca2c1931SNathan Whitehorn else if (end > rle->end) 499ca2c1931SNathan Whitehorn adjend = rle->end; 500ca2c1931SNathan Whitehorn else 501ca2c1931SNathan Whitehorn adjend = end; 502ca2c1931SNathan Whitehorn 503ca2c1931SNathan Whitehorn adjcount = adjend - adjstart; 504ca2c1931SNathan Whitehorn 50598f8e6c0SBenno Rice rm = &sc->sc_mem_rman; 50698f8e6c0SBenno Rice break; 507714aa5b9SAndrew Gallatin 50898f8e6c0SBenno Rice case SYS_RES_IRQ: 509ca2c1931SNathan Whitehorn /* Check for passthrough from subattachments. */ 510ca2c1931SNathan Whitehorn if (device_get_parent(child) != bus) 511ca2c1931SNathan Whitehorn return BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 512ca2c1931SNathan Whitehorn type, rid, start, end, count, 513ca2c1931SNathan Whitehorn flags); 514de2fa7b8SMarcel Moolenaar 515ca2c1931SNathan Whitehorn rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ, 516ca2c1931SNathan Whitehorn *rid); 517ca2c1931SNathan Whitehorn if (rle == NULL) { 518ca2c1931SNathan Whitehorn if (dinfo->udi_ninterrupts >= 6) { 519ca2c1931SNathan Whitehorn device_printf(bus, 520ca2c1931SNathan Whitehorn "%s has more than 6 interrupts\n", 521ca2c1931SNathan Whitehorn device_get_nameunit(child)); 522ca2c1931SNathan Whitehorn return (NULL); 523ca2c1931SNathan Whitehorn } 524ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 525ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts, start, start, 526ca2c1931SNathan Whitehorn 1); 527ca2c1931SNathan Whitehorn 528ca2c1931SNathan Whitehorn dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start; 529ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts++; 530ca2c1931SNathan Whitehorn } 531ca2c1931SNathan Whitehorn 532ca2c1931SNathan Whitehorn return (resource_list_alloc(&dinfo->udi_resources, bus, child, 533ca2c1931SNathan Whitehorn type, rid, start, end, count, 534ca2c1931SNathan Whitehorn flags)); 53598f8e6c0SBenno Rice default: 53698f8e6c0SBenno Rice device_printf(bus, "unknown resource request from %s\n", 53798f8e6c0SBenno Rice device_get_nameunit(child)); 53898f8e6c0SBenno Rice return (NULL); 53998f8e6c0SBenno Rice } 54098f8e6c0SBenno Rice 541ca2c1931SNathan Whitehorn rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags, 542ca2c1931SNathan Whitehorn child); 54398f8e6c0SBenno Rice if (rv == NULL) { 544ca2c1931SNathan Whitehorn device_printf(bus, 545ca2c1931SNathan Whitehorn "failed to reserve resource %#lx - %#lx (%#lx)" 546ca2c1931SNathan Whitehorn " for %s\n", adjstart, adjend, adjcount, 54798f8e6c0SBenno Rice device_get_nameunit(child)); 54898f8e6c0SBenno Rice return (NULL); 54998f8e6c0SBenno Rice } 55098f8e6c0SBenno Rice 551bdfb66f8SWarner Losh rman_set_rid(rv, *rid); 55298f8e6c0SBenno Rice 55398f8e6c0SBenno Rice if (needactivate) { 55498f8e6c0SBenno Rice if (bus_activate_resource(child, type, *rid, rv) != 0) { 55598f8e6c0SBenno Rice device_printf(bus, 55698f8e6c0SBenno Rice "failed to activate resource for %s\n", 55798f8e6c0SBenno Rice device_get_nameunit(child)); 55898f8e6c0SBenno Rice rman_release_resource(rv); 55998f8e6c0SBenno Rice return (NULL); 56098f8e6c0SBenno Rice } 56198f8e6c0SBenno Rice } 56298f8e6c0SBenno Rice 56398f8e6c0SBenno Rice return (rv); 56498f8e6c0SBenno Rice } 56598f8e6c0SBenno Rice 56698f8e6c0SBenno Rice static int 567ca2c1931SNathan Whitehorn unin_chip_release_resource(device_t bus, device_t child, int type, int rid, 56898f8e6c0SBenno Rice struct resource *res) 56998f8e6c0SBenno Rice { 570ca2c1931SNathan Whitehorn if (rman_get_flags(res) & RF_ACTIVE) { 571ca2c1931SNathan Whitehorn int error = bus_deactivate_resource(child, type, rid, res); 572ca2c1931SNathan Whitehorn if (error) 573ca2c1931SNathan Whitehorn return error; 574ca2c1931SNathan Whitehorn } 575ca2c1931SNathan Whitehorn 576ca2c1931SNathan Whitehorn return (rman_release_resource(res)); 577ca2c1931SNathan Whitehorn } 578ca2c1931SNathan Whitehorn 579ca2c1931SNathan Whitehorn static int 580ca2c1931SNathan Whitehorn unin_chip_activate_resource(device_t bus, device_t child, int type, int rid, 581ca2c1931SNathan Whitehorn struct resource *res) 582ca2c1931SNathan Whitehorn { 58398f8e6c0SBenno Rice void *p; 584714aa5b9SAndrew Gallatin 58598f8e6c0SBenno Rice if (type == SYS_RES_IRQ) 58698f8e6c0SBenno Rice return (bus_activate_resource(bus, type, rid, res)); 58798f8e6c0SBenno Rice 588ca2c1931SNathan Whitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 589714aa5b9SAndrew Gallatin vm_offset_t start; 590714aa5b9SAndrew Gallatin 591714aa5b9SAndrew Gallatin start = (vm_offset_t) rman_get_start(res); 592714aa5b9SAndrew Gallatin 593714aa5b9SAndrew Gallatin if (bootverbose) 594*da1b038aSJustin Hibbits printf("unin mapdev: start %zx, len %jd\n", start, 595714aa5b9SAndrew Gallatin rman_get_size(res)); 596714aa5b9SAndrew Gallatin 597714aa5b9SAndrew Gallatin p = pmap_mapdev(start, (vm_size_t) rman_get_size(res)); 59898f8e6c0SBenno Rice if (p == NULL) 59998f8e6c0SBenno Rice return (ENOMEM); 60098f8e6c0SBenno Rice rman_set_virtual(res, p); 601ca2c1931SNathan Whitehorn rman_set_bustag(res, &bs_be_tag); 60298f8e6c0SBenno Rice rman_set_bushandle(res, (u_long)p); 60398f8e6c0SBenno Rice } 60498f8e6c0SBenno Rice 60598f8e6c0SBenno Rice return (rman_activate_resource(res)); 60698f8e6c0SBenno Rice } 60798f8e6c0SBenno Rice 608ca2c1931SNathan Whitehorn 60998f8e6c0SBenno Rice static int 610ca2c1931SNathan Whitehorn unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid, 611ca2c1931SNathan Whitehorn struct resource *res) 61298f8e6c0SBenno Rice { 61398f8e6c0SBenno Rice /* 614ca2c1931SNathan Whitehorn * If this is a memory resource, unmap it. 61598f8e6c0SBenno Rice */ 616ca2c1931SNathan Whitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 617ca2c1931SNathan Whitehorn u_int32_t psize; 61898f8e6c0SBenno Rice 619ca2c1931SNathan Whitehorn psize = rman_get_size(res); 620ca2c1931SNathan Whitehorn pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 62198f8e6c0SBenno Rice } 62298f8e6c0SBenno Rice 623ca2c1931SNathan Whitehorn return (rman_deactivate_resource(res)); 62498f8e6c0SBenno Rice } 62598f8e6c0SBenno Rice 626ca2c1931SNathan Whitehorn 627ca2c1931SNathan Whitehorn static struct resource_list * 628ca2c1931SNathan Whitehorn unin_chip_get_resource_list (device_t dev, device_t child) 62951d163d3SNathan Whitehorn { 630ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 63151d163d3SNathan Whitehorn 632ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 633ca2c1931SNathan Whitehorn return (&dinfo->udi_resources); 63451d163d3SNathan Whitehorn } 63551d163d3SNathan Whitehorn 636ca2c1931SNathan Whitehorn static const struct ofw_bus_devinfo * 637ca2c1931SNathan Whitehorn unin_chip_get_devinfo(device_t dev, device_t child) 63898f8e6c0SBenno Rice { 639ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 64098f8e6c0SBenno Rice 641ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 642ca2c1931SNathan Whitehorn return (&dinfo->udi_obdinfo); 64398f8e6c0SBenno Rice } 64498f8e6c0SBenno Rice 6454702d987SJustin Hibbits int 6464702d987SJustin Hibbits unin_chip_wake(device_t dev) 6474702d987SJustin Hibbits { 6484702d987SJustin Hibbits 6494702d987SJustin Hibbits if (dev == NULL) 6504702d987SJustin Hibbits dev = unin_chip; 6514702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_NORMAL, UNIN_PWR_MASK); 6524702d987SJustin Hibbits DELAY(10); 6534702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_RUNNING, 0); 6544702d987SJustin Hibbits DELAY(100); 6554702d987SJustin Hibbits 6564702d987SJustin Hibbits return (0); 6574702d987SJustin Hibbits } 6584702d987SJustin Hibbits 6594702d987SJustin Hibbits int 6604702d987SJustin Hibbits unin_chip_sleep(device_t dev, int idle) 6614702d987SJustin Hibbits { 6624702d987SJustin Hibbits if (dev == NULL) 6634702d987SJustin Hibbits dev = unin_chip; 6644702d987SJustin Hibbits 6654702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_SLEEPING, 0); 6664702d987SJustin Hibbits DELAY(10); 6674702d987SJustin Hibbits if (idle) 6684702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_IDLE2, UNIN_PWR_MASK); 6694702d987SJustin Hibbits else 6704702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_SLEEP, UNIN_PWR_MASK); 6714702d987SJustin Hibbits DELAY(10); 6724702d987SJustin Hibbits 6734702d987SJustin Hibbits return (0); 6744702d987SJustin Hibbits } 675