2 * linux/drivers/ide/pci/hpt366.c Version 0.36 April 25, 2003
4 * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
5 * Portions Copyright (C) 2001 Sun Microsystems, Inc.
6 * Portions Copyright (C) 2003 Red Hat Inc
8 * Thanks to HighPoint Technologies for their assistance, and hardware.
9 * Special Thanks to Jon Burchmore in SanDiego for the deep pockets, his
10 * donation of an ABit BP6 mainboard, processor, and memory acellerated
11 * development and support.
14 * Highpoint have their own driver (source except for the raid part)
15 * available from http://www.highpoint-tech.com/hpt3xx-opensource-v131.tgz
16 * This may be useful to anyone wanting to work on the mainstream hpt IDE.
18 * Note that final HPT370 support was done by force extraction of GPL.
20 * - add function for getting/setting power status of drive
21 * - the HPT370's state machine can get confused. reset it before each dma
22 * xfer to prevent that from happening.
23 * - reset state engine whenever we get an error.
24 * - check for busmaster state at end of dma.
25 * - use new highpoint timings.
26 * - detect bus speed using highpoint register.
27 * - use pll if we don't have a clock table. added a 66MHz table that's
28 * just 2x the 33MHz table.
29 * - removed turnaround. NOTE: we never want to switch between pll and
30 * pci clocks as the chip can glitch in those cases. the highpoint
31 * approved workaround slows everything down too much to be useful. in
32 * addition, we would have to serialize access to each chip.
33 * Adrian Sun <a.sun@sun.com>
35 * add drive timings for 66MHz PCI bus,
36 * fix ATA Cable signal detection, fix incorrect /proc info
37 * add /proc display for per-drive PIO/DMA/UDMA mode and
38 * per-channel ATA-33/66 Cable detect.
39 * Duncan Laurie <void@sun.com>
41 * fixup /proc output for multiple controllers
42 * Tim Hockin <thockin@sun.com>
45 * Reset the hpt366 on error, reset on dma
46 * Fix disabling Fast Interrupt hpt366.
47 * Mike Waychison <crlf@sun.com>
49 * Added support for 372N clocking and clock switching. The 372N needs
50 * different clocks on read/write. This requires overloading rw_disk and
51 * other deeply crazy things. Thanks to <http://www.hoerstreich.de> for
53 * Alan Cox <alan@redhat.com>
58 #include <linux/config.h>
59 #include <linux/types.h>
60 #include <linux/module.h>
61 #include <linux/kernel.h>
62 #include <linux/delay.h>
63 #include <linux/timer.h>
65 #include <linux/ioport.h>
66 #include <linux/blkdev.h>
67 #include <linux/hdreg.h>
69 #include <linux/interrupt.h>
70 #include <linux/pci.h>
71 #include <linux/init.h>
72 #include <linux/ide.h>
74 #include <asm/uaccess.h>
81 * Hold all the highpoint quirks and revision information in one
87 u8 max_mode; /* Speeds allowed */
88 int revision; /* Chipset revision */
89 int flags; /* Chipset properties */
93 struct chipset_bus_clock_list_entry *speed;
97 * This wants fixing so that we do everything not by classrev
98 * (which breaks on the newest chips) but by creating an
99 * enumeration of chip variants and using that
102 static __devinit u32 hpt_revision (struct pci_dev *dev)
105 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
108 switch(dev->device) {
109 /* Remap new 372N onto 372 */
110 case PCI_DEVICE_ID_TTI_HPT372N:
111 class_rev = PCI_DEVICE_ID_TTI_HPT372; break;
112 case PCI_DEVICE_ID_TTI_HPT374:
113 class_rev = PCI_DEVICE_ID_TTI_HPT374; break;
114 case PCI_DEVICE_ID_TTI_HPT371:
115 class_rev = PCI_DEVICE_ID_TTI_HPT371; break;
116 case PCI_DEVICE_ID_TTI_HPT302:
117 class_rev = PCI_DEVICE_ID_TTI_HPT302; break;
118 case PCI_DEVICE_ID_TTI_HPT372:
119 class_rev = PCI_DEVICE_ID_TTI_HPT372; break;
126 static int check_in_drive_lists(ide_drive_t *drive, const char **list);
128 static u8 hpt3xx_ratemask (ide_drive_t *drive)
130 ide_hwif_t *hwif = drive->hwif;
131 struct hpt_info *info = ide_get_hwifdata(hwif);
134 /* FIXME: TODO - move this to set info->mode once at boot */
136 if (info->revision >= 8) { /* HPT374 */
137 mode = (HPT374_ALLOW_ATA133_6) ? 4 : 3;
138 } else if (info->revision >= 7) { /* HPT371 */
139 mode = (HPT371_ALLOW_ATA133_6) ? 4 : 3;
140 } else if (info->revision >= 6) { /* HPT302 */
141 mode = (HPT302_ALLOW_ATA133_6) ? 4 : 3;
142 } else if (info->revision >= 5) { /* HPT372 */
143 mode = (HPT372_ALLOW_ATA133_6) ? 4 : 3;
144 } else if (info->revision >= 4) { /* HPT370A */
145 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
146 } else if (info->revision >= 3) { /* HPT370 */
147 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
148 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : mode;
149 } else { /* HPT366 and HPT368 */
150 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : 2;
152 if (!eighty_ninty_three(drive) && mode)
153 mode = min(mode, (u8)1);
158 * Note for the future; the SATA hpt37x we must set
159 * either PIO or UDMA modes 0,4,5
162 static u8 hpt3xx_ratefilter (ide_drive_t *drive, u8 speed)
164 ide_hwif_t *hwif = drive->hwif;
165 struct hpt_info *info = ide_get_hwifdata(hwif);
166 u8 mode = hpt3xx_ratemask(drive);
168 if (drive->media != ide_disk)
169 return min(speed, (u8)XFER_PIO_4);
173 speed = min(speed, (u8)XFER_UDMA_6);
176 speed = min(speed, (u8)XFER_UDMA_5);
177 if (info->revision >= 5)
179 if (check_in_drive_lists(drive, bad_ata100_5))
180 speed = min(speed, (u8)XFER_UDMA_4);
183 speed = min(speed, (u8)XFER_UDMA_4);
185 * CHECK ME, Does this need to be set to 5 ??
187 if (info->revision >= 3)
189 if ((check_in_drive_lists(drive, bad_ata66_4)) ||
190 (!(HPT366_ALLOW_ATA66_4)))
191 speed = min(speed, (u8)XFER_UDMA_3);
192 if ((check_in_drive_lists(drive, bad_ata66_3)) ||
193 (!(HPT366_ALLOW_ATA66_3)))
194 speed = min(speed, (u8)XFER_UDMA_2);
197 speed = min(speed, (u8)XFER_UDMA_2);
199 * CHECK ME, Does this need to be set to 5 ??
201 if (info->revision >= 3)
203 if (check_in_drive_lists(drive, bad_ata33))
204 speed = min(speed, (u8)XFER_MW_DMA_2);
208 speed = min(speed, (u8)XFER_MW_DMA_2);
214 static int check_in_drive_lists (ide_drive_t *drive, const char **list)
216 struct hd_driveid *id = drive->id;
218 if (quirk_drives == list) {
220 if (strstr(id->model, *list++))
224 if (!strcmp(*list++,id->model))
230 static unsigned int pci_bus_clock_list (u8 speed, struct chipset_bus_clock_list_entry * chipset_table)
232 for ( ; chipset_table->xfer_speed ; chipset_table++)
233 if (chipset_table->xfer_speed == speed)
234 return chipset_table->chipset_settings;
235 return chipset_table->chipset_settings;
238 static int hpt36x_tune_chipset(ide_drive_t *drive, u8 xferspeed)
240 ide_hwif_t *hwif = drive->hwif;
241 struct pci_dev *dev = hwif->pci_dev;
242 struct hpt_info *info = ide_get_hwifdata(hwif);
243 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
244 u8 regtime = (drive->select.b.unit & 0x01) ? 0x44 : 0x40;
245 u8 regfast = (hwif->channel) ? 0x55 : 0x51;
247 u32 reg1 = 0, reg2 = 0;
250 * Disable the "fast interrupt" prediction.
252 pci_read_config_byte(dev, regfast, &drive_fast);
253 if (drive_fast & 0x80)
254 pci_write_config_byte(dev, regfast, drive_fast & ~0x80);
256 reg2 = pci_bus_clock_list(speed, info->speed);
259 * Disable on-chip PIO FIFO/buffer
260 * (to avoid problems handling I/O errors later)
262 pci_read_config_dword(dev, regtime, ®1);
263 if (speed >= XFER_MW_DMA_0) {
264 reg2 = (reg2 & ~0xc0000000) | (reg1 & 0xc0000000);
266 reg2 = (reg2 & ~0x30070000) | (reg1 & 0x30070000);
270 pci_write_config_dword(dev, regtime, reg2);
272 return ide_config_drive_speed(drive, speed);
275 static int hpt370_tune_chipset(ide_drive_t *drive, u8 xferspeed)
277 ide_hwif_t *hwif = drive->hwif;
278 struct pci_dev *dev = hwif->pci_dev;
279 struct hpt_info *info = ide_get_hwifdata(hwif);
280 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
281 u8 regfast = (drive->hwif->channel) ? 0x55 : 0x51;
282 u8 drive_pci = 0x40 + (drive->dn * 4);
283 u8 new_fast = 0, drive_fast = 0;
284 u32 list_conf = 0, drive_conf = 0;
285 u32 conf_mask = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
288 * Disable the "fast interrupt" prediction.
289 * don't holdoff on interrupts. (== 0x01 despite what the docs say)
291 pci_read_config_byte(dev, regfast, &drive_fast);
292 new_fast = drive_fast;
296 #ifdef HPT_DELAY_INTERRUPT
300 if ((new_fast & 0x01) == 0)
303 if (new_fast != drive_fast)
304 pci_write_config_byte(dev, regfast, new_fast);
306 list_conf = pci_bus_clock_list(speed, info->speed);
308 pci_read_config_dword(dev, drive_pci, &drive_conf);
309 list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
311 if (speed < XFER_MW_DMA_0)
312 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
313 pci_write_config_dword(dev, drive_pci, list_conf);
315 return ide_config_drive_speed(drive, speed);
318 static int hpt372_tune_chipset(ide_drive_t *drive, u8 xferspeed)
320 ide_hwif_t *hwif = drive->hwif;
321 struct pci_dev *dev = hwif->pci_dev;
322 struct hpt_info *info = ide_get_hwifdata(hwif);
323 u8 speed = hpt3xx_ratefilter(drive, xferspeed);
324 u8 regfast = (drive->hwif->channel) ? 0x55 : 0x51;
325 u8 drive_fast = 0, drive_pci = 0x40 + (drive->dn * 4);
326 u32 list_conf = 0, drive_conf = 0;
327 u32 conf_mask = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
330 * Disable the "fast interrupt" prediction.
331 * don't holdoff on interrupts. (== 0x01 despite what the docs say)
333 pci_read_config_byte(dev, regfast, &drive_fast);
335 pci_write_config_byte(dev, regfast, drive_fast);
337 list_conf = pci_bus_clock_list(speed, info->speed);
338 pci_read_config_dword(dev, drive_pci, &drive_conf);
339 list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
340 if (speed < XFER_MW_DMA_0)
341 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
342 pci_write_config_dword(dev, drive_pci, list_conf);
344 return ide_config_drive_speed(drive, speed);
347 static int hpt3xx_tune_chipset (ide_drive_t *drive, u8 speed)
349 ide_hwif_t *hwif = drive->hwif;
350 struct hpt_info *info = ide_get_hwifdata(hwif);
352 if (info->revision >= 8)
353 return hpt372_tune_chipset(drive, speed); /* not a typo */
354 else if (info->revision >= 5)
355 return hpt372_tune_chipset(drive, speed);
356 else if (info->revision >= 3)
357 return hpt370_tune_chipset(drive, speed);
358 else /* hpt368: hpt_minimum_revision(dev, 2) */
359 return hpt36x_tune_chipset(drive, speed);
362 static void hpt3xx_tune_drive (ide_drive_t *drive, u8 pio)
364 pio = ide_get_best_pio_mode(drive, 255, pio, NULL);
365 (void) hpt3xx_tune_chipset(drive, (XFER_PIO_0 + pio));
369 * This allows the configuration of ide_pci chipset registers
370 * for cards that learn about the drive's UDMA, DMA, PIO capabilities
371 * after the drive is reported by the OS. Initially for designed for
372 * HPT366 UDMA chipset by HighPoint|Triones Technologies, Inc.
374 * check_in_drive_lists(drive, bad_ata66_4)
375 * check_in_drive_lists(drive, bad_ata66_3)
376 * check_in_drive_lists(drive, bad_ata33)
379 static int config_chipset_for_dma (ide_drive_t *drive)
381 u8 speed = ide_dma_speed(drive, hpt3xx_ratemask(drive));
382 ide_hwif_t *hwif = drive->hwif;
383 struct hpt_info *info = ide_get_hwifdata(hwif);
388 /* If we don't have any timings we can't do a lot */
389 if (info->speed == NULL)
392 (void) hpt3xx_tune_chipset(drive, speed);
393 return ide_dma_enable(drive);
396 static int hpt3xx_quirkproc (ide_drive_t *drive)
398 return ((int) check_in_drive_lists(drive, quirk_drives));
401 static void hpt3xx_intrproc (ide_drive_t *drive)
403 ide_hwif_t *hwif = drive->hwif;
405 if (drive->quirk_list)
407 /* drives in the quirk_list may not like intr setups/cleanups */
408 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
411 static void hpt3xx_maskproc (ide_drive_t *drive, int mask)
413 ide_hwif_t *hwif = drive->hwif;
414 struct hpt_info *info = ide_get_hwifdata(hwif);
415 struct pci_dev *dev = hwif->pci_dev;
417 if (drive->quirk_list) {
418 if (info->revision >= 3) {
420 pci_read_config_byte(dev, 0x5a, ®5a);
421 if (((reg5a & 0x10) >> 4) != mask)
422 pci_write_config_byte(dev, 0x5a, mask ? (reg5a | 0x10) : (reg5a & ~0x10));
425 disable_irq(hwif->irq);
427 enable_irq(hwif->irq);
432 hwif->OUTB(mask ? (drive->ctl | 2) :
438 static int hpt366_config_drive_xfer_rate (ide_drive_t *drive)
440 ide_hwif_t *hwif = drive->hwif;
441 struct hd_driveid *id = drive->id;
443 drive->init_speed = 0;
445 if ((id->capability & 1) && drive->autodma) {
447 if (ide_use_dma(drive)) {
448 if (config_chipset_for_dma(drive))
449 return hwif->ide_dma_on(drive);
454 } else if ((id->capability & 8) || (id->field_valid & 2)) {
456 hpt3xx_tune_drive(drive, 5);
457 return hwif->ide_dma_off_quietly(drive);
459 /* IORDY not supported */
464 * This is specific to the HPT366 UDMA bios chipset
465 * by HighPoint|Triones Technologies, Inc.
467 static int hpt366_ide_dma_lostirq (ide_drive_t *drive)
469 struct pci_dev *dev = HWIF(drive)->pci_dev;
470 u8 reg50h = 0, reg52h = 0, reg5ah = 0;
472 pci_read_config_byte(dev, 0x50, ®50h);
473 pci_read_config_byte(dev, 0x52, ®52h);
474 pci_read_config_byte(dev, 0x5a, ®5ah);
475 printk("%s: (%s) reg50h=0x%02x, reg52h=0x%02x, reg5ah=0x%02x\n",
476 drive->name, __FUNCTION__, reg50h, reg52h, reg5ah);
478 pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
479 return __ide_dma_lostirq(drive);
482 static void hpt370_clear_engine (ide_drive_t *drive)
484 u8 regstate = HWIF(drive)->channel ? 0x54 : 0x50;
485 pci_write_config_byte(HWIF(drive)->pci_dev, regstate, 0x37);
489 static void hpt370_ide_dma_start(ide_drive_t *drive)
491 #ifdef HPT_RESET_STATE_ENGINE
492 hpt370_clear_engine(drive);
494 ide_dma_start(drive);
497 static int hpt370_ide_dma_end (ide_drive_t *drive)
499 ide_hwif_t *hwif = HWIF(drive);
500 u8 dma_stat = hwif->INB(hwif->dma_status);
502 if (dma_stat & 0x01) {
505 dma_stat = hwif->INB(hwif->dma_status);
507 if ((dma_stat & 0x01) != 0)
509 (void) HWIF(drive)->ide_dma_timeout(drive);
511 return __ide_dma_end(drive);
514 static void hpt370_lostirq_timeout (ide_drive_t *drive)
516 ide_hwif_t *hwif = HWIF(drive);
517 u8 bfifo = 0, reginfo = hwif->channel ? 0x56 : 0x52;
518 u8 dma_stat = 0, dma_cmd = 0;
520 pci_read_config_byte(HWIF(drive)->pci_dev, reginfo, &bfifo);
521 printk(KERN_DEBUG "%s: %d bytes in FIFO\n", drive->name, bfifo);
522 hpt370_clear_engine(drive);
523 /* get dma command mode */
524 dma_cmd = hwif->INB(hwif->dma_command);
526 hwif->OUTB(dma_cmd & ~0x1, hwif->dma_command);
527 dma_stat = hwif->INB(hwif->dma_status);
529 hwif->OUTB(dma_stat | 0x6, hwif->dma_status);
532 static int hpt370_ide_dma_timeout (ide_drive_t *drive)
534 hpt370_lostirq_timeout(drive);
535 hpt370_clear_engine(drive);
536 return __ide_dma_timeout(drive);
539 static int hpt370_ide_dma_lostirq (ide_drive_t *drive)
541 hpt370_lostirq_timeout(drive);
542 hpt370_clear_engine(drive);
543 return __ide_dma_lostirq(drive);
546 /* returns 1 if DMA IRQ issued, 0 otherwise */
547 static int hpt374_ide_dma_test_irq(ide_drive_t *drive)
549 ide_hwif_t *hwif = HWIF(drive);
551 u8 reginfo = hwif->channel ? 0x56 : 0x52;
554 pci_read_config_word(hwif->pci_dev, reginfo, &bfifo);
556 // printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
560 dma_stat = hwif->INB(hwif->dma_status);
561 /* return 1 if INTR asserted */
562 if ((dma_stat & 4) == 4)
565 if (!drive->waiting_for_dma)
566 printk(KERN_WARNING "%s: (%s) called while not waiting\n",
567 drive->name, __FUNCTION__);
571 static int hpt374_ide_dma_end (ide_drive_t *drive)
573 struct pci_dev *dev = HWIF(drive)->pci_dev;
574 ide_hwif_t *hwif = HWIF(drive);
575 u8 msc_stat = 0, mscreg = hwif->channel ? 0x54 : 0x50;
576 u8 bwsr_stat = 0, bwsr_mask = hwif->channel ? 0x02 : 0x01;
578 pci_read_config_byte(dev, 0x6a, &bwsr_stat);
579 pci_read_config_byte(dev, mscreg, &msc_stat);
580 if ((bwsr_stat & bwsr_mask) == bwsr_mask)
581 pci_write_config_byte(dev, mscreg, msc_stat|0x30);
582 return __ide_dma_end(drive);
586 * hpt372n_set_clock - perform clock switching dance
587 * @drive: Drive to switch
588 * @mode: Switching mode (0x21 for write, 0x23 otherwise)
590 * Switch the DPLL clock on the HPT372N devices. This is a
594 static void hpt372n_set_clock(ide_drive_t *drive, int mode)
596 ide_hwif_t *hwif = HWIF(drive);
598 /* FIXME: should we check for DMA active and BUG() */
599 /* Tristate the bus */
600 outb(0x80, hwif->dma_base+0x73);
601 outb(0x80, hwif->dma_base+0x77);
603 /* Switch clock and reset channels */
604 outb(mode, hwif->dma_base+0x7B);
605 outb(0xC0, hwif->dma_base+0x79);
607 /* Reset state machines */
608 outb(0x37, hwif->dma_base+0x70);
609 outb(0x37, hwif->dma_base+0x74);
612 outb(0x00, hwif->dma_base+0x79);
614 /* Reconnect channels to bus */
615 outb(0x00, hwif->dma_base+0x73);
616 outb(0x00, hwif->dma_base+0x77);
620 * hpt372n_rw_disk - wrapper for I/O
621 * @drive: drive for command
622 * @rq: block request structure
623 * @block: block number
625 * This is called when a disk I/O is issued to the 372N instead
626 * of the default functionality. We need it because of the clock
631 static ide_startstop_t hpt372n_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
635 if(rq_data_dir(rq) == READ)
640 if(HWIF(drive)->config_data != wantclock)
642 hpt372n_set_clock(drive, wantclock);
643 HWIF(drive)->config_data = wantclock;
645 return __ide_do_rw_disk(drive, rq, block);
649 * Since SUN Cobalt is attempting to do this operation, I should disclose
650 * this has been a long time ago Thu Jul 27 16:40:57 2000 was the patch date
651 * HOTSWAP ATA Infrastructure.
654 static void hpt3xx_reset (ide_drive_t *drive)
658 static int hpt3xx_tristate (ide_drive_t * drive, int state)
660 ide_hwif_t *hwif = HWIF(drive);
661 struct pci_dev *dev = hwif->pci_dev;
662 u8 reg59h = 0, reset = (hwif->channel) ? 0x80 : 0x40;
663 u8 regXXh = 0, state_reg= (hwif->channel) ? 0x57 : 0x53;
665 pci_read_config_byte(dev, 0x59, ®59h);
666 pci_read_config_byte(dev, state_reg, ®XXh);
669 (void) ide_do_reset(drive);
670 pci_write_config_byte(dev, state_reg, regXXh|0x80);
671 pci_write_config_byte(dev, 0x59, reg59h|reset);
673 pci_write_config_byte(dev, 0x59, reg59h & ~(reset));
674 pci_write_config_byte(dev, state_reg, regXXh & ~(0x80));
675 (void) ide_do_reset(drive);
681 * set/get power state for a drive.
682 * turning the power off does the following things:
683 * 1) soft-reset the drive
684 * 2) tri-states the ide bus
686 * when we turn things back on, we need to re-initialize things.
688 #define TRISTATE_BIT 0x8000
689 static int hpt370_busproc(ide_drive_t * drive, int state)
691 ide_hwif_t *hwif = drive->hwif;
692 struct pci_dev *dev = hwif->pci_dev;
693 u8 tristate = 0, resetmask = 0, bus_reg = 0;
696 hwif->bus_state = state;
699 /* secondary channel */
703 /* primary channel */
709 pci_read_config_word(dev, tristate, &tri_reg);
710 pci_read_config_byte(dev, 0x59, &bus_reg);
712 /* set the state. we don't set it if we don't need to do so.
713 * make sure that the drive knows that it has failed if it's off */
716 hwif->drives[0].failures = 0;
717 hwif->drives[1].failures = 0;
718 if ((bus_reg & resetmask) == 0)
720 tri_reg &= ~TRISTATE_BIT;
721 bus_reg &= ~resetmask;
724 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
725 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
726 if ((tri_reg & TRISTATE_BIT) == 0 && (bus_reg & resetmask))
728 tri_reg &= ~TRISTATE_BIT;
729 bus_reg |= resetmask;
731 case BUSSTATE_TRISTATE:
732 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
733 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
734 if ((tri_reg & TRISTATE_BIT) && (bus_reg & resetmask))
736 tri_reg |= TRISTATE_BIT;
737 bus_reg |= resetmask;
740 pci_write_config_byte(dev, 0x59, bus_reg);
741 pci_write_config_word(dev, tristate, tri_reg);
746 static void __devinit hpt366_clocking(ide_hwif_t *hwif)
749 struct hpt_info *info = ide_get_hwifdata(hwif);
751 pci_read_config_dword(hwif->pci_dev, 0x40, ®1);
753 /* detect bus speed by looking at control reg timing: */
754 switch((reg1 >> 8) & 7) {
756 info->speed = forty_base_hpt366;
759 info->speed = twenty_five_base_hpt366;
763 info->speed = thirty_three_base_hpt366;
768 static void __devinit hpt37x_clocking(ide_hwif_t *hwif)
770 struct hpt_info *info = ide_get_hwifdata(hwif);
771 struct pci_device *dev = hwif->pci_dev;
778 * default to pci clock. make sure MA15/16 are set to output
779 * to prevent drives having problems with 40-pin cables. Needed
780 * for some drives such as IBM-DTLA which will not enter ready
781 * state on reset when PDIAG is a input.
783 * ToDo: should we set 0x21 when using PLL mode ?
785 pci_write_config_byte(dev, 0x5b, 0x23);
788 * set up the PLL. we need to adjust it so that it's stable.
789 * freq = Tpll * 192 / Tpci
791 * Todo. For non x86 should probably check the dword is
792 * set to 0xABCDExxx indicating the BIOS saved f_CNT
794 pci_read_config_word(dev, 0x78, &freq);
798 * The 372N uses different PCI clock information and has
799 * some other complications
800 * On PCI33 timing we must clock switch
801 * On PCI66 timing we must NOT use the PCI clock
803 * Currently we always set up the PLL for the 372N
806 if(info->flags & IS_372N)
808 printk(KERN_INFO "hpt: HPT372N detected, using 372N timing.\n");
818 printk(KERN_INFO "FREQ: %d PLL: %d\n", freq, pll);
820 /* We always use the pll not the PCI clock on 372N */
833 if (pll == F_LOW_PCI_33) {
834 if (info->revision >= 8)
835 info->speed = thirty_three_base_hpt374;
836 else if (info->revision >= 5)
837 info->speed = thirty_three_base_hpt372;
838 else if (info->revision >= 4)
839 info->speed = thirty_three_base_hpt370a;
841 info->speed = thirty_three_base_hpt370;
842 printk(KERN_DEBUG "HPT37X: using 33MHz PCI clock\n");
843 } else if (pll == F_LOW_PCI_40) {
845 } else if (pll == F_LOW_PCI_50) {
846 if (info->revision >= 8)
847 info->speed = fifty_base_hpt370a;
848 else if (info->revision >= 5)
849 info->speed = fifty_base_hpt372;
850 else if (info->revision >= 4)
851 info->speed = fifty_base_hpt370a;
853 info->speed = fifty_base_hpt370a;
854 printk(KERN_DEBUG "HPT37X: using 50MHz PCI clock\n");
856 if (info->revision >= 8) {
857 printk(KERN_ERR "HPT37x: 66MHz timings are not supported.\n");
859 else if (info->revision >= 5)
860 info->speed = sixty_six_base_hpt372;
861 else if (info->revision >= 4)
862 info->speed = sixty_six_base_hpt370a;
864 info->speed = sixty_six_base_hpt370;
865 printk(KERN_DEBUG "HPT37X: using 66MHz PCI clock\n");
870 * only try the pll if we don't have a table for the clock
871 * speed that we're running at. NOTE: the internal PLL will
872 * result in slow reads when using a 33MHz PCI clock. we also
873 * don't like to use the PLL because it will cause glitches
874 * on PRST/SRST when the HPT state engine gets reset.
876 * ToDo: Use 66MHz PLL when ATA133 devices are present on a
877 * 372 device so we can get ATA133 support
880 goto init_hpt37X_done;
882 info->flags |= PLL_MODE;
885 * FIXME: make this work correctly, esp with 372N as per
886 * reference driver code.
888 * adjust PLL based upon PCI clock, enable it, and wait for
892 freq = (pll < F_LOW_PCI_50) ? 2 : 4;
893 while (adjust++ < 6) {
894 pci_write_config_dword(dev, 0x5c, (freq + pll) << 16 |
897 /* wait for clock stabilization */
898 for (i = 0; i < 0x50000; i++) {
899 pci_read_config_byte(dev, 0x5b, ®5bh);
901 /* spin looking for the clock to destabilize */
902 for (i = 0; i < 0x1000; ++i) {
903 pci_read_config_byte(dev, 0x5b,
905 if ((reg5bh & 0x80) == 0)
908 pci_read_config_dword(dev, 0x5c, &pll);
909 pci_write_config_dword(dev, 0x5c,
911 pci_write_config_byte(dev, 0x5b, 0x21);
912 if (info->revision >= 8)
913 info->speed = fifty_base_hpt370a;
914 else if (info->revision >= 5)
915 info->speed = fifty_base_hpt372;
916 else if (info->revision >= 4)
917 info->speed = fifty_base_hpt370a;
919 info->speed = fifty_base_hpt370a;
920 printk("HPT37X: using 50MHz internal PLL\n");
921 goto init_hpt37X_done;
926 pll -= (adjust >> 1);
928 pll += (adjust >> 1);
933 printk(KERN_ERR "HPT37X%s: unknown bus timing [%d %d].\n",
934 (info->flags & IS_372N)?"N":"", pll, freq);
935 /* reset state engine */
936 pci_write_config_byte(dev, 0x50, 0x37);
937 pci_write_config_byte(dev, 0x54, 0x37);
941 static int __devinit init_hpt37x(struct pci_dev *dev)
945 pci_read_config_byte(dev, 0x5a, ®5ah);
946 /* interrupt force enable */
947 pci_write_config_byte(dev, 0x5a, (reg5ah & ~0x10));
951 static int __devinit init_hpt366(struct pci_dev *dev)
957 * Disable the "fast interrupt" prediction.
959 pci_read_config_byte(dev, 0x51, &drive_fast);
960 if (drive_fast & 0x80)
961 pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
962 pci_read_config_dword(dev, 0x40, ®1);
967 static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const char *name)
970 /* FIXME: Not portable */
971 if (dev->resource[PCI_ROM_RESOURCE].start)
972 pci_write_config_byte(dev, PCI_ROM_ADDRESS,
973 dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
975 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
976 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
977 pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
978 pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
980 if (hpt_revision(dev) >= 3)
981 ret = init_hpt37x(dev);
983 ret = init_hpt366(dev);
991 static void __devinit init_hwif_hpt366(ide_hwif_t *hwif)
993 struct pci_dev *dev = hwif->pci_dev;
994 struct hpt_info *info = ide_get_hwifdata(hwif);
995 u8 ata66 = 0, regmask = (hwif->channel) ? 0x01 : 0x02;
997 hwif->tuneproc = &hpt3xx_tune_drive;
998 hwif->speedproc = &hpt3xx_tune_chipset;
999 hwif->quirkproc = &hpt3xx_quirkproc;
1000 hwif->intrproc = &hpt3xx_intrproc;
1001 hwif->maskproc = &hpt3xx_maskproc;
1003 if(info->flags & IS_372N)
1004 hwif->rw_disk = &hpt372n_rw_disk;
1007 * The HPT37x uses the CBLID pins as outputs for MA15/MA16
1008 * address lines to access an external eeprom. To read valid
1009 * cable detect state the pins must be enabled as inputs.
1011 if (info->revision >= 8 && (PCI_FUNC(dev->devfn) & 1)) {
1013 * HPT374 PCI function 1
1014 * - set bit 15 of reg 0x52 to enable TCBLID as input
1015 * - set bit 15 of reg 0x56 to enable FCBLID as input
1018 pci_read_config_word(dev, 0x52, &mcr3);
1019 pci_read_config_word(dev, 0x56, &mcr6);
1020 pci_write_config_word(dev, 0x52, mcr3 | 0x8000);
1021 pci_write_config_word(dev, 0x56, mcr6 | 0x8000);
1022 /* now read cable id register */
1023 pci_read_config_byte(dev, 0x5a, &ata66);
1024 pci_write_config_word(dev, 0x52, mcr3);
1025 pci_write_config_word(dev, 0x56, mcr6);
1026 } else if (info->revision >= 3) {
1028 * HPT370/372 and 374 pcifn 0
1029 * - clear bit 0 of 0x5b to enable P/SCBLID as inputs
1032 pci_read_config_byte(dev, 0x5b, &scr2);
1033 pci_write_config_byte(dev, 0x5b, scr2 & ~1);
1034 /* now read cable id register */
1035 pci_read_config_byte(dev, 0x5a, &ata66);
1036 pci_write_config_byte(dev, 0x5b, scr2);
1038 pci_read_config_byte(dev, 0x5a, &ata66);
1042 printk("HPT366: reg5ah=0x%02x ATA-%s Cable Port%d\n",
1043 ata66, (ata66 & regmask) ? "33" : "66",
1044 PCI_FUNC(hwif->pci_dev->devfn));
1047 #ifdef HPT_SERIALIZE_IO
1048 /* serialize access to this device */
1050 hwif->serialized = hwif->mate->serialized = 1;
1053 if (info->revision >= 3) {
1055 pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
1057 * set up ioctl for power status.
1058 * note: power affects both
1059 * drives on each channel
1061 hwif->resetproc = &hpt3xx_reset;
1062 hwif->busproc = &hpt370_busproc;
1063 } else if (info->revision >= 2) {
1064 hwif->resetproc = &hpt3xx_reset;
1065 hwif->busproc = &hpt3xx_tristate;
1067 hwif->resetproc = &hpt3xx_reset;
1068 hwif->busproc = &hpt3xx_tristate;
1071 if (!hwif->dma_base) {
1072 hwif->drives[0].autotune = 1;
1073 hwif->drives[1].autotune = 1;
1077 hwif->ultra_mask = 0x7f;
1078 hwif->mwdma_mask = 0x07;
1080 if (!(hwif->udma_four))
1081 hwif->udma_four = ((ata66 & regmask) ? 0 : 1);
1082 hwif->ide_dma_check = &hpt366_config_drive_xfer_rate;
1084 if (info->revision >= 8) {
1085 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1086 hwif->ide_dma_end = &hpt374_ide_dma_end;
1087 } else if (info->revision >= 5) {
1088 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1089 hwif->ide_dma_end = &hpt374_ide_dma_end;
1090 } else if (info->revision >= 3) {
1091 hwif->ide_dma_start = &hpt370_ide_dma_start;
1092 hwif->ide_dma_end = &hpt370_ide_dma_end;
1093 hwif->ide_dma_timeout = &hpt370_ide_dma_timeout;
1094 hwif->ide_dma_lostirq = &hpt370_ide_dma_lostirq;
1095 } else if (info->revision >= 2)
1096 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1098 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1102 hwif->drives[0].autodma = hwif->autodma;
1103 hwif->drives[1].autodma = hwif->autodma;
1106 static void __devinit init_dma_hpt366(ide_hwif_t *hwif, unsigned long dmabase)
1108 struct hpt_info *info = ide_get_hwifdata(hwif);
1109 u8 masterdma = 0, slavedma = 0;
1110 u8 dma_new = 0, dma_old = 0;
1111 u8 primary = hwif->channel ? 0x4b : 0x43;
1112 u8 secondary = hwif->channel ? 0x4f : 0x47;
1113 unsigned long flags;
1118 if(info->speed == NULL) {
1119 printk(KERN_WARNING "hpt: no known IDE timings, disabling DMA.\n");
1123 dma_old = hwif->INB(dmabase+2);
1125 local_irq_save(flags);
1128 pci_read_config_byte(hwif->pci_dev, primary, &masterdma);
1129 pci_read_config_byte(hwif->pci_dev, secondary, &slavedma);
1131 if (masterdma & 0x30) dma_new |= 0x20;
1132 if (slavedma & 0x30) dma_new |= 0x40;
1133 if (dma_new != dma_old)
1134 hwif->OUTB(dma_new, dmabase+2);
1136 local_irq_restore(flags);
1138 ide_setup_dma(hwif, dmabase, 8);
1142 * We "borrow" this hook in order to set the data structures
1143 * up early enough before dma or init_hwif calls are made.
1146 static void __devinit init_iops_hpt366(ide_hwif_t *hwif)
1148 struct hpt_info *info = kmalloc(sizeof(struct hpt_info), GFP_KERNEL);
1149 unsigned long dmabase = pci_resource_start(hwif->pci_dev, 4);
1153 printk(KERN_WARNING "hpt366: out of memory.\n");
1156 memset(info, 0, sizeof(struct hpt_info));
1157 ide_set_hwifdata(hwif, info);
1160 did = inb(dmabase + 0x22);
1161 rid = inb(dmabase + 0x28);
1163 if((did == 4 && rid == 6) || (did == 5 && rid > 1))
1164 info->flags |= IS_372N;
1167 info->revision = hpt_revision(hwif->pci_dev);
1169 if (info->revision >= 3)
1170 hpt37x_clocking(hwif);
1172 hpt366_clocking(hwif);
1176 static void __devinit init_setup_hpt374(struct pci_dev *dev, ide_pci_device_t *d)
1178 struct pci_dev *findev = NULL;
1180 if (PCI_FUNC(dev->devfn) & 1)
1183 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1184 if ((findev->vendor == dev->vendor) &&
1185 (findev->device == dev->device) &&
1186 ((findev->devfn - dev->devfn) == 1) &&
1187 (PCI_FUNC(findev->devfn) & 1)) {
1188 if (findev->irq != dev->irq) {
1189 /* FIXME: we need a core pci_set_interrupt() */
1190 findev->irq = dev->irq;
1191 printk(KERN_WARNING "%s: pci-config space interrupt "
1192 "fixed.\n", d->name);
1194 ide_setup_pci_devices(dev, findev, d);
1198 ide_setup_pci_device(dev, d);
1201 static void __devinit init_setup_hpt37x(struct pci_dev *dev, ide_pci_device_t *d)
1203 ide_setup_pci_device(dev, d);
1206 static void __devinit init_setup_hpt366(struct pci_dev *dev, ide_pci_device_t *d)
1208 struct pci_dev *findev = NULL;
1209 u8 pin1 = 0, pin2 = 0;
1210 unsigned int class_rev;
1211 char *chipset_names[] = {"HPT366", "HPT366", "HPT368",
1212 "HPT370", "HPT370A", "HPT372",
1215 if (PCI_FUNC(dev->devfn) & 1)
1218 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1221 if(dev->device == PCI_DEVICE_ID_TTI_HPT372N)
1225 d->name = chipset_names[class_rev];
1231 case 3: ide_setup_pci_device(dev, d);
1238 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin1);
1239 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1240 if ((findev->vendor == dev->vendor) &&
1241 (findev->device == dev->device) &&
1242 ((findev->devfn - dev->devfn) == 1) &&
1243 (PCI_FUNC(findev->devfn) & 1)) {
1244 pci_read_config_byte(findev, PCI_INTERRUPT_PIN, &pin2);
1245 if ((pin1 != pin2) && (dev->irq == findev->irq)) {
1246 d->bootable = ON_BOARD;
1247 printk("%s: onboard version of chipset, "
1248 "pin1=%d pin2=%d\n", d->name,
1251 ide_setup_pci_devices(dev, findev, d);
1255 ide_setup_pci_device(dev, d);
1260 * hpt366_init_one - called when an HPT366 is found
1261 * @dev: the hpt366 device
1262 * @id: the matching pci id
1264 * Called when the PCI registration layer (or the IDE initialization)
1265 * finds a device matching our IDE device tables.
1268 static int __devinit hpt366_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1270 ide_pci_device_t *d = &hpt366_chipsets[id->driver_data];
1272 d->init_setup(dev, d);
1276 static struct pci_device_id hpt366_pci_tbl[] = {
1277 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT366, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1278 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT372, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1279 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT302, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1280 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT371, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
1281 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT374, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
1282 { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT372N, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
1285 MODULE_DEVICE_TABLE(pci, hpt366_pci_tbl);
1287 static struct pci_driver driver = {
1288 .name = "HPT366_IDE",
1289 .id_table = hpt366_pci_tbl,
1290 .probe = hpt366_init_one,
1293 static int hpt366_ide_init(void)
1295 return ide_pci_register_driver(&driver);
1298 module_init(hpt366_ide_init);
1300 MODULE_AUTHOR("Andre Hedrick");
1301 MODULE_DESCRIPTION("PCI driver module for Highpoint HPT366 IDE");
1302 MODULE_LICENSE("GPL");