blob: 6eb7d9f1b0c3e047ac4df91dc886324a731ebccd [file] [log] [blame]
Chandan Nath4ba33452011-10-14 02:58:23 +00001/*
2 * clock.c
3 *
4 * clocks for AM33XX based boards
5 *
6 * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <common.h>
20#include <asm/arch/cpu.h>
21#include <asm/arch/clock.h>
22#include <asm/arch/hardware.h>
23#include <asm/io.h>
24
25#define PRCM_MOD_EN 0x2
26#define PRCM_FORCE_WAKEUP 0x2
Chandan Nath2015c382012-07-24 12:22:17 +000027#define PRCM_FUNCTL 0x0
Chandan Nath4ba33452011-10-14 02:58:23 +000028
29#define PRCM_EMIF_CLK_ACTIVITY BIT(2)
30#define PRCM_L3_GCLK_ACTIVITY BIT(4)
31
32#define PLL_BYPASS_MODE 0x4
33#define ST_MN_BYPASS 0x00000100
34#define ST_DPLL_CLK 0x00000001
35#define CLK_SEL_MASK 0x7ffff
36#define CLK_DIV_MASK 0x1f
37#define CLK_DIV2_MASK 0x7f
38#define CLK_SEL_SHIFT 0x8
39#define CLK_MODE_SEL 0x7
40#define CLK_MODE_MASK 0xfffffff8
41#define CLK_DIV_SEL 0xFFFFFFE0
Chandan Nath2015c382012-07-24 12:22:17 +000042#define CPGMAC0_IDLE 0x30000
Chandan Nath4ba33452011-10-14 02:58:23 +000043
44const struct cm_perpll *cmper = (struct cm_perpll *)CM_PER;
45const struct cm_wkuppll *cmwkup = (struct cm_wkuppll *)CM_WKUP;
46const struct cm_dpll *cmdpll = (struct cm_dpll *)CM_DPLL;
Vaibhav Hiremath2d7da5f2012-03-08 17:15:47 +053047const struct cm_rtc *cmrtc = (struct cm_rtc *)CM_RTC;
Chandan Nath4ba33452011-10-14 02:58:23 +000048
49static void enable_interface_clocks(void)
50{
51 /* Enable all the Interconnect Modules */
52 writel(PRCM_MOD_EN, &cmper->l3clkctrl);
53 while (readl(&cmper->l3clkctrl) != PRCM_MOD_EN)
54 ;
55
56 writel(PRCM_MOD_EN, &cmper->l4lsclkctrl);
57 while (readl(&cmper->l4lsclkctrl) != PRCM_MOD_EN)
58 ;
59
60 writel(PRCM_MOD_EN, &cmper->l4fwclkctrl);
61 while (readl(&cmper->l4fwclkctrl) != PRCM_MOD_EN)
62 ;
63
64 writel(PRCM_MOD_EN, &cmwkup->wkl4wkclkctrl);
65 while (readl(&cmwkup->wkl4wkclkctrl) != PRCM_MOD_EN)
66 ;
67
68 writel(PRCM_MOD_EN, &cmper->l3instrclkctrl);
69 while (readl(&cmper->l3instrclkctrl) != PRCM_MOD_EN)
70 ;
71
72 writel(PRCM_MOD_EN, &cmper->l4hsclkctrl);
73 while (readl(&cmper->l4hsclkctrl) != PRCM_MOD_EN)
74 ;
Tom Rinif8bf6b22012-07-31 07:22:47 -070075
76 writel(PRCM_MOD_EN, &cmwkup->wkgpio0clkctrl);
77 while (readl(&cmwkup->wkgpio0clkctrl) != PRCM_MOD_EN)
78 ;
Chandan Nath4ba33452011-10-14 02:58:23 +000079}
80
81/*
82 * Force power domain wake up transition
83 * Ensure that the corresponding interface clock is active before
84 * using the peripheral
85 */
86static void power_domain_wkup_transition(void)
87{
88 writel(PRCM_FORCE_WAKEUP, &cmper->l3clkstctrl);
89 writel(PRCM_FORCE_WAKEUP, &cmper->l4lsclkstctrl);
90 writel(PRCM_FORCE_WAKEUP, &cmwkup->wkclkstctrl);
91 writel(PRCM_FORCE_WAKEUP, &cmper->l4fwclkstctrl);
92 writel(PRCM_FORCE_WAKEUP, &cmper->l3sclkstctrl);
93}
94
95/*
96 * Enable the peripheral clock for required peripherals
97 */
98static void enable_per_clocks(void)
99{
100 /* Enable the control module though RBL would have done it*/
101 writel(PRCM_MOD_EN, &cmwkup->wkctrlclkctrl);
102 while (readl(&cmwkup->wkctrlclkctrl) != PRCM_MOD_EN)
103 ;
104
105 /* Enable the module clock */
106 writel(PRCM_MOD_EN, &cmper->timer2clkctrl);
107 while (readl(&cmper->timer2clkctrl) != PRCM_MOD_EN)
108 ;
109
Chandan Nath5b5c2122012-01-09 20:38:56 +0000110 /* Select the Master osc 24 MHZ as Timer2 clock source */
111 writel(0x1, &cmdpll->clktimer2clk);
112
Chandan Nath4ba33452011-10-14 02:58:23 +0000113 /* UART0 */
114 writel(PRCM_MOD_EN, &cmwkup->wkup_uart0ctrl);
115 while (readl(&cmwkup->wkup_uart0ctrl) != PRCM_MOD_EN)
116 ;
Chandan Nathd6e97f82012-01-09 20:38:58 +0000117
Andrew Bradfordcca45e72012-10-25 08:21:29 -0400118 /* UART1 */
119#ifdef CONFIG_SERIAL2
120 writel(PRCM_MOD_EN, &cmper->uart1clkctrl);
121 while (readl(&cmper->uart1clkctrl) != PRCM_MOD_EN)
122 ;
123#endif /* CONFIG_SERIAL2 */
124
125 /* UART2 */
126#ifdef CONFIG_SERIAL3
127 writel(PRCM_MOD_EN, &cmper->uart2clkctrl);
128 while (readl(&cmper->uart2clkctrl) != PRCM_MOD_EN)
129 ;
130#endif /* CONFIG_SERIAL3 */
131
132 /* UART3 */
133#ifdef CONFIG_SERIAL4
134 writel(PRCM_MOD_EN, &cmper->uart3clkctrl);
135 while (readl(&cmper->uart3clkctrl) != PRCM_MOD_EN)
136 ;
137#endif /* CONFIG_SERIAL4 */
138
139 /* UART4 */
140#ifdef CONFIG_SERIAL5
141 writel(PRCM_MOD_EN, &cmper->uart4clkctrl);
142 while (readl(&cmper->uart4clkctrl) != PRCM_MOD_EN)
143 ;
144#endif /* CONFIG_SERIAL5 */
145
146 /* UART5 */
147#ifdef CONFIG_SERIAL6
148 writel(PRCM_MOD_EN, &cmper->uart5clkctrl);
149 while (readl(&cmper->uart5clkctrl) != PRCM_MOD_EN)
150 ;
151#endif /* CONFIG_SERIAL6 */
152
Ilya Yanok2ebbb862012-11-06 13:06:30 +0000153 /* GPMC */
154 writel(PRCM_MOD_EN, &cmper->gpmcclkctrl);
155 while (readl(&cmper->gpmcclkctrl) != PRCM_MOD_EN)
156 ;
157
Chandan Nathd6e97f82012-01-09 20:38:58 +0000158 /* MMC0*/
159 writel(PRCM_MOD_EN, &cmper->mmc0clkctrl);
160 while (readl(&cmper->mmc0clkctrl) != PRCM_MOD_EN)
161 ;
Patil, Rachna5f70c512012-01-22 23:47:01 +0000162
163 /* i2c0 */
164 writel(PRCM_MOD_EN, &cmwkup->wkup_i2c0ctrl);
165 while (readl(&cmwkup->wkup_i2c0ctrl) != PRCM_MOD_EN)
166 ;
Steve Sakoman6229e332012-06-04 05:35:34 +0000167
168 /* gpio1 module */
169 writel(PRCM_MOD_EN, &cmper->gpio1clkctrl);
170 while (readl(&cmper->gpio1clkctrl) != PRCM_MOD_EN)
171 ;
172
173 /* gpio2 module */
174 writel(PRCM_MOD_EN, &cmper->gpio2clkctrl);
175 while (readl(&cmper->gpio2clkctrl) != PRCM_MOD_EN)
176 ;
177
178 /* gpio3 module */
179 writel(PRCM_MOD_EN, &cmper->gpio3clkctrl);
180 while (readl(&cmper->gpio3clkctrl) != PRCM_MOD_EN)
181 ;
Steve Sakoman695b1fe2012-06-22 07:45:57 +0000182
183 /* i2c1 */
184 writel(PRCM_MOD_EN, &cmper->i2c1clkctrl);
185 while (readl(&cmper->i2c1clkctrl) != PRCM_MOD_EN)
186 ;
Chandan Nath2015c382012-07-24 12:22:17 +0000187
188 /* Ethernet */
189 writel(PRCM_MOD_EN, &cmper->cpgmac0clkctrl);
190 while ((readl(&cmper->cpgmac0clkctrl) & CPGMAC0_IDLE) != PRCM_FUNCTL)
191 ;
Tom Rini470e2a22012-08-08 14:29:51 -0700192
193 /* spi0 */
194 writel(PRCM_MOD_EN, &cmper->spi0clkctrl);
195 while (readl(&cmper->spi0clkctrl) != PRCM_MOD_EN)
196 ;
Vaibhav Hiremath2d7da5f2012-03-08 17:15:47 +0530197
198 /* RTC */
199 writel(PRCM_MOD_EN, &cmrtc->rtcclkctrl);
200 while (readl(&cmrtc->rtcclkctrl) != PRCM_MOD_EN)
201 ;
Chandan Nath4ba33452011-10-14 02:58:23 +0000202}
203
204static void mpu_pll_config(void)
205{
206 u32 clkmode, clksel, div_m2;
207
208 clkmode = readl(&cmwkup->clkmoddpllmpu);
209 clksel = readl(&cmwkup->clkseldpllmpu);
210 div_m2 = readl(&cmwkup->divm2dpllmpu);
211
212 /* Set the PLL to bypass Mode */
213 writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllmpu);
214 while (readl(&cmwkup->idlestdpllmpu) != ST_MN_BYPASS)
215 ;
216
217 clksel = clksel & (~CLK_SEL_MASK);
218 clksel = clksel | ((MPUPLL_M << CLK_SEL_SHIFT) | MPUPLL_N);
219 writel(clksel, &cmwkup->clkseldpllmpu);
220
221 div_m2 = div_m2 & ~CLK_DIV_MASK;
222 div_m2 = div_m2 | MPUPLL_M2;
223 writel(div_m2, &cmwkup->divm2dpllmpu);
224
225 clkmode = clkmode | CLK_MODE_SEL;
226 writel(clkmode, &cmwkup->clkmoddpllmpu);
227
228 while (readl(&cmwkup->idlestdpllmpu) != ST_DPLL_CLK)
229 ;
230}
231
232static void core_pll_config(void)
233{
234 u32 clkmode, clksel, div_m4, div_m5, div_m6;
235
236 clkmode = readl(&cmwkup->clkmoddpllcore);
237 clksel = readl(&cmwkup->clkseldpllcore);
238 div_m4 = readl(&cmwkup->divm4dpllcore);
239 div_m5 = readl(&cmwkup->divm5dpllcore);
240 div_m6 = readl(&cmwkup->divm6dpllcore);
241
242 /* Set the PLL to bypass Mode */
243 writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllcore);
244
245 while (readl(&cmwkup->idlestdpllcore) != ST_MN_BYPASS)
246 ;
247
248 clksel = clksel & (~CLK_SEL_MASK);
249 clksel = clksel | ((COREPLL_M << CLK_SEL_SHIFT) | COREPLL_N);
250 writel(clksel, &cmwkup->clkseldpllcore);
251
252 div_m4 = div_m4 & ~CLK_DIV_MASK;
253 div_m4 = div_m4 | COREPLL_M4;
254 writel(div_m4, &cmwkup->divm4dpllcore);
255
256 div_m5 = div_m5 & ~CLK_DIV_MASK;
257 div_m5 = div_m5 | COREPLL_M5;
258 writel(div_m5, &cmwkup->divm5dpllcore);
259
260 div_m6 = div_m6 & ~CLK_DIV_MASK;
261 div_m6 = div_m6 | COREPLL_M6;
262 writel(div_m6, &cmwkup->divm6dpllcore);
263
264 clkmode = clkmode | CLK_MODE_SEL;
265 writel(clkmode, &cmwkup->clkmoddpllcore);
266
267 while (readl(&cmwkup->idlestdpllcore) != ST_DPLL_CLK)
268 ;
269}
270
271static void per_pll_config(void)
272{
273 u32 clkmode, clksel, div_m2;
274
275 clkmode = readl(&cmwkup->clkmoddpllper);
276 clksel = readl(&cmwkup->clkseldpllper);
277 div_m2 = readl(&cmwkup->divm2dpllper);
278
279 /* Set the PLL to bypass Mode */
280 writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllper);
281
282 while (readl(&cmwkup->idlestdpllper) != ST_MN_BYPASS)
283 ;
284
285 clksel = clksel & (~CLK_SEL_MASK);
286 clksel = clksel | ((PERPLL_M << CLK_SEL_SHIFT) | PERPLL_N);
287 writel(clksel, &cmwkup->clkseldpllper);
288
289 div_m2 = div_m2 & ~CLK_DIV2_MASK;
290 div_m2 = div_m2 | PERPLL_M2;
291 writel(div_m2, &cmwkup->divm2dpllper);
292
293 clkmode = clkmode | CLK_MODE_SEL;
294 writel(clkmode, &cmwkup->clkmoddpllper);
295
296 while (readl(&cmwkup->idlestdpllper) != ST_DPLL_CLK)
297 ;
298}
299
Tom Rini034aba72012-07-03 09:20:06 -0700300void ddr_pll_config(unsigned int ddrpll_m)
Chandan Nath4ba33452011-10-14 02:58:23 +0000301{
302 u32 clkmode, clksel, div_m2;
303
304 clkmode = readl(&cmwkup->clkmoddpllddr);
305 clksel = readl(&cmwkup->clkseldpllddr);
306 div_m2 = readl(&cmwkup->divm2dpllddr);
307
308 /* Set the PLL to bypass Mode */
309 clkmode = (clkmode & CLK_MODE_MASK) | PLL_BYPASS_MODE;
310 writel(clkmode, &cmwkup->clkmoddpllddr);
311
312 /* Wait till bypass mode is enabled */
313 while ((readl(&cmwkup->idlestdpllddr) & ST_MN_BYPASS)
314 != ST_MN_BYPASS)
315 ;
316
317 clksel = clksel & (~CLK_SEL_MASK);
Tom Rini034aba72012-07-03 09:20:06 -0700318 clksel = clksel | ((ddrpll_m << CLK_SEL_SHIFT) | DDRPLL_N);
Chandan Nath4ba33452011-10-14 02:58:23 +0000319 writel(clksel, &cmwkup->clkseldpllddr);
320
321 div_m2 = div_m2 & CLK_DIV_SEL;
322 div_m2 = div_m2 | DDRPLL_M2;
323 writel(div_m2, &cmwkup->divm2dpllddr);
324
325 clkmode = (clkmode & CLK_MODE_MASK) | CLK_MODE_SEL;
326 writel(clkmode, &cmwkup->clkmoddpllddr);
327
328 /* Wait till dpll is locked */
329 while ((readl(&cmwkup->idlestdpllddr) & ST_DPLL_CLK) != ST_DPLL_CLK)
330 ;
331}
332
333void enable_emif_clocks(void)
334{
335 /* Enable the EMIF_FW Functional clock */
336 writel(PRCM_MOD_EN, &cmper->emiffwclkctrl);
337 /* Enable EMIF0 Clock */
338 writel(PRCM_MOD_EN, &cmper->emifclkctrl);
Chandan Nath4ba33452011-10-14 02:58:23 +0000339 /* Poll if module is functional */
340 while ((readl(&cmper->emifclkctrl)) != PRCM_MOD_EN)
341 ;
342}
343
344/*
345 * Configure the PLL/PRCM for necessary peripherals
346 */
347void pll_init()
348{
349 mpu_pll_config();
350 core_pll_config();
351 per_pll_config();
Chandan Nath4ba33452011-10-14 02:58:23 +0000352
353 /* Enable the required interconnect clocks */
354 enable_interface_clocks();
355
356 /* Power domain wake up transition */
357 power_domain_wkup_transition();
358
359 /* Enable the required peripherals */
360 enable_per_clocks();
361}