STM32: add clocking driver.
[bertos.git] / bertos / cpu / cortex-m3 / drv / clock_stm32.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32 Clocking driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/compiler.h>
39 #include <cfg/debug.h>
40 #include <io/stm32.h>
41 #include "clock_stm32.h"
42
43 struct RCC *RCC;
44
45 INLINE int rcc_get_flag_status(uint32_t flag)
46 {
47         uint32_t id;
48         reg32_t reg;
49
50         /* Get the RCC register index */
51         id = flag >> 5;
52         /* The flag to check is in CR register */
53         if (id == 1)
54                 reg = RCC->CR;
55         /* The flag to check is in BDCR register */
56         else if (id == 2)
57                 reg = RCC->BDCR;
58         /* The flag to check is in CSR register */
59         else
60                 reg = RCC->CSR;
61         /* Get the flag position */
62         id = flag & FLAG_MASK;
63
64         return reg & (1 << id);
65 }
66
67 INLINE uint16_t pll_clock(void)
68 {
69         int div, mul;
70
71         /* Hopefully this is evaluate at compile time... */
72         for (div = 2; div; div--)
73                 for (mul = 2; mul <= 16; mul++)
74                         if (CPU_FREQ >= (PLL_VCO / div * mul))
75                                 break;
76         return mul << 8 | div;
77 }
78
79 INLINE void rcc_pll_config(void)
80 {
81         reg32_t reg = RCC->CFGR & CFGR_PLL_MASK;
82
83         /* Evaluate clock parameters */
84         uint16_t clock = pll_clock();
85         uint32_t pll_mul = ((clock >> 8) - 2) << 18;
86         uint32_t pll_div = ((clock & 0xff) << 1 | 1) << 16;
87
88         /* Set the PLL configuration bits */
89         reg |= pll_div | pll_mul;
90
91         /* Store the new value */
92         RCC->CFGR = reg;
93
94         /* Enable PLL */
95         *CR_PLLON_BB = 1;
96 }
97
98 INLINE void rcc_set_clock_source(uint32_t source)
99 {
100         reg32_t reg;
101
102         reg = RCC->CFGR & CFGR_SW_MASK;
103         reg |= source;
104         RCC->CFGR = reg;
105 }
106
107 void clock_init(void)
108 {
109         /* Initialize global RCC structure */
110         RCC = (struct RCC *)RCC_BASE;
111
112         /* Enable the internal oscillator */
113         *CR_HSION_BB = 1;
114         while (!rcc_get_flag_status(RCC_FLAG_HSIRDY));
115
116         /* Clock the system from internal HSI RC (8 MHz) */
117         rcc_set_clock_source(RCC_SYSCLK_HSI);
118
119         /* Enable external oscillator */
120         RCC->CR &= CR_HSEON_RESET;
121         RCC->CR &= CR_HSEBYP_RESET;
122         RCC->CR |= CR_HSEON_SET;
123         while (!rcc_get_flag_status(RCC_FLAG_HSERDY));
124
125         /* Initialize PLL according to CPU_FREQ */
126         rcc_pll_config();
127         while(!rcc_get_flag_status(RCC_FLAG_PLLRDY));
128
129         /* Set 1 wait state for the flash memory */
130         *(reg32_t *)0x40022000 = 0x12;
131
132         /* Clock the system from the PLL */
133         rcc_set_clock_source(RCC_SYSCLK_PLLCLK);
134 }