ata-cbus.c revision 124534
1/*- 2 * Copyright (c) 2002 - 2004 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 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, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/dev/ata/ata-cbus.c 124534 2004-01-14 21:26:35Z sos $"); 31 32#include "opt_ata.h" 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/ata.h> 37#include <sys/bus.h> 38#include <sys/malloc.h> 39#include <sys/sema.h> 40#include <sys/taskqueue.h> 41#include <vm/uma.h> 42#include <machine/resource.h> 43#include <machine/bus.h> 44#include <sys/rman.h> 45#include <isa/isavar.h> 46#include <dev/ata/ata-all.h> 47 48/* local vars */ 49struct ata_cbus_controller { 50 struct resource *io; 51 struct resource *altio; 52 struct resource *bankio; 53 struct resource *irq; 54 void *ih; 55 void (*setmode)(struct ata_device *, int); 56 void (*locking)(struct ata_channel *, int); 57 int current_bank; 58 struct { 59 void (*function)(void *); 60 void *argument; 61 } interrupt[2]; 62}; 63 64/* local prototypes */ 65static void ata_cbus_intr(void *); 66static void ata_cbus_banking(struct ata_channel *, int); 67static void ata_cbus_setmode(struct ata_device *, int); 68 69static int 70ata_cbus_probe(device_t dev) 71{ 72 struct resource *io; 73 int rid; 74 u_long tmp; 75 76 /* dont probe PnP devices */ 77 if (isa_get_vendorid(dev)) 78 return (ENXIO); 79 80 /* allocate the ioport range */ 81 rid = ATA_IOADDR_RID; 82 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 83 ATA_PC98_IOSIZE, RF_ACTIVE); 84 if (!io) 85 return ENOMEM; 86 87 /* calculate & set the altport range */ 88 rid = ATA_PC98_ALTADDR_RID; 89 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) { 90 bus_set_resource(dev, SYS_RES_IOPORT, rid, 91 rman_get_start(io)+ATA_PC98_ALTOFFSET, ATA_ALTIOSIZE); 92 } 93 94 /* calculate & set the bank range */ 95 rid = ATA_PC98_BANKADDR_RID; 96 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) { 97 bus_set_resource(dev, SYS_RES_IOPORT, rid, 98 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE); 99 } 100 101 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 102 return 0; 103} 104 105static int 106ata_cbus_attach(device_t dev) 107{ 108 struct ata_cbus_controller *ctlr = device_get_softc(dev); 109 int rid; 110 111 /* allocate resources */ 112 rid = ATA_IOADDR_RID; 113 ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 114 ATA_PC98_IOSIZE, RF_ACTIVE); 115 if (!ctlr->io) 116 return ENOMEM; 117 118 rid = ATA_PC98_ALTADDR_RID; 119 ctlr->altio = 120 bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 121 rman_get_start(ctlr->io) + ATA_PC98_ALTOFFSET, ~0, 122 ATA_ALTIOSIZE, RF_ACTIVE); 123 if (!ctlr->altio) { 124 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 125 return ENOMEM; 126 } 127 128 rid = ATA_PC98_BANKADDR_RID; 129 ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 130 ATA_PC98_BANK, ~0, 131 ATA_PC98_BANKIOSIZE, RF_ACTIVE); 132 if (!ctlr->bankio) { 133 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 134 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 135 return ENOMEM; 136 } 137 138 rid = ATA_IRQ_RID; 139 if (!(ctlr->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 140 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE))) { 141 device_printf(dev, "unable to alloc interrupt\n"); 142 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 143 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 144 bus_release_resource(dev, SYS_RES_IOPORT, 145 ATA_PC98_BANKADDR_RID, ctlr->bankio); 146 return ENXIO; 147 } 148 149 if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS, 150 ata_cbus_intr, ctlr, &ctlr->ih))) { 151 device_printf(dev, "unable to setup interrupt\n"); 152 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 153 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 154 bus_release_resource(dev, SYS_RES_IOPORT, 155 ATA_PC98_BANKADDR_RID, ctlr->bankio); 156 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq); 157 return ENXIO; 158 } 159 160 ctlr->locking = ata_cbus_banking; 161 ctlr->current_bank = -1; 162 ctlr->setmode = ata_cbus_setmode;; 163 164 if (!device_add_child(dev, "ata", 0)) 165 return ENOMEM; 166 if (!device_add_child(dev, "ata", 1)) 167 return ENOMEM; 168 169 return bus_generic_attach(dev); 170} 171 172static struct resource * 173ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid, 174 u_long start, u_long end, u_long count, u_int flags) 175{ 176 struct ata_cbus_controller *ctlr = device_get_softc(dev); 177 178 if (type == SYS_RES_IOPORT) { 179 switch (*rid) { 180 case ATA_IOADDR_RID: 181 return ctlr->io; 182 case ATA_ALTADDR_RID: 183 return ctlr->altio; 184 } 185 } 186 if (type == SYS_RES_IRQ) 187 return ctlr->irq; 188 return 0; 189} 190 191static int 192ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq, 193 int flags, driver_intr_t *intr, void *arg, 194 void **cookiep) 195{ 196 struct ata_cbus_controller *controller = device_get_softc(dev); 197 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 198 199 controller->interrupt[unit].function = intr; 200 controller->interrupt[unit].argument = arg; 201 *cookiep = controller; 202 203 return 0; 204} 205 206static int 207ata_cbus_print_child(device_t dev, device_t child) 208{ 209 struct ata_channel *ch = device_get_softc(child); 210 int retval = 0; 211 212 retval += bus_print_child_header(dev, child); 213 retval += printf(" at bank %d", ch->unit); 214 retval += bus_print_child_footer(dev, child); 215 return retval; 216} 217 218static void 219ata_cbus_intr(void *data) 220{ 221 struct ata_cbus_controller *ctlr = data; 222 223 if (ctlr->current_bank != -1 && 224 ctlr->interrupt[ctlr->current_bank].argument) 225 ctlr->interrupt[ctlr->current_bank]. 226 function(ctlr->interrupt[ctlr->current_bank].argument); 227} 228 229static void 230ata_cbus_banking(struct ata_channel *ch, int flags) 231{ 232 struct ata_cbus_controller *ctlr = 233 device_get_softc(device_get_parent(ch->dev)); 234 235 switch (flags) { 236 case ATA_LF_LOCK: 237 if (ctlr->current_bank == ch->unit) 238 break; 239 while (!atomic_cmpset_acq_int(&ctlr->current_bank, -1, ch->unit)) 240 tsleep((caddr_t)ch->locking, PRIBIO, "atabnk", 1); 241 ATA_OUTB(ctlr->bankio, 0, ch->unit); 242 break; 243 244 case ATA_LF_UNLOCK: 245 if (ctlr->current_bank == -1 || ctlr->current_bank != ch->unit) 246 break; 247 atomic_store_rel_int(&ctlr->current_bank, -1); 248 break; 249 } 250 return; 251} 252 253static void 254ata_cbus_setmode(struct ata_device *atadev, int mode) 255{ 256 atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX); 257} 258 259static device_method_t ata_cbus_methods[] = { 260 /* device_interface */ 261 DEVMETHOD(device_probe, ata_cbus_probe), 262 DEVMETHOD(device_attach, ata_cbus_attach), 263 264 /* bus methods */ 265 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource), 266 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr), 267 DEVMETHOD(bus_print_child, ata_cbus_print_child), 268 { 0, 0 } 269}; 270 271static driver_t ata_cbus_driver = { 272 "atacbus", 273 ata_cbus_methods, 274 sizeof(struct ata_cbus_controller), 275}; 276 277static devclass_t ata_cbus_devclass; 278 279DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0); 280 281static int 282ata_cbussub_probe(device_t dev) 283{ 284 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev)); 285 struct ata_channel *ch = device_get_softc(dev); 286 device_t *children; 287 int count, i; 288 289 /* find channel number on this controller */ 290 device_get_children(device_get_parent(dev), &children, &count); 291 for (i = 0; i < count; i++) { 292 if (children[i] == dev) 293 ch->unit = i; 294 } 295 free(children, M_TEMP); 296 297 /* setup the resource vectors */ 298 for (i = ATA_DATA; i <= ATA_STATUS; i ++) { 299 ch->r_io[i].res = ctlr->io; 300 ch->r_io[i].offset = i << 1; 301 } 302 ch->r_io[ATA_ALTSTAT].res = ctlr->altio; 303 ch->r_io[ATA_ALTSTAT].offset = 0; 304 305 /* initialize softc for this channel */ 306 ch->flags |= ATA_USE_16BIT | ATA_USE_PC98GEOM; 307 ch->locking = ctlr->locking; 308 ch->device[MASTER].setmode = ctlr->setmode; 309 ch->device[SLAVE].setmode = ctlr->setmode; 310 return ata_probe(dev); 311} 312 313static device_method_t ata_cbussub_methods[] = { 314 /* device interface */ 315 DEVMETHOD(device_probe, ata_cbussub_probe), 316 DEVMETHOD(device_attach, ata_attach), 317 DEVMETHOD(device_detach, ata_detach), 318 DEVMETHOD(device_resume, ata_resume), 319 { 0, 0 } 320}; 321 322static driver_t ata_cbussub_driver = { 323 "ata", 324 ata_cbussub_methods, 325 sizeof(struct ata_channel), 326}; 327 328DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0); 329