1 /* 2 * Copyright 2003 by Peter Grehan. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 /* 30 * The psim iobus attachment for the OpenPIC interrupt controller. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 #include <sys/kernel.h> 41 42 #include <dev/ofw/openfirm.h> 43 44 #include <machine/bus.h> 45 #include <machine/intr.h> 46 #include <machine/intr_machdep.h> 47 #include <machine/md_var.h> 48 #include <machine/nexusvar.h> 49 #include <machine/pio.h> 50 #include <machine/resource.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 55 #include <sys/rman.h> 56 57 #include <machine/openpicvar.h> 58 #include <powerpc/psim/iobusvar.h> 59 60 #include "pic_if.h" 61 62 struct openpic_iobus_softc { 63 struct openpic_softc osc; 64 struct resource *sc_memr; /* iobus mem resource */ 65 device_t sc_ndev; /* nexus device */ 66 }; 67 68 static struct openpic_iobus_softc *ppicsoftc; 69 70 /* 71 * MacIO interface 72 */ 73 static void openpic_psim_identify(driver_t *, device_t); 74 static int openpic_psim_probe(device_t); 75 static int openpic_psim_attach(device_t); 76 static int openpic_iobus_probe(device_t); 77 static int openpic_iobus_attach(device_t); 78 79 /* 80 * Nexus attachment 81 */ 82 static device_method_t openpic_psim_methods[] = { 83 /* Device interface */ 84 DEVMETHOD(device_identify, openpic_psim_identify), 85 DEVMETHOD(device_probe, openpic_psim_probe), 86 DEVMETHOD(device_attach, openpic_psim_attach), 87 88 /* PIC interface */ 89 DEVMETHOD(pic_allocate_intr, openpic_allocate_intr), 90 DEVMETHOD(pic_setup_intr, openpic_setup_intr), 91 DEVMETHOD(pic_teardown_intr, openpic_teardown_intr), 92 DEVMETHOD(pic_release_intr, openpic_release_intr), 93 94 { 0, 0 } 95 }; 96 97 static driver_t openpic_psim_driver = { 98 "openpic", 99 openpic_psim_methods, 100 sizeof(struct openpic_iobus_softc) 101 }; 102 103 static devclass_t openpic_psim_devclass; 104 105 DRIVER_MODULE(openpic_psim, nexus, openpic_psim_driver, openpic_psim_devclass, 106 0, 0); 107 108 static void 109 openpic_psim_identify(driver_t *driver, device_t parent) 110 { 111 device_t child; 112 phandle_t pic; 113 114 pic = OF_finddevice("/iobus/opic"); 115 if (pic == -1) 116 return; 117 118 child = BUS_ADD_CHILD(parent, 0, "openpic", 0); 119 if (child != NULL) 120 nexus_set_device_type(child, "psim"); 121 } 122 123 static int 124 openpic_psim_probe(device_t dev) 125 { 126 char *name; 127 char *type; 128 129 name = nexus_get_name(dev); 130 type = nexus_get_device_type(dev); 131 132 if (strcmp(name, "openpic") != 0 || 133 strcmp(type, "psim") != 0) 134 return (ENXIO); 135 136 device_set_desc(dev, OPENPIC_DEVSTR); 137 return (0); 138 } 139 140 static int 141 openpic_psim_attach(device_t dev) 142 { 143 KASSERT(ppicsoftc == NULL, ("iobus openpic: already probed")); 144 ppicsoftc = device_get_softc(dev); 145 ppicsoftc->sc_ndev = dev; 146 147 nexus_install_intcntlr(dev); 148 openpic_early_attach(dev); 149 return (0); 150 } 151 152 /* 153 * PSIM IOBus attachment 154 */ 155 static device_method_t openpic_iobus_methods[] = { 156 /* Device interface */ 157 DEVMETHOD(device_probe, openpic_iobus_probe), 158 DEVMETHOD(device_attach, openpic_iobus_attach), 159 160 { 0, 0 }, 161 }; 162 163 static driver_t openpic_iobus_driver = { 164 "openpiciobus", 165 openpic_iobus_methods, 166 0 167 }; 168 169 static devclass_t openpic_iobus_devclass; 170 171 DRIVER_MODULE(openpiciobus, iobus, openpic_iobus_driver, 172 openpic_iobus_devclass, 0, 0); 173 174 static int 175 openpic_iobus_probe(device_t dev) 176 { 177 char *name; 178 179 name = iobus_get_name(dev); 180 if (strcmp(name, "interrupt-controller") != 0) 181 return (ENXIO); 182 183 /* 184 * The description was already printed out in the nexus 185 * probe, so don't do it again here 186 */ 187 device_set_desc(dev, "OpenPIC IOBus interrupt cell"); 188 if (!bootverbose) 189 device_quiet(dev); 190 return (0); 191 } 192 193 static int 194 openpic_iobus_attach(device_t dev) 195 { 196 struct openpic_iobus_softc *sc; 197 int rid; 198 199 sc = ppicsoftc; 200 KASSERT(sc != NULL, ("pic not nexus-probed\n")); 201 202 rid = 0; 203 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 204 RF_ACTIVE); 205 206 if (sc->sc_memr == NULL) { 207 device_printf(dev, "Could not alloc mem resource!\n"); 208 return (ENXIO); 209 } 210 211 sc->osc.sc_psim = 1; 212 sc->osc.sc_bt = rman_get_bustag(sc->sc_memr); 213 sc->osc.sc_bh = rman_get_bushandle(sc->sc_memr); 214 sc->osc.sc_altdev = dev; 215 216 return (openpic_attach(sc->sc_ndev)); 217 } 218 219 220