Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 |
| 4 | * Sascha Hauer, Pengutronix |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Benoît Thébaudeau | 9111653 | 2012-08-14 08:43:47 +0000 | [diff] [blame] | 7 | #include <div64.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Stefano Babic | 78129d9 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 9 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 10 | #include <asm/arch/clock.h> |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Helmut Raiger | 035929c | 2011-09-29 05:45:03 +0000 | [diff] [blame] | 12 | #include <asm/arch/sys_proto.h> |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 13 | |
| 14 | static u32 mx31_decode_pll(u32 reg, u32 infreq) |
| 15 | { |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 16 | u32 mfi = GET_PLL_MFI(reg); |
Benoît Thébaudeau | 9111653 | 2012-08-14 08:43:47 +0000 | [diff] [blame] | 17 | s32 mfn = GET_PLL_MFN(reg); |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 18 | u32 mfd = GET_PLL_MFD(reg); |
| 19 | u32 pd = GET_PLL_PD(reg); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 20 | |
| 21 | mfi = mfi <= 5 ? 5 : mfi; |
Benoît Thébaudeau | 9111653 | 2012-08-14 08:43:47 +0000 | [diff] [blame] | 22 | mfn = mfn >= 512 ? mfn - 1024 : mfn; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 23 | mfd += 1; |
| 24 | pd += 1; |
| 25 | |
Benoît Thébaudeau | 9111653 | 2012-08-14 08:43:47 +0000 | [diff] [blame] | 26 | return lldiv(2 * (u64)infreq * (mfi * mfd + mfn), |
| 27 | mfd * pd); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Guennadi Liakhovetski | 08601a6 | 2008-05-08 10:09:27 +0200 | [diff] [blame] | 30 | static u32 mx31_get_mpl_dpdgck_clk(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 31 | { |
| 32 | u32 infreq; |
| 33 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 34 | if ((readl(CCM_CCMR) & CCMR_PRCS_MASK) == CCMR_FPM) |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 35 | infreq = MXC_CLK32 * 1024; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 36 | else |
Benoît Thébaudeau | 38e2f08 | 2012-08-21 11:06:03 +0000 | [diff] [blame] | 37 | infreq = MXC_HCLK; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 38 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 39 | return mx31_decode_pll(readl(CCM_MPCTL), infreq); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Guennadi Liakhovetski | 08601a6 | 2008-05-08 10:09:27 +0200 | [diff] [blame] | 42 | static u32 mx31_get_mcu_main_clk(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 43 | { |
| 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 Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 50 | static u32 mx31_get_ipg_clk(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 51 | { |
| 52 | u32 freq = mx31_get_mcu_main_clk(); |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 53 | u32 pdr0 = readl(CCM_PDR0); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 54 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 55 | freq /= GET_PDR0_MAX_PODF(pdr0) + 1; |
| 56 | freq /= GET_PDR0_IPG_PODF(pdr0) + 1; |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 57 | |
| 58 | return freq; |
| 59 | } |
| 60 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 61 | /* hsp is the clock for the ipu */ |
| 62 | static 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 Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 72 | void mx31_dump_clocks(void) |
| 73 | { |
| 74 | u32 cpufreq = mx31_get_mcu_main_clk(); |
Fabio Estevam | 8589866 | 2011-11-09 04:15:03 +0000 | [diff] [blame] | 75 | printf("mx31 cpu clock: %dMHz\n", cpufreq / 1000000); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 76 | printf("ipg clock : %dHz\n", mx31_get_ipg_clk()); |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 77 | printf("hsp clock : %dHz\n", mx31_get_hsp_clk()); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Stefano Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 80 | unsigned 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 Babic | 2def40f | 2011-08-30 00:51:13 +0000 | [diff] [blame] | 86 | case MXC_IPG_PERCLK: |
Stefano Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 87 | case MXC_CSPI_CLK: |
| 88 | case MXC_UART_CLK: |
Helmut Raiger | 64c316d | 2012-01-11 03:59:22 +0000 | [diff] [blame] | 89 | case MXC_ESDHC_CLK: |
Matthias Weisser | 99ba342 | 2012-09-24 02:46:53 +0000 | [diff] [blame] | 90 | case MXC_I2C_CLK: |
Stefano Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 91 | return mx31_get_ipg_clk(); |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 92 | case MXC_IPU_CLK: |
| 93 | return mx31_get_hsp_clk(); |
Stefano Babic | 43dc3f0 | 2011-07-13 14:34:52 +0200 | [diff] [blame] | 94 | } |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | u32 imx_get_uartclk(void) |
| 99 | { |
| 100 | return mxc_get_clock(MXC_UART_CLK); |
| 101 | } |
| 102 | |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 103 | void mx31_gpio_mux(unsigned long mode) |
| 104 | { |
| 105 | unsigned long reg, shift, tmp; |
| 106 | |
Magnus Lilja | 532c158 | 2008-08-03 21:44:10 +0200 | [diff] [blame] | 107 | reg = IOMUXC_BASE + (mode & 0x1fc); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 108 | shift = (~mode & 0x3) * 8; |
| 109 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 110 | tmp = readl(reg); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 111 | tmp &= ~(0xff << shift); |
Magnus Lilja | 532c158 | 2008-08-03 21:44:10 +0200 | [diff] [blame] | 112 | tmp |= ((mode >> IOMUX_MODE_POS) & 0xff) << shift; |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 113 | writel(tmp, reg); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 116 | void mx31_set_pad(enum iomux_pins pin, u32 config) |
| 117 | { |
Stefano Babic | 5f09b92 | 2010-10-19 20:19:13 +0200 | [diff] [blame] | 118 | u32 field, l, reg; |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 119 | |
| 120 | pin &= IOMUX_PADNUM_MASK; |
| 121 | reg = (IOMUXC_BASE + 0x154) + (pin + 2) / 3 * 4; |
| 122 | field = (pin + 2) % 3; |
| 123 | |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 124 | l = readl(reg); |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 125 | l &= ~(0x1ff << (field * 10)); |
| 126 | l |= config << (field * 10); |
Helmut Raiger | abd2343 | 2011-10-12 23:08:30 +0200 | [diff] [blame] | 127 | writel(l, reg); |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 128 | |
Fabio Estevam | 87db8c9 | 2011-10-20 16:01:29 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void mx31_set_gpr(enum iomux_gp_func gp, char en) |
| 132 | { |
| 133 | u32 l; |
Fabio Estevam | 3fca691 | 2011-11-09 04:15:02 +0000 | [diff] [blame] | 134 | struct iomuxc_regs *iomuxc = (struct iomuxc_regs *)IOMUXC_BASE; |
Fabio Estevam | 87db8c9 | 2011-10-20 16:01:29 +0000 | [diff] [blame] | 135 | |
Fabio Estevam | 3fca691 | 2011-11-09 04:15:02 +0000 | [diff] [blame] | 136 | l = readl(&iomuxc->gpr); |
Fabio Estevam | 87db8c9 | 2011-10-20 16:01:29 +0000 | [diff] [blame] | 137 | if (en) |
| 138 | l |= gp; |
| 139 | else |
| 140 | l &= ~gp; |
| 141 | |
Fabio Estevam | 3fca691 | 2011-11-09 04:15:02 +0000 | [diff] [blame] | 142 | writel(l, &iomuxc->gpr); |
Stefano Babic | 6272c7e | 2010-10-06 08:59:26 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Helmut Raiger | 035929c | 2011-09-29 05:45:03 +0000 | [diff] [blame] | 145 | void 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 Estevam | 939b978 | 2011-04-11 16:18:12 +0000 | [diff] [blame] | 155 | struct mx3_cpu_type mx31_cpu_type[] = { |
Stefano Babic | 7f5a026 | 2011-04-29 08:56:27 +0200 | [diff] [blame] | 156 | { .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 Estevam | 939b978 | 2011-04-11 16:18:12 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Stefano Babic | 7f5a026 | 2011-04-29 08:56:27 +0200 | [diff] [blame] | 167 | u32 get_cpu_rev(void) |
Fabio Estevam | 939b978 | 2011-04-11 16:18:12 +0000 | [diff] [blame] | 168 | { |
| 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 Fan | 9f54fe6 | 2015-08-13 10:55:32 +0800 | [diff] [blame] | 177 | return mx31_cpu_type[i].v | (MXC_CPU_MX31 << 12); |
Stefano Babic | 7f5a026 | 2011-04-29 08:56:27 +0200 | [diff] [blame] | 178 | |
| 179 | return srev | 0x8000; |
Fabio Estevam | 939b978 | 2011-04-11 16:18:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Stefano Babic | bd94dd2 | 2011-05-17 13:45:41 +0200 | [diff] [blame] | 182 | static char *get_reset_cause(void) |
Fabio Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 183 | { |
| 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 Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 193 | case 0x0001: |
| 194 | return "RST"; |
Fabio Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 195 | case 0x0002: |
| 196 | return "WDOG"; |
Fabio Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 197 | case 0x0006: |
| 198 | return "JTAG"; |
Helmut Raiger | 9468db4 | 2012-02-15 22:44:34 +0000 | [diff] [blame] | 199 | case 0x0007: |
| 200 | return "ARM11P power gating"; |
Fabio Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 201 | default: |
| 202 | return "unknown reset"; |
| 203 | } |
| 204 | } |
| 205 | |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 206 | #if defined(CONFIG_DISPLAY_CPUINFO) |
Fabio Estevam | 8589866 | 2011-11-09 04:15:03 +0000 | [diff] [blame] | 207 | int print_cpuinfo(void) |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 208 | { |
Stefano Babic | 7f5a026 | 2011-04-29 08:56:27 +0200 | [diff] [blame] | 209 | u32 srev = get_cpu_rev(); |
| 210 | |
Fabio Estevam | 298a647 | 2011-09-16 04:01:22 +0000 | [diff] [blame] | 211 | printf("CPU: Freescale i.MX31 rev %d.%d%s at %d MHz.\n", |
Stefano Babic | 7f5a026 | 2011-04-29 08:56:27 +0200 | [diff] [blame] | 212 | (srev & 0xF0) >> 4, (srev & 0x0F), |
| 213 | ((srev & 0x8000) ? " unknown" : ""), |
| 214 | mx31_get_mcu_main_clk() / 1000000); |
Fabio Estevam | 2b0fa45 | 2011-04-18 07:38:11 +0000 | [diff] [blame] | 215 | printf("Reset cause: %s\n", get_reset_cause()); |
Sascha Hauer | 1a7676f | 2008-03-26 20:40:42 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | #endif |