]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/mx5/clock.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[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 = 0;
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 /*
398  * get esdhc clock rate.
399  */
400 static u32 get_esdhc_clk(u32 port)
401 {
402         u32 clk_sel = 0, pred = 0, podf = 0, freq = 0;
403         u32 cscmr1 = readl(&mxc_ccm->cscmr1);
404         u32 cscdr1 = readl(&mxc_ccm->cscdr1);
405
406         switch (port) {
407         case 0:
408                 clk_sel = MXC_CCM_CSCMR1_ESDHC1_MSHC1_CLK_SEL_RD(cscmr1);
409                 pred = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PRED_RD(cscdr1);
410                 podf = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PODF_RD(cscdr1);
411                 break;
412         case 1:
413                 clk_sel = MXC_CCM_CSCMR1_ESDHC2_MSHC2_CLK_SEL_RD(cscmr1);
414                 pred = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PRED_RD(cscdr1);
415                 podf = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PODF_RD(cscdr1);
416                 break;
417         case 2:
418                 if (cscmr1 & MXC_CCM_CSCMR1_ESDHC3_CLK_SEL)
419                         return get_esdhc_clk(1);
420                 else
421                         return get_esdhc_clk(0);
422         case 3:
423                 if (cscmr1 & MXC_CCM_CSCMR1_ESDHC4_CLK_SEL)
424                         return get_esdhc_clk(1);
425                 else
426                         return get_esdhc_clk(0);
427         default:
428                 break;
429         }
430
431         freq = get_standard_pll_sel_clk(clk_sel) / ((pred + 1) * (podf + 1));
432         return freq;
433 }
434
435 static u32 get_axi_a_clk(void)
436 {
437         u32 cbcdr = readl(&mxc_ccm->cbcdr);
438         u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr);
439
440         return  get_periph_clk() / (pdf + 1);
441 }
442
443 static u32 get_axi_b_clk(void)
444 {
445         u32 cbcdr = readl(&mxc_ccm->cbcdr);
446         u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr);
447
448         return  get_periph_clk() / (pdf + 1);
449 }
450
451 static u32 get_emi_slow_clk(void)
452 {
453         u32 cbcdr = readl(&mxc_ccm->cbcdr);
454         u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL;
455         u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr);
456
457         if (emi_clk_sel)
458                 return  get_ahb_clk() / (pdf + 1);
459
460         return  get_periph_clk() / (pdf + 1);
461 }
462
463 static u32 get_ddr_clk(void)
464 {
465         u32 ret_val = 0;
466         u32 cbcmr = readl(&mxc_ccm->cbcmr);
467         u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
468 #ifdef CONFIG_MX51
469         u32 cbcdr = readl(&mxc_ccm->cbcdr);
470         if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) {
471                 u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr);
472
473                 ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
474                 ret_val /= ddr_clk_podf + 1;
475
476                 return ret_val;
477         }
478 #endif
479         switch (ddr_clk_sel) {
480         case 0:
481                 ret_val = get_axi_a_clk();
482                 break;
483         case 1:
484                 ret_val = get_axi_b_clk();
485                 break;
486         case 2:
487                 ret_val = get_emi_slow_clk();
488                 break;
489         case 3:
490                 ret_val = get_ahb_clk();
491                 break;
492         default:
493                 break;
494         }
495
496         return ret_val;
497 }
498
499 /*
500  * The API of get mxc clocks.
501  */
502 unsigned int mxc_get_clock(enum mxc_clock clk)
503 {
504         switch (clk) {
505         case MXC_ARM_CLK:
506                 return get_mcu_main_clk();
507         case MXC_AHB_CLK:
508                 return get_ahb_clk();
509         case MXC_IPG_CLK:
510                 return get_ipg_clk();
511         case MXC_IPG_PERCLK:
512         case MXC_I2C_CLK:
513                 return get_ipg_per_clk();
514         case MXC_UART_CLK:
515                 return get_uart_clk();
516         case MXC_CSPI_CLK:
517                 return imx_get_cspiclk();
518         case MXC_ESDHC_CLK:
519                 return get_esdhc_clk(0);
520         case MXC_ESDHC2_CLK:
521                 return get_esdhc_clk(1);
522         case MXC_ESDHC3_CLK:
523                 return get_esdhc_clk(2);
524         case MXC_ESDHC4_CLK:
525                 return get_esdhc_clk(3);
526         case MXC_FEC_CLK:
527                 return get_ipg_clk();
528         case MXC_SATA_CLK:
529                 return get_ahb_clk();
530         case MXC_DDR_CLK:
531                 return get_ddr_clk();
532         default:
533                 break;
534         }
535         return -EINVAL;
536 }
537
538 u32 imx_get_uartclk(void)
539 {
540         return get_uart_clk();
541 }
542
543 u32 imx_get_fecclk(void)
544 {
545         return get_ipg_clk();
546 }
547
548 static int gcd(int m, int n)
549 {
550         int t;
551         while (m > 0) {
552                 if (n > m) {
553                         t = m;
554                         m = n;
555                         n = t;
556                 } /* swap */
557                 m -= n;
558         }
559         return n;
560 }
561
562 /*
563  * This is to calculate various parameters based on reference clock and
564  * targeted clock based on the equation:
565  *      t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1)
566  * This calculation is based on a fixed MFD value for simplicity.
567  */
568 static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll)
569 {
570         u64 pd, mfi = 1, mfn, mfd, t1;
571         u32 n_target = target;
572         u32 n_ref = ref, i;
573
574         /*
575          * Make sure targeted freq is in the valid range.
576          * Otherwise the following calculation might be wrong!!!
577          */
578         if (n_target < PLL_FREQ_MIN(ref) ||
579                 n_target > PLL_FREQ_MAX(ref)) {
580                 printf("Targeted peripheral clock should be"
581                         "within [%d - %d]\n",
582                         PLL_FREQ_MIN(ref) / SZ_DEC_1M,
583                         PLL_FREQ_MAX(ref) / SZ_DEC_1M);
584                 return -EINVAL;
585         }
586
587         for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) {
588                 if (fixed_mfd[i].ref_clk_hz == ref) {
589                         mfd = fixed_mfd[i].mfd;
590                         break;
591                 }
592         }
593
594         if (i == ARRAY_SIZE(fixed_mfd))
595                 return -EINVAL;
596
597         /* Use n_target and n_ref to avoid overflow */
598         for (pd = 1; pd <= PLL_PD_MAX; pd++) {
599                 t1 = n_target * pd;
600                 do_div(t1, (4 * n_ref));
601                 mfi = t1;
602                 if (mfi > PLL_MFI_MAX)
603                         return -EINVAL;
604                 else if (mfi < 5)
605                         continue;
606                 break;
607         }
608         /*
609          * Now got pd and mfi already
610          *
611          * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref;
612          */
613         t1 = n_target * pd;
614         do_div(t1, 4);
615         t1 -= n_ref * mfi;
616         t1 *= mfd;
617         do_div(t1, n_ref);
618         mfn = t1;
619         debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n",
620                 ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd);
621         i = 1;
622         if (mfn != 0)
623                 i = gcd(mfd, mfn);
624         pll->pd = (u32)pd;
625         pll->mfi = (u32)mfi;
626         do_div(mfn, i);
627         pll->mfn = (u32)mfn;
628         do_div(mfd, i);
629         pll->mfd = (u32)mfd;
630
631         return 0;
632 }
633
634 #define calc_div(tgt_clk, src_clk, limit) ({            \
635                 u32 v = 0;                              \
636                 if (((src_clk) % (tgt_clk)) <= 100)     \
637                         v = (src_clk) / (tgt_clk);      \
638                 else                                    \
639                         v = ((src_clk) / (tgt_clk)) + 1;\
640                 if (v > limit)                          \
641                         v = limit;                      \
642                 (v - 1);                                \
643         })
644
645 #define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \
646         {       \
647                 writel(0x1232, &pll->ctrl);             \
648                 writel(0x2, &pll->config);              \
649                 writel((((pd) - 1) << 0) | ((fi) << 4), \
650                         &pll->op);                      \
651                 writel(fn, &(pll->mfn));                \
652                 writel((fd) - 1, &pll->mfd);            \
653                 writel((((pd) - 1) << 0) | ((fi) << 4), \
654                         &pll->hfs_op);                  \
655                 writel(fn, &pll->hfs_mfn);              \
656                 writel((fd) - 1, &pll->hfs_mfd);        \
657                 writel(0x1232, &pll->ctrl);             \
658                 while (!readl(&pll->ctrl) & 0x1)        \
659                         ;\
660         }
661
662 static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param)
663 {
664         u32 ccsr = readl(&mxc_ccm->ccsr);
665         struct mxc_pll_reg *pll = mxc_plls[index];
666
667         switch (index) {
668         case PLL1_CLOCK:
669                 /* Switch ARM to PLL2 clock */
670                 writel(ccsr | MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
671                                 &mxc_ccm->ccsr);
672                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
673                                         pll_param->mfi, pll_param->mfn,
674                                         pll_param->mfd);
675                 /* Switch back */
676                 writel(ccsr & ~MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
677                                 &mxc_ccm->ccsr);
678                 break;
679         case PLL2_CLOCK:
680                 /* Switch to pll2 bypass clock */
681                 writel(ccsr | MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
682                                 &mxc_ccm->ccsr);
683                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
684                                         pll_param->mfi, pll_param->mfn,
685                                         pll_param->mfd);
686                 /* Switch back */
687                 writel(ccsr & ~MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
688                                 &mxc_ccm->ccsr);
689                 break;
690         case PLL3_CLOCK:
691                 /* Switch to pll3 bypass clock */
692                 writel(ccsr | MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
693                                 &mxc_ccm->ccsr);
694                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
695                                         pll_param->mfi, pll_param->mfn,
696                                         pll_param->mfd);
697                 /* Switch back */
698                 writel(ccsr & ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
699                                 &mxc_ccm->ccsr);
700                 break;
701 #ifdef CONFIG_MX53
702         case PLL4_CLOCK:
703                 /* Switch to pll4 bypass clock */
704                 writel(ccsr | MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
705                                 &mxc_ccm->ccsr);
706                 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
707                                         pll_param->mfi, pll_param->mfn,
708                                         pll_param->mfd);
709                 /* Switch back */
710                 writel(ccsr & ~MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
711                                 &mxc_ccm->ccsr);
712                 break;
713 #endif
714         default:
715                 return -EINVAL;
716         }
717
718         return 0;
719 }
720
721 /* Config CPU clock */
722 static int config_core_clk(u32 ref, u32 freq)
723 {
724         int ret = 0;
725         struct pll_param pll_param;
726
727         memset(&pll_param, 0, sizeof(struct pll_param));
728
729         /* The case that periph uses PLL1 is not considered here */
730         ret = calc_pll_params(ref, freq, &pll_param);
731         if (ret != 0) {
732                 printf("Error:Can't find pll parameters: %d\n", ret);
733                 return ret;
734         }
735
736         return config_pll_clk(PLL1_CLOCK, &pll_param);
737 }
738
739 static int config_nfc_clk(u32 nfc_clk)
740 {
741         u32 parent_rate = get_emi_slow_clk();
742         u32 div;
743
744         if (nfc_clk == 0)
745                 return -EINVAL;
746         div = parent_rate / nfc_clk;
747         if (div == 0)
748                 div++;
749         if (parent_rate / div > NFC_CLK_MAX)
750                 div++;
751         clrsetbits_le32(&mxc_ccm->cbcdr,
752                         MXC_CCM_CBCDR_NFC_PODF_MASK,
753                         MXC_CCM_CBCDR_NFC_PODF(div - 1));
754         while (readl(&mxc_ccm->cdhipr) != 0)
755                 ;
756         return 0;
757 }
758
759 void enable_nfc_clk(unsigned char enable)
760 {
761         unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
762
763         clrsetbits_le32(&mxc_ccm->CCGR5,
764                 MXC_CCM_CCGR5_EMI_ENFC(MXC_CCM_CCGR_CG_MASK),
765                 MXC_CCM_CCGR5_EMI_ENFC(cg));
766 }
767
768 /* Config main_bus_clock for periphs */
769 static int config_periph_clk(u32 ref, u32 freq)
770 {
771         int ret = 0;
772         struct pll_param pll_param;
773
774         memset(&pll_param, 0, sizeof(struct pll_param));
775
776         if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
777                 ret = calc_pll_params(ref, freq, &pll_param);
778                 if (ret != 0) {
779                         printf("Error:Can't find pll parameters: %d\n",
780                                 ret);
781                         return ret;
782                 }
783                 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(
784                                 readl(&mxc_ccm->cbcmr))) {
785                 case 0:
786                         return config_pll_clk(PLL1_CLOCK, &pll_param);
787                         break;
788                 case 1:
789                         return config_pll_clk(PLL3_CLOCK, &pll_param);
790                         break;
791                 default:
792                         return -EINVAL;
793                 }
794         }
795
796         return 0;
797 }
798
799 static int config_ddr_clk(u32 emi_clk)
800 {
801         u32 clk_src;
802         s32 shift = 0, clk_sel, div = 1;
803         u32 cbcmr = readl(&mxc_ccm->cbcmr);
804
805         if (emi_clk > MAX_DDR_CLK) {
806                 printf("Warning:DDR clock should not exceed %d MHz\n",
807                         MAX_DDR_CLK / SZ_DEC_1M);
808                 emi_clk = MAX_DDR_CLK;
809         }
810
811         clk_src = get_periph_clk();
812         /* Find DDR clock input */
813         clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
814         switch (clk_sel) {
815         case 0:
816                 shift = 16;
817                 break;
818         case 1:
819                 shift = 19;
820                 break;
821         case 2:
822                 shift = 22;
823                 break;
824         case 3:
825                 shift = 10;
826                 break;
827         default:
828                 return -EINVAL;
829         }
830
831         if ((clk_src % emi_clk) < 10000000)
832                 div = clk_src / emi_clk;
833         else
834                 div = (clk_src / emi_clk) + 1;
835         if (div > 8)
836                 div = 8;
837
838         clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift);
839         while (readl(&mxc_ccm->cdhipr) != 0)
840                 ;
841         writel(0x0, &mxc_ccm->ccdr);
842
843         return 0;
844 }
845
846 /*
847  * This function assumes the expected core clock has to be changed by
848  * modifying the PLL. This is NOT true always but for most of the times,
849  * it is. So it assumes the PLL output freq is the same as the expected
850  * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN.
851  * In the latter case, it will try to increase the presc value until
852  * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to
853  * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based
854  * on the targeted PLL and reference input clock to the PLL. Lastly,
855  * it sets the register based on these values along with the dividers.
856  * Note 1) There is no value checking for the passed-in divider values
857  *         so the caller has to make sure those values are sensible.
858  *      2) Also adjust the NFC divider such that the NFC clock doesn't
859  *         exceed NFC_CLK_MAX.
860  *      3) IPU HSP clock is independent of AHB clock. Even it can go up to
861  *         177MHz for higher voltage, this function fixes the max to 133MHz.
862  *      4) This function should not have allowed diag_printf() calls since
863  *         the serial driver has been stoped. But leave then here to allow
864  *         easy debugging by NOT calling the cyg_hal_plf_serial_stop().
865  */
866 int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk)
867 {
868         freq *= SZ_DEC_1M;
869
870         switch (clk) {
871         case MXC_ARM_CLK:
872                 if (config_core_clk(ref, freq))
873                         return -EINVAL;
874                 break;
875         case MXC_PERIPH_CLK:
876                 if (config_periph_clk(ref, freq))
877                         return -EINVAL;
878                 break;
879         case MXC_DDR_CLK:
880                 if (config_ddr_clk(freq))
881                         return -EINVAL;
882                 break;
883         case MXC_NFC_CLK:
884                 if (config_nfc_clk(freq))
885                         return -EINVAL;
886                 break;
887         default:
888                 printf("Warning:Unsupported or invalid clock type\n");
889         }
890
891         return 0;
892 }
893
894 #ifdef CONFIG_MX53
895 /*
896  * The clock for the external interface can be set to use internal clock
897  * if fuse bank 4, row 3, bit 2 is set.
898  * This is an undocumented feature and it was confirmed by Freescale's support:
899  * Fuses (but not pins) may be used to configure SATA clocks.
900  * Particularly the i.MX53 Fuse_Map contains the next information
901  * about configuring SATA clocks :  SATA_ALT_REF_CLK[1:0] (offset 0x180C)
902  * '00' - 100MHz (External)
903  * '01' - 50MHz (External)
904  * '10' - 120MHz, internal (USB PHY)
905  * '11' - Reserved
906 */
907 void mxc_set_sata_internal_clock(void)
908 {
909         u32 *tmp_base =
910                 (u32 *)(IIM_BASE_ADDR + 0x180c);
911
912         set_usb_phy_clk();
913
914         clrsetbits_le32(tmp_base, 0x6, 0x4);
915 }
916 #endif
917
918 /*
919  * Dump some core clockes.
920  */
921 int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
922 {
923         u32 freq;
924
925         freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
926         printf("PLL1       %8d MHz\n", freq / 1000000);
927         freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
928         printf("PLL2       %8d MHz\n", freq / 1000000);
929         freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
930         printf("PLL3       %8d MHz\n", freq / 1000000);
931 #ifdef  CONFIG_MX53
932         freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
933         printf("PLL4       %8d MHz\n", freq / 1000000);
934 #endif
935
936         printf("\n");
937         printf("AHB        %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
938         printf("IPG        %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
939         printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
940         printf("DDR        %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
941 #ifdef CONFIG_MXC_SPI
942         printf("CSPI       %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
943 #endif
944         return 0;
945 }
946
947 /***************************************************/
948
949 U_BOOT_CMD(
950         clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks,
951         "display clocks",
952         ""
953 );