blob: 113477028648ece073176b7699813420d7c6e04c [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;
129
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200130 if (i2c_num > 3)
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000131 return -EINVAL;
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200132 if (i2c_num < 3) {
133 mask = MXC_CCM_CCGR_CG_MASK
134 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET
135 + (i2c_num << 1));
136 reg = __raw_readl(&imx_ccm->CCGR2);
137 if (enable)
138 reg |= mask;
139 else
140 reg &= ~mask;
141 __raw_writel(reg, &imx_ccm->CCGR2);
142 } else {
143 mask = MXC_CCM_CCGR_CG_MASK
144 << (MXC_CCM_CCGR1_I2C4_SERIAL_OFFSET);
145 reg = __raw_readl(&imx_ccm->CCGR1);
146 if (enable)
147 reg |= mask;
148 else
149 reg &= ~mask;
150 __raw_writel(reg, &imx_ccm->CCGR1);
151 }
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000152 return 0;
153}
154#endif
155
Heiko Schocher472a68f2014-07-18 06:07:20 +0200156/* spi_num can be from 0 - SPI_MAX_NUM */
157int enable_spi_clk(unsigned char enable, unsigned spi_num)
158{
159 u32 reg;
160 u32 mask;
161
162 if (spi_num > SPI_MAX_NUM)
163 return -EINVAL;
164
165 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
166 reg = __raw_readl(&imx_ccm->CCGR1);
167 if (enable)
168 reg |= mask;
169 else
170 reg &= ~mask;
171 __raw_writel(reg, &imx_ccm->CCGR1);
172 return 0;
173}
Jason Liudec11122011-11-25 00:18:02 +0000174static u32 decode_pll(enum pll_clocks pll, u32 infreq)
175{
176 u32 div;
177
178 switch (pll) {
179 case PLL_SYS:
180 div = __raw_readl(&imx_ccm->analog_pll_sys);
181 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
182
Andre Renaudb0be82e2014-06-10 08:47:13 +1200183 return (infreq * div) >> 1;
Jason Liudec11122011-11-25 00:18:02 +0000184 case PLL_BUS:
185 div = __raw_readl(&imx_ccm->analog_pll_528);
186 div &= BM_ANADIG_PLL_528_DIV_SELECT;
187
188 return infreq * (20 + (div << 1));
189 case PLL_USBOTG:
190 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
191 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
192
193 return infreq * (20 + (div << 1));
194 case PLL_ENET:
195 div = __raw_readl(&imx_ccm->analog_pll_enet);
196 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
197
Fabio Estevam93bc8ea2013-12-03 18:26:13 -0200198 return 25000000 * (div + (div >> 1) + 1);
Jason Liudec11122011-11-25 00:18:02 +0000199 default:
200 return 0;
201 }
202 /* NOTREACHED */
203}
Pierre Auberte8e62a72013-09-19 17:48:59 +0200204static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
205{
206 u32 div;
207 u64 freq;
208
209 switch (pll) {
210 case PLL_BUS:
211 if (pfd_num == 3) {
212 /* No PFD3 on PPL2 */
213 return 0;
214 }
215 div = __raw_readl(&imx_ccm->analog_pfd_528);
216 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
217 break;
218 case PLL_USBOTG:
219 div = __raw_readl(&imx_ccm->analog_pfd_480);
220 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
221 break;
222 default:
223 /* No PFD on other PLL */
224 return 0;
225 }
226
Christian Gmeinere5848142014-01-08 08:24:25 +0100227 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Auberte8e62a72013-09-19 17:48:59 +0200228 ANATOP_PFD_FRAC_SHIFT(pfd_num));
229}
Jason Liudec11122011-11-25 00:18:02 +0000230
231static u32 get_mcu_main_clk(void)
232{
233 u32 reg, freq;
234
235 reg = __raw_readl(&imx_ccm->cacrr);
236 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
237 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000238 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000239
240 return freq / (reg + 1);
241}
242
Fabio Estevam6479f512012-04-29 08:11:13 +0000243u32 get_periph_clk(void)
Jason Liudec11122011-11-25 00:18:02 +0000244{
245 u32 reg, freq = 0;
246
247 reg = __raw_readl(&imx_ccm->cbcdr);
248 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
249 reg = __raw_readl(&imx_ccm->cbcmr);
250 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
251 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
252
253 switch (reg) {
254 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000255 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000256 break;
257 case 1:
258 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000259 freq = MXC_HCLK;
Jason Liudec11122011-11-25 00:18:02 +0000260 break;
261 default:
262 break;
263 }
264 } else {
265 reg = __raw_readl(&imx_ccm->cbcmr);
266 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
267 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
268
269 switch (reg) {
270 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000271 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000272 break;
273 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200274 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000275 break;
276 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200277 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000278 break;
279 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200280 /* static / 2 divider */
281 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liudec11122011-11-25 00:18:02 +0000282 break;
283 default:
284 break;
285 }
286 }
287
288 return freq;
289}
290
Jason Liudec11122011-11-25 00:18:02 +0000291static u32 get_ipg_clk(void)
292{
293 u32 reg, ipg_podf;
294
295 reg = __raw_readl(&imx_ccm->cbcdr);
296 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
297 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
298
299 return get_ahb_clk() / (ipg_podf + 1);
300}
301
302static u32 get_ipg_per_clk(void)
303{
304 u32 reg, perclk_podf;
305
306 reg = __raw_readl(&imx_ccm->cscmr1);
Ye.Lia5422ca2014-10-30 18:20:57 +0800307#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
308 if (reg & MXC_CCM_CSCMR1_PER_CLK_SEL_MASK)
309 return MXC_HCLK; /* OSC 24Mhz */
310#endif
Jason Liudec11122011-11-25 00:18:02 +0000311 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
312
313 return get_ipg_clk() / (perclk_podf + 1);
314}
315
316static u32 get_uart_clk(void)
317{
318 u32 reg, uart_podf;
Pierre Auberte8e62a72013-09-19 17:48:59 +0200319 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liudec11122011-11-25 00:18:02 +0000320 reg = __raw_readl(&imx_ccm->cscdr1);
Fabio Estevam712ab882014-06-24 17:40:58 -0300321#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000322 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
323 freq = MXC_HCLK;
324#endif
Jason Liudec11122011-11-25 00:18:02 +0000325 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
326 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
327
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000328 return freq / (uart_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000329}
330
331static u32 get_cspi_clk(void)
332{
333 u32 reg, cspi_podf;
334
335 reg = __raw_readl(&imx_ccm->cscdr2);
336 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
337 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
338
Pierre Auberte8e62a72013-09-19 17:48:59 +0200339 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liudec11122011-11-25 00:18:02 +0000340}
341
342static u32 get_axi_clk(void)
343{
344 u32 root_freq, axi_podf;
345 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
346
347 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
348 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
349
350 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
351 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200352 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000353 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200354 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Jason Liudec11122011-11-25 00:18:02 +0000355 } else
356 root_freq = get_periph_clk();
357
358 return root_freq / (axi_podf + 1);
359}
360
361static u32 get_emi_slow_clk(void)
362{
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500363 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liudec11122011-11-25 00:18:02 +0000364
365 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
366 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
367 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500368 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
369 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liudec11122011-11-25 00:18:02 +0000370
371 switch (emi_clk_sel) {
372 case 0:
373 root_freq = get_axi_clk();
374 break;
375 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000376 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000377 break;
378 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200379 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000380 break;
381 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200382 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000383 break;
384 }
385
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500386 return root_freq / (emi_slow_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000387}
388
Fabio Estevam712ab882014-06-24 17:40:58 -0300389#if (defined(CONFIG_MX6SL) || defined(CONFIG_MX6SX))
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000390static u32 get_mmdc_ch0_clk(void)
391{
392 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
393 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
394 u32 freq, podf;
395
396 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) \
397 >> MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
398
399 switch ((cbcmr & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
400 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
401 case 0:
402 freq = decode_pll(PLL_BUS, MXC_HCLK);
403 break;
404 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200405 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000406 break;
407 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200408 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000409 break;
410 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200411 /* static / 2 divider */
412 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000413 }
414
415 return freq / (podf + 1);
416
417}
Otavio Salvadordc074432013-12-16 20:44:05 -0200418#else
419static u32 get_mmdc_ch0_clk(void)
420{
421 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
422 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
423 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300424
Otavio Salvadordc074432013-12-16 20:44:05 -0200425 return get_periph_clk() / (mmdc_ch0_podf + 1);
426}
427#endif
428
Peng Fan828e4682014-12-31 11:01:38 +0800429#ifdef CONFIG_MX6SX
430/* qspi_num can be from 0 - 1 */
431void enable_qspi_clk(int qspi_num)
432{
433 u32 reg = 0;
434 /* Enable QuadSPI clock */
435 switch (qspi_num) {
436 case 0:
437 /* disable the clock gate */
438 clrbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
439
440 /* set 50M : (50 = 396 / 2 / 4) */
441 reg = readl(&imx_ccm->cscmr1);
442 reg &= ~(MXC_CCM_CSCMR1_QSPI1_PODF_MASK |
443 MXC_CCM_CSCMR1_QSPI1_CLK_SEL_MASK);
444 reg |= ((1 << MXC_CCM_CSCMR1_QSPI1_PODF_OFFSET) |
445 (2 << MXC_CCM_CSCMR1_QSPI1_CLK_SEL_OFFSET));
446 writel(reg, &imx_ccm->cscmr1);
447
448 /* enable the clock gate */
449 setbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
450 break;
451 case 1:
452 /*
453 * disable the clock gate
454 * QSPI2 and GPMI_BCH_INPUT_GPMI_IO share the same clock gate,
455 * disable both of them.
456 */
457 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
458 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
459
460 /* set 50M : (50 = 396 / 2 / 4) */
461 reg = readl(&imx_ccm->cs2cdr);
462 reg &= ~(MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
463 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
464 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK);
465 reg |= (MXC_CCM_CS2CDR_QSPI2_CLK_PRED(0x1) |
466 MXC_CCM_CS2CDR_QSPI2_CLK_SEL(0x3));
467 writel(reg, &imx_ccm->cs2cdr);
468
469 /*enable the clock gate*/
470 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
471 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
472 break;
473 default:
474 break;
475 }
476}
477#endif
478
Otavio Salvadordc074432013-12-16 20:44:05 -0200479#ifdef CONFIG_FEC_MXC
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200480int enable_fec_anatop_clock(enum enet_freq freq)
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300481{
482 u32 reg = 0;
483 s32 timeout = 100000;
484
485 struct anatop_regs __iomem *anatop =
486 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
487
Stefan Roesed7e07312014-11-27 13:46:43 +0100488 if (freq < ENET_25MHZ || freq > ENET_125MHZ)
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200489 return -EINVAL;
490
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300491 reg = readl(&anatop->pll_enet);
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200492 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
493 reg |= freq;
494
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300495 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
496 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
497 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
498 writel(reg, &anatop->pll_enet);
499 while (timeout--) {
500 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
501 break;
502 }
503 if (timeout < 0)
504 return -ETIMEDOUT;
505 }
506
507 /* Enable FEC clock */
508 reg |= BM_ANADIG_PLL_ENET_ENABLE;
509 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
510 writel(reg, &anatop->pll_enet);
511
Fabio Estevamd4d60382014-08-15 00:24:30 -0300512#ifdef CONFIG_MX6SX
513 /*
514 * Set enet ahb clock to 200MHz
515 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
516 */
517 reg = readl(&imx_ccm->chsccdr);
518 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
519 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
520 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
521 /* PLL2 PFD2 */
522 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
523 /* Div = 2*/
524 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
525 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
526 writel(reg, &imx_ccm->chsccdr);
527
528 /* Enable enet system clock */
529 reg = readl(&imx_ccm->CCGR3);
530 reg |= MXC_CCM_CCGR3_ENET_MASK;
531 writel(reg, &imx_ccm->CCGR3);
532#endif
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300533 return 0;
534}
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000535#endif
Jason Liudec11122011-11-25 00:18:02 +0000536
537static u32 get_usdhc_clk(u32 port)
538{
539 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
540 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
541 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
542
543 switch (port) {
544 case 0:
545 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
546 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
547 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
548
549 break;
550 case 1:
551 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
552 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
553 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
554
555 break;
556 case 2:
557 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
558 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
559 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
560
561 break;
562 case 3:
563 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
564 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
565 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
566
567 break;
568 default:
569 break;
570 }
571
572 if (clk_sel)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200573 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000574 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200575 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000576
577 return root_freq / (usdhc_podf + 1);
578}
579
580u32 imx_get_uartclk(void)
581{
582 return get_uart_clk();
583}
584
Jason Liu92aa90b2011-12-16 05:17:06 +0000585u32 imx_get_fecclk(void)
586{
Markus Niebel6c109b82014-02-05 10:51:25 +0100587 return mxc_get_clock(MXC_IPG_CLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000588}
589
Marek Vasut563dfb22013-12-14 06:27:26 +0100590static int enable_enet_pll(uint32_t en)
Eric Nelsonfdba0762012-03-27 09:52:21 +0000591{
Eric Nelsonfdba0762012-03-27 09:52:21 +0000592 struct mxc_ccm_reg *const imx_ccm
593 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut563dfb22013-12-14 06:27:26 +0100594 s32 timeout = 100000;
595 u32 reg = 0;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000596
597 /* Enable PLLs */
598 reg = readl(&imx_ccm->analog_pll_enet);
599 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
600 writel(reg, &imx_ccm->analog_pll_enet);
601 reg |= BM_ANADIG_PLL_SYS_ENABLE;
602 while (timeout--) {
603 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
604 break;
605 }
606 if (timeout <= 0)
607 return -EIO;
608 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
609 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100610 reg |= en;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000611 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100612 return 0;
613}
614
Fabio Estevam15af7332014-06-24 17:41:00 -0300615#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100616static void ungate_sata_clock(void)
617{
618 struct mxc_ccm_reg *const imx_ccm =
619 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
620
621 /* Enable SATA clock. */
622 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
623}
Fabio Estevam15af7332014-06-24 17:41:00 -0300624#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100625
626static void ungate_pcie_clock(void)
627{
628 struct mxc_ccm_reg *const imx_ccm =
629 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
630
631 /* Enable PCIe clock. */
632 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
633}
634
Fabio Estevam15af7332014-06-24 17:41:00 -0300635#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100636int enable_sata_clock(void)
637{
638 ungate_sata_clock();
639 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
640}
Nikita Kiryanov63659b72014-11-21 12:47:22 +0200641
642void disable_sata_clock(void)
643{
644 struct mxc_ccm_reg *const imx_ccm =
645 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
646
647 clrbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
648}
Fabio Estevam15af7332014-06-24 17:41:00 -0300649#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100650
651int enable_pcie_clock(void)
652{
653 struct anatop_regs *anatop_regs =
654 (struct anatop_regs *)ANATOP_BASE_ADDR;
655 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam211a4902014-08-25 14:26:45 -0300656 u32 lvds1_clk_sel;
Marek Vasut563dfb22013-12-14 06:27:26 +0100657
658 /*
659 * Here be dragons!
660 *
661 * The register ANATOP_MISC1 is not documented in the Freescale
662 * MX6RM. The register that is mapped in the ANATOP space and
663 * marked as ANATOP_MISC1 is actually documented in the PMU section
664 * of the datasheet as PMU_MISC1.
665 *
Fabio Estevam211a4902014-08-25 14:26:45 -0300666 * Switch LVDS clock source to SATA (0xb) on mx6q/dl or PCI (0xa) on
667 * mx6sx, disable clock INPUT and enable clock OUTPUT. This is important
668 * for PCI express link that is clocked from the i.MX6.
Marek Vasut563dfb22013-12-14 06:27:26 +0100669 */
670#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
671#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
672#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
Fabio Estevam211a4902014-08-25 14:26:45 -0300673#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF 0xa
674#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF 0xb
675
676 if (is_cpu_type(MXC_CPU_MX6SX))
677 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF;
678 else
679 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF;
680
Marek Vasut563dfb22013-12-14 06:27:26 +0100681 clrsetbits_le32(&anatop_regs->ana_misc1,
682 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
683 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
Fabio Estevam211a4902014-08-25 14:26:45 -0300684 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | lvds1_clk_sel);
Marek Vasut563dfb22013-12-14 06:27:26 +0100685
686 /* PCIe reference clock sourced from AXI. */
687 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
688
689 /* Party time! Ungate the clock to the PCIe. */
Fabio Estevam15af7332014-06-24 17:41:00 -0300690#ifndef CONFIG_MX6SX
Marek Vasut563dfb22013-12-14 06:27:26 +0100691 ungate_sata_clock();
Fabio Estevam15af7332014-06-24 17:41:00 -0300692#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100693 ungate_pcie_clock();
Eric Nelsonfdba0762012-03-27 09:52:21 +0000694
Marek Vasut563dfb22013-12-14 06:27:26 +0100695 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
696 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelsonfdba0762012-03-27 09:52:21 +0000697}
698
Nitin Gargb1ce7012014-09-16 13:33:25 -0500699#ifdef CONFIG_SECURE_BOOT
700void hab_caam_clock_enable(unsigned char enable)
701{
702 u32 reg;
703
704 /* CG4 ~ CG6, CAAM clocks */
705 reg = __raw_readl(&imx_ccm->CCGR0);
706 if (enable)
707 reg |= (MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
708 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
709 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
710 else
711 reg &= ~(MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
712 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
713 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
714 __raw_writel(reg, &imx_ccm->CCGR0);
715
716 /* EMI slow clk */
717 reg = __raw_readl(&imx_ccm->CCGR6);
718 if (enable)
719 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
720 else
721 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
722 __raw_writel(reg, &imx_ccm->CCGR6);
723}
724#endif
725
Nitin Garg59f3be32014-11-20 21:14:12 +0800726static void enable_pll3(void)
727{
728 struct anatop_regs __iomem *anatop =
729 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
730
731 /* make sure pll3 is enabled */
732 if ((readl(&anatop->usb1_pll_480_ctrl) &
733 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0) {
734 /* enable pll's power */
735 writel(BM_ANADIG_USB1_PLL_480_CTRL_POWER,
736 &anatop->usb1_pll_480_ctrl_set);
737 writel(0x80, &anatop->ana_misc2_clr);
738 /* wait for pll lock */
739 while ((readl(&anatop->usb1_pll_480_ctrl) &
740 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0)
741 ;
742 /* disable bypass */
743 writel(BM_ANADIG_USB1_PLL_480_CTRL_BYPASS,
744 &anatop->usb1_pll_480_ctrl_clr);
745 /* enable pll output */
746 writel(BM_ANADIG_USB1_PLL_480_CTRL_ENABLE,
747 &anatop->usb1_pll_480_ctrl_set);
748 }
749}
750
751void enable_thermal_clk(void)
752{
753 enable_pll3();
754}
755
Jason Liudec11122011-11-25 00:18:02 +0000756unsigned int mxc_get_clock(enum mxc_clock clk)
757{
758 switch (clk) {
759 case MXC_ARM_CLK:
760 return get_mcu_main_clk();
761 case MXC_PER_CLK:
762 return get_periph_clk();
763 case MXC_AHB_CLK:
764 return get_ahb_clk();
765 case MXC_IPG_CLK:
766 return get_ipg_clk();
767 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000768 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +0000769 return get_ipg_per_clk();
770 case MXC_UART_CLK:
771 return get_uart_clk();
772 case MXC_CSPI_CLK:
773 return get_cspi_clk();
774 case MXC_AXI_CLK:
775 return get_axi_clk();
776 case MXC_EMI_SLOW_CLK:
777 return get_emi_slow_clk();
778 case MXC_DDR_CLK:
779 return get_mmdc_ch0_clk();
780 case MXC_ESDHC_CLK:
781 return get_usdhc_clk(0);
782 case MXC_ESDHC2_CLK:
783 return get_usdhc_clk(1);
784 case MXC_ESDHC3_CLK:
785 return get_usdhc_clk(2);
786 case MXC_ESDHC4_CLK:
787 return get_usdhc_clk(3);
788 case MXC_SATA_CLK:
789 return get_ahb_clk();
790 default:
Peng Fan3c7cb492014-11-23 11:52:20 +0800791 printf("Unsupported MXC CLK: %d\n", clk);
Jason Liudec11122011-11-25 00:18:02 +0000792 break;
793 }
794
Peng Fan3c7cb492014-11-23 11:52:20 +0800795 return 0;
Jason Liudec11122011-11-25 00:18:02 +0000796}
797
798/*
799 * Dump some core clockes.
800 */
801int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
802{
803 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000804 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000805 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000806 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000807 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000808 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000809 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000810 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000811 printf("PLL_NET %8d MHz\n", freq / 1000000);
812
813 printf("\n");
814 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
815 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000816#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +0000817 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000818#endif
Jason Liudec11122011-11-25 00:18:02 +0000819 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
820 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
821 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
822 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
823 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
824 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
825 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
826 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
827 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
828
829 return 0;
830}
831
Fabio Estevam15af7332014-06-24 17:41:00 -0300832#ifndef CONFIG_MX6SX
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500833void enable_ipu_clock(void)
834{
835 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
836 int reg;
837 reg = readl(&mxc_ccm->CCGR3);
Pierre Aubert2cb5c382013-09-23 13:37:20 +0200838 reg |= MXC_CCM_CCGR3_IPU1_IPU_MASK;
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -0500839 writel(reg, &mxc_ccm->CCGR3);
840}
Fabio Estevam15af7332014-06-24 17:41:00 -0300841#endif
Jason Liudec11122011-11-25 00:18:02 +0000842/***************************************************/
843
844U_BOOT_CMD(
845 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
846 "display clocks",
847 ""
848);