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