same70/pll.h
Go to the documentation of this file.
1 
33 /*
34  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
35  */
36 
37 #ifndef CHIP_PLL_H_INCLUDED
38 #define CHIP_PLL_H_INCLUDED
39 
40 #include <osc.h>
41 
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
55 #define PLL_OUTPUT_MIN_HZ 160000000
56 #define PLL_OUTPUT_MAX_HZ 500000000
57 
58 #define PLL_INPUT_MIN_HZ 3000000
59 #define PLL_INPUT_MAX_HZ 32000000
60 
61 #define NR_PLLS 2
62 #define PLLA_ID 0
63 #define UPLL_ID 1
64 
65 #define PLL_UPLL_HZ 480000000
66 
67 #define PLL_COUNT 0x3fU
68 
69 enum pll_source {
76 };
77 
78 struct pll_config {
79  uint32_t ctrl;
80 };
81 
82 #define pll_get_default_rate(pll_id) \
83  ((osc_get_rate(CONFIG_PLL##pll_id##_SOURCE) \
84  * CONFIG_PLL##pll_id##_MUL) \
85  / CONFIG_PLL##pll_id##_DIV)
86 
87 /* Force UTMI PLL parameters (Hardware defined) */
88 #ifdef CONFIG_PLL1_SOURCE
89 # undef CONFIG_PLL1_SOURCE
90 #endif
91 #ifdef CONFIG_PLL1_MUL
92 # undef CONFIG_PLL1_MUL
93 #endif
94 #ifdef CONFIG_PLL1_DIV
95 # undef CONFIG_PLL1_DIV
96 #endif
97 #define CONFIG_PLL1_SOURCE PLL_SRC_MAINCK_XTAL
98 #define CONFIG_PLL1_MUL 0
99 #define CONFIG_PLL1_DIV 0
100 
106 static inline void pll_config_init(struct pll_config *p_cfg,
107  enum pll_source e_src, uint32_t ul_div, uint32_t ul_mul)
108 {
109  uint32_t vco_hz;
110 
111  Assert(e_src < PLL_NR_SOURCES);
112 
113  if (ul_div == 0 && ul_mul == 0) { /* Must only be true for UTMI PLL */
115  } else { /* PLLA */
116  /* Calculate internal VCO frequency */
117  vco_hz = osc_get_rate(e_src) / ul_div;
118  Assert(vco_hz >= PLL_INPUT_MIN_HZ);
119  Assert(vco_hz <= PLL_INPUT_MAX_HZ);
120 
121  vco_hz *= ul_mul;
122  Assert(vco_hz >= PLL_OUTPUT_MIN_HZ);
123  Assert(vco_hz <= PLL_OUTPUT_MAX_HZ);
124 
125  /* PMC hardware will automatically make it mul+1 */
126  p_cfg->ctrl = CKGR_PLLAR_MULA(ul_mul - 1) | CKGR_PLLAR_DIVA(ul_div) \
128  }
129 }
130 
131 #define pll_config_defaults(cfg, pll_id) \
132  pll_config_init(cfg, \
133  CONFIG_PLL##pll_id##_SOURCE, \
134  CONFIG_PLL##pll_id##_DIV, \
135  CONFIG_PLL##pll_id##_MUL)
136 
137 static inline void pll_config_read(struct pll_config *p_cfg, uint32_t ul_pll_id)
138 {
139  Assert(ul_pll_id < NR_PLLS);
140 
141  if (ul_pll_id == PLLA_ID) {
142  p_cfg->ctrl = PMC->CKGR_PLLAR;
143  } else {
144  p_cfg->ctrl = PMC->CKGR_UCKR;
145  }
146 }
147 
148 static inline void pll_config_write(const struct pll_config *p_cfg, uint32_t ul_pll_id)
149 {
150  Assert(ul_pll_id < NR_PLLS);
151 
152  if (ul_pll_id == PLLA_ID) {
153  pmc_disable_pllack(); // Always stop PLL first!
154  PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
155  } else {
156  PMC->CKGR_UCKR = p_cfg->ctrl;
157  }
158 }
159 
160 static inline void pll_enable(const struct pll_config *p_cfg, uint32_t ul_pll_id)
161 {
162  Assert(ul_pll_id < NR_PLLS);
163 
164  if (ul_pll_id == PLLA_ID) {
165  pmc_disable_pllack(); // Always stop PLL first!
166  PMC->CKGR_PLLAR = CKGR_PLLAR_ONE | p_cfg->ctrl;
167  } else {
168  PMC->CKGR_UCKR = p_cfg->ctrl | CKGR_UCKR_UPLLEN;
169  }
170 }
171 
175 static inline void pll_disable(uint32_t ul_pll_id)
176 {
177  Assert(ul_pll_id < NR_PLLS);
178 
179  if (ul_pll_id == PLLA_ID) {
181  } else {
182  PMC->CKGR_UCKR &= ~CKGR_UCKR_UPLLEN;
183  }
184 }
185 
186 static inline uint32_t pll_is_locked(uint32_t ul_pll_id)
187 {
188  Assert(ul_pll_id < NR_PLLS);
189 
190  if (ul_pll_id == PLLA_ID) {
191  return pmc_is_locked_pllack();
192  } else {
193  return pmc_is_locked_upll();
194  }
195 }
196 
197 static inline void pll_enable_source(enum pll_source e_src)
198 {
199  switch (e_src) {
203  case PLL_SRC_MAINCK_XTAL:
205  osc_enable(e_src);
206  osc_wait_ready(e_src);
207  break;
208 
209  default:
210  Assert(false);
211  break;
212  }
213 }
214 
215 static inline void pll_enable_config_defaults(unsigned int ul_pll_id)
216 {
217  struct pll_config pllcfg;
218 
219  if (pll_is_locked(ul_pll_id)) {
220  return; // Pll already running
221  }
222  switch (ul_pll_id) {
223 #ifdef CONFIG_PLL0_SOURCE
224  case 0:
226  pll_config_init(&pllcfg,
230  break;
231 #endif
232 #ifdef CONFIG_PLL1_SOURCE
233  case 1:
235  pll_config_init(&pllcfg,
239  break;
240 #endif
241  default:
242  Assert(false);
243  break;
244  }
245  pll_enable(&pllcfg, ul_pll_id);
246  while (!pll_is_locked(ul_pll_id));
247 }
248 
250 
252 
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 
259 #endif /* CHIP_PLL_H_INCLUDED */
static void osc_wait_ready(uint8_t id)
Wait until the oscillator identified by id is ready.
Definition: osc.h:162
#define PMC
(PMC ) Base Address
Definition: same70j19.h:524
#define CONFIG_PLL1_SOURCE
Definition: same70/pll.h:97
static void pll_config_write(const struct pll_config *p_cfg, uint32_t ul_pll_id)
Definition: same70/pll.h:148
static uint32_t osc_get_rate(uint32_t ul_id)
Definition: same70/osc.h:196
#define PLL_OUTPUT_MIN_HZ
Definition: same70/pll.h:55
#define CKGR_UCKR_UPLLCOUNT(value)
#define PLL_OUTPUT_MAX_HZ
Definition: same70/pll.h:56
#define CKGR_PLLAR_MULA(value)
Internal 8MHz RC oscillator.
Definition: same70/pll.h:71
#define PLL_INPUT_MAX_HZ
Definition: same70/pll.h:59
#define OSC_MAINCK_12M_RC
Internal 12MHz RC oscillator.
Definition: same70/osc.h:92
#define CKGR_UCKR_UPLLEN
(CKGR_UCKR) UTMI PLL Enable
static void pll_disable(uint32_t ul_pll_id)
Definition: same70/pll.h:175
Internal 12MHz RC oscillator.
Definition: same70/pll.h:72
Number of PLL sources.
Definition: same70/pll.h:75
pll_source
PLL clock source.
Definition: same70/pll.h:69
#define CONFIG_PLL1_MUL
Definition: same70/pll.h:98
static void pll_enable_source(enum pll_source e_src)
Enable the source of the pll. The source is enabled, if the source is not already running...
Definition: same70/pll.h:197
#define PLL_COUNT
Definition: same70/pll.h:67
External bypass oscillator.
Definition: same70/pll.h:74
uint32_t pmc_is_locked_pllack(void)
Is PLLA locked?
Definition: pmc.c:591
static void pll_enable(const struct pll_config *p_cfg, uint32_t ul_pll_id)
Definition: same70/pll.h:160
static void osc_enable(uint32_t ul_id)
Definition: same70/osc.h:109
#define CKGR_PLLAR_ONE
(CKGR_PLLAR) Must Be Set to 1
#define CONFIG_PLL0_DIV
Definition: conf_clock.h:70
#define CONFIG_PLL1_DIV
Definition: same70/pll.h:99
#define CKGR_PLLAR_DIVA(value)
#define CONFIG_PLL0_SOURCE
Definition: conf_clock.h:68
Hardware-specific representation of PLL configuration.
Definition: same70/pll.h:78
uint32_t ctrl
Definition: same70/pll.h:79
#define OSC_MAINCK_BYPASS
External bypass oscillator.
Definition: same70/osc.h:94
Internal 4MHz RC oscillator.
Definition: same70/pll.h:70
#define CKGR_PLLAR_PLLACOUNT(value)
External crystal oscillator.
Definition: same70/pll.h:73
static void pll_config_read(struct pll_config *p_cfg, uint32_t ul_pll_id)
Definition: same70/pll.h:137
#define CONFIG_PLL0_MUL
Definition: conf_clock.h:69
#define PLL_INPUT_MIN_HZ
Definition: same70/pll.h:58
static void pll_enable_config_defaults(unsigned int ul_pll_id)
Enable the pll with the default configuration. PLL is enabled, if the PLL is not already locked...
Definition: same70/pll.h:215
#define OSC_MAINCK_8M_RC
Internal 8MHz RC oscillator.
Definition: same70/osc.h:91
#define OSC_MAINCK_4M_RC
Internal 4MHz RC oscillator.
Definition: same70/osc.h:90
#define OSC_MAINCK_XTAL
External crystal oscillator.
Definition: same70/osc.h:93
static void pll_config_init(struct pll_config *p_cfg, enum pll_source e_src, uint32_t ul_div, uint32_t ul_mul)
Definition: same70/pll.h:106
#define PLLA_ID
Definition: same70/pll.h:62
Chip-specific oscillator management functions.
#define Assert(expr)
This macro is used to test fatal errors.
Definition: compiler.h:196
#define NR_PLLS
Number of on-chip PLLs.
Definition: same70/pll.h:61
static uint32_t pll_is_locked(uint32_t ul_pll_id)
Definition: same70/pll.h:186
void pmc_disable_pllack(void)
Disable PLLA clock.
Definition: pmc.c:576


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58