1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include "pico.h"
8 #include "hardware/regs/clocks.h"
9 #include "hardware/platform_defs.h"
10 #include "hardware/clocks.h"
11 #include "hardware/watchdog.h"
12 #include "hardware/pll.h"
13 #include "hardware/xosc.h"
14 #include "hardware/irq.h"
15 #include "hardware/gpio.h"
16
17 // The RTC clock frequency is 48MHz divided by power of 2 (to ensure an integer
18 // division ratio will be used in the clocks block). A divisor of 1024 generates
19 // an RTC clock tick of 46875Hz. This frequency is relatively close to the
20 // customary 32 or 32.768kHz 'slow clock' crystals and provides good timing resolution.
21 #define RTC_CLOCK_FREQ_HZ (USB_CLK_KHZ * KHZ / 1024)
22
23 check_hw_layout(clocks_hw_t, clk[clk_adc].selected, CLOCKS_CLK_ADC_SELECTED_OFFSET);
24 check_hw_layout(clocks_hw_t, fc0.result, CLOCKS_FC0_RESULT_OFFSET);
25 check_hw_layout(clocks_hw_t, ints, CLOCKS_INTS_OFFSET);
26
27 static uint32_t configured_freq[CLK_COUNT];
28
29 static resus_callback_t _resus_callback;
30
31 // Clock muxing consists of two components:
32 // - A glitchless mux, which can be switched freely, but whose inputs must be
33 // free-running
34 // - An auxiliary (glitchy) mux, whose output glitches when switched, but has
35 // no constraints on its inputs
36 // Not all clocks have both types of mux.
has_glitchless_mux(enum clock_index clk_index)37 static inline bool has_glitchless_mux(enum clock_index clk_index) {
38 return clk_index == clk_sys || clk_index == clk_ref;
39 }
40
clock_stop(enum clock_index clk_index)41 void clock_stop(enum clock_index clk_index) {
42 clock_hw_t *clock = &clocks_hw->clk[clk_index];
43 hw_clear_bits(&clock->ctrl, CLOCKS_CLK_USB_CTRL_ENABLE_BITS);
44 configured_freq[clk_index] = 0;
45 }
46
47 /// \tag::clock_configure[]
clock_configure(enum clock_index clk_index,uint32_t src,uint32_t auxsrc,uint32_t src_freq,uint32_t freq)48 bool clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq) {
49 uint32_t div;
50
51 assert(src_freq >= freq);
52
53 if (freq > src_freq)
54 return false;
55
56 // Div register is 24.8 int.frac divider so multiply by 2^8 (left shift by 8)
57 div = (uint32_t) (((uint64_t) src_freq << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / freq);
58
59 clock_hw_t *clock = &clocks_hw->clk[clk_index];
60
61 // If increasing divisor, set divisor before source. Otherwise set source
62 // before divisor. This avoids a momentary overspeed when e.g. switching
63 // to a faster source and increasing divisor to compensate.
64 if (div > clock->div)
65 clock->div = div;
66
67 // If switching a glitchless slice (ref or sys) to an aux source, switch
68 // away from aux *first* to avoid passing glitches when changing aux mux.
69 // Assume (!!!) glitchless source 0 is no faster than the aux source.
70 if (has_glitchless_mux(clk_index) && src == CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX) {
71 hw_clear_bits(&clock->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
72 while (!(clock->selected & 1u))
73 tight_loop_contents();
74 }
75 // If no glitchless mux, cleanly stop the clock to avoid glitches
76 // propagating when changing aux mux. Note it would be a really bad idea
77 // to do this on one of the glitchless clocks (clk_sys, clk_ref).
78 else {
79 // Disable clock. On clk_ref and clk_sys this does nothing,
80 // all other clocks have the ENABLE bit in the same position.
81 hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
82 if (configured_freq[clk_index] > 0) {
83 // Delay for 3 cycles of the target clock, for ENABLE propagation.
84 // Note XOSC_COUNT is not helpful here because XOSC is not
85 // necessarily running, nor is timer...:
86 uint delay_cyc = configured_freq[clk_sys] / configured_freq[clk_index] + 1;
87 busy_wait_at_least_cycles(delay_cyc * 3);
88 }
89 }
90
91 // Set aux mux first, and then glitchless mux if this clock has one
92 hw_write_masked(&clock->ctrl,
93 (auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB),
94 CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS
95 );
96
97 if (has_glitchless_mux(clk_index)) {
98 hw_write_masked(&clock->ctrl,
99 src << CLOCKS_CLK_REF_CTRL_SRC_LSB,
100 CLOCKS_CLK_REF_CTRL_SRC_BITS
101 );
102 while (!(clock->selected & (1u << src)))
103 tight_loop_contents();
104 }
105
106 // Enable clock. On clk_ref and clk_sys this does nothing,
107 // all other clocks have the ENABLE bit in the same position.
108 hw_set_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
109
110 // Now that the source is configured, we can trust that the user-supplied
111 // divisor is a safe value.
112 clock->div = div;
113
114 // Store the configured frequency
115 configured_freq[clk_index] = (uint32_t)(((uint64_t) src_freq << 8) / div);
116
117 return true;
118 }
119 /// \end::clock_configure[]
120
clocks_init(void)121 void clocks_init(void) {
122 // Start tick in watchdog, the argument is in 'cycles per microsecond' i.e. MHz
123 watchdog_start_tick(XOSC_KHZ / KHZ);
124
125 // Everything is 48MHz on FPGA apart from RTC. Otherwise set to 0 and will be set in clock configure
126 if (running_on_fpga()) {
127 for (uint i = 0; i < CLK_COUNT; i++) {
128 configured_freq[i] = 48 * MHZ;
129 }
130 configured_freq[clk_rtc] = RTC_CLOCK_FREQ_HZ;
131 return;
132 }
133
134 // Disable resus that may be enabled from previous software
135 clocks_hw->resus.ctrl = 0;
136
137 // Enable the xosc
138 xosc_init();
139
140 // Before we touch PLLs, switch sys and ref cleanly away from their aux sources.
141 hw_clear_bits(&clocks_hw->clk[clk_sys].ctrl, CLOCKS_CLK_SYS_CTRL_SRC_BITS);
142 while (clocks_hw->clk[clk_sys].selected != 0x1)
143 tight_loop_contents();
144 hw_clear_bits(&clocks_hw->clk[clk_ref].ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
145 while (clocks_hw->clk[clk_ref].selected != 0x1)
146 tight_loop_contents();
147
148 /// \tag::pll_init[]
149 pll_init(pll_sys, PLL_COMMON_REFDIV, PLL_SYS_VCO_FREQ_KHZ * KHZ, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2);
150 pll_init(pll_usb, PLL_COMMON_REFDIV, PLL_USB_VCO_FREQ_KHZ * KHZ, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2);
151 /// \end::pll_init[]
152
153 // Configure clocks
154 // CLK_REF = XOSC (usually) 12MHz / 1 = 12MHz
155 clock_configure(clk_ref,
156 CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC,
157 0, // No aux mux
158 XOSC_KHZ * KHZ,
159 XOSC_KHZ * KHZ);
160
161 /// \tag::configure_clk_sys[]
162 // CLK SYS = PLL SYS (usually) 125MHz / 1 = 125MHz
163 clock_configure(clk_sys,
164 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
165 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
166 SYS_CLK_KHZ * KHZ,
167 SYS_CLK_KHZ * KHZ);
168 /// \end::configure_clk_sys[]
169
170 // CLK USB = PLL USB 48MHz / 1 = 48MHz
171 clock_configure(clk_usb,
172 0, // No GLMUX
173 CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
174 USB_CLK_KHZ * KHZ,
175 USB_CLK_KHZ * KHZ);
176
177 // CLK ADC = PLL USB 48MHZ / 1 = 48MHz
178 clock_configure(clk_adc,
179 0, // No GLMUX
180 CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
181 USB_CLK_KHZ * KHZ,
182 USB_CLK_KHZ * KHZ);
183
184 // CLK RTC = PLL USB 48MHz / 1024 = 46875Hz
185 clock_configure(clk_rtc,
186 0, // No GLMUX
187 CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
188 USB_CLK_KHZ * KHZ,
189 RTC_CLOCK_FREQ_HZ);
190
191 // CLK PERI = clk_sys. Used as reference clock for Peripherals. No dividers so just select and enable
192 // Normally choose clk_sys or clk_usb
193 clock_configure(clk_peri,
194 0,
195 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
196 SYS_CLK_KHZ * KHZ,
197 SYS_CLK_KHZ * KHZ);
198 }
199
200 /// \tag::clock_get_hz[]
clock_get_hz(enum clock_index clk_index)201 uint32_t clock_get_hz(enum clock_index clk_index) {
202 return configured_freq[clk_index];
203 }
204 /// \end::clock_get_hz[]
205
clock_set_reported_hz(enum clock_index clk_index,uint hz)206 void clock_set_reported_hz(enum clock_index clk_index, uint hz) {
207 configured_freq[clk_index] = hz;
208 }
209
210 /// \tag::frequency_count_khz[]
frequency_count_khz(uint src)211 uint32_t frequency_count_khz(uint src) {
212 fc_hw_t *fc = &clocks_hw->fc0;
213
214 // If frequency counter is running need to wait for it. It runs even if the source is NULL
215 while(fc->status & CLOCKS_FC0_STATUS_RUNNING_BITS) {
216 tight_loop_contents();
217 }
218
219 // Set reference freq
220 fc->ref_khz = clock_get_hz(clk_ref) / 1000;
221
222 // FIXME: Don't pick random interval. Use best interval
223 fc->interval = 10;
224
225 // No min or max
226 fc->min_khz = 0;
227 fc->max_khz = 0xffffffff;
228
229 // Set SRC which automatically starts the measurement
230 fc->src = src;
231
232 while(!(fc->status & CLOCKS_FC0_STATUS_DONE_BITS)) {
233 tight_loop_contents();
234 }
235
236 // Return the result
237 return fc->result >> CLOCKS_FC0_RESULT_KHZ_LSB;
238 }
239 /// \end::frequency_count_khz[]
240
clocks_handle_resus(void)241 static void clocks_handle_resus(void) {
242 // Set clk_sys back to the ref clock rather than it being forced to clk_ref
243 // by resus. Call the user's resus callback if they have set one
244
245 // CLK SYS = CLK_REF. Must be running for this code to be running
246 uint clk_ref_freq = clock_get_hz(clk_ref);
247 clock_configure(clk_sys,
248 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLK_REF,
249 0,
250 clk_ref_freq,
251 clk_ref_freq);
252
253 // Assert we have been resussed
254 assert(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS);
255
256 // Now we have fixed clk_sys we can safely remove the resus
257 hw_set_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
258 hw_clear_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
259
260 // Now we should no longer be resussed
261 assert(!(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS));
262
263 // Call the user's callback to notify them of the resus event
264 if (_resus_callback) {
265 _resus_callback();
266 }
267 }
268
clocks_irq_handler(void)269 static void clocks_irq_handler(void) {
270 // Clocks interrupt handler. Only resus but handle irq
271 // defensively just in case.
272 uint32_t ints = clocks_hw->ints;
273
274 if (ints & CLOCKS_INTE_CLK_SYS_RESUS_BITS) {
275 ints &= ~CLOCKS_INTE_CLK_SYS_RESUS_BITS;
276 clocks_handle_resus();
277 }
278
279 #ifndef NDEBUG
280 if (ints) {
281 panic("Unexpected clocks irq\n");
282 }
283 #endif
284 }
285
clocks_enable_resus(resus_callback_t resus_callback)286 void clocks_enable_resus(resus_callback_t resus_callback) {
287 // Restart clk_sys if it is stopped by forcing it
288 // to the default source of clk_ref. If clk_ref stops running this will
289 // not work.
290
291 // Store user's resus callback
292 _resus_callback = resus_callback;
293
294 irq_set_exclusive_handler(CLOCKS_IRQ, clocks_irq_handler);
295
296 // Enable the resus interrupt in clocks
297 clocks_hw->inte = CLOCKS_INTE_CLK_SYS_RESUS_BITS;
298
299 // Enable the clocks irq
300 irq_set_enabled(CLOCKS_IRQ, true);
301
302 // 2 * clk_ref freq / clk_sys_min_freq;
303 // assume clk_ref is 3MHz and we want clk_sys to be no lower than 1MHz
304 uint timeout = 2 * 3 * 1;
305
306 // Enable resus with the maximum timeout
307 clocks_hw->resus.ctrl = CLOCKS_CLK_SYS_RESUS_CTRL_ENABLE_BITS | timeout;
308 }
309
clock_gpio_init_int_frac(uint gpio,uint src,uint32_t div_int,uint8_t div_frac)310 void clock_gpio_init_int_frac(uint gpio, uint src, uint32_t div_int, uint8_t div_frac) {
311 // Bit messy but it's as much code to loop through a lookup
312 // table. The sources for each gpout generators are the same
313 // so just call with the sources from GP0
314 uint gpclk = 0;
315 if (gpio == 21) gpclk = clk_gpout0;
316 else if (gpio == 23) gpclk = clk_gpout1;
317 else if (gpio == 24) gpclk = clk_gpout2;
318 else if (gpio == 25) gpclk = clk_gpout3;
319 else {
320 invalid_params_if(CLOCKS, true);
321 }
322
323 // Set up the gpclk generator
324 clocks_hw->clk[gpclk].ctrl = (src << CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_LSB) |
325 CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS;
326 clocks_hw->clk[gpclk].div = (div_int << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) | div_frac;
327
328 // Set gpio pin to gpclock function
329 gpio_set_function(gpio, GPIO_FUNC_GPCK);
330 }
331
332 static const uint8_t gpin0_src[CLK_COUNT] = {
333 CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT0
334 CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT1
335 CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT2
336 CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT3
337 CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_REF
338 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_SYS
339 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_PERI
340 CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_USB
341 CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_ADC
342 CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_RTC
343 };
344
345 // Assert GPIN1 is GPIN0 + 1
346 static_assert(CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
347 static_assert(CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
348 static_assert(CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
349 static_assert(CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
350 static_assert(CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
351 static_assert(CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
352 static_assert(CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
353 static_assert(CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
354 static_assert(CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
355 static_assert(CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
356
clock_configure_gpin(enum clock_index clk_index,uint gpio,uint32_t src_freq,uint32_t freq)357 bool clock_configure_gpin(enum clock_index clk_index, uint gpio, uint32_t src_freq, uint32_t freq) {
358 // Configure a clock to run from a GPIO input
359 uint gpin = 0;
360 if (gpio == 20) gpin = 0;
361 else if (gpio == 22) gpin = 1;
362 else {
363 invalid_params_if(CLOCKS, true);
364 }
365
366 // Work out sources. GPIN is always an auxsrc
367 uint src = 0;
368
369 // GPIN1 == GPIN0 + 1
370 uint auxsrc = gpin0_src[clk_index] + gpin;
371
372 if (has_glitchless_mux(clk_index)) {
373 // AUX src is always 1
374 src = 1;
375 }
376
377 // Set the GPIO function
378 gpio_set_function(gpio, GPIO_FUNC_GPCK);
379
380 // Now we have the src, auxsrc, and configured the gpio input
381 // call clock configure to run the clock from a gpio
382 return clock_configure(clk_index, src, auxsrc, src_freq, freq);
383 }
384