1/*-
2 * Copyright (c) 1998,1999 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 51972 1999-10-07 02:17:21Z msmith $
29 */
30
31#include "ata.h"
32#include "apm.h"
33#include "isa.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/interrupt.h>
44#include <sys/conf.h>
45#include <sys/disk.h>
46#include <sys/module.h>
47#include <sys/bus.h>
--- 24 unchanged lines hidden (view full) ---
72#include <dev/ata/atapi-all.h>
73
74/* misc defines */
75#if SMP == 0
76#define isa_apic_irq(x) x
77#endif
78
79/* prototypes */
80#if NPCI > 0
81static void promise_intr(void *);
82#endif
83static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
84static void ataintr(void *);
85
86static int32_t atanlun = 0;
87struct ata_softc *atadevices[MAXATA];
88static devclass_t ata_devclass;
89
90#if NISA > 0
91static struct isa_pnp_id ata_ids[] = {
92 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
93 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
94 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
95 {0x0306d041, "Generic ATA"}, /* PNP0603 */
96 {0}
97};
98
99static int
100ata_isaprobe(device_t dev)
101{
102 struct resource *port;
103 int rid;
--- 74 unchanged lines hidden (view full) ---
178DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
179#endif
180
181#if NPCI > 0
182static const char *
183ata_pcimatch(device_t dev)
184{
185 switch (pci_get_devid(dev)) {
186 case 0x12308086:
187 return "Intel PIIX IDE controller";
188 case 0x70108086:
189 return "Intel PIIX3 IDE controller";
190 case 0x71118086:
191 return "Intel PIIX4 IDE controller";
192 case 0x4d33105a:
193 return "Promise Ultra/33 IDE controller";
194 case 0x4d38105a:
195 return "Promise Ultra/66 IDE controller";
196 case 0x522910b9:
197 return "AcerLabs Aladdin IDE controller";
198 case 0x00041103:
199 return "HighPoint HPT366 IDE controller";
200 case 0x05711106: /* 82c586 */
201 case 0x05961106: /* 82c596 */
202 return "VIA Apollo IDE controller";
203 case 0x06401095:
204 return "CMD 640 IDE controller";
205 case 0x06461095:
206 return "CMD 646 IDE controller";
207 case 0xc6931080:
208 return "Cypress 82C693 IDE controller";
209 case 0x01021078:
210 return "Cyrix 5530 IDE controller";
211 default:
212 if (pci_get_class(dev) == PCIC_STORAGE &&
213 pci_get_subclass(dev) == PCIS_STORAGE_IDE)
214 return "Unknown PCI IDE controller (using generic mode)";
215 }
216 return NULL;
217}
218
219static int
220ata_pciprobe(device_t dev)
221{
222 const char *desc = ata_pcimatch(dev);
--- 19 unchanged lines hidden (view full) ---
242
243 /* set up vendor-specific stuff */
244 type = pci_get_devid(dev);
245 class = pci_get_class(dev);
246 subclass = pci_get_subclass(dev);
247 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
248
249#ifdef ATA_DEBUG
250 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n",
251 unit, type, class, subclass, cmd);
252#endif
253
254 /* if this is a Promise controller handle it specially */
255 if (type == 0x4d33105a || type == 0x4d38105a) {
256 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
257 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
258 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
259 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
260 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
261 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
262 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
263 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
264 printf("ata-pci%d: Busmastering DMA supported\n", unit);
265 }
266 /* everybody else seems to do it this way */
267 else {
268 if ((unit == 0) &&
269 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
270 iobase_1 = IO_WD1;
271 altiobase_1 = iobase_1 + ATA_ALTPORT;
272 irq1 = 14;
273 }
274 else {
275 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
276 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
277 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
278 }
279 if ((unit == 0) &&
280 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) {
281 iobase_2 = IO_WD2;
282 altiobase_2 = iobase_2 + ATA_ALTPORT;
283 irq2 = 15;
284 }
285 else {
286 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
287 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
288 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
289 }
290
291 /* is this controller busmaster capable ? */
292 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
293 /* is busmastering support turned on ? */
294 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
295 /* is there a valid port range to connect to ? */
296 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
297 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
298 printf("ata-pci%d: Busmastering DMA supported\n", unit);
299 }
300 else
301 printf("ata-pci%d: Busmastering DMA not configured\n",unit);
302 }
303 else
304 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
305 }
306 else
307 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
308 }
309
310 /* now probe the addresse found for "real" ATA/ATAPI hardware */
311 lun = 0;
312 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
313 scp = atadevices[lun];
314 if (iobase_1 == IO_WD1)
315#ifdef __i386__
316 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
317 &bio_imask, INTR_EXCL);
318#endif
319#ifdef __alpha__
320 alpha_platform_setup_ide_intr(0, ataintr, scp);
321#endif
322 else {
323 struct resource *irq;
324 int rid = 0;
325 void *ih;
326
327 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
328 RF_SHAREABLE | RF_ACTIVE);
329 if (!irq)
330 printf("ata_pciattach: Unable to alloc interrupt\n");
331
332 if (type == 0x4d33105a || type == 0x4d38105a)
333 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih);
334 else
335 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
336 }
337 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
338 lun, iobase_1, isa_apic_irq(irq1), unit);
339 }
340 lun = 1;
341 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
342 scp = atadevices[lun];
343 if (iobase_2 == IO_WD2)
344#ifdef __i386__
345 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
346 &bio_imask, INTR_EXCL);
347#endif
348#ifdef __alpha__
349 alpha_platform_setup_ide_intr(1, ataintr, scp);
350#endif
351 else {
352 struct resource *irq;
353 int rid = 0;
354 void *ih;
355
356 if (type != 0x4d33105a && type != 0x4d38105a) {
357 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
358 RF_SHAREABLE | RF_ACTIVE);
359 if (!irq)
360 printf("ata_pciattach: Unable to alloc interrupt\n");
361
362 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
363 }
364 }
365 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
366 lun, iobase_2, isa_apic_irq(irq2), unit);
367 }
368 return 0;
369}
370
371static device_method_t ata_pci_methods[] = {
--- 5 unchanged lines hidden (view full) ---
377
378static driver_t ata_pci_driver = {
379 "ata-pci",
380 ata_pci_methods,
381 sizeof(int),
382};
383
384DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
385
386static void
387promise_intr(void *data)
388{
389 struct ata_softc *scp = (struct ata_softc *)data;
390 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c);
391
392 if (channel & 0x00000400)
393 ataintr(data);
394
395 if (channel & 0x00004000)
396 ataintr(atadevices[scp->lun + 1]);
397}
398#endif
399
400static int32_t
401ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
402 device_t dev, int32_t *unit)
403{
404 struct ata_softc *scp = atadevices[atanlun];
405 int32_t mask = 0;
406 int32_t lun = atanlun;
407 u_int8_t status0, status1;
408
409#ifdef ATA_STATIC_ID
410 atanlun++;
411#endif
412 if (lun > MAXATA) {
413 printf("ata: unit out of range(%d)\n", lun);
414 return 0;
415 }
416 if (scp) {
417 printf("ata%d: unit already attached\n", lun);
418 return 0;
419 }
420 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT);
421 if (scp == NULL) {
422 printf("ata%d: failed to allocate driver storage\n", lun);
423 return 0;
424 }
425 bzero(scp, sizeof(struct ata_softc));
426
427 scp->unit = *unit;
428 scp->lun = lun;
429 scp->ioaddr = ioaddr;
430 scp->altioaddr = altioaddr;
431 scp->active = ATA_IDLE;
432
433 if (bootverbose)
434 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n",
435 scp->lun, scp->ioaddr, scp->altioaddr);
436
437
438 /* do we have any signs of ATA/ATAPI HW being present ? */
--- 64 unchanged lines hidden (view full) ---
503 }
504 TAILQ_INIT(&scp->ata_queue);
505 TAILQ_INIT(&scp->atapi_queue);
506 *unit = scp->lun;
507 scp->dev = dev;
508 if (bmaddr)
509 scp->bmaddr = bmaddr;
510 atadevices[scp->lun] = scp;
511#ifndef ATA_STATIC_ID
512 atanlun++;
513#endif
514#if NAPM > 0
515 scp->resume_hook.ah_fun = (void *)ata_reinit;
516 scp->resume_hook.ah_arg = scp;
517 scp->resume_hook.ah_name = "ATA driver";
518 scp->resume_hook.ah_order = APM_MID_ORDER;
519 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
520#endif
521 return ATA_IOSIZE;
522}
523
524static void
525ataintr(void *data)
526{
527 struct ata_softc *scp =(struct ata_softc *)data;
528
529 scp->status = inb(scp->ioaddr + ATA_STATUS);
530
531 /* find & call the responsible driver to process this interrupt */
532 switch (scp->active) {
533#if NATADISK > 0
534 case ATA_ACTIVE_ATA:
535 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
536 return;
537 break;
538#endif
539#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
540 case ATA_ACTIVE_ATAPI:
541 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
542 return;
543 break;
544#endif
545 case ATA_WAIT_INTR:
546 wakeup((caddr_t)scp);
547 break;
548
549 case ATA_IGNORE_INTR:
550 break;
551
552 default:
553 case ATA_IDLE:
554#ifdef ATA_DEBUG
555 {
556 static int32_t intr_count = 0;
--- 17 unchanged lines hidden (view full) ---
574
575#ifdef ATA_DEBUG
576 printf("ata_start: entered\n");
577#endif
578 if (scp->active != ATA_IDLE)
579 return;
580
581#if NATADISK > 0
582 /* find & call the responsible driver if anything on ATA queue */
583 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
584 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
585 scp->active = ATA_ACTIVE_ATA;
586 scp->running = ad_request;
587 ad_transfer(ad_request);
588#ifdef ATA_DEBUG
589 printf("ata_start: started ata, leaving\n");
590#endif
591 return;
592 }
593#endif
594#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
595 /*
596 * find & call the responsible driver if anything on ATAPI queue.
597 * check for device busy by polling the DSC bit, if busy, check
598 * for requests to the other device on the channel (if any).
599 * if no request can be served, timeout a call to ata_start to
600 * try again in a moment. the timeout should probably scale
601 * so we dont use too much time polling for slow devices.
602 */
603 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
604 struct atapi_softc *atp = atapi_request->device;
605
606 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
607 DELAY(1);
608 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
609 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
610 if (atapi_request->device->unit != atp->unit) {
611 struct atapi_softc *tmpatp = atapi_request->device;
612
613 outb(tmpatp->controller->ioaddr + ATA_DRIVE,
614 ATA_D_IBM | tmpatp->unit);
615 DELAY(1);
616 if (!(inb(tmpatp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC))
617 atapi_request = NULL;
618 break;
619 }
620 }
621 }
622 if (!atapi_request)
623 atapi_request = TAILQ_FIRST(&scp->atapi_queue);
624 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
625 scp->active = ATA_ACTIVE_ATAPI;
626 scp->running = atapi_request;
627 atapi_transfer(atapi_request);
628#ifdef ATA_DEBUG
629 printf("ata_start: started atapi, leaving\n");
630#endif
631 return;
--- 11 unchanged lines hidden (view full) ---
643 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
644 DELAY(1);
645 inb(scp->ioaddr + ATA_STATUS);
646 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
647 DELAY(10000);
648 outb(scp->altioaddr, ATA_A_IDS);
649 DELAY(10000);
650 inb(scp->ioaddr + ATA_ERROR);
651 DELAY(1);
652 outb(scp->altioaddr, ATA_A_4BIT);
653 DELAY(1);
654
655 /* wait for BUSY to go inactive */
656 for (timeout = 0; timeout < 300000; timeout++) {
657 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
658 DELAY(1);
659 status0 = inb(scp->ioaddr + ATA_STATUS);
660 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
661 DELAY(1);
662 status1 = inb(scp->ioaddr + ATA_STATUS);
663 if (*mask == 0x01) /* wait for master only */
664 if (!(status0 & ATA_S_BSY))
665 break;
666 if (*mask == 0x02) /* wait for slave only */
667 if (!(status1 & ATA_S_BSY))
668 break;
669 if (*mask == 0x03) /* wait for both master & slave */
670 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY))
671 break;
672 DELAY(100);
673 }
674 if (status0 & ATA_S_BSY)
675 *mask &= ~0x01;
676 if (status1 & ATA_S_BSY)
677 *mask &= ~0x02;
678 if (bootverbose)
679 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
680 scp->lun, *mask, status0, status1);
681}
682
683int32_t
684ata_reinit(struct ata_softc *scp)
685{
686 int32_t mask = 0;
687
688 printf("ata%d: resetting devices .. ", scp->lun);
689 scp->active = ATA_IDLE;
690 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
691 mask |= 0x01;
692 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
693 mask |= 0x02;
694 ata_reset(scp, &mask);
695
696#if NATADISK > 0
697 if (scp->devices & (ATA_ATA_MASTER))
698 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
699 if (scp->devices & (ATA_ATA_SLAVE))
700 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
701#endif
702#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
703 if (scp->devices & (ATA_ATAPI_MASTER))
704 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
705 if (scp->devices & (ATA_ATAPI_SLAVE))
706 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
707#endif
708 printf("done\n");
709 ata_start(scp);
710 return 0;
711}
712
713int32_t
714ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
715{
716 u_int8_t status;
--- 8 unchanged lines hidden (view full) ---
725 scp->lun, device?"slave":"master");
726 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
727 DELAY(1);
728 status = inb(scp->ioaddr + ATA_STATUS);
729 }
730 if (status == 0xff)
731 return -1;
732 scp->status = status;
733 if (!(status & ATA_S_BSY)) {
734 if (status & ATA_S_ERROR)
735 scp->error = inb(scp->ioaddr + ATA_ERROR);
736 if ((status & mask) == mask)
737 return (status & ATA_S_ERROR);
738 }
739 if (timeout > 1000) {
740 timeout += 1000;
741 DELAY(1000);
--- 15 unchanged lines hidden (view full) ---
757 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, "
758 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
759 scp->lun, scp->ioaddr, device, command,
760 cylinder, head, sector, count, flags);
761#endif
762
763 /* ready to issue command ? */
764 if (ata_wait(scp, device, 0) < 0) {
765 printf("ata%d-%s: timeout waiting to give command s=%02x e=%02x\n",
766 scp->lun, device?"slave":"master", scp->status, scp->error);
767 }
768 outb(scp->ioaddr + ATA_FEATURE, feature);
769 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
770 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
771 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
772 outb(scp->ioaddr + ATA_SECTOR, sector);
773 outb(scp->ioaddr + ATA_COUNT, count);
774
775 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE)
776 printf("DANGER active=%d\n", scp->active);
777
778 switch (flags) {
779 case ATA_WAIT_INTR:
780 scp->active = ATA_WAIT_INTR;
781 outb(scp->ioaddr + ATA_CMD, command);
782 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) {
783 printf("ata_command: timeout waiting for interrupt\n");
784 scp->active = ATA_IDLE;
785 return -1;
786 }
787 break;
788
789 case ATA_IGNORE_INTR:
790 scp->active = ATA_IGNORE_INTR;
791 outb(scp->ioaddr + ATA_CMD, command);
792 break;
793
794 case ATA_IMMEDIATE:
795 default:
796 outb(scp->ioaddr + ATA_CMD, command);
797 break;
798 }
799#ifdef ATA_DEBUG
800 printf("ata_command: leaving\n");
801#endif
802 return 0;
803}
804
805int8_t *
806ata_mode2str(int32_t mode)
807{
808 switch (mode) {
809 case ATA_MODE_PIO:
810 return "PIO";
811 case ATA_MODE_DMA:
812 return "DMA";
813 case ATA_MODE_UDMA33:
814 return "UDMA33";
815 case ATA_MODE_UDMA66:
816 return "UDMA66";
817 default:
818 return "???";
819 }
820}
821
822void
823bswap(int8_t *buf, int32_t len)
824{
825 u_int16_t *p = (u_int16_t*)(buf + len);
826
827 while (--p >= (u_int16_t*)buf)
828 *p = ntohs(*p);
829}
--- 34 unchanged lines hidden ---
2 * Copyright (c) 1998,1999 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 51972 1999-10-07 02:17:21Z msmith $
29 */
30
31#include "ata.h"
32#include "apm.h"
33#include "isa.h"
34#include "pci.h"
35#include "atadisk.h"
36#include "atapicd.h"
37#include "atapifd.h"
38#include "atapist.h"
39#include "opt_global.h"
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/interrupt.h>
44#include <sys/conf.h>
45#include <sys/disk.h>
46#include <sys/module.h>
47#include <sys/bus.h>
--- 24 unchanged lines hidden (view full) ---
72#include <dev/ata/atapi-all.h>
73
74/* misc defines */
75#if SMP == 0
76#define isa_apic_irq(x) x
77#endif
78
79/* prototypes */
80#if NPCI > 0
81static void promise_intr(void *);
82#endif
83static int32_t ata_probe(int32_t, int32_t, int32_t, device_t, int32_t *);
84static void ataintr(void *);
85
86static int32_t atanlun = 0;
87struct ata_softc *atadevices[MAXATA];
88static devclass_t ata_devclass;
89
90#if NISA > 0
91static struct isa_pnp_id ata_ids[] = {
92 {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */
93 {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */
94 {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */
95 {0x0306d041, "Generic ATA"}, /* PNP0603 */
96 {0}
97};
98
99static int
100ata_isaprobe(device_t dev)
101{
102 struct resource *port;
103 int rid;
--- 74 unchanged lines hidden (view full) ---
178DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0);
179#endif
180
181#if NPCI > 0
182static const char *
183ata_pcimatch(device_t dev)
184{
185 switch (pci_get_devid(dev)) {
186 case 0x12308086:
187 return "Intel PIIX IDE controller";
188 case 0x70108086:
189 return "Intel PIIX3 IDE controller";
190 case 0x71118086:
191 return "Intel PIIX4 IDE controller";
192 case 0x4d33105a:
193 return "Promise Ultra/33 IDE controller";
194 case 0x4d38105a:
195 return "Promise Ultra/66 IDE controller";
196 case 0x522910b9:
197 return "AcerLabs Aladdin IDE controller";
198 case 0x00041103:
199 return "HighPoint HPT366 IDE controller";
200 case 0x05711106: /* 82c586 */
201 case 0x05961106: /* 82c596 */
202 return "VIA Apollo IDE controller";
203 case 0x06401095:
204 return "CMD 640 IDE controller";
205 case 0x06461095:
206 return "CMD 646 IDE controller";
207 case 0xc6931080:
208 return "Cypress 82C693 IDE controller";
209 case 0x01021078:
210 return "Cyrix 5530 IDE controller";
211 default:
212 if (pci_get_class(dev) == PCIC_STORAGE &&
213 pci_get_subclass(dev) == PCIS_STORAGE_IDE)
214 return "Unknown PCI IDE controller (using generic mode)";
215 }
216 return NULL;
217}
218
219static int
220ata_pciprobe(device_t dev)
221{
222 const char *desc = ata_pcimatch(dev);
--- 19 unchanged lines hidden (view full) ---
242
243 /* set up vendor-specific stuff */
244 type = pci_get_devid(dev);
245 class = pci_get_class(dev);
246 subclass = pci_get_subclass(dev);
247 cmd = pci_read_config(dev, PCIR_COMMAND, 4);
248
249#ifdef ATA_DEBUG
250 printf("ata-pci%d: type=%08x class=%02x subclass=%02x cmd=%08x\n",
251 unit, type, class, subclass, cmd);
252#endif
253
254 /* if this is a Promise controller handle it specially */
255 if (type == 0x4d33105a || type == 0x4d38105a) {
256 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
257 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
258 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
259 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
260 irq1 = irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
261 bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc;
262 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
263 outb(bmaddr_1 + 0x1f, inb(bmaddr_1 + 0x1f) | 0x01);
264 printf("ata-pci%d: Busmastering DMA supported\n", unit);
265 }
266 /* everybody else seems to do it this way */
267 else {
268 if ((unit == 0) &&
269 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODEPRIM) == 0) {
270 iobase_1 = IO_WD1;
271 altiobase_1 = iobase_1 + ATA_ALTPORT;
272 irq1 = 14;
273 }
274 else {
275 iobase_1 = pci_read_config(dev, 0x10, 4) & 0xfffc;
276 altiobase_1 = pci_read_config(dev, 0x14, 4) & 0xfffc;
277 irq1 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
278 }
279 if ((unit == 0) &&
280 (pci_get_progif(dev) & PCIP_STORAGE_IDE_MODESEC) == 0) {
281 iobase_2 = IO_WD2;
282 altiobase_2 = iobase_2 + ATA_ALTPORT;
283 irq2 = 15;
284 }
285 else {
286 iobase_2 = pci_read_config(dev, 0x18, 4) & 0xfffc;
287 altiobase_2 = pci_read_config(dev, 0x1c, 4) & 0xfffc;
288 irq2 = pci_read_config(dev, PCI_INTERRUPT_REG, 4) & 0xff;
289 }
290
291 /* is this controller busmaster capable ? */
292 if (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) {
293 /* is busmastering support turned on ? */
294 if ((pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4) & 5) == 5) {
295 /* is there a valid port range to connect to ? */
296 if ((bmaddr_1 = pci_read_config(dev, 0x20, 4) & 0xfffc)) {
297 bmaddr_2 = bmaddr_1 + ATA_BM_OFFSET1;
298 printf("ata-pci%d: Busmastering DMA supported\n", unit);
299 }
300 else
301 printf("ata-pci%d: Busmastering DMA not configured\n",unit);
302 }
303 else
304 printf("ata-pci%d: Busmastering DMA not enabled\n", unit);
305 }
306 else
307 printf("ata-pci%d: Busmastering DMA not supported\n", unit);
308 }
309
310 /* now probe the addresse found for "real" ATA/ATAPI hardware */
311 lun = 0;
312 if (ata_probe(iobase_1, altiobase_1, bmaddr_1, dev, &lun)) {
313 scp = atadevices[lun];
314 if (iobase_1 == IO_WD1)
315#ifdef __i386__
316 inthand_add(device_get_nameunit(dev), irq1, ataintr, scp,
317 &bio_imask, INTR_EXCL);
318#endif
319#ifdef __alpha__
320 alpha_platform_setup_ide_intr(0, ataintr, scp);
321#endif
322 else {
323 struct resource *irq;
324 int rid = 0;
325 void *ih;
326
327 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
328 RF_SHAREABLE | RF_ACTIVE);
329 if (!irq)
330 printf("ata_pciattach: Unable to alloc interrupt\n");
331
332 if (type == 0x4d33105a || type == 0x4d38105a)
333 bus_setup_intr(dev, irq, INTR_TYPE_BIO, promise_intr, scp, &ih);
334 else
335 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
336 }
337 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
338 lun, iobase_1, isa_apic_irq(irq1), unit);
339 }
340 lun = 1;
341 if (ata_probe(iobase_2, altiobase_2, bmaddr_2, dev, &lun)) {
342 scp = atadevices[lun];
343 if (iobase_2 == IO_WD2)
344#ifdef __i386__
345 inthand_add(device_get_nameunit(dev), irq2, ataintr, scp,
346 &bio_imask, INTR_EXCL);
347#endif
348#ifdef __alpha__
349 alpha_platform_setup_ide_intr(1, ataintr, scp);
350#endif
351 else {
352 struct resource *irq;
353 int rid = 0;
354 void *ih;
355
356 if (type != 0x4d33105a && type != 0x4d38105a) {
357 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
358 RF_SHAREABLE | RF_ACTIVE);
359 if (!irq)
360 printf("ata_pciattach: Unable to alloc interrupt\n");
361
362 bus_setup_intr(dev, irq, INTR_TYPE_BIO, ataintr, scp, &ih);
363 }
364 }
365 printf("ata%d at 0x%04x irq %d on ata-pci%d\n",
366 lun, iobase_2, isa_apic_irq(irq2), unit);
367 }
368 return 0;
369}
370
371static device_method_t ata_pci_methods[] = {
--- 5 unchanged lines hidden (view full) ---
377
378static driver_t ata_pci_driver = {
379 "ata-pci",
380 ata_pci_methods,
381 sizeof(int),
382};
383
384DRIVER_MODULE(ata, pci, ata_pci_driver, ata_devclass, 0, 0);
385
386static void
387promise_intr(void *data)
388{
389 struct ata_softc *scp = (struct ata_softc *)data;
390 int32_t channel = inl((pci_read_config(scp->dev, 0x20, 4) & 0xfffc) + 0x1c);
391
392 if (channel & 0x00000400)
393 ataintr(data);
394
395 if (channel & 0x00004000)
396 ataintr(atadevices[scp->lun + 1]);
397}
398#endif
399
400static int32_t
401ata_probe(int32_t ioaddr, int32_t altioaddr, int32_t bmaddr,
402 device_t dev, int32_t *unit)
403{
404 struct ata_softc *scp = atadevices[atanlun];
405 int32_t mask = 0;
406 int32_t lun = atanlun;
407 u_int8_t status0, status1;
408
409#ifdef ATA_STATIC_ID
410 atanlun++;
411#endif
412 if (lun > MAXATA) {
413 printf("ata: unit out of range(%d)\n", lun);
414 return 0;
415 }
416 if (scp) {
417 printf("ata%d: unit already attached\n", lun);
418 return 0;
419 }
420 scp = malloc(sizeof(struct ata_softc), M_DEVBUF, M_NOWAIT);
421 if (scp == NULL) {
422 printf("ata%d: failed to allocate driver storage\n", lun);
423 return 0;
424 }
425 bzero(scp, sizeof(struct ata_softc));
426
427 scp->unit = *unit;
428 scp->lun = lun;
429 scp->ioaddr = ioaddr;
430 scp->altioaddr = altioaddr;
431 scp->active = ATA_IDLE;
432
433 if (bootverbose)
434 printf("ata%d: iobase=0x%04x altiobase=0x%04x\n",
435 scp->lun, scp->ioaddr, scp->altioaddr);
436
437
438 /* do we have any signs of ATA/ATAPI HW being present ? */
--- 64 unchanged lines hidden (view full) ---
503 }
504 TAILQ_INIT(&scp->ata_queue);
505 TAILQ_INIT(&scp->atapi_queue);
506 *unit = scp->lun;
507 scp->dev = dev;
508 if (bmaddr)
509 scp->bmaddr = bmaddr;
510 atadevices[scp->lun] = scp;
511#ifndef ATA_STATIC_ID
512 atanlun++;
513#endif
514#if NAPM > 0
515 scp->resume_hook.ah_fun = (void *)ata_reinit;
516 scp->resume_hook.ah_arg = scp;
517 scp->resume_hook.ah_name = "ATA driver";
518 scp->resume_hook.ah_order = APM_MID_ORDER;
519 apm_hook_establish(APM_HOOK_RESUME, &scp->resume_hook);
520#endif
521 return ATA_IOSIZE;
522}
523
524static void
525ataintr(void *data)
526{
527 struct ata_softc *scp =(struct ata_softc *)data;
528
529 scp->status = inb(scp->ioaddr + ATA_STATUS);
530
531 /* find & call the responsible driver to process this interrupt */
532 switch (scp->active) {
533#if NATADISK > 0
534 case ATA_ACTIVE_ATA:
535 if (ad_interrupt(scp->running) == ATA_OP_CONTINUES)
536 return;
537 break;
538#endif
539#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
540 case ATA_ACTIVE_ATAPI:
541 if (atapi_interrupt(scp->running) == ATA_OP_CONTINUES)
542 return;
543 break;
544#endif
545 case ATA_WAIT_INTR:
546 wakeup((caddr_t)scp);
547 break;
548
549 case ATA_IGNORE_INTR:
550 break;
551
552 default:
553 case ATA_IDLE:
554#ifdef ATA_DEBUG
555 {
556 static int32_t intr_count = 0;
--- 17 unchanged lines hidden (view full) ---
574
575#ifdef ATA_DEBUG
576 printf("ata_start: entered\n");
577#endif
578 if (scp->active != ATA_IDLE)
579 return;
580
581#if NATADISK > 0
582 /* find & call the responsible driver if anything on ATA queue */
583 if ((ad_request = TAILQ_FIRST(&scp->ata_queue))) {
584 TAILQ_REMOVE(&scp->ata_queue, ad_request, chain);
585 scp->active = ATA_ACTIVE_ATA;
586 scp->running = ad_request;
587 ad_transfer(ad_request);
588#ifdef ATA_DEBUG
589 printf("ata_start: started ata, leaving\n");
590#endif
591 return;
592 }
593#endif
594#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
595 /*
596 * find & call the responsible driver if anything on ATAPI queue.
597 * check for device busy by polling the DSC bit, if busy, check
598 * for requests to the other device on the channel (if any).
599 * if no request can be served, timeout a call to ata_start to
600 * try again in a moment. the timeout should probably scale
601 * so we dont use too much time polling for slow devices.
602 */
603 if ((atapi_request = TAILQ_FIRST(&scp->atapi_queue))) {
604 struct atapi_softc *atp = atapi_request->device;
605
606 outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
607 DELAY(1);
608 if (!(inb(atp->controller->ioaddr + ATA_STATUS) & ATA_S_DSC)) {
609 while ((atapi_request = TAILQ_NEXT(atapi_request, chain))) {
610 if (atapi_request->device->unit != atp->unit) {
611 struct atapi_softc *tmpatp = atapi_request->device;
612
613 outb(tmpatp->controller->ioaddr + ATA_DRIVE,
614 ATA_D_IBM | tmpatp->unit);
615 DELAY(1);
616 if (!(inb(tmpatp->controller->ioaddr+ATA_STATUS)&ATA_S_DSC))
617 atapi_request = NULL;
618 break;
619 }
620 }
621 }
622 if (!atapi_request)
623 atapi_request = TAILQ_FIRST(&scp->atapi_queue);
624 TAILQ_REMOVE(&scp->atapi_queue, atapi_request, chain);
625 scp->active = ATA_ACTIVE_ATAPI;
626 scp->running = atapi_request;
627 atapi_transfer(atapi_request);
628#ifdef ATA_DEBUG
629 printf("ata_start: started atapi, leaving\n");
630#endif
631 return;
--- 11 unchanged lines hidden (view full) ---
643 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
644 DELAY(1);
645 inb(scp->ioaddr + ATA_STATUS);
646 outb(scp->altioaddr, ATA_A_IDS | ATA_A_RESET);
647 DELAY(10000);
648 outb(scp->altioaddr, ATA_A_IDS);
649 DELAY(10000);
650 inb(scp->ioaddr + ATA_ERROR);
651 DELAY(1);
652 outb(scp->altioaddr, ATA_A_4BIT);
653 DELAY(1);
654
655 /* wait for BUSY to go inactive */
656 for (timeout = 0; timeout < 300000; timeout++) {
657 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
658 DELAY(1);
659 status0 = inb(scp->ioaddr + ATA_STATUS);
660 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
661 DELAY(1);
662 status1 = inb(scp->ioaddr + ATA_STATUS);
663 if (*mask == 0x01) /* wait for master only */
664 if (!(status0 & ATA_S_BSY))
665 break;
666 if (*mask == 0x02) /* wait for slave only */
667 if (!(status1 & ATA_S_BSY))
668 break;
669 if (*mask == 0x03) /* wait for both master & slave */
670 if (!(status0 & ATA_S_BSY) && !(status1 & ATA_S_BSY))
671 break;
672 DELAY(100);
673 }
674 if (status0 & ATA_S_BSY)
675 *mask &= ~0x01;
676 if (status1 & ATA_S_BSY)
677 *mask &= ~0x02;
678 if (bootverbose)
679 printf("ata%d: mask=%02x status0=%02x status1=%02x\n",
680 scp->lun, *mask, status0, status1);
681}
682
683int32_t
684ata_reinit(struct ata_softc *scp)
685{
686 int32_t mask = 0;
687
688 printf("ata%d: resetting devices .. ", scp->lun);
689 scp->active = ATA_IDLE;
690 if (scp->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER))
691 mask |= 0x01;
692 if (scp->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE))
693 mask |= 0x02;
694 ata_reset(scp, &mask);
695
696#if NATADISK > 0
697 if (scp->devices & (ATA_ATA_MASTER))
698 ad_reinit((struct ad_softc *)scp->dev_softc[0]);
699 if (scp->devices & (ATA_ATA_SLAVE))
700 ad_reinit((struct ad_softc *)scp->dev_softc[1]);
701#endif
702#if NATAPICD > 0 || NATAPIFD > 0 || NATAPIST > 0
703 if (scp->devices & (ATA_ATAPI_MASTER))
704 atapi_reinit((struct atapi_softc *)scp->dev_softc[0]);
705 if (scp->devices & (ATA_ATAPI_SLAVE))
706 atapi_reinit((struct atapi_softc *)scp->dev_softc[1]);
707#endif
708 printf("done\n");
709 ata_start(scp);
710 return 0;
711}
712
713int32_t
714ata_wait(struct ata_softc *scp, int32_t device, u_int8_t mask)
715{
716 u_int8_t status;
--- 8 unchanged lines hidden (view full) ---
725 scp->lun, device?"slave":"master");
726 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device);
727 DELAY(1);
728 status = inb(scp->ioaddr + ATA_STATUS);
729 }
730 if (status == 0xff)
731 return -1;
732 scp->status = status;
733 if (!(status & ATA_S_BSY)) {
734 if (status & ATA_S_ERROR)
735 scp->error = inb(scp->ioaddr + ATA_ERROR);
736 if ((status & mask) == mask)
737 return (status & ATA_S_ERROR);
738 }
739 if (timeout > 1000) {
740 timeout += 1000;
741 DELAY(1000);
--- 15 unchanged lines hidden (view full) ---
757 printf("ata%d: ata_command: addr=%04x, device=%02x, cmd=%02x, "
758 "c=%d, h=%d, s=%d, count=%d, flags=%02x\n",
759 scp->lun, scp->ioaddr, device, command,
760 cylinder, head, sector, count, flags);
761#endif
762
763 /* ready to issue command ? */
764 if (ata_wait(scp, device, 0) < 0) {
765 printf("ata%d-%s: timeout waiting to give command s=%02x e=%02x\n",
766 scp->lun, device?"slave":"master", scp->status, scp->error);
767 }
768 outb(scp->ioaddr + ATA_FEATURE, feature);
769 outb(scp->ioaddr + ATA_CYL_LSB, cylinder);
770 outb(scp->ioaddr + ATA_CYL_MSB, cylinder >> 8);
771 outb(scp->ioaddr + ATA_DRIVE, ATA_D_IBM | device | head);
772 outb(scp->ioaddr + ATA_SECTOR, sector);
773 outb(scp->ioaddr + ATA_COUNT, count);
774
775 if (scp->active != ATA_IDLE && flags != ATA_IMMEDIATE)
776 printf("DANGER active=%d\n", scp->active);
777
778 switch (flags) {
779 case ATA_WAIT_INTR:
780 scp->active = ATA_WAIT_INTR;
781 outb(scp->ioaddr + ATA_CMD, command);
782 if (tsleep((caddr_t)scp, PRIBIO, "atacmd", 500)) {
783 printf("ata_command: timeout waiting for interrupt\n");
784 scp->active = ATA_IDLE;
785 return -1;
786 }
787 break;
788
789 case ATA_IGNORE_INTR:
790 scp->active = ATA_IGNORE_INTR;
791 outb(scp->ioaddr + ATA_CMD, command);
792 break;
793
794 case ATA_IMMEDIATE:
795 default:
796 outb(scp->ioaddr + ATA_CMD, command);
797 break;
798 }
799#ifdef ATA_DEBUG
800 printf("ata_command: leaving\n");
801#endif
802 return 0;
803}
804
805int8_t *
806ata_mode2str(int32_t mode)
807{
808 switch (mode) {
809 case ATA_MODE_PIO:
810 return "PIO";
811 case ATA_MODE_DMA:
812 return "DMA";
813 case ATA_MODE_UDMA33:
814 return "UDMA33";
815 case ATA_MODE_UDMA66:
816 return "UDMA66";
817 default:
818 return "???";
819 }
820}
821
822void
823bswap(int8_t *buf, int32_t len)
824{
825 u_int16_t *p = (u_int16_t*)(buf + len);
826
827 while (--p >= (u_int16_t*)buf)
828 *p = ntohs(*p);
829}
--- 34 unchanged lines hidden ---