blob: ab7ac3d703e2050de9c9ab92f575e8f7055fb17d [file] [log] [blame]
Jason Liu23608e22011-11-25 00:18:02 +00001/*
2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Liu23608e22011-11-25 00:18:02 +00005 */
6
7#include <common.h>
Christian Gmeiner5a660162014-01-08 08:24:25 +01008#include <div64.h>
Jason Liu23608e22011-11-25 00:18:02 +00009#include <asm/io.h>
10#include <asm/errno.h>
11#include <asm/arch/imx-regs.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000012#include <asm/arch/crm_regs.h>
Jason Liu23608e22011-11-25 00:18:02 +000013#include <asm/arch/clock.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000014#include <asm/arch/sys_proto.h>
Jason Liu23608e22011-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 Estevam6a376042012-04-29 08:11:13 +000023struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liu23608e22011-11-25 00:18:02 +000024
Benoît Thébaudeau112fd2e2013-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
Nikita Kiryanov224beb82014-08-20 15:08:49 +030039#ifdef CONFIG_NAND_MXS
40void setup_gpmi_io_clk(u32 cfg)
41{
42 /* Disable clocks per ERR007177 from MX6 errata */
43 clrbits_le32(&imx_ccm->CCGR4,
44 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
45 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
46 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
47 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
48 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
49
50 clrbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
51
52 clrsetbits_le32(&imx_ccm->cs2cdr,
53 MXC_CCM_CS2CDR_ENFC_CLK_PODF_MASK |
54 MXC_CCM_CS2CDR_ENFC_CLK_PRED_MASK |
55 MXC_CCM_CS2CDR_ENFC_CLK_SEL_MASK,
56 cfg);
57
58 setbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
59 setbits_le32(&imx_ccm->CCGR4,
60 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
61 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
62 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
63 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
64 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
65}
66#endif
67
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000068void enable_usboh3_clk(unsigned char enable)
69{
70 u32 reg;
71
72 reg = __raw_readl(&imx_ccm->CCGR6);
73 if (enable)
Eric Nelson0bb7e312012-09-21 07:33:51 +000074 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000075 else
Eric Nelson0bb7e312012-09-21 07:33:51 +000076 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000077 __raw_writel(reg, &imx_ccm->CCGR6);
78
79}
80
Stefano Babic3d8f1792014-09-10 13:02:40 +020081#if defined(CONFIG_FEC_MXC) && !defined(CONFIG_MX6SX)
Nikita Kiryanov224beb82014-08-20 15:08:49 +030082void enable_enet_clk(unsigned char enable)
83{
84 u32 mask = MXC_CCM_CCGR1_ENET_CLK_ENABLE_MASK;
85
86 if (enable)
87 setbits_le32(&imx_ccm->CCGR1, mask);
88 else
89 clrbits_le32(&imx_ccm->CCGR1, mask);
90}
91#endif
92
93#ifdef CONFIG_MXC_UART
94void enable_uart_clk(unsigned char enable)
95{
96 u32 mask = MXC_CCM_CCGR5_UART_MASK | MXC_CCM_CCGR5_UART_SERIAL_MASK;
97
98 if (enable)
99 setbits_le32(&imx_ccm->CCGR5, mask);
100 else
101 clrbits_le32(&imx_ccm->CCGR5, mask);
102}
103#endif
104
105#ifdef CONFIG_SPI
106/* spi_num can be from 0 - 4 */
107int enable_cspi_clock(unsigned char enable, unsigned spi_num)
108{
109 u32 mask;
110
111 if (spi_num > 4)
112 return -EINVAL;
113
114 mask = MXC_CCM_CCGR_CG_MASK << (spi_num * 2);
115 if (enable)
116 setbits_le32(&imx_ccm->CCGR1, mask);
117 else
118 clrbits_le32(&imx_ccm->CCGR1, mask);
119
120 return 0;
121}
122#endif
123
124#ifdef CONFIG_MMC
125int enable_usdhc_clk(unsigned char enable, unsigned bus_num)
126{
127 u32 mask;
128
129 if (bus_num > 3)
130 return -EINVAL;
131
132 mask = MXC_CCM_CCGR_CG_MASK << (bus_num * 2 + 2);
133 if (enable)
134 setbits_le32(&imx_ccm->CCGR6, mask);
135 else
136 clrbits_le32(&imx_ccm->CCGR6, mask);
137
138 return 0;
139}
140#endif
141
tremfac96402013-09-21 18:13:35 +0200142#ifdef CONFIG_SYS_I2C_MXC
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000143/* i2c_num can be from 0 - 2 */
144int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
145{
146 u32 reg;
147 u32 mask;
148
149 if (i2c_num > 2)
150 return -EINVAL;
Eric Nelson0bb7e312012-09-21 07:33:51 +0000151
152 mask = MXC_CCM_CCGR_CG_MASK
153 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET + (i2c_num << 1));
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000154 reg = __raw_readl(&imx_ccm->CCGR2);
155 if (enable)
156 reg |= mask;
157 else
158 reg &= ~mask;
159 __raw_writel(reg, &imx_ccm->CCGR2);
160 return 0;
161}
162#endif
163
Heiko Schochera0ae0092014-07-18 06:07:20 +0200164/* spi_num can be from 0 - SPI_MAX_NUM */
165int enable_spi_clk(unsigned char enable, unsigned spi_num)
166{
167 u32 reg;
168 u32 mask;
169
170 if (spi_num > SPI_MAX_NUM)
171 return -EINVAL;
172
173 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
174 reg = __raw_readl(&imx_ccm->CCGR1);
175 if (enable)
176 reg |= mask;
177 else
178 reg &= ~mask;
179 __raw_writel(reg, &imx_ccm->CCGR1);
180 return 0;
181}
Jason Liu23608e22011-11-25 00:18:02 +0000182static u32 decode_pll(enum pll_clocks pll, u32 infreq)
183{
184 u32 div;
185
186 switch (pll) {
187 case PLL_SYS:
188 div = __raw_readl(&imx_ccm->analog_pll_sys);
189 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
190
Andre Renaud2eb268f2014-06-10 08:47:13 +1200191 return (infreq * div) >> 1;
Jason Liu23608e22011-11-25 00:18:02 +0000192 case PLL_BUS:
193 div = __raw_readl(&imx_ccm->analog_pll_528);
194 div &= BM_ANADIG_PLL_528_DIV_SELECT;
195
196 return infreq * (20 + (div << 1));
197 case PLL_USBOTG:
198 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
199 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
200
201 return infreq * (20 + (div << 1));
202 case PLL_ENET:
203 div = __raw_readl(&imx_ccm->analog_pll_enet);
204 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
205
Fabio Estevam89cfd0f2013-12-03 18:26:13 -0200206 return 25000000 * (div + (div >> 1) + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000207 default:
208 return 0;
209 }
210 /* NOTREACHED */
211}
Pierre Aubert762a88c2013-09-19 17:48:59 +0200212static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
213{
214 u32 div;
215 u64 freq;
216
217 switch (pll) {
218 case PLL_BUS:
219 if (pfd_num == 3) {
220 /* No PFD3 on PPL2 */
221 return 0;
222 }
223 div = __raw_readl(&imx_ccm->analog_pfd_528);
224 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
225 break;
226 case PLL_USBOTG:
227 div = __raw_readl(&imx_ccm->analog_pfd_480);
228 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
229 break;
230 default:
231 /* No PFD on other PLL */
232 return 0;
233 }
234
Christian Gmeiner5a660162014-01-08 08:24:25 +0100235 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Aubert762a88c2013-09-19 17:48:59 +0200236 ANATOP_PFD_FRAC_SHIFT(pfd_num));
237}
Jason Liu23608e22011-11-25 00:18:02 +0000238
239static u32 get_mcu_main_clk(void)
240{
241 u32 reg, freq;
242
243 reg = __raw_readl(&imx_ccm->cacrr);
244 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
245 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000246 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000247
248 return freq / (reg + 1);
249}
250
Fabio Estevam6a376042012-04-29 08:11:13 +0000251u32 get_periph_clk(void)
Jason Liu23608e22011-11-25 00:18:02 +0000252{
253 u32 reg, freq = 0;
254
255 reg = __raw_readl(&imx_ccm->cbcdr);
256 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
257 reg = __raw_readl(&imx_ccm->cbcmr);
258 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
259 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
260
261 switch (reg) {
262 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000263 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000264 break;
265 case 1:
266 case 2:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000267 freq = MXC_HCLK;
Jason Liu23608e22011-11-25 00:18:02 +0000268 break;
269 default:
270 break;
271 }
272 } else {
273 reg = __raw_readl(&imx_ccm->cbcmr);
274 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
275 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
276
277 switch (reg) {
278 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000279 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000280 break;
281 case 1:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200282 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000283 break;
284 case 2:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200285 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +0000286 break;
287 case 3:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200288 /* static / 2 divider */
289 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liu23608e22011-11-25 00:18:02 +0000290 break;
291 default:
292 break;
293 }
294 }
295
296 return freq;
297}
298
Jason Liu23608e22011-11-25 00:18:02 +0000299static u32 get_ipg_clk(void)
300{
301 u32 reg, ipg_podf;
302
303 reg = __raw_readl(&imx_ccm->cbcdr);
304 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
305 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
306
307 return get_ahb_clk() / (ipg_podf + 1);
308}
309
310static u32 get_ipg_per_clk(void)
311{
312 u32 reg, perclk_podf;
313
314 reg = __raw_readl(&imx_ccm->cscmr1);
Ye.Lie68661a2014-10-30 18:20:57 +0800315#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
316 if (reg & MXC_CCM_CSCMR1_PER_CLK_SEL_MASK)
317 return MXC_HCLK; /* OSC 24Mhz */
318#endif
Jason Liu23608e22011-11-25 00:18:02 +0000319 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
320
321 return get_ipg_clk() / (perclk_podf + 1);
322}
323
324static u32 get_uart_clk(void)
325{
326 u32 reg, uart_podf;
Pierre Aubert762a88c2013-09-19 17:48:59 +0200327 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liu23608e22011-11-25 00:18:02 +0000328 reg = __raw_readl(&imx_ccm->cscdr1);
Fabio Estevam05d54b82014-06-24 17:40:58 -0300329#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000330 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
331 freq = MXC_HCLK;
332#endif
Jason Liu23608e22011-11-25 00:18:02 +0000333 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
334 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
335
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000336 return freq / (uart_podf + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000337}
338
339static u32 get_cspi_clk(void)
340{
341 u32 reg, cspi_podf;
342
343 reg = __raw_readl(&imx_ccm->cscdr2);
344 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
345 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
346
Pierre Aubert762a88c2013-09-19 17:48:59 +0200347 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liu23608e22011-11-25 00:18:02 +0000348}
349
350static u32 get_axi_clk(void)
351{
352 u32 root_freq, axi_podf;
353 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
354
355 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
356 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
357
358 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
359 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Aubert762a88c2013-09-19 17:48:59 +0200360 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000361 else
Pierre Aubert762a88c2013-09-19 17:48:59 +0200362 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Jason Liu23608e22011-11-25 00:18:02 +0000363 } else
364 root_freq = get_periph_clk();
365
366 return root_freq / (axi_podf + 1);
367}
368
369static u32 get_emi_slow_clk(void)
370{
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500371 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liu23608e22011-11-25 00:18:02 +0000372
373 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
374 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
375 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500376 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
377 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liu23608e22011-11-25 00:18:02 +0000378
379 switch (emi_clk_sel) {
380 case 0:
381 root_freq = get_axi_clk();
382 break;
383 case 1:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000384 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000385 break;
386 case 2:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200387 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000388 break;
389 case 3:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200390 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +0000391 break;
392 }
393
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500394 return root_freq / (emi_slow_podf + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000395}
396
Fabio Estevam05d54b82014-06-24 17:40:58 -0300397#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000398static u32 get_mmdc_ch0_clk(void)
399{
400 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
401 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
402 u32 freq, podf;
403
404 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) \
405 >> MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
406
407 switch ((cbcmr & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
408 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
409 case 0:
410 freq = decode_pll(PLL_BUS, MXC_HCLK);
411 break;
412 case 1:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200413 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000414 break;
415 case 2:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200416 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000417 break;
418 case 3:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200419 /* static / 2 divider */
420 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000421 }
422
423 return freq / (podf + 1);
424
425}
Otavio Salvadorc655b812013-12-16 20:44:05 -0200426#else
427static u32 get_mmdc_ch0_clk(void)
428{
429 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
430 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
431 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
Fabio Estevam31f07962013-09-13 00:36:28 -0300432
Otavio Salvadorc655b812013-12-16 20:44:05 -0200433 return get_periph_clk() / (mmdc_ch0_podf + 1);
434}
435#endif
436
437#ifdef CONFIG_FEC_MXC
Fabio Estevam5f98d0b2014-01-03 15:55:57 -0200438int enable_fec_anatop_clock(enum enet_freq freq)
Fabio Estevam31f07962013-09-13 00:36:28 -0300439{
440 u32 reg = 0;
441 s32 timeout = 100000;
442
443 struct anatop_regs __iomem *anatop =
444 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
445
Fabio Estevam5f98d0b2014-01-03 15:55:57 -0200446 if (freq < ENET_25MHz || freq > ENET_125MHz)
447 return -EINVAL;
448
Fabio Estevam31f07962013-09-13 00:36:28 -0300449 reg = readl(&anatop->pll_enet);
Fabio Estevam5f98d0b2014-01-03 15:55:57 -0200450 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
451 reg |= freq;
452
Fabio Estevam31f07962013-09-13 00:36:28 -0300453 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
454 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
455 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
456 writel(reg, &anatop->pll_enet);
457 while (timeout--) {
458 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
459 break;
460 }
461 if (timeout < 0)
462 return -ETIMEDOUT;
463 }
464
465 /* Enable FEC clock */
466 reg |= BM_ANADIG_PLL_ENET_ENABLE;
467 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
468 writel(reg, &anatop->pll_enet);
469
Fabio Estevam5c045cd2014-08-15 00:24:30 -0300470#ifdef CONFIG_MX6SX
471 /*
472 * Set enet ahb clock to 200MHz
473 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
474 */
475 reg = readl(&imx_ccm->chsccdr);
476 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
477 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
478 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
479 /* PLL2 PFD2 */
480 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
481 /* Div = 2*/
482 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
483 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
484 writel(reg, &imx_ccm->chsccdr);
485
486 /* Enable enet system clock */
487 reg = readl(&imx_ccm->CCGR3);
488 reg |= MXC_CCM_CCGR3_ENET_MASK;
489 writel(reg, &imx_ccm->CCGR3);
490#endif
Fabio Estevam31f07962013-09-13 00:36:28 -0300491 return 0;
492}
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000493#endif
Jason Liu23608e22011-11-25 00:18:02 +0000494
495static u32 get_usdhc_clk(u32 port)
496{
497 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
498 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
499 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
500
501 switch (port) {
502 case 0:
503 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
504 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
505 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
506
507 break;
508 case 1:
509 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
510 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
511 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
512
513 break;
514 case 2:
515 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
516 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
517 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
518
519 break;
520 case 3:
521 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
522 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
523 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
524
525 break;
526 default:
527 break;
528 }
529
530 if (clk_sel)
Pierre Aubert762a88c2013-09-19 17:48:59 +0200531 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +0000532 else
Pierre Aubert762a88c2013-09-19 17:48:59 +0200533 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000534
535 return root_freq / (usdhc_podf + 1);
536}
537
538u32 imx_get_uartclk(void)
539{
540 return get_uart_clk();
541}
542
Jason Liuff167df2011-12-16 05:17:06 +0000543u32 imx_get_fecclk(void)
544{
Markus Niebeladadc912014-02-05 10:51:25 +0100545 return mxc_get_clock(MXC_IPG_CLK);
Jason Liuff167df2011-12-16 05:17:06 +0000546}
547
Marek Vasut79814492013-12-14 06:27:26 +0100548static int enable_enet_pll(uint32_t en)
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000549{
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000550 struct mxc_ccm_reg *const imx_ccm
551 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut79814492013-12-14 06:27:26 +0100552 s32 timeout = 100000;
553 u32 reg = 0;
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000554
555 /* Enable PLLs */
556 reg = readl(&imx_ccm->analog_pll_enet);
557 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
558 writel(reg, &imx_ccm->analog_pll_enet);
559 reg |= BM_ANADIG_PLL_SYS_ENABLE;
560 while (timeout--) {
561 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
562 break;
563 }
564 if (timeout <= 0)
565 return -EIO;
566 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
567 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut79814492013-12-14 06:27:26 +0100568 reg |= en;
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000569 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut79814492013-12-14 06:27:26 +0100570 return 0;
571}
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000572
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300573#ifndef CONFIG_MX6SX
Marek Vasut79814492013-12-14 06:27:26 +0100574static void ungate_sata_clock(void)
575{
576 struct mxc_ccm_reg *const imx_ccm =
577 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
578
579 /* Enable SATA clock. */
580 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
581}
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300582#endif
Marek Vasut79814492013-12-14 06:27:26 +0100583
584static void ungate_pcie_clock(void)
585{
586 struct mxc_ccm_reg *const imx_ccm =
587 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
588
589 /* Enable PCIe clock. */
590 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
591}
592
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300593#ifndef CONFIG_MX6SX
Marek Vasut79814492013-12-14 06:27:26 +0100594int enable_sata_clock(void)
595{
596 ungate_sata_clock();
597 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
598}
Nikita Kiryanov8d29cef2014-11-21 12:47:22 +0200599
600void disable_sata_clock(void)
601{
602 struct mxc_ccm_reg *const imx_ccm =
603 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
604
605 clrbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
606}
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300607#endif
Marek Vasut79814492013-12-14 06:27:26 +0100608
609int enable_pcie_clock(void)
610{
611 struct anatop_regs *anatop_regs =
612 (struct anatop_regs *)ANATOP_BASE_ADDR;
613 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300614 u32 lvds1_clk_sel;
Marek Vasut79814492013-12-14 06:27:26 +0100615
616 /*
617 * Here be dragons!
618 *
619 * The register ANATOP_MISC1 is not documented in the Freescale
620 * MX6RM. The register that is mapped in the ANATOP space and
621 * marked as ANATOP_MISC1 is actually documented in the PMU section
622 * of the datasheet as PMU_MISC1.
623 *
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300624 * Switch LVDS clock source to SATA (0xb) on mx6q/dl or PCI (0xa) on
625 * mx6sx, disable clock INPUT and enable clock OUTPUT. This is important
626 * for PCI express link that is clocked from the i.MX6.
Marek Vasut79814492013-12-14 06:27:26 +0100627 */
628#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
629#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
630#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300631#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF 0xa
632#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF 0xb
633
634 if (is_cpu_type(MXC_CPU_MX6SX))
635 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF;
636 else
637 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF;
638
Marek Vasut79814492013-12-14 06:27:26 +0100639 clrsetbits_le32(&anatop_regs->ana_misc1,
640 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
641 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300642 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | lvds1_clk_sel);
Marek Vasut79814492013-12-14 06:27:26 +0100643
644 /* PCIe reference clock sourced from AXI. */
645 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
646
647 /* Party time! Ungate the clock to the PCIe. */
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300648#ifndef CONFIG_MX6SX
Marek Vasut79814492013-12-14 06:27:26 +0100649 ungate_sata_clock();
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300650#endif
Marek Vasut79814492013-12-14 06:27:26 +0100651 ungate_pcie_clock();
652
653 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
654 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000655}
656
Nitin Garg36c1ca42014-09-16 13:33:25 -0500657#ifdef CONFIG_SECURE_BOOT
658void hab_caam_clock_enable(unsigned char enable)
659{
660 u32 reg;
661
662 /* CG4 ~ CG6, CAAM clocks */
663 reg = __raw_readl(&imx_ccm->CCGR0);
664 if (enable)
665 reg |= (MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
666 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
667 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
668 else
669 reg &= ~(MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
670 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
671 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
672 __raw_writel(reg, &imx_ccm->CCGR0);
673
674 /* EMI slow clk */
675 reg = __raw_readl(&imx_ccm->CCGR6);
676 if (enable)
677 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
678 else
679 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
680 __raw_writel(reg, &imx_ccm->CCGR6);
681}
682#endif
683
Nitin Gargcf202d22014-11-20 21:14:12 +0800684static void enable_pll3(void)
685{
686 struct anatop_regs __iomem *anatop =
687 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
688
689 /* make sure pll3 is enabled */
690 if ((readl(&anatop->usb1_pll_480_ctrl) &
691 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0) {
692 /* enable pll's power */
693 writel(BM_ANADIG_USB1_PLL_480_CTRL_POWER,
694 &anatop->usb1_pll_480_ctrl_set);
695 writel(0x80, &anatop->ana_misc2_clr);
696 /* wait for pll lock */
697 while ((readl(&anatop->usb1_pll_480_ctrl) &
698 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0)
699 ;
700 /* disable bypass */
701 writel(BM_ANADIG_USB1_PLL_480_CTRL_BYPASS,
702 &anatop->usb1_pll_480_ctrl_clr);
703 /* enable pll output */
704 writel(BM_ANADIG_USB1_PLL_480_CTRL_ENABLE,
705 &anatop->usb1_pll_480_ctrl_set);
706 }
707}
708
709void enable_thermal_clk(void)
710{
711 enable_pll3();
712}
713
Jason Liu23608e22011-11-25 00:18:02 +0000714unsigned int mxc_get_clock(enum mxc_clock clk)
715{
716 switch (clk) {
717 case MXC_ARM_CLK:
718 return get_mcu_main_clk();
719 case MXC_PER_CLK:
720 return get_periph_clk();
721 case MXC_AHB_CLK:
722 return get_ahb_clk();
723 case MXC_IPG_CLK:
724 return get_ipg_clk();
725 case MXC_IPG_PERCLK:
Matthias Weissere7bed5c2012-09-24 02:46:53 +0000726 case MXC_I2C_CLK:
Jason Liu23608e22011-11-25 00:18:02 +0000727 return get_ipg_per_clk();
728 case MXC_UART_CLK:
729 return get_uart_clk();
730 case MXC_CSPI_CLK:
731 return get_cspi_clk();
732 case MXC_AXI_CLK:
733 return get_axi_clk();
734 case MXC_EMI_SLOW_CLK:
735 return get_emi_slow_clk();
736 case MXC_DDR_CLK:
737 return get_mmdc_ch0_clk();
738 case MXC_ESDHC_CLK:
739 return get_usdhc_clk(0);
740 case MXC_ESDHC2_CLK:
741 return get_usdhc_clk(1);
742 case MXC_ESDHC3_CLK:
743 return get_usdhc_clk(2);
744 case MXC_ESDHC4_CLK:
745 return get_usdhc_clk(3);
746 case MXC_SATA_CLK:
747 return get_ahb_clk();
748 default:
749 break;
750 }
751
752 return -1;
753}
754
755/*
756 * Dump some core clockes.
757 */
758int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
759{
760 u32 freq;
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000761 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000762 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000763 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000764 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000765 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000766 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000767 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000768 printf("PLL_NET %8d MHz\n", freq / 1000000);
769
770 printf("\n");
771 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
772 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamcc446722012-11-16 01:30:10 +0000773#ifdef CONFIG_MXC_SPI
Jason Liu23608e22011-11-25 00:18:02 +0000774 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamcc446722012-11-16 01:30:10 +0000775#endif
Jason Liu23608e22011-11-25 00:18:02 +0000776 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
777 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
778 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
779 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
780 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
781 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
782 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
783 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
784 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
785
786 return 0;
787}
788
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300789#ifndef CONFIG_MX6SX
Pardeep Kumar Singla5ea7f0e2013-07-25 12:12:13 -0500790void enable_ipu_clock(void)
791{
792 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
793 int reg;
794 reg = readl(&mxc_ccm->CCGR3);
Pierre Auberta0a0dac2013-09-23 13:37:20 +0200795 reg |= MXC_CCM_CCGR3_IPU1_IPU_MASK;
Pardeep Kumar Singla5ea7f0e2013-07-25 12:12:13 -0500796 writel(reg, &mxc_ccm->CCGR3);
797}
Fabio Estevamd95b6ab2014-06-24 17:41:00 -0300798#endif
Jason Liu23608e22011-11-25 00:18:02 +0000799/***************************************************/
800
801U_BOOT_CMD(
802 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
803 "display clocks",
804 ""
805);