ide-xfer-mode.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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" };
  12. static const char *swdma_str[] =
  13. { "SWDMA0", "SWDMA1", "SWDMA2" };
  14. static const char *pio_str[] =
  15. { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" };
  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_2)
  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_5)
  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_has_iordy(id)) {
  72. if (id[ATA_ID_PIO_MODES] & 7) {
  73. overridden = 0;
  74. if (id[ATA_ID_PIO_MODES] & 4)
  75. pio_mode = 5;
  76. else if (id[ATA_ID_PIO_MODES] & 2)
  77. pio_mode = 4;
  78. else
  79. pio_mode = 3;
  80. }
  81. }
  82. }
  83. if (overridden)
  84. printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n",
  85. drive->name);
  86. }
  87. if (pio_mode > max_mode)
  88. pio_mode = max_mode;
  89. return pio_mode;
  90. }
  91. EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);
  92. int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  93. {
  94. ide_hwif_t *hwif = drive->hwif;
  95. const struct ide_port_ops *port_ops = hwif->port_ops;
  96. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  97. return 0;
  98. if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  99. return -1;
  100. /*
  101. * TODO: temporary hack for some legacy host drivers that didn't
  102. * set transfer mode on the device in ->set_pio_mode method...
  103. */
  104. if (port_ops->set_dma_mode == NULL) {
  105. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  106. return 0;
  107. }
  108. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  109. if (ide_config_drive_speed(drive, mode))
  110. return -1;
  111. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  112. return 0;
  113. } else {
  114. port_ops->set_pio_mode(drive, mode - XFER_PIO_0);
  115. return ide_config_drive_speed(drive, mode);
  116. }
  117. }
  118. int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  119. {
  120. ide_hwif_t *hwif = drive->hwif;
  121. const struct ide_port_ops *port_ops = hwif->port_ops;
  122. if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  123. return 0;
  124. if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  125. return -1;
  126. if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  127. if (ide_config_drive_speed(drive, mode))
  128. return -1;
  129. port_ops->set_dma_mode(drive, mode);
  130. return 0;
  131. } else {
  132. port_ops->set_dma_mode(drive, mode);
  133. return ide_config_drive_speed(drive, mode);
  134. }
  135. }
  136. EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  137. /* req_pio == "255" for auto-tune */
  138. void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  139. {
  140. ide_hwif_t *hwif = drive->hwif;
  141. const struct ide_port_ops *port_ops = hwif->port_ops;
  142. u8 host_pio, pio;
  143. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  144. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  145. return;
  146. BUG_ON(hwif->pio_mask == 0x00);
  147. host_pio = fls(hwif->pio_mask) - 1;
  148. pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  149. /*
  150. * TODO:
  151. * - report device max PIO mode
  152. * - check req_pio != 255 against device max PIO mode
  153. */
  154. printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n",
  155. drive->name, host_pio, req_pio,
  156. req_pio == 255 ? "(auto-tune)" : "", pio);
  157. (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  158. }
  159. EXPORT_SYMBOL_GPL(ide_set_pio);
  160. /**
  161. * ide_rate_filter - filter transfer mode
  162. * @drive: IDE device
  163. * @speed: desired speed
  164. *
  165. * Given the available transfer modes this function returns
  166. * the best available speed at or below the speed requested.
  167. *
  168. * TODO: check device PIO capabilities
  169. */
  170. static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  171. {
  172. ide_hwif_t *hwif = drive->hwif;
  173. u8 mode = ide_find_dma_mode(drive, speed);
  174. if (mode == 0) {
  175. if (hwif->pio_mask)
  176. mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  177. else
  178. mode = XFER_PIO_4;
  179. }
  180. /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */
  181. return min(speed, mode);
  182. }
  183. /**
  184. * ide_set_xfer_rate - set transfer rate
  185. * @drive: drive to set
  186. * @rate: speed to attempt to set
  187. *
  188. * General helper for setting the speed of an IDE device. This
  189. * function knows about user enforced limits from the configuration
  190. * which ->set_pio_mode/->set_dma_mode does not.
  191. */
  192. int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  193. {
  194. ide_hwif_t *hwif = drive->hwif;
  195. const struct ide_port_ops *port_ops = hwif->port_ops;
  196. if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  197. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  198. return -1;
  199. rate = ide_rate_filter(drive, rate);
  200. BUG_ON(rate < XFER_PIO_0);
  201. if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5)
  202. return ide_set_pio_mode(drive, rate);
  203. return ide_set_dma_mode(drive, rate);
  204. }