summaryrefslogtreecommitdiffstats
path: root/drivers/ide/ide-features.c
blob: fcf0aab6337729b7a25ae7023a848ca899251f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * linux/drivers/block/ide-features.c	Version 0.03	Feb. 10, 2000
 *
 *  Copyright (C) 1999-2000	Linus Torvalds & authors (see below)
 *  
 *  Copyright (C) 1999-2000	Andre Hedrick <andre@suse.com>
 *
 *  Extracts if ide.c to address the evolving transfer rate code for
 *  the SETFEATURES_XFER callouts.  Various parts of any given function
 *  are credited to previous ATA-IDE maintainers.
 *
 *  May be copied or modified under the terms of the GNU General Public License
 */

#define __NO_VERSION__
#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/genhd.h>
#include <linux/blkpg.h>
#include <linux/malloc.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/hdreg.h>
#include <linux/ide.h>

#include <asm/byteorder.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/bitops.h>

#define SETFEATURES_CONTROL_REG		(0)	/* some arch's may need */

/*
 * A Verbose noise maker for debugging on the attempted transfer rates.
 */
char *ide_xfer_verbose (byte xfer_rate)
{
	switch(xfer_rate) {
		case XFER_UDMA_7:	return("UDMA 7");
		case XFER_UDMA_6:	return("UDMA 6");
		case XFER_UDMA_5:	return("UDMA 5");
		case XFER_UDMA_4:	return("UDMA 4");
		case XFER_UDMA_3:	return("UDMA 3");
		case XFER_UDMA_2:	return("UDMA 2");
		case XFER_UDMA_1:	return("UDMA 1");
		case XFER_UDMA_0:	return("UDMA 0");
		case XFER_MW_DMA_2:	return("MW DMA 2");
		case XFER_MW_DMA_1:	return("MW DMA 1");
		case XFER_MW_DMA_0:	return("MW DMA 0");
		case XFER_SW_DMA_2:	return("SW DMA 2");
		case XFER_SW_DMA_1:	return("SW DMA 1");
		case XFER_SW_DMA_0:	return("SW DMA 0");
		case XFER_PIO_4:	return("PIO 4");
		case XFER_PIO_3:	return("PIO 3");
		case XFER_PIO_2:	return("PIO 2");
		case XFER_PIO_1:	return("PIO 1");
		case XFER_PIO_0:	return("PIO 0");
		case XFER_PIO_SLOW:	return("PIO SLOW");
		default:		return("XFER ERROR");
	}
}

/*
 *
 */
char *ide_media_verbose (ide_drive_t *drive)
{
	switch (drive->media) {
		case ide_scsi:		return("scsi   ");
		case ide_disk:		return("disk   ");
		case ide_optical:	return("optical");
		case ide_cdrom:		return("cdrom  ");
		case ide_tape:		return("tape   ");
		case ide_floppy:	return("floppy ");
		default:		return("???????");
	}
}

/*
 * A Verbose noise maker for debugging on the attempted dmaing calls.
 */
char *ide_dmafunc_verbose (ide_dma_action_t dmafunc)
{
	switch (dmafunc) {
		case ide_dma_read:		return("ide_dma_read");
		case ide_dma_write:		return("ide_dma_write");
		case ide_dma_begin:		return("ide_dma_begin");
		case ide_dma_end:		return("ide_dma_end:");
		case ide_dma_check:		return("ide_dma_check");
		case ide_dma_on:		return("ide_dma_on");
		case ide_dma_off:		return("ide_dma_off");
		case ide_dma_off_quietly:	return("ide_dma_off_quietly");
		case ide_dma_test_irq:		return("ide_dma_test_irq");
		case ide_dma_bad_drive:		return("ide_dma_bad_drive");
		case ide_dma_good_drive:	return("ide_dma_good_drive");
		case ide_dma_verbose:		return("ide_dma_verbose");
		case ide_dma_retune:		return("ide_dma_retune");
		case ide_dma_lostirq:		return("ide_dma_lostirq");
		case ide_dma_timeout:		return("ide_dma_timeout");
		default:			return("unknown");
	}
}

/*
 *
 */
