Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cpu / cortex-m3 / drv / clock_sam3.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 Atmel SAM3 clock setup.
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  */
37
38 #include "clock_sam3.h"
39 #include <cfg/compiler.h>
40 #include <cfg/macros.h>
41 #include <io/sam3.h>
42
43
44 /* Frequency of board main oscillator */
45 #define BOARDOSC_FREQ  12000000
46
47 /* Timer countdown timeout for clock initialization operations */
48 #define CLOCK_TIMEOUT    0xFFFFFFFF
49
50
51 #if CPU_FREQ == 84000000 || CPU_FREQ == 48000000
52
53 INLINE uint32_t evaluate_pll(void)
54 {
55         return CKGR_PLLR_MUL(CPU_FREQ / BOARDOSC_FREQ * 2 - 1) | CKGR_PLLR_DIV(2);
56 }
57
58 #else
59
60 #warning CPU clock frequency non-standard setting: multiplier and divider values \
61  will be computed at runtime: effective computed frequency could be different \
62  from expected.
63
64 /*
65  * Try to evaluate the correct divider and multiplier value depending
66  * on the desired CPU frequency.
67  *
68  * We try all combinations in a certain range of divider and multiplier
69  * values.  Start with higher multipliers and divisors, generally better.
70  */
71 INLINE uint32_t evaluate_pll(void)
72 {
73         int mul, div, best_mul, best_div;
74         int best_delta = CPU_FREQ;
75         int freq = 0;
76
77         for (mul = 13; mul > 0; mul--)
78         {
79                 for (div = 24; div > 0; div--)
80                 {
81                         freq = BOARDOSC_FREQ / div * (1 + mul);
82                         if (ABS((int)CPU_FREQ - freq) < best_delta) {
83                                 best_delta = ABS((int)CPU_FREQ - freq);
84                                 best_mul = mul;
85                                 best_div = div;
86                         }
87                 }
88         }
89
90         return CKGR_PLLR_DIV(best_div) | CKGR_PLLR_MUL(best_mul);
91 }
92
93 #endif /* CPU_FREQ */
94
95 void clock_init(void)
96 {
97         uint32_t timeout;
98
99         /* Disable watchdog */
100         WDT_MR = BV(WDT_WDDIS);
101
102         /* Set wait states for flash access, needed for higher CPU clock rates */
103         EEFC0_FMR = EEFC_FMR_FWS(3);
104 #ifdef EEFC1_FMR
105         EEFC1_FMR = EEFC_FMR_FWS(3);
106 #endif
107
108         // Initialize main oscillator
109         if (!(CKGR_MOR & BV(CKGR_MOR_MOSCSEL)))
110         {
111                 CKGR_MOR = CKGR_MOR_KEY(0x37) | CKGR_MOR_MOSCXTST(0x8)
112                         | BV(CKGR_MOR_MOSCRCEN) | BV(CKGR_MOR_MOSCXTEN);
113                 timeout = CLOCK_TIMEOUT;
114                 while (!(PMC_SR & BV(PMC_SR_MOSCXTS)) && --timeout);
115         }
116
117         // Switch to external oscillator
118         CKGR_MOR = CKGR_MOR_KEY(0x37) | CKGR_MOR_MOSCXTST(0x8)
119                 | BV(CKGR_MOR_MOSCRCEN) | BV(CKGR_MOR_MOSCXTEN) | BV(CKGR_MOR_MOSCSEL);
120         timeout = CLOCK_TIMEOUT;
121         while (!(PMC_SR & BV(PMC_SR_MOSCXTS)) && --timeout);
122
123         // Initialize and enable PLL clock
124         CKGR_PLLR = evaluate_pll() | BV(CKGR_PLLR_STUCKTO1) | CKGR_PLLR_PLLCOUNT(0x2);
125         timeout = CLOCK_TIMEOUT;
126         while (!(PMC_SR & BV(PMC_SR_LOCK)) && --timeout);
127
128         PMC_MCKR = PMC_MCKR_CSS_MAIN_CLK;
129         timeout = CLOCK_TIMEOUT;
130         while (!(PMC_SR & BV(PMC_SR_MCKRDY)) && --timeout);
131
132         PMC_MCKR = PMC_MCKR_CSS_PLL_CLK;
133         timeout = CLOCK_TIMEOUT;
134         while (!(PMC_SR & BV(PMC_SR_MCKRDY)) && --timeout);
135
136         /* Enable clock on PIO for inputs */
137         // TODO: move this in gpio_init() for better power management?
138         pmc_periphEnable(PIOA_ID);
139         pmc_periphEnable(PIOB_ID);
140         pmc_periphEnable(PIOC_ID);
141 #ifdef PIOF_ID
142         pmc_periphEnable(PIOD_ID);
143         pmc_periphEnable(PIOE_ID);
144         pmc_periphEnable(PIOF_ID);
145 #endif
146 }