blob: fddb3733ae28aeba30ea8c953c29153dcca7a4a9 [file] [log] [blame]
Jason Liu23608e22011-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 Estevam6a376042012-04-29 08:11:13 +000027#include <asm/arch/crm_regs.h>
Jason Liu23608e22011-11-25 00:18:02 +000028#include <asm/arch/clock.h>
Fabio Estevam6a376042012-04-29 08:11:13 +000029#include <asm/arch/sys_proto.h>
Jason Liu23608e22011-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 Estevam6a376042012-04-29 08:11:13 +000038struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
Jason Liu23608e22011-11-25 00:18:02 +000039
Wolfgang Grandegger3f467522012-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)
46 reg |= MXC_CCM_CCGR_CG_MASK << MXC_CCM_CCGR0_CG0_OFFSET;
47 else
48 reg &= ~(MXC_CCM_CCGR_CG_MASK << MXC_CCM_CCGR0_CG0_OFFSET);
49 __raw_writel(reg, &imx_ccm->CCGR6);
50
51}
52
Troy Kiskycc54a0f2012-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;
62 mask = MXC_CCM_CCGR_CG_MASK << ((i2c_num + 3) << 1);
63 reg = __raw_readl(&imx_ccm->CCGR2);
64 if (enable)
65 reg |= mask;
66 else
67 reg &= ~mask;
68 __raw_writel(reg, &imx_ccm->CCGR2);
69 return 0;
70}
71#endif
72
Jason Liu23608e22011-11-25 00:18:02 +000073static u32 decode_pll(enum pll_clocks pll, u32 infreq)
74{
75 u32 div;
76
77 switch (pll) {
78 case PLL_SYS:
79 div = __raw_readl(&imx_ccm->analog_pll_sys);
80 div &= BM_ANADIG_PLL_SYS_DIV_SELECT;
81
82 return infreq * (div >> 1);
83 case PLL_BUS:
84 div = __raw_readl(&imx_ccm->analog_pll_528);
85 div &= BM_ANADIG_PLL_528_DIV_SELECT;
86
87 return infreq * (20 + (div << 1));
88 case PLL_USBOTG:
89 div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl);
90 div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT;
91
92 return infreq * (20 + (div << 1));
93 case PLL_ENET:
94 div = __raw_readl(&imx_ccm->analog_pll_enet);
95 div &= BM_ANADIG_PLL_ENET_DIV_SELECT;
96
97 return (div == 3 ? 125000000 : 25000000 * (div << 1));
98 default:
99 return 0;
100 }
101 /* NOTREACHED */
102}
103
104static u32 get_mcu_main_clk(void)
105{
106 u32 reg, freq;
107
108 reg = __raw_readl(&imx_ccm->cacrr);
109 reg &= MXC_CCM_CACRR_ARM_PODF_MASK;
110 reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET;
111 freq = decode_pll(PLL_SYS, CONFIG_SYS_MX6_HCLK);
112
113 return freq / (reg + 1);
114}
115
Fabio Estevam6a376042012-04-29 08:11:13 +0000116u32 get_periph_clk(void)
Jason Liu23608e22011-11-25 00:18:02 +0000117{
118 u32 reg, freq = 0;
119
120 reg = __raw_readl(&imx_ccm->cbcdr);
121 if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
122 reg = __raw_readl(&imx_ccm->cbcmr);
123 reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK;
124 reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET;
125
126 switch (reg) {
127 case 0:
128 freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK);
129 break;
130 case 1:
131 case 2:
132 freq = CONFIG_SYS_MX6_HCLK;
133 break;
134 default:
135 break;
136 }
137 } else {
138 reg = __raw_readl(&imx_ccm->cbcmr);
139 reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK;
140 reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET;
141
142 switch (reg) {
143 case 0:
144 freq = decode_pll(PLL_BUS, CONFIG_SYS_MX6_HCLK);
145 break;
146 case 1:
147 freq = PLL2_PFD2_FREQ;
148 break;
149 case 2:
150 freq = PLL2_PFD0_FREQ;
151 break;
152 case 3:
153 freq = PLL2_PFD2_DIV_FREQ;
154 break;
155 default:
156 break;
157 }
158 }
159
160 return freq;
161}
162
Jason Liu23608e22011-11-25 00:18:02 +0000163static u32 get_ipg_clk(void)
164{
165 u32 reg, ipg_podf;
166
167 reg = __raw_readl(&imx_ccm->cbcdr);
168 reg &= MXC_CCM_CBCDR_IPG_PODF_MASK;
169 ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET;
170
171 return get_ahb_clk() / (ipg_podf + 1);
172}
173
174static u32 get_ipg_per_clk(void)
175{
176 u32 reg, perclk_podf;
177
178 reg = __raw_readl(&imx_ccm->cscmr1);
179 perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK;
180
181 return get_ipg_clk() / (perclk_podf + 1);
182}
183
184static u32 get_uart_clk(void)
185{
186 u32 reg, uart_podf;
187
188 reg = __raw_readl(&imx_ccm->cscdr1);
189 reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK;
190 uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET;
191
192 return PLL3_80M / (uart_podf + 1);
193}
194
195static u32 get_cspi_clk(void)
196{
197 u32 reg, cspi_podf;
198
199 reg = __raw_readl(&imx_ccm->cscdr2);
200 reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK;
201 cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET;
202
203 return PLL3_60M / (cspi_podf + 1);
204}
205
206static u32 get_axi_clk(void)
207{
208 u32 root_freq, axi_podf;
209 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
210
211 axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK;
212 axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET;
213
214 if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) {
215 if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL)
216 root_freq = PLL2_PFD2_FREQ;
217 else
218 root_freq = PLL3_PFD1_FREQ;
219 } else
220 root_freq = get_periph_clk();
221
222 return root_freq / (axi_podf + 1);
223}
224
225static u32 get_emi_slow_clk(void)
226{
227 u32 emi_clk_sel, emi_slow_pof, cscmr1, root_freq = 0;
228
229 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
230 emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK;
231 emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET;
232 emi_slow_pof = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK;
233 emi_slow_pof >>= MXC_CCM_CSCMR1_ACLK_EMI_PODF_OFFSET;
234
235 switch (emi_clk_sel) {
236 case 0:
237 root_freq = get_axi_clk();
238 break;
239 case 1:
240 root_freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK);
241 break;
242 case 2:
243 root_freq = PLL2_PFD2_FREQ;
244 break;
245 case 3:
246 root_freq = PLL2_PFD0_FREQ;
247 break;
248 }
249
250 return root_freq / (emi_slow_pof + 1);
251}
252
253static u32 get_mmdc_ch0_clk(void)
254{
255 u32 cbcdr = __raw_readl(&imx_ccm->cbcdr);
256 u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >>
257 MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET;
258
259 return get_periph_clk() / (mmdc_ch0_podf + 1);
260}
261
262static u32 get_usdhc_clk(u32 port)
263{
264 u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0;
265 u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1);
266 u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1);
267
268 switch (port) {
269 case 0:
270 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >>
271 MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET;
272 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL;
273
274 break;
275 case 1:
276 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >>
277 MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET;
278 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL;
279
280 break;
281 case 2:
282 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >>
283 MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET;
284 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL;
285
286 break;
287 case 3:
288 usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >>
289 MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET;
290 clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL;
291
292 break;
293 default:
294 break;
295 }
296
297 if (clk_sel)
298 root_freq = PLL2_PFD0_FREQ;
299 else
300 root_freq = PLL2_PFD2_FREQ;
301
302 return root_freq / (usdhc_podf + 1);
303}
304
305u32 imx_get_uartclk(void)
306{
307 return get_uart_clk();
308}
309
Jason Liuff167df2011-12-16 05:17:06 +0000310u32 imx_get_fecclk(void)
311{
312 return decode_pll(PLL_ENET, CONFIG_SYS_MX6_HCLK);
313}
314
Eric Nelson64e7cdb2012-03-27 09:52:21 +0000315int enable_sata_clock(void)
316{
317 u32 reg = 0;
318 s32 timeout = 100000;
319 struct mxc_ccm_reg *const imx_ccm
320 = (struct mxc_ccm_reg *) CCM_BASE_ADDR;
321
322 /* Enable sata clock */
323 reg = readl(&imx_ccm->CCGR5); /* CCGR5 */
324 reg |= MXC_CCM_CCGR5_CG2_MASK;
325 writel(reg, &imx_ccm->CCGR5);
326
327 /* Enable PLLs */
328 reg = readl(&imx_ccm->analog_pll_enet);
329 reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN;
330 writel(reg, &imx_ccm->analog_pll_enet);
331 reg |= BM_ANADIG_PLL_SYS_ENABLE;
332 while (timeout--) {
333 if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK)
334 break;
335 }
336 if (timeout <= 0)
337 return -EIO;
338 reg &= ~BM_ANADIG_PLL_SYS_BYPASS;
339 writel(reg, &imx_ccm->analog_pll_enet);
340 reg |= BM_ANADIG_PLL_ENET_ENABLE_SATA;
341 writel(reg, &imx_ccm->analog_pll_enet);
342
343 return 0 ;
344}
345
Jason Liu23608e22011-11-25 00:18:02 +0000346unsigned int mxc_get_clock(enum mxc_clock clk)
347{
348 switch (clk) {
349 case MXC_ARM_CLK:
350 return get_mcu_main_clk();
351 case MXC_PER_CLK:
352 return get_periph_clk();
353 case MXC_AHB_CLK:
354 return get_ahb_clk();
355 case MXC_IPG_CLK:
356 return get_ipg_clk();
357 case MXC_IPG_PERCLK:
358 return get_ipg_per_clk();
359 case MXC_UART_CLK:
360 return get_uart_clk();
361 case MXC_CSPI_CLK:
362 return get_cspi_clk();
363 case MXC_AXI_CLK:
364 return get_axi_clk();
365 case MXC_EMI_SLOW_CLK:
366 return get_emi_slow_clk();
367 case MXC_DDR_CLK:
368 return get_mmdc_ch0_clk();
369 case MXC_ESDHC_CLK:
370 return get_usdhc_clk(0);
371 case MXC_ESDHC2_CLK:
372 return get_usdhc_clk(1);
373 case MXC_ESDHC3_CLK:
374 return get_usdhc_clk(2);
375 case MXC_ESDHC4_CLK:
376 return get_usdhc_clk(3);
377 case MXC_SATA_CLK:
378 return get_ahb_clk();
379 default:
380 break;
381 }
382
383 return -1;
384}
385
386/*
387 * Dump some core clockes.
388 */
389int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
390{
391 u32 freq;
392 freq = decode_pll(PLL_SYS, CONFIG_SYS_MX6_HCLK);
393 printf("PLL_SYS %8d MHz\n", freq / 1000000);
394 freq = decode_pll(PLL_BUS, CONFIG_SYS_MX6_HCLK);
395 printf("PLL_BUS %8d MHz\n", freq / 1000000);
396 freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK);
397 printf("PLL_OTG %8d MHz\n", freq / 1000000);
398 freq = decode_pll(PLL_ENET, CONFIG_SYS_MX6_HCLK);
399 printf("PLL_NET %8d MHz\n", freq / 1000000);
400
401 printf("\n");
402 printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
403 printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000);
404 printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000);
405 printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
406 printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000);
407 printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
408 printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000);
409 printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000);
410 printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000);
411 printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000);
412 printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000);
413 printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
414
415 return 0;
416}
417
418/***************************************************/
419
420U_BOOT_CMD(
421 clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks,
422 "display clocks",
423 ""
424);