Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 2 | /* |
| 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 10 | */ |
| 11 | #include <stdlib.h> |
| 12 | #include <stdio.h> |
| 13 | typedef 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 V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 49 | int get_m_n_optimized(u32 target_freq_khz, u32 ref_freq_khz, u32 *M, u32 *N) |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 50 | { |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 51 | u32 freq = target_freq_khz; |
| 52 | u32 m_optimal, n_optimal, freq_optimal = 0, freq_old; |
| 53 | u32 m, n; |
| 54 | n = 1; |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 55 | while (1) { |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 56 | m = target_freq_khz / ref_freq_khz / 2 * n; |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 57 | freq_old = 0; |
| 58 | while (1) { |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 59 | freq = ref_freq_khz * 2 * m / n; |
| 60 | if (freq > target_freq_khz) { |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 61 | freq = freq_old; |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 62 | m--; |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 63 | break; |
| 64 | } |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 65 | m++; |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 66 | freq_old = freq; |
| 67 | } |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 68 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 76 | break; |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 77 | } |
| 78 | } |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 79 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | void main(void) |
| 88 | { |
| 89 | u32 m, n; |
| 90 | printf("\nMPU - 2000000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 91 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 98 | |
| 99 | printf("\nMPU - 1200000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 100 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 107 | |
| 108 | printf("\nMPU - 1584000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 109 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 116 | |
| 117 | printf("\nCore 1600000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 118 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 125 | |
| 126 | printf("\nPER 1536000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 127 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 134 | |
| 135 | printf("\nIVA 1862000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 136 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 152 | |
| 153 | printf("\nABE 196608 sys clk\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 154 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 161 | |
| 162 | printf("\nABE 196608 32K\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 163 | get_m_n_optimized(196608000/4, 32768, &m, &n); |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 164 | |
| 165 | printf("\nUSB 1920000\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 166 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 173 | |
| 174 | printf("\nCore ES1 1523712\n"); |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 175 | 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 V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 181 | |
| 182 | /* exact recommendation for SDPs */ |
Aneesh V | 902b92c | 2011-11-21 23:38:57 +0000 | [diff] [blame] | 183 | get_m_n_optimized(1523712, 38400, &m, &n); |
Aneesh V | 0d2628b | 2011-07-21 09:10:07 -0400 | [diff] [blame] | 184 | |
| 185 | } |