]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/mx5/clock.c
mx51: Fix I2C clock ID check
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / mx5 / clock.c
1 /*
2  * (C) Copyright 2007
3  * Sascha Hauer, Pengutronix
4  *
5  * (C) Copyright 2009 Freescale Semiconductor, Inc.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #include <common.h>
27 #include <asm/io.h>
28 #include <asm/errno.h>
29 #include <asm/arch/imx-regs.h>
30 #include <asm/arch/crm_regs.h>
31 #include <asm/arch/clock.h>
32 #include <div64.h>
33 #include <asm/arch/sys_proto.h>
34
35 enum pll_clocks {
36         PLL1_CLOCK = 0,
37         PLL2_CLOCK,
38         PLL3_CLOCK,
39 #ifdef CONFIG_MX53
40         PLL4_CLOCK,
41 #endif
42         PLL_CLOCKS,
43 };
44
45 struct mxc_pll_reg *mxc_plls[PLL_CLOCKS] = {
46         [PLL1_CLOCK] = (struct mxc_pll_reg *)PLL1_BASE_ADDR,
47         [PLL2_CLOCK] = (struct mxc_pll_reg *)PLL2_BASE_ADDR,
48         [PLL3_CLOCK] = (struct mxc_pll_reg *)PLL3_BASE_ADDR,
49 #ifdef  CONFIG_MX53
50         [PLL4_CLOCK] = (struct mxc_pll_reg *)PLL4_BASE_ADDR,
51 #endif
52 };
53
54 #define AHB_CLK_ROOT    133333333
55 #define SZ_DEC_1M       1000000
56 #define PLL_PD_MAX      16      /* Actual pd+1 */
57 #define PLL_MFI_MAX     15
58 #define PLL_MFI_MIN     5
59 #define ARM_DIV_MAX     8
60 #define IPG_DIV_MAX     4
61 #define AHB_DIV_MAX     8
62 #define EMI_DIV_MAX     8
63 #define NFC_DIV_MAX     8
64
65 #define MX5_CBCMR       0x00015154
66 #define MX5_CBCDR       0x02888945
67
68 struct fixed_pll_mfd {
69         u32 ref_clk_hz;
70         u32 mfd;
71 };
72
73 const struct fixed_pll_mfd fixed_mfd[] = {
74         {MXC_HCLK, 24 * 16},
75 };
76
77 struct pll_param {
78         u32 pd;
79         u32 mfi;
80         u32 mfn;
81         u32 mfd;
82 };
83
84 #define PLL_FREQ_MAX(ref_clk)  (4 * (ref_clk) * PLL_MFI_MAX)
85 #define PLL_FREQ_MIN(ref_clk) \
86                 ((2 * (ref_clk) * (PLL_MFI_MIN - 1)) / PLL_PD_MAX)
87 #define MAX_DDR_CLK     420000000
88 #define NFC_CLK_MAX     34000000
89
90 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE;
91
92 void set_usboh3_clk(void)
93 {
94         clrsetbits_le32(&mxc_ccm->cscmr1,
95                         MXC_CCM_CSCMR1_USBOH3_CLK_SEL_MASK,
96                         MXC_CCM_CSCMR1_USBOH3_CLK_SEL(1));
97         clrsetbits_le32(&mxc_ccm->cscdr1,
98                         MXC_CCM_CSCDR1_USBOH3_CLK_PODF_MASK |
99                         MXC_CCM_CSCDR1_USBOH3_CLK_PRED_MASK,
100                         MXC_CCM_CSCDR1_USBOH3_CLK_PRED(4) |
101                         MXC_CCM_CSCDR1_USBOH3_CLK_PODF(1));
102 }
103
104 void enable_usboh3_clk(unsigned char enable)
105 {
106         unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
107
108         clrsetbits_le32(&mxc_ccm->CCGR2,
109                         MXC_CCM_CCGR2_USBOH3_60M(MXC_CCM_CCGR_CG_MASK),
110                         MXC_CCM_CCGR2_USBOH3_60M(cg));
111 }
112
113 #ifdef CONFIG_I2C_MXC
114 /* i2c_num can be from 0, to 1 for i.MX51 and 2 for i.MX53 */
115 int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
116 {
117         u32 mask;
118
119 #if defined(CONFIG_MX51)
120         if (i2c_num > 1)
121 #elif defined(CONFIG_MX53)
122         if (i2c_num > 2)
123 #endif
124                 return -EINVAL;
125         mask = MXC_CCM_CCGR_CG_MASK <<
126                         (MXC_CCM_CCGR1_I2C1_OFFSET + (i2c_num << 1));
127         if (enable)
128                 setbits_le32(&mxc_ccm->CCGR1, mask);
129         else
130                 clrbits_le32(&mxc_ccm->CCGR1, mask);
131         return 0;
132 }
133 #endif
134
135 void set_usb_phy_clk(void)
136 {
137         clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL);
138 }
139
140 #if defined(CONFIG_MX51)
141 void enable_usb_phy1_clk(unsigned char enable)
142 {
143         unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
144
145         clrsetbits_le32(&mxc_ccm->CCGR2,
146                         MXC_CCM_CCGR2_USB_PHY(MXC_CCM_CCGR_CG_MASK),
147                         MXC_CCM_CCGR2_USB_PHY(cg));
148 }
149
150 void enable_usb_phy2_clk(unsigned char enable)
151 {
152         /* i.MX51 has a single USB PHY clock, so do nothing here. */
153 }
154 #elif defined(CONFIG_MX53)
155 void enable_usb_phy1_clk(unsigned char enable)
156 {
157         unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
158
159         clrsetbits_le32(&mxc_ccm->CCGR4,
160                         MXC_CCM_CCGR4_USB_PHY1(MXC_CCM_CCGR_CG_MASK),
161                         MXC_CCM_CCGR4_USB_PHY1(cg));
162 }
163
164 void enable_usb_phy2_clk(unsigned char enable)
165 {
166         unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
167
168         clrsetbits_le32(&mxc_ccm->CCGR4,
169                         MXC_CCM_CCGR4_USB_PHY2(MXC_CCM_CCGR_CG_MASK),
170                         MXC_CCM_CCGR4_USB_PHY2(cg));
171 }
172 #endif
173
174 /*
175  * Calculate the frequency of PLLn.
176  */
177 static uint32_t decode_pll(struct mxc_pll_reg *pll, uint32_t infreq)
178 {
179         uint32_t ctrl, op, mfd, mfn, mfi, pdf, ret;
180         uint64_t refclk, temp;
181         int32_t mfn_abs;
182
183         ctrl = readl(&pll->ctrl);
184
185         if (ctrl & MXC_DPLLC_CTL_HFSM) {
186                 mfn = readl(&pll->hfs_mfn);
187                 mfd = readl(&pll->hfs_mfd);
188                 op = readl(&pll->hfs_op);
189         } else {
190                 mfn = readl(&pll->mfn);
191                 mfd = readl(&pll->mfd);
192                 op = readl(&pll->op);
193         }
194
195         mfd &= MXC_DPLLC_MFD_MFD_MASK;
196         mfn &= MXC_DPLLC_MFN_MFN_MASK;
197         pdf = op & MXC_DPLLC_OP_PDF_MASK;
198         mfi = MXC_DPLLC_OP_MFI_RD(op);
199
200         /* 21.2.3 */
201         if (mfi < 5)
202                 mfi = 5;
203
204         /* Sign extend */
205         if (mfn >= 0x04000000) {
206                 mfn |= 0xfc000000;
207                 mfn_abs = -mfn;
208         } else
209                 mfn_abs = mfn;
210
211         refclk = infreq * 2;
212         if (ctrl & MXC_DPLLC_CTL_DPDCK0_2_EN)
213                 refclk *= 2;
214
215         do_div(refclk, pdf + 1);
216         temp = refclk * mfn_abs;
217         do_div(temp, mfd + 1);
218         ret = refclk * mfi;
219
220         if ((int)mfn < 0)
221                 ret -= temp;
222         else
223                 ret += temp;
224
225         return ret;
226 }
227
228 #ifdef CONFIG_MX51
229 /*
230  * This function returns the Frequency Pre-Multiplier clock.
231  */
232 static u32 get_fpm(void)
233 {
234         u32 mult;
235         u32 ccr = readl(&mxc_ccm->ccr);
236
237         if (ccr & MXC_CCM_CCR_FPM_MULT)
238                 mult = 1024;
239         else
240                 mult = 512;
241
242         return MXC_CLK32 * mult;
243 }
244 #endif
245
246 /*
247  * This function returns the low power audio clock.
248  */
249 static u32 get_lp_apm(void)
250 {
251         u32 ret_val = 0;
252         u32 ccsr = readl(&mxc_ccm->ccsr);
253
254         if (ccsr & MXC_CCM_CCSR_LP_APM)
255 #if defined(CONFIG_MX51)
256                 ret_val = get_fpm();
257 #elif defined(CONFIG_MX53)
258                 ret_val = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
259 #endif
260         else
261                 ret_val = MXC_HCLK;
262
263         return ret_val;
264 }
265
266 /*
267  * Get mcu main rate
268  */
269 u32 get_mcu_main_clk(void)
270 {
271         u32 reg, freq;
272
273         reg = MXC_CCM_CACRR_ARM_PODF_RD(readl(&mxc_ccm->cacrr));
274         freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
275         return freq / (reg + 1);
276 }
277
278 /*
279  * Get the rate of peripheral's root clock.
280  */
281 u32 get_periph_clk(void)
282 {
283         u32 reg;
284
285         reg = readl(&mxc_ccm->cbcdr);
286         if (!(reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL))
287                 return decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
288         reg = readl(&mxc_ccm->cbcmr);
289         switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(reg)) {
290         case 0:
291                 return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
292         case 1:
293                 return decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
294         case 2:
295                 return get_lp_apm();
296         default:
297                 return 0;
298         }
299         /* NOTREACHED */
300 }
301
302 /*
303  * Get the rate of ipg clock.
304  */
305 static u32 get_ipg_clk(void)
306 {
307         uint32_t freq, reg, div;
308
309         freq = get_ahb_clk();
310
311         reg = readl(&mxc_ccm->cbcdr);
312         div = MXC_CCM_CBCDR_IPG_PODF_RD(reg) + 1;
313
314         return freq / div;
315 }
316
317 /*
318  * Get the rate of ipg_per clock.
319  */
320 static u32 get_ipg_per_clk(void)
321 {
322         u32 freq, pred1, pred2, podf;
323
324         if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_IPG_CLK_SEL)
325                 return get_ipg_clk();
326
327         if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_LP_APM_CLK_SEL)
328                 freq = get_lp_apm();
329         else
330                 freq = get_periph_clk();
331         podf = readl(&mxc_ccm->cbcdr);
332         pred1 = MXC_CCM_CBCDR_PERCLK_PRED1_RD(podf);
333         pred2 = MXC_CCM_CBCDR_PERCLK_PRED2_RD(podf);
334         podf = MXC_CCM_CBCDR_PERCLK_PODF_RD(podf);
335         return freq / ((pred1 + 1) * (pred2 + 1) * (podf + 1));
336 }
337
338 /* Get the output clock rate of a standard PLL MUX for peripherals. */
339 static u32 get_standard_pll_sel_clk(u32 clk_sel)
340 {
341         u32 freq;
342
343         switch (clk_sel & 0x3) {
344         case 0:
345                 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
346                 break;
347         case 1:
348                 freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
349                 break;
350         case 2:
351                 freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
352                 break;
353         case 3:
354                 freq = get_lp_apm();
355                 break;
356         }
357
358         return freq;
359 }
360
361 /*
362  * Get the rate of uart clk.
363  */
364 static u32 get_uart_clk(void)
365 {
366         unsigned int clk_sel, freq, reg, pred, podf;
367
368         reg = readl(&mxc_ccm->cscmr1);
369         clk_sel = MXC_CCM_CSCMR1_UART_CLK_SEL_RD(reg);
370         freq = get_standard_pll_sel_clk(clk_sel);
371
372         reg = readl(&mxc_ccm->cscdr1);
373         pred = MXC_CCM_CSCDR1_UART_CLK_PRED_RD(reg);
374         podf = MXC_CCM_CSCDR1_UART_CLK_PODF_RD(reg);
375         freq /= (pred + 1) * (podf + 1);
376
377         return freq;
378 }
379
380 /*
381  * get cspi clock rate.
382  */
383 static u32 imx_get_cspiclk(void)
384 {
385         u32 ret_val = 0, pdf, pre_pdf, clk_sel, freq;
386         u32 cscmr1 = readl(&mxc_ccm->cscmr1);
387         u32 cscdr2 = readl(&mxc_ccm->cscdr2);
388
389         pre_pdf = MXC_CCM_CSCDR2_CSPI_CLK_PRED_RD(cscdr2);
390         pdf = MXC_CCM_CSCDR2_CSPI_CLK_PODF_RD(cscdr2);
391         clk_sel = MXC_CCM_CSCMR1_CSPI_CLK_SEL_RD(cscmr1);
392         freq = get_standard_pll_sel_clk(clk_sel);
393         ret_val = freq / ((pre_pdf + 1) * (pdf + 1));
394         return ret_val;
395 }
396
397 static u32 get_axi_a_clk(void)
398 {
399         u32 cbcdr = readl(&mxc_ccm->cbcdr);
400         u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr);
401
402         return  get_periph_clk() / (pdf + 1);
403 }
404
405 static u32 get_axi_b_clk(void)
406 {
407         u32 cbcdr = readl(&mxc_ccm->cbcdr);
408         u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr);
409
410         return  get_periph_clk() / (pdf + 1);
411 }
412
413 static u32 get_emi_slow_clk(void)
414 {
415         u32 cbcdr = readl(&mxc_ccm->cbcdr);
416         u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL;
417         u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr);
418
419         if (emi_clk_sel)
420                 return  get_ahb_clk() / (pdf + 1);
421
422         return  get_periph_clk() / (pdf + 1);
423 }
424
425 static u32 get_ddr_clk(void)
426 {
427         u32 ret_val = 0;
428         u32 cbcmr = readl(&mxc_ccm->cbcmr);
429         u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
430 #ifdef CONFIG_MX51
431         u32 cbcdr = readl(&mxc_ccm->cbcdr);
432         if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) {
433                 u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr);
434
435                 ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
436                 ret_val /= ddr_clk_podf + 1;
437
438                 return ret_val;
439         }
440 #endif
441         switch (ddr_clk_sel) {
442         case 0:
443                 ret_val = get_axi_a_clk();
444                 break;
445         case 1:
446                 ret_val = get_axi_b_clk();
447                 break;
448         case 2:
449                 ret_val = get_emi_slow_clk();
450                 break;
451         case 3:
452                 ret_val = get_ahb_clk();
453                 break;
454         default:
455                 break;
456         }
457
458         return ret_val;
459 }
460
461 /*
462  * The API of get mxc clocks.
463  */
464 unsigned int mxc_get_clock(enum mxc_clock clk)
465 {
466         switch (clk) {
467         case MXC_ARM_CLK:
468                 return get_mcu_main_clk();
469         case MXC_AHB_CLK:
470                 return get_ahb_clk();
471         case MXC_IPG_CLK:
472                 return get_ipg_clk();
473         case MXC_IPG_PERCLK:
474         case MXC_I2C_CLK:
475                 return get_ipg_per_clk();
476         case MXC_UART_CLK:
477                 return get_uart_clk();
478         case MXC_CSPI_CLK:
479                 return imx_get_cspiclk();
480         case MXC_FEC_CLK:
481                 return get_ipg_clk();
482         case MXC_SATA_CLK:
483                 return get_ahb_clk();
484         case MXC_DDR_CLK:
485                 return get_ddr_clk();
486         default:
487                 break;
488         }
489         return -EINVAL;
490 }
491
492 u32 imx_get_uartclk(void)
493 {
494         return get_uart_clk();
495 }
496
497 u32 imx_get_fecclk(void)
498 {
499         return get_ipg_clk();
500 }
501
502 static int gcd(int m, int n)
503 {
504         int t;
505         while (m > 0) {
506                 if (n > m) {
507                         t = m;
508                         m = n;
509                         n = t;
510                 } /* swap */
511                 m -= n;
512         }
513         return n;
514 }
515
516 /*
517  * This is to calculate various parameters based on reference clock and
518  * targeted clock based on the equation:
519  *      t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1)
520  * This calculation is based on a fixed MFD value for simplicity.
521  */
522 static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll)
523 {
524         u64 pd, mfi = 1, mfn, mfd, t1;
525         u32 n_target = target;
526         u32 n_ref = ref, i;
527
528         /*
529          * Make sure targeted freq is in the valid range.
530          * Otherwise the following calculation might be wrong!!!
531          */
532         if (n_target < PLL_FREQ_MIN(ref) ||
533                 n_target > PLL_FREQ_MAX(ref)) {
534                 printf("Targeted peripheral clock should be"
535                         "within [%d - %d]\n",
536                         PLL_FREQ_MIN(ref) / SZ_DEC_1M,
537                         PLL_FREQ_MAX(ref) / SZ_DEC_1M);
538                 return -EINVAL;
539         }
540
541         for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) {
542                 if (fixed_mfd[i].ref_clk_hz == ref) {
543                         mfd = fixed_mfd[i].mfd;
544                         break;
545                 }
546         }
547
548         if (i == ARRAY_SIZE(fixed_mfd))
549                 return -EINVAL;
550
551         /* Use n_target and n_ref to avoid overflow */
552         for (pd = 1; pd <= PLL_PD_MAX; pd++) {
553                 t1 = n_target * pd;
554                 do_div(t1, (4 * n_ref));
555                 mfi = t1;
556                 if (mfi > PLL_MFI_MAX)
557                         return -EINVAL;
558                 else if (mfi < 5)
559                         continue;
560                 break;
561         }
562         /*
563          * Now got pd and mfi already
564          *
565          * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref;
566          */
567         t1 = n_target * pd;
568         do_div(t1, 4);
569         t1 -= n_ref * mfi;
570         t1 *= mfd;
571         do_div(t1, n_ref);
572         mfn = t1;
573         debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n",
574                 ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd);
575         i = 1;
576         if (mfn != 0)
577                 i = gcd(mfd, mfn);
578         pll->pd = (u32)pd;
579         pll->mfi = (u32)mfi;
580         do_div(mfn, i);
581         pll->mfn = (u32)mfn;
582         do_div(mfd, i);
583         pll->mfd = (u32)mfd;
584
585         return 0;
586 }
587
588 #define calc_div(tgt_clk, src_clk, limit) ({            \
589                 u32 v = 0;                              \
590                 if (((src_clk) % (tgt_clk)) <= 100)     \
591                         v = (src_clk) / (tgt_clk);      \
592                 else                                    \
593                         v = ((src_clk) / (tgt_clk)) + 1;\
594                 if (v > limit)                          \
595                         v = limit;                      \
596                 (v - 1);                                \
597         })
598
599 #define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \
600         {       \
601                 writel(0x1232, &pll->ctrl);             \
602                 writel(0x2, &pll->config);              \
603                 writel((((pd) - 1) << 0) | ((fi) << 4), \
604                         &pll->op);                      \
605                 writel(fn, &(pll->mfn));                \
606                 writel((fd) - 1, &pll->mfd);            \
607                 writel((((pd) - 1) << 0) | ((fi) << 4), \
608                         &pll->hfs_op);                  \
609                 writel(fn, &pll->hfs_mfn);              \
610                 writel((fd) - 1, &pll->hfs_mfd);        \
611                 writel(0x1232, &pll->ctrl);             \
612                 while (!readl(&pll->ctrl) & 0x1)        \
613                         ;\
614         }
615
616 static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param)
617 {
618         u32 ccsr = readl(&mxc_ccm->ccsr);
619         struct mxc_pll_reg *pll = mxc_plls[index];
620
621         switch (index) {
622         case PLL1_CLOCK:
623                 /* Switch ARM to PLL2 clock */
624                 writel(ccsr | MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
625                                 &mxc_ccm->ccsr);
626                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
627                                         pll_param->mfi, pll_param->mfn,
628                                         pll_param->mfd);
629                 /* Switch back */
630                 writel(ccsr & ~MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
631                                 &mxc_ccm->ccsr);
632                 break;
633         case PLL2_CLOCK:
634                 /* Switch to pll2 bypass clock */
635                 writel(ccsr | MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
636                                 &mxc_ccm->ccsr);
637                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
638                                         pll_param->mfi, pll_param->mfn,
639                                         pll_param->mfd);
640                 /* Switch back */
641                 writel(ccsr & ~MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
642                                 &mxc_ccm->ccsr);
643                 break;
644         case PLL3_CLOCK:
645                 /* Switch to pll3 bypass clock */
646                 writel(ccsr | MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
647                                 &mxc_ccm->ccsr);
648                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
649                                         pll_param->mfi, pll_param->mfn,
650                                         pll_param->mfd);
651                 /* Switch back */
652                 writel(ccsr & ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
653                                 &mxc_ccm->ccsr);
654                 break;
655 #ifdef CONFIG_MX53
656         case PLL4_CLOCK:
657                 /* Switch to pll4 bypass clock */
658                 writel(ccsr | MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
659                                 &mxc_ccm->ccsr);
660                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
661                                         pll_param->mfi, pll_param->mfn,
662                                         pll_param->mfd);
663                 /* Switch back */
664                 writel(ccsr & ~MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
665                                 &mxc_ccm->ccsr);
666                 break;
667 #endif
668         default:
669                 return -EINVAL;
670         }
671
672         return 0;
673 }
674
675 /* Config CPU clock */
676 static int config_core_clk(u32 ref, u32 freq)
677 {
678         int ret = 0;
679         struct pll_param pll_param;
680
681         memset(&pll_param, 0, sizeof(struct pll_param));
682
683         /* The case that periph uses PLL1 is not considered here */
684         ret = calc_pll_params(ref, freq, &pll_param);
685         if (ret != 0) {
686                 printf("Error:Can't find pll parameters: %d\n", ret);
687                 return ret;
688         }
689
690         return config_pll_clk(PLL1_CLOCK, &pll_param);
691 }
692
693 static int config_nfc_clk(u32 nfc_clk)
694 {
695         u32 parent_rate = get_emi_slow_clk();
696         u32 div = parent_rate / nfc_clk;
697
698         if (nfc_clk <= 0)
699                 return -EINVAL;
700         if (div == 0)
701                 div++;
702         if (parent_rate / div > NFC_CLK_MAX)
703                 div++;
704         clrsetbits_le32(&mxc_ccm->cbcdr,
705                         MXC_CCM_CBCDR_NFC_PODF_MASK,
706                         MXC_CCM_CBCDR_NFC_PODF(div - 1));
707         while (readl(&mxc_ccm->cdhipr) != 0)
708                 ;
709         return 0;
710 }
711
712 /* Config main_bus_clock for periphs */
713 static int config_periph_clk(u32 ref, u32 freq)
714 {
715         int ret = 0;
716         struct pll_param pll_param;
717
718         memset(&pll_param, 0, sizeof(struct pll_param));
719
720         if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
721                 ret = calc_pll_params(ref, freq, &pll_param);
722                 if (ret != 0) {
723                         printf("Error:Can't find pll parameters: %d\n",
724                                 ret);
725                         return ret;
726                 }
727                 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(
728                                 readl(&mxc_ccm->cbcmr))) {
729                 case 0:
730                         return config_pll_clk(PLL1_CLOCK, &pll_param);
731                         break;
732                 case 1:
733                         return config_pll_clk(PLL3_CLOCK, &pll_param);
734                         break;
735                 default:
736                         return -EINVAL;
737                 }
738         }
739
740         return 0;
741 }
742
743 static int config_ddr_clk(u32 emi_clk)
744 {
745         u32 clk_src;
746         s32 shift = 0, clk_sel, div = 1;
747         u32 cbcmr = readl(&mxc_ccm->cbcmr);
748
749         if (emi_clk > MAX_DDR_CLK) {
750                 printf("Warning:DDR clock should not exceed %d MHz\n",
751                         MAX_DDR_CLK / SZ_DEC_1M);
752                 emi_clk = MAX_DDR_CLK;
753         }
754
755         clk_src = get_periph_clk();
756         /* Find DDR clock input */
757         clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
758         switch (clk_sel) {
759         case 0:
760                 shift = 16;
761                 break;
762         case 1:
763                 shift = 19;
764                 break;
765         case 2:
766                 shift = 22;
767                 break;
768         case 3:
769                 shift = 10;
770                 break;
771         default:
772                 return -EINVAL;
773         }
774
775         if ((clk_src % emi_clk) < 10000000)
776                 div = clk_src / emi_clk;
777         else
778                 div = (clk_src / emi_clk) + 1;
779         if (div > 8)
780                 div = 8;
781
782         clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift);
783         while (readl(&mxc_ccm->cdhipr) != 0)
784                 ;
785         writel(0x0, &mxc_ccm->ccdr);
786
787         return 0;
788 }
789
790 /*
791  * This function assumes the expected core clock has to be changed by
792  * modifying the PLL. This is NOT true always but for most of the times,
793  * it is. So it assumes the PLL output freq is the same as the expected
794  * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN.
795  * In the latter case, it will try to increase the presc value until
796  * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to
797  * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based
798  * on the targeted PLL and reference input clock to the PLL. Lastly,
799  * it sets the register based on these values along with the dividers.
800  * Note 1) There is no value checking for the passed-in divider values
801  *         so the caller has to make sure those values are sensible.
802  *      2) Also adjust the NFC divider such that the NFC clock doesn't
803  *         exceed NFC_CLK_MAX.
804  *      3) IPU HSP clock is independent of AHB clock. Even it can go up to
805  *         177MHz for higher voltage, this function fixes the max to 133MHz.
806  *      4) This function should not have allowed diag_printf() calls since
807  *         the serial driver has been stoped. But leave then here to allow
808  *         easy debugging by NOT calling the cyg_hal_plf_serial_stop().
809  */
810 int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk)
811 {
812         freq *= SZ_DEC_1M;
813
814         switch (clk) {
815         case MXC_ARM_CLK:
816                 if (config_core_clk(ref, freq))
817                         return -EINVAL;
818                 break;
819         case MXC_PERIPH_CLK:
820                 if (config_periph_clk(ref, freq))
821                         return -EINVAL;
822                 break;
823         case MXC_DDR_CLK:
824                 if (config_ddr_clk(freq))
825                         return -EINVAL;
826                 break;
827         case MXC_NFC_CLK:
828                 if (config_nfc_clk(freq))
829                         return -EINVAL;
830                 break;
831         default:
832                 printf("Warning:Unsupported or invalid clock type\n");
833         }
834
835         return 0;
836 }
837
838 #ifdef CONFIG_MX53
839 /*
840  * The clock for the external interface can be set to use internal clock
841  * if fuse bank 4, row 3, bit 2 is set.
842  * This is an undocumented feature and it was confirmed by Freescale's support:
843  * Fuses (but not pins) may be used to configure SATA clocks.
844  * Particularly the i.MX53 Fuse_Map contains the next information
845  * about configuring SATA clocks :  SATA_ALT_REF_CLK[1:0] (offset 0x180C)
846  * '00' - 100MHz (External)
847  * '01' - 50MHz (External)
848  * '10' - 120MHz, internal (USB PHY)
849  * '11' - Reserved
850 */
851 void mxc_set_sata_internal_clock(void)
852 {
853         u32 *tmp_base =
854                 (u32 *)(IIM_BASE_ADDR + 0x180c);
855
856         set_usb_phy_clk();
857
858         clrsetbits_le32(tmp_base, 0x6, 0x4);
859 }
860 #endif
861
862 /*
863  * Dump some core clockes.
864  */
865 int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
866 {
867         u32 freq;
868
869         freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
870         printf("PLL1       %8d MHz\n", freq / 1000000);
871         freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
872         printf("PLL2       %8d MHz\n", freq / 1000000);
873         freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
874         printf("PLL3       %8d MHz\n", freq / 1000000);
875 #ifdef  CONFIG_MX53
876         freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
877         printf("PLL4       %8d MHz\n", freq / 1000000);
878 #endif
879
880         printf("\n");
881         printf("AHB        %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
882         printf("IPG        %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
883         printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
884         printf("DDR        %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
885
886         return 0;
887 }
888
889 /***************************************************/
890
891 U_BOOT_CMD(
892         clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks,
893         "display clocks",
894         ""
895 );