pllnv04.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 1993-2003 NVIDIA, Corporation
  3. * Copyright 2007-2009 Stuart Bennett
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  20. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "pll.h"
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/pll.h>
  26. static int
  27. getMNP_single(struct nvkm_subdev *subdev, struct nvbios_pll *info, int clk,
  28. int *pN, int *pM, int *pP)
  29. {
  30. /* Find M, N and P for a single stage PLL
  31. *
  32. * Note that some bioses (NV3x) have lookup tables of precomputed MNP
  33. * values, but we're too lazy to use those atm
  34. *
  35. * "clk" parameter in kHz
  36. * returns calculated clock
  37. */
  38. struct nvkm_bios *bios = subdev->device->bios;
  39. int minvco = info->vco1.min_freq, maxvco = info->vco1.max_freq;
  40. int minM = info->vco1.min_m, maxM = info->vco1.max_m;
  41. int minN = info->vco1.min_n, maxN = info->vco1.max_n;
  42. int minU = info->vco1.min_inputfreq;
  43. int maxU = info->vco1.max_inputfreq;
  44. int minP = info->min_p;
  45. int maxP = info->max_p_usable;
  46. int crystal = info->refclk;
  47. int M, N, thisP, P;
  48. int clkP, calcclk;
  49. int delta, bestdelta = INT_MAX;
  50. int bestclk = 0;
  51. /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
  52. /* possibly correlated with introduction of 27MHz crystal */
  53. if (bios->version.major < 0x60) {
  54. int cv = bios->version.chip;
  55. if (cv < 0x17 || cv == 0x1a || cv == 0x20) {
  56. if (clk > 250000)
  57. maxM = 6;
  58. if (clk > 340000)
  59. maxM = 2;
  60. } else if (cv < 0x40) {
  61. if (clk > 150000)
  62. maxM = 6;
  63. if (clk > 200000)
  64. maxM = 4;
  65. if (clk > 340000)
  66. maxM = 2;
  67. }
  68. }
  69. P = 1 << maxP;
  70. if ((clk * P) < minvco) {
  71. minvco = clk * maxP;
  72. maxvco = minvco * 2;
  73. }
  74. if (clk + clk/200 > maxvco) /* +0.5% */
  75. maxvco = clk + clk/200;
  76. /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
  77. for (thisP = minP; thisP <= maxP; thisP++) {
  78. P = 1 << thisP;
  79. clkP = clk * P;
  80. if (clkP < minvco)
  81. continue;
  82. if (clkP > maxvco)
  83. return bestclk;
  84. for (M = minM; M <= maxM; M++) {
  85. if (crystal/M < minU)
  86. return bestclk;
  87. if (crystal/M > maxU)
  88. continue;
  89. /* add crystal/2 to round better */
  90. N = (clkP * M + crystal/2) / crystal;
  91. if (N < minN)
  92. continue;
  93. if (N > maxN)
  94. break;
  95. /* more rounding additions */
  96. calcclk = ((N * crystal + P/2) / P + M/2) / M;
  97. delta = abs(calcclk - clk);
  98. /* we do an exhaustive search rather than terminating
  99. * on an optimality condition...
  100. */
  101. if (delta < bestdelta) {
  102. bestdelta = delta;
  103. bestclk = calcclk;
  104. *pN = N;
  105. *pM = M;
  106. *pP = thisP;
  107. if (delta == 0) /* except this one */
  108. return bestclk;
  109. }
  110. }
  111. }
  112. return bestclk;
  113. }
  114. static int
  115. getMNP_double(struct nvkm_subdev *subdev, struct nvbios_pll *info, int clk,
  116. int *pN1, int *pM1, int *pN2, int *pM2, int *pP)
  117. {
  118. /* Find M, N and P for a two stage PLL
  119. *
  120. * Note that some bioses (NV30+) have lookup tables of precomputed MNP
  121. * values, but we're too lazy to use those atm
  122. *
  123. * "clk" parameter in kHz
  124. * returns calculated clock
  125. */
  126. int chip_version = subdev->device->bios->version.chip;
  127. int minvco1 = info->vco1.min_freq, maxvco1 = info->vco1.max_freq;
  128. int minvco2 = info->vco2.min_freq, maxvco2 = info->vco2.max_freq;
  129. int minU1 = info->vco1.min_inputfreq, minU2 = info->vco2.min_inputfreq;
  130. int maxU1 = info->vco1.max_inputfreq, maxU2 = info->vco2.max_inputfreq;
  131. int minM1 = info->vco1.min_m, maxM1 = info->vco1.max_m;
  132. int minN1 = info->vco1.min_n, maxN1 = info->vco1.max_n;
  133. int minM2 = info->vco2.min_m, maxM2 = info->vco2.max_m;
  134. int minN2 = info->vco2.min_n, maxN2 = info->vco2.max_n;
  135. int maxlog2P = info->max_p_usable;
  136. int crystal = info->refclk;
  137. bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
  138. int M1, N1, M2, N2, log2P;
  139. int clkP, calcclk1, calcclk2, calcclkout;
  140. int delta, bestdelta = INT_MAX;
  141. int bestclk = 0;
  142. int vco2 = (maxvco2 - maxvco2/200) / 2;
  143. for (log2P = 0; clk && log2P < maxlog2P && clk <= (vco2 >> log2P); log2P++)
  144. ;
  145. clkP = clk << log2P;
  146. if (maxvco2 < clk + clk/200) /* +0.5% */
  147. maxvco2 = clk + clk/200;
  148. for (M1 = minM1; M1 <= maxM1; M1++) {
  149. if (crystal/M1 < minU1)
  150. return bestclk;
  151. if (crystal/M1 > maxU1)
  152. continue;
  153. for (N1 = minN1; N1 <= maxN1; N1++) {
  154. calcclk1 = crystal * N1 / M1;
  155. if (calcclk1 < minvco1)
  156. continue;
  157. if (calcclk1 > maxvco1)
  158. break;
  159. for (M2 = minM2; M2 <= maxM2; M2++) {
  160. if (calcclk1/M2 < minU2)
  161. break;
  162. if (calcclk1/M2 > maxU2)
  163. continue;
  164. /* add calcclk1/2 to round better */
  165. N2 = (clkP * M2 + calcclk1/2) / calcclk1;
  166. if (N2 < minN2)
  167. continue;
  168. if (N2 > maxN2)
  169. break;
  170. if (!fixedgain2) {
  171. if (chip_version < 0x60)
  172. if (N2/M2 < 4 || N2/M2 > 10)
  173. continue;
  174. calcclk2 = calcclk1 * N2 / M2;
  175. if (calcclk2 < minvco2)
  176. break;
  177. if (calcclk2 > maxvco2)
  178. continue;
  179. } else
  180. calcclk2 = calcclk1;
  181. calcclkout = calcclk2 >> log2P;
  182. delta = abs(calcclkout - clk);
  183. /* we do an exhaustive search rather than terminating
  184. * on an optimality condition...
  185. */
  186. if (delta < bestdelta) {
  187. bestdelta = delta;
  188. bestclk = calcclkout;
  189. *pN1 = N1;
  190. *pM1 = M1;
  191. *pN2 = N2;
  192. *pM2 = M2;
  193. *pP = log2P;
  194. if (delta == 0) /* except this one */
  195. return bestclk;
  196. }
  197. }
  198. }
  199. }
  200. return bestclk;
  201. }
  202. int
  203. nv04_pll_calc(struct nvkm_subdev *subdev, struct nvbios_pll *info, u32 freq,
  204. int *N1, int *M1, int *N2, int *M2, int *P)
  205. {
  206. int ret;
  207. if (!info->vco2.max_freq || !N2) {
  208. ret = getMNP_single(subdev, info, freq, N1, M1, P);
  209. if (N2) {
  210. *N2 = 1;
  211. *M2 = 1;
  212. }
  213. } else {
  214. ret = getMNP_double(subdev, info, freq, N1, M1, N2, M2, P);
  215. }
  216. if (!ret)
  217. nvkm_error(subdev, "unable to compute acceptable pll values\n");
  218. return ret;
  219. }