ide-xfer-mode.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. static 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. int ide_pio_need_iordy(ide_drive_t *drive, const u8 pio)
  95. {
  96. /*
  97. * IORDY may lead to controller lock up on certain controllers
  98. * if the port is not occupied.
  99. */
  100. if (pio == 0 && (drive->hwif->port_flags & IDE_PFLAG_PROBING))
  101. return 0;
  102. return ata_id_pio_need_iordy(drive->id, pio);
  103. }
  104. EXPORT_SYMBOL_GPL(ide_pio_need_iordy);
  105. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  106. {
  107. ide_hwif_t *hwif = drive->hwif;
  108. const struct ide_port_ops *port_ops = hwif->port_ops;
  109. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  110. return 0;
  111. if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  112. return -1;
  113. /*
  114. * TODO: temporary hack for some legacy host drivers that didn't
  115. * set transfer mode on the device in ->set_pio_mode method...
  116. */
  117. if (port_ops->set_dma_mode == NULL) {
  118. drive->pio_mode = mode;
  119. port_ops->set_pio_mode(hwif, drive);
  120. return 0;
  121. }
  122. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  123. if (ide_config_drive_speed(drive, mode))
  124. return -1;
  125. drive->pio_mode = mode;
  126. port_ops->set_pio_mode(hwif, drive);
  127. return 0;
  128. } else {
  129. drive->pio_mode = mode;
  130. port_ops->set_pio_mode(hwif, drive);
  131. return ide_config_drive_speed(drive, mode);
  132. }
  133. }
  134. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  135. {
  136. ide_hwif_t *hwif = drive->hwif;
  137. const struct ide_port_ops *port_ops = hwif->port_ops;
  138. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  139. return 0;
  140. if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  141. return -1;
  142. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  143. if (ide_config_drive_speed(drive, mode))
  144. return -1;
  145. drive->dma_mode = mode;
  146. port_ops->set_dma_mode(hwif, drive);
  147. return 0;
  148. } else {
  149. drive->dma_mode = mode;
  150. port_ops->set_dma_mode(hwif, drive);
  151. return ide_config_drive_speed(drive, mode);
  152. }
  153. }
  154. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  155. /* req_pio == "255" for auto-tune */
  156. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  157. {
  158. ide_hwif_t *hwif = drive->hwif;
  159. const struct ide_port_ops *port_ops = hwif->port_ops;
  160. u8 host_pio, pio;
  161. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  162. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  163. return;
  164. BUG_ON(hwif->pio_mask == 0x00);
  165. host_pio = fls(hwif->pio_mask) - 1;
  166. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  167. /*
  168. * TODO:
  169. * - report device max PIO mode
  170. * - check req_pio != 255 against device max PIO mode
  171. */
  172. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  173. drive->name, host_pio, req_pio,
  174. req_pio == 255 ? "(auto-tune)" : "", pio);
  175. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  176. }
  177. EXPORT_SYMBOL_GPL(ide_set_pio);
  178. /**
  179. * ide_rate_filter - filter transfer mode
  180. * @drive: IDE device
  181. * @speed: desired speed
  182. *
  183. * Given the available transfer modes this function returns
  184. * the best available speed at or below the speed requested.
  185. *
  186. * TODO: check device PIO capabilities
  187. */
  188. static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  189. {
  190. ide_hwif_t *hwif = drive->hwif;
  191. u8 mode = ide_find_dma_mode(drive, speed);
  192. if (mode == 0) {
  193. if (hwif->pio_mask)
  194. mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  195. else
  196. mode = XFER_PIO_4;
  197. }
  198. /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
  199. return min(speed, mode);
  200. }
  201. /**
  202. * ide_set_xfer_rate - set transfer rate
  203. * @drive: drive to set
  204. * @rate: speed to attempt to set
  205. *
  206. * General helper for setting the speed of an IDE device. This
  207. * function knows about user enforced limits from the configuration
  208. * which ->set_pio_mode/->set_dma_mode does not.
  209. */
  210. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  211. {
  212. ide_hwif_t *hwif = drive->hwif;
  213. const struct ide_port_ops *port_ops = hwif->port_ops;
  214. if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  215. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  216. return -1;
  217. rate = ide_rate_filter(drive, rate);
  218. BUG_ON(rate < XFER_PIO_0);
  219. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_6)
  220. return ide_set_pio_mode(drive, rate);
  221. return ide_set_dma_mode(drive, rate);
  222. }