byte ide_auto_reduce_xfer (ide_drive_t *drive)
{
	switch(drive->current_speed) {
		case XFER_UDMA_7:	return XFER_UDMA_6;
		case XFER_UDMA_6:	return XFER_UDMA_5;
		case XFER_UDMA_5:	return XFER_UDMA_4;
		case XFER_UDMA_4:	return XFER_UDMA_3;
		case XFER_UDMA_3:	return XFER_UDMA_2;
		case XFER_UDMA_2:	return XFER_UDMA_1;
		case XFER_UDMA_1:	return XFER_UDMA_0;
		case XFER_UDMA_0:
			if (drive->id->dma_mword & 0x0004) return XFER_MW_DMA_2;
			else if (drive->id->dma_mword & 0x0002) return XFER_MW_DMA_1;
			else if (drive->id->dma_mword & 0x0001) return XFER_MW_DMA_0;
			else return XFER_PIO_4;
		case XFER_MW_DMA_2:	return XFER_MW_DMA_1;
		case XFER_MW_DMA_1:	return XFER_MW_DMA_0;
		case XFER_MW_DMA_0:
			if (drive->id->dma_1word & 0x0004) return XFER_SW_DMA_2;
			else if (drive->id->dma_1word & 0x0002) return XFER_SW_DMA_1;
			else if (drive->id->dma_1word & 0x0001) return XFER_SW_DMA_0;
			else return XFER_PIO_4;
		case XFER_SW_DMA_2:	return XFER_SW_DMA_1;
		case XFER_SW_DMA_1:	return XFER_SW_DMA_0;
		case XFER_SW_DMA_0:
			{
				return XFER_PIO_4;
			}
		case XFER_PIO_4:	return XFER_PIO_3;
		case XFER_PIO_3:	return XFER_PIO_2;
		case XFER_PIO_2:	return XFER_PIO_1;
		case XFER_PIO_1:	return XFER_PIO_0;
		case XFER_PIO_0:
		default:		return XFER_PIO_SLOW;
	}
}

/*
 * Update the 
 */
int ide_driveid_update (ide_drive_t *drive)
{
	/*
	 * Re-read drive->id for possible DMA mode
	 * change (copied from ide-probe.c)
	 */
	struct hd_driveid *id;
	unsigned long timeout, irqs, flags;

	probe_irq_off(probe_irq_on());
	irqs = probe_irq_on();
	if (IDE_CONTROL_REG)
		OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
	ide_delay_50ms();
	OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
	timeout = jiffies + WAIT_WORSTCASE;
	do {
		if (0 < (signed long)(jiffies - timeout)) {
			if (irqs)
				(void) probe_irq_off(irqs);
			return 0;	/* drive timed-out */
		}
		ide_delay_50ms();	/* give drive a breather */
	} while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
	ide_delay_50ms();	/* wait for IRQ and DRQ_STAT */
	if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
		printk("%s: CHECK for good STATUS\n", drive->name);
		return 0;
	}
	__save_flags(flags);	/* local CPU only */
	__cli();		/* local CPU only; some systems need this */
	id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
	ide_input_data(drive, id, SECTOR_WORDS);
	(void) GET_STAT();	/* clear drive IRQ */
	ide__sti();		/* local CPU only */
	__restore_flags(flags);	/* local CPU only */
	ide_fix_driveid(id);
	if (id) {
		drive->id->dma_ultra = id->dma_ultra;
		drive->id->dma_mword = id->dma_mword;
		drive->id->dma_1word = id->dma_1word;
		/* anything more ? */
		kfree(id);
	}

	return 1;
}

/*
 * Verify that we are doing an approved SETFEATURES_XFER with respect
 * to the hardware being able to support request.  Since some hardware
 * can improperly report capabilties, we check to see if the host adapter
 * in combination with the device (usually a disk) properly detect
 * and acknowledge each end of the ribbon.
 */
