blob: fc56baccfcd6e6a013f819568b2f433336dced38 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sascha Hauer1a7676f2008-03-26 20:40:42 +01002/*
3 * (C) Copyright 2007
4 * Sascha Hauer, Pengutronix
Sascha Hauer1a7676f2008-03-26 20:40:42 +01005 */
6
Benoît Thébaudeau91116532012-08-14 08:43:47 +00007#include <div64.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Stefano Babic78129d92011-03-14 15:43:56 +01009#include <asm/arch/imx-regs.h>
Stefano Babic43dc3f02011-07-13 14:34:52 +020010#include <asm/arch/clock.h>
Stefano Babic6272c7e2010-10-06 08:59:26 +020011#include <asm/io.h>
Helmut Raiger035929c2011-09-29 05:45:03 +000012#include <asm/arch/sys_proto.h>
Sascha Hauer1a7676f2008-03-26 20:40:42 +010013
14static u32 mx31_decode_pll(u32 reg, u32 infreq)
15{
Helmut Raigerabd23432011-10-12 23:08:30 +020016 u32 mfi = GET_PLL_MFI(reg);
Benoît Thébaudeau91116532012-08-14 08:43:47 +000017 s32 mfn = GET_PLL_MFN(reg);
Helmut Raigerabd23432011-10-12 23:08:30 +020018 u32 mfd = GET_PLL_MFD(reg);
19 u32 pd = GET_PLL_PD(reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010020
21 mfi = mfi <= 5 ? 5 : mfi;
Benoît Thébaudeau91116532012-08-14 08:43:47 +000022 mfn = mfn >= 512 ? mfn - 1024 : mfn;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010023 mfd += 1;
24 pd += 1;
25
Benoît Thébaudeau91116532012-08-14 08:43:47 +000026 return lldiv(2 * (u64)infreq * (mfi * mfd + mfn),
27 mfd * pd);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010028}
29
Guennadi Liakhovetski08601a62008-05-08 10:09:27 +020030static u32 mx31_get_mpl_dpdgck_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010031{
32 u32 infreq;
33
Helmut Raigerabd23432011-10-12 23:08:30 +020034 if ((readl(CCM_CCMR) & CCMR_PRCS_MASK) == CCMR_FPM)
Benoît Thébaudeau38e2f082012-08-21 11:06:03 +000035 infreq = MXC_CLK32 * 1024;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010036 else
Benoît Thébaudeau38e2f082012-08-21 11:06:03 +000037 infreq = MXC_HCLK;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010038
Helmut Raigerabd23432011-10-12 23:08:30 +020039 return mx31_decode_pll(readl(CCM_MPCTL), infreq);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010040}
41
Guennadi Liakhovetski08601a62008-05-08 10:09:27 +020042static u32 mx31_get_mcu_main_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010043{
44 /* For now we assume mpl_dpdgck_clk == mcu_main_clk
45 * which should be correct for most boards
46 */
47 return mx31_get_mpl_dpdgck_clk();
48}
49
Stefano Babic43dc3f02011-07-13 14:34:52 +020050static u32 mx31_get_ipg_clk(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +010051{
52 u32 freq = mx31_get_mcu_main_clk();
Helmut Raigerabd23432011-10-12 23:08:30 +020053 u32 pdr0 = readl(CCM_PDR0);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010054
Helmut Raigerabd23432011-10-12 23:08:30 +020055 freq /= GET_PDR0_MAX_PODF(pdr0) + 1;
56 freq /= GET_PDR0_IPG_PODF(pdr0) + 1;
Sascha Hauer1a7676f2008-03-26 20:40:42 +010057
58 return freq;
59}
60
Helmut Raigerabd23432011-10-12 23:08:30 +020061/* hsp is the clock for the ipu */
62static u32 mx31_get_hsp_clk(void)
63{
64 u32 freq = mx31_get_mcu_main_clk();
65 u32 pdr0 = readl(CCM_PDR0);
66
67 freq /= GET_PDR0_HSP_PODF(pdr0) + 1;
68
69 return freq;
70}
71
Sascha Hauer1a7676f2008-03-26 20:40:42 +010072void mx31_dump_clocks(void)
73{
74 u32 cpufreq = mx31_get_mcu_main_clk();
Fabio Estevam85898662011-11-09 04:15:03 +000075 printf("mx31 cpu clock: %dMHz\n", cpufreq / 1000000);
Sascha Hauer1a7676f2008-03-26 20:40:42 +010076 printf("ipg clock : %dHz\n", mx31_get_ipg_clk());
Helmut Raigerabd23432011-10-12 23:08:30 +020077 printf("hsp clock : %dHz\n", mx31_get_hsp_clk());
Sascha Hauer1a7676f2008-03-26 20:40:42 +010078}
79
Stefano Babic43dc3f02011-07-13 14:34:52 +020080unsigned int mxc_get_clock(enum mxc_clock clk)
81{
82 switch (clk) {
83 case MXC_ARM_CLK:
84 return mx31_get_mcu_main_clk();
85 case MXC_IPG_CLK:
Stefano Babic2def40f2011-08-30 00:51:13 +000086 case MXC_IPG_PERCLK:
Stefano Babic43dc3f02011-07-13 14:34:52 +020087 case MXC_CSPI_CLK:
88 case MXC_UART_CLK:
Helmut Raiger64c316d2012-01-11 03:59:22 +000089 case MXC_ESDHC_CLK:
Matthias Weisser99ba3422012-09-24 02:46:53 +000090 case MXC_I2C_CLK:
Stefano Babic43dc3f02011-07-13 14:34:52 +020091 return mx31_get_ipg_clk();
Helmut Raigerabd23432011-10-12 23:08:30 +020092 case MXC_IPU_CLK:
93 return mx31_get_hsp_clk();
Stefano Babic43dc3f02011-07-13 14:34:52 +020094 }
95 return -1;
96}
97
98u32 imx_get_uartclk(void)
99{
100 return mxc_get_clock(MXC_UART_CLK);
101}
102
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100103void mx31_gpio_mux(unsigned long mode)
104{
105 unsigned long reg, shift, tmp;
106
Magnus Lilja532c1582008-08-03 21:44:10 +0200107 reg = IOMUXC_BASE + (mode & 0x1fc);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100108 shift = (~mode & 0x3) * 8;
109
Helmut Raigerabd23432011-10-12 23:08:30 +0200110 tmp = readl(reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100111 tmp &= ~(0xff << shift);
Magnus Lilja532c1582008-08-03 21:44:10 +0200112 tmp |= ((mode >> IOMUX_MODE_POS) & 0xff) << shift;
Helmut Raigerabd23432011-10-12 23:08:30 +0200113 writel(tmp, reg);
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100114}
115
Stefano Babic6272c7e2010-10-06 08:59:26 +0200116void mx31_set_pad(enum iomux_pins pin, u32 config)
117{
Stefano Babic5f09b922010-10-19 20:19:13 +0200118 u32 field, l, reg;
Stefano Babic6272c7e2010-10-06 08:59:26 +0200119
120 pin &= IOMUX_PADNUM_MASK;
121 reg = (IOMUXC_BASE + 0x154) + (pin + 2) / 3 * 4;
122 field = (pin + 2) % 3;
123
Helmut Raigerabd23432011-10-12 23:08:30 +0200124 l = readl(reg);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200125 l &= ~(0x1ff << (field * 10));
126 l |= config << (field * 10);
Helmut Raigerabd23432011-10-12 23:08:30 +0200127 writel(l, reg);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200128
Fabio Estevam87db8c92011-10-20 16:01:29 +0000129}
130
131void mx31_set_gpr(enum iomux_gp_func gp, char en)
132{
133 u32 l;
Fabio Estevam3fca6912011-11-09 04:15:02 +0000134 struct iomuxc_regs *iomuxc = (struct iomuxc_regs *)IOMUXC_BASE;
Fabio Estevam87db8c92011-10-20 16:01:29 +0000135
Fabio Estevam3fca6912011-11-09 04:15:02 +0000136 l = readl(&iomuxc->gpr);
Fabio Estevam87db8c92011-10-20 16:01:29 +0000137 if (en)
138 l |= gp;
139 else
140 l &= ~gp;
141
Fabio Estevam3fca6912011-11-09 04:15:02 +0000142 writel(l, &iomuxc->gpr);
Stefano Babic6272c7e2010-10-06 08:59:26 +0200143}
144
Helmut Raiger035929c2011-09-29 05:45:03 +0000145void mxc_setup_weimcs(int cs, const struct mxc_weimcs *weimcs)
146{
147 struct mx31_weim *weim = (struct mx31_weim *) WEIM_BASE;
148 struct mx31_weim_cscr *cscr = &weim->cscr[cs];
149
150 writel(weimcs->upper, &cscr->upper);
151 writel(weimcs->lower, &cscr->lower);
152 writel(weimcs->additional, &cscr->additional);
153}
154
Fabio Estevam939b9782011-04-11 16:18:12 +0000155struct mx3_cpu_type mx31_cpu_type[] = {
Stefano Babic7f5a0262011-04-29 08:56:27 +0200156 { .srev = 0x00, .v = 0x10 },
157 { .srev = 0x10, .v = 0x11 },
158 { .srev = 0x11, .v = 0x11 },
159 { .srev = 0x12, .v = 0x1F },
160 { .srev = 0x13, .v = 0x1F },
161 { .srev = 0x14, .v = 0x12 },
162 { .srev = 0x15, .v = 0x12 },
163 { .srev = 0x28, .v = 0x20 },
164 { .srev = 0x29, .v = 0x20 },
Fabio Estevam939b9782011-04-11 16:18:12 +0000165};
166
Stefano Babic7f5a0262011-04-29 08:56:27 +0200167u32 get_cpu_rev(void)
Fabio Estevam939b9782011-04-11 16:18:12 +0000168{
169 u32 i, srev;
170
171 /* read SREV register from IIM module */
172 struct iim_regs *iim = (struct iim_regs *)MX31_IIM_BASE_ADDR;
173 srev = readl(&iim->iim_srev);
174
175 for (i = 0; i < ARRAY_SIZE(mx31_cpu_type); i++)
176 if (srev == mx31_cpu_type[i].srev)
Peng Fan9f54fe62015-08-13 10:55:32 +0800177 return mx31_cpu_type[i].v | (MXC_CPU_MX31 << 12);
Stefano Babic7f5a0262011-04-29 08:56:27 +0200178
179 return srev | 0x8000;
Fabio Estevam939b9782011-04-11 16:18:12 +0000180}
181
Stefano Babicbd94dd22011-05-17 13:45:41 +0200182static char *get_reset_cause(void)
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000183{
184 /* read RCSR register from CCM module */
185 struct clock_control_regs *ccm =
186 (struct clock_control_regs *)CCM_BASE;
187
188 u32 cause = readl(&ccm->rcsr) & 0x07;
189
190 switch (cause) {
191 case 0x0000:
192 return "POR";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000193 case 0x0001:
194 return "RST";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000195 case 0x0002:
196 return "WDOG";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000197 case 0x0006:
198 return "JTAG";
Helmut Raiger9468db42012-02-15 22:44:34 +0000199 case 0x0007:
200 return "ARM11P power gating";
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000201 default:
202 return "unknown reset";
203 }
204}
205
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100206#if defined(CONFIG_DISPLAY_CPUINFO)
Fabio Estevam85898662011-11-09 04:15:03 +0000207int print_cpuinfo(void)
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100208{
Stefano Babic7f5a0262011-04-29 08:56:27 +0200209 u32 srev = get_cpu_rev();
210
Fabio Estevam298a6472011-09-16 04:01:22 +0000211 printf("CPU: Freescale i.MX31 rev %d.%d%s at %d MHz.\n",
Stefano Babic7f5a0262011-04-29 08:56:27 +0200212 (srev & 0xF0) >> 4, (srev & 0x0F),
213 ((srev & 0x8000) ? " unknown" : ""),
214 mx31_get_mcu_main_clk() / 1000000);
Fabio Estevam2b0fa452011-04-18 07:38:11 +0000215 printf("Reset cause: %s\n", get_reset_cause());
Sascha Hauer1a7676f2008-03-26 20:40:42 +0100216 return 0;
217}
218#endif