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