blob: eeb61d0d102028ee57887f32957a19ed61a8a119 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
John Rigby9c146032010-01-25 23:12:56 -07002/*
3 * (C) Copyright 2009 DENX Software Engineering
4 * Author: John Rigby <jrigby@gmail.com>
5 *
6 * Based on mx27/generic.c:
7 * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org>
8 * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com>
John Rigby9c146032010-01-25 23:12:56 -07009 */
10
11#include <common.h>
12#include <div64.h>
13#include <netdev.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070014#include <vsprintf.h>
John Rigby9c146032010-01-25 23:12:56 -070015#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
Yangbo Lu73340382019-06-21 11:42:28 +080020#ifdef CONFIG_FSL_ESDHC_IMX
21#include <fsl_esdhc_imx.h>
Benoît Thébaudeau95646052012-09-27 10:28:29 +000022
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ébaudeau9f2aa982017-05-03 11:59:04 +020061static ulong imx_get_upllclk(void)
62{
63 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
64 ulong fref = MXC_HCLK;
65
66 return imx_decode_pll(readl(&ccm->upctl), fref);
67}
68
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000069static ulong imx_get_armclk(void)
John Rigby9c146032010-01-25 23:12:56 -070070{
71 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000072 ulong cctl = readl(&ccm->cctl);
73 ulong fref = imx_get_mpllclk();
John Rigby9c146032010-01-25 23:12:56 -070074 ulong div;
75
76 if (cctl & CCM_CCTL_ARM_SRC)
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000077 fref = lldiv((u64) fref * 3, 4);
John Rigby9c146032010-01-25 23:12:56 -070078
79 div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT)
80 & CCM_CCTL_ARM_DIV_MASK) + 1;
81
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000082 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070083}
84
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000085static ulong imx_get_ahbclk(void)
John Rigby9c146032010-01-25 23:12:56 -070086{
87 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000088 ulong cctl = readl(&ccm->cctl);
89 ulong fref = imx_get_armclk();
John Rigby9c146032010-01-25 23:12:56 -070090 ulong div;
91
92 div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT)
93 & CCM_CCTL_AHB_DIV_MASK) + 1;
94
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000095 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -070096}
97
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +000098static ulong imx_get_ipgclk(void)
99{
100 return imx_get_ahbclk() / 2;
101}
102
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +0000103static ulong imx_get_perclk(int clk)
John Rigby9c146032010-01-25 23:12:56 -0700104{
105 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Benoît Thébaudeau9f2aa982017-05-03 11:59:04 +0200106 ulong fref = readl(&ccm->mcr) & (1 << clk) ? imx_get_upllclk() :
107 imx_get_ahbclk();
John Rigby9c146032010-01-25 23:12:56 -0700108 ulong div;
109
Fabio Estevamf231efb2011-10-13 05:34:59 +0000110 div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]);
111 div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1;
John Rigby9c146032010-01-25 23:12:56 -0700112
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +0000113 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -0700114}
115
Benoît Thébaudeau9d694242017-05-03 11:59:05 +0200116int imx_set_perclk(enum mxc_clock clk, bool from_upll, unsigned int freq)
117{
118 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
119 ulong fref = from_upll ? imx_get_upllclk() : imx_get_ahbclk();
120 ulong div = (fref + freq - 1) / freq;
121
122 if (clk > MXC_UART_CLK || !div || --div > CCM_PERCLK_MASK)
123 return -EINVAL;
124
125 clrsetbits_le32(&ccm->pcdr[CCM_PERCLK_REG(clk)],
126 CCM_PERCLK_MASK << CCM_PERCLK_SHIFT(clk),
127 div << CCM_PERCLK_SHIFT(clk));
128 if (from_upll)
129 setbits_le32(&ccm->mcr, 1 << clk);
130 else
131 clrbits_le32(&ccm->mcr, 1 << clk);
132 return 0;
133}
134
Timo Ketola738fa8d2012-04-18 22:55:28 +0000135unsigned int mxc_get_clock(enum mxc_clock clk)
136{
137 if (clk >= MXC_CLK_NUM)
138 return -1;
139 switch (clk) {
140 case MXC_ARM_CLK:
141 return imx_get_armclk();
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +0000142 case MXC_AHB_CLK:
143 return imx_get_ahbclk();
144 case MXC_IPG_CLK:
145 case MXC_CSPI_CLK:
Timo Ketola738fa8d2012-04-18 22:55:28 +0000146 case MXC_FEC_CLK:
Benoît Thébaudeau88a23822012-09-27 10:27:44 +0000147 return imx_get_ipgclk();
Timo Ketola738fa8d2012-04-18 22:55:28 +0000148 default:
149 return imx_get_perclk(clk);
150 }
151}
152
Fabio Estevam51f23542011-09-02 05:38:54 +0000153u32 get_cpu_rev(void)
154{
155 u32 srev;
156 u32 system_rev = 0x25000;
157
158 /* read SREV register from IIM module */
159 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
160 srev = readl(&iim->iim_srev);
161
162 switch (srev) {
163 case 0x00:
164 system_rev |= CHIP_REV_1_0;
165 break;
166 case 0x01:
167 system_rev |= CHIP_REV_1_1;
168 break;
Eric Benardc47d73f2012-09-23 02:03:05 +0000169 case 0x02:
170 system_rev |= CHIP_REV_1_2;
171 break;
Fabio Estevam51f23542011-09-02 05:38:54 +0000172 default:
173 system_rev |= 0x8000;
174 break;
175 }
176
177 return system_rev;
178}
179
John Rigby9c146032010-01-25 23:12:56 -0700180#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam4c6b02e2011-09-23 05:13:22 +0000181static char *get_reset_cause(void)
182{
183 /* read RCSR register from CCM module */
184 struct ccm_regs *ccm =
185 (struct ccm_regs *)IMX_CCM_BASE;
186
187 u32 cause = readl(&ccm->rcsr) & 0x0f;
188
189 if (cause == 0)
190 return "POR";
191 else if (cause == 1)
192 return "RST";
193 else if ((cause & 2) == 2)
194 return "WDOG";
195 else if ((cause & 4) == 4)
196 return "SW RESET";
197 else if ((cause & 8) == 8)
198 return "JTAG";
199 else
200 return "unknown reset";
201
202}
203
Fabio Estevamf231efb2011-10-13 05:34:59 +0000204int print_cpuinfo(void)
John Rigby9c146032010-01-25 23:12:56 -0700205{
206 char buf[32];
Fabio Estevam51f23542011-09-02 05:38:54 +0000207 u32 cpurev = get_cpu_rev();
John Rigby9c146032010-01-25 23:12:56 -0700208
Fabio Estevam9a423242011-09-02 05:38:55 +0000209 printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n",
Fabio Estevam51f23542011-09-02 05:38:54 +0000210 (cpurev & 0xF0) >> 4, (cpurev & 0x0F),
211 ((cpurev & 0x8000) ? " unknown" : ""),
Fabio Estevamf231efb2011-10-13 05:34:59 +0000212 strmhz(buf, imx_get_armclk()));
Fabio Estevam9882e202015-01-06 14:10:05 -0200213 printf("Reset cause: %s\n", get_reset_cause());
John Rigby9c146032010-01-25 23:12:56 -0700214 return 0;
215}
216#endif
217
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000218#if defined(CONFIG_FEC_MXC)
219/*
220 * Initializes on-chip ethernet controllers.
221 * to override, implement board_eth_init()
222 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000223int cpu_eth_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700224{
John Rigby9c146032010-01-25 23:12:56 -0700225 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
226 ulong val;
227
Fabio Estevamf231efb2011-10-13 05:34:59 +0000228 val = readl(&ccm->cgr0);
John Rigby9c146032010-01-25 23:12:56 -0700229 val |= (1 << 23);
Fabio Estevamf231efb2011-10-13 05:34:59 +0000230 writel(val, &ccm->cgr0);
231 return fecmxc_initialize(bis);
Timo Ketola738fa8d2012-04-18 22:55:28 +0000232}
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000233#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000234
235int get_clocks(void)
236{
Yangbo Lu73340382019-06-21 11:42:28 +0800237#ifdef CONFIG_FSL_ESDHC_IMX
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000238#if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE
Simon Glass9e247d12012-12-13 20:49:05 +0000239 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000240#else
Simon Glass9e247d12012-12-13 20:49:05 +0000241 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000242#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000243#endif
244 return 0;
John Rigby9c146032010-01-25 23:12:56 -0700245}
246
Yangbo Lu73340382019-06-21 11:42:28 +0800247#ifdef CONFIG_FSL_ESDHC_IMX
John Rigby9c146032010-01-25 23:12:56 -0700248/*
249 * Initializes on-chip MMC controllers.
250 * to override, implement board_mmc_init()
251 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000252int cpu_mmc_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700253{
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000254 return fsl_esdhc_mmc_init(bis);
John Rigby9c146032010-01-25 23:12:56 -0700255}
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000256#endif
John Rigby9c146032010-01-25 23:12:56 -0700257
John Rigby9c146032010-01-25 23:12:56 -0700258#ifdef CONFIG_FEC_MXC
Fabio Estevam04fc1282011-12-20 05:46:31 +0000259void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
Liu Hui-R643434df66192010-11-18 23:45:55 +0000260{
261 int i;
262 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
263 struct fuse_bank *bank = &iim->bank[0];
264 struct fuse_bank0_regs *fuse =
265 (struct fuse_bank0_regs *)bank->fuse_regs;
266
267 for (i = 0; i < 6; i++)
268 mac[i] = readl(&fuse->mac_addr[i]) & 0xff;
269}
John Rigby9c146032010-01-25 23:12:56 -0700270#endif /* CONFIG_FEC_MXC */