blob: 299562884add94b3f2e228827ba8b6d7374ba538 [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>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090010#include <linux/errno.h>
Jason Liudec11122011-11-25 00:18:02 +000011#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 */
Peng Fanbd0c46f2016-01-06 11:06:31 +080021 PLL_AUDIO, /* AUDIO PLL */
22 PLL_VIDEO, /* AUDIO PLL */
Jason Liudec11122011-11-25 00:18:02 +000023};
24
Fabio Estevam6479f512012-04-29 08:11:13 +000025struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liudec11122011-11-25 00:18:02 +000026
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000027#ifdef CONFIG_MXC_OCOTP
28void enable_ocotp_clk(unsigned char enable)
29{
30 u32 reg;
31
32 reg = __raw_readl(&imx_ccm->CCGR2);
33 if (enable)
34 reg |= MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
35 else
36 reg &= ~MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
37 __raw_writel(reg, &imx_ccm->CCGR2);
38}
39#endif
40
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030041#ifdef CONFIG_NAND_MXS
42void setup_gpmi_io_clk(u32 cfg)
43{
44 /* Disable clocks per ERR007177 from MX6 errata */
45 clrbits_le32(&imx_ccm->CCGR4,
46 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
47 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
48 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
49 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
50 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
51
Ye.Li1475bff2015-01-12 16:46:17 +080052#if defined(CONFIG_MX6SX)
53 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK);
54
55 clrsetbits_le32(&imx_ccm->cs2cdr,
56 MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
57 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
58 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK,
59 cfg);
60
61 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK);
62#else
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030063 clrbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
64
65 clrsetbits_le32(&imx_ccm->cs2cdr,
66 MXC_CCM_CS2CDR_ENFC_CLK_PODF_MASK |
67 MXC_CCM_CS2CDR_ENFC_CLK_PRED_MASK |
68 MXC_CCM_CS2CDR_ENFC_CLK_SEL_MASK,
69 cfg);
70
71 setbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
Ye.Li1475bff2015-01-12 16:46:17 +080072#endif
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030073 setbits_le32(&imx_ccm->CCGR4,
74 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
75 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
76 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
77 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
78 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
79}
80#endif
81
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000082void enable_usboh3_clk(unsigned char enable)
83{
84 u32 reg;
85
86 reg = __raw_readl(&imx_ccm->CCGR6);
87 if (enable)
Eric Nelsone4279542012-09-21 07:33:51 +000088 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000089 else
Eric Nelsone4279542012-09-21 07:33:51 +000090 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000091 __raw_writel(reg, &imx_ccm->CCGR6);
92
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030093}
94
Stefano Babic198249b2014-09-10 13:02:40 +020095#if defined(CONFIG_FEC_MXC) && !defined(CONFIG_MX6SX)
Nikita Kiryanov98b76b42014-08-20 15:08:49 +030096void enable_enet_clk(unsigned char enable)
97{
Peng Fan40a6ed12015-07-20 19:28:27 +080098 u32 mask, *addr;
99
Peng Fance7a7122016-08-11 14:02:47 +0800100 if (is_mx6ull()) {
101 mask = MXC_CCM_CCGR0_ENET_CLK_ENABLE_MASK;
102 addr = &imx_ccm->CCGR0;
103 } else if (is_mx6ul()) {
Peng Fan40a6ed12015-07-20 19:28:27 +0800104 mask = MXC_CCM_CCGR3_ENET_MASK;
105 addr = &imx_ccm->CCGR3;
106 } else {
107 mask = MXC_CCM_CCGR1_ENET_MASK;
108 addr = &imx_ccm->CCGR1;
109 }
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300110
111 if (enable)
Peng Fan40a6ed12015-07-20 19:28:27 +0800112 setbits_le32(addr, mask);
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300113 else
Peng Fan40a6ed12015-07-20 19:28:27 +0800114 clrbits_le32(addr, mask);
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300115}
116#endif
117
118#ifdef CONFIG_MXC_UART
119void enable_uart_clk(unsigned char enable)
120{
Peng Fan40a6ed12015-07-20 19:28:27 +0800121 u32 mask;
122
Peng Fance7a7122016-08-11 14:02:47 +0800123 if (is_mx6ul() || is_mx6ull())
Peng Fan40a6ed12015-07-20 19:28:27 +0800124 mask = MXC_CCM_CCGR5_UART_MASK;
125 else
126 mask = MXC_CCM_CCGR5_UART_MASK | MXC_CCM_CCGR5_UART_SERIAL_MASK;
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300127
128 if (enable)
129 setbits_le32(&imx_ccm->CCGR5, mask);
130 else
131 clrbits_le32(&imx_ccm->CCGR5, mask);
132}
133#endif
134
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300135#ifdef CONFIG_MMC
136int enable_usdhc_clk(unsigned char enable, unsigned bus_num)
137{
138 u32 mask;
139
140 if (bus_num > 3)
141 return -EINVAL;
142
143 mask = MXC_CCM_CCGR_CG_MASK << (bus_num * 2 + 2);
144 if (enable)
145 setbits_le32(&imx_ccm->CCGR6, mask);
146 else
147 clrbits_le32(&imx_ccm->CCGR6, mask);
148
149 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000150}
Nikita Kiryanov98b76b42014-08-20 15:08:49 +0300151#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000152
trema49f40a2013-09-21 18:13:35 +0200153#ifdef CONFIG_SYS_I2C_MXC
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200154/* i2c_num can be from 0 - 3 */
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000155int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
156{
157 u32 reg;
158 u32 mask;
Peng Fand847db72015-07-01 17:01:50 +0800159 u32 *addr;
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000160
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200161 if (i2c_num > 3)
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000162 return -EINVAL;
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200163 if (i2c_num < 3) {
164 mask = MXC_CCM_CCGR_CG_MASK
165 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET
166 + (i2c_num << 1));
167 reg = __raw_readl(&imx_ccm->CCGR2);
168 if (enable)
169 reg |= mask;
170 else
171 reg &= ~mask;
172 __raw_writel(reg, &imx_ccm->CCGR2);
173 } else {
Peng Fance7a7122016-08-11 14:02:47 +0800174 if (is_mx6sx() || is_mx6ul() || is_mx6ull()) {
Peng Fand847db72015-07-01 17:01:50 +0800175 mask = MXC_CCM_CCGR6_I2C4_MASK;
176 addr = &imx_ccm->CCGR6;
177 } else {
178 mask = MXC_CCM_CCGR1_I2C4_SERIAL_MASK;
179 addr = &imx_ccm->CCGR1;
180 }
181 reg = __raw_readl(addr);
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200182 if (enable)
183 reg |= mask;
184 else
185 reg &= ~mask;
Peng Fand847db72015-07-01 17:01:50 +0800186 __raw_writel(reg, addr);
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200187 }
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000188 return 0;
189}
190#endif
191
Heiko Schocher472a68f2014-07-18 06:07:20 +0200192/* spi_num can be from 0 - SPI_MAX_NUM */
193int enable_spi_clk(unsigned char enable, unsigned spi_num)
194{
195 u32 reg;
196 u32 mask;
197
198 if (spi_num > SPI_MAX_NUM)
199 return -EINVAL;
200
201 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
202 reg = __raw_readl(&imx_ccm->CCGR1);
203 if (enable)
204 reg |= mask;
205 else
206 reg &= ~mask;
207 __raw_writel(reg, &imx_ccm->CCGR1);
208 return 0;
209}
Jason Liudec11122011-11-25 00:18:02 +0000210static u32 decode_pll(enum pll_clocks pll, u32 infreq)
211{
Peng Fanbd0c46f2016-01-06 11:06:31 +0800212 u32 div, test_div, pll_num, pll_denom;
Jason Liudec11122011-11-25 00:18:02 +0000213
214 switch (pll) {
215 case PLL_SYS:
216 div = __raw_readl(&imx_ccm->analog_pll_sys);
217 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
218
Andre Renaudb0be82e2014-06-10 08:47:13 +1200219 return (infreq * div) >> 1;
Jason Liudec11122011-11-25 00:18:02 +0000220 case PLL_BUS:
221 div = __raw_readl(&imx_ccm->analog_pll_528);
222 div &= BM_ANADIG_PLL_528_DIV_SELECT;
223
224 return infreq * (20 + (div << 1));
225 case PLL_USBOTG:
226 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
227 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
228
229 return infreq * (20 + (div << 1));
230 case PLL_ENET:
231 div = __raw_readl(&imx_ccm->analog_pll_enet);
232 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
233
Fabio Estevam93bc8ea2013-12-03 18:26:13 -0200234 return 25000000 * (div + (div >> 1) + 1);
Peng Fanbd0c46f2016-01-06 11:06:31 +0800235 case PLL_AUDIO:
236 div = __raw_readl(&imx_ccm->analog_pll_audio);
237 if (!(div & BM_ANADIG_PLL_AUDIO_ENABLE))
238 return 0;
239 /* BM_ANADIG_PLL_AUDIO_BYPASS_CLK_SRC is ignored */
240 if (div & BM_ANADIG_PLL_AUDIO_BYPASS)
241 return MXC_HCLK;
242 pll_num = __raw_readl(&imx_ccm->analog_pll_audio_num);
243 pll_denom = __raw_readl(&imx_ccm->analog_pll_audio_denom);
244 test_div = (div & BM_ANADIG_PLL_AUDIO_TEST_DIV_SELECT) >>
245 BP_ANADIG_PLL_AUDIO_TEST_DIV_SELECT;
246 div &= BM_ANADIG_PLL_AUDIO_DIV_SELECT;
247 if (test_div == 3) {
248 debug("Error test_div\n");
249 return 0;
250 }
251 test_div = 1 << (2 - test_div);
252
253 return infreq * (div + pll_num / pll_denom) / test_div;
254 case PLL_VIDEO:
255 div = __raw_readl(&imx_ccm->analog_pll_video);
256 if (!(div & BM_ANADIG_PLL_VIDEO_ENABLE))
257 return 0;
258 /* BM_ANADIG_PLL_AUDIO_BYPASS_CLK_SRC is ignored */
259 if (div & BM_ANADIG_PLL_VIDEO_BYPASS)
260 return MXC_HCLK;
261 pll_num = __raw_readl(&imx_ccm->analog_pll_video_num);
262 pll_denom = __raw_readl(&imx_ccm->analog_pll_video_denom);
263 test_div = (div & BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT) >>
264 BP_ANADIG_PLL_VIDEO_POST_DIV_SELECT;
265 div &= BM_ANADIG_PLL_VIDEO_DIV_SELECT;
266 if (test_div == 3) {
267 debug("Error test_div\n");
268 return 0;
269 }
270 test_div = 1 << (2 - test_div);
271
272 return infreq * (div + pll_num / pll_denom) / test_div;
Jason Liudec11122011-11-25 00:18:02 +0000273 default:
274 return 0;
275 }
276 /* NOTREACHED */
277}
Pierre Auberte8e62a72013-09-19 17:48:59 +0200278static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
279{
280 u32 div;
281 u64 freq;
282
283 switch (pll) {
284 case PLL_BUS:
Peng Fance7a7122016-08-11 14:02:47 +0800285 if (!is_mx6ul() && !is_mx6ull()) {
Peng Fan40a6ed12015-07-20 19:28:27 +0800286 if (pfd_num == 3) {
Peng Fan8bed5f92016-06-30 21:08:00 +0800287 /* No PFD3 on PLL2 */
Peng Fan40a6ed12015-07-20 19:28:27 +0800288 return 0;
289 }
Pierre Auberte8e62a72013-09-19 17:48:59 +0200290 }
291 div = __raw_readl(&imx_ccm->analog_pfd_528);
292 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
293 break;
294 case PLL_USBOTG:
295 div = __raw_readl(&imx_ccm->analog_pfd_480);
296 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
297 break;
298 default:
299 /* No PFD on other PLL */
300 return 0;
301 }
302
Christian Gmeinere5848142014-01-08 08:24:25 +0100303 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Auberte8e62a72013-09-19 17:48:59 +0200304 ANATOP_PFD_FRAC_SHIFT(pfd_num));
305}
Jason Liudec11122011-11-25 00:18:02 +0000306
307static u32 get_mcu_main_clk(void)
308{
309 u32 reg, freq;
310
311 reg = __raw_readl(&imx_ccm->cacrr);
312 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
313 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000314 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000315
316 return freq / (reg + 1);
317}
318
Fabio Estevam6479f512012-04-29 08:11:13 +0000319u32 get_periph_clk(void)
Jason Liudec11122011-11-25 00:18:02 +0000320{
Peng Fan40a6ed12015-07-20 19:28:27 +0800321 u32 reg, div = 0, freq = 0;
Jason Liudec11122011-11-25 00:18:02 +0000322
323 reg = __raw_readl(&imx_ccm->cbcdr);
324 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
Peng Fan40a6ed12015-07-20 19:28:27 +0800325 div = (reg & MXC_CCM_CBCDR_PERIPH_CLK2_PODF_MASK) >>
326 MXC_CCM_CBCDR_PERIPH_CLK2_PODF_OFFSET;
Jason Liudec11122011-11-25 00:18:02 +0000327 reg = __raw_readl(&imx_ccm->cbcmr);
328 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
329 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
330
331 switch (reg) {
332 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000333 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000334 break;
335 case 1:
336 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000337 freq = MXC_HCLK;
Jason Liudec11122011-11-25 00:18:02 +0000338 break;
339 default:
340 break;
341 }
342 } else {
343 reg = __raw_readl(&imx_ccm->cbcmr);
344 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
345 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
346
347 switch (reg) {
348 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000349 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000350 break;
351 case 1:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200352 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000353 break;
354 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200355 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000356 break;
357 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200358 /* static / 2 divider */
359 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liudec11122011-11-25 00:18:02 +0000360 break;
361 default:
362 break;
363 }
364 }
365
Peng Fan40a6ed12015-07-20 19:28:27 +0800366 return freq / (div + 1);
Jason Liudec11122011-11-25 00:18:02 +0000367}
368
Jason Liudec11122011-11-25 00:18:02 +0000369static u32 get_ipg_clk(void)
370{
371 u32 reg, ipg_podf;
372
373 reg = __raw_readl(&imx_ccm->cbcdr);
374 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
375 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
376
377 return get_ahb_clk() / (ipg_podf + 1);
378}
379
380static u32 get_ipg_per_clk(void)
381{
382 u32 reg, perclk_podf;
383
384 reg = __raw_readl(&imx_ccm->cscmr1);
Peng Fan6861c5a2016-05-23 18:35:54 +0800385 if (is_mx6sl() || is_mx6sx() ||
Peng Fance7a7122016-08-11 14:02:47 +0800386 is_mx6dqp() || is_mx6ul() || is_mx6ull()) {
Peng Fan53f3c9e2015-07-11 11:38:43 +0800387 if (reg & MXC_CCM_CSCMR1_PER_CLK_SEL_MASK)
388 return MXC_HCLK; /* OSC 24Mhz */
389 }
390
Jason Liudec11122011-11-25 00:18:02 +0000391 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
392
393 return get_ipg_clk() / (perclk_podf + 1);
394}
395
396static u32 get_uart_clk(void)
397{
398 u32 reg, uart_podf;
Pierre Auberte8e62a72013-09-19 17:48:59 +0200399 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liudec11122011-11-25 00:18:02 +0000400 reg = __raw_readl(&imx_ccm->cscdr1);
Peng Fan53f3c9e2015-07-11 11:38:43 +0800401
Peng Fance7a7122016-08-11 14:02:47 +0800402 if (is_mx6sl() || is_mx6sx() || is_mx6dqp() || is_mx6ul() ||
403 is_mx6ull()) {
Peng Fan53f3c9e2015-07-11 11:38:43 +0800404 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
405 freq = MXC_HCLK;
406 }
407
Jason Liudec11122011-11-25 00:18:02 +0000408 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
409 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
410
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000411 return freq / (uart_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000412}
413
414static u32 get_cspi_clk(void)
415{
416 u32 reg, cspi_podf;
417
418 reg = __raw_readl(&imx_ccm->cscdr2);
Peng Fan53f3c9e2015-07-11 11:38:43 +0800419 cspi_podf = (reg & MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK) >>
420 MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
421
Peng Fance7a7122016-08-11 14:02:47 +0800422 if (is_mx6dqp() || is_mx6sl() || is_mx6sx() || is_mx6ul() ||
423 is_mx6ull()) {
Peng Fan53f3c9e2015-07-11 11:38:43 +0800424 if (reg & MXC_CCM_CSCDR2_ECSPI_CLK_SEL_MASK)
425 return MXC_HCLK / (cspi_podf + 1);
426 }
Jason Liudec11122011-11-25 00:18:02 +0000427
Pierre Auberte8e62a72013-09-19 17:48:59 +0200428 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liudec11122011-11-25 00:18:02 +0000429}
430
431static u32 get_axi_clk(void)
432{
433 u32 root_freq, axi_podf;
434 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
435
436 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
437 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
438
439 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
440 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200441 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Fabio Estevam50f78c92016-07-18 10:19:28 -0300442 else
443 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000444 } else
445 root_freq = get_periph_clk();
446
447 return root_freq / (axi_podf + 1);
448}
449
450static u32 get_emi_slow_clk(void)
451{
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500452 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liudec11122011-11-25 00:18:02 +0000453
454 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
455 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
456 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500457 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
458 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liudec11122011-11-25 00:18:02 +0000459
460 switch (emi_clk_sel) {
461 case 0:
462 root_freq = get_axi_clk();
463 break;
464 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000465 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000466 break;
467 case 2:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200468 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000469 break;
470 case 3:
Pierre Auberte8e62a72013-09-19 17:48:59 +0200471 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000472 break;
473 }
474
Andrew Gabbasov4740e242013-07-04 06:27:32 -0500475 return root_freq / (emi_slow_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000476}
477
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000478static u32 get_mmdc_ch0_clk(void)
479{
480 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
481 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000482
Peng Fanbd0c46f2016-01-06 11:06:31 +0800483 u32 freq, podf, per2_clk2_podf, pmu_misc2_audio_div;
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000484
Peng Fance7a7122016-08-11 14:02:47 +0800485 if (is_mx6sx() || is_mx6ul() || is_mx6ull() || is_mx6sl()) {
Peng Fan40a6ed12015-07-20 19:28:27 +0800486 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) >>
487 MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
488 if (cbcdr & MXC_CCM_CBCDR_PERIPH2_CLK_SEL) {
489 per2_clk2_podf = (cbcdr & MXC_CCM_CBCDR_PERIPH2_CLK2_PODF_MASK) >>
490 MXC_CCM_CBCDR_PERIPH2_CLK2_PODF_OFFSET;
Peng Fan6861c5a2016-05-23 18:35:54 +0800491 if (is_mx6sl()) {
Peng Fan40a6ed12015-07-20 19:28:27 +0800492 if (cbcmr & MXC_CCM_CBCMR_PERIPH2_CLK2_SEL)
493 freq = MXC_HCLK;
494 else
495 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
496 } else {
497 if (cbcmr & MXC_CCM_CBCMR_PERIPH2_CLK2_SEL)
498 freq = decode_pll(PLL_BUS, MXC_HCLK);
499 else
500 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
501 }
502 } else {
503 per2_clk2_podf = 0;
504 switch ((cbcmr &
505 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
506 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
507 case 0:
508 freq = decode_pll(PLL_BUS, MXC_HCLK);
509 break;
510 case 1:
511 freq = mxc_get_pll_pfd(PLL_BUS, 2);
512 break;
513 case 2:
514 freq = mxc_get_pll_pfd(PLL_BUS, 0);
515 break;
516 case 3:
Peng Fanbd0c46f2016-01-06 11:06:31 +0800517 pmu_misc2_audio_div = PMU_MISC2_AUDIO_DIV(__raw_readl(&imx_ccm->pmu_misc2));
518 switch (pmu_misc2_audio_div) {
519 case 0:
520 case 2:
521 pmu_misc2_audio_div = 1;
522 break;
523 case 1:
524 pmu_misc2_audio_div = 2;
525 break;
526 case 3:
527 pmu_misc2_audio_div = 4;
528 break;
529 }
530 freq = decode_pll(PLL_AUDIO, MXC_HCLK) /
531 pmu_misc2_audio_div;
Peng Fan40a6ed12015-07-20 19:28:27 +0800532 break;
533 }
534 }
535 return freq / (podf + 1) / (per2_clk2_podf + 1);
536 } else {
537 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
538 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
539 return get_periph_clk() / (podf + 1);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000540 }
Otavio Salvadordc074432013-12-16 20:44:05 -0200541}
Otavio Salvadordc074432013-12-16 20:44:05 -0200542
Peng Fan53ebda82015-10-29 15:54:47 +0800543#if defined(CONFIG_VIDEO_MXS)
544static int enable_pll_video(u32 pll_div, u32 pll_num, u32 pll_denom,
545 u32 post_div)
546{
547 u32 reg = 0;
548 ulong start;
549
550 debug("pll5 div = %d, num = %d, denom = %d\n",
551 pll_div, pll_num, pll_denom);
552
553 /* Power up PLL5 video */
554 writel(BM_ANADIG_PLL_VIDEO_POWERDOWN |
555 BM_ANADIG_PLL_VIDEO_BYPASS |
556 BM_ANADIG_PLL_VIDEO_DIV_SELECT |
557 BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT,
558 &imx_ccm->analog_pll_video_clr);
559
560 /* Set div, num and denom */
561 switch (post_div) {
562 case 1:
563 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
564 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x2),
565 &imx_ccm->analog_pll_video_set);
566 break;
567 case 2:
568 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
569 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x1),
570 &imx_ccm->analog_pll_video_set);
571 break;
572 case 4:
573 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
574 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x0),
575 &imx_ccm->analog_pll_video_set);
576 break;
577 default:
578 puts("Wrong test_div!\n");
579 return -EINVAL;
580 }
581
582 writel(BF_ANADIG_PLL_VIDEO_NUM_A(pll_num),
583 &imx_ccm->analog_pll_video_num);
584 writel(BF_ANADIG_PLL_VIDEO_DENOM_B(pll_denom),
585 &imx_ccm->analog_pll_video_denom);
586
587 /* Wait PLL5 lock */
588 start = get_timer(0); /* Get current timestamp */
589
590 do {
591 reg = readl(&imx_ccm->analog_pll_video);
592 if (reg & BM_ANADIG_PLL_VIDEO_LOCK) {
593 /* Enable PLL out */
594 writel(BM_ANADIG_PLL_VIDEO_ENABLE,
595 &imx_ccm->analog_pll_video_set);
596 return 0;
597 }
598 } while (get_timer(0) < (start + 10)); /* Wait 10ms */
599
600 puts("Lock PLL5 timeout\n");
601
602 return -ETIME;
603}
604
605/*
606 * 24M--> PLL_VIDEO -> LCDIFx_PRED -> LCDIFx_PODF -> LCD
607 *
608 * 'freq' using KHz as unit, see driver/video/mxsfb.c.
609 */
610void mxs_set_lcdclk(u32 base_addr, u32 freq)
611{
612 u32 reg = 0;
613 u32 hck = MXC_HCLK / 1000;
614 /* DIV_SELECT ranges from 27 to 54 */
615 u32 min = hck * 27;
616 u32 max = hck * 54;
617 u32 temp, best = 0;
618 u32 i, j, max_pred = 8, max_postd = 8, pred = 1, postd = 1;
619 u32 pll_div, pll_num, pll_denom, post_div = 1;
620
621 debug("mxs_set_lcdclk, freq = %dKHz\n", freq);
622
Peng Fance7a7122016-08-11 14:02:47 +0800623 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull()) {
Peng Fan53ebda82015-10-29 15:54:47 +0800624 debug("This chip not support lcd!\n");
625 return;
626 }
627
628 if (base_addr == LCDIF1_BASE_ADDR) {
629 reg = readl(&imx_ccm->cscdr2);
630 /* Can't change clocks when clock not from pre-mux */
631 if ((reg & MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK) != 0)
632 return;
633 }
634
Peng Fan6861c5a2016-05-23 18:35:54 +0800635 if (is_mx6sx()) {
Peng Fan53ebda82015-10-29 15:54:47 +0800636 reg = readl(&imx_ccm->cscdr2);
637 /* Can't change clocks when clock not from pre-mux */
638 if ((reg & MXC_CCM_CSCDR2_LCDIF2_CLK_SEL_MASK) != 0)
639 return;
640 }
641
642 temp = freq * max_pred * max_postd;
Peng Fan53ebda82015-10-29 15:54:47 +0800643 if (temp < min) {
644 /*
645 * Register: PLL_VIDEO
646 * Bit Field: POST_DIV_SELECT
647 * 00 — Divide by 4.
648 * 01 — Divide by 2.
649 * 10 — Divide by 1.
650 * 11 — Reserved
651 * No need to check post_div(1)
652 */
653 for (post_div = 2; post_div <= 4; post_div <<= 1) {
654 if ((temp * post_div) > min) {
655 freq *= post_div;
656 break;
657 }
658 }
659
660 if (post_div > 4) {
661 printf("Fail to set rate to %dkhz", freq);
662 return;
663 }
664 }
665
666 /* Choose the best pred and postd to match freq for lcd */
667 for (i = 1; i <= max_pred; i++) {
668 for (j = 1; j <= max_postd; j++) {
669 temp = freq * i * j;
670 if (temp > max || temp < min)
671 continue;
672 if (best == 0 || temp < best) {
673 best = temp;
674 pred = i;
675 postd = j;
676 }
677 }
678 }
679
680 if (best == 0) {
681 printf("Fail to set rate to %dKHz", freq);
682 return;
683 }
684
685 debug("best %d, pred = %d, postd = %d\n", best, pred, postd);
686
687 pll_div = best / hck;
688 pll_denom = 1000000;
689 pll_num = (best - hck * pll_div) * pll_denom / hck;
690
691 /*
692 * pll_num
693 * (24MHz * (pll_div + --------- ))
694 * pll_denom
695 *freq KHz = --------------------------------
696 * post_div * pred * postd * 1000
697 */
698
699 if (base_addr == LCDIF1_BASE_ADDR) {
700 if (enable_pll_video(pll_div, pll_num, pll_denom, post_div))
701 return;
702
703 /* Select pre-lcd clock to PLL5 and set pre divider */
704 clrsetbits_le32(&imx_ccm->cscdr2,
705 MXC_CCM_CSCDR2_LCDIF1_PRED_SEL_MASK |
706 MXC_CCM_CSCDR2_LCDIF1_PRE_DIV_MASK,
707 (0x2 << MXC_CCM_CSCDR2_LCDIF1_PRED_SEL_OFFSET) |
708 ((pred - 1) <<
709 MXC_CCM_CSCDR2_LCDIF1_PRE_DIV_OFFSET));
710
711 /* Set the post divider */
712 clrsetbits_le32(&imx_ccm->cbcmr,
713 MXC_CCM_CBCMR_LCDIF1_PODF_MASK,
714 ((postd - 1) <<
715 MXC_CCM_CBCMR_LCDIF1_PODF_OFFSET));
Peng Fan6861c5a2016-05-23 18:35:54 +0800716 } else if (is_mx6sx()) {
Peng Fan53ebda82015-10-29 15:54:47 +0800717 /* Setting LCDIF2 for i.MX6SX */
718 if (enable_pll_video(pll_div, pll_num, pll_denom, post_div))
719 return;
720
721 /* Select pre-lcd clock to PLL5 and set pre divider */
722 clrsetbits_le32(&imx_ccm->cscdr2,
723 MXC_CCM_CSCDR2_LCDIF2_PRED_SEL_MASK |
724 MXC_CCM_CSCDR2_LCDIF2_PRE_DIV_MASK,
725 (0x2 << MXC_CCM_CSCDR2_LCDIF2_PRED_SEL_OFFSET) |
726 ((pred - 1) <<
727 MXC_CCM_CSCDR2_LCDIF2_PRE_DIV_OFFSET));
728
729 /* Set the post divider */
730 clrsetbits_le32(&imx_ccm->cscmr1,
731 MXC_CCM_CSCMR1_LCDIF2_PODF_MASK,
732 ((postd - 1) <<
733 MXC_CCM_CSCMR1_LCDIF2_PODF_OFFSET));
734 }
735}
736
737int enable_lcdif_clock(u32 base_addr)
738{
739 u32 reg = 0;
740 u32 lcdif_clk_sel_mask, lcdif_ccgr3_mask;
741
Peng Fan6861c5a2016-05-23 18:35:54 +0800742 if (is_mx6sx()) {
Ye Lie9534c32016-01-26 22:01:57 +0800743 if ((base_addr != LCDIF1_BASE_ADDR) &&
744 (base_addr != LCDIF2_BASE_ADDR)) {
Peng Fan53ebda82015-10-29 15:54:47 +0800745 puts("Wrong LCD interface!\n");
746 return -EINVAL;
747 }
748 /* Set to pre-mux clock at default */
749 lcdif_clk_sel_mask = (base_addr == LCDIF2_BASE_ADDR) ?
750 MXC_CCM_CSCDR2_LCDIF2_CLK_SEL_MASK :
751 MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK;
752 lcdif_ccgr3_mask = (base_addr == LCDIF2_BASE_ADDR) ?
753 (MXC_CCM_CCGR3_LCDIF2_PIX_MASK |
754 MXC_CCM_CCGR3_DISP_AXI_MASK) :
755 (MXC_CCM_CCGR3_LCDIF1_PIX_MASK |
756 MXC_CCM_CCGR3_DISP_AXI_MASK);
Peng Fance7a7122016-08-11 14:02:47 +0800757 } else if (is_mx6ul() || is_mx6ull()) {
Peng Fan53ebda82015-10-29 15:54:47 +0800758 if (base_addr != LCDIF1_BASE_ADDR) {
759 puts("Wrong LCD interface!\n");
760 return -EINVAL;
761 }
762 /* Set to pre-mux clock at default */
763 lcdif_clk_sel_mask = MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK;
764 lcdif_ccgr3_mask = MXC_CCM_CCGR3_LCDIF1_PIX_MASK;
765 } else {
766 return 0;
767 }
768
769 reg = readl(&imx_ccm->cscdr2);
770 reg &= ~lcdif_clk_sel_mask;
771 writel(reg, &imx_ccm->cscdr2);
772
773 /* Enable the LCDIF pix clock */
774 reg = readl(&imx_ccm->CCGR3);
775 reg |= lcdif_ccgr3_mask;
776 writel(reg, &imx_ccm->CCGR3);
777
778 reg = readl(&imx_ccm->CCGR2);
779 reg |= MXC_CCM_CCGR2_LCD_MASK;
780 writel(reg, &imx_ccm->CCGR2);
Jeroen Hofsteeb1a4ac62015-11-29 18:30:34 +0100781
782 return 0;
Peng Fan53ebda82015-10-29 15:54:47 +0800783}
784#endif
785
Peng Fan40a6ed12015-07-20 19:28:27 +0800786#ifdef CONFIG_FSL_QSPI
Peng Fan828e4682014-12-31 11:01:38 +0800787/* qspi_num can be from 0 - 1 */
788void enable_qspi_clk(int qspi_num)
789{
790 u32 reg = 0;
791 /* Enable QuadSPI clock */
792 switch (qspi_num) {
793 case 0:
794 /* disable the clock gate */
795 clrbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
796
797 /* set 50M : (50 = 396 / 2 / 4) */
798 reg = readl(&imx_ccm->cscmr1);
799 reg &= ~(MXC_CCM_CSCMR1_QSPI1_PODF_MASK |
800 MXC_CCM_CSCMR1_QSPI1_CLK_SEL_MASK);
801 reg |= ((1 << MXC_CCM_CSCMR1_QSPI1_PODF_OFFSET) |
802 (2 << MXC_CCM_CSCMR1_QSPI1_CLK_SEL_OFFSET));
803 writel(reg, &imx_ccm->cscmr1);
804
805 /* enable the clock gate */
806 setbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
807 break;
808 case 1:
809 /*
810 * disable the clock gate
811 * QSPI2 and GPMI_BCH_INPUT_GPMI_IO share the same clock gate,
812 * disable both of them.
813 */
814 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
815 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
816
817 /* set 50M : (50 = 396 / 2 / 4) */
818 reg = readl(&imx_ccm->cs2cdr);
819 reg &= ~(MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
820 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
821 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK);
822 reg |= (MXC_CCM_CS2CDR_QSPI2_CLK_PRED(0x1) |
823 MXC_CCM_CS2CDR_QSPI2_CLK_SEL(0x3));
824 writel(reg, &imx_ccm->cs2cdr);
825
826 /*enable the clock gate*/
827 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
828 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
829 break;
830 default:
831 break;
832 }
833}
834#endif
835
Otavio Salvadordc074432013-12-16 20:44:05 -0200836#ifdef CONFIG_FEC_MXC
Peng Fan967a83b2015-08-12 17:46:50 +0800837int enable_fec_anatop_clock(int fec_id, enum enet_freq freq)
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300838{
839 u32 reg = 0;
840 s32 timeout = 100000;
841
842 struct anatop_regs __iomem *anatop =
843 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
844
Stefan Roesed7e07312014-11-27 13:46:43 +0100845 if (freq < ENET_25MHZ || freq > ENET_125MHZ)
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200846 return -EINVAL;
847
Peng Fanf868f9f2015-09-06 17:15:47 +0800848 reg = readl(&anatop->pll_enet);
849
Peng Fan967a83b2015-08-12 17:46:50 +0800850 if (fec_id == 0) {
851 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
852 reg |= BF_ANADIG_PLL_ENET_DIV_SELECT(freq);
853 } else if (fec_id == 1) {
854 /* Only i.MX6SX/UL support ENET2 */
Peng Fance7a7122016-08-11 14:02:47 +0800855 if (!(is_mx6sx() || is_mx6ul() || is_mx6ull()))
Peng Fan967a83b2015-08-12 17:46:50 +0800856 return -EINVAL;
857 reg &= ~BM_ANADIG_PLL_ENET2_DIV_SELECT;
858 reg |= BF_ANADIG_PLL_ENET2_DIV_SELECT(freq);
859 } else {
860 return -EINVAL;
861 }
Fabio Estevamb2903ae2014-01-03 15:55:57 -0200862
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300863 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
864 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
865 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
866 writel(reg, &anatop->pll_enet);
867 while (timeout--) {
868 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
869 break;
870 }
871 if (timeout < 0)
872 return -ETIMEDOUT;
873 }
874
875 /* Enable FEC clock */
Peng Fan967a83b2015-08-12 17:46:50 +0800876 if (fec_id == 0)
877 reg |= BM_ANADIG_PLL_ENET_ENABLE;
878 else
879 reg |= BM_ANADIG_PLL_ENET2_ENABLE;
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300880 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
881 writel(reg, &anatop->pll_enet);
882
Fabio Estevamd4d60382014-08-15 00:24:30 -0300883#ifdef CONFIG_MX6SX
Ye.Lie91df612016-10-08 16:58:29 +0800884 /* Disable enet system clcok before switching clock parent */
885 reg = readl(&imx_ccm->CCGR3);
886 reg &= ~MXC_CCM_CCGR3_ENET_MASK;
887 writel(reg, &imx_ccm->CCGR3);
888
Fabio Estevamd4d60382014-08-15 00:24:30 -0300889 /*
890 * Set enet ahb clock to 200MHz
891 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
892 */
893 reg = readl(&imx_ccm->chsccdr);
894 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
895 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
896 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
897 /* PLL2 PFD2 */
898 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
899 /* Div = 2*/
900 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
901 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
902 writel(reg, &imx_ccm->chsccdr);
903
904 /* Enable enet system clock */
905 reg = readl(&imx_ccm->CCGR3);
906 reg |= MXC_CCM_CCGR3_ENET_MASK;
907 writel(reg, &imx_ccm->CCGR3);
908#endif
Fabio Estevam67b8b9d2013-09-13 00:36:28 -0300909 return 0;
910}
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000911#endif
Jason Liudec11122011-11-25 00:18:02 +0000912
913static u32 get_usdhc_clk(u32 port)
914{
915 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
916 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
917 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
918
919 switch (port) {
920 case 0:
921 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
922 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
923 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
924
925 break;
926 case 1:
927 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
928 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
929 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
930
931 break;
932 case 2:
933 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
934 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
935 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
936
937 break;
938 case 3:
939 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
940 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
941 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
942
943 break;
944 default:
945 break;
946 }
947
948 if (clk_sel)
Pierre Auberte8e62a72013-09-19 17:48:59 +0200949 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liudec11122011-11-25 00:18:02 +0000950 else
Pierre Auberte8e62a72013-09-19 17:48:59 +0200951 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liudec11122011-11-25 00:18:02 +0000952
953 return root_freq / (usdhc_podf + 1);
954}
955
956u32 imx_get_uartclk(void)
957{
958 return get_uart_clk();
959}
960
Jason Liu92aa90b2011-12-16 05:17:06 +0000961u32 imx_get_fecclk(void)
962{
Markus Niebel6c109b82014-02-05 10:51:25 +0100963 return mxc_get_clock(MXC_IPG_CLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000964}
965
Peng Fan40a6ed12015-07-20 19:28:27 +0800966#if defined(CONFIG_CMD_SATA) || defined(CONFIG_PCIE_IMX)
Marek Vasut563dfb22013-12-14 06:27:26 +0100967static int enable_enet_pll(uint32_t en)
Eric Nelsonfdba0762012-03-27 09:52:21 +0000968{
Eric Nelsonfdba0762012-03-27 09:52:21 +0000969 struct mxc_ccm_reg *const imx_ccm
970 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut563dfb22013-12-14 06:27:26 +0100971 s32 timeout = 100000;
972 u32 reg = 0;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000973
974 /* Enable PLLs */
975 reg = readl(&imx_ccm->analog_pll_enet);
976 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
977 writel(reg, &imx_ccm->analog_pll_enet);
978 reg |= BM_ANADIG_PLL_SYS_ENABLE;
979 while (timeout--) {
980 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
981 break;
982 }
983 if (timeout <= 0)
984 return -EIO;
985 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
986 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100987 reg |= en;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000988 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut563dfb22013-12-14 06:27:26 +0100989 return 0;
990}
Peng Fan40a6ed12015-07-20 19:28:27 +0800991#endif
Marek Vasut563dfb22013-12-14 06:27:26 +0100992
Peng Fan40a6ed12015-07-20 19:28:27 +0800993#ifdef CONFIG_CMD_SATA
Marek Vasut563dfb22013-12-14 06:27:26 +0100994static void ungate_sata_clock(void)
995{
996 struct mxc_ccm_reg *const imx_ccm =
997 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
998
999 /* Enable SATA clock. */
1000 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
1001}
1002
Marek Vasut563dfb22013-12-14 06:27:26 +01001003int enable_sata_clock(void)
1004{
1005 ungate_sata_clock();
1006 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
1007}
Nikita Kiryanov63659b72014-11-21 12:47:22 +02001008
1009void disable_sata_clock(void)
1010{
1011 struct mxc_ccm_reg *const imx_ccm =
1012 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1013
1014 clrbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
1015}
Fabio Estevam15af7332014-06-24 17:41:00 -03001016#endif
Marek Vasut563dfb22013-12-14 06:27:26 +01001017
Peng Fan40a6ed12015-07-20 19:28:27 +08001018#ifdef CONFIG_PCIE_IMX
1019static void ungate_pcie_clock(void)
1020{
1021 struct mxc_ccm_reg *const imx_ccm =
1022 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1023
1024 /* Enable PCIe clock. */
1025 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
1026}
1027
Marek Vasut563dfb22013-12-14 06:27:26 +01001028int enable_pcie_clock(void)
1029{
1030 struct anatop_regs *anatop_regs =
1031 (struct anatop_regs *)ANATOP_BASE_ADDR;
1032 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam211a4902014-08-25 14:26:45 -03001033 u32 lvds1_clk_sel;
Marek Vasut563dfb22013-12-14 06:27:26 +01001034
1035 /*
1036 * Here be dragons!
1037 *
1038 * The register ANATOP_MISC1 is not documented in the Freescale
1039 * MX6RM. The register that is mapped in the ANATOP space and
1040 * marked as ANATOP_MISC1 is actually documented in the PMU section
1041 * of the datasheet as PMU_MISC1.
1042 *
Fabio Estevam211a4902014-08-25 14:26:45 -03001043 * Switch LVDS clock source to SATA (0xb) on mx6q/dl or PCI (0xa) on
1044 * mx6sx, disable clock INPUT and enable clock OUTPUT. This is important
1045 * for PCI express link that is clocked from the i.MX6.
Marek Vasut563dfb22013-12-14 06:27:26 +01001046 */
1047#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
1048#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
1049#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
Fabio Estevam211a4902014-08-25 14:26:45 -03001050#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF 0xa
1051#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF 0xb
1052
Peng Fan6861c5a2016-05-23 18:35:54 +08001053 if (is_mx6sx())
Fabio Estevam211a4902014-08-25 14:26:45 -03001054 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF;
1055 else
1056 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF;
1057
Marek Vasut563dfb22013-12-14 06:27:26 +01001058 clrsetbits_le32(&anatop_regs->ana_misc1,
1059 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
1060 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
Fabio Estevam211a4902014-08-25 14:26:45 -03001061 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | lvds1_clk_sel);
Marek Vasut563dfb22013-12-14 06:27:26 +01001062
1063 /* PCIe reference clock sourced from AXI. */
1064 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
1065
1066 /* Party time! Ungate the clock to the PCIe. */
Peng Fan40a6ed12015-07-20 19:28:27 +08001067#ifdef CONFIG_CMD_SATA
Marek Vasut563dfb22013-12-14 06:27:26 +01001068 ungate_sata_clock();
Fabio Estevam15af7332014-06-24 17:41:00 -03001069#endif
Marek Vasut563dfb22013-12-14 06:27:26 +01001070 ungate_pcie_clock();
Eric Nelsonfdba0762012-03-27 09:52:21 +00001071
Marek Vasut563dfb22013-12-14 06:27:26 +01001072 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
1073 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelsonfdba0762012-03-27 09:52:21 +00001074}
Peng Fan40a6ed12015-07-20 19:28:27 +08001075#endif
Eric Nelsonfdba0762012-03-27 09:52:21 +00001076
Nitin Gargb1ce7012014-09-16 13:33:25 -05001077#ifdef CONFIG_SECURE_BOOT
1078void hab_caam_clock_enable(unsigned char enable)
1079{
1080 u32 reg;
1081
Peng Fance7a7122016-08-11 14:02:47 +08001082 if (is_mx6ull()) {
1083 /* CG5, DCP clock */
1084 reg = __raw_readl(&imx_ccm->CCGR0);
1085 if (enable)
1086 reg |= MXC_CCM_CCGR0_DCP_CLK_MASK;
1087 else
1088 reg &= ~MXC_CCM_CCGR0_DCP_CLK_MASK;
1089 __raw_writel(reg, &imx_ccm->CCGR0);
1090 } else {
1091 /* CG4 ~ CG6, CAAM clocks */
1092 reg = __raw_readl(&imx_ccm->CCGR0);
1093 if (enable)
1094 reg |= (MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
1095 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
1096 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
1097 else
1098 reg &= ~(MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
1099 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
1100 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
1101 __raw_writel(reg, &imx_ccm->CCGR0);
1102 }
Nitin Gargb1ce7012014-09-16 13:33:25 -05001103
1104 /* EMI slow clk */
1105 reg = __raw_readl(&imx_ccm->CCGR6);
1106 if (enable)
1107 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
1108 else
1109 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
1110 __raw_writel(reg, &imx_ccm->CCGR6);
1111}
1112#endif
1113
Nitin Garg59f3be32014-11-20 21:14:12 +08001114static void enable_pll3(void)
1115{
1116 struct anatop_regs __iomem *anatop =
1117 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
1118
1119 /* make sure pll3 is enabled */
1120 if ((readl(&anatop->usb1_pll_480_ctrl) &
1121 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0) {
1122 /* enable pll's power */
1123 writel(BM_ANADIG_USB1_PLL_480_CTRL_POWER,
1124 &anatop->usb1_pll_480_ctrl_set);
1125 writel(0x80, &anatop->ana_misc2_clr);
1126 /* wait for pll lock */
1127 while ((readl(&anatop->usb1_pll_480_ctrl) &
1128 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0)
1129 ;
1130 /* disable bypass */
1131 writel(BM_ANADIG_USB1_PLL_480_CTRL_BYPASS,
1132 &anatop->usb1_pll_480_ctrl_clr);
1133 /* enable pll output */
1134 writel(BM_ANADIG_USB1_PLL_480_CTRL_ENABLE,
1135 &anatop->usb1_pll_480_ctrl_set);
1136 }
1137}
1138
1139void enable_thermal_clk(void)
1140{
1141 enable_pll3();
1142}
1143
Jason Liudec11122011-11-25 00:18:02 +00001144unsigned int mxc_get_clock(enum mxc_clock clk)
1145{
1146 switch (clk) {
1147 case MXC_ARM_CLK:
1148 return get_mcu_main_clk();
1149 case MXC_PER_CLK:
1150 return get_periph_clk();
1151 case MXC_AHB_CLK:
1152 return get_ahb_clk();
1153 case MXC_IPG_CLK:
1154 return get_ipg_clk();
1155 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +00001156 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +00001157 return get_ipg_per_clk();
1158 case MXC_UART_CLK:
1159 return get_uart_clk();
1160 case MXC_CSPI_CLK:
1161 return get_cspi_clk();
1162 case MXC_AXI_CLK:
1163 return get_axi_clk();
1164 case MXC_EMI_SLOW_CLK:
1165 return get_emi_slow_clk();
1166 case MXC_DDR_CLK:
1167 return get_mmdc_ch0_clk();
1168 case MXC_ESDHC_CLK:
1169 return get_usdhc_clk(0);
1170 case MXC_ESDHC2_CLK:
1171 return get_usdhc_clk(1);
1172 case MXC_ESDHC3_CLK:
1173 return get_usdhc_clk(2);
1174 case MXC_ESDHC4_CLK:
1175 return get_usdhc_clk(3);
1176 case MXC_SATA_CLK:
1177 return get_ahb_clk();
1178 default:
Peng Fan3c7cb492014-11-23 11:52:20 +08001179 printf("Unsupported MXC CLK: %d\n", clk);
Jason Liudec11122011-11-25 00:18:02 +00001180 break;
1181 }
1182
Peng Fan3c7cb492014-11-23 11:52:20 +08001183 return 0;
Jason Liudec11122011-11-25 00:18:02 +00001184}
1185
1186/*
1187 * Dump some core clockes.
1188 */
1189int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1190{
1191 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +00001192 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +00001193 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +00001194 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +00001195 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +00001196 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +00001197 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +00001198 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +00001199 printf("PLL_NET %8d MHz\n", freq / 1000000);
1200
1201 printf("\n");
Peng Fan219e8c82016-03-09 16:44:38 +08001202 printf("ARM %8d kHz\n", mxc_get_clock(MXC_ARM_CLK) / 1000);
Jason Liudec11122011-11-25 00:18:02 +00001203 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
1204 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +00001205#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +00001206 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +00001207#endif
Jason Liudec11122011-11-25 00:18:02 +00001208 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
1209 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
1210 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
1211 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
1212 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
1213 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
1214 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
1215 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
1216 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
1217
1218 return 0;
1219}
1220
Fabio Estevam15af7332014-06-24 17:41:00 -03001221#ifndef CONFIG_MX6SX
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -05001222void enable_ipu_clock(void)
1223{
1224 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1225 int reg;
1226 reg = readl(&mxc_ccm->CCGR3);
Pierre Aubert2cb5c382013-09-23 13:37:20 +02001227 reg |= MXC_CCM_CCGR3_IPU1_IPU_MASK;
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -05001228 writel(reg, &mxc_ccm->CCGR3);
Peng Fan5a62fcc2015-07-11 11:38:45 +08001229
1230 if (is_mx6dqp()) {
1231 setbits_le32(&mxc_ccm->CCGR6, MXC_CCM_CCGR6_PRG_CLK0_MASK);
1232 setbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_IPU2_IPU_MASK);
1233 }
Pardeep Kumar Singlac1fa1302013-07-25 12:12:13 -05001234}
Fabio Estevam15af7332014-06-24 17:41:00 -03001235#endif
Akshay Bhat4e364e62016-04-12 18:13:56 -04001236
1237#if defined(CONFIG_MX6Q) || defined(CONFIG_MX6D) || defined(CONFIG_MX6DL) || \
1238 defined(CONFIG_MX6S)
1239static void disable_ldb_di_clock_sources(void)
1240{
1241 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1242 int reg;
1243
1244 /* Make sure PFDs are disabled at boot. */
1245 reg = readl(&mxc_ccm->analog_pfd_528);
1246 /* Cannot disable pll2_pfd2_396M, as it is the MMDC clock in iMX6DL */
Peng Fan6861c5a2016-05-23 18:35:54 +08001247 if (is_mx6sdl())
Akshay Bhat4e364e62016-04-12 18:13:56 -04001248 reg |= 0x80008080;
1249 else
1250 reg |= 0x80808080;
1251 writel(reg, &mxc_ccm->analog_pfd_528);
1252
1253 /* Disable PLL3 PFDs */
1254 reg = readl(&mxc_ccm->analog_pfd_480);
1255 reg |= 0x80808080;
1256 writel(reg, &mxc_ccm->analog_pfd_480);
1257
1258 /* Disable PLL5 */
1259 reg = readl(&mxc_ccm->analog_pll_video);
1260 reg &= ~(1 << 13);
1261 writel(reg, &mxc_ccm->analog_pll_video);
1262}
1263
1264static void enable_ldb_di_clock_sources(void)
1265{
1266 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1267 int reg;
1268
1269 reg = readl(&mxc_ccm->analog_pfd_528);
Peng Fan6861c5a2016-05-23 18:35:54 +08001270 if (is_mx6sdl())
Akshay Bhat4e364e62016-04-12 18:13:56 -04001271 reg &= ~(0x80008080);
1272 else
1273 reg &= ~(0x80808080);
1274 writel(reg, &mxc_ccm->analog_pfd_528);
1275
1276 reg = readl(&mxc_ccm->analog_pfd_480);
1277 reg &= ~(0x80808080);
1278 writel(reg, &mxc_ccm->analog_pfd_480);
1279}
1280
1281/*
1282 * Try call this function as early in the boot process as possible since the
1283 * function temporarily disables PLL2 PFD's, PLL3 PFD's and PLL5.
1284 */
1285void select_ldb_di_clock_source(enum ldb_di_clock clk)
1286{
1287 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1288 int reg;
1289
1290 /*
1291 * Need to follow a strict procedure when changing the LDB
1292 * clock, else we can introduce a glitch. Things to keep in
1293 * mind:
1294 * 1. The current and new parent clocks must be disabled.
1295 * 2. The default clock for ldb_dio_clk is mmdc_ch1 which has
1296 * no CG bit.
1297 * 3. In the RTL implementation of the LDB_DI_CLK_SEL mux
1298 * the top four options are in one mux and the PLL3 option along
1299 * with another option is in the second mux. There is third mux
1300 * used to decide between the first and second mux.
1301 * The code below switches the parent to the bottom mux first
1302 * and then manipulates the top mux. This ensures that no glitch
1303 * will enter the divider.
1304 *
1305 * Need to disable MMDC_CH1 clock manually as there is no CG bit
1306 * for this clock. The only way to disable this clock is to move
1307 * it to pll3_sw_clk and then to disable pll3_sw_clk
1308 * Make sure periph2_clk2_sel is set to pll3_sw_clk
1309 */
1310
1311 /* Disable all ldb_di clock parents */
1312 disable_ldb_di_clock_sources();
1313
1314 /* Set MMDC_CH1 mask bit */
1315 reg = readl(&mxc_ccm->ccdr);
1316 reg |= MXC_CCM_CCDR_MMDC_CH1_HS_MASK;
1317 writel(reg, &mxc_ccm->ccdr);
1318
1319 /* Set periph2_clk2_sel to be sourced from PLL3_sw_clk */
1320 reg = readl(&mxc_ccm->cbcmr);
1321 reg &= ~MXC_CCM_CBCMR_PERIPH2_CLK2_SEL;
1322 writel(reg, &mxc_ccm->cbcmr);
1323
1324 /*
1325 * Set the periph2_clk_sel to the top mux so that
1326 * mmdc_ch1 is from pll3_sw_clk.
1327 */
1328 reg = readl(&mxc_ccm->cbcdr);
1329 reg |= MXC_CCM_CBCDR_PERIPH2_CLK_SEL;
1330 writel(reg, &mxc_ccm->cbcdr);
1331
1332 /* Wait for the clock switch */
1333 while (readl(&mxc_ccm->cdhipr))
1334 ;
1335 /* Disable pll3_sw_clk by selecting bypass clock source */
1336 reg = readl(&mxc_ccm->ccsr);
1337 reg |= MXC_CCM_CCSR_PLL3_SW_CLK_SEL;
1338 writel(reg, &mxc_ccm->ccsr);
1339
1340 /* Set the ldb_di0_clk and ldb_di1_clk to 111b */
1341 reg = readl(&mxc_ccm->cs2cdr);
1342 reg |= ((7 << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1343 | (7 << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1344 writel(reg, &mxc_ccm->cs2cdr);
1345
1346 /* Set the ldb_di0_clk and ldb_di1_clk to 100b */
1347 reg = readl(&mxc_ccm->cs2cdr);
1348 reg &= ~(MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_MASK
1349 | MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_MASK);
1350 reg |= ((4 << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1351 | (4 << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1352 writel(reg, &mxc_ccm->cs2cdr);
1353
1354 /* Set the ldb_di0_clk and ldb_di1_clk to desired source */
1355 reg = readl(&mxc_ccm->cs2cdr);
1356 reg &= ~(MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_MASK
1357 | MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_MASK);
1358 reg |= ((clk << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1359 | (clk << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1360 writel(reg, &mxc_ccm->cs2cdr);
1361
1362 /* Unbypass pll3_sw_clk */
1363 reg = readl(&mxc_ccm->ccsr);
1364 reg &= ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL;
1365 writel(reg, &mxc_ccm->ccsr);
1366
1367 /*
1368 * Set the periph2_clk_sel back to the bottom mux so that
1369 * mmdc_ch1 is from its original parent.
1370 */
1371 reg = readl(&mxc_ccm->cbcdr);
1372 reg &= ~MXC_CCM_CBCDR_PERIPH2_CLK_SEL;
1373 writel(reg, &mxc_ccm->cbcdr);
1374
1375 /* Wait for the clock switch */
1376 while (readl(&mxc_ccm->cdhipr))
1377 ;
1378 /* Clear MMDC_CH1 mask bit */
1379 reg = readl(&mxc_ccm->ccdr);
1380 reg &= ~MXC_CCM_CCDR_MMDC_CH1_HS_MASK;
1381 writel(reg, &mxc_ccm->ccdr);
1382
1383 enable_ldb_di_clock_sources();
1384}
1385#endif
1386
Lukasz Majewski529498e2016-11-28 07:18:14 +01001387#ifndef CONFIG_SYS_NO_FLASH
1388void enable_eim_clk(unsigned char enable)
1389{
1390 u32 reg;
1391
1392 reg = __raw_readl(&imx_ccm->CCGR6);
1393 if (enable)
1394 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
1395 else
1396 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
1397 __raw_writel(reg, &imx_ccm->CCGR6);
1398}
1399#endif
1400
Jason Liudec11122011-11-25 00:18:02 +00001401/***************************************************/
1402
1403U_BOOT_CMD(
1404 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
1405 "display clocks",
1406 ""
1407);