160727d8bSWarner Losh /*- 271e3c308SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 371e3c308SPedro 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[] = { 10498f8e6c0SBenno Rice /* Device interface */ 105ca2c1931SNathan Whitehorn DEVMETHOD(device_probe, unin_chip_probe), 106ca2c1931SNathan Whitehorn DEVMETHOD(device_attach, unin_chip_attach), 10798f8e6c0SBenno Rice 10898f8e6c0SBenno Rice /* Bus interface */ 109ca2c1931SNathan Whitehorn DEVMETHOD(bus_print_child, unin_chip_print_child), 110ca2c1931SNathan Whitehorn DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch), 11198f8e6c0SBenno Rice DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 11298f8e6c0SBenno Rice DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 11398f8e6c0SBenno Rice 114ca2c1931SNathan Whitehorn DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource), 115ca2c1931SNathan Whitehorn DEVMETHOD(bus_release_resource, unin_chip_release_resource), 116ca2c1931SNathan Whitehorn DEVMETHOD(bus_activate_resource, unin_chip_activate_resource), 117ca2c1931SNathan Whitehorn DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource), 118ca2c1931SNathan Whitehorn DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list), 11998f8e6c0SBenno Rice 120*ddfc9c4cSWarner Losh DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo), 1214fe8025dSNathan Whitehorn 12251d163d3SNathan Whitehorn /* ofw_bus interface */ 123ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo), 124ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 125ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 126ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 127ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 128ca2c1931SNathan Whitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 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 147bfed756aSJustin Hibbits EARLY_DRIVER_MODULE(unin, ofwbus, unin_chip_driver, unin_chip_devclass, 0, 0, 148bfed756aSJustin Hibbits BUS_PASS_BUS); 14998f8e6c0SBenno Rice 15051d163d3SNathan Whitehorn /* 151ca2c1931SNathan Whitehorn * Add an interrupt to the dev's resource list if present 15251d163d3SNathan Whitehorn */ 153ca2c1931SNathan Whitehorn static void 154ca2c1931SNathan Whitehorn unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo) 155ca2c1931SNathan Whitehorn { 156eaef5f0aSNathan Whitehorn phandle_t iparent; 157ca2c1931SNathan Whitehorn int *intr; 158ca2c1931SNathan Whitehorn int i, nintr; 159ca2c1931SNathan Whitehorn int icells; 16051d163d3SNathan Whitehorn 161ca2c1931SNathan Whitehorn if (dinfo->udi_ninterrupts >= 6) { 162ca2c1931SNathan Whitehorn printf("unin: device has more than 6 interrupts\n"); 163ca2c1931SNathan Whitehorn return; 16451d163d3SNathan Whitehorn } 16598f8e6c0SBenno Rice 166217d17bcSOleksandr Tymoshenko nintr = OF_getprop_alloc_multi(devnode, "interrupts", sizeof(*intr), 167ca2c1931SNathan Whitehorn (void **)&intr); 168ca2c1931SNathan Whitehorn if (nintr == -1) { 169217d17bcSOleksandr Tymoshenko nintr = OF_getprop_alloc_multi(devnode, "AAPL,interrupts", 170ca2c1931SNathan Whitehorn sizeof(*intr), (void **)&intr); 171ca2c1931SNathan Whitehorn if (nintr == -1) 172ca2c1931SNathan Whitehorn return; 173ca2c1931SNathan Whitehorn } 174ca2c1931SNathan Whitehorn 175ca2c1931SNathan Whitehorn if (intr[0] == -1) 176ca2c1931SNathan Whitehorn return; 177ca2c1931SNathan Whitehorn 178ca2c1931SNathan Whitehorn if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) 179ca2c1931SNathan Whitehorn <= 0) 180ca2c1931SNathan Whitehorn panic("Interrupt but no interrupt parent!\n"); 181ca2c1931SNathan Whitehorn 182ca2c1931SNathan Whitehorn if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells)) 183ca2c1931SNathan Whitehorn <= 0) 184ca2c1931SNathan Whitehorn icells = 1; 185ca2c1931SNathan Whitehorn 186ca2c1931SNathan Whitehorn for (i = 0; i < nintr; i+=icells) { 187607ebaafSMarcel Moolenaar u_int irq = MAP_IRQ(iparent, intr[i]); 188607ebaafSMarcel Moolenaar 189ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 190607ebaafSMarcel Moolenaar dinfo->udi_ninterrupts, irq, irq, 1); 191ca2c1931SNathan Whitehorn 192eaef5f0aSNathan Whitehorn if (icells > 1) { 193607ebaafSMarcel Moolenaar powerpc_config_intr(irq, 194eaef5f0aSNathan Whitehorn (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL : 195a7a32004SNathan Whitehorn INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 196eaef5f0aSNathan Whitehorn } 197eaef5f0aSNathan Whitehorn 198607ebaafSMarcel Moolenaar dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 199ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts++; 200ca2c1931SNathan Whitehorn } 201ca2c1931SNathan Whitehorn } 202ca2c1931SNathan Whitehorn 203ca2c1931SNathan Whitehorn static void 204ca2c1931SNathan Whitehorn unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo) 205ca2c1931SNathan Whitehorn { 206ca2c1931SNathan Whitehorn struct unin_chip_reg *reg; 207ca2c1931SNathan Whitehorn int i, nreg; 208ca2c1931SNathan Whitehorn 209217d17bcSOleksandr Tymoshenko nreg = OF_getprop_alloc_multi(devnode, "reg", sizeof(*reg), (void **)®); 210ca2c1931SNathan Whitehorn if (nreg == -1) 211ca2c1931SNathan Whitehorn return; 212ca2c1931SNathan Whitehorn 213ca2c1931SNathan Whitehorn for (i = 0; i < nreg; i++) { 214ca2c1931SNathan Whitehorn resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i, 215ca2c1931SNathan Whitehorn reg[i].mr_base, 216ca2c1931SNathan Whitehorn reg[i].mr_base + reg[i].mr_size, 217ca2c1931SNathan Whitehorn reg[i].mr_size); 218ca2c1931SNathan Whitehorn } 219ca2c1931SNathan Whitehorn } 220ca2c1931SNathan Whitehorn 221ca2c1931SNathan Whitehorn static void 2224702d987SJustin Hibbits unin_update_reg(device_t dev, uint32_t regoff, uint32_t set, uint32_t clr) 223ca2c1931SNathan Whitehorn { 2244702d987SJustin Hibbits volatile u_int *reg; 225ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 226ca2c1931SNathan Whitehorn u_int32_t tmpl; 227ca2c1931SNathan Whitehorn 228ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 2294702d987SJustin Hibbits reg = (void *)(sc->sc_addr + regoff); 2304702d987SJustin Hibbits tmpl = inl(reg); 2314702d987SJustin Hibbits tmpl &= ~clr; 2324702d987SJustin Hibbits tmpl |= set; 2334702d987SJustin Hibbits outl(reg, tmpl); 2344702d987SJustin Hibbits } 2354702d987SJustin Hibbits 2364702d987SJustin Hibbits static void 2374702d987SJustin Hibbits unin_enable_gmac(device_t dev) 2384702d987SJustin Hibbits { 2394702d987SJustin Hibbits unin_update_reg(dev, UNIN_CLOCKCNTL, UNIN_CLOCKCNTL_GMAC, 0); 240ca2c1931SNathan Whitehorn } 241ca2c1931SNathan Whitehorn 242ca2c1931SNathan Whitehorn static void 243ca2c1931SNathan Whitehorn unin_enable_mpic(device_t dev) 244ca2c1931SNathan Whitehorn { 2454702d987SJustin Hibbits unin_update_reg(dev, UNIN_TOGGLE_REG, UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE, 0); 246ca2c1931SNathan Whitehorn } 247ca2c1931SNathan Whitehorn 248ca2c1931SNathan Whitehorn static int 249ca2c1931SNathan Whitehorn unin_chip_probe(device_t dev) 250ca2c1931SNathan Whitehorn { 251ca2c1931SNathan Whitehorn const char *name; 252ca2c1931SNathan Whitehorn 253ca2c1931SNathan Whitehorn name = ofw_bus_get_name(dev); 254ca2c1931SNathan Whitehorn 255ca2c1931SNathan Whitehorn if (name == NULL) 25698f8e6c0SBenno Rice return (ENXIO); 25798f8e6c0SBenno Rice 258ca2c1931SNathan Whitehorn if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0 259ca2c1931SNathan Whitehorn && strcmp(name, "u4") != 0) 26098f8e6c0SBenno Rice return (ENXIO); 261ca2c1931SNathan Whitehorn 262ca2c1931SNathan Whitehorn device_set_desc(dev, "Apple UniNorth System Controller"); 263ca2c1931SNathan Whitehorn return (0); 26498f8e6c0SBenno Rice } 26598f8e6c0SBenno Rice 266ca2c1931SNathan Whitehorn static int 267ca2c1931SNathan Whitehorn unin_chip_attach(device_t dev) 268ca2c1931SNathan Whitehorn { 269ca2c1931SNathan Whitehorn struct unin_chip_softc *sc; 270ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 271ca2c1931SNathan Whitehorn phandle_t root; 272ca2c1931SNathan Whitehorn phandle_t child; 27343cbf1a6SAndreas Tobler phandle_t iparent; 274ca2c1931SNathan Whitehorn device_t cdev; 2754ee63553SNathan Whitehorn cell_t acells, scells; 276ca2c1931SNathan Whitehorn char compat[32]; 27743cbf1a6SAndreas Tobler char name[32]; 27843cbf1a6SAndreas Tobler u_int irq, reg[3]; 279ca2c1931SNathan Whitehorn int error, i = 0; 280ca2c1931SNathan Whitehorn 281ca2c1931SNathan Whitehorn sc = device_get_softc(dev); 282ca2c1931SNathan Whitehorn root = ofw_bus_get_node(dev); 283ca2c1931SNathan Whitehorn 284ca2c1931SNathan Whitehorn if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8) 28598f8e6c0SBenno Rice return (ENXIO); 286ca2c1931SNathan Whitehorn 2874ee63553SNathan Whitehorn acells = scells = 1; 2884ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#address-cells", &acells, sizeof(acells)); 2894ee63553SNathan Whitehorn OF_getprop(OF_parent(root), "#size-cells", &scells, sizeof(scells)); 290ca2c1931SNathan Whitehorn 2914ee63553SNathan Whitehorn i = 0; 2924ee63553SNathan Whitehorn sc->sc_physaddr = reg[i++]; 2934ee63553SNathan Whitehorn if (acells == 2) { 2944ee63553SNathan Whitehorn sc->sc_physaddr <<= 32; 2954ee63553SNathan Whitehorn sc->sc_physaddr |= reg[i++]; 2964ee63553SNathan Whitehorn } 2974ee63553SNathan Whitehorn sc->sc_size = reg[i++]; 2984ee63553SNathan Whitehorn if (scells == 2) { 2994ee63553SNathan Whitehorn sc->sc_size <<= 32; 3004ee63553SNathan Whitehorn sc->sc_size |= reg[i++]; 3014ee63553SNathan Whitehorn } 302ca2c1931SNathan Whitehorn 30398f8e6c0SBenno Rice sc->sc_mem_rman.rm_type = RMAN_ARRAY; 304ca2c1931SNathan Whitehorn sc->sc_mem_rman.rm_descr = "UniNorth Device Memory"; 305ca2c1931SNathan Whitehorn 306bda386dbSKevin Lo error = rman_init(&sc->sc_mem_rman); 307ca2c1931SNathan Whitehorn 308bda386dbSKevin Lo if (error) { 309bda386dbSKevin Lo device_printf(dev, "rman_init() failed. error = %d\n", error); 310bda386dbSKevin Lo return (error); 31198f8e6c0SBenno Rice } 312ca2c1931SNathan Whitehorn 313ca2c1931SNathan Whitehorn error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr, 314ca2c1931SNathan Whitehorn sc->sc_physaddr + sc->sc_size - 1); 315bda386dbSKevin Lo if (error) { 31698f8e6c0SBenno Rice device_printf(dev, 317ca2c1931SNathan Whitehorn "rman_manage_region() failed. error = %d\n", 318ca2c1931SNathan Whitehorn error); 319bda386dbSKevin Lo return (error); 32098f8e6c0SBenno Rice } 32198f8e6c0SBenno Rice 3224702d987SJustin Hibbits if (unin_chip == NULL) 3234702d987SJustin Hibbits unin_chip = dev; 3244702d987SJustin Hibbits 325d48bbef3SPeter Grehan /* 326ca2c1931SNathan Whitehorn * Iterate through the sub-devices 327d48bbef3SPeter Grehan */ 328ca2c1931SNathan Whitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 329ca2c1931SNathan Whitehorn dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO); 330ca2c1931SNathan Whitehorn if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child) 331ca2c1931SNathan Whitehorn != 0) 332ca2c1931SNathan Whitehorn { 333ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 334ca2c1931SNathan Whitehorn continue; 335ca2c1931SNathan Whitehorn } 336d48bbef3SPeter Grehan 337ca2c1931SNathan Whitehorn resource_list_init(&dinfo->udi_resources); 338ca2c1931SNathan Whitehorn dinfo->udi_ninterrupts = 0; 339ca2c1931SNathan Whitehorn unin_chip_add_intr(child, dinfo); 340ca2c1931SNathan Whitehorn 34143cbf1a6SAndreas Tobler /* 34243cbf1a6SAndreas Tobler * Some Apple machines do have a bug in OF, they miss 34343cbf1a6SAndreas Tobler * the interrupt entries on the U3 I2C node. That means they 34443cbf1a6SAndreas Tobler * do not have an entry with number of interrupts nor the 34543cbf1a6SAndreas Tobler * entry of the interrupt parent handle. 34643cbf1a6SAndreas Tobler * We define an interrupt and hardwire it to the /u3/mpic 34743cbf1a6SAndreas Tobler * handle. 34843cbf1a6SAndreas Tobler */ 34943cbf1a6SAndreas Tobler 35043cbf1a6SAndreas Tobler if (OF_getprop(child, "name", name, sizeof(name)) <= 0) 35143cbf1a6SAndreas Tobler device_printf(dev, "device has no name!\n"); 35243cbf1a6SAndreas Tobler if (dinfo->udi_ninterrupts == 0 && 35343cbf1a6SAndreas Tobler (strcmp(name, "i2c-bus") == 0 || 35443cbf1a6SAndreas Tobler strcmp(name, "i2c") == 0)) { 35543cbf1a6SAndreas Tobler if (OF_getprop(child, "interrupt-parent", &iparent, 35643cbf1a6SAndreas Tobler sizeof(iparent)) <= 0) { 35743cbf1a6SAndreas Tobler iparent = OF_finddevice("/u3/mpic"); 35843cbf1a6SAndreas Tobler device_printf(dev, "Set /u3/mpic as iparent!\n"); 35943cbf1a6SAndreas Tobler } 36043cbf1a6SAndreas Tobler /* Add an interrupt number 0 to the parent. */ 36143cbf1a6SAndreas Tobler irq = MAP_IRQ(iparent, 0); 36243cbf1a6SAndreas Tobler resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 36343cbf1a6SAndreas Tobler dinfo->udi_ninterrupts, irq, irq, 1); 36443cbf1a6SAndreas Tobler dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 36543cbf1a6SAndreas Tobler dinfo->udi_ninterrupts++; 36643cbf1a6SAndreas Tobler } 36743cbf1a6SAndreas Tobler 368ca2c1931SNathan Whitehorn unin_chip_add_reg(child, dinfo); 369ca2c1931SNathan Whitehorn 370ca2c1931SNathan Whitehorn cdev = device_add_child(dev, NULL, -1); 371ca2c1931SNathan Whitehorn if (cdev == NULL) { 372ca2c1931SNathan Whitehorn device_printf(dev, "<%s>: device_add_child failed\n", 373ca2c1931SNathan Whitehorn dinfo->udi_obdinfo.obd_name); 374ca2c1931SNathan Whitehorn resource_list_free(&dinfo->udi_resources); 375ca2c1931SNathan Whitehorn ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo); 376ca2c1931SNathan Whitehorn free(dinfo, M_UNIN); 377ca2c1931SNathan Whitehorn continue; 378ca2c1931SNathan Whitehorn } 379ca2c1931SNathan Whitehorn 380ca2c1931SNathan Whitehorn device_set_ivars(cdev, dinfo); 381ca2c1931SNathan Whitehorn } 382ca2c1931SNathan Whitehorn 383ca2c1931SNathan Whitehorn /* 384ca2c1931SNathan Whitehorn * Only map the first page, since that is where the registers 385ca2c1931SNathan Whitehorn * of interest lie. 386ca2c1931SNathan Whitehorn */ 387ca2c1931SNathan Whitehorn sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE); 388ca2c1931SNathan Whitehorn 389ca2c1931SNathan Whitehorn sc->sc_version = *(u_int *)sc->sc_addr; 390ca2c1931SNathan Whitehorn device_printf(dev, "Version %d\n", sc->sc_version); 391ca2c1931SNathan Whitehorn 392ca2c1931SNathan Whitehorn /* 393ca2c1931SNathan Whitehorn * Enable the GMAC Ethernet cell and the integrated OpenPIC 394ca2c1931SNathan Whitehorn * if Open Firmware says they are used. 395ca2c1931SNathan Whitehorn */ 396ca2c1931SNathan Whitehorn for (child = OF_child(root); child; child = OF_peer(child)) { 397d48bbef3SPeter Grehan memset(compat, 0, sizeof(compat)); 398d48bbef3SPeter Grehan OF_getprop(child, "compatible", compat, sizeof(compat)); 399ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 400ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 401ca2c1931SNathan Whitehorn if (strcmp(compat, "chrp,open-pic") == 0) 402ca2c1931SNathan Whitehorn unin_enable_mpic(dev); 403d48bbef3SPeter Grehan } 404d48bbef3SPeter Grehan 405ca2c1931SNathan Whitehorn /* 406ca2c1931SNathan Whitehorn * GMAC lives under the PCI bus, so just check if enet is gmac. 407ca2c1931SNathan Whitehorn */ 408ca2c1931SNathan Whitehorn child = OF_finddevice("enet"); 409ca2c1931SNathan Whitehorn memset(compat, 0, sizeof(compat)); 410ca2c1931SNathan Whitehorn OF_getprop(child, "compatible", compat, sizeof(compat)); 411ca2c1931SNathan Whitehorn if (strcmp(compat, "gmac") == 0) 412ca2c1931SNathan Whitehorn unin_enable_gmac(dev); 41394b4a038SNathan Whitehorn 41498f8e6c0SBenno Rice return (bus_generic_attach(dev)); 41598f8e6c0SBenno Rice } 41698f8e6c0SBenno Rice 41798f8e6c0SBenno Rice static int 418ca2c1931SNathan Whitehorn unin_chip_print_child(device_t dev, device_t child) 41998f8e6c0SBenno Rice { 420ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 421ca2c1931SNathan Whitehorn struct resource_list *rl; 422ca2c1931SNathan Whitehorn int retval = 0; 42398f8e6c0SBenno Rice 424ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 425ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 42698f8e6c0SBenno Rice 427ca2c1931SNathan Whitehorn retval += bus_print_child_header(dev, child); 42898f8e6c0SBenno Rice 429f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx"); 430f8fd3fb5SJustin Hibbits retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 43198f8e6c0SBenno Rice 432ca2c1931SNathan Whitehorn retval += bus_print_child_footer(dev, child); 43398f8e6c0SBenno Rice 434ca2c1931SNathan Whitehorn return (retval); 43598f8e6c0SBenno Rice } 43698f8e6c0SBenno Rice 43798f8e6c0SBenno Rice static void 438ca2c1931SNathan Whitehorn unin_chip_probe_nomatch(device_t dev, device_t child) 43998f8e6c0SBenno Rice { 440ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 441ca2c1931SNathan Whitehorn struct resource_list *rl; 442ca2c1931SNathan Whitehorn const char *type; 44398f8e6c0SBenno Rice 444ca2c1931SNathan Whitehorn if (bootverbose) { 445ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 446ca2c1931SNathan Whitehorn rl = &dinfo->udi_resources; 44798f8e6c0SBenno Rice 448ca2c1931SNathan Whitehorn if ((type = ofw_bus_get_type(child)) == NULL) 449ca2c1931SNathan Whitehorn type = "(unknown)"; 450ca2c1931SNathan Whitehorn device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 451f8fd3fb5SJustin Hibbits resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx"); 452f8fd3fb5SJustin Hibbits resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); 453ca2c1931SNathan Whitehorn printf(" (no driver attached)\n"); 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) 594da1b038aSJustin 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 60898f8e6c0SBenno Rice static int 609ca2c1931SNathan Whitehorn unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid, 610ca2c1931SNathan Whitehorn struct resource *res) 61198f8e6c0SBenno Rice { 61298f8e6c0SBenno Rice /* 613ca2c1931SNathan Whitehorn * If this is a memory resource, unmap it. 61498f8e6c0SBenno Rice */ 615ca2c1931SNathan Whitehorn if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 616ca2c1931SNathan Whitehorn u_int32_t psize; 61798f8e6c0SBenno Rice 618ca2c1931SNathan Whitehorn psize = rman_get_size(res); 619ca2c1931SNathan Whitehorn pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 62098f8e6c0SBenno Rice } 62198f8e6c0SBenno Rice 622ca2c1931SNathan Whitehorn return (rman_deactivate_resource(res)); 62398f8e6c0SBenno Rice } 62498f8e6c0SBenno Rice 625ca2c1931SNathan Whitehorn static struct resource_list * 626ca2c1931SNathan Whitehorn unin_chip_get_resource_list (device_t dev, device_t child) 62751d163d3SNathan Whitehorn { 628ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 62951d163d3SNathan Whitehorn 630ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 631ca2c1931SNathan Whitehorn return (&dinfo->udi_resources); 63251d163d3SNathan Whitehorn } 63351d163d3SNathan Whitehorn 634ca2c1931SNathan Whitehorn static const struct ofw_bus_devinfo * 635ca2c1931SNathan Whitehorn unin_chip_get_devinfo(device_t dev, device_t child) 63698f8e6c0SBenno Rice { 637ca2c1931SNathan Whitehorn struct unin_chip_devinfo *dinfo; 63898f8e6c0SBenno Rice 639ca2c1931SNathan Whitehorn dinfo = device_get_ivars(child); 640ca2c1931SNathan Whitehorn return (&dinfo->udi_obdinfo); 64198f8e6c0SBenno Rice } 64298f8e6c0SBenno Rice 6434702d987SJustin Hibbits int 6444702d987SJustin Hibbits unin_chip_wake(device_t dev) 6454702d987SJustin Hibbits { 6464702d987SJustin Hibbits 6474702d987SJustin Hibbits if (dev == NULL) 6484702d987SJustin Hibbits dev = unin_chip; 6494702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_NORMAL, UNIN_PWR_MASK); 6504702d987SJustin Hibbits DELAY(10); 6514702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_RUNNING, 0); 6524702d987SJustin Hibbits DELAY(100); 6534702d987SJustin Hibbits 6544702d987SJustin Hibbits return (0); 6554702d987SJustin Hibbits } 6564702d987SJustin Hibbits 6574702d987SJustin Hibbits int 6584702d987SJustin Hibbits unin_chip_sleep(device_t dev, int idle) 6594702d987SJustin Hibbits { 6604702d987SJustin Hibbits if (dev == NULL) 6614702d987SJustin Hibbits dev = unin_chip; 6624702d987SJustin Hibbits 6634702d987SJustin Hibbits unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_SLEEPING, 0); 6644702d987SJustin Hibbits DELAY(10); 6654702d987SJustin Hibbits if (idle) 6664702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_IDLE2, UNIN_PWR_MASK); 6674702d987SJustin Hibbits else 6684702d987SJustin Hibbits unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_SLEEP, UNIN_PWR_MASK); 6694702d987SJustin Hibbits DELAY(10); 6704702d987SJustin Hibbits 6714702d987SJustin Hibbits return (0); 6724702d987SJustin Hibbits } 673