1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "pico.h"
8 
9 // For MHZ definitions etc
10 #include "hardware/clocks.h"
11 
12 #include "hardware/platform_defs.h"
13 #include "hardware/regs/xosc.h"
14 #include "hardware/structs/xosc.h"
15 
xosc_init(void)16 void xosc_init(void) {
17     // Assumes 1-15 MHz input
18     assert(XOSC_MHZ <= 15);
19     xosc_hw->ctrl = XOSC_CTRL_FREQ_RANGE_VALUE_1_15MHZ;
20 
21     // Set xosc startup delay
22     uint32_t startup_delay = (((12 * MHZ) / 1000) + 128) / 256;
23     xosc_hw->startup = startup_delay;
24 
25     // Set the enable bit now that we have set freq range and startup delay
26     hw_set_bits(&xosc_hw->ctrl, XOSC_CTRL_ENABLE_VALUE_ENABLE << XOSC_CTRL_ENABLE_LSB);
27 
28     // Wait for XOSC to be stable
29     while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS));
30 }
31 
xosc_disable(void)32 void xosc_disable(void) {
33     uint32_t tmp = xosc_hw->ctrl;
34     tmp &= (~XOSC_CTRL_ENABLE_BITS);
35     tmp |= (XOSC_CTRL_ENABLE_VALUE_DISABLE << XOSC_CTRL_ENABLE_LSB);
36     xosc_hw->ctrl = tmp;
37     // Wait for stable to go away
38     while(xosc_hw->status & XOSC_STATUS_STABLE_BITS);
39 }
40 
xosc_dormant(void)41 void xosc_dormant(void) {
42     // WARNING: This stops the xosc until woken up by an irq
43     xosc_hw->dormant = XOSC_DORMANT_VALUE_DORMANT;
44     // Wait for it to become stable once woken up
45     while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS));
46 }