1 /* 2 * Copyright 2002 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 * $FreeBSD$ 28 */ 29 30 /* 31 * Mac-io ATA controller 32 */ 33 #include "opt_ata.h" 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/malloc.h> 40 #include <machine/stdarg.h> 41 #include <machine/resource.h> 42 #include <machine/bus.h> 43 #include <sys/rman.h> 44 #include <sys/ata.h> 45 #include <dev/ata/ata-all.h> 46 47 #include <dev/ofw/openfirm.h> 48 #include <powerpc/powermac/maciovar.h> 49 50 struct ata_macio_softc { 51 struct resource *sc_memres; 52 int sc_memrid; 53 bus_space_tag_t sc_bt; 54 bus_space_handle_t sc_bh; 55 struct rman sc_mem_rman; 56 }; 57 58 /* 59 * Define the macio ata bus attachment. This creates a pseudo-bus that 60 * the ATA device can be attached to 61 */ 62 static int ata_macio_attach(device_t dev); 63 static int ata_macio_probe(device_t dev); 64 static int ata_macio_print_child(device_t dev, device_t child); 65 struct resource *ata_macio_alloc_resource(device_t, device_t, int, int *, 66 u_long, u_long, u_long, u_int); 67 static int ata_macio_release_resource(device_t, device_t, int, int, 68 struct resource *); 69 70 static device_method_t ata_macio_methods[] = { 71 /* Device interface */ 72 DEVMETHOD(device_probe, ata_macio_probe), 73 DEVMETHOD(device_attach, ata_macio_attach), 74 DEVMETHOD(device_shutdown, bus_generic_shutdown), 75 DEVMETHOD(device_suspend, bus_generic_suspend), 76 DEVMETHOD(device_resume, bus_generic_resume), 77 78 /* Bus methods */ 79 DEVMETHOD(bus_print_child, ata_macio_print_child), 80 DEVMETHOD(bus_alloc_resource, ata_macio_alloc_resource), 81 DEVMETHOD(bus_release_resource, ata_macio_release_resource), 82 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 83 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 84 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 85 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 86 87 { 0, 0 } 88 }; 89 90 static driver_t ata_macio_driver = { 91 "atamacio", 92 ata_macio_methods, 93 sizeof(struct ata_macio_softc), 94 }; 95 96 static devclass_t ata_macio_devclass; 97 98 DRIVER_MODULE(atamacio, macio, ata_macio_driver, ata_macio_devclass, 0, 0); 99 100 101 static int 102 ata_macio_probe(device_t dev) 103 { 104 char *type = macio_get_devtype(dev); 105 106 if (strcmp(type, "ata") != 0) 107 return (ENXIO); 108 109 /* Print keylargo/pangea ??? */ 110 device_set_desc(dev, "MacIO ATA Controller"); 111 return (0); 112 } 113 114 115 static int 116 ata_macio_attach(device_t dev) 117 { 118 struct ata_macio_softc *sc; 119 120 sc = device_get_softc(dev); 121 122 sc->sc_memrid = 0; 123 sc->sc_memres = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_memrid, 124 0, ~1, 1, RF_ACTIVE | PPC_BUS_SPARSE4); 125 if (sc->sc_memres == NULL) { 126 device_printf(dev, "could not allocate memory\n"); 127 return (ENXIO); 128 } 129 130 sc->sc_bt = rman_get_bustag(sc->sc_memres); 131 sc->sc_bh = rman_get_bushandle(sc->sc_memres); 132 133 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 134 sc->sc_mem_rman.rm_descr = device_get_nameunit(dev); 135 if (rman_init(&sc->sc_mem_rman) != 0) { 136 device_printf(dev, "failed to init memory rman\n"); 137 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, 138 sc->sc_memres); 139 return (ENXIO); 140 } 141 rman_manage_region(&sc->sc_mem_rman, 0, rman_get_size(sc->sc_memres)); 142 143 /* 144 * Add a single child per controller. Should be able 145 * to add two 146 */ 147 device_add_child(dev, "ata", 148 devclass_find_free_unit(ata_devclass, 0)); 149 150 return (bus_generic_attach(dev)); 151 } 152 153 154 static int 155 ata_macio_print_child(device_t dev, device_t child) 156 { 157 int retval = 0; 158 159 retval += bus_print_child_header(dev, child); 160 retval += bus_print_child_footer(dev, child); 161 162 return (retval); 163 } 164 165 /* offset to control registers from base */ 166 #define ATA_MACIO_ALTOFFSET 0x160 167 168 struct resource * 169 ata_macio_alloc_resource(device_t dev, device_t child, int type, int *rid, 170 u_long start, u_long end, u_long count, u_int flags) 171 { 172 struct ata_macio_softc *sc; 173 struct resource *res; 174 int myrid; 175 bus_space_handle_t bh; 176 177 sc = device_get_softc(dev); 178 res = NULL; 179 180 /* 181 * The offset for the register bank is in the first ofw register, 182 * with the base address coming from the parent macio bus (but 183 * accessible via the ata-macio child) 184 */ 185 if (type == SYS_RES_IOPORT) { 186 switch (*rid) { 187 case ATA_IOADDR_RID: 188 start = 0; 189 end = (ATA_IOSIZE << 4) - 1; 190 count = ATA_IOSIZE << 4; 191 res = rman_reserve_resource(&sc->sc_mem_rman, start, 192 end, count, flags | PPC_BUS_SPARSE4, child); 193 if (res == NULL) { 194 device_printf(dev, "rman_reserve failed\n"); 195 return (NULL); 196 } 197 198 rman_set_bustag(res, sc->sc_bt); 199 bus_space_subregion(sc->sc_bt, sc->sc_bh, 200 rman_get_start(res), rman_get_size(res), &bh); 201 rman_set_bushandle(res, bh); 202 rman_set_rid(res, ATA_IOADDR_RID); 203 204 break; 205 206 case ATA_ALTADDR_RID: 207 start = ATA_MACIO_ALTOFFSET; 208 end = start + (ATA_ALTIOSIZE << 4) - 1; 209 count = ATA_ALTIOSIZE << 4; 210 211 res = rman_reserve_resource(&sc->sc_mem_rman, start, 212 end, count, flags | PPC_BUS_SPARSE4, child); 213 if (res == NULL) { 214 device_printf(dev, "rman_reserve failed\n"); 215 return (NULL); 216 } 217 218 rman_set_bustag(res, sc->sc_bt); 219 bus_space_subregion(sc->sc_bt, sc->sc_bh, 220 rman_get_start(res), rman_get_size(res), &bh); 221 rman_set_bushandle(res, bh); 222 rman_set_rid(res, ATA_ALTADDR_RID); 223 224 break; 225 226 case ATA_BMADDR_RID: 227 /* looks difficult to support DBDMA in FreeBSD... */ 228 break; 229 } 230 return (res); 231 232 } else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 233 /* 234 * Pass this on to the parent, using the IRQ from the 235 * ATA pseudo-bus resource 236 */ 237 myrid = 0; 238 res = bus_generic_rl_alloc_resource(device_get_parent(dev), 239 dev, SYS_RES_IRQ, &myrid, 0, ~0, 1, flags); 240 return (res); 241 242 } else { 243 return (NULL); 244 } 245 } 246 247 248 static int 249 ata_macio_release_resource(device_t dev, device_t child, int type, int rid, 250 struct resource *r) 251 { 252 253 if (type == SYS_RES_IRQ) 254 return (bus_release_resource(device_get_parent(dev), type, 255 rid, r)); 256 257 if (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY) 258 return (rman_release_resource(r)); 259 260 return (ENXIO); 261 } 262 263 264 /* 265 * Define the actual ATA device. This is a sub-bus to the ata-macio layer 266 * to allow the higher layer bus to massage the resource allocation. 267 */ 268 269 static int ata_macio_sub_probe(device_t dev); 270 271 static device_method_t ata_macio_sub_methods[] = { 272 /* Device interface */ 273 DEVMETHOD(device_probe, ata_macio_sub_probe), 274 DEVMETHOD(device_attach, ata_attach), 275 DEVMETHOD(device_detach, ata_detach), 276 DEVMETHOD(device_resume, ata_resume), 277 278 { 0, 0 } 279 }; 280 281 static driver_t ata_macio_sub_driver = { 282 "ata", 283 ata_macio_sub_methods, 284 sizeof(struct ata_channel), 285 }; 286 287 DRIVER_MODULE(ata, atamacio, ata_macio_sub_driver, ata_devclass, 0, 0); 288 289 static void 290 ata_macio_locknoop(struct ata_channel *ch, int type) 291 { 292 /* XXX SMP ? */ 293 } 294 295 static void 296 ata_macio_setmode(struct ata_device *atadev, int mode) 297 { 298 atadev->mode = ATA_PIO; 299 } 300 301 static int 302 ata_macio_sub_probe(device_t dev) 303 { 304 struct ata_channel *ch; 305 306 ch = device_get_softc(dev); 307 308 ch->unit = 0; 309 ch->flags |= ATA_USE_16BIT; 310 ch->locking = ata_macio_locknoop; 311 ch->device[MASTER].setmode = ata_macio_setmode; 312 ch->device[SLAVE].setmode = ata_macio_setmode; 313 314 return (ata_probe(dev)); 315 } 316 317 318 319