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