1 /* $Id: cmd646.c,v 1.15 1999/07/23 01:48:37 davem Exp $
2 * cmd646.c: Enable interrupts at initialization time on Ultra/PCI machines.
3 * Note, this driver is not used at all on other systems because
4 * there the "BIOS" has done all of the following already.
5 * Due to massive hardware bugs, UltraDMA is only supported
6 * on the 646U2 and not on the 646U.
8 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
9 * Copyright (C) 1998 David S. Miller (davem@dm.cobaltmicro.com)
12 #include <linux/types.h>
13 #include <linux/pci.h>
14 #include <linux/delay.h>
15 #include <linux/hdreg.h>
16 #include <linux/ide.h>
20 static int cmd646_config_drive_for_dma(ide_drive_t
*drive
)
22 struct hd_driveid
*id
= drive
->id
;
23 ide_hwif_t
*hwif
= HWIF(drive
);
25 /* Even if the drive is not _currently_ in a DMA
26 * mode, we succeed, and we'll enable it manually
27 * below in cmd646_dma_onoff.
29 * This is done for disks only, CDROMs and other
30 * IDE devices are just too quirky.
33 ((id
->capability
& 1) != 0) &&
35 (drive
->media
== ide_disk
)) {
36 if(id
->field_valid
& 0x0004) {
37 if(id
->dma_ultra
& 0x0007)
38 return hwif
->dmaproc(ide_dma_on
, drive
);
40 if(id
->field_valid
& 0x0002)
41 if((id
->dma_mword
& 0x0004) || (id
->dma_1word
& 0x0004))
42 return hwif
->dmaproc(ide_dma_on
, drive
);
44 return hwif
->dmaproc(ide_dma_off_quietly
, drive
);
47 /* This is fun. -DaveM */
48 #define IDE_SETXFER SETFEATURES_XFER
49 #define IDE_SETFEATURE WIN_SETFEATURES
50 #define IDE_DMA2_ENABLE XFER_MW_DMA_2
51 #define IDE_DMA1_ENABLE XFER_MW_DMA_1
52 #define IDE_DMA0_ENABLE XFER_MW_DMA_0
53 #define IDE_UDMA2_ENABLE XFER_UDMA_2
54 #define IDE_UDMA1_ENABLE XFER_UDMA_1
55 #define IDE_UDMA0_ENABLE XFER_UDMA_0
57 static __inline__
unsigned char dma2_bits_to_command(unsigned char bits
)
60 return IDE_DMA2_ENABLE
;
62 return IDE_DMA1_ENABLE
;
63 return IDE_DMA0_ENABLE
;
66 static __inline__
unsigned char udma2_bits_to_command(unsigned char bits
)
69 return IDE_UDMA2_ENABLE
;
71 return IDE_UDMA1_ENABLE
;
72 return IDE_UDMA0_ENABLE
;
75 static __inline__
int wait_for_ready(ide_drive_t
*drive
)
83 printk("STAT(%2x) ", stat
);
84 if(!(stat
& BUSY_STAT
)) {
85 if((stat
& READY_STAT
) || (stat
& ERR_STAT
))
90 if((stat
& ERR_STAT
) || timeout
<= 0)
95 static void cmd646_do_setfeature(ide_drive_t
*drive
, byte command
)
98 (void) ide_config_drive_speed(drive
, command
);
106 old_select
= IN_BYTE(IDE_SELECT_REG
);
107 OUT_BYTE(drive
->select
.all
, IDE_SELECT_REG
);
109 OUT_BYTE(IDE_SETXFER
, IDE_FEATURE_REG
);
111 OUT_BYTE(command
, IDE_NSECTOR_REG
);
113 if(wait_for_ready(drive
))
115 printk("SETFEATURE ");
116 OUT_BYTE(IDE_SETFEATURE
, IDE_COMMAND_REG
);
118 (void) wait_for_ready(drive
);
120 OUT_BYTE(old_select
, IDE_SELECT_REG
);
121 restore_flags(flags
);
125 static void cmd646_dma2_enable(ide_drive_t
*drive
, unsigned long dma_base
)
127 byte unit
= (drive
->select
.b
.unit
& 0x01);
128 byte bits
= (drive
->id
->dma_mword
| drive
->id
->dma_1word
) & 0x07;
130 printk("CMD646: MDMA enable [");
131 if((((drive
->id
->dma_mword
& 0x0007) << 8) !=
132 (drive
->id
->dma_mword
& 0x0700)))
133 cmd646_do_setfeature(drive
, dma2_bits_to_command(bits
));
135 outb(inb(dma_base
+2)|(1<<(5+unit
)), dma_base
+2);
139 static void cmd646_udma_enable(ide_drive_t
*drive
, unsigned long dma_base
)
141 byte unit
= (drive
->select
.b
.unit
& 0x01);
142 byte udma_ctrl
, bits
= drive
->id
->dma_ultra
& 0x07;
143 byte udma_timing_bits
;
145 printk("CMD646: UDMA enable [");
146 if(((drive
->id
->dma_ultra
& 0x0007) << 8) !=
147 (drive
->id
->dma_ultra
& 0x0700))
148 cmd646_do_setfeature(drive
, udma2_bits_to_command(bits
));
150 /* Enable DMA and UltraDMA */
152 outb(inb(dma_base
+2)|(1<<(5+unit
)), dma_base
+2);
154 udma_ctrl
= inb(dma_base
+ 3);
156 /* Put this channel into UDMA mode. */
157 printk("UDMA_CTRL ");
158 udma_ctrl
|= (1 << unit
);
160 /* Set UDMA2 usable timings. */
162 udma_timing_bits
= 0x10;
164 udma_timing_bits
= 0x20;
166 udma_timing_bits
= 0x30;
167 udma_ctrl
&= ~(0x30 << (unit
* 2));
168 udma_ctrl
|= (udma_timing_bits
<< (unit
* 2));
170 outb(udma_ctrl
, dma_base
+3);
174 static int cmd646_dma_onoff(ide_drive_t
*drive
, int enable
)
177 ide_hwif_t
*hwif
= HWIF(drive
);
178 unsigned long dma_base
= hwif
->dma_base
;
179 struct hd_driveid
*id
= drive
->id
;
180 unsigned int class_rev
;
182 /* UltraDMA only supported on PCI646U and PCI646U2,
183 * which correspond to revisions 0x03 and 0x05 respectively.
184 * Actually, although the CMD tech support people won't
185 * tell me the details, the 0x03 revision cannot support
186 * UDMA correctly without hardware modifications, and even
187 * then it only works with Quantum disks due to some
188 * hold time assumptions in the 646U part which are fixed
190 * So we only do UltraDMA on revision 0x05 chipsets.
192 pci_read_config_dword(hwif
->pci_dev
,
196 if((class_rev
== 0x05) &&
197 (id
->field_valid
& 0x0004) &&
198 (id
->dma_ultra
& 0x07)) {
199 /* UltraDMA modes. */
200 cmd646_udma_enable(drive
, dma_base
);
202 /* Normal MultiWord DMA modes. */
203 cmd646_dma2_enable(drive
, dma_base
);
206 drive
->using_dma
= enable
;
210 static int cmd646_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
)
212 if(func
== ide_dma_check
)
213 return cmd646_config_drive_for_dma(drive
);
214 else if(func
== ide_dma_on
|| func
== ide_dma_off
|| func
== ide_dma_off_quietly
)
215 return cmd646_dma_onoff(drive
, (func
== ide_dma_on
));
217 /* Other cases are done by generic IDE-DMA code. */
218 return ide_dmaproc(func
, drive
);
222 * ASUS P55T2P4D with CMD646 chipset revision 0x01 requires the old
223 * event order for DMA transfers.
225 static int cmd646_1_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
)
227 ide_hwif_t
*hwif
= HWIF(drive
);
228 unsigned long dma_base
= hwif
->dma_base
;
231 if (func
== ide_dma_end
) {
232 drive
->waiting_for_dma
= 0;
233 dma_stat
= inb(dma_base
+2); /* get DMA status */
234 outb(inb(dma_base
)&~1, dma_base
); /* stop DMA */
235 outb(dma_stat
|6, dma_base
+2); /* clear the INTR & ERROR bits */
236 return (dma_stat
& 7) != 4; /* verify good DMA status */
239 /* Other cases are done by generic IDE-DMA code. */
240 return cmd646_dmaproc(func
, drive
);
243 void __init
ide_init_cmd646 (ide_hwif_t
*hwif
)
245 struct pci_dev
*dev
= hwif
->pci_dev
;
246 unsigned char mrdmode
;
247 unsigned int class_rev
;
249 pci_read_config_dword(hwif
->pci_dev
, PCI_CLASS_REVISION
, &class_rev
);
252 hwif
->chipset
= ide_cmd646
;
254 /* Set a good latency timer and cache line size value. */
255 (void) pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, 64);
257 (void) pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
, 0x10);
260 /* Setup interrupts. */
261 (void) pci_read_config_byte(dev
, 0x71, &mrdmode
);
263 (void) pci_write_config_byte(dev
, 0x71, mrdmode
);
265 /* Use MEMORY READ LINE for reads.
266 * NOTE: Although not mentioned in the PCI0646U specs,
267 * these bits are write only and won't be read
268 * back as set or not. The PCI0646U2 specs clarify
271 (void) pci_write_config_byte(dev
, 0x71, mrdmode
| 0x02);
273 /* Set reasonable active/recovery/address-setup values. */
274 (void) pci_write_config_byte(dev
, 0x53, 0x40);
275 (void) pci_write_config_byte(dev
, 0x54, 0x3f);
276 (void) pci_write_config_byte(dev
, 0x55, 0x40);
277 (void) pci_write_config_byte(dev
, 0x56, 0x3f);
278 (void) pci_write_config_byte(dev
, 0x57, 0x5c);
279 (void) pci_write_config_byte(dev
, 0x58, 0x3f);
280 (void) pci_write_config_byte(dev
, 0x5b, 0x3f);
282 if (hwif
->dma_base
) {
283 if (class_rev
== 0x01) {
284 hwif
->dmaproc
= &cmd646_1_dmaproc
;
286 hwif
->dmaproc
= &cmd646_dmaproc
;