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