sam3: fix clock initialization to work with sam3x too
[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 // TODO: wizard config
46 #define BOARDOSC_FREQ  12000000
47
48 /* Main crystal oscillator startup time, optimal value for CPU_FREQ == 48 MHz */
49 #define BOARD_OSC_COUNT  (CKGR_MOR_MOSCXTST(0x8))
50
51 /* Timer countdown timeout for clock initialization operations */
52 #define CLOCK_TIMEOUT    0xFFFFFFFF
53
54
55 /*
56  * Try to evaluate the correct divider and multiplier value depending
57  * on the desired CPU frequency.
58  *
59  * We try all combinations in a certain range of divider and multiplier
60  * values.  The range can change, with better match with "strange"
61  * frequencies, but boot time will be longer.
62  *
63  * Limits for SAM3N: divider [1,255], multiplier [1,2047].
64  */
65 INLINE uint32_t evaluate_pll(void)
66 {
67         int mul, div, best_mul, best_div;
68         int best_delta = CPU_FREQ;
69         int freq = 0;
70
71         for (mul = 1; mul <= 8; mul++)
72         {
73                 for (div = 1; div <= 24; div++)
74                 {
75                         freq = BOARDOSC_FREQ / div * (1 + mul);
76                         if (ABS((int)CPU_FREQ - freq) < best_delta) {
77                                 best_delta = ABS((int)CPU_FREQ - freq);
78                                 best_mul = mul;
79                                 best_div = div;
80                         }
81                 }
82         }
83
84         return CKGR_PLLR_DIV(best_div) | CKGR_PLLR_MUL(best_mul);
85 }
86
87
88 void clock_init(void)
89 {
90         uint32_t timeout;
91
92         /* Disable watchdog */
93         WDT_MR = BV(WDT_WDDIS);
94
95 #if CPU_CM3_SAM3X
96         /* Set wait states for flash access, needed for higher CPU clock rates */
97         EEFC0_FMR = EEFC_FMR_FWS(2);
98         EEFC1_FMR = EEFC_FMR_FWS(2);
99 #else
100         EEFC0_FMR = EEFC_FMR_FWS(3);
101
102         // TODO: check if this is needed in sam3n-ek too, very slow start-up
103         // Select external slow clock
104         if (!(SUPC_SR & BV(SUPC_SR_OSCSEL)))
105         {
106                 SUPC_CR = BV(SUPC_CR_XTALSEL) | SUPC_CR_KEY(0xA5);
107                 while (!(SUPC_SR & BV(SUPC_SR_OSCSEL)));
108         }
109 #endif
110
111         // Initialize main oscillator
112         if (!(CKGR_MOR & BV(CKGR_MOR_MOSCSEL)))
113         {
114                 CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSC_COUNT | BV(CKGR_MOR_MOSCRCEN) | BV(CKGR_MOR_MOSCXTEN);
115                 timeout = CLOCK_TIMEOUT;
116                 while (!(PMC_SR & BV(PMC_SR_MOSCXTS)) && --timeout);
117         }
118
119         // Switch to external oscillator
120         CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSC_COUNT | BV(CKGR_MOR_MOSCRCEN) | BV(CKGR_MOR_MOSCXTEN) | BV(CKGR_MOR_MOSCSEL);
121         timeout = CLOCK_TIMEOUT;
122         while (!(PMC_SR & BV(PMC_SR_MOSCSELS)) && --timeout);
123
124         PMC_MCKR = (PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_MASK) | PMC_MCKR_CSS_MAIN_CLK;
125         timeout = CLOCK_TIMEOUT;
126         while (!(PMC_SR & BV(PMC_SR_MCKRDY)) && --timeout);
127
128         // Initialize and enable PLL clock
129         CKGR_PLLR = evaluate_pll() | BV(CKGR_PLLR_STUCKTO1) | CKGR_PLLR_PLLCOUNT(0x1);
130         timeout = CLOCK_TIMEOUT;
131         while (!(PMC_SR & BV(PMC_SR_LOCK)) && --timeout);
132
133         PMC_MCKR = PMC_MCKR_CSS_MAIN_CLK;
134         timeout = CLOCK_TIMEOUT;
135         while (!(PMC_SR & BV(PMC_SR_MCKRDY)) && --timeout);
136
137         PMC_MCKR = PMC_MCKR_CSS_PLL_CLK;
138         timeout = CLOCK_TIMEOUT;
139         while (!(PMC_SR & BV(PMC_SR_MCKRDY)) && --timeout);
140
141         /* Enable clock on PIO for inputs */
142         // TODO: move this in gpio_init() for better power management?
143 #if CPU_CM3_SAM3X
144         PMC_PCER = BV(PIOA_ID) | BV(PIOB_ID) | BV(PIOC_ID)
145                 | BV(PIOD_ID) | BV(PIOE_ID) | BV(PIOF_ID);
146 #else
147         PMC_PCER = BV(PIOA_ID) | BV(PIOB_ID) | BV(PIOC_ID);
148 #endif
149 }