]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/omap-common/clocks-common.c
ARM: OMAP5: DRA7xx: support class 0 optimized voltages
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / omap-common / clocks-common.c
1 /*
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 <i2c.h>
34 #include <asm/omap_common.h>
35 #include <asm/gpio.h>
36 #include <asm/arch/clock.h>
37 #include <asm/arch/sys_proto.h>
38 #include <asm/utils.h>
39 #include <asm/omap_gpio.h>
40 #include <asm/emif.h>
41
42 #ifndef CONFIG_SPL_BUILD
43 /*
44  * printing to console doesn't work unless
45  * this code is executed from SPL
46  */
47 #define printf(fmt, args...)
48 #define puts(s)
49 #endif
50
51 const u32 sys_clk_array[8] = {
52         12000000,              /* 12 MHz */
53         13000000,              /* 13 MHz */
54         16800000,              /* 16.8 MHz */
55         19200000,              /* 19.2 MHz */
56         26000000,              /* 26 MHz */
57         27000000,              /* 27 MHz */
58         38400000,              /* 38.4 MHz */
59         20000000,               /* 20 MHz */
60 };
61
62 static inline u32 __get_sys_clk_index(void)
63 {
64         s8 ind;
65         /*
66          * For ES1 the ROM code calibration of sys clock is not reliable
67          * due to hw issue. So, use hard-coded value. If this value is not
68          * correct for any board over-ride this function in board file
69          * From ES2.0 onwards you will get this information from
70          * CM_SYS_CLKSEL
71          */
72         if (omap_revision() == OMAP4430_ES1_0)
73                 ind = OMAP_SYS_CLK_IND_38_4_MHZ;
74         else {
75                 /* SYS_CLKSEL - 1 to match the dpll param array indices */
76                 ind = (readl((*prcm)->cm_sys_clksel) &
77                         CM_SYS_CLKSEL_SYS_CLKSEL_MASK) - 1;
78                 /*
79                  * SYS_CLKSEL value for 20MHz is 0. This is introduced newly
80                  * in DRA7XX socs. SYS_CLKSEL -1 will be greater than
81                  * NUM_SYS_CLK. So considering the last 3 bits as the index
82                  * for the dpll param array.
83                  */
84                 ind &= CM_SYS_CLKSEL_SYS_CLKSEL_MASK;
85         }
86         return ind;
87 }
88
89 u32 get_sys_clk_index(void)
90         __attribute__ ((weak, alias("__get_sys_clk_index")));
91
92 u32 get_sys_clk_freq(void)
93 {
94         u8 index = get_sys_clk_index();
95         return sys_clk_array[index];
96 }
97
98 void setup_post_dividers(u32 const base, const struct dpll_params *params)
99 {
100         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
101
102         /* Setup post-dividers */
103         if (params->m2 >= 0)
104                 writel(params->m2, &dpll_regs->cm_div_m2_dpll);
105         if (params->m3 >= 0)
106                 writel(params->m3, &dpll_regs->cm_div_m3_dpll);
107         if (params->m4_h11 >= 0)
108                 writel(params->m4_h11, &dpll_regs->cm_div_m4_h11_dpll);
109         if (params->m5_h12 >= 0)
110                 writel(params->m5_h12, &dpll_regs->cm_div_m5_h12_dpll);
111         if (params->m6_h13 >= 0)
112                 writel(params->m6_h13, &dpll_regs->cm_div_m6_h13_dpll);
113         if (params->m7_h14 >= 0)
114                 writel(params->m7_h14, &dpll_regs->cm_div_m7_h14_dpll);
115         if (params->h21 >= 0)
116                 writel(params->h21, &dpll_regs->cm_div_h21_dpll);
117         if (params->h22 >= 0)
118                 writel(params->h22, &dpll_regs->cm_div_h22_dpll);
119         if (params->h23 >= 0)
120                 writel(params->h23, &dpll_regs->cm_div_h23_dpll);
121         if (params->h24 >= 0)
122                 writel(params->h24, &dpll_regs->cm_div_h24_dpll);
123 }
124
125 static inline void do_bypass_dpll(u32 const base)
126 {
127         struct dpll_regs *dpll_regs = (struct dpll_regs *)base;
128
129         clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
130                         CM_CLKMODE_DPLL_DPLL_EN_MASK,
131                         DPLL_EN_FAST_RELOCK_BYPASS <<
132                         CM_CLKMODE_DPLL_EN_SHIFT);
133 }
134
135 static inline void wait_for_bypass(u32 const base)
136 {
137         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
138
139         if (!wait_on_value(ST_DPLL_CLK_MASK, 0, &dpll_regs->cm_idlest_dpll,
140                                 LDELAY)) {
141                 printf("Bypassing DPLL failed %x\n", base);
142         }
143 }
144
145 static inline void do_lock_dpll(u32 const base)
146 {
147         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
148
149         clrsetbits_le32(&dpll_regs->cm_clkmode_dpll,
150                       CM_CLKMODE_DPLL_DPLL_EN_MASK,
151                       DPLL_EN_LOCK << CM_CLKMODE_DPLL_EN_SHIFT);
152 }
153
154 static inline void wait_for_lock(u32 const base)
155 {
156         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
157
158         if (!wait_on_value(ST_DPLL_CLK_MASK, ST_DPLL_CLK_MASK,
159                 &dpll_regs->cm_idlest_dpll, LDELAY)) {
160                 printf("DPLL locking failed for %x\n", base);
161                 hang();
162         }
163 }
164
165 inline u32 check_for_lock(u32 const base)
166 {
167         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
168         u32 lock = readl(&dpll_regs->cm_idlest_dpll) & ST_DPLL_CLK_MASK;
169
170         return lock;
171 }
172
173 const struct dpll_params *get_mpu_dpll_params(struct dplls const *dpll_data)
174 {
175         u32 sysclk_ind = get_sys_clk_index();
176         return &dpll_data->mpu[sysclk_ind];
177 }
178
179 const struct dpll_params *get_core_dpll_params(struct dplls const *dpll_data)
180 {
181         u32 sysclk_ind = get_sys_clk_index();
182         return &dpll_data->core[sysclk_ind];
183 }
184
185 const struct dpll_params *get_per_dpll_params(struct dplls const *dpll_data)
186 {
187         u32 sysclk_ind = get_sys_clk_index();
188         return &dpll_data->per[sysclk_ind];
189 }
190
191 const struct dpll_params *get_iva_dpll_params(struct dplls const *dpll_data)
192 {
193         u32 sysclk_ind = get_sys_clk_index();
194         return &dpll_data->iva[sysclk_ind];
195 }
196
197 const struct dpll_params *get_usb_dpll_params(struct dplls const *dpll_data)
198 {
199         u32 sysclk_ind = get_sys_clk_index();
200         return &dpll_data->usb[sysclk_ind];
201 }
202
203 const struct dpll_params *get_abe_dpll_params(struct dplls const *dpll_data)
204 {
205 #ifdef CONFIG_SYS_OMAP_ABE_SYSCK
206         u32 sysclk_ind = get_sys_clk_index();
207         return &dpll_data->abe[sysclk_ind];
208 #else
209         return dpll_data->abe;
210 #endif
211 }
212
213 static const struct dpll_params *get_ddr_dpll_params
214                         (struct dplls const *dpll_data)
215 {
216         u32 sysclk_ind = get_sys_clk_index();
217
218         if (!dpll_data->ddr)
219                 return NULL;
220         return &dpll_data->ddr[sysclk_ind];
221 }
222
223 static void do_setup_dpll(u32 const base, const struct dpll_params *params,
224                                 u8 lock, char *dpll)
225 {
226         u32 temp, M, N;
227         struct dpll_regs *const dpll_regs = (struct dpll_regs *)base;
228
229         if (!params)
230                 return;
231
232         temp = readl(&dpll_regs->cm_clksel_dpll);
233
234         if (check_for_lock(base)) {
235                 /*
236                  * The Dpll has already been locked by rom code using CH.
237                  * Check if M,N are matching with Ideal nominal opp values.
238                  * If matches, skip the rest otherwise relock.
239                  */
240                 M = (temp & CM_CLKSEL_DPLL_M_MASK) >> CM_CLKSEL_DPLL_M_SHIFT;
241                 N = (temp & CM_CLKSEL_DPLL_N_MASK) >> CM_CLKSEL_DPLL_N_SHIFT;
242                 if ((M != (params->m)) || (N != (params->n))) {
243                         debug("\n %s Dpll locked, but not for ideal M = %d,"
244                                 "N = %d values, current values are M = %d,"
245                                 "N= %d" , dpll, params->m, params->n,
246                                 M, N);
247                 } else {
248                         /* Dpll locked with ideal values for nominal opps. */
249                         debug("\n %s Dpll already locked with ideal"
250                                                 "nominal opp values", dpll);
251                         goto setup_post_dividers;
252                 }
253         }
254
255         bypass_dpll(base);
256
257         /* Set M & N */
258         temp &= ~CM_CLKSEL_DPLL_M_MASK;
259         temp |= (params->m << CM_CLKSEL_DPLL_M_SHIFT) & CM_CLKSEL_DPLL_M_MASK;
260
261         temp &= ~CM_CLKSEL_DPLL_N_MASK;
262         temp |= (params->n << CM_CLKSEL_DPLL_N_SHIFT) & CM_CLKSEL_DPLL_N_MASK;
263
264         writel(temp, &dpll_regs->cm_clksel_dpll);
265
266         /* Lock */
267         if (lock)
268                 do_lock_dpll(base);
269
270 setup_post_dividers:
271         setup_post_dividers(base, params);
272
273         /* Wait till the DPLL locks */
274         if (lock)
275                 wait_for_lock(base);
276 }
277
278 u32 omap_ddr_clk(void)
279 {
280         u32 ddr_clk, sys_clk_khz, omap_rev, divider;
281         const struct dpll_params *core_dpll_params;
282
283         omap_rev = omap_revision();
284         sys_clk_khz = get_sys_clk_freq() / 1000;
285
286         core_dpll_params = get_core_dpll_params(*dplls_data);
287
288         debug("sys_clk %d\n ", sys_clk_khz * 1000);
289
290         /* Find Core DPLL locked frequency first */
291         ddr_clk = sys_clk_khz * 2 * core_dpll_params->m /
292                         (core_dpll_params->n + 1);
293
294         if (omap_rev < OMAP5430_ES1_0) {
295                 /*
296                  * DDR frequency is PHY_ROOT_CLK/2
297                  * PHY_ROOT_CLK = Fdpll/2/M2
298                  */
299                 divider = 4;
300         } else {
301                 /*
302                  * DDR frequency is PHY_ROOT_CLK
303                  * PHY_ROOT_CLK = Fdpll/2/M2
304                  */
305                 divider = 2;
306         }
307
308         ddr_clk = ddr_clk / divider / core_dpll_params->m2;
309         ddr_clk *= 1000;        /* convert to Hz */
310         debug("ddr_clk %d\n ", ddr_clk);
311
312         return ddr_clk;
313 }
314
315 /*
316  * Lock MPU dpll
317  *
318  * Resulting MPU frequencies:
319  * 4430 ES1.0   : 600 MHz
320  * 4430 ES2.x   : 792 MHz (OPP Turbo)
321  * 4460         : 920 MHz (OPP Turbo) - DCC disabled
322  */
323 void configure_mpu_dpll(void)
324 {
325         const struct dpll_params *params;
326         struct dpll_regs *mpu_dpll_regs;
327         u32 omap_rev;
328         omap_rev = omap_revision();
329
330         /*
331          * DCC and clock divider settings for 4460.
332          * DCC is required, if more than a certain frequency is required.
333          * For, 4460 > 1GHZ.
334          *     5430 > 1.4GHZ.
335          */
336         if ((omap_rev >= OMAP4460_ES1_0) && (omap_rev < OMAP5430_ES1_0)) {
337                 mpu_dpll_regs =
338                         (struct dpll_regs *)((*prcm)->cm_clkmode_dpll_mpu);
339                 bypass_dpll((*prcm)->cm_clkmode_dpll_mpu);
340                 clrbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
341                         MPU_CLKCTRL_CLKSEL_EMIF_DIV_MODE_MASK);
342                 setbits_le32((*prcm)->cm_mpu_mpu_clkctrl,
343                         MPU_CLKCTRL_CLKSEL_ABE_DIV_MODE_MASK);
344                 clrbits_le32(&mpu_dpll_regs->cm_clksel_dpll,
345                         CM_CLKSEL_DCC_EN_MASK);
346         }
347
348         params = get_mpu_dpll_params(*dplls_data);
349
350         do_setup_dpll((*prcm)->cm_clkmode_dpll_mpu, params, DPLL_LOCK, "mpu");
351         debug("MPU DPLL locked\n");
352 }
353
354 #ifdef CONFIG_USB_EHCI_OMAP
355 static void setup_usb_dpll(void)
356 {
357         const struct dpll_params *params;
358         u32 sys_clk_khz, sd_div, num, den;
359
360         sys_clk_khz = get_sys_clk_freq() / 1000;
361         /*
362          * USB:
363          * USB dpll is J-type. Need to set DPLL_SD_DIV for jitter correction
364          * DPLL_SD_DIV = CEILING ([DPLL_MULT/(DPLL_DIV+1)]* CLKINP / 250)
365          *      - where CLKINP is sys_clk in MHz
366          * Use CLKINP in KHz and adjust the denominator accordingly so
367          * that we have enough accuracy and at the same time no overflow
368          */
369         params = get_usb_dpll_params(*dplls_data);
370         num = params->m * sys_clk_khz;
371         den = (params->n + 1) * 250 * 1000;
372         num += den - 1;
373         sd_div = num / den;
374         clrsetbits_le32((*prcm)->cm_clksel_dpll_usb,
375                         CM_CLKSEL_DPLL_DPLL_SD_DIV_MASK,
376                         sd_div << CM_CLKSEL_DPLL_DPLL_SD_DIV_SHIFT);
377
378         /* Now setup the dpll with the regular function */
379         do_setup_dpll((*prcm)->cm_clkmode_dpll_usb, params, DPLL_LOCK, "usb");
380 }
381 #endif
382
383 static void setup_dplls(void)
384 {
385         u32 temp;
386         const struct dpll_params *params;
387
388         debug("setup_dplls\n");
389
390         /* CORE dpll */
391         params = get_core_dpll_params(*dplls_data);     /* default - safest */
392         /*
393          * Do not lock the core DPLL now. Just set it up.
394          * Core DPLL will be locked after setting up EMIF
395          * using the FREQ_UPDATE method(freq_update_core())
396          */
397         if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
398                 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
399                                                         DPLL_NO_LOCK, "core");
400         else
401                 do_setup_dpll((*prcm)->cm_clkmode_dpll_core, params,
402                                                         DPLL_LOCK, "core");
403         /* Set the ratios for CORE_CLK, L3_CLK, L4_CLK */
404         temp = (CLKSEL_CORE_X2_DIV_1 << CLKSEL_CORE_SHIFT) |
405             (CLKSEL_L3_CORE_DIV_2 << CLKSEL_L3_SHIFT) |
406             (CLKSEL_L4_L3_DIV_2 << CLKSEL_L4_SHIFT);
407         writel(temp, (*prcm)->cm_clksel_core);
408         debug("Core DPLL configured\n");
409
410         /* lock PER dpll */
411         params = get_per_dpll_params(*dplls_data);
412         do_setup_dpll((*prcm)->cm_clkmode_dpll_per,
413                         params, DPLL_LOCK, "per");
414         debug("PER DPLL locked\n");
415
416         /* MPU dpll */
417         configure_mpu_dpll();
418
419 #ifdef CONFIG_USB_EHCI_OMAP
420         setup_usb_dpll();
421 #endif
422         params = get_ddr_dpll_params(*dplls_data);
423         do_setup_dpll((*prcm)->cm_clkmode_dpll_ddrphy,
424                       params, DPLL_LOCK, "ddr");
425 }
426
427 #ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
428 static void setup_non_essential_dplls(void)
429 {
430         u32 abe_ref_clk;
431         const struct dpll_params *params;
432
433         /* IVA */
434         clrsetbits_le32((*prcm)->cm_bypclk_dpll_iva,
435                 CM_BYPCLK_DPLL_IVA_CLKSEL_MASK, DPLL_IVA_CLKSEL_CORE_X2_DIV_2);
436
437         params = get_iva_dpll_params(*dplls_data);
438         do_setup_dpll((*prcm)->cm_clkmode_dpll_iva, params, DPLL_LOCK, "iva");
439
440         /* Configure ABE dpll */
441         params = get_abe_dpll_params(*dplls_data);
442 #ifdef CONFIG_SYS_OMAP_ABE_SYSCK
443         abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_SYSCLK;
444 #else
445         abe_ref_clk = CM_ABE_PLL_REF_CLKSEL_CLKSEL_32KCLK;
446         /*
447          * We need to enable some additional options to achieve
448          * 196.608MHz from 32768 Hz
449          */
450         setbits_le32((*prcm)->cm_clkmode_dpll_abe,
451                         CM_CLKMODE_DPLL_DRIFTGUARD_EN_MASK|
452                         CM_CLKMODE_DPLL_RELOCK_RAMP_EN_MASK|
453                         CM_CLKMODE_DPLL_LPMODE_EN_MASK|
454                         CM_CLKMODE_DPLL_REGM4XEN_MASK);
455         /* Spend 4 REFCLK cycles at each stage */
456         clrsetbits_le32((*prcm)->cm_clkmode_dpll_abe,
457                         CM_CLKMODE_DPLL_RAMP_RATE_MASK,
458                         1 << CM_CLKMODE_DPLL_RAMP_RATE_SHIFT);
459 #endif
460
461         /* Select the right reference clk */
462         clrsetbits_le32((*prcm)->cm_abe_pll_ref_clksel,
463                         CM_ABE_PLL_REF_CLKSEL_CLKSEL_MASK,
464                         abe_ref_clk << CM_ABE_PLL_REF_CLKSEL_CLKSEL_SHIFT);
465         /* Lock the dpll */
466         do_setup_dpll((*prcm)->cm_clkmode_dpll_abe, params, DPLL_LOCK, "abe");
467 }
468 #endif
469
470 u32 get_offset_code(u32 volt_offset, struct pmic_data *pmic)
471 {
472         u32 offset_code;
473
474         volt_offset -= pmic->base_offset;
475
476         offset_code = (volt_offset + pmic->step - 1) / pmic->step;
477
478         /*
479          * Offset codes 1-6 all give the base voltage in Palmas
480          * Offset code 0 switches OFF the SMPS
481          */
482         return offset_code + pmic->start_code;
483 }
484
485 void do_scale_vcore(u32 vcore_reg, u32 volt_mv, struct pmic_data *pmic)
486 {
487         u32 offset_code;
488         u32 offset = volt_mv;
489         int ret = 0;
490
491         if (!volt_mv)
492                 return;
493
494         pmic->pmic_bus_init();
495         /* See if we can first get the GPIO if needed */
496         if (pmic->gpio_en)
497                 ret = gpio_request(pmic->gpio, "PMIC_GPIO");
498
499         if (ret < 0) {
500                 printf("%s: gpio %d request failed %d\n", __func__,
501                                                         pmic->gpio, ret);
502                 return;
503         }
504
505         /* Pull the GPIO low to select SET0 register, while we program SET1 */
506         if (pmic->gpio_en)
507                 gpio_direction_output(pmic->gpio, 0);
508
509         /* convert to uV for better accuracy in the calculations */
510         offset *= 1000;
511
512         offset_code = get_offset_code(offset, pmic);
513
514         debug("do_scale_vcore: volt - %d offset_code - 0x%x\n", volt_mv,
515                 offset_code);
516
517         if (pmic->pmic_write(pmic->i2c_slave_addr, vcore_reg, offset_code))
518                 printf("Scaling voltage failed for 0x%x\n", vcore_reg);
519
520         if (pmic->gpio_en)
521                 gpio_direction_output(pmic->gpio, 1);
522 }
523
524 static u32 optimize_vcore_voltage(struct volts const *v)
525 {
526         u32 val;
527         if (!v->value)
528                 return 0;
529         if (!v->efuse.reg)
530                 return v->value;
531
532         switch (v->efuse.reg_bits) {
533         case 16:
534                 val = readw(v->efuse.reg);
535                 break;
536         case 32:
537                 val = readl(v->efuse.reg);
538                 break;
539         default:
540                 printf("Error: efuse 0x%08x bits=%d unknown\n",
541                        v->efuse.reg, v->efuse.reg_bits);
542                 return v->value;
543         }
544
545         if (!val) {
546                 printf("Error: efuse 0x%08x bits=%d val=0, using %d\n",
547                        v->efuse.reg, v->efuse.reg_bits, v->value);
548                 return v->value;
549         }
550
551         debug("%s:efuse 0x%08x bits=%d Vnom=%d, using efuse value %d\n",
552               __func__, v->efuse.reg, v->efuse.reg_bits, v->value, val);
553         return val;
554 }
555
556 /*
557  * Setup the voltages for vdd_mpu, vdd_core, and vdd_iva
558  * We set the maximum voltages allowed here because Smart-Reflex is not
559  * enabled in bootloader. Voltage initialization in the kernel will set
560  * these to the nominal values after enabling Smart-Reflex
561  */
562 void scale_vcores(struct vcores_data const *vcores)
563 {
564         u32 val;
565
566         val = optimize_vcore_voltage(&vcores->core);
567         do_scale_vcore(vcores->core.addr, val, vcores->core.pmic);
568
569         val = optimize_vcore_voltage(&vcores->mpu);
570         do_scale_vcore(vcores->mpu.addr, val, vcores->mpu.pmic);
571
572         /* Configure MPU ABB LDO after scale */
573         abb_setup((*ctrl)->control_std_fuse_opp_vdd_mpu_2,
574                   (*ctrl)->control_wkup_ldovbb_mpu_voltage_ctrl,
575                   (*prcm)->prm_abbldo_mpu_setup,
576                   (*prcm)->prm_abbldo_mpu_ctrl,
577                   (*prcm)->prm_irqstatus_mpu_2,
578                   OMAP_ABB_MPU_TXDONE_MASK,
579                   OMAP_ABB_FAST_OPP);
580
581         val = optimize_vcore_voltage(&vcores->mm);
582         do_scale_vcore(vcores->mm.addr, val, vcores->mm.pmic);
583
584         val = optimize_vcore_voltage(&vcores->gpu);
585         do_scale_vcore(vcores->gpu.addr, val, vcores->gpu.pmic);
586
587         val = optimize_vcore_voltage(&vcores->eve);
588         do_scale_vcore(vcores->eve.addr, val, vcores->eve.pmic);
589
590         val = optimize_vcore_voltage(&vcores->iva);
591         do_scale_vcore(vcores->iva.addr, val, vcores->iva.pmic);
592
593          if (emif_sdram_type() == EMIF_SDRAM_TYPE_DDR3) {
594                 /* Configure LDO SRAM "magic" bits */
595                 writel(2, (*prcm)->prm_sldo_core_setup);
596                 writel(2, (*prcm)->prm_sldo_mpu_setup);
597                 writel(2, (*prcm)->prm_sldo_mm_setup);
598         }
599 }
600
601 static inline void enable_clock_domain(u32 const clkctrl_reg, u32 enable_mode)
602 {
603         clrsetbits_le32(clkctrl_reg, CD_CLKCTRL_CLKTRCTRL_MASK,
604                         enable_mode << CD_CLKCTRL_CLKTRCTRL_SHIFT);
605         debug("Enable clock domain - %x\n", clkctrl_reg);
606 }
607
608 static inline void wait_for_clk_enable(u32 clkctrl_addr)
609 {
610         u32 clkctrl, idlest = MODULE_CLKCTRL_IDLEST_DISABLED;
611         u32 bound = LDELAY;
612
613         while ((idlest == MODULE_CLKCTRL_IDLEST_DISABLED) ||
614                 (idlest == MODULE_CLKCTRL_IDLEST_TRANSITIONING)) {
615
616                 clkctrl = readl(clkctrl_addr);
617                 idlest = (clkctrl & MODULE_CLKCTRL_IDLEST_MASK) >>
618                          MODULE_CLKCTRL_IDLEST_SHIFT;
619                 if (--bound == 0) {
620                         printf("Clock enable failed for 0x%x idlest 0x%x\n",
621                                 clkctrl_addr, clkctrl);
622                         return;
623                 }
624         }
625 }
626
627 static inline void enable_clock_module(u32 const clkctrl_addr, u32 enable_mode,
628                                 u32 wait_for_enable)
629 {
630         clrsetbits_le32(clkctrl_addr, MODULE_CLKCTRL_MODULEMODE_MASK,
631                         enable_mode << MODULE_CLKCTRL_MODULEMODE_SHIFT);
632         debug("Enable clock module - %x\n", clkctrl_addr);
633         if (wait_for_enable)
634                 wait_for_clk_enable(clkctrl_addr);
635 }
636
637 void freq_update_core(void)
638 {
639         u32 freq_config1 = 0;
640         const struct dpll_params *core_dpll_params;
641         u32 omap_rev = omap_revision();
642
643         core_dpll_params = get_core_dpll_params(*dplls_data);
644         /* Put EMIF clock domain in sw wakeup mode */
645         enable_clock_domain((*prcm)->cm_memif_clkstctrl,
646                                 CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
647         wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
648         wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
649
650         freq_config1 = SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK |
651             SHADOW_FREQ_CONFIG1_DLL_RESET_MASK;
652
653         freq_config1 |= (DPLL_EN_LOCK << SHADOW_FREQ_CONFIG1_DPLL_EN_SHIFT) &
654                                 SHADOW_FREQ_CONFIG1_DPLL_EN_MASK;
655
656         freq_config1 |= (core_dpll_params->m2 <<
657                         SHADOW_FREQ_CONFIG1_M2_DIV_SHIFT) &
658                         SHADOW_FREQ_CONFIG1_M2_DIV_MASK;
659
660         writel(freq_config1, (*prcm)->cm_shadow_freq_config1);
661         if (!wait_on_value(SHADOW_FREQ_CONFIG1_FREQ_UPDATE_MASK, 0,
662                         (u32 *) (*prcm)->cm_shadow_freq_config1, LDELAY)) {
663                 puts("FREQ UPDATE procedure failed!!");
664                 hang();
665         }
666
667         /*
668          * Putting EMIF in HW_AUTO is seen to be causing issues with
669          * EMIF clocks and the master DLL. Keep EMIF in SW_WKUP
670          * in OMAP5430 ES1.0 silicon
671          */
672         if (omap_rev != OMAP5430_ES1_0) {
673                 /* Put EMIF clock domain back in hw auto mode */
674                 enable_clock_domain((*prcm)->cm_memif_clkstctrl,
675                                         CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
676                 wait_for_clk_enable((*prcm)->cm_memif_emif_1_clkctrl);
677                 wait_for_clk_enable((*prcm)->cm_memif_emif_2_clkctrl);
678         }
679 }
680
681 void bypass_dpll(u32 const base)
682 {
683         do_bypass_dpll(base);
684         wait_for_bypass(base);
685 }
686
687 void lock_dpll(u32 const base)
688 {
689         do_lock_dpll(base);
690         wait_for_lock(base);
691 }
692
693 void setup_clocks_for_console(void)
694 {
695         /* Do not add any spl_debug prints in this function */
696         clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
697                         CD_CLKCTRL_CLKTRCTRL_SW_WKUP <<
698                         CD_CLKCTRL_CLKTRCTRL_SHIFT);
699
700         /* Enable all UARTs - console will be on one of them */
701         clrsetbits_le32((*prcm)->cm_l4per_uart1_clkctrl,
702                         MODULE_CLKCTRL_MODULEMODE_MASK,
703                         MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
704                         MODULE_CLKCTRL_MODULEMODE_SHIFT);
705
706         clrsetbits_le32((*prcm)->cm_l4per_uart2_clkctrl,
707                         MODULE_CLKCTRL_MODULEMODE_MASK,
708                         MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
709                         MODULE_CLKCTRL_MODULEMODE_SHIFT);
710
711         clrsetbits_le32((*prcm)->cm_l4per_uart3_clkctrl,
712                         MODULE_CLKCTRL_MODULEMODE_MASK,
713                         MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
714                         MODULE_CLKCTRL_MODULEMODE_SHIFT);
715
716         clrsetbits_le32((*prcm)->cm_l4per_uart4_clkctrl,
717                         MODULE_CLKCTRL_MODULEMODE_MASK,
718                         MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN <<
719                         MODULE_CLKCTRL_MODULEMODE_SHIFT);
720
721         clrsetbits_le32((*prcm)->cm_l4per_clkstctrl, CD_CLKCTRL_CLKTRCTRL_MASK,
722                         CD_CLKCTRL_CLKTRCTRL_HW_AUTO <<
723                         CD_CLKCTRL_CLKTRCTRL_SHIFT);
724 }
725
726 void do_enable_clocks(u32 const *clk_domains,
727                             u32 const *clk_modules_hw_auto,
728                             u32 const *clk_modules_explicit_en,
729                             u8 wait_for_enable)
730 {
731         u32 i, max = 100;
732
733         /* Put the clock domains in SW_WKUP mode */
734         for (i = 0; (i < max) && clk_domains[i]; i++) {
735                 enable_clock_domain(clk_domains[i],
736                                     CD_CLKCTRL_CLKTRCTRL_SW_WKUP);
737         }
738
739         /* Clock modules that need to be put in HW_AUTO */
740         for (i = 0; (i < max) && clk_modules_hw_auto[i]; i++) {
741                 enable_clock_module(clk_modules_hw_auto[i],
742                                     MODULE_CLKCTRL_MODULEMODE_HW_AUTO,
743                                     wait_for_enable);
744         };
745
746         /* Clock modules that need to be put in SW_EXPLICIT_EN mode */
747         for (i = 0; (i < max) && clk_modules_explicit_en[i]; i++) {
748                 enable_clock_module(clk_modules_explicit_en[i],
749                                     MODULE_CLKCTRL_MODULEMODE_SW_EXPLICIT_EN,
750                                     wait_for_enable);
751         };
752
753         /* Put the clock domains in HW_AUTO mode now */
754         for (i = 0; (i < max) && clk_domains[i]; i++) {
755                 enable_clock_domain(clk_domains[i],
756                                     CD_CLKCTRL_CLKTRCTRL_HW_AUTO);
757         }
758 }
759
760 void prcm_init(void)
761 {
762         switch (omap_hw_init_context()) {
763         case OMAP_INIT_CONTEXT_SPL:
764         case OMAP_INIT_CONTEXT_UBOOT_FROM_NOR:
765         case OMAP_INIT_CONTEXT_UBOOT_AFTER_CH:
766                 enable_basic_clocks();
767                 timer_init();
768                 scale_vcores(*omap_vcores);
769                 setup_dplls();
770 #ifdef CONFIG_SYS_CLOCKS_ENABLE_ALL
771                 setup_non_essential_dplls();
772                 enable_non_essential_clocks();
773 #endif
774                 setup_warmreset_time();
775                 break;
776         default:
777                 break;
778         }
779
780         if (OMAP_INIT_CONTEXT_SPL != omap_hw_init_context())
781                 enable_basic_uboot_clocks();
782 }
783
784 void gpi2c_init(void)
785 {
786         static int gpi2c = 1;
787
788         if (gpi2c) {
789                 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
790                 gpi2c = 0;
791         }
792 }