blob: 8e7411d43781186bee0ceed9159394bafec63e6b [file] [log] [blame]
Aneesh V37768012011-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Aneesh V37768012011-07-21 09:10:07 -040015 */
16#include <common.h>
Lokesh Vutla63fc0c72013-05-30 03:19:29 +000017#include <i2c.h>
Aneesh V37768012011-07-21 09:10:07 -040018#include <asm/omap_common.h>
Sanjeev Premi3b690eb2011-09-08 10:48:39 -040019#include <asm/gpio.h>
Lokesh Vutlaaf1d0022013-05-30 02:54:32 +000020#include <asm/arch/clock.h>
Aneesh V37768012011-07-21 09:10:07 -040021#include <asm/arch/sys_proto.h>
22#include <asm/utils.h>
Aneesh Vd5067192011-07-21 09:29:32 -040023#include <asm/omap_gpio.h>
Lokesh Vutla9ca8bfe2013-02-04 04:21:59 +000024#include <asm/emif.h>
Aneesh V37768012011-07-21 09:10:07 -040025
26#ifndef CONFIG_SPL_BUILD
27/*
28 * printing to console doesn't work unless
29 * this code is executed from SPL
30 */
31#define printf(fmt, args...)
32#define puts(s)
33#endif
34
SRICHARAN Ree9447b2013-02-04 04:22:01 +000035const u32 sys_clk_array[8] = {
36 12000000, /* 12 MHz */
Lokesh Vutla97405d82013-05-30 03:19:38 +000037 20000000, /* 20 MHz */
SRICHARAN Ree9447b2013-02-04 04:22:01 +000038 16800000, /* 16.8 MHz */
39 19200000, /* 19.2 MHz */
40 26000000, /* 26 MHz */
41 27000000, /* 27 MHz */
42 38400000, /* 38.4 MHz */
43};
44
Aneesh V37768012011-07-21 09:10:07 -040045static inline u32 __get_sys_clk_index(void)
46{
Lokesh Vutlaea8eff12013-02-12 21:29:05 +000047 s8 ind;
Aneesh V37768012011-07-21 09:10:07 -040048 /*
49 * For ES1 the ROM code calibration of sys clock is not reliable
50 * due to hw issue. So, use hard-coded value. If this value is not
51 * correct for any board over-ride this function in board file
52 * From ES2.0 onwards you will get this information from
53 * CM_SYS_CLKSEL
54 */
55 if (omap_revision() == OMAP4430_ES1_0)
56 ind = OMAP_SYS_CLK_IND_38_4_MHZ;
57 else {
58 /* SYS_CLKSEL - 1 to match the dpll param array indices */
SRICHARAN R01b753f2013-02-04 04:22:00 +000059 ind = (readl((*prcm)->cm_sys_clksel) &
Aneesh V37768012011-07-21 09:10:07 -040060 CM_SYS_CLKSEL_SYS_CLKSEL_MASK) - 1;
61 }
62 return ind;
63}
64
65u32 get_sys_clk_index(void)
66 __attribute__ ((weak, alias("__get_sys_clk_index")));
67
68u32 get_sys_clk_freq(void)
69{
70 u8 index = get_sys_clk_index();
71 return sys_clk_array[index];
72}
73
SRICHARAN Ree9447b2013-02-04 04:22:01 +000074void setup_post_dividers(u32 const base, const struct dpll_params *params)
75{
76 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
77
78 /* Setup post-dividers */
79 if (params->m2 >= 0)
80 writel(params->m2, &dpll_regs->cm_div_m2_dpll);
81 if (params->m3 >= 0)
82 writel(params->m3, &dpll_regs->cm_div_m3_dpll);
83 if (params->m4_h11 >= 0)
84 writel(params->m4_h11, &dpll_regs->cm_div_m4_h11_dpll);
85 if (params->m5_h12 >= 0)
86 writel(params->m5_h12, &dpll_regs->cm_div_m5_h12_dpll);
87 if (params->m6_h13 >= 0)
88 writel(params->m6_h13, &dpll_regs->cm_div_m6_h13_dpll);
89 if (params->m7_h14 >= 0)
90 writel(params->m7_h14, &dpll_regs->cm_div_m7_h14_dpll);
SRICHARAN R47abc3d2013-02-12 01:33:43 +000091 if (params->h21 >= 0)
92 writel(params->h21, &dpll_regs->cm_div_h21_dpll);
SRICHARAN Ree9447b2013-02-04 04:22:01 +000093 if (params->h22 >= 0)
94 writel(params->h22, &dpll_regs->cm_div_h22_dpll);
95 if (params->h23 >= 0)
96 writel(params->h23, &dpll_regs->cm_div_h23_dpll);
SRICHARAN R47abc3d2013-02-12 01:33:43 +000097 if (params->h24 >= 0)
98 writel(params->h24, &dpll_regs->cm_div_h24_dpll);
SRICHARAN Ree9447b2013-02-04 04:22:01 +000099}
100
SRICHARAN R01b753f2013-02-04 04:22:00 +0000101static inline void do_bypass_dpll(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400102{
103 struct dpll_regs *dpll_regs = (struct dpll_regs *)base;
104
105 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
106 CM_CLKMODE_DPLL_DPLL_EN_MASK,
107 DPLL_EN_FAST_RELOCK_BYPASS <<
108 CM_CLKMODE_DPLL_EN_SHIFT);
109}
110
SRICHARAN R01b753f2013-02-04 04:22:00 +0000111static inline void wait_for_bypass(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400112{
113 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
114
115 if (!wait_on_value(ST_DPLL_CLK_MASK, 0, &dpll_regs->cm_idlest_dpll,
116 LDELAY)) {
SRICHARAN R01b753f2013-02-04 04:22:00 +0000117 printf("Bypassing DPLL failed %x\n", base);
Aneesh V37768012011-07-21 09:10:07 -0400118 }
119}
120
SRICHARAN R01b753f2013-02-04 04:22:00 +0000121static inline void do_lock_dpll(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400122{
123 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
124
125 clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
126 CM_CLKMODE_DPLL_DPLL_EN_MASK,
127 DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
128}
129
SRICHARAN R01b753f2013-02-04 04:22:00 +0000130static inline void wait_for_lock(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400131{
132 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
133
134 if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
135 &dpll_regs->cm_idlest_dpll, LDELAY)) {
SRICHARAN R01b753f2013-02-04 04:22:00 +0000136 printf("DPLL locking failed for %x\n", base);
Aneesh V37768012011-07-21 09:10:07 -0400137 hang();
138 }
139}
140
SRICHARAN R01b753f2013-02-04 04:22:00 +0000141inline u32 check_for_lock(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400142{
Aneesh V37768012011-07-21 09:10:07 -0400143 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
Sricharan78f455c2011-11-15 09:50:03 -0500144 u32 lock = readl(&dpll_regs->cm_idlest_dpll) & ST_DPLL_CLK_MASK;
145
146 return lock;
147}
148
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000149const struct dpll_params *get_mpu_dpll_params(struct dplls const *dpll_data)
150{
151 u32 sysclk_ind = get_sys_clk_index();
152 return &dpll_data->mpu[sysclk_ind];
153}
154
155const struct dpll_params *get_core_dpll_params(struct dplls const *dpll_data)
156{
157 u32 sysclk_ind = get_sys_clk_index();
158 return &dpll_data->core[sysclk_ind];
159}
160
161const struct dpll_params *get_per_dpll_params(struct dplls const *dpll_data)
162{
163 u32 sysclk_ind = get_sys_clk_index();
164 return &dpll_data->per[sysclk_ind];
165}
166
167const struct dpll_params *get_iva_dpll_params(struct dplls const *dpll_data)
168{
169 u32 sysclk_ind = get_sys_clk_index();
170 return &dpll_data->iva[sysclk_ind];
171}
172
173const struct dpll_params *get_usb_dpll_params(struct dplls const *dpll_data)
174{
175 u32 sysclk_ind = get_sys_clk_index();
176 return &dpll_data->usb[sysclk_ind];
177}
178
179const struct dpll_params *get_abe_dpll_params(struct dplls const *dpll_data)
180{
181#ifdef CONFIG_SYS_OMAP_ABE_SYSCK
182 u32 sysclk_ind = get_sys_clk_index();
183 return &dpll_data->abe[sysclk_ind];
184#else
185 return dpll_data->abe;
186#endif
187}
188
Lokesh Vutlaea8eff12013-02-12 21:29:05 +0000189static const struct dpll_params *get_ddr_dpll_params
190 (struct dplls const *dpll_data)
191{
192 u32 sysclk_ind = get_sys_clk_index();
193
194 if (!dpll_data->ddr)
195 return NULL;
196 return &dpll_data->ddr[sysclk_ind];
197}
198
Lokesh Vutla65e9d562013-07-08 16:04:39 +0530199#ifdef CONFIG_DRIVER_TI_CPSW
200static const struct dpll_params *get_gmac_dpll_params
201 (struct dplls const *dpll_data)
202{
203 u32 sysclk_ind = get_sys_clk_index();
204
205 if (!dpll_data->gmac)
206 return NULL;
207 return &dpll_data->gmac[sysclk_ind];
208}
209#endif
210
SRICHARAN R01b753f2013-02-04 04:22:00 +0000211static void do_setup_dpll(u32 const base, const struct dpll_params *params,
Sricharan78f455c2011-11-15 09:50:03 -0500212 u8 lock, char *dpll)
213{
214 u32 temp, M, N;
215 struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
216
Lokesh Vutlaea8eff12013-02-12 21:29:05 +0000217 if (!params)
218 return;
219
Sricharan78f455c2011-11-15 09:50:03 -0500220 temp = readl(&dpll_regs->cm_clksel_dpll);
221
222 if (check_for_lock(base)) {
223 /*
224 * The Dpll has already been locked by rom code using CH.
225 * Check if M,N are matching with Ideal nominal opp values.
226 * If matches, skip the rest otherwise relock.
227 */
228 M = (temp & CM_CLKSEL_DPLL_M_MASK) >> CM_CLKSEL_DPLL_M_SHIFT;
229 N = (temp & CM_CLKSEL_DPLL_N_MASK) >> CM_CLKSEL_DPLL_N_SHIFT;
230 if ((M != (params->m)) || (N != (params->n))) {
231 debug("\n %s Dpll locked, but not for ideal M = %d,"
232 "N = %d values, current values are M = %d,"
233 "N= %d" , dpll, params->m, params->n,
234 M, N);
235 } else {
236 /* Dpll locked with ideal values for nominal opps. */
237 debug("\n %s Dpll already locked with ideal"
238 "nominal opp values", dpll);
239 goto setup_post_dividers;
240 }
241 }
Aneesh V37768012011-07-21 09:10:07 -0400242
243 bypass_dpll(base);
244
245 /* Set M & N */
Aneesh V37768012011-07-21 09:10:07 -0400246 temp &= ~CM_CLKSEL_DPLL_M_MASK;
247 temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
248
249 temp &= ~CM_CLKSEL_DPLL_N_MASK;
250 temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
251
252 writel(temp, &dpll_regs->cm_clksel_dpll);
253
254 /* Lock */
255 if (lock)
256 do_lock_dpll(base);
257
Sricharan78f455c2011-11-15 09:50:03 -0500258setup_post_dividers:
Sricharan2e5ba482011-11-15 09:49:58 -0500259 setup_post_dividers(base, params);
Aneesh V37768012011-07-21 09:10:07 -0400260
261 /* Wait till the DPLL locks */
262 if (lock)
263 wait_for_lock(base);
264}
265
Sricharan2e5ba482011-11-15 09:49:58 -0500266u32 omap_ddr_clk(void)
Aneesh V37768012011-07-21 09:10:07 -0400267{
Sricharan2e5ba482011-11-15 09:49:58 -0500268 u32 ddr_clk, sys_clk_khz, omap_rev, divider;
Aneesh V37768012011-07-21 09:10:07 -0400269 const struct dpll_params *core_dpll_params;
270
Sricharan2e5ba482011-11-15 09:49:58 -0500271 omap_rev = omap_revision();
Aneesh V37768012011-07-21 09:10:07 -0400272 sys_clk_khz = get_sys_clk_freq() / 1000;
273
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000274 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V37768012011-07-21 09:10:07 -0400275
276 debug("sys_clk %d\n ", sys_clk_khz * 1000);
277
278 /* Find Core DPLL locked frequency first */
279 ddr_clk = sys_clk_khz * 2 * core_dpll_params->m /
280 (core_dpll_params->n + 1);
Aneesh V37768012011-07-21 09:10:07 -0400281
Sricharan2e5ba482011-11-15 09:49:58 -0500282 if (omap_rev < OMAP5430_ES1_0) {
283 /*
284 * DDR frequency is PHY_ROOT_CLK/2
285 * PHY_ROOT_CLK = Fdpll/2/M2
286 */
287 divider = 4;
288 } else {
289 /*
290 * DDR frequency is PHY_ROOT_CLK
291 * PHY_ROOT_CLK = Fdpll/2/M2
292 */
293 divider = 2;
294 }
295
296 ddr_clk = ddr_clk / divider / core_dpll_params->m2;
Aneesh V37768012011-07-21 09:10:07 -0400297 ddr_clk *= 1000; /* convert to Hz */
298 debug("ddr_clk %d\n ", ddr_clk);
299
300 return ddr_clk;
301}
302
Aneesh Vb4dc6442011-07-21 09:29:36 -0400303/*
304 * Lock MPU dpll
305 *
306 * Resulting MPU frequencies:
307 * 4430 ES1.0 : 600 MHz
308 * 4430 ES2.x : 792 MHz (OPP Turbo)
309 * 4460 : 920 MHz (OPP Turbo) - DCC disabled
310 */
311void configure_mpu_dpll(void)
312{
313 const struct dpll_params *params;
314 struct dpll_regs *mpu_dpll_regs;
Sricharan2e5ba482011-11-15 09:49:58 -0500315 u32 omap_rev;
316 omap_rev = omap_revision();
Aneesh Vb4dc6442011-07-21 09:29:36 -0400317
Sricharan2e5ba482011-11-15 09:49:58 -0500318 /*
319 * DCC and clock divider settings for 4460.
320 * DCC is required, if more than a certain frequency is required.
321 * For, 4460 > 1GHZ.
322 * 5430 > 1.4GHZ.
323 */
324 if ((omap_rev >= OMAP4460_ES1_0) && (omap_rev < OMAP5430_ES1_0)) {
Aneesh Vb4dc6442011-07-21 09:29:36 -0400325 mpu_dpll_regs =
SRICHARAN R01b753f2013-02-04 04:22:00 +0000326 (struct dpll_regs *)((*prcm)->cm_clkmode_dpll_mpu);
327 bypass_dpll((*prcm)->cm_clkmode_dpll_mpu);
328 clrbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Vb4dc6442011-07-21 09:29:36 -0400329 MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000330 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
Aneesh Vb4dc6442011-07-21 09:29:36 -0400331 MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
332 clrbits_le32(&mpu_dpll_regs->cm_clksel_dpll,
333 CM_CLKSEL_DCC_EN_MASK);
334 }
335
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000336 params = get_mpu_dpll_params(*dplls_data);
Sricharan78f455c2011-11-15 09:50:03 -0500337
SRICHARAN R01b753f2013-02-04 04:22:00 +0000338 do_setup_dpll((*prcm)->cm_clkmode_dpll_mpu, params, DPLL_LOCK, "mpu");
Aneesh Vb4dc6442011-07-21 09:29:36 -0400339 debug("MPU DPLL locked\n");
340}
341
Dan Murphyd861a332013-08-26 08:54:50 -0500342#if defined(CONFIG_USB_EHCI_OMAP) || defined(CONFIG_USB_XHCI_OMAP)
Govindraj.R860004c2012-02-06 03:55:36 +0000343static void setup_usb_dpll(void)
344{
345 const struct dpll_params *params;
346 u32 sys_clk_khz, sd_div, num, den;
347
348 sys_clk_khz = get_sys_clk_freq() / 1000;
349 /*
350 * USB:
351 * USB dpll is J-type. Need to set DPLL_SD_DIV for jitter correction
352 * DPLL_SD_DIV = CEILING ([DPLL_MULT/(DPLL_DIV+1)]* CLKINP / 250)
353 * - where CLKINP is sys_clk in MHz
354 * Use CLKINP in KHz and adjust the denominator accordingly so
355 * that we have enough accuracy and at the same time no overflow
356 */
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000357 params = get_usb_dpll_params(*dplls_data);
Govindraj.R860004c2012-02-06 03:55:36 +0000358 num = params->m * sys_clk_khz;
359 den = (params->n + 1) * 250 * 1000;
360 num += den - 1;
361 sd_div = num / den;
SRICHARAN R01b753f2013-02-04 04:22:00 +0000362 clrsetbits_le32((*prcm)->cm_clksel_dpll_usb,
Govindraj.R860004c2012-02-06 03:55:36 +0000363 CM_CLKSEL_DPLL_DPLL_SD_DIV_MASK,
364 sd_div << CM_CLKSEL_DPLL_DPLL_SD_DIV_SHIFT);
365
366 /* Now setup the dpll with the regular function */
SRICHARAN R01b753f2013-02-04 04:22:00 +0000367 do_setup_dpll((*prcm)->cm_clkmode_dpll_usb, params, DPLL_LOCK, "usb");
Govindraj.R860004c2012-02-06 03:55:36 +0000368}
369#endif
370
Aneesh V37768012011-07-21 09:10:07 -0400371static void setup_dplls(void)
372{
Anatolij Gustschin164a7502011-12-03 06:46:14 +0000373 u32 temp;
Aneesh V37768012011-07-21 09:10:07 -0400374 const struct dpll_params *params;
Aneesh V37768012011-07-21 09:10:07 -0400375
Anatolij Gustschin164a7502011-12-03 06:46:14 +0000376 debug("setup_dplls\n");
Aneesh V37768012011-07-21 09:10:07 -0400377
378 /* CORE dpll */
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000379 params = get_core_dpll_params(*dplls_data); /* default - safest */
Aneesh V37768012011-07-21 09:10:07 -0400380 /*
381 * Do not lock the core DPLL now. Just set it up.
382 * Core DPLL will be locked after setting up EMIF
383 * using the FREQ_UPDATE method(freq_update_core())
384 */
Lokesh Vutla9ca8bfe2013-02-04 04:21:59 +0000385 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
SRICHARAN R01b753f2013-02-04 04:22:00 +0000386 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutla753bae82012-05-22 00:03:26 +0000387 DPLL_NO_LOCK, "core");
388 else
SRICHARAN R01b753f2013-02-04 04:22:00 +0000389 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
Lokesh Vutla753bae82012-05-22 00:03:26 +0000390 DPLL_LOCK, "core");
Aneesh V37768012011-07-21 09:10:07 -0400391 /* Set the ratios for CORE_CLK, L3_CLK, L4_CLK */
392 temp = (CLKSEL_CORE_X2_DIV_1 << CLKSEL_CORE_SHIFT) |
393 (CLKSEL_L3_CORE_DIV_2 << CLKSEL_L3_SHIFT) |
394 (CLKSEL_L4_L3_DIV_2 << CLKSEL_L4_SHIFT);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000395 writel(temp, (*prcm)->cm_clksel_core);
Aneesh V37768012011-07-21 09:10:07 -0400396 debug("Core DPLL configured\n");
397
398 /* lock PER dpll */
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000399 params = get_per_dpll_params(*dplls_data);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000400 do_setup_dpll((*prcm)->cm_clkmode_dpll_per,
Sricharan78f455c2011-11-15 09:50:03 -0500401 params, DPLL_LOCK, "per");
Aneesh V37768012011-07-21 09:10:07 -0400402 debug("PER DPLL locked\n");
403
404 /* MPU dpll */
Aneesh Vb4dc6442011-07-21 09:29:36 -0400405 configure_mpu_dpll();
Govindraj.R860004c2012-02-06 03:55:36 +0000406
Dan Murphyd861a332013-08-26 08:54:50 -0500407#if defined(CONFIG_USB_EHCI_OMAP) || defined(CONFIG_USB_XHCI_OMAP)
Govindraj.R860004c2012-02-06 03:55:36 +0000408 setup_usb_dpll();
409#endif
Lokesh Vutlaea8eff12013-02-12 21:29:05 +0000410 params = get_ddr_dpll_params(*dplls_data);
411 do_setup_dpll((*prcm)->cm_clkmode_dpll_ddrphy,
412 params, DPLL_LOCK, "ddr");
Lokesh Vutla65e9d562013-07-08 16:04:39 +0530413
414#ifdef CONFIG_DRIVER_TI_CPSW
415 params = get_gmac_dpll_params(*dplls_data);
416 do_setup_dpll((*prcm)->cm_clkmode_dpll_gmac, params,
417 DPLL_LOCK, "gmac");
418#endif
Aneesh V37768012011-07-21 09:10:07 -0400419}
420
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000421u32 get_offset_code(u32 volt_offset, struct pmic_data *pmic)
Aneesh Vd5067192011-07-21 09:29:32 -0400422{
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000423 u32 offset_code;
Nishanth Menon3acb5532012-03-01 14:17:38 +0000424
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000425 volt_offset -= pmic->base_offset;
Nishanth Menon3acb5532012-03-01 14:17:38 +0000426
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000427 offset_code = (volt_offset + pmic->step - 1) / pmic->step;
Aneesh Vd5067192011-07-21 09:29:32 -0400428
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000429 /*
430 * Offset codes 1-6 all give the base voltage in Palmas
431 * Offset code 0 switches OFF the SMPS
432 */
433 return offset_code + pmic->start_code;
Aneesh Vd5067192011-07-21 09:29:32 -0400434}
435
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000436void do_scale_vcore(u32 vcore_reg, u32 volt_mv, struct pmic_data *pmic)
Aneesh V37768012011-07-21 09:10:07 -0400437{
Nishanth Menona78274b2012-03-01 14:17:37 +0000438 u32 offset_code;
Aneesh V37768012011-07-21 09:10:07 -0400439 u32 offset = volt_mv;
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000440 int ret = 0;
441
Lokesh Vutla63fc0c72013-05-30 03:19:29 +0000442 if (!volt_mv)
443 return;
444
Lokesh Vutla4ca94d82013-05-30 02:54:33 +0000445 pmic->pmic_bus_init();
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000446 /* See if we can first get the GPIO if needed */
447 if (pmic->gpio_en)
448 ret = gpio_request(pmic->gpio, "PMIC_GPIO");
449
450 if (ret < 0) {
451 printf("%s: gpio %d request failed %d\n", __func__,
452 pmic->gpio, ret);
453 return;
454 }
455
456 /* Pull the GPIO low to select SET0 register, while we program SET1 */
457 if (pmic->gpio_en)
458 gpio_direction_output(pmic->gpio, 0);
Aneesh V37768012011-07-21 09:10:07 -0400459
460 /* convert to uV for better accuracy in the calculations */
461 offset *= 1000;
462
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000463 offset_code = get_offset_code(offset, pmic);
Aneesh V37768012011-07-21 09:10:07 -0400464
465 debug("do_scale_vcore: volt - %d offset_code - 0x%x\n", volt_mv,
466 offset_code);
SRICHARAN R8de17f42012-03-12 02:25:38 +0000467
Lokesh Vutla4ca94d82013-05-30 02:54:33 +0000468 if (pmic->pmic_write(pmic->i2c_slave_addr, vcore_reg, offset_code))
Aneesh V37768012011-07-21 09:10:07 -0400469 printf("Scaling voltage failed for 0x%x\n", vcore_reg);
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000470
471 if (pmic->gpio_en)
472 gpio_direction_output(pmic->gpio, 1);
473}
474
Nishanth Menon18c9d552013-05-30 03:19:31 +0000475static u32 optimize_vcore_voltage(struct volts const *v)
476{
477 u32 val;
478 if (!v->value)
479 return 0;
480 if (!v->efuse.reg)
481 return v->value;
482
483 switch (v->efuse.reg_bits) {
484 case 16:
485 val = readw(v->efuse.reg);
486 break;
487 case 32:
488 val = readl(v->efuse.reg);
489 break;
490 default:
491 printf("Error: efuse 0x%08x bits=%d unknown\n",
492 v->efuse.reg, v->efuse.reg_bits);
493 return v->value;
494 }
495
496 if (!val) {
497 printf("Error: efuse 0x%08x bits=%d val=0, using %d\n",
498 v->efuse.reg, v->efuse.reg_bits, v->value);
499 return v->value;
500 }
501
502 debug("%s:efuse 0x%08x bits=%d Vnom=%d, using efuse value %d\n",
503 __func__, v->efuse.reg, v->efuse.reg_bits, v->value, val);
504 return val;
505}
506
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000507/*
508 * Setup the voltages for vdd_mpu, vdd_core, and vdd_iva
509 * We set the maximum voltages allowed here because Smart-Reflex is not
510 * enabled in bootloader. Voltage initialization in the kernel will set
511 * these to the nominal values after enabling Smart-Reflex
512 */
513void scale_vcores(struct vcores_data const *vcores)
514{
Nishanth Menon18c9d552013-05-30 03:19:31 +0000515 u32 val;
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000516
Nishanth Menon18c9d552013-05-30 03:19:31 +0000517 val = optimize_vcore_voltage(&vcores->core);
518 do_scale_vcore(vcores->core.addr, val, vcores->core.pmic);
519
520 val = optimize_vcore_voltage(&vcores->mpu);
521 do_scale_vcore(vcores->mpu.addr, val, vcores->mpu.pmic);
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000522
Andrii Tseglytskyie69c5852013-05-20 22:42:09 +0000523 /* Configure MPU ABB LDO after scale */
524 abb_setup((*ctrl)->control_std_fuse_opp_vdd_mpu_2,
525 (*ctrl)->control_wkup_ldovbb_mpu_voltage_ctrl,
526 (*prcm)->prm_abbldo_mpu_setup,
527 (*prcm)->prm_abbldo_mpu_ctrl,
528 (*prcm)->prm_irqstatus_mpu_2,
529 OMAP_ABB_MPU_TXDONE_MASK,
530 OMAP_ABB_FAST_OPP);
531
Nishanth Menon18c9d552013-05-30 03:19:31 +0000532 val = optimize_vcore_voltage(&vcores->mm);
533 do_scale_vcore(vcores->mm.addr, val, vcores->mm.pmic);
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000534
Nishanth Menon18c9d552013-05-30 03:19:31 +0000535 val = optimize_vcore_voltage(&vcores->gpu);
536 do_scale_vcore(vcores->gpu.addr, val, vcores->gpu.pmic);
Lokesh Vutla63fc0c72013-05-30 03:19:29 +0000537
Nishanth Menon18c9d552013-05-30 03:19:31 +0000538 val = optimize_vcore_voltage(&vcores->eve);
539 do_scale_vcore(vcores->eve.addr, val, vcores->eve.pmic);
Lokesh Vutla63fc0c72013-05-30 03:19:29 +0000540
Nishanth Menon18c9d552013-05-30 03:19:31 +0000541 val = optimize_vcore_voltage(&vcores->iva);
542 do_scale_vcore(vcores->iva.addr, val, vcores->iva.pmic);
Aneesh V37768012011-07-21 09:10:07 -0400543}
544
SRICHARAN R01b753f2013-02-04 04:22:00 +0000545static inline void enable_clock_domain(u32 const clkctrl_reg, u32 enable_mode)
Aneesh V37768012011-07-21 09:10:07 -0400546{
547 clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
548 enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000549 debug("Enable clock domain - %x\n", clkctrl_reg);
Aneesh V37768012011-07-21 09:10:07 -0400550}
551
SRICHARAN R01b753f2013-02-04 04:22:00 +0000552static inline void wait_for_clk_enable(u32 clkctrl_addr)
Aneesh V37768012011-07-21 09:10:07 -0400553{
554 u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
555 u32 bound = LDELAY;
556
557 while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
558 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
559
560 clkctrl = readl(clkctrl_addr);
561 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
562 MODULE_CLKCTRL_IDLEST_SHIFT;
563 if (--bound == 0) {
SRICHARAN R01b753f2013-02-04 04:22:00 +0000564 printf("Clock enable failed for 0x%x idlest 0x%x\n",
Aneesh V37768012011-07-21 09:10:07 -0400565 clkctrl_addr, clkctrl);
566 return;
567 }
568 }
569}
570
SRICHARAN R01b753f2013-02-04 04:22:00 +0000571static inline void enable_clock_module(u32 const clkctrl_addr, u32 enable_mode,
Aneesh V37768012011-07-21 09:10:07 -0400572 u32 wait_for_enable)
573{
574 clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
575 enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000576 debug("Enable clock module - %x\n", clkctrl_addr);
Aneesh V37768012011-07-21 09:10:07 -0400577 if (wait_for_enable)
578 wait_for_clk_enable(clkctrl_addr);
579}
580
Aneesh V37768012011-07-21 09:10:07 -0400581void freq_update_core(void)
582{
583 u32 freq_config1 = 0;
584 const struct dpll_params *core_dpll_params;
SRICHARAN Rf4010732012-03-12 02:25:37 +0000585 u32 omap_rev = omap_revision();
Aneesh V37768012011-07-21 09:10:07 -0400586
SRICHARAN Ree9447b2013-02-04 04:22:01 +0000587 core_dpll_params = get_core_dpll_params(*dplls_data);
Aneesh V37768012011-07-21 09:10:07 -0400588 /* Put EMIF clock domain in sw wakeup mode */
SRICHARAN R01b753f2013-02-04 04:22:00 +0000589 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
Aneesh V37768012011-07-21 09:10:07 -0400590 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000591 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
592 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
Aneesh V37768012011-07-21 09:10:07 -0400593
594 freq_config1 = SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK |
595 SHADOW_FREQ_CONFIG1_DLL_RESET_MASK;
596
597 freq_config1 |= (DPLL_EN_LOCK << SHADOW_FREQ_CONFIG1_DPLL_EN_SHIFT) &
598 SHADOW_FREQ_CONFIG1_DPLL_EN_MASK;
599
600 freq_config1 |= (core_dpll_params->m2 <<
601 SHADOW_FREQ_CONFIG1_M2_DIV_SHIFT) &
602 SHADOW_FREQ_CONFIG1_M2_DIV_MASK;
603
SRICHARAN R01b753f2013-02-04 04:22:00 +0000604 writel(freq_config1, (*prcm)->cm_shadow_freq_config1);
Aneesh V37768012011-07-21 09:10:07 -0400605 if (!wait_on_value(SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK, 0,
SRICHARAN R01b753f2013-02-04 04:22:00 +0000606 (u32 *) (*prcm)->cm_shadow_freq_config1, LDELAY)) {
Aneesh V37768012011-07-21 09:10:07 -0400607 puts("FREQ UPDATE procedure failed!!");
608 hang();
609 }
610
SRICHARAN Rf4010732012-03-12 02:25:37 +0000611 /*
612 * Putting EMIF in HW_AUTO is seen to be causing issues with
Lubomir Popova8f408a2013-04-04 05:51:45 +0000613 * EMIF clocks and the master DLL. Keep EMIF in SW_WKUP
SRICHARAN Rf4010732012-03-12 02:25:37 +0000614 * in OMAP5430 ES1.0 silicon
615 */
616 if (omap_rev != OMAP5430_ES1_0) {
617 /* Put EMIF clock domain back in hw auto mode */
SRICHARAN R01b753f2013-02-04 04:22:00 +0000618 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
SRICHARAN Rf4010732012-03-12 02:25:37 +0000619 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
SRICHARAN R01b753f2013-02-04 04:22:00 +0000620 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
621 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
SRICHARAN Rf4010732012-03-12 02:25:37 +0000622 }
Aneesh V37768012011-07-21 09:10:07 -0400623}
624
SRICHARAN R01b753f2013-02-04 04:22:00 +0000625void bypass_dpll(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400626{
627 do_bypass_dpll(base);
628 wait_for_bypass(base);
629}
630
SRICHARAN R01b753f2013-02-04 04:22:00 +0000631void lock_dpll(u32 const base)
Aneesh V37768012011-07-21 09:10:07 -0400632{
633 do_lock_dpll(base);
634 wait_for_lock(base);
635}
636
Aneesh Vbcae7212011-07-21 09:10:21 -0400637void setup_clocks_for_console(void)
638{
639 /* Do not add any spl_debug prints in this function */
SRICHARAN R01b753f2013-02-04 04:22:00 +0000640 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vbcae7212011-07-21 09:10:21 -0400641 CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
642 CD_CLKCTRL_CLKTRCTRL_SHIFT);
643
644 /* Enable all UARTs - console will be on one of them */
SRICHARAN R01b753f2013-02-04 04:22:00 +0000645 clrsetbits_le32((*prcm)->cm_l4per_uart1_clkctrl,
Aneesh Vbcae7212011-07-21 09:10:21 -0400646 MODULE_CLKCTRL_MODULEMODE_MASK,
647 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
648 MODULE_CLKCTRL_MODULEMODE_SHIFT);
649
SRICHARAN R01b753f2013-02-04 04:22:00 +0000650 clrsetbits_le32((*prcm)->cm_l4per_uart2_clkctrl,
Aneesh Vbcae7212011-07-21 09:10:21 -0400651 MODULE_CLKCTRL_MODULEMODE_MASK,
652 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
653 MODULE_CLKCTRL_MODULEMODE_SHIFT);
654
SRICHARAN R01b753f2013-02-04 04:22:00 +0000655 clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
Aneesh Vbcae7212011-07-21 09:10:21 -0400656 MODULE_CLKCTRL_MODULEMODE_MASK,
657 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
658 MODULE_CLKCTRL_MODULEMODE_SHIFT);
659
Lubomir Popova8f408a2013-04-04 05:51:45 +0000660 clrsetbits_le32((*prcm)->cm_l4per_uart4_clkctrl,
Aneesh Vbcae7212011-07-21 09:10:21 -0400661 MODULE_CLKCTRL_MODULEMODE_MASK,
662 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
663 MODULE_CLKCTRL_MODULEMODE_SHIFT);
664
SRICHARAN R01b753f2013-02-04 04:22:00 +0000665 clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
Aneesh Vbcae7212011-07-21 09:10:21 -0400666 CD_CLKCTRL_CLKTRCTRL_HW_AUTO <<
667 CD_CLKCTRL_CLKTRCTRL_SHIFT);
668}
669
SRICHARAN R01b753f2013-02-04 04:22:00 +0000670void do_enable_clocks(u32 const *clk_domains,
671 u32 const *clk_modules_hw_auto,
672 u32 const *clk_modules_explicit_en,
Sricharan2e5ba482011-11-15 09:49:58 -0500673 u8 wait_for_enable)
674{
675 u32 i, max = 100;
676
677 /* Put the clock domains in SW_WKUP mode */
678 for (i = 0; (i < max) && clk_domains[i]; i++) {
679 enable_clock_domain(clk_domains[i],
680 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
681 }
682
683 /* Clock modules that need to be put in HW_AUTO */
684 for (i = 0; (i < max) && clk_modules_hw_auto[i]; i++) {
685 enable_clock_module(clk_modules_hw_auto[i],
686 MODULE_CLKCTRL_MODULEMODE_HW_AUTO,
687 wait_for_enable);
688 };
689
690 /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
691 for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
692 enable_clock_module(clk_modules_explicit_en[i],
693 MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
694 wait_for_enable);
695 };
696
697 /* Put the clock domains in HW_AUTO mode now */
698 for (i = 0; (i < max) && clk_domains[i]; i++) {
699 enable_clock_domain(clk_domains[i],
700 CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
701 }
702}
703
Aneesh V37768012011-07-21 09:10:07 -0400704void prcm_init(void)
705{
Sricharan508a58f2011-11-15 09:49:55 -0500706 switch (omap_hw_init_context()) {
Aneesh V37768012011-07-21 09:10:07 -0400707 case OMAP_INIT_CONTEXT_SPL:
708 case OMAP_INIT_CONTEXT_UBOOT_FROM_NOR:
709 case OMAP_INIT_CONTEXT_UBOOT_AFTER_CH:
Aneesh V25223a62011-07-21 09:29:29 -0400710 enable_basic_clocks();
Lokesh Vutla3332b242013-05-30 03:19:30 +0000711 timer_init();
SRICHARAN R3fcdd4a2013-02-04 04:22:02 +0000712 scale_vcores(*omap_vcores);
Aneesh V37768012011-07-21 09:10:07 -0400713 setup_dplls();
Lokesh Vutla0b1b60c2013-04-17 20:49:40 +0000714 setup_warmreset_time();
Aneesh V37768012011-07-21 09:10:07 -0400715 break;
716 default:
717 break;
718 }
Sricharan78f455c2011-11-15 09:50:03 -0500719
720 if (OMAP_INIT_CONTEXT_SPL != omap_hw_init_context())
721 enable_basic_uboot_clocks();
Aneesh V37768012011-07-21 09:10:07 -0400722}
Lokesh Vutla63fc0c72013-05-30 03:19:29 +0000723
724void gpi2c_init(void)
725{
726 static int gpi2c = 1;
727
728 if (gpi2c) {
Heiko Schocher6789e842013-10-22 11:03:18 +0200729 i2c_init(CONFIG_SYS_OMAP24_I2C_SPEED,
730 CONFIG_SYS_OMAP24_I2C_SLAVE);
Lokesh Vutla63fc0c72013-05-30 03:19:29 +0000731 gpi2c = 0;
732 }
733}