blob: 57e25753a2f063c0e46f638e4a9bbcbc086aba64 [file] [log] [blame]
Aneesh V37768012011-07-21 09:10:07 -04001/*
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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Aneesh V37768012011-07-21 09:10:07 -040011 */
12#include <stdlib.h>
13#include <stdio.h>
14typedef unsigned int u32;
15#define MAX_N 127
16
17/*
18 * get_m_n_optimized() - Finds optimal DPLL multiplier(M) and divider(N)
19 * values based on the reference frequency, required output frequency,
20 * maximum tolerance for output frequency etc.
21 *
22 * target_freq_khz - output frequency required in KHz
23 * ref_freq_khz - reference(input) frequency in KHz
24 * m - pointer to computed M value
25 * n - pointer to computed N value
26 * tolerance_khz - tolerance for the output frequency. When the algorithm
27 * succeeds in finding vialble M and N values the corresponding output
28 * frequency will be in the range:
29 * [target_freq_khz - tolerance_khz, target_freq_khz]
30 *
31 * Formula:
32 * Fdpll = (2 * M * Fref) / (N + 1)
33 *
34 * Considerations for lock-time:
35 * - Smaller the N, better lock-time, especially lock-time will be
36 * - For acceptable lock-times:
37 * Fref / (M + 1) >= 1 MHz
38 *
39 * Considerations for power:
40 * - The difference in power for different N values giving the same
41 * output is negligible. So, we optimize for lock-time
42 *
43 * Hard-constraints:
44 * - N can not be greater than 127(7 bit field for representing N)
45 *
46 * Usage:
47 * $ gcc clocks_get_m_n.c
48 * $ ./a.out
49 */
Aneesh V28a181f2011-11-21 23:38:57 +000050int get_m_n_optimized(u32 target_freq_khz, u32 ref_freq_khz, u32 *M, u32 *N)
Aneesh V37768012011-07-21 09:10:07 -040051{
Aneesh V28a181f2011-11-21 23:38:57 +000052 u32 freq = target_freq_khz;
53 u32 m_optimal, n_optimal, freq_optimal = 0, freq_old;
54 u32 m, n;
55 n = 1;
Aneesh V37768012011-07-21 09:10:07 -040056 while (1) {
Aneesh V28a181f2011-11-21 23:38:57 +000057 m = target_freq_khz / ref_freq_khz / 2 * n;
Aneesh V37768012011-07-21 09:10:07 -040058 freq_old = 0;
59 while (1) {
Aneesh V28a181f2011-11-21 23:38:57 +000060 freq = ref_freq_khz * 2 * m / n;
61 if (freq > target_freq_khz) {
Aneesh V37768012011-07-21 09:10:07 -040062 freq = freq_old;
Aneesh V28a181f2011-11-21 23:38:57 +000063 m--;
Aneesh V37768012011-07-21 09:10:07 -040064 break;
65 }
Aneesh V28a181f2011-11-21 23:38:57 +000066 m++;
Aneesh V37768012011-07-21 09:10:07 -040067 freq_old = freq;
68 }
Aneesh V28a181f2011-11-21 23:38:57 +000069 if (freq > freq_optimal) {
70 freq_optimal = freq;
71 m_optimal = m;
72 n_optimal = n;
73 }
74 n++;
75 if ((freq_optimal == target_freq_khz) ||
76 ((ref_freq_khz / n) < 1000)) {
Aneesh V37768012011-07-21 09:10:07 -040077 break;
Aneesh V37768012011-07-21 09:10:07 -040078 }
79 }
Aneesh V28a181f2011-11-21 23:38:57 +000080 n--;
81 *M = m_optimal;
82 *N = n_optimal - 1;
83 printf("ref %d m %d n %d target %d locked %d\n", ref_freq_khz,
84 m_optimal, n_optimal - 1, target_freq_khz, freq_optimal);
Aneesh V37768012011-07-21 09:10:07 -040085 return 0;
86}
87
88void main(void)
89{
90 u32 m, n;
91 printf("\nMPU - 2000000\n");
Aneesh V28a181f2011-11-21 23:38:57 +000092 get_m_n_optimized(2000000, 12000, &m, &n);
93 get_m_n_optimized(2000000, 13000, &m, &n);
94 get_m_n_optimized(2000000, 16800, &m, &n);
95 get_m_n_optimized(2000000, 19200, &m, &n);
96 get_m_n_optimized(2000000, 26000, &m, &n);
97 get_m_n_optimized(2000000, 27000, &m, &n);
98 get_m_n_optimized(2000000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -040099
100 printf("\nMPU - 1200000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000101 get_m_n_optimized(1200000, 12000, &m, &n);
102 get_m_n_optimized(1200000, 13000, &m, &n);
103 get_m_n_optimized(1200000, 16800, &m, &n);
104 get_m_n_optimized(1200000, 19200, &m, &n);
105 get_m_n_optimized(1200000, 26000, &m, &n);
106 get_m_n_optimized(1200000, 27000, &m, &n);
107 get_m_n_optimized(1200000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400108
109 printf("\nMPU - 1584000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000110 get_m_n_optimized(1584000, 12000, &m, &n);
111 get_m_n_optimized(1584000, 13000, &m, &n);
112 get_m_n_optimized(1584000, 16800, &m, &n);
113 get_m_n_optimized(1584000, 19200, &m, &n);
114 get_m_n_optimized(1584000, 26000, &m, &n);
115 get_m_n_optimized(1584000, 27000, &m, &n);
116 get_m_n_optimized(1584000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400117
118 printf("\nCore 1600000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000119 get_m_n_optimized(1600000, 12000, &m, &n);
120 get_m_n_optimized(1600000, 13000, &m, &n);
121 get_m_n_optimized(1600000, 16800, &m, &n);
122 get_m_n_optimized(1600000, 19200, &m, &n);
123 get_m_n_optimized(1600000, 26000, &m, &n);
124 get_m_n_optimized(1600000, 27000, &m, &n);
125 get_m_n_optimized(1600000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400126
127 printf("\nPER 1536000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000128 get_m_n_optimized(1536000, 12000, &m, &n);
129 get_m_n_optimized(1536000, 13000, &m, &n);
130 get_m_n_optimized(1536000, 16800, &m, &n);
131 get_m_n_optimized(1536000, 19200, &m, &n);
132 get_m_n_optimized(1536000, 26000, &m, &n);
133 get_m_n_optimized(1536000, 27000, &m, &n);
134 get_m_n_optimized(1536000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400135
136 printf("\nIVA 1862000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000137 get_m_n_optimized(1862000, 12000, &m, &n);
138 get_m_n_optimized(1862000, 13000, &m, &n);
139 get_m_n_optimized(1862000, 16800, &m, &n);
140 get_m_n_optimized(1862000, 19200, &m, &n);
141 get_m_n_optimized(1862000, 26000, &m, &n);
142 get_m_n_optimized(1862000, 27000, &m, &n);
143 get_m_n_optimized(1862000, 38400, &m, &n);
144
145 printf("\nIVA Nitro - 1290000\n");
146 get_m_n_optimized(1290000, 12000, &m, &n);
147 get_m_n_optimized(1290000, 13000, &m, &n);
148 get_m_n_optimized(1290000, 16800, &m, &n);
149 get_m_n_optimized(1290000, 19200, &m, &n);
150 get_m_n_optimized(1290000, 26000, &m, &n);
151 get_m_n_optimized(1290000, 27000, &m, &n);
152 get_m_n_optimized(1290000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400153
154 printf("\nABE 196608 sys clk\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000155 get_m_n_optimized(196608, 12000, &m, &n);
156 get_m_n_optimized(196608, 13000, &m, &n);
157 get_m_n_optimized(196608, 16800, &m, &n);
158 get_m_n_optimized(196608, 19200, &m, &n);
159 get_m_n_optimized(196608, 26000, &m, &n);
160 get_m_n_optimized(196608, 27000, &m, &n);
161 get_m_n_optimized(196608, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400162
163 printf("\nABE 196608 32K\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000164 get_m_n_optimized(196608000/4, 32768, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400165
166 printf("\nUSB 1920000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000167 get_m_n_optimized(1920000, 12000, &m, &n);
168 get_m_n_optimized(1920000, 13000, &m, &n);
169 get_m_n_optimized(1920000, 16800, &m, &n);
170 get_m_n_optimized(1920000, 19200, &m, &n);
171 get_m_n_optimized(1920000, 26000, &m, &n);
172 get_m_n_optimized(1920000, 27000, &m, &n);
173 get_m_n_optimized(1920000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400174
175 printf("\nCore ES1 1523712\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000176 get_m_n_optimized(1524000, 12000, &m, &n);
177 get_m_n_optimized(1524000, 13000, &m, &n);
178 get_m_n_optimized(1524000, 16800, &m, &n);
179 get_m_n_optimized(1524000, 19200, &m, &n);
180 get_m_n_optimized(1524000, 26000, &m, &n);
181 get_m_n_optimized(1524000, 27000, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400182
183 /* exact recommendation for SDPs */
Aneesh V28a181f2011-11-21 23:38:57 +0000184 get_m_n_optimized(1523712, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400185
186}