1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright 2002 by Peter Grehan. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * PSIM local bus ATA controller 35 */ 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/malloc.h> 42 #include <sys/sema.h> 43 #include <sys/taskqueue.h> 44 #include <machine/stdarg.h> 45 #include <vm/uma.h> 46 #include <machine/resource.h> 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <sys/ata.h> 50 #include <dev/ata/ata-all.h> 51 #include <ata_if.h> 52 53 #include <dev/ofw/openfirm.h> 54 #include <powerpc/psim/iobusvar.h> 55 56 /* 57 * Define the iobus ata bus attachment. This creates a pseudo-bus that 58 * the ATA device can be attached to 59 */ 60 static int ata_iobus_attach(device_t dev); 61 static int ata_iobus_probe(device_t dev); 62 static int ata_iobus_print_child(device_t dev, device_t child); 63 struct resource *ata_iobus_alloc_resource(device_t, device_t, int, int *, 64 rman_res_t, rman_res_t, rman_res_t, 65 u_int); 66 static int ata_iobus_release_resource(device_t, device_t, int, int, 67 struct resource *); 68 69 static device_method_t ata_iobus_methods[] = { 70 /* Device interface */ 71 DEVMETHOD(device_probe, ata_iobus_probe), 72 DEVMETHOD(device_attach, ata_iobus_attach), 73 DEVMETHOD(device_shutdown, bus_generic_shutdown), 74 DEVMETHOD(device_suspend, bus_generic_suspend), 75 DEVMETHOD(device_resume, bus_generic_resume), 76 77 /* Bus methods */ 78 DEVMETHOD(bus_print_child, ata_iobus_print_child), 79 DEVMETHOD(bus_alloc_resource, ata_iobus_alloc_resource), 80 DEVMETHOD(bus_release_resource, ata_iobus_release_resource), 81 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 82 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 83 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 84 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 85 86 DEVMETHOD_END 87 }; 88 89 static driver_t ata_iobus_driver = { 90 "ataiobus", 91 ata_iobus_methods, 92 0, 93 }; 94 95 DRIVER_MODULE(ataiobus, iobus, ata_iobus_driver, NULL, NULL); 96 MODULE_DEPEND(ata, ata, 1, 1, 1); 97 98 static int 99 ata_iobus_probe(device_t dev) 100 { 101 char *type = iobus_get_name(dev); 102 103 if (strncmp(type, "ata", 3) != 0) 104 return (ENXIO); 105 106 device_set_desc(dev, "PSIM ATA Controller"); 107 return (0); 108 } 109 110 static int 111 ata_iobus_attach(device_t dev) 112 { 113 /* 114 * Add a single child per controller. Should be able 115 * to add two 116 */ 117 device_add_child(dev, "ata", -1); 118 return (bus_generic_attach(dev)); 119 } 120 121 static int 122 ata_iobus_print_child(device_t dev, device_t child) 123 { 124 int retval = 0; 125 126 retval += bus_print_child_header(dev, child); 127 /* irq ? */ 128 retval += bus_print_child_footer(dev, child); 129 130 return (retval); 131 } 132 133 struct resource * 134 ata_iobus_alloc_resource(device_t dev, device_t child, int type, int *rid, 135 rman_res_t start, rman_res_t end, rman_res_t count, 136 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_CTLADDR_RID: 169 myrid = 0; 170 start = ofw_regs[10]; 171 end = start + ATA_CTLIOSIZE - 1; 172 count = ATA_CTLIOSIZE; 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 static int 197 ata_iobus_release_resource(device_t dev, device_t child, int type, int rid, 198 struct resource *r) 199 { 200 /* no hotplug... */ 201 return (0); 202 } 203 204 /* 205 * Define the actual ATA device. This is a sub-bus to the ata-iobus layer 206 * to allow the higher layer bus to massage the resource allocation. 207 */ 208 209 static int ata_iobus_sub_probe(device_t dev); 210 static int ata_iobus_sub_setmode(device_t dev, int target, int mode); 211 212 static device_method_t ata_iobus_sub_methods[] = { 213 /* Device interface */ 214 DEVMETHOD(device_probe, ata_iobus_sub_probe), 215 DEVMETHOD(device_attach, ata_attach), 216 DEVMETHOD(device_detach, ata_detach), 217 DEVMETHOD(device_resume, ata_resume), 218 219 /* ATA interface */ 220 DEVMETHOD(ata_setmode, ata_iobus_sub_setmode), 221 DEVMETHOD_END 222 }; 223 224 static driver_t ata_iobus_sub_driver = { 225 "ata", 226 ata_iobus_sub_methods, 227 sizeof(struct ata_channel), 228 }; 229 230 DRIVER_MODULE(ata, ataiobus, ata_iobus_sub_driver, NULL, NULL); 231 232 static int 233 ata_iobus_sub_probe(device_t dev) 234 { 235 struct ata_channel *ch = device_get_softc(dev); 236 237 /* Only a single unit per controller thus far */ 238 ch->unit = 0; 239 ch->flags = (ATA_USE_16BIT|ATA_NO_SLAVE); 240 ata_generic_hw(dev); 241 242 return ata_probe(dev); 243 } 244 245 static int 246 ata_iobus_sub_setmode(device_t parent, int target, int mode) 247 { 248 /* Only ever PIO mode here... */ 249 return (ATA_PIO); 250 } 251