1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
4 #include <metal/machine/platform.h>
6 #ifdef METAL_SIFIVE_FE310_G000_PLL
11 #include <metal/machine.h>
12 #include <metal/drivers/sifive_fe310-g000_pll.h>
15 #define PLL_R 0x00000007UL
16 #define PLL_F 0x000003F0UL
17 #define PLL_Q 0x00000C00UL
18 #define PLL_SEL 0x00010000UL
19 #define PLL_REFSEL 0x00020000UL
20 #define PLL_BYPASS 0x00040000UL
21 #define PLL_LOCK 0x80000000UL
23 #define DIV_DIV 0x0000003FUL
24 #define DIV_1 0x00000100UL
26 #define PLL_R_SHIFT(r) ((r << 0) & PLL_R)
27 #define PLL_F_SHIFT(f) ((f << 4) & PLL_F)
28 #define PLL_Q_SHIFT(q) ((q << 10) & PLL_Q)
29 #define PLL_DIV_SHIFT(d) ((d << 0) & DIV_DIV)
32 unsigned long multiplier;
33 unsigned long divisor;
34 unsigned long min_input_rate;
35 unsigned long max_input_rate;
39 long d; /* < 0 if disabled */
42 static const struct pll_config_t pll_configs[] = {
47 * | | ^ max_input_rate
54 { 1, 32, 12000000, 24000000, 1, 31, 3, 63},
55 { 1, 32, 24000000, 48000000, 3, 31, 2, 63},
56 { 1, 16, 6000000, 12000000, 0, 31, 3, 63},
57 { 1, 16, 12000000, 24000000, 1, 31, 2, 63},
58 { 1, 16, 24000000, 48000000, 3, 31, 2, 31},
59 { 1, 8, 6000000, 12000000, 0, 31, 3, 31},
60 { 1, 8, 12000000, 24000000, 1, 31, 2, 31},
61 { 1, 8, 24000000, 48000000, 3, 31, 2, 15},
62 { 1, 4, 6000000, 12000000, 0, 31, 3, 15},
63 { 1, 4, 12000000, 24000000, 1, 31, 2, 15},
64 { 1, 4, 24000000, 48000000, 3, 31, 2, 7},
65 { 1, 2, 6000000, 12000000, 0, 31, 2, 15},
66 { 1, 2, 12000000, 24000000, 1, 31, 1, 15},
67 { 1, 2, 24000000, 48000000, 3, 31, 1, 7},
68 { 2, 1, 6000000, 12000000, 0, 31, 1, 7},
69 { 2, 1, 12000000, 24000000, 1, 31, 1, 3},
70 { 2, 1, 24000000, 48000000, 3, 31, 3, -1},
71 { 4, 1, 6000000, 12000000, 0, 31, 3, 0},
72 { 4, 1, 12000000, 24000000, 1, 31, 3, -1},
73 { 4, 1, 24000000, 48000000, 3, 31, 2, -1},
74 { 6, 1, 6000000, 10666666, 0, 35, 1, 2},
75 { 6, 1, 10666666, 12000000, 0, 23, 3, -1},
76 { 6, 1, 12000000, 16000000, 1, 47, 3, -1},
77 { 6, 1, 16000000, 18000000, 1, 23, 2, -1},
78 { 6, 1, 18000000, 21333333, 2, 35, 2, -1},
79 { 8, 1, 6000000, 12000000, 0, 31, 3, -1},
80 { 8, 1, 12000000, 24000000, 1, 31, 2, -1},
81 { 8, 1, 24000000, 48000000, 3, 31, 1, -1},
82 {10, 1, 6000000, 9600000, 0, 39, 3, -1},
83 {10, 1, 9600000, 12000000, 0, 19, 2, -1},
84 {10, 1, 12000000, 19200000, 1, 39, 2, -1},
85 {10, 1, 19200000, 24000000, 1, 19, 1, -1},
86 {10, 1, 24000000, 38400000, 3, 39, 1, -1},
87 {12, 1, 6000000, 8000000, 0, 47, 3, -1},
88 {12, 1, 8000000, 12000000, 0, 23, 2, -1},
89 {12, 1, 12000000, 16000000, 1, 47, 2, -1},
90 {12, 1, 16000000, 24000000, 1, 23, 1, -1},
91 {12, 1, 24000000, 30000000, 3, 47, 1, -1},
92 {12, 1, 30000000, 32000000, 3, 47, 1, -1},
93 {14, 1, 6000000, 6857142, 0, 55, 3, -1},
94 {14, 1, 6857143, 12000000, 0, 27, 2, -1},
95 {14, 1, 12000000, 13714285, 1, 55, 2, -1},
96 {14, 1, 13714286, 24000000, 1, 27, 1, -1},
97 {14, 1, 24000000, 27428571, 3, 55, 1, -1},
98 {16, 1, 6000000, 12000000, 0, 31, 2, -1},
99 {16, 1, 12000000, 24000000, 1, 31, 1, -1},
100 {18, 1, 6000000, 10666666, 0, 35, 2, -1},
101 {18, 1, 10666667, 12000000, 0, 17, 1, -1},
102 {18, 1, 12000000, 21333333, 1, 35, 1, -1},
103 {20, 1, 6000000, 9600000, 0, 39, 2, -1},
104 {20, 1, 9600000, 12000000, 0, 19, 1, -1},
105 {20, 1, 12000000, 19200000, 1, 39, 1, -1},
106 {22, 1, 6000000, 8727272, 0, 43, 2, -1},
107 {22, 1, 8727273, 12000000, 0, 21, 1, -1},
108 {22, 1, 12000000, 17454545, 1, 43, 1, -1},
109 {24, 1, 6000000, 8000000, 0, 47, 2, -1},
110 {24, 1, 8000000, 12000000, 0, 23, 1, -1},
111 {24, 1, 12000000, 16000000, 1, 47, 1, -1},
112 {26, 1, 6000000, 7384615, 0, 51, 2, -1},
113 {26, 1, 7384616, 12000000, 0, 25, 1, -1},
114 {26, 1, 12000000, 14768230, 1, 51, 1, -1},
115 {28, 1, 6000000, 6857142, 0, 55, 2, -1},
116 {28, 1, 6857143, 12000000, 0, 27, 1, -1},
117 {28, 1, 12000000, 13714285, 1, 55, 1, -1},
118 {30, 1, 6000000, 6400000, 0, 59, 2, -1},
119 {30, 1, 6400000, 12000000, 0, 29, 1, -1},
120 {30, 1, 12000000, 12800000, 1, 59, 1, -1},
121 {32, 1, 6000000, 12000000, 0, 31, 1, -1}
124 #define PLL_CONFIG_NOT_VALID -1
126 void __metal_driver_sifive_fe310_g000_pll_init(struct __metal_driver_sifive_fe310_g000_pll *pll);
128 /* Given the rate of the PLL input frequency and a PLL configuration, what
129 * will the resulting PLL output frequency be?
131 * - pll_input_rate the PLL input frequency in hertz
132 * - config the PLL configuration
134 * - PLL_CONFIG_NOT_VALID if the configuration is not valid for the input frequency
135 * - the output frequency, in hertz */
136 static long get_pll_config_freq(unsigned long pll_input_rate, const struct pll_config_t *config)
138 if(pll_input_rate < config->min_input_rate || pll_input_rate > config->max_input_rate)
139 return PLL_CONFIG_NOT_VALID;
141 return pll_input_rate * config->multiplier / config->divisor;
144 #ifdef __METAL_DT_SIFIVE_FE310_G000_PLL_HANDLE
146 static void metal_sifive_fe310_g000_pll_init(void) __attribute__((constructor));
147 static void metal_sifive_fe310_g000_pll_init(void) {
148 long init_rate = __metal_driver_sifive_fe310_g000_pll_init_rate();
149 /* If the PLL init_rate is zero, don't initialize the PLL */
151 __metal_driver_sifive_fe310_g000_pll_init(__METAL_DT_SIFIVE_FE310_G000_PLL_HANDLE);
154 #endif /* __METAL_DT_SIFIVE_FE310_G000__PLL_HANDLE */
156 void __metal_driver_sifive_fe310_g000_pll_init(struct __metal_driver_sifive_fe310_g000_pll *pll) {
157 struct metal_clock *pllref = __metal_driver_sifive_fe310_g000_pll_pllref(&(pll->clock));
158 long init_rate = __metal_driver_sifive_fe310_g000_pll_init_rate();
159 long config_offset = __metal_driver_sifive_fe310_g000_pll_config_offset();
160 long base = __metal_driver_sifive_fe310_g000_prci_base();
162 __metal_io_u32 *pllcfg = (__metal_io_u32 *) (base + config_offset);
164 /* If the PLL clock has had a _pre_rate_change_callback configured, call it */
165 _metal_clock_call_all_callbacks(pll->clock._pre_rate_change_callback);
167 /* If we're running off of the PLL, switch off before we start configuring it*/
168 if((__METAL_ACCESS_ONCE(pllcfg) & PLL_SEL) == 0)
169 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_SEL);
171 /* Make sure we're running off of the external oscillator for stability */
173 __METAL_ACCESS_ONCE(pllcfg) |= PLL_REFSEL;
175 /* Configure the PLL to run at the requested init frequency.
176 * Using the vtable instead of the user API because we want to control
177 * when the callbacks occur. */
178 pll->clock.vtable->set_rate_hz(&(pll->clock), init_rate);
180 /* If the PLL clock has had a rate_change_callback configured, call it */
181 _metal_clock_call_all_callbacks(pll->clock._post_rate_change_callback);
184 long __metal_driver_sifive_fe310_g000_pll_get_rate_hz(const struct metal_clock *clock)
186 struct metal_clock *pllref = __metal_driver_sifive_fe310_g000_pll_pllref(clock);
187 struct metal_clock *pllsel0 = __metal_driver_sifive_fe310_g000_pll_pllsel0(clock);
188 long config_offset = __metal_driver_sifive_fe310_g000_pll_config_offset(clock);
189 struct __metal_driver_sifive_fe310_g000_prci *config_base =
190 __metal_driver_sifive_fe310_g000_pll_config_base(clock);
191 long divider_offset = __metal_driver_sifive_fe310_g000_pll_divider_offset(clock);
192 struct __metal_driver_sifive_fe310_g000_prci *divider_base =
193 __metal_driver_sifive_fe310_g000_pll_divider_base(clock);
194 const struct __metal_driver_vtable_sifive_fe310_g000_prci *vtable =
195 __metal_driver_sifive_fe310_g000_prci_vtable();
197 long cfg = vtable->get_reg(config_base, config_offset);
198 long div = vtable->get_reg(divider_base, divider_offset);
200 /* At the end of the PLL there's one big mux: it either selects the HFROSC
201 * (bypassing the PLL entirely) or uses the PLL. */
202 if (__METAL_GET_FIELD(cfg, PLL_SEL) == 0)
203 return metal_clock_get_rate_hz(pllsel0);
205 /* There's a clock mux before the PLL that selects between the HFROSC adn
206 * the HFXOSC as the PLL's input clock. */
207 long ref_hz = metal_clock_get_rate_hz(__METAL_GET_FIELD(cfg, PLL_REFSEL) ? pllref : pllsel0);
209 /* It's possible to bypass the PLL, which is an internal bpyass. This
210 * still obays the PLL's input clock mu. */
211 if (__METAL_GET_FIELD(cfg, PLL_BYPASS))
214 /* Logically the PLL is a three stage div-mul-div. */
215 long div_r = __METAL_GET_FIELD(cfg, PLL_R) + 1;
216 long mul_f = 2 * (__METAL_GET_FIELD(cfg, PLL_F) + 1);
217 if (__METAL_GET_FIELD(cfg, PLL_Q) == 0)
219 long div_q = 1 << __METAL_GET_FIELD(cfg, PLL_Q);
221 /* In addition to the dividers inherent in the PLL, there's an additional
222 * clock divider that lives after the PLL and lets us pick a more
223 * interesting range of frequencies. */
224 long pllout = (((ref_hz / div_r) * mul_f) / div_q);
225 if (__METAL_GET_FIELD(div, DIV_1))
228 return pllout / (2 * (__METAL_GET_FIELD(div, DIV_DIV) + 1));
231 /* Find a valid configuration for the PLL which is closest to the desired
234 * - ref_hz PLL input frequency
235 * - rate desired PLL output frequency
237 * -1 if no valid configuration is available
238 * the index into pll_configs of a valid configuration */
239 static int find_closest_config(long ref_hz, long rate)
241 int closest_index = -1;
242 long closest_diff = LONG_MAX;
244 /* We're probably trying for a fast output frequency, so start from
245 * the high end of the configs. */
246 for(int i = (sizeof(pll_configs) / sizeof(pll_configs[0])) - 1; i >= 0; i--)
248 long config_freq = get_pll_config_freq(ref_hz, &(pll_configs[i]));
249 if(config_freq != PLL_CONFIG_NOT_VALID)
251 long freq_diff = abs(config_freq - rate);
252 if(freq_diff < closest_diff)
255 closest_diff = freq_diff;
260 return closest_index;
263 /* Configure the PLL and wait for it to lock */
264 static void configure_pll(__metal_io_u32 *pllcfg, __metal_io_u32 *plloutdiv, const struct pll_config_t *config)
266 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_R);
267 __METAL_ACCESS_ONCE(pllcfg) |= PLL_R_SHIFT(config->r);
269 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_F);
270 __METAL_ACCESS_ONCE(pllcfg) |= PLL_F_SHIFT(config->f);
272 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_Q);
273 __METAL_ACCESS_ONCE(pllcfg) |= PLL_Q_SHIFT(config->q);
277 /* disable final divider */
278 __METAL_ACCESS_ONCE(plloutdiv) |= DIV_1;
280 __METAL_ACCESS_ONCE(plloutdiv) &= ~(DIV_DIV);
281 __METAL_ACCESS_ONCE(plloutdiv) |= PLL_DIV_SHIFT(1);
285 __METAL_ACCESS_ONCE(plloutdiv) &= ~(DIV_1);
287 __METAL_ACCESS_ONCE(plloutdiv) &= ~(DIV_DIV);
288 __METAL_ACCESS_ONCE(plloutdiv) |= PLL_DIV_SHIFT(config->d);
291 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_BYPASS);
293 /* Wait for PLL to lock */
294 while((__METAL_ACCESS_ONCE(pllcfg) & PLL_LOCK) == 0) ;
297 long __metal_driver_sifive_fe310_g000_pll_set_rate_hz(struct metal_clock *clock, long rate)
299 struct metal_clock *pllref = __metal_driver_sifive_fe310_g000_pll_pllref(clock);
300 struct metal_clock *pllsel0 = __metal_driver_sifive_fe310_g000_pll_pllsel0(clock);
301 long config_offset = __metal_driver_sifive_fe310_g000_pll_config_offset(clock);
302 long divider_offset = __metal_driver_sifive_fe310_g000_pll_divider_offset(clock);
303 long base = __metal_driver_sifive_fe310_g000_prci_base();
305 __metal_io_u32 *pllcfg = (__metal_io_u32 *) (base + config_offset);
306 __metal_io_u32 *plloutdiv = (__metal_io_u32 *) (base + divider_offset);
308 /* We can't modify the PLL if coreclk is driven by it, so switch it off */
309 if (__METAL_ACCESS_ONCE(pllcfg) & PLL_SEL)
310 __METAL_ACCESS_ONCE(pllcfg) &= ~(PLL_SEL);
312 /* There's a clock mux before the PLL that selects between the HFROSC and
313 * the HFXOSC as the PLL's input clock. */
314 long ref_hz = metal_clock_get_rate_hz(__METAL_ACCESS_ONCE(pllcfg) & PLL_REFSEL ? pllref : pllsel0);
316 /* if the desired rate is within 75%-125% of the input clock, bypass the PLL */
317 if((ref_hz * 3 / 4) <= rate && (ref_hz * 5 / 4) >= rate)
319 __METAL_ACCESS_ONCE(pllcfg) |= PLL_BYPASS;
323 int config_index = find_closest_config(ref_hz, rate);
324 if(config_index != -1)
326 configure_pll(pllcfg, plloutdiv, &(pll_configs[config_index]));
330 /* unable to find a valid configuration */
331 __METAL_ACCESS_ONCE(pllcfg) |= PLL_BYPASS;
336 __METAL_ACCESS_ONCE(pllcfg) |= PLL_SEL;
338 return __metal_driver_sifive_fe310_g000_pll_get_rate_hz(clock);
341 #ifdef __METAL_DT_SIFIVE_FE310_G000_PLL_HANDLE
342 static void use_hfxosc(void) __attribute__((constructor));
343 static void use_hfxosc(void)
345 long init_rate = __metal_driver_sifive_fe310_g000_pll_init_rate();
346 metal_clock_set_rate_hz(
347 &__METAL_DT_SIFIVE_FE310_G000_PLL_HANDLE->clock, init_rate
352 __METAL_DEFINE_VTABLE(__metal_driver_vtable_sifive_fe310_g000_pll) = {
353 .init = __metal_driver_sifive_fe310_g000_pll_init,
354 .clock.get_rate_hz = __metal_driver_sifive_fe310_g000_pll_get_rate_hz,
355 .clock.set_rate_hz = __metal_driver_sifive_fe310_g000_pll_set_rate_hz,
358 #endif /* METAL_SIFIVE_FE310_G000_PLL */
360 typedef int no_empty_translation_units;