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_ati_ident(dev))
98 if (!ata_cyrix_ident(dev))
102 if (!ata_cypress_ident(dev))
105 case ATA_HIGHPOINT_ID:
106 if (!ata_highpoint_ident(dev))
110 if (!ata_intel_ident(dev))
114 if (!ata_ite_ident(dev))
117 case ATA_NATIONAL_ID:
118 if (!ata_national_ident(dev))
122 if (!ata_nvidia_ident(dev))
126 if (!ata_promise_ident(dev))
129 case ATA_SERVERWORKS_ID:
130 if (!ata_serverworks_ident(dev))
133 case ATA_SILICON_IMAGE_ID:
134 if (!ata_sii_ident(dev))
138 if (!ata_sis_ident(dev))
142 if (!ata_via_ident(dev))
146 if (pci_get_devid(dev) == ATA_CENATEK_ROCKET) {
147 ata_generic_ident(dev);
148 device_set_desc(dev, "Cenatek Rocket Drive controller");
153 if (pci_get_devid(dev) == ATA_MICRON_RZ1000 ||
154 pci_get_devid(dev) == ATA_MICRON_RZ1001) {
155 ata_generic_ident(dev);
157 "RZ 100? ATA controller !WARNING! data loss/corruption risk");
163 /* unknown chipset, try generic DMA if it seems possible */
164 if ((pci_get_class(dev) == PCIC_STORAGE) &&
165 (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
166 return ata_generic_ident(dev);
172 ata_pci_attach(device_t dev)
174 struct ata_pci_controller *ctlr = device_get_softc(dev);
178 /* do chipset specific setups only needed once */
179 if (ata_legacy(dev) || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
183 ctlr->allocate = ata_pci_allocate;
184 ctlr->dmainit = ata_pci_dmainit;
187 /* if needed try to enable busmastering */
188 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
189 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
190 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
191 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
194 /* if busmastering mode "stuck" use it */
195 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
196 ctlr->r_type1 = SYS_RES_IOPORT;
197 ctlr->r_rid1 = ATA_BMADDR_RID;
198 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
204 /* attach all channels on this controller */
205 for (unit = 0; unit < ctlr->channels; unit++) {
206 if (unit == 0 && (pci_get_progif(dev) & 0x81) == 0x80) {
207 device_add_child(dev, "ata", unit);
210 if (unit == 1 && (pci_get_progif(dev) & 0x84) == 0x80) {
211 device_add_child(dev, "ata", unit);
214 device_add_child(dev, "ata", devclass_find_free_unit(ata_devclass, 2));
216 bus_generic_attach(dev);
221 ata_pci_detach(device_t dev)
223 struct ata_pci_controller *ctlr = device_get_softc(dev);
227 /* detach & delete all children */
228 if (!device_get_children(dev, &children, &nchildren)) {
229 for (i = 0; i < nchildren; i++)
230 device_delete_child(dev, children[i]);
231 free(children, M_TEMP);
235 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
236 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
239 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
241 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
247 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
248 u_long start, u_long end, u_long count, u_int flags)
250 struct ata_pci_controller *controller = device_get_softc(dev);
251 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
252 struct resource *res = NULL;
255 if (type == SYS_RES_IOPORT) {
258 if (ata_legacy(dev)) {
259 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
261 end = start + count - 1;
263 myrid = PCIR_BAR(0) + (unit << 3);
264 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
265 SYS_RES_IOPORT, &myrid,
266 start, end, count, flags);
269 case ATA_CTLADDR_RID:
270 if (ata_legacy(dev)) {
271 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_CTLOFFSET;
272 count = ATA_CTLIOSIZE;
273 end = start + count - 1;
275 myrid = PCIR_BAR(1) + (unit << 3);
276 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
277 SYS_RES_IOPORT, &myrid,
278 start, end, count, flags);
282 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
283 if (ata_legacy(dev)) {
285 res = alpha_platform_alloc_ide_intr(unit);
287 int irq = (unit == 0 ? 14 : 15);
289 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
290 SYS_RES_IRQ, rid, irq, irq, 1, flags);
294 res = controller->r_irq;
300 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
303 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
305 if (type == SYS_RES_IOPORT) {
308 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
310 PCIR_BAR(0) + (unit << 3), r);
313 case ATA_CTLADDR_RID:
314 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
316 PCIR_BAR(1) + (unit << 3), r);
322 if (type == SYS_RES_IRQ) {
323 if (rid != ATA_IRQ_RID)
326 if (ata_legacy(dev)) {
328 return alpha_platform_release_ide_intr(unit, r);
330 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
331 SYS_RES_IRQ, rid, r);
341 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
342 int flags, driver_intr_t *function, void *argument,
345 if (ata_legacy(dev)) {
347 return alpha_platform_setup_ide_intr(child, irq, function, argument,
350 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
351 flags, function, argument, cookiep);
355 struct ata_pci_controller *controller = device_get_softc(dev);
356 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
358 controller->interrupt[unit].function = function;
359 controller->interrupt[unit].argument = argument;
360 *cookiep = controller;
366 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
369 if (ata_legacy(dev)) {
371 return alpha_platform_teardown_ide_intr(child, irq, cookie);
373 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
377 struct ata_pci_controller *controller = device_get_softc(dev);
378 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
380 controller->interrupt[unit].function = NULL;
381 controller->interrupt[unit].argument = NULL;
387 ata_pci_allocate(device_t dev)
389 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
390 struct ata_channel *ch = device_get_softc(dev);
391 struct resource *io = NULL, *ctlio = NULL;
394 rid = ATA_IOADDR_RID;
395 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
398 rid = ATA_CTLADDR_RID;
399 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
400 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
404 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
405 ch->r_io[i].res = io;
406 ch->r_io[i].offset = i;
408 ch->r_io[ATA_CONTROL].res = ctlio;
409 ch->r_io[ATA_CONTROL].offset = ata_legacy(device_get_parent(dev)) ? 0 : 2;
410 ch->r_io[ATA_IDX_ADDR].res = io;
411 ata_default_registers(dev);
413 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
414 ch->r_io[i].res = ctlr->r_res1;
415 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
424 ata_pci_dmastart(device_t dev)
426 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
428 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
429 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
430 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
431 ch->dma->flags |= ATA_DMA_ACTIVE;
432 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
433 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
434 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
435 ATA_BMCMD_START_STOP);
440 ata_pci_dmastop(device_t dev)
442 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
445 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
446 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
447 ch->dma->flags &= ~ATA_DMA_ACTIVE;
448 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
449 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
454 ata_pci_dmareset(device_t dev)
456 struct ata_channel *ch = device_get_softc(dev);
458 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
459 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
460 ch->dma->flags &= ~ATA_DMA_ACTIVE;
461 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
462 ch->dma->unload(dev);
466 ata_pci_dmainit(device_t dev)
468 struct ata_channel *ch = device_get_softc(dev);
472 ch->dma->start = ata_pci_dmastart;
473 ch->dma->stop = ata_pci_dmastop;
474 ch->dma->reset = ata_pci_dmareset;
478 static device_method_t ata_pci_methods[] = {
479 /* device interface */
480 DEVMETHOD(device_probe, ata_pci_probe),
481 DEVMETHOD(device_attach, ata_pci_attach),
482 DEVMETHOD(device_detach, ata_pci_detach),
483 DEVMETHOD(device_shutdown, bus_generic_shutdown),
484 DEVMETHOD(device_suspend, bus_generic_suspend),
485 DEVMETHOD(device_resume, bus_generic_resume),
488 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
489 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
490 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
491 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
492 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
493 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
498 devclass_t atapci_devclass;
500 static driver_t ata_pci_driver = {
503 sizeof(struct ata_pci_controller),
506 DRIVER_MODULE(atapci, pci, ata_pci_driver, atapci_devclass, 0, 0);
507 MODULE_VERSION(atapci, 1);
508 MODULE_DEPEND(atapci, ata, 1, 1, 1);
511 ata_pcichannel_probe(device_t dev)
513 struct ata_channel *ch = device_get_softc(dev);
518 /* take care of green memory */
519 bzero(ch, sizeof(struct ata_channel));
521 /* find channel number on this controller */
522 device_get_children(device_get_parent(dev), &children, &count);
523 for (i = 0; i < count; i++) {
524 if (children[i] == dev)
527 free(children, M_TEMP);
529 sprintf(buffer, "ATA channel %d", ch->unit);
530 device_set_desc_copy(dev, buffer);
532 return ata_probe(dev);
536 ata_pcichannel_attach(device_t dev)
538 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
539 struct ata_channel *ch = device_get_softc(dev);
547 if ((error = ctlr->allocate(dev)))
550 return ata_attach(dev);
554 ata_pcichannel_detach(device_t dev)
556 struct ata_channel *ch = device_get_softc(dev);
559 if ((error = ata_detach(dev)))
565 /* XXX SOS free resources for io and ctlio ?? */
571 ata_pcichannel_locking(device_t dev, int mode)
573 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
574 struct ata_channel *ch = device_get_softc(dev);
577 return ctlr->locking(dev, mode);
583 ata_pcichannel_reset(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);
588 /* if DMA engine present reset it */
592 ch->dma->unload(dev);
595 /* reset the controller HW */
599 ata_generic_reset(dev);
603 ata_pcichannel_setmode(device_t parent, device_t dev)
605 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
606 struct ata_device *atadev = device_get_softc(dev);
607 int mode = atadev->mode;
609 ctlr->setmode(dev, ATA_PIO_MAX);
611 ctlr->setmode(dev, mode);
614 static device_method_t ata_pcichannel_methods[] = {
615 /* device interface */
616 DEVMETHOD(device_probe, ata_pcichannel_probe),
617 DEVMETHOD(device_attach, ata_pcichannel_attach),
618 DEVMETHOD(device_detach, ata_pcichannel_detach),
619 DEVMETHOD(device_shutdown, bus_generic_shutdown),
620 DEVMETHOD(device_suspend, ata_suspend),
621 DEVMETHOD(device_resume, ata_resume),
624 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
625 DEVMETHOD(ata_locking, ata_pcichannel_locking),
626 DEVMETHOD(ata_reset, ata_pcichannel_reset),
631 driver_t ata_pcichannel_driver = {
633 ata_pcichannel_methods,
634 sizeof(struct ata_channel),
637 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);