blob: 820b8d51547c5fb144d130f088e8a40270bf8874 [file] [log] [blame]
Jason Liudec11122011-11-25 00:18:02 +00001/*
2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Liudec11122011-11-25 00:18:02 +00005 */
6
7#include <common.h>
Christian Gmeinere5848142014-01-08 08:24:25 +01008#include <div64.h>
Jason Liudec11122011-11-25 00:18:02 +00009#include <asm/io.h>
10#include <asm/errno.h>
11#include <asm/arch/imx-regs.h>
Fabio Estevam6479f512012-04-29 08:11:13 +000012#include <asm/arch/crm_regs.h>
Jason Liudec11122011-11-25 00:18:02 +000013#include <asm/arch/clock.h>
Fabio Estevam6479f512012-04-29 08:11:13 +000014#include <asm/arch/sys_proto.h>
Jason Liudec11122011-11-25 00:18:02 +000015
16enum pll_clocks {
17 PLL_SYS, /* System PLL */
18 PLL_BUS, /* System Bus PLL*/
19 PLL_USBOTG, /* OTG USB PLL */
20 PLL_ENET, /* ENET PLL */
21};
22
Fabio Estevam6479f512012-04-29 08:11:13 +000023struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liudec11122011-11-25 00:18:02 +000024
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000025#ifdef CONFIG_MXC_OCOTP
26void enable_ocotp_clk(unsigned char enable)
27{
28 u32 reg;
29
30 reg = __raw_readl(&imx_ccm->CCGR2);
31 if (enable)
32 reg |= MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
33 else
34 reg &= ~MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
35 __raw_writel(reg, &imx_ccm->CCGR2);
36}
37#endif
38
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000039void enable_usboh3_clk(unsigned char enable)
40{
41 u32 reg;
42
43 reg = __raw_readl(&imx_ccm->CCGR6);
44 if (enable)
Eric Nelsone4279542012-09-21 07:33:51 +000045 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000046 else
Eric Nelsone4279542012-09-21 07:33:51 +000047 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000048 __raw_writel(reg, &imx_ccm->CCGR6);
49
50}
51
trema49f40a2013-09-21 18:13:35 +020052#ifdef CONFIG_SYS_I2C_MXC
Troy Kiskyd4fdc992012-07-19 08:18:25 +000053/* i2c_num can be from 0 - 2 */
54int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
55{
56 u32 reg;
57 u32 mask;
58
59 if (i2c_num > 2)
60 return -EINVAL;
Eric Nelsone4279542012-09-21 07:33:51 +000061
62 mask = MXC_CCM_CCGR_CG_MASK
63 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET + (i2c_num << 1));
Troy Kiskyd4fdc992012-07-19 08:18:25 +000064 reg = __raw_readl(&imx_ccm->CCGR2);
65 if (enable)
66 reg |= mask;
67 else
68 reg &= ~mask;
69 __raw_writel(reg, &imx_ccm->CCGR2);
70 return 0;
71}
72#endif
73
Heiko Schocher472a68f2014-07-18 06:07:20 +020074/* spi_num can be from 0 - SPI_MAX_NUM */
75int enable_spi_clk(unsigned char enable, unsigned spi_num)
76{
77 u32 reg;
78 u32 mask;
79
80 if (spi_num > SPI_MAX_NUM)
81 return -EINVAL;
82
83 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
84 reg = __raw_readl(&imx_ccm->CCGR1);
85 if (enable)
86 reg |= mask;
87 else
88 reg &= ~mask;
89 __raw_writel(reg, &imx_ccm->CCGR1);
90 return 0;
91}
Jason Liudec11122011-11-25 00:18:02 +000092static u32 decode_pll(enum pll_clocks pll, u32 infreq)
93{
94 u32 div;
95
96 switch (pll) {
97 case PLL_SYS:
98 div = __raw_readl(&imx_ccm->analog_pll_sys);
99 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
100
Andre Renaudb0be82e2014-06-10 08:47:13 +1200101 return (infreq * div) >> 1;
Jason Liudec11122011-11-25 00:18:02 +0000102 case PLL_BUS:
103 div = __raw_readl(&imx_ccm->analog_pll_528);
104 div &= BM_ANADIG_PLL_528_DIV_SELECT;
105
106 return infreq * (20 + (div << 1));
107 case PLL_USBOTG:
108 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
109 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
110
111 return infreq * (20 + (div << 1));
112 case PLL_ENET:
113 div = __raw_readl(&imx_ccm->analog_pll_enet);
114 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
115
Fabio Estevam93bc8ea2013-12-03 18:26:13 -0200116 return 25000000 * (div + (div >> 1) + 1);
Jason Liudec11122011-11-25 00:18:02 +0000117 default:
118 return 0;
119 }
120 /* NOTREACHED */
121}
Pierre Auberte8e62a72013-09-19 17:48:59 +0200122static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
123{
124 u32 div;
125 u64 freq;
126
127 switch (pll) {
128 case PLL_BUS:
129 if (pfd_num == 3) {
130 /* No PFD3 on PPL2 */
131 return 0;
132 }
133 div = __raw_readl(&imx_ccm->analog_pfd_528);
134 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
135 break;
136 case PLL_USBOTG:
137 div = __raw_readl(&imx_ccm->analog_pfd_480);
138 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
139 break;
140 default:
141 /* No PFD on other PLL */
142 return 0;
143 }
144
Christian Gmeinere5848142014-01-08 08:24:25 +0100145 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Auberte8e62a72013-09-19 17:48:59 +0200146 ANATOP_PFD_FRAC_SHIFT(pfd_num));
147}
Jason Liudec11122011-11-25 00:18:02 +0000148
149static u32 get_mcu_main_clk(void)
150{
151 u32 reg, freq;
152
153 reg = __raw_readl(&imx_ccm->cacrr);
154 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
155 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000156 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000157
158 return freq / (reg + 1);
159}
160
Fabio Estevam6479f512012-04-29 08:11:13 +0000161u32 get_periph_clk(void)
Jason Liudec11122011-11-25 00:18:02 +0000162{
163 u32 reg, freq = 0;
164
165 reg = __raw_readl(&imx_ccm->cbcdr);
166 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
167 reg = __raw_readl(&imx_ccm->cbcmr);
168 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
169 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
170
171 switch (reg) {
172 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000173 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000174 break;
175 case 1:
176 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000177 freq = MXC_HCLK;
Jason Liudec11122011-11-25 00:18:02 +0000178 break;
179 default:
180 break;
181 }
182 } else {
183 reg = __raw_readl(&imx_ccm->cbcmr);
184 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
185 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
186
187 switch (reg) {
188 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000189 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000190 break;
191 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200192 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000193 break;
194 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200195 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000196 break;
197 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200198 /* static / 2 divider */
199 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liudec11122011-11-25 00:18:02 +0000200 break;
201 default:
202 break;
203 }
204 }
205
206 return freq;
207}
208
Jason Liudec11122011-11-25 00:18:02 +0000209static u32 get_ipg_clk(void)
210{
211 u32 reg, ipg_podf;
212
213 reg = __raw_readl(&imx_ccm->cbcdr);
214 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
215 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
216
217 return get_ahb_clk() / (ipg_podf + 1);
218}
219
220static u32 get_ipg_per_clk(void)
221{
222 u32 reg, perclk_podf;
223
224 reg = __raw_readl(&imx_ccm->cscmr1);
225 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
226
227 return get_ipg_clk() / (perclk_podf + 1);
228}
229
230static u32 get_uart_clk(void)
231{
232 u32 reg, uart_podf;
Pierre Auberte8e62a72013-09-19 17:48:59 +0200233 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liudec11122011-11-25 00:18:02 +0000234 reg = __raw_readl(&imx_ccm->cscdr1);
Fabio Estevam712ab882014-06-24 17:40:58 -0300235#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000236 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
237 freq = MXC_HCLK;
238#endif
Jason Liudec11122011-11-25 00:18:02 +0000239 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
240 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
241
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000242 return freq / (uart_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000243}
244
245static u32 get_cspi_clk(void)
246{
247 u32 reg, cspi_podf;
248
249 reg = __raw_readl(&imx_ccm->cscdr2);
250 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
251 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
252
Pierre Auberte8e62a72013-09-19 17:48:59 +0200253 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liudec11122011-11-25 00:18:02 +0000254}
255
256static u32 get_axi_clk(void)
257{
258 u32 root_freq, axi_podf;
259 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
260
261 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
262 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
263
264 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
265 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200266 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000267 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200268 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Jason Liudec11122011-11-25 00:18:02 +0000269 } else
270 root_freq = get_periph_clk();
271
272 return root_freq / (axi_podf + 1);
273}
274
275static u32 get_emi_slow_clk(void)
276{
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500277 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liudec11122011-11-25 00:18:02 +0000278
279 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
280 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
281 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500282 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
283 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liudec11122011-11-25 00:18:02 +0000284
285 switch (emi_clk_sel) {
286 case 0:
287 root_freq = get_axi_clk();
288 break;
289 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000290 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000291 break;
292 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200293 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000294 break;
295 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200296 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000297 break;
298 }
299
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500300 return root_freq / (emi_slow_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000301}
302
Fabio Estevam712ab882014-06-24 17:40:58 -0300303#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000304static u32 get_mmdc_ch0_clk(void)
305{
306 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
307 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
308 u32 freq, podf;
309
310 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) \
311 >> MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
312
313 switch ((cbcmr & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
314 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
315 case 0:
316 freq = decode_pll(PLL_BUS, MXC_HCLK);
317 break;
318 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200319 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000320 break;
321 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200322 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000323 break;
324 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200325 /* static / 2 divider */
326 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000327 }
328
329 return freq / (podf + 1);
330
331}
Otavio Salvadordc074432013-12-16 20:44:05 -0200332#else
333static u32 get_mmdc_ch0_clk(void)
334{
335 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
336 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
337 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300338
Otavio Salvadordc074432013-12-16 20:44:05 -0200339 return get_periph_clk() / (mmdc_ch0_podf + 1);
340}
341#endif
342
343#ifdef CONFIG_FEC_MXC
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200344int enable_fec_anatop_clock(enum enet_freq freq)
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300345{
346 u32 reg = 0;
347 s32 timeout = 100000;
348
349 struct anatop_regs __iomem *anatop =
350 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
351
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200352 if (freq < ENET_25MHz || freq > ENET_125MHz)
353 return -EINVAL;
354
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300355 reg = readl(&anatop->pll_enet);
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200356 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
357 reg |= freq;
358
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300359 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
360 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
361 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
362 writel(reg, &anatop->pll_enet);
363 while (timeout--) {
364 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
365 break;
366 }
367 if (timeout < 0)
368 return -ETIMEDOUT;
369 }
370
371 /* Enable FEC clock */
372 reg |= BM_ANADIG_PLL_ENET_ENABLE;
373 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
374 writel(reg, &anatop->pll_enet);
375
Fabio Estevamd4d60382014-08-15 00:24:30 -0300376#ifdef CONFIG_MX6SX
377 /*
378 * Set enet ahb clock to 200MHz
379 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
380 */
381 reg = readl(&imx_ccm->chsccdr);
382 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
383 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
384 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
385 /* PLL2 PFD2 */
386 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
387 /* Div = 2*/
388 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
389 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
390 writel(reg, &imx_ccm->chsccdr);
391
392 /* Enable enet system clock */
393 reg = readl(&imx_ccm->CCGR3);
394 reg |= MXC_CCM_CCGR3_ENET_MASK;
395 writel(reg, &imx_ccm->CCGR3);
396#endif
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300397 return 0;
398}
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000399#endif
Jason Liudec11122011-11-25 00:18:02 +0000400
401static u32 get_usdhc_clk(u32 port)
402{
403 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
404 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
405 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
406
407 switch (port) {
408 case 0:
409 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
410 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
411 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
412
413 break;
414 case 1:
415 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
416 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
417 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
418
419 break;
420 case 2:
421 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
422 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
423 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
424
425 break;
426 case 3:
427 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
428 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
429 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
430
431 break;
432 default:
433 break;
434 }
435
436 if (clk_sel)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200437 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000438 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200439 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000440
441 return root_freq / (usdhc_podf + 1);
442}
443
444u32 imx_get_uartclk(void)
445{
446 return get_uart_clk();
447}
448
Jason Liu92aa90b2011-12-16 05:17:06 +0000449u32 imx_get_fecclk(void)
450{
Markus Niebel6c109b82014-02-05 10:51:25 +0100451 return mxc_get_clock(MXC_IPG_CLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000452}
453
Marek Vasut563dfb22013-12-14 06:27:26 +0100454static int enable_enet_pll(uint32_t en)
Eric Nelsonfdba0762012-03-27 09:52:21 +0000455{
Eric Nelsonfdba0762012-03-27 09:52:21 +0000456 struct mxc_ccm_reg *const imx_ccm
457 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut563dfb22013-12-14 06:27:26 +0100458 s32 timeout = 100000;
459 u32 reg = 0;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000460
461 /* Enable PLLs */
462 reg = readl(&imx_ccm->analog_pll_enet);
463 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
464 writel(reg, &imx_ccm->analog_pll_enet);
465 reg |= BM_ANADIG_PLL_SYS_ENABLE;
466 while (timeout--) {
467 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
468 break;
469 }
470 if (timeout <= 0)
471 return -EIO;
472 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
473 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100474 reg |= en;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000475 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100476 return 0;
477}
478
Fabio Estevam15af7332014-06-24 17:41:00 -0300479#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100480static void ungate_sata_clock(void)
481{
482 struct mxc_ccm_reg *const imx_ccm =
483 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
484
485 /* Enable SATA clock. */
486 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
487}
Fabio Estevam15af7332014-06-24 17:41:00 -0300488#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100489
490static void ungate_pcie_clock(void)
491{
492 struct mxc_ccm_reg *const imx_ccm =
493 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
494
495 /* Enable PCIe clock. */
496 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
497}
498
Fabio Estevam15af7332014-06-24 17:41:00 -0300499#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100500int enable_sata_clock(void)
501{
502 ungate_sata_clock();
503 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
504}
Fabio Estevam15af7332014-06-24 17:41:00 -0300505#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100506
507int enable_pcie_clock(void)
508{
509 struct anatop_regs *anatop_regs =
510 (struct anatop_regs *)ANATOP_BASE_ADDR;
511 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
512
513 /*
514 * Here be dragons!
515 *
516 * The register ANATOP_MISC1 is not documented in the Freescale
517 * MX6RM. The register that is mapped in the ANATOP space and
518 * marked as ANATOP_MISC1 is actually documented in the PMU section
519 * of the datasheet as PMU_MISC1.
520 *
521 * Switch LVDS clock source to SATA (0xb), disable clock INPUT and
522 * enable clock OUTPUT. This is important for PCI express link that
523 * is clocked from the i.MX6.
524 */
525#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
526#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
527#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
528 clrsetbits_le32(&anatop_regs->ana_misc1,
529 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
530 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
531 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | 0xb);
532
533 /* PCIe reference clock sourced from AXI. */
534 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
535
536 /* Party time! Ungate the clock to the PCIe. */
Fabio Estevam15af7332014-06-24 17:41:00 -0300537#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100538 ungate_sata_clock();
Fabio Estevam15af7332014-06-24 17:41:00 -0300539#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100540 ungate_pcie_clock();
Eric Nelsonfdba0762012-03-27 09:52:21 +0000541
Marek Vasut563dfb22013-12-14 06:27:26 +0100542 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
543 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelsonfdba0762012-03-27 09:52:21 +0000544}
545
Jason Liudec11122011-11-25 00:18:02 +0000546unsigned int mxc_get_clock(enum mxc_clock clk)
547{
548 switch (clk) {
549 case MXC_ARM_CLK:
550 return get_mcu_main_clk();
551 case MXC_PER_CLK:
552 return get_periph_clk();
553 case MXC_AHB_CLK:
554 return get_ahb_clk();
555 case MXC_IPG_CLK:
556 return get_ipg_clk();
557 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000558 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +0000559 return get_ipg_per_clk();
560 case MXC_UART_CLK:
561 return get_uart_clk();
562 case MXC_CSPI_CLK:
563 return get_cspi_clk();
564 case MXC_AXI_CLK:
565 return get_axi_clk();
566 case MXC_EMI_SLOW_CLK:
567 return get_emi_slow_clk();
568 case MXC_DDR_CLK:
569 return get_mmdc_ch0_clk();
570 case MXC_ESDHC_CLK:
571 return get_usdhc_clk(0);
572 case MXC_ESDHC2_CLK:
573 return get_usdhc_clk(1);
574 case MXC_ESDHC3_CLK:
575 return get_usdhc_clk(2);
576 case MXC_ESDHC4_CLK:
577 return get_usdhc_clk(3);
578 case MXC_SATA_CLK:
579 return get_ahb_clk();
580 default:
581 break;
582 }
583
584 return -1;
585}
586
587/*
588 * Dump some core clockes.
589 */
590int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
591{
592 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000593 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000594 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000595 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000596 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000597 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000598 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000599 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000600 printf("PLL_NET %8d MHz\n", freq / 1000000);
601
602 printf("\n");
603 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
604 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000605#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +0000606 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000607#endif
Jason Liudec11122011-11-25 00:18:02 +0000608 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
609 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
610 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
611 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
612 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
613 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
614 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
615 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
616 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
617
618 return 0;
619}
620
Fabio Estevam15af7332014-06-24 17:41:00 -0300621#ifndef CONFIG_MX6SX
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500622void enable_ipu_clock(void)
623{
624 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
625 int reg;
626 reg = readl(&mxc_ccm->CCGR3);
Pierre Aubert2cb5c382013-09-23 13:37:20 +0200627 reg |= MXC_CCM_CCGR3_IPU1_IPU_MASK;
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500628 writel(reg, &mxc_ccm->CCGR3);
629}
Fabio Estevam15af7332014-06-24 17:41:00 -0300630#endif
Jason Liudec11122011-11-25 00:18:02 +0000631/***************************************************/
632
633U_BOOT_CMD(
634 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
635 "display clocks",
636 ""
637);