Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cpu / cortex-m3 / drv / clock_lm3s.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 LM3S1968 Clocking driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "clock_lm3s.h"
39
40 #include <cfg/compiler.h>
41 #include <cfg/debug.h>
42
43 #include <io/lm3s.h>
44
45
46 /* The PLL VCO frequency is 400 MHz */
47 #define PLL_VCO 400000000UL
48
49 /* Extract the system clock divisor from the RCC register */
50 #define RCC_TO_DIV(rcc)                                         \
51                 (((rcc & SYSCTL_RCC_SYSDIV_MASK) >>             \
52                                 SYSCTL_RCC_SYSDIV_SHIFT) + 1)
53
54 /*
55  * Very small delay: each loop takes 3 cycles.
56  */
57 void NAKED lm3s_busyWait(unsigned long iterations)
58 {
59         register uint32_t __n asm("r0") = iterations;
60
61         asm volatile (
62                 "1: subs r0, #1\n\t"
63                 "bne 1b\n\t"
64                 "bx lr\n\t"
65                 : : "r"(__n) : "memory", "cc");
66 }
67
68 INLINE unsigned long clock_get_rate(void)
69 {
70         reg32_t rcc = HWREG(SYSCTL_RCC);
71
72         return rcc & SYSCTL_RCC_USESYSDIV ?
73                         PLL_VCO / 2 / RCC_TO_DIV(rcc) : PLL_VCO;
74 }
75
76 /*
77  * Try to evaluate the correct SYSDIV value depending on the desired CPU
78  * frequency.
79  */
80 INLINE int evaluate_sysdiv(unsigned long freq)
81 {
82         int i;
83
84          /*
85           * NOTE: with BYPASS=0, SYSDIV < 3 are reserved values (see LM3S1968
86           * Microcontroller DATASHEET, p.78).
87           */
88         for (i = 3; i < 16; i++)
89                 if (freq >= (PLL_VCO / 2 / (i + 1)))
90                         break;
91         return i;
92 }
93
94 void clock_init(void)
95 {
96         reg32_t rcc, rcc2;
97         unsigned long clk;
98         int i;
99
100         /*
101          * PLL may not function properly at default LDO setting.
102          *
103          * Description:
104          *
105          * In designs that enable and use the PLL module, unstable device
106          * behavior may occur with the LDO set at its default of 2.5 volts or
107          * below (minimum of 2.25 volts). Designs that do not use the PLL
108          * module are not affected.
109          *
110          * Workaround: Prior to enabling the PLL module, it is recommended that
111          * the default LDO voltage setting of 2.5 V be adjusted to 2.75 V using
112          * the LDO Power Control (LDOPCTL) register.
113          *
114          * Silicon Revision Affected: A1, A2
115          *
116          * See also: Stellaris LM3S1968 A2 Errata documentation.
117          */
118         if (REVISION_IS_A1 | REVISION_IS_A2)
119                 HWREG(SYSCTL_LDOPCTL) = SYSCTL_LDOPCTL_2_75V;
120
121         rcc = HWREG(SYSCTL_RCC);
122         rcc2 = HWREG(SYSCTL_RCC2);
123
124         /*
125          * Step #1: bypass the PLL and system clock divider by setting the
126          * BYPASS bit and clearing the USESYS bit in the RCC register. This
127          * configures the system to run off a “raw” clock source (using the
128          * main oscillator or internal oscillator) and allows for the new PLL
129          * configuration to be validated before switching the system clock to
130          * the PLL.
131          */
132         rcc |= SYSCTL_RCC_BYPASS;
133         rcc &= ~SYSCTL_RCC_USESYSDIV;
134         rcc2 |= SYSCTL_RCC2_BYPASS2;
135
136         /* Write back RCC/RCC2 registers */
137         HWREG(SYSCTL_RCC) = rcc;
138         HWREG(SYSCTL_RCC) = rcc2;
139
140         lm3s_busyWait(16);
141
142         /*
143          * Step #2: select the crystal value (XTAL) and oscillator source
144          * (OSCSRC), and clear the PWRDN bit in RCC/RCC2. Setting the XTAL
145          * field automatically pulls valid PLL configuration data for the
146          * appropriate crystal, and clearing the PWRDN bit powers and enables
147          * the PLL and its output.
148          */
149
150         /* Enable the main oscillator first. */
151         rcc &= ~(SYSCTL_RCC_IOSCDIS | SYSCTL_RCC_MOSCDIS);
152         rcc |= SYSCTL_RCC_IOSCDIS;
153
154         /* Do not override RCC register fields */
155         rcc2 &= ~SYSCTL_RCC2_USERCC2;
156
157         rcc &= ~(SYSCTL_RCC_XTAL_M | SYSCTL_RCC_OSCSRC_M | SYSCTL_RCC_PWRDN);
158         rcc |= XTAL_FREQ | SYSCTL_RCC_OSCSRC_MAIN;
159
160         /* Clear the PLL lock interrupt. */
161         HWREG(SYSCTL_MISC) = SYSCTL_INT_PLL_LOCK;
162
163         HWREG(SYSCTL_RCC) = rcc;
164         HWREG(SYSCTL_RCC) = rcc2;
165
166         lm3s_busyWait(16);
167
168         /*
169          * Step #3: select the desired system divider (SYSDIV) in RCC/RCC2 and
170          * set the USESYS bit in RCC. The SYSDIV field determines the system
171          * frequency for the microcontroller.
172          */
173         rcc &= ~(SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV);
174
175         clk = PLL_VCO / 2;
176         for (i = 3; i < 16; i++)
177                 if (CPU_FREQ >= (clk / (i + 1)))
178                         break;
179         rcc |= SYSCTL_RCC_USESYSDIV |
180                         (evaluate_sysdiv(CPU_FREQ) << SYSCTL_RCC_SYSDIV_SHIFT);
181
182         /*
183          * Step #4: wait for the PLL to lock by polling the PLLLRIS bit in the
184          * Raw Interrupt Status (RIS) register.
185          */
186         for (i = 0; i < 32768; i++)
187                 if (HWREG(SYSCTL_RIS) & SYSCTL_INT_PLL_LOCK)
188                         break;
189
190         /*
191          * Step #5: enable use of the PLL by clearing the BYPASS bit in
192          * RCC/RCC2.
193          */
194         rcc &= ~SYSCTL_RCC_BYPASS;
195
196         HWREG(SYSCTL_RCC) = rcc;
197
198         lm3s_busyWait(16);
199 }