lm3s1968: try to evaluate the correct SYSDIV value depending on the desired CPU frequ...
[bertos.git] / bertos / cpu / cortex-m3 / drv / clock.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 <cfg/compiler.h>
39 #include <cfg/debug.h>
40 #include "io/lm3s.h"
41 #include "clock.h"
42
43 /* See: LM3S1968 Microcontroller DATASHEET, p.80 */
44 static const unsigned long xtal_clk[] =
45 {
46         1000000,
47         1843200,
48         2000000,
49         2457600,
50         3579545,
51         3686400,
52         4000000,
53         4096000,
54         4915200,
55         5000000,
56         5120000,
57         6000000,
58         6144000,
59         7372800,
60         8000000,
61         8192000,
62         10000000,
63         12000000,
64         12288000,
65         13560000,
66         14318180,
67         16000000,
68         16384000,
69 };
70
71 /* Extract the main oscillator frequency from the RCC register */
72 #define RCC_TO_CLK(rcc) \
73                 (xtal_clk[(((rcc) & SYSCTL_RCC_XTAL_MASK) >> \
74                                 SYSCTL_RCC_XTAL_SHIFT)])
75
76 /* Extract the main oscillator frequency from the RCC register */
77 #define RCC_TO_SYSDIV(rcc) \
78                 (((rcc & SYSCTL_RCC_SYSDIV_MASK) >> \
79                         SYSCTL_RCC_SYSDIV_SHIFT) + 1)
80
81 /*
82  * Very small delay: each loop takes 3 cycles.
83  */
84 INLINE void __delay(unsigned long iterations)
85 {
86         asm volatile (
87                 "1:     subs    %0, #1\n\t"
88                 "       bne 1b\n\t"
89                 : "=r"(iterations) : : "memory", "cc");
90 }
91
92 unsigned long clock_get_rate(void)
93 {
94         unsigned long rcc, clk;
95
96         rcc = HWREG(SYSCTL_RCC);
97
98         /* Get the main oscillator frequency */
99         clk = RCC_TO_CLK(rcc);
100         /* Apply system clock divider */
101         clk /= RCC_TO_SYSDIV(rcc);
102
103         return clk;
104 }
105
106 void clock_set_rate(void)
107 {
108         reg32_t rcc, rcc2;
109         unsigned long clk;
110         int i;
111
112         rcc = HWREG(SYSCTL_RCC);
113         rcc2 = HWREG(SYSCTL_RCC2);
114
115         /*
116          * Step #1: bypass the PLL and system clock divider by setting the
117          * BYPASS bit and clearing the USESYS bit in the RCC register. This
118          * configures the system to run off a “raw” clock source (using the
119          * main oscillator or internal oscillator) and allows for the new PLL
120          * configuration to be validated before switching the system clock to
121          * the PLL.
122          */
123         rcc |= SYSCTL_RCC_BYPASS;
124         rcc &= ~SYSCTL_RCC_USESYSDIV;
125         rcc2 |= SYSCTL_RCC2_BYPASS2;
126
127         /* Write back RCC/RCC2 registers */
128         HWREG(SYSCTL_RCC) = rcc;
129         HWREG(SYSCTL_RCC) = rcc2;
130
131         /*
132          * Step #2: select the crystal value (XTAL) and oscillator source
133          * (OSCSRC), and clear the PWRDN bit in RCC/RCC2. Setting the XTAL
134          * field automatically pulls valid PLL configuration data for the
135          * appropriate crystal, and clearing the PWRDN bit powers and enables
136          * the PLL and its output.
137          */
138
139         /* Enable the main oscillator first. */
140         rcc &= ~(SYSCTL_RCC_IOSCDIS | SYSCTL_RCC_MOSCDIS);
141         rcc |= SYSCTL_RCC_IOSCDIS;
142
143         /* Do not override RCC register fields */
144         rcc2 &= ~SYSCTL_RCC2_USERCC2;
145
146         rcc &= ~(SYSCTL_RCC_XTAL_M | SYSCTL_RCC_OSCSRC_M | SYSCTL_RCC_PWRDN);
147         rcc |= XTAL_FREQ | SYSCTL_RCC_OSCSRC_MAIN;
148
149         /* Clear the PLL lock interrupt. */
150         HWREG(SYSCTL_MISC) = SYSCTL_INT_PLL_LOCK;
151
152         HWREG(SYSCTL_RCC) = rcc;
153
154         __delay(16);
155
156         /*
157          * Step #3: select the desired system divider (SYSDIV) in RCC/RCC2 and
158          * set the USESYS bit in RCC. The SYSDIV field determines the system
159          * frequency for the microcontroller.
160          */
161         rcc &= ~(SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV);
162
163         /*
164          * Try to evaluate the correct SYSDIV value depending on the desired
165          * CPU frequency.
166          */
167         clk = RCC_TO_CLK(rcc);
168         for (i = 0; i < 16; i++)
169         {
170                 clk = clk / (i + 1);
171                 if (CPU_FREQ >= clk)
172                         break;
173         }
174         if (i)
175         {
176                 rcc |= SYSCTL_RCC_USESYSDIV;
177                 rcc |= i << SYSCTL_RCC_SYSDIV_SHIFT;
178         }
179
180         /*
181          * Step #4: wait for the PLL to lock by polling the PLLLRIS bit in the
182          * Raw Interrupt Status (RIS) register.
183          */
184         for (i = 0; i < 32768; i++)
185                 if (HWREG(SYSCTL_RIS) & SYSCTL_INT_PLL_LOCK)
186                         break;
187
188         /*
189          * Step #5: enable use of the PLL by clearing the BYPASS bit in
190          * RCC/RCC2.
191          */
192         rcc &= ~SYSCTL_RCC_BYPASS;
193
194         HWREG(SYSCTL_RCC) = rcc;
195
196         __delay(16);
197 }