Debian Codegraph @

himbaechel/uarch/gatemate/pll.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include <boost/algorithm/string.hpp>
21
22 #include "pack.h"
23
24 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
25 #include "himbaechel_constids.h"
26
27 namespace {
28 USING_NEXTPNR_NAMESPACE;
29
30 double calculate_delta_stepsize(double num)
31 {
32 std::ostringstream out;
33 out << std::fixed << std::setprecision(4) << num;
34 std::string str = out.str();
35 switch (4 - (str.size() - str.find_last_not_of('0') - 1)) {
36 case 4:
37 return 0.0001;
38 case 3:
39 return 0.001;
40 case 2:
41 return 0.01;
42 case 1:
43 return 0.1;
44 default:
45 return 1.0;
46 }
47 }
48
49 int getDCO_optimized_value(int f_dco_min, int f_dco_max, double f_dco)
50 {
51 /*
52 optimization parameters
53 DCO_OPT:
54 1: Optimized to lower DCO frequency.
55 2: Optimized to the middle of the DCO range (DEFAULT)
56 3: Optimized to upper DCO frequency.
57 */
58 const int DCO_OPT = 2; // 1: to lower DCO, 2: to DCO middle range, 3: to upper DCO, default = 2
59 const int DCO_FREQ_OPT_FACTOR = 24; // proportional factor btw. frequency match and dco optimization
60
61 switch (DCO_OPT) {
62 case 1:
63 return round(abs(f_dco - f_dco_min)) * DCO_FREQ_OPT_FACTOR;
64 case 3:
65 return round(abs(f_dco - f_dco_max)) * DCO_FREQ_OPT_FACTOR;
66 default: {
67 int f_dco_middle = (f_dco_max + f_dco_min) / 2; // Middle of DCO Range in MHz
68 return round(abs(f_dco - f_dco_middle)) * DCO_FREQ_OPT_FACTOR;
69 }
70 }
71 }
72
73 void get_M1_M2(double f_core, double f_core_delta, PllCfgRecord &setting, double max_input_freq)
74 {
75 // precise output frequency preference, highest priority
76 // the larger value the higher priority
77 const int OUT_FREQ_OPT_FACTOR = 4200000;
78
79 std::vector<PllCfgRecord> res_arr;
80 for (int M1 = 1; M1 <= 64; M1++) {
81 for (int M2 = 1; M2 <= 1024; M2++) {
82 if (((M1 * M2) % 2) != 0) {
83 // M1*M2-->even number(except one);
84 // this is important to ensure 90 degree phase shifting of clock output
85 if (M1 * M2 != 1)
86 continue;
87 }
88
89 double f_core_local = setting.f_dco / (2 * setting.PDIV1 * M1 * M2);
90 if (f_core_local > max_input_freq / 4)
91 continue; // f_core max limit
92 if ((f_core_local - f_core) < -f_core_delta)
93 break; // lower limit jump to parent loop
94 if (abs(f_core_local - f_core) > f_core_delta)
95 continue; // reg limit continue
96 if (setting.f_dco / setting.PDIV1 > max_input_freq)
97 continue; // M1 input freq limit
98 if ((setting.f_dco / setting.PDIV1) / M1 > max_input_freq / 2)
99 continue; // M2 input freq limit
100
101 PllCfgRecord tmp;
102 tmp.f_core = f_core_local;
103 tmp.M1 = M1;
104 tmp.M2 = M2;
105 tmp.weight = abs(f_core_local - f_core) * OUT_FREQ_OPT_FACTOR;
106 res_arr.push_back(tmp);
107
108 // frequency match
109 if (abs(f_core_local - f_core) <= f_core_delta) {
110 auto it = std::min_element(
111 res_arr.begin(), res_arr.end(),
112 [](const PllCfgRecord &a, const PllCfgRecord &b) { return a.weight < b.weight; });
113 size_t index = std::distance(res_arr.begin(), it);
114 setting.M1 = res_arr[index].M1;
115 setting.M2 = res_arr[index].M2;
116 setting.f_core = res_arr[index].f_core;
117 setting.f_core_delta = abs(res_arr[index].f_core - f_core);
118 setting.core_weight = res_arr[index].weight + setting.M1 + setting.M2 + setting.N1 + setting.N2 +
119 setting.K + setting.PDIV1;
120 return;
121 }
122 }
123 }
124 }
125
126 void get_DCO_ext_feedback(double f_core, double f_ref, PllCfgRecord &setting, int f_dco_min, int f_dco_max,
127 double f_dco, double max_input_freq)
128 {
129 std::vector<PllCfgRecord> res_arr;
130
131 for (int M1 = 1; M1 <= 64; M1++) {
132 for (int M2 = 1; M2 <= 1024; M2++) {
133 if (((M1 * M2) % 2) != 0) {
134 // M1*M2-->even number(except one);
135 // this is important to ensure 90 degree phase shifting of clock output
136 if (M1 * M2 != 1)
137 continue;
138 }
139 double f_dco_local = (f_ref / setting.K) * 2 * setting.PDIV1 * setting.N1 * setting.N2 * M1 * M2;
140 if (f_dco_local > f_dco_max)
141 break; // upper limit
142 if ((f_dco_local < f_dco_min) || (f_dco_local > f_dco_max))
143 continue; // dco out of range limit
144 if (f_dco_local / setting.PDIV1 > max_input_freq)
145 continue; // M1 input freq limit
146 if ((f_dco_local / setting.PDIV1) / M1 > max_input_freq / 2)
147 continue; // M2 input freq limit
148
149 PllCfgRecord tmp;
150 tmp.f_dco = f_dco_local;
151 tmp.M1 = M1;
152 tmp.M2 = M2;
153 tmp.weight = f_dco_local + getDCO_optimized_value(f_dco_min, f_dco_max, f_dco_local);
154 res_arr.push_back(tmp);
155 }
156 }
157
158 auto it = std::min_element(res_arr.begin(), res_arr.end(),
159 [](const PllCfgRecord &a, const PllCfgRecord &b) { return a.weight < b.weight; });
160 size_t index = std::distance(res_arr.begin(), it);
161 setting.M1 = res_arr[index].M1;
162 setting.M2 = res_arr[index].M2;
163 setting.f_dco = res_arr[index].f_dco;
164 }
165 }; // namespace
166
167 NEXTPNR_NAMESPACE_BEGIN
168
169 PllCfgRecord GateMatePacker::get_pll_settings(double f_ref, double f_core, int mode, int low_jitter, bool pdiv0_mux,
170 bool feedback)
171 {
172 const int MATCH_LIMIT = 10; // only for low jitter = false
173 // frequency tolerance for low jitter = false, the maximum frequency deviation in MHz from f_core
174 const double DELTA_LIMIT = 0.1;
175
176 double max_input_freq;
177 int f_dco_min, f_dco_max, pdiv1_min;
178 double f_dco;
179 std::vector<PllCfgRecord> pll_cfg_arr;
180 PllCfgRecord pll_cfg_rec;
181
182 switch (mode) {
183 case 1: {
184 // low power
185 f_dco_min = 500;
186 f_dco_max = 1000;
187 pdiv1_min = 1;
188 max_input_freq = 1000;
189 break;
190 }
191 case 2: {
192 // economy
193 f_dco_min = 1000;
194 f_dco_max = 2000;
195 pdiv1_min = 2;
196 max_input_freq = 1250;
197 break;
198 }
199 default: {
200 // initialize as speed
201 f_dco_min = 1250;
202 f_dco_max = 2500;
203 pdiv1_min = 2;
204 max_input_freq = 1666.67;
205 break;
206 }
207 }
208 double f_core_par = f_core;
209
210 if (f_ref > 50)
211 log_warning("The PLL input frequency is outside the specified frequency (max 50 MHz) range.\n");
212
213 if (pdiv0_mux && feedback) {
214 double res;
215 if (modf(f_core / f_ref, &res) != 0)
216 log_warning("In this PLL mode f_core can only be greater and multiple of f_ref.\n");
217 }
218
219 if (pdiv0_mux) {
220 if (f_core > max_input_freq / 4) {
221 f_core = max_input_freq / 4;
222 log_warning("Frequency out of range; PLL max output frequency for mode: %d: %.5f MHz.\n", mode,
223 max_input_freq / 4);
224 }
225 }
226
227 pll_cfg_rec.K = 1;
228 pll_cfg_rec.N1 = 1;
229 pll_cfg_rec.N2 = 1;
230 pll_cfg_rec.M1 = 1;
231 pll_cfg_rec.M2 = 1;
232 pll_cfg_rec.PDIV1 = 2;
233 pll_cfg_rec.f_dco = 0;
234 pll_cfg_rec.f_core_delta = std::numeric_limits<double>::max();
235 pll_cfg_rec.f_core = pll_cfg_rec.f_dco / (2 * pll_cfg_rec.PDIV1);
236 pll_cfg_rec.core_weight = std::numeric_limits<double>::max();
237 pll_cfg_arr.push_back(pll_cfg_rec);
238
239 double f_core_delta_stepsize = calculate_delta_stepsize(f_core);
240
241 int K = 1;
242 int K_max = low_jitter ? 1 : 1024;
243 int match_cnt = 0;
244 int match_delta_cnt = 0;
245 while (K <= K_max) {
246 for (int N1 = 1; N1 <= 64; N1++) {
247 for (int N2 = 1; N2 <= 1024; N2++) {
248 for (int PDIV1 = pdiv1_min; PDIV1 <= 2; PDIV1++) {
249 if (feedback) {
250 // extern feedback
251 int f_core_local = (f_ref / K) * N1 * N2;
252 if (f_core_local > max_input_freq / 4)
253 continue;
254 if (abs(f_core - f_core_local) < pll_cfg_arr[0].f_core_delta) {
255 // search for best frequency match
256 pll_cfg_arr[0].f_core = f_core_local;
257 pll_cfg_arr[0].f_core_delta = abs(f_core - f_core_local);
258 pll_cfg_arr[0].K = K;
259 pll_cfg_arr[0].N1 = N1;
260 pll_cfg_arr[0].N2 = N2;
261 pll_cfg_arr[0].PDIV1 = PDIV1;
262 }
263 if (pll_cfg_arr[0].f_core_delta == 0) {
264 get_DCO_ext_feedback(f_core, f_ref, pll_cfg_arr[0], f_dco_min, f_dco_max, f_dco,
265 max_input_freq);
266 log_info(" PLL fout= %.4f MHz (fout error %.5f%% of requested %.4f MHz)\n",
267 pll_cfg_arr[0].f_core,
268 100 - (100 * std::min(pll_cfg_arr[0].f_core, f_core_par) /
269 std::max(pll_cfg_arr[0].f_core, f_core_par)),
270 f_core);
271 return pll_cfg_arr[0];
272 }
273 } else {
274 f_dco = (f_ref / K) * PDIV1 * N1 * N2;
275
276 if ((f_dco <= f_dco_min) || (f_dco > f_dco_max))
277 continue; // DCO out of range
278 if (f_dco == 0)
279 continue; // DCO = 0
280 if (f_dco / PDIV1 > max_input_freq)
281 continue; // N1 input freq limit
282 if ((f_dco / PDIV1) / N1 > max_input_freq / 2)
283 continue; // N2 input freq limit
284 if (int(f_core) > int(f_dco / (2 * PDIV1)))
285 continue; // > f_core max
286
287 pll_cfg_rec.f_core = 0;
288 pll_cfg_rec.f_dco = f_dco;
289 pll_cfg_rec.f_core_delta = std::numeric_limits<double>::max();
290 pll_cfg_rec.K = K;
291 pll_cfg_rec.N1 = N1;
292 pll_cfg_rec.N2 = N2;
293 pll_cfg_rec.PDIV1 = PDIV1;
294 pll_cfg_rec.M1 = 0;
295 pll_cfg_rec.M2 = 0;
296 pll_cfg_rec.core_weight = std::numeric_limits<double>::max();
297
298 if (!pdiv0_mux) {
299 // f_core = f_dco/2
300 pll_cfg_rec.M1 = 1;
301 pll_cfg_rec.M2 = 1;
302 pll_cfg_rec.f_core = pll_cfg_rec.f_dco / 2;
303 pll_cfg_rec.core_weight = abs(pll_cfg_rec.f_core - f_core);
304 } else {
305 // default clock path
306 // calculate M1, M2 then calculate weight for intern loop feedback
307 bool found = false;
308 double f_core_delta = 0;
309 while (f_core_delta < (round((max_input_freq / 4) - f_ref / K))) {
310 get_M1_M2(f_core, f_core_delta, pll_cfg_rec, max_input_freq);
311 if (pll_cfg_rec.f_core != 0 && pll_cfg_rec.f_core_delta <= f_core_delta) {
312 // best result
313 pll_cfg_rec.core_weight +=
314 pll_cfg_rec.f_dco + getDCO_optimized_value(f_dco_min, f_dco_max, f_dco);
315 found = true;
316 }
317 if (found)
318 break; // best result found
319 f_core_delta += f_core_delta_stepsize;
320 }
321 }
322 pll_cfg_arr.push_back(pll_cfg_rec);
323 if (pll_cfg_rec.f_core_delta == 0)
324 match_cnt++;
325 if (pll_cfg_rec.f_core_delta < DELTA_LIMIT)
326 match_delta_cnt++;
327 }
328 }
329 }
330 }
331 K++;
332 // only with low_jitter false
333 if (match_cnt > MATCH_LIMIT)
334 break;
335 if ((match_cnt == 0) && (match_delta_cnt > MATCH_LIMIT))
336 break;
337 }
338 if (feedback) {
339 // extern feedback if not exact match pick the best here
340 get_DCO_ext_feedback(f_core, f_ref, pll_cfg_arr[0], f_dco_min, f_dco_max, f_dco, max_input_freq);
341 }
342 auto it =
343 std::min_element(pll_cfg_arr.begin(), pll_cfg_arr.end(), [](const PllCfgRecord &a, const PllCfgRecord &b) {
344 return a.core_weight < b.core_weight;
345 });
346 PllCfgRecord val = pll_cfg_arr.at(std::distance(pll_cfg_arr.begin(), it));
347 log_info(" PLL fout= %.4f MHz (fout error %.5f%% of requested %.4f MHz)\n", val.f_core,
348 100 - (100 * std::min(val.f_core, f_core_par) / std::max(val.f_core, f_core_par)), f_core);
349 return val;
350 }
351
352 NEXTPNR_NAMESPACE_END