2 * linux/drivers/ide/pci/hpt366.c Version 0.34 Sept 17, 2002
4 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
5 * Portions Copyright (C) 2001 Sun Microsystems, Inc.
7 * Thanks to HighPoint Technologies for their assistance, and hardware.
8 * Special Thanks to Jon Burchmore in SanDiego for the deep pockets, his
9 * donation of an ABit BP6 mainboard, processor, and memory acellerated
10 * development and support.
12 * Note that final HPT370 support was done by force extraction of GPL.
14 * - add function for getting/setting power status of drive
15 * - the HPT370's state machine can get confused. reset it before each dma
16 * xfer to prevent that from happening.
17 * - reset state engine whenever we get an error.
18 * - check for busmaster state at end of dma.
19 * - use new highpoint timings.
20 * - detect bus speed using highpoint register.
21 * - use pll if we don't have a clock table. added a 66MHz table that's
22 * just 2x the 33MHz table.
23 * - removed turnaround. NOTE: we never want to switch between pll and
24 * pci clocks as the chip can glitch in those cases. the highpoint
25 * approved workaround slows everything down too much to be useful. in
26 * addition, we would have to serialize access to each chip.
27 * Adrian Sun <a.sun@sun.com>
29 * add drive timings for 66MHz PCI bus,
30 * fix ATA Cable signal detection, fix incorrect /proc info
31 * add /proc display for per-drive PIO/DMA/UDMA mode and
32 * per-channel ATA-33/66 Cable detect.
33 * Duncan Laurie <void@sun.com>
35 * fixup /proc output for multiple controllers
36 * Tim Hockin <thockin@sun.com>
39 * Reset the hpt366 on error, reset on dma
40 * Fix disabling Fast Interrupt hpt366.
41 * Mike Waychison <crlf@sun.com>
45 #include <linux/config.h>
46 #include <linux/types.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/delay.h>
50 #include <linux/timer.h>
52 #include <linux/ioport.h>
53 #include <linux/blkdev.h>
54 #include <linux/hdreg.h>
56 #include <linux/interrupt.h>
57 #include <linux/pci.h>
58 #include <linux/init.h>
59 #include <linux/ide.h>
61 #include <asm/uaccess.h>
67 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
68 #include <linux/stat.h>
69 #include <linux/proc_fs.h>
70 #endif /* defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS) */
72 static unsigned int hpt_revision(struct pci_dev *dev);
73 static unsigned int hpt_minimum_revision(struct pci_dev *dev, int revision);
75 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
77 static u8 hpt366_proc = 0;
78 static struct pci_dev *hpt_devs[HPT366_MAX_DEVS];
79 static int n_hpt_devs;
81 static int hpt366_get_info (char *buffer, char **addr, off_t offset, int count)
84 char *chipset_nums[] = {"366", "366", "368",
86 "302", "371", "374" };
90 "HighPoint HPT366/368/370/372/374\n");
91 for (i = 0; i < n_hpt_devs; i++) {
92 struct pci_dev *dev = hpt_devs[i];
93 unsigned long iobase = dev->resource[4].start;
94 u32 class_rev = hpt_revision(dev);
97 p += sprintf(p, "\nController: %d\n", i);
98 p += sprintf(p, "Chipset: HPT%s\n", chipset_nums[class_rev]);
99 p += sprintf(p, "--------------- Primary Channel "
100 "--------------- Secondary Channel "
103 /* get the bus master status registers */
104 c0 = inb(iobase + 0x2);
105 c1 = inb(iobase + 0xa);
106 p += sprintf(p, "Enabled: %s"
108 (c0 & 0x80) ? "no" : "yes",
109 (c1 & 0x80) ? "no" : "yes");
111 if (hpt_minimum_revision(dev, 3)) {
113 cbl = inb(iobase + 0x7b);
114 outb(cbl | 1, iobase + 0x7b);
115 outb(cbl & ~1, iobase + 0x7b);
116 cbl = inb(iobase + 0x7a);
117 p += sprintf(p, "Cable: ATA-%d"
119 (cbl & 0x02) ? 33 : 66,
120 (cbl & 0x01) ? 33 : 66);
121 p += sprintf(p, "\n");
124 p += sprintf(p, "--------------- drive0 --------- drive1 "
125 "------- drive0 ---------- drive1 -------\n");
126 p += sprintf(p, "DMA capable: %s %s"
128 (c0 & 0x20) ? "yes" : "no ",
129 (c0 & 0x40) ? "yes" : "no ",
130 (c1 & 0x20) ? "yes" : "no ",
131 (c1 & 0x40) ? "yes" : "no ");
135 /* older revs don't have these registers mapped
137 pci_read_config_byte(dev, 0x43, &c0);
138 pci_read_config_byte(dev, 0x47, &c1);
139 pci_read_config_byte(dev, 0x4b, &c2);
140 pci_read_config_byte(dev, 0x4f, &c3);
142 p += sprintf(p, "Mode: %s %s"
144 (c0 & 0x10) ? "UDMA" : (c0 & 0x20) ? "DMA " :
145 (c0 & 0x80) ? "PIO " : "off ",
146 (c1 & 0x10) ? "UDMA" : (c1 & 0x20) ? "DMA " :
147 (c1 & 0x80) ? "PIO " : "off ",
148 (c2 & 0x10) ? "UDMA" : (c2 & 0x20) ? "DMA " :
149 (c2 & 0x80) ? "PIO " : "off ",
150 (c3 & 0x10) ? "UDMA" : (c3 & 0x20) ? "DMA " :
151 (c3 & 0x80) ? "PIO " : "off ");
154 p += sprintf(p, "\n");
156 /* p - buffer must be less than 4k! */
157 len = (p - buffer) - offset;
158 *addr = buffer + offset;
160 return len > count ? count : len;
162 #endif /* defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS) */
164 static u32 hpt_revision (struct pci_dev *dev)
167 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
170 switch(dev->device) {
171 case PCI_DEVICE_ID_TTI_HPT374:
172 class_rev = PCI_DEVICE_ID_TTI_HPT374; break;
173 case PCI_DEVICE_ID_TTI_HPT371:
174 class_rev = PCI_DEVICE_ID_TTI_HPT371; break;
175 case PCI_DEVICE_ID_TTI_HPT302:
176 class_rev = PCI_DEVICE_ID_TTI_HPT302; break;
177 case PCI_DEVICE_ID_TTI_HPT372:
178 class_rev = PCI_DEVICE_ID_TTI_HPT372; break;
185 static u32 hpt_minimum_revision (struct pci_dev *dev, int revision)
187 unsigned int class_rev = hpt_revision(dev);
189 return ((int) (class_rev > revision) ? 1 : 0);
192 static int check_in_drive_lists(ide_drive_t *drive, const char **list);
194 static u8 hpt3xx_ratemask (ide_drive_t *drive)
196 struct pci_dev *dev = HWIF(drive)->pci_dev;
199 if (hpt_minimum_revision(dev, 8)) { /* HPT374 */
200 mode = (HPT374_ALLOW_ATA133_6) ? 4 : 3;
201 } else if (hpt_minimum_revision(dev, 7)) { /* HPT371 */
202 mode = (HPT371_ALLOW_ATA133_6) ? 4 : 3;
203 } else if (hpt_minimum_revision(dev, 6)) { /* HPT302 */
204 mode = (HPT302_ALLOW_ATA133_6) ? 4 : 3;
205 } else if (hpt_minimum_revision(dev, 5)) { /* HPT372 */
206 mode = (HPT372_ALLOW_ATA133_6) ? 4 : 3;
207 } else if (hpt_minimum_revision(dev, 4)) { /* HPT370A */
208 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
209 } else if (hpt_minimum_revision(dev, 3)) { /* HPT370 */
210 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
211 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : mode;
212 } else { /* HPT366 and HPT368 */
213 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : 2;
215 if (!eighty_ninty_three(drive) && (mode))
216 mode = min(mode, (u8)1);
220 static u8 hpt3xx_ratefilter (ide_drive_t *drive, u8 speed)
222 struct pci_dev *dev = HWIF(drive)->pci_dev;
223 u8 mode = hpt3xx_ratemask(drive);
225 if (drive->media != ide_disk)
226 return min(speed, (u8)XFER_PIO_4);
230 speed = min(speed, (u8)XFER_UDMA_6);
233 speed = min(speed, (u8)XFER_UDMA_5);
234 if (hpt_minimum_revision(dev, 5))
236 if (check_in_drive_lists(drive, bad_ata100_5))
237 speed = min(speed, (u8)XFER_UDMA_4);
240 speed = min(speed, (u8)XFER_UDMA_4);
242 * CHECK ME, Does this need to be set to 5 ??
244 if (hpt_minimum_revision(dev, 3))
246 if ((check_in_drive_lists(drive, bad_ata66_4)) ||
247 (!(HPT366_ALLOW_ATA66_4)))
248 speed = min(speed, (u8)XFER_UDMA_3);
249 if ((check_in_drive_lists(drive, bad_ata66_3)) ||
250 (!(HPT366_ALLOW_ATA66_3)))
251 speed = min(speed, (u8)XFER_UDMA_2);
254 speed = min(speed, (u8)XFER_UDMA_2);
256 * CHECK ME, Does this need to be set to 5 ??
258 if (hpt_minimum_revision(dev, 3))
260 if (check_in_drive_lists(drive, bad_ata33))
261 speed = min(speed, (u8)XFER_MW_DMA_2);
265 speed = min(speed, (u8)XFER_MW_DMA_2);
271 static int check_in_drive_lists (ide_drive_t *drive, const char **list)
273 struct hd_driveid *id = drive->id;
275 if (quirk_drives == list) {
277 if (strstr(id->model, *list++))
281 if (!strcmp(*list++,id->model))
287 static unsigned int pci_bus_clock_list (u8 speed, struct chipset_bus_clock_list_entry * chipset_table)
289 for ( ; chipset_table->xfer_speed ; chipset_table++)
290 if (chipset_table->xfer_speed == speed)
291 return chipset_table->chipset_settings;
292 return chipset_table->chipset_settings;
295 static int hpt36x_tune_chipset(ide_drive_t *drive, u8 xferspeed)
297 struct pci_dev *dev = HWIF(drive)->pci_dev;
298 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
299 // u8 speed = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
300 u8 regtime = (drive->select.b.unit & 0x01) ? 0x44 : 0x40;
301 u8 regfast = (HWIF(drive)->channel) ? 0x55 : 0x51;
303 u32 reg1 = 0, reg2 = 0;
306 * Disable the "fast interrupt" prediction.
308 pci_read_config_byte(dev, regfast, &drive_fast);
310 if (drive_fast & 0x02)
311 pci_write_config_byte(dev, regfast, drive_fast & ~0x20);
313 if (drive_fast & 0x80)
314 pci_write_config_byte(dev, regfast, drive_fast & ~0x80);
317 reg2 = pci_bus_clock_list(speed,
318 (struct chipset_bus_clock_list_entry *) pci_get_drvdata(dev));
320 * Disable on-chip PIO FIFO/buffer
321 * (to avoid problems handling I/O errors later)
323 pci_read_config_dword(dev, regtime, ®1);
324 if (speed >= XFER_MW_DMA_0) {
325 reg2 = (reg2 & ~0xc0000000) | (reg1 & 0xc0000000);
327 reg2 = (reg2 & ~0x30070000) | (reg1 & 0x30070000);
331 pci_write_config_dword(dev, regtime, reg2);
333 return ide_config_drive_speed(drive, speed);
336 static int hpt370_tune_chipset(ide_drive_t *drive, u8 xferspeed)
338 struct pci_dev *dev = HWIF(drive)->pci_dev;
339 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
340 // u8 speed = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
341 u8 regfast = (HWIF(drive)->channel) ? 0x55 : 0x51;
342 u8 drive_pci = 0x40 + (drive->dn * 4);
343 u8 new_fast = 0, drive_fast = 0;
344 u32 list_conf = 0, drive_conf = 0;
345 u32 conf_mask = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
348 * Disable the "fast interrupt" prediction.
349 * don't holdoff on interrupts. (== 0x01 despite what the docs say)
351 pci_read_config_byte(dev, regfast, &drive_fast);
352 new_fast = drive_fast;
356 #ifdef HPT_DELAY_INTERRUPT
360 if ((new_fast & 0x01) == 0)
363 if (new_fast != drive_fast)
364 pci_write_config_byte(dev, regfast, new_fast);
366 list_conf = pci_bus_clock_list(speed,
367 (struct chipset_bus_clock_list_entry *)
368 pci_get_drvdata(dev));
370 pci_read_config_dword(dev, drive_pci, &drive_conf);
371 list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
373 if (speed < XFER_MW_DMA_0) {
374 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
377 pci_write_config_dword(dev, drive_pci, list_conf);
379 return ide_config_drive_speed(drive, speed);
382 static int hpt372_tune_chipset(ide_drive_t *drive, u8 xferspeed)
384 struct pci_dev *dev = HWIF(drive)->pci_dev;
385 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
386 // u8 speed = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
387 u8 regfast = (HWIF(drive)->channel) ? 0x55 : 0x51;
388 u8 drive_fast = 0, drive_pci = 0x40 + (drive->dn * 4);
389 u32 list_conf = 0, drive_conf = 0;
390 u32 conf_mask = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
393 * Disable the "fast interrupt" prediction.
394 * don't holdoff on interrupts. (== 0x01 despite what the docs say)
396 pci_read_config_byte(dev, regfast, &drive_fast);
398 pci_write_config_byte(dev, regfast, drive_fast);
400 list_conf = pci_bus_clock_list(speed,
401 (struct chipset_bus_clock_list_entry *)
402 pci_get_drvdata(dev));
403 pci_read_config_dword(dev, drive_pci, &drive_conf);
404 list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
405 if (speed < XFER_MW_DMA_0)
406 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
407 pci_write_config_dword(dev, drive_pci, list_conf);
409 return ide_config_drive_speed(drive, speed);
412 static int hpt3xx_tune_chipset (ide_drive_t *drive, u8 speed)
414 struct pci_dev *dev = HWIF(drive)->pci_dev;
416 if (hpt_minimum_revision(dev, 8))
417 return hpt372_tune_chipset(drive, speed); /* not a typo */
419 else if (hpt_minimum_revision(dev, 7))
420 hpt371_tune_chipset(drive, speed);
421 else if (hpt_minimum_revision(dev, 6))
422 hpt302_tune_chipset(drive, speed);
424 else if (hpt_minimum_revision(dev, 5))
425 return hpt372_tune_chipset(drive, speed);
426 else if (hpt_minimum_revision(dev, 3))
427 return hpt370_tune_chipset(drive, speed);
428 else /* hpt368: hpt_minimum_revision(dev, 2) */
429 return hpt36x_tune_chipset(drive, speed);
432 static void hpt3xx_tune_drive (ide_drive_t *drive, u8 pio)
434 pio = ide_get_best_pio_mode(drive, 255, pio, NULL);
435 (void) hpt3xx_tune_chipset(drive, (XFER_PIO_0 + pio));
439 * This allows the configuration of ide_pci chipset registers
440 * for cards that learn about the drive's UDMA, DMA, PIO capabilities
441 * after the drive is reported by the OS. Initially for designed for
442 * HPT366 UDMA chipset by HighPoint|Triones Technologies, Inc.
444 * check_in_drive_lists(drive, bad_ata66_4)
445 * check_in_drive_lists(drive, bad_ata66_3)
446 * check_in_drive_lists(drive, bad_ata33)
449 static int config_chipset_for_dma (ide_drive_t *drive)
451 u8 speed = ide_dma_speed(drive, hpt3xx_ratemask(drive));
456 (void) hpt3xx_tune_chipset(drive, speed);
457 return ide_dma_enable(drive);
460 static int hpt3xx_quirkproc (ide_drive_t *drive)
462 return ((int) check_in_drive_lists(drive, quirk_drives));
465 static void hpt3xx_intrproc (ide_drive_t *drive)
467 ide_hwif_t *hwif = HWIF(drive);
469 if (drive->quirk_list)
471 /* drives in the quirk_list may not like intr setups/cleanups */
472 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
475 static void hpt3xx_maskproc (ide_drive_t *drive, int mask)
477 struct pci_dev *dev = HWIF(drive)->pci_dev;
479 if (drive->quirk_list) {
480 if (hpt_minimum_revision(dev,3)) {
482 pci_read_config_byte(dev, 0x5a, ®5a);
483 if (((reg5a & 0x10) >> 4) != mask)
484 pci_write_config_byte(dev, 0x5a, mask ? (reg5a | 0x10) : (reg5a & ~0x10));
487 disable_irq(HWIF(drive)->irq);
489 enable_irq(HWIF(drive)->irq);
494 HWIF(drive)->OUTB(mask ? (drive->ctl | 2) :
500 static int hpt366_config_drive_xfer_rate (ide_drive_t *drive)
502 ide_hwif_t *hwif = HWIF(drive);
503 struct hd_driveid *id = drive->id;
505 drive->init_speed = 0;
507 if (id && (id->capability & 1) && drive->autodma) {
508 /* Consult the list of known "bad" drives */
509 if (__ide_dma_bad_drive(drive))
511 if (id->field_valid & 4) {
512 if (id->dma_ultra & hwif->ultra_mask) {
513 /* Force if Capable UltraDMA */
514 int dma = config_chipset_for_dma(drive);
515 if ((id->field_valid & 2) && !dma)
518 } else if (id->field_valid & 2) {
520 if (id->dma_mword & hwif->mwdma_mask) {
521 /* Force if Capable regular DMA modes */
522 if (!config_chipset_for_dma(drive))
525 } else if (__ide_dma_good_drive(drive) &&
526 (id->eide_dma_time < 150)) {
527 /* Consult the list of known "good" drives */
528 if (!config_chipset_for_dma(drive))
533 return hwif->ide_dma_on(drive);
534 } else if ((id->capability & 8) || (id->field_valid & 2)) {
537 hpt3xx_tune_drive(drive, 5);
538 return hwif->ide_dma_off_quietly(drive);
540 /* IORDY not supported */
545 * This is specific to the HPT366 UDMA bios chipset
546 * by HighPoint|Triones Technologies, Inc.
548 static int hpt366_ide_dma_lostirq (ide_drive_t *drive)
550 struct pci_dev *dev = HWIF(drive)->pci_dev;
551 u8 reg50h = 0, reg52h = 0, reg5ah = 0;
553 pci_read_config_byte(dev, 0x50, ®50h);
554 pci_read_config_byte(dev, 0x52, ®52h);
555 pci_read_config_byte(dev, 0x5a, ®5ah);
556 printk("%s: (%s) reg50h=0x%02x, reg52h=0x%02x, reg5ah=0x%02x\n",
557 drive->name, __FUNCTION__, reg50h, reg52h, reg5ah);
559 pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
561 /* how about we flush and reset, mmmkay? */
562 pci_write_config_byte(dev, 0x51, 0x1F);
563 /* fall through to a reset */
566 /* reset the chips state over and over.. */
567 pci_write_config_byte(dev, 0x51, 0x13);
569 return __ide_dma_lostirq(drive);
572 static void hpt370_clear_engine (ide_drive_t *drive)
574 u8 regstate = HWIF(drive)->channel ? 0x54 : 0x50;
575 pci_write_config_byte(HWIF(drive)->pci_dev, regstate, 0x37);
579 static int hpt370_ide_dma_begin (ide_drive_t *drive)
581 #ifdef HPT_RESET_STATE_ENGINE
582 hpt370_clear_engine(drive);
584 return __ide_dma_begin(drive);
587 static int hpt370_ide_dma_end (ide_drive_t *drive)
589 ide_hwif_t *hwif = HWIF(drive);
590 u8 dma_stat = hwif->INB(hwif->dma_status);
592 if (dma_stat & 0x01) {
595 dma_stat = hwif->INB(hwif->dma_status);
597 if ((dma_stat & 0x01) != 0)
599 (void) HWIF(drive)->ide_dma_timeout(drive);
601 return __ide_dma_end(drive);
604 static void hpt370_lostirq_timeout (ide_drive_t *drive)
606 ide_hwif_t *hwif = HWIF(drive);
607 u8 bfifo = 0, reginfo = hwif->channel ? 0x56 : 0x52;
608 u8 dma_stat = 0, dma_cmd = 0;
610 pci_read_config_byte(HWIF(drive)->pci_dev, reginfo, &bfifo);
611 printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
612 hpt370_clear_engine(drive);
613 /* get dma command mode */
614 dma_cmd = hwif->INB(hwif->dma_command);
616 hwif->OUTB(dma_cmd & ~0x1, hwif->dma_command);
617 dma_stat = hwif->INB(hwif->dma_status);
619 hwif->OUTB(dma_stat | 0x6, hwif->dma_status);
622 static int hpt370_ide_dma_timeout (ide_drive_t *drive)
624 hpt370_lostirq_timeout(drive);
625 hpt370_clear_engine(drive);
626 return __ide_dma_timeout(drive);
629 static int hpt370_ide_dma_lostirq (ide_drive_t *drive)
631 hpt370_lostirq_timeout(drive);
632 hpt370_clear_engine(drive);
633 return __ide_dma_lostirq(drive);
636 /* returns 1 if DMA IRQ issued, 0 otherwise */
637 static int hpt374_ide_dma_test_irq(ide_drive_t *drive)
639 ide_hwif_t *hwif = HWIF(drive);
641 u8 reginfo = hwif->channel ? 0x56 : 0x52;
644 pci_read_config_word(hwif->pci_dev, reginfo, &bfifo);
646 // printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
650 dma_stat = hwif->INB(hwif->dma_status);
651 /* return 1 if INTR asserted */
652 if ((dma_stat & 4) == 4)
655 if (!drive->waiting_for_dma)
656 printk(KERN_WARNING "%s: (%s) called while not waiting\n",
657 drive->name, __FUNCTION__);
661 static int hpt374_ide_dma_end (ide_drive_t *drive)
663 struct pci_dev *dev = HWIF(drive)->pci_dev;
664 ide_hwif_t *hwif = HWIF(drive);
665 u8 msc_stat = 0, mscreg = hwif->channel ? 0x54 : 0x50;
666 u8 bwsr_stat = 0, bwsr_mask = hwif->channel ? 0x02 : 0x01;
668 pci_read_config_byte(dev, 0x6a, &bwsr_stat);
669 pci_read_config_byte(dev, mscreg, &msc_stat);
670 if ((bwsr_stat & bwsr_mask) == bwsr_mask)
671 pci_write_config_byte(dev, mscreg, msc_stat|0x30);
672 return __ide_dma_end(drive);
676 * Since SUN Cobalt is attempting to do this operation, I should disclose
677 * this has been a long time ago Thu Jul 27 16:40:57 2000 was the patch date
678 * HOTSWAP ATA Infrastructure.
681 static void hpt3xx_reset (ide_drive_t *drive)
684 unsigned long high_16 = pci_resource_start(HWIF(drive)->pci_dev, 4);
685 u8 reset = (HWIF(drive)->channel) ? 0x80 : 0x40;
688 pci_read_config_byte(HWIF(drive)->pci_dev, 0x59, ®59h);
689 pci_write_config_byte(HWIF(drive)->pci_dev, 0x59, reg59h|reset);
690 pci_write_config_byte(HWIF(drive)->pci_dev, 0x59, reg59h);
694 static int hpt3xx_tristate (ide_drive_t * drive, int state)
696 ide_hwif_t *hwif = HWIF(drive);
697 struct pci_dev *dev = hwif->pci_dev;
698 u8 reg59h = 0, reset = (hwif->channel) ? 0x80 : 0x40;
699 u8 regXXh = 0, state_reg= (hwif->channel) ? 0x57 : 0x53;
704 // hwif->bus_state = state;
706 pci_read_config_byte(dev, 0x59, ®59h);
707 pci_read_config_byte(dev, state_reg, ®XXh);
710 (void) ide_do_reset(drive);
711 pci_write_config_byte(dev, state_reg, regXXh|0x80);
712 pci_write_config_byte(dev, 0x59, reg59h|reset);
714 pci_write_config_byte(dev, 0x59, reg59h & ~(reset));
715 pci_write_config_byte(dev, state_reg, regXXh & ~(0x80));
716 (void) ide_do_reset(drive);
722 * set/get power state for a drive.
723 * turning the power off does the following things:
724 * 1) soft-reset the drive
725 * 2) tri-states the ide bus
727 * when we turn things back on, we need to re-initialize things.
729 #define TRISTATE_BIT 0x8000
730 static int hpt370_busproc(ide_drive_t * drive, int state)
732 ide_hwif_t *hwif = HWIF(drive);
733 struct pci_dev *dev = hwif->pci_dev;
734 u8 tristate = 0, resetmask = 0, bus_reg = 0;
740 hwif->bus_state = state;
743 /* secondary channel */
747 /* primary channel */
753 pci_read_config_word(dev, tristate, &tri_reg);
754 pci_read_config_byte(dev, 0x59, &bus_reg);
756 /* set the state. we don't set it if we don't need to do so.
757 * make sure that the drive knows that it has failed if it's off */
760 hwif->drives[0].failures = 0;
761 hwif->drives[1].failures = 0;
762 if ((bus_reg & resetmask) == 0)
764 tri_reg &= ~TRISTATE_BIT;
765 bus_reg &= ~resetmask;
768 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
769 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
770 if ((tri_reg & TRISTATE_BIT) == 0 && (bus_reg & resetmask))
772 tri_reg &= ~TRISTATE_BIT;
773 bus_reg |= resetmask;
775 case BUSSTATE_TRISTATE:
776 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
777 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
778 if ((tri_reg & TRISTATE_BIT) && (bus_reg & resetmask))
780 tri_reg |= TRISTATE_BIT;
781 bus_reg |= resetmask;
784 pci_write_config_byte(dev, 0x59, bus_reg);
785 pci_write_config_word(dev, tristate, tri_reg);
790 static int __devinit init_hpt37x(struct pci_dev *dev)
799 pci_read_config_byte(dev, 0x5a, ®5ah);
800 /* interrupt force enable */
801 pci_write_config_byte(dev, 0x5a, (reg5ah & ~0x10));
805 * default to pci clock. make sure MA15/16 are set to output
806 * to prevent drives having problems with 40-pin cables.
808 pci_write_config_byte(dev, 0x5b, 0x23);
811 * set up the PLL. we need to adjust it so that it's stable.
812 * freq = Tpll * 192 / Tpci
814 pci_read_config_word(dev, 0x78, &freq);
818 if (hpt_minimum_revision(dev,8))
819 pci_set_drvdata(dev, (void *) thirty_three_base_hpt374);
820 else if (hpt_minimum_revision(dev,5))
821 pci_set_drvdata(dev, (void *) thirty_three_base_hpt372);
822 else if (hpt_minimum_revision(dev,4))
823 pci_set_drvdata(dev, (void *) thirty_three_base_hpt370a);
825 pci_set_drvdata(dev, (void *) thirty_three_base_hpt370);
826 printk("HPT37X: using 33MHz PCI clock\n");
827 } else if (freq < 0xb0) {
829 } else if (freq < 0xc8) {
831 if (hpt_minimum_revision(dev,8))
832 pci_set_drvdata(dev, NULL);
833 else if (hpt_minimum_revision(dev,5))
834 pci_set_drvdata(dev, (void *) fifty_base_hpt372);
835 else if (hpt_minimum_revision(dev,4))
836 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
838 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
839 printk("HPT37X: using 50MHz PCI clock\n");
842 if (hpt_minimum_revision(dev,8))
844 printk(KERN_ERR "HPT37x: 66MHz timings are not supported.\n");
845 pci_set_drvdata(dev, NULL);
847 else if (hpt_minimum_revision(dev,5))
848 pci_set_drvdata(dev, (void *) sixty_six_base_hpt372);
849 else if (hpt_minimum_revision(dev,4))
850 pci_set_drvdata(dev, (void *) sixty_six_base_hpt370a);
852 pci_set_drvdata(dev, (void *) sixty_six_base_hpt370);
853 printk("HPT37X: using 66MHz PCI clock\n");
857 * only try the pll if we don't have a table for the clock
858 * speed that we're running at. NOTE: the internal PLL will
859 * result in slow reads when using a 33MHz PCI clock. we also
860 * don't like to use the PLL because it will cause glitches
861 * on PRST/SRST when the HPT state engine gets reset.
863 if (pci_get_drvdata(dev))
864 goto init_hpt37X_done;
867 * adjust PLL based upon PCI clock, enable it, and wait for
871 freq = (pll < F_LOW_PCI_50) ? 2 : 4;
872 while (adjust++ < 6) {
873 pci_write_config_dword(dev, 0x5c, (freq + pll) << 16 |
876 /* wait for clock stabilization */
877 for (i = 0; i < 0x50000; i++) {
878 pci_read_config_byte(dev, 0x5b, ®5bh);
880 /* spin looking for the clock to destabilize */
881 for (i = 0; i < 0x1000; ++i) {
882 pci_read_config_byte(dev, 0x5b,
884 if ((reg5bh & 0x80) == 0)
887 pci_read_config_dword(dev, 0x5c, &pll);
888 pci_write_config_dword(dev, 0x5c,
890 pci_write_config_byte(dev, 0x5b, 0x21);
891 if (hpt_minimum_revision(dev,8))
892 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
893 else if (hpt_minimum_revision(dev,5))
894 pci_set_drvdata(dev, (void *) fifty_base_hpt372);
895 else if (hpt_minimum_revision(dev,4))
896 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
898 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
899 printk("HPT37X: using 50MHz internal PLL\n");
900 goto init_hpt37X_done;
905 pll -= (adjust >> 1);
907 pll += (adjust >> 1);
911 /* reset state engine */
912 pci_write_config_byte(dev, 0x50, 0x37);
913 pci_write_config_byte(dev, 0x54, 0x37);
918 static int __devinit init_hpt366(struct pci_dev *dev)
924 * Disable the "fast interrupt" prediction.
926 pci_read_config_byte(dev, 0x51, &drive_fast);
927 if (drive_fast & 0x80)
928 pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
929 pci_read_config_dword(dev, 0x40, ®1);
931 /* detect bus speed by looking at control reg timing: */
932 switch((reg1 >> 8) & 7) {
934 pci_set_drvdata(dev, (void *) forty_base_hpt366);
937 pci_set_drvdata(dev, (void *) twenty_five_base_hpt366);
941 pci_set_drvdata(dev, (void *) thirty_three_base_hpt366);
945 if (!pci_get_drvdata(dev))
947 printk(KERN_ERR "hpt366: unknown bus timing.\n");
948 pci_set_drvdata(dev, NULL);
953 static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const char *name)
958 if (dev->resource[PCI_ROM_RESOURCE].start)
959 pci_write_config_byte(dev, PCI_ROM_ADDRESS,
960 dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
962 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &test);
963 if (test != (L1_CACHE_BYTES / 4))
964 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
965 (L1_CACHE_BYTES / 4));
967 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &test);
969 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
971 pci_read_config_byte(dev, PCI_MIN_GNT, &test);
973 pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
975 pci_read_config_byte(dev, PCI_MAX_LAT, &test);
977 pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
979 if (hpt_minimum_revision(dev, 3)) {
980 ret = init_hpt37x(dev);
982 ret =init_hpt366(dev);
987 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
988 hpt_devs[n_hpt_devs++] = dev;
992 ide_pci_create_host_proc("hpt366", hpt366_get_info);
994 #endif /* DISPLAY_HPT366_TIMINGS && CONFIG_PROC_FS */
999 static void __devinit init_hwif_hpt366(ide_hwif_t *hwif)
1001 struct pci_dev *dev = hwif->pci_dev;
1002 u8 ata66 = 0, regmask = (hwif->channel) ? 0x01 : 0x02;
1004 hwif->tuneproc = &hpt3xx_tune_drive;
1005 hwif->speedproc = &hpt3xx_tune_chipset;
1006 hwif->quirkproc = &hpt3xx_quirkproc;
1007 hwif->intrproc = &hpt3xx_intrproc;
1008 hwif->maskproc = &hpt3xx_maskproc;
1011 * The HPT37x uses the CBLID pins as outputs for MA15/MA16
1012 * address lines to access an external eeprom. To read valid
1013 * cable detect state the pins must be enabled as inputs.
1015 if (hpt_minimum_revision(dev, 8) && PCI_FUNC(dev->devfn) & 1) {
1017 * HPT374 PCI function 1
1018 * - set bit 15 of reg 0x52 to enable TCBLID as input
1019 * - set bit 15 of reg 0x56 to enable FCBLID as input
1022 pci_read_config_word(dev, 0x52, &mcr3);
1023 pci_read_config_word(dev, 0x56, &mcr6);
1024 pci_write_config_word(dev, 0x52, mcr3 | 0x8000);
1025 pci_write_config_word(dev, 0x56, mcr6 | 0x8000);
1026 /* now read cable id register */
1027 pci_read_config_byte(dev, 0x5a, &ata66);
1028 pci_write_config_word(dev, 0x52, mcr3);
1029 pci_write_config_word(dev, 0x56, mcr6);
1030 } else if (hpt_minimum_revision(dev, 3)) {
1032 * HPT370/372 and 374 pcifn 0
1033 * - clear bit 0 of 0x5b to enable P/SCBLID as inputs
1036 pci_read_config_byte(dev, 0x5b, &scr2);
1037 pci_write_config_byte(dev, 0x5b, scr2 & ~1);
1038 /* now read cable id register */
1039 pci_read_config_byte(dev, 0x5a, &ata66);
1040 pci_write_config_byte(dev, 0x5b, scr2);
1042 pci_read_config_byte(dev, 0x5a, &ata66);
1046 printk("HPT366: reg5ah=0x%02x ATA-%s Cable Port%d\n",
1047 ata66, (ata66 & regmask) ? "33" : "66",
1048 PCI_FUNC(hwif->pci_dev->devfn));
1051 #ifdef HPT_SERIALIZE_IO
1052 /* serialize access to this device */
1054 hwif->serialized = hwif->mate->serialized = 1;
1057 if (hpt_minimum_revision(dev,3)) {
1059 pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
1061 * set up ioctl for power status.
1062 * note: power affects both
1063 * drives on each channel
1065 hwif->resetproc = &hpt3xx_reset;
1066 hwif->busproc = &hpt370_busproc;
1067 // hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
1068 } else if (hpt_minimum_revision(dev,2)) {
1069 hwif->resetproc = &hpt3xx_reset;
1070 hwif->busproc = &hpt3xx_tristate;
1072 hwif->resetproc = &hpt3xx_reset;
1073 hwif->busproc = &hpt3xx_tristate;
1076 if (!hwif->dma_base) {
1077 hwif->drives[0].autotune = 1;
1078 hwif->drives[1].autotune = 1;
1082 hwif->ultra_mask = 0x7f;
1083 hwif->mwdma_mask = 0x07;
1085 if (!(hwif->udma_four))
1086 hwif->udma_four = ((ata66 & regmask) ? 0 : 1);
1087 hwif->ide_dma_check = &hpt366_config_drive_xfer_rate;
1089 if (hpt_minimum_revision(dev,8)) {
1090 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1091 hwif->ide_dma_end = &hpt374_ide_dma_end;
1092 } else if (hpt_minimum_revision(dev,5)) {
1093 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1094 hwif->ide_dma_end = &hpt374_ide_dma_end;
1095 } else if (hpt_minimum_revision(dev,3)) {
1096 hwif->ide_dma_begin = &hpt370_ide_dma_begin;
1097 hwif->ide_dma_end = &hpt370_ide_dma_end;
1098 hwif->ide_dma_timeout = &hpt370_ide_dma_timeout;
1099 hwif->ide_dma_lostirq = &hpt370_ide_dma_lostirq;
1100 } else if (hpt_minimum_revision(dev,2))
1101 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1103 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1107 hwif->drives[0].autodma = hwif->autodma;
1108 hwif->drives[1].autodma = hwif->autodma;
1111 static void __devinit init_dma_hpt366(ide_hwif_t *hwif, unsigned long dmabase)
1113 u8 masterdma = 0, slavedma = 0;
1114 u8 dma_new = 0, dma_old = 0;
1115 u8 primary = hwif->channel ? 0x4b : 0x43;
1116 u8 secondary = hwif->channel ? 0x4f : 0x47;
1117 unsigned long flags;
1122 if(pci_get_drvdata(hwif->pci_dev) == NULL)
1124 printk(KERN_WARNING "hpt: no known IDE timings, disabling DMA.\n");
1128 dma_old = hwif->INB(dmabase+2);
1130 local_irq_save(flags);
1133 pci_read_config_byte(hwif->pci_dev, primary, &masterdma);
1134 pci_read_config_byte(hwif->pci_dev, secondary, &slavedma);
1136 if (masterdma & 0x30) dma_new |= 0x20;
1137 if (slavedma & 0x30) dma_new |= 0x40;
1138 if (dma_new != dma_old)
1139 hwif->OUTB(dma_new, dmabase+2);
1141 local_irq_restore(flags);
1143 ide_setup_dma(hwif, dmabase, 8);
1146 static void __devinit init_setup_hpt374(struct pci_dev *dev, ide_pci_device_t *d)
1148 struct pci_dev *findev = NULL;
1150 if (PCI_FUNC(dev->devfn) & 1)
1153 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1154 if ((findev->vendor == dev->vendor) &&
1155 (findev->device == dev->device) &&
1156 ((findev->devfn - dev->devfn) == 1) &&
1157 (PCI_FUNC(findev->devfn) & 1)) {
1158 if (findev->irq != dev->irq) {
1159 /* FIXME: we need a core pci_set_interrupt() */
1160 findev->irq = dev->irq;
1161 printk(KERN_WARNING "%s: pci-config space interrupt "
1162 "fixed.\n", d->name);
1164 ide_setup_pci_devices(dev, findev, d);
1168 ide_setup_pci_device(dev, d);
1171 static void __devinit init_setup_hpt37x(struct pci_dev *dev, ide_pci_device_t *d)
1173 ide_setup_pci_device(dev, d);
1176 static void __devinit init_setup_hpt366(struct pci_dev *dev, ide_pci_device_t *d)
1178 struct pci_dev *findev = NULL;
1179 u8 pin1 = 0, pin2 = 0;
1180 unsigned int class_rev;
1181 char *chipset_names[] = {"HPT366", "HPT366", "HPT368",
1182 "HPT370", "HPT370A", "HPT372"};
1184 if (PCI_FUNC(dev->devfn) & 1)
1187 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1190 strcpy(d->name, chipset_names[class_rev]);
1195 case 3: ide_setup_pci_device(dev, d);
1202 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin1);
1203 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1204 if ((findev->vendor == dev->vendor) &&
1205 (findev->device == dev->device) &&
1206 ((findev->devfn - dev->devfn) == 1) &&
1207 (PCI_FUNC(findev->devfn) & 1)) {
1208 pci_read_config_byte(findev, PCI_INTERRUPT_PIN, &pin2);
1209 if ((pin1 != pin2) && (dev->irq == findev->irq)) {
1210 d->bootable = ON_BOARD;
1211 printk("%s: onboard version of chipset, "
1212 "pin1=%d pin2=%d\n", d->name,
1215 ide_setup_pci_devices(dev, findev, d);
1219 ide_setup_pci_device(dev, d);
1224 * hpt366_init_one - called when an HPT366 is found
1225 * @dev: the hpt366 device
1226 * @id: the matching pci id
1228 * Called when the PCI registration layer (or the IDE initialization)
1229 * finds a device matching our IDE device tables.
1232 static int __devinit hpt366_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1234 ide_pci_device_t *d = &hpt366_chipsets[id->driver_data];
1236 d->init_setup(dev, d);
1240 static struct pci_device_id hpt366_pci_tbl[] = {
1241 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT366, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1242 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT372, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1243 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT302, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1244 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT371, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
1245 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT374, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
1248 MODULE_DEVICE_TABLE(pci, hpt366_pci_tbl);
1250 static struct pci_driver driver = {
1251 .name = "HPT366 IDE",
1252 .id_table = hpt366_pci_tbl,
1253 .probe = hpt366_init_one,
1256 static int hpt366_ide_init(void)
1258 return ide_pci_register_driver(&driver);
1261 module_init(hpt366_ide_init);
1263 MODULE_AUTHOR("Andre Hedrick");
1264 MODULE_DESCRIPTION("PCI driver module for Highpoint HPT366 IDE");
1265 MODULE_LICENSE("GPL");