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 72106 2001-02-06 16:44:25Z 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#define ATA_IRQ_RID 0
69#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \
70 (pci_get_progif(dev) & 0x05) != 0x05)
71
72/* prototypes */
73static int ata_probe(device_t);
74static int ata_attach(device_t);
75static int ata_detach(device_t);
76static int ata_resume(device_t);
77static void ata_boot_attach(void);
78static void ata_intr(void *);
--- 19 unchanged lines hidden (view full) ---
98 {0x0306d041, "Generic ATA"}, /* PNP0603 */
99 {0}
100};
101
102static int
103ata_isa_probe(device_t dev)
104{
105 struct ata_softc *scp = device_get_softc(dev);
106 struct resource *io;
107 int rid;
108 u_long tmp;
109
110 /* check isapnp ids */
111 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
112 return ENXIO;
113
114 /* allocate the io port range to get the start address */
115 rid = ATA_IOADDR_RID;
116 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
117 ATA_IOSIZE, RF_ACTIVE);
118 if (!io)
119 return ENOMEM;
120
121 /* set the altport range */
122 if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
123 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
124 rman_get_start(io) + ATA_ALTOFFSET, ATA_ALTIOSIZE);
125 }
126
127 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
128 scp->channel = 0;
129 scp->flags |= ATA_USE_16BIT;
130 return ata_probe(dev);
131}
132
133static device_method_t ata_isa_methods[] = {
134 /* device interface */
135 DEVMETHOD(device_probe, ata_isa_probe),
--- 11 unchanged lines hidden (view full) ---
147DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
148#endif
149
150#if NCARD > 0
151static int
152ata_pccard_probe(device_t dev)
153{
154 struct ata_softc *scp = device_get_softc(dev);
155 struct resource *io;
156 int rid, len, start, end;
157 u_long tmp;
158
159 /* allocate the io range to get start and length */
160 rid = ATA_IOADDR_RID;
161 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
162 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
163 ATA_IOSIZE, RF_ACTIVE);
164 if (!io)
165 return ENOMEM;
166
167 /* reallocate the io address to only cover the io ports */
168 start = rman_get_start(io);
169 end = start + ATA_IOSIZE - 1;
170 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
171 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
172 start, end, ATA_IOSIZE, RF_ACTIVE);
173 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
174
175 /*
176 * if we got more than the default ATA_IOSIZE ports, this is likely
177 * a pccard system where the altio ports are located at offset 14
178 * otherwise its the normal altio offset
179 */
180 if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
181 if (len > ATA_IOSIZE) {
182 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
183 start + ATA_PCCARD_ALTOFFSET, ATA_ALTIOSIZE);
184 }
185 else {
186 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
187 start + ATA_ALTOFFSET, ATA_ALTIOSIZE);
188 }
189 }
190 else
191 return ENOMEM;
192
193 scp->channel = 0;
194 scp->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
195 return ata_probe(dev);
196}
197
198static device_method_t ata_pccard_methods[] = {
199 /* device interface */
200 DEVMETHOD(device_probe, ata_pccard_probe),
--- 9 unchanged lines hidden (view full) ---
210};
211
212DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
213#endif
214
215#if NPCI > 0
216struct ata_pci_softc {
217 struct resource *bmio;
218 int bmaddr;
219 struct resource *irq;
220 int irqcnt;
221};
222
223int
224ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid)
225{
226 device_t *children, child;
--- 194 unchanged lines hidden (view full) ---
421 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
422 pci_write_config(dev, 0x53,
423 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
424 break;
425
426 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */
427 case 0x4d30105a:
428 case 0x0d30105a:
429 ATA_OUTB(sc->bmio, 0x11, ATA_INB(sc->bmio, 0x11) | 0x0a);
430 /* FALLTHROUGH */
431
432 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */
433 ATA_OUTB(sc->bmio, 0x1f, ATA_INB(sc->bmio, 0x1f) | 0x01);
434 break;
435
436 case 0x00041103: /* HighPoint */
437 switch (pci_get_revid(dev)) {
438 case 0x00:
439 case 0x01:
440 /* turn off interrupt prediction */
441 pci_write_config(dev, 0x51,
--- 42 unchanged lines hidden (view full) ---
484 pci_write_config(dev, 0x50,
485 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
486 }
487 break;
488
489 case 0x10001042: /* RZ 100? known bad, no DMA */
490 case 0x10011042:
491 case 0x06401095: /* CMD 640 known bad, no DMA */
492 sc->bmio = NULL;
493 device_printf(dev, "Busmastering DMA disabled\n");
494 }
495
496 if (sc->bmio) {
497 sc->bmaddr = rman_get_start(sc->bmio);
498 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
499 SYS_RES_IOPORT, rid, sc->bmio);
500 sc->bmio = NULL;
501 }
502
503 /*
504 * the Cypress chip is a mess, it contains two ATA functions, but
505 * both channels are visible on the first one.
506 * simply ignore the second function for now, as the right
507 * solution (ignoring the second channel on the first function)
508 * doesn't work with the crappy ATA interrupt setup on the alpha.
509 */
510 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
--- 9 unchanged lines hidden (view full) ---
520
521static int
522ata_pci_print_child(device_t dev, device_t child)
523{
524 struct ata_softc *scp = device_get_softc(child);
525 int retval = 0;
526
527 retval += bus_print_child_header(dev, child);
528 retval += printf(": at 0x%lx", rman_get_start(scp->r_io));
529
530 if (ATA_MASTERDEV(dev))
531 retval += printf(" irq %d", 14 + scp->channel);
532
533 retval += bus_print_child_footer(dev, child);
534
535 return retval;
536}
537
538static struct resource *
539ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
540 u_long start, u_long end, u_long count, u_int flags)
541{
542 struct ata_pci_softc *sc = device_get_softc(dev);
543 struct resource *res = NULL;
544 int channel = ((struct ata_softc *)device_get_softc(child))->channel;
545 int myrid;
546
547 if (type == SYS_RES_IOPORT) {
548 switch (*rid) {
549 case ATA_IOADDR_RID:
550 if (ATA_MASTERDEV(dev)) {
551 myrid = 0;
552 start = (channel == 0 ? IO_WD1 : IO_WD2);
553 end = start + ATA_IOSIZE - 1;
554 count = ATA_IOSIZE;
555 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
556 SYS_RES_IOPORT, &myrid,
557 start, end, count, flags);
558 }
559 else {
560 myrid = 0x10 + 8 * channel;
561 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
562 SYS_RES_IOPORT, &myrid,
563 start, end, count, flags);
564 }
565 break;
566
567 case ATA_ALTADDR_RID:
568 if (ATA_MASTERDEV(dev)) {
569 myrid = 0;
570 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET;
571 end = start + ATA_ALTIOSIZE - 1;
572 count = ATA_ALTIOSIZE;
573 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
574 SYS_RES_IOPORT, &myrid,
575 start, end, count, flags);
576 }
577 else {
578 myrid = 0x14 + 8 * channel;
579 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
580 SYS_RES_IOPORT, &myrid,
581 start, end, count, flags);
582 if (res) {
583 start = rman_get_start(res) + 2;
584 end = start + ATA_ALTIOSIZE - 1;
585 count = ATA_ALTIOSIZE;
586 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
587 SYS_RES_IOPORT, myrid, res);
588 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
589 SYS_RES_IOPORT, &myrid,
590 start, end, count, flags);
591 }
592 }
593 break;
594
595 case ATA_BMADDR_RID:
596 if (sc->bmaddr) {
597 myrid = 0x20;
598 start = (channel == 0 ? sc->bmaddr : sc->bmaddr + ATA_BMIOSIZE);
599 end = start + ATA_BMIOSIZE - 1;
600 count = ATA_BMIOSIZE;
601 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
602 SYS_RES_IOPORT, &myrid,
603 start, end, count, flags);
604 }
605 }
606 return res;
607 }
608
609 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
610 if (ATA_MASTERDEV(dev)) {
611#ifdef __alpha__
612 return alpha_platform_alloc_ide_intr(channel);
613#else
614 int irq = (channel == 0 ? 14 : 15);
615
616 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
617 SYS_RES_IRQ, rid,
618 irq, irq, 1, flags & ~RF_SHAREABLE);
619#endif
620 }
621 else {
622 /* primary and secondary channels share the same interrupt */
623 sc->irqcnt++;
624 if (!sc->irq)
625 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
626 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
627 return sc->irq;
628 }
629 }
--- 20 unchanged lines hidden (view full) ---
650 case ATA_ALTADDR_RID:
651 if (ATA_MASTERDEV(dev))
652 myrid = 0;
653 else
654 myrid = 0x14 + 8 * channel;
655 break;
656
657 case ATA_BMADDR_RID:
658 myrid = 0x20;
659 break;
660
661 default:
662 return ENOENT;
663 }
664
665 if (ATA_MASTERDEV(dev))
666 /* make the parent just pass through the allocation. */
667 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
668 SYS_RES_IOPORT, myrid, r);
669 else
670 /* we are using the parent resource directly. */
671 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
672 SYS_RES_IOPORT, myrid, r);
673 }
674 if (type == SYS_RES_IRQ) {
675 if (rid != ATA_IRQ_RID)
676 return ENOENT;
677
678 if (ATA_MASTERDEV(dev)) {
679#ifdef __alpha__
680 return alpha_platform_release_ide_intr(channel, r);
681#else
682 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
683 child, SYS_RES_IRQ, rid, r);
--- 106 unchanged lines hidden (view full) ---
790
791DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
792#endif
793
794static int
795ata_probe(device_t dev)
796{
797 struct ata_softc *scp = device_get_softc(dev);
798 int rid;
799 int mask = 0;
800 u_int8_t status0, status1;
801
802 if (!scp || scp->flags & ATA_ATTACHED)
803 return ENXIO;
804
805 /* initialize the softc basics */
806 scp->active = ATA_IDLE;
807 scp->dev = dev;
808 scp->devices = 0;
809
810 rid = ATA_IOADDR_RID;
811 scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
812 ATA_IOSIZE, RF_ACTIVE);
813 if (!scp->r_io)
814 goto failure;
815
816 rid = ATA_ALTADDR_RID;
817 scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
818 ATA_ALTIOSIZE, RF_ACTIVE);
819 if (!scp->r_altio)
820 goto failure;
821
822 rid = ATA_BMADDR_RID;
823 scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
824 ATA_BMIOSIZE, RF_ACTIVE);
825
826 if (bootverbose)
827 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
828 (int)rman_get_start(scp->r_io),
829 (int)rman_get_start(scp->r_altio),
830 (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0);
831
832 /* do we have any signs of ATA/ATAPI HW being present ? */
833 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
834 DELAY(1);
835 status0 = ATA_INB(scp->r_io, ATA_STATUS);
836 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
837 mask |= 0x01;
838 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
839 DELAY(1);
840 status1 = ATA_INB(scp->r_io, ATA_STATUS);
841 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
842 mask |= 0x02;
843
844 if (bootverbose)
845 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
846 mask, status0, status1);
847 if (!mask)
848 goto failure;
849
850 ata_reset(scp, &mask);
851
852 if (!mask)
853 goto failure;
854
855 TAILQ_INIT(&scp->ata_queue);
856 TAILQ_INIT(&scp->atapi_queue);
857 return 0;
858
859failure:
860 if (scp->r_io)
861 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
862 if (scp->r_altio)
863 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
864 if (scp->r_bmio)
865 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
866 if (bootverbose)
867 ata_printf(scp, -1, "probe allocation failed\n");
868 return ENXIO;
869}
870
871static int
872ata_attach(device_t dev)
873{
874 struct ata_softc *scp = device_get_softc(dev);
875 int error, rid;
876
877 if (!scp || scp->flags & ATA_ATTACHED)
878 return ENXIO;
879
880 rid = ATA_IRQ_RID;
881 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
882 RF_SHAREABLE | RF_ACTIVE);
883 if (!scp->r_irq) {
884 ata_printf(scp, -1, "unable to allocate interrupt\n");
885 return ENXIO;
886 }
887 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
888 scp, &scp->ih)))
--- 37 unchanged lines hidden (view full) ---
926static int
927ata_detach(device_t dev)
928{
929 struct ata_softc *scp = device_get_softc(dev);
930
931 if (!scp || !(scp->flags & ATA_ATTACHED))
932 return ENXIO;
933
934 /* disable interrupts on devices */
935 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
936 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
937 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
938 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
939
940#ifdef DEV_ATADISK
941 if (scp->devices & ATA_ATA_MASTER)
942 ad_detach(scp->dev_softc[0]);
943 if (scp->devices & ATA_ATA_SLAVE)
944 ad_detach(scp->dev_softc[1]);
945#endif
946#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
947 if (scp->devices & ATA_ATAPI_MASTER)
948 atapi_detach(scp->dev_softc[0]);
949 if (scp->devices & ATA_ATAPI_SLAVE)
950 atapi_detach(scp->dev_softc[1]);
951#endif
952
953 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
954 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
955 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
956 }
957 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
958 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
959 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
960 }
961 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
962 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
963 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
964 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
965
966 bus_teardown_intr(dev, scp->r_irq, scp->ih);
967 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq);
968 if (scp->r_bmio)
969 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
970 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio);
971 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
972 scp->flags &= ~ATA_ATTACHED;
973 return 0;
974}
975
976static int
977ata_resume(device_t dev)
978{
--- 6 unchanged lines hidden (view full) ---
985static int
986ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
987{
988 struct ata_params *ata_parm;
989 int8_t buffer[DEV_BSIZE];
990 int retry = 0;
991
992 /* select drive */
993 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
994 DELAY(1);
995
996 /* enable interrupt */
997 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
998 DELAY(1);
999
1000 /* apparently some devices needs this repeated */
1001 do {
1002 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
1003 ata_printf(scp, device, "identify failed\n");
1004 return -1;
1005 }
1006 if (retry++ > 4) {
1007 ata_printf(scp, device, "identify retries exceeded\n");
1008 return -1;
1009 }
1010 } while (ata_wait(scp, device,
1011 ((command == ATA_C_ATAPI_IDENTIFY) ?
1012 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
1013
1014 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer,
1015 sizeof(buffer)/sizeof(int16_t));
1016 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1017 if (!ata_parm) {
1018 ata_printf(scp, device, "malloc for identify data failed\n");
1019 return -1;
1020 }
1021 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1022 if (command == ATA_C_ATA_IDENTIFY ||
1023 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
--- 64 unchanged lines hidden (view full) ---
1088 ata_delayed_attach = NULL;
1089 }
1090}
1091
1092static void
1093ata_intr(void *data)
1094{
1095 struct ata_softc *scp = (struct ata_softc *)data;
1096 u_int8_t dmastat = 0;
1097
1098 /*
1099 * since we might share the IRQ with another device, and in some
1100 * cases with our twin channel, we only want to process interrupts
1101 * that we know this channel generated.
1102 */
1103 switch (scp->chiptype) {
1104#if NPCI > 0
1105 case 0x00041103: /* HighPoint HPT366/368/370 */
1106 if (((dmastat = ata_dmastatus(scp)) &
1107 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
1108 return;
1109 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1110 break;
1111
1112 case 0x06481095: /* CMD 648 */
1113 case 0x06491095: /* CMD 649 */
1114 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) &
1115 (scp->channel ? 0x08 : 0x04)))
1116 return;
1117 goto out;
1118
1119 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */
1120 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */
1121 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */
1122 case 0x0d30105a: /* Promise OEM ATA100 */
1123 if (!(ATA_INL(scp->r_bmio, (scp->channel ? 0x14 : 0x1c)) &
1124 (scp->channel ? 0x00004000 : 0x00000400)))
1125 return;
1126 /* FALLTHROUGH */
1127out:
1128#endif
1129 default:
1130 if (scp->flags & ATA_DMA_ACTIVE) {
1131 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1132 return;
1133 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
1134 dmastat | ATA_BMSTAT_INTERRUPT);
1135 }
1136 }
1137 DELAY(1);
1138
1139 /* if drive is busy it didn't interrupt */
1140 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY)
1141 return;
1142
1143 /* clear interrupt and get status */
1144 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1145
1146 if (scp->status & ATA_S_ERROR)
1147 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1148
1149 /* find & call the responsible driver to process this interrupt */
1150 switch (scp->active) {
1151#ifdef DEV_ATADISK
1152 case ATA_ACTIVE_ATA:
1153 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1154 return;
1155 break;
--- 91 unchanged lines hidden (view full) ---
1247void
1248ata_reset(struct ata_softc *scp, int *mask)
1249{
1250 int timeout;
1251 u_int8_t a, b, ostat0, ostat1;
1252 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY;
1253
1254 /* get the current status of the devices */
1255 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1256 DELAY(10);
1257 ostat1 = ATA_INB(scp->r_io, ATA_STATUS);
1258 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1259 DELAY(10);
1260 ostat0 = ATA_INB(scp->r_io, ATA_STATUS);
1261
1262 /* in some setups we dont want to test for a slave */
1263 if (scp->flags & ATA_NO_SLAVE)
1264 *mask &= ~0x02;
1265
1266 if (bootverbose)
1267 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
1268 *mask, ostat0, ostat1);
1269
1270 /* reset channel */
1271 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
1272 DELAY(10000);
1273 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS);
1274 DELAY(100000);
1275 ATA_INB(scp->r_io, ATA_ERROR);
1276 scp->devices = 0;
1277
1278 /* wait for BUSY to go inactive */
1279 for (timeout = 0; timeout < 310000; timeout++) {
1280 if (status0 & ATA_S_BUSY) {
1281 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1282 DELAY(10);
1283 status0 = ATA_INB(scp->r_io, ATA_STATUS);
1284 if (!(status0 & ATA_S_BUSY)) {
1285 /* check for ATAPI signature while its still there */
1286 a = ATA_INB(scp->r_io, ATA_CYL_LSB);
1287 b = ATA_INB(scp->r_io, ATA_CYL_MSB);
1288 if (bootverbose)
1289 ata_printf(scp, ATA_MASTER,
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_MASTER;
1293 }
1294 }
1295 if (status1 & ATA_S_BUSY) {
1296 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1297 DELAY(10);
1298 status1 = ATA_INB(scp->r_io, ATA_STATUS);
1299 if (!(status1 & ATA_S_BUSY)) {
1300 /* check for ATAPI signature while its still there */
1301 a = ATA_INB(scp->r_io, ATA_CYL_LSB);
1302 b = ATA_INB(scp->r_io, ATA_CYL_MSB);
1303 if (bootverbose)
1304 ata_printf(scp, ATA_SLAVE,
1305 "ATAPI probe a=%02x b=%02x\n", a, b);
1306 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1307 scp->devices |= ATA_ATAPI_SLAVE;
1308 }
1309 }
1310 if (*mask == 0x01) /* wait for master only */
1311 if (!(status0 & ATA_S_BUSY))
1312 break;
1313 if (*mask == 0x02) /* wait for slave only */
1314 if (!(status1 & ATA_S_BUSY))
1315 break;
1316 if (*mask == 0x03) /* wait for both master & slave */
1317 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1318 break;
1319 DELAY(100);
1320 }
1321 DELAY(10);
1322 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1323
1324 if (status0 & ATA_S_BUSY)
1325 *mask &= ~0x01;
1326 if (status1 & ATA_S_BUSY)
1327 *mask &= ~0x02;
1328 if (bootverbose)
1329 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1330 *mask, status0, status1);
1331 if (!*mask)
1332 return;
1333
1334 if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
1335 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1336 DELAY(10);
1337 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
1338 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
1339 a = ATA_INB(scp->r_io, ATA_ERROR);
1340 b = ATA_INB(scp->r_io, ATA_CYL_LSB);
1341 if (bootverbose)
1342 ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b);
1343 if (a != 0x58 && b == 0xa5)
1344 scp->devices |= ATA_ATA_MASTER;
1345 }
1346 if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
1347 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1348 DELAY(10);
1349 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
1350 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
1351 a = ATA_INB(scp->r_io, ATA_ERROR);
1352 b = ATA_INB(scp->r_io, ATA_CYL_LSB);
1353 if (bootverbose)
1354 ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b);
1355 if (a != 0x58 && b == 0xa5)
1356 scp->devices |= ATA_ATA_SLAVE;
1357 }
1358 if (bootverbose)
1359 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
1360}
--- 35 unchanged lines hidden (view full) ---
1396 return 0;
1397}
1398
1399static int
1400ata_service(struct ata_softc *scp)
1401{
1402 /* do we have a SERVICE request from the drive ? */
1403 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
1404 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
1405 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
1406#ifdef DEV_ATADISK
1407 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
1408 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0])
1409 return ad_service((struct ad_softc *)scp->dev_softc[0], 0);
1410 }
1411 else {
1412 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1])
1413 return ad_service((struct ad_softc *)scp->dev_softc[1], 0);
1414 }
1415#endif
1416 }
1417 return ATA_OP_FINISHED;
1418}
1419
1420int
1421ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
1422{
1423 int timeout = 0;
1424
1425 DELAY(1);
1426 while (timeout < 5000000) { /* timeout 5 secs */
1427 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1428
1429 /* if drive fails status, reselect the drive just to be sure */
1430 if (scp->status == 0xff) {
1431 ata_printf(scp, device, "no status, reselecting device\n");
1432 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
1433 DELAY(1);
1434 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1435 }
1436
1437 /* are we done ? */
1438 if (!(scp->status & ATA_S_BUSY))
1439 break;
1440
1441 if (timeout > 1000) {
1442 timeout += 1000;
1443 DELAY(1000);
1444 }
1445 else {
1446 timeout += 10;
1447 DELAY(10);
1448 }
1449 }
1450 if (scp->status & ATA_S_ERROR)
1451 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1452 if (timeout >= 5000000)
1453 return -1;
1454 if (!mask)
1455 return (scp->status & ATA_S_ERROR);
1456
1457 /* Wait 50 msec for bits wanted. */
1458 timeout = 5000;
1459 while (timeout--) {
1460 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1461 if ((scp->status & mask) == mask) {
1462 if (scp->status & ATA_S_ERROR)
1463 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1464 return (scp->status & ATA_S_ERROR);
1465 }
1466 DELAY (10);
1467 }
1468 return -1;
1469}
1470
1471int
1472ata_command(struct ata_softc *scp, int device, u_int8_t command,
1473 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
1474 u_int8_t count, u_int8_t feature, int flags)
1475{
1476 int error = 0;
1477#ifdef ATA_DEBUG
1478 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1479 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
1480 rman_get_start(scp->r_io), command, cylinder, head, sector,
1481 count, feature, flags);
1482#endif
1483
1484 /* disable interrupt from device */
1485 if (scp->flags & ATA_QUEUED)
1486 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1487
1488 /* select device */
1489 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
1490
1491 /* ready to issue command ? */
1492 if (ata_wait(scp, device, 0) < 0) {
1493 ata_printf(scp, device,
1494 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1495 command, scp->status, scp->error);
1496 return -1;
1497 }
1498
1499 ATA_OUTB(scp->r_io, ATA_FEATURE, feature);
1500 ATA_OUTB(scp->r_io, ATA_COUNT, count);
1501 ATA_OUTB(scp->r_io, ATA_SECTOR, sector);
1502 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8);
1503 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder);
1504 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head);
1505
1506 switch (flags) {
1507 case ATA_WAIT_INTR:
1508 scp->active = ATA_WAIT_INTR;
1509 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1510 ATA_OUTB(scp->r_io, ATA_CMD, command);
1511
1512 /* enable interrupt */
1513 if (scp->flags & ATA_QUEUED)
1514 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1515
1516 if (await(PRIBIO, 10 * hz)) {
1517 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1518 scp->active = ATA_IDLE;
1519 error = -1;
1520 }
1521 break;
1522
1523 case ATA_WAIT_READY:
1524 if (scp->active != ATA_REINITING)
1525 scp->active = ATA_WAIT_READY;
1526 ATA_OUTB(scp->r_io, ATA_CMD, command);
1527 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1528 ata_printf(scp, device,
1529 "timeout waiting for command=%02x s=%02x e=%02x\n",
1530 command, scp->status, scp->error);
1531 error = -1;
1532 }
1533 if (scp->active != ATA_REINITING)
1534 scp->active = ATA_IDLE;
1535 break;
1536
1537 case ATA_IMMEDIATE:
1538 ATA_OUTB(scp->r_io, ATA_CMD, command);
1539 break;
1540
1541 default:
1542 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1543 active2str(flags));
1544 }
1545 /* enable interrupt */
1546 if (scp->flags & ATA_QUEUED)
1547 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1548 return error;
1549}
1550
1551int
1552ata_get_lun(u_int32_t *map)
1553{
1554 int lun = ffs(~*map) - 1;
1555
--- 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 72106 2001-02-06 16:44:25Z 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#define ATA_IRQ_RID 0
69#define ATA_MASTERDEV(dev) ((pci_get_progif(dev) & 0x80) && \
70 (pci_get_progif(dev) & 0x05) != 0x05)
71
72/* prototypes */
73static int ata_probe(device_t);
74static int ata_attach(device_t);
75static int ata_detach(device_t);
76static int ata_resume(device_t);
77static void ata_boot_attach(void);
78static void ata_intr(void *);
--- 19 unchanged lines hidden (view full) ---
98 {0x0306d041, "Generic ATA"}, /* PNP0603 */
99 {0}
100};
101
102static int
103ata_isa_probe(device_t dev)
104{
105 struct ata_softc *scp = device_get_softc(dev);
106 struct resource *io;
107 int rid;
108 u_long tmp;
109
110 /* check isapnp ids */
111 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
112 return ENXIO;
113
114 /* allocate the io port range to get the start address */
115 rid = ATA_IOADDR_RID;
116 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
117 ATA_IOSIZE, RF_ACTIVE);
118 if (!io)
119 return ENOMEM;
120
121 /* set the altport range */
122 if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
123 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
124 rman_get_start(io) + ATA_ALTOFFSET, ATA_ALTIOSIZE);
125 }
126
127 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
128 scp->channel = 0;
129 scp->flags |= ATA_USE_16BIT;
130 return ata_probe(dev);
131}
132
133static device_method_t ata_isa_methods[] = {
134 /* device interface */
135 DEVMETHOD(device_probe, ata_isa_probe),
--- 11 unchanged lines hidden (view full) ---
147DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
148#endif
149
150#if NCARD > 0
151static int
152ata_pccard_probe(device_t dev)
153{
154 struct ata_softc *scp = device_get_softc(dev);
155 struct resource *io;
156 int rid, len, start, end;
157 u_long tmp;
158
159 /* allocate the io range to get start and length */
160 rid = ATA_IOADDR_RID;
161 len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
162 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
163 ATA_IOSIZE, RF_ACTIVE);
164 if (!io)
165 return ENOMEM;
166
167 /* reallocate the io address to only cover the io ports */
168 start = rman_get_start(io);
169 end = start + ATA_IOSIZE - 1;
170 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
171 io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
172 start, end, ATA_IOSIZE, RF_ACTIVE);
173 bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
174
175 /*
176 * if we got more than the default ATA_IOSIZE ports, this is likely
177 * a pccard system where the altio ports are located at offset 14
178 * otherwise its the normal altio offset
179 */
180 if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
181 if (len > ATA_IOSIZE) {
182 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
183 start + ATA_PCCARD_ALTOFFSET, ATA_ALTIOSIZE);
184 }
185 else {
186 bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
187 start + ATA_ALTOFFSET, ATA_ALTIOSIZE);
188 }
189 }
190 else
191 return ENOMEM;
192
193 scp->channel = 0;
194 scp->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
195 return ata_probe(dev);
196}
197
198static device_method_t ata_pccard_methods[] = {
199 /* device interface */
200 DEVMETHOD(device_probe, ata_pccard_probe),
--- 9 unchanged lines hidden (view full) ---
210};
211
212DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
213#endif
214
215#if NPCI > 0
216struct ata_pci_softc {
217 struct resource *bmio;
218 int bmaddr;
219 struct resource *irq;
220 int irqcnt;
221};
222
223int
224ata_find_dev(device_t dev, u_int32_t type, u_int32_t revid)
225{
226 device_t *children, child;
--- 194 unchanged lines hidden (view full) ---
421 case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
422 pci_write_config(dev, 0x53,
423 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
424 break;
425
426 case 0x4d38105a: /* Promise 66 & 100 need their clock changed */
427 case 0x4d30105a:
428 case 0x0d30105a:
429 ATA_OUTB(sc->bmio, 0x11, ATA_INB(sc->bmio, 0x11) | 0x0a);
430 /* FALLTHROUGH */
431
432 case 0x4d33105a: /* Promise (all) need burst mode to be turned on */
433 ATA_OUTB(sc->bmio, 0x1f, ATA_INB(sc->bmio, 0x1f) | 0x01);
434 break;
435
436 case 0x00041103: /* HighPoint */
437 switch (pci_get_revid(dev)) {
438 case 0x00:
439 case 0x01:
440 /* turn off interrupt prediction */
441 pci_write_config(dev, 0x51,
--- 42 unchanged lines hidden (view full) ---
484 pci_write_config(dev, 0x50,
485 pci_read_config(dev, 0x50, 4) | 0x070f070f, 4);
486 }
487 break;
488
489 case 0x10001042: /* RZ 100? known bad, no DMA */
490 case 0x10011042:
491 case 0x06401095: /* CMD 640 known bad, no DMA */
492 sc->bmio = NULL;
493 device_printf(dev, "Busmastering DMA disabled\n");
494 }
495
496 if (sc->bmio) {
497 sc->bmaddr = rman_get_start(sc->bmio);
498 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
499 SYS_RES_IOPORT, rid, sc->bmio);
500 sc->bmio = NULL;
501 }
502
503 /*
504 * the Cypress chip is a mess, it contains two ATA functions, but
505 * both channels are visible on the first one.
506 * simply ignore the second function for now, as the right
507 * solution (ignoring the second channel on the first function)
508 * doesn't work with the crappy ATA interrupt setup on the alpha.
509 */
510 if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
--- 9 unchanged lines hidden (view full) ---
520
521static int
522ata_pci_print_child(device_t dev, device_t child)
523{
524 struct ata_softc *scp = device_get_softc(child);
525 int retval = 0;
526
527 retval += bus_print_child_header(dev, child);
528 retval += printf(": at 0x%lx", rman_get_start(scp->r_io));
529
530 if (ATA_MASTERDEV(dev))
531 retval += printf(" irq %d", 14 + scp->channel);
532
533 retval += bus_print_child_footer(dev, child);
534
535 return retval;
536}
537
538static struct resource *
539ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
540 u_long start, u_long end, u_long count, u_int flags)
541{
542 struct ata_pci_softc *sc = device_get_softc(dev);
543 struct resource *res = NULL;
544 int channel = ((struct ata_softc *)device_get_softc(child))->channel;
545 int myrid;
546
547 if (type == SYS_RES_IOPORT) {
548 switch (*rid) {
549 case ATA_IOADDR_RID:
550 if (ATA_MASTERDEV(dev)) {
551 myrid = 0;
552 start = (channel == 0 ? IO_WD1 : IO_WD2);
553 end = start + ATA_IOSIZE - 1;
554 count = ATA_IOSIZE;
555 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
556 SYS_RES_IOPORT, &myrid,
557 start, end, count, flags);
558 }
559 else {
560 myrid = 0x10 + 8 * channel;
561 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
562 SYS_RES_IOPORT, &myrid,
563 start, end, count, flags);
564 }
565 break;
566
567 case ATA_ALTADDR_RID:
568 if (ATA_MASTERDEV(dev)) {
569 myrid = 0;
570 start = (channel == 0 ? IO_WD1 : IO_WD2) + ATA_ALTOFFSET;
571 end = start + ATA_ALTIOSIZE - 1;
572 count = ATA_ALTIOSIZE;
573 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
574 SYS_RES_IOPORT, &myrid,
575 start, end, count, flags);
576 }
577 else {
578 myrid = 0x14 + 8 * channel;
579 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
580 SYS_RES_IOPORT, &myrid,
581 start, end, count, flags);
582 if (res) {
583 start = rman_get_start(res) + 2;
584 end = start + ATA_ALTIOSIZE - 1;
585 count = ATA_ALTIOSIZE;
586 BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
587 SYS_RES_IOPORT, myrid, res);
588 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
589 SYS_RES_IOPORT, &myrid,
590 start, end, count, flags);
591 }
592 }
593 break;
594
595 case ATA_BMADDR_RID:
596 if (sc->bmaddr) {
597 myrid = 0x20;
598 start = (channel == 0 ? sc->bmaddr : sc->bmaddr + ATA_BMIOSIZE);
599 end = start + ATA_BMIOSIZE - 1;
600 count = ATA_BMIOSIZE;
601 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
602 SYS_RES_IOPORT, &myrid,
603 start, end, count, flags);
604 }
605 }
606 return res;
607 }
608
609 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
610 if (ATA_MASTERDEV(dev)) {
611#ifdef __alpha__
612 return alpha_platform_alloc_ide_intr(channel);
613#else
614 int irq = (channel == 0 ? 14 : 15);
615
616 return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
617 SYS_RES_IRQ, rid,
618 irq, irq, 1, flags & ~RF_SHAREABLE);
619#endif
620 }
621 else {
622 /* primary and secondary channels share the same interrupt */
623 sc->irqcnt++;
624 if (!sc->irq)
625 sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
626 SYS_RES_IRQ, rid, 0, ~0, 1, flags);
627 return sc->irq;
628 }
629 }
--- 20 unchanged lines hidden (view full) ---
650 case ATA_ALTADDR_RID:
651 if (ATA_MASTERDEV(dev))
652 myrid = 0;
653 else
654 myrid = 0x14 + 8 * channel;
655 break;
656
657 case ATA_BMADDR_RID:
658 myrid = 0x20;
659 break;
660
661 default:
662 return ENOENT;
663 }
664
665 if (ATA_MASTERDEV(dev))
666 /* make the parent just pass through the allocation. */
667 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
668 SYS_RES_IOPORT, myrid, r);
669 else
670 /* we are using the parent resource directly. */
671 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
672 SYS_RES_IOPORT, myrid, r);
673 }
674 if (type == SYS_RES_IRQ) {
675 if (rid != ATA_IRQ_RID)
676 return ENOENT;
677
678 if (ATA_MASTERDEV(dev)) {
679#ifdef __alpha__
680 return alpha_platform_release_ide_intr(channel, r);
681#else
682 return BUS_RELEASE_RESOURCE(device_get_parent(dev),
683 child, SYS_RES_IRQ, rid, r);
--- 106 unchanged lines hidden (view full) ---
790
791DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
792#endif
793
794static int
795ata_probe(device_t dev)
796{
797 struct ata_softc *scp = device_get_softc(dev);
798 int rid;
799 int mask = 0;
800 u_int8_t status0, status1;
801
802 if (!scp || scp->flags & ATA_ATTACHED)
803 return ENXIO;
804
805 /* initialize the softc basics */
806 scp->active = ATA_IDLE;
807 scp->dev = dev;
808 scp->devices = 0;
809
810 rid = ATA_IOADDR_RID;
811 scp->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
812 ATA_IOSIZE, RF_ACTIVE);
813 if (!scp->r_io)
814 goto failure;
815
816 rid = ATA_ALTADDR_RID;
817 scp->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
818 ATA_ALTIOSIZE, RF_ACTIVE);
819 if (!scp->r_altio)
820 goto failure;
821
822 rid = ATA_BMADDR_RID;
823 scp->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
824 ATA_BMIOSIZE, RF_ACTIVE);
825
826 if (bootverbose)
827 ata_printf(scp, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
828 (int)rman_get_start(scp->r_io),
829 (int)rman_get_start(scp->r_altio),
830 (scp->r_bmio) ? (int)rman_get_start(scp->r_bmio) : 0);
831
832 /* do we have any signs of ATA/ATAPI HW being present ? */
833 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
834 DELAY(1);
835 status0 = ATA_INB(scp->r_io, ATA_STATUS);
836 if ((status0 & 0xf8) != 0xf8 && status0 != 0xa5)
837 mask |= 0x01;
838 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
839 DELAY(1);
840 status1 = ATA_INB(scp->r_io, ATA_STATUS);
841 if ((status1 & 0xf8) != 0xf8 && status1 != 0xa5)
842 mask |= 0x02;
843
844 if (bootverbose)
845 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
846 mask, status0, status1);
847 if (!mask)
848 goto failure;
849
850 ata_reset(scp, &mask);
851
852 if (!mask)
853 goto failure;
854
855 TAILQ_INIT(&scp->ata_queue);
856 TAILQ_INIT(&scp->atapi_queue);
857 return 0;
858
859failure:
860 if (scp->r_io)
861 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
862 if (scp->r_altio)
863 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,scp->r_altio);
864 if (scp->r_bmio)
865 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
866 if (bootverbose)
867 ata_printf(scp, -1, "probe allocation failed\n");
868 return ENXIO;
869}
870
871static int
872ata_attach(device_t dev)
873{
874 struct ata_softc *scp = device_get_softc(dev);
875 int error, rid;
876
877 if (!scp || scp->flags & ATA_ATTACHED)
878 return ENXIO;
879
880 rid = ATA_IRQ_RID;
881 scp->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
882 RF_SHAREABLE | RF_ACTIVE);
883 if (!scp->r_irq) {
884 ata_printf(scp, -1, "unable to allocate interrupt\n");
885 return ENXIO;
886 }
887 if ((error = bus_setup_intr(dev, scp->r_irq, INTR_TYPE_BIO, ata_intr,
888 scp, &scp->ih)))
--- 37 unchanged lines hidden (view full) ---
926static int
927ata_detach(device_t dev)
928{
929 struct ata_softc *scp = device_get_softc(dev);
930
931 if (!scp || !(scp->flags & ATA_ATTACHED))
932 return ENXIO;
933
934 /* disable interrupts on devices */
935 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
936 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
937 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
938 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
939
940#ifdef DEV_ATADISK
941 if (scp->devices & ATA_ATA_MASTER)
942 ad_detach(scp->dev_softc[0]);
943 if (scp->devices & ATA_ATA_SLAVE)
944 ad_detach(scp->dev_softc[1]);
945#endif
946#if defined(DEV_ATAPICD) || defined(DEV_ATAPIFD) || defined(DEV_ATAPIST)
947 if (scp->devices & ATA_ATAPI_MASTER)
948 atapi_detach(scp->dev_softc[0]);
949 if (scp->devices & ATA_ATAPI_SLAVE)
950 atapi_detach(scp->dev_softc[1]);
951#endif
952
953 if (scp->dev_param[ATA_DEV(ATA_MASTER)]) {
954 free(scp->dev_param[ATA_DEV(ATA_MASTER)], M_ATA);
955 scp->dev_param[ATA_DEV(ATA_MASTER)] = NULL;
956 }
957 if (scp->dev_param[ATA_DEV(ATA_SLAVE)]) {
958 free(scp->dev_param[ATA_DEV(ATA_SLAVE)], M_ATA);
959 scp->dev_param[ATA_DEV(ATA_SLAVE)] = NULL;
960 }
961 scp->dev_softc[ATA_DEV(ATA_MASTER)] = NULL;
962 scp->dev_softc[ATA_DEV(ATA_SLAVE)] = NULL;
963 scp->mode[ATA_DEV(ATA_MASTER)] = ATA_PIO;
964 scp->mode[ATA_DEV(ATA_SLAVE)] = ATA_PIO;
965
966 bus_teardown_intr(dev, scp->r_irq, scp->ih);
967 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, scp->r_irq);
968 if (scp->r_bmio)
969 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, scp->r_bmio);
970 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, scp->r_altio);
971 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, scp->r_io);
972 scp->flags &= ~ATA_ATTACHED;
973 return 0;
974}
975
976static int
977ata_resume(device_t dev)
978{
--- 6 unchanged lines hidden (view full) ---
985static int
986ata_getparam(struct ata_softc *scp, int device, u_int8_t command)
987{
988 struct ata_params *ata_parm;
989 int8_t buffer[DEV_BSIZE];
990 int retry = 0;
991
992 /* select drive */
993 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
994 DELAY(1);
995
996 /* enable interrupt */
997 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
998 DELAY(1);
999
1000 /* apparently some devices needs this repeated */
1001 do {
1002 if (ata_command(scp, device, command, 0, 0, 0, 0, 0, ATA_WAIT_INTR)) {
1003 ata_printf(scp, device, "identify failed\n");
1004 return -1;
1005 }
1006 if (retry++ > 4) {
1007 ata_printf(scp, device, "identify retries exceeded\n");
1008 return -1;
1009 }
1010 } while (ata_wait(scp, device,
1011 ((command == ATA_C_ATAPI_IDENTIFY) ?
1012 ATA_S_DRQ : (ATA_S_READY | ATA_S_DSC | ATA_S_DRQ))));
1013
1014 ATA_INSW(scp->r_io, ATA_DATA, (int16_t *)buffer,
1015 sizeof(buffer)/sizeof(int16_t));
1016 ata_parm = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
1017 if (!ata_parm) {
1018 ata_printf(scp, device, "malloc for identify data failed\n");
1019 return -1;
1020 }
1021 bcopy(buffer, ata_parm, sizeof(struct ata_params));
1022 if (command == ATA_C_ATA_IDENTIFY ||
1023 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
--- 64 unchanged lines hidden (view full) ---
1088 ata_delayed_attach = NULL;
1089 }
1090}
1091
1092static void
1093ata_intr(void *data)
1094{
1095 struct ata_softc *scp = (struct ata_softc *)data;
1096 u_int8_t dmastat = 0;
1097
1098 /*
1099 * since we might share the IRQ with another device, and in some
1100 * cases with our twin channel, we only want to process interrupts
1101 * that we know this channel generated.
1102 */
1103 switch (scp->chiptype) {
1104#if NPCI > 0
1105 case 0x00041103: /* HighPoint HPT366/368/370 */
1106 if (((dmastat = ata_dmastatus(scp)) &
1107 (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
1108 return;
1109 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
1110 break;
1111
1112 case 0x06481095: /* CMD 648 */
1113 case 0x06491095: /* CMD 649 */
1114 if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) &
1115 (scp->channel ? 0x08 : 0x04)))
1116 return;
1117 goto out;
1118
1119 case 0x4d33105a: /* Promise Ultra/Fasttrak 33 */
1120 case 0x4d38105a: /* Promise Ultra/Fasttrak 66 */
1121 case 0x4d30105a: /* Promise Ultra/Fasttrak 100 */
1122 case 0x0d30105a: /* Promise OEM ATA100 */
1123 if (!(ATA_INL(scp->r_bmio, (scp->channel ? 0x14 : 0x1c)) &
1124 (scp->channel ? 0x00004000 : 0x00000400)))
1125 return;
1126 /* FALLTHROUGH */
1127out:
1128#endif
1129 default:
1130 if (scp->flags & ATA_DMA_ACTIVE) {
1131 if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
1132 return;
1133 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
1134 dmastat | ATA_BMSTAT_INTERRUPT);
1135 }
1136 }
1137 DELAY(1);
1138
1139 /* if drive is busy it didn't interrupt */
1140 if (ATA_INB(scp->r_altio, ATA_ALTSTAT) & ATA_S_BUSY)
1141 return;
1142
1143 /* clear interrupt and get status */
1144 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1145
1146 if (scp->status & ATA_S_ERROR)
1147 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1148
1149 /* find & call the responsible driver to process this interrupt */
1150 switch (scp->active) {
1151#ifdef DEV_ATADISK
1152 case ATA_ACTIVE_ATA:
1153 if (!scp->running || ad_interrupt(scp->running) == ATA_OP_CONTINUES)
1154 return;
1155 break;
--- 91 unchanged lines hidden (view full) ---
1247void
1248ata_reset(struct ata_softc *scp, int *mask)
1249{
1250 int timeout;
1251 u_int8_t a, b, ostat0, ostat1;
1252 u_int8_t status0 = ATA_S_BUSY, status1 = ATA_S_BUSY;
1253
1254 /* get the current status of the devices */
1255 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1256 DELAY(10);
1257 ostat1 = ATA_INB(scp->r_io, ATA_STATUS);
1258 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1259 DELAY(10);
1260 ostat0 = ATA_INB(scp->r_io, ATA_STATUS);
1261
1262 /* in some setups we dont want to test for a slave */
1263 if (scp->flags & ATA_NO_SLAVE)
1264 *mask &= ~0x02;
1265
1266 if (bootverbose)
1267 ata_printf(scp, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
1268 *mask, ostat0, ostat1);
1269
1270 /* reset channel */
1271 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
1272 DELAY(10000);
1273 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS);
1274 DELAY(100000);
1275 ATA_INB(scp->r_io, ATA_ERROR);
1276 scp->devices = 0;
1277
1278 /* wait for BUSY to go inactive */
1279 for (timeout = 0; timeout < 310000; timeout++) {
1280 if (status0 & ATA_S_BUSY) {
1281 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1282 DELAY(10);
1283 status0 = ATA_INB(scp->r_io, ATA_STATUS);
1284 if (!(status0 & ATA_S_BUSY)) {
1285 /* check for ATAPI signature while its still there */
1286 a = ATA_INB(scp->r_io, ATA_CYL_LSB);
1287 b = ATA_INB(scp->r_io, ATA_CYL_MSB);
1288 if (bootverbose)
1289 ata_printf(scp, ATA_MASTER,
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_MASTER;
1293 }
1294 }
1295 if (status1 & ATA_S_BUSY) {
1296 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1297 DELAY(10);
1298 status1 = ATA_INB(scp->r_io, ATA_STATUS);
1299 if (!(status1 & ATA_S_BUSY)) {
1300 /* check for ATAPI signature while its still there */
1301 a = ATA_INB(scp->r_io, ATA_CYL_LSB);
1302 b = ATA_INB(scp->r_io, ATA_CYL_MSB);
1303 if (bootverbose)
1304 ata_printf(scp, ATA_SLAVE,
1305 "ATAPI probe a=%02x b=%02x\n", a, b);
1306 if (a == ATAPI_MAGIC_LSB && b == ATAPI_MAGIC_MSB)
1307 scp->devices |= ATA_ATAPI_SLAVE;
1308 }
1309 }
1310 if (*mask == 0x01) /* wait for master only */
1311 if (!(status0 & ATA_S_BUSY))
1312 break;
1313 if (*mask == 0x02) /* wait for slave only */
1314 if (!(status1 & ATA_S_BUSY))
1315 break;
1316 if (*mask == 0x03) /* wait for both master & slave */
1317 if (!(status0 & ATA_S_BUSY) && !(status1 & ATA_S_BUSY))
1318 break;
1319 DELAY(100);
1320 }
1321 DELAY(10);
1322 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1323
1324 if (status0 & ATA_S_BUSY)
1325 *mask &= ~0x01;
1326 if (status1 & ATA_S_BUSY)
1327 *mask &= ~0x02;
1328 if (bootverbose)
1329 ata_printf(scp, -1, "mask=%02x status0=%02x status1=%02x\n",
1330 *mask, status0, status1);
1331 if (!*mask)
1332 return;
1333
1334 if (*mask & 0x01 && ostat0 != 0x00 && !(scp->devices & ATA_ATAPI_MASTER)) {
1335 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
1336 DELAY(10);
1337 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
1338 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
1339 a = ATA_INB(scp->r_io, ATA_ERROR);
1340 b = ATA_INB(scp->r_io, ATA_CYL_LSB);
1341 if (bootverbose)
1342 ata_printf(scp, ATA_MASTER, "ATA probe a=%02x b=%02x\n", a, b);
1343 if (a != 0x58 && b == 0xa5)
1344 scp->devices |= ATA_ATA_MASTER;
1345 }
1346 if (*mask & 0x02 && ostat1 != 0x00 && !(scp->devices & ATA_ATAPI_SLAVE)) {
1347 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
1348 DELAY(10);
1349 ATA_OUTB(scp->r_io, ATA_ERROR, 0x58);
1350 ATA_OUTB(scp->r_io, ATA_CYL_LSB, 0xa5);
1351 a = ATA_INB(scp->r_io, ATA_ERROR);
1352 b = ATA_INB(scp->r_io, ATA_CYL_LSB);
1353 if (bootverbose)
1354 ata_printf(scp, ATA_SLAVE, "ATA probe a=%02x b=%02x\n", a, b);
1355 if (a != 0x58 && b == 0xa5)
1356 scp->devices |= ATA_ATA_SLAVE;
1357 }
1358 if (bootverbose)
1359 ata_printf(scp, -1, "devices=%02x\n", scp->devices);
1360}
--- 35 unchanged lines hidden (view full) ---
1396 return 0;
1397}
1398
1399static int
1400ata_service(struct ata_softc *scp)
1401{
1402 /* do we have a SERVICE request from the drive ? */
1403 if ((scp->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE){
1404 ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT,
1405 ata_dmastatus(scp) | ATA_BMSTAT_INTERRUPT);
1406#ifdef DEV_ATADISK
1407 if ((ATA_INB(scp->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
1408 if ((scp->devices & ATA_ATA_MASTER) && scp->dev_softc[0])
1409 return ad_service((struct ad_softc *)scp->dev_softc[0], 0);
1410 }
1411 else {
1412 if ((scp->devices & ATA_ATA_SLAVE) && scp->dev_softc[1])
1413 return ad_service((struct ad_softc *)scp->dev_softc[1], 0);
1414 }
1415#endif
1416 }
1417 return ATA_OP_FINISHED;
1418}
1419
1420int
1421ata_wait(struct ata_softc *scp, int device, u_int8_t mask)
1422{
1423 int timeout = 0;
1424
1425 DELAY(1);
1426 while (timeout < 5000000) { /* timeout 5 secs */
1427 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1428
1429 /* if drive fails status, reselect the drive just to be sure */
1430 if (scp->status == 0xff) {
1431 ata_printf(scp, device, "no status, reselecting device\n");
1432 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
1433 DELAY(1);
1434 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1435 }
1436
1437 /* are we done ? */
1438 if (!(scp->status & ATA_S_BUSY))
1439 break;
1440
1441 if (timeout > 1000) {
1442 timeout += 1000;
1443 DELAY(1000);
1444 }
1445 else {
1446 timeout += 10;
1447 DELAY(10);
1448 }
1449 }
1450 if (scp->status & ATA_S_ERROR)
1451 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1452 if (timeout >= 5000000)
1453 return -1;
1454 if (!mask)
1455 return (scp->status & ATA_S_ERROR);
1456
1457 /* Wait 50 msec for bits wanted. */
1458 timeout = 5000;
1459 while (timeout--) {
1460 scp->status = ATA_INB(scp->r_io, ATA_STATUS);
1461 if ((scp->status & mask) == mask) {
1462 if (scp->status & ATA_S_ERROR)
1463 scp->error = ATA_INB(scp->r_io, ATA_ERROR);
1464 return (scp->status & ATA_S_ERROR);
1465 }
1466 DELAY (10);
1467 }
1468 return -1;
1469}
1470
1471int
1472ata_command(struct ata_softc *scp, int device, u_int8_t command,
1473 u_int16_t cylinder, u_int8_t head, u_int8_t sector,
1474 u_int8_t count, u_int8_t feature, int flags)
1475{
1476 int error = 0;
1477#ifdef ATA_DEBUG
1478 ata_printf(scp, device, "ata_command: addr=%04x, cmd=%02x, "
1479 "c=%d, h=%d, s=%d, count=%d, feature=%d, flags=%02x\n",
1480 rman_get_start(scp->r_io), command, cylinder, head, sector,
1481 count, feature, flags);
1482#endif
1483
1484 /* disable interrupt from device */
1485 if (scp->flags & ATA_QUEUED)
1486 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1487
1488 /* select device */
1489 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device);
1490
1491 /* ready to issue command ? */
1492 if (ata_wait(scp, device, 0) < 0) {
1493 ata_printf(scp, device,
1494 "timeout waiting to give command=%02x s=%02x e=%02x\n",
1495 command, scp->status, scp->error);
1496 return -1;
1497 }
1498
1499 ATA_OUTB(scp->r_io, ATA_FEATURE, feature);
1500 ATA_OUTB(scp->r_io, ATA_COUNT, count);
1501 ATA_OUTB(scp->r_io, ATA_SECTOR, sector);
1502 ATA_OUTB(scp->r_io, ATA_CYL_MSB, cylinder >> 8);
1503 ATA_OUTB(scp->r_io, ATA_CYL_LSB, cylinder);
1504 ATA_OUTB(scp->r_io, ATA_DRIVE, ATA_D_IBM | device | head);
1505
1506 switch (flags) {
1507 case ATA_WAIT_INTR:
1508 scp->active = ATA_WAIT_INTR;
1509 asleep((caddr_t)scp, PRIBIO, "atacmd", 10 * hz);
1510 ATA_OUTB(scp->r_io, ATA_CMD, command);
1511
1512 /* enable interrupt */
1513 if (scp->flags & ATA_QUEUED)
1514 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1515
1516 if (await(PRIBIO, 10 * hz)) {
1517 ata_printf(scp, device, "ata_command: timeout waiting for intr\n");
1518 scp->active = ATA_IDLE;
1519 error = -1;
1520 }
1521 break;
1522
1523 case ATA_WAIT_READY:
1524 if (scp->active != ATA_REINITING)
1525 scp->active = ATA_WAIT_READY;
1526 ATA_OUTB(scp->r_io, ATA_CMD, command);
1527 if (ata_wait(scp, device, ATA_S_READY) < 0) {
1528 ata_printf(scp, device,
1529 "timeout waiting for command=%02x s=%02x e=%02x\n",
1530 command, scp->status, scp->error);
1531 error = -1;
1532 }
1533 if (scp->active != ATA_REINITING)
1534 scp->active = ATA_IDLE;
1535 break;
1536
1537 case ATA_IMMEDIATE:
1538 ATA_OUTB(scp->r_io, ATA_CMD, command);
1539 break;
1540
1541 default:
1542 ata_printf(scp, device, "DANGER: illegal interrupt flag=%s\n",
1543 active2str(flags));
1544 }
1545 /* enable interrupt */
1546 if (scp->flags & ATA_QUEUED)
1547 ATA_OUTB(scp->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1548 return error;
1549}
1550
1551int
1552ata_get_lun(u_int32_t *map)
1553{
1554 int lun = ffs(~*map) - 1;
1555
--- 271 unchanged lines hidden ---