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