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