int ide_ata66_check (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
{
	if ((cmd == WIN_SETFEATURES) &&
	    (nsect > XFER_UDMA_2) &&
	    (feature == SETFEATURES_XFER)) {
		if (!HWIF(drive)->udma_four) {
			printk("%s: Speed warnings UDMA 3/4 is not functional.\n", HWIF(drive)->name);
			return 1;
		}
		if ((drive->id->hw_config & 0x2000) == 0) {
			printk("%s: Speed warnings UDMA 3/4 is not functional.\n", drive->name);
			return 1;
		}
	}
	return 0;
}

/*
 * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
 * 1 : Safe to update drive->id DMA registers.
 * 0 : OOPs not allowed.
 */
int set_transfer (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
{
	if ((cmd == WIN_SETFEATURES) &&
	    (nsect >= XFER_SW_DMA_0) &&
	    (feature == SETFEATURES_XFER) &&
	    (drive->id->dma_ultra ||
	     drive->id->dma_mword ||
	     drive->id->dma_1word))
		return 1;

	return 0;
}

/*
 * Similar to ide_wait_stat(), except it never calls ide_error internally.
 * This is a kludge to handle the new ide_config_drive_speed() function,
 * and should not otherwise be used anywhere.  Eventually, the tuneproc's
 * should be updated to return ide_startstop_t, in which case we can get
 * rid of this abomination again.  :)   -ml
 *
 * It is gone..........
 *
 * const char *msg == consider adding for verbose errors.
 */
int ide_config_drive_speed (ide_drive_t *drive, byte speed)
{
	ide_hwif_t *hwif = HWIF(drive);
	int	i, error = 1;
	byte unit = (drive->select.b.unit & 0x01);
	byte stat;

	outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);

	/*
	 * Don't use ide_wait_cmd here - it will
	 * attempt to set_geometry and recalibrate,
	 * but for some reason these don't work at
	 * this point (lost interrupt).
	 */
        /*
         * Select the drive, and issue the SETFEATURES command
         */
	disable_irq(hwif->irq);	/* disable_irq_nosync ?? */
	udelay(1);
	SELECT_DRIVE(HWIF(drive), drive);
	udelay(1);
	if (IDE_CONTROL_REG)
		OUT_BYTE(drive->ctl | 2, IDE_CONTROL_REG);
	OUT_BYTE(speed, IDE_NSECTOR_REG);
	OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
	OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
	if ((IDE_CONTROL_REG) && (SETFEATURES_CONTROL_REG))
		OUT_BYTE(drive->ctl, IDE_CONTROL_REG);
	udelay(1);
	/*
	 * Wait for drive to become non-BUSY
	 */
	if ((stat = GET_STAT()) & BUSY_STAT) {
		unsigned long flags, timeout;
		__save_flags(flags);	/* local CPU only */
		ide__sti();		/* local CPU only -- for jiffies */
		timeout = jiffies + WAIT_CMD;
		while ((stat = GET_STAT()) & BUSY_STAT) {
			if (0 < (signed long)(jiffies - timeout))
				break;
		}
		__restore_flags(flags); /* local CPU only */
	}

	/*
	 * Allow status to settle, then read it again.
	 * A few rare drives vastly violate the 400ns spec here,
	 * so we'll wait up to 10usec for a "good" status
	 * rather than expensively fail things immediately.
	 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
	 */
	for (i = 0; i < 10; i++) {
		udelay(1);
		if (OK_STAT((stat = GET_STAT()), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
			error = 0;
			break;
		}
	}

	enable_irq(hwif->irq);

	if (error) {
		(void) ide_dump_status(drive, "set_drive_speed_status", stat);
		return error;
	}

	drive->id->dma_ultra &= ~0xFF00;
	drive->id->dma_mword &= ~0x0F00;
	drive->id->dma_1word &= ~0x0F00;

	if (speed > XFER_PIO_4) {
		outb(inb(hwif->dma_base+2)|(1<<(5+unit)), hwif->dma_base+2);
	} else {
		outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);
	}

	switch(speed) {
		case XFER_UDMA_7:   drive->id->dma_ultra |= 0x8080; break;
		case XFER_UDMA_6:   drive->id->dma_ultra |= 0x4040; break;
		case XFER_UDMA_5:   drive->id->dma_ultra |= 0x2020; break;
		case XFER_UDMA_4:   drive->id->dma_ultra |= 0x1010; break;
		case XFER_UDMA_3:   drive->id->dma_ultra |= 0x0808; break;
		case XFER_UDMA_2:   drive->id->dma_ultra |= 0x0404; break;
		case XFER_UDMA_1:   drive->id->dma_ultra |= 0x0202; break;
		case XFER_UDMA_0:   drive->id->dma_ultra |= 0x0101; break;
		case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
		case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
		case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
		case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
		case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
		case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
		default: break;
	}
	return error;
}

EXPORT_SYMBOL(ide_auto_reduce_xfer);
EXPORT_SYMBOL(ide_driveid_update);
EXPORT_SYMBOL(ide_ata66_check);
EXPORT_SYMBOL(set_transfer);
EXPORT_SYMBOL(ide_config_drive_speed);