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