blob: 9ed18995ead3f1b4b8cff20b88f672b32807423c [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 */
Lokesh Vutla5e70e292013-02-12 21:29:05 +000058 20000000, /* 20 MHz */
SRICHARAN R1a79cab2013-02-04 04:22:01 +000059};
60
Aneesh V0d2628b2011-07-21 09:10:07 -040061static inline u32 __get_sys_clk_index(void)
62{
Lokesh Vutla5e70e292013-02-12 21:29:05 +000063 s8 ind;
Aneesh V0d2628b2011-07-21 09:10:07 -040064 /*
65 * For ES1 the ROM code calibration of sys clock is not reliable
66 * due to hw issue. So, use hard-coded value. If this value is not
67 * correct for any board over-ride this function in board file
68 * From ES2.0 onwards you will get this information from
69 * CM_SYS_CLKSEL
70 */
71 if (omap_revision() == OMAP4430_ES1_0)
72 ind = OMAP_SYS_CLK_IND_38_4_MHZ;
73 else {
74 /* SYS_CLKSEL - 1 to match the dpll param array indices */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +000075 ind = (readl((*prcm)->cm_sys_clksel) &
Aneesh V0d2628b2011-07-21 09:10:07 -040076 CM_SYS_CLKSEL_SYS_CLKSEL_MASK) - 1;
Lokesh Vutla5e70e292013-02-12 21:29:05 +000077 /*
78 * SYS_CLKSEL value for 20MHz is 0. This is introduced newly
79 * in DRA7XX socs. SYS_CLKSEL -1 will be greater than
80 * NUM_SYS_CLK. So considering the last 3 bits as the index
81 * for the dpll param array.
82 */
83 ind &= CM_SYS_CLKSEL_SYS_CLKSEL_MASK;
Aneesh V0d2628b2011-07-21 09:10:07 -040084 }
85 return ind;
86}
87
88u32 get_sys_clk_index(void)
89 __attribute__ ((weak, alias("__get_sys_clk_index")));
90
91u32 get_sys_clk_freq(void)
92{
93 u8 index = get_sys_clk_index();
94 return sys_clk_array[index];
95}
96
SRICHARAN R1a79cab2013-02-04 04:22:01 +000097void setup_post_dividers(u32 const base, const struct dpll_params *params)
98{
99 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
100
101 /* Setup post-dividers */
102 if (params->m2 >= 0)
103 writel(params->m2, &dpll_regs->cm_div_m2_dpll);
104 if (params->m3 >= 0)
105 writel(params->m3, &dpll_regs->cm_div_m3_dpll);
106 if (params->m4_h11 >= 0)
107 writel(params->m4_h11, &dpll_regs->cm_div_m4_h11_dpll);
108 if (params->m5_h12 >= 0)
109 writel(params->m5_h12, &dpll_regs->cm_div_m5_h12_dpll);
110 if (params->m6_h13 >= 0)
111 writel(params->m6_h13, &dpll_regs->cm_div_m6_h13_dpll);
112 if (params->m7_h14 >= 0)
113 writel(params->m7_h14, &dpll_regs->cm_div_m7_h14_dpll);
SRICHARAN Ra04ed142013-02-12 01:33:43 +0000114 if (params->h21 >= 0)
115 writel(params->h21, &dpll_regs->cm_div_h21_dpll);
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000116 if (params->h22 >= 0)
117 writel(params->h22, &dpll_regs->cm_div_h22_dpll);
118 if (params->h23 >= 0)
119 writel(params->h23, &dpll_regs->cm_div_h23_dpll);
SRICHARAN Ra04ed142013-02-12 01:33:43 +0000120 if (params->h24 >= 0)
121 writel(params->h24, &dpll_regs->cm_div_h24_dpll);
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000122}
123
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000124static inline void do_bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400125{
126 struct dpll_regs *dpll_regs = (struct dpll_regs *)base;
127
128 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
129 CM_CLKMODE_DPLL_DPLL_EN_MASK,
130 DPLL_EN_FAST_RELOCK_BYPASS <<
131 CM_CLKMODE_DPLL_EN_SHIFT);
132}
133
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000134static inline void wait_for_bypass(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400135{
136 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
137
138 if (!wait_on_value(ST_DPLL_CLK_MASK, 0, &dpll_regs->cm_idlest_dpll,
139 LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000140 printf("Bypassing DPLL failed %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400141 }
142}
143
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000144static inline void do_lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400145{
146 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
147
148 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
149 CM_CLKMODE_DPLL_DPLL_EN_MASK,
150 DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
151}
152
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000153static inline void wait_for_lock(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400154{
155 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
156
157 if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
158 &dpll_regs->cm_idlest_dpll, LDELAY)) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000159 printf("DPLL locking failed for %x\n", base);
Aneesh V0d2628b2011-07-21 09:10:07 -0400160 hang();
161 }
162}
163
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000164inline u32 check_for_lock(u32 const base)
Sricharan308fe922011-11-15 09:50:03 -0500165{
166 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
167 u32 lock = readl(&dpll_regs->cm_idlest_dpll) & ST_DPLL_CLK_MASK;
168
169 return lock;
170}
171
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000172const struct dpll_params *get_mpu_dpll_params(struct dplls const *dpll_data)
173{
174 u32 sysclk_ind = get_sys_clk_index();
175 return &dpll_data->mpu[sysclk_ind];
176}
177
178const struct dpll_params *get_core_dpll_params(struct dplls const *dpll_data)
179{
180 u32 sysclk_ind = get_sys_clk_index();
181 return &dpll_data->core[sysclk_ind];
182}
183
184const struct dpll_params *get_per_dpll_params(struct dplls const *dpll_data)
185{
186 u32 sysclk_ind = get_sys_clk_index();
187 return &dpll_data->per[sysclk_ind];
188}
189
190const struct dpll_params *get_iva_dpll_params(struct dplls const *dpll_data)
191{
192 u32 sysclk_ind = get_sys_clk_index();
193 return &dpll_data->iva[sysclk_ind];
194}
195
196const struct dpll_params *get_usb_dpll_params(struct dplls const *dpll_data)
197{
198 u32 sysclk_ind = get_sys_clk_index();
199 return &dpll_data->usb[sysclk_ind];
200}
201
202const struct dpll_params *get_abe_dpll_params(struct dplls const *dpll_data)
203{
204#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
205 u32 sysclk_ind = get_sys_clk_index();
206 return &dpll_data->abe[sysclk_ind];
207#else
208 return dpll_data->abe;
209#endif
210}
211
Lokesh Vutla5e70e292013-02-12 21:29:05 +0000212static const struct dpll_params *get_ddr_dpll_params
213 (struct dplls const *dpll_data)
214{
215 u32 sysclk_ind = get_sys_clk_index();
216
217 if (!dpll_data->ddr)
218 return NULL;
219 return &dpll_data->ddr[sysclk_ind];
220}
221
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000222static void do_setup_dpll(u32 const base, const struct dpll_params *params,
Sricharan308fe922011-11-15 09:50:03 -0500223 u8 lock, char *dpll)
Aneesh V0d2628b2011-07-21 09:10:07 -0400224{
Sricharan308fe922011-11-15 09:50:03 -0500225 u32 temp, M, N;
Aneesh V0d2628b2011-07-21 09:10:07 -0400226 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
227
Lokesh Vutla5e70e292013-02-12 21:29:05 +0000228 if (!params)
229 return;
230
Sricharan308fe922011-11-15 09:50:03 -0500231 temp = readl(&dpll_regs->cm_clksel_dpll);
232
233 if (check_for_lock(base)) {
234 /*
235 * The Dpll has already been locked by rom code using CH.
236 * Check if M,N are matching with Ideal nominal opp values.
237 * If matches, skip the rest otherwise relock.
238 */
239 M = (temp & CM_CLKSEL_DPLL_M_MASK) >> CM_CLKSEL_DPLL_M_SHIFT;
240 N = (temp & CM_CLKSEL_DPLL_N_MASK) >> CM_CLKSEL_DPLL_N_SHIFT;
241 if ((M != (params->m)) || (N != (params->n))) {
242 debug("\n %s Dpll locked, but not for ideal M = %d,"
243 "N = %d values, current values are M = %d,"
244 "N= %d" , dpll, params->m, params->n,
245 M, N);
246 } else {
247 /* Dpll locked with ideal values for nominal opps. */
248 debug("\n %s Dpll already locked with ideal"
249 "nominal opp values", dpll);
250 goto setup_post_dividers;
251 }
252 }
253
Aneesh V0d2628b2011-07-21 09:10:07 -0400254 bypass_dpll(base);
255
256 /* Set M & N */
Aneesh V0d2628b2011-07-21 09:10:07 -0400257 temp &= ~CM_CLKSEL_DPLL_M_MASK;
258 temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
259
260 temp &= ~CM_CLKSEL_DPLL_N_MASK;
261 temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
262
263 writel(temp, &dpll_regs->cm_clksel_dpll);
264
265 /* Lock */
266 if (lock)
267 do_lock_dpll(base);
268
Sricharan308fe922011-11-15 09:50:03 -0500269setup_post_dividers:
Sricharan9784f1f2011-11-15 09:49:58 -0500270 setup_post_dividers(base, params);
Aneesh V0d2628b2011-07-21 09:10:07 -0400271
272 /* Wait till the DPLL locks */
273 if (lock)
274 wait_for_lock(base);
275}
276
Sricharan9784f1f2011-11-15 09:49:58 -0500277u32 omap_ddr_clk(void)
Aneesh V0d2628b2011-07-21 09:10:07 -0400278{
Sricharan9784f1f2011-11-15 09:49:58 -0500279 u32 ddr_clk, sys_clk_khz, omap_rev, divider;
Aneesh V0d2628b2011-07-21 09:10:07 -0400280 const struct dpll_params *core_dpll_params;
281
Sricharan9784f1f2011-11-15 09:49:58 -0500282 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400283 sys_clk_khz = get_sys_clk_freq() / 1000;
284
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000285 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400286
287 debug("sys_clk %d\n ", sys_clk_khz * 1000);
288
289 /* Find Core DPLL locked frequency first */
290 ddr_clk = sys_clk_khz * 2 * core_dpll_params->m /
291 (core_dpll_params->n + 1);
Sricharan9784f1f2011-11-15 09:49:58 -0500292
293 if (omap_rev < OMAP5430_ES1_0) {
294 /*
295 * DDR frequency is PHY_ROOT_CLK/2
296 * PHY_ROOT_CLK = Fdpll/2/M2
297 */
298 divider = 4;
299 } else {
300 /*
301 * DDR frequency is PHY_ROOT_CLK
302 * PHY_ROOT_CLK = Fdpll/2/M2
303 */
304 divider = 2;
305 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400306
Sricharan9784f1f2011-11-15 09:49:58 -0500307 ddr_clk = ddr_clk / divider / core_dpll_params->m2;
Aneesh V0d2628b2011-07-21 09:10:07 -0400308 ddr_clk *= 1000; /* convert to Hz */
309 debug("ddr_clk %d\n ", ddr_clk);
310
311 return ddr_clk;
312}
313
Aneesh Va47a79f2011-07-21 09:29:36 -0400314/*
315 * Lock MPU dpll
316 *
317 * Resulting MPU frequencies:
318 * 4430 ES1.0 : 600 MHz
319 * 4430 ES2.x : 792 MHz (OPP Turbo)
320 * 4460 : 920 MHz (OPP Turbo) - DCC disabled
321 */
322void configure_mpu_dpll(void)
323{
324 const struct dpll_params *params;
325 struct dpll_regs *mpu_dpll_regs;
Sricharan9784f1f2011-11-15 09:49:58 -0500326 u32 omap_rev;
327 omap_rev = omap_revision();
Aneesh Va47a79f2011-07-21 09:29:36 -0400328
Sricharan9784f1f2011-11-15 09:49:58 -0500329 /*
330 * DCC and clock divider settings for 4460.
331 * DCC is required, if more than a certain frequency is required.
332 * For, 4460 > 1GHZ.
333 * 5430 > 1.4GHZ.
334 */
335 if ((omap_rev >= OMAP4460_ES1_0) && (omap_rev < OMAP5430_ES1_0)) {
Aneesh Va47a79f2011-07-21 09:29:36 -0400336 mpu_dpll_regs =
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000337 (struct dpll_regs *)((*prcm)->cm_clkmode_dpll_mpu);
338 bypass_dpll((*prcm)->cm_clkmode_dpll_mpu);
339 clrbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400340 MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000341 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Va47a79f2011-07-21 09:29:36 -0400342 MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
343 clrbits_le32(&mpu_dpll_regs->cm_clksel_dpll,
344 CM_CLKSEL_DCC_EN_MASK);
345 }
346
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000347 params = get_mpu_dpll_params(*dplls_data);
Sricharan308fe922011-11-15 09:50:03 -0500348
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000349 do_setup_dpll((*prcm)->cm_clkmode_dpll_mpu, params, DPLL_LOCK, "mpu");
Aneesh Va47a79f2011-07-21 09:29:36 -0400350 debug("MPU DPLL locked\n");
351}
352
Govindraj.Rad4426b2012-02-06 03:55:36 +0000353#ifdef CONFIG_USB_EHCI_OMAP
354static void setup_usb_dpll(void)
355{
356 const struct dpll_params *params;
357 u32 sys_clk_khz, sd_div, num, den;
358
359 sys_clk_khz = get_sys_clk_freq() / 1000;
360 /*
361 * USB:
362 * USB dpll is J-type. Need to set DPLL_SD_DIV for jitter correction
363 * DPLL_SD_DIV = CEILING ([DPLL_MULT/(DPLL_DIV+1)]* CLKINP / 250)
364 * - where CLKINP is sys_clk in MHz
365 * Use CLKINP in KHz and adjust the denominator accordingly so
366 * that we have enough accuracy and at the same time no overflow
367 */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000368 params = get_usb_dpll_params(*dplls_data);
Govindraj.Rad4426b2012-02-06 03:55:36 +0000369 num = params->m * sys_clk_khz;
370 den = (params->n + 1) * 250 * 1000;
371 num += den - 1;
372 sd_div = num / den;
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000373 clrsetbits_le32((*prcm)->cm_clksel_dpll_usb,
Govindraj.Rad4426b2012-02-06 03:55:36 +0000374 CM_CLKSEL_DPLL_DPLL_SD_DIV_MASK,
375 sd_div << CM_CLKSEL_DPLL_DPLL_SD_DIV_SHIFT);
376
377 /* Now setup the dpll with the regular function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000378 do_setup_dpll((*prcm)->cm_clkmode_dpll_usb, params, DPLL_LOCK, "usb");
Govindraj.Rad4426b2012-02-06 03:55:36 +0000379}
380#endif
381
Aneesh V0d2628b2011-07-21 09:10:07 -0400382static void setup_dplls(void)
383{
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000384 u32 temp;
Aneesh V0d2628b2011-07-21 09:10:07 -0400385 const struct dpll_params *params;
Aneesh V0d2628b2011-07-21 09:10:07 -0400386
Anatolij Gustschin20f23512011-12-03 06:46:14 +0000387 debug("setup_dplls\n");
Aneesh V0d2628b2011-07-21 09:10:07 -0400388
389 /* CORE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000390 params = get_core_dpll_params(*dplls_data); /* default - safest */
Aneesh V0d2628b2011-07-21 09:10:07 -0400391 /*
392 * Do not lock the core DPLL now. Just set it up.
393 * Core DPLL will be locked after setting up EMIF
394 * using the FREQ_UPDATE method(freq_update_core())
395 */
Lokesh Vutlafef54c32013-02-04 04:21:59 +0000396 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000397 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000398 DPLL_NO_LOCK, "core");
399 else
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000400 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +0000401 DPLL_LOCK, "core");
Aneesh V0d2628b2011-07-21 09:10:07 -0400402 /* Set the ratios for CORE_CLK, L3_CLK, L4_CLK */
403 temp = (CLKSEL_CORE_X2_DIV_1 << CLKSEL_CORE_SHIFT) |
404 (CLKSEL_L3_CORE_DIV_2 << CLKSEL_L3_SHIFT) |
405 (CLKSEL_L4_L3_DIV_2 << CLKSEL_L4_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000406 writel(temp, (*prcm)->cm_clksel_core);
Aneesh V0d2628b2011-07-21 09:10:07 -0400407 debug("Core DPLL configured\n");
408
409 /* lock PER dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000410 params = get_per_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000411 do_setup_dpll((*prcm)->cm_clkmode_dpll_per,
Sricharan308fe922011-11-15 09:50:03 -0500412 params, DPLL_LOCK, "per");
Aneesh V0d2628b2011-07-21 09:10:07 -0400413 debug("PER DPLL locked\n");
414
415 /* MPU dpll */
Aneesh Va47a79f2011-07-21 09:29:36 -0400416 configure_mpu_dpll();
Govindraj.Rad4426b2012-02-06 03:55:36 +0000417
418#ifdef CONFIG_USB_EHCI_OMAP
419 setup_usb_dpll();
420#endif
Lokesh Vutla5e70e292013-02-12 21:29:05 +0000421 params = get_ddr_dpll_params(*dplls_data);
422 do_setup_dpll((*prcm)->cm_clkmode_dpll_ddrphy,
423 params, DPLL_LOCK, "ddr");
Aneesh V0d2628b2011-07-21 09:10:07 -0400424}
425
Sricharan308fe922011-11-15 09:50:03 -0500426#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400427static void setup_non_essential_dplls(void)
428{
Anatolij Gustschind75ffd42012-03-27 23:13:43 +0000429 u32 abe_ref_clk;
Aneesh V0d2628b2011-07-21 09:10:07 -0400430 const struct dpll_params *params;
431
Aneesh V0d2628b2011-07-21 09:10:07 -0400432 /* IVA */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000433 clrsetbits_le32((*prcm)->cm_bypclk_dpll_iva,
Aneesh V0d2628b2011-07-21 09:10:07 -0400434 CM_BYPCLK_DPLL_IVA_CLKSEL_MASK, DPLL_IVA_CLKSEL_CORE_X2_DIV_2);
435
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000436 params = get_iva_dpll_params(*dplls_data);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000437 do_setup_dpll((*prcm)->cm_clkmode_dpll_iva, params, DPLL_LOCK, "iva");
Aneesh V0d2628b2011-07-21 09:10:07 -0400438
Sricharan9784f1f2011-11-15 09:49:58 -0500439 /* Configure ABE dpll */
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000440 params = get_abe_dpll_params(*dplls_data);
Sricharan9784f1f2011-11-15 09:49:58 -0500441#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
Aneesh V0d2628b2011-07-21 09:10:07 -0400442 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_SYSCLK;
443#else
Aneesh V0d2628b2011-07-21 09:10:07 -0400444 abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_32KCLK;
445 /*
446 * We need to enable some additional options to achieve
447 * 196.608MHz from 32768 Hz
448 */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000449 setbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400450 CM_CLKMODE_DPLL_DRIFTGUARD_EN_MASK|
451 CM_CLKMODE_DPLL_RELOCK_RAMP_EN_MASK|
452 CM_CLKMODE_DPLL_LPMODE_EN_MASK|
453 CM_CLKMODE_DPLL_REGM4XEN_MASK);
454 /* Spend 4 REFCLK cycles at each stage */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000455 clrsetbits_le32((*prcm)->cm_clkmode_dpll_abe,
Aneesh V0d2628b2011-07-21 09:10:07 -0400456 CM_CLKMODE_DPLL_RAMP_RATE_MASK,
457 1 << CM_CLKMODE_DPLL_RAMP_RATE_SHIFT);
458#endif
459
460 /* Select the right reference clk */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000461 clrsetbits_le32((*prcm)->cm_abe_pll_ref_clksel,
Aneesh V0d2628b2011-07-21 09:10:07 -0400462 CM_ABE_PLL_REF_CLKSEL_CLKSEL_MASK,
463 abe_ref_clk << CM_ABE_PLL_REF_CLKSEL_CLKSEL_SHIFT);
464 /* Lock the dpll */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000465 do_setup_dpll((*prcm)->cm_clkmode_dpll_abe, params, DPLL_LOCK, "abe");
Aneesh V0d2628b2011-07-21 09:10:07 -0400466}
Sricharan308fe922011-11-15 09:50:03 -0500467#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400468
SRICHARAN R00d328c2013-02-04 04:22:02 +0000469u32 get_offset_code(u32 volt_offset, struct pmic_data *pmic)
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400470{
SRICHARAN R00d328c2013-02-04 04:22:02 +0000471 u32 offset_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400472
SRICHARAN R00d328c2013-02-04 04:22:02 +0000473 volt_offset -= pmic->base_offset;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400474
SRICHARAN R00d328c2013-02-04 04:22:02 +0000475 offset_code = (volt_offset + pmic->step - 1) / pmic->step;
Nishanth Menona0f45c12012-03-01 14:17:38 +0000476
SRICHARAN R00d328c2013-02-04 04:22:02 +0000477 /*
478 * Offset codes 1-6 all give the base voltage in Palmas
479 * Offset code 0 switches OFF the SMPS
480 */
481 return offset_code + pmic->start_code;
Aneesh V0fa1d1b2011-07-21 09:29:32 -0400482}
483
SRICHARAN R00d328c2013-02-04 04:22:02 +0000484void do_scale_vcore(u32 vcore_reg, u32 volt_mv, struct pmic_data *pmic)
Aneesh V0d2628b2011-07-21 09:10:07 -0400485{
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000486 u32 offset_code;
Aneesh V0d2628b2011-07-21 09:10:07 -0400487 u32 offset = volt_mv;
SRICHARAN R00d328c2013-02-04 04:22:02 +0000488 int ret = 0;
489
490 /* See if we can first get the GPIO if needed */
491 if (pmic->gpio_en)
492 ret = gpio_request(pmic->gpio, "PMIC_GPIO");
493
494 if (ret < 0) {
495 printf("%s: gpio %d request failed %d\n", __func__,
496 pmic->gpio, ret);
497 return;
498 }
499
500 /* Pull the GPIO low to select SET0 register, while we program SET1 */
501 if (pmic->gpio_en)
502 gpio_direction_output(pmic->gpio, 0);
Aneesh V0d2628b2011-07-21 09:10:07 -0400503
504 /* convert to uV for better accuracy in the calculations */
505 offset *= 1000;
506
SRICHARAN R00d328c2013-02-04 04:22:02 +0000507 offset_code = get_offset_code(offset, pmic);
Aneesh V0d2628b2011-07-21 09:10:07 -0400508
509 debug("do_scale_vcore: volt - %d offset_code - 0x%x\n", volt_mv,
510 offset_code);
SRICHARAN R698a1f22012-03-12 02:25:38 +0000511
Nishanth Menon41d7ab12012-03-01 14:17:37 +0000512 if (omap_vc_bypass_send_value(SMPS_I2C_SLAVE_ADDR,
513 vcore_reg, offset_code))
Aneesh V0d2628b2011-07-21 09:10:07 -0400514 printf("Scaling voltage failed for 0x%x\n", vcore_reg);
SRICHARAN R00d328c2013-02-04 04:22:02 +0000515
516 if (pmic->gpio_en)
517 gpio_direction_output(pmic->gpio, 1);
518}
519
520/*
521 * Setup the voltages for vdd_mpu, vdd_core, and vdd_iva
522 * We set the maximum voltages allowed here because Smart-Reflex is not
523 * enabled in bootloader. Voltage initialization in the kernel will set
524 * these to the nominal values after enabling Smart-Reflex
525 */
526void scale_vcores(struct vcores_data const *vcores)
527{
528 omap_vc_init(PRM_VC_I2C_CHANNEL_FREQ_KHZ);
529
530 do_scale_vcore(vcores->core.addr, vcores->core.value,
531 vcores->core.pmic);
532
533 do_scale_vcore(vcores->mpu.addr, vcores->mpu.value,
534 vcores->mpu.pmic);
535
536 do_scale_vcore(vcores->mm.addr, vcores->mm.value,
537 vcores->mm.pmic);
538
539 if (emif_sdram_type() == EMIF_SDRAM_TYPE_DDR3) {
540 /* Configure LDO SRAM "magic" bits */
541 writel(2, (*prcm)->prm_sldo_core_setup);
542 writel(2, (*prcm)->prm_sldo_mpu_setup);
543 writel(2, (*prcm)->prm_sldo_mm_setup);
544 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400545}
546
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000547static inline void enable_clock_domain(u32 const clkctrl_reg, u32 enable_mode)
Aneesh V0d2628b2011-07-21 09:10:07 -0400548{
549 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
550 enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000551 debug("Enable clock domain - %x\n", clkctrl_reg);
Aneesh V0d2628b2011-07-21 09:10:07 -0400552}
553
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000554static inline void wait_for_clk_enable(u32 clkctrl_addr)
Aneesh V0d2628b2011-07-21 09:10:07 -0400555{
556 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
557 u32 bound = LDELAY;
558
559 while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
560 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
561
562 clkctrl = readl(clkctrl_addr);
563 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
564 MODULE_CLKCTRL_IDLEST_SHIFT;
565 if (--bound == 0) {
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000566 printf("Clock enable failed for 0x%x idlest 0x%x\n",
Aneesh V0d2628b2011-07-21 09:10:07 -0400567 clkctrl_addr, clkctrl);
568 return;
569 }
570 }
571}
572
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000573static inline void enable_clock_module(u32 const clkctrl_addr, u32 enable_mode,
Aneesh V0d2628b2011-07-21 09:10:07 -0400574 u32 wait_for_enable)
575{
576 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
577 enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000578 debug("Enable clock module - %x\n", clkctrl_addr);
Aneesh V0d2628b2011-07-21 09:10:07 -0400579 if (wait_for_enable)
580 wait_for_clk_enable(clkctrl_addr);
581}
582
Aneesh V0d2628b2011-07-21 09:10:07 -0400583void freq_update_core(void)
584{
585 u32 freq_config1 = 0;
586 const struct dpll_params *core_dpll_params;
SRICHARAN R3d534962012-03-12 02:25:37 +0000587 u32 omap_rev = omap_revision();
Aneesh V0d2628b2011-07-21 09:10:07 -0400588
SRICHARAN R1a79cab2013-02-04 04:22:01 +0000589 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V0d2628b2011-07-21 09:10:07 -0400590 /* Put EMIF clock domain in sw wakeup mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000591 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
Aneesh V0d2628b2011-07-21 09:10:07 -0400592 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000593 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
594 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
Aneesh V0d2628b2011-07-21 09:10:07 -0400595
596 freq_config1 = SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK |
597 SHADOW_FREQ_CONFIG1_DLL_RESET_MASK;
598
599 freq_config1 |= (DPLL_EN_LOCK << SHADOW_FREQ_CONFIG1_DPLL_EN_SHIFT) &
600 SHADOW_FREQ_CONFIG1_DPLL_EN_MASK;
601
602 freq_config1 |= (core_dpll_params->m2 <<
603 SHADOW_FREQ_CONFIG1_M2_DIV_SHIFT) &
604 SHADOW_FREQ_CONFIG1_M2_DIV_MASK;
605
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000606 writel(freq_config1, (*prcm)->cm_shadow_freq_config1);
Aneesh V0d2628b2011-07-21 09:10:07 -0400607 if (!wait_on_value(SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK, 0,
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000608 (u32 *) (*prcm)->cm_shadow_freq_config1, LDELAY)) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400609 puts("FREQ UPDATE procedure failed!!");
610 hang();
611 }
612
SRICHARAN R3d534962012-03-12 02:25:37 +0000613 /*
614 * Putting EMIF in HW_AUTO is seen to be causing issues with
615 * EMIF clocks and the master DLL. Put EMIF in SW_WKUP
616 * in OMAP5430 ES1.0 silicon
617 */
618 if (omap_rev != OMAP5430_ES1_0) {
619 /* Put EMIF clock domain back in hw auto mode */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000620 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
SRICHARAN R3d534962012-03-12 02:25:37 +0000621 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000622 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
623 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
SRICHARAN R3d534962012-03-12 02:25:37 +0000624 }
Aneesh V0d2628b2011-07-21 09:10:07 -0400625}
626
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000627void bypass_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400628{
629 do_bypass_dpll(base);
630 wait_for_bypass(base);
631}
632
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000633void lock_dpll(u32 const base)
Aneesh V0d2628b2011-07-21 09:10:07 -0400634{
635 do_lock_dpll(base);
636 wait_for_lock(base);
637}
638
Aneesh Vb8e60b92011-07-21 09:10:21 -0400639void setup_clocks_for_console(void)
640{
641 /* Do not add any spl_debug prints in this function */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000642 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400643 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
644 CD_CLKCTRL_CLKTRCTRL_SHIFT);
645
646 /* Enable all UARTs - console will be on one of them */
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000647 clrsetbits_le32((*prcm)->cm_l4per_uart1_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400648 MODULE_CLKCTRL_MODULEMODE_MASK,
649 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
650 MODULE_CLKCTRL_MODULEMODE_SHIFT);
651
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000652 clrsetbits_le32((*prcm)->cm_l4per_uart2_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400653 MODULE_CLKCTRL_MODULEMODE_MASK,
654 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
655 MODULE_CLKCTRL_MODULEMODE_SHIFT);
656
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000657 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400658 MODULE_CLKCTRL_MODULEMODE_MASK,
659 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
660 MODULE_CLKCTRL_MODULEMODE_SHIFT);
661
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000662 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400663 MODULE_CLKCTRL_MODULEMODE_MASK,
664 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
665 MODULE_CLKCTRL_MODULEMODE_SHIFT);
666
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000667 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vb8e60b92011-07-21 09:10:21 -0400668 CD_CLKCTRL_CLKTRCTRL_HW_AUTO <<
669 CD_CLKCTRL_CLKTRCTRL_SHIFT);
670}
671
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +0000672void do_enable_clocks(u32 const *clk_domains,
673 u32 const *clk_modules_hw_auto,
674 u32 const *clk_modules_explicit_en,
Sricharan9784f1f2011-11-15 09:49:58 -0500675 u8 wait_for_enable)
676{
677 u32 i, max = 100;
678
679 /* Put the clock domains in SW_WKUP mode */
680 for (i = 0; (i < max) && clk_domains[i]; i++) {
681 enable_clock_domain(clk_domains[i],
682 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
683 }
684
685 /* Clock modules that need to be put in HW_AUTO */
686 for (i = 0; (i < max) && clk_modules_hw_auto[i]; i++) {
687 enable_clock_module(clk_modules_hw_auto[i],
688 MODULE_CLKCTRL_MODULEMODE_HW_AUTO,
689 wait_for_enable);
690 };
691
692 /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
693 for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
694 enable_clock_module(clk_modules_explicit_en[i],
695 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
696 wait_for_enable);
697 };
698
699 /* Put the clock domains in HW_AUTO mode now */
700 for (i = 0; (i < max) && clk_domains[i]; i++) {
701 enable_clock_domain(clk_domains[i],
702 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
703 }
704}
705
Aneesh V0d2628b2011-07-21 09:10:07 -0400706void prcm_init(void)
707{
Sricharan9310ff72011-11-15 09:49:55 -0500708 switch (omap_hw_init_context()) {
Aneesh V0d2628b2011-07-21 09:10:07 -0400709 case OMAP_INIT_CONTEXT_SPL:
710 case OMAP_INIT_CONTEXT_UBOOT_FROM_NOR:
711 case OMAP_INIT_CONTEXT_UBOOT_AFTER_CH:
Aneesh V9a390882011-07-21 09:29:29 -0400712 enable_basic_clocks();
SRICHARAN R00d328c2013-02-04 04:22:02 +0000713 scale_vcores(*omap_vcores);
Aneesh V0d2628b2011-07-21 09:10:07 -0400714 setup_dplls();
Sricharan308fe922011-11-15 09:50:03 -0500715#ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
Aneesh V0d2628b2011-07-21 09:10:07 -0400716 setup_non_essential_dplls();
717 enable_non_essential_clocks();
Sricharan308fe922011-11-15 09:50:03 -0500718#endif
Aneesh V0d2628b2011-07-21 09:10:07 -0400719 break;
720 default:
721 break;
722 }
Sricharan308fe922011-11-15 09:50:03 -0500723
724 if (OMAP_INIT_CONTEXT_SPL != omap_hw_init_context())
725 enable_basic_uboot_clocks();
Aneesh V0d2628b2011-07-21 09:10:07 -0400726}