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 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * PSIM local bus ATA controller 33 */ 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 <sys/sema.h> 41 #include <sys/taskqueue.h> 42 #include <machine/stdarg.h> 43 #include <vm/uma.h> 44 #include <machine/resource.h> 45 #include <machine/bus.h> 46 #include <sys/rman.h> 47 #include <sys/ata.h> 48 #include <dev/ata/ata-all.h> 49 #include <ata_if.h> 50 51 #include <dev/ofw/openfirm.h> 52 #include <powerpc/psim/iobusvar.h> 53 54 /* 55 * Define the iobus ata bus attachment. This creates a pseudo-bus that 56 * the ATA device can be attached to 57 */ 58 static int ata_iobus_attach(device_t dev); 59 static int ata_iobus_probe(device_t dev); 60 static int ata_iobus_print_child(device_t dev, device_t child); 61 struct resource *ata_iobus_alloc_resource(device_t, device_t, int, int *, 62 rman_res_t, rman_res_t, rman_res_t, 63 u_int); 64 static int ata_iobus_release_resource(device_t, device_t, int, int, 65 struct resource *); 66 67 static device_method_t ata_iobus_methods[] = { 68 /* Device interface */ 69 DEVMETHOD(device_probe, ata_iobus_probe), 70 DEVMETHOD(device_attach, ata_iobus_attach), 71 DEVMETHOD(device_shutdown, bus_generic_shutdown), 72 DEVMETHOD(device_suspend, bus_generic_suspend), 73 DEVMETHOD(device_resume, bus_generic_resume), 74 75 /* Bus methods */ 76 DEVMETHOD(bus_print_child, ata_iobus_print_child), 77 DEVMETHOD(bus_alloc_resource, ata_iobus_alloc_resource), 78 DEVMETHOD(bus_release_resource, ata_iobus_release_resource), 79 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 80 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 81 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 82 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 83 84 DEVMETHOD_END 85 }; 86 87 static driver_t ata_iobus_driver = { 88 "ataiobus", 89 ata_iobus_methods, 90 0, 91 }; 92 93 static devclass_t ata_iobus_devclass; 94 95 DRIVER_MODULE(ataiobus, iobus, ata_iobus_driver, ata_iobus_devclass, NULL, 96 NULL); 97 MODULE_DEPEND(ata, ata, 1, 1, 1); 98 99 static int 100 ata_iobus_probe(device_t dev) 101 { 102 char *type = iobus_get_name(dev); 103 104 if (strncmp(type, "ata", 3) != 0) 105 return (ENXIO); 106 107 device_set_desc(dev, "PSIM ATA Controller"); 108 return (0); 109 } 110 111 112 static int 113 ata_iobus_attach(device_t dev) 114 { 115 /* 116 * Add a single child per controller. Should be able 117 * to add two 118 */ 119 device_add_child(dev, "ata", -1); 120 return (bus_generic_attach(dev)); 121 } 122 123 124 static int 125 ata_iobus_print_child(device_t dev, device_t child) 126 { 127 int retval = 0; 128 129 retval += bus_print_child_header(dev, child); 130 /* irq ? */ 131 retval += bus_print_child_footer(dev, child); 132 133 return (retval); 134 } 135 136 137 struct resource * 138 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid, 139 rman_res_t start, rman_res_t end, rman_res_t count, 140 u_int flags) 141 { 142 struct resource *res = NULL; 143 int myrid; 144 u_int *ofw_regs; 145 146 ofw_regs = iobus_get_regs(dev); 147 148 /* 149 * The reg array for the PSIM ata device has 6 start/size entries: 150 * 0 - unused 151 * 1/2/3 - unused 152 * 4/5/6 - primary command 153 * 7/8/9 - secondary command 154 * 10/11/12 - primary control 155 * 13/14/15 - secondary control 156 * 16/17/18 - primary/secondary dma registers, unimplemented 157 * 158 * The resource values are calculated from these registers 159 */ 160 if (type == SYS_RES_IOPORT) { 161 switch (*rid) { 162 case ATA_IOADDR_RID: 163 myrid = 0; 164 start = ofw_regs[4]; 165 end = start + ATA_IOSIZE - 1; 166 count = ATA_IOSIZE; 167 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 168 SYS_RES_MEMORY, &myrid, 169 start, end, count, flags); 170 break; 171 172 case ATA_CTLADDR_RID: 173 myrid = 0; 174 start = ofw_regs[10]; 175 end = start + ATA_CTLIOSIZE - 1; 176 count = ATA_CTLIOSIZE; 177 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 178 SYS_RES_MEMORY, &myrid, 179 start, end, count, flags); 180 break; 181 182 case ATA_BMADDR_RID: 183 /* DMA not properly supported by psim */ 184 break; 185 } 186 return (res); 187 188 } else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 189 /* 190 * Pass this on to the parent 191 */ 192 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 193 SYS_RES_IRQ, rid, 0, ~0, 1, flags)); 194 195 } else { 196 return (NULL); 197 } 198 } 199 200 201 static int 202 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid, 203 struct resource *r) 204 { 205 /* no hotplug... */ 206 return (0); 207 } 208 209 210 /* 211 * Define the actual ATA device. This is a sub-bus to the ata-iobus layer 212 * to allow the higher layer bus to massage the resource allocation. 213 */ 214 215 static int ata_iobus_sub_probe(device_t dev); 216 static int ata_iobus_sub_setmode(device_t dev, int target, int mode); 217 218 static device_method_t ata_iobus_sub_methods[] = { 219 /* Device interface */ 220 DEVMETHOD(device_probe, ata_iobus_sub_probe), 221 DEVMETHOD(device_attach, ata_attach), 222 DEVMETHOD(device_detach, ata_detach), 223 DEVMETHOD(device_resume, ata_resume), 224 225 /* ATA interface */ 226 DEVMETHOD(ata_setmode, ata_iobus_sub_setmode), 227 DEVMETHOD_END 228 }; 229 230 static driver_t ata_iobus_sub_driver = { 231 "ata", 232 ata_iobus_sub_methods, 233 sizeof(struct ata_channel), 234 }; 235 236 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, ata_devclass, NULL, NULL); 237 238 static int 239 ata_iobus_sub_probe(device_t dev) 240 { 241 struct ata_channel *ch = device_get_softc(dev); 242 243 /* Only a single unit per controller thus far */ 244 ch->unit = 0; 245 ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE); 246 ata_generic_hw(dev); 247 248 return ata_probe(dev); 249 } 250 251 static int 252 ata_iobus_sub_setmode(device_t parent, int target, int mode) 253 { 254 /* Only ever PIO mode here... */ 255 return (ATA_PIO); 256 } 257