2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <sys/taskqueue.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/ata/ata-all.h>
49 #include <dev/ata/ata-pci.h>
53 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
56 #define IOMASK 0xfffffffc
57 #define ATA_PROBE_OK -10
60 ata_legacy(device_t dev)
62 return (((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
63 ((pci_read_config(dev, PCIR_PROGIF, 1) &
64 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
65 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
66 (!pci_read_config(dev, PCIR_BAR(0), 4) &&
67 !pci_read_config(dev, PCIR_BAR(1), 4) &&
68 !pci_read_config(dev, PCIR_BAR(2), 4) &&
69 !pci_read_config(dev, PCIR_BAR(3), 4) &&
70 !pci_read_config(dev, PCIR_BAR(5), 4)));
74 ata_pci_probe(device_t dev)
76 if (pci_get_class(dev) != PCIC_STORAGE)
79 /* if this is an AHCI chipset grab it */
80 if (pci_get_subclass(dev) == PCIS_STORAGE_SATA) {
81 if (!ata_ahci_ident(dev))
85 /* run through the vendor specific drivers */
86 switch (pci_get_vendor(dev)) {
88 if (!ata_acard_ident(dev))
91 case ATA_ACER_LABS_ID:
92 if (!ata_ali_ident(dev))
96 if (!ata_amd_ident(dev))
100 if (!ata_adaptec_ident(dev))
104 if (!ata_ati_ident(dev))
108 if (!ata_cyrix_ident(dev))
112 if (!ata_cypress_ident(dev))
115 case ATA_HIGHPOINT_ID:
116 if (!ata_highpoint_ident(dev))
120 if (!ata_intel_ident(dev))
124 if (!ata_ite_ident(dev))
128 if (!ata_jmicron_ident(dev))
132 if (!ata_marvell_ident(dev))
135 case ATA_NATIONAL_ID:
136 if (!ata_national_ident(dev))
140 if (!ata_netcell_ident(dev))
144 if (!ata_nvidia_ident(dev))
148 if (!ata_promise_ident(dev))
151 case ATA_SERVERWORKS_ID:
152 if (!ata_serverworks_ident(dev))
155 case ATA_SILICON_IMAGE_ID:
156 if (!ata_sii_ident(dev))
160 if (!ata_sis_ident(dev))
164 if (!ata_via_ident(dev))
168 if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) {
169 ata_generic_ident(dev);
170 device_set_desc(dev, "Cenatek Rocket Drive controller");
175 if (pci_get_devid(dev) == ATA_MICRON_RZ1000 ||
176 pci_get_devid(dev) == ATA_MICRON_RZ1001) {
177 ata_generic_ident(dev);
179 "RZ 100? ATA controller !WARNING! data loss/corruption risk");
185 /* unknown chipset, try generic DMA if it seems possible */
186 if (pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
187 if (!ata_generic_ident(dev))
194 ata_pci_attach(device_t dev)
196 struct ata_pci_controller *ctlr = device_get_softc(dev);
200 /* do chipset specific setups only needed once */
201 if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
205 ctlr->allocate = ata_pci_allocate;
206 ctlr->dmainit = ata_pci_dmainit;
209 /* if needed try to enable busmastering */
210 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
211 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
212 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
213 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
216 /* if busmastering mode "stuck" use it */
217 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
218 ctlr->r_type1 = SYS_RES_IOPORT;
219 ctlr->r_rid1 = ATA_BMADDR_RID;
220 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
224 if (ctlr->chipinit(dev))
227 /* attach all channels on this controller */
228 for (unit = 0; unit < ctlr->channels; unit++) {
229 if ((unit == 0 || unit == 1) && ata_legacy(dev)) {
230 device_add_child(dev, "ata", unit);
233 device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
235 bus_generic_attach(dev);
240 ata_pci_detach(device_t dev)
242 struct ata_pci_controller *ctlr = device_get_softc(dev);
246 /* detach & delete all children */
247 if (!device_get_children(dev, &children, &nchildren)) {
248 for (i = 0; i < nchildren; i++)
249 device_delete_child(dev, children[i]);
250 free(children, M_TEMP);
254 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
255 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
258 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
260 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
266 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
267 u_long start, u_long end, u_long count, u_int flags)
269 struct ata_pci_controller *controller = device_get_softc(dev);
270 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
271 struct resource *res = NULL;
274 if (type == SYS_RES_IOPORT) {
277 if (ata_legacy(dev)) {
278 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
280 end = start + count - 1;
282 myrid = PCIR_BAR(0) + (unit << 3);
283 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
284 SYS_RES_IOPORT, &myrid,
285 start, end, count, flags);
288 case ATA_CTLADDR_RID:
289 if (ata_legacy(dev)) {
290 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
291 count = ATA_CTLIOSIZE;
292 end = start + count - 1;
294 myrid = PCIR_BAR(1) + (unit << 3);
295 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
296 SYS_RES_IOPORT, &myrid,
297 start, end, count, flags);
301 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
302 if (ata_legacy(dev)) {
303 int irq = (unit == 0 ? 14 : 15);
305 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
306 SYS_RES_IRQ, rid, irq, irq, 1, flags);
309 res = controller->r_irq;
315 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
318 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
320 if (type == SYS_RES_IOPORT) {
323 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
325 PCIR_BAR(0) + (unit << 3), r);
328 case ATA_CTLADDR_RID:
329 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
331 PCIR_BAR(1) + (unit << 3), r);
337 if (type == SYS_RES_IRQ) {
338 if (rid != ATA_IRQ_RID)
341 if (ata_legacy(dev)) {
342 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
343 SYS_RES_IRQ, rid, r);
352 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
353 int flags, driver_filter_t *filter, driver_intr_t *function,
354 void *argument, void **cookiep)
356 if (ata_legacy(dev)) {
357 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
358 flags, filter, function, argument, cookiep);
361 struct ata_pci_controller *controller = device_get_softc(dev);
362 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
364 if (filter != NULL) {
365 printf("ata-pci.c: we cannot use a filter here\n");
368 controller->interrupt[unit].function = function;
369 controller->interrupt[unit].argument = argument;
370 *cookiep = controller;
376 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
379 if (ata_legacy(dev)) {
380 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
383 struct ata_pci_controller *controller = device_get_softc(dev);
384 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
386 controller->interrupt[unit].function = NULL;
387 controller->interrupt[unit].argument = NULL;
393 ata_pci_allocate(device_t dev)
395 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
396 struct ata_channel *ch = device_get_softc(dev);
397 struct resource *io = NULL, *ctlio = NULL;
400 rid = ATA_IOADDR_RID;
401 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
404 rid = ATA_CTLADDR_RID;
405 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
406 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
410 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
411 ch->r_io[i].res = io;
412 ch->r_io[i].offset = i;
414 ch->r_io[ATA_CONTROL].res = ctlio;
415 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
416 ch->r_io[ATA_IDX_ADDR].res = io;
417 ata_default_registers(dev);
419 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
420 ch->r_io[i].res = ctlr->r_res1;
421 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
430 ata_pci_status(device_t dev)
432 struct ata_channel *ch = device_get_softc(dev);
434 if ((dumping || !ata_legacy(device_get_parent(dev))) &&
435 ((ch->flags & ATA_ALWAYS_DMASTAT) ||
436 (ch->dma.flags & ATA_DMA_ACTIVE))) {
437 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
439 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
440 ATA_BMSTAT_INTERRUPT)
442 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
445 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
447 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
454 ata_pci_hw(device_t dev)
456 struct ata_channel *ch = device_get_softc(dev);
459 ch->hw.status = ata_pci_status;
463 ata_pci_dmastart(struct ata_request *request)
465 struct ata_channel *ch = device_get_softc(request->parent);
467 ATA_DEBUG_RQ(request, "dmastart");
469 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
470 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
471 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
472 ch->dma.flags |= ATA_DMA_ACTIVE;
473 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
474 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
475 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
476 ATA_BMCMD_START_STOP);
481 ata_pci_dmastop(struct ata_request *request)
483 struct ata_channel *ch = device_get_softc(request->parent);
486 ATA_DEBUG_RQ(request, "dmastop");
488 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
489 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
490 ch->dma.flags &= ~ATA_DMA_ACTIVE;
491 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
492 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
497 ata_pci_dmareset(device_t dev)
499 struct ata_channel *ch = device_get_softc(dev);
500 struct ata_request *request;
502 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
503 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
504 ch->dma.flags &= ~ATA_DMA_ACTIVE;
505 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
506 if ((request = ch->running)) {
507 device_printf(request->dev, "DMA reset calling unload\n");
508 ch->dma.unload(request);
513 ata_pci_dmainit(device_t dev)
515 struct ata_channel *ch = device_get_softc(dev);
518 ch->dma.start = ata_pci_dmastart;
519 ch->dma.stop = ata_pci_dmastop;
520 ch->dma.reset = ata_pci_dmareset;
524 ata_pcivendor2str(device_t dev)
526 switch (pci_get_vendor(dev)) {
527 case ATA_ACARD_ID: return "Acard";
528 case ATA_ACER_LABS_ID: return "AcerLabs";
529 case ATA_AMD_ID: return "AMD";
530 case ATA_ADAPTEC_ID: return "Adaptec";
531 case ATA_ATI_ID: return "ATI";
532 case ATA_CYRIX_ID: return "Cyrix";
533 case ATA_CYPRESS_ID: return "Cypress";
534 case ATA_HIGHPOINT_ID: return "HighPoint";
535 case ATA_INTEL_ID: return "Intel";
536 case ATA_ITE_ID: return "ITE";
537 case ATA_JMICRON_ID: return "JMicron";
538 case ATA_MARVELL_ID: return "Marvell";
539 case ATA_NATIONAL_ID: return "National";
540 case ATA_NETCELL_ID: return "Netcell";
541 case ATA_NVIDIA_ID: return "nVidia";
542 case ATA_PROMISE_ID: return "Promise";
543 case ATA_SERVERWORKS_ID: return "ServerWorks";
544 case ATA_SILICON_IMAGE_ID: return "SiI";
545 case ATA_SIS_ID: return "SiS";
546 case ATA_VIA_ID: return "VIA";
547 case ATA_CENATEK_ID: return "Cenatek";
548 case ATA_MICRON_ID: return "Micron";
549 default: return "Generic";
553 static device_method_t ata_pci_methods[] = {
554 /* device interface */
555 DEVMETHOD(device_probe, ata_pci_probe),
556 DEVMETHOD(device_attach, ata_pci_attach),
557 DEVMETHOD(device_detach, ata_pci_detach),
558 DEVMETHOD(device_shutdown, bus_generic_shutdown),
559 DEVMETHOD(device_suspend, bus_generic_suspend),
560 DEVMETHOD(device_resume, bus_generic_resume),
563 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
564 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
565 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
566 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
567 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
568 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
573 devclass_t atapci_devclass;
575 static driver_t ata_pci_driver = {
578 sizeof(struct ata_pci_controller),
581 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
582 MODULE_VERSION(atapci, 1);
583 MODULE_DEPEND(atapci, ata, 1, 1, 1);
586 ata_pcichannel_probe(device_t dev)
588 struct ata_channel *ch = device_get_softc(dev);
593 /* take care of green memory */
594 bzero(ch, sizeof(struct ata_channel));
596 /* find channel number on this controller */
597 device_get_children(device_get_parent(dev), &children, &count);
598 for (i = 0; i < count; i++) {
599 if (children[i] == dev)
602 free(children, M_TEMP);
604 sprintf(buffer, "ATA channel %d", ch->unit);
605 device_set_desc_copy(dev, buffer);
607 return ata_probe(dev);
611 ata_pcichannel_attach(device_t dev)
613 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
619 if ((error = ctlr->allocate(dev)))
622 return ata_attach(dev);
626 ata_pcichannel_detach(device_t dev)
628 struct ata_channel *ch = device_get_softc(dev);
631 if ((error = ata_detach(dev)))
636 /* XXX SOS free resources for io and ctlio ?? */
642 ata_pcichannel_locking(device_t dev, int mode)
644 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
645 struct ata_channel *ch = device_get_softc(dev);
648 return ctlr->locking(dev, mode);
654 ata_pcichannel_reset(device_t dev)
656 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
657 struct ata_channel *ch = device_get_softc(dev);
659 /* if DMA engine present reset it */
663 /* reset the controller HW */
667 ata_generic_reset(dev);
671 ata_pcichannel_setmode(device_t parent, device_t dev)
673 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
674 struct ata_device *atadev = device_get_softc(dev);
675 int mode = atadev->mode;
677 ctlr->setmode(dev, ATA_PIO_MAX);
679 ctlr->setmode(dev, mode);
682 static device_method_t ata_pcichannel_methods[] = {
683 /* device interface */
684 DEVMETHOD(device_probe, ata_pcichannel_probe),
685 DEVMETHOD(device_attach, ata_pcichannel_attach),
686 DEVMETHOD(device_detach, ata_pcichannel_detach),
687 DEVMETHOD(device_shutdown, bus_generic_shutdown),
688 DEVMETHOD(device_suspend, ata_suspend),
689 DEVMETHOD(device_resume, ata_resume),
692 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
693 DEVMETHOD(ata_locking, ata_pcichannel_locking),
694 DEVMETHOD(ata_reset, ata_pcichannel_reset),
699 driver_t ata_pcichannel_driver = {
701 ata_pcichannel_methods,
702 sizeof(struct ata_channel),
705 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);