blob: 060d46b82ca02b1bef8b779ab30fa308da421c9b [file] [log] [blame]
Sascha Hauer1a7676f2008-03-26 20:40:42 +01001/*
2 * (C) Copyright 2007
3 * Sascha Hauer, Pengutronix
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Sascha Hauer1a7676f2008-03-26 20:40:42 +01006 */
7
8#include <common.h>
Benoît Thébaudeau91116532012-08-14 08:43:47 +00009#include <div64.h>
Stefano Babic78129d92011-03-14 15:43:56 +010010#include <asm/arch/imx-regs.h>
Stefano Babic43dc3f02011-07-13 14:34:52 +020011#include <asm/arch/clock.h>
Stefano Babic6272c7e2010-10-06 08:59:26 +020012#include <asm/io.h>
Helmut Raiger035929c2011-09-29 05:45:03 +000013#include <asm/arch/sys_proto.h>
Sascha Hauer1a7676f2008-03-26 20:40:42 +010014
15static u32 mx31_decode_pll(u32 reg, u32 infreq)
16{
Helmut Raigerabd23432011-10-12 23:08:30 +020017 u32 mfi = GET_PLL_MFI(reg);
Benoît Thébaudeau91116532012-08-14 08:43:47 +000018 s32 mfn = GET_PLL_MFN(reg);
Helmut Raigerabd23432011-10-12 23:08:30 +020019 u32 mfd = GET_PLL_MFD(reg);
20 u32 pd = GET_PLL_PD(reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010021
22 mfi = mfi <= 5 ? 5 : mfi;
Benoît Thébaudeau91116532012-08-14 08:43:47 +000023 mfn = mfn >= 512 ? mfn - 1024 : mfn;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010024 mfd += 1;
25 pd += 1;
26
Benoît Thébaudeau91116532012-08-14 08:43:47 +000027 return lldiv(2 * (u64)infreq * (mfi * mfd + mfn),
28 mfd * pd);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010029}
30
Guennadi Liakhovetski08601a62008-05-08 10:09:27 +020031static u32 mx31_get_mpl_dpdgck_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010032{
33 u32 infreq;
34
Helmut Raigerabd23432011-10-12 23:08:30 +020035 if ((readl(CCM_CCMR) & CCMR_PRCS_MASK) == CCMR_FPM)
Benoît Thébaudeau38e2f082012-08-21 11:06:03 +000036 infreq = MXC_CLK32 * 1024;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010037 else
Benoît Thébaudeau38e2f082012-08-21 11:06:03 +000038 infreq = MXC_HCLK;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010039
Helmut Raigerabd23432011-10-12 23:08:30 +020040 return mx31_decode_pll(readl(CCM_MPCTL), infreq);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010041}
42
Guennadi Liakhovetski08601a62008-05-08 10:09:27 +020043static u32 mx31_get_mcu_main_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010044{
45 /* For now we assume mpl_dpdgck_clk == mcu_main_clk
46 * which should be correct for most boards
47 */
48 return mx31_get_mpl_dpdgck_clk();
49}
50
Stefano Babic43dc3f02011-07-13 14:34:52 +020051static u32 mx31_get_ipg_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010052{
53 u32 freq = mx31_get_mcu_main_clk();
Helmut Raigerabd23432011-10-12 23:08:30 +020054 u32 pdr0 = readl(CCM_PDR0);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010055
Helmut Raigerabd23432011-10-12 23:08:30 +020056 freq /= GET_PDR0_MAX_PODF(pdr0) + 1;
57 freq /= GET_PDR0_IPG_PODF(pdr0) + 1;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010058
59 return freq;
60}
61
Helmut Raigerabd23432011-10-12 23:08:30 +020062/* hsp is the clock for the ipu */
63static u32 mx31_get_hsp_clk(void)
64{
65 u32 freq = mx31_get_mcu_main_clk();
66 u32 pdr0 = readl(CCM_PDR0);
67
68 freq /= GET_PDR0_HSP_PODF(pdr0) + 1;
69
70 return freq;
71}
72
Sascha Hauer1a7676f2008-03-26 20:40:42 +010073void mx31_dump_clocks(void)
74{
75 u32 cpufreq = mx31_get_mcu_main_clk();
Fabio Estevam85898662011-11-09 04:15:03 +000076 printf("mx31 cpu clock: %dMHz\n", cpufreq / 1000000);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010077 printf("ipg clock : %dHz\n", mx31_get_ipg_clk());
Helmut Raigerabd23432011-10-12 23:08:30 +020078 printf("hsp clock : %dHz\n", mx31_get_hsp_clk());
Sascha Hauer1a7676f2008-03-26 20:40:42 +010079}
80
Stefano Babic43dc3f02011-07-13 14:34:52 +020081unsigned int mxc_get_clock(enum mxc_clock clk)
82{
83 switch (clk) {
84 case MXC_ARM_CLK:
85 return mx31_get_mcu_main_clk();
86 case MXC_IPG_CLK:
Stefano Babic2def40f2011-08-30 00:51:13 +000087 case MXC_IPG_PERCLK:
Stefano Babic43dc3f02011-07-13 14:34:52 +020088 case MXC_CSPI_CLK:
89 case MXC_UART_CLK:
Helmut Raiger64c316d2012-01-11 03:59:22 +000090 case MXC_ESDHC_CLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +000091 case MXC_I2C_CLK:
Stefano Babic43dc3f02011-07-13 14:34:52 +020092 return mx31_get_ipg_clk();
Helmut Raigerabd23432011-10-12 23:08:30 +020093 case MXC_IPU_CLK:
94 return mx31_get_hsp_clk();
Stefano Babic43dc3f02011-07-13 14:34:52 +020095 }
96 return -1;
97}
98
99u32 imx_get_uartclk(void)
100{
101 return mxc_get_clock(MXC_UART_CLK);
102}
103
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100104void mx31_gpio_mux(unsigned long mode)
105{
106 unsigned long reg, shift, tmp;
107
Magnus Lilja532c1582008-08-03 21:44:10 +0200108 reg = IOMUXC_BASE + (mode & 0x1fc);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100109 shift = (~mode & 0x3) * 8;
110
Helmut Raigerabd23432011-10-12 23:08:30 +0200111 tmp = readl(reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100112 tmp &= ~(0xff << shift);
Magnus Lilja532c1582008-08-03 21:44:10 +0200113 tmp |= ((mode >> IOMUX_MODE_POS) & 0xff) << shift;
Helmut Raigerabd23432011-10-12 23:08:30 +0200114 writel(tmp, reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100115}
116
Stefano Babic6272c7e2010-10-06 08:59:26 +0200117void mx31_set_pad(enum iomux_pins pin, u32 config)
118{
Stefano Babic5f09b922010-10-19 20:19:13 +0200119 u32 field, l, reg;
Stefano Babic6272c7e2010-10-06 08:59:26 +0200120
121 pin &= IOMUX_PADNUM_MASK;
122 reg = (IOMUXC_BASE + 0x154) + (pin + 2) / 3 * 4;
123 field = (pin + 2) % 3;
124
Helmut Raigerabd23432011-10-12 23:08:30 +0200125 l = readl(reg);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200126 l &= ~(0x1ff << (field * 10));
127 l |= config << (field * 10);
Helmut Raigerabd23432011-10-12 23:08:30 +0200128 writel(l, reg);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200129
Fabio Estevam87db8c92011-10-20 16:01:29 +0000130}
131
132void mx31_set_gpr(enum iomux_gp_func gp, char en)
133{
134 u32 l;
Fabio Estevam3fca6912011-11-09 04:15:02 +0000135 struct iomuxc_regs *iomuxc = (struct iomuxc_regs *)IOMUXC_BASE;
Fabio Estevam87db8c92011-10-20 16:01:29 +0000136
Fabio Estevam3fca6912011-11-09 04:15:02 +0000137 l = readl(&iomuxc->gpr);
Fabio Estevam87db8c92011-10-20 16:01:29 +0000138 if (en)
139 l |= gp;
140 else
141 l &= ~gp;
142
Fabio Estevam3fca6912011-11-09 04:15:02 +0000143 writel(l, &iomuxc->gpr);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200144}
145
Helmut Raiger035929c2011-09-29 05:45:03 +0000146void mxc_setup_weimcs(int cs, const struct mxc_weimcs *weimcs)
147{
148 struct mx31_weim *weim = (struct mx31_weim *) WEIM_BASE;
149 struct mx31_weim_cscr *cscr = &weim->cscr[cs];
150
151 writel(weimcs->upper, &cscr->upper);
152 writel(weimcs->lower, &cscr->lower);
153 writel(weimcs->additional, &cscr->additional);
154}
155
Fabio Estevam939b9782011-04-11 16:18:12 +0000156struct mx3_cpu_type mx31_cpu_type[] = {
Stefano Babic7f5a0262011-04-29 08:56:27 +0200157 { .srev = 0x00, .v = 0x10 },
158 { .srev = 0x10, .v = 0x11 },
159 { .srev = 0x11, .v = 0x11 },
160 { .srev = 0x12, .v = 0x1F },
161 { .srev = 0x13, .v = 0x1F },
162 { .srev = 0x14, .v = 0x12 },
163 { .srev = 0x15, .v = 0x12 },
164 { .srev = 0x28, .v = 0x20 },
165 { .srev = 0x29, .v = 0x20 },
Fabio Estevam939b9782011-04-11 16:18:12 +0000166};
167
Stefano Babic7f5a0262011-04-29 08:56:27 +0200168u32 get_cpu_rev(void)
Fabio Estevam939b9782011-04-11 16:18:12 +0000169{
170 u32 i, srev;
171
172 /* read SREV register from IIM module */
173 struct iim_regs *iim = (struct iim_regs *)MX31_IIM_BASE_ADDR;
174 srev = readl(&iim->iim_srev);
175
176 for (i = 0; i < ARRAY_SIZE(mx31_cpu_type); i++)
177 if (srev == mx31_cpu_type[i].srev)
178 return mx31_cpu_type[i].v;
Stefano Babic7f5a0262011-04-29 08:56:27 +0200179
180 return srev | 0x8000;
Fabio Estevam939b9782011-04-11 16:18:12 +0000181}
182
Stefano Babicbd94dd22011-05-17 13:45:41 +0200183static char *get_reset_cause(void)
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000184{
185 /* read RCSR register from CCM module */
186 struct clock_control_regs *ccm =
187 (struct clock_control_regs *)CCM_BASE;
188
189 u32 cause = readl(&ccm->rcsr) & 0x07;
190
191 switch (cause) {
192 case 0x0000:
193 return "POR";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000194 case 0x0001:
195 return "RST";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000196 case 0x0002:
197 return "WDOG";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000198 case 0x0006:
199 return "JTAG";
Helmut Raiger9468db42012-02-15 22:44:34 +0000200 case 0x0007:
201 return "ARM11P power gating";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000202 default:
203 return "unknown reset";
204 }
205}
206
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100207#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam85898662011-11-09 04:15:03 +0000208int print_cpuinfo(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100209{
Stefano Babic7f5a0262011-04-29 08:56:27 +0200210 u32 srev = get_cpu_rev();
211
Fabio Estevam298a6472011-09-16 04:01:22 +0000212 printf("CPU: Freescale i.MX31 rev %d.%d%s at %d MHz.\n",
Stefano Babic7f5a0262011-04-29 08:56:27 +0200213 (srev & 0xF0) >> 4, (srev & 0x0F),
214 ((srev & 0x8000) ? " unknown" : ""),
215 mx31_get_mcu_main_clk() / 1000000);
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000216 printf("Reset cause: %s\n", get_reset_cause());
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100217 return 0;
218}
219#endif