patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / pci / cmd64x.c
1 /* $Id: cmd64x.c,v 1.21 2000/01/30 23:23:16
2  *
3  * linux/drivers/ide/pci/cmd64x.c               Version 1.30    Sept 10, 2002
4  *
5  * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
6  *           Note, this driver is not used at all on other systems because
7  *           there the "BIOS" has done all of the following already.
8  *           Due to massive hardware bugs, UltraDMA is only supported
9  *           on the 646U2 and not on the 646U.
10  *
11  * Copyright (C) 1998           Eddie C. Dost  (ecd@skynet.be)
12  * Copyright (C) 1998           David S. Miller (davem@redhat.com)
13  *
14  * Copyright (C) 1999-2002      Andre Hedrick <andre@linux-ide.org>
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/pci.h>
21 #include <linux/delay.h>
22 #include <linux/hdreg.h>
23 #include <linux/ide.h>
24 #include <linux/init.h>
25
26 #include <asm/io.h>
27
28 #include "cmd64x.h"
29
30 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
31 #include <linux/stat.h>
32 #include <linux/proc_fs.h>
33
34 static u8 cmd64x_proc = 0;
35
36 #define CMD_MAX_DEVS            5
37
38 static struct pci_dev *cmd_devs[CMD_MAX_DEVS];
39 static int n_cmd_devs;
40
41 #undef DEBUG_CMD_REGS
42
43 static char * print_cmd64x_get_info (char *buf, struct pci_dev *dev, int index)
44 {
45         char *p = buf;
46
47         u8 reg53 = 0, reg54 = 0, reg55 = 0, reg56 = 0;  /* primary */
48         u8 reg57 = 0, reg58 = 0, reg5b;                 /* secondary */
49         u8 reg72 = 0, reg73 = 0;                        /* primary */
50         u8 reg7a = 0, reg7b = 0;                        /* secondary */
51         u8 reg50 = 0, reg71 = 0;                        /* extra */
52 #ifdef DEBUG_CMD_REGS
53         u8 hi_byte = 0, lo_byte = 0;
54 #endif /* DEBUG_CMD_REGS */
55
56         p += sprintf(p, "\nController: %d\n", index);
57         p += sprintf(p, "CMD%x Chipset.\n", dev->device);
58         (void) pci_read_config_byte(dev, CFR,       &reg50);
59         (void) pci_read_config_byte(dev, ARTTIM0,   &reg53);
60         (void) pci_read_config_byte(dev, DRWTIM0,   &reg54);
61         (void) pci_read_config_byte(dev, ARTTIM1,   &reg55);
62         (void) pci_read_config_byte(dev, DRWTIM1,   &reg56);
63         (void) pci_read_config_byte(dev, ARTTIM2,   &reg57);
64         (void) pci_read_config_byte(dev, DRWTIM2,   &reg58);
65         (void) pci_read_config_byte(dev, DRWTIM3,   &reg5b);
66         (void) pci_read_config_byte(dev, MRDMODE,   &reg71);
67         (void) pci_read_config_byte(dev, BMIDESR0,  &reg72);
68         (void) pci_read_config_byte(dev, UDIDETCR0, &reg73);
69         (void) pci_read_config_byte(dev, BMIDESR1,  &reg7a);
70         (void) pci_read_config_byte(dev, UDIDETCR1, &reg7b);
71
72         p += sprintf(p, "--------------- Primary Channel "
73                         "---------------- Secondary Channel "
74                         "-------------\n");
75         p += sprintf(p, "                %sabled           "
76                         "              %sabled\n",
77                 (reg72&0x80)?"dis":" en",
78                 (reg7a&0x80)?"dis":" en");
79         p += sprintf(p, "--------------- drive0 "
80                 "--------- drive1 -------- drive0 "
81                 "---------- drive1 ------\n");
82         p += sprintf(p, "DMA enabled:    %s              %s"
83                         "             %s               %s\n",
84                 (reg72&0x20)?"yes":"no ", (reg72&0x40)?"yes":"no ",
85                 (reg7a&0x20)?"yes":"no ", (reg7a&0x40)?"yes":"no ");
86
87         p += sprintf(p, "DMA Mode:       %s(%s)          %s(%s)",
88                 (reg72&0x20)?((reg73&0x01)?"UDMA":" DMA"):" PIO",
89                 (reg72&0x20)?(
90                         ((reg73&0x30)==0x30)?(((reg73&0x35)==0x35)?"3":"0"):
91                         ((reg73&0x20)==0x20)?(((reg73&0x25)==0x25)?"3":"1"):
92                         ((reg73&0x10)==0x10)?(((reg73&0x15)==0x15)?"4":"2"):
93                         ((reg73&0x00)==0x00)?(((reg73&0x05)==0x05)?"5":"2"):
94                         "X"):"?",
95                 (reg72&0x40)?((reg73&0x02)?"UDMA":" DMA"):" PIO",
96                 (reg72&0x40)?(
97                         ((reg73&0xC0)==0xC0)?(((reg73&0xC5)==0xC5)?"3":"0"):
98                         ((reg73&0x80)==0x80)?(((reg73&0x85)==0x85)?"3":"1"):
99                         ((reg73&0x40)==0x40)?(((reg73&0x4A)==0x4A)?"4":"2"):
100                         ((reg73&0x00)==0x00)?(((reg73&0x0A)==0x0A)?"5":"2"):
101                         "X"):"?");
102         p += sprintf(p, "         %s(%s)           %s(%s)\n",
103                 (reg7a&0x20)?((reg7b&0x01)?"UDMA":" DMA"):" PIO",
104                 (reg7a&0x20)?(
105                         ((reg7b&0x30)==0x30)?(((reg7b&0x35)==0x35)?"3":"0"):
106                         ((reg7b&0x20)==0x20)?(((reg7b&0x25)==0x25)?"3":"1"):
107                         ((reg7b&0x10)==0x10)?(((reg7b&0x15)==0x15)?"4":"2"):
108                         ((reg7b&0x00)==0x00)?(((reg7b&0x05)==0x05)?"5":"2"):
109                         "X"):"?",
110                 (reg7a&0x40)?((reg7b&0x02)?"UDMA":" DMA"):" PIO",
111                 (reg7a&0x40)?(
112                         ((reg7b&0xC0)==0xC0)?(((reg7b&0xC5)==0xC5)?"3":"0"):
113                         ((reg7b&0x80)==0x80)?(((reg7b&0x85)==0x85)?"3":"1"):
114                         ((reg7b&0x40)==0x40)?(((reg7b&0x4A)==0x4A)?"4":"2"):
115                         ((reg7b&0x00)==0x00)?(((reg7b&0x0A)==0x0A)?"5":"2"):
116                         "X"):"?" );
117         p += sprintf(p, "PIO Mode:       %s                %s"
118                         "               %s                 %s\n",
119                         "?", "?", "?", "?");
120         p += sprintf(p, "                %s                     %s\n",
121                 (reg50 & CFR_INTR_CH0) ? "interrupting" : "polling     ",
122                 (reg57 & ARTTIM23_INTR_CH1) ? "interrupting" : "polling");
123         p += sprintf(p, "                %s                          %s\n",
124                 (reg71 & MRDMODE_INTR_CH0) ? "pending" : "clear  ",
125                 (reg71 & MRDMODE_INTR_CH1) ? "pending" : "clear");
126         p += sprintf(p, "                %s                          %s\n",
127                 (reg71 & MRDMODE_BLK_CH0) ? "blocked" : "enabled",
128                 (reg71 & MRDMODE_BLK_CH1) ? "blocked" : "enabled");
129
130 #ifdef DEBUG_CMD_REGS
131         SPLIT_BYTE(reg50, hi_byte, lo_byte);
132         p += sprintf(p, "CFR       = 0x%02x, HI = 0x%02x, "
133                         "LOW = 0x%02x\n", reg50, hi_byte, lo_byte);
134         SPLIT_BYTE(reg57, hi_byte, lo_byte);
135         p += sprintf(p, "ARTTIM23  = 0x%02x, HI = 0x%02x, "
136                         "LOW = 0x%02x\n", reg57, hi_byte, lo_byte);
137         SPLIT_BYTE(reg71, hi_byte, lo_byte);
138         p += sprintf(p, "MRDMODE   = 0x%02x, HI = 0x%02x, "
139                         "LOW = 0x%02x\n", reg71, hi_byte, lo_byte);
140 #endif /* DEBUG_CMD_REGS */
141
142         return (char *)p;
143 }
144
145 static int cmd64x_get_info (char *buffer, char **addr, off_t offset, int count)
146 {
147         char *p = buffer;
148         int i;
149
150         p += sprintf(p, "\n");
151         for (i = 0; i < n_cmd_devs; i++) {
152                 struct pci_dev *dev     = cmd_devs[i];
153                 p = print_cmd64x_get_info(p, dev, i);
154         }
155         return p-buffer;        /* => must be less than 4k! */
156 }
157
158 #endif  /* defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS) */
159
160 /*
161  * Registers and masks for easy access by drive index:
162  */
163 #if 0
164 static u8 prefetch_regs[4]  = {CNTRL, CNTRL, ARTTIM23, ARTTIM23};
165 static u8 prefetch_masks[4] = {CNTRL_DIS_RA0, CNTRL_DIS_RA1, ARTTIM23_DIS_RA2, ARTTIM23_DIS_RA3};
166 #endif
167
168 /*
169  * This routine writes the prepared setup/active/recovery counts
170  * for a drive into the cmd646 chipset registers to active them.
171  */
172 static void program_drive_counts (ide_drive_t *drive, int setup_count, int active_count, int recovery_count)
173 {
174         unsigned long flags;
175         struct pci_dev *dev = HWIF(drive)->pci_dev;
176         ide_drive_t *drives = HWIF(drive)->drives;
177         u8 temp_b;
178         static const u8 setup_counts[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
179         static const u8 recovery_counts[] =
180                 {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
181         static const u8 arttim_regs[2][2] = {
182                         { ARTTIM0, ARTTIM1 },
183                         { ARTTIM23, ARTTIM23 }
184                 };
185         static const u8 drwtim_regs[2][2] = {
186                         { DRWTIM0, DRWTIM1 },
187                         { DRWTIM2, DRWTIM3 }
188                 };
189         int channel = (int) HWIF(drive)->channel;
190         int slave = (drives != drive);  /* Is this really the best way to determine this?? */
191
192         cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n",
193                 setup_count, active_count, recovery_count, drive->present);
194         /*
195          * Set up address setup count registers.
196          * Primary interface has individual count/timing registers for
197          * each drive.  Secondary interface has one common set of registers,
198          * for address setup so we merge these timings, using the slowest
199          * value.
200          */
201         if (channel) {
202                 drive->drive_data = setup_count;
203                 setup_count = max(drives[0].drive_data,
204                                         drives[1].drive_data);
205                 cmdprintk("Secondary interface, setup_count = %d\n",
206                                         setup_count);
207         }
208
209         /*
210          * Convert values to internal chipset representation
211          */
212         setup_count = (setup_count > 5) ? 0xc0 : (int) setup_counts[setup_count];
213         active_count &= 0xf; /* Remember, max value is 16 */
214         recovery_count = (int) recovery_counts[recovery_count];
215
216         cmdprintk("Final values = %d,%d,%d\n",
217                 setup_count, active_count, recovery_count);
218
219         /*
220          * Now that everything is ready, program the new timings
221          */
222         local_irq_save(flags);
223         /*
224          * Program the address_setup clocks into ARTTIM reg,
225          * and then the active/recovery counts into the DRWTIM reg
226          */
227         (void) pci_read_config_byte(dev, arttim_regs[channel][slave], &temp_b);
228         (void) pci_write_config_byte(dev, arttim_regs[channel][slave],
229                 ((u8) setup_count) | (temp_b & 0x3f));
230         (void) pci_write_config_byte(dev, drwtim_regs[channel][slave],
231                 (u8) ((active_count << 4) | recovery_count));
232         cmdprintk ("Write %x to %x\n",
233                 ((u8) setup_count) | (temp_b & 0x3f),
234                 arttim_regs[channel][slave]);
235         cmdprintk ("Write %x to %x\n",
236                 (u8) ((active_count << 4) | recovery_count),
237                 drwtim_regs[channel][slave]);
238         local_irq_restore(flags);
239 }
240
241 /*
242  * Attempts to set the interface PIO mode.
243  * The preferred method of selecting PIO modes (e.g. mode 4) is 
244  * "echo 'piomode:4' > /proc/ide/hdx/settings".  Special cases are
245  * 8: prefetch off, 9: prefetch on, 255: auto-select best mode.
246  * Called with 255 at boot time.
247  */
248
249 static void cmd64x_tuneproc (ide_drive_t *drive, u8 mode_wanted)
250 {
251         int setup_time, active_time, recovery_time;
252         int clock_time, pio_mode, cycle_time;
253         u8 recovery_count2, cycle_count;
254         int setup_count, active_count, recovery_count;
255         int bus_speed = system_bus_clock();
256         /*byte b;*/
257         ide_pio_data_t  d;
258
259         switch (mode_wanted) {
260                 case 8: /* set prefetch off */
261                 case 9: /* set prefetch on */
262                         mode_wanted &= 1;
263                         /*set_prefetch_mode(index, mode_wanted);*/
264                         cmdprintk("%s: %sabled cmd640 prefetch\n",
265                                 drive->name, mode_wanted ? "en" : "dis");
266                         return;
267         }
268
269         mode_wanted = ide_get_best_pio_mode (drive, mode_wanted, 5, &d);
270         pio_mode = d.pio_mode;
271         cycle_time = d.cycle_time;
272
273         /*
274          * I copied all this complicated stuff from cmd640.c and made a few
275          * minor changes.  For now I am just going to pray that it is correct.
276          */
277         if (pio_mode > 5)
278                 pio_mode = 5;
279         setup_time  = ide_pio_timings[pio_mode].setup_time;
280         active_time = ide_pio_timings[pio_mode].active_time;
281         recovery_time = cycle_time - (setup_time + active_time);
282         clock_time = 1000 / bus_speed;
283         cycle_count = (cycle_time + clock_time - 1) / clock_time;
284
285         setup_count = (setup_time + clock_time - 1) / clock_time;
286
287         active_count = (active_time + clock_time - 1) / clock_time;
288
289         recovery_count = (recovery_time + clock_time - 1) / clock_time;
290         recovery_count2 = cycle_count - (setup_count + active_count);
291         if (recovery_count2 > recovery_count)
292                 recovery_count = recovery_count2;
293         if (recovery_count > 16) {
294                 active_count += recovery_count - 16;
295                 recovery_count = 16;
296         }
297         if (active_count > 16)
298                 active_count = 16; /* maximum allowed by cmd646 */
299
300         /*
301          * In a perfect world, we might set the drive pio mode here
302          * (using WIN_SETFEATURE) before continuing.
303          *
304          * But we do not, because:
305          *      1) this is the wrong place to do it
306          *              (proper is do_special() in ide.c)
307          *      2) in practice this is rarely, if ever, necessary
308          */
309         program_drive_counts (drive, setup_count, active_count, recovery_count);
310
311         cmdprintk("%s: selected cmd646 PIO mode%d : %d (%dns)%s, "
312                 "clocks=%d/%d/%d\n",
313                 drive->name, pio_mode, mode_wanted, cycle_time,
314                 d.overridden ? " (overriding vendor mode)" : "",
315                 setup_count, active_count, recovery_count);
316 }
317
318 static u8 cmd64x_ratemask (ide_drive_t *drive)
319 {
320         struct pci_dev *dev     = HWIF(drive)->pci_dev;
321         u8 mode = 0;
322
323         switch(dev->device) {
324                 case PCI_DEVICE_ID_CMD_649:
325                         mode = 3;
326                         break;
327                 case PCI_DEVICE_ID_CMD_648:
328                         mode = 2;
329                         break;
330                 case PCI_DEVICE_ID_CMD_643:
331                         return 0;
332
333                 case PCI_DEVICE_ID_CMD_646:
334                 {
335                         unsigned int class_rev  = 0;
336                         pci_read_config_dword(dev,
337                                 PCI_CLASS_REVISION, &class_rev);
338                         class_rev &= 0xff;
339                 /*
340                  * UltraDMA only supported on PCI646U and PCI646U2, which
341                  * correspond to revisions 0x03, 0x05 and 0x07 respectively.
342                  * Actually, although the CMD tech support people won't
343                  * tell me the details, the 0x03 revision cannot support
344                  * UDMA correctly without hardware modifications, and even
345                  * then it only works with Quantum disks due to some
346                  * hold time assumptions in the 646U part which are fixed
347                  * in the 646U2.
348                  *
349                  * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
350                  */
351                         switch(class_rev) {
352                                 case 0x07:
353                                 case 0x05:
354                                         return 1;
355                                 case 0x03:
356                                 case 0x01:
357                                 default:
358                                         return 0;
359                         }
360                 }
361         }
362         if (!eighty_ninty_three(drive))
363                 mode = min(mode, (u8)1);
364         return mode;
365 }
366
367 static void config_cmd64x_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
368 {
369         u8 speed        = 0x00;
370         u8 set_pio      = ide_get_best_pio_mode(drive, 4, 5, NULL);
371
372         cmd64x_tuneproc(drive, set_pio);
373         speed = XFER_PIO_0 + set_pio;
374         if (set_speed)
375                 (void) ide_config_drive_speed(drive, speed);
376 }
377
378 static void config_chipset_for_pio (ide_drive_t *drive, u8 set_speed)
379 {
380         config_cmd64x_chipset_for_pio(drive, set_speed);
381 }
382
383 static int cmd64x_tune_chipset (ide_drive_t *drive, u8 xferspeed)
384 {
385         ide_hwif_t *hwif        = HWIF(drive);
386         struct pci_dev *dev     = hwif->pci_dev;
387
388         u8 unit                 = (drive->select.b.unit & 0x01);
389         u8 regU = 0, pciU       = (hwif->channel) ? UDIDETCR1 : UDIDETCR0;
390         u8 regD = 0, pciD       = (hwif->channel) ? BMIDESR1 : BMIDESR0;
391
392         u8 speed        = ide_rate_filter(cmd64x_ratemask(drive), xferspeed);
393
394         if (speed > XFER_PIO_4) {
395                 (void) pci_read_config_byte(dev, pciD, &regD);
396                 (void) pci_read_config_byte(dev, pciU, &regU);
397                 regD &= ~(unit ? 0x40 : 0x20);
398                 regU &= ~(unit ? 0xCA : 0x35);
399                 (void) pci_write_config_byte(dev, pciD, regD);
400                 (void) pci_write_config_byte(dev, pciU, regU);
401                 (void) pci_read_config_byte(dev, pciD, &regD);
402                 (void) pci_read_config_byte(dev, pciU, &regU);
403         }
404
405         switch(speed) {
406                 case XFER_UDMA_5:       regU |= (unit ? 0x0A : 0x05); break;
407                 case XFER_UDMA_4:       regU |= (unit ? 0x4A : 0x15); break;
408                 case XFER_UDMA_3:       regU |= (unit ? 0x8A : 0x25); break;
409                 case XFER_UDMA_2:       regU |= (unit ? 0x42 : 0x11); break;
410                 case XFER_UDMA_1:       regU |= (unit ? 0x82 : 0x21); break;
411                 case XFER_UDMA_0:       regU |= (unit ? 0xC2 : 0x31); break;
412                 case XFER_MW_DMA_2:     regD |= (unit ? 0x40 : 0x10); break;
413                 case XFER_MW_DMA_1:     regD |= (unit ? 0x80 : 0x20); break;
414                 case XFER_MW_DMA_0:     regD |= (unit ? 0xC0 : 0x30); break;
415                 case XFER_SW_DMA_2:     regD |= (unit ? 0x40 : 0x10); break;
416                 case XFER_SW_DMA_1:     regD |= (unit ? 0x80 : 0x20); break;
417                 case XFER_SW_DMA_0:     regD |= (unit ? 0xC0 : 0x30); break;
418                 case XFER_PIO_4:        cmd64x_tuneproc(drive, 4); break;
419                 case XFER_PIO_3:        cmd64x_tuneproc(drive, 3); break;
420                 case XFER_PIO_2:        cmd64x_tuneproc(drive, 2); break;
421                 case XFER_PIO_1:        cmd64x_tuneproc(drive, 1); break;
422                 case XFER_PIO_0:        cmd64x_tuneproc(drive, 0); break;
423
424                 default:
425                         return 1;
426         }
427
428         if (speed > XFER_PIO_4) {
429                 (void) pci_write_config_byte(dev, pciU, regU);
430                 regD |= (unit ? 0x40 : 0x20);
431                 (void) pci_write_config_byte(dev, pciD, regD);
432         }
433
434         return (ide_config_drive_speed(drive, speed));
435 }
436
437 static int config_chipset_for_dma (ide_drive_t *drive)
438 {
439         u8 speed        = ide_dma_speed(drive, cmd64x_ratemask(drive));
440
441         config_chipset_for_pio(drive, !speed);
442
443         if (!speed)
444                 return 0;
445
446         if(ide_set_xfer_rate(drive, speed))
447                 return 0; 
448
449         if (!drive->init_speed)
450                 drive->init_speed = speed;
451
452         return ide_dma_enable(drive);
453 }
454
455 static int cmd64x_config_drive_for_dma (ide_drive_t *drive)
456 {
457         ide_hwif_t *hwif        = HWIF(drive);
458         struct hd_driveid *id   = drive->id;
459
460         if ((id != NULL) && ((id->capability & 1) != 0) && drive->autodma) {
461                 /* Consult the list of known "bad" drives */
462                 if (__ide_dma_bad_drive(drive))
463                         goto fast_ata_pio;
464                 if ((id->field_valid & 4) && cmd64x_ratemask(drive)) {
465                         if (id->dma_ultra & hwif->ultra_mask) {
466                                 /* Force if Capable UltraDMA */
467                                 int dma = config_chipset_for_dma(drive);
468                                 if ((id->field_valid & 2) && !dma)
469                                         goto try_dma_modes;
470                         }
471                 } else if (id->field_valid & 2) {
472 try_dma_modes:
473                         if ((id->dma_mword & hwif->mwdma_mask) ||
474                             (id->dma_1word & hwif->swdma_mask)) {
475                                 /* Force if Capable regular DMA modes */
476                                 if (!config_chipset_for_dma(drive))
477                                         goto no_dma_set;
478                         }
479                 } else if (__ide_dma_good_drive(drive) &&
480                            (id->eide_dma_time < 150)) {
481                         /* Consult the list of known "good" drives */
482                         if (!config_chipset_for_dma(drive))
483                                 goto no_dma_set;
484                 } else {
485                         goto fast_ata_pio;
486                 }
487                 return hwif->ide_dma_on(drive);
488         } else if ((id->capability & 8) || (id->field_valid & 2)) {
489 fast_ata_pio:
490 no_dma_set:
491                 config_chipset_for_pio(drive, 1);
492                 return hwif->ide_dma_off_quietly(drive);
493         }
494         /* IORDY not supported */
495         return 0;
496 }
497
498 static int cmd64x_alt_dma_status (struct pci_dev *dev)
499 {
500         switch(dev->device) {
501                 case PCI_DEVICE_ID_CMD_648:
502                 case PCI_DEVICE_ID_CMD_649:
503                         return 1;
504                 default:
505                         break;
506         }
507         return 0;
508 }
509
510 static int cmd64x_ide_dma_end (ide_drive_t *drive)
511 {
512         u8 dma_stat = 0, dma_cmd = 0;
513         ide_hwif_t *hwif        = HWIF(drive);
514         struct pci_dev *dev     = hwif->pci_dev;
515
516         drive->waiting_for_dma = 0;
517         /* read DMA command state */
518         dma_cmd = hwif->INB(hwif->dma_command);
519         /* stop DMA */
520         hwif->OUTB((dma_cmd & ~1), hwif->dma_command);
521         /* get DMA status */
522         dma_stat = hwif->INB(hwif->dma_status);
523         /* clear the INTR & ERROR bits */
524         hwif->OUTB(dma_stat|6, hwif->dma_status);
525         if (cmd64x_alt_dma_status(dev)) {
526                 u8 dma_intr     = 0;
527                 u8 dma_mask     = (hwif->channel) ? ARTTIM23_INTR_CH1 :
528                                                     CFR_INTR_CH0;
529                 u8 dma_reg      = (hwif->channel) ? ARTTIM2 : CFR;
530                 (void) pci_read_config_byte(dev, dma_reg, &dma_intr);
531                 /* clear the INTR bit */
532                 (void) pci_write_config_byte(dev, dma_reg, dma_intr|dma_mask);
533         }
534         /* purge DMA mappings */
535         ide_destroy_dmatable(drive);
536         /* verify good DMA status */
537         return (dma_stat & 7) != 4;
538 }
539
540 static int cmd64x_ide_dma_test_irq (ide_drive_t *drive)
541 {
542         ide_hwif_t *hwif                = HWIF(drive);
543         struct pci_dev *dev             = hwif->pci_dev;
544         u8 dma_alt_stat = 0, mask       = (hwif->channel) ? MRDMODE_INTR_CH1 :
545                                                             MRDMODE_INTR_CH0;
546         u8 dma_stat = hwif->INB(hwif->dma_status);
547
548         (void) pci_read_config_byte(dev, MRDMODE, &dma_alt_stat);
549 #ifdef DEBUG
550         printk("%s: dma_stat: 0x%02x dma_alt_stat: "
551                 "0x%02x mask: 0x%02x\n", drive->name,
552                 dma_stat, dma_alt_stat, mask);
553 #endif
554         if (!(dma_alt_stat & mask))
555                 return 0;
556
557         /* return 1 if INTR asserted */
558         if ((dma_stat & 4) == 4)
559                 return 1;
560
561         return 0;
562 }
563
564 /*
565  * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
566  * event order for DMA transfers.
567  */
568
569 static int cmd646_1_ide_dma_end (ide_drive_t *drive)
570 {
571         ide_hwif_t *hwif = HWIF(drive);
572         u8 dma_stat = 0, dma_cmd = 0;
573
574         drive->waiting_for_dma = 0;
575         /* get DMA status */
576         dma_stat = hwif->INB(hwif->dma_status);
577         /* read DMA command state */
578         dma_cmd = hwif->INB(hwif->dma_command);
579         /* stop DMA */
580         hwif->OUTB((dma_cmd & ~1), hwif->dma_command);
581         /* clear the INTR & ERROR bits */
582         hwif->OUTB(dma_stat|6, hwif->dma_status);
583         /* and free any DMA resources */
584         ide_destroy_dmatable(drive);
585         /* verify good DMA status */
586         return (dma_stat & 7) != 4;
587 }
588
589 static unsigned int __devinit init_chipset_cmd64x(struct pci_dev *dev, const char *name)
590 {
591         u32 class_rev = 0;
592         u8 mrdmode = 0;
593
594         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
595         class_rev &= 0xff;
596
597 #ifdef __i386__
598         if (dev->resource[PCI_ROM_RESOURCE].start) {
599                 pci_write_config_byte(dev, PCI_ROM_ADDRESS, dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
600                 printk(KERN_INFO "%s: ROM enabled at 0x%08lx\n", name, dev->resource[PCI_ROM_RESOURCE].start);
601         }
602 #endif
603
604         switch(dev->device) {
605                 case PCI_DEVICE_ID_CMD_643:
606                         break;
607                 case PCI_DEVICE_ID_CMD_646:
608                         printk(KERN_INFO "%s: chipset revision 0x%02X, ", name, class_rev);
609                         switch(class_rev) {
610                                 case 0x07:
611                                 case 0x05:
612                                         printk("UltraDMA Capable");
613                                         break;
614                                 case 0x03:
615                                         printk("MultiWord DMA Force Limited");
616                                         break;
617                                 case 0x01:
618                                 default:
619                                         printk("MultiWord DMA Limited, IRQ workaround enabled");
620                                         break;
621                                 }
622                         printk("\n");
623                         break;
624                 case PCI_DEVICE_ID_CMD_648:
625                 case PCI_DEVICE_ID_CMD_649:
626                         break;
627                 default:
628                         break;
629         }
630
631         /* Set a good latency timer and cache line size value. */
632         (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
633         /* FIXME: pci_set_master() to ensure a good latency timer value */
634
635         /* Setup interrupts. */
636         (void) pci_read_config_byte(dev, MRDMODE, &mrdmode);
637         mrdmode &= ~(0x30);
638         (void) pci_write_config_byte(dev, MRDMODE, mrdmode);
639
640         /* Use MEMORY READ LINE for reads.
641          * NOTE: Although not mentioned in the PCI0646U specs,
642          *       these bits are write only and won't be read
643          *       back as set or not.  The PCI0646U2 specs clarify
644          *       this point.
645          */
646         (void) pci_write_config_byte(dev, MRDMODE, mrdmode | 0x02);
647
648         /* Set reasonable active/recovery/address-setup values. */
649         (void) pci_write_config_byte(dev, ARTTIM0,  0x40);
650         (void) pci_write_config_byte(dev, DRWTIM0,  0x3f);
651         (void) pci_write_config_byte(dev, ARTTIM1,  0x40);
652         (void) pci_write_config_byte(dev, DRWTIM1,  0x3f);
653 #ifdef __i386__
654         (void) pci_write_config_byte(dev, ARTTIM23, 0x1c);
655 #else
656         (void) pci_write_config_byte(dev, ARTTIM23, 0x5c);
657 #endif
658         (void) pci_write_config_byte(dev, DRWTIM23, 0x3f);
659         (void) pci_write_config_byte(dev, DRWTIM3,  0x3f);
660 #ifdef CONFIG_PPC
661         (void) pci_write_config_byte(dev, UDIDETCR0, 0xf0);
662 #endif /* CONFIG_PPC */
663
664 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
665
666         cmd_devs[n_cmd_devs++] = dev;
667
668         if (!cmd64x_proc) {
669                 cmd64x_proc = 1;
670                 ide_pci_create_host_proc("cmd64x", cmd64x_get_info);
671         }
672 #endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
673
674         return 0;
675 }
676
677 static unsigned int __devinit ata66_cmd64x(ide_hwif_t *hwif)
678 {
679         u8 ata66 = 0, mask = (hwif->channel) ? 0x02 : 0x01;
680
681         switch(hwif->pci_dev->device) {
682                 case PCI_DEVICE_ID_CMD_643:
683                 case PCI_DEVICE_ID_CMD_646:
684                         return ata66;
685                 default:
686                         break;
687         }
688         pci_read_config_byte(hwif->pci_dev, BMIDECSR, &ata66);
689         return (ata66 & mask) ? 1 : 0;
690 }
691
692 static void __devinit init_hwif_cmd64x(ide_hwif_t *hwif)
693 {
694         struct pci_dev *dev     = hwif->pci_dev;
695         unsigned int class_rev;
696
697         hwif->autodma = 0;
698         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
699         class_rev &= 0xff;
700
701         hwif->tuneproc  = &cmd64x_tuneproc;
702         hwif->speedproc = &cmd64x_tune_chipset;
703
704         if (!hwif->dma_base) {
705                 hwif->drives[0].autotune = 1;
706                 hwif->drives[1].autotune = 1;
707                 return;
708         }
709
710         hwif->atapi_dma = 1;
711
712         hwif->ultra_mask = 0x3f;
713         hwif->mwdma_mask = 0x07;
714         hwif->swdma_mask = 0x07;
715
716         if (dev->device == PCI_DEVICE_ID_CMD_643)
717                 hwif->ultra_mask = 0x80;
718         if (dev->device == PCI_DEVICE_ID_CMD_646)
719                 hwif->ultra_mask = (class_rev > 0x04) ? 0x07 : 0x80;
720         if (dev->device == PCI_DEVICE_ID_CMD_648)
721                 hwif->ultra_mask = 0x1f;
722
723         hwif->ide_dma_check = &cmd64x_config_drive_for_dma;
724         if (!(hwif->udma_four))
725                 hwif->udma_four = ata66_cmd64x(hwif);
726
727         if (dev->device == PCI_DEVICE_ID_CMD_646) {
728                 hwif->chipset = ide_cmd646;
729                 if (class_rev == 0x01) {
730                         hwif->ide_dma_end = &cmd646_1_ide_dma_end;
731                 } else {
732                         hwif->ide_dma_end = &cmd64x_ide_dma_end;
733                         hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
734                 }
735         } else {
736                 hwif->ide_dma_end = &cmd64x_ide_dma_end;
737                 hwif->ide_dma_test_irq = &cmd64x_ide_dma_test_irq;
738         }
739
740
741         if (!noautodma)
742                 hwif->autodma = 1;
743         hwif->drives[0].autodma = hwif->autodma;
744         hwif->drives[1].autodma = hwif->autodma;
745 }
746
747 static int __devinit cmd64x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
748 {
749         ide_setup_pci_device(dev, &cmd64x_chipsets[id->driver_data]);
750         return 0;
751 }
752
753 static struct pci_device_id cmd64x_pci_tbl[] = {
754         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_643, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
755         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
756         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_648, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
757         { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_649, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
758         { 0, },
759 };
760 MODULE_DEVICE_TABLE(pci, cmd64x_pci_tbl);
761
762 static struct pci_driver driver = {
763         .name           = "CMD64x IDE",
764         .id_table       = cmd64x_pci_tbl,
765         .probe          = cmd64x_init_one,
766 };
767
768 static int cmd64x_ide_init(void)
769 {
770         return ide_pci_register_driver(&driver);
771 }
772
773 module_init(cmd64x_ide_init);
774
775 MODULE_AUTHOR("Eddie Dost, David Miller, Andre Hedrick");
776 MODULE_DESCRIPTION("PCI driver module for CMD64x IDE");
777 MODULE_LICENSE("GPL");