libata-pata-timings.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Helper library for PATA timings
  4. *
  5. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  6. * Copyright 2003-2004 Jeff Garzik
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/libata.h>
  11. /*
  12. * This mode timing computation functionality is ported over from
  13. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  14. */
  15. /*
  16. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  17. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  18. * for UDMA6, which is currently supported only by Maxtor drives.
  19. *
  20. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  21. */
  22. static const struct ata_timing ata_timing[] = {
  23. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
  24. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
  25. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
  26. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
  27. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
  28. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
  29. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
  30. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
  31. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
  32. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
  33. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
  34. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
  35. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
  36. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
  37. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
  38. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
  39. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  40. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
  41. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
  42. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
  43. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
  44. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
  45. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
  46. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
  47. { 0xFF }
  48. };
  49. #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
  50. #define EZ(v, unit) ((v)?ENOUGH(((v) * 1000), unit):0)
  51. static void ata_timing_quantize(const struct ata_timing *t,
  52. struct ata_timing *q, int T, int UT)
  53. {
  54. q->setup = EZ(t->setup, T);
  55. q->act8b = EZ(t->act8b, T);
  56. q->rec8b = EZ(t->rec8b, T);
  57. q->cyc8b = EZ(t->cyc8b, T);
  58. q->active = EZ(t->active, T);
  59. q->recover = EZ(t->recover, T);
  60. q->dmack_hold = EZ(t->dmack_hold, T);
  61. q->cycle = EZ(t->cycle, T);
  62. q->udma = EZ(t->udma, UT);
  63. }
  64. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  65. struct ata_timing *m, unsigned int what)
  66. {
  67. if (what & ATA_TIMING_SETUP)
  68. m->setup = max(a->setup, b->setup);
  69. if (what & ATA_TIMING_ACT8B)
  70. m->act8b = max(a->act8b, b->act8b);
  71. if (what & ATA_TIMING_REC8B)
  72. m->rec8b = max(a->rec8b, b->rec8b);
  73. if (what & ATA_TIMING_CYC8B)
  74. m->cyc8b = max(a->cyc8b, b->cyc8b);
  75. if (what & ATA_TIMING_ACTIVE)
  76. m->active = max(a->active, b->active);
  77. if (what & ATA_TIMING_RECOVER)
  78. m->recover = max(a->recover, b->recover);
  79. if (what & ATA_TIMING_DMACK_HOLD)
  80. m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
  81. if (what & ATA_TIMING_CYCLE)
  82. m->cycle = max(a->cycle, b->cycle);
  83. if (what & ATA_TIMING_UDMA)
  84. m->udma = max(a->udma, b->udma);
  85. }
  86. EXPORT_SYMBOL_GPL(ata_timing_merge);
  87. const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
  88. {
  89. const struct ata_timing *t = ata_timing;
  90. while (xfer_mode > t->mode)
  91. t++;
  92. if (xfer_mode == t->mode)
  93. return t;
  94. WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
  95. __func__, xfer_mode);
  96. return NULL;
  97. }
  98. EXPORT_SYMBOL_GPL(ata_timing_find_mode);
  99. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  100. struct ata_timing *t, int T, int UT)
  101. {
  102. const u16 *id = adev->id;
  103. const struct ata_timing *s;
  104. struct ata_timing p;
  105. /*
  106. * Find the mode.
  107. */
  108. s = ata_timing_find_mode(speed);
  109. if (!s)
  110. return -EINVAL;
  111. memcpy(t, s, sizeof(*s));
  112. /*
  113. * If the drive is an EIDE drive, it can tell us it needs extended
  114. * PIO/MW_DMA cycle timing.
  115. */
  116. if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  117. memset(&p, 0, sizeof(p));
  118. if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
  119. if (speed <= XFER_PIO_2)
  120. p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
  121. else if ((speed <= XFER_PIO_4) ||
  122. (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
  123. p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
  124. } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
  125. p.cycle = id[ATA_ID_EIDE_DMA_MIN];
  126. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  127. }
  128. /*
  129. * Convert the timing to bus clock counts.
  130. */
  131. ata_timing_quantize(t, t, T, UT);
  132. /*
  133. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  134. * S.M.A.R.T * and some other commands. We have to ensure that the
  135. * DMA cycle timing is slower/equal than the fastest PIO timing.
  136. */
  137. if (speed > XFER_PIO_6) {
  138. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  139. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  140. }
  141. /*
  142. * Lengthen active & recovery time so that cycle time is correct.
  143. */
  144. if (t->act8b + t->rec8b < t->cyc8b) {
  145. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  146. t->rec8b = t->cyc8b - t->act8b;
  147. }
  148. if (t->active + t->recover < t->cycle) {
  149. t->active += (t->cycle - (t->active + t->recover)) / 2;
  150. t->recover = t->cycle - t->active;
  151. }
  152. /*
  153. * In a few cases quantisation may produce enough errors to
  154. * leave t->cycle too low for the sum of active and recovery
  155. * if so we must correct this.
  156. */
  157. if (t->active + t->recover > t->cycle)
  158. t->cycle = t->active + t->recover;
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(ata_timing_compute);