1/*-
2 * Copyright (c) 1998,1999,2000,2001 S�ren Schmidt
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
--- 11 unchanged lines hidden (view full) ---
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 * $FreeBSD: head/sys/dev/ata/ata-all.c 72096 2001-02-06 12:49:33Z sos $
29 */
30
31#include "card.h"
32#include "pci.h"
33#include "opt_global.h"
34#include "opt_isa.h"
35#include "opt_ata.h"
36#include <sys/param.h>
--- 23 unchanged lines hidden (view full) ---
60#include <dev/ata/ata-disk.h>
61#include <dev/ata/atapi-all.h>
62
63/* misc defines */
64#define IOMASK 0xfffffffc
65#define ATA_IOADDR_RID 0
66#define ATA_ALTADDR_RID 1
67#define ATA_BMADDR_RID 2
68#if NPCI > 0
69#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \
70 (pci_get_progif(dev) & 0x05) != 0x05)
71#else
72#define ATA_MASTERDEV(dev) (1)
73#endif
74
75/* prototypes */
76static int ata_probe(device_t);
77static int ata_attach(device_t);
78static int ata_detach(device_t);
79static int ata_resume(device_t);
80static void ata_boot_attach(void);
81static void ata_intr(void *);
--- 19 unchanged lines hidden (view full) ---
101 {0x0306d041, "Generic ATA"}, /* PNP0603 */
102 {0}
103};
104
105static int
106ata_isa_probe(device_t dev)
107{
108 struct ata_softc *scp = device_get_softc(dev);
109 struct resource *port;
110 int rid;
111 u_long tmp;
112
113 /* check isapnp ids */
114 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
115 return ENXIO;
116
117 /* allocate the port range */
118 rid = ATA_IOADDR_RID;
119 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
120 ATA_IOSIZE, RF_ACTIVE);
121 if (!port)
122 return ENOMEM;
123
124 /* alloctate the altport range */
125 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
126 bus_set_resource(dev, SYS_RES_IOPORT, 1,
127 rman_get_start(port) + ATA_ALTOFFSET,
128 ATA_ALTIOSIZE);
129 }
130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
131 scp->channel = 0;
132 scp->flags |= ATA_USE_16BIT;
133 return ata_probe(dev);
134}
135
136static device_method_t ata_isa_methods[] = {
137 /* device interface */
138 DEVMETHOD(device_probe, ata_isa_probe),
--- 11 unchanged lines hidden (view full) ---
150DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
151#endif
152
153#if NCARD > 0
154static int
155ata_pccard_probe(device_t dev)
156{
157 struct ata_softc *scp = device_get_softc(dev);
158 struct resource *port;
159 int rid, len;
160
161 /* allocate the port range */
162 rid = ATA_IOADDR_RID;
163 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
164 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE);
165 if (!port)
166 return ENOMEM;
167
168 /*
169 * if we got more than the default ATA_IOSIZE ports, this is likely
170 * a pccard system where the altio ports are located just after the
171 * normal io ports, so no need to allocate them.
172 */
173 if (len <= ATA_IOSIZE) {
174 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
175 rman_get_start(port) + ATA_ALTOFFSET, ATA_ALTIOSIZE);
176 }
177 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
178 scp->channel = 0;
179 scp->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
180 return ata_probe(dev);
181}
182
183static device_method_t ata_pccard_methods[] = {
184 /* device interface */
185 DEVMETHOD(device_probe, ata_pccard_probe),
--- 9 unchanged lines hidden (view full) ---
195};
196
197DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
198#endif
199
200#if NPCI > 0
201struct ata_pci_softc {
202 struct resource *bmio;
203 struct resource bmio_1;
204 struct resource bmio_2;
205 struct resource *irq;
206 int irqcnt;
207};
208
209int
210ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid)
211{
212 device_t *children, child;
--- 194 unchanged lines hidden (view full) ---
407 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
408 pci_write_config(dev, 0x53,
409 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
410 break;
411
412 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */
413 case 0x4d30105a:
414 case 0x0d30105a:
415 outb(rman_get_start(sc->bmio) + 0x11,
416 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
417 /* FALLTHROUGH */
418
419 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */
420 outb(rman_get_start(sc->bmio) + 0x1f,
421 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
422 break;
423
424 case 0x00041103: /* HighPoint */
425 switch (pci_get_revid(dev)) {
426 case 0x00:
427 case 0x01:
428 /* turn off interrupt prediction */
429 pci_write_config(dev, 0x51,
--- 42 unchanged lines hidden (view full) ---
472 pci_write_config(dev, 0x50,
473 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
474 }
475 break;
476
477 case 0x10001042: /* RZ 100? known bad, no DMA */
478 case 0x10011042:
479 case 0x06401095: /* CMD 640 known bad, no DMA */
480 sc->bmio = 0x0;
481 device_printf(dev, "Busmastering DMA disabled\n");
482 }
483
484 /*
485 * the Cypress chip is a mess, it contains two ATA functions, but
486 * both channels are visible on the first one.
487 * simply ignore the second function for now, as the right
488 * solution (ignoring the second channel on the first function)
489 * doesn't work with the crappy ATA interrupt setup on the alpha.
490 */
491 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
--- 9 unchanged lines hidden (view full) ---
501
502static int
503ata_pci_print_child(device_t dev, device_t child)
504{
505 struct ata_softc *scp = device_get_softc(child);
506 int retval = 0;
507
508 retval += bus_print_child_header(dev, child);
509 retval += printf(": at 0x%x", scp->ioaddr);
510
511 if (ATA_MASTERDEV(dev))
512 retval += printf(" irq %d", 14 + scp->channel);
513
514 retval += bus_print_child_footer(dev, child);
515
516 return retval;
517}
518
519static struct resource *
520ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
521 u_long start, u_long end, u_long count, u_int flags)
522{
523 struct ata_pci_softc *sc = device_get_softc(dev);
524 int channel = ((struct ata_softc *)device_get_softc(child))->channel;
525 int myrid;
526
527 if (type == SYS_RES_IOPORT) {
528 switch (*rid) {
529 case ATA_IOADDR_RID:
530 if (ATA_MASTERDEV(dev)) {
531 myrid = 0;
532 start = (channel == 0 ? IO_WD1 : IO_WD2);
533 end = start + ATA_IOSIZE - 1;
534 count = ATA_IOSIZE;
535 }
536 else
537 myrid = 0x10 + 8 * channel;
538 break;
539
540 case ATA_ALTADDR_RID:
541 if (ATA_MASTERDEV(dev)) {
542 myrid = 0;
543 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET;
544 end = start + ATA_ALTIOSIZE - 1;
545 count = ATA_ALTIOSIZE;
546 }
547 else
548 myrid = 0x14 + 8 * channel;
549 break;
550
551 case ATA_BMADDR_RID:
552 /* the busmaster resource is shared between the two channels */
553 if (sc->bmio) {
554 if (channel == 0) {
555 sc->bmio_1 = *sc->bmio;
556 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
557 return &sc->bmio_1;
558 } else {
559 sc->bmio_2 = *sc->bmio;
560 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
561 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
562 return &sc->bmio_2;
563 }
564 }
565 return 0;
566
567 default:
568 return 0;
569 }
570
571 if (ATA_MASTERDEV(dev))
572 /* make the parent just pass through the allocation. */
573 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
574 SYS_RES_IOPORT, &myrid,
575 start, end, count, flags);
576 else
577 /* we are using the parent resource directly. */
578 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
579 SYS_RES_IOPORT, &myrid,
580 start, end, count, flags);
581 }
582
583 if (type == SYS_RES_IRQ) {
584 if (*rid != 0)
585 return 0;
586
587 if (ATA_MASTERDEV(dev)) {
588#ifdef __alpha__
589 return alpha_platform_alloc_ide_intr(channel);
590#else
591 int irq = (channel == 0 ? 14 : 15);
592
593 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
594 SYS_RES_IRQ, rid,
595 irq, irq, 1, flags & ~RF_SHAREABLE);
596#endif
597 } else {
598 /* primary and secondary channels share the same interrupt */
599 sc->irqcnt++;
600 if (!sc->irq)
601 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
602 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
603 return sc->irq;
604 }
605 }
--- 20 unchanged lines hidden (view full) ---
626 case ATA_ALTADDR_RID:
627 if (ATA_MASTERDEV(dev))
628 myrid = 0;
629 else
630 myrid = 0x14 + 8 * channel;
631 break;
632
633 case ATA_BMADDR_RID:
634 return 0;
635
636 default:
637 return ENOENT;
638 }
639
640 if (ATA_MASTERDEV(dev))
641 /* make the parent just pass through the allocation. */
642 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
643 SYS_RES_IOPORT, myrid, r);
644 else
645 /* we are using the parent resource directly. */
646 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
647 SYS_RES_IOPORT, myrid, r);
648 }
649 if (type == SYS_RES_IRQ) {
650 if (rid != 0)
651 return ENOENT;
652
653 if (ATA_MASTERDEV(dev)) {
654#ifdef __alpha__
655 return alpha_platform_release_ide_intr(channel, r);
656#else
657 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
658 child, SYS_RES_IRQ, rid, r);
--- 106 unchanged lines hidden (view full) ---
765
766DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
767#endif
768
769static int
770ata_probe(device_t dev)
771{
772 struct ata_softc *scp = device_get_softc(dev);
773 struct resource *io = 0;
774 struct resource *altio = 0;
775 struct resource *bmio = 0;
776 int rid;
777 u_int32_t ioaddr, altioaddr, bmaddr;
778 int mask = 0;
779 u_int8_t status0, status1;
780
781 if (!scp || scp->flags & ATA_ATTACHED)
782 return ENXIO;
783
784 /* initialize the softc basics */
785 scp->active = ATA_IDLE;
786 scp->dev = dev;
787 scp->devices = 0;
788
789 rid = ATA_IOADDR_RID;
790 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
791 ATA_IOSIZE, RF_ACTIVE);
792 if (!io)
793 goto failure;
794 ioaddr = rman_get_start(io);
795
796 rid = ATA_ALTADDR_RID;
797 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
798 ATA_ALTIOSIZE, RF_ACTIVE);
799 if (altio) {
800 if (scp->flags & ATA_USE_16BIT || ATA_MASTERDEV(device_get_parent(dev)))
801 altioaddr = rman_get_start(altio);
802 else
803 altioaddr = rman_get_start(altio) + 0x02;
804 }
805 else
806 altioaddr = ioaddr + ATA_PCCARD_ALTOFFSET;
807
808 rid = ATA_BMADDR_RID;
809 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
810 bmaddr = bmio ? rman_get_start(bmio) : 0;
811
812 /* store the IO resources for eventual later release */
813 scp->r_io = io;
814 scp->r_altio = altio;
815 scp->r_bmio = bmio;
816
817 /* store the physical IO addresse for easy access */
818 scp->ioaddr = ioaddr;
819 scp->altioaddr = altioaddr;
820 scp->bmaddr = bmaddr;
821
822 if (bootverbose)
823 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
824 scp->ioaddr, scp->altioaddr, scp->bmaddr);
825
826 /* do we have any signs of ATA/ATAPI HW being present ? */
827 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
828 DELAY(1);
829 status0 = inb(scp->ioaddr + ATA_STATUS);
830 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
831 mask |= 0x01;
832 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
833 DELAY(1);
834 status1 = inb(scp->ioaddr + ATA_STATUS);
835 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
836 mask |= 0x02;
837
838 if (bootverbose)
839 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
840 mask, status0, status1);
841 if (!mask)
842 goto failure;
843
844 ata_reset(scp, &mask);
845
846 if (!mask)
847 goto failure;
848
849 TAILQ_INIT(&scp->ata_queue);
850 TAILQ_INIT(&scp->atapi_queue);
851 return 0;
852
853failure:
854 if (io)
855 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
856 if (altio)
857 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio);
858 if (bmio)
859 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
860 if (bootverbose)
861 ata_printf(scp, -1, "probe allocation failed\n");
862 return ENXIO;
863}
864
865static int
866ata_attach(device_t dev)
867{
868 struct ata_softc *scp = device_get_softc(dev);
869 int error, rid = 0;
870
871 if (!scp || scp->flags & ATA_ATTACHED)
872 return ENXIO;
873
874 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
875 RF_SHAREABLE | RF_ACTIVE);
876 if (!scp->r_irq) {
877 ata_printf(scp, -1, "unable to allocate interrupt\n");
878 return ENXIO;
879 }
880 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
881 scp, &scp->ih)))
--- 37 unchanged lines hidden (view full) ---
919static int
920ata_detach(device_t dev)
921{
922 struct ata_softc *scp = device_get_softc(dev);
923
924 if (!scp || !(scp->flags & ATA_ATTACHED))
925 return ENXIO;
926
927#ifdef DEV_ATADISK
928 if (scp->devices & ATA_ATA_MASTER)
929 ad_detach(scp->dev_softc[0]);
930 if (scp->devices & ATA_ATA_SLAVE)
931 ad_detach(scp->dev_softc[1]);
932#endif
933#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
934 if (scp->devices & ATA_ATAPI_MASTER)
935 atapi_detach(scp->dev_softc[0]);
936 if (scp->devices & ATA_ATAPI_SLAVE)
937 atapi_detach(scp->dev_softc[1]);
938#endif
939 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
940 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
941 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
942 }
943 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
944 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
945 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
946 }
947 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
948 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
949 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
950 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
951 bus_teardown_intr(dev, scp->r_irq, scp->ih);
952 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
953 if (scp->r_bmio)
954 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
955 if (scp->r_altio)
956 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
957 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
958 scp->flags &= ~ATA_ATTACHED;
959 return 0;
960}
961
962static int
963ata_resume(device_t dev)
964{
--- 6 unchanged lines hidden (view full) ---
971static int
972ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
973{
974 struct ata_params *ata_parm;
975 int8_t buffer[DEV_BSIZE];
976 int retry = 0;
977
978 /* select drive */
979 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
980 DELAY(1);
981
982 /* enable interrupt */
983 outb(scp->altioaddr, ATA_A_4BIT);
984 DELAY(1);
985
986 /* apparently some devices needs this repeated */
987 do {
988 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
989 ata_printf(scp, device, "identify failed\n");
990 return -1;
991 }
992 if (retry++ > 4) {
993 ata_printf(scp, device, "identify retries exceeded\n");
994 return -1;
995 }
996 } while (ata_wait(scp, device,
997 ((command == ATA_C_ATAPI_IDENTIFY) ?
998 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
999
1000 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
1001 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1002 if (!ata_parm) {
1003 ata_printf(scp, device, "malloc for identify data failed\n");
1004 return -1;
1005 }
1006 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1007 if (command == ATA_C_ATA_IDENTIFY ||
1008 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
--- 64 unchanged lines hidden (view full) ---
1073 ata_delayed_attach = NULL;
1074 }
1075}
1076
1077static void
1078ata_intr(void *data)
1079{
1080 struct ata_softc *scp = (struct ata_softc *)data;
1081 struct ata_pci_softc *sc = device_get_softc(device_get_parent(scp->dev));
1082 u_int8_t dmastat = 0;
1083
1084 /*
1085 * since we might share the IRQ with another device, and in some
1086 * cases with our twin channel, we only want to process interrupts
1087 * that we know this channel generated.
1088 */
1089 switch (scp->chiptype) {
1090#if NPCI > 0
1091 case 0x00041103: /* HighPoint HPT366/368/370 */
1092 if (((dmastat = ata_dmastatus(scp)) &
1093 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
1094 return;
1095 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1096 break;
1097
1098 case 0x06481095: /* CMD 648 */
1099 case 0x06491095: /* CMD 649 */
1100 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) &
1101 (scp->channel ? 0x08 : 0x04)))
1102 return;
1103 goto out;
1104
1105 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */
1106 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */
1107 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */
1108 case 0x0d30105a: /* Promise OEM ATA100 */
1109 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1110 (scp->channel ? 0x00004000 : 0x00000400)))
1111 return;
1112 /* FALLTHROUGH */
1113out:
1114#endif
1115 default:
1116 if (scp->flags & ATA_DMA_ACTIVE) {
1117 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1118 return;
1119 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1120 }
1121 }
1122 DELAY(1);
1123
1124 /* if drive is busy it didn't interrupt */
1125 if (inb(scp->altioaddr) & ATA_S_BUSY)
1126 return;
1127
1128 /* clear interrupt and get status */
1129 scp->status = inb(scp->ioaddr + ATA_STATUS);
1130
1131 if (scp->status & ATA_S_ERROR)
1132 scp->error = inb(scp->ioaddr + ATA_ERROR);
1133
1134 /* find & call the responsible driver to process this interrupt */
1135 switch (scp->active) {
1136#ifdef DEV_ATADISK
1137 case ATA_ACTIVE_ATA:
1138 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1139 return;
1140 break;
--- 91 unchanged lines hidden (view full) ---
1232void
1233ata_reset(struct ata_softc *scp, int *mask)
1234{
1235 int timeout;
1236 u_int8_t a, b, ostat0, ostat1;
1237 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY;
1238
1239 /* get the current status of the devices */
1240 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1241 DELAY(10);
1242 ostat1 = inb(scp->ioaddr + ATA_STATUS);
1243 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1244 DELAY(10);
1245 ostat0 = inb(scp->ioaddr + ATA_STATUS);
1246
1247 /* in some setups we dont want to test for a slave */
1248 if (scp->flags & ATA_NO_SLAVE)
1249 *mask &= ~0x02;
1250
1251 if (bootverbose)
1252 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
1253 *mask, ostat0, ostat1);
1254
1255 /* reset channel */
1256 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1257 DELAY(10000);
1258 outb(scp->altioaddr, ATA_A_IDS);
1259 DELAY(100000);
1260 inb(scp->ioaddr + ATA_ERROR);
1261 scp->devices = 0;
1262
1263 /* wait for BUSY to go inactive */
1264 for (timeout = 0; timeout < 310000; timeout++) {
1265 if (status0 & ATA_S_BUSY) {
1266 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1267 DELAY(10);
1268 status0 = inb(scp->ioaddr + ATA_STATUS);
1269 if (!(status0 & ATA_S_BUSY)) {
1270 /* check for ATAPI signature while its still there */
1271 a = inb(scp->ioaddr + ATA_CYL_LSB);
1272 b = inb(scp->ioaddr + ATA_CYL_MSB);
1273 if (bootverbose)
1274 ata_printf(scp, ATA_MASTER,
1275 "ATAPI probe a=%02x b=%02x\n", a, b);
1276 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1277 scp->devices |= ATA_ATAPI_MASTER;
1278 }
1279 }
1280 if (status1 & ATA_S_BUSY) {
1281 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1282 DELAY(10);
1283 status1 = inb(scp->ioaddr + ATA_STATUS);
1284 if (!(status1 & ATA_S_BUSY)) {
1285 /* check for ATAPI signature while its still there */
1286 a = inb(scp->ioaddr + ATA_CYL_LSB);
1287 b = inb(scp->ioaddr + ATA_CYL_MSB);
1288 if (bootverbose)
1289 ata_printf(scp, ATA_SLAVE,
1290 "ATAPI probe a=%02x b=%02x\n", a, b);
1291 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1292 scp->devices |= ATA_ATAPI_SLAVE;
1293 }
1294 }
1295 if (*mask == 0x01) /* wait for master only */
1296 if (!(status0 & ATA_S_BUSY))
1297 break;
1298 if (*mask == 0x02) /* wait for slave only */
1299 if (!(status1 & ATA_S_BUSY))
1300 break;
1301 if (*mask == 0x03) /* wait for both master & slave */
1302 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1303 break;
1304 DELAY(100);
1305 }
1306 DELAY(10);
1307 outb(scp->altioaddr, ATA_A_4BIT);
1308
1309 if (status0 & ATA_S_BUSY)
1310 *mask &= ~0x01;
1311 if (status1 & ATA_S_BUSY)
1312 *mask &= ~0x02;
1313 if (bootverbose)
1314 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1315 *mask, status0, status1);
1316 if (!*mask)
1317 return;
1318
1319 if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
1320 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1321 DELAY(10);
1322 outb(scp->ioaddr + ATA_ERROR, 0x58);
1323 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1324 a = inb(scp->ioaddr + ATA_ERROR);
1325 b = inb(scp->ioaddr + ATA_CYL_LSB);
1326 if (bootverbose)
1327 ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b);
1328 if (a != 0x58 && b == 0xa5)
1329 scp->devices |= ATA_ATA_MASTER;
1330 }
1331 if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
1332 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1333 DELAY(10);
1334 outb(scp->ioaddr + ATA_ERROR, 0x58);
1335 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1336 a = inb(scp->ioaddr + ATA_ERROR);
1337 b = inb(scp->ioaddr + ATA_CYL_LSB);
1338 if (bootverbose)
1339 ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b);
1340 if (a != 0x58 && b == 0xa5)
1341 scp->devices |= ATA_ATA_SLAVE;
1342 }
1343 if (bootverbose)
1344 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
1345}
--- 35 unchanged lines hidden (view full) ---
1381 return 0;
1382}
1383
1384static int
1385ata_service(struct ata_softc *scp)
1386{
1387 /* do we have a SERVICE request from the drive ? */
1388 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
1389 outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
1390#ifdef DEV_ATADISK
1391 if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
1392 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0])
1393 return ad_service((struct ad_softc *)scp->dev_softc[0], 0);
1394 }
1395 else {
1396 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1])
1397 return ad_service((struct ad_softc *)scp->dev_softc[1], 0);
1398 }
1399#endif
1400 }
1401 return ATA_OP_FINISHED;
1402}
1403
1404int
1405ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
1406{
1407 int timeout = 0;
1408 int statio = scp->ioaddr + ATA_STATUS;
1409
1410 DELAY(1);
1411 while (timeout < 5000000) { /* timeout 5 secs */
1412 scp->status = inb(statio);
1413
1414 /* if drive fails status, reselect the drive just to be sure */
1415 if (scp->status == 0xff) {
1416 ata_printf(scp, device, "no status, reselecting device\n");
1417 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1418 DELAY(1);
1419 scp->status = inb(statio);
1420 }
1421
1422 /* are we done ? */
1423 if (!(scp->status & ATA_S_BUSY))
1424 break;
1425
1426 if (timeout > 1000) {
1427 timeout += 1000;
1428 DELAY(1000);
1429 }
1430 else {
1431 timeout += 10;
1432 DELAY(10);
1433 }
1434 }
1435 if (scp->status & ATA_S_ERROR)
1436 scp->error = inb(scp->ioaddr + ATA_ERROR);
1437 if (timeout >= 5000000)
1438 return -1;
1439 if (!mask)
1440 return (scp->status & ATA_S_ERROR);
1441
1442 /* Wait 50 msec for bits wanted. */
1443 timeout = 5000;
1444 while (timeout--) {
1445 scp->status = inb(statio);
1446 if ((scp->status & mask) == mask) {
1447 if (scp->status & ATA_S_ERROR)
1448 scp->error = inb(scp->ioaddr + ATA_ERROR);
1449 return (scp->status & ATA_S_ERROR);
1450 }
1451 DELAY (10);
1452 }
1453 return -1;
1454}
1455
1456int
1457ata_command(struct ata_softc *scp, int device, u_int8_t command,
1458 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
1459 u_int8_t count, u_int8_t feature, int flags)
1460{
1461 int error = 0;
1462#ifdef ATA_DEBUG
1463 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1464 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
1465 scp->ioaddr, command, cylinder, head, sector,
1466 count, feature, flags);
1467#endif
1468
1469 /* disable interrupt from device */
1470 if (scp->flags & ATA_QUEUED)
1471 outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT);
1472
1473 /* select device */
1474 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1475
1476 /* ready to issue command ? */
1477 if (ata_wait(scp, device, 0) < 0) {
1478 ata_printf(scp, device,
1479 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1480 command, scp->status, scp->error);
1481 return -1;
1482 }
1483
1484 outb(scp->ioaddr + ATA_FEATURE, feature);
1485 outb(scp->ioaddr + ATA_COUNT, count);
1486 outb(scp->ioaddr + ATA_SECTOR, sector);
1487 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1488 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1489 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1490
1491 switch (flags) {
1492 case ATA_WAIT_INTR:
1493 scp->active = ATA_WAIT_INTR;
1494 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1495 outb(scp->ioaddr + ATA_CMD, command);
1496
1497 /* enable interrupt */
1498 if (scp->flags & ATA_QUEUED)
1499 outb(scp->altioaddr, ATA_A_4BIT);
1500
1501 if (await(PRIBIO, 10 * hz)) {
1502 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1503 scp->active = ATA_IDLE;
1504 error = -1;
1505 }
1506 break;
1507
1508 case ATA_WAIT_READY:
1509 if (scp->active != ATA_REINITING)
1510 scp->active = ATA_WAIT_READY;
1511 outb(scp->ioaddr + ATA_CMD, command);
1512 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1513 ata_printf(scp, device,
1514 "timeout waiting for command=%02x s=%02x e=%02x\n",
1515 command, scp->status, scp->error);
1516 error = -1;
1517 }
1518 if (scp->active != ATA_REINITING)
1519 scp->active = ATA_IDLE;
1520 break;
1521
1522 case ATA_IMMEDIATE:
1523 outb(scp->ioaddr + ATA_CMD, command);
1524 break;
1525
1526 default:
1527 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1528 active2str(flags));
1529 }
1530 /* enable interrupt */
1531 if (scp->flags & ATA_QUEUED)
1532 outb(scp->altioaddr, ATA_A_4BIT);
1533 return error;
1534}
1535
1536int
1537ata_get_lun(u_int32_t *map)
1538{
1539 int lun = ffs(~*map) - 1;
1540
--- 271 unchanged lines hidden ---
2 * Copyright (c) 1998,1999,2000,2001 S�ren Schmidt
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
--- 11 unchanged lines hidden (view full) ---
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 * $FreeBSD: head/sys/dev/ata/ata-all.c 72096 2001-02-06 12:49:33Z sos $
29 */
30
31#include "card.h"
32#include "pci.h"
33#include "opt_global.h"
34#include "opt_isa.h"
35#include "opt_ata.h"
36#include <sys/param.h>
--- 23 unchanged lines hidden (view full) ---
60#include <dev/ata/ata-disk.h>
61#include <dev/ata/atapi-all.h>
62
63/* misc defines */
64#define IOMASK 0xfffffffc
65#define ATA_IOADDR_RID 0
66#define ATA_ALTADDR_RID 1
67#define ATA_BMADDR_RID 2
68#if NPCI > 0
69#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \
70 (pci_get_progif(dev) & 0x05) != 0x05)
71#else
72#define ATA_MASTERDEV(dev) (1)
73#endif
74
75/* prototypes */
76static int ata_probe(device_t);
77static int ata_attach(device_t);
78static int ata_detach(device_t);
79static int ata_resume(device_t);
80static void ata_boot_attach(void);
81static void ata_intr(void *);
--- 19 unchanged lines hidden (view full) ---
101 {0x0306d041, "Generic ATA"}, /* PNP0603 */
102 {0}
103};
104
105static int
106ata_isa_probe(device_t dev)
107{
108 struct ata_softc *scp = device_get_softc(dev);
109 struct resource *port;
110 int rid;
111 u_long tmp;
112
113 /* check isapnp ids */
114 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
115 return ENXIO;
116
117 /* allocate the port range */
118 rid = ATA_IOADDR_RID;
119 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
120 ATA_IOSIZE, RF_ACTIVE);
121 if (!port)
122 return ENOMEM;
123
124 /* alloctate the altport range */
125 if (bus_get_resource(dev, SYS_RES_IOPORT, 1, &tmp, &tmp)) {
126 bus_set_resource(dev, SYS_RES_IOPORT, 1,
127 rman_get_start(port) + ATA_ALTOFFSET,
128 ATA_ALTIOSIZE);
129 }
130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
131 scp->channel = 0;
132 scp->flags |= ATA_USE_16BIT;
133 return ata_probe(dev);
134}
135
136static device_method_t ata_isa_methods[] = {
137 /* device interface */
138 DEVMETHOD(device_probe, ata_isa_probe),
--- 11 unchanged lines hidden (view full) ---
150DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
151#endif
152
153#if NCARD > 0
154static int
155ata_pccard_probe(device_t dev)
156{
157 struct ata_softc *scp = device_get_softc(dev);
158 struct resource *port;
159 int rid, len;
160
161 /* allocate the port range */
162 rid = ATA_IOADDR_RID;
163 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
164 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, len, RF_ACTIVE);
165 if (!port)
166 return ENOMEM;
167
168 /*
169 * if we got more than the default ATA_IOSIZE ports, this is likely
170 * a pccard system where the altio ports are located just after the
171 * normal io ports, so no need to allocate them.
172 */
173 if (len <= ATA_IOSIZE) {
174 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
175 rman_get_start(port) + ATA_ALTOFFSET, ATA_ALTIOSIZE);
176 }
177 bus_release_resource(dev, SYS_RES_IOPORT, 0, port);
178 scp->channel = 0;
179 scp->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
180 return ata_probe(dev);
181}
182
183static device_method_t ata_pccard_methods[] = {
184 /* device interface */
185 DEVMETHOD(device_probe, ata_pccard_probe),
--- 9 unchanged lines hidden (view full) ---
195};
196
197DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
198#endif
199
200#if NPCI > 0
201struct ata_pci_softc {
202 struct resource *bmio;
203 struct resource bmio_1;
204 struct resource bmio_2;
205 struct resource *irq;
206 int irqcnt;
207};
208
209int
210ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid)
211{
212 device_t *children, child;
--- 194 unchanged lines hidden (view full) ---
407 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
408 pci_write_config(dev, 0x53,
409 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
410 break;
411
412 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */
413 case 0x4d30105a:
414 case 0x0d30105a:
415 outb(rman_get_start(sc->bmio) + 0x11,
416 inb(rman_get_start(sc->bmio) + 0x11) | 0x0a);
417 /* FALLTHROUGH */
418
419 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */
420 outb(rman_get_start(sc->bmio) + 0x1f,
421 inb(rman_get_start(sc->bmio) + 0x1f) | 0x01);
422 break;
423
424 case 0x00041103: /* HighPoint */
425 switch (pci_get_revid(dev)) {
426 case 0x00:
427 case 0x01:
428 /* turn off interrupt prediction */
429 pci_write_config(dev, 0x51,
--- 42 unchanged lines hidden (view full) ---
472 pci_write_config(dev, 0x50,
473 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
474 }
475 break;
476
477 case 0x10001042: /* RZ 100? known bad, no DMA */
478 case 0x10011042:
479 case 0x06401095: /* CMD 640 known bad, no DMA */
480 sc->bmio = 0x0;
481 device_printf(dev, "Busmastering DMA disabled\n");
482 }
483
484 /*
485 * the Cypress chip is a mess, it contains two ATA functions, but
486 * both channels are visible on the first one.
487 * simply ignore the second function for now, as the right
488 * solution (ignoring the second channel on the first function)
489 * doesn't work with the crappy ATA interrupt setup on the alpha.
490 */
491 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
--- 9 unchanged lines hidden (view full) ---
501
502static int
503ata_pci_print_child(device_t dev, device_t child)
504{
505 struct ata_softc *scp = device_get_softc(child);
506 int retval = 0;
507
508 retval += bus_print_child_header(dev, child);
509 retval += printf(": at 0x%x", scp->ioaddr);
510
511 if (ATA_MASTERDEV(dev))
512 retval += printf(" irq %d", 14 + scp->channel);
513
514 retval += bus_print_child_footer(dev, child);
515
516 return retval;
517}
518
519static struct resource *
520ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
521 u_long start, u_long end, u_long count, u_int flags)
522{
523 struct ata_pci_softc *sc = device_get_softc(dev);
524 int channel = ((struct ata_softc *)device_get_softc(child))->channel;
525 int myrid;
526
527 if (type == SYS_RES_IOPORT) {
528 switch (*rid) {
529 case ATA_IOADDR_RID:
530 if (ATA_MASTERDEV(dev)) {
531 myrid = 0;
532 start = (channel == 0 ? IO_WD1 : IO_WD2);
533 end = start + ATA_IOSIZE - 1;
534 count = ATA_IOSIZE;
535 }
536 else
537 myrid = 0x10 + 8 * channel;
538 break;
539
540 case ATA_ALTADDR_RID:
541 if (ATA_MASTERDEV(dev)) {
542 myrid = 0;
543 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET;
544 end = start + ATA_ALTIOSIZE - 1;
545 count = ATA_ALTIOSIZE;
546 }
547 else
548 myrid = 0x14 + 8 * channel;
549 break;
550
551 case ATA_BMADDR_RID:
552 /* the busmaster resource is shared between the two channels */
553 if (sc->bmio) {
554 if (channel == 0) {
555 sc->bmio_1 = *sc->bmio;
556 sc->bmio_1.r_end = sc->bmio->r_start + ATA_BM_OFFSET1;
557 return &sc->bmio_1;
558 } else {
559 sc->bmio_2 = *sc->bmio;
560 sc->bmio_2.r_start = sc->bmio->r_start + ATA_BM_OFFSET1;
561 sc->bmio_2.r_end = sc->bmio_2.r_start + ATA_BM_OFFSET1;
562 return &sc->bmio_2;
563 }
564 }
565 return 0;
566
567 default:
568 return 0;
569 }
570
571 if (ATA_MASTERDEV(dev))
572 /* make the parent just pass through the allocation. */
573 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
574 SYS_RES_IOPORT, &myrid,
575 start, end, count, flags);
576 else
577 /* we are using the parent resource directly. */
578 return BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
579 SYS_RES_IOPORT, &myrid,
580 start, end, count, flags);
581 }
582
583 if (type == SYS_RES_IRQ) {
584 if (*rid != 0)
585 return 0;
586
587 if (ATA_MASTERDEV(dev)) {
588#ifdef __alpha__
589 return alpha_platform_alloc_ide_intr(channel);
590#else
591 int irq = (channel == 0 ? 14 : 15);
592
593 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
594 SYS_RES_IRQ, rid,
595 irq, irq, 1, flags & ~RF_SHAREABLE);
596#endif
597 } else {
598 /* primary and secondary channels share the same interrupt */
599 sc->irqcnt++;
600 if (!sc->irq)
601 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
602 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
603 return sc->irq;
604 }
605 }
--- 20 unchanged lines hidden (view full) ---
626 case ATA_ALTADDR_RID:
627 if (ATA_MASTERDEV(dev))
628 myrid = 0;
629 else
630 myrid = 0x14 + 8 * channel;
631 break;
632
633 case ATA_BMADDR_RID:
634 return 0;
635
636 default:
637 return ENOENT;
638 }
639
640 if (ATA_MASTERDEV(dev))
641 /* make the parent just pass through the allocation. */
642 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
643 SYS_RES_IOPORT, myrid, r);
644 else
645 /* we are using the parent resource directly. */
646 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
647 SYS_RES_IOPORT, myrid, r);
648 }
649 if (type == SYS_RES_IRQ) {
650 if (rid != 0)
651 return ENOENT;
652
653 if (ATA_MASTERDEV(dev)) {
654#ifdef __alpha__
655 return alpha_platform_release_ide_intr(channel, r);
656#else
657 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
658 child, SYS_RES_IRQ, rid, r);
--- 106 unchanged lines hidden (view full) ---
765
766DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
767#endif
768
769static int
770ata_probe(device_t dev)
771{
772 struct ata_softc *scp = device_get_softc(dev);
773 struct resource *io = 0;
774 struct resource *altio = 0;
775 struct resource *bmio = 0;
776 int rid;
777 u_int32_t ioaddr, altioaddr, bmaddr;
778 int mask = 0;
779 u_int8_t status0, status1;
780
781 if (!scp || scp->flags & ATA_ATTACHED)
782 return ENXIO;
783
784 /* initialize the softc basics */
785 scp->active = ATA_IDLE;
786 scp->dev = dev;
787 scp->devices = 0;
788
789 rid = ATA_IOADDR_RID;
790 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
791 ATA_IOSIZE, RF_ACTIVE);
792 if (!io)
793 goto failure;
794 ioaddr = rman_get_start(io);
795
796 rid = ATA_ALTADDR_RID;
797 altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
798 ATA_ALTIOSIZE, RF_ACTIVE);
799 if (altio) {
800 if (scp->flags & ATA_USE_16BIT || ATA_MASTERDEV(device_get_parent(dev)))
801 altioaddr = rman_get_start(altio);
802 else
803 altioaddr = rman_get_start(altio) + 0x02;
804 }
805 else
806 altioaddr = ioaddr + ATA_PCCARD_ALTOFFSET;
807
808 rid = ATA_BMADDR_RID;
809 bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
810 bmaddr = bmio ? rman_get_start(bmio) : 0;
811
812 /* store the IO resources for eventual later release */
813 scp->r_io = io;
814 scp->r_altio = altio;
815 scp->r_bmio = bmio;
816
817 /* store the physical IO addresse for easy access */
818 scp->ioaddr = ioaddr;
819 scp->altioaddr = altioaddr;
820 scp->bmaddr = bmaddr;
821
822 if (bootverbose)
823 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
824 scp->ioaddr, scp->altioaddr, scp->bmaddr);
825
826 /* do we have any signs of ATA/ATAPI HW being present ? */
827 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
828 DELAY(1);
829 status0 = inb(scp->ioaddr + ATA_STATUS);
830 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
831 mask |= 0x01;
832 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
833 DELAY(1);
834 status1 = inb(scp->ioaddr + ATA_STATUS);
835 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
836 mask |= 0x02;
837
838 if (bootverbose)
839 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
840 mask, status0, status1);
841 if (!mask)
842 goto failure;
843
844 ata_reset(scp, &mask);
845
846 if (!mask)
847 goto failure;
848
849 TAILQ_INIT(&scp->ata_queue);
850 TAILQ_INIT(&scp->atapi_queue);
851 return 0;
852
853failure:
854 if (io)
855 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
856 if (altio)
857 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, altio);
858 if (bmio)
859 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, bmio);
860 if (bootverbose)
861 ata_printf(scp, -1, "probe allocation failed\n");
862 return ENXIO;
863}
864
865static int
866ata_attach(device_t dev)
867{
868 struct ata_softc *scp = device_get_softc(dev);
869 int error, rid = 0;
870
871 if (!scp || scp->flags & ATA_ATTACHED)
872 return ENXIO;
873
874 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
875 RF_SHAREABLE | RF_ACTIVE);
876 if (!scp->r_irq) {
877 ata_printf(scp, -1, "unable to allocate interrupt\n");
878 return ENXIO;
879 }
880 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
881 scp, &scp->ih)))
--- 37 unchanged lines hidden (view full) ---
919static int
920ata_detach(device_t dev)
921{
922 struct ata_softc *scp = device_get_softc(dev);
923
924 if (!scp || !(scp->flags & ATA_ATTACHED))
925 return ENXIO;
926
927#ifdef DEV_ATADISK
928 if (scp->devices & ATA_ATA_MASTER)
929 ad_detach(scp->dev_softc[0]);
930 if (scp->devices & ATA_ATA_SLAVE)
931 ad_detach(scp->dev_softc[1]);
932#endif
933#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
934 if (scp->devices & ATA_ATAPI_MASTER)
935 atapi_detach(scp->dev_softc[0]);
936 if (scp->devices & ATA_ATAPI_SLAVE)
937 atapi_detach(scp->dev_softc[1]);
938#endif
939 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
940 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
941 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
942 }
943 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
944 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
945 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
946 }
947 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
948 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
949 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
950 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
951 bus_teardown_intr(dev, scp->r_irq, scp->ih);
952 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->r_irq);
953 if (scp->r_bmio)
954 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
955 if (scp->r_altio)
956 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
957 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
958 scp->flags &= ~ATA_ATTACHED;
959 return 0;
960}
961
962static int
963ata_resume(device_t dev)
964{
--- 6 unchanged lines hidden (view full) ---
971static int
972ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
973{
974 struct ata_params *ata_parm;
975 int8_t buffer[DEV_BSIZE];
976 int retry = 0;
977
978 /* select drive */
979 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
980 DELAY(1);
981
982 /* enable interrupt */
983 outb(scp->altioaddr, ATA_A_4BIT);
984 DELAY(1);
985
986 /* apparently some devices needs this repeated */
987 do {
988 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
989 ata_printf(scp, device, "identify failed\n");
990 return -1;
991 }
992 if (retry++ > 4) {
993 ata_printf(scp, device, "identify retries exceeded\n");
994 return -1;
995 }
996 } while (ata_wait(scp, device,
997 ((command == ATA_C_ATAPI_IDENTIFY) ?
998 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
999
1000 insw(scp->ioaddr + ATA_DATA, buffer, sizeof(buffer)/sizeof(int16_t));
1001 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1002 if (!ata_parm) {
1003 ata_printf(scp, device, "malloc for identify data failed\n");
1004 return -1;
1005 }
1006 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1007 if (command == ATA_C_ATA_IDENTIFY ||
1008 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
--- 64 unchanged lines hidden (view full) ---
1073 ata_delayed_attach = NULL;
1074 }
1075}
1076
1077static void
1078ata_intr(void *data)
1079{
1080 struct ata_softc *scp = (struct ata_softc *)data;
1081 struct ata_pci_softc *sc = device_get_softc(device_get_parent(scp->dev));
1082 u_int8_t dmastat = 0;
1083
1084 /*
1085 * since we might share the IRQ with another device, and in some
1086 * cases with our twin channel, we only want to process interrupts
1087 * that we know this channel generated.
1088 */
1089 switch (scp->chiptype) {
1090#if NPCI > 0
1091 case 0x00041103: /* HighPoint HPT366/368/370 */
1092 if (((dmastat = ata_dmastatus(scp)) &
1093 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
1094 return;
1095 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1096 break;
1097
1098 case 0x06481095: /* CMD 648 */
1099 case 0x06491095: /* CMD 649 */
1100 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) &
1101 (scp->channel ? 0x08 : 0x04)))
1102 return;
1103 goto out;
1104
1105 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */
1106 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */
1107 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */
1108 case 0x0d30105a: /* Promise OEM ATA100 */
1109 if (!(inl(rman_get_start(sc->bmio) + 0x1c) &
1110 (scp->channel ? 0x00004000 : 0x00000400)))
1111 return;
1112 /* FALLTHROUGH */
1113out:
1114#endif
1115 default:
1116 if (scp->flags & ATA_DMA_ACTIVE) {
1117 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1118 return;
1119 outb(scp->bmaddr + ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1120 }
1121 }
1122 DELAY(1);
1123
1124 /* if drive is busy it didn't interrupt */
1125 if (inb(scp->altioaddr) & ATA_S_BUSY)
1126 return;
1127
1128 /* clear interrupt and get status */
1129 scp->status = inb(scp->ioaddr + ATA_STATUS);
1130
1131 if (scp->status & ATA_S_ERROR)
1132 scp->error = inb(scp->ioaddr + ATA_ERROR);
1133
1134 /* find & call the responsible driver to process this interrupt */
1135 switch (scp->active) {
1136#ifdef DEV_ATADISK
1137 case ATA_ACTIVE_ATA:
1138 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1139 return;
1140 break;
--- 91 unchanged lines hidden (view full) ---
1232void
1233ata_reset(struct ata_softc *scp, int *mask)
1234{
1235 int timeout;
1236 u_int8_t a, b, ostat0, ostat1;
1237 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY;
1238
1239 /* get the current status of the devices */
1240 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1241 DELAY(10);
1242 ostat1 = inb(scp->ioaddr + ATA_STATUS);
1243 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1244 DELAY(10);
1245 ostat0 = inb(scp->ioaddr + ATA_STATUS);
1246
1247 /* in some setups we dont want to test for a slave */
1248 if (scp->flags & ATA_NO_SLAVE)
1249 *mask &= ~0x02;
1250
1251 if (bootverbose)
1252 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
1253 *mask, ostat0, ostat1);
1254
1255 /* reset channel */
1256 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
1257 DELAY(10000);
1258 outb(scp->altioaddr, ATA_A_IDS);
1259 DELAY(100000);
1260 inb(scp->ioaddr + ATA_ERROR);
1261 scp->devices = 0;
1262
1263 /* wait for BUSY to go inactive */
1264 for (timeout = 0; timeout < 310000; timeout++) {
1265 if (status0 & ATA_S_BUSY) {
1266 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1267 DELAY(10);
1268 status0 = inb(scp->ioaddr + ATA_STATUS);
1269 if (!(status0 & ATA_S_BUSY)) {
1270 /* check for ATAPI signature while its still there */
1271 a = inb(scp->ioaddr + ATA_CYL_LSB);
1272 b = inb(scp->ioaddr + ATA_CYL_MSB);
1273 if (bootverbose)
1274 ata_printf(scp, ATA_MASTER,
1275 "ATAPI probe a=%02x b=%02x\n", a, b);
1276 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1277 scp->devices |= ATA_ATAPI_MASTER;
1278 }
1279 }
1280 if (status1 & ATA_S_BUSY) {
1281 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1282 DELAY(10);
1283 status1 = inb(scp->ioaddr + ATA_STATUS);
1284 if (!(status1 & ATA_S_BUSY)) {
1285 /* check for ATAPI signature while its still there */
1286 a = inb(scp->ioaddr + ATA_CYL_LSB);
1287 b = inb(scp->ioaddr + ATA_CYL_MSB);
1288 if (bootverbose)
1289 ata_printf(scp, ATA_SLAVE,
1290 "ATAPI probe a=%02x b=%02x\n", a, b);
1291 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1292 scp->devices |= ATA_ATAPI_SLAVE;
1293 }
1294 }
1295 if (*mask == 0x01) /* wait for master only */
1296 if (!(status0 & ATA_S_BUSY))
1297 break;
1298 if (*mask == 0x02) /* wait for slave only */
1299 if (!(status1 & ATA_S_BUSY))
1300 break;
1301 if (*mask == 0x03) /* wait for both master & slave */
1302 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1303 break;
1304 DELAY(100);
1305 }
1306 DELAY(10);
1307 outb(scp->altioaddr, ATA_A_4BIT);
1308
1309 if (status0 & ATA_S_BUSY)
1310 *mask &= ~0x01;
1311 if (status1 & ATA_S_BUSY)
1312 *mask &= ~0x02;
1313 if (bootverbose)
1314 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1315 *mask, status0, status1);
1316 if (!*mask)
1317 return;
1318
1319 if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
1320 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1321 DELAY(10);
1322 outb(scp->ioaddr + ATA_ERROR, 0x58);
1323 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1324 a = inb(scp->ioaddr + ATA_ERROR);
1325 b = inb(scp->ioaddr + ATA_CYL_LSB);
1326 if (bootverbose)
1327 ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b);
1328 if (a != 0x58 && b == 0xa5)
1329 scp->devices |= ATA_ATA_MASTER;
1330 }
1331 if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
1332 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1333 DELAY(10);
1334 outb(scp->ioaddr + ATA_ERROR, 0x58);
1335 outb(scp->ioaddr + ATA_CYL_LSB, 0xa5);
1336 a = inb(scp->ioaddr + ATA_ERROR);
1337 b = inb(scp->ioaddr + ATA_CYL_LSB);
1338 if (bootverbose)
1339 ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b);
1340 if (a != 0x58 && b == 0xa5)
1341 scp->devices |= ATA_ATA_SLAVE;
1342 }
1343 if (bootverbose)
1344 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
1345}
--- 35 unchanged lines hidden (view full) ---
1381 return 0;
1382}
1383
1384static int
1385ata_service(struct ata_softc *scp)
1386{
1387 /* do we have a SERVICE request from the drive ? */
1388 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
1389 outb(scp->bmaddr + ATA_BMSTAT_PORT, ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
1390#ifdef DEV_ATADISK
1391 if ((inb(scp->ioaddr + ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
1392 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0])
1393 return ad_service((struct ad_softc *)scp->dev_softc[0], 0);
1394 }
1395 else {
1396 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1])
1397 return ad_service((struct ad_softc *)scp->dev_softc[1], 0);
1398 }
1399#endif
1400 }
1401 return ATA_OP_FINISHED;
1402}
1403
1404int
1405ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
1406{
1407 int timeout = 0;
1408 int statio = scp->ioaddr + ATA_STATUS;
1409
1410 DELAY(1);
1411 while (timeout < 5000000) { /* timeout 5 secs */
1412 scp->status = inb(statio);
1413
1414 /* if drive fails status, reselect the drive just to be sure */
1415 if (scp->status == 0xff) {
1416 ata_printf(scp, device, "no status, reselecting device\n");
1417 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1418 DELAY(1);
1419 scp->status = inb(statio);
1420 }
1421
1422 /* are we done ? */
1423 if (!(scp->status & ATA_S_BUSY))
1424 break;
1425
1426 if (timeout > 1000) {
1427 timeout += 1000;
1428 DELAY(1000);
1429 }
1430 else {
1431 timeout += 10;
1432 DELAY(10);
1433 }
1434 }
1435 if (scp->status & ATA_S_ERROR)
1436 scp->error = inb(scp->ioaddr + ATA_ERROR);
1437 if (timeout >= 5000000)
1438 return -1;
1439 if (!mask)
1440 return (scp->status & ATA_S_ERROR);
1441
1442 /* Wait 50 msec for bits wanted. */
1443 timeout = 5000;
1444 while (timeout--) {
1445 scp->status = inb(statio);
1446 if ((scp->status & mask) == mask) {
1447 if (scp->status & ATA_S_ERROR)
1448 scp->error = inb(scp->ioaddr + ATA_ERROR);
1449 return (scp->status & ATA_S_ERROR);
1450 }
1451 DELAY (10);
1452 }
1453 return -1;
1454}
1455
1456int
1457ata_command(struct ata_softc *scp, int device, u_int8_t command,
1458 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
1459 u_int8_t count, u_int8_t feature, int flags)
1460{
1461 int error = 0;
1462#ifdef ATA_DEBUG
1463 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1464 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
1465 scp->ioaddr, command, cylinder, head, sector,
1466 count, feature, flags);
1467#endif
1468
1469 /* disable interrupt from device */
1470 if (scp->flags & ATA_QUEUED)
1471 outb(scp->altioaddr, ATA_A_IDS | ATA_A_4BIT);
1472
1473 /* select device */
1474 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
1475
1476 /* ready to issue command ? */
1477 if (ata_wait(scp, device, 0) < 0) {
1478 ata_printf(scp, device,
1479 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1480 command, scp->status, scp->error);
1481 return -1;
1482 }
1483
1484 outb(scp->ioaddr + ATA_FEATURE, feature);
1485 outb(scp->ioaddr + ATA_COUNT, count);
1486 outb(scp->ioaddr + ATA_SECTOR, sector);
1487 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
1488 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
1489 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
1490
1491 switch (flags) {
1492 case ATA_WAIT_INTR:
1493 scp->active = ATA_WAIT_INTR;
1494 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1495 outb(scp->ioaddr + ATA_CMD, command);
1496
1497 /* enable interrupt */
1498 if (scp->flags & ATA_QUEUED)
1499 outb(scp->altioaddr, ATA_A_4BIT);
1500
1501 if (await(PRIBIO, 10 * hz)) {
1502 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1503 scp->active = ATA_IDLE;
1504 error = -1;
1505 }
1506 break;
1507
1508 case ATA_WAIT_READY:
1509 if (scp->active != ATA_REINITING)
1510 scp->active = ATA_WAIT_READY;
1511 outb(scp->ioaddr + ATA_CMD, command);
1512 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1513 ata_printf(scp, device,
1514 "timeout waiting for command=%02x s=%02x e=%02x\n",
1515 command, scp->status, scp->error);
1516 error = -1;
1517 }
1518 if (scp->active != ATA_REINITING)
1519 scp->active = ATA_IDLE;
1520 break;
1521
1522 case ATA_IMMEDIATE:
1523 outb(scp->ioaddr + ATA_CMD, command);
1524 break;
1525
1526 default:
1527 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1528 active2str(flags));
1529 }
1530 /* enable interrupt */
1531 if (scp->flags & ATA_QUEUED)
1532 outb(scp->altioaddr, ATA_A_4BIT);
1533 return error;
1534}
1535
1536int
1537ata_get_lun(u_int32_t *map)
1538{
1539 int lun = ffs(~*map) - 1;
1540
--- 271 unchanged lines hidden ---