1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/kernel.h>
4 #include <linux/interrupt.h>
6 #include <linux/bitops.h>
8 static const char *udma_str[] =
9 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
10 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
11 static const char *mwdma_str[] =
12 { "MWDMA0", "MWDMA1", "MWDMA2", "MWDMA3", "MWDMA4" };
13 static const char *swdma_str[] =
14 { "SWDMA0", "SWDMA1", "SWDMA2" };
15 static const char *pio_str[] =
16 { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5", "PIO6" };
19 * ide_xfer_verbose - return IDE mode names
20 * @mode: transfer mode
22 * Returns a constant string giving the name of the mode
26 const char *ide_xfer_verbose(u8 mode)
31 if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
33 else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_4)
35 else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
37 else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_6)
39 else if (mode == XFER_PIO_SLOW)
46 EXPORT_SYMBOL(ide_xfer_verbose);
49 * ide_get_best_pio_mode - get PIO mode from drive
50 * @drive: drive to consider
51 * @mode_wanted: preferred mode
52 * @max_mode: highest allowed mode
54 * This routine returns the recommended PIO settings for a given drive,
55 * based on the drive->id information and the ide_pio_blacklist[].
57 * Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
58 * This is used by most chipset support modules when "auto-tuning".
61 u8 ide_get_best_pio_mode(ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
64 int pio_mode = -1, overridden = 0;
66 if (mode_wanted != 255)
67 return min_t(u8, mode_wanted, max_mode);
69 if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
70 pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);
73 printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name);
75 pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
76 if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */
81 if (id[ATA_ID_FIELD_VALID] & 2) { /* ATA2? */
82 if (ata_id_is_cfa(id) && (id[ATA_ID_CFA_MODES] & 7))
83 pio_mode = 4 + min_t(int, 2,
84 id[ATA_ID_CFA_MODES] & 7);
85 else if (ata_id_has_iordy(id)) {
86 if (id[ATA_ID_PIO_MODES] & 7) {
88 if (id[ATA_ID_PIO_MODES] & 4)
90 else if (id[ATA_ID_PIO_MODES] & 2)
99 printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
103 if (pio_mode > max_mode)
108 EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
110 int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
112 ide_hwif_t *hwif = drive->hwif;
113 const struct ide_port_ops *port_ops = hwif->port_ops;
115 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
118 if (port_ops == NULL || port_ops->set_pio_mode == NULL)
122 * TODO: temporary hack for some legacy host drivers that didn't
123 * set transfer mode on the device in ->set_pio_mode method...
125 if (port_ops->set_dma_mode == NULL) {
126 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
130 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
131 if (ide_config_drive_speed(drive, mode))
133 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
136 port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
137 return ide_config_drive_speed(drive, mode);
141 int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
143 ide_hwif_t *hwif = drive->hwif;
144 const struct ide_port_ops *port_ops = hwif->port_ops;
146 if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
149 if (port_ops == NULL || port_ops->set_dma_mode == NULL)
152 if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
153 if (ide_config_drive_speed(drive, mode))
155 port_ops->set_dma_mode(drive, mode);
158 port_ops->set_dma_mode(drive, mode);
159 return ide_config_drive_speed(drive, mode);
162 EXPORT_SYMBOL_GPL(ide_set_dma_mode);
164 /* req_pio == "255" for auto-tune */
165 void ide_set_pio(ide_drive_t *drive, u8 req_pio)
167 ide_hwif_t *hwif = drive->hwif;
168 const struct ide_port_ops *port_ops = hwif->port_ops;
171 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
172 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
175 BUG_ON(hwif->pio_mask == 0x00);
177 host_pio = fls(hwif->pio_mask) - 1;
179 pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
183 * - report device max PIO mode
184 * - check req_pio != 255 against device max PIO mode
186 printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
187 drive->name, host_pio, req_pio,
188 req_pio == 255 ? "(auto-tune)" : "", pio);
190 (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
192 EXPORT_SYMBOL_GPL(ide_set_pio);
195 * ide_rate_filter - filter transfer mode
197 * @speed: desired speed
199 * Given the available transfer modes this function returns
200 * the best available speed at or below the speed requested.
202 * TODO: check device PIO capabilities
205 static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
207 ide_hwif_t *hwif = drive->hwif;
208 u8 mode = ide_find_dma_mode(drive, speed);
212 mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
217 /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
219 return min(speed, mode);
223 * ide_set_xfer_rate - set transfer rate
224 * @drive: drive to set
225 * @rate: speed to attempt to set
227 * General helper for setting the speed of an IDE device. This
228 * function knows about user enforced limits from the configuration
229 * which ->set_pio_mode/->set_dma_mode does not.
232 int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
234 ide_hwif_t *hwif = drive->hwif;
235 const struct ide_port_ops *port_ops = hwif->port_ops;
237 if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
238 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
241 rate = ide_rate_filter(drive, rate);
243 BUG_ON(rate < XFER_PIO_0);
245 if (rate >= XFER_PIO_0 && rate <= XFER_PIO_6)
246 return ide_set_pio_mode(drive, rate);
248 return ide_set_dma_mode(drive, rate);