1 /*- 2 * Copyright (c) 1998 - 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 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ata.h" 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/malloc.h> 39 #include <sys/taskqueue.h> 40 #include <machine/stdarg.h> 41 #include <machine/resource.h> 42 #include <machine/bus.h> 43 #ifdef __alpha__ 44 #include <machine/md_var.h> 45 #endif 46 #include <sys/rman.h> 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pcireg.h> 49 #include <dev/ata/ata-all.h> 50 #include <dev/ata/ata-pci.h> 51 52 /* local vars */ 53 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI"); 54 55 /* misc defines */ 56 #define IOMASK 0xfffffffc 57 58 /* prototypes */ 59 static int ata_pci_allocate(device_t, struct ata_channel *); 60 static void ata_pci_dmainit(struct ata_channel *); 61 static void ata_pci_locknoop(struct ata_channel *, int); 62 63 static int 64 ata_pci_probe(device_t dev) 65 { 66 if (pci_get_class(dev) != PCIC_STORAGE) 67 return ENXIO; 68 69 switch (pci_get_vendor(dev)) { 70 case ATA_ACARD_ID: 71 if (!ata_acard_ident(dev)) 72 return 0; 73 break; 74 case ATA_ACER_LABS_ID: 75 if (!ata_ali_ident(dev)) 76 return 0; 77 break; 78 case ATA_AMD_ID: 79 if (!ata_amd_ident(dev)) 80 return 0; 81 break; 82 case ATA_CYRIX_ID: 83 if (!ata_cyrix_ident(dev)) 84 return 0; 85 break; 86 case ATA_CYPRESS_ID: 87 if (!ata_cypress_ident(dev)) 88 return 0; 89 break; 90 case ATA_HIGHPOINT_ID: 91 if (!ata_highpoint_ident(dev)) 92 return 0; 93 break; 94 case ATA_INTEL_ID: 95 if (!ata_intel_ident(dev)) 96 return 0; 97 break; 98 case ATA_NVIDIA_ID: 99 if (!ata_nvidia_ident(dev)) 100 return 0; 101 break; 102 case ATA_PROMISE_ID: 103 if (!ata_promise_ident(dev)) 104 return 0; 105 break; 106 case ATA_SERVERWORKS_ID: 107 if (!ata_serverworks_ident(dev)) 108 return 0; 109 break; 110 case ATA_SILICON_IMAGE_ID: 111 if (!ata_sii_ident(dev)) 112 return 0; 113 break; 114 case ATA_SIS_ID: 115 if (!ata_sis_ident(dev)) 116 return 0; 117 break; 118 case ATA_VIA_ID: 119 if (!ata_via_ident(dev)) 120 return 0; 121 break; 122 case 0x16ca: 123 if (pci_get_devid(dev) == 0x000116ca) { 124 ata_generic_ident(dev); 125 device_set_desc(dev, "Cenatek Rocket Drive controller"); 126 return 0; 127 } 128 break; 129 case 0x1042: 130 if (pci_get_devid(dev)==0x10001042 || pci_get_devid(dev)==0x10011042) { 131 ata_generic_ident(dev); 132 device_set_desc(dev, 133 "RZ 100? ATA controller !WARNING! buggy HW data loss possible"); 134 return 0; 135 } 136 break; 137 } 138 139 /* unknown chipset, try generic DMA if it seems possible */ 140 if ((pci_get_class(dev) == PCIC_STORAGE) && 141 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) 142 return ata_generic_ident(dev); 143 144 return ENXIO; 145 } 146 147 static int 148 ata_pci_attach(device_t dev) 149 { 150 struct ata_pci_controller *ctlr = device_get_softc(dev); 151 u_int8_t class, subclass; 152 u_int32_t type, cmd; 153 int unit; 154 155 /* set up vendor-specific stuff */ 156 type = pci_get_devid(dev); 157 class = pci_get_class(dev); 158 subclass = pci_get_subclass(dev); 159 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 160 161 if (!(cmd & PCIM_CMD_PORTEN)) { 162 device_printf(dev, "ATA channel disabled by BIOS\n"); 163 return 0; 164 } 165 166 /* do chipset specific setups only needed once */ 167 if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK) 168 ctlr->channels = 2; 169 else 170 ctlr->channels = 1; 171 ctlr->allocate = ata_pci_allocate; 172 ctlr->dmainit = ata_pci_dmainit; 173 ctlr->locking = ata_pci_locknoop; 174 175 #ifdef __sparc64__ 176 if (!(cmd & PCIM_CMD_BUSMASTEREN)) { 177 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2); 178 cmd = pci_read_config(dev, PCIR_COMMAND, 2); 179 } 180 #endif 181 /* if busmastering configured get the I/O resource */ 182 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) { 183 int rid = ATA_BMADDR_RID; 184 185 ctlr->r_io1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 186 0, ~0, 1, RF_ACTIVE); 187 } 188 189 ctlr->chipinit(dev); 190 191 /* attach all channels on this controller */ 192 for (unit = 0; unit < ctlr->channels; unit++) 193 device_add_child(dev, "ata", ATA_MASTERDEV(dev) ? 194 unit : devclass_find_free_unit(ata_devclass, 2)); 195 196 return bus_generic_attach(dev); 197 } 198 199 200 static int 201 ata_pci_print_child(device_t dev, device_t child) 202 { 203 struct ata_channel *ch = device_get_softc(child); 204 int retval = 0; 205 206 retval += bus_print_child_header(dev, child); 207 retval += printf(": at 0x%lx", rman_get_start(ch->r_io[ATA_IDX_ADDR].res)); 208 209 if (ATA_MASTERDEV(dev)) 210 retval += printf(" irq %d", 14 + ch->unit); 211 212 retval += bus_print_child_footer(dev, child); 213 214 return retval; 215 } 216 217 static struct resource * 218 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 219 u_long start, u_long end, u_long count, u_int flags) 220 { 221 struct ata_pci_controller *controller = device_get_softc(dev); 222 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 223 struct resource *res = NULL; 224 int myrid; 225 226 if (type == SYS_RES_IOPORT) { 227 switch (*rid) { 228 case ATA_IOADDR_RID: 229 if (ATA_MASTERDEV(dev)) { 230 myrid = 0; 231 start = (unit ? ATA_SECONDARY : ATA_PRIMARY); 232 end = start + ATA_IOSIZE - 1; 233 count = ATA_IOSIZE; 234 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 235 SYS_RES_IOPORT, &myrid, 236 start, end, count, flags); 237 } 238 else { 239 myrid = 0x10 + 8 * unit; 240 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 241 SYS_RES_IOPORT, &myrid, 242 start, end, count, flags); 243 } 244 break; 245 246 case ATA_ALTADDR_RID: 247 if (ATA_MASTERDEV(dev)) { 248 myrid = 0; 249 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET; 250 end = start + ATA_ALTIOSIZE - 1; 251 count = ATA_ALTIOSIZE; 252 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 253 SYS_RES_IOPORT, &myrid, 254 start, end, count, flags); 255 } 256 else { 257 myrid = 0x14 + 8 * unit; 258 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 259 SYS_RES_IOPORT, &myrid, 260 start, end, count, flags); 261 if (res) { 262 start = rman_get_start(res) + 2; 263 end = start + ATA_ALTIOSIZE - 1; 264 count = ATA_ALTIOSIZE; 265 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 266 SYS_RES_IOPORT, myrid, res); 267 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, 268 SYS_RES_IOPORT, &myrid, 269 start, end, count, flags); 270 } 271 } 272 break; 273 } 274 return res; 275 } 276 277 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) { 278 if (ATA_MASTERDEV(dev)) { 279 #ifdef __alpha__ 280 return alpha_platform_alloc_ide_intr(unit); 281 #else 282 int irq = (unit == 0 ? 14 : 15); 283 284 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 285 SYS_RES_IRQ, rid, irq, irq, 1, flags); 286 #endif 287 } 288 else { 289 return controller->r_irq; 290 } 291 } 292 return 0; 293 } 294 295 static int 296 ata_pci_release_resource(device_t dev, device_t child, int type, int rid, 297 struct resource *r) 298 { 299 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 300 301 if (type == SYS_RES_IOPORT) { 302 switch (rid) { 303 case ATA_IOADDR_RID: 304 if (ATA_MASTERDEV(dev)) 305 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 306 SYS_RES_IOPORT, 0x0, r); 307 else 308 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 309 SYS_RES_IOPORT, 0x10 + 8 * unit, r); 310 break; 311 312 case ATA_ALTADDR_RID: 313 if (ATA_MASTERDEV(dev)) 314 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 315 SYS_RES_IOPORT, 0x0, r); 316 else 317 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, 318 SYS_RES_IOPORT, 0x14 + 8 * unit, r); 319 break; 320 default: 321 return ENOENT; 322 } 323 } 324 if (type == SYS_RES_IRQ) { 325 if (rid != ATA_IRQ_RID) 326 return ENOENT; 327 328 if (ATA_MASTERDEV(dev)) { 329 #ifdef __alpha__ 330 return alpha_platform_release_ide_intr(unit, r); 331 #else 332 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 333 SYS_RES_IRQ, rid, r); 334 #endif 335 } 336 else 337 return 0; 338 } 339 return EINVAL; 340 } 341 342 static int 343 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 344 int flags, driver_intr_t *function, void *argument, 345 void **cookiep) 346 { 347 if (ATA_MASTERDEV(dev)) { 348 #ifdef __alpha__ 349 return alpha_platform_setup_ide_intr(child, irq, function, argument, 350 cookiep); 351 #else 352 return BUS_SETUP_INTR(device_get_parent(dev), child, irq, 353 flags, function, argument, cookiep); 354 #endif 355 } 356 else { 357 struct ata_pci_controller *controller = device_get_softc(dev); 358 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 359 360 controller->interrupt[unit].function = function; 361 controller->interrupt[unit].argument = argument; 362 *cookiep = controller; 363 return 0; 364 } 365 } 366 367 static int 368 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 369 void *cookie) 370 { 371 if (ATA_MASTERDEV(dev)) { 372 #ifdef __alpha__ 373 return alpha_platform_teardown_ide_intr(child, irq, cookie); 374 #else 375 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie); 376 #endif 377 } 378 else { 379 struct ata_pci_controller *controller = device_get_softc(dev); 380 int unit = ((struct ata_channel *)device_get_softc(child))->unit; 381 382 controller->interrupt[unit].function = NULL; 383 controller->interrupt[unit].argument = NULL; 384 return 0; 385 } 386 } 387 388 static int 389 ata_pci_allocate(device_t dev, struct ata_channel *ch) 390 { 391 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 392 struct resource *io = NULL, *altio = NULL; 393 int i, rid; 394 395 rid = ATA_IOADDR_RID; 396 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 397 0, ~0, ATA_IOSIZE, RF_ACTIVE); 398 if (!io) 399 return ENXIO; 400 401 rid = ATA_ALTADDR_RID; 402 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 403 0, ~0, ATA_ALTIOSIZE, RF_ACTIVE); 404 if (!altio) { 405 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 406 return ENXIO; 407 } 408 409 for (i = ATA_DATA; i <= ATA_STATUS; i ++) { 410 ch->r_io[i].res = io; 411 ch->r_io[i].offset = i; 412 } 413 ch->r_io[ATA_ALTSTAT].res = altio; 414 ch->r_io[ATA_ALTSTAT].offset = 0; 415 ch->r_io[ATA_IDX_ADDR].res = io; 416 417 if (ctlr->r_io1) { 418 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) { 419 ch->r_io[i].res = ctlr->r_io1; 420 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE); 421 } 422 423 /* if simplex controller, only allow DMA on primary channel */ 424 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & 425 (ATA_BMSTAT_DMA_MASTER | ATA_BMSTAT_DMA_SLAVE)); 426 if (ch->unit > 0 && 427 (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_DMA_SIMPLEX)) 428 device_printf(dev, "simplex device, DMA on primary only\n"); 429 else 430 ctlr->dmainit(ch); 431 } 432 return 0; 433 } 434 435 static int 436 ata_pci_dmastart(struct ata_channel *ch) 437 { 438 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 439 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR))); 440 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->mdmatab); 441 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 442 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) | 443 ATA_BMCMD_START_STOP); 444 return 0; 445 } 446 447 static int 448 ata_pci_dmastop(struct ata_channel *ch) 449 { 450 int error; 451 452 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK; 453 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 454 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP); 455 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 456 return error; 457 } 458 459 static void 460 ata_pci_dmainit(struct ata_channel *ch) 461 { 462 ata_dmainit(ch); 463 if (ch->dma) { 464 ch->dma->start = ata_pci_dmastart; 465 ch->dma->stop = ata_pci_dmastop; 466 } 467 } 468 469 static void 470 ata_pci_locknoop(struct ata_channel *ch, int flags) 471 { 472 } 473 474 static device_method_t ata_pci_methods[] = { 475 /* device interface */ 476 DEVMETHOD(device_probe, ata_pci_probe), 477 DEVMETHOD(device_attach, ata_pci_attach), 478 DEVMETHOD(device_shutdown, bus_generic_shutdown), 479 DEVMETHOD(device_suspend, bus_generic_suspend), 480 DEVMETHOD(device_resume, bus_generic_resume), 481 482 /* bus methods */ 483 DEVMETHOD(bus_print_child, ata_pci_print_child), 484 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource), 485 DEVMETHOD(bus_release_resource, ata_pci_release_resource), 486 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 487 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 488 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr), 489 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr), 490 { 0, 0 } 491 }; 492 493 static driver_t ata_pci_driver = { 494 "atapci", 495 ata_pci_methods, 496 sizeof(struct ata_pci_controller), 497 }; 498 499 static devclass_t ata_pci_devclass; 500 501 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0); 502 503 static int 504 ata_pcisub_probe(device_t dev) 505 { 506 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev)); 507 struct ata_channel *ch = device_get_softc(dev); 508 device_t *children; 509 int count, error, i; 510 511 /* find channel number on this controller */ 512 device_get_children(device_get_parent(dev), &children, &count); 513 for (i = 0; i < count; i++) { 514 if (children[i] == dev) 515 ch->unit = i; 516 } 517 free(children, M_TEMP); 518 519 if ((error = ctlr->allocate(dev, ch))) 520 return error; 521 522 ch->device[MASTER].setmode = ctlr->setmode; 523 ch->device[SLAVE].setmode = ctlr->setmode; 524 ch->locking = ctlr->locking; 525 return ata_probe(dev); 526 } 527 528 static device_method_t ata_pcisub_methods[] = { 529 /* device interface */ 530 DEVMETHOD(device_probe, ata_pcisub_probe), 531 DEVMETHOD(device_attach, ata_attach), 532 DEVMETHOD(device_detach, ata_detach), 533 DEVMETHOD(device_suspend, ata_suspend), 534 DEVMETHOD(device_resume, ata_resume), 535 { 0, 0 } 536 }; 537 538 static driver_t ata_pcisub_driver = { 539 "ata", 540 ata_pcisub_methods, 541 sizeof(struct ata_channel), 542 }; 543 544 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0); 545