3 * Sascha Hauer, Pengutronix
5 * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/errno.h>
30 #include <asm/arch/imx-regs.h>
31 #include <asm/arch/crm_regs.h>
32 #include <asm/arch/clock.h>
33 #include <asm/arch/sys_proto.h>
34 #ifdef CONFIG_FSL_ESDHC
35 #include <fsl_esdhc.h>
40 #define CLK_CODE(arm, ahb, sel) (((arm) << 16) + ((ahb) << 8) + (sel))
41 #define CLK_CODE_ARM(c) (((c) >> 16) & 0xFF)
42 #define CLK_CODE_AHB(c) (((c) >> 8) & 0xFF)
43 #define CLK_CODE_PATH(c) ((c) & 0xFF)
45 #define CCM_GET_DIVIDER(x, m, o) (((x) & (m)) >> (o))
47 #ifdef CONFIG_FSL_ESDHC
48 DECLARE_GLOBAL_DATA_PTR;
51 static int g_clk_mux_auto[8] = {
52 CLK_CODE(1, 3, 0), CLK_CODE(1, 2, 1), CLK_CODE(2, 1, 1), -1,
53 CLK_CODE(1, 6, 0), CLK_CODE(1, 4, 1), CLK_CODE(2, 2, 1), -1,
56 static int g_clk_mux_consumer[16] = {
57 CLK_CODE(1, 4, 0), CLK_CODE(1, 3, 1), CLK_CODE(1, 3, 1), -1,
58 -1, -1, CLK_CODE(4, 1, 0), CLK_CODE(1, 5, 0),
59 CLK_CODE(1, 8, 1), CLK_CODE(1, 6, 1), CLK_CODE(2, 4, 0), -1,
60 -1, -1, CLK_CODE(4, 2, 0), -1,
63 static int hsp_div_table[3][16] = {
64 {4, 3, 2, -1, -1, -1, 1, 5, 4, 3, 2, -1, -1, -1, 1, -1},
65 {-1, -1, -1, -1, -1, -1, -1, -1, 8, 6, 4, -1, -1, -1, 2, -1},
66 {3, -1, -1, -1, -1, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1},
72 struct iim_regs *iim =
73 (struct iim_regs *)IIM_BASE_ADDR;
74 reg = readl(&iim->iim_srev);
76 reg = readw(ROMPATCH_REV);
82 return 0x35000 + (reg & 0xFF);
85 static u32 get_arm_div(u32 pdr0, u32 *fi, u32 *fd)
88 if (pdr0 & MXC_CCM_PDR0_AUTO_CON) {
89 pclk_mux = g_clk_mux_consumer +
90 ((pdr0 & MXC_CCM_PDR0_CON_MUX_DIV_MASK) >>
91 MXC_CCM_PDR0_CON_MUX_DIV_OFFSET);
93 pclk_mux = g_clk_mux_auto +
94 ((pdr0 & MXC_CCM_PDR0_AUTO_MUX_DIV_MASK) >>
95 MXC_CCM_PDR0_AUTO_MUX_DIV_OFFSET);
98 if ((*pclk_mux) == -1)
102 if (!CLK_CODE_PATH(*pclk_mux)) {
104 return CLK_CODE_ARM(*pclk_mux);
106 if (pdr0 & MXC_CCM_PDR0_AUTO_CON) {
114 return CLK_CODE_ARM(*pclk_mux);
117 static int get_ahb_div(u32 pdr0)
121 pclk_mux = g_clk_mux_consumer +
122 ((pdr0 & MXC_CCM_PDR0_CON_MUX_DIV_MASK) >>
123 MXC_CCM_PDR0_CON_MUX_DIV_OFFSET);
125 if ((*pclk_mux) == -1)
128 return CLK_CODE_AHB(*pclk_mux);
131 static u32 decode_pll(u32 reg, u32 infreq)
133 u32 mfi = (reg >> 10) & 0xf;
134 s32 mfn = reg & 0x3ff;
135 u32 mfd = (reg >> 16) & 0x3ff;
136 u32 pd = (reg >> 26) & 0xf;
138 mfi = mfi <= 5 ? 5 : mfi;
139 mfn = mfn >= 512 ? mfn - 1024 : mfn;
143 return lldiv(2 * (u64)infreq * (mfi * mfd + mfn),
147 static u32 get_mcu_main_clk(void)
149 u32 arm_div = 0, fi = 0, fd = 0;
150 struct ccm_regs *ccm =
151 (struct ccm_regs *)IMX_CCM_BASE;
152 arm_div = get_arm_div(readl(&ccm->pdr0), &fi, &fd);
153 fi *= decode_pll(readl(&ccm->mpctl), MXC_HCLK);
154 return fi / (arm_div * fd);
157 static u32 get_ipg_clk(void)
159 u32 freq = get_mcu_main_clk();
160 struct ccm_regs *ccm =
161 (struct ccm_regs *)IMX_CCM_BASE;
162 u32 pdr0 = readl(&ccm->pdr0);
164 return freq / (get_ahb_div(pdr0) * 2);
167 static u32 get_ipg_per_clk(void)
169 u32 freq = get_mcu_main_clk();
170 struct ccm_regs *ccm =
171 (struct ccm_regs *)IMX_CCM_BASE;
172 u32 pdr0 = readl(&ccm->pdr0);
173 u32 pdr4 = readl(&ccm->pdr4);
175 if (pdr0 & MXC_CCM_PDR0_PER_SEL) {
176 div = CCM_GET_DIVIDER(pdr4,
177 MXC_CCM_PDR4_PER0_PODF_MASK,
178 MXC_CCM_PDR4_PER0_PODF_OFFSET) + 1;
180 div = CCM_GET_DIVIDER(pdr0,
181 MXC_CCM_PDR0_PER_PODF_MASK,
182 MXC_CCM_PDR0_PER_PODF_OFFSET) + 1;
183 div *= get_ahb_div(pdr0);
188 u32 imx_get_uartclk(void)
191 struct ccm_regs *ccm =
192 (struct ccm_regs *)IMX_CCM_BASE;
193 u32 pdr4 = readl(&ccm->pdr4);
195 if (readl(&ccm->pdr3) & MXC_CCM_PDR3_UART_M_U)
196 freq = get_mcu_main_clk();
198 freq = decode_pll(readl(&ccm->ppctl), MXC_HCLK);
199 freq /= CCM_GET_DIVIDER(pdr4,
200 MXC_CCM_PDR4_UART_PODF_MASK,
201 MXC_CCM_PDR4_UART_PODF_OFFSET) + 1;
205 unsigned int mxc_get_main_clock(enum mxc_main_clock clk)
207 u32 nfc_pdf, hsp_podf;
208 u32 pll, ret_val = 0, usb_podf;
209 struct ccm_regs *ccm =
210 (struct ccm_regs *)IMX_CCM_BASE;
212 u32 reg = readl(&ccm->pdr0);
213 u32 reg4 = readl(&ccm->pdr4);
219 ret_val = get_mcu_main_clk();
222 ret_val = get_mcu_main_clk();
225 if (reg & CLKMODE_CONSUMER) {
226 hsp_podf = (reg >> 20) & 0x3;
227 pll = get_mcu_main_clk();
228 hsp_podf = hsp_div_table[hsp_podf][(reg>>16)&0xF];
230 ret_val = pll / hsp_podf;
232 puts("mismatch HSP with ARM clock setting\n");
236 ret_val = get_mcu_main_clk();
240 ret_val = get_ipg_clk();
243 ret_val = get_ipg_per_clk();
246 nfc_pdf = (reg4 >> 28) & 0xF;
247 pll = get_mcu_main_clk();
249 ret_val = pll / (nfc_pdf + 1);
252 usb_podf = (reg4 >> 22) & 0x3F;
254 pll = get_mcu_main_clk();
256 pll = decode_pll(readl(&ccm->ppctl), MXC_HCLK);
258 ret_val = pll / (usb_podf + 1);
261 printf("Unknown clock: %d\n", clk);
267 unsigned int mxc_get_peri_clock(enum mxc_peri_clock clk)
269 u32 ret_val = 0, pdf, pre_pdf, clk_sel;
270 struct ccm_regs *ccm =
271 (struct ccm_regs *)IMX_CCM_BASE;
272 u32 mpdr2 = readl(&ccm->pdr2);
273 u32 mpdr3 = readl(&ccm->pdr3);
274 u32 mpdr4 = readl(&ccm->pdr4);
280 clk_sel = mpdr3 & (1 << 14);
281 pdf = (mpdr4 >> 10) & 0x3F;
282 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
283 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) / (pdf + 1);
286 pre_pdf = (mpdr2 >> 24) & 0x7;
288 clk_sel = mpdr2 & (1 << 6);
289 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
290 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) /
291 ((pre_pdf + 1) * (pdf + 1));
294 pre_pdf = (mpdr2 >> 27) & 0x7;
295 pdf = (mpdr2 >> 8) & 0x3F;
296 clk_sel = mpdr2 & (1 << 6);
297 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
298 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) /
299 ((pre_pdf + 1) * (pdf + 1));
302 clk_sel = mpdr2 & (1 << 7);
303 pdf = (mpdr2 >> 16) & 0x3F;
304 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
305 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) / (pdf + 1);
308 pre_pdf = readl(&ccm->pdr1);
309 clk_sel = (pre_pdf & 0x80);
310 pdf = (pre_pdf >> 22) & 0x3F;
311 pre_pdf = (pre_pdf >> 28) & 0x7;
312 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
313 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) /
314 ((pre_pdf + 1) * (pdf + 1));
317 clk_sel = mpdr3 & 0x40;
319 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
320 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) / (pdf + 1);
323 clk_sel = mpdr3 & 0x40;
324 pdf = (mpdr3 >> 8) & 0x3F;
325 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
326 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) / (pdf + 1);
329 clk_sel = mpdr3 & 0x40;
330 pdf = (mpdr3 >> 16) & 0x3F;
331 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
332 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) / (pdf + 1);
335 clk_sel = mpdr3 & 0x400000;
336 pre_pdf = (mpdr3 >> 29) & 0x7;
337 pdf = (mpdr3 >> 23) & 0x3F;
338 ret_val = ((clk_sel != 0) ? mxc_get_main_clock(CPU_CLK) :
339 decode_pll(readl(&ccm->ppctl), MXC_HCLK)) /
340 ((pre_pdf + 1) * (pdf + 1));
343 printf("%s(): This clock: %d not supported yet\n",
351 unsigned int mxc_get_clock(enum mxc_clock clk)
355 return get_mcu_main_clk();
359 return get_ipg_clk();
362 return get_ipg_per_clk();
364 return imx_get_uartclk();
366 return mxc_get_peri_clock(ESDHC1_CLK);
368 return mxc_get_peri_clock(ESDHC2_CLK);
370 return mxc_get_peri_clock(ESDHC3_CLK);
372 return mxc_get_main_clock(USB_CLK);
374 return get_ipg_clk();
376 return get_ipg_clk();
381 #ifdef CONFIG_FEC_MXC
383 * The MX35 has no fuse for MAC, return a NULL MAC
385 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
390 u32 imx_get_fecclk(void)
392 return mxc_get_clock(MXC_IPG_CLK);
396 int do_mx35_showclocks(cmd_tbl_t *cmdtp,
397 int flag, int argc, char * const argv[])
399 u32 cpufreq = get_mcu_main_clk();
400 printf("mx35 cpu clock: %dMHz\n", cpufreq / 1000000);
401 printf("ipg clock : %dHz\n", get_ipg_clk());
402 printf("ipg per clock : %dHz\n", get_ipg_per_clk());
403 printf("uart clock : %dHz\n", mxc_get_clock(MXC_UART_CLK));
409 clocks, CONFIG_SYS_MAXARGS, 1, do_mx35_showclocks,
414 #if defined(CONFIG_DISPLAY_CPUINFO)
415 static char *get_reset_cause(void)
417 /* read RCSR register from CCM module */
418 struct ccm_regs *ccm =
419 (struct ccm_regs *)IMX_CCM_BASE;
421 u32 cause = readl(&ccm->rcsr) & 0x0F;
433 return "unknown reset";
437 int print_cpuinfo(void)
439 u32 srev = get_cpu_rev();
441 printf("CPU: Freescale i.MX35 rev %d.%d at %d MHz.\n",
442 (srev & 0xF0) >> 4, (srev & 0x0F),
443 get_mcu_main_clk() / 1000000);
445 printf("Reset cause: %s\n", get_reset_cause());
452 * Initializes on-chip ethernet controllers.
453 * to override, implement board_eth_init()
455 int cpu_eth_init(bd_t *bis)
459 #if defined(CONFIG_FEC_MXC)
460 rc = fecmxc_initialize(bis);
466 #ifdef CONFIG_FSL_ESDHC
468 * Initializes on-chip MMC controllers.
469 * to override, implement board_mmc_init()
471 int cpu_mmc_init(bd_t *bis)
473 return fsl_esdhc_mmc_init(bis);
479 #ifdef CONFIG_FSL_ESDHC
480 #if CONFIG_SYS_FSL_ESDHC_ADDR == MMC_SDHC2_BASE_ADDR
481 gd->sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
482 #elif CONFIG_SYS_FSL_ESDHC_ADDR == MMC_SDHC3_BASE_ADDR
483 gd->sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
485 gd->sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
491 #define RCSR_MEM_CTL_WEIM 0
492 #define RCSR_MEM_CTL_NAND 1
493 #define RCSR_MEM_CTL_ATA 2
494 #define RCSR_MEM_CTL_EXPANSION 3
495 #define RCSR_MEM_TYPE_NOR 0
496 #define RCSR_MEM_TYPE_ONENAND 2
497 #define RCSR_MEM_TYPE_SD 0
498 #define RCSR_MEM_TYPE_I2C 2
499 #define RCSR_MEM_TYPE_SPI 3
501 u32 spl_boot_device(void)
503 struct ccm_regs *ccm =
504 (struct ccm_regs *)IMX_CCM_BASE;
506 u32 rcsr = readl(&ccm->rcsr);
507 u32 mem_type, mem_ctl;
509 /* In external mode, no boot device is returned */
510 if ((rcsr >> 10) & 0x03)
511 return BOOT_DEVICE_NONE;
513 mem_ctl = (rcsr >> 25) & 0x03;
514 mem_type = (rcsr >> 23) & 0x03;
517 case RCSR_MEM_CTL_WEIM:
519 case RCSR_MEM_TYPE_NOR:
520 return BOOT_DEVICE_NOR;
521 case RCSR_MEM_TYPE_ONENAND:
522 return BOOT_DEVICE_ONE_NAND;
524 return BOOT_DEVICE_NONE;
526 case RCSR_MEM_CTL_NAND:
527 return BOOT_DEVICE_NAND;
528 case RCSR_MEM_CTL_EXPANSION:
530 case RCSR_MEM_TYPE_SD:
531 return BOOT_DEVICE_MMC1;
532 case RCSR_MEM_TYPE_I2C:
533 return BOOT_DEVICE_I2C;
534 case RCSR_MEM_TYPE_SPI:
535 return BOOT_DEVICE_SPI;
537 return BOOT_DEVICE_NONE;
541 return BOOT_DEVICE_NONE;
544 #ifdef CONFIG_SPL_BUILD
545 u32 spl_boot_mode(void)
547 switch (spl_boot_device()) {
548 case BOOT_DEVICE_MMC1:
549 #ifdef CONFIG_SPL_FAT_SUPPORT
550 return MMCSD_MODE_FAT;
552 return MMCSD_MODE_RAW;
555 case BOOT_DEVICE_NAND:
559 puts("spl: ERROR: unsupported device\n");