Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include <linux/string.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/interrupt.h> |
| 5 | #include <linux/ide.h> |
| 6 | #include <linux/bitops.h> |
| 7 | |
| 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[] = |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 12 | { "MWDMA0", "MWDMA1", "MWDMA2", "MWDMA3", "MWDMA4" }; |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 13 | static const char *swdma_str[] = |
| 14 | { "SWDMA0", "SWDMA1", "SWDMA2" }; |
| 15 | static const char *pio_str[] = |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 16 | { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5", "PIO6" }; |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * ide_xfer_verbose - return IDE mode names |
| 20 | * @mode: transfer mode |
| 21 | * |
| 22 | * Returns a constant string giving the name of the mode |
| 23 | * requested. |
| 24 | */ |
| 25 | |
| 26 | const char *ide_xfer_verbose(u8 mode) |
| 27 | { |
| 28 | const char *s; |
| 29 | u8 i = mode & 0xf; |
| 30 | |
| 31 | if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7) |
| 32 | s = udma_str[i]; |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 33 | else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_4) |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 34 | s = mwdma_str[i]; |
| 35 | else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2) |
| 36 | s = swdma_str[i]; |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 37 | else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_6) |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 38 | s = pio_str[i & 0x7]; |
| 39 | else if (mode == XFER_PIO_SLOW) |
| 40 | s = "PIO SLOW"; |
| 41 | else |
| 42 | s = "XFER ERROR"; |
| 43 | |
| 44 | return s; |
| 45 | } |
| 46 | EXPORT_SYMBOL(ide_xfer_verbose); |
| 47 | |
| 48 | /** |
| 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 |
| 53 | * |
| 54 | * This routine returns the recommended PIO settings for a given drive, |
| 55 | * based on the drive->id information and the ide_pio_blacklist[]. |
| 56 | * |
| 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". |
| 59 | */ |
| 60 | |
| 61 | u8 ide_get_best_pio_mode(ide_drive_t *drive, u8 mode_wanted, u8 max_mode) |
| 62 | { |
| 63 | u16 *id = drive->id; |
| 64 | int pio_mode = -1, overridden = 0; |
| 65 | |
| 66 | if (mode_wanted != 255) |
| 67 | return min_t(u8, mode_wanted, max_mode); |
| 68 | |
| 69 | if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0) |
| 70 | pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]); |
| 71 | |
| 72 | if (pio_mode != -1) { |
| 73 | printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name); |
| 74 | } else { |
| 75 | pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8; |
| 76 | if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */ |
| 77 | pio_mode = 2; |
| 78 | overridden = 1; |
| 79 | } |
| 80 | |
| 81 | if (id[ATA_ID_FIELD_VALID] & 2) { /* ATA2? */ |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 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)) { |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 86 | if (id[ATA_ID_PIO_MODES] & 7) { |
| 87 | overridden = 0; |
| 88 | if (id[ATA_ID_PIO_MODES] & 4) |
| 89 | pio_mode = 5; |
| 90 | else if (id[ATA_ID_PIO_MODES] & 2) |
| 91 | pio_mode = 4; |
| 92 | else |
| 93 | pio_mode = 3; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (overridden) |
| 99 | printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n", |
| 100 | drive->name); |
| 101 | } |
| 102 | |
| 103 | if (pio_mode > max_mode) |
| 104 | pio_mode = max_mode; |
| 105 | |
| 106 | return pio_mode; |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(ide_get_best_pio_mode); |
| 109 | |
Bartlomiej Zolnierkiewicz | c9ef59f | 2009-06-15 18:52:53 +0200 | [diff] [blame] | 110 | int ide_pio_need_iordy(ide_drive_t *drive, const u8 pio) |
| 111 | { |
Bartlomiej Zolnierkiewicz | 5880b5d | 2009-06-15 18:52:54 +0200 | [diff] [blame] | 112 | /* |
| 113 | * IORDY may lead to controller lock up on certain controllers |
| 114 | * if the port is not occupied. |
| 115 | */ |
| 116 | if (pio == 0 && (drive->hwif->port_flags & IDE_PFLAG_PROBING)) |
| 117 | return 0; |
Bartlomiej Zolnierkiewicz | c9ef59f | 2009-06-15 18:52:53 +0200 | [diff] [blame] | 118 | return ata_id_pio_need_iordy(drive->id, pio); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(ide_pio_need_iordy); |
| 121 | |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 122 | int ide_set_pio_mode(ide_drive_t *drive, const u8 mode) |
| 123 | { |
| 124 | ide_hwif_t *hwif = drive->hwif; |
| 125 | const struct ide_port_ops *port_ops = hwif->port_ops; |
| 126 | |
| 127 | if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE) |
| 128 | return 0; |
| 129 | |
| 130 | if (port_ops == NULL || port_ops->set_pio_mode == NULL) |
| 131 | return -1; |
| 132 | |
| 133 | /* |
| 134 | * TODO: temporary hack for some legacy host drivers that didn't |
| 135 | * set transfer mode on the device in ->set_pio_mode method... |
| 136 | */ |
| 137 | if (port_ops->set_dma_mode == NULL) { |
| 138 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) { |
| 143 | if (ide_config_drive_speed(drive, mode)) |
| 144 | return -1; |
| 145 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); |
| 146 | return 0; |
| 147 | } else { |
| 148 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); |
| 149 | return ide_config_drive_speed(drive, mode); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int ide_set_dma_mode(ide_drive_t *drive, const u8 mode) |
| 154 | { |
| 155 | ide_hwif_t *hwif = drive->hwif; |
| 156 | const struct ide_port_ops *port_ops = hwif->port_ops; |
| 157 | |
| 158 | if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE) |
| 159 | return 0; |
| 160 | |
| 161 | if (port_ops == NULL || port_ops->set_dma_mode == NULL) |
| 162 | return -1; |
| 163 | |
| 164 | if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) { |
| 165 | if (ide_config_drive_speed(drive, mode)) |
| 166 | return -1; |
| 167 | port_ops->set_dma_mode(drive, mode); |
| 168 | return 0; |
| 169 | } else { |
| 170 | port_ops->set_dma_mode(drive, mode); |
| 171 | return ide_config_drive_speed(drive, mode); |
| 172 | } |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(ide_set_dma_mode); |
| 175 | |
| 176 | /* req_pio == "255" for auto-tune */ |
| 177 | void ide_set_pio(ide_drive_t *drive, u8 req_pio) |
| 178 | { |
| 179 | ide_hwif_t *hwif = drive->hwif; |
| 180 | const struct ide_port_ops *port_ops = hwif->port_ops; |
| 181 | u8 host_pio, pio; |
| 182 | |
| 183 | if (port_ops == NULL || port_ops->set_pio_mode == NULL || |
| 184 | (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)) |
| 185 | return; |
| 186 | |
| 187 | BUG_ON(hwif->pio_mask == 0x00); |
| 188 | |
| 189 | host_pio = fls(hwif->pio_mask) - 1; |
| 190 | |
| 191 | pio = ide_get_best_pio_mode(drive, req_pio, host_pio); |
| 192 | |
| 193 | /* |
| 194 | * TODO: |
| 195 | * - report device max PIO mode |
| 196 | * - check req_pio != 255 against device max PIO mode |
| 197 | */ |
| 198 | printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n", |
| 199 | drive->name, host_pio, req_pio, |
| 200 | req_pio == 255 ? "(auto-tune)" : "", pio); |
| 201 | |
| 202 | (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio); |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(ide_set_pio); |
| 205 | |
| 206 | /** |
| 207 | * ide_rate_filter - filter transfer mode |
| 208 | * @drive: IDE device |
| 209 | * @speed: desired speed |
| 210 | * |
| 211 | * Given the available transfer modes this function returns |
| 212 | * the best available speed at or below the speed requested. |
| 213 | * |
| 214 | * TODO: check device PIO capabilities |
| 215 | */ |
| 216 | |
| 217 | static u8 ide_rate_filter(ide_drive_t *drive, u8 speed) |
| 218 | { |
| 219 | ide_hwif_t *hwif = drive->hwif; |
| 220 | u8 mode = ide_find_dma_mode(drive, speed); |
| 221 | |
| 222 | if (mode == 0) { |
| 223 | if (hwif->pio_mask) |
| 224 | mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0; |
| 225 | else |
| 226 | mode = XFER_PIO_4; |
| 227 | } |
| 228 | |
| 229 | /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */ |
| 230 | |
| 231 | return min(speed, mode); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * ide_set_xfer_rate - set transfer rate |
| 236 | * @drive: drive to set |
| 237 | * @rate: speed to attempt to set |
| 238 | * |
| 239 | * General helper for setting the speed of an IDE device. This |
| 240 | * function knows about user enforced limits from the configuration |
| 241 | * which ->set_pio_mode/->set_dma_mode does not. |
| 242 | */ |
| 243 | |
| 244 | int ide_set_xfer_rate(ide_drive_t *drive, u8 rate) |
| 245 | { |
| 246 | ide_hwif_t *hwif = drive->hwif; |
| 247 | const struct ide_port_ops *port_ops = hwif->port_ops; |
| 248 | |
| 249 | if (port_ops == NULL || port_ops->set_dma_mode == NULL || |
| 250 | (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)) |
| 251 | return -1; |
| 252 | |
| 253 | rate = ide_rate_filter(drive, rate); |
| 254 | |
| 255 | BUG_ON(rate < XFER_PIO_0); |
| 256 | |
Sergei Shtylyov | 74638c8 | 2009-03-31 20:15:28 +0200 | [diff] [blame] | 257 | if (rate >= XFER_PIO_0 && rate <= XFER_PIO_6) |
Bartlomiej Zolnierkiewicz | 7eeaaaa | 2009-03-24 23:22:46 +0100 | [diff] [blame] | 258 | return ide_set_pio_mode(drive, rate); |
| 259 | |
| 260 | return ide_set_dma_mode(drive, rate); |
| 261 | } |