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