File: /usr/src/linux/drivers/ide/ide-features.c
1 /*
2 * linux/drivers/block/ide-features.c Version 0.04 June 9, 2000
3 *
4 * Copyright (C) 1999-2000 Linus Torvalds & authors (see below)
5 *
6 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
7 *
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.
11 *
12 * Auto-CRC downgrade for Ultra DMA(ing)
13 *
14 * May be copied or modified under the terms of the GNU General Public License
15 */
16
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>
24 #include <linux/mm.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/slab.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/hdreg.h>
34 #include <linux/ide.h>
35
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/bitops.h>
41
42 /*
43 * A Verbose noise maker for debugging on the attempted transfer rates.
44 */
45 char *ide_xfer_verbose (byte xfer_rate)
46 {
47 switch(xfer_rate) {
48 case XFER_UDMA_7: return("UDMA 7");
49 case XFER_UDMA_6: return("UDMA 6");
50 case XFER_UDMA_5: return("UDMA 5");
51 case XFER_UDMA_4: return("UDMA 4");
52 case XFER_UDMA_3: return("UDMA 3");
53 case XFER_UDMA_2: return("UDMA 2");
54 case XFER_UDMA_1: return("UDMA 1");
55 case XFER_UDMA_0: return("UDMA 0");
56 case XFER_MW_DMA_2: return("MW DMA 2");
57 case XFER_MW_DMA_1: return("MW DMA 1");
58 case XFER_MW_DMA_0: return("MW DMA 0");
59 case XFER_SW_DMA_2: return("SW DMA 2");
60 case XFER_SW_DMA_1: return("SW DMA 1");
61 case XFER_SW_DMA_0: return("SW DMA 0");
62 case XFER_PIO_4: return("PIO 4");
63 case XFER_PIO_3: return("PIO 3");
64 case XFER_PIO_2: return("PIO 2");
65 case XFER_PIO_1: return("PIO 1");
66 case XFER_PIO_0: return("PIO 0");
67 case XFER_PIO_SLOW: return("PIO SLOW");
68 default: return("XFER ERROR");
69 }
70 }
71
72 /*
73 *
74 */
75 char *ide_media_verbose (ide_drive_t *drive)
76 {
77 switch (drive->media) {
78 case ide_scsi: return("scsi ");
79 case ide_disk: return("disk ");
80 case ide_optical: return("optical");
81 case ide_cdrom: return("cdrom ");
82 case ide_tape: return("tape ");
83 case ide_floppy: return("floppy ");
84 default: return("???????");
85 }
86 }
87
88 /*
89 * A Verbose noise maker for debugging on the attempted dmaing calls.
90 */
91 char *ide_dmafunc_verbose (ide_dma_action_t dmafunc)
92 {
93 switch (dmafunc) {
94 case ide_dma_read: return("ide_dma_read");
95 case ide_dma_write: return("ide_dma_write");
96 case ide_dma_begin: return("ide_dma_begin");
97 case ide_dma_end: return("ide_dma_end:");
98 case ide_dma_check: return("ide_dma_check");
99 case ide_dma_on: return("ide_dma_on");
100 case ide_dma_off: return("ide_dma_off");
101 case ide_dma_off_quietly: return("ide_dma_off_quietly");
102 case ide_dma_test_irq: return("ide_dma_test_irq");
103 case ide_dma_bad_drive: return("ide_dma_bad_drive");
104 case ide_dma_good_drive: return("ide_dma_good_drive");
105 case ide_dma_verbose: return("ide_dma_verbose");
106 case ide_dma_retune: return("ide_dma_retune");
107 case ide_dma_lostirq: return("ide_dma_lostirq");
108 case ide_dma_timeout: return("ide_dma_timeout");
109 default: return("unknown");
110 }
111 }
112
113 /*
114 *
115 */
116 byte ide_auto_reduce_xfer (ide_drive_t *drive)
117 {
118 if (!drive->crc_count)
119 return drive->current_speed;
120 drive->crc_count = 0;
121
122 switch(drive->current_speed) {
123 case XFER_UDMA_7: return XFER_UDMA_6;
124 case XFER_UDMA_6: return XFER_UDMA_5;
125 case XFER_UDMA_5: return XFER_UDMA_4;
126 case XFER_UDMA_4: return XFER_UDMA_3;
127 case XFER_UDMA_3: return XFER_UDMA_2;
128 case XFER_UDMA_2: return XFER_UDMA_1;
129 case XFER_UDMA_1: return XFER_UDMA_0;
130 case XFER_UDMA_0:
131 if (drive->id->dma_mword & 0x0004) return XFER_MW_DMA_2;
132 else if (drive->id->dma_mword & 0x0002) return XFER_MW_DMA_1;
133 else if (drive->id->dma_mword & 0x0001) return XFER_MW_DMA_0;
134 else return XFER_PIO_4;
135 case XFER_MW_DMA_2: return XFER_MW_DMA_1;
136 case XFER_MW_DMA_1: return XFER_MW_DMA_0;
137 case XFER_MW_DMA_0:
138 if (drive->id->dma_1word & 0x0004) return XFER_SW_DMA_2;
139 else if (drive->id->dma_1word & 0x0002) return XFER_SW_DMA_1;
140 else if (drive->id->dma_1word & 0x0001) return XFER_SW_DMA_0;
141 else return XFER_PIO_4;
142 case XFER_SW_DMA_2: return XFER_SW_DMA_1;
143 case XFER_SW_DMA_1: return XFER_SW_DMA_0;
144 case XFER_SW_DMA_0:
145 {
146 return XFER_PIO_4;
147 }
148 case XFER_PIO_4: return XFER_PIO_3;
149 case XFER_PIO_3: return XFER_PIO_2;
150 case XFER_PIO_2: return XFER_PIO_1;
151 case XFER_PIO_1: return XFER_PIO_0;
152 case XFER_PIO_0:
153 default: return XFER_PIO_SLOW;
154 }
155 }
156
157 /*
158 * Update the
159 */
160 int ide_driveid_update (ide_drive_t *drive)
161 {
162 /*
163 * Re-read drive->id for possible DMA mode
164 * change (copied from ide-probe.c)
165 */
166 struct hd_driveid *id;
167 unsigned long timeout, flags;
168
169 SELECT_MASK(HWIF(drive), drive, 1);
170 if (IDE_CONTROL_REG)
171 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
172 ide_delay_50ms();
173 OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
174 timeout = jiffies + WAIT_WORSTCASE;
175 do {
176 if (0 < (signed long)(jiffies - timeout)) {
177 SELECT_MASK(HWIF(drive), drive, 0);
178 return 0; /* drive timed-out */
179 }
180 ide_delay_50ms(); /* give drive a breather */
181 } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
182 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
183 if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
184 SELECT_MASK(HWIF(drive), drive, 0);
185 printk("%s: CHECK for good STATUS\n", drive->name);
186 return 0;
187 }
188 __save_flags(flags); /* local CPU only */
189 __cli(); /* local CPU only; some systems need this */
190 SELECT_MASK(HWIF(drive), drive, 0);
191 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
192 if (!id) {
193 __restore_flags(flags); /* local CPU only */
194 return 0;
195 }
196 ide_input_data(drive, id, SECTOR_WORDS);
197 (void) GET_STAT(); /* clear drive IRQ */
198 ide__sti(); /* local CPU only */
199 __restore_flags(flags); /* local CPU only */
200 ide_fix_driveid(id);
201 if (id) {
202 drive->id->dma_ultra = id->dma_ultra;
203 drive->id->dma_mword = id->dma_mword;
204 drive->id->dma_1word = id->dma_1word;
205 /* anything more ? */
206 kfree(id);
207 }
208
209 return 1;
210 }
211
212 /*
213 * Verify that we are doing an approved SETFEATURES_XFER with respect
214 * to the hardware being able to support request. Since some hardware
215 * can improperly report capabilties, we check to see if the host adapter
216 * in combination with the device (usually a disk) properly detect
217 * and acknowledge each end of the ribbon.
218 */
219 int ide_ata66_check (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
220 {
221 if ((cmd == WIN_SETFEATURES) &&
222 (nsect > XFER_UDMA_2) &&
223 (feature == SETFEATURES_XFER)) {
224 if (!HWIF(drive)->udma_four) {
225 printk("%s: Speed warnings UDMA 3/4/5 is not functional.\n", HWIF(drive)->name);
226 return 1;
227 }
228 #ifndef CONFIG_IDEDMA_IVB
229 if ((drive->id->hw_config & 0x6000) == 0) {
230 #else /* !CONFIG_IDEDMA_IVB */
231 if (((drive->id->hw_config & 0x2000) == 0) ||
232 ((drive->id->hw_config & 0x4000) == 0)) {
233 #endif /* CONFIG_IDEDMA_IVB */
234 printk("%s: Speed warnings UDMA 3/4/5 is not functional.\n", drive->name);
235 return 1;
236 }
237 }
238 return 0;
239 }
240
241 /*
242 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
243 * 1 : Safe to update drive->id DMA registers.
244 * 0 : OOPs not allowed.
245 */
246 int set_transfer (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
247 {
248 if ((cmd == WIN_SETFEATURES) &&
249 (nsect >= XFER_SW_DMA_0) &&
250 (feature == SETFEATURES_XFER) &&
251 (drive->id->dma_ultra ||
252 drive->id->dma_mword ||
253 drive->id->dma_1word))
254 return 1;
255
256 return 0;
257 }
258
259 /*
260 * All hosts that use the 80c ribbon mus use!
261 */
262 byte eighty_ninty_three (ide_drive_t *drive)
263 {
264 return ((byte) ((HWIF(drive)->udma_four) &&
265 #ifndef CONFIG_IDEDMA_IVB
266 (drive->id->hw_config & 0x4000) &&
267 #endif /* CONFIG_IDEDMA_IVB */
268 (drive->id->hw_config & 0x6000)) ? 1 : 0);
269 }
270
271 /*
272 * Similar to ide_wait_stat(), except it never calls ide_error internally.
273 * This is a kludge to handle the new ide_config_drive_speed() function,
274 * and should not otherwise be used anywhere. Eventually, the tuneproc's
275 * should be updated to return ide_startstop_t, in which case we can get
276 * rid of this abomination again. :) -ml
277 *
278 * It is gone..........
279 *
280 * const char *msg == consider adding for verbose errors.
281 */
282 int ide_config_drive_speed (ide_drive_t *drive, byte speed)
283 {
284 ide_hwif_t *hwif = HWIF(drive);
285 int i, error = 1;
286 byte stat;
287
288 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
289 byte unit = (drive->select.b.unit & 0x01);
290 outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);
291 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
292
293 /*
294 * Don't use ide_wait_cmd here - it will
295 * attempt to set_geometry and recalibrate,
296 * but for some reason these don't work at
297 * this point (lost interrupt).
298 */
299 /*
300 * Select the drive, and issue the SETFEATURES command
301 */
302 disable_irq(hwif->irq); /* disable_irq_nosync ?? */
303 udelay(1);
304 SELECT_DRIVE(HWIF(drive), drive);
305 SELECT_MASK(HWIF(drive), drive, 0);
306 udelay(1);
307 if (IDE_CONTROL_REG)
308 OUT_BYTE(drive->ctl | 2, IDE_CONTROL_REG);
309 OUT_BYTE(speed, IDE_NSECTOR_REG);
310 OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
311 OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
312 if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
313 OUT_BYTE(drive->ctl, IDE_CONTROL_REG);
314 udelay(1);
315 /*
316 * Wait for drive to become non-BUSY
317 */
318 if ((stat = GET_STAT()) & BUSY_STAT) {
319 unsigned long flags, timeout;
320 __save_flags(flags); /* local CPU only */
321 ide__sti(); /* local CPU only -- for jiffies */
322 timeout = jiffies + WAIT_CMD;
323 while ((stat = GET_STAT()) & BUSY_STAT) {
324 if (0 < (signed long)(jiffies - timeout))
325 break;
326 }
327 __restore_flags(flags); /* local CPU only */
328 }
329
330 /*
331 * Allow status to settle, then read it again.
332 * A few rare drives vastly violate the 400ns spec here,
333 * so we'll wait up to 10usec for a "good" status
334 * rather than expensively fail things immediately.
335 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
336 */
337 for (i = 0; i < 10; i++) {
338 udelay(1);
339 if (OK_STAT((stat = GET_STAT()), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
340 error = 0;
341 break;
342 }
343 }
344
345 SELECT_MASK(HWIF(drive), drive, 0);
346
347 enable_irq(hwif->irq);
348
349 if (error) {
350 (void) ide_dump_status(drive, "set_drive_speed_status", stat);
351 return error;
352 }
353
354 drive->id->dma_ultra &= ~0xFF00;
355 drive->id->dma_mword &= ~0x0F00;
356 drive->id->dma_1word &= ~0x0F00;
357
358 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
359 if (speed > XFER_PIO_4) {
360 outb(inb(hwif->dma_base+2)|(1<<(5+unit)), hwif->dma_base+2);
361 } else {
362 outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);
363 }
364 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
365
366 switch(speed) {
367 case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
368 case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
369 case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
370 case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
371 case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
372 case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
373 case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
374 case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
375 case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
376 case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
377 case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
378 case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
379 case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
380 case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
381 default: break;
382 }
383 return error;
384 }
385
386 EXPORT_SYMBOL(ide_auto_reduce_xfer);
387 EXPORT_SYMBOL(ide_driveid_update);
388 EXPORT_SYMBOL(ide_ata66_check);
389 EXPORT_SYMBOL(set_transfer);
390 EXPORT_SYMBOL(eighty_ninty_three);
391 EXPORT_SYMBOL(ide_config_drive_speed);
392