ata-cbus.c revision 111188
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 111188 2003-02-20 20:02:32Z 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 */ 45static bus_addr_t ata_pc98_ports[] = { 46 0x0, 0x2, 0x4, 0x6, 0x8, 0xa, 0xc, 0xe 47}; 48 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 = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, ata_pc98_ports, 83 ATA_IOSIZE, RF_ACTIVE); 84 if (!io) 85 return ENOMEM; 86 isa_load_resourcev(io, ata_pc98_ports, ATA_IOSIZE); 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 = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, ata_pc98_ports, 115 ATA_IOSIZE, RF_ACTIVE); 116 if (!ctlr->io) 117 return ENOMEM; 118 119 isa_load_resourcev(ctlr->io, ata_pc98_ports, ATA_IOSIZE); 120 121 rid = ATA_PC98_ALTADDR_RID; 122 ctlr->altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 123 rman_get_start(ctlr->io)+ATA_PC98_ALTOFFSET, 124 ~0, ATA_ALTIOSIZE, RF_ACTIVE); 125 if (!ctlr->altio) 126 return ENOMEM; 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 return ENOMEM; 134 135 rid = 0; 136 if (!(ctlr->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 137 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE))) { 138 device_printf(dev, "unable to alloc interrupt\n"); 139 return ENXIO; 140 } 141 142 if ((bus_setup_intr(dev, ctlr->irq, INTR_TYPE_BIO | INTR_ENTROPY, 143 ata_cbus_intr, ctlr, &ctlr->ih))) { 144 device_printf(dev, "unable to setup interrupt\n"); 145 return ENXIO; 146 } 147 148 ctlr->locking = ata_cbus_banking; 149 ctlr->current_bank = -1; 150 ctlr->setmode = ata_cbus_setmode;; 151 152 if (!device_add_child(dev, "ata", 0)) 153 return ENOMEM; 154 if (!device_add_child(dev, "ata", 1)) 155 return ENOMEM; 156 157 return bus_generic_attach(dev); 158} 159 160static struct resource * 161ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid, 162 u_long start, u_long end, u_long count, u_int flags) 163{ 164 struct ata_cbus_controller *ctlr = device_get_softc(dev); 165 166 if (type == SYS_RES_IOPORT) { 167 switch (*rid) { 168 case ATA_IOADDR_RID: 169 return ctlr->io; 170 case ATA_ALTADDR_RID: 171 return ctlr->altio; 172 } 173 } 174 if (type == SYS_RES_IRQ) { 175 return ctlr->irq; 176 } 177 return 0; 178} 179 180static int 181ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq, 182 int flags, driver_intr_t *intr, void *arg, 183 void **cookiep) 184{ 185 struct ata_cbus_controller *controller = device_get_softc(dev); 186 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 187 188 controller->interrupt[unit].function = intr; 189 controller->interrupt[unit].argument = arg; 190 *cookiep = controller; 191 192 return 0; 193} 194 195static int 196ata_cbus_print_child(device_t dev, device_t child) 197{ 198 struct ata_channel *ch = device_get_softc(child); 199 int retval = 0; 200 201 retval += bus_print_child_header(dev, child); 202 retval += printf(" at bank %d", ch->unit); 203 retval += bus_print_child_footer(dev, child); 204 return retval; 205} 206 207static void 208ata_cbus_intr(void *data) 209{ 210 struct ata_cbus_controller *ctlr = data; 211 212 if (ctlr->current_bank != -1 && 213 ctlr->interrupt[ctlr->current_bank].argument) 214 ctlr->interrupt[ctlr->current_bank]. 215 function(ctlr->interrupt[ctlr->current_bank].argument); 216} 217 218static void 219ata_cbus_banking(struct ata_channel *ch, int flags) 220{ 221 struct ata_cbus_controller *ctlr = 222 device_get_softc(device_get_parent(ch->dev)); 223 224 switch (flags) { 225 case ATA_LF_LOCK: 226 if (ctlr->current_bank == ch->unit) 227 break; 228 while (!atomic_cmpset_acq_int(&ctlr->current_bank, -1, ch->unit)) 229 tsleep((caddr_t)ch->locking, PRIBIO, "atalck", 1); 230 bus_space_write_1(rman_get_bustag(ctlr->bankio), 231 rman_get_bushandle(ctlr->bankio), 0, ch->unit); 232 break; 233 234 case ATA_LF_UNLOCK: 235 if (ctlr->current_bank == -1 || ctlr->current_bank != ch->unit) 236 break; 237 atomic_store_rel_int(&ctlr->current_bank, -1); 238 break; 239 } 240 return; 241} 242 243static void 244ata_cbus_setmode(struct ata_device *atadev, int mode) 245{ 246 atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX); 247} 248 249static device_method_t ata_cbus_methods[] = { 250 /* device_interface */ 251 DEVMETHOD(device_probe, ata_cbus_probe), 252 DEVMETHOD(device_attach, ata_cbus_attach), 253 254 /* bus methods */ 255 DEVMETHOD(bus_alloc_resource, ata_cbus_alloc_resource), 256 DEVMETHOD(bus_setup_intr, ata_cbus_setup_intr), 257 DEVMETHOD(bus_print_child, ata_cbus_print_child), 258 { 0, 0 } 259}; 260 261static driver_t ata_cbus_driver = { 262 "atacbus", 263 ata_cbus_methods, 264 sizeof(struct ata_cbus_controller), 265}; 266 267static devclass_t ata_cbus_devclass; 268 269DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0); 270 271static int 272ata_cbussub_probe(device_t dev) 273{ 274 struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev)); 275 struct ata_channel *ch = device_get_softc(dev); 276 device_t *children; 277 int count, i; 278 279 /* find channel number on this controller */ 280 device_get_children(device_get_parent(dev), &children, &count); 281 for (i = 0; i < count; i++) { 282 if (children[i] == dev) 283 ch->unit = i; 284 } 285 free(children, M_TEMP); 286 ch->flags |= ATA_USE_16BIT | ATA_USE_PC98GEOM; 287 ch->device[MASTER].setmode = ctlr->setmode; 288 ch->device[SLAVE].setmode = ctlr->setmode; 289 ch->locking = ctlr->locking; 290 return ata_probe(dev); 291} 292 293static device_method_t ata_cbussub_methods[] = { 294 /* device interface */ 295 DEVMETHOD(device_probe, ata_cbussub_probe), 296 DEVMETHOD(device_attach, ata_attach), 297 DEVMETHOD(device_detach, ata_detach), 298 DEVMETHOD(device_resume, ata_resume), 299 { 0, 0 } 300}; 301 302static driver_t ata_cbussub_driver = { 303 "ata", 304 ata_cbussub_methods, 305 sizeof(struct ata_channel), 306}; 307 308DRIVER_MODULE(ata, atacbus, ata_cbussub_driver, ata_devclass, 0, 0); 309