2 * Copyright (c) 1998 - 2006 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.
26 * $FreeBSD: src/sys/dev/ata/ata-pci.c,v 1.117 2006/05/12 05:04:40 jhb Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-pci.c,v 1.2 2006/12/20 18:14:38 dillon Exp $
32 #include <sys/param.h>
34 #include <sys/bus_resource.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
39 #include <sys/systm.h>
41 #include <bus/pci/pcireg.h>
42 #include <bus/pci/pcivar.h>
49 static MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
52 #define IOMASK 0xfffffffc
53 #define ATA_PROBE_OK -10
56 ata_legacy(device_t dev)
58 return ((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV) &&
59 ((pci_read_config(dev, PCIR_PROGIF, 1) &
60 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
61 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)));
65 ata_pci_probe(device_t dev)
67 if (pci_get_class(dev) != PCIC_STORAGE)
70 switch (pci_get_vendor(dev)) {
72 if (!ata_acard_ident(dev))
75 case ATA_ACER_LABS_ID:
76 if (!ata_ali_ident(dev))
80 if (!ata_amd_ident(dev))
84 if (!ata_ati_ident(dev))
88 if (!ata_cyrix_ident(dev))
92 if (!ata_cypress_ident(dev))
95 case ATA_HIGHPOINT_ID:
96 if (!ata_highpoint_ident(dev))
100 if (!ata_intel_ident(dev))
104 if (!ata_ite_ident(dev))
108 if (!ata_jmicron_ident(dev))
112 if (!ata_marvell_ident(dev))
115 case ATA_NATIONAL_ID:
116 if (!ata_national_ident(dev))
120 if (!ata_nvidia_ident(dev))
124 if (!ata_promise_ident(dev))
127 case ATA_SERVERWORKS_ID:
128 if (!ata_serverworks_ident(dev))
131 case ATA_SILICON_IMAGE_ID:
132 if (!ata_sii_ident(dev))
136 if (!ata_sis_ident(dev))
140 if (!ata_via_ident(dev))
144 if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) {
145 ata_generic_ident(dev);
146 device_set_desc(dev, "Cenatek Rocket Drive controller");
151 if (pci_get_devid(dev) == ATA_MICRON_RZ1000 ||
152 pci_get_devid(dev) == ATA_MICRON_RZ1001) {
153 ata_generic_ident(dev);
155 "RZ 100? ATA controller !WARNING! data loss/corruption risk");
161 /* unknown chipset, try generic DMA if it seems possible */
162 if ((pci_get_class(dev) == PCIC_STORAGE) &&
163 (pci_get_subclass(dev) == PCIS_STORAGE_IDE)) {
164 if (!ata_generic_ident(dev))
171 ata_pci_attach(device_t dev)
173 struct ata_pci_controller *ctlr = device_get_softc(dev);
177 /* do chipset specific setups only needed once */
178 if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
182 ctlr->allocate = ata_pci_allocate;
183 ctlr->deallocate = ata_pci_deallocate;
184 ctlr->dmainit = ata_pci_dmainit;
185 ctlr->dmauninit = ata_pci_dmauninit;
188 /* if needed try to enable busmastering */
189 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
190 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
191 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
192 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
195 /* if busmastering mode "stuck" use it */
196 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
197 ctlr->r_type1 = SYS_RES_IOPORT;
198 ctlr->r_rid1 = ATA_BMADDR_RID;
199 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
203 if (ctlr->chipinit(dev))
206 /* attach all channels on this controller */
207 for (unit = 0; unit < ctlr->channels; unit++) {
209 if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) {
210 device_add_child(dev, "ata", unit);
213 if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) {
214 device_add_child(dev, "ata", unit);
217 /* XXX TGEN devclass_find_free_unit() implementation */
218 while (freeunit < devclass_get_maxunit(ata_devclass) &&
219 devclass_get_device(ata_devclass, freeunit) != NULL)
221 device_add_child(dev, "ata", freeunit);
223 bus_generic_attach(dev);
228 ata_pci_detach(device_t dev)
230 struct ata_pci_controller *ctlr = device_get_softc(dev);
234 /* detach & delete all children */
235 if (!device_get_children(dev, &children, &nchildren)) {
236 for (i = 0; i < nchildren; i++)
237 device_delete_child(dev, children[i]);
238 kfree(children, M_TEMP);
242 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
243 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
246 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
248 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
254 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
255 u_long start, u_long end, u_long count, u_int flags)
257 struct ata_pci_controller *controller = device_get_softc(dev);
258 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
259 struct resource *res = NULL;
262 if (type == SYS_RES_IOPORT) {
265 if (ata_legacy(dev)) {
266 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
268 end = start + count - 1;
270 myrid = PCIR_BAR(0) + (unit << 3);
271 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
272 SYS_RES_IOPORT, &myrid,
273 start, end, count, flags);
276 case ATA_CTLADDR_RID:
277 if (ata_legacy(dev)) {
278 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
279 count = ATA_CTLIOSIZE;
280 end = start + count - 1;
282 myrid = PCIR_BAR(1) + (unit << 3);
283 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
284 SYS_RES_IOPORT, &myrid,
285 start, end, count, flags);
289 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
290 if (ata_legacy(dev)) {
291 int irq = (unit == 0 ? 14 : 15);
293 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
294 SYS_RES_IRQ, rid, irq, irq, 1, flags);
297 res = controller->r_irq;
303 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
306 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
308 if (type == SYS_RES_IOPORT) {
311 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
313 PCIR_BAR(0) + (unit << 3), r);
316 case ATA_CTLADDR_RID:
317 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
319 PCIR_BAR(1) + (unit << 3), r);
325 if (type == SYS_RES_IRQ) {
326 if (rid != ATA_IRQ_RID)
329 if (ata_legacy(dev)) {
330 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
331 SYS_RES_IRQ, rid, r);
340 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
341 int flags, driver_intr_t *function, void *argument,
344 if (ata_legacy(dev)) {
345 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
346 flags, function, argument, cookiep, NULL);
349 struct ata_pci_controller *controller = device_get_softc(dev);
350 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
352 controller->interrupt[unit].function = function;
353 controller->interrupt[unit].argument = argument;
354 *cookiep = controller;
360 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
363 if (ata_legacy(dev)) {
364 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
367 struct ata_pci_controller *controller = device_get_softc(dev);
368 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
370 controller->interrupt[unit].function = NULL;
371 controller->interrupt[unit].argument = NULL;
377 ata_pci_allocate(device_t dev)
379 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
380 struct ata_channel *ch = device_get_softc(dev);
381 struct resource *io = NULL, *ctlio = NULL;
384 rid = ATA_IOADDR_RID;
385 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
388 rid = ATA_CTLADDR_RID;
389 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
390 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
394 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
395 ch->r_io[i].res = io;
396 ch->r_io[i].offset = i;
398 ch->r_io[ATA_CONTROL].res = ctlio;
399 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
400 ch->r_io[ATA_IDX_ADDR].res = io;
401 ata_default_registers(dev);
403 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
404 ch->r_io[i].res = ctlr->r_res1;
405 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
414 * Deallocate resources that have been allocated for the ATA channel.
417 ata_pci_deallocate(device_t dev)
419 struct ata_channel *ch = device_get_softc(dev);
421 if (ch->r_io[ATA_CONTROL].res)
422 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
423 ch->r_io[ATA_CONTROL].res);
424 if (ch->r_io[ATA_IDX_ADDR].res)
425 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
426 ch->r_io[ATA_IDX_ADDR].res);
432 ata_pci_hw(device_t dev)
434 struct ata_channel *ch = device_get_softc(dev);
437 ch->hw.status = ata_pci_status;
441 ata_pci_status(device_t dev)
443 struct ata_channel *ch = device_get_softc(dev);
445 if ((dumping || !ata_legacy(device_get_parent(dev))) &&
446 ch->dma && ((ch->flags & ATA_ALWAYS_DMASTAT) ||
447 (ch->dma->flags & ATA_DMA_ACTIVE))) {
448 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
450 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
451 ATA_BMSTAT_INTERRUPT)
453 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
456 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
458 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
465 ata_pci_dmastart(device_t dev)
467 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
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, ch->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 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
476 ATA_BMCMD_START_STOP);
481 ata_pci_dmastop(device_t dev)
483 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
486 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
487 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
488 ch->dma->flags &= ~ATA_DMA_ACTIVE;
489 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
490 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
495 ata_pci_dmareset(device_t dev)
497 struct ata_channel *ch = device_get_softc(dev);
499 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
500 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
501 ch->dma->flags &= ~ATA_DMA_ACTIVE;
502 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
503 ch->dma->unload(dev);
507 ata_pci_dmainit(device_t dev)
509 struct ata_channel *ch = device_get_softc(dev);
513 ch->dma->start = ata_pci_dmastart;
514 ch->dma->stop = ata_pci_dmastop;
515 ch->dma->reset = ata_pci_dmareset;
520 ata_pci_dmauninit(device_t dev)
525 static device_method_t ata_pci_methods[] = {
526 /* device interface */
527 DEVMETHOD(device_probe, ata_pci_probe),
528 DEVMETHOD(device_attach, ata_pci_attach),
529 DEVMETHOD(device_detach, ata_pci_detach),
530 DEVMETHOD(device_shutdown, bus_generic_shutdown),
531 DEVMETHOD(device_suspend, bus_generic_suspend),
532 DEVMETHOD(device_resume, bus_generic_resume),
535 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
536 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
537 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
538 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
539 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
540 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
545 devclass_t atapci_devclass;
547 static driver_t ata_pci_driver = {
550 sizeof(struct ata_pci_controller),
553 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
554 MODULE_VERSION(atapci, 1);
555 MODULE_DEPEND(atapci, ata, 1, 1, 1);
558 ata_pcichannel_probe(device_t dev)
560 struct ata_channel *ch = device_get_softc(dev);
565 /* take care of green memory */
566 bzero(ch, sizeof(struct ata_channel));
568 /* find channel number on this controller */
569 device_get_children(device_get_parent(dev), &children, &count);
570 for (i = 0; i < count; i++) {
571 if (children[i] == dev)
574 kfree(children, M_TEMP);
576 ksprintf(buffer, "ATA channel %d", ch->unit);
577 device_set_desc_copy(dev, buffer);
579 return ata_probe(dev);
583 ata_pcichannel_attach(device_t dev)
585 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
586 struct ata_channel *ch = device_get_softc(dev);
594 if ((error = ctlr->allocate(dev)))
597 return ata_attach(dev);
601 ata_pcichannel_detach(device_t dev)
603 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
604 struct ata_channel *ch = device_get_softc(dev);
607 if ((error = ata_detach(dev)))
613 ctlr->dmauninit(dev);
615 if ((error = ctlr->deallocate(dev)))
622 ata_pcichannel_locking(device_t dev, int mode)
624 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
625 struct ata_channel *ch = device_get_softc(dev);
628 return ctlr->locking(dev, mode);
634 ata_pcichannel_reset(device_t dev)
636 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
637 struct ata_channel *ch = device_get_softc(dev);
639 /* if DMA engine present reset it */
643 ch->dma->unload(dev);
646 /* reset the controller HW */
650 ata_generic_reset(dev);
654 ata_pcichannel_setmode(device_t parent, device_t dev)
656 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
657 struct ata_device *atadev = device_get_softc(dev);
658 int mode = atadev->mode;
660 ctlr->setmode(dev, ATA_PIO_MAX);
662 ctlr->setmode(dev, mode);
665 static device_method_t ata_pcichannel_methods[] = {
666 /* device interface */
667 DEVMETHOD(device_probe, ata_pcichannel_probe),
668 DEVMETHOD(device_attach, ata_pcichannel_attach),
669 DEVMETHOD(device_detach, ata_pcichannel_detach),
670 DEVMETHOD(device_shutdown, bus_generic_shutdown),
671 DEVMETHOD(device_suspend, ata_suspend),
672 DEVMETHOD(device_resume, ata_resume),
675 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
676 DEVMETHOD(ata_locking, ata_pcichannel_locking),
677 DEVMETHOD(ata_reset, ata_pcichannel_reset),
682 driver_t ata_pcichannel_driver = {
684 ata_pcichannel_methods,
685 sizeof(struct ata_channel),
688 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);