ide-xfer-mode.c 6.7 KB

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