blob: b9914186b19bad91dbd9202eefc54b38573cbcf1 [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 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <div64.h>
27#include <netdev.h>
28#include <asm/io.h>
29#include <asm/arch/imx-regs.h>
30#include <asm/arch/imx25-pinmux.h>
Timo Ketola738fa8d2012-04-18 22:55:28 +000031#include <asm/arch/clock.h>
John Rigby9c146032010-01-25 23:12:56 -070032
Timo Ketola738fa8d2012-04-18 22:55:28 +000033#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +000034#include <fsl_esdhc.h>
35
Timo Ketola738fa8d2012-04-18 22:55:28 +000036DECLARE_GLOBAL_DATA_PTR;
37#endif
38
John Rigby9c146032010-01-25 23:12:56 -070039/*
40 * get the system pll clock in Hz
41 *
42 * mfi + mfn / (mfd +1)
43 * f = 2 * f_ref * --------------------
44 * pd + 1
45 */
Fabio Estevamf231efb2011-10-13 05:34:59 +000046static unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
John Rigby9c146032010-01-25 23:12:56 -070047{
48 unsigned int mfi = (pll >> CCM_PLL_MFI_SHIFT)
49 & CCM_PLL_MFI_MASK;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000050 int mfn = (pll >> CCM_PLL_MFN_SHIFT)
John Rigby9c146032010-01-25 23:12:56 -070051 & CCM_PLL_MFN_MASK;
52 unsigned int mfd = (pll >> CCM_PLL_MFD_SHIFT)
53 & CCM_PLL_MFD_MASK;
54 unsigned int pd = (pll >> CCM_PLL_PD_SHIFT)
55 & CCM_PLL_PD_MASK;
56
57 mfi = mfi <= 5 ? 5 : mfi;
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000058 mfn = mfn >= 512 ? mfn - 1024 : mfn;
59 mfd += 1;
60 pd += 1;
John Rigby9c146032010-01-25 23:12:56 -070061
Benoît Thébaudeauaa1cf2f2012-09-27 10:26:54 +000062 return lldiv(2 * (u64) f_ref * (mfi * mfd + mfn),
63 mfd * pd);
John Rigby9c146032010-01-25 23:12:56 -070064}
65
Fabio Estevamf231efb2011-10-13 05:34:59 +000066static ulong imx_get_mpllclk(void)
John Rigby9c146032010-01-25 23:12:56 -070067{
68 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Benoît Thébaudeaud2dd29d2012-08-21 11:05:12 +000069 ulong fref = MXC_HCLK;
John Rigby9c146032010-01-25 23:12:56 -070070
Fabio Estevamf231efb2011-10-13 05:34:59 +000071 return imx_decode_pll(readl(&ccm->mpctl), fref);
John Rigby9c146032010-01-25 23:12:56 -070072}
73
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +000074static ulong imx_get_armclk(void)
John Rigby9c146032010-01-25 23:12:56 -070075{
76 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000077 ulong cctl = readl(&ccm->cctl);
78 ulong fref = imx_get_mpllclk();
John Rigby9c146032010-01-25 23:12:56 -070079 ulong div;
80
81 if (cctl & CCM_CCTL_ARM_SRC)
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +000082 fref = lldiv((u64) fref * 3, 4);
John Rigby9c146032010-01-25 23:12:56 -070083
84 div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT)
85 & CCM_CCTL_ARM_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ébaudeaub3ab1392012-09-27 10:27:57 +000090static ulong imx_get_ahbclk(void)
John Rigby9c146032010-01-25 23:12:56 -070091{
92 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +000093 ulong cctl = readl(&ccm->cctl);
94 ulong fref = imx_get_armclk();
John Rigby9c146032010-01-25 23:12:56 -070095 ulong div;
96
97 div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT)
98 & CCM_CCTL_AHB_DIV_MASK) + 1;
99
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +0000100 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -0700101}
102
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +0000103static ulong imx_get_ipgclk(void)
104{
105 return imx_get_ahbclk() / 2;
106}
107
Benoît Thébaudeaub3ab1392012-09-27 10:27:57 +0000108static ulong imx_get_perclk(int clk)
John Rigby9c146032010-01-25 23:12:56 -0700109{
110 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +0000111 ulong fref = imx_get_ahbclk();
John Rigby9c146032010-01-25 23:12:56 -0700112 ulong div;
113
Fabio Estevamf231efb2011-10-13 05:34:59 +0000114 div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]);
115 div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1;
John Rigby9c146032010-01-25 23:12:56 -0700116
Benoît Thébaudeau48bffe02012-09-27 10:27:14 +0000117 return fref / div;
John Rigby9c146032010-01-25 23:12:56 -0700118}
119
Timo Ketola738fa8d2012-04-18 22:55:28 +0000120unsigned int mxc_get_clock(enum mxc_clock clk)
121{
122 if (clk >= MXC_CLK_NUM)
123 return -1;
124 switch (clk) {
125 case MXC_ARM_CLK:
126 return imx_get_armclk();
Benoît Thébaudeau05dd78f2012-09-27 10:27:28 +0000127 case MXC_AHB_CLK:
128 return imx_get_ahbclk();
129 case MXC_IPG_CLK:
130 case MXC_CSPI_CLK:
Timo Ketola738fa8d2012-04-18 22:55:28 +0000131 case MXC_FEC_CLK:
Benoît Thébaudeau88a23822012-09-27 10:27:44 +0000132 return imx_get_ipgclk();
Timo Ketola738fa8d2012-04-18 22:55:28 +0000133 default:
134 return imx_get_perclk(clk);
135 }
136}
137
Fabio Estevam51f23542011-09-02 05:38:54 +0000138u32 get_cpu_rev(void)
139{
140 u32 srev;
141 u32 system_rev = 0x25000;
142
143 /* read SREV register from IIM module */
144 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
145 srev = readl(&iim->iim_srev);
146
147 switch (srev) {
148 case 0x00:
149 system_rev |= CHIP_REV_1_0;
150 break;
151 case 0x01:
152 system_rev |= CHIP_REV_1_1;
153 break;
Eric Benardc47d73f2012-09-23 02:03:05 +0000154 case 0x02:
155 system_rev |= CHIP_REV_1_2;
156 break;
Fabio Estevam51f23542011-09-02 05:38:54 +0000157 default:
158 system_rev |= 0x8000;
159 break;
160 }
161
162 return system_rev;
163}
164
John Rigby9c146032010-01-25 23:12:56 -0700165#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam4c6b02e2011-09-23 05:13:22 +0000166static char *get_reset_cause(void)
167{
168 /* read RCSR register from CCM module */
169 struct ccm_regs *ccm =
170 (struct ccm_regs *)IMX_CCM_BASE;
171
172 u32 cause = readl(&ccm->rcsr) & 0x0f;
173
174 if (cause == 0)
175 return "POR";
176 else if (cause == 1)
177 return "RST";
178 else if ((cause & 2) == 2)
179 return "WDOG";
180 else if ((cause & 4) == 4)
181 return "SW RESET";
182 else if ((cause & 8) == 8)
183 return "JTAG";
184 else
185 return "unknown reset";
186
187}
188
Fabio Estevamf231efb2011-10-13 05:34:59 +0000189int print_cpuinfo(void)
John Rigby9c146032010-01-25 23:12:56 -0700190{
191 char buf[32];
Fabio Estevam51f23542011-09-02 05:38:54 +0000192 u32 cpurev = get_cpu_rev();
John Rigby9c146032010-01-25 23:12:56 -0700193
Fabio Estevam9a423242011-09-02 05:38:55 +0000194 printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n",
Fabio Estevam51f23542011-09-02 05:38:54 +0000195 (cpurev & 0xF0) >> 4, (cpurev & 0x0F),
196 ((cpurev & 0x8000) ? " unknown" : ""),
Fabio Estevamf231efb2011-10-13 05:34:59 +0000197 strmhz(buf, imx_get_armclk()));
Fabio Estevam9a423242011-09-02 05:38:55 +0000198 printf("Reset cause: %s\n\n", get_reset_cause());
John Rigby9c146032010-01-25 23:12:56 -0700199 return 0;
200}
201#endif
202
Benoît Thébaudeau463b6852012-08-14 03:17:33 +0000203void enable_caches(void)
204{
205#ifndef CONFIG_SYS_DCACHE_OFF
206 /* Enable D-cache. I-cache is already enabled in start.S */
207 dcache_enable();
208#endif
209}
210
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000211#if defined(CONFIG_FEC_MXC)
212/*
213 * Initializes on-chip ethernet controllers.
214 * to override, implement board_eth_init()
215 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000216int cpu_eth_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700217{
John Rigby9c146032010-01-25 23:12:56 -0700218 struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE;
219 ulong val;
220
Fabio Estevamf231efb2011-10-13 05:34:59 +0000221 val = readl(&ccm->cgr0);
John Rigby9c146032010-01-25 23:12:56 -0700222 val |= (1 << 23);
Fabio Estevamf231efb2011-10-13 05:34:59 +0000223 writel(val, &ccm->cgr0);
224 return fecmxc_initialize(bis);
Timo Ketola738fa8d2012-04-18 22:55:28 +0000225}
Benoît Thébaudeau6991d6a2012-09-27 10:28:09 +0000226#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000227
228int get_clocks(void)
229{
230#ifdef CONFIG_FSL_ESDHC
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000231#if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE
232 gd->sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
233#else
234 gd->sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK);
235#endif
Timo Ketola738fa8d2012-04-18 22:55:28 +0000236#endif
237 return 0;
John Rigby9c146032010-01-25 23:12:56 -0700238}
239
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000240#ifdef CONFIG_FSL_ESDHC
John Rigby9c146032010-01-25 23:12:56 -0700241/*
242 * Initializes on-chip MMC controllers.
243 * to override, implement board_mmc_init()
244 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000245int cpu_mmc_init(bd_t *bis)
John Rigby9c146032010-01-25 23:12:56 -0700246{
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000247 return fsl_esdhc_mmc_init(bis);
John Rigby9c146032010-01-25 23:12:56 -0700248}
Benoît Thébaudeau95646052012-09-27 10:28:29 +0000249#endif
John Rigby9c146032010-01-25 23:12:56 -0700250
251#ifdef CONFIG_MXC_UART
Fabio Estevam59e6fc52011-03-02 10:14:27 +0100252void mx25_uart1_init_pins(void)
John Rigby9c146032010-01-25 23:12:56 -0700253{
254 struct iomuxc_mux_ctl *muxctl;
255 struct iomuxc_pad_ctl *padctl;
256 u32 inpadctl;
257 u32 outpadctl;
258 u32 muxmode0;
259
260 muxctl = (struct iomuxc_mux_ctl *)IMX_IOPADMUX_BASE;
261 padctl = (struct iomuxc_pad_ctl *)IMX_IOPADCTL_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +0000262 muxmode0 = MX25_PIN_MUX_MODE(0);
John Rigby9c146032010-01-25 23:12:56 -0700263 /*
264 * set up input pins with hysteresis and 100K pull-ups
265 */
266 inpadctl = MX25_PIN_PAD_CTL_HYS
267 | MX25_PIN_PAD_CTL_PKE
268 | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PU;
269
270 /*
271 * set up output pins with 100K pull-downs
272 * FIXME: need to revisit this
273 * PUE is ignored if PKE is not set
274 * so the right value here is likely
275 * 0x0 for no pull up/down
276 * or
277 * 0xc0 for 100k pull down
278 */
279 outpadctl = MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
280
281 /* UART1 */
282 /* rxd */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000283 writel(muxmode0, &muxctl->pad_uart1_rxd);
284 writel(inpadctl, &padctl->pad_uart1_rxd);
John Rigby9c146032010-01-25 23:12:56 -0700285
286 /* txd */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000287 writel(muxmode0, &muxctl->pad_uart1_txd);
288 writel(outpadctl, &padctl->pad_uart1_txd);
John Rigby9c146032010-01-25 23:12:56 -0700289
290 /* rts */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000291 writel(muxmode0, &muxctl->pad_uart1_rts);
292 writel(outpadctl, &padctl->pad_uart1_rts);
John Rigby9c146032010-01-25 23:12:56 -0700293
294 /* cts */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000295 writel(muxmode0, &muxctl->pad_uart1_cts);
296 writel(inpadctl, &padctl->pad_uart1_cts);
John Rigby9c146032010-01-25 23:12:56 -0700297}
298#endif /* CONFIG_MXC_UART */
299
300#ifdef CONFIG_FEC_MXC
Fabio Estevamf231efb2011-10-13 05:34:59 +0000301void mx25_fec_init_pins(void)
John Rigby9c146032010-01-25 23:12:56 -0700302{
303 struct iomuxc_mux_ctl *muxctl;
304 struct iomuxc_pad_ctl *padctl;
305 u32 inpadctl_100kpd;
306 u32 inpadctl_22kpu;
307 u32 outpadctl;
308 u32 muxmode0;
309
310 muxctl = (struct iomuxc_mux_ctl *)IMX_IOPADMUX_BASE;
311 padctl = (struct iomuxc_pad_ctl *)IMX_IOPADCTL_BASE;
Fabio Estevamf231efb2011-10-13 05:34:59 +0000312 muxmode0 = MX25_PIN_MUX_MODE(0);
John Rigby9c146032010-01-25 23:12:56 -0700313 inpadctl_100kpd = MX25_PIN_PAD_CTL_HYS
314 | MX25_PIN_PAD_CTL_PKE
315 | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
316 inpadctl_22kpu = MX25_PIN_PAD_CTL_HYS
317 | MX25_PIN_PAD_CTL_PKE
318 | MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_22K_PU;
319 /*
320 * set up output pins with 100K pull-downs
321 * FIXME: need to revisit this
322 * PUE is ignored if PKE is not set
323 * so the right value here is likely
324 * 0x0 for no pull
325 * or
326 * 0xc0 for 100k pull down
327 */
328 outpadctl = MX25_PIN_PAD_CTL_PUE | MX25_PIN_PAD_CTL_100K_PD;
329
330 /* FEC_TX_CLK */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000331 writel(muxmode0, &muxctl->pad_fec_tx_clk);
332 writel(inpadctl_100kpd, &padctl->pad_fec_tx_clk);
John Rigby9c146032010-01-25 23:12:56 -0700333
334 /* FEC_RX_DV */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000335 writel(muxmode0, &muxctl->pad_fec_rx_dv);
336 writel(inpadctl_100kpd, &padctl->pad_fec_rx_dv);
John Rigby9c146032010-01-25 23:12:56 -0700337
338 /* FEC_RDATA0 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000339 writel(muxmode0, &muxctl->pad_fec_rdata0);
340 writel(inpadctl_100kpd, &padctl->pad_fec_rdata0);
John Rigby9c146032010-01-25 23:12:56 -0700341
342 /* FEC_TDATA0 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000343 writel(muxmode0, &muxctl->pad_fec_tdata0);
344 writel(outpadctl, &padctl->pad_fec_tdata0);
John Rigby9c146032010-01-25 23:12:56 -0700345
346 /* FEC_TX_EN */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000347 writel(muxmode0, &muxctl->pad_fec_tx_en);
348 writel(outpadctl, &padctl->pad_fec_tx_en);
John Rigby9c146032010-01-25 23:12:56 -0700349
350 /* FEC_MDC */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000351 writel(muxmode0, &muxctl->pad_fec_mdc);
352 writel(outpadctl, &padctl->pad_fec_mdc);
John Rigby9c146032010-01-25 23:12:56 -0700353
354 /* FEC_MDIO */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000355 writel(muxmode0, &muxctl->pad_fec_mdio);
356 writel(inpadctl_22kpu, &padctl->pad_fec_mdio);
John Rigby9c146032010-01-25 23:12:56 -0700357
358 /* FEC_RDATA1 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000359 writel(muxmode0, &muxctl->pad_fec_rdata1);
360 writel(inpadctl_100kpd, &padctl->pad_fec_rdata1);
John Rigby9c146032010-01-25 23:12:56 -0700361
362 /* FEC_TDATA1 */
Fabio Estevamf231efb2011-10-13 05:34:59 +0000363 writel(muxmode0, &muxctl->pad_fec_tdata1);
364 writel(outpadctl, &padctl->pad_fec_tdata1);
John Rigby9c146032010-01-25 23:12:56 -0700365
366}
Liu Hui-R643434df66192010-11-18 23:45:55 +0000367
Fabio Estevam04fc1282011-12-20 05:46:31 +0000368void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
Liu Hui-R643434df66192010-11-18 23:45:55 +0000369{
370 int i;
371 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
372 struct fuse_bank *bank = &iim->bank[0];
373 struct fuse_bank0_regs *fuse =
374 (struct fuse_bank0_regs *)bank->fuse_regs;
375
376 for (i = 0; i < 6; i++)
377 mac[i] = readl(&fuse->mac_addr[i]) & 0xff;
378}
John Rigby9c146032010-01-25 23:12:56 -0700379#endif /* CONFIG_FEC_MXC */