blob: 8b0af3b1855a257bac5c9031641af08776279824 [file] [log] [blame]
Stefano Babic64fdf452010-01-20 18:19:32 +01001/*
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>
Stefano Babice4d34492010-03-05 17:54:37 +010031#include <asm/arch/clock.h>
Marek Vasutbf2eaf52011-09-23 11:43:47 +020032#include <div64.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000033#include <asm/arch/sys_proto.h>
Stefano Babic64fdf452010-01-20 18:19:32 +010034
35enum pll_clocks {
36 PLL1_CLOCK = 0,
37 PLL2_CLOCK,
38 PLL3_CLOCK,
Marek Vasutbf2eaf52011-09-23 11:43:47 +020039 PLL4_CLOCK,
Stefano Babic64fdf452010-01-20 18:19:32 +010040 PLL_CLOCKS,
41};
42
43struct 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,
Marek Vasutbf2eaf52011-09-23 11:43:47 +020047#ifdef CONFIG_MX53
48 [PLL4_CLOCK] = (struct mxc_pll_reg *)PLL4_BASE_ADDR,
49#endif
Stefano Babic64fdf452010-01-20 18:19:32 +010050};
51
Fabio Estevam70cc86a2012-04-30 08:12:02 +000052#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
66struct fixed_pll_mfd {
67 u32 ref_clk_hz;
68 u32 mfd;
69};
70
71const struct fixed_pll_mfd fixed_mfd[] = {
Benoît Thébaudeau833b6432012-09-27 10:19:58 +000072 {MXC_HCLK, 24 * 16},
Fabio Estevam70cc86a2012-04-30 08:12:02 +000073};
74
75struct 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
Stefano Babice4d34492010-03-05 17:54:37 +010088struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE;
Stefano Babic64fdf452010-01-20 18:19:32 +010089
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +010090void set_usboh3_clk(void)
91{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +000092 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));
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100100}
101
102void enable_usboh3_clk(unsigned char enable)
103{
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100104 if (enable)
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000105 setbits_le32(&mxc_ccm->CCGR2, 1 << MXC_CCM_CCGR2_CG14_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100106 else
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000107 clrbits_le32(&mxc_ccm->CCGR2, 1 << MXC_CCM_CCGR2_CG14_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100108}
109
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000110#ifdef CONFIG_I2C_MXC
111/* i2c_num can be from 0 - 2 */
112int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
113{
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000114 u32 mask;
115
116 if (i2c_num > 2)
117 return -EINVAL;
118 mask = MXC_CCM_CCGR_CG_MASK << ((i2c_num + 9) << 1);
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000119 if (enable)
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000120 setbits_le32(&mxc_ccm->CCGR1, mask);
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000121 else
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000122 clrbits_le32(&mxc_ccm->CCGR1, mask);
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000123 return 0;
124}
125#endif
126
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100127void set_usb_phy1_clk(void)
128{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000129 clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100130}
131
132void enable_usb_phy1_clk(unsigned char enable)
133{
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100134 if (enable)
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000135 setbits_le32(&mxc_ccm->CCGR4, 1 << MXC_CCM_CCGR4_CG5_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100136 else
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000137 clrbits_le32(&mxc_ccm->CCGR4, 1 << MXC_CCM_CCGR4_CG5_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100138}
139
140void set_usb_phy2_clk(void)
141{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000142 clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100143}
144
145void enable_usb_phy2_clk(unsigned char enable)
146{
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100147 if (enable)
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000148 setbits_le32(&mxc_ccm->CCGR4, 1 << MXC_CCM_CCGR4_CG6_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100149 else
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000150 clrbits_le32(&mxc_ccm->CCGR4, 1 << MXC_CCM_CCGR4_CG6_OFFSET);
Wolfgang Grandegger5d2947a2011-11-11 14:03:34 +0100151}
152
Stefano Babic64fdf452010-01-20 18:19:32 +0100153/*
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200154 * Calculate the frequency of PLLn.
Stefano Babic64fdf452010-01-20 18:19:32 +0100155 */
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200156static uint32_t decode_pll(struct mxc_pll_reg *pll, uint32_t infreq)
Stefano Babic64fdf452010-01-20 18:19:32 +0100157{
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200158 uint32_t ctrl, op, mfd, mfn, mfi, pdf, ret;
159 uint64_t refclk, temp;
160 int32_t mfn_abs;
Stefano Babic64fdf452010-01-20 18:19:32 +0100161
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200162 ctrl = readl(&pll->ctrl);
Stefano Babic64fdf452010-01-20 18:19:32 +0100163
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200164 if (ctrl & MXC_DPLLC_CTL_HFSM) {
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000165 mfn = readl(&pll->hfs_mfn);
166 mfd = readl(&pll->hfs_mfd);
167 op = readl(&pll->hfs_op);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200168 } else {
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000169 mfn = readl(&pll->mfn);
170 mfd = readl(&pll->mfd);
171 op = readl(&pll->op);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200172 }
173
174 mfd &= MXC_DPLLC_MFD_MFD_MASK;
175 mfn &= MXC_DPLLC_MFN_MFN_MASK;
176 pdf = op & MXC_DPLLC_OP_PDF_MASK;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000177 mfi = MXC_DPLLC_OP_MFI_RD(op);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200178
179 /* 21.2.3 */
180 if (mfi < 5)
181 mfi = 5;
182
183 /* Sign extend */
184 if (mfn >= 0x04000000) {
185 mfn |= 0xfc000000;
186 mfn_abs = -mfn;
187 } else
188 mfn_abs = mfn;
189
190 refclk = infreq * 2;
191 if (ctrl & MXC_DPLLC_CTL_DPDCK0_2_EN)
192 refclk *= 2;
193
Simon Glass5acc9072011-11-05 04:25:22 +0000194 do_div(refclk, pdf + 1);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200195 temp = refclk * mfn_abs;
196 do_div(temp, mfd + 1);
197 ret = refclk * mfi;
198
199 if ((int)mfn < 0)
200 ret -= temp;
201 else
202 ret += temp;
203
204 return ret;
Stefano Babic64fdf452010-01-20 18:19:32 +0100205}
206
207/*
208 * Get mcu main rate
209 */
210u32 get_mcu_main_clk(void)
211{
212 u32 reg, freq;
213
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000214 reg = MXC_CCM_CACRR_ARM_PODF_RD(readl(&mxc_ccm->cacrr));
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000215 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100216 return freq / (reg + 1);
217}
218
219/*
220 * Get the rate of peripheral's root clock.
221 */
Fabio Estevam6a376042012-04-29 08:11:13 +0000222u32 get_periph_clk(void)
Stefano Babic64fdf452010-01-20 18:19:32 +0100223{
224 u32 reg;
225
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000226 reg = readl(&mxc_ccm->cbcdr);
Stefano Babic64fdf452010-01-20 18:19:32 +0100227 if (!(reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL))
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000228 return decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000229 reg = readl(&mxc_ccm->cbcmr);
230 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(reg)) {
Stefano Babic64fdf452010-01-20 18:19:32 +0100231 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000232 return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100233 case 1:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000234 return decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100235 default:
236 return 0;
237 }
238 /* NOTREACHED */
239}
240
241/*
242 * Get the rate of ipg clock.
243 */
244static u32 get_ipg_clk(void)
245{
Marek Vasut95c0eb12011-09-22 09:20:37 +0000246 uint32_t freq, reg, div;
Stefano Babic64fdf452010-01-20 18:19:32 +0100247
Marek Vasut95c0eb12011-09-22 09:20:37 +0000248 freq = get_ahb_clk();
249
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000250 reg = readl(&mxc_ccm->cbcdr);
251 div = MXC_CCM_CBCDR_IPG_PODF_RD(reg) + 1;
Marek Vasut95c0eb12011-09-22 09:20:37 +0000252
253 return freq / div;
Stefano Babic64fdf452010-01-20 18:19:32 +0100254}
255
256/*
257 * Get the rate of ipg_per clock.
258 */
259static u32 get_ipg_per_clk(void)
260{
261 u32 pred1, pred2, podf;
262
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000263 if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_IPG_CLK_SEL)
Stefano Babic64fdf452010-01-20 18:19:32 +0100264 return get_ipg_clk();
265 /* Fixme: not handle what about lpm*/
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000266 podf = readl(&mxc_ccm->cbcdr);
267 pred1 = MXC_CCM_CBCDR_PERCLK_PRED1_RD(podf);
268 pred2 = MXC_CCM_CBCDR_PERCLK_PRED2_RD(podf);
269 podf = MXC_CCM_CBCDR_PERCLK_PODF_RD(podf);
Stefano Babic64fdf452010-01-20 18:19:32 +0100270 return get_periph_clk() / ((pred1 + 1) * (pred2 + 1) * (podf + 1));
271}
272
273/*
274 * Get the rate of uart clk.
275 */
276static u32 get_uart_clk(void)
277{
278 unsigned int freq, reg, pred, podf;
279
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000280 reg = readl(&mxc_ccm->cscmr1);
281 switch (MXC_CCM_CSCMR1_UART_CLK_SEL_RD(reg)) {
Stefano Babic64fdf452010-01-20 18:19:32 +0100282 case 0x0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000283 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100284 break;
285 case 0x1:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000286 freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100287 break;
288 case 0x2:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000289 freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Stefano Babic64fdf452010-01-20 18:19:32 +0100290 break;
291 default:
292 return 66500000;
293 }
294
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000295 reg = readl(&mxc_ccm->cscdr1);
296 pred = MXC_CCM_CSCDR1_UART_CLK_PRED_RD(reg);
297 podf = MXC_CCM_CSCDR1_UART_CLK_PODF_RD(reg);
Stefano Babic64fdf452010-01-20 18:19:32 +0100298 freq /= (pred + 1) * (podf + 1);
299
300 return freq;
301}
302
303/*
304 * This function returns the low power audio clock.
305 */
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000306static u32 get_lp_apm(void)
Stefano Babic64fdf452010-01-20 18:19:32 +0100307{
308 u32 ret_val = 0;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000309 u32 ccsr = readl(&mxc_ccm->ccsr);
Stefano Babic64fdf452010-01-20 18:19:32 +0100310
311 if (((ccsr >> 9) & 1) == 0)
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000312 ret_val = MXC_HCLK;
Stefano Babic64fdf452010-01-20 18:19:32 +0100313 else
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000314 ret_val = MXC_CLK32 * 1024;
Stefano Babic64fdf452010-01-20 18:19:32 +0100315
316 return ret_val;
317}
318
319/*
320 * get cspi clock rate.
321 */
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000322static u32 imx_get_cspiclk(void)
Stefano Babic64fdf452010-01-20 18:19:32 +0100323{
324 u32 ret_val = 0, pdf, pre_pdf, clk_sel;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000325 u32 cscmr1 = readl(&mxc_ccm->cscmr1);
326 u32 cscdr2 = readl(&mxc_ccm->cscdr2);
Stefano Babic64fdf452010-01-20 18:19:32 +0100327
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000328 pre_pdf = MXC_CCM_CSCDR2_CSPI_CLK_PRED_RD(cscdr2);
329 pdf = MXC_CCM_CSCDR2_CSPI_CLK_PODF_RD(cscdr2);
330 clk_sel = MXC_CCM_CSCMR1_CSPI_CLK_SEL_RD(cscmr1);
Stefano Babic64fdf452010-01-20 18:19:32 +0100331
332 switch (clk_sel) {
333 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000334 ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK) /
Stefano Babic64fdf452010-01-20 18:19:32 +0100335 ((pre_pdf + 1) * (pdf + 1));
336 break;
337 case 1:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000338 ret_val = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK) /
Stefano Babic64fdf452010-01-20 18:19:32 +0100339 ((pre_pdf + 1) * (pdf + 1));
340 break;
341 case 2:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000342 ret_val = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK) /
Stefano Babic64fdf452010-01-20 18:19:32 +0100343 ((pre_pdf + 1) * (pdf + 1));
344 break;
345 default:
346 ret_val = get_lp_apm() / ((pre_pdf + 1) * (pdf + 1));
347 break;
348 }
349
350 return ret_val;
351}
352
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000353static u32 get_axi_a_clk(void)
354{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000355 u32 cbcdr = readl(&mxc_ccm->cbcdr);
356 u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000357
358 return get_periph_clk() / (pdf + 1);
359}
360
361static u32 get_axi_b_clk(void)
362{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000363 u32 cbcdr = readl(&mxc_ccm->cbcdr);
364 u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000365
366 return get_periph_clk() / (pdf + 1);
367}
368
369static u32 get_emi_slow_clk(void)
370{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000371 u32 cbcdr = readl(&mxc_ccm->cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000372 u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000373 u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000374
375 if (emi_clk_sel)
376 return get_ahb_clk() / (pdf + 1);
377
378 return get_periph_clk() / (pdf + 1);
379}
380
381static u32 get_ddr_clk(void)
382{
383 u32 ret_val = 0;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000384 u32 cbcmr = readl(&mxc_ccm->cbcmr);
385 u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000386#ifdef CONFIG_MX51
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000387 u32 cbcdr = readl(&mxc_ccm->cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000388 if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) {
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000389 u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000390
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000391 ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000392 ret_val /= ddr_clk_podf + 1;
393
394 return ret_val;
395 }
396#endif
397 switch (ddr_clk_sel) {
398 case 0:
399 ret_val = get_axi_a_clk();
400 break;
401 case 1:
402 ret_val = get_axi_b_clk();
403 break;
404 case 2:
405 ret_val = get_emi_slow_clk();
406 break;
407 case 3:
408 ret_val = get_ahb_clk();
409 break;
410 default:
411 break;
412 }
413
414 return ret_val;
415}
416
Stefano Babic64fdf452010-01-20 18:19:32 +0100417/*
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000418 * The API of get mxc clocks.
Stefano Babic64fdf452010-01-20 18:19:32 +0100419 */
420unsigned int mxc_get_clock(enum mxc_clock clk)
421{
422 switch (clk) {
423 case MXC_ARM_CLK:
424 return get_mcu_main_clk();
425 case MXC_AHB_CLK:
Marek Vasut95c0eb12011-09-22 09:20:37 +0000426 return get_ahb_clk();
Stefano Babic64fdf452010-01-20 18:19:32 +0100427 case MXC_IPG_CLK:
428 return get_ipg_clk();
429 case MXC_IPG_PERCLK:
Matthias Weissere7bed5c2012-09-24 02:46:53 +0000430 case MXC_I2C_CLK:
Stefano Babic64fdf452010-01-20 18:19:32 +0100431 return get_ipg_per_clk();
432 case MXC_UART_CLK:
433 return get_uart_clk();
434 case MXC_CSPI_CLK:
435 return imx_get_cspiclk();
436 case MXC_FEC_CLK:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000437 return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babicd87c85c2012-02-22 00:24:36 +0000438 case MXC_SATA_CLK:
439 return get_ahb_clk();
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000440 case MXC_DDR_CLK:
441 return get_ddr_clk();
Stefano Babic64fdf452010-01-20 18:19:32 +0100442 default:
443 break;
444 }
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000445 return -EINVAL;
Stefano Babic64fdf452010-01-20 18:19:32 +0100446}
447
448u32 imx_get_uartclk(void)
449{
450 return get_uart_clk();
451}
452
453
454u32 imx_get_fecclk(void)
455{
456 return mxc_get_clock(MXC_IPG_CLK);
457}
458
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000459static int gcd(int m, int n)
460{
461 int t;
462 while (m > 0) {
463 if (n > m) {
464 t = m;
465 m = n;
466 n = t;
467 } /* swap */
468 m -= n;
469 }
470 return n;
471}
472
473/*
474 * This is to calculate various parameters based on reference clock and
475 * targeted clock based on the equation:
476 * t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1)
477 * This calculation is based on a fixed MFD value for simplicity.
478 */
479static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll)
480{
481 u64 pd, mfi = 1, mfn, mfd, t1;
482 u32 n_target = target;
483 u32 n_ref = ref, i;
484
485 /*
486 * Make sure targeted freq is in the valid range.
487 * Otherwise the following calculation might be wrong!!!
488 */
489 if (n_target < PLL_FREQ_MIN(ref) ||
490 n_target > PLL_FREQ_MAX(ref)) {
491 printf("Targeted peripheral clock should be"
492 "within [%d - %d]\n",
493 PLL_FREQ_MIN(ref) / SZ_DEC_1M,
494 PLL_FREQ_MAX(ref) / SZ_DEC_1M);
495 return -EINVAL;
496 }
497
498 for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) {
499 if (fixed_mfd[i].ref_clk_hz == ref) {
500 mfd = fixed_mfd[i].mfd;
501 break;
502 }
503 }
504
505 if (i == ARRAY_SIZE(fixed_mfd))
506 return -EINVAL;
507
508 /* Use n_target and n_ref to avoid overflow */
509 for (pd = 1; pd <= PLL_PD_MAX; pd++) {
510 t1 = n_target * pd;
511 do_div(t1, (4 * n_ref));
512 mfi = t1;
513 if (mfi > PLL_MFI_MAX)
514 return -EINVAL;
515 else if (mfi < 5)
516 continue;
517 break;
518 }
519 /*
520 * Now got pd and mfi already
521 *
522 * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref;
523 */
524 t1 = n_target * pd;
525 do_div(t1, 4);
526 t1 -= n_ref * mfi;
527 t1 *= mfd;
528 do_div(t1, n_ref);
529 mfn = t1;
530 debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n",
531 ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd);
532 i = 1;
533 if (mfn != 0)
534 i = gcd(mfd, mfn);
535 pll->pd = (u32)pd;
536 pll->mfi = (u32)mfi;
537 do_div(mfn, i);
538 pll->mfn = (u32)mfn;
539 do_div(mfd, i);
540 pll->mfd = (u32)mfd;
541
542 return 0;
543}
544
545#define calc_div(tgt_clk, src_clk, limit) ({ \
546 u32 v = 0; \
547 if (((src_clk) % (tgt_clk)) <= 100) \
548 v = (src_clk) / (tgt_clk); \
549 else \
550 v = ((src_clk) / (tgt_clk)) + 1;\
551 if (v > limit) \
552 v = limit; \
553 (v - 1); \
554 })
555
556#define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \
557 { \
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000558 writel(0x1232, &pll->ctrl); \
559 writel(0x2, &pll->config); \
560 writel((((pd) - 1) << 0) | ((fi) << 4), \
561 &pll->op); \
562 writel(fn, &(pll->mfn)); \
563 writel((fd) - 1, &pll->mfd); \
564 writel((((pd) - 1) << 0) | ((fi) << 4), \
565 &pll->hfs_op); \
566 writel(fn, &pll->hfs_mfn); \
567 writel((fd) - 1, &pll->hfs_mfd); \
568 writel(0x1232, &pll->ctrl); \
569 while (!readl(&pll->ctrl) & 0x1) \
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000570 ;\
571 }
572
573static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param)
574{
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000575 u32 ccsr = readl(&mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000576 struct mxc_pll_reg *pll = mxc_plls[index];
577
578 switch (index) {
579 case PLL1_CLOCK:
580 /* Switch ARM to PLL2 clock */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000581 writel(ccsr | 0x4, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000582 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
583 pll_param->mfi, pll_param->mfn,
584 pll_param->mfd);
585 /* Switch back */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000586 writel(ccsr & ~0x4, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000587 break;
588 case PLL2_CLOCK:
589 /* Switch to pll2 bypass clock */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000590 writel(ccsr | 0x2, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000591 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
592 pll_param->mfi, pll_param->mfn,
593 pll_param->mfd);
594 /* Switch back */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000595 writel(ccsr & ~0x2, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000596 break;
597 case PLL3_CLOCK:
598 /* Switch to pll3 bypass clock */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000599 writel(ccsr | 0x1, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000600 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
601 pll_param->mfi, pll_param->mfn,
602 pll_param->mfd);
603 /* Switch back */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000604 writel(ccsr & ~0x1, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000605 break;
606 case PLL4_CLOCK:
607 /* Switch to pll4 bypass clock */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000608 writel(ccsr | 0x20, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000609 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
610 pll_param->mfi, pll_param->mfn,
611 pll_param->mfd);
612 /* Switch back */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000613 writel(ccsr & ~0x20, &mxc_ccm->ccsr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000614 break;
615 default:
616 return -EINVAL;
617 }
618
619 return 0;
620}
621
622/* Config CPU clock */
623static int config_core_clk(u32 ref, u32 freq)
624{
625 int ret = 0;
626 struct pll_param pll_param;
627
628 memset(&pll_param, 0, sizeof(struct pll_param));
629
630 /* The case that periph uses PLL1 is not considered here */
631 ret = calc_pll_params(ref, freq, &pll_param);
632 if (ret != 0) {
633 printf("Error:Can't find pll parameters: %d\n", ret);
634 return ret;
635 }
636
637 return config_pll_clk(PLL1_CLOCK, &pll_param);
638}
639
640static int config_nfc_clk(u32 nfc_clk)
641{
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000642 u32 parent_rate = get_emi_slow_clk();
643 u32 div = parent_rate / nfc_clk;
644
645 if (nfc_clk <= 0)
646 return -EINVAL;
647 if (div == 0)
648 div++;
649 if (parent_rate / div > NFC_CLK_MAX)
650 div++;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000651 clrsetbits_le32(&mxc_ccm->cbcdr,
652 MXC_CCM_CBCDR_NFC_PODF_MASK,
653 MXC_CCM_CBCDR_NFC_PODF(div - 1));
654 while (readl(&mxc_ccm->cdhipr) != 0)
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000655 ;
656 return 0;
657}
658
659/* Config main_bus_clock for periphs */
660static int config_periph_clk(u32 ref, u32 freq)
661{
662 int ret = 0;
663 struct pll_param pll_param;
664
665 memset(&pll_param, 0, sizeof(struct pll_param));
666
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000667 if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000668 ret = calc_pll_params(ref, freq, &pll_param);
669 if (ret != 0) {
670 printf("Error:Can't find pll parameters: %d\n",
671 ret);
672 return ret;
673 }
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000674 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(
675 readl(&mxc_ccm->cbcmr))) {
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000676 case 0:
677 return config_pll_clk(PLL1_CLOCK, &pll_param);
678 break;
679 case 1:
680 return config_pll_clk(PLL3_CLOCK, &pll_param);
681 break;
682 default:
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
690static int config_ddr_clk(u32 emi_clk)
691{
692 u32 clk_src;
693 s32 shift = 0, clk_sel, div = 1;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000694 u32 cbcmr = readl(&mxc_ccm->cbcmr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000695
696 if (emi_clk > MAX_DDR_CLK) {
697 printf("Warning:DDR clock should not exceed %d MHz\n",
698 MAX_DDR_CLK / SZ_DEC_1M);
699 emi_clk = MAX_DDR_CLK;
700 }
701
702 clk_src = get_periph_clk();
703 /* Find DDR clock input */
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000704 clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000705 switch (clk_sel) {
706 case 0:
707 shift = 16;
708 break;
709 case 1:
710 shift = 19;
711 break;
712 case 2:
713 shift = 22;
714 break;
715 case 3:
716 shift = 10;
717 break;
718 default:
719 return -EINVAL;
720 }
721
722 if ((clk_src % emi_clk) < 10000000)
723 div = clk_src / emi_clk;
724 else
725 div = (clk_src / emi_clk) + 1;
726 if (div > 8)
727 div = 8;
728
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000729 clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift);
730 while (readl(&mxc_ccm->cdhipr) != 0)
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000731 ;
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000732 writel(0x0, &mxc_ccm->ccdr);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000733
734 return 0;
735}
736
737/*
738 * This function assumes the expected core clock has to be changed by
739 * modifying the PLL. This is NOT true always but for most of the times,
740 * it is. So it assumes the PLL output freq is the same as the expected
741 * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN.
742 * In the latter case, it will try to increase the presc value until
743 * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to
744 * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based
745 * on the targeted PLL and reference input clock to the PLL. Lastly,
746 * it sets the register based on these values along with the dividers.
747 * Note 1) There is no value checking for the passed-in divider values
748 * so the caller has to make sure those values are sensible.
749 * 2) Also adjust the NFC divider such that the NFC clock doesn't
750 * exceed NFC_CLK_MAX.
751 * 3) IPU HSP clock is independent of AHB clock. Even it can go up to
752 * 177MHz for higher voltage, this function fixes the max to 133MHz.
753 * 4) This function should not have allowed diag_printf() calls since
754 * the serial driver has been stoped. But leave then here to allow
755 * easy debugging by NOT calling the cyg_hal_plf_serial_stop().
756 */
757int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk)
758{
759 freq *= SZ_DEC_1M;
760
761 switch (clk) {
762 case MXC_ARM_CLK:
763 if (config_core_clk(ref, freq))
764 return -EINVAL;
765 break;
766 case MXC_PERIPH_CLK:
767 if (config_periph_clk(ref, freq))
768 return -EINVAL;
769 break;
770 case MXC_DDR_CLK:
771 if (config_ddr_clk(freq))
772 return -EINVAL;
773 break;
774 case MXC_NFC_CLK:
775 if (config_nfc_clk(freq))
776 return -EINVAL;
777 break;
778 default:
779 printf("Warning:Unsupported or invalid clock type\n");
780 }
781
782 return 0;
783}
784
Stefano Babic8c38b5d2012-02-22 00:24:38 +0000785#ifdef CONFIG_MX53
786/*
787 * The clock for the external interface can be set to use internal clock
788 * if fuse bank 4, row 3, bit 2 is set.
789 * This is an undocumented feature and it was confirmed by Freescale's support:
790 * Fuses (but not pins) may be used to configure SATA clocks.
791 * Particularly the i.MX53 Fuse_Map contains the next information
792 * about configuring SATA clocks : SATA_ALT_REF_CLK[1:0] (offset 0x180C)
793 * '00' - 100MHz (External)
794 * '01' - 50MHz (External)
795 * '10' - 120MHz, internal (USB PHY)
796 * '11' - Reserved
797*/
798void mxc_set_sata_internal_clock(void)
799{
800 u32 *tmp_base =
801 (u32 *)(IIM_BASE_ADDR + 0x180c);
802
803 set_usb_phy1_clk();
804
Benoît Thébaudeau846b3892012-09-27 10:20:33 +0000805 clrsetbits_le32(tmp_base, 0x6, 0x4);
Stefano Babic8c38b5d2012-02-22 00:24:38 +0000806}
807#endif
808
Stefano Babic64fdf452010-01-20 18:19:32 +0100809/*
810 * Dump some core clockes.
811 */
Stefano Babic9a004412010-10-28 11:08:52 +0200812int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefano Babic64fdf452010-01-20 18:19:32 +0100813{
814 u32 freq;
815
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000816 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Marek Vasut37a6d202011-09-14 14:09:04 +0000817 printf("PLL1 %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000818 freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Marek Vasut37a6d202011-09-14 14:09:04 +0000819 printf("PLL2 %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000820 freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Marek Vasut37a6d202011-09-14 14:09:04 +0000821 printf("PLL3 %8d MHz\n", freq / 1000000);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200822#ifdef CONFIG_MX53
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000823 freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
Marek Vasut37a6d202011-09-14 14:09:04 +0000824 printf("PLL4 %8d MHz\n", freq / 1000000);
Marek Vasutbf2eaf52011-09-23 11:43:47 +0200825#endif
Marek Vasut37a6d202011-09-14 14:09:04 +0000826
827 printf("\n");
828 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
829 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
830 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
Fabio Estevam70cc86a2012-04-30 08:12:02 +0000831 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
Stefano Babic64fdf452010-01-20 18:19:32 +0100832
833 return 0;
834}
835
836/***************************************************/
837
838U_BOOT_CMD(
Stefano Babic7acec2592011-08-17 17:52:40 +0200839 clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks,
840 "display clocks",
Stefano Babic64fdf452010-01-20 18:19:32 +0100841 ""
842);