2 * Copyright (c) 1998 - 2005 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.
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <sys/taskqueue.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <machine/md_var.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/ata/ata-all.h>
53 #include <dev/ata/ata-pci.h>
57 static MALLOC_DEFINE(M_ATAPCI, "ATA PCI", "ATA driver PCI");
60 #define IOMASK 0xfffffffc
63 static void ata_pci_dmainit(device_t);
66 ata_legacy(device_t dev)
68 return ((pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV) &&
69 ((pci_read_config(dev, PCIR_PROGIF, 1) &
70 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
71 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)));
75 ata_pci_probe(device_t dev)
77 if (pci_get_class(dev) != PCIC_STORAGE)
80 switch (pci_get_vendor(dev)) {
82 if (!ata_acard_ident(dev))
85 case ATA_ACER_LABS_ID:
86 if (!ata_ali_ident(dev))
90 if (!ata_amd_ident(dev))
94 if (!ata_cyrix_ident(dev))
98 if (!ata_cypress_ident(dev))
101 case ATA_HIGHPOINT_ID:
102 if (!ata_highpoint_ident(dev))
106 if (!ata_intel_ident(dev))
110 if (!ata_ite_ident(dev))
113 case ATA_NATIONAL_ID:
114 if (!ata_national_ident(dev))
118 if (!ata_nvidia_ident(dev))
122 if (!ata_promise_ident(dev))
125 case ATA_SERVERWORKS_ID:
126 if (!ata_serverworks_ident(dev))
129 case ATA_SILICON_IMAGE_ID:
130 if (!ata_sii_ident(dev))
134 if (!ata_sis_ident(dev))
138 if (!ata_via_ident(dev))
142 if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) {
143 ata_generic_ident(dev);
144 device_set_desc(dev, "Cenatek Rocket Drive controller");
149 if (pci_get_devid(dev) == ATA_MICRON_RZ1000 ||
150 pci_get_devid(dev) == ATA_MICRON_RZ1001) {
151 ata_generic_ident(dev);
153 "RZ 100? ATA controller !WARNING! data loss/corruption risk");
159 /* unknown chipset, try generic DMA if it seems possible */
160 if ((pci_get_class(dev) == PCIC_STORAGE) &&
161 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
162 return ata_generic_ident(dev);
168 ata_pci_attach(device_t dev)
170 struct ata_pci_controller *ctlr = device_get_softc(dev);
174 /* do chipset specific setups only needed once */
175 if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
179 ctlr->allocate = ata_pci_allocate;
180 ctlr->dmainit = ata_pci_dmainit;
183 /* if needed try to enable busmastering */
184 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
185 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
186 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
187 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
190 /* if busmastering mode "stuck" use it */
191 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
192 ctlr->r_type1 = SYS_RES_IOPORT;
193 ctlr->r_rid1 = ATA_BMADDR_RID;
194 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
200 /* attach all channels on this controller */
201 for (unit = 0; unit < ctlr->channels; unit++) {
202 if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) {
203 device_add_child(dev, "ata", unit);
206 if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) {
207 device_add_child(dev, "ata", unit);
210 device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
212 bus_generic_attach(dev);
217 ata_pci_detach(device_t dev)
219 struct ata_pci_controller *ctlr = device_get_softc(dev);
223 /* detach & delete all children */
224 if (!device_get_children(dev, &children, &nchildren)) {
225 for (i = 0; i < nchildren; i++)
226 device_delete_child(dev, children[i]);
227 free(children, M_TEMP);
231 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
232 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
235 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
237 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
243 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
244 u_long start, u_long end, u_long count, u_int flags)
246 struct ata_pci_controller *controller = device_get_softc(dev);
247 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
248 struct resource *res = NULL;
251 if (type == SYS_RES_IOPORT) {
254 if (ata_legacy(dev)) {
255 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
257 end = start + count - 1;
259 myrid = PCIR_BAR(0) + (unit << 3);
260 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
261 SYS_RES_IOPORT, &myrid,
262 start, end, count, flags);
265 case ATA_CTLADDR_RID:
266 if (ata_legacy(dev)) {
267 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
268 count = ATA_CTLIOSIZE;
269 end = start + count - 1;
271 myrid = PCIR_BAR(1) + (unit << 3);
272 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
273 SYS_RES_IOPORT, &myrid,
274 start, end, count, flags);
278 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
279 if (ata_legacy(dev)) {
281 res = alpha_platform_alloc_ide_intr(unit);
283 int irq = (unit == 0 ? 14 : 15);
285 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
286 SYS_RES_IRQ, rid, irq, irq, 1, flags);
290 res = controller->r_irq;
296 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
299 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
301 if (type == SYS_RES_IOPORT) {
304 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
306 PCIR_BAR(0) + (unit << 3), r);
309 case ATA_CTLADDR_RID:
310 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
312 PCIR_BAR(1) + (unit << 3), r);
318 if (type == SYS_RES_IRQ) {
319 if (rid != ATA_IRQ_RID)
322 if (ata_legacy(dev)) {
324 return alpha_platform_release_ide_intr(unit, r);
326 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
327 SYS_RES_IRQ, rid, r);
337 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
338 int flags, driver_intr_t *function, void *argument,
341 if (ata_legacy(dev)) {
343 return alpha_platform_setup_ide_intr(child, irq, function, argument,
346 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
347 flags, function, argument, cookiep);
351 struct ata_pci_controller *controller = device_get_softc(dev);
352 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
354 controller->interrupt[unit].function = function;
355 controller->interrupt[unit].argument = argument;
356 *cookiep = controller;
362 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
365 if (ata_legacy(dev)) {
367 return alpha_platform_teardown_ide_intr(child, irq, cookie);
369 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
373 struct ata_pci_controller *controller = device_get_softc(dev);
374 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
376 controller->interrupt[unit].function = NULL;
377 controller->interrupt[unit].argument = NULL;
383 ata_pci_allocate(device_t dev)
385 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
386 struct ata_channel *ch = device_get_softc(dev);
387 struct resource *io = NULL, *ctlio = NULL;
390 rid = ATA_IOADDR_RID;
391 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
394 rid = ATA_CTLADDR_RID;
395 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
396 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
400 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
401 ch->r_io[i].res = io;
402 ch->r_io[i].offset = i;
404 ch->r_io[ATA_CONTROL].res = ctlio;
405 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
406 ch->r_io[ATA_IDX_ADDR].res = io;
407 ata_default_registers(dev);
409 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
410 ch->r_io[i].res = ctlr->r_res1;
411 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
420 ata_pci_dmastart(device_t dev)
422 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
424 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
425 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
426 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
427 ch->dma->flags |= ATA_DMA_ACTIVE;
428 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
429 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
430 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
431 ATA_BMCMD_START_STOP);
436 ata_pci_dmastop(device_t dev)
438 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
441 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
442 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
443 ch->dma->flags &= ~ATA_DMA_ACTIVE;
444 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
445 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
450 ata_pci_dmareset(device_t dev)
452 struct ata_channel *ch = device_get_softc(dev);
454 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
455 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
456 ch->dma->flags &= ~ATA_DMA_ACTIVE;
457 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
458 ch->dma->unload(dev);
462 ata_pci_dmainit(device_t dev)
464 struct ata_channel *ch = device_get_softc(dev);
468 ch->dma->start = ata_pci_dmastart;
469 ch->dma->stop = ata_pci_dmastop;
470 ch->dma->reset = ata_pci_dmareset;
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_detach, ata_pci_detach),
479 DEVMETHOD(device_shutdown, bus_generic_shutdown),
480 DEVMETHOD(device_suspend, bus_generic_suspend),
481 DEVMETHOD(device_resume, bus_generic_resume),
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),
494 devclass_t atapci_devclass;
496 static driver_t ata_pci_driver = {
499 sizeof(struct ata_pci_controller),
502 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
503 MODULE_VERSION(atapci, 1);
504 MODULE_DEPEND(atapci, ata, 1, 1, 1);
507 ata_pcichannel_probe(device_t dev)
509 struct ata_channel *ch = device_get_softc(dev);
514 /* take care of green memory */
515 bzero(ch, sizeof(struct ata_channel));
517 /* find channel number on this controller */
518 device_get_children(device_get_parent(dev), &children, &count);
519 for (i = 0; i < count; i++) {
520 if (children[i] == dev)
523 free(children, M_TEMP);
525 sprintf(buffer, "ATA channel %d", ch->unit);
526 device_set_desc_copy(dev, buffer);
528 return ata_probe(dev);
532 ata_pcichannel_attach(device_t dev)
534 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
535 struct ata_channel *ch = device_get_softc(dev);
543 if ((error = ctlr->allocate(dev)))
546 return ata_attach(dev);
550 ata_pcichannel_detach(device_t dev)
552 struct ata_channel *ch = device_get_softc(dev);
555 if ((error = ata_detach(dev)))
561 /* XXX SOS free resources for io and ctlio ?? */
567 ata_pcichannel_locking(device_t dev, int mode)
569 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
570 struct ata_channel *ch = device_get_softc(dev);
573 return ctlr->locking(dev, mode);
579 ata_pcichannel_reset(device_t dev)
581 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
582 struct ata_channel *ch = device_get_softc(dev);
584 /* if DMA engine present reset it */
588 ch->dma->unload(dev);
591 /* reset the controller HW */
595 ata_generic_reset(dev);
599 ata_pcichannel_setmode(device_t parent, device_t dev)
601 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
602 struct ata_device *atadev = device_get_softc(dev);
603 int mode = atadev->mode;
605 ctlr->setmode(dev, ATA_PIO_MAX);
607 ctlr->setmode(dev, mode);
610 static device_method_t ata_pcichannel_methods[] = {
611 /* device interface */
612 DEVMETHOD(device_probe, ata_pcichannel_probe),
613 DEVMETHOD(device_attach, ata_pcichannel_attach),
614 DEVMETHOD(device_detach, ata_pcichannel_detach),
615 DEVMETHOD(device_shutdown, bus_generic_shutdown),
616 DEVMETHOD(device_suspend, ata_suspend),
617 DEVMETHOD(device_resume, ata_resume),
620 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
621 DEVMETHOD(ata_locking, ata_pcichannel_locking),
622 DEVMETHOD(ata_reset, ata_pcichannel_reset),
627 driver_t ata_pcichannel_driver = {
629 ata_pcichannel_methods,
630 sizeof(struct ata_channel),
633 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);