blob: 818a96337e30ef023fb7d4843a7aa2d9a35ededb [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);
SRICHARAN Ra04ed142013-02-12 01:33:43 +0000106 if (params->h21 >= 0)
107 writel(params->h21, &dpll_regs->cm_div_h21_dpll);
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000108 if (params->h22 >= 0)
109 writel(params->h22, &dpll_regs->cm_div_h22_dpll);
110 if (params->h23 >= 0)
111 writel(params->h23, &dpll_regs->cm_div_h23_dpll);
SRICHARAN Ra04ed142013-02-12 01:33:43 +0000112 if (params->h24 >= 0)
113 writel(params->h24, &dpll_regs->cm_div_h24_dpll);
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000114}
115
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000116static inline void do_bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400117{
118 struct dpll_regs *dpll_regs = (struct dpll_regs *)base;
119
120 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
121 CM_CLKMODE_DPLL_DPLL_EN_MASK,
122 DPLL_EN_FAST_RELOCK_BYPASS <<
123 CM_CLKMODE_DPLL_EN_SHIFT);
124}
125
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000126static inline void wait_for_bypass(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400127{
128 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
129
130 if (!wait_on_value(ST_DPLL_CLK_MASK, 0, &dpll_regs->cm_idlest_dpll,
131 LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000132 printf("Bypassing DPLL failed %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400133 }
134}
135
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000136static inline void do_lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400137{
138 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
139
140 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
141 CM_CLKMODE_DPLL_DPLL_EN_MASK,
142 DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
143}
144
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000145static inline void wait_for_lock(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400146{
147 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
148
149 if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
150 &dpll_regs->cm_idlest_dpll, LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000151 printf("DPLL locking failed for %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400152 hang();
153 }
154}
155
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000156inline u32 check_for_lock(u32 const base)
Sricharan308fe922011-11-15 09:50:03 -0500157{
158 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
159 u32 lock = readl(&dpll_regs->cm_idlest_dpll) & ST_DPLL_CLK_MASK;
160
161 return lock;
162}
163
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000164const struct dpll_params *get_mpu_dpll_params(struct dplls const *dpll_data)
165{
166 u32 sysclk_ind = get_sys_clk_index();
167 return &dpll_data->mpu[sysclk_ind];
168}
169
170const struct dpll_params *get_core_dpll_params(struct dplls const *dpll_data)
171{
172 u32 sysclk_ind = get_sys_clk_index();
173 return &dpll_data->core[sysclk_ind];
174}
175
176const struct dpll_params *get_per_dpll_params(struct dplls const *dpll_data)
177{
178 u32 sysclk_ind = get_sys_clk_index();
179 return &dpll_data->per[sysclk_ind];
180}
181
182const struct dpll_params *get_iva_dpll_params(struct dplls const *dpll_data)
183{
184 u32 sysclk_ind = get_sys_clk_index();
185 return &dpll_data->iva[sysclk_ind];
186}
187
188const struct dpll_params *get_usb_dpll_params(struct dplls const *dpll_data)
189{
190 u32 sysclk_ind = get_sys_clk_index();
191 return &dpll_data->usb[sysclk_ind];
192}
193
194const struct dpll_params *get_abe_dpll_params(struct dplls const *dpll_data)
195{
196#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
197 u32 sysclk_ind = get_sys_clk_index();
198 return &dpll_data->abe[sysclk_ind];
199#else
200 return dpll_data->abe;
201#endif
202}
203
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000204static void do_setup_dpll(u32 const base, const struct dpll_params *params,
Sricharan308fe922011-11-15 09:50:03 -0500205 u8 lock, char *dpll)
Aneesh V0d2628b2011-07-21 09:10:07 -0400206{
Sricharan308fe922011-11-15 09:50:03 -0500207 u32 temp, M, N;
Aneesh V0d2628b2011-07-21 09:10:07 -0400208 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
209
Sricharan308fe922011-11-15 09:50:03 -0500210 temp = readl(&dpll_regs->cm_clksel_dpll);
211
212 if (check_for_lock(base)) {
213 /*
214 * The Dpll has already been locked by rom code using CH.
215 * Check if M,N are matching with Ideal nominal opp values.
216 * If matches, skip the rest otherwise relock.
217 */
218 M = (temp & CM_CLKSEL_DPLL_M_MASK) >> CM_CLKSEL_DPLL_M_SHIFT;
219 N = (temp & CM_CLKSEL_DPLL_N_MASK) >> CM_CLKSEL_DPLL_N_SHIFT;
220 if ((M != (params->m)) || (N != (params->n))) {
221 debug("\n %s Dpll locked, but not for ideal M = %d,"
222 "N = %d values, current values are M = %d,"
223 "N= %d" , dpll, params->m, params->n,
224 M, N);
225 } else {
226 /* Dpll locked with ideal values for nominal opps. */
227 debug("\n %s Dpll already locked with ideal"
228 "nominal opp values", dpll);
229 goto setup_post_dividers;
230 }
231 }
232
Aneesh V0d2628b2011-07-21 09:10:07 -0400233 bypass_dpll(base);
234
235 /* Set M & N */
Aneesh V0d2628b2011-07-21 09:10:07 -0400236 temp &= ~CM_CLKSEL_DPLL_M_MASK;
237 temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
238
239 temp &= ~CM_CLKSEL_DPLL_N_MASK;
240 temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
241
242 writel(temp, &dpll_regs->cm_clksel_dpll);
243
244 /* Lock */
245 if (lock)
246 do_lock_dpll(base);
247
Sricharan308fe922011-11-15 09:50:03 -0500248setup_post_dividers:
Sricharan9784f1f2011-11-15 09:49:58 -0500249 setup_post_dividers(base, params);
Aneesh V0d2628b2011-07-21 09:10:07 -0400250
251 /* Wait till the DPLL locks */
252 if (lock)
253 wait_for_lock(base);
254}
255
Sricharan9784f1f2011-11-15 09:49:58 -0500256u32 omap_ddr_clk(void)
Aneesh V0d2628b2011-07-21 09:10:07 -0400257{
Sricharan9784f1f2011-11-15 09:49:58 -0500258 u32 ddr_clk, sys_clk_khz, omap_rev, divider;
Aneesh V0d2628b2011-07-21 09:10:07 -0400259 const struct dpll_params *core_dpll_params;
260
Sricharan9784f1f2011-11-15 09:49:58 -0500261 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400262 sys_clk_khz = get_sys_clk_freq() / 1000;
263
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000264 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400265
266 debug("sys_clk %d\n ", sys_clk_khz * 1000);
267
268 /* Find Core DPLL locked frequency first */
269 ddr_clk = sys_clk_khz * 2 * core_dpll_params->m /
270 (core_dpll_params->n + 1);
Sricharan9784f1f2011-11-15 09:49:58 -0500271
272 if (omap_rev < OMAP5430_ES1_0) {
273 /*
274 * DDR frequency is PHY_ROOT_CLK/2
275 * PHY_ROOT_CLK = Fdpll/2/M2
276 */
277 divider = 4;
278 } else {
279 /*
280 * DDR frequency is PHY_ROOT_CLK
281 * PHY_ROOT_CLK = Fdpll/2/M2
282 */
283 divider = 2;
284 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400285
Sricharan9784f1f2011-11-15 09:49:58 -0500286 ddr_clk = ddr_clk / divider / core_dpll_params->m2;
Aneesh V0d2628b2011-07-21 09:10:07 -0400287 ddr_clk *= 1000; /* convert to Hz */
288 debug("ddr_clk %d\n ", ddr_clk);
289
290 return ddr_clk;
291}
292
Aneesh Va47a79f2011-07-21 09:29:36 -0400293/*
294 * Lock MPU dpll
295 *
296 * Resulting MPU frequencies:
297 * 4430 ES1.0 : 600 MHz
298 * 4430 ES2.x : 792 MHz (OPP Turbo)
299 * 4460 : 920 MHz (OPP Turbo) - DCC disabled
300 */
301void configure_mpu_dpll(void)
302{
303 const struct dpll_params *params;
304 struct dpll_regs *mpu_dpll_regs;
Sricharan9784f1f2011-11-15 09:49:58 -0500305 u32 omap_rev;
306 omap_rev = omap_revision();
Aneesh Va47a79f2011-07-21 09:29:36 -0400307
Sricharan9784f1f2011-11-15 09:49:58 -0500308 /*
309 * DCC and clock divider settings for 4460.
310 * DCC is required, if more than a certain frequency is required.
311 * For, 4460 > 1GHZ.
312 * 5430 > 1.4GHZ.
313 */
314 if ((omap_rev >= OMAP4460_ES1_0) && (omap_rev < OMAP5430_ES1_0)) {
Aneesh Va47a79f2011-07-21 09:29:36 -0400315 mpu_dpll_regs =
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000316 (struct dpll_regs *)((*prcm)->cm_clkmode_dpll_mpu);
317 bypass_dpll((*prcm)->cm_clkmode_dpll_mpu);
318 clrbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400319 MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000320 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400321 MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
322 clrbits_le32(&mpu_dpll_regs->cm_clksel_dpll,
323 CM_CLKSEL_DCC_EN_MASK);
324 }
325
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000326 params = get_mpu_dpll_params(*dplls_data);
Sricharan308fe922011-11-15 09:50:03 -0500327
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000328 do_setup_dpll((*prcm)->cm_clkmode_dpll_mpu, params, DPLL_LOCK, "mpu");
Aneesh Va47a79f2011-07-21 09:29:36 -0400329 debug("MPU DPLL locked\n");
330}
331
Govindraj.Rad4426b2012-02-06 03:55:36 +0000332#ifdef CONFIG_USB_EHCI_OMAP
333static void setup_usb_dpll(void)
334{
335 const struct dpll_params *params;
336 u32 sys_clk_khz, sd_div, num, den;
337
338 sys_clk_khz = get_sys_clk_freq() / 1000;
339 /*
340 * USB:
341 * USB dpll is J-type. Need to set DPLL_SD_DIV for jitter correction
342 * DPLL_SD_DIV = CEILING ([DPLL_MULT/(DPLL_DIV+1)]* CLKINP / 250)
343 * - where CLKINP is sys_clk in MHz
344 * Use CLKINP in KHz and adjust the denominator accordingly so
345 * that we have enough accuracy and at the same time no overflow
346 */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000347 params = get_usb_dpll_params(*dplls_data);
Govindraj.Rad4426b2012-02-06 03:55:36 +0000348 num = params->m * sys_clk_khz;
349 den = (params->n + 1) * 250 * 1000;
350 num += den - 1;
351 sd_div = num / den;
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000352 clrsetbits_le32((*prcm)->cm_clksel_dpll_usb,
Govindraj.Rad4426b2012-02-06 03:55:36 +0000353 CM_CLKSEL_DPLL_DPLL_SD_DIV_MASK,
354 sd_div << CM_CLKSEL_DPLL_DPLL_SD_DIV_SHIFT);
355
356 /* Now setup the dpll with the regular function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000357 do_setup_dpll((*prcm)->cm_clkmode_dpll_usb, params, DPLL_LOCK, "usb");
Govindraj.Rad4426b2012-02-06 03:55:36 +0000358}
359#endif
360
Aneesh V0d2628b2011-07-21 09:10:07 -0400361static void setup_dplls(void)
362{
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000363 u32 temp;
Aneesh V0d2628b2011-07-21 09:10:07 -0400364 const struct dpll_params *params;
Aneesh V0d2628b2011-07-21 09:10:07 -0400365
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000366 debug("setup_dplls\n");
Aneesh V0d2628b2011-07-21 09:10:07 -0400367
368 /* CORE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000369 params = get_core_dpll_params(*dplls_data); /* default - safest */
Aneesh V0d2628b2011-07-21 09:10:07 -0400370 /*
371 * Do not lock the core DPLL now. Just set it up.
372 * Core DPLL will be locked after setting up EMIF
373 * using the FREQ_UPDATE method(freq_update_core())
374 */
Lokesh Vutlafef54c32013-02-04 04:21:59 +0000375 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000376 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000377 DPLL_NO_LOCK, "core");
378 else
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000379 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000380 DPLL_LOCK, "core");
Aneesh V0d2628b2011-07-21 09:10:07 -0400381 /* Set the ratios for CORE_CLK, L3_CLK, L4_CLK */
382 temp = (CLKSEL_CORE_X2_DIV_1 << CLKSEL_CORE_SHIFT) |
383 (CLKSEL_L3_CORE_DIV_2 << CLKSEL_L3_SHIFT) |
384 (CLKSEL_L4_L3_DIV_2 << CLKSEL_L4_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000385 writel(temp, (*prcm)->cm_clksel_core);
Aneesh V0d2628b2011-07-21 09:10:07 -0400386 debug("Core DPLL configured\n");
387
388 /* lock PER dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000389 params = get_per_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000390 do_setup_dpll((*prcm)->cm_clkmode_dpll_per,
Sricharan308fe922011-11-15 09:50:03 -0500391 params, DPLL_LOCK, "per");
Aneesh V0d2628b2011-07-21 09:10:07 -0400392 debug("PER DPLL locked\n");
393
394 /* MPU dpll */
Aneesh Va47a79f2011-07-21 09:29:36 -0400395 configure_mpu_dpll();
Govindraj.Rad4426b2012-02-06 03:55:36 +0000396
397#ifdef CONFIG_USB_EHCI_OMAP
398 setup_usb_dpll();
399#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400400}
401
Sricharan308fe922011-11-15 09:50:03 -0500402#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400403static void setup_non_essential_dplls(void)
404{
Anatolij Gustschind75ffd42012-03-27 23:13:43 +0000405 u32 abe_ref_clk;
Aneesh V0d2628b2011-07-21 09:10:07 -0400406 const struct dpll_params *params;
407
Aneesh V0d2628b2011-07-21 09:10:07 -0400408 /* IVA */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000409 clrsetbits_le32((*prcm)->cm_bypclk_dpll_iva,
Aneesh V0d2628b2011-07-21 09:10:07 -0400410 CM_BYPCLK_DPLL_IVA_CLKSEL_MASK, DPLL_IVA_CLKSEL_CORE_X2_DIV_2);
411
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000412 params = get_iva_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000413 do_setup_dpll((*prcm)->cm_clkmode_dpll_iva, params, DPLL_LOCK, "iva");
Aneesh V0d2628b2011-07-21 09:10:07 -0400414
Sricharan9784f1f2011-11-15 09:49:58 -0500415 /* Configure ABE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000416 params = get_abe_dpll_params(*dplls_data);
Sricharan9784f1f2011-11-15 09:49:58 -0500417#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
Aneesh V0d2628b2011-07-21 09:10:07 -0400418 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_SYSCLK;
419#else
Aneesh V0d2628b2011-07-21 09:10:07 -0400420 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_32KCLK;
421 /*
422 * We need to enable some additional options to achieve
423 * 196.608MHz from 32768 Hz
424 */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000425 setbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400426 CM_CLKMODE_DPLL_DRIFTGUARD_EN_MASK|
427 CM_CLKMODE_DPLL_RELOCK_RAMP_EN_MASK|
428 CM_CLKMODE_DPLL_LPMODE_EN_MASK|
429 CM_CLKMODE_DPLL_REGM4XEN_MASK);
430 /* Spend 4 REFCLK cycles at each stage */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000431 clrsetbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400432 CM_CLKMODE_DPLL_RAMP_RATE_MASK,
433 1 << CM_CLKMODE_DPLL_RAMP_RATE_SHIFT);
434#endif
435
436 /* Select the right reference clk */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000437 clrsetbits_le32((*prcm)->cm_abe_pll_ref_clksel,
Aneesh V0d2628b2011-07-21 09:10:07 -0400438 CM_ABE_PLL_REF_CLKSEL_CLKSEL_MASK,
439 abe_ref_clk << CM_ABE_PLL_REF_CLKSEL_CLKSEL_SHIFT);
440 /* Lock the dpll */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000441 do_setup_dpll((*prcm)->cm_clkmode_dpll_abe, params, DPLL_LOCK, "abe");
Aneesh V0d2628b2011-07-21 09:10:07 -0400442}
Sricharan308fe922011-11-15 09:50:03 -0500443#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400444
SRICHARAN R00d328c2013-02-04 04:22:02 +0000445u32 get_offset_code(u32 volt_offset, struct pmic_data *pmic)
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400446{
SRICHARAN R00d328c2013-02-04 04:22:02 +0000447 u32 offset_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400448
SRICHARAN R00d328c2013-02-04 04:22:02 +0000449 volt_offset -= pmic->base_offset;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400450
SRICHARAN R00d328c2013-02-04 04:22:02 +0000451 offset_code = (volt_offset + pmic->step - 1) / pmic->step;
Nishanth Menona0f45c12012-03-01 14:17:38 +0000452
SRICHARAN R00d328c2013-02-04 04:22:02 +0000453 /*
454 * Offset codes 1-6 all give the base voltage in Palmas
455 * Offset code 0 switches OFF the SMPS
456 */
457 return offset_code + pmic->start_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400458}
459
SRICHARAN R00d328c2013-02-04 04:22:02 +0000460void do_scale_vcore(u32 vcore_reg, u32 volt_mv, struct pmic_data *pmic)
Aneesh V0d2628b2011-07-21 09:10:07 -0400461{
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000462 u32 offset_code;
Aneesh V0d2628b2011-07-21 09:10:07 -0400463 u32 offset = volt_mv;
SRICHARAN R00d328c2013-02-04 04:22:02 +0000464 int ret = 0;
465
466 /* See if we can first get the GPIO if needed */
467 if (pmic->gpio_en)
468 ret = gpio_request(pmic->gpio, "PMIC_GPIO");
469
470 if (ret < 0) {
471 printf("%s: gpio %d request failed %d\n", __func__,
472 pmic->gpio, ret);
473 return;
474 }
475
476 /* Pull the GPIO low to select SET0 register, while we program SET1 */
477 if (pmic->gpio_en)
478 gpio_direction_output(pmic->gpio, 0);
Aneesh V0d2628b2011-07-21 09:10:07 -0400479
480 /* convert to uV for better accuracy in the calculations */
481 offset *= 1000;
482
SRICHARAN R00d328c2013-02-04 04:22:02 +0000483 offset_code = get_offset_code(offset, pmic);
Aneesh V0d2628b2011-07-21 09:10:07 -0400484
485 debug("do_scale_vcore: volt - %d offset_code - 0x%x\n", volt_mv,
486 offset_code);
SRICHARAN R698a1f22012-03-12 02:25:38 +0000487
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000488 if (omap_vc_bypass_send_value(SMPS_I2C_SLAVE_ADDR,
489 vcore_reg, offset_code))
Aneesh V0d2628b2011-07-21 09:10:07 -0400490 printf("Scaling voltage failed for 0x%x\n", vcore_reg);
SRICHARAN R00d328c2013-02-04 04:22:02 +0000491
492 if (pmic->gpio_en)
493 gpio_direction_output(pmic->gpio, 1);
494}
495
496/*
497 * Setup the voltages for vdd_mpu, vdd_core, and vdd_iva
498 * We set the maximum voltages allowed here because Smart-Reflex is not
499 * enabled in bootloader. Voltage initialization in the kernel will set
500 * these to the nominal values after enabling Smart-Reflex
501 */
502void scale_vcores(struct vcores_data const *vcores)
503{
504 omap_vc_init(PRM_VC_I2C_CHANNEL_FREQ_KHZ);
505
506 do_scale_vcore(vcores->core.addr, vcores->core.value,
507 vcores->core.pmic);
508
509 do_scale_vcore(vcores->mpu.addr, vcores->mpu.value,
510 vcores->mpu.pmic);
511
512 do_scale_vcore(vcores->mm.addr, vcores->mm.value,
513 vcores->mm.pmic);
514
515 if (emif_sdram_type() == EMIF_SDRAM_TYPE_DDR3) {
516 /* Configure LDO SRAM "magic" bits */
517 writel(2, (*prcm)->prm_sldo_core_setup);
518 writel(2, (*prcm)->prm_sldo_mpu_setup);
519 writel(2, (*prcm)->prm_sldo_mm_setup);
520 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400521}
522
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000523static inline void enable_clock_domain(u32 const clkctrl_reg, u32 enable_mode)
Aneesh V0d2628b2011-07-21 09:10:07 -0400524{
525 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
526 enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000527 debug("Enable clock domain - %x\n", clkctrl_reg);
Aneesh V0d2628b2011-07-21 09:10:07 -0400528}
529
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000530static inline void wait_for_clk_enable(u32 clkctrl_addr)
Aneesh V0d2628b2011-07-21 09:10:07 -0400531{
532 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
533 u32 bound = LDELAY;
534
535 while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
536 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
537
538 clkctrl = readl(clkctrl_addr);
539 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
540 MODULE_CLKCTRL_IDLEST_SHIFT;
541 if (--bound == 0) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000542 printf("Clock enable failed for 0x%x idlest 0x%x\n",
Aneesh V0d2628b2011-07-21 09:10:07 -0400543 clkctrl_addr, clkctrl);
544 return;
545 }
546 }
547}
548
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000549static inline void enable_clock_module(u32 const clkctrl_addr, u32 enable_mode,
Aneesh V0d2628b2011-07-21 09:10:07 -0400550 u32 wait_for_enable)
551{
552 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
553 enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000554 debug("Enable clock module - %x\n", clkctrl_addr);
Aneesh V0d2628b2011-07-21 09:10:07 -0400555 if (wait_for_enable)
556 wait_for_clk_enable(clkctrl_addr);
557}
558
Aneesh V0d2628b2011-07-21 09:10:07 -0400559void freq_update_core(void)
560{
561 u32 freq_config1 = 0;
562 const struct dpll_params *core_dpll_params;
SRICHARAN R3d534962012-03-12 02:25:37 +0000563 u32 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400564
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000565 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400566 /* Put EMIF clock domain in sw wakeup mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000567 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
Aneesh V0d2628b2011-07-21 09:10:07 -0400568 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000569 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
570 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
Aneesh V0d2628b2011-07-21 09:10:07 -0400571
572 freq_config1 = SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK |
573 SHADOW_FREQ_CONFIG1_DLL_RESET_MASK;
574
575 freq_config1 |= (DPLL_EN_LOCK << SHADOW_FREQ_CONFIG1_DPLL_EN_SHIFT) &
576 SHADOW_FREQ_CONFIG1_DPLL_EN_MASK;
577
578 freq_config1 |= (core_dpll_params->m2 <<
579 SHADOW_FREQ_CONFIG1_M2_DIV_SHIFT) &
580 SHADOW_FREQ_CONFIG1_M2_DIV_MASK;
581
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000582 writel(freq_config1, (*prcm)->cm_shadow_freq_config1);
Aneesh V0d2628b2011-07-21 09:10:07 -0400583 if (!wait_on_value(SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK, 0,
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000584 (u32 *) (*prcm)->cm_shadow_freq_config1, LDELAY)) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400585 puts("FREQ UPDATE procedure failed!!");
586 hang();
587 }
588
SRICHARAN R3d534962012-03-12 02:25:37 +0000589 /*
590 * Putting EMIF in HW_AUTO is seen to be causing issues with
591 * EMIF clocks and the master DLL. Put EMIF in SW_WKUP
592 * in OMAP5430 ES1.0 silicon
593 */
594 if (omap_rev != OMAP5430_ES1_0) {
595 /* Put EMIF clock domain back in hw auto mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000596 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
SRICHARAN R3d534962012-03-12 02:25:37 +0000597 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000598 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
599 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
SRICHARAN R3d534962012-03-12 02:25:37 +0000600 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400601}
602
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000603void bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400604{
605 do_bypass_dpll(base);
606 wait_for_bypass(base);
607}
608
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000609void lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400610{
611 do_lock_dpll(base);
612 wait_for_lock(base);
613}
614
Aneesh Vb8e60b92011-07-21 09:10:21 -0400615void setup_clocks_for_console(void)
616{
617 /* Do not add any spl_debug prints in this function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000618 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400619 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
620 CD_CLKCTRL_CLKTRCTRL_SHIFT);
621
622 /* Enable all UARTs - console will be on one of them */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000623 clrsetbits_le32((*prcm)->cm_l4per_uart1_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400624 MODULE_CLKCTRL_MODULEMODE_MASK,
625 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
626 MODULE_CLKCTRL_MODULEMODE_SHIFT);
627
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000628 clrsetbits_le32((*prcm)->cm_l4per_uart2_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400629 MODULE_CLKCTRL_MODULEMODE_MASK,
630 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
631 MODULE_CLKCTRL_MODULEMODE_SHIFT);
632
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000633 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400634 MODULE_CLKCTRL_MODULEMODE_MASK,
635 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
636 MODULE_CLKCTRL_MODULEMODE_SHIFT);
637
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000638 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400639 MODULE_CLKCTRL_MODULEMODE_MASK,
640 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
641 MODULE_CLKCTRL_MODULEMODE_SHIFT);
642
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000643 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400644 CD_CLKCTRL_CLKTRCTRL_HW_AUTO <<
645 CD_CLKCTRL_CLKTRCTRL_SHIFT);
646}
647
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000648void do_enable_clocks(u32 const *clk_domains,
649 u32 const *clk_modules_hw_auto,
650 u32 const *clk_modules_explicit_en,
Sricharan9784f1f2011-11-15 09:49:58 -0500651 u8 wait_for_enable)
652{
653 u32 i, max = 100;
654
655 /* Put the clock domains in SW_WKUP mode */
656 for (i = 0; (i < max) && clk_domains[i]; i++) {
657 enable_clock_domain(clk_domains[i],
658 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
659 }
660
661 /* Clock modules that need to be put in HW_AUTO */
662 for (i = 0; (i < max) && clk_modules_hw_auto[i]; i++) {
663 enable_clock_module(clk_modules_hw_auto[i],
664 MODULE_CLKCTRL_MODULEMODE_HW_AUTO,
665 wait_for_enable);
666 };
667
668 /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
669 for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
670 enable_clock_module(clk_modules_explicit_en[i],
671 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
672 wait_for_enable);
673 };
674
675 /* Put the clock domains in HW_AUTO mode now */
676 for (i = 0; (i < max) && clk_domains[i]; i++) {
677 enable_clock_domain(clk_domains[i],
678 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
679 }
680}
681
Aneesh V0d2628b2011-07-21 09:10:07 -0400682void prcm_init(void)
683{
Sricharan9310ff72011-11-15 09:49:55 -0500684 switch (omap_hw_init_context()) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400685 case OMAP_INIT_CONTEXT_SPL:
686 case OMAP_INIT_CONTEXT_UBOOT_FROM_NOR:
687 case OMAP_INIT_CONTEXT_UBOOT_AFTER_CH:
Aneesh V9a390882011-07-21 09:29:29 -0400688 enable_basic_clocks();
SRICHARAN R00d328c2013-02-04 04:22:02 +0000689 scale_vcores(*omap_vcores);
Aneesh V0d2628b2011-07-21 09:10:07 -0400690 setup_dplls();
Sricharan308fe922011-11-15 09:50:03 -0500691#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400692 setup_non_essential_dplls();
693 enable_non_essential_clocks();
Sricharan308fe922011-11-15 09:50:03 -0500694#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400695 break;
696 default:
697 break;
698 }
Sricharan308fe922011-11-15 09:50:03 -0500699
700 if (OMAP_INIT_CONTEXT_SPL != omap_hw_init_context())
701 enable_basic_uboot_clocks();
Aneesh V0d2628b2011-07-21 09:10:07 -0400702}