blob: 6a9e673ca2781be3019c1a1fba2a3bda0b9f38f0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Liu23608e22011-11-25 00:18:02 +00002/*
3 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
Jason Liu23608e22011-11-25 00:18:02 +00004 */
5
6#include <common.h>
Christian Gmeiner5a660162014-01-08 08:24:25 +01007#include <div64.h>
Jason Liu23608e22011-11-25 00:18:02 +00008#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09009#include <linux/errno.h>
Jason Liu23608e22011-11-25 00:18:02 +000010#include <asm/arch/imx-regs.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000011#include <asm/arch/crm_regs.h>
Jason Liu23608e22011-11-25 00:18:02 +000012#include <asm/arch/clock.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000013#include <asm/arch/sys_proto.h>
Jason Liu23608e22011-11-25 00:18:02 +000014
15enum pll_clocks {
16 PLL_SYS, /* System PLL */
17 PLL_BUS, /* System Bus PLL*/
18 PLL_USBOTG, /* OTG USB PLL */
19 PLL_ENET, /* ENET PLL */
Peng Fan9ba18ff2016-01-06 11:06:31 +080020 PLL_AUDIO, /* AUDIO PLL */
Anatolij Gustschinff3a5fc2017-08-02 16:05:12 +020021 PLL_VIDEO, /* VIDEO PLL */
Jason Liu23608e22011-11-25 00:18:02 +000022};
23
Fabio Estevam6a376042012-04-29 08:11:13 +000024struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liu23608e22011-11-25 00:18:02 +000025
Benoît Thébaudeau112fd2e2013-04-23 10:17:44 +000026#ifdef CONFIG_MXC_OCOTP
27void enable_ocotp_clk(unsigned char enable)
28{
29 u32 reg;
30
31 reg = __raw_readl(&imx_ccm->CCGR2);
32 if (enable)
33 reg |= MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
34 else
35 reg &= ~MXC_CCM_CCGR2_OCOTP_CTRL_MASK;
36 __raw_writel(reg, &imx_ccm->CCGR2);
37}
38#endif
39
Nikita Kiryanov224beb82014-08-20 15:08:49 +030040#ifdef CONFIG_NAND_MXS
41void setup_gpmi_io_clk(u32 cfg)
42{
43 /* Disable clocks per ERR007177 from MX6 errata */
44 clrbits_le32(&imx_ccm->CCGR4,
45 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
46 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
47 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
48 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
49 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
50
Ye.Li90447ef2015-01-12 16:46:17 +080051#if defined(CONFIG_MX6SX)
52 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK);
53
54 clrsetbits_le32(&imx_ccm->cs2cdr,
55 MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
56 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
57 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK,
58 cfg);
59
60 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK);
61#else
Nikita Kiryanov224beb82014-08-20 15:08:49 +030062 clrbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
63
64 clrsetbits_le32(&imx_ccm->cs2cdr,
65 MXC_CCM_CS2CDR_ENFC_CLK_PODF_MASK |
66 MXC_CCM_CS2CDR_ENFC_CLK_PRED_MASK |
67 MXC_CCM_CS2CDR_ENFC_CLK_SEL_MASK,
68 cfg);
69
70 setbits_le32(&imx_ccm->CCGR2, MXC_CCM_CCGR2_IOMUX_IPT_CLK_IO_MASK);
Ye.Li90447ef2015-01-12 16:46:17 +080071#endif
Nikita Kiryanov224beb82014-08-20 15:08:49 +030072 setbits_le32(&imx_ccm->CCGR4,
73 MXC_CCM_CCGR4_RAWNAND_U_BCH_INPUT_APB_MASK |
74 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_BCH_MASK |
75 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK |
76 MXC_CCM_CCGR4_RAWNAND_U_GPMI_INPUT_APB_MASK |
77 MXC_CCM_CCGR4_PL301_MX6QPER1_BCH_MASK);
78}
79#endif
80
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000081void enable_usboh3_clk(unsigned char enable)
82{
83 u32 reg;
84
85 reg = __raw_readl(&imx_ccm->CCGR6);
86 if (enable)
Eric Nelson0bb7e312012-09-21 07:33:51 +000087 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000088 else
Eric Nelson0bb7e312012-09-21 07:33:51 +000089 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000090 __raw_writel(reg, &imx_ccm->CCGR6);
91
92}
93
Stefano Babic3d8f1792014-09-10 13:02:40 +020094#if defined(CONFIG_FEC_MXC) && !defined(CONFIG_MX6SX)
Nikita Kiryanov224beb82014-08-20 15:08:49 +030095void enable_enet_clk(unsigned char enable)
96{
Peng Fan43cb1272015-07-20 19:28:27 +080097 u32 mask, *addr;
98
Peng Fan3974b7f2016-08-11 14:02:47 +080099 if (is_mx6ull()) {
100 mask = MXC_CCM_CCGR0_ENET_CLK_ENABLE_MASK;
101 addr = &imx_ccm->CCGR0;
102 } else if (is_mx6ul()) {
Peng Fan43cb1272015-07-20 19:28:27 +0800103 mask = MXC_CCM_CCGR3_ENET_MASK;
104 addr = &imx_ccm->CCGR3;
105 } else {
106 mask = MXC_CCM_CCGR1_ENET_MASK;
107 addr = &imx_ccm->CCGR1;
108 }
Nikita Kiryanov224beb82014-08-20 15:08:49 +0300109
110 if (enable)
Peng Fan43cb1272015-07-20 19:28:27 +0800111 setbits_le32(addr, mask);
Nikita Kiryanov224beb82014-08-20 15:08:49 +0300112 else
Peng Fan43cb1272015-07-20 19:28:27 +0800113 clrbits_le32(addr, mask);
Nikita Kiryanov224beb82014-08-20 15:08:49 +0300114}
115#endif
116
117#ifdef CONFIG_MXC_UART
118void enable_uart_clk(unsigned char enable)
119{
Peng Fan43cb1272015-07-20 19:28:27 +0800120 u32 mask;
121
Peng Fan3974b7f2016-08-11 14:02:47 +0800122 if (is_mx6ul() || is_mx6ull())
Peng Fan43cb1272015-07-20 19:28:27 +0800123 mask = MXC_CCM_CCGR5_UART_MASK;
124 else
125 mask = MXC_CCM_CCGR5_UART_MASK | MXC_CCM_CCGR5_UART_SERIAL_MASK;
Nikita Kiryanov224beb82014-08-20 15:08:49 +0300126
127 if (enable)
128 setbits_le32(&imx_ccm->CCGR5, mask);
129 else
130 clrbits_le32(&imx_ccm->CCGR5, mask);
131}
132#endif
133
Nikita Kiryanov224beb82014-08-20 15:08:49 +0300134#ifdef CONFIG_MMC
135int enable_usdhc_clk(unsigned char enable, unsigned bus_num)
136{
137 u32 mask;
138
139 if (bus_num > 3)
140 return -EINVAL;
141
142 mask = MXC_CCM_CCGR_CG_MASK << (bus_num * 2 + 2);
143 if (enable)
144 setbits_le32(&imx_ccm->CCGR6, mask);
145 else
146 clrbits_le32(&imx_ccm->CCGR6, mask);
147
148 return 0;
149}
150#endif
151
tremfac96402013-09-21 18:13:35 +0200152#ifdef CONFIG_SYS_I2C_MXC
Heiko Schocher21a26942015-05-18 10:56:24 +0200153/* i2c_num can be from 0 - 3 */
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000154int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
155{
156 u32 reg;
157 u32 mask;
Peng Fan19c6ec72015-07-01 17:01:50 +0800158 u32 *addr;
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000159
Heiko Schocher21a26942015-05-18 10:56:24 +0200160 if (i2c_num > 3)
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000161 return -EINVAL;
Heiko Schocher21a26942015-05-18 10:56:24 +0200162 if (i2c_num < 3) {
163 mask = MXC_CCM_CCGR_CG_MASK
164 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET
165 + (i2c_num << 1));
166 reg = __raw_readl(&imx_ccm->CCGR2);
167 if (enable)
168 reg |= mask;
169 else
170 reg &= ~mask;
171 __raw_writel(reg, &imx_ccm->CCGR2);
172 } else {
Peng Fandfca2462016-12-11 19:24:29 +0800173 if (is_mx6sll())
174 return -EINVAL;
Peng Fan3974b7f2016-08-11 14:02:47 +0800175 if (is_mx6sx() || is_mx6ul() || is_mx6ull()) {
Peng Fan19c6ec72015-07-01 17:01:50 +0800176 mask = MXC_CCM_CCGR6_I2C4_MASK;
177 addr = &imx_ccm->CCGR6;
178 } else {
179 mask = MXC_CCM_CCGR1_I2C4_SERIAL_MASK;
180 addr = &imx_ccm->CCGR1;
181 }
182 reg = __raw_readl(addr);
Heiko Schocher21a26942015-05-18 10:56:24 +0200183 if (enable)
184 reg |= mask;
185 else
186 reg &= ~mask;
Peng Fan19c6ec72015-07-01 17:01:50 +0800187 __raw_writel(reg, addr);
Heiko Schocher21a26942015-05-18 10:56:24 +0200188 }
Troy Kiskycc54a0f2012-07-19 08:18:25 +0000189 return 0;
190}
191#endif
192
Heiko Schochera0ae0092014-07-18 06:07:20 +0200193/* spi_num can be from 0 - SPI_MAX_NUM */
194int enable_spi_clk(unsigned char enable, unsigned spi_num)
195{
196 u32 reg;
197 u32 mask;
198
199 if (spi_num > SPI_MAX_NUM)
200 return -EINVAL;
201
202 mask = MXC_CCM_CCGR_CG_MASK << (spi_num << 1);
203 reg = __raw_readl(&imx_ccm->CCGR1);
204 if (enable)
205 reg |= mask;
206 else
207 reg &= ~mask;
208 __raw_writel(reg, &imx_ccm->CCGR1);
209 return 0;
210}
Jason Liu23608e22011-11-25 00:18:02 +0000211static u32 decode_pll(enum pll_clocks pll, u32 infreq)
212{
Peng Fan9ba18ff2016-01-06 11:06:31 +0800213 u32 div, test_div, pll_num, pll_denom;
Jason Liu23608e22011-11-25 00:18:02 +0000214
215 switch (pll) {
216 case PLL_SYS:
217 div = __raw_readl(&imx_ccm->analog_pll_sys);
218 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
219
Andre Renaud2eb268f2014-06-10 08:47:13 +1200220 return (infreq * div) >> 1;
Jason Liu23608e22011-11-25 00:18:02 +0000221 case PLL_BUS:
222 div = __raw_readl(&imx_ccm->analog_pll_528);
223 div &= BM_ANADIG_PLL_528_DIV_SELECT;
224
225 return infreq * (20 + (div << 1));
226 case PLL_USBOTG:
227 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
228 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
229
230 return infreq * (20 + (div << 1));
231 case PLL_ENET:
232 div = __raw_readl(&imx_ccm->analog_pll_enet);
233 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
234
Fabio Estevam89cfd0f2013-12-03 18:26:13 -0200235 return 25000000 * (div + (div >> 1) + 1);
Peng Fan9ba18ff2016-01-06 11:06:31 +0800236 case PLL_AUDIO:
237 div = __raw_readl(&imx_ccm->analog_pll_audio);
238 if (!(div & BM_ANADIG_PLL_AUDIO_ENABLE))
239 return 0;
240 /* BM_ANADIG_PLL_AUDIO_BYPASS_CLK_SRC is ignored */
241 if (div & BM_ANADIG_PLL_AUDIO_BYPASS)
242 return MXC_HCLK;
243 pll_num = __raw_readl(&imx_ccm->analog_pll_audio_num);
244 pll_denom = __raw_readl(&imx_ccm->analog_pll_audio_denom);
245 test_div = (div & BM_ANADIG_PLL_AUDIO_TEST_DIV_SELECT) >>
246 BP_ANADIG_PLL_AUDIO_TEST_DIV_SELECT;
247 div &= BM_ANADIG_PLL_AUDIO_DIV_SELECT;
248 if (test_div == 3) {
249 debug("Error test_div\n");
250 return 0;
251 }
252 test_div = 1 << (2 - test_div);
253
254 return infreq * (div + pll_num / pll_denom) / test_div;
255 case PLL_VIDEO:
256 div = __raw_readl(&imx_ccm->analog_pll_video);
257 if (!(div & BM_ANADIG_PLL_VIDEO_ENABLE))
258 return 0;
259 /* BM_ANADIG_PLL_AUDIO_BYPASS_CLK_SRC is ignored */
260 if (div & BM_ANADIG_PLL_VIDEO_BYPASS)
261 return MXC_HCLK;
262 pll_num = __raw_readl(&imx_ccm->analog_pll_video_num);
263 pll_denom = __raw_readl(&imx_ccm->analog_pll_video_denom);
264 test_div = (div & BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT) >>
265 BP_ANADIG_PLL_VIDEO_POST_DIV_SELECT;
266 div &= BM_ANADIG_PLL_VIDEO_DIV_SELECT;
267 if (test_div == 3) {
268 debug("Error test_div\n");
269 return 0;
270 }
271 test_div = 1 << (2 - test_div);
272
273 return infreq * (div + pll_num / pll_denom) / test_div;
Jason Liu23608e22011-11-25 00:18:02 +0000274 default:
275 return 0;
276 }
277 /* NOTREACHED */
278}
Pierre Aubert762a88c2013-09-19 17:48:59 +0200279static u32 mxc_get_pll_pfd(enum pll_clocks pll, int pfd_num)
280{
281 u32 div;
282 u64 freq;
283
284 switch (pll) {
285 case PLL_BUS:
Peng Fan3974b7f2016-08-11 14:02:47 +0800286 if (!is_mx6ul() && !is_mx6ull()) {
Peng Fan43cb1272015-07-20 19:28:27 +0800287 if (pfd_num == 3) {
Peng Fan1f175622016-06-30 21:08:00 +0800288 /* No PFD3 on PLL2 */
Peng Fan43cb1272015-07-20 19:28:27 +0800289 return 0;
290 }
Pierre Aubert762a88c2013-09-19 17:48:59 +0200291 }
292 div = __raw_readl(&imx_ccm->analog_pfd_528);
293 freq = (u64)decode_pll(PLL_BUS, MXC_HCLK);
294 break;
295 case PLL_USBOTG:
296 div = __raw_readl(&imx_ccm->analog_pfd_480);
297 freq = (u64)decode_pll(PLL_USBOTG, MXC_HCLK);
298 break;
299 default:
300 /* No PFD on other PLL */
301 return 0;
302 }
303
Christian Gmeiner5a660162014-01-08 08:24:25 +0100304 return lldiv(freq * 18, (div & ANATOP_PFD_FRAC_MASK(pfd_num)) >>
Pierre Aubert762a88c2013-09-19 17:48:59 +0200305 ANATOP_PFD_FRAC_SHIFT(pfd_num));
306}
Jason Liu23608e22011-11-25 00:18:02 +0000307
308static u32 get_mcu_main_clk(void)
309{
310 u32 reg, freq;
311
312 reg = __raw_readl(&imx_ccm->cacrr);
313 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
314 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000315 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000316
317 return freq / (reg + 1);
318}
319
Fabio Estevam6a376042012-04-29 08:11:13 +0000320u32 get_periph_clk(void)
Jason Liu23608e22011-11-25 00:18:02 +0000321{
Peng Fan43cb1272015-07-20 19:28:27 +0800322 u32 reg, div = 0, freq = 0;
Jason Liu23608e22011-11-25 00:18:02 +0000323
324 reg = __raw_readl(&imx_ccm->cbcdr);
325 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
Peng Fan43cb1272015-07-20 19:28:27 +0800326 div = (reg & MXC_CCM_CBCDR_PERIPH_CLK2_PODF_MASK) >>
327 MXC_CCM_CBCDR_PERIPH_CLK2_PODF_OFFSET;
Jason Liu23608e22011-11-25 00:18:02 +0000328 reg = __raw_readl(&imx_ccm->cbcmr);
329 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
330 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
331
332 switch (reg) {
333 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000334 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000335 break;
336 case 1:
337 case 2:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000338 freq = MXC_HCLK;
Jason Liu23608e22011-11-25 00:18:02 +0000339 break;
340 default:
341 break;
342 }
343 } else {
344 reg = __raw_readl(&imx_ccm->cbcmr);
345 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
346 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
347
348 switch (reg) {
349 case 0:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000350 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000351 break;
352 case 1:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200353 freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000354 break;
355 case 2:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200356 freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +0000357 break;
358 case 3:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200359 /* static / 2 divider */
360 freq = mxc_get_pll_pfd(PLL_BUS, 2) / 2;
Jason Liu23608e22011-11-25 00:18:02 +0000361 break;
362 default:
363 break;
364 }
365 }
366
Peng Fan43cb1272015-07-20 19:28:27 +0800367 return freq / (div + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000368}
369
Jason Liu23608e22011-11-25 00:18:02 +0000370static u32 get_ipg_clk(void)
371{
372 u32 reg, ipg_podf;
373
374 reg = __raw_readl(&imx_ccm->cbcdr);
375 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
376 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
377
378 return get_ahb_clk() / (ipg_podf + 1);
379}
380
381static u32 get_ipg_per_clk(void)
382{
383 u32 reg, perclk_podf;
384
385 reg = __raw_readl(&imx_ccm->cscmr1);
Peng Fandfca2462016-12-11 19:24:29 +0800386 if (is_mx6sll() || is_mx6sl() || is_mx6sx() ||
Peng Fan3974b7f2016-08-11 14:02:47 +0800387 is_mx6dqp() || is_mx6ul() || is_mx6ull()) {
Peng Fane1c2d682015-07-11 11:38:43 +0800388 if (reg & MXC_CCM_CSCMR1_PER_CLK_SEL_MASK)
389 return MXC_HCLK; /* OSC 24Mhz */
390 }
391
Jason Liu23608e22011-11-25 00:18:02 +0000392 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
393
394 return get_ipg_clk() / (perclk_podf + 1);
395}
396
397static u32 get_uart_clk(void)
398{
399 u32 reg, uart_podf;
Pierre Aubert762a88c2013-09-19 17:48:59 +0200400 u32 freq = decode_pll(PLL_USBOTG, MXC_HCLK) / 6; /* static divider */
Jason Liu23608e22011-11-25 00:18:02 +0000401 reg = __raw_readl(&imx_ccm->cscdr1);
Peng Fane1c2d682015-07-11 11:38:43 +0800402
Peng Fan3974b7f2016-08-11 14:02:47 +0800403 if (is_mx6sl() || is_mx6sx() || is_mx6dqp() || is_mx6ul() ||
Peng Fandfca2462016-12-11 19:24:29 +0800404 is_mx6sll() || is_mx6ull()) {
Peng Fane1c2d682015-07-11 11:38:43 +0800405 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
406 freq = MXC_HCLK;
407 }
408
Jason Liu23608e22011-11-25 00:18:02 +0000409 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
410 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
411
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000412 return freq / (uart_podf + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000413}
414
415static u32 get_cspi_clk(void)
416{
417 u32 reg, cspi_podf;
418
419 reg = __raw_readl(&imx_ccm->cscdr2);
Peng Fane1c2d682015-07-11 11:38:43 +0800420 cspi_podf = (reg & MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK) >>
421 MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
422
Peng Fan3974b7f2016-08-11 14:02:47 +0800423 if (is_mx6dqp() || is_mx6sl() || is_mx6sx() || is_mx6ul() ||
Peng Fandfca2462016-12-11 19:24:29 +0800424 is_mx6sll() || is_mx6ull()) {
Peng Fane1c2d682015-07-11 11:38:43 +0800425 if (reg & MXC_CCM_CSCDR2_ECSPI_CLK_SEL_MASK)
426 return MXC_HCLK / (cspi_podf + 1);
427 }
Jason Liu23608e22011-11-25 00:18:02 +0000428
Pierre Aubert762a88c2013-09-19 17:48:59 +0200429 return decode_pll(PLL_USBOTG, MXC_HCLK) / (8 * (cspi_podf + 1));
Jason Liu23608e22011-11-25 00:18:02 +0000430}
431
432static u32 get_axi_clk(void)
433{
434 u32 root_freq, axi_podf;
435 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
436
437 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
438 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
439
440 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
441 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
Pierre Aubert762a88c2013-09-19 17:48:59 +0200442 root_freq = mxc_get_pll_pfd(PLL_USBOTG, 1);
Fabio Estevam8f2e2f12016-07-18 10:19:28 -0300443 else
444 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000445 } else
446 root_freq = get_periph_clk();
447
448 return root_freq / (axi_podf + 1);
449}
450
451static u32 get_emi_slow_clk(void)
452{
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500453 u32 emi_clk_sel, emi_slow_podf, cscmr1, root_freq = 0;
Jason Liu23608e22011-11-25 00:18:02 +0000454
455 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
456 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
457 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500458 emi_slow_podf = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
459 emi_slow_podf >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_OFFSET;
Jason Liu23608e22011-11-25 00:18:02 +0000460
461 switch (emi_clk_sel) {
462 case 0:
463 root_freq = get_axi_clk();
464 break;
465 case 1:
Benoît Thébaudeau833b6432012-09-27 10:19:58 +0000466 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +0000467 break;
468 case 2:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200469 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +0000470 break;
471 case 3:
Pierre Aubert762a88c2013-09-19 17:48:59 +0200472 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +0000473 break;
474 }
475
Andrew Gabbasovd55e0da2013-07-04 06:27:32 -0500476 return root_freq / (emi_slow_podf + 1);
Jason Liu23608e22011-11-25 00:18:02 +0000477}
478
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000479static u32 get_mmdc_ch0_clk(void)
480{
481 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
482 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000483
Peng Fan9ba18ff2016-01-06 11:06:31 +0800484 u32 freq, podf, per2_clk2_podf, pmu_misc2_audio_div;
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000485
Peng Fandfca2462016-12-11 19:24:29 +0800486 if (is_mx6sx() || is_mx6ul() || is_mx6ull() || is_mx6sl() ||
487 is_mx6sll()) {
Peng Fan43cb1272015-07-20 19:28:27 +0800488 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) >>
489 MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
490 if (cbcdr & MXC_CCM_CBCDR_PERIPH2_CLK_SEL) {
491 per2_clk2_podf = (cbcdr & MXC_CCM_CBCDR_PERIPH2_CLK2_PODF_MASK) >>
492 MXC_CCM_CBCDR_PERIPH2_CLK2_PODF_OFFSET;
Peng Fanb949fd22016-05-23 18:35:54 +0800493 if (is_mx6sl()) {
Peng Fan43cb1272015-07-20 19:28:27 +0800494 if (cbcmr & MXC_CCM_CBCMR_PERIPH2_CLK2_SEL)
495 freq = MXC_HCLK;
496 else
497 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
498 } else {
499 if (cbcmr & MXC_CCM_CBCMR_PERIPH2_CLK2_SEL)
500 freq = decode_pll(PLL_BUS, MXC_HCLK);
501 else
502 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
503 }
504 } else {
505 per2_clk2_podf = 0;
506 switch ((cbcmr &
507 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
508 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
509 case 0:
510 freq = decode_pll(PLL_BUS, MXC_HCLK);
511 break;
512 case 1:
513 freq = mxc_get_pll_pfd(PLL_BUS, 2);
514 break;
515 case 2:
516 freq = mxc_get_pll_pfd(PLL_BUS, 0);
517 break;
518 case 3:
Peng Fan0e819822016-12-11 19:24:25 +0800519 if (is_mx6sl()) {
520 freq = mxc_get_pll_pfd(PLL_BUS, 2) >> 1;
521 break;
522 }
523
Peng Fan9ba18ff2016-01-06 11:06:31 +0800524 pmu_misc2_audio_div = PMU_MISC2_AUDIO_DIV(__raw_readl(&imx_ccm->pmu_misc2));
525 switch (pmu_misc2_audio_div) {
526 case 0:
527 case 2:
528 pmu_misc2_audio_div = 1;
529 break;
530 case 1:
531 pmu_misc2_audio_div = 2;
532 break;
533 case 3:
534 pmu_misc2_audio_div = 4;
535 break;
536 }
537 freq = decode_pll(PLL_AUDIO, MXC_HCLK) /
538 pmu_misc2_audio_div;
Peng Fan43cb1272015-07-20 19:28:27 +0800539 break;
540 }
541 }
542 return freq / (podf + 1) / (per2_clk2_podf + 1);
543 } else {
544 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
545 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
546 return get_periph_clk() / (podf + 1);
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000547 }
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000548}
Fabio Estevam31f07962013-09-13 00:36:28 -0300549
Peng Fanad153782015-10-29 15:54:47 +0800550#if defined(CONFIG_VIDEO_MXS)
551static int enable_pll_video(u32 pll_div, u32 pll_num, u32 pll_denom,
552 u32 post_div)
553{
554 u32 reg = 0;
555 ulong start;
556
557 debug("pll5 div = %d, num = %d, denom = %d\n",
558 pll_div, pll_num, pll_denom);
559
560 /* Power up PLL5 video */
561 writel(BM_ANADIG_PLL_VIDEO_POWERDOWN |
562 BM_ANADIG_PLL_VIDEO_BYPASS |
563 BM_ANADIG_PLL_VIDEO_DIV_SELECT |
564 BM_ANADIG_PLL_VIDEO_POST_DIV_SELECT,
565 &imx_ccm->analog_pll_video_clr);
566
567 /* Set div, num and denom */
568 switch (post_div) {
569 case 1:
570 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
571 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x2),
572 &imx_ccm->analog_pll_video_set);
573 break;
574 case 2:
575 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
576 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x1),
577 &imx_ccm->analog_pll_video_set);
578 break;
579 case 4:
580 writel(BF_ANADIG_PLL_VIDEO_DIV_SELECT(pll_div) |
581 BF_ANADIG_PLL_VIDEO_POST_DIV_SELECT(0x0),
582 &imx_ccm->analog_pll_video_set);
583 break;
584 default:
585 puts("Wrong test_div!\n");
586 return -EINVAL;
587 }
588
589 writel(BF_ANADIG_PLL_VIDEO_NUM_A(pll_num),
590 &imx_ccm->analog_pll_video_num);
591 writel(BF_ANADIG_PLL_VIDEO_DENOM_B(pll_denom),
592 &imx_ccm->analog_pll_video_denom);
593
594 /* Wait PLL5 lock */
595 start = get_timer(0); /* Get current timestamp */
596
597 do {
598 reg = readl(&imx_ccm->analog_pll_video);
599 if (reg & BM_ANADIG_PLL_VIDEO_LOCK) {
600 /* Enable PLL out */
601 writel(BM_ANADIG_PLL_VIDEO_ENABLE,
602 &imx_ccm->analog_pll_video_set);
603 return 0;
604 }
605 } while (get_timer(0) < (start + 10)); /* Wait 10ms */
606
607 puts("Lock PLL5 timeout\n");
608
609 return -ETIME;
610}
611
612/*
613 * 24M--> PLL_VIDEO -> LCDIFx_PRED -> LCDIFx_PODF -> LCD
614 *
615 * 'freq' using KHz as unit, see driver/video/mxsfb.c.
616 */
617void mxs_set_lcdclk(u32 base_addr, u32 freq)
618{
619 u32 reg = 0;
620 u32 hck = MXC_HCLK / 1000;
621 /* DIV_SELECT ranges from 27 to 54 */
622 u32 min = hck * 27;
623 u32 max = hck * 54;
624 u32 temp, best = 0;
625 u32 i, j, max_pred = 8, max_postd = 8, pred = 1, postd = 1;
626 u32 pll_div, pll_num, pll_denom, post_div = 1;
627
628 debug("mxs_set_lcdclk, freq = %dKHz\n", freq);
629
Peng Fandfca2462016-12-11 19:24:29 +0800630 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl() &&
631 !is_mx6sll()) {
Peng Fanad153782015-10-29 15:54:47 +0800632 debug("This chip not support lcd!\n");
633 return;
634 }
635
Peng Fane3326232016-12-11 19:24:27 +0800636 if (!is_mx6sl()) {
637 if (base_addr == LCDIF1_BASE_ADDR) {
638 reg = readl(&imx_ccm->cscdr2);
639 /* Can't change clocks when clock not from pre-mux */
640 if ((reg & MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK) != 0)
641 return;
642 }
Peng Fanad153782015-10-29 15:54:47 +0800643 }
644
Peng Fanb949fd22016-05-23 18:35:54 +0800645 if (is_mx6sx()) {
Peng Fanad153782015-10-29 15:54:47 +0800646 reg = readl(&imx_ccm->cscdr2);
647 /* Can't change clocks when clock not from pre-mux */
648 if ((reg & MXC_CCM_CSCDR2_LCDIF2_CLK_SEL_MASK) != 0)
649 return;
650 }
651
652 temp = freq * max_pred * max_postd;
Peng Fanad153782015-10-29 15:54:47 +0800653 if (temp < min) {
654 /*
655 * Register: PLL_VIDEO
656 * Bit Field: POST_DIV_SELECT
657 * 00 — Divide by 4.
658 * 01 — Divide by 2.
659 * 10 — Divide by 1.
660 * 11 — Reserved
661 * No need to check post_div(1)
662 */
663 for (post_div = 2; post_div <= 4; post_div <<= 1) {
664 if ((temp * post_div) > min) {
665 freq *= post_div;
666 break;
667 }
668 }
669
670 if (post_div > 4) {
671 printf("Fail to set rate to %dkhz", freq);
672 return;
673 }
674 }
675
676 /* Choose the best pred and postd to match freq for lcd */
677 for (i = 1; i <= max_pred; i++) {
678 for (j = 1; j <= max_postd; j++) {
679 temp = freq * i * j;
680 if (temp > max || temp < min)
681 continue;
682 if (best == 0 || temp < best) {
683 best = temp;
684 pred = i;
685 postd = j;
686 }
687 }
688 }
689
690 if (best == 0) {
691 printf("Fail to set rate to %dKHz", freq);
692 return;
693 }
694
695 debug("best %d, pred = %d, postd = %d\n", best, pred, postd);
696
697 pll_div = best / hck;
698 pll_denom = 1000000;
699 pll_num = (best - hck * pll_div) * pll_denom / hck;
700
701 /*
702 * pll_num
703 * (24MHz * (pll_div + --------- ))
704 * pll_denom
705 *freq KHz = --------------------------------
706 * post_div * pred * postd * 1000
707 */
708
709 if (base_addr == LCDIF1_BASE_ADDR) {
710 if (enable_pll_video(pll_div, pll_num, pll_denom, post_div))
711 return;
712
Peng Fan708f6922016-12-11 19:24:28 +0800713 enable_lcdif_clock(base_addr, 0);
Peng Fane3326232016-12-11 19:24:27 +0800714 if (!is_mx6sl()) {
715 /* Select pre-lcd clock to PLL5 and set pre divider */
716 clrsetbits_le32(&imx_ccm->cscdr2,
717 MXC_CCM_CSCDR2_LCDIF1_PRED_SEL_MASK |
718 MXC_CCM_CSCDR2_LCDIF1_PRE_DIV_MASK,
719 (0x2 << MXC_CCM_CSCDR2_LCDIF1_PRED_SEL_OFFSET) |
720 ((pred - 1) <<
721 MXC_CCM_CSCDR2_LCDIF1_PRE_DIV_OFFSET));
Peng Fanad153782015-10-29 15:54:47 +0800722
Peng Fane3326232016-12-11 19:24:27 +0800723 /* Set the post divider */
724 clrsetbits_le32(&imx_ccm->cbcmr,
725 MXC_CCM_CBCMR_LCDIF1_PODF_MASK,
726 ((postd - 1) <<
727 MXC_CCM_CBCMR_LCDIF1_PODF_OFFSET));
728 } else {
729 /* Select pre-lcd clock to PLL5 and set pre divider */
730 clrsetbits_le32(&imx_ccm->cscdr2,
731 MXC_CCM_CSCDR2_LCDIF_PIX_CLK_SEL_MASK |
732 MXC_CCM_CSCDR2_LCDIF_PIX_PRE_DIV_MASK,
733 (0x2 << MXC_CCM_CSCDR2_LCDIF_PIX_CLK_SEL_OFFSET) |
734 ((pred - 1) <<
735 MXC_CCM_CSCDR2_LCDIF_PIX_PRE_DIV_OFFSET));
736
737 /* Set the post divider */
738 clrsetbits_le32(&imx_ccm->cscmr1,
739 MXC_CCM_CSCMR1_LCDIF_PIX_PODF_MASK,
740 (((postd - 1)^0x6) <<
741 MXC_CCM_CSCMR1_LCDIF_PIX_PODF_OFFSET));
742 }
Peng Fan708f6922016-12-11 19:24:28 +0800743
744 enable_lcdif_clock(base_addr, 1);
Peng Fanb949fd22016-05-23 18:35:54 +0800745 } else if (is_mx6sx()) {
Peng Fanad153782015-10-29 15:54:47 +0800746 /* Setting LCDIF2 for i.MX6SX */
747 if (enable_pll_video(pll_div, pll_num, pll_denom, post_div))
748 return;
749
Peng Fan708f6922016-12-11 19:24:28 +0800750 enable_lcdif_clock(base_addr, 0);
Peng Fanad153782015-10-29 15:54:47 +0800751 /* Select pre-lcd clock to PLL5 and set pre divider */
752 clrsetbits_le32(&imx_ccm->cscdr2,
753 MXC_CCM_CSCDR2_LCDIF2_PRED_SEL_MASK |
754 MXC_CCM_CSCDR2_LCDIF2_PRE_DIV_MASK,
755 (0x2 << MXC_CCM_CSCDR2_LCDIF2_PRED_SEL_OFFSET) |
756 ((pred - 1) <<
757 MXC_CCM_CSCDR2_LCDIF2_PRE_DIV_OFFSET));
758
759 /* Set the post divider */
760 clrsetbits_le32(&imx_ccm->cscmr1,
761 MXC_CCM_CSCMR1_LCDIF2_PODF_MASK,
762 ((postd - 1) <<
763 MXC_CCM_CSCMR1_LCDIF2_PODF_OFFSET));
Peng Fan708f6922016-12-11 19:24:28 +0800764
765 enable_lcdif_clock(base_addr, 1);
Peng Fanad153782015-10-29 15:54:47 +0800766 }
767}
768
Peng Fan708f6922016-12-11 19:24:28 +0800769int enable_lcdif_clock(u32 base_addr, bool enable)
Peng Fanad153782015-10-29 15:54:47 +0800770{
771 u32 reg = 0;
772 u32 lcdif_clk_sel_mask, lcdif_ccgr3_mask;
773
Peng Fanb949fd22016-05-23 18:35:54 +0800774 if (is_mx6sx()) {
Ye Li9655ebd2016-01-26 22:01:57 +0800775 if ((base_addr != LCDIF1_BASE_ADDR) &&
776 (base_addr != LCDIF2_BASE_ADDR)) {
Peng Fanad153782015-10-29 15:54:47 +0800777 puts("Wrong LCD interface!\n");
778 return -EINVAL;
779 }
780 /* Set to pre-mux clock at default */
781 lcdif_clk_sel_mask = (base_addr == LCDIF2_BASE_ADDR) ?
782 MXC_CCM_CSCDR2_LCDIF2_CLK_SEL_MASK :
783 MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK;
784 lcdif_ccgr3_mask = (base_addr == LCDIF2_BASE_ADDR) ?
785 (MXC_CCM_CCGR3_LCDIF2_PIX_MASK |
786 MXC_CCM_CCGR3_DISP_AXI_MASK) :
787 (MXC_CCM_CCGR3_LCDIF1_PIX_MASK |
788 MXC_CCM_CCGR3_DISP_AXI_MASK);
Peng Fandfca2462016-12-11 19:24:29 +0800789 } else if (is_mx6ul() || is_mx6ull() || is_mx6sll()) {
Peng Fanad153782015-10-29 15:54:47 +0800790 if (base_addr != LCDIF1_BASE_ADDR) {
791 puts("Wrong LCD interface!\n");
792 return -EINVAL;
793 }
794 /* Set to pre-mux clock at default */
795 lcdif_clk_sel_mask = MXC_CCM_CSCDR2_LCDIF1_CLK_SEL_MASK;
796 lcdif_ccgr3_mask = MXC_CCM_CCGR3_LCDIF1_PIX_MASK;
Peng Fane3326232016-12-11 19:24:27 +0800797 } else if (is_mx6sl()) {
798 if (base_addr != LCDIF1_BASE_ADDR) {
799 puts("Wrong LCD interface!\n");
800 return -EINVAL;
801 }
802
803 reg = readl(&imx_ccm->CCGR3);
804 reg &= ~(MXC_CCM_CCGR3_LCDIF_AXI_MASK |
805 MXC_CCM_CCGR3_LCDIF_PIX_MASK);
806 writel(reg, &imx_ccm->CCGR3);
807
Peng Fan708f6922016-12-11 19:24:28 +0800808 if (enable) {
809 reg = readl(&imx_ccm->cscdr3);
810 reg &= ~MXC_CCM_CSCDR3_LCDIF_AXI_CLK_SEL_MASK;
811 reg |= 1 << MXC_CCM_CSCDR3_LCDIF_AXI_CLK_SEL_OFFSET;
812 writel(reg, &imx_ccm->cscdr3);
Peng Fane3326232016-12-11 19:24:27 +0800813
Peng Fan708f6922016-12-11 19:24:28 +0800814 reg = readl(&imx_ccm->CCGR3);
815 reg |= MXC_CCM_CCGR3_LCDIF_AXI_MASK |
816 MXC_CCM_CCGR3_LCDIF_PIX_MASK;
817 writel(reg, &imx_ccm->CCGR3);
818 }
Peng Fane3326232016-12-11 19:24:27 +0800819
820 return 0;
Peng Fanad153782015-10-29 15:54:47 +0800821 } else {
822 return 0;
823 }
824
Peng Fan70ac1692016-12-11 19:24:26 +0800825 /* Gate LCDIF clock first */
826 reg = readl(&imx_ccm->CCGR3);
827 reg &= ~lcdif_ccgr3_mask;
828 writel(reg, &imx_ccm->CCGR3);
829
830 reg = readl(&imx_ccm->CCGR2);
831 reg &= ~MXC_CCM_CCGR2_LCD_MASK;
832 writel(reg, &imx_ccm->CCGR2);
833
Peng Fan708f6922016-12-11 19:24:28 +0800834 if (enable) {
835 /* Select pre-mux */
836 reg = readl(&imx_ccm->cscdr2);
837 reg &= ~lcdif_clk_sel_mask;
838 writel(reg, &imx_ccm->cscdr2);
Peng Fanad153782015-10-29 15:54:47 +0800839
Peng Fan708f6922016-12-11 19:24:28 +0800840 /* Enable the LCDIF pix clock */
841 reg = readl(&imx_ccm->CCGR3);
842 reg |= lcdif_ccgr3_mask;
843 writel(reg, &imx_ccm->CCGR3);
Peng Fanad153782015-10-29 15:54:47 +0800844
Peng Fan708f6922016-12-11 19:24:28 +0800845 reg = readl(&imx_ccm->CCGR2);
846 reg |= MXC_CCM_CCGR2_LCD_MASK;
847 writel(reg, &imx_ccm->CCGR2);
848 }
Jeroen Hofstee0ff47e52015-11-29 18:30:34 +0100849
850 return 0;
Peng Fanad153782015-10-29 15:54:47 +0800851}
852#endif
853
Peng Fan43cb1272015-07-20 19:28:27 +0800854#ifdef CONFIG_FSL_QSPI
Peng Fanb93ab2e2014-12-31 11:01:38 +0800855/* qspi_num can be from 0 - 1 */
856void enable_qspi_clk(int qspi_num)
857{
858 u32 reg = 0;
859 /* Enable QuadSPI clock */
860 switch (qspi_num) {
861 case 0:
862 /* disable the clock gate */
863 clrbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
864
865 /* set 50M : (50 = 396 / 2 / 4) */
866 reg = readl(&imx_ccm->cscmr1);
867 reg &= ~(MXC_CCM_CSCMR1_QSPI1_PODF_MASK |
868 MXC_CCM_CSCMR1_QSPI1_CLK_SEL_MASK);
869 reg |= ((1 << MXC_CCM_CSCMR1_QSPI1_PODF_OFFSET) |
870 (2 << MXC_CCM_CSCMR1_QSPI1_CLK_SEL_OFFSET));
871 writel(reg, &imx_ccm->cscmr1);
872
873 /* enable the clock gate */
874 setbits_le32(&imx_ccm->CCGR3, MXC_CCM_CCGR3_QSPI1_MASK);
875 break;
876 case 1:
877 /*
878 * disable the clock gate
879 * QSPI2 and GPMI_BCH_INPUT_GPMI_IO share the same clock gate,
880 * disable both of them.
881 */
882 clrbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
883 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
884
885 /* set 50M : (50 = 396 / 2 / 4) */
886 reg = readl(&imx_ccm->cs2cdr);
887 reg &= ~(MXC_CCM_CS2CDR_QSPI2_CLK_PODF_MASK |
888 MXC_CCM_CS2CDR_QSPI2_CLK_PRED_MASK |
889 MXC_CCM_CS2CDR_QSPI2_CLK_SEL_MASK);
890 reg |= (MXC_CCM_CS2CDR_QSPI2_CLK_PRED(0x1) |
891 MXC_CCM_CS2CDR_QSPI2_CLK_SEL(0x3));
892 writel(reg, &imx_ccm->cs2cdr);
893
894 /*enable the clock gate*/
895 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_QSPI2_ENFC_MASK |
896 MXC_CCM_CCGR4_RAWNAND_U_GPMI_BCH_INPUT_GPMI_IO_MASK);
897 break;
898 default:
899 break;
900 }
901}
902#endif
903
Otavio Salvadorc655b812013-12-16 20:44:05 -0200904#ifdef CONFIG_FEC_MXC
Peng Fan6d97dc12015-08-12 17:46:50 +0800905int enable_fec_anatop_clock(int fec_id, enum enet_freq freq)
Fabio Estevam31f07962013-09-13 00:36:28 -0300906{
907 u32 reg = 0;
908 s32 timeout = 100000;
909
910 struct anatop_regs __iomem *anatop =
911 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
912
Stefan Roese77317452014-11-27 13:46:43 +0100913 if (freq < ENET_25MHZ || freq > ENET_125MHZ)
Fabio Estevam5f98d0b2014-01-03 15:55:57 -0200914 return -EINVAL;
915
Peng Fane2748b42015-09-06 17:15:47 +0800916 reg = readl(&anatop->pll_enet);
917
Peng Fan6d97dc12015-08-12 17:46:50 +0800918 if (fec_id == 0) {
919 reg &= ~BM_ANADIG_PLL_ENET_DIV_SELECT;
920 reg |= BF_ANADIG_PLL_ENET_DIV_SELECT(freq);
921 } else if (fec_id == 1) {
922 /* Only i.MX6SX/UL support ENET2 */
Peng Fan3974b7f2016-08-11 14:02:47 +0800923 if (!(is_mx6sx() || is_mx6ul() || is_mx6ull()))
Peng Fan6d97dc12015-08-12 17:46:50 +0800924 return -EINVAL;
925 reg &= ~BM_ANADIG_PLL_ENET2_DIV_SELECT;
926 reg |= BF_ANADIG_PLL_ENET2_DIV_SELECT(freq);
927 } else {
928 return -EINVAL;
929 }
Fabio Estevam5f98d0b2014-01-03 15:55:57 -0200930
Fabio Estevam31f07962013-09-13 00:36:28 -0300931 if ((reg & BM_ANADIG_PLL_ENET_POWERDOWN) ||
932 (!(reg & BM_ANADIG_PLL_ENET_LOCK))) {
933 reg &= ~BM_ANADIG_PLL_ENET_POWERDOWN;
934 writel(reg, &anatop->pll_enet);
935 while (timeout--) {
936 if (readl(&anatop->pll_enet) & BM_ANADIG_PLL_ENET_LOCK)
937 break;
938 }
939 if (timeout < 0)
940 return -ETIMEDOUT;
941 }
942
943 /* Enable FEC clock */
Peng Fan6d97dc12015-08-12 17:46:50 +0800944 if (fec_id == 0)
945 reg |= BM_ANADIG_PLL_ENET_ENABLE;
946 else
947 reg |= BM_ANADIG_PLL_ENET2_ENABLE;
Fabio Estevam31f07962013-09-13 00:36:28 -0300948 reg &= ~BM_ANADIG_PLL_ENET_BYPASS;
949 writel(reg, &anatop->pll_enet);
950
Fabio Estevam5c045cd2014-08-15 00:24:30 -0300951#ifdef CONFIG_MX6SX
Ye.Li27e3a3c2016-10-08 16:58:29 +0800952 /* Disable enet system clcok before switching clock parent */
953 reg = readl(&imx_ccm->CCGR3);
954 reg &= ~MXC_CCM_CCGR3_ENET_MASK;
955 writel(reg, &imx_ccm->CCGR3);
956
Fabio Estevam5c045cd2014-08-15 00:24:30 -0300957 /*
958 * Set enet ahb clock to 200MHz
959 * pll2_pfd2_396m-> ENET_PODF-> ENET_AHB
960 */
961 reg = readl(&imx_ccm->chsccdr);
962 reg &= ~(MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_MASK
963 | MXC_CCM_CHSCCDR_ENET_PODF_MASK
964 | MXC_CCM_CHSCCDR_ENET_CLK_SEL_MASK);
965 /* PLL2 PFD2 */
966 reg |= (4 << MXC_CCM_CHSCCDR_ENET_PRE_CLK_SEL_OFFSET);
967 /* Div = 2*/
968 reg |= (1 << MXC_CCM_CHSCCDR_ENET_PODF_OFFSET);
969 reg |= (0 << MXC_CCM_CHSCCDR_ENET_CLK_SEL_OFFSET);
970 writel(reg, &imx_ccm->chsccdr);
971
972 /* Enable enet system clock */
973 reg = readl(&imx_ccm->CCGR3);
974 reg |= MXC_CCM_CCGR3_ENET_MASK;
975 writel(reg, &imx_ccm->CCGR3);
976#endif
Fabio Estevam31f07962013-09-13 00:36:28 -0300977 return 0;
978}
Fabio Estevam25b4aa12013-04-10 09:32:57 +0000979#endif
Jason Liu23608e22011-11-25 00:18:02 +0000980
981static u32 get_usdhc_clk(u32 port)
982{
983 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
984 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
985 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
986
Peng Fandfca2462016-12-11 19:24:29 +0800987 if (is_mx6ul() || is_mx6ull()) {
988 if (port > 1)
989 return 0;
990 }
991
992 if (is_mx6sll()) {
993 if (port > 2)
994 return 0;
995 }
996
Jason Liu23608e22011-11-25 00:18:02 +0000997 switch (port) {
998 case 0:
999 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
1000 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
1001 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
1002
1003 break;
1004 case 1:
1005 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
1006 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
1007 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
1008
1009 break;
1010 case 2:
1011 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
1012 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
1013 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
1014
1015 break;
1016 case 3:
1017 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
1018 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
1019 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
1020
1021 break;
1022 default:
1023 break;
1024 }
1025
1026 if (clk_sel)
Pierre Aubert762a88c2013-09-19 17:48:59 +02001027 root_freq = mxc_get_pll_pfd(PLL_BUS, 0);
Jason Liu23608e22011-11-25 00:18:02 +00001028 else
Pierre Aubert762a88c2013-09-19 17:48:59 +02001029 root_freq = mxc_get_pll_pfd(PLL_BUS, 2);
Jason Liu23608e22011-11-25 00:18:02 +00001030
1031 return root_freq / (usdhc_podf + 1);
1032}
1033
1034u32 imx_get_uartclk(void)
1035{
1036 return get_uart_clk();
1037}
1038
Jason Liuff167df2011-12-16 05:17:06 +00001039u32 imx_get_fecclk(void)
1040{
Markus Niebeladadc912014-02-05 10:51:25 +01001041 return mxc_get_clock(MXC_IPG_CLK);
Jason Liuff167df2011-12-16 05:17:06 +00001042}
1043
Simon Glass10e40d52017-06-14 21:28:25 -06001044#if defined(CONFIG_SATA) || defined(CONFIG_PCIE_IMX)
Marek Vasut79814492013-12-14 06:27:26 +01001045static int enable_enet_pll(uint32_t en)
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001046{
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001047 struct mxc_ccm_reg *const imx_ccm
1048 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
Marek Vasut79814492013-12-14 06:27:26 +01001049 s32 timeout = 100000;
1050 u32 reg = 0;
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001051
1052 /* Enable PLLs */
1053 reg = readl(&imx_ccm->analog_pll_enet);
1054 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
1055 writel(reg, &imx_ccm->analog_pll_enet);
1056 reg |= BM_ANADIG_PLL_SYS_ENABLE;
1057 while (timeout--) {
1058 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
1059 break;
1060 }
1061 if (timeout <= 0)
1062 return -EIO;
1063 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
1064 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut79814492013-12-14 06:27:26 +01001065 reg |= en;
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001066 writel(reg, &imx_ccm->analog_pll_enet);
Marek Vasut79814492013-12-14 06:27:26 +01001067 return 0;
1068}
Peng Fan43cb1272015-07-20 19:28:27 +08001069#endif
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001070
Simon Glass10e40d52017-06-14 21:28:25 -06001071#ifdef CONFIG_SATA
Marek Vasut79814492013-12-14 06:27:26 +01001072static void ungate_sata_clock(void)
1073{
1074 struct mxc_ccm_reg *const imx_ccm =
1075 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1076
1077 /* Enable SATA clock. */
1078 setbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
1079}
1080
Marek Vasut79814492013-12-14 06:27:26 +01001081int enable_sata_clock(void)
1082{
1083 ungate_sata_clock();
1084 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA);
1085}
Nikita Kiryanov8d29cef2014-11-21 12:47:22 +02001086
1087void disable_sata_clock(void)
1088{
1089 struct mxc_ccm_reg *const imx_ccm =
1090 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1091
1092 clrbits_le32(&imx_ccm->CCGR5, MXC_CCM_CCGR5_SATA_MASK);
1093}
Fabio Estevamd95b6ab2014-06-24 17:41:00 -03001094#endif
Marek Vasut79814492013-12-14 06:27:26 +01001095
Peng Fan43cb1272015-07-20 19:28:27 +08001096#ifdef CONFIG_PCIE_IMX
1097static void ungate_pcie_clock(void)
1098{
1099 struct mxc_ccm_reg *const imx_ccm =
1100 (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1101
1102 /* Enable PCIe clock. */
1103 setbits_le32(&imx_ccm->CCGR4, MXC_CCM_CCGR4_PCIE_MASK);
1104}
1105
Marek Vasut79814492013-12-14 06:27:26 +01001106int enable_pcie_clock(void)
1107{
1108 struct anatop_regs *anatop_regs =
1109 (struct anatop_regs *)ANATOP_BASE_ADDR;
1110 struct mxc_ccm_reg *ccm_regs = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam1b8ad742014-08-25 14:26:45 -03001111 u32 lvds1_clk_sel;
Marek Vasut79814492013-12-14 06:27:26 +01001112
1113 /*
1114 * Here be dragons!
1115 *
1116 * The register ANATOP_MISC1 is not documented in the Freescale
1117 * MX6RM. The register that is mapped in the ANATOP space and
1118 * marked as ANATOP_MISC1 is actually documented in the PMU section
1119 * of the datasheet as PMU_MISC1.
1120 *
Fabio Estevam1b8ad742014-08-25 14:26:45 -03001121 * Switch LVDS clock source to SATA (0xb) on mx6q/dl or PCI (0xa) on
1122 * mx6sx, disable clock INPUT and enable clock OUTPUT. This is important
1123 * for PCI express link that is clocked from the i.MX6.
Marek Vasut79814492013-12-14 06:27:26 +01001124 */
1125#define ANADIG_ANA_MISC1_LVDSCLK1_IBEN (1 << 12)
1126#define ANADIG_ANA_MISC1_LVDSCLK1_OBEN (1 << 10)
1127#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK 0x0000001F
Fabio Estevam1b8ad742014-08-25 14:26:45 -03001128#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF 0xa
1129#define ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF 0xb
1130
Peng Fanb949fd22016-05-23 18:35:54 +08001131 if (is_mx6sx())
Fabio Estevam1b8ad742014-08-25 14:26:45 -03001132 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_PCIE_REF;
1133 else
1134 lvds1_clk_sel = ANADIG_ANA_MISC1_LVDS1_CLK_SEL_SATA_REF;
1135
Marek Vasut79814492013-12-14 06:27:26 +01001136 clrsetbits_le32(&anatop_regs->ana_misc1,
1137 ANADIG_ANA_MISC1_LVDSCLK1_IBEN |
1138 ANADIG_ANA_MISC1_LVDS1_CLK_SEL_MASK,
Fabio Estevam1b8ad742014-08-25 14:26:45 -03001139 ANADIG_ANA_MISC1_LVDSCLK1_OBEN | lvds1_clk_sel);
Marek Vasut79814492013-12-14 06:27:26 +01001140
1141 /* PCIe reference clock sourced from AXI. */
1142 clrbits_le32(&ccm_regs->cbcmr, MXC_CCM_CBCMR_PCIE_AXI_CLK_SEL);
1143
1144 /* Party time! Ungate the clock to the PCIe. */
Simon Glass10e40d52017-06-14 21:28:25 -06001145#ifdef CONFIG_SATA
Marek Vasut79814492013-12-14 06:27:26 +01001146 ungate_sata_clock();
Fabio Estevamd95b6ab2014-06-24 17:41:00 -03001147#endif
Marek Vasut79814492013-12-14 06:27:26 +01001148 ungate_pcie_clock();
1149
1150 return enable_enet_pll(BM_ANADIG_PLL_ENET_ENABLE_SATA |
1151 BM_ANADIG_PLL_ENET_ENABLE_PCIE);
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001152}
Peng Fan43cb1272015-07-20 19:28:27 +08001153#endif
Eric Nelson64e7cdb2012-03-27 09:52:21 +00001154
Stefano Babicd714a752019-09-20 08:47:53 +02001155#ifdef CONFIG_IMX_HAB
Nitin Garg36c1ca42014-09-16 13:33:25 -05001156void hab_caam_clock_enable(unsigned char enable)
1157{
1158 u32 reg;
1159
Peng Fandfca2462016-12-11 19:24:29 +08001160 if (is_mx6ull() || is_mx6sll()) {
Peng Fan3974b7f2016-08-11 14:02:47 +08001161 /* CG5, DCP clock */
1162 reg = __raw_readl(&imx_ccm->CCGR0);
1163 if (enable)
1164 reg |= MXC_CCM_CCGR0_DCP_CLK_MASK;
1165 else
1166 reg &= ~MXC_CCM_CCGR0_DCP_CLK_MASK;
1167 __raw_writel(reg, &imx_ccm->CCGR0);
1168 } else {
1169 /* CG4 ~ CG6, CAAM clocks */
1170 reg = __raw_readl(&imx_ccm->CCGR0);
1171 if (enable)
1172 reg |= (MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
1173 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
1174 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
1175 else
1176 reg &= ~(MXC_CCM_CCGR0_CAAM_WRAPPER_IPG_MASK |
1177 MXC_CCM_CCGR0_CAAM_WRAPPER_ACLK_MASK |
1178 MXC_CCM_CCGR0_CAAM_SECURE_MEM_MASK);
1179 __raw_writel(reg, &imx_ccm->CCGR0);
1180 }
Nitin Garg36c1ca42014-09-16 13:33:25 -05001181
1182 /* EMI slow clk */
1183 reg = __raw_readl(&imx_ccm->CCGR6);
1184 if (enable)
1185 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
1186 else
1187 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
1188 __raw_writel(reg, &imx_ccm->CCGR6);
1189}
1190#endif
1191
Nitin Gargcf202d22014-11-20 21:14:12 +08001192static void enable_pll3(void)
1193{
1194 struct anatop_regs __iomem *anatop =
1195 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
1196
1197 /* make sure pll3 is enabled */
1198 if ((readl(&anatop->usb1_pll_480_ctrl) &
1199 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0) {
1200 /* enable pll's power */
1201 writel(BM_ANADIG_USB1_PLL_480_CTRL_POWER,
1202 &anatop->usb1_pll_480_ctrl_set);
1203 writel(0x80, &anatop->ana_misc2_clr);
1204 /* wait for pll lock */
1205 while ((readl(&anatop->usb1_pll_480_ctrl) &
1206 BM_ANADIG_USB1_PLL_480_CTRL_LOCK) == 0)
1207 ;
1208 /* disable bypass */
1209 writel(BM_ANADIG_USB1_PLL_480_CTRL_BYPASS,
1210 &anatop->usb1_pll_480_ctrl_clr);
1211 /* enable pll output */
1212 writel(BM_ANADIG_USB1_PLL_480_CTRL_ENABLE,
1213 &anatop->usb1_pll_480_ctrl_set);
1214 }
1215}
1216
1217void enable_thermal_clk(void)
1218{
1219 enable_pll3();
1220}
1221
Anatolij Gustschin38df3702017-08-28 21:46:26 +02001222#ifdef CONFIG_MTD_NOR_FLASH
1223void enable_eim_clk(unsigned char enable)
1224{
1225 u32 reg;
1226
1227 reg = __raw_readl(&imx_ccm->CCGR6);
1228 if (enable)
1229 reg |= MXC_CCM_CCGR6_EMI_SLOW_MASK;
1230 else
1231 reg &= ~MXC_CCM_CCGR6_EMI_SLOW_MASK;
1232 __raw_writel(reg, &imx_ccm->CCGR6);
1233}
1234#endif
1235
Jason Liu23608e22011-11-25 00:18:02 +00001236unsigned int mxc_get_clock(enum mxc_clock clk)
1237{
1238 switch (clk) {
1239 case MXC_ARM_CLK:
1240 return get_mcu_main_clk();
1241 case MXC_PER_CLK:
1242 return get_periph_clk();
1243 case MXC_AHB_CLK:
1244 return get_ahb_clk();
1245 case MXC_IPG_CLK:
1246 return get_ipg_clk();
1247 case MXC_IPG_PERCLK:
Matthias Weissere7bed5c2012-09-24 02:46:53 +00001248 case MXC_I2C_CLK:
Jason Liu23608e22011-11-25 00:18:02 +00001249 return get_ipg_per_clk();
1250 case MXC_UART_CLK:
1251 return get_uart_clk();
1252 case MXC_CSPI_CLK:
1253 return get_cspi_clk();
1254 case MXC_AXI_CLK:
1255 return get_axi_clk();
1256 case MXC_EMI_SLOW_CLK:
1257 return get_emi_slow_clk();
1258 case MXC_DDR_CLK:
1259 return get_mmdc_ch0_clk();
1260 case MXC_ESDHC_CLK:
1261 return get_usdhc_clk(0);
1262 case MXC_ESDHC2_CLK:
1263 return get_usdhc_clk(1);
1264 case MXC_ESDHC3_CLK:
1265 return get_usdhc_clk(2);
1266 case MXC_ESDHC4_CLK:
1267 return get_usdhc_clk(3);
1268 case MXC_SATA_CLK:
1269 return get_ahb_clk();
1270 default:
Peng Faneb412d72014-11-23 11:52:20 +08001271 printf("Unsupported MXC CLK: %d\n", clk);
Jason Liu23608e22011-11-25 00:18:02 +00001272 break;
1273 }
1274
Peng Faneb412d72014-11-23 11:52:20 +08001275 return 0;
Jason Liu23608e22011-11-25 00:18:02 +00001276}
1277
Fabio Estevamb8bffab2019-07-12 09:10:35 -03001278#ifndef CONFIG_MX6SX
1279void enable_ipu_clock(void)
1280{
1281 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Fabio Estevam1be51fe2019-07-12 09:32:22 -03001282
1283 setbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_IPU1_IPU_MASK);
Fabio Estevamb8bffab2019-07-12 09:10:35 -03001284
1285 if (is_mx6dqp()) {
1286 setbits_le32(&mxc_ccm->CCGR6, MXC_CCM_CCGR6_PRG_CLK0_MASK);
1287 setbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_IPU2_IPU_MASK);
1288 }
1289}
Fabio Estevamfa64df42019-07-12 09:32:23 -03001290
1291void disable_ipu_clock(void)
1292{
1293 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1294
1295 clrbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_IPU1_IPU_MASK);
1296
1297 if (is_mx6dqp()) {
1298 clrbits_le32(&mxc_ccm->CCGR6, MXC_CCM_CCGR6_PRG_CLK0_MASK);
1299 clrbits_le32(&mxc_ccm->CCGR3, MXC_CCM_CCGR3_IPU2_IPU_MASK);
1300 }
1301}
Fabio Estevamb8bffab2019-07-12 09:10:35 -03001302#endif
1303
Anatolij Gustschin38df3702017-08-28 21:46:26 +02001304#ifndef CONFIG_SPL_BUILD
Jason Liu23608e22011-11-25 00:18:02 +00001305/*
1306 * Dump some core clockes.
1307 */
1308int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1309{
1310 u32 freq;
Benoît Thébaudeau833b6432012-09-27 10:19:58 +00001311 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +00001312 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +00001313 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +00001314 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +00001315 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +00001316 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeau833b6432012-09-27 10:19:58 +00001317 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liu23608e22011-11-25 00:18:02 +00001318 printf("PLL_NET %8d MHz\n", freq / 1000000);
1319
1320 printf("\n");
Peng Fand78e7f22016-03-09 16:44:38 +08001321 printf("ARM %8d kHz\n", mxc_get_clock(MXC_ARM_CLK) / 1000);
Jason Liu23608e22011-11-25 00:18:02 +00001322 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
1323 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamcc446722012-11-16 01:30:10 +00001324#ifdef CONFIG_MXC_SPI
Jason Liu23608e22011-11-25 00:18:02 +00001325 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamcc446722012-11-16 01:30:10 +00001326#endif
Jason Liu23608e22011-11-25 00:18:02 +00001327 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
1328 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
1329 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
1330 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
1331 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
1332 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
1333 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
1334 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
1335 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
1336
1337 return 0;
1338}
1339
Akshay Bhat90d7cc42016-04-12 18:13:56 -04001340#if defined(CONFIG_MX6Q) || defined(CONFIG_MX6D) || defined(CONFIG_MX6DL) || \
1341 defined(CONFIG_MX6S)
1342static void disable_ldb_di_clock_sources(void)
1343{
1344 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1345 int reg;
1346
1347 /* Make sure PFDs are disabled at boot. */
1348 reg = readl(&mxc_ccm->analog_pfd_528);
1349 /* Cannot disable pll2_pfd2_396M, as it is the MMDC clock in iMX6DL */
Peng Fanb949fd22016-05-23 18:35:54 +08001350 if (is_mx6sdl())
Akshay Bhat90d7cc42016-04-12 18:13:56 -04001351 reg |= 0x80008080;
1352 else
1353 reg |= 0x80808080;
1354 writel(reg, &mxc_ccm->analog_pfd_528);
1355
1356 /* Disable PLL3 PFDs */
1357 reg = readl(&mxc_ccm->analog_pfd_480);
1358 reg |= 0x80808080;
1359 writel(reg, &mxc_ccm->analog_pfd_480);
1360
1361 /* Disable PLL5 */
1362 reg = readl(&mxc_ccm->analog_pll_video);
1363 reg &= ~(1 << 13);
1364 writel(reg, &mxc_ccm->analog_pll_video);
1365}
1366
1367static void enable_ldb_di_clock_sources(void)
1368{
1369 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1370 int reg;
1371
1372 reg = readl(&mxc_ccm->analog_pfd_528);
Peng Fanb949fd22016-05-23 18:35:54 +08001373 if (is_mx6sdl())
Akshay Bhat90d7cc42016-04-12 18:13:56 -04001374 reg &= ~(0x80008080);
1375 else
1376 reg &= ~(0x80808080);
1377 writel(reg, &mxc_ccm->analog_pfd_528);
1378
1379 reg = readl(&mxc_ccm->analog_pfd_480);
1380 reg &= ~(0x80808080);
1381 writel(reg, &mxc_ccm->analog_pfd_480);
1382}
1383
1384/*
1385 * Try call this function as early in the boot process as possible since the
1386 * function temporarily disables PLL2 PFD's, PLL3 PFD's and PLL5.
1387 */
1388void select_ldb_di_clock_source(enum ldb_di_clock clk)
1389{
1390 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
1391 int reg;
1392
1393 /*
1394 * Need to follow a strict procedure when changing the LDB
1395 * clock, else we can introduce a glitch. Things to keep in
1396 * mind:
1397 * 1. The current and new parent clocks must be disabled.
1398 * 2. The default clock for ldb_dio_clk is mmdc_ch1 which has
1399 * no CG bit.
1400 * 3. In the RTL implementation of the LDB_DI_CLK_SEL mux
1401 * the top four options are in one mux and the PLL3 option along
1402 * with another option is in the second mux. There is third mux
1403 * used to decide between the first and second mux.
1404 * The code below switches the parent to the bottom mux first
1405 * and then manipulates the top mux. This ensures that no glitch
1406 * will enter the divider.
1407 *
1408 * Need to disable MMDC_CH1 clock manually as there is no CG bit
1409 * for this clock. The only way to disable this clock is to move
1410 * it to pll3_sw_clk and then to disable pll3_sw_clk
1411 * Make sure periph2_clk2_sel is set to pll3_sw_clk
1412 */
1413
1414 /* Disable all ldb_di clock parents */
1415 disable_ldb_di_clock_sources();
1416
1417 /* Set MMDC_CH1 mask bit */
1418 reg = readl(&mxc_ccm->ccdr);
1419 reg |= MXC_CCM_CCDR_MMDC_CH1_HS_MASK;
1420 writel(reg, &mxc_ccm->ccdr);
1421
1422 /* Set periph2_clk2_sel to be sourced from PLL3_sw_clk */
1423 reg = readl(&mxc_ccm->cbcmr);
1424 reg &= ~MXC_CCM_CBCMR_PERIPH2_CLK2_SEL;
1425 writel(reg, &mxc_ccm->cbcmr);
1426
1427 /*
1428 * Set the periph2_clk_sel to the top mux so that
1429 * mmdc_ch1 is from pll3_sw_clk.
1430 */
1431 reg = readl(&mxc_ccm->cbcdr);
1432 reg |= MXC_CCM_CBCDR_PERIPH2_CLK_SEL;
1433 writel(reg, &mxc_ccm->cbcdr);
1434
1435 /* Wait for the clock switch */
1436 while (readl(&mxc_ccm->cdhipr))
1437 ;
1438 /* Disable pll3_sw_clk by selecting bypass clock source */
1439 reg = readl(&mxc_ccm->ccsr);
1440 reg |= MXC_CCM_CCSR_PLL3_SW_CLK_SEL;
1441 writel(reg, &mxc_ccm->ccsr);
1442
1443 /* Set the ldb_di0_clk and ldb_di1_clk to 111b */
1444 reg = readl(&mxc_ccm->cs2cdr);
1445 reg |= ((7 << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1446 | (7 << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1447 writel(reg, &mxc_ccm->cs2cdr);
1448
1449 /* Set the ldb_di0_clk and ldb_di1_clk to 100b */
1450 reg = readl(&mxc_ccm->cs2cdr);
1451 reg &= ~(MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_MASK
1452 | MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_MASK);
1453 reg |= ((4 << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1454 | (4 << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1455 writel(reg, &mxc_ccm->cs2cdr);
1456
1457 /* Set the ldb_di0_clk and ldb_di1_clk to desired source */
1458 reg = readl(&mxc_ccm->cs2cdr);
1459 reg &= ~(MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_MASK
1460 | MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_MASK);
1461 reg |= ((clk << MXC_CCM_CS2CDR_LDB_DI1_CLK_SEL_OFFSET)
1462 | (clk << MXC_CCM_CS2CDR_LDB_DI0_CLK_SEL_OFFSET));
1463 writel(reg, &mxc_ccm->cs2cdr);
1464
1465 /* Unbypass pll3_sw_clk */
1466 reg = readl(&mxc_ccm->ccsr);
1467 reg &= ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL;
1468 writel(reg, &mxc_ccm->ccsr);
1469
1470 /*
1471 * Set the periph2_clk_sel back to the bottom mux so that
1472 * mmdc_ch1 is from its original parent.
1473 */
1474 reg = readl(&mxc_ccm->cbcdr);
1475 reg &= ~MXC_CCM_CBCDR_PERIPH2_CLK_SEL;
1476 writel(reg, &mxc_ccm->cbcdr);
1477
1478 /* Wait for the clock switch */
1479 while (readl(&mxc_ccm->cdhipr))
1480 ;
1481 /* Clear MMDC_CH1 mask bit */
1482 reg = readl(&mxc_ccm->ccdr);
1483 reg &= ~MXC_CCM_CCDR_MMDC_CH1_HS_MASK;
1484 writel(reg, &mxc_ccm->ccdr);
1485
1486 enable_ldb_di_clock_sources();
1487}
1488#endif
1489
Jason Liu23608e22011-11-25 00:18:02 +00001490/***************************************************/
1491
1492U_BOOT_CMD(
1493 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
1494 "display clocks",
1495 ""
1496);
Anatolij Gustschin38df3702017-08-28 21:46:26 +02001497#endif