blob: b4618989d9b66108fea2e22bc8000e0bfe6ed2e6 [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
Nikita Kiryanov98b76b42014-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 Grandegger1859b702012-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 Nelsone4279542012-09-21 07:33:51 +000074 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000075 else
Eric Nelsone4279542012-09-21 07:33:51 +000076 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000077 __raw_writel(reg, &imx_ccm->CCGR6);
78
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030079}
80
Stefano Babic198249b2014-09-10 13:02:40 +020081#if defined(CONFIG_FEC_MXC) && !defined(CONFIG_MX6SX)
Nikita Kiryanov98b76b42014-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
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300105#ifdef CONFIG_MMC
106int enable_usdhc_clk(unsigned char enable, unsigned bus_num)
107{
108 u32 mask;
109
110 if (bus_num > 3)
111 return -EINVAL;
112
113 mask = MXC_CCM_CCGR_CG_MASK << (bus_num * 2 + 2);
114 if (enable)
115 setbits_le32(&imx_ccm->CCGR6, mask);
116 else
117 clrbits_le32(&imx_ccm->CCGR6, mask);
118
119 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000120}
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300121#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000122
trema49f40a2013-09-21 18:13:35 +0200123#ifdef CONFIG_SYS_I2C_MXC
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200124/* i2c_num can be from 0 - 3 */
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000125int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
126{
127 u32 reg;
128 u32 mask;
Peng Fand847db72015-07-01 17:01:50 +0800129 u32 *addr;
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000130
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200131 if (i2c_num > 3)
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000132 return -EINVAL;
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200133 if (i2c_num < 3) {
134 mask = MXC_CCM_CCGR_CG_MASK
135 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET
136 + (i2c_num << 1));
137 reg = __raw_readl(&imx_ccm->CCGR2);
138 if (enable)
139 reg |= mask;
140 else
141 reg &= ~mask;
142 __raw_writel(reg, &imx_ccm->CCGR2);
143 } else {
Peng Fand847db72015-07-01 17:01:50 +0800144 if (is_cpu_type(MXC_CPU_MX6SX)) {
145 mask = MXC_CCM_CCGR6_I2C4_MASK;
146 addr = &imx_ccm->CCGR6;
147 } else {
148 mask = MXC_CCM_CCGR1_I2C4_SERIAL_MASK;
149 addr = &imx_ccm->CCGR1;
150 }
151 reg = __raw_readl(addr);
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200152 if (enable)
153 reg |= mask;
154 else
155 reg &= ~mask;
Peng Fand847db72015-07-01 17:01:50 +0800156 __raw_writel(reg, addr);
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200157 }
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000158 return 0;
159}
160#endif
161
Heiko Schocher472a68f2014-07-18 06:07:20 +0200162/* spi_num can be from 0 - SPI_MAX_NUM */
163int enable_spi_clk(unsigned char enable, unsigned spi_num)
164{
165 u32 reg;
166 u32 mask;
167
168 if (spi_num > SPI_MAX_NUM)
169 return -EINVAL;
170
171 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
172 reg = __raw_readl(&imx_ccm->CCGR1);
173 if (enable)
174 reg |= mask;
175 else
176 reg &= ~mask;
177 __raw_writel(reg, &imx_ccm->CCGR1);
178 return 0;
179}
Jason Liudec11122011-11-25 00:18:02 +0000180static u32 decode_pll(enum pll_clocks pll, u32 infreq)
181{
182 u32 div;
183
184 switch (pll) {
185 case PLL_SYS:
186 div = __raw_readl(&imx_ccm->analog_pll_sys);
187 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
188
Andre Renaudb0be82e2014-06-10 08:47:13 +1200189 return (infreq * div) >> 1;
Jason Liudec11122011-11-25 00:18:02 +0000190 case PLL_BUS:
191 div = __raw_readl(&imx_ccm->analog_pll_528);
192 div &= BM_ANADIG_PLL_528_DIV_SELECT;
193
194 return infreq * (20 + (div << 1));
195 case PLL_USBOTG:
196 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
197 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
198
199 return infreq * (20 + (div << 1));
200 case PLL_ENET:
201 div = __raw_readl(&imx_ccm->analog_pll_enet);
202 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
203
Fabio Estevam93bc8ea2013-12-03 18:26:13 -0200204 return 25000000 * (div + (div >> 1) + 1);
Jason Liudec11122011-11-25 00:18:02 +0000205 default:
206 return 0;
207 }
208 /* NOTREACHED */
209}
Pierre Auberte8e62a72013-09-19 17:48:59 +0200210static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
211{
212 u32 div;
213 u64 freq;
214
215 switch (pll) {
216 case PLL_BUS:
217 if (pfd_num == 3) {
218 /* No PFD3 on PPL2 */
219 return 0;
220 }
221 div = __raw_readl(&imx_ccm->analog_pfd_528);
222 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
223 break;
224 case PLL_USBOTG:
225 div = __raw_readl(&imx_ccm->analog_pfd_480);
226 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
227 break;
228 default:
229 /* No PFD on other PLL */
230 return 0;
231 }
232
Christian Gmeinere5848142014-01-08 08:24:25 +0100233 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Auberte8e62a72013-09-19 17:48:59 +0200234 ANATOP_PFD_FRAC_SHIFT(pfd_num));
235}
Jason Liudec11122011-11-25 00:18:02 +0000236
237static u32 get_mcu_main_clk(void)
238{
239 u32 reg, freq;
240
241 reg = __raw_readl(&imx_ccm->cacrr);
242 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
243 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000244 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000245
246 return freq / (reg + 1);
247}
248
Fabio Estevam6479f512012-04-29 08:11:13 +0000249u32 get_periph_clk(void)
Jason Liudec11122011-11-25 00:18:02 +0000250{
251 u32 reg, freq = 0;
252
253 reg = __raw_readl(&imx_ccm->cbcdr);
254 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
255 reg = __raw_readl(&imx_ccm->cbcmr);
256 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
257 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
258
259 switch (reg) {
260 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000261 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000262 break;
263 case 1:
264 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000265 freq = MXC_HCLK;
Jason Liudec11122011-11-25 00:18:02 +0000266 break;
267 default:
268 break;
269 }
270 } else {
271 reg = __raw_readl(&imx_ccm->cbcmr);
272 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
273 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
274
275 switch (reg) {
276 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000277 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000278 break;
279 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200280 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000281 break;
282 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200283 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000284 break;
285 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200286 /* static / 2 divider */
287 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liudec11122011-11-25 00:18:02 +0000288 break;
289 default:
290 break;
291 }
292 }
293
294 return freq;
295}
296
Jason Liudec11122011-11-25 00:18:02 +0000297static u32 get_ipg_clk(void)
298{
299 u32 reg, ipg_podf;
300
301 reg = __raw_readl(&imx_ccm->cbcdr);
302 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
303 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
304
305 return get_ahb_clk() / (ipg_podf + 1);
306}
307
308static u32 get_ipg_per_clk(void)
309{
310 u32 reg, perclk_podf;
311
312 reg = __raw_readl(&imx_ccm->cscmr1);
Ye.Lia5422ca2014-10-30 18:20:57 +0800313#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
314 if (reg & MXC_CCM_CSCMR1_PER_CLK_SEL_MASK)
315 return MXC_HCLK; /* OSC 24Mhz */
316#endif
Jason Liudec11122011-11-25 00:18:02 +0000317 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
318
319 return get_ipg_clk() / (perclk_podf + 1);
320}
321
322static u32 get_uart_clk(void)
323{
324 u32 reg, uart_podf;
Pierre Auberte8e62a72013-09-19 17:48:59 +0200325 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liudec11122011-11-25 00:18:02 +0000326 reg = __raw_readl(&imx_ccm->cscdr1);
Fabio Estevam712ab882014-06-24 17:40:58 -0300327#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000328 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
329 freq = MXC_HCLK;
330#endif
Jason Liudec11122011-11-25 00:18:02 +0000331 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
332 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
333
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000334 return freq / (uart_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000335}
336
337static u32 get_cspi_clk(void)
338{
339 u32 reg, cspi_podf;
340
341 reg = __raw_readl(&imx_ccm->cscdr2);
342 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
343 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
344
Pierre Auberte8e62a72013-09-19 17:48:59 +0200345 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liudec11122011-11-25 00:18:02 +0000346}
347
348static u32 get_axi_clk(void)
349{
350 u32 root_freq, axi_podf;
351 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
352
353 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
354 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
355
356 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
357 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200358 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000359 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200360 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Jason Liudec11122011-11-25 00:18:02 +0000361 } else
362 root_freq = get_periph_clk();
363
364 return root_freq / (axi_podf + 1);
365}
366
367static u32 get_emi_slow_clk(void)
368{
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500369 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liudec11122011-11-25 00:18:02 +0000370
371 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
372 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
373 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500374 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
375 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liudec11122011-11-25 00:18:02 +0000376
377 switch (emi_clk_sel) {
378 case 0:
379 root_freq = get_axi_clk();
380 break;
381 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000382 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000383 break;
384 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200385 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000386 break;
387 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200388 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000389 break;
390 }
391
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500392 return root_freq / (emi_slow_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000393}
394
Fabio Estevam712ab882014-06-24 17:40:58 -0300395#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000396static u32 get_mmdc_ch0_clk(void)
397{
398 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
399 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
400 u32 freq, podf;
401
402 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) \
403 >> MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
404
405 switch ((cbcmr & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
406 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
407 case 0:
408 freq = decode_pll(PLL_BUS, MXC_HCLK);
409 break;
410 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200411 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000412 break;
413 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200414 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000415 break;
416 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200417 /* static / 2 divider */
418 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000419 }
420
421 return freq / (podf + 1);
422
423}
Otavio Salvadordc074432013-12-16 20:44:05 -0200424#else
425static u32 get_mmdc_ch0_clk(void)
426{
427 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
428 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
429 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300430
Otavio Salvadordc074432013-12-16 20:44:05 -0200431 return get_periph_clk() / (mmdc_ch0_podf + 1);
432}
433#endif
434
Peng Fan828e4682014-12-31 11:01:38 +0800435#ifdef CONFIG_MX6SX
436/* qspi_num can be from 0 - 1 */
437void enable_qspi_clk(int qspi_num)
438{
439 u32 reg = 0;
440 /* Enable QuadSPI clock */
441 switch (qspi_num) {
442 case 0:
443 /* disable the clock gate */
444 clrbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
445
446 /* set 50M : (50 = 396 / 2 / 4) */
447 reg = readl(&imx_ccm->cscmr1);
448 reg &= ~(MXC_CCM_CSCMR1_QSPI1_PODF_MASK |
449 MXC_CCM_CSCMR1_QSPI1_CLK_SEL_MASK);
450 reg |= ((1 << MXC_CCM_CSCMR1_QSPI1_PODF_OFFSET) |
451 (2 << MXC_CCM_CSCMR1_QSPI1_CLK_SEL_OFFSET));
452 writel(reg, &imx_ccm->cscmr1);
453
454 /* enable the clock gate */
455 setbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
456 break;
457 case 1:
458 /*
459 * disable the clock gate
460 * QSPI2 and GPMI_BCH_INPUT_GPMI_IO share the same clock gate,
461 * disable both of them.
462 */
463 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
464 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
465
466 /* set 50M : (50 = 396 / 2 / 4) */
467 reg = readl(&imx_ccm->cs2cdr);
468 reg &= ~(MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
469 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
470 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK);
471 reg |= (MXC_CCM_CS2CDR_QSPI2_CLK_PRED(0x1) |
472 MXC_CCM_CS2CDR_QSPI2_CLK_SEL(0x3));
473 writel(reg, &imx_ccm->cs2cdr);
474
475 /*enable the clock gate*/
476 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
477 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
478 break;
479 default:
480 break;
481 }
482}
483#endif
484
Otavio Salvadordc074432013-12-16 20:44:05 -0200485#ifdef CONFIG_FEC_MXC
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200486int enable_fec_anatop_clock(enum enet_freq freq)
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300487{
488 u32 reg = 0;
489 s32 timeout = 100000;
490
491 struct anatop_regs __iomem *anatop =
492 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
493
Stefan Roesed7e07312014-11-27 13:46:43 +0100494 if (freq < ENET_25MHZ || freq > ENET_125MHZ)
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200495 return -EINVAL;
496
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300497 reg = readl(&anatop->pll_enet);
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200498 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
499 reg |= freq;
500
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300501 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
502 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
503 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
504 writel(reg, &anatop->pll_enet);
505 while (timeout--) {
506 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
507 break;
508 }
509 if (timeout < 0)
510 return -ETIMEDOUT;
511 }
512
513 /* Enable FEC clock */
514 reg |= BM_ANADIG_PLL_ENET_ENABLE;
515 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
516 writel(reg, &anatop->pll_enet);
517
Fabio Estevamd4d60382014-08-15 00:24:30 -0300518#ifdef CONFIG_MX6SX
519 /*
520 * Set enet ahb clock to 200MHz
521 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
522 */
523 reg = readl(&imx_ccm->chsccdr);
524 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
525 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
526 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
527 /* PLL2 PFD2 */
528 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
529 /* Div = 2*/
530 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
531 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
532 writel(reg, &imx_ccm->chsccdr);
533
534 /* Enable enet system clock */
535 reg = readl(&imx_ccm->CCGR3);
536 reg |= MXC_CCM_CCGR3_ENET_MASK;
537 writel(reg, &imx_ccm->CCGR3);
538#endif
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300539 return 0;
540}
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000541#endif
Jason Liudec11122011-11-25 00:18:02 +0000542
543static u32 get_usdhc_clk(u32 port)
544{
545 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
546 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
547 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
548
549 switch (port) {
550 case 0:
551 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
552 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
553 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
554
555 break;
556 case 1:
557 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
558 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
559 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
560
561 break;
562 case 2:
563 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
564 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
565 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
566
567 break;
568 case 3:
569 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
570 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
571 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
572
573 break;
574 default:
575 break;
576 }
577
578 if (clk_sel)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200579 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000580 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200581 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000582
583 return root_freq / (usdhc_podf + 1);
584}
585
586u32 imx_get_uartclk(void)
587{
588 return get_uart_clk();
589}
590
Jason Liu92aa90b2011-12-16 05:17:06 +0000591u32 imx_get_fecclk(void)
592{
Markus Niebel6c109b82014-02-05 10:51:25 +0100593 return mxc_get_clock(MXC_IPG_CLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000594}
595
Marek Vasut563dfb22013-12-14 06:27:26 +0100596static int enable_enet_pll(uint32_t en)
Eric Nelsonfdba0762012-03-27 09:52:21 +0000597{
Eric Nelsonfdba0762012-03-27 09:52:21 +0000598 struct mxc_ccm_reg *const imx_ccm
599 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut563dfb22013-12-14 06:27:26 +0100600 s32 timeout = 100000;
601 u32 reg = 0;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000602
603 /* Enable PLLs */
604 reg = readl(&imx_ccm->analog_pll_enet);
605 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
606 writel(reg, &imx_ccm->analog_pll_enet);
607 reg |= BM_ANADIG_PLL_SYS_ENABLE;
608 while (timeout--) {
609 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
610 break;
611 }
612 if (timeout <= 0)
613 return -EIO;
614 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
615 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100616 reg |= en;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000617 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100618 return 0;
619}
620
Fabio Estevam15af7332014-06-24 17:41:00 -0300621#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100622static void ungate_sata_clock(void)
623{
624 struct mxc_ccm_reg *const imx_ccm =
625 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
626
627 /* Enable SATA clock. */
628 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
629}
Fabio Estevam15af7332014-06-24 17:41:00 -0300630#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100631
632static void ungate_pcie_clock(void)
633{
634 struct mxc_ccm_reg *const imx_ccm =
635 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
636
637 /* Enable PCIe clock. */
638 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
639}
640
Fabio Estevam15af7332014-06-24 17:41:00 -0300641#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100642int enable_sata_clock(void)
643{
644 ungate_sata_clock();
645 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
646}
Nikita Kiryanov63659b72014-11-21 12:47:22 +0200647
648void disable_sata_clock(void)
649{
650 struct mxc_ccm_reg *const imx_ccm =
651 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
652
653 clrbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
654}
Fabio Estevam15af7332014-06-24 17:41:00 -0300655#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100656
657int enable_pcie_clock(void)
658{
659 struct anatop_regs *anatop_regs =
660 (struct anatop_regs *)ANATOP_BASE_ADDR;
661 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam211a4902014-08-25 14:26:45 -0300662 u32 lvds1_clk_sel;
Marek Vasut563dfb22013-12-14 06:27:26 +0100663
664 /*
665 * Here be dragons!
666 *
667 * The register ANATOP_MISC1 is not documented in the Freescale
668 * MX6RM. The register that is mapped in the ANATOP space and
669 * marked as ANATOP_MISC1 is actually documented in the PMU section
670 * of the datasheet as PMU_MISC1.
671 *
Fabio Estevam211a4902014-08-25 14:26:45 -0300672 * Switch LVDS clock source to SATA (0xb) on mx6q/dl or PCI (0xa) on
673 * mx6sx, disable clock INPUT and enable clock OUTPUT. This is important
674 * for PCI express link that is clocked from the i.MX6.
Marek Vasut563dfb22013-12-14 06:27:26 +0100675 */
676#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
677#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
678#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
Fabio Estevam211a4902014-08-25 14:26:45 -0300679#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF 0xa
680#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF 0xb
681
682 if (is_cpu_type(MXC_CPU_MX6SX))
683 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF;
684 else
685 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF;
686
Marek Vasut563dfb22013-12-14 06:27:26 +0100687 clrsetbits_le32(&anatop_regs->ana_misc1,
688 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
689 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
Fabio Estevam211a4902014-08-25 14:26:45 -0300690 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | lvds1_clk_sel);
Marek Vasut563dfb22013-12-14 06:27:26 +0100691
692 /* PCIe reference clock sourced from AXI. */
693 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
694
695 /* Party time! Ungate the clock to the PCIe. */
Fabio Estevam15af7332014-06-24 17:41:00 -0300696#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100697 ungate_sata_clock();
Fabio Estevam15af7332014-06-24 17:41:00 -0300698#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100699 ungate_pcie_clock();
Eric Nelsonfdba0762012-03-27 09:52:21 +0000700
Marek Vasut563dfb22013-12-14 06:27:26 +0100701 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
702 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelsonfdba0762012-03-27 09:52:21 +0000703}
704
Nitin Gargb1ce7012014-09-16 13:33:25 -0500705#ifdef CONFIG_SECURE_BOOT
706void hab_caam_clock_enable(unsigned char enable)
707{
708 u32 reg;
709
710 /* CG4 ~ CG6, CAAM clocks */
711 reg = __raw_readl(&imx_ccm->CCGR0);
712 if (enable)
713 reg |= (MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
714 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
715 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
716 else
717 reg &= ~(MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
718 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
719 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
720 __raw_writel(reg, &imx_ccm->CCGR0);
721
722 /* EMI slow clk */
723 reg = __raw_readl(&imx_ccm->CCGR6);
724 if (enable)
725 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
726 else
727 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
728 __raw_writel(reg, &imx_ccm->CCGR6);
729}
730#endif
731
Nitin Garg59f3be32014-11-20 21:14:12 +0800732static void enable_pll3(void)
733{
734 struct anatop_regs __iomem *anatop =
735 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
736
737 /* make sure pll3 is enabled */
738 if ((readl(&anatop->usb1_pll_480_ctrl) &
739 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0) {
740 /* enable pll's power */
741 writel(BM_ANADIG_USB1_PLL_480_CTRL_POWER,
742 &anatop->usb1_pll_480_ctrl_set);
743 writel(0x80, &anatop->ana_misc2_clr);
744 /* wait for pll lock */
745 while ((readl(&anatop->usb1_pll_480_ctrl) &
746 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0)
747 ;
748 /* disable bypass */
749 writel(BM_ANADIG_USB1_PLL_480_CTRL_BYPASS,
750 &anatop->usb1_pll_480_ctrl_clr);
751 /* enable pll output */
752 writel(BM_ANADIG_USB1_PLL_480_CTRL_ENABLE,
753 &anatop->usb1_pll_480_ctrl_set);
754 }
755}
756
757void enable_thermal_clk(void)
758{
759 enable_pll3();
760}
761
Jason Liudec11122011-11-25 00:18:02 +0000762unsigned int mxc_get_clock(enum mxc_clock clk)
763{
764 switch (clk) {
765 case MXC_ARM_CLK:
766 return get_mcu_main_clk();
767 case MXC_PER_CLK:
768 return get_periph_clk();
769 case MXC_AHB_CLK:
770 return get_ahb_clk();
771 case MXC_IPG_CLK:
772 return get_ipg_clk();
773 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000774 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +0000775 return get_ipg_per_clk();
776 case MXC_UART_CLK:
777 return get_uart_clk();
778 case MXC_CSPI_CLK:
779 return get_cspi_clk();
780 case MXC_AXI_CLK:
781 return get_axi_clk();
782 case MXC_EMI_SLOW_CLK:
783 return get_emi_slow_clk();
784 case MXC_DDR_CLK:
785 return get_mmdc_ch0_clk();
786 case MXC_ESDHC_CLK:
787 return get_usdhc_clk(0);
788 case MXC_ESDHC2_CLK:
789 return get_usdhc_clk(1);
790 case MXC_ESDHC3_CLK:
791 return get_usdhc_clk(2);
792 case MXC_ESDHC4_CLK:
793 return get_usdhc_clk(3);
794 case MXC_SATA_CLK:
795 return get_ahb_clk();
796 default:
Peng Fan3c7cb492014-11-23 11:52:20 +0800797 printf("Unsupported MXC CLK: %d\n", clk);
Jason Liudec11122011-11-25 00:18:02 +0000798 break;
799 }
800
Peng Fan3c7cb492014-11-23 11:52:20 +0800801 return 0;
Jason Liudec11122011-11-25 00:18:02 +0000802}
803
804/*
805 * Dump some core clockes.
806 */
807int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
808{
809 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000810 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000811 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000812 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000813 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000814 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000815 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000816 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000817 printf("PLL_NET %8d MHz\n", freq / 1000000);
818
819 printf("\n");
820 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
821 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000822#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +0000823 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000824#endif
Jason Liudec11122011-11-25 00:18:02 +0000825 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
826 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
827 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
828 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
829 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
830 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
831 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
832 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
833 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
834
835 return 0;
836}
837
Fabio Estevam15af7332014-06-24 17:41:00 -0300838#ifndef CONFIG_MX6SX
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500839void enable_ipu_clock(void)
840{
841 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
842 int reg;
843 reg = readl(&mxc_ccm->CCGR3);
Pierre Aubert2cb5c382013-09-23 13:37:20 +0200844 reg |= MXC_CCM_CCGR3_IPU1_IPU_MASK;
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500845 writel(reg, &mxc_ccm->CCGR3);
846}
Fabio Estevam15af7332014-06-24 17:41:00 -0300847#endif
Jason Liudec11122011-11-25 00:18:02 +0000848/***************************************************/
849
850U_BOOT_CMD(
851 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
852 "display clocks",
853 ""
854);