blob: e21b0e2b60b6b7c26476eb57162f58a23eaa0934 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Aneesh V37768012011-07-21 09:10:07 -04002/*
3 * Program for finding M & N values for DPLLs
4 * To be run on Host PC
5 *
6 * (C) Copyright 2010
7 * Texas Instruments, <www.ti.com>
8 *
9 * Aneesh V <aneesh@ti.com>
Aneesh V37768012011-07-21 09:10:07 -040010 */
11#include <stdlib.h>
12#include <stdio.h>
13typedef unsigned int u32;
14#define MAX_N 127
15
16/*
17 * get_m_n_optimized() - Finds optimal DPLL multiplier(M) and divider(N)
18 * values based on the reference frequency, required output frequency,
19 * maximum tolerance for output frequency etc.
20 *
21 * target_freq_khz - output frequency required in KHz
22 * ref_freq_khz - reference(input) frequency in KHz
23 * m - pointer to computed M value
24 * n - pointer to computed N value
25 * tolerance_khz - tolerance for the output frequency. When the algorithm
26 * succeeds in finding vialble M and N values the corresponding output
27 * frequency will be in the range:
28 * [target_freq_khz - tolerance_khz, target_freq_khz]
29 *
30 * Formula:
31 * Fdpll = (2 * M * Fref) / (N + 1)
32 *
33 * Considerations for lock-time:
34 * - Smaller the N, better lock-time, especially lock-time will be
35 * - For acceptable lock-times:
36 * Fref / (M + 1) >= 1 MHz
37 *
38 * Considerations for power:
39 * - The difference in power for different N values giving the same
40 * output is negligible. So, we optimize for lock-time
41 *
42 * Hard-constraints:
43 * - N can not be greater than 127(7 bit field for representing N)
44 *
45 * Usage:
46 * $ gcc clocks_get_m_n.c
47 * $ ./a.out
48 */
Aneesh V28a181f2011-11-21 23:38:57 +000049int get_m_n_optimized(u32 target_freq_khz, u32 ref_freq_khz, u32 *M, u32 *N)
Aneesh V37768012011-07-21 09:10:07 -040050{
Aneesh V28a181f2011-11-21 23:38:57 +000051 u32 freq = target_freq_khz;
52 u32 m_optimal, n_optimal, freq_optimal = 0, freq_old;
53 u32 m, n;
54 n = 1;
Aneesh V37768012011-07-21 09:10:07 -040055 while (1) {
Aneesh V28a181f2011-11-21 23:38:57 +000056 m = target_freq_khz / ref_freq_khz / 2 * n;
Aneesh V37768012011-07-21 09:10:07 -040057 freq_old = 0;
58 while (1) {
Aneesh V28a181f2011-11-21 23:38:57 +000059 freq = ref_freq_khz * 2 * m / n;
60 if (freq > target_freq_khz) {
Aneesh V37768012011-07-21 09:10:07 -040061 freq = freq_old;
Aneesh V28a181f2011-11-21 23:38:57 +000062 m--;
Aneesh V37768012011-07-21 09:10:07 -040063 break;
64 }
Aneesh V28a181f2011-11-21 23:38:57 +000065 m++;
Aneesh V37768012011-07-21 09:10:07 -040066 freq_old = freq;
67 }
Aneesh V28a181f2011-11-21 23:38:57 +000068 if (freq > freq_optimal) {
69 freq_optimal = freq;
70 m_optimal = m;
71 n_optimal = n;
72 }
73 n++;
74 if ((freq_optimal == target_freq_khz) ||
75 ((ref_freq_khz / n) < 1000)) {
Aneesh V37768012011-07-21 09:10:07 -040076 break;
Aneesh V37768012011-07-21 09:10:07 -040077 }
78 }
Aneesh V28a181f2011-11-21 23:38:57 +000079 n--;
80 *M = m_optimal;
81 *N = n_optimal - 1;
82 printf("ref %d m %d n %d target %d locked %d\n", ref_freq_khz,
83 m_optimal, n_optimal - 1, target_freq_khz, freq_optimal);
Aneesh V37768012011-07-21 09:10:07 -040084 return 0;
85}
86
87void main(void)
88{
89 u32 m, n;
90 printf("\nMPU - 2000000\n");
Aneesh V28a181f2011-11-21 23:38:57 +000091 get_m_n_optimized(2000000, 12000, &m, &n);
92 get_m_n_optimized(2000000, 13000, &m, &n);
93 get_m_n_optimized(2000000, 16800, &m, &n);
94 get_m_n_optimized(2000000, 19200, &m, &n);
95 get_m_n_optimized(2000000, 26000, &m, &n);
96 get_m_n_optimized(2000000, 27000, &m, &n);
97 get_m_n_optimized(2000000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -040098
99 printf("\nMPU - 1200000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000100 get_m_n_optimized(1200000, 12000, &m, &n);
101 get_m_n_optimized(1200000, 13000, &m, &n);
102 get_m_n_optimized(1200000, 16800, &m, &n);
103 get_m_n_optimized(1200000, 19200, &m, &n);
104 get_m_n_optimized(1200000, 26000, &m, &n);
105 get_m_n_optimized(1200000, 27000, &m, &n);
106 get_m_n_optimized(1200000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400107
108 printf("\nMPU - 1584000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000109 get_m_n_optimized(1584000, 12000, &m, &n);
110 get_m_n_optimized(1584000, 13000, &m, &n);
111 get_m_n_optimized(1584000, 16800, &m, &n);
112 get_m_n_optimized(1584000, 19200, &m, &n);
113 get_m_n_optimized(1584000, 26000, &m, &n);
114 get_m_n_optimized(1584000, 27000, &m, &n);
115 get_m_n_optimized(1584000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400116
117 printf("\nCore 1600000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000118 get_m_n_optimized(1600000, 12000, &m, &n);
119 get_m_n_optimized(1600000, 13000, &m, &n);
120 get_m_n_optimized(1600000, 16800, &m, &n);
121 get_m_n_optimized(1600000, 19200, &m, &n);
122 get_m_n_optimized(1600000, 26000, &m, &n);
123 get_m_n_optimized(1600000, 27000, &m, &n);
124 get_m_n_optimized(1600000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400125
126 printf("\nPER 1536000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000127 get_m_n_optimized(1536000, 12000, &m, &n);
128 get_m_n_optimized(1536000, 13000, &m, &n);
129 get_m_n_optimized(1536000, 16800, &m, &n);
130 get_m_n_optimized(1536000, 19200, &m, &n);
131 get_m_n_optimized(1536000, 26000, &m, &n);
132 get_m_n_optimized(1536000, 27000, &m, &n);
133 get_m_n_optimized(1536000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400134
135 printf("\nIVA 1862000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000136 get_m_n_optimized(1862000, 12000, &m, &n);
137 get_m_n_optimized(1862000, 13000, &m, &n);
138 get_m_n_optimized(1862000, 16800, &m, &n);
139 get_m_n_optimized(1862000, 19200, &m, &n);
140 get_m_n_optimized(1862000, 26000, &m, &n);
141 get_m_n_optimized(1862000, 27000, &m, &n);
142 get_m_n_optimized(1862000, 38400, &m, &n);
143
144 printf("\nIVA Nitro - 1290000\n");
145 get_m_n_optimized(1290000, 12000, &m, &n);
146 get_m_n_optimized(1290000, 13000, &m, &n);
147 get_m_n_optimized(1290000, 16800, &m, &n);
148 get_m_n_optimized(1290000, 19200, &m, &n);
149 get_m_n_optimized(1290000, 26000, &m, &n);
150 get_m_n_optimized(1290000, 27000, &m, &n);
151 get_m_n_optimized(1290000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400152
153 printf("\nABE 196608 sys clk\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000154 get_m_n_optimized(196608, 12000, &m, &n);
155 get_m_n_optimized(196608, 13000, &m, &n);
156 get_m_n_optimized(196608, 16800, &m, &n);
157 get_m_n_optimized(196608, 19200, &m, &n);
158 get_m_n_optimized(196608, 26000, &m, &n);
159 get_m_n_optimized(196608, 27000, &m, &n);
160 get_m_n_optimized(196608, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400161
162 printf("\nABE 196608 32K\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000163 get_m_n_optimized(196608000/4, 32768, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400164
165 printf("\nUSB 1920000\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000166 get_m_n_optimized(1920000, 12000, &m, &n);
167 get_m_n_optimized(1920000, 13000, &m, &n);
168 get_m_n_optimized(1920000, 16800, &m, &n);
169 get_m_n_optimized(1920000, 19200, &m, &n);
170 get_m_n_optimized(1920000, 26000, &m, &n);
171 get_m_n_optimized(1920000, 27000, &m, &n);
172 get_m_n_optimized(1920000, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400173
174 printf("\nCore ES1 1523712\n");
Aneesh V28a181f2011-11-21 23:38:57 +0000175 get_m_n_optimized(1524000, 12000, &m, &n);
176 get_m_n_optimized(1524000, 13000, &m, &n);
177 get_m_n_optimized(1524000, 16800, &m, &n);
178 get_m_n_optimized(1524000, 19200, &m, &n);
179 get_m_n_optimized(1524000, 26000, &m, &n);
180 get_m_n_optimized(1524000, 27000, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400181
182 /* exact recommendation for SDPs */
Aneesh V28a181f2011-11-21 23:38:57 +0000183 get_m_n_optimized(1523712, 38400, &m, &n);
Aneesh V37768012011-07-21 09:10:07 -0400184
185}