]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/asf/common/services/clock/sam4l/sysclk.h
Add SAM4L demo.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4L_Atmel_Studio / src / asf / common / services / clock / sam4l / sysclk.h
1 /**\r
2  * \file\r
3  *\r
4  * \brief Chip-specific system clock management functions\r
5  *\r
6  * Copyright (c) 2012 Atmel Corporation. All rights reserved.\r
7  *\r
8  * \asf_license_start\r
9  *\r
10  * \page License\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions are met:\r
14  *\r
15  * 1. Redistributions of source code must retain the above copyright notice,\r
16  *    this list of conditions and the following disclaimer.\r
17  *\r
18  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
19  *    this list of conditions and the following disclaimer in the documentation\r
20  *    and/or other materials provided with the distribution.\r
21  *\r
22  * 3. The name of Atmel may not be used to endorse or promote products derived\r
23  *    from this software without specific prior written permission.\r
24  *\r
25  * 4. This software may only be redistributed and used in connection with an\r
26  *    Atmel microcontroller product.\r
27  *\r
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
38  * POSSIBILITY OF SUCH DAMAGE.\r
39  *\r
40  * \asf_license_stop\r
41  *\r
42  */\r
43 #ifndef CHIP_SYSCLK_H_INCLUDED\r
44 #define CHIP_SYSCLK_H_INCLUDED\r
45 \r
46 #include <board.h>\r
47 \r
48 #ifdef __cplusplus\r
49 extern "C" {\r
50 #endif\r
51 \r
52 /**\r
53  * \page sysclk_quickstart Quick Start Guide for the System Clock Management service (SAM4L)\r
54  *\r
55  * This is the quick start guide for the \ref sysclk_group "System Clock Management"\r
56  * service, with step-by-step instructions on how to configure and use the service for\r
57  * specific use cases.\r
58  *\r
59  * \section sysclk_quickstart_usecases System Clock Management use cases\r
60  * - \ref sysclk_quickstart_basic\r
61  *\r
62  * \section sysclk_quickstart_basic Basic usage of the System Clock Management service\r
63  * This section will present a basic use case for the System Clock Management service.\r
64  * This use case will configure the main system clock to 48MHz, using an internal DFLL\r
65  * module to multiply the frequency of a crystal attached to the microcontroller. The\r
66  * peripheral bus clocks are scaled down from the speed of the main system clock.\r
67  *\r
68  * \subsection sysclk_quickstart_use_case_1_prereq Prerequisites\r
69  *  - None\r
70  *\r
71  * \subsection sysclk_quickstart_use_case_1_setup_steps Initialization code\r
72  * Add to the application initialization code:\r
73  * \code\r
74  *    sysclk_init();\r
75  * \endcode\r
76  *\r
77  * \subsection sysclk_quickstart_use_case_1_setup_steps_workflow Workflow\r
78  * -# Configure the system clocks according to the settings in conf_clock.h:\r
79  *    \code sysclk_init(); \endcode\r
80  *\r
81  * \subsection sysclk_quickstart_use_case_1_example_code Example code\r
82  *   Add or uncomment the following in your conf_clock.h header file, commenting out all other\r
83  *   definitions of the same symbol(s):\r
84  *   \code\r
85  *   #define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_DFLL0\r
86  *\r
87  *   // Fdfll = (Fclk * DFLL_mul) / DFLL_div\r
88  *   #define CONFIG_DFLL0_SOURCE         GENCLK_SRC_OSC32K\r
89  *   #define CONFIG_DFLL0_FREQ           48000000UL\r
90  *   #define CONFIG_DFLL0_MUL            (CONFIG_DFLL0_FREQ / BOARD_OSC32_HZ)\r
91  *   #define CONFIG_DFLL0_DIV            1\r
92  *\r
93  *   // Fbus = Fsys / (2 ^ BUS_div)\r
94  *   #define CONFIG_SYSCLK_CPU_DIV       0\r
95  *   #define CONFIG_SYSCLK_PBA_DIV       1\r
96  *   #define CONFIG_SYSCLK_PBB_DIV       1\r
97  *   #define CONFIG_SYSCLK_PBC_DIV       1\r
98  *   #define CONFIG_SYSCLK_PBD_DIV       1\r
99  *   \endcode\r
100  *\r
101  * \subsection sysclk_quickstart_use_case_1_example_workflow Workflow\r
102  *  -# Configure the main system clock to use the output of the DFLL0 module as its source:\r
103  *   \code #define CONFIG_SYSCLK_SOURCE      SYSCLK_SRC_DFLL0 \endcode\r
104  *  -# Configure the DFLL0 module to use external crystal oscillator OSC0 as its source:\r
105  *   \code #define CONFIG_DFLL0_SOURCE       GENCLK_SRC_OSC32K \endcode\r
106  *  -# Configure the DFLL0 module to multiply the external oscillator OSC0 frequency up to 48MHz:\r
107  *   \code\r
108  *   #define CONFIG_DFLL0_FREQ           48000000UL\r
109  *   #define CONFIG_DFLL0_MUL            (CONFIG_DFLL0_FREQ / BOARD_OSC32_HZ)\r
110  *   #define CONFIG_DFLL0_DIV            1\r
111  *   \endcode\r
112  *   \note For user boards, \c BOARD_OSC0_HZ should be defined in the board \c conf_board.h configuration\r
113  *         file as the frequency of the crystal attached to OSC0.\r
114  *  -# Configure the main clock to run at the full 48MHz, scale the peripheral busses to run at one\r
115  *     half (2 to the power of 1) of the system clock speed:\r
116  *    \code\r
117  *    #define CONFIG_SYSCLK_CPU_DIV       0\r
118  *    #define CONFIG_SYSCLK_PBA_DIV       1\r
119  *    #define CONFIG_SYSCLK_PBB_DIV       1\r
120  *    #define CONFIG_SYSCLK_PBC_DIV       1\r
121  *    #define CONFIG_SYSCLK_PBD_DIV       1\r
122  *    \endcode\r
123  *    \note Some dividers are powers of two, while others are integer division factors. Refer to the\r
124  *          formulas in the conf_clock.h template commented above each division define.\r
125  */\r
126 \r
127 \r
128 /**\r
129  * \weakgroup sysclk_group\r
130  * @{\r
131  */\r
132 \r
133 //! \name System clock source\r
134 //@{\r
135 #define SYSCLK_SRC_RCSYS        0     //!< System RC oscillator\r
136 #define SYSCLK_SRC_OSC0         1     //!< Oscillator 0\r
137 #define SYSCLK_SRC_PLL0         2     //!< Phase Locked Loop 0\r
138 #define SYSCLK_SRC_DFLL         3     //!< Digital Frequency Locked Loop\r
139 #define SYSCLK_SRC_RC80M        4     //!< 80 MHz RC oscillator\r
140 #define SYSCLK_SRC_RCFAST       5     //!< 4-8-12 MHz RC oscillator\r
141 #define SYSCLK_SRC_RC1M         6     //!< 1 MHz RC oscillator\r
142 //@}\r
143 \r
144 //! \name USB Clock Sources\r
145 //@{\r
146 #define USBCLK_SRC_OSC0         GENCLK_SRC_OSC0     //!< Use OSC0\r
147 #define USBCLK_SRC_PLL0         GENCLK_SRC_PLL0     //!< Use PLL0\r
148 #define USBCLK_SRC_DFLL         GENCLK_SRC_DFLL     //!< Use DFLL\r
149 #define USBCLK_SRC_GCLKIN0      GENCLK_SRC_GCLKIN0  //!< Use GCLKIN0\r
150 //@}\r
151 \r
152 //! \name Bus index of maskable module clocks\r
153 //@{\r
154 #define PM_CLK_GRP_CPU               0\r
155 #define PM_CLK_GRP_HSB               1\r
156 #define PM_CLK_GRP_PBA               2\r
157 #define PM_CLK_GRP_PBB               3\r
158 #define PM_CLK_GRP_PBC               4\r
159 #define PM_CLK_GRP_PBD               5\r
160 //@}\r
161 \r
162 //! \name Clocks derived from the CPU clock\r
163 //@{\r
164 //! On-Chip Debug system\r
165 #define SYSCLK_OCD              0\r
166 //@}\r
167 \r
168 //! \name Clocks derived from the HSB clock\r
169 //@{\r
170 //! PDCA memory interface\r
171 #define SYSCLK_PDCA_HSB         0\r
172 //! Flash data interface\r
173 #define SYSCLK_HFLASHC_DATA     1\r
174 //! HRAMC data interface\r
175 #define SYSCLK_HRAMC1_DATA      2\r
176 //! USBC DMA and FIFO interface\r
177 #define SYSCLK_USBC_DATA        3\r
178 //! CRCCU data interface\r
179 #define SYSCLK_CRCCU_DATA       4\r
180 //! HSB<->PBA bridge\r
181 #define SYSCLK_PBA_BRIDGE       5\r
182 //! HSB<->PBB bridge\r
183 #define SYSCLK_PBB_BRIDGE       6\r
184 //! HSB<->PBC bridge\r
185 #define SYSCLK_PBC_BRIDGE       7\r
186 //! HSB<->PBD bridge\r
187 #define SYSCLK_PBD_BRIDGE       8\r
188 //! Advanced Encryption Standard\r
189 #define SYSCLK_AESA_HSB         9\r
190 //@}\r
191 \r
192 //! \name Clocks derived from the PBA clock\r
193 //@{\r
194 //! IISC Controller\r
195 #define SYSCLK_IISC             0\r
196 //! SPI Controller\r
197 #define SYSCLK_SPI              1\r
198 //! Timer/Counter 0\r
199 #define SYSCLK_TC0              2\r
200 //! Timer/Counter 1\r
201 #define SYSCLK_TC1              3\r
202 //! TWI Master 0\r
203 #define SYSCLK_TWIM0            4\r
204 //! TWI Slave 0\r
205 #define SYSCLK_TWIS0            5\r
206 //! TWI Master 1\r
207 #define SYSCLK_TWIM1            6\r
208 //! TWI Slave 1\r
209 #define SYSCLK_TWIS1            7\r
210 //! USART 0\r
211 #define SYSCLK_USART0           8\r
212 //! USART 1\r
213 #define SYSCLK_USART1           9\r
214 //! USART 2\r
215 #define SYSCLK_USART2           10\r
216 //! USART 3\r
217 #define SYSCLK_USART3           11\r
218 //! A/D Converter\r
219 #define SYSCLK_ADCIFE           12\r
220 //! D/A Converter\r
221 #define SYSCLK_DACC             13\r
222 //! Analog Comparator\r
223 #define SYSCLK_ACIFC            14\r
224 //! Glue Logic Controller\r
225 #define SYSCLK_GLOC             15\r
226 //! ABDACB Controller\r
227 #define SYSCLK_ABDACB           16\r
228 //! TRNG Controller\r
229 #define SYSCLK_TRNG             17\r
230 //! PARC Controller\r
231 #define SYSCLK_PARC             18\r
232 //! Capacitive Touch\r
233 #define SYSCLK_CATB             19\r
234 //! TWI Master 2\r
235 #define SYSCLK_TWIM2            21\r
236 //! TWI Master 3\r
237 #define SYSCLK_TWIM3            22\r
238 //! LCD Controller\r
239 #define SYSCLK_LCDCA            23\r
240 \r
241 //@}\r
242 \r
243 //! \name Clocks derived from the PBB clock\r
244 //@{\r
245 //! Flash Controller registers\r
246 #define SYSCLK_HFLASHC_REGS     0\r
247 //! HRAMC Controller registers\r
248 #define SYSCLK_HRAMC1_REGS      1\r
249 //! HSB Matrix configuration\r
250 #define SYSCLK_HMATRIX          2\r
251 //! PDCA peripheral bus interface\r
252 #define SYSCLK_PDCA_PB          3\r
253 //! CRCCU registers\r
254 #define SYSCLK_CRCCU_REGS       4\r
255 //! USBC registers\r
256 #define SYSCLK_USBC_REGS        5\r
257 //! PEVC Controller\r
258 #define SYSCLK_PEVC             6\r
259 //@}\r
260 \r
261 //! \name Clocks derived from the PBC clock\r
262 //@{\r
263 //! PM configuration\r
264 #define SYSCLK_PM               0\r
265 //! CHIPID Controller\r
266 #define SYSCLK_CHIPID           1\r
267 //! System Control Interface\r
268 #define SYSCLK_SCIF             2\r
269 //! Frequency Meter\r
270 #define SYSCLK_FREQM            3\r
271 //! General-Purpose I/O\r
272 #define SYSCLK_GPIO             4\r
273 //@}\r
274 \r
275 //! \name Clocks derived from the PBD clock\r
276 //@{\r
277 //! BPM configuration\r
278 #define SYSCLK_BPM              0\r
279 //! BSCIF configuration\r
280 #define SYSCLK_BSCIF            1\r
281 //! Asynchronous Timer\r
282 #define SYSCLK_AST              2\r
283 //! Watchdog Timer\r
284 #define SYSCLK_WDT              3\r
285 //! External Interrupt Controller\r
286 #define SYSCLK_EIC              4\r
287 //! PICOUART\r
288 #define SYSCLK_PICOUART         5\r
289 //@}\r
290 \r
291 //! \name Divided clock mask derived from the PBA clock\r
292 //@{\r
293 //! TIMER_CLOCK2 mask\r
294 #define PBA_DIVMASK_TIMER_CLOCK2     (1u << 0)\r
295 //! TIMER_CLOCK3 mask\r
296 #define PBA_DIVMASK_TIMER_CLOCK3     (1u << 2)\r
297 //! CLK_USART/DIV mask\r
298 #define PBA_DIVMASK_CLK_USART        (1u << 2)\r
299 //! TIMER_CLOCK4 mask\r
300 #define PBA_DIVMASK_TIMER_CLOCK4     (1u << 4)\r
301 //! TIMER_CLOCK5 mask\r
302 #define PBA_DIVMASK_TIMER_CLOCK5     (1u << 6)\r
303 //! Bitfield mask\r
304 #define PBA_DIVMASK_Msk              (0x7Fu << 0)\r
305 //@}\r
306 \r
307 #ifndef __ASSEMBLY__\r
308 \r
309 #include <compiler.h>\r
310 #include <dfll.h>\r
311 #include <osc.h>\r
312 #include <pll.h>\r
313 #include <genclk.h>\r
314 \r
315 // Use the slow clock (RCOSC) with no prescaling if config was empty.\r
316 #ifndef CONFIG_SYSCLK_SOURCE\r
317 # define CONFIG_SYSCLK_SOURCE    SYSCLK_SRC_RCSYS\r
318 #endif /* CONFIG_SYSCLK_SOURCE */\r
319 \r
320 /*\r
321  * Enable PicoCache for flash access by default.\r
322  * 0: disable PicoCache, 1: enable PicoCache.\r
323  */\r
324 #ifndef CONFIG_HCACHE_ENABLE\r
325 #define CONFIG_HCACHE_ENABLE          1\r
326 #endif\r
327 \r
328 /**\r
329  * \def CONFIG_SYSCLK_CPU_DIV\r
330  * \brief Configuration symbol for dividing the CPU clock frequency by\r
331  * \f$2^{CONFIG\_SYSCLK\_CPU\_DIV}\f$\r
332  *\r
333  * If this symbol is not defined, the CPU clock frequency is not divided.\r
334  *\r
335  * This symbol may be defined in \ref conf_clock.h.\r
336  */\r
337 #ifndef CONFIG_SYSCLK_CPU_DIV\r
338 # define CONFIG_SYSCLK_CPU_DIV   0\r
339 #endif /* CONFIG_SYSCLK_CPU_DIV */\r
340 \r
341 /**\r
342  * \def CONFIG_SYSCLK_INIT_HSBMASK\r
343  * \brief Configuration symbol for the HSB clocks enabled at power-on after the\r
344  * sysclock module has been initialized. By default, all HSB clocks are left\r
345  * enabled, however to save power these can be automatically disabled by defining\r
346  * this value to a mask of \c SYSCLOCK_xxx settings.\r
347  *\r
348  * If this symbol is not defined, then all HSB clocks are left enabled.\r
349  *\r
350  * This symbol may be defined in \ref conf_clock.h.\r
351  */\r
352 #ifdef __DOXYGEN__\r
353 # define CONFIG_SYSCLK_INIT_HSBMASK\r
354 #endif\r
355 \r
356 /**\r
357  * \def CONFIG_SYSCLK_PBA_DIV\r
358  * \brief Configuration symbol for dividing the PBA clock frequency by\r
359  * \f$2^{CONFIG\_SYSCLK\_PBA\_DIV}\f$\r
360  *\r
361  * If this symbol is not defined, the PBA clock frequency is not divided.\r
362  *\r
363  * This symbol may be defined in \ref conf_clock.h.\r
364  */\r
365 #ifndef CONFIG_SYSCLK_PBA_DIV\r
366 # define CONFIG_SYSCLK_PBA_DIV   0\r
367 #endif /* CONFIG_SYSCLK_PBA_DIV */\r
368 \r
369 /**\r
370  * \def CONFIG_SYSCLK_PBB_DIV\r
371  * \brief Configuration symbol for dividing the PBB clock frequency by\r
372  * \f$2^{CONFIG\_SYSCLK\_PBB\_DIV}\f$\r
373  *\r
374  * If this symbol is not defined, the PBB clock frequency is not divided.\r
375  *\r
376  * This symbol may be defined in \ref conf_clock.h.\r
377  */\r
378 #ifndef CONFIG_SYSCLK_PBB_DIV\r
379 # define CONFIG_SYSCLK_PBB_DIV   0\r
380 #endif /* CONFIG_SYSCLK_PBB_DIV */\r
381 \r
382 /**\r
383  * \def CONFIG_SYSCLK_PBC_DIV\r
384  * \brief Configuration symbol for dividing the PBC clock frequency by\r
385  * \f$2^{CONFIG\_SYSCLK\_PBC\_DIV}\f$\r
386  *\r
387  * If this symbol is not defined, the PBC clock frequency is not divided.\r
388  *\r
389  * This symbol may be defined in \ref conf_clock.h.\r
390  */\r
391 #ifndef CONFIG_SYSCLK_PBC_DIV\r
392 # define CONFIG_SYSCLK_PBC_DIV   0\r
393 #endif /* CONFIG_SYSCLK_PBC_DIV */\r
394 \r
395 /**\r
396  * \def CONFIG_SYSCLK_PBD_DIV\r
397  * \brief Configuration symbol for dividing the PBD clock frequency by\r
398  * \f$2^{CONFIG\_SYSCLK\_PBD\_DIV}\f$\r
399  *\r
400  * If this symbol is not defined, the PBD clock frequency is not divided.\r
401  *\r
402  * This symbol may be defined in \ref conf_clock.h.\r
403  */\r
404 #ifndef CONFIG_SYSCLK_PBD_DIV\r
405 # define CONFIG_SYSCLK_PBD_DIV   0\r
406 #endif /* CONFIG_SYSCLK_PBD_DIV */\r
407 \r
408 /**\r
409  * \def CONFIG_SYSCLK_INIT_CPUMASK\r
410  * \brief Configuration symbol for the CPU clocks enabled at power-on after the\r
411  * sysclock module has been initialized. By default, all CPU clocks are left\r
412  * enabled, however to save power these can be automatically disabled by defining\r
413  * this value to a mask of \c SYSCLOCK_xxx settings.\r
414  *\r
415  * If this symbol is not defined, then all CPU clocks are left enabled.\r
416  *\r
417  * This symbol may be defined in \ref conf_clock.h.\r
418  */\r
419 #ifdef __DOXYGEN__\r
420 # define CONFIG_SYSCLK_INIT_CPUMASK\r
421 #endif\r
422 \r
423 /**\r
424  * \def CONFIG_SYSCLK_INIT_PBAMASK\r
425  * \brief Configuration symbol for the PBA clocks enabled at power-on after the\r
426  * sysclock module has been initialized. By default, all PBA clocks are left\r
427  * enabled, however to save power these can be automatically disabled by defining\r
428  * this value to a mask of \c SYSCLOCK_xxx settings.\r
429  *\r
430  * If this symbol is not defined, then all PBA clocks are left enabled.\r
431  *\r
432  * This symbol may be defined in \ref conf_clock.h.\r
433  */\r
434 #ifdef __DOXYGEN__\r
435 # define CONFIG_SYSCLK_INIT_PBAMASK\r
436 #endif\r
437 \r
438 /**\r
439  * \def CONFIG_SYSCLK_INIT_PBBMASK\r
440  * \brief Configuration symbol for the PBB clocks enabled at power-on after the\r
441  * sysclock module has been initialized. By default, all PBB clocks are left\r
442  * enabled, however to save power these can be automatically disabled by defining\r
443  * this value to a mask of \c SYSCLOCK_xxx settings.\r
444  *\r
445  * If this symbol is not defined, then all PBB clocks are left enabled.\r
446  *\r
447  * This symbol may be defined in \ref conf_clock.h.\r
448  */\r
449 #ifdef __DOXYGEN__\r
450 # define CONFIG_SYSCLK_INIT_PBBMASK\r
451 #endif\r
452 \r
453 /**\r
454  * \def CONFIG_SYSCLK_INIT_PBCMASK\r
455  * \brief Configuration symbol for the PBC clocks enabled at power-on after the\r
456  * sysclock module has been initialized. By default, all PBC clocks are left\r
457  * enabled, however to save power these can be automatically disabled by defining\r
458  * this value to a mask of \c SYSCLOCK_xxx settings.\r
459  *\r
460  * If this symbol is not defined, then all PBC clocks are left enabled.\r
461  *\r
462  * This symbol may be defined in \ref conf_clock.h.\r
463  */\r
464 #ifdef __DOXYGEN__\r
465 # define CONFIG_SYSCLK_INIT_PBCMASK\r
466 #endif\r
467 \r
468 /**\r
469  * \def CONFIG_SYSCLK_INIT_PBDMASK\r
470  * \brief Configuration symbol for the PBD clocks enabled at power-on after the\r
471  * sysclock module has been initialized. By default, all PBD clocks are left\r
472  * enabled, however to save power these can be automatically disabled by defining\r
473  * this value to a mask of \c SYSCLOCK_xxx settings.\r
474  *\r
475  * If this symbol is not defined, then all PBD clocks are left enabled.\r
476  *\r
477  * This symbol may be defined in \ref conf_clock.h.\r
478  */\r
479 #ifdef __DOXYGEN__\r
480 # define CONFIG_SYSCLK_INIT_PBDMASK\r
481 #endif\r
482 \r
483 /**\r
484  * \def CONFIG_USBCLK_SOURCE\r
485  * \brief Configuration symbol for the USB generic clock source\r
486  *\r
487  * Sets the clock source to use for the USB. The source must also be properly\r
488  * configured.\r
489  *\r
490  * Define this to one of the \c USBCLK_SRC_xxx settings. Leave it undefined if\r
491  * USB is not required.\r
492  */\r
493 #ifdef __DOXYGEN__\r
494 # define CONFIG_USBCLK_SOURCE\r
495 #endif\r
496 \r
497 /**\r
498  * \def CONFIG_USBCLK_DIV\r
499  * \brief Configuration symbol for the USB generic clock divider setting\r
500  *\r
501  * Sets the clock division for the USB generic clock. If a USB clock source is\r
502  * selected with CONFIG_USBCLK_SOURCE, this configuration symbol must also be\r
503  * defined.\r
504  *\r
505  * Define this as any value that does not exceed \c GENCLK_DIV_MAX, and which\r
506  * will give a 48 MHz clock frequency from the selected source.\r
507  */\r
508 #ifdef __DOXYGEN__\r
509 # define CONFIG_USBCLK_DIV\r
510 #endif\r
511 \r
512 /**\r
513  * \name Querying the system clock and its derived clocks\r
514  *\r
515  * The following functions may be used to query the current frequency of\r
516  * the system clock and the CPU and bus clocks derived from it.\r
517  * sysclk_get_main_hz() and sysclk_get_cpu_hz() can be assumed to be\r
518  * available on all platforms, although some platforms may define\r
519  * additional accessors for various chip-internal bus clocks. These are\r
520  * usually not intended to be queried directly by generic code.\r
521  */\r
522 //@{\r
523 \r
524 /**\r
525  * \brief Return the current rate in Hz of the main system clock\r
526  *\r
527  * \todo This function assumes that the main clock source never changes\r
528  * once it's been set up, and that PLL0 always runs at the compile-time\r
529  * configured default rate. While this is probably the most common\r
530  * configuration, which we want to support as a special case for\r
531  * performance reasons, we will at some point need to support more\r
532  * dynamic setups as well.\r
533  */\r
534 #if (defined CONFIG_SYSCLK_DEFAULT_RETURNS_SLOW_OSC)\r
535 extern bool sysclk_initialized;\r
536 #endif\r
537 static inline uint32_t sysclk_get_main_hz(void)\r
538 {\r
539 #if (defined CONFIG_SYSCLK_DEFAULT_RETURNS_SLOW_OSC)\r
540         if (!sysclk_initialized ) {\r
541                 return OSC_RCSYS_NOMINAL_HZ;\r
542         }\r
543 #endif\r
544 \r
545         if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_RCSYS) {\r
546                 return OSC_RCSYS_NOMINAL_HZ;\r
547         }\r
548 \r
549 #ifdef BOARD_OSC0_HZ\r
550         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_OSC0) {\r
551                 return BOARD_OSC0_HZ;\r
552         }\r
553 #endif\r
554 \r
555 #ifdef CONFIG_PLL0_SOURCE\r
556         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_PLL0) {\r
557                 return pll_get_default_rate(0);\r
558         }\r
559 #endif\r
560 \r
561 #ifdef CONFIG_DFLL0_SOURCE\r
562         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_DFLL) {\r
563                 return dfll_get_default_rate(0);\r
564         }\r
565 #endif\r
566 \r
567         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_RC80M) {\r
568                 return OSC_RC80M_NOMINAL_HZ;\r
569         }\r
570 \r
571         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_RCFAST) {\r
572                 if (CONFIG_RCFAST_FRANGE == 2) {\r
573                         return OSC_RCFAST12M_NOMINAL_HZ;\r
574                 } else if (CONFIG_RCFAST_FRANGE == 1) {\r
575                         return OSC_RCFAST8M_NOMINAL_HZ;\r
576                 } else {\r
577                         return OSC_RCFAST4M_NOMINAL_HZ;\r
578                 }\r
579         }\r
580 \r
581         else if (CONFIG_SYSCLK_SOURCE == SYSCLK_SRC_RC1M) {\r
582                 return OSC_RC1M_NOMINAL_HZ;\r
583         }\r
584 \r
585 \r
586         else {\r
587                 /* unhandled_case(CONFIG_SYSCLK_SOURCE); */\r
588                 return 0;\r
589         }\r
590 }\r
591 \r
592 /**\r
593  * \brief Return the current rate in Hz of the CPU clock\r
594  *\r
595  * \todo This function assumes that the CPU always runs at the system\r
596  * clock frequency. We want to support at least two more scenarios:\r
597  * Fixed CPU/bus clock dividers (config symbols) and dynamic CPU/bus\r
598  * clock dividers (which may change at run time). Ditto for all the bus\r
599  * clocks.\r
600  *\r
601  * \return Frequency of the CPU clock, in Hz.\r
602  */\r
603 static inline uint32_t sysclk_get_cpu_hz(void)\r
604 {\r
605         return sysclk_get_main_hz() >> CONFIG_SYSCLK_CPU_DIV;\r
606 }\r
607 \r
608 /**\r
609  * \brief Return the current rate in Hz of the High-Speed Bus clock\r
610  *\r
611  * \return Frequency of the High Speed Peripheral Bus clock, in Hz.\r
612  */\r
613 static inline uint32_t sysclk_get_hsb_hz(void)\r
614 {\r
615         return sysclk_get_main_hz() >> CONFIG_SYSCLK_CPU_DIV;\r
616 }\r
617 \r
618 /**\r
619  * \brief Return the current rate in Hz of the Peripheral Bus A clock\r
620  *\r
621  * \return Frequency of the Peripheral Bus A clock, in Hz.\r
622  */\r
623 static inline uint32_t sysclk_get_pba_hz(void)\r
624 {\r
625         return sysclk_get_main_hz() >> CONFIG_SYSCLK_PBA_DIV;\r
626 }\r
627 \r
628 /**\r
629  * \brief Return the current rate in Hz of the Peripheral Bus B clock\r
630  *\r
631  * \return Frequency of the Peripheral Bus B clock, in Hz.\r
632  */\r
633 static inline uint32_t sysclk_get_pbb_hz(void)\r
634 {\r
635         return sysclk_get_main_hz() >> CONFIG_SYSCLK_PBB_DIV;\r
636 }\r
637 \r
638 /**\r
639  * \brief Return the current rate in Hz of the Peripheral Bus C clock\r
640  *\r
641  * \return Frequency of the Peripheral Bus C clock, in Hz.\r
642  */\r
643 static inline uint32_t sysclk_get_pbc_hz(void)\r
644 {\r
645         return sysclk_get_main_hz() >> CONFIG_SYSCLK_PBC_DIV;\r
646 }\r
647 \r
648 /**\r
649  * \brief Return the current rate in Hz of the Peripheral Bus D clock\r
650  *\r
651  * \return Frequency of the Peripheral Bus D clock, in Hz.\r
652  */\r
653 static inline uint32_t sysclk_get_pbd_hz(void)\r
654 {\r
655         return sysclk_get_main_hz() >> CONFIG_SYSCLK_PBD_DIV;\r
656 }\r
657 \r
658 extern uint32_t sysclk_get_peripheral_bus_hz(const volatile void *module);\r
659 //@}\r
660 \r
661 extern void sysclk_priv_enable_module(uint32_t bus_id, uint32_t module_index);\r
662 extern void sysclk_priv_disable_module(uint32_t bus_id, uint32_t module_index);\r
663 \r
664 //! \name Enabling and disabling synchronous clocks\r
665 //@{\r
666 \r
667 /**\r
668  * \brief Enable a module clock derived from the CPU clock\r
669  * \param index Index of the module clock in the CPUMASK register\r
670  */\r
671 static inline void sysclk_enable_cpu_module(uint32_t index)\r
672 {\r
673         sysclk_priv_enable_module(PM_CLK_GRP_CPU, index);\r
674 }\r
675 \r
676 /**\r
677  * \brief Disable a module clock derived from the CPU clock\r
678  * \param index Index of the module clock in the CPUMASK register\r
679  */\r
680 static inline void sysclk_disable_cpu_module(uint32_t index)\r
681 {\r
682         sysclk_priv_disable_module(PM_CLK_GRP_CPU, index);\r
683 }\r
684 \r
685 /**\r
686  * \brief Enable a module clock derived from the HSB clock\r
687  * \param index Index of the module clock in the HSBMASK register\r
688  */\r
689 static inline void sysclk_enable_hsb_module(uint32_t index)\r
690 {\r
691         sysclk_priv_enable_module(PM_CLK_GRP_HSB, index);\r
692 }\r
693 \r
694 /**\r
695  * \brief Disable a module clock derived from the HSB clock\r
696  * \param index Index of the module clock in the HSBMASK register\r
697  */\r
698 static inline void sysclk_disable_hsb_module(uint32_t index)\r
699 {\r
700         sysclk_priv_disable_module(PM_CLK_GRP_HSB, index);\r
701 }\r
702 \r
703 extern void sysclk_enable_pba_module(uint32_t index);\r
704 extern void sysclk_disable_pba_module(uint32_t index);\r
705 extern void sysclk_enable_pbb_module(uint32_t index);\r
706 extern void sysclk_disable_pbb_module(uint32_t index);\r
707 \r
708 /**\r
709  * \brief Enable a module clock derived from the PBC clock\r
710  * \param index Index of the module clock in the PBAMASK register\r
711  */\r
712 static inline void sysclk_enable_pbc_module(uint32_t index)\r
713 {\r
714         sysclk_priv_enable_module(PM_CLK_GRP_PBC, index);\r
715 }\r
716 \r
717 /**\r
718  * \brief Disable a module clock derived from the PBC clock\r
719  * \param index Index of the module clock in the PBAMASK register\r
720  */\r
721 static inline void sysclk_disable_pbc_module(uint32_t index)\r
722 {\r
723         sysclk_priv_disable_module(PM_CLK_GRP_PBC, index);\r
724 }\r
725 \r
726 /**\r
727  * \brief Enable a module clock derived from the PBD clock\r
728  * \param index Index of the module clock in the PBAMASK register\r
729  */\r
730 static inline void sysclk_enable_pbd_module(uint32_t index)\r
731 {\r
732         sysclk_priv_enable_module(PM_CLK_GRP_PBD, index);\r
733 }\r
734 \r
735 /**\r
736  * \brief Disable a module clock derived from the PBD clock\r
737  * \param index Index of the module clock in the PBAMASK register\r
738  */\r
739 static inline void sysclk_disable_pbd_module(uint32_t index)\r
740 {\r
741         sysclk_priv_disable_module(PM_CLK_GRP_PBD, index);\r
742 }\r
743 \r
744 /**\r
745  * \brief Enable divided clock mask derived from the PBA clock\r
746  * \param mask mask of the divided clock in the PBADIVMASK register\r
747  */\r
748 static inline void sysclk_enable_pba_divmask(uint32_t mask)\r
749 {\r
750         uint32_t temp_mask;\r
751 \r
752         temp_mask = PM->PM_PBADIVMASK;\r
753         temp_mask |= mask;\r
754         PM->PM_UNLOCK = PM_UNLOCK_KEY(0xAAu)\r
755                         | PM_UNLOCK_ADDR((uint32_t)&PM->PM_PBADIVMASK - (uint32_t)PM);\r
756         PM->PM_PBADIVMASK = temp_mask;\r
757 }\r
758 \r
759 /**\r
760  * \brief Disable divided clock mask derived from the PBA clock\r
761  * \param mask mask of the divided clock in the PBADIVMASK register\r
762  */\r
763 static inline void sysclk_disable_pba_divmask(uint32_t mask)\r
764 {\r
765         uint32_t temp_mask;\r
766 \r
767         temp_mask = PM->PM_PBADIVMASK;\r
768         temp_mask &= ~mask;\r
769         PM->PM_UNLOCK = PM_UNLOCK_KEY(0xAAu)\r
770                         | PM_UNLOCK_ADDR((uint32_t)&PM->PM_PBADIVMASK - (uint32_t)PM);\r
771         PM->PM_PBADIVMASK = temp_mask;\r
772 }\r
773 \r
774 extern void sysclk_enable_peripheral_clock(const volatile void *module);\r
775 extern void sysclk_disable_peripheral_clock(const volatile void *module);\r
776 \r
777 //@}\r
778 \r
779 //! \name System Clock Source and Prescaler configuration\r
780 //@{\r
781 \r
782 extern void sysclk_set_prescalers(uint32_t cpu_shift,\r
783                 uint32_t pba_shift, uint32_t pbb_shift,\r
784                 uint32_t pbc_shift, uint32_t pbd_shift);\r
785 extern void sysclk_set_source(uint32_t src);\r
786 \r
787 //@}\r
788 \r
789 #if defined(CONFIG_USBCLK_SOURCE) || defined(__DOXYGEN__)\r
790 \r
791 /**\r
792  * \def USBCLK_STARTUP_TIMEOUT\r
793  * \brief Number of us to wait for USB clock to start\r
794  */\r
795 #ifdef CONFIG_USBCLK_STARTUP_TIMEOUT\r
796 #   define USBCLK_STARTUP_TIMEOUT    (CONFIG_USBCLK_STARTUP_TIMEOUT)\r
797 #else\r
798 #   define USBCLK_STARTUP_TIMEOUT    (OSC0_STARTUP_TIMEOUT*(1000000/OSC_RCSYS_NOMINAL_HZ))\r
799 #endif\r
800 \r
801 extern void sysclk_enable_usb(void);\r
802 extern void sysclk_disable_usb(void);\r
803 #endif\r
804 \r
805 extern void sysclk_init(void);\r
806 \r
807 #endif /* !__ASSEMBLY__ */\r
808 \r
809 //! @}\r
810 \r
811 #ifdef __cplusplus\r
812 }\r
813 #endif\r
814 \r
815 #endif /* CHIP_SYSCLK_H_INCLUDED */\r