blob: 88e5336850cfbab0698ad65a90fbf86b357d8121 [file] [log] [blame]
Aneesh V0d2628b2011-07-21 09:10:07 -04001/*
2 *
3 * Clock initialization for OMAP4
4 *
5 * (C) Copyright 2010
6 * Texas Instruments, <www.ti.com>
7 *
8 * Aneesh V <aneesh@ti.com>
9 *
10 * Based on previous work by:
11 * Santosh Shilimkar <santosh.shilimkar@ti.com>
12 * Rajendra Nayak <rnayak@ti.com>
13 *
14 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 */
32#include <common.h>
33#include <asm/omap_common.h>
Sanjeev Premi0c2c8ac2011-09-08 10:48:39 -040034#include <asm/gpio.h>
Aneesh V0d2628b2011-07-21 09:10:07 -040035#include <asm/arch/clocks.h>
36#include <asm/arch/sys_proto.h>
37#include <asm/utils.h>
Aneesh V0fa1d1b2011-07-21 09:29:32 -040038#include <asm/omap_gpio.h>
Lokesh Vutlafef54c32013-02-04 04:21:59 +000039#include <asm/emif.h>
Aneesh V0d2628b2011-07-21 09:10:07 -040040
41#ifndef CONFIG_SPL_BUILD
42/*
43 * printing to console doesn't work unless
44 * this code is executed from SPL
45 */
46#define printf(fmt, args...)
47#define puts(s)
48#endif
49
SRICHARAN R1a79cab2013-02-04 04:22:01 +000050const u32 sys_clk_array[8] = {
51 12000000, /* 12 MHz */
52 13000000, /* 13 MHz */
53 16800000, /* 16.8 MHz */
54 19200000, /* 19.2 MHz */
55 26000000, /* 26 MHz */
56 27000000, /* 27 MHz */
57 38400000, /* 38.4 MHz */
58};
59
Aneesh V0d2628b2011-07-21 09:10:07 -040060static inline u32 __get_sys_clk_index(void)
61{
62 u32 ind;
63 /*
64 * For ES1 the ROM code calibration of sys clock is not reliable
65 * due to hw issue. So, use hard-coded value. If this value is not
66 * correct for any board over-ride this function in board file
67 * From ES2.0 onwards you will get this information from
68 * CM_SYS_CLKSEL
69 */
70 if (omap_revision() == OMAP4430_ES1_0)
71 ind = OMAP_SYS_CLK_IND_38_4_MHZ;
72 else {
73 /* SYS_CLKSEL - 1 to match the dpll param array indices */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +000074 ind = (readl((*prcm)->cm_sys_clksel) &
Aneesh V0d2628b2011-07-21 09:10:07 -040075 CM_SYS_CLKSEL_SYS_CLKSEL_MASK) - 1;
76 }
77 return ind;
78}
79
80u32 get_sys_clk_index(void)
81 __attribute__ ((weak, alias("__get_sys_clk_index")));
82
83u32 get_sys_clk_freq(void)
84{
85 u8 index = get_sys_clk_index();
86 return sys_clk_array[index];
87}
88
SRICHARAN R1a79cab2013-02-04 04:22:01 +000089void setup_post_dividers(u32 const base, const struct dpll_params *params)
90{
91 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
92
93 /* Setup post-dividers */
94 if (params->m2 >= 0)
95 writel(params->m2, &dpll_regs->cm_div_m2_dpll);
96 if (params->m3 >= 0)
97 writel(params->m3, &dpll_regs->cm_div_m3_dpll);
98 if (params->m4_h11 >= 0)
99 writel(params->m4_h11, &dpll_regs->cm_div_m4_h11_dpll);
100 if (params->m5_h12 >= 0)
101 writel(params->m5_h12, &dpll_regs->cm_div_m5_h12_dpll);
102 if (params->m6_h13 >= 0)
103 writel(params->m6_h13, &dpll_regs->cm_div_m6_h13_dpll);
104 if (params->m7_h14 >= 0)
105 writel(params->m7_h14, &dpll_regs->cm_div_m7_h14_dpll);
106 if (params->h22 >= 0)
107 writel(params->h22, &dpll_regs->cm_div_h22_dpll);
108 if (params->h23 >= 0)
109 writel(params->h23, &dpll_regs->cm_div_h23_dpll);
110}
111
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000112static inline void do_bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400113{
114 struct dpll_regs *dpll_regs = (struct dpll_regs *)base;
115
116 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
117 CM_CLKMODE_DPLL_DPLL_EN_MASK,
118 DPLL_EN_FAST_RELOCK_BYPASS <<
119 CM_CLKMODE_DPLL_EN_SHIFT);
120}
121
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000122static inline void wait_for_bypass(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400123{
124 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
125
126 if (!wait_on_value(ST_DPLL_CLK_MASK, 0, &dpll_regs->cm_idlest_dpll,
127 LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000128 printf("Bypassing DPLL failed %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400129 }
130}
131
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000132static inline void do_lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400133{
134 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
135
136 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
137 CM_CLKMODE_DPLL_DPLL_EN_MASK,
138 DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
139}
140
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000141static inline void wait_for_lock(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400142{
143 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
144
145 if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
146 &dpll_regs->cm_idlest_dpll, LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000147 printf("DPLL locking failed for %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400148 hang();
149 }
150}
151
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000152inline u32 check_for_lock(u32 const base)
Sricharan308fe922011-11-15 09:50:03 -0500153{
154 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
155 u32 lock = readl(&dpll_regs->cm_idlest_dpll) & ST_DPLL_CLK_MASK;
156
157 return lock;
158}
159
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000160const struct dpll_params *get_mpu_dpll_params(struct dplls const *dpll_data)
161{
162 u32 sysclk_ind = get_sys_clk_index();
163 return &dpll_data->mpu[sysclk_ind];
164}
165
166const struct dpll_params *get_core_dpll_params(struct dplls const *dpll_data)
167{
168 u32 sysclk_ind = get_sys_clk_index();
169 return &dpll_data->core[sysclk_ind];
170}
171
172const struct dpll_params *get_per_dpll_params(struct dplls const *dpll_data)
173{
174 u32 sysclk_ind = get_sys_clk_index();
175 return &dpll_data->per[sysclk_ind];
176}
177
178const struct dpll_params *get_iva_dpll_params(struct dplls const *dpll_data)
179{
180 u32 sysclk_ind = get_sys_clk_index();
181 return &dpll_data->iva[sysclk_ind];
182}
183
184const struct dpll_params *get_usb_dpll_params(struct dplls const *dpll_data)
185{
186 u32 sysclk_ind = get_sys_clk_index();
187 return &dpll_data->usb[sysclk_ind];
188}
189
190const struct dpll_params *get_abe_dpll_params(struct dplls const *dpll_data)
191{
192#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
193 u32 sysclk_ind = get_sys_clk_index();
194 return &dpll_data->abe[sysclk_ind];
195#else
196 return dpll_data->abe;
197#endif
198}
199
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000200static void do_setup_dpll(u32 const base, const struct dpll_params *params,
Sricharan308fe922011-11-15 09:50:03 -0500201 u8 lock, char *dpll)
Aneesh V0d2628b2011-07-21 09:10:07 -0400202{
Sricharan308fe922011-11-15 09:50:03 -0500203 u32 temp, M, N;
Aneesh V0d2628b2011-07-21 09:10:07 -0400204 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
205
Sricharan308fe922011-11-15 09:50:03 -0500206 temp = readl(&dpll_regs->cm_clksel_dpll);
207
208 if (check_for_lock(base)) {
209 /*
210 * The Dpll has already been locked by rom code using CH.
211 * Check if M,N are matching with Ideal nominal opp values.
212 * If matches, skip the rest otherwise relock.
213 */
214 M = (temp & CM_CLKSEL_DPLL_M_MASK) >> CM_CLKSEL_DPLL_M_SHIFT;
215 N = (temp & CM_CLKSEL_DPLL_N_MASK) >> CM_CLKSEL_DPLL_N_SHIFT;
216 if ((M != (params->m)) || (N != (params->n))) {
217 debug("\n %s Dpll locked, but not for ideal M = %d,"
218 "N = %d values, current values are M = %d,"
219 "N= %d" , dpll, params->m, params->n,
220 M, N);
221 } else {
222 /* Dpll locked with ideal values for nominal opps. */
223 debug("\n %s Dpll already locked with ideal"
224 "nominal opp values", dpll);
225 goto setup_post_dividers;
226 }
227 }
228
Aneesh V0d2628b2011-07-21 09:10:07 -0400229 bypass_dpll(base);
230
231 /* Set M & N */
Aneesh V0d2628b2011-07-21 09:10:07 -0400232 temp &= ~CM_CLKSEL_DPLL_M_MASK;
233 temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
234
235 temp &= ~CM_CLKSEL_DPLL_N_MASK;
236 temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
237
238 writel(temp, &dpll_regs->cm_clksel_dpll);
239
240 /* Lock */
241 if (lock)
242 do_lock_dpll(base);
243
Sricharan308fe922011-11-15 09:50:03 -0500244setup_post_dividers:
Sricharan9784f1f2011-11-15 09:49:58 -0500245 setup_post_dividers(base, params);
Aneesh V0d2628b2011-07-21 09:10:07 -0400246
247 /* Wait till the DPLL locks */
248 if (lock)
249 wait_for_lock(base);
250}
251
Sricharan9784f1f2011-11-15 09:49:58 -0500252u32 omap_ddr_clk(void)
Aneesh V0d2628b2011-07-21 09:10:07 -0400253{
Sricharan9784f1f2011-11-15 09:49:58 -0500254 u32 ddr_clk, sys_clk_khz, omap_rev, divider;
Aneesh V0d2628b2011-07-21 09:10:07 -0400255 const struct dpll_params *core_dpll_params;
256
Sricharan9784f1f2011-11-15 09:49:58 -0500257 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400258 sys_clk_khz = get_sys_clk_freq() / 1000;
259
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000260 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400261
262 debug("sys_clk %d\n ", sys_clk_khz * 1000);
263
264 /* Find Core DPLL locked frequency first */
265 ddr_clk = sys_clk_khz * 2 * core_dpll_params->m /
266 (core_dpll_params->n + 1);
Sricharan9784f1f2011-11-15 09:49:58 -0500267
268 if (omap_rev < OMAP5430_ES1_0) {
269 /*
270 * DDR frequency is PHY_ROOT_CLK/2
271 * PHY_ROOT_CLK = Fdpll/2/M2
272 */
273 divider = 4;
274 } else {
275 /*
276 * DDR frequency is PHY_ROOT_CLK
277 * PHY_ROOT_CLK = Fdpll/2/M2
278 */
279 divider = 2;
280 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400281
Sricharan9784f1f2011-11-15 09:49:58 -0500282 ddr_clk = ddr_clk / divider / core_dpll_params->m2;
Aneesh V0d2628b2011-07-21 09:10:07 -0400283 ddr_clk *= 1000; /* convert to Hz */
284 debug("ddr_clk %d\n ", ddr_clk);
285
286 return ddr_clk;
287}
288
Aneesh Va47a79f2011-07-21 09:29:36 -0400289/*
290 * Lock MPU dpll
291 *
292 * Resulting MPU frequencies:
293 * 4430 ES1.0 : 600 MHz
294 * 4430 ES2.x : 792 MHz (OPP Turbo)
295 * 4460 : 920 MHz (OPP Turbo) - DCC disabled
296 */
297void configure_mpu_dpll(void)
298{
299 const struct dpll_params *params;
300 struct dpll_regs *mpu_dpll_regs;
Sricharan9784f1f2011-11-15 09:49:58 -0500301 u32 omap_rev;
302 omap_rev = omap_revision();
Aneesh Va47a79f2011-07-21 09:29:36 -0400303
Sricharan9784f1f2011-11-15 09:49:58 -0500304 /*
305 * DCC and clock divider settings for 4460.
306 * DCC is required, if more than a certain frequency is required.
307 * For, 4460 > 1GHZ.
308 * 5430 > 1.4GHZ.
309 */
310 if ((omap_rev >= OMAP4460_ES1_0) && (omap_rev < OMAP5430_ES1_0)) {
Aneesh Va47a79f2011-07-21 09:29:36 -0400311 mpu_dpll_regs =
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000312 (struct dpll_regs *)((*prcm)->cm_clkmode_dpll_mpu);
313 bypass_dpll((*prcm)->cm_clkmode_dpll_mpu);
314 clrbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400315 MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000316 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400317 MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
318 clrbits_le32(&mpu_dpll_regs->cm_clksel_dpll,
319 CM_CLKSEL_DCC_EN_MASK);
320 }
321
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000322 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
SRICHARAN Rb1ee0bc2012-03-12 02:25:34 +0000323 MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000324 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
SRICHARAN Rb1ee0bc2012-03-12 02:25:34 +0000325 MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
326
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000327 params = get_mpu_dpll_params(*dplls_data);
Sricharan308fe922011-11-15 09:50:03 -0500328
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000329 do_setup_dpll((*prcm)->cm_clkmode_dpll_mpu, params, DPLL_LOCK, "mpu");
Aneesh Va47a79f2011-07-21 09:29:36 -0400330 debug("MPU DPLL locked\n");
331}
332
Govindraj.Rad4426b2012-02-06 03:55:36 +0000333#ifdef CONFIG_USB_EHCI_OMAP
334static void setup_usb_dpll(void)
335{
336 const struct dpll_params *params;
337 u32 sys_clk_khz, sd_div, num, den;
338
339 sys_clk_khz = get_sys_clk_freq() / 1000;
340 /*
341 * USB:
342 * USB dpll is J-type. Need to set DPLL_SD_DIV for jitter correction
343 * DPLL_SD_DIV = CEILING ([DPLL_MULT/(DPLL_DIV+1)]* CLKINP / 250)
344 * - where CLKINP is sys_clk in MHz
345 * Use CLKINP in KHz and adjust the denominator accordingly so
346 * that we have enough accuracy and at the same time no overflow
347 */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000348 params = get_usb_dpll_params(*dplls_data);
Govindraj.Rad4426b2012-02-06 03:55:36 +0000349 num = params->m * sys_clk_khz;
350 den = (params->n + 1) * 250 * 1000;
351 num += den - 1;
352 sd_div = num / den;
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000353 clrsetbits_le32((*prcm)->cm_clksel_dpll_usb,
Govindraj.Rad4426b2012-02-06 03:55:36 +0000354 CM_CLKSEL_DPLL_DPLL_SD_DIV_MASK,
355 sd_div << CM_CLKSEL_DPLL_DPLL_SD_DIV_SHIFT);
356
357 /* Now setup the dpll with the regular function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000358 do_setup_dpll((*prcm)->cm_clkmode_dpll_usb, params, DPLL_LOCK, "usb");
Govindraj.Rad4426b2012-02-06 03:55:36 +0000359}
360#endif
361
Aneesh V0d2628b2011-07-21 09:10:07 -0400362static void setup_dplls(void)
363{
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000364 u32 temp;
Aneesh V0d2628b2011-07-21 09:10:07 -0400365 const struct dpll_params *params;
Aneesh V0d2628b2011-07-21 09:10:07 -0400366
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000367 debug("setup_dplls\n");
Aneesh V0d2628b2011-07-21 09:10:07 -0400368
369 /* CORE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000370 params = get_core_dpll_params(*dplls_data); /* default - safest */
Aneesh V0d2628b2011-07-21 09:10:07 -0400371 /*
372 * Do not lock the core DPLL now. Just set it up.
373 * Core DPLL will be locked after setting up EMIF
374 * using the FREQ_UPDATE method(freq_update_core())
375 */
Lokesh Vutlafef54c32013-02-04 04:21:59 +0000376 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000377 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000378 DPLL_NO_LOCK, "core");
379 else
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000380 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000381 DPLL_LOCK, "core");
Aneesh V0d2628b2011-07-21 09:10:07 -0400382 /* Set the ratios for CORE_CLK, L3_CLK, L4_CLK */
383 temp = (CLKSEL_CORE_X2_DIV_1 << CLKSEL_CORE_SHIFT) |
384 (CLKSEL_L3_CORE_DIV_2 << CLKSEL_L3_SHIFT) |
385 (CLKSEL_L4_L3_DIV_2 << CLKSEL_L4_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000386 writel(temp, (*prcm)->cm_clksel_core);
Aneesh V0d2628b2011-07-21 09:10:07 -0400387 debug("Core DPLL configured\n");
388
389 /* lock PER dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000390 params = get_per_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000391 do_setup_dpll((*prcm)->cm_clkmode_dpll_per,
Sricharan308fe922011-11-15 09:50:03 -0500392 params, DPLL_LOCK, "per");
Aneesh V0d2628b2011-07-21 09:10:07 -0400393 debug("PER DPLL locked\n");
394
395 /* MPU dpll */
Aneesh Va47a79f2011-07-21 09:29:36 -0400396 configure_mpu_dpll();
Govindraj.Rad4426b2012-02-06 03:55:36 +0000397
398#ifdef CONFIG_USB_EHCI_OMAP
399 setup_usb_dpll();
400#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400401}
402
Sricharan308fe922011-11-15 09:50:03 -0500403#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400404static void setup_non_essential_dplls(void)
405{
Anatolij Gustschind75ffd42012-03-27 23:13:43 +0000406 u32 abe_ref_clk;
Aneesh V0d2628b2011-07-21 09:10:07 -0400407 const struct dpll_params *params;
408
Aneesh V0d2628b2011-07-21 09:10:07 -0400409 /* IVA */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000410 clrsetbits_le32((*prcm)->cm_bypclk_dpll_iva,
Aneesh V0d2628b2011-07-21 09:10:07 -0400411 CM_BYPCLK_DPLL_IVA_CLKSEL_MASK, DPLL_IVA_CLKSEL_CORE_X2_DIV_2);
412
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000413 params = get_iva_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000414 do_setup_dpll((*prcm)->cm_clkmode_dpll_iva, params, DPLL_LOCK, "iva");
Aneesh V0d2628b2011-07-21 09:10:07 -0400415
Sricharan9784f1f2011-11-15 09:49:58 -0500416 /* Configure ABE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000417 params = get_abe_dpll_params(*dplls_data);
Sricharan9784f1f2011-11-15 09:49:58 -0500418#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
Aneesh V0d2628b2011-07-21 09:10:07 -0400419 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_SYSCLK;
420#else
Aneesh V0d2628b2011-07-21 09:10:07 -0400421 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_32KCLK;
422 /*
423 * We need to enable some additional options to achieve
424 * 196.608MHz from 32768 Hz
425 */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000426 setbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400427 CM_CLKMODE_DPLL_DRIFTGUARD_EN_MASK|
428 CM_CLKMODE_DPLL_RELOCK_RAMP_EN_MASK|
429 CM_CLKMODE_DPLL_LPMODE_EN_MASK|
430 CM_CLKMODE_DPLL_REGM4XEN_MASK);
431 /* Spend 4 REFCLK cycles at each stage */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000432 clrsetbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400433 CM_CLKMODE_DPLL_RAMP_RATE_MASK,
434 1 << CM_CLKMODE_DPLL_RAMP_RATE_SHIFT);
435#endif
436
437 /* Select the right reference clk */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000438 clrsetbits_le32((*prcm)->cm_abe_pll_ref_clksel,
Aneesh V0d2628b2011-07-21 09:10:07 -0400439 CM_ABE_PLL_REF_CLKSEL_CLKSEL_MASK,
440 abe_ref_clk << CM_ABE_PLL_REF_CLKSEL_CLKSEL_SHIFT);
441 /* Lock the dpll */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000442 do_setup_dpll((*prcm)->cm_clkmode_dpll_abe, params, DPLL_LOCK, "abe");
Aneesh V0d2628b2011-07-21 09:10:07 -0400443}
Sricharan308fe922011-11-15 09:50:03 -0500444#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400445
SRICHARAN R00d328c2013-02-04 04:22:02 +0000446u32 get_offset_code(u32 volt_offset, struct pmic_data *pmic)
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400447{
SRICHARAN R00d328c2013-02-04 04:22:02 +0000448 u32 offset_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400449
SRICHARAN R00d328c2013-02-04 04:22:02 +0000450 volt_offset -= pmic->base_offset;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400451
SRICHARAN R00d328c2013-02-04 04:22:02 +0000452 offset_code = (volt_offset + pmic->step - 1) / pmic->step;
Nishanth Menona0f45c12012-03-01 14:17:38 +0000453
SRICHARAN R00d328c2013-02-04 04:22:02 +0000454 /*
455 * Offset codes 1-6 all give the base voltage in Palmas
456 * Offset code 0 switches OFF the SMPS
457 */
458 return offset_code + pmic->start_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400459}
460
SRICHARAN R00d328c2013-02-04 04:22:02 +0000461void do_scale_vcore(u32 vcore_reg, u32 volt_mv, struct pmic_data *pmic)
Aneesh V0d2628b2011-07-21 09:10:07 -0400462{
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000463 u32 offset_code;
Aneesh V0d2628b2011-07-21 09:10:07 -0400464 u32 offset = volt_mv;
SRICHARAN R00d328c2013-02-04 04:22:02 +0000465 int ret = 0;
466
467 /* See if we can first get the GPIO if needed */
468 if (pmic->gpio_en)
469 ret = gpio_request(pmic->gpio, "PMIC_GPIO");
470
471 if (ret < 0) {
472 printf("%s: gpio %d request failed %d\n", __func__,
473 pmic->gpio, ret);
474 return;
475 }
476
477 /* Pull the GPIO low to select SET0 register, while we program SET1 */
478 if (pmic->gpio_en)
479 gpio_direction_output(pmic->gpio, 0);
Aneesh V0d2628b2011-07-21 09:10:07 -0400480
481 /* convert to uV for better accuracy in the calculations */
482 offset *= 1000;
483
SRICHARAN R00d328c2013-02-04 04:22:02 +0000484 offset_code = get_offset_code(offset, pmic);
Aneesh V0d2628b2011-07-21 09:10:07 -0400485
486 debug("do_scale_vcore: volt - %d offset_code - 0x%x\n", volt_mv,
487 offset_code);
SRICHARAN R698a1f22012-03-12 02:25:38 +0000488
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000489 if (omap_vc_bypass_send_value(SMPS_I2C_SLAVE_ADDR,
490 vcore_reg, offset_code))
Aneesh V0d2628b2011-07-21 09:10:07 -0400491 printf("Scaling voltage failed for 0x%x\n", vcore_reg);
SRICHARAN R00d328c2013-02-04 04:22:02 +0000492
493 if (pmic->gpio_en)
494 gpio_direction_output(pmic->gpio, 1);
495}
496
497/*
498 * Setup the voltages for vdd_mpu, vdd_core, and vdd_iva
499 * We set the maximum voltages allowed here because Smart-Reflex is not
500 * enabled in bootloader. Voltage initialization in the kernel will set
501 * these to the nominal values after enabling Smart-Reflex
502 */
503void scale_vcores(struct vcores_data const *vcores)
504{
505 omap_vc_init(PRM_VC_I2C_CHANNEL_FREQ_KHZ);
506
507 do_scale_vcore(vcores->core.addr, vcores->core.value,
508 vcores->core.pmic);
509
510 do_scale_vcore(vcores->mpu.addr, vcores->mpu.value,
511 vcores->mpu.pmic);
512
513 do_scale_vcore(vcores->mm.addr, vcores->mm.value,
514 vcores->mm.pmic);
515
516 if (emif_sdram_type() == EMIF_SDRAM_TYPE_DDR3) {
517 /* Configure LDO SRAM "magic" bits */
518 writel(2, (*prcm)->prm_sldo_core_setup);
519 writel(2, (*prcm)->prm_sldo_mpu_setup);
520 writel(2, (*prcm)->prm_sldo_mm_setup);
521 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400522}
523
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000524static inline void enable_clock_domain(u32 const clkctrl_reg, u32 enable_mode)
Aneesh V0d2628b2011-07-21 09:10:07 -0400525{
526 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
527 enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000528 debug("Enable clock domain - %x\n", clkctrl_reg);
Aneesh V0d2628b2011-07-21 09:10:07 -0400529}
530
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000531static inline void wait_for_clk_enable(u32 clkctrl_addr)
Aneesh V0d2628b2011-07-21 09:10:07 -0400532{
533 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
534 u32 bound = LDELAY;
535
536 while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
537 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
538
539 clkctrl = readl(clkctrl_addr);
540 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
541 MODULE_CLKCTRL_IDLEST_SHIFT;
542 if (--bound == 0) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000543 printf("Clock enable failed for 0x%x idlest 0x%x\n",
Aneesh V0d2628b2011-07-21 09:10:07 -0400544 clkctrl_addr, clkctrl);
545 return;
546 }
547 }
548}
549
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000550static inline void enable_clock_module(u32 const clkctrl_addr, u32 enable_mode,
Aneesh V0d2628b2011-07-21 09:10:07 -0400551 u32 wait_for_enable)
552{
553 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
554 enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000555 debug("Enable clock module - %x\n", clkctrl_addr);
Aneesh V0d2628b2011-07-21 09:10:07 -0400556 if (wait_for_enable)
557 wait_for_clk_enable(clkctrl_addr);
558}
559
Aneesh V0d2628b2011-07-21 09:10:07 -0400560void freq_update_core(void)
561{
562 u32 freq_config1 = 0;
563 const struct dpll_params *core_dpll_params;
SRICHARAN R3d534962012-03-12 02:25:37 +0000564 u32 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400565
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000566 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400567 /* Put EMIF clock domain in sw wakeup mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000568 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
Aneesh V0d2628b2011-07-21 09:10:07 -0400569 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000570 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
571 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
Aneesh V0d2628b2011-07-21 09:10:07 -0400572
573 freq_config1 = SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK |
574 SHADOW_FREQ_CONFIG1_DLL_RESET_MASK;
575
576 freq_config1 |= (DPLL_EN_LOCK << SHADOW_FREQ_CONFIG1_DPLL_EN_SHIFT) &
577 SHADOW_FREQ_CONFIG1_DPLL_EN_MASK;
578
579 freq_config1 |= (core_dpll_params->m2 <<
580 SHADOW_FREQ_CONFIG1_M2_DIV_SHIFT) &
581 SHADOW_FREQ_CONFIG1_M2_DIV_MASK;
582
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000583 writel(freq_config1, (*prcm)->cm_shadow_freq_config1);
Aneesh V0d2628b2011-07-21 09:10:07 -0400584 if (!wait_on_value(SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK, 0,
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000585 (u32 *) (*prcm)->cm_shadow_freq_config1, LDELAY)) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400586 puts("FREQ UPDATE procedure failed!!");
587 hang();
588 }
589
SRICHARAN R3d534962012-03-12 02:25:37 +0000590 /*
591 * Putting EMIF in HW_AUTO is seen to be causing issues with
592 * EMIF clocks and the master DLL. Put EMIF in SW_WKUP
593 * in OMAP5430 ES1.0 silicon
594 */
595 if (omap_rev != OMAP5430_ES1_0) {
596 /* Put EMIF clock domain back in hw auto mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000597 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
SRICHARAN R3d534962012-03-12 02:25:37 +0000598 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000599 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
600 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
SRICHARAN R3d534962012-03-12 02:25:37 +0000601 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400602}
603
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000604void bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400605{
606 do_bypass_dpll(base);
607 wait_for_bypass(base);
608}
609
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000610void lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400611{
612 do_lock_dpll(base);
613 wait_for_lock(base);
614}
615
Aneesh Vb8e60b92011-07-21 09:10:21 -0400616void setup_clocks_for_console(void)
617{
618 /* Do not add any spl_debug prints in this function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000619 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400620 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
621 CD_CLKCTRL_CLKTRCTRL_SHIFT);
622
623 /* Enable all UARTs - console will be on one of them */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000624 clrsetbits_le32((*prcm)->cm_l4per_uart1_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400625 MODULE_CLKCTRL_MODULEMODE_MASK,
626 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
627 MODULE_CLKCTRL_MODULEMODE_SHIFT);
628
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000629 clrsetbits_le32((*prcm)->cm_l4per_uart2_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400630 MODULE_CLKCTRL_MODULEMODE_MASK,
631 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
632 MODULE_CLKCTRL_MODULEMODE_SHIFT);
633
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000634 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400635 MODULE_CLKCTRL_MODULEMODE_MASK,
636 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
637 MODULE_CLKCTRL_MODULEMODE_SHIFT);
638
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000639 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400640 MODULE_CLKCTRL_MODULEMODE_MASK,
641 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
642 MODULE_CLKCTRL_MODULEMODE_SHIFT);
643
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000644 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400645 CD_CLKCTRL_CLKTRCTRL_HW_AUTO <<
646 CD_CLKCTRL_CLKTRCTRL_SHIFT);
647}
648
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000649void do_enable_clocks(u32 const *clk_domains,
650 u32 const *clk_modules_hw_auto,
651 u32 const *clk_modules_explicit_en,
Sricharan9784f1f2011-11-15 09:49:58 -0500652 u8 wait_for_enable)
653{
654 u32 i, max = 100;
655
656 /* Put the clock domains in SW_WKUP mode */
657 for (i = 0; (i < max) && clk_domains[i]; i++) {
658 enable_clock_domain(clk_domains[i],
659 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
660 }
661
662 /* Clock modules that need to be put in HW_AUTO */
663 for (i = 0; (i < max) && clk_modules_hw_auto[i]; i++) {
664 enable_clock_module(clk_modules_hw_auto[i],
665 MODULE_CLKCTRL_MODULEMODE_HW_AUTO,
666 wait_for_enable);
667 };
668
669 /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
670 for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
671 enable_clock_module(clk_modules_explicit_en[i],
672 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
673 wait_for_enable);
674 };
675
676 /* Put the clock domains in HW_AUTO mode now */
677 for (i = 0; (i < max) && clk_domains[i]; i++) {
678 enable_clock_domain(clk_domains[i],
679 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
680 }
681}
682
Aneesh V0d2628b2011-07-21 09:10:07 -0400683void prcm_init(void)
684{
Sricharan9310ff72011-11-15 09:49:55 -0500685 switch (omap_hw_init_context()) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400686 case OMAP_INIT_CONTEXT_SPL:
687 case OMAP_INIT_CONTEXT_UBOOT_FROM_NOR:
688 case OMAP_INIT_CONTEXT_UBOOT_AFTER_CH:
Aneesh V9a390882011-07-21 09:29:29 -0400689 enable_basic_clocks();
SRICHARAN R00d328c2013-02-04 04:22:02 +0000690 scale_vcores(*omap_vcores);
Aneesh V0d2628b2011-07-21 09:10:07 -0400691 setup_dplls();
Sricharan308fe922011-11-15 09:50:03 -0500692#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400693 setup_non_essential_dplls();
694 enable_non_essential_clocks();
Sricharan308fe922011-11-15 09:50:03 -0500695#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400696 break;
697 default:
698 break;
699 }
Sricharan308fe922011-11-15 09:50:03 -0500700
701 if (OMAP_INIT_CONTEXT_SPL != omap_hw_init_context())
702 enable_basic_uboot_clocks();
Aneesh V0d2628b2011-07-21 09:10:07 -0400703}