blob: 0b1a8f4bbc2ad1bcb319e2ef45a477b8bd9cc494 [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>
Adrian Alonsoa7209a22015-10-12 13:48:07 -050016#include <asm/arch-imx/cpu.h>
John Rigby9c146032010-01-25 23:12:56 -070017#include <asm/arch/imx-regs.h>
Timo Ketola738fa8d2012-04-18 22:55:28 +000018#include <asm/arch/clock.h>
John Rigby9c146032010-01-25 23:12:56 -070019
Timo Ketola738fa8d2012-04-18 22:55:28 +000020#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +000021#include <fsl_esdhc.h>
22
Timo Ketola738fa8d2012-04-18 22:55:28 +000023DECLARE_GLOBAL_DATA_PTR;
24#endif
25
John Rigby9c146032010-01-25 23:12:56 -070026/*
27 * get the system pll clock in Hz
28 *
29 * mfi + mfn / (mfd +1)
30 * f = 2 * f_ref * --------------------
31 * pd + 1
32 */
Fabio Estevamf231efb2011-10-13 05:34:59 +000033static unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
John Rigby9c146032010-01-25 23:12:56 -070034{
35 unsigned int mfi = (pll >> CCM_PLL_MFI_SHIFT)
36 & CCM_PLL_MFI_MASK;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000037 int mfn = (pll >> CCM_PLL_MFN_SHIFT)
John Rigby9c146032010-01-25 23:12:56 -070038 & CCM_PLL_MFN_MASK;
39 unsigned int mfd = (pll >> CCM_PLL_MFD_SHIFT)
40 & CCM_PLL_MFD_MASK;
41 unsigned int pd = (pll >> CCM_PLL_PD_SHIFT)
42 & CCM_PLL_PD_MASK;
43
44 mfi = mfi <= 5 ? 5 : mfi;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000045 mfn = mfn >= 512 ? mfn - 1024 : mfn;
46 mfd += 1;
47 pd += 1;
John Rigby9c146032010-01-25 23:12:56 -070048
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000049 return lldiv(2 * (u64) f_ref * (mfi * mfd + mfn),
50 mfd * pd);
John Rigby9c146032010-01-25 23:12:56 -070051}
52
Fabio Estevamf231efb2011-10-13 05:34:59 +000053static ulong imx_get_mpllclk(void)
John Rigby9c146032010-01-25 23:12:56 -070054{
55 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000056 ulong fref = MXC_HCLK;
John Rigby9c146032010-01-25 23:12:56 -070057
Fabio Estevamf231efb2011-10-13 05:34:59 +000058 return imx_decode_pll(readl(&ccm->mpctl), fref);
John Rigby9c146032010-01-25 23:12:56 -070059}
60
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000061static ulong imx_get_armclk(void)
John Rigby9c146032010-01-25 23:12:56 -070062{
63 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000064 ulong cctl = readl(&ccm->cctl);
65 ulong fref = imx_get_mpllclk();
John Rigby9c146032010-01-25 23:12:56 -070066 ulong div;
67
68 if (cctl & CCM_CCTL_ARM_SRC)
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000069 fref = lldiv((u64) fref * 3, 4);
John Rigby9c146032010-01-25 23:12:56 -070070
71 div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT)
72 & CCM_CCTL_ARM_DIV_MASK) + 1;
73
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000074 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070075}
76
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000077static ulong imx_get_ahbclk(void)
John Rigby9c146032010-01-25 23:12:56 -070078{
79 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000080 ulong cctl = readl(&ccm->cctl);
81 ulong fref = imx_get_armclk();
John Rigby9c146032010-01-25 23:12:56 -070082 ulong div;
83
84 div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT)
85 & CCM_CCTL_AHB_DIV_MASK) + 1;
86
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000087 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070088}
89
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +000090static ulong imx_get_ipgclk(void)
91{
92 return imx_get_ahbclk() / 2;
93}
94
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000095static ulong imx_get_perclk(int clk)
John Rigby9c146032010-01-25 23:12:56 -070096{
97 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000098 ulong fref = imx_get_ahbclk();
John Rigby9c146032010-01-25 23:12:56 -070099 ulong div;
100
Fabio Estevamf231efb2011-10-13 05:34:59 +0000101 div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]);
102 div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1;
John Rigby9c146032010-01-25 23:12:56 -0700103
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +0000104 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -0700105}
106
Timo Ketola738fa8d2012-04-18 22:55:28 +0000107unsigned int mxc_get_clock(enum mxc_clock clk)
108{
109 if (clk >= MXC_CLK_NUM)
110 return -1;
111 switch (clk) {
112 case MXC_ARM_CLK:
113 return imx_get_armclk();
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +0000114 case MXC_AHB_CLK:
115 return imx_get_ahbclk();
116 case MXC_IPG_CLK:
117 case MXC_CSPI_CLK:
Timo Ketola738fa8d2012-04-18 22:55:28 +0000118 case MXC_FEC_CLK:
Benoît Thébaudeau88a23822012-09-27 10:27:44 +0000119 return imx_get_ipgclk();
Timo Ketola738fa8d2012-04-18 22:55:28 +0000120 default:
121 return imx_get_perclk(clk);
122 }
123}
124
Fabio Estevam51f23542011-09-02 05:38:54 +0000125u32 get_cpu_rev(void)
126{
127 u32 srev;
128 u32 system_rev = 0x25000;
129
130 /* read SREV register from IIM module */
131 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
132 srev = readl(&iim->iim_srev);
133
134 switch (srev) {
135 case 0x00:
136 system_rev |= CHIP_REV_1_0;
137 break;
138 case 0x01:
139 system_rev |= CHIP_REV_1_1;
140 break;
Eric Benardc47d73f2012-09-23 02:03:05 +0000141 case 0x02:
142 system_rev |= CHIP_REV_1_2;
143 break;
Fabio Estevam51f23542011-09-02 05:38:54 +0000144 default:
145 system_rev |= 0x8000;
146 break;
147 }
148
149 return system_rev;
150}
151
John Rigby9c146032010-01-25 23:12:56 -0700152#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam4c6b02e2011-09-23 05:13:22 +0000153static char *get_reset_cause(void)
154{
155 /* read RCSR register from CCM module */
156 struct ccm_regs *ccm =
157 (struct ccm_regs *)IMX_CCM_BASE;
158
159 u32 cause = readl(&ccm->rcsr) & 0x0f;
160
161 if (cause == 0)
162 return "POR";
163 else if (cause == 1)
164 return "RST";
165 else if ((cause & 2) == 2)
166 return "WDOG";
167 else if ((cause & 4) == 4)
168 return "SW RESET";
169 else if ((cause & 8) == 8)
170 return "JTAG";
171 else
172 return "unknown reset";
173
174}
175
Fabio Estevamf231efb2011-10-13 05:34:59 +0000176int print_cpuinfo(void)
John Rigby9c146032010-01-25 23:12:56 -0700177{
178 char buf[32];
Fabio Estevam51f23542011-09-02 05:38:54 +0000179 u32 cpurev = get_cpu_rev();
John Rigby9c146032010-01-25 23:12:56 -0700180
Fabio Estevam9a423242011-09-02 05:38:55 +0000181 printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n",
Fabio Estevam51f23542011-09-02 05:38:54 +0000182 (cpurev & 0xF0) >> 4, (cpurev & 0x0F),
183 ((cpurev & 0x8000) ? " unknown" : ""),
Fabio Estevamf231efb2011-10-13 05:34:59 +0000184 strmhz(buf, imx_get_armclk()));
Fabio Estevam9882e202015-01-06 14:10:05 -0200185 printf("Reset cause: %s\n", get_reset_cause());
John Rigby9c146032010-01-25 23:12:56 -0700186 return 0;
187}
188#endif
189
Benoît Thébaudeau463b6852012-08-14 03:17:33 +0000190void enable_caches(void)
191{
192#ifndef CONFIG_SYS_DCACHE_OFF
193 /* Enable D-cache. I-cache is already enabled in start.S */
194 dcache_enable();
195#endif
196}
197
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000198#if defined(CONFIG_FEC_MXC)
199/*
200 * Initializes on-chip ethernet controllers.
201 * to override, implement board_eth_init()
202 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000203int cpu_eth_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700204{
John Rigby9c146032010-01-25 23:12:56 -0700205 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
206 ulong val;
207
Fabio Estevamf231efb2011-10-13 05:34:59 +0000208 val = readl(&ccm->cgr0);
John Rigby9c146032010-01-25 23:12:56 -0700209 val |= (1 << 23);
Fabio Estevamf231efb2011-10-13 05:34:59 +0000210 writel(val, &ccm->cgr0);
211 return fecmxc_initialize(bis);
Timo Ketola738fa8d2012-04-18 22:55:28 +0000212}
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000213#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000214
215int get_clocks(void)
216{
217#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000218#if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE
Simon Glass9e247d12012-12-13 20:49:05 +0000219 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000220#else
Simon Glass9e247d12012-12-13 20:49:05 +0000221 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000222#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000223#endif
224 return 0;
John Rigby9c146032010-01-25 23:12:56 -0700225}
226
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000227#ifdef CONFIG_FSL_ESDHC
John Rigby9c146032010-01-25 23:12:56 -0700228/*
229 * Initializes on-chip MMC controllers.
230 * to override, implement board_mmc_init()
231 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000232int cpu_mmc_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700233{
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000234 return fsl_esdhc_mmc_init(bis);
John Rigby9c146032010-01-25 23:12:56 -0700235}
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000236#endif
John Rigby9c146032010-01-25 23:12:56 -0700237
John Rigby9c146032010-01-25 23:12:56 -0700238#ifdef CONFIG_FEC_MXC
Fabio Estevam04fc1282011-12-20 05:46:31 +0000239void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
Liu Hui-R643434df66192010-11-18 23:45:55 +0000240{
241 int i;
242 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
243 struct fuse_bank *bank = &iim->bank[0];
244 struct fuse_bank0_regs *fuse =
245 (struct fuse_bank0_regs *)bank->fuse_regs;
246
247 for (i = 0; i < 6; i++)
248 mac[i] = readl(&fuse->mac_addr[i]) & 0xff;
249}
John Rigby9c146032010-01-25 23:12:56 -0700250#endif /* CONFIG_FEC_MXC */