blob: 2fabdd2eae8ed8c93ed1d60bb4832eeac7cc35a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefano Babica521a772010-01-20 18:19:32 +01002/*
3 * (C) Copyright 2007
4 * Sascha Hauer, Pengutronix
5 *
6 * (C) Copyright 2009 Freescale Semiconductor, Inc.
Stefano Babica521a772010-01-20 18:19:32 +01007 */
8
9#include <common.h>
10#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Stefano Babica521a772010-01-20 18:19:32 +010012#include <asm/arch/imx-regs.h>
13#include <asm/arch/crm_regs.h>
Stefano Babicac41d4d2010-03-05 17:54:37 +010014#include <asm/arch/clock.h>
Marek Vasut3c844f32011-09-23 11:43:47 +020015#include <div64.h>
Fabio Estevam6479f512012-04-29 08:11:13 +000016#include <asm/arch/sys_proto.h>
Stefano Babica521a772010-01-20 18:19:32 +010017
18enum pll_clocks {
19 PLL1_CLOCK = 0,
20 PLL2_CLOCK,
21 PLL3_CLOCK,
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +000022#ifdef CONFIG_MX53
Marek Vasut3c844f32011-09-23 11:43:47 +020023 PLL4_CLOCK,
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +000024#endif
Stefano Babica521a772010-01-20 18:19:32 +010025 PLL_CLOCKS,
26};
27
28struct mxc_pll_reg *mxc_plls[PLL_CLOCKS] = {
29 [PLL1_CLOCK] = (struct mxc_pll_reg *)PLL1_BASE_ADDR,
30 [PLL2_CLOCK] = (struct mxc_pll_reg *)PLL2_BASE_ADDR,
31 [PLL3_CLOCK] = (struct mxc_pll_reg *)PLL3_BASE_ADDR,
Marek Vasut3c844f32011-09-23 11:43:47 +020032#ifdef CONFIG_MX53
33 [PLL4_CLOCK] = (struct mxc_pll_reg *)PLL4_BASE_ADDR,
34#endif
Stefano Babica521a772010-01-20 18:19:32 +010035};
36
Fabio Estevamb4121012012-04-30 08:12:02 +000037#define AHB_CLK_ROOT 133333333
38#define SZ_DEC_1M 1000000
39#define PLL_PD_MAX 16 /* Actual pd+1 */
40#define PLL_MFI_MAX 15
41#define PLL_MFI_MIN 5
42#define ARM_DIV_MAX 8
43#define IPG_DIV_MAX 4
44#define AHB_DIV_MAX 8
45#define EMI_DIV_MAX 8
46#define NFC_DIV_MAX 8
47
48#define MX5_CBCMR 0x00015154
49#define MX5_CBCDR 0x02888945
50
51struct fixed_pll_mfd {
52 u32 ref_clk_hz;
53 u32 mfd;
54};
55
56const struct fixed_pll_mfd fixed_mfd[] = {
Benoît Thébaudeauafac1652012-09-27 10:19:58 +000057 {MXC_HCLK, 24 * 16},
Fabio Estevamb4121012012-04-30 08:12:02 +000058};
59
60struct pll_param {
61 u32 pd;
62 u32 mfi;
63 u32 mfn;
64 u32 mfd;
65};
66
67#define PLL_FREQ_MAX(ref_clk) (4 * (ref_clk) * PLL_MFI_MAX)
68#define PLL_FREQ_MIN(ref_clk) \
69 ((2 * (ref_clk) * (PLL_MFI_MIN - 1)) / PLL_PD_MAX)
70#define MAX_DDR_CLK 420000000
71#define NFC_CLK_MAX 34000000
72
Stefano Babicac41d4d2010-03-05 17:54:37 +010073struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE;
Stefano Babica521a772010-01-20 18:19:32 +010074
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +010075void set_usboh3_clk(void)
76{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +000077 clrsetbits_le32(&mxc_ccm->cscmr1,
78 MXC_CCM_CSCMR1_USBOH3_CLK_SEL_MASK,
79 MXC_CCM_CSCMR1_USBOH3_CLK_SEL(1));
80 clrsetbits_le32(&mxc_ccm->cscdr1,
81 MXC_CCM_CSCDR1_USBOH3_CLK_PODF_MASK |
82 MXC_CCM_CSCDR1_USBOH3_CLK_PRED_MASK,
83 MXC_CCM_CSCDR1_USBOH3_CLK_PRED(4) |
84 MXC_CCM_CSCDR1_USBOH3_CLK_PODF(1));
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +010085}
86
Fabio Estevam800cb812013-07-26 13:54:28 -030087void enable_usboh3_clk(bool enable)
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +010088{
Benoît Thébaudeau6dfc6302012-09-27 10:21:22 +000089 unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
90
91 clrsetbits_le32(&mxc_ccm->CCGR2,
92 MXC_CCM_CCGR2_USBOH3_60M(MXC_CCM_CCGR_CG_MASK),
93 MXC_CCM_CCGR2_USBOH3_60M(cg));
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +010094}
95
trema49f40a2013-09-21 18:13:35 +020096#ifdef CONFIG_SYS_I2C_MXC
Benoît Thébaudeaua23408c2012-09-27 10:24:13 +000097/* i2c_num can be from 0, to 1 for i.MX51 and 2 for i.MX53 */
Troy Kiskyd4fdc992012-07-19 08:18:25 +000098int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
99{
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000100 u32 mask;
101
Benoît Thébaudeaua23408c2012-09-27 10:24:13 +0000102#if defined(CONFIG_MX51)
103 if (i2c_num > 1)
104#elif defined(CONFIG_MX53)
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000105 if (i2c_num > 2)
Benoît Thébaudeaua23408c2012-09-27 10:24:13 +0000106#endif
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000107 return -EINVAL;
Benoît Thébaudeau461a00a2012-09-27 10:21:00 +0000108 mask = MXC_CCM_CCGR_CG_MASK <<
109 (MXC_CCM_CCGR1_I2C1_OFFSET + (i2c_num << 1));
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000110 if (enable)
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000111 setbits_le32(&mxc_ccm->CCGR1, mask);
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000112 else
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000113 clrbits_le32(&mxc_ccm->CCGR1, mask);
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000114 return 0;
115}
116#endif
117
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000118void set_usb_phy_clk(void)
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100119{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000120 clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL);
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100121}
122
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000123#if defined(CONFIG_MX51)
Fabio Estevam800cb812013-07-26 13:54:28 -0300124void enable_usb_phy1_clk(bool enable)
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100125{
Benoît Thébaudeau6dfc6302012-09-27 10:21:22 +0000126 unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
127
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000128 clrsetbits_le32(&mxc_ccm->CCGR2,
129 MXC_CCM_CCGR2_USB_PHY(MXC_CCM_CCGR_CG_MASK),
130 MXC_CCM_CCGR2_USB_PHY(cg));
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100131}
132
Fabio Estevam800cb812013-07-26 13:54:28 -0300133void enable_usb_phy2_clk(bool enable)
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100134{
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000135 /* i.MX51 has a single USB PHY clock, so do nothing here. */
136}
137#elif defined(CONFIG_MX53)
Fabio Estevam800cb812013-07-26 13:54:28 -0300138void enable_usb_phy1_clk(bool enable)
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000139{
140 unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
141
142 clrsetbits_le32(&mxc_ccm->CCGR4,
143 MXC_CCM_CCGR4_USB_PHY1(MXC_CCM_CCGR_CG_MASK),
144 MXC_CCM_CCGR4_USB_PHY1(cg));
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100145}
146
Fabio Estevam800cb812013-07-26 13:54:28 -0300147void enable_usb_phy2_clk(bool enable)
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100148{
Benoît Thébaudeau6dfc6302012-09-27 10:21:22 +0000149 unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
150
151 clrsetbits_le32(&mxc_ccm->CCGR4,
152 MXC_CCM_CCGR4_USB_PHY2(MXC_CCM_CCGR_CG_MASK),
153 MXC_CCM_CCGR4_USB_PHY2(cg));
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100154}
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000155#endif
Wolfgang Grandegger8c1e4d32011-11-11 14:03:34 +0100156
Stefano Babica521a772010-01-20 18:19:32 +0100157/*
Marek Vasut3c844f32011-09-23 11:43:47 +0200158 * Calculate the frequency of PLLn.
Stefano Babica521a772010-01-20 18:19:32 +0100159 */
Marek Vasut3c844f32011-09-23 11:43:47 +0200160static uint32_t decode_pll(struct mxc_pll_reg *pll, uint32_t infreq)
Stefano Babica521a772010-01-20 18:19:32 +0100161{
Marek Vasut3c844f32011-09-23 11:43:47 +0200162 uint32_t ctrl, op, mfd, mfn, mfi, pdf, ret;
163 uint64_t refclk, temp;
164 int32_t mfn_abs;
Stefano Babica521a772010-01-20 18:19:32 +0100165
Marek Vasut3c844f32011-09-23 11:43:47 +0200166 ctrl = readl(&pll->ctrl);
Stefano Babica521a772010-01-20 18:19:32 +0100167
Marek Vasut3c844f32011-09-23 11:43:47 +0200168 if (ctrl & MXC_DPLLC_CTL_HFSM) {
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000169 mfn = readl(&pll->hfs_mfn);
170 mfd = readl(&pll->hfs_mfd);
171 op = readl(&pll->hfs_op);
Marek Vasut3c844f32011-09-23 11:43:47 +0200172 } else {
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000173 mfn = readl(&pll->mfn);
174 mfd = readl(&pll->mfd);
175 op = readl(&pll->op);
Marek Vasut3c844f32011-09-23 11:43:47 +0200176 }
177
178 mfd &= MXC_DPLLC_MFD_MFD_MASK;
179 mfn &= MXC_DPLLC_MFN_MFN_MASK;
180 pdf = op & MXC_DPLLC_OP_PDF_MASK;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000181 mfi = MXC_DPLLC_OP_MFI_RD(op);
Marek Vasut3c844f32011-09-23 11:43:47 +0200182
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
Simon Glass3d557882011-11-05 04:25:22 +0000198 do_div(refclk, pdf + 1);
Marek Vasut3c844f32011-09-23 11:43:47 +0200199 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;
Stefano Babica521a772010-01-20 18:19:32 +0100209}
210
Benoît Thébaudeau9c05cc72012-09-27 10:22:37 +0000211#ifdef CONFIG_MX51
212/*
213 * This function returns the Frequency Pre-Multiplier clock.
214 */
215static u32 get_fpm(void)
216{
217 u32 mult;
218 u32 ccr = readl(&mxc_ccm->ccr);
219
220 if (ccr & MXC_CCM_CCR_FPM_MULT)
221 mult = 1024;
222 else
223 mult = 512;
224
225 return MXC_CLK32 * mult;
226}
227#endif
228
Stefano Babica521a772010-01-20 18:19:32 +0100229/*
Benoît Thébaudeau96d9df32012-09-27 10:22:51 +0000230 * This function returns the low power audio clock.
231 */
232static u32 get_lp_apm(void)
233{
234 u32 ret_val = 0;
235 u32 ccsr = readl(&mxc_ccm->ccsr);
236
237 if (ccsr & MXC_CCM_CCSR_LP_APM)
238#if defined(CONFIG_MX51)
239 ret_val = get_fpm();
240#elif defined(CONFIG_MX53)
241 ret_val = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
242#endif
243 else
244 ret_val = MXC_HCLK;
245
246 return ret_val;
247}
248
249/*
Stefano Babica521a772010-01-20 18:19:32 +0100250 * Get mcu main rate
251 */
252u32 get_mcu_main_clk(void)
253{
254 u32 reg, freq;
255
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000256 reg = MXC_CCM_CACRR_ARM_PODF_RD(readl(&mxc_ccm->cacrr));
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000257 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babica521a772010-01-20 18:19:32 +0100258 return freq / (reg + 1);
259}
260
261/*
262 * Get the rate of peripheral's root clock.
263 */
Fabio Estevam6479f512012-04-29 08:11:13 +0000264u32 get_periph_clk(void)
Stefano Babica521a772010-01-20 18:19:32 +0100265{
266 u32 reg;
267
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000268 reg = readl(&mxc_ccm->cbcdr);
Stefano Babica521a772010-01-20 18:19:32 +0100269 if (!(reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL))
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000270 return decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000271 reg = readl(&mxc_ccm->cbcmr);
272 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(reg)) {
Stefano Babica521a772010-01-20 18:19:32 +0100273 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000274 return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babica521a772010-01-20 18:19:32 +0100275 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000276 return decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Benoît Thébaudeau96d9df32012-09-27 10:22:51 +0000277 case 2:
278 return get_lp_apm();
Stefano Babica521a772010-01-20 18:19:32 +0100279 default:
280 return 0;
281 }
282 /* NOTREACHED */
283}
284
285/*
286 * Get the rate of ipg clock.
287 */
288static u32 get_ipg_clk(void)
289{
Marek Vasut6674bf52011-09-22 09:20:37 +0000290 uint32_t freq, reg, div;
Stefano Babica521a772010-01-20 18:19:32 +0100291
Marek Vasut6674bf52011-09-22 09:20:37 +0000292 freq = get_ahb_clk();
293
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000294 reg = readl(&mxc_ccm->cbcdr);
295 div = MXC_CCM_CBCDR_IPG_PODF_RD(reg) + 1;
Marek Vasut6674bf52011-09-22 09:20:37 +0000296
297 return freq / div;
Stefano Babica521a772010-01-20 18:19:32 +0100298}
299
300/*
301 * Get the rate of ipg_per clock.
302 */
303static u32 get_ipg_per_clk(void)
304{
Benoît Thébaudeau0cef3ae2012-09-27 10:23:08 +0000305 u32 freq, pred1, pred2, podf;
Stefano Babica521a772010-01-20 18:19:32 +0100306
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000307 if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_IPG_CLK_SEL)
Stefano Babica521a772010-01-20 18:19:32 +0100308 return get_ipg_clk();
Benoît Thébaudeau0cef3ae2012-09-27 10:23:08 +0000309
310 if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_LP_APM_CLK_SEL)
311 freq = get_lp_apm();
312 else
313 freq = get_periph_clk();
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000314 podf = readl(&mxc_ccm->cbcdr);
315 pred1 = MXC_CCM_CBCDR_PERCLK_PRED1_RD(podf);
316 pred2 = MXC_CCM_CBCDR_PERCLK_PRED2_RD(podf);
317 podf = MXC_CCM_CBCDR_PERCLK_PODF_RD(podf);
Benoît Thébaudeau0cef3ae2012-09-27 10:23:08 +0000318 return freq / ((pred1 + 1) * (pred2 + 1) * (podf + 1));
Stefano Babica521a772010-01-20 18:19:32 +0100319}
320
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000321/* Get the output clock rate of a standard PLL MUX for peripherals. */
322static u32 get_standard_pll_sel_clk(u32 clk_sel)
Stefano Babica521a772010-01-20 18:19:32 +0100323{
Stefano Babicfd375f32012-10-24 10:06:28 +0200324 u32 freq = 0;
Stefano Babica521a772010-01-20 18:19:32 +0100325
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000326 switch (clk_sel & 0x3) {
327 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000328 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Stefano Babica521a772010-01-20 18:19:32 +0100329 break;
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000330 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000331 freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Stefano Babica521a772010-01-20 18:19:32 +0100332 break;
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000333 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000334 freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Stefano Babica521a772010-01-20 18:19:32 +0100335 break;
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000336 case 3:
337 freq = get_lp_apm();
338 break;
Stefano Babica521a772010-01-20 18:19:32 +0100339 }
340
Benoît Thébaudeau9aa99c42012-09-27 10:23:23 +0000341 return freq;
342}
343
344/*
345 * Get the rate of uart clk.
346 */
347static u32 get_uart_clk(void)
348{
349 unsigned int clk_sel, freq, reg, pred, podf;
350
351 reg = readl(&mxc_ccm->cscmr1);
352 clk_sel = MXC_CCM_CSCMR1_UART_CLK_SEL_RD(reg);
353 freq = get_standard_pll_sel_clk(clk_sel);
354
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000355 reg = readl(&mxc_ccm->cscdr1);
356 pred = MXC_CCM_CSCDR1_UART_CLK_PRED_RD(reg);
357 podf = MXC_CCM_CSCDR1_UART_CLK_PODF_RD(reg);
Stefano Babica521a772010-01-20 18:19:32 +0100358 freq /= (pred + 1) * (podf + 1);
359
360 return freq;
361}
362
363/*
Stefano Babica521a772010-01-20 18:19:32 +0100364 * get cspi clock rate.
365 */
Fabio Estevamb4121012012-04-30 08:12:02 +0000366static u32 imx_get_cspiclk(void)
Stefano Babica521a772010-01-20 18:19:32 +0100367{
Benoît Thébaudeau81fb2742012-09-27 10:23:42 +0000368 u32 ret_val = 0, pdf, pre_pdf, clk_sel, freq;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000369 u32 cscmr1 = readl(&mxc_ccm->cscmr1);
370 u32 cscdr2 = readl(&mxc_ccm->cscdr2);
Stefano Babica521a772010-01-20 18:19:32 +0100371
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000372 pre_pdf = MXC_CCM_CSCDR2_CSPI_CLK_PRED_RD(cscdr2);
373 pdf = MXC_CCM_CSCDR2_CSPI_CLK_PODF_RD(cscdr2);
374 clk_sel = MXC_CCM_CSCMR1_CSPI_CLK_SEL_RD(cscmr1);
Benoît Thébaudeau81fb2742012-09-27 10:23:42 +0000375 freq = get_standard_pll_sel_clk(clk_sel);
376 ret_val = freq / ((pre_pdf + 1) * (pdf + 1));
Stefano Babica521a772010-01-20 18:19:32 +0100377 return ret_val;
378}
379
Benoît Thébaudeaueb9c2552012-09-27 10:24:37 +0000380/*
381 * get esdhc clock rate.
382 */
383static u32 get_esdhc_clk(u32 port)
384{
385 u32 clk_sel = 0, pred = 0, podf = 0, freq = 0;
386 u32 cscmr1 = readl(&mxc_ccm->cscmr1);
387 u32 cscdr1 = readl(&mxc_ccm->cscdr1);
388
389 switch (port) {
390 case 0:
391 clk_sel = MXC_CCM_CSCMR1_ESDHC1_MSHC1_CLK_SEL_RD(cscmr1);
392 pred = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PRED_RD(cscdr1);
393 podf = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PODF_RD(cscdr1);
394 break;
395 case 1:
396 clk_sel = MXC_CCM_CSCMR1_ESDHC2_MSHC2_CLK_SEL_RD(cscmr1);
397 pred = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PRED_RD(cscdr1);
398 podf = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PODF_RD(cscdr1);
399 break;
400 case 2:
401 if (cscmr1 & MXC_CCM_CSCMR1_ESDHC3_CLK_SEL)
402 return get_esdhc_clk(1);
403 else
404 return get_esdhc_clk(0);
405 case 3:
406 if (cscmr1 & MXC_CCM_CSCMR1_ESDHC4_CLK_SEL)
407 return get_esdhc_clk(1);
408 else
409 return get_esdhc_clk(0);
410 default:
411 break;
412 }
413
414 freq = get_standard_pll_sel_clk(clk_sel) / ((pred + 1) * (podf + 1));
415 return freq;
416}
417
Fabio Estevamb4121012012-04-30 08:12:02 +0000418static u32 get_axi_a_clk(void)
419{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000420 u32 cbcdr = readl(&mxc_ccm->cbcdr);
421 u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000422
423 return get_periph_clk() / (pdf + 1);
424}
425
426static u32 get_axi_b_clk(void)
427{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000428 u32 cbcdr = readl(&mxc_ccm->cbcdr);
429 u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000430
431 return get_periph_clk() / (pdf + 1);
432}
433
434static u32 get_emi_slow_clk(void)
435{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000436 u32 cbcdr = readl(&mxc_ccm->cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000437 u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000438 u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000439
440 if (emi_clk_sel)
441 return get_ahb_clk() / (pdf + 1);
442
443 return get_periph_clk() / (pdf + 1);
444}
445
446static u32 get_ddr_clk(void)
447{
448 u32 ret_val = 0;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000449 u32 cbcmr = readl(&mxc_ccm->cbcmr);
450 u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000451#ifdef CONFIG_MX51
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000452 u32 cbcdr = readl(&mxc_ccm->cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000453 if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) {
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000454 u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000455
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000456 ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Fabio Estevamb4121012012-04-30 08:12:02 +0000457 ret_val /= ddr_clk_podf + 1;
458
459 return ret_val;
460 }
461#endif
462 switch (ddr_clk_sel) {
463 case 0:
464 ret_val = get_axi_a_clk();
465 break;
466 case 1:
467 ret_val = get_axi_b_clk();
468 break;
469 case 2:
470 ret_val = get_emi_slow_clk();
471 break;
472 case 3:
473 ret_val = get_ahb_clk();
474 break;
475 default:
476 break;
477 }
478
479 return ret_val;
480}
481
Stefano Babica521a772010-01-20 18:19:32 +0100482/*
Fabio Estevamb4121012012-04-30 08:12:02 +0000483 * The API of get mxc clocks.
Stefano Babica521a772010-01-20 18:19:32 +0100484 */
485unsigned int mxc_get_clock(enum mxc_clock clk)
486{
487 switch (clk) {
488 case MXC_ARM_CLK:
489 return get_mcu_main_clk();
490 case MXC_AHB_CLK:
Marek Vasut6674bf52011-09-22 09:20:37 +0000491 return get_ahb_clk();
Stefano Babica521a772010-01-20 18:19:32 +0100492 case MXC_IPG_CLK:
493 return get_ipg_clk();
494 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000495 case MXC_I2C_CLK:
Stefano Babica521a772010-01-20 18:19:32 +0100496 return get_ipg_per_clk();
497 case MXC_UART_CLK:
498 return get_uart_clk();
499 case MXC_CSPI_CLK:
500 return imx_get_cspiclk();
Benoît Thébaudeaueb9c2552012-09-27 10:24:37 +0000501 case MXC_ESDHC_CLK:
502 return get_esdhc_clk(0);
503 case MXC_ESDHC2_CLK:
504 return get_esdhc_clk(1);
505 case MXC_ESDHC3_CLK:
506 return get_esdhc_clk(2);
507 case MXC_ESDHC4_CLK:
508 return get_esdhc_clk(3);
Stefano Babica521a772010-01-20 18:19:32 +0100509 case MXC_FEC_CLK:
Benoît Thébaudeau743656e2012-09-27 10:23:58 +0000510 return get_ipg_clk();
Stefano Babicd38db762012-02-22 00:24:36 +0000511 case MXC_SATA_CLK:
512 return get_ahb_clk();
Fabio Estevamb4121012012-04-30 08:12:02 +0000513 case MXC_DDR_CLK:
514 return get_ddr_clk();
Stefano Babica521a772010-01-20 18:19:32 +0100515 default:
516 break;
517 }
Fabio Estevamb4121012012-04-30 08:12:02 +0000518 return -EINVAL;
Stefano Babica521a772010-01-20 18:19:32 +0100519}
520
521u32 imx_get_uartclk(void)
522{
523 return get_uart_clk();
524}
525
Stefano Babica521a772010-01-20 18:19:32 +0100526u32 imx_get_fecclk(void)
527{
Benoît Thébaudeau743656e2012-09-27 10:23:58 +0000528 return get_ipg_clk();
Stefano Babica521a772010-01-20 18:19:32 +0100529}
530
Fabio Estevamb4121012012-04-30 08:12:02 +0000531static int gcd(int m, int n)
532{
533 int t;
534 while (m > 0) {
535 if (n > m) {
536 t = m;
537 m = n;
538 n = t;
539 } /* swap */
540 m -= n;
541 }
542 return n;
543}
544
545/*
546 * This is to calculate various parameters based on reference clock and
547 * targeted clock based on the equation:
548 * t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1)
549 * This calculation is based on a fixed MFD value for simplicity.
550 */
551static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll)
552{
553 u64 pd, mfi = 1, mfn, mfd, t1;
554 u32 n_target = target;
555 u32 n_ref = ref, i;
556
557 /*
558 * Make sure targeted freq is in the valid range.
559 * Otherwise the following calculation might be wrong!!!
560 */
561 if (n_target < PLL_FREQ_MIN(ref) ||
562 n_target > PLL_FREQ_MAX(ref)) {
563 printf("Targeted peripheral clock should be"
564 "within [%d - %d]\n",
565 PLL_FREQ_MIN(ref) / SZ_DEC_1M,
566 PLL_FREQ_MAX(ref) / SZ_DEC_1M);
567 return -EINVAL;
568 }
569
570 for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) {
571 if (fixed_mfd[i].ref_clk_hz == ref) {
572 mfd = fixed_mfd[i].mfd;
573 break;
574 }
575 }
576
577 if (i == ARRAY_SIZE(fixed_mfd))
578 return -EINVAL;
579
580 /* Use n_target and n_ref to avoid overflow */
581 for (pd = 1; pd <= PLL_PD_MAX; pd++) {
582 t1 = n_target * pd;
583 do_div(t1, (4 * n_ref));
584 mfi = t1;
585 if (mfi > PLL_MFI_MAX)
586 return -EINVAL;
587 else if (mfi < 5)
588 continue;
589 break;
590 }
591 /*
592 * Now got pd and mfi already
593 *
594 * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref;
595 */
596 t1 = n_target * pd;
597 do_div(t1, 4);
598 t1 -= n_ref * mfi;
599 t1 *= mfd;
600 do_div(t1, n_ref);
601 mfn = t1;
602 debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n",
603 ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd);
604 i = 1;
605 if (mfn != 0)
606 i = gcd(mfd, mfn);
607 pll->pd = (u32)pd;
608 pll->mfi = (u32)mfi;
609 do_div(mfn, i);
610 pll->mfn = (u32)mfn;
611 do_div(mfd, i);
612 pll->mfd = (u32)mfd;
613
614 return 0;
615}
616
617#define calc_div(tgt_clk, src_clk, limit) ({ \
618 u32 v = 0; \
619 if (((src_clk) % (tgt_clk)) <= 100) \
620 v = (src_clk) / (tgt_clk); \
621 else \
622 v = ((src_clk) / (tgt_clk)) + 1;\
623 if (v > limit) \
624 v = limit; \
625 (v - 1); \
626 })
627
628#define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \
629 { \
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000630 writel(0x1232, &pll->ctrl); \
631 writel(0x2, &pll->config); \
632 writel((((pd) - 1) << 0) | ((fi) << 4), \
633 &pll->op); \
634 writel(fn, &(pll->mfn)); \
635 writel((fd) - 1, &pll->mfd); \
636 writel((((pd) - 1) << 0) | ((fi) << 4), \
637 &pll->hfs_op); \
638 writel(fn, &pll->hfs_mfn); \
639 writel((fd) - 1, &pll->hfs_mfd); \
640 writel(0x1232, &pll->ctrl); \
641 while (!readl(&pll->ctrl) & 0x1) \
Fabio Estevamb4121012012-04-30 08:12:02 +0000642 ;\
643 }
644
645static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param)
646{
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000647 u32 ccsr = readl(&mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000648 struct mxc_pll_reg *pll = mxc_plls[index];
649
650 switch (index) {
651 case PLL1_CLOCK:
652 /* Switch ARM to PLL2 clock */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000653 writel(ccsr | MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
654 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000655 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
656 pll_param->mfi, pll_param->mfn,
657 pll_param->mfd);
658 /* Switch back */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000659 writel(ccsr & ~MXC_CCM_CCSR_PLL1_SW_CLK_SEL,
660 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000661 break;
662 case PLL2_CLOCK:
663 /* Switch to pll2 bypass clock */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000664 writel(ccsr | MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
665 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000666 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
667 pll_param->mfi, pll_param->mfn,
668 pll_param->mfd);
669 /* Switch back */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000670 writel(ccsr & ~MXC_CCM_CCSR_PLL2_SW_CLK_SEL,
671 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000672 break;
673 case PLL3_CLOCK:
674 /* Switch to pll3 bypass clock */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000675 writel(ccsr | MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
676 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000677 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
678 pll_param->mfi, pll_param->mfn,
679 pll_param->mfd);
680 /* Switch back */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000681 writel(ccsr & ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL,
682 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000683 break;
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000684#ifdef CONFIG_MX53
Fabio Estevamb4121012012-04-30 08:12:02 +0000685 case PLL4_CLOCK:
686 /* Switch to pll4 bypass clock */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000687 writel(ccsr | MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
688 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000689 CHANGE_PLL_SETTINGS(pll, pll_param->pd,
690 pll_param->mfi, pll_param->mfn,
691 pll_param->mfd);
692 /* Switch back */
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000693 writel(ccsr & ~MXC_CCM_CCSR_PLL4_SW_CLK_SEL,
694 &mxc_ccm->ccsr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000695 break;
Benoît Thébaudeau3c98eb52012-09-27 10:22:22 +0000696#endif
Fabio Estevamb4121012012-04-30 08:12:02 +0000697 default:
698 return -EINVAL;
699 }
700
701 return 0;
702}
703
704/* Config CPU clock */
705static int config_core_clk(u32 ref, u32 freq)
706{
707 int ret = 0;
708 struct pll_param pll_param;
709
710 memset(&pll_param, 0, sizeof(struct pll_param));
711
712 /* The case that periph uses PLL1 is not considered here */
713 ret = calc_pll_params(ref, freq, &pll_param);
714 if (ret != 0) {
715 printf("Error:Can't find pll parameters: %d\n", ret);
716 return ret;
717 }
718
719 return config_pll_clk(PLL1_CLOCK, &pll_param);
720}
721
722static int config_nfc_clk(u32 nfc_clk)
723{
Fabio Estevamb4121012012-04-30 08:12:02 +0000724 u32 parent_rate = get_emi_slow_clk();
Marek Vasut24775482013-04-21 05:52:26 +0000725 u32 div;
Fabio Estevamb4121012012-04-30 08:12:02 +0000726
Marek Vasut24775482013-04-21 05:52:26 +0000727 if (nfc_clk == 0)
Fabio Estevamb4121012012-04-30 08:12:02 +0000728 return -EINVAL;
Marek Vasut24775482013-04-21 05:52:26 +0000729 div = parent_rate / nfc_clk;
Fabio Estevamb4121012012-04-30 08:12:02 +0000730 if (div == 0)
731 div++;
732 if (parent_rate / div > NFC_CLK_MAX)
733 div++;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000734 clrsetbits_le32(&mxc_ccm->cbcdr,
735 MXC_CCM_CBCDR_NFC_PODF_MASK,
736 MXC_CCM_CBCDR_NFC_PODF(div - 1));
737 while (readl(&mxc_ccm->cdhipr) != 0)
Fabio Estevamb4121012012-04-30 08:12:02 +0000738 ;
739 return 0;
740}
741
Marek Vasut24775482013-04-21 05:52:26 +0000742void enable_nfc_clk(unsigned char enable)
743{
744 unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
745
746 clrsetbits_le32(&mxc_ccm->CCGR5,
747 MXC_CCM_CCGR5_EMI_ENFC(MXC_CCM_CCGR_CG_MASK),
748 MXC_CCM_CCGR5_EMI_ENFC(cg));
749}
750
Sergey Alyoshina1d1ff62013-12-17 23:24:54 +0400751#ifdef CONFIG_FSL_IIM
752void enable_efuse_prog_supply(bool enable)
753{
754 if (enable)
755 setbits_le32(&mxc_ccm->cgpr,
756 MXC_CCM_CGPR_EFUSE_PROG_SUPPLY_GATE);
757 else
758 clrbits_le32(&mxc_ccm->cgpr,
759 MXC_CCM_CGPR_EFUSE_PROG_SUPPLY_GATE);
760}
761#endif
762
Fabio Estevamb4121012012-04-30 08:12:02 +0000763/* Config main_bus_clock for periphs */
764static int config_periph_clk(u32 ref, u32 freq)
765{
766 int ret = 0;
767 struct pll_param pll_param;
768
769 memset(&pll_param, 0, sizeof(struct pll_param));
770
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000771 if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
Fabio Estevamb4121012012-04-30 08:12:02 +0000772 ret = calc_pll_params(ref, freq, &pll_param);
773 if (ret != 0) {
774 printf("Error:Can't find pll parameters: %d\n",
775 ret);
776 return ret;
777 }
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000778 switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(
779 readl(&mxc_ccm->cbcmr))) {
Fabio Estevamb4121012012-04-30 08:12:02 +0000780 case 0:
781 return config_pll_clk(PLL1_CLOCK, &pll_param);
782 break;
783 case 1:
784 return config_pll_clk(PLL3_CLOCK, &pll_param);
785 break;
786 default:
787 return -EINVAL;
788 }
789 }
790
791 return 0;
792}
793
794static int config_ddr_clk(u32 emi_clk)
795{
796 u32 clk_src;
797 s32 shift = 0, clk_sel, div = 1;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000798 u32 cbcmr = readl(&mxc_ccm->cbcmr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000799
800 if (emi_clk > MAX_DDR_CLK) {
801 printf("Warning:DDR clock should not exceed %d MHz\n",
802 MAX_DDR_CLK / SZ_DEC_1M);
803 emi_clk = MAX_DDR_CLK;
804 }
805
806 clk_src = get_periph_clk();
807 /* Find DDR clock input */
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000808 clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000809 switch (clk_sel) {
810 case 0:
811 shift = 16;
812 break;
813 case 1:
814 shift = 19;
815 break;
816 case 2:
817 shift = 22;
818 break;
819 case 3:
820 shift = 10;
821 break;
822 default:
823 return -EINVAL;
824 }
825
826 if ((clk_src % emi_clk) < 10000000)
827 div = clk_src / emi_clk;
828 else
829 div = (clk_src / emi_clk) + 1;
830 if (div > 8)
831 div = 8;
832
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000833 clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift);
834 while (readl(&mxc_ccm->cdhipr) != 0)
Fabio Estevamb4121012012-04-30 08:12:02 +0000835 ;
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000836 writel(0x0, &mxc_ccm->ccdr);
Fabio Estevamb4121012012-04-30 08:12:02 +0000837
838 return 0;
839}
840
Marek Vasut01466102018-10-10 10:40:02 +0200841#ifdef CONFIG_MX53
842static int config_ldb_clk(u32 ref, u32 freq)
843{
844 int ret = 0;
845 struct pll_param pll_param;
846
847 memset(&pll_param, 0, sizeof(struct pll_param));
848
849 ret = calc_pll_params(ref, freq, &pll_param);
850 if (ret != 0) {
851 printf("Error:Can't find pll parameters: %d\n",
852 ret);
853 return ret;
854 }
855
856 return config_pll_clk(PLL4_CLOCK, &pll_param);
857}
858#else
859static int config_ldb_clk(u32 ref, u32 freq)
860{
861 /* Platform not supported */
862 return -EINVAL;
863}
864#endif
865
Fabio Estevamb4121012012-04-30 08:12:02 +0000866/*
867 * This function assumes the expected core clock has to be changed by
868 * modifying the PLL. This is NOT true always but for most of the times,
869 * it is. So it assumes the PLL output freq is the same as the expected
870 * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN.
871 * In the latter case, it will try to increase the presc value until
872 * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to
873 * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based
874 * on the targeted PLL and reference input clock to the PLL. Lastly,
875 * it sets the register based on these values along with the dividers.
876 * Note 1) There is no value checking for the passed-in divider values
877 * so the caller has to make sure those values are sensible.
878 * 2) Also adjust the NFC divider such that the NFC clock doesn't
879 * exceed NFC_CLK_MAX.
880 * 3) IPU HSP clock is independent of AHB clock. Even it can go up to
881 * 177MHz for higher voltage, this function fixes the max to 133MHz.
882 * 4) This function should not have allowed diag_printf() calls since
883 * the serial driver has been stoped. But leave then here to allow
884 * easy debugging by NOT calling the cyg_hal_plf_serial_stop().
885 */
886int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk)
887{
888 freq *= SZ_DEC_1M;
889
890 switch (clk) {
891 case MXC_ARM_CLK:
892 if (config_core_clk(ref, freq))
893 return -EINVAL;
894 break;
895 case MXC_PERIPH_CLK:
896 if (config_periph_clk(ref, freq))
897 return -EINVAL;
898 break;
899 case MXC_DDR_CLK:
900 if (config_ddr_clk(freq))
901 return -EINVAL;
902 break;
903 case MXC_NFC_CLK:
904 if (config_nfc_clk(freq))
905 return -EINVAL;
906 break;
Marek Vasut01466102018-10-10 10:40:02 +0200907 case MXC_LDB_CLK:
908 if (config_ldb_clk(ref, freq))
909 return -EINVAL;
910 break;
Fabio Estevamb4121012012-04-30 08:12:02 +0000911 default:
912 printf("Warning:Unsupported or invalid clock type\n");
913 }
914
915 return 0;
916}
917
Stefano Babicc378abf2012-02-22 00:24:38 +0000918#ifdef CONFIG_MX53
919/*
920 * The clock for the external interface can be set to use internal clock
921 * if fuse bank 4, row 3, bit 2 is set.
922 * This is an undocumented feature and it was confirmed by Freescale's support:
923 * Fuses (but not pins) may be used to configure SATA clocks.
924 * Particularly the i.MX53 Fuse_Map contains the next information
925 * about configuring SATA clocks : SATA_ALT_REF_CLK[1:0] (offset 0x180C)
926 * '00' - 100MHz (External)
927 * '01' - 50MHz (External)
928 * '10' - 120MHz, internal (USB PHY)
929 * '11' - Reserved
930*/
931void mxc_set_sata_internal_clock(void)
932{
933 u32 *tmp_base =
934 (u32 *)(IIM_BASE_ADDR + 0x180c);
935
Benoît Thébaudeauf2f00382012-09-28 07:09:03 +0000936 set_usb_phy_clk();
Stefano Babicc378abf2012-02-22 00:24:38 +0000937
Benoît Thébaudeaue5a10172012-09-27 10:20:33 +0000938 clrsetbits_le32(tmp_base, 0x6, 0x4);
Stefano Babicc378abf2012-02-22 00:24:38 +0000939}
940#endif
941
Tom Rini2f218872018-01-03 08:52:39 -0500942#ifndef CONFIG_SPL_BUILD
Stefano Babica521a772010-01-20 18:19:32 +0100943/*
944 * Dump some core clockes.
945 */
Tom Rini2f218872018-01-03 08:52:39 -0500946static int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefano Babica521a772010-01-20 18:19:32 +0100947{
948 u32 freq;
949
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000950 freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
Marek Vasut421bf452011-09-14 14:09:04 +0000951 printf("PLL1 %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000952 freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
Marek Vasut421bf452011-09-14 14:09:04 +0000953 printf("PLL2 %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000954 freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
Marek Vasut421bf452011-09-14 14:09:04 +0000955 printf("PLL3 %8d MHz\n", freq / 1000000);
Marek Vasut3c844f32011-09-23 11:43:47 +0200956#ifdef CONFIG_MX53
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000957 freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
Marek Vasut421bf452011-09-14 14:09:04 +0000958 printf("PLL4 %8d MHz\n", freq / 1000000);
Marek Vasut3c844f32011-09-23 11:43:47 +0200959#endif
Marek Vasut421bf452011-09-14 14:09:04 +0000960
961 printf("\n");
962 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
963 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
964 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
Fabio Estevamb4121012012-04-30 08:12:02 +0000965 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
Fabio Estevama82fee02012-11-15 11:23:22 +0000966#ifdef CONFIG_MXC_SPI
967 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
968#endif
Stefano Babica521a772010-01-20 18:19:32 +0100969 return 0;
970}
971
972/***************************************************/
973
974U_BOOT_CMD(
Stefano Babicc8a02c32011-08-17 17:52:40 +0200975 clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks,
976 "display clocks",
Stefano Babica521a772010-01-20 18:19:32 +0100977 ""
978);
Tom Rini2f218872018-01-03 08:52:39 -0500979#endif