VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / ide / pci / hpt366.c
1 /*
2  * linux/drivers/ide/pci/hpt366.c               Version 0.36    April 25, 2003
3  *
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
7  *
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.
12  *
13  * Note that final HPT370 support was done by force extraction of GPL.
14  *
15  * - add function for getting/setting power status of drive
16  * - the HPT370's state machine can get confused. reset it before each dma 
17  *   xfer to prevent that from happening.
18  * - reset state engine whenever we get an error.
19  * - check for busmaster state at end of dma. 
20  * - use new highpoint timings.
21  * - detect bus speed using highpoint register.
22  * - use pll if we don't have a clock table. added a 66MHz table that's
23  *   just 2x the 33MHz table.
24  * - removed turnaround. NOTE: we never want to switch between pll and
25  *   pci clocks as the chip can glitch in those cases. the highpoint
26  *   approved workaround slows everything down too much to be useful. in
27  *   addition, we would have to serialize access to each chip.
28  *      Adrian Sun <a.sun@sun.com>
29  *
30  * add drive timings for 66MHz PCI bus,
31  * fix ATA Cable signal detection, fix incorrect /proc info
32  * add /proc display for per-drive PIO/DMA/UDMA mode and
33  * per-channel ATA-33/66 Cable detect.
34  *      Duncan Laurie <void@sun.com>
35  *
36  * fixup /proc output for multiple controllers
37  *      Tim Hockin <thockin@sun.com>
38  *
39  * On hpt366: 
40  * Reset the hpt366 on error, reset on dma
41  * Fix disabling Fast Interrupt hpt366.
42  *      Mike Waychison <crlf@sun.com>
43  *
44  * Added support for 372N clocking and clock switching. The 372N needs
45  * different clocks on read/write. This requires overloading rw_disk and
46  * other deeply crazy things. Thanks to <http://www.hoerstreich.de> for
47  * keeping me sane. 
48  *              Alan Cox <alan@redhat.com>
49  *
50  */
51
52
53 #include <linux/config.h>
54 #include <linux/types.h>
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/delay.h>
58 #include <linux/timer.h>
59 #include <linux/mm.h>
60 #include <linux/ioport.h>
61 #include <linux/blkdev.h>
62 #include <linux/hdreg.h>
63
64 #include <linux/interrupt.h>
65 #include <linux/pci.h>
66 #include <linux/init.h>
67 #include <linux/ide.h>
68
69 #include <asm/uaccess.h>
70 #include <asm/io.h>
71 #include <asm/irq.h>
72
73 #include "hpt366.h"
74
75 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
76 #include <linux/stat.h>
77 #include <linux/proc_fs.h>
78 #endif  /* defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS) */
79
80 static unsigned int hpt_revision(struct pci_dev *dev);
81 static unsigned int hpt_minimum_revision(struct pci_dev *dev, int revision);
82
83 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
84
85 static u8 hpt366_proc = 0;
86 static struct pci_dev *hpt_devs[HPT366_MAX_DEVS];
87 static int n_hpt_devs;
88
89 static int hpt366_get_info (char *buffer, char **addr, off_t offset, int count)
90 {
91         char *p = buffer;
92         char *chipset_nums[] = {"366", "366",  "368",
93                                 "370", "370A", "372",
94                                 "302", "371",  "374" };
95         int i, len;
96
97         p += sprintf(p, "\n                             "
98                 "HighPoint HPT366/368/370/372/374\n");
99         for (i = 0; i < n_hpt_devs; i++) {
100                 struct pci_dev *dev = hpt_devs[i];
101                 unsigned long iobase = dev->resource[4].start;
102                 u32 class_rev = hpt_revision(dev);
103                 u8 c0, c1;
104
105                 p += sprintf(p, "\nController: %d\n", i);
106                 p += sprintf(p, "Chipset: HPT%s\n", chipset_nums[class_rev]);
107                 p += sprintf(p, "--------------- Primary Channel "
108                                 "--------------- Secondary Channel "
109                                 "--------------\n");
110
111                 /* get the bus master status registers */
112                 c0 = inb(iobase + 0x2);
113                 c1 = inb(iobase + 0xa);
114                 p += sprintf(p, "Enabled:        %s"
115                                 "                             %s\n",
116                         (c0 & 0x80) ? "no" : "yes",
117                         (c1 & 0x80) ? "no" : "yes");
118 #if 0
119                 if (hpt_minimum_revision(dev, 3)) {
120                         u8 cbl;
121                         cbl = inb(iobase + 0x7b);
122                         outb(cbl | 1, iobase + 0x7b);
123                         outb(cbl & ~1, iobase + 0x7b);
124                         cbl = inb(iobase + 0x7a);
125                         p += sprintf(p, "Cable:          ATA-%d"
126                                         "                          ATA-%d\n",
127                                 (cbl & 0x02) ? 33 : 66,
128                                 (cbl & 0x01) ? 33 : 66);
129                         p += sprintf(p, "\n");
130                 }
131 #endif
132                 p += sprintf(p, "--------------- drive0 --------- drive1 "
133                                 "------- drive0 ---------- drive1 -------\n");
134                 p += sprintf(p, "DMA capable:    %s              %s" 
135                                 "            %s               %s\n",
136                         (c0 & 0x20) ? "yes" : "no ", 
137                         (c0 & 0x40) ? "yes" : "no ",
138                         (c1 & 0x20) ? "yes" : "no ", 
139                         (c1 & 0x40) ? "yes" : "no ");
140
141                 {
142                         u8 c2, c3;
143                         /* older revs don't have these registers mapped 
144                          * into io space */
145                         pci_read_config_byte(dev, 0x43, &c0);
146                         pci_read_config_byte(dev, 0x47, &c1);
147                         pci_read_config_byte(dev, 0x4b, &c2);
148                         pci_read_config_byte(dev, 0x4f, &c3);
149
150                         p += sprintf(p, "Mode:           %s             %s"
151                                         "           %s              %s\n",
152                                 (c0 & 0x10) ? "UDMA" : (c0 & 0x20) ? "DMA " : 
153                                         (c0 & 0x80) ? "PIO " : "off ",
154                                 (c1 & 0x10) ? "UDMA" : (c1 & 0x20) ? "DMA " :
155                                         (c1 & 0x80) ? "PIO " : "off ",
156                                 (c2 & 0x10) ? "UDMA" : (c2 & 0x20) ? "DMA " :
157                                         (c2 & 0x80) ? "PIO " : "off ",
158                                 (c3 & 0x10) ? "UDMA" : (c3 & 0x20) ? "DMA " :
159                                         (c3 & 0x80) ? "PIO " : "off ");
160                 }
161         }
162         p += sprintf(p, "\n");
163
164         /* p - buffer must be less than 4k! */
165         len = (p - buffer) - offset;
166         *addr = buffer + offset;
167         
168         return len > count ? count : len;
169 }
170 #endif  /* defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS) */
171
172 static u32 hpt_revision (struct pci_dev *dev)
173 {
174         u32 class_rev;
175         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
176         class_rev &= 0xff;
177
178         switch(dev->device) {
179                 /* Remap new 372N onto 372 */
180                 case PCI_DEVICE_ID_TTI_HPT372N:
181                         class_rev = PCI_DEVICE_ID_TTI_HPT372; break;
182                 case PCI_DEVICE_ID_TTI_HPT374:
183                         class_rev = PCI_DEVICE_ID_TTI_HPT374; break;
184                 case PCI_DEVICE_ID_TTI_HPT371:
185                         class_rev = PCI_DEVICE_ID_TTI_HPT371; break;
186                 case PCI_DEVICE_ID_TTI_HPT302:
187                         class_rev = PCI_DEVICE_ID_TTI_HPT302; break;
188                 case PCI_DEVICE_ID_TTI_HPT372:
189                         class_rev = PCI_DEVICE_ID_TTI_HPT372; break;
190                 default:
191                         break;
192         }
193         return class_rev;
194 }
195
196 static u32 hpt_minimum_revision (struct pci_dev *dev, int revision)
197 {
198         unsigned int class_rev = hpt_revision(dev);
199         revision--;
200         return ((int) (class_rev > revision) ? 1 : 0);
201 }
202
203 static int check_in_drive_lists(ide_drive_t *drive, const char **list);
204
205 static u8 hpt3xx_ratemask (ide_drive_t *drive)
206 {
207         struct pci_dev *dev     = HWIF(drive)->pci_dev;
208         u8 mode                 = 0;
209
210         if (hpt_minimum_revision(dev, 8)) {             /* HPT374 */
211                 mode = (HPT374_ALLOW_ATA133_6) ? 4 : 3;
212         } else if (hpt_minimum_revision(dev, 7)) {      /* HPT371 */
213                 mode = (HPT371_ALLOW_ATA133_6) ? 4 : 3;
214         } else if (hpt_minimum_revision(dev, 6)) {      /* HPT302 */
215                 mode = (HPT302_ALLOW_ATA133_6) ? 4 : 3;
216         } else if (hpt_minimum_revision(dev, 5)) {      /* HPT372 */
217                 mode = (HPT372_ALLOW_ATA133_6) ? 4 : 3;
218         } else if (hpt_minimum_revision(dev, 4)) {      /* HPT370A */
219                 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
220         } else if (hpt_minimum_revision(dev, 3)) {      /* HPT370 */
221                 mode = (HPT370_ALLOW_ATA100_5) ? 3 : 2;
222                 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : mode;
223         } else {                                /* HPT366 and HPT368 */
224                 mode = (check_in_drive_lists(drive, bad_ata33)) ? 0 : 2;
225         }
226         if (!eighty_ninty_three(drive) && (mode))
227                 mode = min(mode, (u8)1);
228         return mode;
229 }
230
231 /*
232  *      Note for the future; the SATA hpt37x we must set
233  *      either PIO or UDMA modes 0,4,5
234  */
235  
236 static u8 hpt3xx_ratefilter (ide_drive_t *drive, u8 speed)
237 {
238         struct pci_dev *dev     = HWIF(drive)->pci_dev;
239         u8 mode                 = hpt3xx_ratemask(drive);
240
241         if (drive->media != ide_disk)
242                 return min(speed, (u8)XFER_PIO_4);
243
244         switch(mode) {
245                 case 0x04:
246                         speed = min(speed, (u8)XFER_UDMA_6);
247                         break;
248                 case 0x03:
249                         speed = min(speed, (u8)XFER_UDMA_5);
250                         if (hpt_minimum_revision(dev, 5))
251                                 break;
252                         if (check_in_drive_lists(drive, bad_ata100_5))
253                                 speed = min(speed, (u8)XFER_UDMA_4);
254                         break;
255                 case 0x02:
256                         speed = min(speed, (u8)XFER_UDMA_4);
257         /*
258          * CHECK ME, Does this need to be set to 5 ??
259          */
260                         if (hpt_minimum_revision(dev, 3))
261                                 break;
262                         if ((check_in_drive_lists(drive, bad_ata66_4)) ||
263                             (!(HPT366_ALLOW_ATA66_4)))
264                                 speed = min(speed, (u8)XFER_UDMA_3);
265                         if ((check_in_drive_lists(drive, bad_ata66_3)) ||
266                             (!(HPT366_ALLOW_ATA66_3)))
267                                 speed = min(speed, (u8)XFER_UDMA_2);
268                         break;
269                 case 0x01:
270                         speed = min(speed, (u8)XFER_UDMA_2);
271         /*
272          * CHECK ME, Does this need to be set to 5 ??
273          */
274                         if (hpt_minimum_revision(dev, 3))
275                                 break;
276                         if (check_in_drive_lists(drive, bad_ata33))
277                                 speed = min(speed, (u8)XFER_MW_DMA_2);
278                         break;
279                 case 0x00:
280                 default:
281                         speed = min(speed, (u8)XFER_MW_DMA_2);
282                         break;
283         }
284         return speed;
285 }
286
287 static int check_in_drive_lists (ide_drive_t *drive, const char **list)
288 {
289         struct hd_driveid *id = drive->id;
290
291         if (quirk_drives == list) {
292                 while (*list)
293                         if (strstr(id->model, *list++))
294                                 return 1;
295         } else {
296                 while (*list)
297                         if (!strcmp(*list++,id->model))
298                                 return 1;
299         }
300         return 0;
301 }
302
303 static unsigned int pci_bus_clock_list (u8 speed, struct chipset_bus_clock_list_entry * chipset_table)
304 {
305         for ( ; chipset_table->xfer_speed ; chipset_table++)
306                 if (chipset_table->xfer_speed == speed)
307                         return chipset_table->chipset_settings;
308         return chipset_table->chipset_settings;
309 }
310
311 static int hpt36x_tune_chipset(ide_drive_t *drive, u8 xferspeed)
312 {
313         struct pci_dev *dev     = HWIF(drive)->pci_dev;
314         u8 speed                = hpt3xx_ratefilter(drive, xferspeed);
315 //      u8 speed                = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
316         u8 regtime              = (drive->select.b.unit & 0x01) ? 0x44 : 0x40;
317         u8 regfast              = (HWIF(drive)->channel) ? 0x55 : 0x51;
318         u8 drive_fast           = 0;
319         u32 reg1 = 0, reg2      = 0;
320
321         /*
322          * Disable the "fast interrupt" prediction.
323          */
324         pci_read_config_byte(dev, regfast, &drive_fast);
325 #if 0
326         if (drive_fast & 0x02)
327                 pci_write_config_byte(dev, regfast, drive_fast & ~0x20);
328 #else
329         if (drive_fast & 0x80)
330                 pci_write_config_byte(dev, regfast, drive_fast & ~0x80);
331 #endif
332
333         reg2 = pci_bus_clock_list(speed,
334                 (struct chipset_bus_clock_list_entry *) pci_get_drvdata(dev));
335         /*
336          * Disable on-chip PIO FIFO/buffer
337          *  (to avoid problems handling I/O errors later)
338          */
339         pci_read_config_dword(dev, regtime, &reg1);
340         if (speed >= XFER_MW_DMA_0) {
341                 reg2 = (reg2 & ~0xc0000000) | (reg1 & 0xc0000000);
342         } else {
343                 reg2 = (reg2 & ~0x30070000) | (reg1 & 0x30070000);
344         }       
345         reg2 &= ~0x80000000;
346
347         pci_write_config_dword(dev, regtime, reg2);
348
349         return ide_config_drive_speed(drive, speed);
350 }
351
352 static int hpt370_tune_chipset(ide_drive_t *drive, u8 xferspeed)
353 {
354         struct pci_dev *dev = HWIF(drive)->pci_dev;
355         u8 speed        = hpt3xx_ratefilter(drive, xferspeed);
356 //      u8 speed        = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
357         u8 regfast      = (HWIF(drive)->channel) ? 0x55 : 0x51;
358         u8 drive_pci    = 0x40 + (drive->dn * 4);
359         u8 new_fast     = 0, drive_fast = 0;
360         u32 list_conf   = 0, drive_conf = 0;
361         u32 conf_mask   = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
362
363         /*
364          * Disable the "fast interrupt" prediction.
365          * don't holdoff on interrupts. (== 0x01 despite what the docs say) 
366          */
367         pci_read_config_byte(dev, regfast, &drive_fast);
368         new_fast = drive_fast;
369         if (new_fast & 0x02)
370                 new_fast &= ~0x02;
371
372 #ifdef HPT_DELAY_INTERRUPT
373         if (new_fast & 0x01)
374                 new_fast &= ~0x01;
375 #else
376         if ((new_fast & 0x01) == 0)
377                 new_fast |= 0x01;
378 #endif
379         if (new_fast != drive_fast)
380                 pci_write_config_byte(dev, regfast, new_fast);
381
382         list_conf = pci_bus_clock_list(speed, 
383                                        (struct chipset_bus_clock_list_entry *)
384                                        pci_get_drvdata(dev));
385
386         pci_read_config_dword(dev, drive_pci, &drive_conf);
387         list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
388         
389         if (speed < XFER_MW_DMA_0) {
390                 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
391         }
392
393         pci_write_config_dword(dev, drive_pci, list_conf);
394
395         return ide_config_drive_speed(drive, speed);
396 }
397
398 static int hpt372_tune_chipset(ide_drive_t *drive, u8 xferspeed)
399 {
400         struct pci_dev *dev     = HWIF(drive)->pci_dev;
401         u8 speed        = hpt3xx_ratefilter(drive, xferspeed);
402 //      u8 speed        = ide_rate_filter(hpt3xx_ratemask(drive), xferspeed);
403         u8 regfast      = (HWIF(drive)->channel) ? 0x55 : 0x51;
404         u8 drive_fast   = 0, drive_pci = 0x40 + (drive->dn * 4);
405         u32 list_conf   = 0, drive_conf = 0;
406         u32 conf_mask   = (speed >= XFER_MW_DMA_0) ? 0xc0000000 : 0x30070000;
407
408         /*
409          * Disable the "fast interrupt" prediction.
410          * don't holdoff on interrupts. (== 0x01 despite what the docs say)
411          */
412         pci_read_config_byte(dev, regfast, &drive_fast);
413         drive_fast &= ~0x07;
414         pci_write_config_byte(dev, regfast, drive_fast);
415                                         
416         list_conf = pci_bus_clock_list(speed,
417                         (struct chipset_bus_clock_list_entry *)
418                                         pci_get_drvdata(dev));
419         pci_read_config_dword(dev, drive_pci, &drive_conf);
420         list_conf = (list_conf & ~conf_mask) | (drive_conf & conf_mask);
421         if (speed < XFER_MW_DMA_0)
422                 list_conf &= ~0x80000000; /* Disable on-chip PIO FIFO/buffer */
423         pci_write_config_dword(dev, drive_pci, list_conf);
424
425         return ide_config_drive_speed(drive, speed);
426 }
427
428 static int hpt3xx_tune_chipset (ide_drive_t *drive, u8 speed)
429 {
430         struct pci_dev *dev     = HWIF(drive)->pci_dev;
431
432         if (hpt_minimum_revision(dev, 8))
433                 return hpt372_tune_chipset(drive, speed); /* not a typo */
434 #if 0
435         else if (hpt_minimum_revision(dev, 7))
436                 hpt371_tune_chipset(drive, speed);
437         else if (hpt_minimum_revision(dev, 6))
438                 hpt302_tune_chipset(drive, speed);
439 #endif
440         else if (hpt_minimum_revision(dev, 5))
441                 return hpt372_tune_chipset(drive, speed);
442         else if (hpt_minimum_revision(dev, 3))
443                 return hpt370_tune_chipset(drive, speed);
444         else    /* hpt368: hpt_minimum_revision(dev, 2) */
445                 return hpt36x_tune_chipset(drive, speed);
446 }
447
448 static void hpt3xx_tune_drive (ide_drive_t *drive, u8 pio)
449 {
450         pio = ide_get_best_pio_mode(drive, 255, pio, NULL);
451         (void) hpt3xx_tune_chipset(drive, (XFER_PIO_0 + pio));
452 }
453
454 /*
455  * This allows the configuration of ide_pci chipset registers
456  * for cards that learn about the drive's UDMA, DMA, PIO capabilities
457  * after the drive is reported by the OS.  Initially for designed for
458  * HPT366 UDMA chipset by HighPoint|Triones Technologies, Inc.
459  *
460  * check_in_drive_lists(drive, bad_ata66_4)
461  * check_in_drive_lists(drive, bad_ata66_3)
462  * check_in_drive_lists(drive, bad_ata33)
463  *
464  */
465 static int config_chipset_for_dma (ide_drive_t *drive)
466 {
467         u8 speed = ide_dma_speed(drive, hpt3xx_ratemask(drive));
468
469         if (!(speed))
470                 return 0;
471
472         (void) hpt3xx_tune_chipset(drive, speed);
473         return ide_dma_enable(drive);
474 }
475
476 static int hpt3xx_quirkproc (ide_drive_t *drive)
477 {
478         return ((int) check_in_drive_lists(drive, quirk_drives));
479 }
480
481 static void hpt3xx_intrproc (ide_drive_t *drive)
482 {
483         ide_hwif_t *hwif = HWIF(drive);
484
485         if (drive->quirk_list)
486                 return;
487         /* drives in the quirk_list may not like intr setups/cleanups */
488         hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
489 }
490
491 static void hpt3xx_maskproc (ide_drive_t *drive, int mask)
492 {
493         struct pci_dev *dev = HWIF(drive)->pci_dev;
494
495         if (drive->quirk_list) {
496                 if (hpt_minimum_revision(dev,3)) {
497                         u8 reg5a = 0;
498                         pci_read_config_byte(dev, 0x5a, &reg5a);
499                         if (((reg5a & 0x10) >> 4) != mask)
500                                 pci_write_config_byte(dev, 0x5a, mask ? (reg5a | 0x10) : (reg5a & ~0x10));
501                 } else {
502                         if (mask) {
503                                 disable_irq(HWIF(drive)->irq);
504                         } else {
505                                 enable_irq(HWIF(drive)->irq);
506                         }
507                 }
508         } else {
509                 if (IDE_CONTROL_REG)
510                         HWIF(drive)->OUTB(mask ? (drive->ctl | 2) :
511                                                  (drive->ctl & ~2),
512                                                  IDE_CONTROL_REG);
513         }
514 }
515
516 static int hpt366_config_drive_xfer_rate (ide_drive_t *drive)
517 {
518         ide_hwif_t *hwif        = HWIF(drive);
519         struct hd_driveid *id   = drive->id;
520
521         drive->init_speed = 0;
522
523         if (id && (id->capability & 1) && drive->autodma) {
524                 /* Consult the list of known "bad" drives */
525                 if (__ide_dma_bad_drive(drive))
526                         goto fast_ata_pio;
527                 if (id->field_valid & 4) {
528                         if (id->dma_ultra & hwif->ultra_mask) {
529                                 /* Force if Capable UltraDMA */
530                                 int dma = config_chipset_for_dma(drive);
531                                 if ((id->field_valid & 2) && !dma)
532                                         goto try_dma_modes;
533                         }
534                 } else if (id->field_valid & 2) {
535 try_dma_modes:
536                         if (id->dma_mword & hwif->mwdma_mask) {
537                                 /* Force if Capable regular DMA modes */
538                                 if (!config_chipset_for_dma(drive))
539                                         goto no_dma_set;
540                         }
541                 } else if (__ide_dma_good_drive(drive) &&
542                            (id->eide_dma_time < 150)) {
543                         /* Consult the list of known "good" drives */
544                         if (!config_chipset_for_dma(drive))
545                                 goto no_dma_set;
546                 } else {
547                         goto fast_ata_pio;
548                 }
549                 return hwif->ide_dma_on(drive);
550         } else if ((id->capability & 8) || (id->field_valid & 2)) {
551 fast_ata_pio:
552 no_dma_set:
553                 hpt3xx_tune_drive(drive, 5);
554                 return hwif->ide_dma_off_quietly(drive);
555         }
556         /* IORDY not supported */
557         return 0;
558 }
559
560 /*
561  * This is specific to the HPT366 UDMA bios chipset
562  * by HighPoint|Triones Technologies, Inc.
563  */
564 static int hpt366_ide_dma_lostirq (ide_drive_t *drive)
565 {
566         struct pci_dev *dev     = HWIF(drive)->pci_dev;
567         u8 reg50h = 0, reg52h = 0, reg5ah = 0;
568
569         pci_read_config_byte(dev, 0x50, &reg50h);
570         pci_read_config_byte(dev, 0x52, &reg52h);
571         pci_read_config_byte(dev, 0x5a, &reg5ah);
572         printk("%s: (%s)  reg50h=0x%02x, reg52h=0x%02x, reg5ah=0x%02x\n",
573                 drive->name, __FUNCTION__, reg50h, reg52h, reg5ah);
574         if (reg5ah & 0x10)
575                 pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
576 #if 0
577         /* how about we flush and reset, mmmkay? */
578         pci_write_config_byte(dev, 0x51, 0x1F);
579         /* fall through to a reset */
580         case ide_dma_begin:
581         case ide_dma_end:
582         /* reset the chips state over and over.. */
583         pci_write_config_byte(dev, 0x51, 0x13);
584 #endif
585         return __ide_dma_lostirq(drive);
586 }
587
588 static void hpt370_clear_engine (ide_drive_t *drive)
589 {
590         u8 regstate = HWIF(drive)->channel ? 0x54 : 0x50;
591         pci_write_config_byte(HWIF(drive)->pci_dev, regstate, 0x37);
592         udelay(10);
593 }
594
595 static int hpt370_ide_dma_begin (ide_drive_t *drive)
596 {
597 #ifdef HPT_RESET_STATE_ENGINE
598         hpt370_clear_engine(drive);
599 #endif
600         return __ide_dma_begin(drive);
601 }
602
603 static int hpt370_ide_dma_end (ide_drive_t *drive)
604 {
605         ide_hwif_t *hwif        = HWIF(drive);
606         u8 dma_stat             = hwif->INB(hwif->dma_status);
607
608         if (dma_stat & 0x01) {
609                 /* wait a little */
610                 udelay(20);
611                 dma_stat = hwif->INB(hwif->dma_status);
612         }
613         if ((dma_stat & 0x01) != 0) 
614                 /* fallthrough */
615                 (void) HWIF(drive)->ide_dma_timeout(drive);
616
617         return __ide_dma_end(drive);
618 }
619
620 static void hpt370_lostirq_timeout (ide_drive_t *drive)
621 {
622         ide_hwif_t *hwif        = HWIF(drive);
623         u8 bfifo = 0, reginfo   = hwif->channel ? 0x56 : 0x52;
624         u8 dma_stat = 0, dma_cmd = 0;
625
626         pci_read_config_byte(HWIF(drive)->pci_dev, reginfo, &bfifo);
627         printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
628         hpt370_clear_engine(drive);
629         /* get dma command mode */
630         dma_cmd = hwif->INB(hwif->dma_command);
631         /* stop dma */
632         hwif->OUTB(dma_cmd & ~0x1, hwif->dma_command);
633         dma_stat = hwif->INB(hwif->dma_status);
634         /* clear errors */
635         hwif->OUTB(dma_stat | 0x6, hwif->dma_status);
636 }
637
638 static int hpt370_ide_dma_timeout (ide_drive_t *drive)
639 {
640         hpt370_lostirq_timeout(drive);
641         hpt370_clear_engine(drive);
642         return __ide_dma_timeout(drive);
643 }
644
645 static int hpt370_ide_dma_lostirq (ide_drive_t *drive)
646 {
647         hpt370_lostirq_timeout(drive);
648         hpt370_clear_engine(drive);
649         return __ide_dma_lostirq(drive);
650 }
651
652 /* returns 1 if DMA IRQ issued, 0 otherwise */
653 static int hpt374_ide_dma_test_irq(ide_drive_t *drive)
654 {
655         ide_hwif_t *hwif        = HWIF(drive);
656         u16 bfifo               = 0;
657         u8 reginfo              = hwif->channel ? 0x56 : 0x52;
658         u8 dma_stat;
659
660         pci_read_config_word(hwif->pci_dev, reginfo, &bfifo);
661         if (bfifo & 0x1FF) {
662 //              printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
663                 return 0;
664         }
665
666         dma_stat = hwif->INB(hwif->dma_status);
667         /* return 1 if INTR asserted */
668         if ((dma_stat & 4) == 4)
669                 return 1;
670
671         if (!drive->waiting_for_dma)
672                 printk(KERN_WARNING "%s: (%s) called while not waiting\n",
673                                 drive->name, __FUNCTION__);
674         return 0;
675 }
676
677 static int hpt374_ide_dma_end (ide_drive_t *drive)
678 {
679         struct pci_dev *dev     = HWIF(drive)->pci_dev;
680         ide_hwif_t *hwif        = HWIF(drive);
681         u8 msc_stat = 0, mscreg = hwif->channel ? 0x54 : 0x50;
682         u8 bwsr_stat = 0, bwsr_mask = hwif->channel ? 0x02 : 0x01;
683
684         pci_read_config_byte(dev, 0x6a, &bwsr_stat);
685         pci_read_config_byte(dev, mscreg, &msc_stat);
686         if ((bwsr_stat & bwsr_mask) == bwsr_mask)
687                 pci_write_config_byte(dev, mscreg, msc_stat|0x30);
688         return __ide_dma_end(drive);
689 }
690
691 /**
692  *      hpt372n_set_clock       -       perform clock switching dance
693  *      @drive: Drive to switch
694  *      @mode: Switching mode (0x21 for write, 0x23 otherwise)
695  *
696  *      Switch the DPLL clock on the HPT372N devices. This is a
697  *      right mess.
698  */
699  
700 static void hpt372n_set_clock(ide_drive_t *drive, int mode)
701 {
702         ide_hwif_t *hwif        = HWIF(drive);
703         
704         /* FIXME: should we check for DMA active and BUG() */
705         /* Tristate the bus */
706         outb(0x80, hwif->dma_base+0x73);
707         outb(0x80, hwif->dma_base+0x77);
708         
709         /* Switch clock and reset channels */
710         outb(mode, hwif->dma_base+0x7B);
711         outb(0xC0, hwif->dma_base+0x79);
712         
713         /* Reset state machines */
714         outb(0x37, hwif->dma_base+0x70);
715         outb(0x37, hwif->dma_base+0x74);
716         
717         /* Complete reset */
718         outb(0x00, hwif->dma_base+0x79);
719         
720         /* Reconnect channels to bus */
721         outb(0x00, hwif->dma_base+0x73);
722         outb(0x00, hwif->dma_base+0x77);
723 }
724
725 /**
726  *      hpt372n_rw_disk         -       wrapper for I/O
727  *      @drive: drive for command
728  *      @rq: block request structure
729  *      @block: block number
730  *
731  *      This is called when a disk I/O is issued to the 372N instead
732  *      of the default functionality. We need it because of the clock
733  *      switching
734  *
735  */
736  
737 static ide_startstop_t hpt372n_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
738 {
739         int wantclock;
740         
741         if(rq_data_dir(rq) == READ)
742                 wantclock = 0x21;
743         else
744                 wantclock = 0x23;
745                 
746         if(HWIF(drive)->config_data != wantclock)
747         {
748                 hpt372n_set_clock(drive, wantclock);
749                 HWIF(drive)->config_data = wantclock;
750         }
751         return __ide_do_rw_disk(drive, rq, block);
752 }
753
754 /*
755  * Since SUN Cobalt is attempting to do this operation, I should disclose
756  * this has been a long time ago Thu Jul 27 16:40:57 2000 was the patch date
757  * HOTSWAP ATA Infrastructure.
758  */
759
760 static void hpt3xx_reset (ide_drive_t *drive)
761 {
762 #if 0
763         unsigned long high_16   = pci_resource_start(HWIF(drive)->pci_dev, 4);
764         u8 reset        = (HWIF(drive)->channel) ? 0x80 : 0x40;
765         u8 reg59h       = 0;
766
767         pci_read_config_byte(HWIF(drive)->pci_dev, 0x59, &reg59h);
768         pci_write_config_byte(HWIF(drive)->pci_dev, 0x59, reg59h|reset);
769         pci_write_config_byte(HWIF(drive)->pci_dev, 0x59, reg59h);
770 #endif
771 }
772
773 static int hpt3xx_tristate (ide_drive_t * drive, int state)
774 {
775         ide_hwif_t *hwif        = HWIF(drive);
776         struct pci_dev *dev     = hwif->pci_dev;
777         u8 reg59h = 0, reset    = (hwif->channel) ? 0x80 : 0x40;
778         u8 regXXh = 0, state_reg= (hwif->channel) ? 0x57 : 0x53;
779
780         if (!hwif)
781                 return -EINVAL;
782
783 //      hwif->bus_state = state;
784
785         pci_read_config_byte(dev, 0x59, &reg59h);
786         pci_read_config_byte(dev, state_reg, &regXXh);
787
788         if (state) {
789                 (void) ide_do_reset(drive);
790                 pci_write_config_byte(dev, state_reg, regXXh|0x80);
791                 pci_write_config_byte(dev, 0x59, reg59h|reset);
792         } else {
793                 pci_write_config_byte(dev, 0x59, reg59h & ~(reset));
794                 pci_write_config_byte(dev, state_reg, regXXh & ~(0x80));
795                 (void) ide_do_reset(drive);
796         }
797         return 0;
798 }
799
800 /* 
801  * set/get power state for a drive.
802  * turning the power off does the following things:
803  *   1) soft-reset the drive
804  *   2) tri-states the ide bus
805  *
806  * when we turn things back on, we need to re-initialize things.
807  */
808 #define TRISTATE_BIT  0x8000
809 static int hpt370_busproc(ide_drive_t * drive, int state)
810 {
811         ide_hwif_t *hwif        = HWIF(drive);
812         struct pci_dev *dev     = hwif->pci_dev;
813         u8 tristate = 0, resetmask = 0, bus_reg = 0;
814         u16 tri_reg;
815
816         if (!hwif)
817                 return -EINVAL;
818
819         hwif->bus_state = state;
820
821         if (hwif->channel) { 
822                 /* secondary channel */
823                 tristate = 0x56;
824                 resetmask = 0x80; 
825         } else { 
826                 /* primary channel */
827                 tristate = 0x52;
828                 resetmask = 0x40;
829         }
830
831         /* grab status */
832         pci_read_config_word(dev, tristate, &tri_reg);
833         pci_read_config_byte(dev, 0x59, &bus_reg);
834
835         /* set the state. we don't set it if we don't need to do so.
836          * make sure that the drive knows that it has failed if it's off */
837         switch (state) {
838         case BUSSTATE_ON:
839                 hwif->drives[0].failures = 0;
840                 hwif->drives[1].failures = 0;
841                 if ((bus_reg & resetmask) == 0)
842                         return 0;
843                 tri_reg &= ~TRISTATE_BIT;
844                 bus_reg &= ~resetmask;
845                 break;
846         case BUSSTATE_OFF:
847                 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
848                 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
849                 if ((tri_reg & TRISTATE_BIT) == 0 && (bus_reg & resetmask))
850                         return 0;
851                 tri_reg &= ~TRISTATE_BIT;
852                 bus_reg |= resetmask;
853                 break;
854         case BUSSTATE_TRISTATE:
855                 hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
856                 hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
857                 if ((tri_reg & TRISTATE_BIT) && (bus_reg & resetmask))
858                         return 0;
859                 tri_reg |= TRISTATE_BIT;
860                 bus_reg |= resetmask;
861                 break;
862         }
863         pci_write_config_byte(dev, 0x59, bus_reg);
864         pci_write_config_word(dev, tristate, tri_reg);
865
866         return 0;
867 }
868
869 static int __devinit init_hpt37x(struct pci_dev *dev)
870 {
871         int adjust, i;
872         u16 freq;
873         u32 pll;
874         u8 reg5bh;
875         u8 reg5ah = 0;
876         unsigned long dmabase = pci_resource_start(dev, 4);
877         u8 did, rid;    
878         int is_372n = 0;
879         
880         pci_read_config_byte(dev, 0x5a, &reg5ah);
881         /* interrupt force enable */
882         pci_write_config_byte(dev, 0x5a, (reg5ah & ~0x10));
883
884         if(dmabase)
885         {
886                 did = inb(dmabase + 0x22);
887                 rid = inb(dmabase + 0x28);
888         
889                 if((did == 4 && rid == 6) || (did == 5 && rid > 1))
890                         is_372n = 1;
891         }
892
893         /*
894          * default to pci clock. make sure MA15/16 are set to output
895          * to prevent drives having problems with 40-pin cables.
896          */
897         pci_write_config_byte(dev, 0x5b, 0x23);
898
899         /*
900          * set up the PLL. we need to adjust it so that it's stable. 
901          * freq = Tpll * 192 / Tpci
902          *
903          * Todo. For non x86 should probably check the dword is
904          * set to 0xABCDExxx indicating the BIOS saved f_CNT
905          */
906         pci_read_config_word(dev, 0x78, &freq);
907         freq &= 0x1FF;
908         
909         /*
910          * The 372N uses different PCI clock information and has
911          * some other complications
912          *      On PCI33 timing we must clock switch
913          *      On PCI66 timing we must NOT use the PCI clock
914          *
915          * Currently we always set up the PLL for the 372N
916          */
917          
918         pci_set_drvdata(dev, NULL);
919         
920         if(is_372n)
921         {
922                 printk(KERN_INFO "hpt: HPT372N detected, using 372N timing.\n");
923                 if(freq < 0x55)
924                         pll = F_LOW_PCI_33;
925                 else if(freq < 0x70)
926                         pll = F_LOW_PCI_40;
927                 else if(freq < 0x7F)
928                         pll = F_LOW_PCI_50;
929                 else
930                         pll = F_LOW_PCI_66;
931                         
932                 printk(KERN_INFO "FREQ: %d PLL: %d\n", freq, pll);
933                         
934                 /* We always use the pll not the PCI clock on 372N */
935         }
936         else
937         {
938                 if(freq < 0x9C)
939                         pll = F_LOW_PCI_33;
940                 else if(freq < 0xb0)
941                         pll = F_LOW_PCI_40;
942                 else if(freq <0xc8)
943                         pll = F_LOW_PCI_50;
944                 else
945                         pll = F_LOW_PCI_66;
946         
947                 if (pll == F_LOW_PCI_33) {
948                         if (hpt_minimum_revision(dev,8))
949                                 pci_set_drvdata(dev, (void *) thirty_three_base_hpt374);
950                         else if (hpt_minimum_revision(dev,5))
951                                 pci_set_drvdata(dev, (void *) thirty_three_base_hpt372);
952                         else if (hpt_minimum_revision(dev,4))
953                                 pci_set_drvdata(dev, (void *) thirty_three_base_hpt370a);
954                         else
955                                 pci_set_drvdata(dev, (void *) thirty_three_base_hpt370);
956                         printk("HPT37X: using 33MHz PCI clock\n");
957                 } else if (pll == F_LOW_PCI_40) {
958                         /* Unsupported */
959                 } else if (pll == F_LOW_PCI_50) {
960                         if (hpt_minimum_revision(dev,8))
961                                 pci_set_drvdata(dev, NULL);
962                         else if (hpt_minimum_revision(dev,5))
963                                 pci_set_drvdata(dev, (void *) fifty_base_hpt372);
964                         else if (hpt_minimum_revision(dev,4))
965                                 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
966                         else
967                                 pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
968                         printk("HPT37X: using 50MHz PCI clock\n");
969                 } else {
970                         if (hpt_minimum_revision(dev,8))
971                         {
972                                 printk(KERN_ERR "HPT37x: 66MHz timings are not supported.\n");
973                         }
974                         else if (hpt_minimum_revision(dev,5))
975                                 pci_set_drvdata(dev, (void *) sixty_six_base_hpt372);
976                         else if (hpt_minimum_revision(dev,4))
977                                 pci_set_drvdata(dev, (void *) sixty_six_base_hpt370a);
978                         else
979                                 pci_set_drvdata(dev, (void *) sixty_six_base_hpt370);
980                         printk("HPT37X: using 66MHz PCI clock\n");
981                 }
982         }
983         
984         /*
985          * only try the pll if we don't have a table for the clock
986          * speed that we're running at. NOTE: the internal PLL will
987          * result in slow reads when using a 33MHz PCI clock. we also
988          * don't like to use the PLL because it will cause glitches
989          * on PRST/SRST when the HPT state engine gets reset.
990          */
991         if (pci_get_drvdata(dev)) 
992                 goto init_hpt37X_done;
993         
994         if (hpt_minimum_revision(dev,8))
995         {
996                 printk(KERN_ERR "HPT374: Only 33MHz PCI timings are supported.\n");
997                 return -EOPNOTSUPP;
998         }
999         /*
1000          * adjust PLL based upon PCI clock, enable it, and wait for
1001          * stabilization.
1002          */
1003         adjust = 0;
1004         freq = (pll < F_LOW_PCI_50) ? 2 : 4;
1005         while (adjust++ < 6) {
1006                 pci_write_config_dword(dev, 0x5c, (freq + pll) << 16 |
1007                                        pll | 0x100);
1008
1009                 /* wait for clock stabilization */
1010                 for (i = 0; i < 0x50000; i++) {
1011                         pci_read_config_byte(dev, 0x5b, &reg5bh);
1012                         if (reg5bh & 0x80) {
1013                                 /* spin looking for the clock to destabilize */
1014                                 for (i = 0; i < 0x1000; ++i) {
1015                                         pci_read_config_byte(dev, 0x5b, 
1016                                                              &reg5bh);
1017                                         if ((reg5bh & 0x80) == 0)
1018                                                 goto pll_recal;
1019                                 }
1020                                 pci_read_config_dword(dev, 0x5c, &pll);
1021                                 pci_write_config_dword(dev, 0x5c, 
1022                                                        pll & ~0x100);
1023                                 pci_write_config_byte(dev, 0x5b, 0x21);
1024                                 if (hpt_minimum_revision(dev,8))
1025                                         pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
1026                                 else if (hpt_minimum_revision(dev,5))
1027                                         pci_set_drvdata(dev, (void *) fifty_base_hpt372);
1028                                 else if (hpt_minimum_revision(dev,4))
1029                                         pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
1030                                 else
1031                                         pci_set_drvdata(dev, (void *) fifty_base_hpt370a);
1032                                 printk("HPT37X: using 50MHz internal PLL\n");
1033                                 goto init_hpt37X_done;
1034                         }
1035                 }
1036 pll_recal:
1037                 if (adjust & 1)
1038                         pll -= (adjust >> 1);
1039                 else
1040                         pll += (adjust >> 1);
1041         } 
1042
1043 init_hpt37X_done:
1044         /* reset state engine */
1045         pci_write_config_byte(dev, 0x50, 0x37); 
1046         pci_write_config_byte(dev, 0x54, 0x37); 
1047         udelay(100);
1048         return 0;
1049 }
1050
1051 static int __devinit init_hpt366(struct pci_dev *dev)
1052 {
1053         u32 reg1        = 0;
1054         u8 drive_fast   = 0;
1055
1056         /*
1057          * Disable the "fast interrupt" prediction.
1058          */
1059         pci_read_config_byte(dev, 0x51, &drive_fast);
1060         if (drive_fast & 0x80)
1061                 pci_write_config_byte(dev, 0x51, drive_fast & ~0x80);
1062         pci_read_config_dword(dev, 0x40, &reg1);
1063                                                                         
1064         /* detect bus speed by looking at control reg timing: */
1065         switch((reg1 >> 8) & 7) {
1066                 case 5:
1067                         pci_set_drvdata(dev, (void *) forty_base_hpt366);
1068                         break;
1069                 case 9:
1070                         pci_set_drvdata(dev, (void *) twenty_five_base_hpt366);
1071                         break;
1072                 case 7:
1073                 default:
1074                         pci_set_drvdata(dev, (void *) thirty_three_base_hpt366);
1075                         break;
1076         }
1077
1078         if (!pci_get_drvdata(dev))
1079         {
1080                 printk(KERN_ERR "hpt366: unknown bus timing.\n");
1081                 pci_set_drvdata(dev, NULL);
1082         }
1083         return 0;
1084 }
1085
1086 static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev, const char *name)
1087 {
1088         int ret = 0;
1089         u8 test = 0;
1090
1091         if (dev->resource[PCI_ROM_RESOURCE].start)
1092                 pci_write_config_byte(dev, PCI_ROM_ADDRESS,
1093                         dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
1094
1095         pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &test);
1096         if (test != (L1_CACHE_BYTES / 4))
1097                 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1098                         (L1_CACHE_BYTES / 4));
1099
1100         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &test);
1101         if (test != 0x78)
1102                 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
1103
1104         pci_read_config_byte(dev, PCI_MIN_GNT, &test);
1105         if (test != 0x08)
1106                 pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
1107
1108         pci_read_config_byte(dev, PCI_MAX_LAT, &test);
1109         if (test != 0x08)
1110                 pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
1111
1112         if (hpt_minimum_revision(dev, 3)) {
1113                 ret = init_hpt37x(dev);
1114         } else {
1115                 ret =init_hpt366(dev);
1116         }
1117         if (ret)
1118                 return ret;
1119         
1120 #if defined(DISPLAY_HPT366_TIMINGS) && defined(CONFIG_PROC_FS)
1121         hpt_devs[n_hpt_devs++] = dev;
1122
1123         if (!hpt366_proc) {
1124                 hpt366_proc = 1;
1125                 ide_pci_create_host_proc("hpt366", hpt366_get_info);
1126         }
1127 #endif /* DISPLAY_HPT366_TIMINGS && CONFIG_PROC_FS */
1128
1129         return dev->irq;
1130 }
1131
1132 static void __devinit init_hwif_hpt366(ide_hwif_t *hwif)
1133 {
1134         struct pci_dev *dev             = hwif->pci_dev;
1135         u8 ata66 = 0, regmask           = (hwif->channel) ? 0x01 : 0x02;
1136         u8 did, rid;
1137         unsigned long dmabase           = hwif->dma_base;
1138         int is_372n = 0;
1139         
1140         if(dmabase)
1141         {
1142                 did = inb(dmabase + 0x22);
1143                 rid = inb(dmabase + 0x28);
1144         
1145                 if((did == 4 && rid == 6) || (did == 5 && rid > 1))
1146                         is_372n = 1;
1147         }
1148                 
1149         hwif->tuneproc                  = &hpt3xx_tune_drive;
1150         hwif->speedproc                 = &hpt3xx_tune_chipset;
1151         hwif->quirkproc                 = &hpt3xx_quirkproc;
1152         hwif->intrproc                  = &hpt3xx_intrproc;
1153         hwif->maskproc                  = &hpt3xx_maskproc;
1154         
1155         if(is_372n)
1156                 hwif->rw_disk = &hpt372n_rw_disk;
1157
1158         /*
1159          * The HPT37x uses the CBLID pins as outputs for MA15/MA16
1160          * address lines to access an external eeprom.  To read valid
1161          * cable detect state the pins must be enabled as inputs.
1162          */
1163         if (hpt_minimum_revision(dev, 8) && PCI_FUNC(dev->devfn) & 1) {
1164                 /*
1165                  * HPT374 PCI function 1
1166                  * - set bit 15 of reg 0x52 to enable TCBLID as input
1167                  * - set bit 15 of reg 0x56 to enable FCBLID as input
1168                  */
1169                 u16 mcr3, mcr6;
1170                 pci_read_config_word(dev, 0x52, &mcr3);
1171                 pci_read_config_word(dev, 0x56, &mcr6);
1172                 pci_write_config_word(dev, 0x52, mcr3 | 0x8000);
1173                 pci_write_config_word(dev, 0x56, mcr6 | 0x8000);
1174                 /* now read cable id register */
1175                 pci_read_config_byte(dev, 0x5a, &ata66);
1176                 pci_write_config_word(dev, 0x52, mcr3);
1177                 pci_write_config_word(dev, 0x56, mcr6);
1178         } else if (hpt_minimum_revision(dev, 3)) {
1179                 /*
1180                  * HPT370/372 and 374 pcifn 0
1181                  * - clear bit 0 of 0x5b to enable P/SCBLID as inputs
1182                  */
1183                 u8 scr2;
1184                 pci_read_config_byte(dev, 0x5b, &scr2);
1185                 pci_write_config_byte(dev, 0x5b, scr2 & ~1);
1186                 /* now read cable id register */
1187                 pci_read_config_byte(dev, 0x5a, &ata66);
1188                 pci_write_config_byte(dev, 0x5b, scr2);
1189         } else {
1190                 pci_read_config_byte(dev, 0x5a, &ata66);
1191         }
1192
1193 #ifdef DEBUG
1194         printk("HPT366: reg5ah=0x%02x ATA-%s Cable Port%d\n",
1195                 ata66, (ata66 & regmask) ? "33" : "66",
1196                 PCI_FUNC(hwif->pci_dev->devfn));
1197 #endif /* DEBUG */
1198
1199 #ifdef HPT_SERIALIZE_IO
1200         /* serialize access to this device */
1201         if (hwif->mate)
1202                 hwif->serialized = hwif->mate->serialized = 1;
1203 #endif
1204
1205         if (hpt_minimum_revision(dev,3)) {
1206                 u8 reg5ah = 0;
1207                         pci_write_config_byte(dev, 0x5a, reg5ah & ~0x10);
1208                 /*
1209                  * set up ioctl for power status.
1210                  * note: power affects both
1211                  * drives on each channel
1212                  */
1213                 hwif->resetproc = &hpt3xx_reset;
1214                 hwif->busproc   = &hpt370_busproc;
1215 //              hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
1216         } else if (hpt_minimum_revision(dev,2)) {
1217                 hwif->resetproc = &hpt3xx_reset;
1218                 hwif->busproc   = &hpt3xx_tristate;
1219         } else {
1220                 hwif->resetproc = &hpt3xx_reset;
1221                 hwif->busproc   = &hpt3xx_tristate;
1222         }
1223
1224         if (!hwif->dma_base) {
1225                 hwif->drives[0].autotune = 1;
1226                 hwif->drives[1].autotune = 1;
1227                 return;
1228         }
1229
1230         hwif->ultra_mask = 0x7f;
1231         hwif->mwdma_mask = 0x07;
1232
1233         if (!(hwif->udma_four))
1234                 hwif->udma_four = ((ata66 & regmask) ? 0 : 1);
1235         hwif->ide_dma_check = &hpt366_config_drive_xfer_rate;
1236
1237         if (hpt_minimum_revision(dev,8)) {
1238                 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1239                 hwif->ide_dma_end = &hpt374_ide_dma_end;
1240         } else if (hpt_minimum_revision(dev,5)) {
1241                 hwif->ide_dma_test_irq = &hpt374_ide_dma_test_irq;
1242                 hwif->ide_dma_end = &hpt374_ide_dma_end;
1243         } else if (hpt_minimum_revision(dev,3)) {
1244                 hwif->ide_dma_begin = &hpt370_ide_dma_begin;
1245                 hwif->ide_dma_end = &hpt370_ide_dma_end;
1246                 hwif->ide_dma_timeout = &hpt370_ide_dma_timeout;
1247                 hwif->ide_dma_lostirq = &hpt370_ide_dma_lostirq;
1248         } else if (hpt_minimum_revision(dev,2))
1249                 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1250         else
1251                 hwif->ide_dma_lostirq = &hpt366_ide_dma_lostirq;
1252
1253         if (!noautodma)
1254                 hwif->autodma = 1;
1255         hwif->drives[0].autodma = hwif->autodma;
1256         hwif->drives[1].autodma = hwif->autodma;
1257 }
1258
1259 static void __devinit init_dma_hpt366(ide_hwif_t *hwif, unsigned long dmabase)
1260 {
1261         u8 masterdma    = 0, slavedma = 0;
1262         u8 dma_new      = 0, dma_old = 0;
1263         u8 primary      = hwif->channel ? 0x4b : 0x43;
1264         u8 secondary    = hwif->channel ? 0x4f : 0x47;
1265         unsigned long flags;
1266
1267         if (!dmabase)
1268                 return;
1269                 
1270         if(pci_get_drvdata(hwif->pci_dev) == NULL)
1271         {
1272                 printk(KERN_WARNING "hpt: no known IDE timings, disabling DMA.\n");
1273                 return;
1274         }
1275
1276         dma_old = hwif->INB(dmabase+2);
1277
1278         local_irq_save(flags);
1279
1280         dma_new = dma_old;
1281         pci_read_config_byte(hwif->pci_dev, primary, &masterdma);
1282         pci_read_config_byte(hwif->pci_dev, secondary, &slavedma);
1283
1284         if (masterdma & 0x30)   dma_new |= 0x20;
1285         if (slavedma & 0x30)    dma_new |= 0x40;
1286         if (dma_new != dma_old)
1287                 hwif->OUTB(dma_new, dmabase+2);
1288
1289         local_irq_restore(flags);
1290
1291         ide_setup_dma(hwif, dmabase, 8);
1292 }
1293
1294 static void __devinit init_setup_hpt374(struct pci_dev *dev, ide_pci_device_t *d)
1295 {
1296         struct pci_dev *findev = NULL;
1297
1298         if (PCI_FUNC(dev->devfn) & 1)
1299                 return;
1300
1301         while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1302                 if ((findev->vendor == dev->vendor) &&
1303                     (findev->device == dev->device) &&
1304                     ((findev->devfn - dev->devfn) == 1) &&
1305                     (PCI_FUNC(findev->devfn) & 1)) {
1306                         if (findev->irq != dev->irq) {
1307                                 /* FIXME: we need a core pci_set_interrupt() */
1308                                 findev->irq = dev->irq;
1309                                 printk(KERN_WARNING "%s: pci-config space interrupt "
1310                                         "fixed.\n", d->name);
1311                         }
1312                         ide_setup_pci_devices(dev, findev, d);
1313                         return;
1314                 }
1315         }
1316         ide_setup_pci_device(dev, d);
1317 }
1318
1319 static void __devinit init_setup_hpt37x(struct pci_dev *dev, ide_pci_device_t *d)
1320 {
1321         ide_setup_pci_device(dev, d);
1322 }
1323
1324 static void __devinit init_setup_hpt366(struct pci_dev *dev, ide_pci_device_t *d)
1325 {
1326         struct pci_dev *findev = NULL;
1327         u8 pin1 = 0, pin2 = 0;
1328         unsigned int class_rev;
1329         char *chipset_names[] = {"HPT366", "HPT366",  "HPT368",
1330                                  "HPT370", "HPT370A", "HPT372",
1331                                  "HPT372N" };
1332
1333         if (PCI_FUNC(dev->devfn) & 1)
1334                 return;
1335
1336         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1337         class_rev &= 0xff;
1338
1339         if(dev->device == PCI_DEVICE_ID_TTI_HPT372N)
1340                 class_rev = 6;
1341                 
1342         if(class_rev <= 6)
1343                 d->name = chipset_names[class_rev];
1344
1345         switch(class_rev) {
1346                 case 6:
1347                 case 5:
1348                 case 4:
1349                 case 3: ide_setup_pci_device(dev, d);
1350                         return;
1351                 default:        break;
1352         }
1353
1354         d->channels = 1;
1355
1356         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin1);
1357         while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
1358                 if ((findev->vendor == dev->vendor) &&
1359                     (findev->device == dev->device) &&
1360                     ((findev->devfn - dev->devfn) == 1) &&
1361                     (PCI_FUNC(findev->devfn) & 1)) {
1362                         pci_read_config_byte(findev, PCI_INTERRUPT_PIN, &pin2);
1363                         if ((pin1 != pin2) && (dev->irq == findev->irq)) {
1364                                 d->bootable = ON_BOARD;
1365                                 printk("%s: onboard version of chipset, "
1366                                         "pin1=%d pin2=%d\n", d->name,
1367                                         pin1, pin2);
1368                         }
1369                         ide_setup_pci_devices(dev, findev, d);
1370                         return;
1371                 }
1372         }
1373         ide_setup_pci_device(dev, d);
1374 }
1375
1376
1377 /**
1378  *      hpt366_init_one -       called when an HPT366 is found
1379  *      @dev: the hpt366 device
1380  *      @id: the matching pci id
1381  *
1382  *      Called when the PCI registration layer (or the IDE initialization)
1383  *      finds a device matching our IDE device tables.
1384  */
1385  
1386 static int __devinit hpt366_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1387 {
1388         ide_pci_device_t *d = &hpt366_chipsets[id->driver_data];
1389
1390         d->init_setup(dev, d);
1391         return 0;
1392 }
1393
1394 static struct pci_device_id hpt366_pci_tbl[] = {
1395         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT366, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1396         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT372, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1397         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT302, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1398         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT371, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
1399         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT374, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
1400         { PCI_VENDOR_ID_TTI, PCI_DEVICE_ID_TTI_HPT372N, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
1401         { 0, },
1402 };
1403 MODULE_DEVICE_TABLE(pci, hpt366_pci_tbl);
1404
1405 static struct pci_driver driver = {
1406         .name           = "HPT366 IDE",
1407         .id_table       = hpt366_pci_tbl,
1408         .probe          = hpt366_init_one,
1409 };
1410
1411 static int hpt366_ide_init(void)
1412 {
1413         return ide_pci_register_driver(&driver);
1414 }
1415
1416 module_init(hpt366_ide_init);
1417
1418 MODULE_AUTHOR("Andre Hedrick");
1419 MODULE_DESCRIPTION("PCI driver module for Highpoint HPT366 IDE");
1420 MODULE_LICENSE("GPL");