blob: 8cba4fd78e6efeea2a855efa438d9c5d9ea48b3f [file] [log] [blame]
Jason Liudec11122011-11-25 00:18:02 +00001/*
2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <asm/io.h>
25#include <asm/errno.h>
26#include <asm/arch/imx-regs.h>
Fabio Estevam6479f512012-04-29 08:11:13 +000027#include <asm/arch/crm_regs.h>
Jason Liudec11122011-11-25 00:18:02 +000028#include <asm/arch/clock.h>
Fabio Estevam6479f512012-04-29 08:11:13 +000029#include <asm/arch/sys_proto.h>
Jason Liudec11122011-11-25 00:18:02 +000030
31enum pll_clocks {
32 PLL_SYS, /* System PLL */
33 PLL_BUS, /* System Bus PLL*/
34 PLL_USBOTG, /* OTG USB PLL */
35 PLL_ENET, /* ENET PLL */
36};
37
Fabio Estevam6479f512012-04-29 08:11:13 +000038struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liudec11122011-11-25 00:18:02 +000039
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000040void enable_usboh3_clk(unsigned char enable)
41{
42 u32 reg;
43
44 reg = __raw_readl(&imx_ccm->CCGR6);
45 if (enable)
Eric Nelsone4279542012-09-21 07:33:51 +000046 reg |= MXC_CCM_CCGR6_USBOH3_MASK;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000047 else
Eric Nelsone4279542012-09-21 07:33:51 +000048 reg &= ~(MXC_CCM_CCGR6_USBOH3_MASK);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000049 __raw_writel(reg, &imx_ccm->CCGR6);
50
51}
52
Troy Kiskyd4fdc992012-07-19 08:18:25 +000053#ifdef CONFIG_I2C_MXC
54/* i2c_num can be from 0 - 2 */
55int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
56{
57 u32 reg;
58 u32 mask;
59
60 if (i2c_num > 2)
61 return -EINVAL;
Eric Nelsone4279542012-09-21 07:33:51 +000062
63 mask = MXC_CCM_CCGR_CG_MASK
64 << (MXC_CCM_CCGR2_I2C1_SERIAL_OFFSET + (i2c_num << 1));
Troy Kiskyd4fdc992012-07-19 08:18:25 +000065 reg = __raw_readl(&imx_ccm->CCGR2);
66 if (enable)
67 reg |= mask;
68 else
69 reg &= ~mask;
70 __raw_writel(reg, &imx_ccm->CCGR2);
71 return 0;
72}
73#endif
74
Jason Liudec11122011-11-25 00:18:02 +000075static u32 decode_pll(enum pll_clocks pll, u32 infreq)
76{
77 u32 div;
78
79 switch (pll) {
80 case PLL_SYS:
81 div = __raw_readl(&imx_ccm->analog_pll_sys);
82 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
83
84 return infreq * (div >> 1);
85 case PLL_BUS:
86 div = __raw_readl(&imx_ccm->analog_pll_528);
87 div &= BM_ANADIG_PLL_528_DIV_SELECT;
88
89 return infreq * (20 + (div << 1));
90 case PLL_USBOTG:
91 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
92 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
93
94 return infreq * (20 + (div << 1));
95 case PLL_ENET:
96 div = __raw_readl(&imx_ccm->analog_pll_enet);
97 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
98
99 return (div == 3 ? 125000000 : 25000000 * (div << 1));
100 default:
101 return 0;
102 }
103 /* NOTREACHED */
104}
105
106static u32 get_mcu_main_clk(void)
107{
108 u32 reg, freq;
109
110 reg = __raw_readl(&imx_ccm->cacrr);
111 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
112 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000113 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000114
115 return freq / (reg + 1);
116}
117
Fabio Estevam6479f512012-04-29 08:11:13 +0000118u32 get_periph_clk(void)
Jason Liudec11122011-11-25 00:18:02 +0000119{
120 u32 reg, freq = 0;
121
122 reg = __raw_readl(&imx_ccm->cbcdr);
123 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
124 reg = __raw_readl(&imx_ccm->cbcmr);
125 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
126 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
127
128 switch (reg) {
129 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000130 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000131 break;
132 case 1:
133 case 2:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000134 freq = MXC_HCLK;
Jason Liudec11122011-11-25 00:18:02 +0000135 break;
136 default:
137 break;
138 }
139 } else {
140 reg = __raw_readl(&imx_ccm->cbcmr);
141 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
142 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
143
144 switch (reg) {
145 case 0:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000146 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000147 break;
148 case 1:
149 freq = PLL2_PFD2_FREQ;
150 break;
151 case 2:
152 freq = PLL2_PFD0_FREQ;
153 break;
154 case 3:
155 freq = PLL2_PFD2_DIV_FREQ;
156 break;
157 default:
158 break;
159 }
160 }
161
162 return freq;
163}
164
Jason Liudec11122011-11-25 00:18:02 +0000165static u32 get_ipg_clk(void)
166{
167 u32 reg, ipg_podf;
168
169 reg = __raw_readl(&imx_ccm->cbcdr);
170 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
171 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
172
173 return get_ahb_clk() / (ipg_podf + 1);
174}
175
176static u32 get_ipg_per_clk(void)
177{
178 u32 reg, perclk_podf;
179
180 reg = __raw_readl(&imx_ccm->cscmr1);
181 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
182
183 return get_ipg_clk() / (perclk_podf + 1);
184}
185
186static u32 get_uart_clk(void)
187{
188 u32 reg, uart_podf;
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000189 u32 freq = PLL3_80M;
Jason Liudec11122011-11-25 00:18:02 +0000190 reg = __raw_readl(&imx_ccm->cscdr1);
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000191#ifdef CONFIG_MX6SL
192 if (reg & MXC_CCM_CSCDR1_UART_CLK_SEL)
193 freq = MXC_HCLK;
194#endif
Jason Liudec11122011-11-25 00:18:02 +0000195 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
196 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
197
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000198 return freq / (uart_podf + 1);
Jason Liudec11122011-11-25 00:18:02 +0000199}
200
201static u32 get_cspi_clk(void)
202{
203 u32 reg, cspi_podf;
204
205 reg = __raw_readl(&imx_ccm->cscdr2);
206 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
207 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
208
209 return PLL3_60M / (cspi_podf + 1);
210}
211
212static u32 get_axi_clk(void)
213{
214 u32 root_freq, axi_podf;
215 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
216
217 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
218 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
219
220 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
221 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
222 root_freq = PLL2_PFD2_FREQ;
223 else
224 root_freq = PLL3_PFD1_FREQ;
225 } else
226 root_freq = get_periph_clk();
227
228 return root_freq / (axi_podf + 1);
229}
230
231static u32 get_emi_slow_clk(void)
232{
233 u32 emi_clk_sel, emi_slow_pof, cscmr1, root_freq = 0;
234
235 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
236 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
237 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
238 emi_slow_pof = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
239 emi_slow_pof >>= MXC_CCM_CSCMR1_ACLK_EMI_PODF_OFFSET;
240
241 switch (emi_clk_sel) {
242 case 0:
243 root_freq = get_axi_clk();
244 break;
245 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000246 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000247 break;
248 case 2:
249 root_freq = PLL2_PFD2_FREQ;
250 break;
251 case 3:
252 root_freq = PLL2_PFD0_FREQ;
253 break;
254 }
255
256 return root_freq / (emi_slow_pof + 1);
257}
258
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000259#ifdef CONFIG_MX6SL
260static u32 get_mmdc_ch0_clk(void)
261{
262 u32 cbcmr = __raw_readl(&imx_ccm->cbcmr);
263 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
264 u32 freq, podf;
265
266 podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH1_PODF_MASK) \
267 >> MXC_CCM_CBCDR_MMDC_CH1_PODF_OFFSET;
268
269 switch ((cbcmr & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK) >>
270 MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET) {
271 case 0:
272 freq = decode_pll(PLL_BUS, MXC_HCLK);
273 break;
274 case 1:
275 freq = PLL2_PFD2_FREQ;
276 break;
277 case 2:
278 freq = PLL2_PFD0_FREQ;
279 break;
280 case 3:
281 freq = PLL2_PFD2_DIV_FREQ;
282 }
283
284 return freq / (podf + 1);
285
286}
287#else
Jason Liudec11122011-11-25 00:18:02 +0000288static u32 get_mmdc_ch0_clk(void)
289{
290 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
291 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
292 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
293
294 return get_periph_clk() / (mmdc_ch0_podf + 1);
295}
Fabio Estevamf7b9ac22013-04-10 09:32:57 +0000296#endif
Jason Liudec11122011-11-25 00:18:02 +0000297
298static u32 get_usdhc_clk(u32 port)
299{
300 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
301 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
302 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
303
304 switch (port) {
305 case 0:
306 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
307 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
308 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
309
310 break;
311 case 1:
312 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
313 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
314 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
315
316 break;
317 case 2:
318 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
319 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
320 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
321
322 break;
323 case 3:
324 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
325 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
326 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
327
328 break;
329 default:
330 break;
331 }
332
333 if (clk_sel)
334 root_freq = PLL2_PFD0_FREQ;
335 else
336 root_freq = PLL2_PFD2_FREQ;
337
338 return root_freq / (usdhc_podf + 1);
339}
340
341u32 imx_get_uartclk(void)
342{
343 return get_uart_clk();
344}
345
Jason Liu92aa90b2011-12-16 05:17:06 +0000346u32 imx_get_fecclk(void)
347{
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000348 return decode_pll(PLL_ENET, MXC_HCLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000349}
350
Eric Nelsonfdba0762012-03-27 09:52:21 +0000351int enable_sata_clock(void)
352{
353 u32 reg = 0;
354 s32 timeout = 100000;
355 struct mxc_ccm_reg *const imx_ccm
356 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
357
358 /* Enable sata clock */
359 reg = readl(&imx_ccm->CCGR5); /* CCGR5 */
Eric Nelsone4279542012-09-21 07:33:51 +0000360 reg |= MXC_CCM_CCGR5_SATA_MASK;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000361 writel(reg, &imx_ccm->CCGR5);
362
363 /* Enable PLLs */
364 reg = readl(&imx_ccm->analog_pll_enet);
365 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
366 writel(reg, &imx_ccm->analog_pll_enet);
367 reg |= BM_ANADIG_PLL_SYS_ENABLE;
368 while (timeout--) {
369 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
370 break;
371 }
372 if (timeout <= 0)
373 return -EIO;
374 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
375 writel(reg, &imx_ccm->analog_pll_enet);
376 reg |= BM_ANADIG_PLL_ENET_ENABLE_SATA;
377 writel(reg, &imx_ccm->analog_pll_enet);
378
379 return 0 ;
380}
381
Jason Liudec11122011-11-25 00:18:02 +0000382unsigned int mxc_get_clock(enum mxc_clock clk)
383{
384 switch (clk) {
385 case MXC_ARM_CLK:
386 return get_mcu_main_clk();
387 case MXC_PER_CLK:
388 return get_periph_clk();
389 case MXC_AHB_CLK:
390 return get_ahb_clk();
391 case MXC_IPG_CLK:
392 return get_ipg_clk();
393 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000394 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +0000395 return get_ipg_per_clk();
396 case MXC_UART_CLK:
397 return get_uart_clk();
398 case MXC_CSPI_CLK:
399 return get_cspi_clk();
400 case MXC_AXI_CLK:
401 return get_axi_clk();
402 case MXC_EMI_SLOW_CLK:
403 return get_emi_slow_clk();
404 case MXC_DDR_CLK:
405 return get_mmdc_ch0_clk();
406 case MXC_ESDHC_CLK:
407 return get_usdhc_clk(0);
408 case MXC_ESDHC2_CLK:
409 return get_usdhc_clk(1);
410 case MXC_ESDHC3_CLK:
411 return get_usdhc_clk(2);
412 case MXC_ESDHC4_CLK:
413 return get_usdhc_clk(3);
414 case MXC_SATA_CLK:
415 return get_ahb_clk();
416 default:
417 break;
418 }
419
420 return -1;
421}
422
423/*
424 * Dump some core clockes.
425 */
426int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
427{
428 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000429 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000430 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000431 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000432 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000433 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000434 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000435 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000436 printf("PLL_NET %8d MHz\n", freq / 1000000);
437
438 printf("\n");
439 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
440 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000441#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +0000442 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000443#endif
Jason Liudec11122011-11-25 00:18:02 +0000444 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
445 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
446 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
447 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
448 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
449 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
450 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
451 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
452 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
453
454 return 0;
455}
456
457/***************************************************/
458
459U_BOOT_CMD(
460 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
461 "display clocks",
462 ""
463);