1 /* $Id: cmd64x.c,v 1.21 2000/01/30 23:23:16
3 * cmd64x.c: Enable interrupts at initialization time on Ultra/PCI machines.
4 * Note, this driver is not used at all on other systems because
5 * there the "BIOS" has done all of the following already.
6 * Due to massive hardware bugs, UltraDMA is only supported
7 * on the 646U2 and not on the 646U.
9 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
10 * Copyright (C) 1998 David S. Miller (davem@redhat.com)
11 * Copyright (C) 1999-2000 Andre Hedrick (andre@suse.com)
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/hdreg.h>
18 #include <linux/ide.h>
21 #include "ide_modes.h"
27 #define cmdprintk(x...) printk(##x)
29 #define cmdprintk(x...)
33 * CMD64x specific registers definition.
37 #define CNTRL_DIS_RA0 0x40
38 #define CNTRL_DIS_RA1 0x80
39 #define CNTRL_ENA_2ND 0x08
47 #define ARTTIM23_DIS_RA2 0x04
48 #define ARTTIM23_DIS_RA3 0x08
56 #define DISPLAY_CMD64X_TIMINGS
58 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
59 #include <linux/stat.h>
60 #include <linux/proc_fs.h>
62 static int cmd64x_get_info(char *, char **, off_t
, int);
63 extern int (*cmd64x_display_info
)(char *, char **, off_t
, int); /* ide-proc.c */
64 extern char *ide_media_verbose(ide_drive_t
*);
65 static struct pci_dev
*bmide_dev
;
67 static int cmd64x_get_info (char *buffer
, char **addr
, off_t offset
, int count
)
70 u32 bibma
= bmide_dev
->resource
[4].start
;
73 switch(bmide_dev
->device
) {
74 case PCI_DEVICE_ID_CMD_648
:
75 p
+= sprintf(p
, "\n CMD648 Chipset.\n");
77 case PCI_DEVICE_ID_CMD_646
:
78 p
+= sprintf(p
, "\n CMD646 Chipset.\n");
80 case PCI_DEVICE_ID_CMD_643
:
81 p
+= sprintf(p
, "\n CMD643 Chipset.\n");
84 p
+= sprintf(p
, "\n CMD64? Chipse.\n");
89 * at that point bibma+0x2 et bibma+0xa are byte registers
92 c0
= inb_p((unsigned short)bibma
+ 0x02);
93 c1
= inb_p((unsigned short)bibma
+ 0x0a);
94 p
+= sprintf(p
, "--------------- Primary Channel ---------------- Secondary Channel -------------\n");
95 p
+= sprintf(p
, " %sabled %sabled\n",
96 (c0
&0x80) ? "dis" : " en",
97 (c1
&0x80) ? "dis" : " en");
98 p
+= sprintf(p
, "--------------- drive0 --------- drive1 -------- drive0 ---------- drive1 ------\n");
99 p
+= sprintf(p
, "DMA enabled: %s %s %s %s\n",
100 (c0
&0x20) ? "yes" : "no ", (c0
&0x40) ? "yes" : "no ",
101 (c1
&0x20) ? "yes" : "no ", (c1
&0x40) ? "yes" : "no " );
103 p
+= sprintf(p
, "UDMA\n");
104 p
+= sprintf(p
, "DMA\n");
105 p
+= sprintf(p
, "PIO\n");
107 return p
-buffer
; /* => must be less than 4k! */
109 #endif /* defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS) */
111 byte cmd64x_proc
= 0;
114 * Registers and masks for easy access by drive index:
117 static byte prefetch_regs
[4] = {CNTRL
, CNTRL
, ARTTIM23
, ARTTIM23
};
118 static byte prefetch_masks
[4] = {CNTRL_DIS_RA0
, CNTRL_DIS_RA1
, ARTTIM23_DIS_RA2
, ARTTIM23_DIS_RA3
};
122 * This routine writes the prepared setup/active/recovery counts
123 * for a drive into the cmd646 chipset registers to active them.
125 static void program_drive_counts (ide_drive_t
*drive
, int setup_count
, int active_count
, int recovery_count
)
128 ide_drive_t
*drives
= HWIF(drive
)->drives
;
130 static const byte setup_counts
[] = {0x40, 0x40, 0x40, 0x80, 0, 0xc0};
131 static const byte recovery_counts
[] =
132 {15, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0};
133 static const byte arttim_regs
[2][2] = {
134 { ARTTIM0
, ARTTIM1
},
135 { ARTTIM23
, ARTTIM23
}
137 static const byte drwtim_regs
[2][2] = {
138 { DRWTIM0
, DRWTIM1
},
141 int channel
= (int) HWIF(drive
)->channel
;
142 int slave
= (drives
!= drive
); /* Is this really the best way to determine this?? */
144 cmdprintk("program_drive_count parameters = s(%d),a(%d),r(%d),p(%d)\n", setup_count
,
145 active_count
, recovery_count
, drive
->present
);
147 * Set up address setup count registers.
148 * Primary interface has individual count/timing registers for
149 * each drive. Secondary interface has one common set of registers,
150 * for address setup so we merge these timings, using the slowest
154 drive
->drive_data
= setup_count
;
155 setup_count
= IDE_MAX(drives
[0].drive_data
, drives
[1].drive_data
);
156 cmdprintk("Secondary interface, setup_count = %d\n", setup_count
);
160 * Convert values to internal chipset representation
162 setup_count
= (setup_count
> 5) ? 0xc0 : (int) setup_counts
[setup_count
];
163 active_count
&= 0xf; /* Remember, max value is 16 */
164 recovery_count
= (int) recovery_counts
[recovery_count
];
166 cmdprintk("Final values = %d,%d,%d\n", setup_count
, active_count
, recovery_count
);
169 * Now that everything is ready, program the new timings
171 __save_flags (flags
);
174 * Program the address_setup clocks into ARTTIM reg,
175 * and then the active/recovery counts into the DRWTIM reg
177 (void) pci_read_config_byte(HWIF(drive
)->pci_dev
, arttim_regs
[channel
][slave
], &temp_b
);
179 (void) pci_write_config_byte(HWIF(drive
)->pci_dev
, arttim_regs
[channel
][slave
],
180 ((byte
) setup_count
) | (temp_b
& 0x3f));
181 (void) pci_write_config_byte(HWIF(drive
)->pci_dev
, drwtim_regs
[channel
][slave
],
182 (byte
) ((active_count
<< 4) | recovery_count
));
184 cmdprintk ("Write %x to %x\n", ((byte
) setup_count
) | (temp_b
& 0x3f), arttim_regs
[channel
][slave
]);
185 cmdprintk ("Write %x to %x\n", (byte
) ((active_count
<< 4) | recovery_count
), drwtim_regs
[channel
][slave
]);
186 __restore_flags(flags
);
190 * Attempts to set the interface PIO mode.
191 * The preferred method of selecting PIO modes (e.g. mode 4) is
192 * "echo 'piomode:4' > /proc/ide/hdx/settings". Special cases are
193 * 8: prefetch off, 9: prefetch on, 255: auto-select best mode.
194 * Called with 255 at boot time.
196 static void cmd64x_tuneproc (ide_drive_t
*drive
, byte mode_wanted
)
198 int setup_time
, active_time
, recovery_time
, clock_time
, pio_mode
, cycle_time
;
199 byte recovery_count2
, cycle_count
;
200 int setup_count
, active_count
, recovery_count
;
201 int bus_speed
= ide_system_bus_speed();
205 switch (mode_wanted
) {
206 case 8: /* set prefetch off */
207 case 9: /* set prefetch on */
209 /*set_prefetch_mode(index, mode_wanted);*/
210 cmdprintk("%s: %sabled cmd640 prefetch\n", drive
->name
, mode_wanted
? "en" : "dis");
214 (void) ide_get_best_pio_mode (drive
, mode_wanted
, 5, &d
);
215 pio_mode
= d
.pio_mode
;
216 cycle_time
= d
.cycle_time
;
219 * I copied all this complicated stuff from cmd640.c and made a few minor changes.
220 * For now I am just going to pray that it is correct.
224 setup_time
= ide_pio_timings
[pio_mode
].setup_time
;
225 active_time
= ide_pio_timings
[pio_mode
].active_time
;
226 recovery_time
= cycle_time
- (setup_time
+ active_time
);
227 clock_time
= 1000 / bus_speed
;
228 cycle_count
= (cycle_time
+ clock_time
- 1) / clock_time
;
230 setup_count
= (setup_time
+ clock_time
- 1) / clock_time
;
232 active_count
= (active_time
+ clock_time
- 1) / clock_time
;
234 recovery_count
= (recovery_time
+ clock_time
- 1) / clock_time
;
235 recovery_count2
= cycle_count
- (setup_count
+ active_count
);
236 if (recovery_count2
> recovery_count
)
237 recovery_count
= recovery_count2
;
238 if (recovery_count
> 16) {
239 active_count
+= recovery_count
- 16;
242 if (active_count
> 16)
243 active_count
= 16; /* maximum allowed by cmd646 */
246 * In a perfect world, we might set the drive pio mode here
247 * (using WIN_SETFEATURE) before continuing.
249 * But we do not, because:
250 * 1) this is the wrong place to do it (proper is do_special() in ide.c)
251 * 2) in practice this is rarely, if ever, necessary
253 program_drive_counts (drive
, setup_count
, active_count
, recovery_count
);
255 printk ("%s: selected cmd646 PIO mode%d (%dns)%s, clocks=%d/%d/%d\n",
256 drive
->name
, pio_mode
, cycle_time
,
257 d
.overridden
? " (overriding vendor mode)" : "",
258 setup_count
, active_count
, recovery_count
);
261 static int config_chipset_for_dma (ide_drive_t
*drive
, unsigned int rev
, byte ultra_66
)
263 struct hd_driveid
*id
= drive
->id
;
264 ide_hwif_t
*hwif
= HWIF(drive
);
265 struct pci_dev
*dev
= hwif
->pci_dev
;
266 unsigned long dma_base
= hwif
->dma_base
;
268 byte unit
= (drive
->select
.b
.unit
& 0x01);
270 byte udma_timing_bits
= 0x00;
271 byte udma_33
= ((rev
>= 0x05) || (ultra_66
)) ? 1 : 0;
272 byte udma_66
= ((id
->hw_config
& 0x2000) && (hwif
->udma_four
)) ? 1 : 0;
273 /* int drive_number = ((hwif->channel ? 2 : 0) + unit); */
276 switch(dev
->device
) {
277 case PCI_DEVICE_ID_CMD_643
:
278 case PCI_DEVICE_ID_CMD_646
:
279 case PCI_DEVICE_ID_CMD_648
:
284 if (drive
->media
!= ide_disk
) {
285 cmdprintk("CMD64X: drive->media != ide_disk at double check, inital check failed!!\n");
286 return ((int) ide_dma_off
);
289 /* UltraDMA only supported on PCI646U and PCI646U2,
290 * which correspond to revisions 0x03, 0x05 and 0x07 respectively.
291 * Actually, although the CMD tech support people won't
292 * tell me the details, the 0x03 revision cannot support
293 * UDMA correctly without hardware modifications, and even
294 * then it only works with Quantum disks due to some
295 * hold time assumptions in the 646U part which are fixed
297 * So we only do UltraDMA on revision 0x05 and 0x07 chipsets.
300 if ((id
->dma_ultra
& 0x0010) && (udma_66
) && (udma_33
)) {
302 udma_timing_bits
= 0x10; /* 2 clock */
303 } else if ((id
->dma_ultra
& 0x0008) && (udma_66
) && (udma_33
)) {
305 udma_timing_bits
= 0x20; /* 3 clock */
306 } else if ((id
->dma_ultra
& 0x0004) && (udma_33
)) {
308 udma_timing_bits
= 0x10; /* 2 clock */
309 } else if ((id
->dma_ultra
& 0x0002) && (udma_33
)) {
311 udma_timing_bits
= 0x20; /* 3 clock */
312 } else if ((id
->dma_ultra
& 0x0001) && (udma_33
)) {
314 udma_timing_bits
= 0x30; /* 4 clock */
315 } else if (id
->dma_mword
& 0x0004) {
316 speed
= XFER_MW_DMA_2
;
317 } else if (id
->dma_mword
& 0x0002) {
318 speed
= XFER_MW_DMA_1
;
319 } else if (id
->dma_mword
& 0x0001) {
320 speed
= XFER_MW_DMA_0
;
321 } else if (id
->dma_1word
& 0x0004) {
322 speed
= XFER_SW_DMA_2
;
323 } else if (id
->dma_1word
& 0x0002) {
324 speed
= XFER_SW_DMA_1
;
325 } else if (id
->dma_1word
& 0x0001) {
326 speed
= XFER_SW_DMA_0
;
328 return ((int) ide_dma_off_quietly
);
331 (void) ide_config_drive_speed(drive
, speed
);
332 outb(inb(dma_base
+2)|(1<<(5+unit
)), dma_base
+2);
334 if (speed
>= XFER_UDMA_0
) {
335 byte udma_ctrl
= inb(dma_base
+ 3);
336 /* Put this channel into UDMA mode. */
337 udma_ctrl
|= (1 << unit
);
338 udma_ctrl
&= ~(0x04 << unit
);
340 udma_ctrl
|= (0x04 << unit
);
341 udma_ctrl
&= ~(0x30 << (unit
* 2));
342 udma_ctrl
|= (udma_timing_bits
<< (unit
* 2));
343 outb(udma_ctrl
, dma_base
+3);
346 rval
= (int)( ((id
->dma_ultra
>> 11) & 3) ? ide_dma_on
:
347 ((id
->dma_ultra
>> 8) & 7) ? ide_dma_on
:
348 ((id
->dma_mword
>> 8) & 7) ? ide_dma_on
:
349 ((id
->dma_1word
>> 8) & 7) ? ide_dma_on
:
350 ide_dma_off_quietly
);
355 static void config_chipset_for_pio (ide_drive_t
*drive
, unsigned int rev
)
357 cmd64x_tuneproc(drive
, 5);
360 static int cmd64x_config_drive_for_dma (ide_drive_t
*drive
)
362 struct hd_driveid
*id
= drive
->id
;
363 ide_hwif_t
*hwif
= HWIF(drive
);
364 struct pci_dev
*dev
= hwif
->pci_dev
;
365 unsigned int class_rev
= 0;
366 byte can_ultra_33
= 0;
367 byte can_ultra_66
= 0;
368 ide_dma_action_t dma_func
= ide_dma_on
;
370 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class_rev
);
373 switch(dev
->device
) {
374 case PCI_DEVICE_ID_CMD_643
:
378 case PCI_DEVICE_ID_CMD_646
:
379 can_ultra_33
= (class_rev
>= 0x05) ? 1 : 0;
382 case PCI_DEVICE_ID_CMD_648
:
387 return hwif
->dmaproc(ide_dma_off
, drive
);
390 if ((id
!= NULL
) && ((id
->capability
& 1) != 0) &&
391 hwif
->autodma
&& (drive
->media
== ide_disk
)) {
392 /* Consult the list of known "bad" drives */
393 if (ide_dmaproc(ide_dma_bad_drive
, drive
)) {
394 dma_func
= ide_dma_off
;
397 dma_func
= ide_dma_off_quietly
;
398 if ((id
->field_valid
& 4) && (can_ultra_33
)) {
399 if (id
->dma_ultra
& 0x001F) {
400 /* Force if Capable UltraDMA */
401 dma_func
= config_chipset_for_dma(drive
, class_rev
, can_ultra_66
);
402 if ((id
->field_valid
& 2) &&
403 (dma_func
!= ide_dma_on
))
406 } else if (id
->field_valid
& 2) {
408 if ((id
->dma_mword
& 0x0007) ||
409 (id
->dma_1word
& 0x0007)) {
410 /* Force if Capable regular DMA modes */
411 dma_func
= config_chipset_for_dma(drive
, class_rev
, 0);
412 if (dma_func
!= ide_dma_on
)
415 } else if (ide_dmaproc(ide_dma_good_drive
, drive
)) {
416 if (id
->eide_dma_time
> 150) {
419 /* Consult the list of known "good" drives */
420 dma_func
= config_chipset_for_dma(drive
, class_rev
, 0);
421 if (dma_func
!= ide_dma_on
)
426 } else if ((id
->capability
& 8) || (id
->field_valid
& 2)) {
428 dma_func
= ide_dma_off_quietly
;
430 config_chipset_for_pio(drive
, class_rev
);
432 return HWIF(drive
)->dmaproc(dma_func
, drive
);
435 static int cmd64x_dmaproc (ide_dma_action_t func
, ide_drive_t
*drive
)
439 return cmd64x_config_drive_for_dma(drive
);
443 /* Other cases are done by generic IDE-DMA code. */
444 return ide_dmaproc(func
, drive
);
448 * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
449 * event order for DMA transfers.
451 static int cmd646_1_dmaproc (ide_dma_action_t func
, ide_drive_t
*drive
)
453 ide_hwif_t
*hwif
= HWIF(drive
);
454 unsigned long dma_base
= hwif
->dma_base
;
457 if (func
== ide_dma_end
) {
458 drive
->waiting_for_dma
= 0;
459 dma_stat
= inb(dma_base
+2); /* get DMA status */
460 outb(inb(dma_base
)&~1, dma_base
); /* stop DMA */
461 outb(dma_stat
|6, dma_base
+2); /* clear the INTR & ERROR bits */
462 ide_destroy_dmatable(drive
); /* and free any DMA resources */
463 return (dma_stat
& 7) != 4; /* verify good DMA status */
466 /* Other cases are done by generic IDE-DMA code. */
467 return cmd64x_dmaproc(func
, drive
);
470 unsigned int __init
pci_init_cmd64x (struct pci_dev
*dev
, const char *name
)
472 unsigned char mrdmode
;
473 unsigned int class_rev
;
475 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class_rev
);
478 switch(dev
->device
) {
479 case PCI_DEVICE_ID_CMD_643
:
481 case PCI_DEVICE_ID_CMD_646
:
482 printk("%s: chipset revision 0x%02X, ", name
, class_rev
);
486 printk("UltraDMA Capable");
489 printk("MultiWord DMA Force Limited");
493 printk("MultiWord DMA Limited, IRQ workaround enabled");
498 case PCI_DEVICE_ID_CMD_648
:
504 /* Set a good latency timer and cache line size value. */
505 (void) pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 64);
507 (void) pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
, 0x10);
510 /* Setup interrupts. */
511 (void) pci_read_config_byte(dev
, MRDMODE
, &mrdmode
);
513 (void) pci_write_config_byte(dev
, MRDMODE
, mrdmode
);
515 /* Use MEMORY READ LINE for reads.
516 * NOTE: Although not mentioned in the PCI0646U specs,
517 * these bits are write only and won't be read
518 * back as set or not. The PCI0646U2 specs clarify
521 (void) pci_write_config_byte(dev
, MRDMODE
, mrdmode
| 0x02);
523 /* Set reasonable active/recovery/address-setup values. */
524 (void) pci_write_config_byte(dev
, ARTTIM0
, 0x40);
525 (void) pci_write_config_byte(dev
, DRWTIM0
, 0x3f);
526 (void) pci_write_config_byte(dev
, ARTTIM1
, 0x40);
527 (void) pci_write_config_byte(dev
, DRWTIM1
, 0x3f);
528 (void) pci_write_config_byte(dev
, ARTTIM23
, 0x5c);
529 (void) pci_write_config_byte(dev
, DRWTIM23
, 0x3f);
530 (void) pci_write_config_byte(dev
, DRWTIM3
, 0x3f);
532 (void) pci_write_config_byte(dev
, ARTTIM23
, 0x1c);
535 #if defined(DISPLAY_CMD64X_TIMINGS) && defined(CONFIG_PROC_FS)
538 cmd64x_display_info
= &cmd64x_get_info
;
539 #endif /* DISPLAY_CMD64X_TIMINGS && CONFIG_PROC_FS */
544 unsigned int __init
ata66_cmd64x (ide_hwif_t
*hwif
)
547 byte mask
= (hwif
->channel
) ? 0x02 : 0x01;
549 pci_read_config_byte(hwif
->pci_dev
, 0x79, &ata66
);
550 return (ata66
& mask
) ? 1 : 0;
553 void __init
ide_init_cmd64x (ide_hwif_t
*hwif
)
555 struct pci_dev
*dev
= hwif
->pci_dev
;
556 unsigned int class_rev
;
558 pci_read_config_dword(dev
, PCI_CLASS_REVISION
, &class_rev
);
561 hwif
->tuneproc
= &cmd64x_tuneproc
;
563 if (!hwif
->dma_base
) {
564 hwif
->drives
[0].autotune
= 1;
565 hwif
->drives
[1].autotune
= 1;
569 switch(dev
->device
) {
570 case PCI_DEVICE_ID_CMD_643
:
571 hwif
->dmaproc
= &cmd64x_dmaproc
;
573 case PCI_DEVICE_ID_CMD_646
:
574 hwif
->chipset
= ide_cmd646
;
575 if (class_rev
== 0x01) {
576 hwif
->dmaproc
= &cmd646_1_dmaproc
;
578 hwif
->dmaproc
= &cmd64x_dmaproc
;
581 case PCI_DEVICE_ID_CMD_648
:
582 hwif
->dmaproc
= &cmd64x_dmaproc
;