clocks_get_m_n.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Program for finding M & N values for DPLLs
  3. * To be run on Host PC
  4. *
  5. * (C) Copyright 2010
  6. * Texas Instruments, <www.ti.com>
  7. *
  8. * Aneesh V <aneesh@ti.com>
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. typedef unsigned int u32;
  31. #define MAX_N 127
  32. /*
  33. * get_m_n_optimized() - Finds optimal DPLL multiplier(M) and divider(N)
  34. * values based on the reference frequency, required output frequency,
  35. * maximum tolerance for output frequency etc.
  36. *
  37. * target_freq_khz - output frequency required in KHz
  38. * ref_freq_khz - reference(input) frequency in KHz
  39. * m - pointer to computed M value
  40. * n - pointer to computed N value
  41. * tolerance_khz - tolerance for the output frequency. When the algorithm
  42. * succeeds in finding vialble M and N values the corresponding output
  43. * frequency will be in the range:
  44. * [target_freq_khz - tolerance_khz, target_freq_khz]
  45. *
  46. * Formula:
  47. * Fdpll = (2 * M * Fref) / (N + 1)
  48. *
  49. * Considerations for lock-time:
  50. * - Smaller the N, better lock-time, especially lock-time will be
  51. * - For acceptable lock-times:
  52. * Fref / (M + 1) >= 1 MHz
  53. *
  54. * Considerations for power:
  55. * - The difference in power for different N values giving the same
  56. * output is negligible. So, we optimize for lock-time
  57. *
  58. * Hard-constraints:
  59. * - N can not be greater than 127(7 bit field for representing N)
  60. *
  61. * Usage:
  62. * $ gcc clocks_get_m_n.c
  63. * $ ./a.out
  64. */
  65. int get_m_n_optimized(u32 target_freq_khz, u32 ref_freq_khz, u32 *M, u32 *N)
  66. {
  67. u32 freq = target_freq_khz;
  68. u32 m_optimal, n_optimal, freq_optimal = 0, freq_old;
  69. u32 m, n;
  70. n = 1;
  71. while (1) {
  72. m = target_freq_khz / ref_freq_khz / 2 * n;
  73. freq_old = 0;
  74. while (1) {
  75. freq = ref_freq_khz * 2 * m / n;
  76. if (freq > target_freq_khz) {
  77. freq = freq_old;
  78. m--;
  79. break;
  80. }
  81. m++;
  82. freq_old = freq;
  83. }
  84. if (freq > freq_optimal) {
  85. freq_optimal = freq;
  86. m_optimal = m;
  87. n_optimal = n;
  88. }
  89. n++;
  90. if ((freq_optimal == target_freq_khz) ||
  91. ((ref_freq_khz / n) < 1000)) {
  92. break;
  93. }
  94. }
  95. n--;
  96. *M = m_optimal;
  97. *N = n_optimal - 1;
  98. printf("ref %d m %d n %d target %d locked %d\n", ref_freq_khz,
  99. m_optimal, n_optimal - 1, target_freq_khz, freq_optimal);
  100. return 0;
  101. }
  102. void main(void)
  103. {
  104. u32 m, n;
  105. printf("\nMPU - 2000000\n");
  106. get_m_n_optimized(2000000, 12000, &m, &n);
  107. get_m_n_optimized(2000000, 13000, &m, &n);
  108. get_m_n_optimized(2000000, 16800, &m, &n);
  109. get_m_n_optimized(2000000, 19200, &m, &n);
  110. get_m_n_optimized(2000000, 26000, &m, &n);
  111. get_m_n_optimized(2000000, 27000, &m, &n);
  112. get_m_n_optimized(2000000, 38400, &m, &n);
  113. printf("\nMPU - 1200000\n");
  114. get_m_n_optimized(1200000, 12000, &m, &n);
  115. get_m_n_optimized(1200000, 13000, &m, &n);
  116. get_m_n_optimized(1200000, 16800, &m, &n);
  117. get_m_n_optimized(1200000, 19200, &m, &n);
  118. get_m_n_optimized(1200000, 26000, &m, &n);
  119. get_m_n_optimized(1200000, 27000, &m, &n);
  120. get_m_n_optimized(1200000, 38400, &m, &n);
  121. printf("\nMPU - 1584000\n");
  122. get_m_n_optimized(1584000, 12000, &m, &n);
  123. get_m_n_optimized(1584000, 13000, &m, &n);
  124. get_m_n_optimized(1584000, 16800, &m, &n);
  125. get_m_n_optimized(1584000, 19200, &m, &n);
  126. get_m_n_optimized(1584000, 26000, &m, &n);
  127. get_m_n_optimized(1584000, 27000, &m, &n);
  128. get_m_n_optimized(1584000, 38400, &m, &n);
  129. printf("\nCore 1600000\n");
  130. get_m_n_optimized(1600000, 12000, &m, &n);
  131. get_m_n_optimized(1600000, 13000, &m, &n);
  132. get_m_n_optimized(1600000, 16800, &m, &n);
  133. get_m_n_optimized(1600000, 19200, &m, &n);
  134. get_m_n_optimized(1600000, 26000, &m, &n);
  135. get_m_n_optimized(1600000, 27000, &m, &n);
  136. get_m_n_optimized(1600000, 38400, &m, &n);
  137. printf("\nPER 1536000\n");
  138. get_m_n_optimized(1536000, 12000, &m, &n);
  139. get_m_n_optimized(1536000, 13000, &m, &n);
  140. get_m_n_optimized(1536000, 16800, &m, &n);
  141. get_m_n_optimized(1536000, 19200, &m, &n);
  142. get_m_n_optimized(1536000, 26000, &m, &n);
  143. get_m_n_optimized(1536000, 27000, &m, &n);
  144. get_m_n_optimized(1536000, 38400, &m, &n);
  145. printf("\nIVA 1862000\n");
  146. get_m_n_optimized(1862000, 12000, &m, &n);
  147. get_m_n_optimized(1862000, 13000, &m, &n);
  148. get_m_n_optimized(1862000, 16800, &m, &n);
  149. get_m_n_optimized(1862000, 19200, &m, &n);
  150. get_m_n_optimized(1862000, 26000, &m, &n);
  151. get_m_n_optimized(1862000, 27000, &m, &n);
  152. get_m_n_optimized(1862000, 38400, &m, &n);
  153. printf("\nIVA Nitro - 1290000\n");
  154. get_m_n_optimized(1290000, 12000, &m, &n);
  155. get_m_n_optimized(1290000, 13000, &m, &n);
  156. get_m_n_optimized(1290000, 16800, &m, &n);
  157. get_m_n_optimized(1290000, 19200, &m, &n);
  158. get_m_n_optimized(1290000, 26000, &m, &n);
  159. get_m_n_optimized(1290000, 27000, &m, &n);
  160. get_m_n_optimized(1290000, 38400, &m, &n);
  161. printf("\nABE 196608 sys clk\n");
  162. get_m_n_optimized(196608, 12000, &m, &n);
  163. get_m_n_optimized(196608, 13000, &m, &n);
  164. get_m_n_optimized(196608, 16800, &m, &n);
  165. get_m_n_optimized(196608, 19200, &m, &n);
  166. get_m_n_optimized(196608, 26000, &m, &n);
  167. get_m_n_optimized(196608, 27000, &m, &n);
  168. get_m_n_optimized(196608, 38400, &m, &n);
  169. printf("\nABE 196608 32K\n");
  170. get_m_n_optimized(196608000/4, 32768, &m, &n);
  171. printf("\nUSB 1920000\n");
  172. get_m_n_optimized(1920000, 12000, &m, &n);
  173. get_m_n_optimized(1920000, 13000, &m, &n);
  174. get_m_n_optimized(1920000, 16800, &m, &n);
  175. get_m_n_optimized(1920000, 19200, &m, &n);
  176. get_m_n_optimized(1920000, 26000, &m, &n);
  177. get_m_n_optimized(1920000, 27000, &m, &n);
  178. get_m_n_optimized(1920000, 38400, &m, &n);
  179. printf("\nCore ES1 1523712\n");
  180. get_m_n_optimized(1524000, 12000, &m, &n);
  181. get_m_n_optimized(1524000, 13000, &m, &n);
  182. get_m_n_optimized(1524000, 16800, &m, &n);
  183. get_m_n_optimized(1524000, 19200, &m, &n);
  184. get_m_n_optimized(1524000, 26000, &m, &n);
  185. get_m_n_optimized(1524000, 27000, &m, &n);
  186. /* exact recommendation for SDPs */
  187. get_m_n_optimized(1523712, 38400, &m, &n);
  188. }