More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / ide-lib.c
blobd295ee6ee3694a71701d19e9c68af7ecaa0af906
1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/types.h>
4 #include <linux/string.h>
5 #include <linux/kernel.h>
6 #include <linux/timer.h>
7 #include <linux/mm.h>
8 #include <linux/interrupt.h>
9 #include <linux/major.h>
10 #include <linux/errno.h>
11 #include <linux/genhd.h>
12 #include <linux/blkpg.h>
13 #include <linux/slab.h>
14 #include <linux/pci.h>
15 #include <linux/delay.h>
16 #include <linux/hdreg.h>
17 #include <linux/ide.h>
19 #include <asm/byteorder.h>
20 #include <asm/irq.h>
21 #include <asm/uaccess.h>
22 #include <asm/io.h>
23 #include <asm/bitops.h>
25 #include "ide_modes.h"
28 * IDE library routines. These are plug in code that most
29 * drivers can use but occasionally may be weird enough
30 * to want to do their own thing with
32 * Add common non I/O op stuff here. Make sure it has proper
33 * kernel-doc function headers or your patch will be rejected
37 /**
38 * ide_xfer_verbose - return IDE mode names
39 * @xfer_rate: rate to name
41 * Returns a constant string giving the name of the mode
42 * requested.
45 char *ide_xfer_verbose (u8 xfer_rate)
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");
72 EXPORT_SYMBOL(ide_xfer_verbose);
74 /**
75 * ide_dma_speed - compute DMA speed
76 * @drive: drive
77 * @mode; intended mode
79 * Checks the drive capabilities and returns the speed to use
80 * for the transfer. Returns -1 if the requested mode is unknown
81 * (eg PIO)
84 u8 ide_dma_speed(ide_drive_t *drive, u8 mode)
86 struct hd_driveid *id = drive->id;
87 ide_hwif_t *hwif = HWIF(drive);
88 u8 speed = 0;
90 if (drive->media != ide_disk && hwif->atapi_dma == 0)
91 return 0;
93 switch(mode) {
94 case 0x04:
95 if ((id->dma_ultra & 0x0040) &&
96 (id->dma_ultra & hwif->ultra_mask))
97 { speed = XFER_UDMA_6; break; }
98 case 0x03:
99 if ((id->dma_ultra & 0x0020) &&
100 (id->dma_ultra & hwif->ultra_mask))
101 { speed = XFER_UDMA_5; break; }
102 case 0x02:
103 if ((id->dma_ultra & 0x0010) &&
104 (id->dma_ultra & hwif->ultra_mask))
105 { speed = XFER_UDMA_4; break; }
106 if ((id->dma_ultra & 0x0008) &&
107 (id->dma_ultra & hwif->ultra_mask))
108 { speed = XFER_UDMA_3; break; }
109 case 0x01:
110 if ((id->dma_ultra & 0x0004) &&
111 (id->dma_ultra & hwif->ultra_mask))
112 { speed = XFER_UDMA_2; break; }
113 if ((id->dma_ultra & 0x0002) &&
114 (id->dma_ultra & hwif->ultra_mask))
115 { speed = XFER_UDMA_1; break; }
116 if ((id->dma_ultra & 0x0001) &&
117 (id->dma_ultra & hwif->ultra_mask))
118 { speed = XFER_UDMA_0; break; }
119 case 0x00:
120 if ((id->dma_mword & 0x0004) &&
121 (id->dma_mword & hwif->mwdma_mask))
122 { speed = XFER_MW_DMA_2; break; }
123 if ((id->dma_mword & 0x0002) &&
124 (id->dma_mword & hwif->mwdma_mask))
125 { speed = XFER_MW_DMA_1; break; }
126 if ((id->dma_mword & 0x0001) &&
127 (id->dma_mword & hwif->mwdma_mask))
128 { speed = XFER_MW_DMA_0; break; }
129 if ((id->dma_1word & 0x0004) &&
130 (id->dma_1word & hwif->swdma_mask))
131 { speed = XFER_SW_DMA_2; break; }
132 if ((id->dma_1word & 0x0002) &&
133 (id->dma_1word & hwif->swdma_mask))
134 { speed = XFER_SW_DMA_1; break; }
135 if ((id->dma_1word & 0x0001) &&
136 (id->dma_1word & hwif->swdma_mask))
137 { speed = XFER_SW_DMA_0; break; }
140 // printk("%s: %s: mode 0x%02x, speed 0x%02x\n",
141 // __FUNCTION__, drive->name, mode, speed);
143 return speed;
146 EXPORT_SYMBOL(ide_dma_speed);
150 * ide_rate_filter - return best speed for mode
151 * @mode: modes available
152 * @speed: desired speed
154 * Given the available DMA/UDMA mode this function returns
155 * the best available speed at or below the speed requested.
158 u8 ide_rate_filter (u8 mode, u8 speed)
160 #ifdef CONFIG_BLK_DEV_IDEDMA
161 static u8 speed_max[] = {
162 XFER_MW_DMA_2, XFER_UDMA_2, XFER_UDMA_4,
163 XFER_UDMA_5, XFER_UDMA_6
166 // printk("%s: mode 0x%02x, speed 0x%02x\n", __FUNCTION__, mode, speed);
168 /* So that we remember to update this if new modes appear */
169 if (mode > 4)
170 BUG();
171 return min(speed, speed_max[mode]);
172 #else /* !CONFIG_BLK_DEV_IDEDMA */
173 return min(speed, XFER_PIO_4);
174 #endif /* CONFIG_BLK_DEV_IDEDMA */
177 EXPORT_SYMBOL(ide_rate_filter);
179 int ide_dma_enable (ide_drive_t *drive)
181 ide_hwif_t *hwif = HWIF(drive);
182 struct hd_driveid *id = drive->id;
184 return ((int) ((((id->dma_ultra >> 8) & hwif->ultra_mask) ||
185 ((id->dma_mword >> 8) & hwif->mwdma_mask) ||
186 ((id->dma_1word >> 8) & hwif->swdma_mask)) ? 1 : 0));
189 EXPORT_SYMBOL(ide_dma_enable);
191 const ide_pio_timings_t ide_pio_timings[6] = {
192 { 70, 165, 600 }, /* PIO Mode 0 */
193 { 50, 125, 383 }, /* PIO Mode 1 */
194 { 30, 100, 240 }, /* PIO Mode 2 */
195 { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
196 { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
197 { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
200 EXPORT_SYMBOL_GPL(ide_pio_timings);
203 * Black list. Some drives incorrectly report their maximal PIO mode,
204 * at least in respect to CMD640. Here we keep info on some known drives.
206 static struct ide_pio_info {
207 const char *name;
208 int pio;
209 } ide_pio_blacklist [] = {
210 /* { "Conner Peripherals 1275MB - CFS1275A", 4 }, */
211 { "Conner Peripherals 540MB - CFS540A", 3 },
213 { "WDC AC2700", 3 },
214 { "WDC AC2540", 3 },
215 { "WDC AC2420", 3 },
216 { "WDC AC2340", 3 },
217 { "WDC AC2250", 0 },
218 { "WDC AC2200", 0 },
219 { "WDC AC21200", 4 },
220 { "WDC AC2120", 0 },
221 { "WDC AC2850", 3 },
222 { "WDC AC1270", 3 },
223 { "WDC AC1170", 1 },
224 { "WDC AC1210", 1 },
225 { "WDC AC280", 0 },
226 /* { "WDC AC21000", 4 }, */
227 { "WDC AC31000", 3 },
228 { "WDC AC31200", 3 },
229 /* { "WDC AC31600", 4 }, */
231 { "Maxtor 7131 AT", 1 },
232 { "Maxtor 7171 AT", 1 },
233 { "Maxtor 7213 AT", 1 },
234 { "Maxtor 7245 AT", 1 },
235 { "Maxtor 7345 AT", 1 },
236 { "Maxtor 7546 AT", 3 },
237 { "Maxtor 7540 AV", 3 },
239 { "SAMSUNG SHD-3121A", 1 },
240 { "SAMSUNG SHD-3122A", 1 },
241 { "SAMSUNG SHD-3172A", 1 },
243 /* { "ST51080A", 4 },
244 * { "ST51270A", 4 },
245 * { "ST31220A", 4 },
246 * { "ST31640A", 4 },
247 * { "ST32140A", 4 },
248 * { "ST3780A", 4 },
250 { "ST5660A", 3 },
251 { "ST3660A", 3 },
252 { "ST3630A", 3 },
253 { "ST3655A", 3 },
254 { "ST3391A", 3 },
255 { "ST3390A", 1 },
256 { "ST3600A", 1 },
257 { "ST3290A", 0 },
258 { "ST3144A", 0 },
259 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
260 /* drive) according to Seagates FIND-ATA program */
262 { "QUANTUM ELS127A", 0 },
263 { "QUANTUM ELS170A", 0 },
264 { "QUANTUM LPS240A", 0 },
265 { "QUANTUM LPS210A", 3 },
266 { "QUANTUM LPS270A", 3 },
267 { "QUANTUM LPS365A", 3 },
268 { "QUANTUM LPS540A", 3 },
269 { "QUANTUM LIGHTNING 540A", 3 },
270 { "QUANTUM LIGHTNING 730A", 3 },
272 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
273 { "QUANTUM FIREBALL_640", 3 },
274 { "QUANTUM FIREBALL_1080", 3 },
275 { "QUANTUM FIREBALL_1280", 3 },
276 { NULL, 0 }
280 * ide_scan_pio_blacklist - check for a blacklisted drive
281 * @model: Drive model string
283 * This routine searches the ide_pio_blacklist for an entry
284 * matching the start/whole of the supplied model name.
286 * Returns -1 if no match found.
287 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
290 static int ide_scan_pio_blacklist (char *model)
292 struct ide_pio_info *p;
294 for (p = ide_pio_blacklist; p->name != NULL; p++) {
295 if (strncmp(p->name, model, strlen(p->name)) == 0)
296 return p->pio;
298 return -1;
302 * ide_get_best_pio_mode - get PIO mode from drive
303 * @driver: drive to consider
304 * @mode_wanted: preferred mode
305 * @max_mode: highest allowed
306 * @d: pio data
308 * This routine returns the recommended PIO settings for a given drive,
309 * based on the drive->id information and the ide_pio_blacklist[].
310 * This is used by most chipset support modules when "auto-tuning".
312 * Drive PIO mode auto selection
315 u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode, ide_pio_data_t *d)
317 int pio_mode;
318 int cycle_time = 0;
319 int use_iordy = 0;
320 struct hd_driveid* id = drive->id;
321 int overridden = 0;
322 int blacklisted = 0;
324 if (mode_wanted != 255) {
325 pio_mode = mode_wanted;
326 } else if (!drive->id) {
327 pio_mode = 0;
328 } else if ((pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {
329 overridden = 1;
330 blacklisted = 1;
331 use_iordy = (pio_mode > 2);
332 } else {
333 pio_mode = id->tPIO;
334 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
335 pio_mode = 2;
336 overridden = 1;
338 if (id->field_valid & 2) { /* drive implements ATA2? */
339 if (id->capability & 8) { /* drive supports use_iordy? */
340 use_iordy = 1;
341 cycle_time = id->eide_pio_iordy;
342 if (id->eide_pio_modes & 7) {
343 overridden = 0;
344 if (id->eide_pio_modes & 4)
345 pio_mode = 5;
346 else if (id->eide_pio_modes & 2)
347 pio_mode = 4;
348 else
349 pio_mode = 3;
351 } else {
352 cycle_time = id->eide_pio;
356 #if 0
357 if (drive->id->major_rev_num & 0x0004) printk("ATA-2 ");
358 #endif
361 * Conservative "downgrade" for all pre-ATA2 drives
363 if (pio_mode && pio_mode < 4) {
364 pio_mode--;
365 overridden = 1;
366 #if 0
367 use_iordy = (pio_mode > 2);
368 #endif
369 if (cycle_time && cycle_time < ide_pio_timings[pio_mode].cycle_time)
370 cycle_time = 0; /* use standard timing */
373 if (pio_mode > max_mode) {
374 pio_mode = max_mode;
375 cycle_time = 0;
377 if (d) {
378 d->pio_mode = pio_mode;
379 d->cycle_time = cycle_time ? cycle_time : ide_pio_timings[pio_mode].cycle_time;
380 d->use_iordy = use_iordy;
381 d->overridden = overridden;
382 d->blacklisted = blacklisted;
384 return pio_mode;
387 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
390 * ide_toggle_bounce - handle bounce buffering
391 * @drive: drive to update
392 * @on: on/off boolean
394 * Enable or disable bounce buffering for the device. Drives move
395 * between PIO and DMA and that changes the rules we need.
398 void ide_toggle_bounce(ide_drive_t *drive, int on)
400 u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */
402 if (on && drive->media == ide_disk) {
403 if (!PCI_DMA_BUS_IS_PHYS)
404 addr = BLK_BOUNCE_ANY;
405 else if (HWIF(drive)->pci_dev)
406 addr = HWIF(drive)->pci_dev->dma_mask;
409 blk_queue_bounce_limit(&drive->queue, addr);
412 EXPORT_SYMBOL(ide_toggle_bounce);
415 * ide_set_xfer_rate - set transfer rate
416 * @drive: drive to set
417 * @speed: speed to attempt to set
419 * General helper for setting the speed of an IDE device. This
420 * function knows about user enforced limits from the configuration
421 * which speedproc() does not. High level drivers should never
422 * invoke speedproc() directly.
425 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
427 #ifndef CONFIG_BLK_DEV_IDEDMA
428 rate = min(rate, (u8) XFER_PIO_4);
429 #endif
430 if(HWIF(drive)->speedproc)
431 return HWIF(drive)->speedproc(drive, rate);
432 else
433 return -1;
436 EXPORT_SYMBOL_GPL(ide_set_xfer_rate);