blob: f45828141e6013ecdcc67de2382ebb54716dcd0d [file] [log] [blame]
Sascha Hauer9b56f4f2008-03-26 20:40:42 +01001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
Stefano Babic86271112011-03-14 15:43:56 +010025#include <asm/arch/imx-regs.h>
Stefano Babic9f008bb2011-07-13 14:34:52 +020026#include <asm/arch/clock.h>
Stefano Babicf76888c2010-10-06 08:59:26 +020027#include <asm/io.h>
Helmut Raiger47c54552011-09-29 05:45:03 +000028#include <asm/arch/sys_proto.h>
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010029
30static u32 mx31_decode_pll(u32 reg, u32 infreq)
31{
Helmut Raigerf0029192011-10-12 23:08:30 +020032 u32 mfi = GET_PLL_MFI(reg);
33 u32 mfn = GET_PLL_MFN(reg);
34 u32 mfd = GET_PLL_MFD(reg);
35 u32 pd = GET_PLL_PD(reg);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010036
37 mfi = mfi <= 5 ? 5 : mfi;
38 mfd += 1;
39 pd += 1;
40
41 return ((2 * (infreq >> 10) * (mfi * mfd + mfn)) /
42 (mfd * pd)) << 10;
43}
44
Guennadi Liakhovetski2ab02fd2008-05-08 10:09:27 +020045static u32 mx31_get_mpl_dpdgck_clk(void)
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010046{
47 u32 infreq;
48
Helmut Raigerf0029192011-10-12 23:08:30 +020049 if ((readl(CCM_CCMR) & CCMR_PRCS_MASK) == CCMR_FPM)
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010050 infreq = CONFIG_MX31_CLK32 * 1024;
51 else
52 infreq = CONFIG_MX31_HCLK_FREQ;
53
Helmut Raigerf0029192011-10-12 23:08:30 +020054 return mx31_decode_pll(readl(CCM_MPCTL), infreq);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010055}
56
Guennadi Liakhovetski2ab02fd2008-05-08 10:09:27 +020057static u32 mx31_get_mcu_main_clk(void)
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010058{
59 /* For now we assume mpl_dpdgck_clk == mcu_main_clk
60 * which should be correct for most boards
61 */
62 return mx31_get_mpl_dpdgck_clk();
63}
64
Stefano Babic9f008bb2011-07-13 14:34:52 +020065static u32 mx31_get_ipg_clk(void)
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010066{
67 u32 freq = mx31_get_mcu_main_clk();
Helmut Raigerf0029192011-10-12 23:08:30 +020068 u32 pdr0 = readl(CCM_PDR0);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010069
Helmut Raigerf0029192011-10-12 23:08:30 +020070 freq /= GET_PDR0_MAX_PODF(pdr0) + 1;
71 freq /= GET_PDR0_IPG_PODF(pdr0) + 1;
72
73 return freq;
74}
75
76/* hsp is the clock for the ipu */
77static u32 mx31_get_hsp_clk(void)
78{
79 u32 freq = mx31_get_mcu_main_clk();
80 u32 pdr0 = readl(CCM_PDR0);
81
82 freq /= GET_PDR0_HSP_PODF(pdr0) + 1;
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010083
84 return freq;
85}
86
87void mx31_dump_clocks(void)
88{
89 u32 cpufreq = mx31_get_mcu_main_clk();
Fabio Estevamc0225d12011-11-09 04:15:03 +000090 printf("mx31 cpu clock: %dMHz\n", cpufreq / 1000000);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010091 printf("ipg clock : %dHz\n", mx31_get_ipg_clk());
Helmut Raigerf0029192011-10-12 23:08:30 +020092 printf("hsp clock : %dHz\n", mx31_get_hsp_clk());
Sascha Hauer9b56f4f2008-03-26 20:40:42 +010093}
94
Stefano Babic9f008bb2011-07-13 14:34:52 +020095unsigned int mxc_get_clock(enum mxc_clock clk)
96{
97 switch (clk) {
98 case MXC_ARM_CLK:
99 return mx31_get_mcu_main_clk();
100 case MXC_IPG_CLK:
Stefano Babic67f463b2011-08-30 00:51:13 +0000101 case MXC_IPG_PERCLK:
Stefano Babic9f008bb2011-07-13 14:34:52 +0200102 case MXC_CSPI_CLK:
103 case MXC_UART_CLK:
104 return mx31_get_ipg_clk();
Helmut Raigerf0029192011-10-12 23:08:30 +0200105 case MXC_IPU_CLK:
106 return mx31_get_hsp_clk();
Stefano Babic9f008bb2011-07-13 14:34:52 +0200107 }
108 return -1;
109}
110
111u32 imx_get_uartclk(void)
112{
113 return mxc_get_clock(MXC_UART_CLK);
114}
115
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100116void mx31_gpio_mux(unsigned long mode)
117{
118 unsigned long reg, shift, tmp;
119
Magnus Lilja5276a352008-08-03 21:44:10 +0200120 reg = IOMUXC_BASE + (mode & 0x1fc);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100121 shift = (~mode & 0x3) * 8;
122
Helmut Raigerf0029192011-10-12 23:08:30 +0200123 tmp = readl(reg);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100124 tmp &= ~(0xff << shift);
Magnus Lilja5276a352008-08-03 21:44:10 +0200125 tmp |= ((mode >> IOMUX_MODE_POS) & 0xff) << shift;
Helmut Raigerf0029192011-10-12 23:08:30 +0200126 writel(tmp, reg);
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100127}
128
Stefano Babicf76888c2010-10-06 08:59:26 +0200129void mx31_set_pad(enum iomux_pins pin, u32 config)
130{
Stefano Babicd078b7c2010-10-19 20:19:13 +0200131 u32 field, l, reg;
Stefano Babicf76888c2010-10-06 08:59:26 +0200132
133 pin &= IOMUX_PADNUM_MASK;
134 reg = (IOMUXC_BASE + 0x154) + (pin + 2) / 3 * 4;
135 field = (pin + 2) % 3;
136
Helmut Raigerf0029192011-10-12 23:08:30 +0200137 l = readl(reg);
Stefano Babicf76888c2010-10-06 08:59:26 +0200138 l &= ~(0x1ff << (field * 10));
139 l |= config << (field * 10);
Helmut Raigerf0029192011-10-12 23:08:30 +0200140 writel(l, reg);
Stefano Babicf76888c2010-10-06 08:59:26 +0200141
142}
143
Fabio Estevam6d0fb3d2011-10-20 16:01:29 +0000144void mx31_set_gpr(enum iomux_gp_func gp, char en)
145{
146 u32 l;
Fabio Estevamce93dc92011-11-09 04:15:02 +0000147 struct iomuxc_regs *iomuxc = (struct iomuxc_regs *)IOMUXC_BASE;
Fabio Estevam6d0fb3d2011-10-20 16:01:29 +0000148
Fabio Estevamce93dc92011-11-09 04:15:02 +0000149 l = readl(&iomuxc->gpr);
Fabio Estevam6d0fb3d2011-10-20 16:01:29 +0000150 if (en)
151 l |= gp;
152 else
153 l &= ~gp;
154
Fabio Estevamce93dc92011-11-09 04:15:02 +0000155 writel(l, &iomuxc->gpr);
Fabio Estevam6d0fb3d2011-10-20 16:01:29 +0000156}
157
Helmut Raiger47c54552011-09-29 05:45:03 +0000158void mxc_setup_weimcs(int cs, const struct mxc_weimcs *weimcs)
159{
160 struct mx31_weim *weim = (struct mx31_weim *) WEIM_BASE;
161 struct mx31_weim_cscr *cscr = &weim->cscr[cs];
162
163 writel(weimcs->upper, &cscr->upper);
164 writel(weimcs->lower, &cscr->lower);
165 writel(weimcs->additional, &cscr->additional);
166}
167
Fabio Estevam4adaf9b2011-04-11 16:18:12 +0000168struct mx3_cpu_type mx31_cpu_type[] = {
Stefano Babic2f220452011-04-29 08:56:27 +0200169 { .srev = 0x00, .v = 0x10 },
170 { .srev = 0x10, .v = 0x11 },
171 { .srev = 0x11, .v = 0x11 },
172 { .srev = 0x12, .v = 0x1F },
173 { .srev = 0x13, .v = 0x1F },
174 { .srev = 0x14, .v = 0x12 },
175 { .srev = 0x15, .v = 0x12 },
176 { .srev = 0x28, .v = 0x20 },
177 { .srev = 0x29, .v = 0x20 },
Fabio Estevam4adaf9b2011-04-11 16:18:12 +0000178};
179
Stefano Babic2f220452011-04-29 08:56:27 +0200180u32 get_cpu_rev(void)
Fabio Estevam4adaf9b2011-04-11 16:18:12 +0000181{
182 u32 i, srev;
183
184 /* read SREV register from IIM module */
185 struct iim_regs *iim = (struct iim_regs *)MX31_IIM_BASE_ADDR;
186 srev = readl(&iim->iim_srev);
187
188 for (i = 0; i < ARRAY_SIZE(mx31_cpu_type); i++)
189 if (srev == mx31_cpu_type[i].srev)
190 return mx31_cpu_type[i].v;
Stefano Babic2f220452011-04-29 08:56:27 +0200191
192 return srev | 0x8000;
Fabio Estevam4adaf9b2011-04-11 16:18:12 +0000193}
194
Stefano Babicd43458d2011-05-17 13:45:41 +0200195static char *get_reset_cause(void)
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000196{
197 /* read RCSR register from CCM module */
198 struct clock_control_regs *ccm =
199 (struct clock_control_regs *)CCM_BASE;
200
201 u32 cause = readl(&ccm->rcsr) & 0x07;
202
203 switch (cause) {
204 case 0x0000:
205 return "POR";
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000206 case 0x0001:
207 return "RST";
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000208 case 0x0002:
209 return "WDOG";
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000210 case 0x0006:
211 return "JTAG";
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000212 default:
213 return "unknown reset";
214 }
215}
216
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100217#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevamc0225d12011-11-09 04:15:03 +0000218int print_cpuinfo(void)
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100219{
Stefano Babic2f220452011-04-29 08:56:27 +0200220 u32 srev = get_cpu_rev();
221
Fabio Estevamb6ce4792011-09-16 04:01:22 +0000222 printf("CPU: Freescale i.MX31 rev %d.%d%s at %d MHz.\n",
Stefano Babic2f220452011-04-29 08:56:27 +0200223 (srev & 0xF0) >> 4, (srev & 0x0F),
224 ((srev & 0x8000) ? " unknown" : ""),
225 mx31_get_mcu_main_clk() / 1000000);
Fabio Estevam25d8e1b2011-04-18 07:38:11 +0000226 printf("Reset cause: %s\n", get_reset_cause());
Sascha Hauer9b56f4f2008-03-26 20:40:42 +0100227 return 0;
228}
229#endif