xref: /openbmc/u-boot/tools/omap/clocks_get_m_n.c (revision d9b23e26)
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  * SPDX-License-Identifier:	GPL-2.0+
11  */
12 #include <stdlib.h>
13 #include <stdio.h>
14 typedef 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  */
50 int get_m_n_optimized(u32 target_freq_khz, u32 ref_freq_khz, u32 *M, u32 *N)
51 {
52 	u32 freq = target_freq_khz;
53 	u32 m_optimal, n_optimal, freq_optimal = 0, freq_old;
54 	u32 m, n;
55 	n = 1;
56 	while (1) {
57 		m = target_freq_khz / ref_freq_khz / 2 * n;
58 		freq_old = 0;
59 		while (1) {
60 			freq = ref_freq_khz * 2 * m / n;
61 			if (freq > target_freq_khz) {
62 				freq = freq_old;
63 				m--;
64 				break;
65 			}
66 			m++;
67 			freq_old = freq;
68 		}
69 		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)) {
77 			break;
78 		}
79 	}
80 	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);
85 	return 0;
86 }
87 
88 void main(void)
89 {
90 	u32 m, n;
91 	printf("\nMPU - 2000000\n");
92 	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);
99 
100 	printf("\nMPU - 1200000\n");
101 	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);
108 
109 	printf("\nMPU - 1584000\n");
110 	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);
117 
118 	printf("\nCore 1600000\n");
119 	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);
126 
127 	printf("\nPER 1536000\n");
128 	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);
135 
136 	printf("\nIVA 1862000\n");
137 	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);
153 
154 	printf("\nABE 196608 sys clk\n");
155 	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);
162 
163 	printf("\nABE 196608 32K\n");
164 	get_m_n_optimized(196608000/4, 32768, &m, &n);
165 
166 	printf("\nUSB 1920000\n");
167 	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);
174 
175 	printf("\nCore ES1 1523712\n");
176 	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);
182 
183 	/* exact recommendation for SDPs */
184 	get_m_n_optimized(1523712, 38400, &m, &n);
185 
186 }
187