blob: a50db70b19e45da1810cf82e2cb63496446e3eb9 [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;
189
190 reg = __raw_readl(&imx_ccm->cscdr1);
191 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
192 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
193
194 return PLL3_80M / (uart_podf + 1);
195}
196
197static u32 get_cspi_clk(void)
198{
199 u32 reg, cspi_podf;
200
201 reg = __raw_readl(&imx_ccm->cscdr2);
202 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
203 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
204
205 return PLL3_60M / (cspi_podf + 1);
206}
207
208static u32 get_axi_clk(void)
209{
210 u32 root_freq, axi_podf;
211 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
212
213 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
214 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
215
216 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
217 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
218 root_freq = PLL2_PFD2_FREQ;
219 else
220 root_freq = PLL3_PFD1_FREQ;
221 } else
222 root_freq = get_periph_clk();
223
224 return root_freq / (axi_podf + 1);
225}
226
227static u32 get_emi_slow_clk(void)
228{
229 u32 emi_clk_sel, emi_slow_pof, cscmr1, root_freq = 0;
230
231 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
232 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
233 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
234 emi_slow_pof = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
235 emi_slow_pof >>= MXC_CCM_CSCMR1_ACLK_EMI_PODF_OFFSET;
236
237 switch (emi_clk_sel) {
238 case 0:
239 root_freq = get_axi_clk();
240 break;
241 case 1:
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000242 root_freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000243 break;
244 case 2:
245 root_freq = PLL2_PFD2_FREQ;
246 break;
247 case 3:
248 root_freq = PLL2_PFD0_FREQ;
249 break;
250 }
251
252 return root_freq / (emi_slow_pof + 1);
253}
254
255static u32 get_mmdc_ch0_clk(void)
256{
257 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
258 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
259 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
260
261 return get_periph_clk() / (mmdc_ch0_podf + 1);
262}
263
264static u32 get_usdhc_clk(u32 port)
265{
266 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
267 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
268 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
269
270 switch (port) {
271 case 0:
272 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
273 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
274 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
275
276 break;
277 case 1:
278 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
279 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
280 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
281
282 break;
283 case 2:
284 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
285 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
286 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
287
288 break;
289 case 3:
290 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
291 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
292 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
293
294 break;
295 default:
296 break;
297 }
298
299 if (clk_sel)
300 root_freq = PLL2_PFD0_FREQ;
301 else
302 root_freq = PLL2_PFD2_FREQ;
303
304 return root_freq / (usdhc_podf + 1);
305}
306
307u32 imx_get_uartclk(void)
308{
309 return get_uart_clk();
310}
311
Jason Liu92aa90b2011-12-16 05:17:06 +0000312u32 imx_get_fecclk(void)
313{
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000314 return decode_pll(PLL_ENET, MXC_HCLK);
Jason Liu92aa90b2011-12-16 05:17:06 +0000315}
316
Eric Nelsonfdba0762012-03-27 09:52:21 +0000317int enable_sata_clock(void)
318{
319 u32 reg = 0;
320 s32 timeout = 100000;
321 struct mxc_ccm_reg *const imx_ccm
322 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
323
324 /* Enable sata clock */
325 reg = readl(&imx_ccm->CCGR5); /* CCGR5 */
Eric Nelsone4279542012-09-21 07:33:51 +0000326 reg |= MXC_CCM_CCGR5_SATA_MASK;
Eric Nelsonfdba0762012-03-27 09:52:21 +0000327 writel(reg, &imx_ccm->CCGR5);
328
329 /* Enable PLLs */
330 reg = readl(&imx_ccm->analog_pll_enet);
331 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
332 writel(reg, &imx_ccm->analog_pll_enet);
333 reg |= BM_ANADIG_PLL_SYS_ENABLE;
334 while (timeout--) {
335 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
336 break;
337 }
338 if (timeout <= 0)
339 return -EIO;
340 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
341 writel(reg, &imx_ccm->analog_pll_enet);
342 reg |= BM_ANADIG_PLL_ENET_ENABLE_SATA;
343 writel(reg, &imx_ccm->analog_pll_enet);
344
345 return 0 ;
346}
347
Jason Liudec11122011-11-25 00:18:02 +0000348unsigned int mxc_get_clock(enum mxc_clock clk)
349{
350 switch (clk) {
351 case MXC_ARM_CLK:
352 return get_mcu_main_clk();
353 case MXC_PER_CLK:
354 return get_periph_clk();
355 case MXC_AHB_CLK:
356 return get_ahb_clk();
357 case MXC_IPG_CLK:
358 return get_ipg_clk();
359 case MXC_IPG_PERCLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +0000360 case MXC_I2C_CLK:
Jason Liudec11122011-11-25 00:18:02 +0000361 return get_ipg_per_clk();
362 case MXC_UART_CLK:
363 return get_uart_clk();
364 case MXC_CSPI_CLK:
365 return get_cspi_clk();
366 case MXC_AXI_CLK:
367 return get_axi_clk();
368 case MXC_EMI_SLOW_CLK:
369 return get_emi_slow_clk();
370 case MXC_DDR_CLK:
371 return get_mmdc_ch0_clk();
372 case MXC_ESDHC_CLK:
373 return get_usdhc_clk(0);
374 case MXC_ESDHC2_CLK:
375 return get_usdhc_clk(1);
376 case MXC_ESDHC3_CLK:
377 return get_usdhc_clk(2);
378 case MXC_ESDHC4_CLK:
379 return get_usdhc_clk(3);
380 case MXC_SATA_CLK:
381 return get_ahb_clk();
382 default:
383 break;
384 }
385
386 return -1;
387}
388
389/*
390 * Dump some core clockes.
391 */
392int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
393{
394 u32 freq;
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000395 freq = decode_pll(PLL_SYS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000396 printf("PLL_SYS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000397 freq = decode_pll(PLL_BUS, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000398 printf("PLL_BUS %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000399 freq = decode_pll(PLL_USBOTG, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000400 printf("PLL_OTG %8d MHz\n", freq / 1000000);
Benoît Thébaudeauafac1652012-09-27 10:19:58 +0000401 freq = decode_pll(PLL_ENET, MXC_HCLK);
Jason Liudec11122011-11-25 00:18:02 +0000402 printf("PLL_NET %8d MHz\n", freq / 1000000);
403
404 printf("\n");
405 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
406 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000407#ifdef CONFIG_MXC_SPI
Jason Liudec11122011-11-25 00:18:02 +0000408 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
Fabio Estevamf6fde412012-11-16 01:30:10 +0000409#endif
Jason Liudec11122011-11-25 00:18:02 +0000410 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
411 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
412 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
413 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
414 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
415 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
416 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
417 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
418 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
419
420 return 0;
421}
422
423/***************************************************/
424
425U_BOOT_CMD(
426 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
427 "display clocks",
428 ""
429);