ata-cbus.c revision 130026
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 130026 2004-06-03 06:10:02Z phk $"); 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/module.h> 40#include <sys/sema.h> 41#include <sys/taskqueue.h> 42#include <vm/uma.h> 43#include <machine/resource.h> 44#include <machine/bus.h> 45#include <sys/rman.h> 46#include <isa/isavar.h> 47#include <dev/ata/ata-all.h> 48 49/* local vars */ 50struct ata_cbus_controller { 51 struct resource *io; 52 struct resource *altio; 53 struct resource *bankio; 54 struct resource *irq; 55 void *ih; 56 void (*setmode)(struct ata_device *, int); 57 void (*locking)(struct ata_channel *, int); 58 int current_bank; 59 struct { 60 void (*function)(void *); 61 void *argument; 62 } interrupt[2]; 63}; 64 65/* local prototypes */ 66static void ata_cbus_intr(void *); 67static void ata_cbus_banking(struct ata_channel *, int); 68static void ata_cbus_setmode(struct ata_device *, int); 69 70static int 71ata_cbus_probe(device_t dev) 72{ 73 struct resource *io; 74 int rid; 75 u_long tmp; 76 77 /* dont probe PnP devices */ 78 if (isa_get_vendorid(dev)) 79 return (ENXIO); 80 81 /* allocate the ioport range */ 82 rid = ATA_IOADDR_RID; 83 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 84 ATA_PC98_IOSIZE, RF_ACTIVE); 85 if (!io) 86 return ENOMEM; 87 88 /* calculate & set the altport range */ 89 rid = ATA_PC98_ALTADDR_RID; 90 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) { 91 bus_set_resource(dev, SYS_RES_IOPORT, rid, 92 rman_get_start(io)+ATA_PC98_ALTOFFSET, ATA_ALTIOSIZE); 93 } 94 95 /* calculate & set the bank range */ 96 rid = ATA_PC98_BANKADDR_RID; 97 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) { 98 bus_set_resource(dev, SYS_RES_IOPORT, rid, 99 ATA_PC98_BANK, ATA_PC98_BANKIOSIZE); 100 } 101 102 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 103 return 0; 104} 105 106static int 107ata_cbus_attach(device_t dev) 108{ 109 struct ata_cbus_controller *ctlr = device_get_softc(dev); 110 int rid; 111 112 /* allocate resources */ 113 rid = ATA_IOADDR_RID; 114 ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 115 ATA_PC98_IOSIZE, RF_ACTIVE); 116 if (!ctlr->io) 117 return ENOMEM; 118 119 rid = ATA_PC98_ALTADDR_RID; 120 ctlr->altio = 121 bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 122 rman_get_start(ctlr->io) + ATA_PC98_ALTOFFSET, ~0, 123 ATA_ALTIOSIZE, RF_ACTIVE); 124 if (!ctlr->altio) { 125 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 126 return ENOMEM; 127 } 128 129 rid = ATA_PC98_BANKADDR_RID; 130 ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 131 ATA_PC98_BANK, ~0, 132 ATA_PC98_BANKIOSIZE, RF_ACTIVE); 133 if (!ctlr->bankio) { 134 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 135 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 136 return ENOMEM; 137 } 138 139 rid = ATA_IRQ_RID; 140 if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 141 RF_ACTIVE | RF_SHAREABLE))) { 142 device_printf(dev, "unable to alloc interrupt\n"); 143 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 144 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 145 bus_release_resource(dev, SYS_RES_IOPORT, 146 ATA_PC98_BANKADDR_RID, ctlr->bankio); 147 return ENXIO; 148 } 149 150 if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS, 151 ata_cbus_intr, ctlr, &ctlr->ih))) { 152 device_printf(dev, "unable to setup interrupt\n"); 153 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io); 154 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ctlr->altio); 155 bus_release_resource(dev, SYS_RES_IOPORT, 156 ATA_PC98_BANKADDR_RID, ctlr->bankio); 157 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq); 158 return ENXIO; 159 } 160 161 ctlr->locking = ata_cbus_banking; 162 ctlr->current_bank = -1; 163 ctlr->setmode = ata_cbus_setmode;; 164 165 if (!device_add_child(dev, "ata", 0)) 166 return ENOMEM; 167 if (!device_add_child(dev, "ata", 1)) 168 return ENOMEM; 169 170 return bus_generic_attach(dev); 171} 172 173static struct resource * 174ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid, 175 u_long start, u_long end, u_long count, u_int flags) 176{ 177 struct ata_cbus_controller *ctlr = device_get_softc(dev); 178 179 if (type == SYS_RES_IOPORT) { 180 switch (*rid) { 181 case ATA_IOADDR_RID: 182 return ctlr->io; 183 case ATA_ALTADDR_RID: 184 return ctlr->altio; 185 } 186 } 187 if (type == SYS_RES_IRQ) 188 return ctlr->irq; 189 return 0; 190} 191 192static int 193ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq, 194 int flags, driver_intr_t *intr, void *arg, 195 void **cookiep) 196{ 197 struct ata_cbus_controller *controller = device_get_softc(dev); 198 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 199 200 controller->interrupt[unit].function = intr; 201 controller->interrupt[unit].argument = arg; 202 *cookiep = controller; 203 204 return 0; 205} 206 207static int 208ata_cbus_print_child(device_t dev, device_t child) 209{ 210 struct ata_channel *ch = device_get_softc(child); 211 int retval = 0; 212 213 retval += bus_print_child_header(dev, child); 214 retval += printf(" at bank %d", ch->unit); 215 retval += bus_print_child_footer(dev, child); 216 return retval; 217} 218 219static void 220ata_cbus_intr(void *data) 221{ 222 struct ata_cbus_controller *ctlr = data; 223 224 if (ctlr->current_bank != -1 && 225 ctlr->interrupt[ctlr->current_bank].argument) 226 ctlr->interrupt[ctlr->current_bank]. 227 function(ctlr->interrupt[ctlr->current_bank].argument); 228} 229 230static void 231ata_cbus_banking(struct ata_channel *ch, int flags) 232{ 233 struct ata_cbus_controller *ctlr = 234 device_get_softc(device_get_parent(ch->dev)); 235 236 switch (flags) { 237 case ATA_LF_LOCK: 238 if (ctlr->current_bank == ch->unit) 239 break; 240 while (!atomic_cmpset_acq_int(&ctlr->current_bank, -1, ch->unit)) 241 tsleep((caddr_t)ch->locking, PRIBIO, "atabnk", 1); 242 ATA_OUTB(ctlr->bankio, 0, ch->unit); 243 break; 244 245 case ATA_LF_UNLOCK: 246 if (ctlr->current_bank == -1 || ctlr->current_bank != ch->unit) 247 break; 248 atomic_store_rel_int(&ctlr->current_bank, -1); 249 break; 250 } 251 return; 252} 253 254static void 255ata_cbus_setmode(struct ata_device *atadev, int mode) 256{ 257 atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX); 258} 259 260static device_method_t ata_cbus_methods[] = { 261 /* device_interface */ 262 DEVMETHOD(device_probe, ata_cbus_probe), 263 DEVMETHOD(device_attach, ata_cbus_attach), 264 265 /* bus methods */ 266 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource), 267 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr), 268 DEVMETHOD(bus_print_child, ata_cbus_print_child), 269 { 0, 0 } 270}; 271 272static driver_t ata_cbus_driver = { 273 "atacbus", 274 ata_cbus_methods, 275 sizeof(struct ata_cbus_controller), 276}; 277 278static devclass_t ata_cbus_devclass; 279 280DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0); 281 282static int 283ata_cbussub_probe(device_t dev) 284{ 285 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev)); 286 struct ata_channel *ch = device_get_softc(dev); 287 device_t *children; 288 int count, i; 289 290 /* find channel number on this controller */ 291 device_get_children(device_get_parent(dev), &children, &count); 292 for (i = 0; i < count; i++) { 293 if (children[i] == dev) 294 ch->unit = i; 295 } 296 free(children, M_TEMP); 297 298 /* setup the resource vectors */ 299 for (i = ATA_DATA; i <= ATA_STATUS; i ++) { 300 ch->r_io[i].res = ctlr->io; 301 ch->r_io[i].offset = i << 1; 302 } 303 ch->r_io[ATA_ALTSTAT].res = ctlr->altio; 304 ch->r_io[ATA_ALTSTAT].offset = 0; 305 306 /* initialize softc for this channel */ 307 ch->flags |= ATA_USE_16BIT | ATA_USE_PC98GEOM; 308 ch->locking = ctlr->locking; 309 ch->device[MASTER].setmode = ctlr->setmode; 310 ch->device[SLAVE].setmode = ctlr->setmode; 311 ata_generic_hw(ch); 312 return ata_probe(dev); 313} 314 315static device_method_t ata_cbussub_methods[] = { 316 /* device interface */ 317 DEVMETHOD(device_probe, ata_cbussub_probe), 318 DEVMETHOD(device_attach, ata_attach), 319 DEVMETHOD(device_detach, ata_detach), 320 DEVMETHOD(device_resume, ata_resume), 321 { 0, 0 } 322}; 323 324static driver_t ata_cbussub_driver = { 325 "ata", 326 ata_cbussub_methods, 327 sizeof(struct ata_channel), 328}; 329 330DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0); 331