2 * linux/drivers/block/ide-features.c Version 0.04 June 9, 2000
4 * Copyright (C) 1999-2000 Linus Torvalds & authors (see below)
6 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
8 * Extracts if ide.c to address the evolving transfer rate code for
9 * the SETFEATURES_XFER callouts. Various parts of any given function
10 * are credited to previous ATA-IDE maintainers.
12 * Auto-CRC downgrade for Ultra DMA(ing)
14 * May be copied or modified under the terms of the GNU General Public License
17 #include <linux/config.h>
18 #define __NO_VERSION__
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/blkpg.h>
30 #include <linux/malloc.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/hdreg.h>
34 #include <linux/ide.h>
36 #include <asm/byteorder.h>
38 #include <asm/uaccess.h>
40 #include <asm/bitops.h>
42 #define SETFEATURES_CONTROL_REG (0) /* some arch's may need */
45 * A Verbose noise maker for debugging on the attempted transfer rates.
47 char *ide_xfer_verbose (byte xfer_rate
)
50 case XFER_UDMA_7
: return("UDMA 7");
51 case XFER_UDMA_6
: return("UDMA 6");
52 case XFER_UDMA_5
: return("UDMA 5");
53 case XFER_UDMA_4
: return("UDMA 4");
54 case XFER_UDMA_3
: return("UDMA 3");
55 case XFER_UDMA_2
: return("UDMA 2");
56 case XFER_UDMA_1
: return("UDMA 1");
57 case XFER_UDMA_0
: return("UDMA 0");
58 case XFER_MW_DMA_2
: return("MW DMA 2");
59 case XFER_MW_DMA_1
: return("MW DMA 1");
60 case XFER_MW_DMA_0
: return("MW DMA 0");
61 case XFER_SW_DMA_2
: return("SW DMA 2");
62 case XFER_SW_DMA_1
: return("SW DMA 1");
63 case XFER_SW_DMA_0
: return("SW DMA 0");
64 case XFER_PIO_4
: return("PIO 4");
65 case XFER_PIO_3
: return("PIO 3");
66 case XFER_PIO_2
: return("PIO 2");
67 case XFER_PIO_1
: return("PIO 1");
68 case XFER_PIO_0
: return("PIO 0");
69 case XFER_PIO_SLOW
: return("PIO SLOW");
70 default: return("XFER ERROR");
77 char *ide_media_verbose (ide_drive_t
*drive
)
79 switch (drive
->media
) {
80 case ide_scsi
: return("scsi ");
81 case ide_disk
: return("disk ");
82 case ide_optical
: return("optical");
83 case ide_cdrom
: return("cdrom ");
84 case ide_tape
: return("tape ");
85 case ide_floppy
: return("floppy ");
86 default: return("???????");
91 * A Verbose noise maker for debugging on the attempted dmaing calls.
93 char *ide_dmafunc_verbose (ide_dma_action_t dmafunc
)
96 case ide_dma_read
: return("ide_dma_read");
97 case ide_dma_write
: return("ide_dma_write");
98 case ide_dma_begin
: return("ide_dma_begin");
99 case ide_dma_end
: return("ide_dma_end:");
100 case ide_dma_check
: return("ide_dma_check");
101 case ide_dma_on
: return("ide_dma_on");
102 case ide_dma_off
: return("ide_dma_off");
103 case ide_dma_off_quietly
: return("ide_dma_off_quietly");
104 case ide_dma_test_irq
: return("ide_dma_test_irq");
105 case ide_dma_bad_drive
: return("ide_dma_bad_drive");
106 case ide_dma_good_drive
: return("ide_dma_good_drive");
107 case ide_dma_verbose
: return("ide_dma_verbose");
108 case ide_dma_retune
: return("ide_dma_retune");
109 case ide_dma_lostirq
: return("ide_dma_lostirq");
110 case ide_dma_timeout
: return("ide_dma_timeout");
111 default: return("unknown");
118 byte
ide_auto_reduce_xfer (ide_drive_t
*drive
)
120 if (!drive
->crc_count
)
121 return drive
->current_speed
;
122 drive
->crc_count
= 0;
124 switch(drive
->current_speed
) {
125 case XFER_UDMA_7
: return XFER_UDMA_6
;
126 case XFER_UDMA_6
: return XFER_UDMA_5
;
127 case XFER_UDMA_5
: return XFER_UDMA_4
;
128 case XFER_UDMA_4
: return XFER_UDMA_3
;
129 case XFER_UDMA_3
: return XFER_UDMA_2
;
130 case XFER_UDMA_2
: return XFER_UDMA_1
;
131 case XFER_UDMA_1
: return XFER_UDMA_0
;
133 if (drive
->id
->dma_mword
& 0x0004) return XFER_MW_DMA_2
;
134 else if (drive
->id
->dma_mword
& 0x0002) return XFER_MW_DMA_1
;
135 else if (drive
->id
->dma_mword
& 0x0001) return XFER_MW_DMA_0
;
136 else return XFER_PIO_4
;
137 case XFER_MW_DMA_2
: return XFER_MW_DMA_1
;
138 case XFER_MW_DMA_1
: return XFER_MW_DMA_0
;
140 if (drive
->id
->dma_1word
& 0x0004) return XFER_SW_DMA_2
;
141 else if (drive
->id
->dma_1word
& 0x0002) return XFER_SW_DMA_1
;
142 else if (drive
->id
->dma_1word
& 0x0001) return XFER_SW_DMA_0
;
143 else return XFER_PIO_4
;
144 case XFER_SW_DMA_2
: return XFER_SW_DMA_1
;
145 case XFER_SW_DMA_1
: return XFER_SW_DMA_0
;
150 case XFER_PIO_4
: return XFER_PIO_3
;
151 case XFER_PIO_3
: return XFER_PIO_2
;
152 case XFER_PIO_2
: return XFER_PIO_1
;
153 case XFER_PIO_1
: return XFER_PIO_0
;
155 default: return XFER_PIO_SLOW
;
162 int ide_driveid_update (ide_drive_t
*drive
)
165 * Re-read drive->id for possible DMA mode
166 * change (copied from ide-probe.c)
168 struct hd_driveid
*id
;
169 unsigned long timeout
, irqs
, flags
;
171 probe_irq_off(probe_irq_on());
172 irqs
= probe_irq_on();
173 SELECT_MASK(HWIF(drive
), drive
, 1);
175 OUT_BYTE(drive
->ctl
,IDE_CONTROL_REG
);
177 OUT_BYTE(WIN_IDENTIFY
, IDE_COMMAND_REG
);
178 timeout
= jiffies
+ WAIT_WORSTCASE
;
180 if (0 < (signed long)(jiffies
- timeout
)) {
182 (void) probe_irq_off(irqs
);
183 SELECT_MASK(HWIF(drive
), drive
, 0);
184 return 0; /* drive timed-out */
186 ide_delay_50ms(); /* give drive a breather */
187 } while (IN_BYTE(IDE_ALTSTATUS_REG
) & BUSY_STAT
);
188 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
189 if (!OK_STAT(GET_STAT(),DRQ_STAT
,BAD_R_STAT
)) {
190 SELECT_MASK(HWIF(drive
), drive
, 0);
191 printk("%s: CHECK for good STATUS\n", drive
->name
);
194 __save_flags(flags
); /* local CPU only */
195 __cli(); /* local CPU only; some systems need this */
196 SELECT_MASK(HWIF(drive
), drive
, 0);
197 id
= kmalloc(SECTOR_WORDS
*4, GFP_ATOMIC
);
198 ide_input_data(drive
, id
, SECTOR_WORDS
);
199 (void) GET_STAT(); /* clear drive IRQ */
200 ide__sti(); /* local CPU only */
201 __restore_flags(flags
); /* local CPU only */
204 drive
->id
->dma_ultra
= id
->dma_ultra
;
205 drive
->id
->dma_mword
= id
->dma_mword
;
206 drive
->id
->dma_1word
= id
->dma_1word
;
207 /* anything more ? */
215 * Verify that we are doing an approved SETFEATURES_XFER with respect
216 * to the hardware being able to support request. Since some hardware
217 * can improperly report capabilties, we check to see if the host adapter
218 * in combination with the device (usually a disk) properly detect
219 * and acknowledge each end of the ribbon.
221 int ide_ata66_check (ide_drive_t
*drive
, byte cmd
, byte nsect
, byte feature
)
223 if ((cmd
== WIN_SETFEATURES
) &&
224 (nsect
> XFER_UDMA_2
) &&
225 (feature
== SETFEATURES_XFER
)) {
226 if (!HWIF(drive
)->udma_four
) {
227 printk("%s: Speed warnings UDMA 3/4/5 is not functional.\n", HWIF(drive
)->name
);
230 #ifndef CONFIG_IDEDMA_IVB
231 if ((drive
->id
->hw_config
& 0x6000) == 0) {
232 #else /* !CONFIG_IDEDMA_IVB */
233 if ((drive
->id
->hw_config
& 0x2000) == 0) {
234 #endif /* CONFIG_IDEDMA_IVB */
235 printk("%s: Speed warnings UDMA 3/4/5 is not functional.\n", drive
->name
);
243 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
244 * 1 : Safe to update drive->id DMA registers.
245 * 0 : OOPs not allowed.
247 int set_transfer (ide_drive_t
*drive
, byte cmd
, byte nsect
, byte feature
)
249 if ((cmd
== WIN_SETFEATURES
) &&
250 (nsect
>= XFER_SW_DMA_0
) &&
251 (feature
== SETFEATURES_XFER
) &&
252 (drive
->id
->dma_ultra
||
253 drive
->id
->dma_mword
||
254 drive
->id
->dma_1word
))
261 * All hosts that use the 80c ribbon mus use!
263 byte
eighty_ninty_three (ide_drive_t
*drive
)
265 return ((byte
) ((HWIF(drive
)->udma_four
) &&
266 #ifndef CONFIG_IDEDMA_IVB
267 (drive
->id
->hw_config
& 0x4000) &&
268 #endif /* CONFIG_IDEDMA_IVB */
269 (drive
->id
->hw_config
& 0x2000)) ? 1 : 0);
273 * Similar to ide_wait_stat(), except it never calls ide_error internally.
274 * This is a kludge to handle the new ide_config_drive_speed() function,
275 * and should not otherwise be used anywhere. Eventually, the tuneproc's
276 * should be updated to return ide_startstop_t, in which case we can get
277 * rid of this abomination again. :) -ml
279 * It is gone..........
281 * const char *msg == consider adding for verbose errors.
283 int ide_config_drive_speed (ide_drive_t
*drive
, byte speed
)
285 ide_hwif_t
*hwif
= HWIF(drive
);
289 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
290 byte unit
= (drive
->select
.b
.unit
& 0x01);
291 outb(inb(hwif
->dma_base
+2) & ~(1<<(5+unit
)), hwif
->dma_base
+2);
292 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
295 * Don't use ide_wait_cmd here - it will
296 * attempt to set_geometry and recalibrate,
297 * but for some reason these don't work at
298 * this point (lost interrupt).
301 * Select the drive, and issue the SETFEATURES command
303 disable_irq(hwif
->irq
); /* disable_irq_nosync ?? */
305 SELECT_DRIVE(HWIF(drive
), drive
);
306 SELECT_MASK(HWIF(drive
), drive
, 0);
309 OUT_BYTE(drive
->ctl
| 2, IDE_CONTROL_REG
);
310 OUT_BYTE(speed
, IDE_NSECTOR_REG
);
311 OUT_BYTE(SETFEATURES_XFER
, IDE_FEATURE_REG
);
312 OUT_BYTE(WIN_SETFEATURES
, IDE_COMMAND_REG
);
313 if ((IDE_CONTROL_REG
) && (SETFEATURES_CONTROL_REG
))
314 OUT_BYTE(drive
->ctl
, IDE_CONTROL_REG
);
317 * Wait for drive to become non-BUSY
319 if ((stat
= GET_STAT()) & BUSY_STAT
) {
320 unsigned long flags
, timeout
;
321 __save_flags(flags
); /* local CPU only */
322 ide__sti(); /* local CPU only -- for jiffies */
323 timeout
= jiffies
+ WAIT_CMD
;
324 while ((stat
= GET_STAT()) & BUSY_STAT
) {
325 if (0 < (signed long)(jiffies
- timeout
))
328 __restore_flags(flags
); /* local CPU only */
332 * Allow status to settle, then read it again.
333 * A few rare drives vastly violate the 400ns spec here,
334 * so we'll wait up to 10usec for a "good" status
335 * rather than expensively fail things immediately.
336 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
338 for (i
= 0; i
< 10; i
++) {
340 if (OK_STAT((stat
= GET_STAT()), DRIVE_READY
, BUSY_STAT
|DRQ_STAT
|ERR_STAT
)) {
346 SELECT_MASK(HWIF(drive
), drive
, 0);
348 enable_irq(hwif
->irq
);
351 (void) ide_dump_status(drive
, "set_drive_speed_status", stat
);
355 drive
->id
->dma_ultra
&= ~0xFF00;
356 drive
->id
->dma_mword
&= ~0x0F00;
357 drive
->id
->dma_1word
&= ~0x0F00;
359 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
360 if (speed
> XFER_PIO_4
) {
361 outb(inb(hwif
->dma_base
+2)|(1<<(5+unit
)), hwif
->dma_base
+2);
363 outb(inb(hwif
->dma_base
+2) & ~(1<<(5+unit
)), hwif
->dma_base
+2);
365 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
368 case XFER_UDMA_7
: drive
->id
->dma_ultra
|= 0x8080; break;
369 case XFER_UDMA_6
: drive
->id
->dma_ultra
|= 0x4040; break;
370 case XFER_UDMA_5
: drive
->id
->dma_ultra
|= 0x2020; break;
371 case XFER_UDMA_4
: drive
->id
->dma_ultra
|= 0x1010; break;
372 case XFER_UDMA_3
: drive
->id
->dma_ultra
|= 0x0808; break;
373 case XFER_UDMA_2
: drive
->id
->dma_ultra
|= 0x0404; break;
374 case XFER_UDMA_1
: drive
->id
->dma_ultra
|= 0x0202; break;
375 case XFER_UDMA_0
: drive
->id
->dma_ultra
|= 0x0101; break;
376 case XFER_MW_DMA_2
: drive
->id
->dma_mword
|= 0x0404; break;
377 case XFER_MW_DMA_1
: drive
->id
->dma_mword
|= 0x0202; break;
378 case XFER_MW_DMA_0
: drive
->id
->dma_mword
|= 0x0101; break;
379 case XFER_SW_DMA_2
: drive
->id
->dma_1word
|= 0x0404; break;
380 case XFER_SW_DMA_1
: drive
->id
->dma_1word
|= 0x0202; break;
381 case XFER_SW_DMA_0
: drive
->id
->dma_1word
|= 0x0101; break;
387 EXPORT_SYMBOL(ide_auto_reduce_xfer
);
388 EXPORT_SYMBOL(ide_driveid_update
);
389 EXPORT_SYMBOL(ide_ata66_check
);
390 EXPORT_SYMBOL(set_transfer
);
391 EXPORT_SYMBOL(eighty_ninty_three
);
392 EXPORT_SYMBOL(ide_config_drive_speed
);