blob: 4e9c0b548119d754b0b4b633277ea8e84a62410c [file] [log] [blame]
John Rigby9c146032010-01-25 23:12:56 -07001/*
2 * (C) Copyright 2009 DENX Software Engineering
3 * Author: John Rigby <jrigby@gmail.com>
4 *
5 * Based on mx27/generic.c:
6 * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org>
7 * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com>
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
John Rigby9c146032010-01-25 23:12:56 -070010 */
11
12#include <common.h>
13#include <div64.h>
14#include <netdev.h>
15#include <asm/io.h>
16#include <asm/arch/imx-regs.h>
Timo Ketola738fa8d2012-04-18 22:55:28 +000017#include <asm/arch/clock.h>
John Rigby9c146032010-01-25 23:12:56 -070018
Timo Ketola738fa8d2012-04-18 22:55:28 +000019#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +000020#include <fsl_esdhc.h>
21
Timo Ketola738fa8d2012-04-18 22:55:28 +000022DECLARE_GLOBAL_DATA_PTR;
23#endif
24
John Rigby9c146032010-01-25 23:12:56 -070025/*
26 * get the system pll clock in Hz
27 *
28 * mfi + mfn / (mfd +1)
29 * f = 2 * f_ref * --------------------
30 * pd + 1
31 */
Fabio Estevamf231efb2011-10-13 05:34:59 +000032static unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
John Rigby9c146032010-01-25 23:12:56 -070033{
34 unsigned int mfi = (pll >> CCM_PLL_MFI_SHIFT)
35 & CCM_PLL_MFI_MASK;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000036 int mfn = (pll >> CCM_PLL_MFN_SHIFT)
John Rigby9c146032010-01-25 23:12:56 -070037 & CCM_PLL_MFN_MASK;
38 unsigned int mfd = (pll >> CCM_PLL_MFD_SHIFT)
39 & CCM_PLL_MFD_MASK;
40 unsigned int pd = (pll >> CCM_PLL_PD_SHIFT)
41 & CCM_PLL_PD_MASK;
42
43 mfi = mfi <= 5 ? 5 : mfi;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000044 mfn = mfn >= 512 ? mfn - 1024 : mfn;
45 mfd += 1;
46 pd += 1;
John Rigby9c146032010-01-25 23:12:56 -070047
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000048 return lldiv(2 * (u64) f_ref * (mfi * mfd + mfn),
49 mfd * pd);
John Rigby9c146032010-01-25 23:12:56 -070050}
51
Fabio Estevamf231efb2011-10-13 05:34:59 +000052static ulong imx_get_mpllclk(void)
John Rigby9c146032010-01-25 23:12:56 -070053{
54 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000055 ulong fref = MXC_HCLK;
John Rigby9c146032010-01-25 23:12:56 -070056
Fabio Estevamf231efb2011-10-13 05:34:59 +000057 return imx_decode_pll(readl(&ccm->mpctl), fref);
John Rigby9c146032010-01-25 23:12:56 -070058}
59
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000060static ulong imx_get_armclk(void)
John Rigby9c146032010-01-25 23:12:56 -070061{
62 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000063 ulong cctl = readl(&ccm->cctl);
64 ulong fref = imx_get_mpllclk();
John Rigby9c146032010-01-25 23:12:56 -070065 ulong div;
66
67 if (cctl & CCM_CCTL_ARM_SRC)
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000068 fref = lldiv((u64) fref * 3, 4);
John Rigby9c146032010-01-25 23:12:56 -070069
70 div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT)
71 & CCM_CCTL_ARM_DIV_MASK) + 1;
72
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000073 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070074}
75
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000076static ulong imx_get_ahbclk(void)
John Rigby9c146032010-01-25 23:12:56 -070077{
78 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000079 ulong cctl = readl(&ccm->cctl);
80 ulong fref = imx_get_armclk();
John Rigby9c146032010-01-25 23:12:56 -070081 ulong div;
82
83 div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT)
84 & CCM_CCTL_AHB_DIV_MASK) + 1;
85
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000086 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070087}
88
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +000089static ulong imx_get_ipgclk(void)
90{
91 return imx_get_ahbclk() / 2;
92}
93
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000094static ulong imx_get_perclk(int clk)
John Rigby9c146032010-01-25 23:12:56 -070095{
96 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000097 ulong fref = imx_get_ahbclk();
John Rigby9c146032010-01-25 23:12:56 -070098 ulong div;
99
Fabio Estevamf231efb2011-10-13 05:34:59 +0000100 div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]);
101 div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1;
John Rigby9c146032010-01-25 23:12:56 -0700102
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +0000103 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -0700104}
105
Timo Ketola738fa8d2012-04-18 22:55:28 +0000106unsigned int mxc_get_clock(enum mxc_clock clk)
107{
108 if (clk >= MXC_CLK_NUM)
109 return -1;
110 switch (clk) {
111 case MXC_ARM_CLK:
112 return imx_get_armclk();
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +0000113 case MXC_AHB_CLK:
114 return imx_get_ahbclk();
115 case MXC_IPG_CLK:
116 case MXC_CSPI_CLK:
Timo Ketola738fa8d2012-04-18 22:55:28 +0000117 case MXC_FEC_CLK:
Benoît Thébaudeau88a23822012-09-27 10:27:44 +0000118 return imx_get_ipgclk();
Timo Ketola738fa8d2012-04-18 22:55:28 +0000119 default:
120 return imx_get_perclk(clk);
121 }
122}
123
Fabio Estevam51f23542011-09-02 05:38:54 +0000124u32 get_cpu_rev(void)
125{
126 u32 srev;
127 u32 system_rev = 0x25000;
128
129 /* read SREV register from IIM module */
130 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
131 srev = readl(&iim->iim_srev);
132
133 switch (srev) {
134 case 0x00:
135 system_rev |= CHIP_REV_1_0;
136 break;
137 case 0x01:
138 system_rev |= CHIP_REV_1_1;
139 break;
Eric Benardc47d73f2012-09-23 02:03:05 +0000140 case 0x02:
141 system_rev |= CHIP_REV_1_2;
142 break;
Fabio Estevam51f23542011-09-02 05:38:54 +0000143 default:
144 system_rev |= 0x8000;
145 break;
146 }
147
148 return system_rev;
149}
150
John Rigby9c146032010-01-25 23:12:56 -0700151#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam4c6b02e2011-09-23 05:13:22 +0000152static char *get_reset_cause(void)
153{
154 /* read RCSR register from CCM module */
155 struct ccm_regs *ccm =
156 (struct ccm_regs *)IMX_CCM_BASE;
157
158 u32 cause = readl(&ccm->rcsr) & 0x0f;
159
160 if (cause == 0)
161 return "POR";
162 else if (cause == 1)
163 return "RST";
164 else if ((cause & 2) == 2)
165 return "WDOG";
166 else if ((cause & 4) == 4)
167 return "SW RESET";
168 else if ((cause & 8) == 8)
169 return "JTAG";
170 else
171 return "unknown reset";
172
173}
174
Fabio Estevamf231efb2011-10-13 05:34:59 +0000175int print_cpuinfo(void)
John Rigby9c146032010-01-25 23:12:56 -0700176{
177 char buf[32];
Fabio Estevam51f23542011-09-02 05:38:54 +0000178 u32 cpurev = get_cpu_rev();
John Rigby9c146032010-01-25 23:12:56 -0700179
Fabio Estevam9a423242011-09-02 05:38:55 +0000180 printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n",
Fabio Estevam51f23542011-09-02 05:38:54 +0000181 (cpurev & 0xF0) >> 4, (cpurev & 0x0F),
182 ((cpurev & 0x8000) ? " unknown" : ""),
Fabio Estevamf231efb2011-10-13 05:34:59 +0000183 strmhz(buf, imx_get_armclk()));
Fabio Estevam9a423242011-09-02 05:38:55 +0000184 printf("Reset cause: %s\n\n", get_reset_cause());
John Rigby9c146032010-01-25 23:12:56 -0700185 return 0;
186}
187#endif
188
Benoît Thébaudeau463b6852012-08-14 03:17:33 +0000189void enable_caches(void)
190{
191#ifndef CONFIG_SYS_DCACHE_OFF
192 /* Enable D-cache. I-cache is already enabled in start.S */
193 dcache_enable();
194#endif
195}
196
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000197#if defined(CONFIG_FEC_MXC)
198/*
199 * Initializes on-chip ethernet controllers.
200 * to override, implement board_eth_init()
201 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000202int cpu_eth_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700203{
John Rigby9c146032010-01-25 23:12:56 -0700204 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
205 ulong val;
206
Fabio Estevamf231efb2011-10-13 05:34:59 +0000207 val = readl(&ccm->cgr0);
John Rigby9c146032010-01-25 23:12:56 -0700208 val |= (1 << 23);
Fabio Estevamf231efb2011-10-13 05:34:59 +0000209 writel(val, &ccm->cgr0);
210 return fecmxc_initialize(bis);
Timo Ketola738fa8d2012-04-18 22:55:28 +0000211}
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000212#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000213
214int get_clocks(void)
215{
216#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000217#if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE
Simon Glass9e247d12012-12-13 20:49:05 +0000218 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000219#else
Simon Glass9e247d12012-12-13 20:49:05 +0000220 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000221#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000222#endif
223 return 0;
John Rigby9c146032010-01-25 23:12:56 -0700224}
225
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000226#ifdef CONFIG_FSL_ESDHC
John Rigby9c146032010-01-25 23:12:56 -0700227/*
228 * Initializes on-chip MMC controllers.
229 * to override, implement board_mmc_init()
230 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000231int cpu_mmc_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700232{
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000233 return fsl_esdhc_mmc_init(bis);
John Rigby9c146032010-01-25 23:12:56 -0700234}
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000235#endif
John Rigby9c146032010-01-25 23:12:56 -0700236
John Rigby9c146032010-01-25 23:12:56 -0700237#ifdef CONFIG_FEC_MXC
Fabio Estevam04fc1282011-12-20 05:46:31 +0000238void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
Liu Hui-R643434df66192010-11-18 23:45:55 +0000239{
240 int i;
241 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
242 struct fuse_bank *bank = &iim->bank[0];
243 struct fuse_bank0_regs *fuse =
244 (struct fuse_bank0_regs *)bank->fuse_regs;
245
246 for (i = 0; i < 6; i++)
247 mac[i] = readl(&fuse->mac_addr[i]) & 0xff;
248}
John Rigby9c146032010-01-25 23:12:56 -0700249#endif /* CONFIG_FEC_MXC */