Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 DENX Software Engineering |
| 4 | * Author: John Rigby <jrigby@gmail.com> |
| 5 | * |
| 6 | * Based on mx27/generic.c: |
| 7 | * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org> |
| 8 | * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com> |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <div64.h> |
| 13 | #include <netdev.h> |
Simon Glass | f5c208d | 2019-11-14 12:57:20 -0700 | [diff] [blame] | 14 | #include <vsprintf.h> |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 15 | #include <asm/io.h> |
Adrian Alonso | a7209a2 | 2015-10-12 13:48:07 -0500 | [diff] [blame] | 16 | #include <asm/arch-imx/cpu.h> |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 17 | #include <asm/arch/imx-regs.h> |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 19 | |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 20 | #ifdef CONFIG_FSL_ESDHC_IMX |
| 21 | #include <fsl_esdhc_imx.h> |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 22 | |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | #endif |
| 25 | |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 26 | /* |
| 27 | * get the system pll clock in Hz |
| 28 | * |
| 29 | * mfi + mfn / (mfd +1) |
| 30 | * f = 2 * f_ref * -------------------- |
| 31 | * pd + 1 |
| 32 | */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 33 | static unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 34 | { |
| 35 | unsigned int mfi = (pll >> CCM_PLL_MFI_SHIFT) |
| 36 | & CCM_PLL_MFI_MASK; |
Benoît Thébaudeau | aa1cf2f | 2012-09-27 10:26:54 +0000 | [diff] [blame] | 37 | int mfn = (pll >> CCM_PLL_MFN_SHIFT) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 38 | & CCM_PLL_MFN_MASK; |
| 39 | unsigned int mfd = (pll >> CCM_PLL_MFD_SHIFT) |
| 40 | & CCM_PLL_MFD_MASK; |
| 41 | unsigned int pd = (pll >> CCM_PLL_PD_SHIFT) |
| 42 | & CCM_PLL_PD_MASK; |
| 43 | |
| 44 | mfi = mfi <= 5 ? 5 : mfi; |
Benoît Thébaudeau | aa1cf2f | 2012-09-27 10:26:54 +0000 | [diff] [blame] | 45 | mfn = mfn >= 512 ? mfn - 1024 : mfn; |
| 46 | mfd += 1; |
| 47 | pd += 1; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 48 | |
Benoît Thébaudeau | aa1cf2f | 2012-09-27 10:26:54 +0000 | [diff] [blame] | 49 | return lldiv(2 * (u64) f_ref * (mfi * mfd + mfn), |
| 50 | mfd * pd); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 53 | static ulong imx_get_mpllclk(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 54 | { |
| 55 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
Benoît Thébaudeau | d2dd29d | 2012-08-21 11:05:12 +0000 | [diff] [blame] | 56 | ulong fref = MXC_HCLK; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 57 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 58 | return imx_decode_pll(readl(&ccm->mpctl), fref); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Benoît Thébaudeau | 9f2aa98 | 2017-05-03 11:59:04 +0200 | [diff] [blame] | 61 | static ulong imx_get_upllclk(void) |
| 62 | { |
| 63 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
| 64 | ulong fref = MXC_HCLK; |
| 65 | |
| 66 | return imx_decode_pll(readl(&ccm->upctl), fref); |
| 67 | } |
| 68 | |
Benoît Thébaudeau | b3ab139 | 2012-09-27 10:27:57 +0000 | [diff] [blame] | 69 | static ulong imx_get_armclk(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 70 | { |
| 71 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 72 | ulong cctl = readl(&ccm->cctl); |
| 73 | ulong fref = imx_get_mpllclk(); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 74 | ulong div; |
| 75 | |
| 76 | if (cctl & CCM_CCTL_ARM_SRC) |
Benoît Thébaudeau | 48bffe0 | 2012-09-27 10:27:14 +0000 | [diff] [blame] | 77 | fref = lldiv((u64) fref * 3, 4); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 78 | |
| 79 | div = ((cctl >> CCM_CCTL_ARM_DIV_SHIFT) |
| 80 | & CCM_CCTL_ARM_DIV_MASK) + 1; |
| 81 | |
Benoît Thébaudeau | 48bffe0 | 2012-09-27 10:27:14 +0000 | [diff] [blame] | 82 | return fref / div; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Benoît Thébaudeau | b3ab139 | 2012-09-27 10:27:57 +0000 | [diff] [blame] | 85 | static ulong imx_get_ahbclk(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 86 | { |
| 87 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 88 | ulong cctl = readl(&ccm->cctl); |
| 89 | ulong fref = imx_get_armclk(); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 90 | ulong div; |
| 91 | |
| 92 | div = ((cctl >> CCM_CCTL_AHB_DIV_SHIFT) |
| 93 | & CCM_CCTL_AHB_DIV_MASK) + 1; |
| 94 | |
Benoît Thébaudeau | 48bffe0 | 2012-09-27 10:27:14 +0000 | [diff] [blame] | 95 | return fref / div; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Benoît Thébaudeau | 05dd78f | 2012-09-27 10:27:28 +0000 | [diff] [blame] | 98 | static ulong imx_get_ipgclk(void) |
| 99 | { |
| 100 | return imx_get_ahbclk() / 2; |
| 101 | } |
| 102 | |
Benoît Thébaudeau | b3ab139 | 2012-09-27 10:27:57 +0000 | [diff] [blame] | 103 | static ulong imx_get_perclk(int clk) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 104 | { |
| 105 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
Benoît Thébaudeau | 9f2aa98 | 2017-05-03 11:59:04 +0200 | [diff] [blame] | 106 | ulong fref = readl(&ccm->mcr) & (1 << clk) ? imx_get_upllclk() : |
| 107 | imx_get_ahbclk(); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 108 | ulong div; |
| 109 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 110 | div = readl(&ccm->pcdr[CCM_PERCLK_REG(clk)]); |
| 111 | div = ((div >> CCM_PERCLK_SHIFT(clk)) & CCM_PERCLK_MASK) + 1; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 112 | |
Benoît Thébaudeau | 48bffe0 | 2012-09-27 10:27:14 +0000 | [diff] [blame] | 113 | return fref / div; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Benoît Thébaudeau | 9d69424 | 2017-05-03 11:59:05 +0200 | [diff] [blame] | 116 | int imx_set_perclk(enum mxc_clock clk, bool from_upll, unsigned int freq) |
| 117 | { |
| 118 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
| 119 | ulong fref = from_upll ? imx_get_upllclk() : imx_get_ahbclk(); |
| 120 | ulong div = (fref + freq - 1) / freq; |
| 121 | |
| 122 | if (clk > MXC_UART_CLK || !div || --div > CCM_PERCLK_MASK) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | clrsetbits_le32(&ccm->pcdr[CCM_PERCLK_REG(clk)], |
| 126 | CCM_PERCLK_MASK << CCM_PERCLK_SHIFT(clk), |
| 127 | div << CCM_PERCLK_SHIFT(clk)); |
| 128 | if (from_upll) |
| 129 | setbits_le32(&ccm->mcr, 1 << clk); |
| 130 | else |
| 131 | clrbits_le32(&ccm->mcr, 1 << clk); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 135 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 136 | { |
| 137 | if (clk >= MXC_CLK_NUM) |
| 138 | return -1; |
| 139 | switch (clk) { |
| 140 | case MXC_ARM_CLK: |
| 141 | return imx_get_armclk(); |
Benoît Thébaudeau | 05dd78f | 2012-09-27 10:27:28 +0000 | [diff] [blame] | 142 | case MXC_AHB_CLK: |
| 143 | return imx_get_ahbclk(); |
| 144 | case MXC_IPG_CLK: |
| 145 | case MXC_CSPI_CLK: |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 146 | case MXC_FEC_CLK: |
Benoît Thébaudeau | 88a2382 | 2012-09-27 10:27:44 +0000 | [diff] [blame] | 147 | return imx_get_ipgclk(); |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 148 | default: |
| 149 | return imx_get_perclk(clk); |
| 150 | } |
| 151 | } |
| 152 | |
Fabio Estevam | 51f2354 | 2011-09-02 05:38:54 +0000 | [diff] [blame] | 153 | u32 get_cpu_rev(void) |
| 154 | { |
| 155 | u32 srev; |
| 156 | u32 system_rev = 0x25000; |
| 157 | |
| 158 | /* read SREV register from IIM module */ |
| 159 | struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE; |
| 160 | srev = readl(&iim->iim_srev); |
| 161 | |
| 162 | switch (srev) { |
| 163 | case 0x00: |
| 164 | system_rev |= CHIP_REV_1_0; |
| 165 | break; |
| 166 | case 0x01: |
| 167 | system_rev |= CHIP_REV_1_1; |
| 168 | break; |
Eric Benard | c47d73f | 2012-09-23 02:03:05 +0000 | [diff] [blame] | 169 | case 0x02: |
| 170 | system_rev |= CHIP_REV_1_2; |
| 171 | break; |
Fabio Estevam | 51f2354 | 2011-09-02 05:38:54 +0000 | [diff] [blame] | 172 | default: |
| 173 | system_rev |= 0x8000; |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | return system_rev; |
| 178 | } |
| 179 | |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 180 | #if defined(CONFIG_DISPLAY_CPUINFO) |
Fabio Estevam | 4c6b02e | 2011-09-23 05:13:22 +0000 | [diff] [blame] | 181 | static char *get_reset_cause(void) |
| 182 | { |
| 183 | /* read RCSR register from CCM module */ |
| 184 | struct ccm_regs *ccm = |
| 185 | (struct ccm_regs *)IMX_CCM_BASE; |
| 186 | |
| 187 | u32 cause = readl(&ccm->rcsr) & 0x0f; |
| 188 | |
| 189 | if (cause == 0) |
| 190 | return "POR"; |
| 191 | else if (cause == 1) |
| 192 | return "RST"; |
| 193 | else if ((cause & 2) == 2) |
| 194 | return "WDOG"; |
| 195 | else if ((cause & 4) == 4) |
| 196 | return "SW RESET"; |
| 197 | else if ((cause & 8) == 8) |
| 198 | return "JTAG"; |
| 199 | else |
| 200 | return "unknown reset"; |
| 201 | |
| 202 | } |
| 203 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 204 | int print_cpuinfo(void) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 205 | { |
| 206 | char buf[32]; |
Fabio Estevam | 51f2354 | 2011-09-02 05:38:54 +0000 | [diff] [blame] | 207 | u32 cpurev = get_cpu_rev(); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 208 | |
Fabio Estevam | 9a42324 | 2011-09-02 05:38:55 +0000 | [diff] [blame] | 209 | printf("CPU: Freescale i.MX25 rev%d.%d%s at %s MHz\n", |
Fabio Estevam | 51f2354 | 2011-09-02 05:38:54 +0000 | [diff] [blame] | 210 | (cpurev & 0xF0) >> 4, (cpurev & 0x0F), |
| 211 | ((cpurev & 0x8000) ? " unknown" : ""), |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 212 | strmhz(buf, imx_get_armclk())); |
Fabio Estevam | 9882e20 | 2015-01-06 14:10:05 -0200 | [diff] [blame] | 213 | printf("Reset cause: %s\n", get_reset_cause()); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | #endif |
| 217 | |
Benoît Thébaudeau | 6991d6a | 2012-09-27 10:28:09 +0000 | [diff] [blame] | 218 | #if defined(CONFIG_FEC_MXC) |
| 219 | /* |
| 220 | * Initializes on-chip ethernet controllers. |
| 221 | * to override, implement board_eth_init() |
| 222 | */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 223 | int cpu_eth_init(bd_t *bis) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 224 | { |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 225 | struct ccm_regs *ccm = (struct ccm_regs *)IMX_CCM_BASE; |
| 226 | ulong val; |
| 227 | |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 228 | val = readl(&ccm->cgr0); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 229 | val |= (1 << 23); |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 230 | writel(val, &ccm->cgr0); |
| 231 | return fecmxc_initialize(bis); |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 232 | } |
Benoît Thébaudeau | 6991d6a | 2012-09-27 10:28:09 +0000 | [diff] [blame] | 233 | #endif |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 234 | |
| 235 | int get_clocks(void) |
| 236 | { |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 237 | #ifdef CONFIG_FSL_ESDHC_IMX |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 238 | #if CONFIG_SYS_FSL_ESDHC_ADDR == IMX_MMC_SDHC2_BASE |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 239 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK); |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 240 | #else |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 241 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC1_CLK); |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 242 | #endif |
Timo Ketola | 738fa8d | 2012-04-18 22:55:28 +0000 | [diff] [blame] | 243 | #endif |
| 244 | return 0; |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Yangbo Lu | 7334038 | 2019-06-21 11:42:28 +0800 | [diff] [blame] | 247 | #ifdef CONFIG_FSL_ESDHC_IMX |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Initializes on-chip MMC controllers. |
| 250 | * to override, implement board_mmc_init() |
| 251 | */ |
Fabio Estevam | f231efb | 2011-10-13 05:34:59 +0000 | [diff] [blame] | 252 | int cpu_mmc_init(bd_t *bis) |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 253 | { |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 254 | return fsl_esdhc_mmc_init(bis); |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 255 | } |
Benoît Thébaudeau | 9564605 | 2012-09-27 10:28:29 +0000 | [diff] [blame] | 256 | #endif |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 257 | |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_FEC_MXC |
Fabio Estevam | 04fc128 | 2011-12-20 05:46:31 +0000 | [diff] [blame] | 259 | void imx_get_mac_from_fuse(int dev_id, unsigned char *mac) |
Liu Hui-R64343 | 4df6619 | 2010-11-18 23:45:55 +0000 | [diff] [blame] | 260 | { |
| 261 | int i; |
| 262 | struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE; |
| 263 | struct fuse_bank *bank = &iim->bank[0]; |
| 264 | struct fuse_bank0_regs *fuse = |
| 265 | (struct fuse_bank0_regs *)bank->fuse_regs; |
| 266 | |
| 267 | for (i = 0; i < 6; i++) |
| 268 | mac[i] = readl(&fuse->mac_addr[i]) & 0xff; |
| 269 | } |
John Rigby | 9c14603 | 2010-01-25 23:12:56 -0700 | [diff] [blame] | 270 | #endif /* CONFIG_FEC_MXC */ |