Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Sascha Hauer, Pengutronix |
| 4 | * |
| 5 | * (C) Copyright 2009 Freescale Semiconductor, Inc. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <asm/io.h> |
| 28 | #include <asm/errno.h> |
| 29 | #include <asm/arch/imx-regs.h> |
| 30 | #include <asm/arch/crm_regs.h> |
Stefano Babic | ac41d4d | 2010-03-05 17:54:37 +0100 | [diff] [blame] | 31 | #include <asm/arch/clock.h> |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 32 | #include <div64.h> |
Fabio Estevam | 6479f51 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 33 | #include <asm/arch/sys_proto.h> |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 34 | |
| 35 | enum pll_clocks { |
| 36 | PLL1_CLOCK = 0, |
| 37 | PLL2_CLOCK, |
| 38 | PLL3_CLOCK, |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 39 | #ifdef CONFIG_MX53 |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 40 | PLL4_CLOCK, |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 41 | #endif |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 42 | PLL_CLOCKS, |
| 43 | }; |
| 44 | |
| 45 | struct mxc_pll_reg *mxc_plls[PLL_CLOCKS] = { |
| 46 | [PLL1_CLOCK] = (struct mxc_pll_reg *)PLL1_BASE_ADDR, |
| 47 | [PLL2_CLOCK] = (struct mxc_pll_reg *)PLL2_BASE_ADDR, |
| 48 | [PLL3_CLOCK] = (struct mxc_pll_reg *)PLL3_BASE_ADDR, |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_MX53 |
| 50 | [PLL4_CLOCK] = (struct mxc_pll_reg *)PLL4_BASE_ADDR, |
| 51 | #endif |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 54 | #define AHB_CLK_ROOT 133333333 |
| 55 | #define SZ_DEC_1M 1000000 |
| 56 | #define PLL_PD_MAX 16 /* Actual pd+1 */ |
| 57 | #define PLL_MFI_MAX 15 |
| 58 | #define PLL_MFI_MIN 5 |
| 59 | #define ARM_DIV_MAX 8 |
| 60 | #define IPG_DIV_MAX 4 |
| 61 | #define AHB_DIV_MAX 8 |
| 62 | #define EMI_DIV_MAX 8 |
| 63 | #define NFC_DIV_MAX 8 |
| 64 | |
| 65 | #define MX5_CBCMR 0x00015154 |
| 66 | #define MX5_CBCDR 0x02888945 |
| 67 | |
| 68 | struct fixed_pll_mfd { |
| 69 | u32 ref_clk_hz; |
| 70 | u32 mfd; |
| 71 | }; |
| 72 | |
| 73 | const struct fixed_pll_mfd fixed_mfd[] = { |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 74 | {MXC_HCLK, 24 * 16}, |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct pll_param { |
| 78 | u32 pd; |
| 79 | u32 mfi; |
| 80 | u32 mfn; |
| 81 | u32 mfd; |
| 82 | }; |
| 83 | |
| 84 | #define PLL_FREQ_MAX(ref_clk) (4 * (ref_clk) * PLL_MFI_MAX) |
| 85 | #define PLL_FREQ_MIN(ref_clk) \ |
| 86 | ((2 * (ref_clk) * (PLL_MFI_MIN - 1)) / PLL_PD_MAX) |
| 87 | #define MAX_DDR_CLK 420000000 |
| 88 | #define NFC_CLK_MAX 34000000 |
| 89 | |
Stefano Babic | ac41d4d | 2010-03-05 17:54:37 +0100 | [diff] [blame] | 90 | struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 91 | |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 92 | void set_usboh3_clk(void) |
| 93 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 94 | clrsetbits_le32(&mxc_ccm->cscmr1, |
| 95 | MXC_CCM_CSCMR1_USBOH3_CLK_SEL_MASK, |
| 96 | MXC_CCM_CSCMR1_USBOH3_CLK_SEL(1)); |
| 97 | clrsetbits_le32(&mxc_ccm->cscdr1, |
| 98 | MXC_CCM_CSCDR1_USBOH3_CLK_PODF_MASK | |
| 99 | MXC_CCM_CSCDR1_USBOH3_CLK_PRED_MASK, |
| 100 | MXC_CCM_CSCDR1_USBOH3_CLK_PRED(4) | |
| 101 | MXC_CCM_CSCDR1_USBOH3_CLK_PODF(1)); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void enable_usboh3_clk(unsigned char enable) |
| 105 | { |
Benoît Thébaudeau | 6dfc630 | 2012-09-27 10:21:22 +0000 | [diff] [blame] | 106 | unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF; |
| 107 | |
| 108 | clrsetbits_le32(&mxc_ccm->CCGR2, |
| 109 | MXC_CCM_CCGR2_USBOH3_60M(MXC_CCM_CCGR_CG_MASK), |
| 110 | MXC_CCM_CCGR2_USBOH3_60M(cg)); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 113 | #ifdef CONFIG_I2C_MXC |
Benoît Thébaudeau | a23408c | 2012-09-27 10:24:13 +0000 | [diff] [blame] | 114 | /* i2c_num can be from 0, to 1 for i.MX51 and 2 for i.MX53 */ |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 115 | int enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
| 116 | { |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 117 | u32 mask; |
| 118 | |
Benoît Thébaudeau | a23408c | 2012-09-27 10:24:13 +0000 | [diff] [blame] | 119 | #if defined(CONFIG_MX51) |
| 120 | if (i2c_num > 1) |
| 121 | #elif defined(CONFIG_MX53) |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 122 | if (i2c_num > 2) |
Benoît Thébaudeau | a23408c | 2012-09-27 10:24:13 +0000 | [diff] [blame] | 123 | #endif |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 124 | return -EINVAL; |
Benoît Thébaudeau | 461a00a | 2012-09-27 10:21:00 +0000 | [diff] [blame] | 125 | mask = MXC_CCM_CCGR_CG_MASK << |
| 126 | (MXC_CCM_CCGR1_I2C1_OFFSET + (i2c_num << 1)); |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 127 | if (enable) |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 128 | setbits_le32(&mxc_ccm->CCGR1, mask); |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 129 | else |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 130 | clrbits_le32(&mxc_ccm->CCGR1, mask); |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | #endif |
| 134 | |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 135 | void set_usb_phy_clk(void) |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 136 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 137 | clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 140 | #if defined(CONFIG_MX51) |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 141 | void enable_usb_phy1_clk(unsigned char enable) |
| 142 | { |
Benoît Thébaudeau | 6dfc630 | 2012-09-27 10:21:22 +0000 | [diff] [blame] | 143 | unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF; |
| 144 | |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 145 | clrsetbits_le32(&mxc_ccm->CCGR2, |
| 146 | MXC_CCM_CCGR2_USB_PHY(MXC_CCM_CCGR_CG_MASK), |
| 147 | MXC_CCM_CCGR2_USB_PHY(cg)); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 150 | void enable_usb_phy2_clk(unsigned char enable) |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 151 | { |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 152 | /* i.MX51 has a single USB PHY clock, so do nothing here. */ |
| 153 | } |
| 154 | #elif defined(CONFIG_MX53) |
| 155 | void enable_usb_phy1_clk(unsigned char enable) |
| 156 | { |
| 157 | unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF; |
| 158 | |
| 159 | clrsetbits_le32(&mxc_ccm->CCGR4, |
| 160 | MXC_CCM_CCGR4_USB_PHY1(MXC_CCM_CCGR_CG_MASK), |
| 161 | MXC_CCM_CCGR4_USB_PHY1(cg)); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void enable_usb_phy2_clk(unsigned char enable) |
| 165 | { |
Benoît Thébaudeau | 6dfc630 | 2012-09-27 10:21:22 +0000 | [diff] [blame] | 166 | unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF; |
| 167 | |
| 168 | clrsetbits_le32(&mxc_ccm->CCGR4, |
| 169 | MXC_CCM_CCGR4_USB_PHY2(MXC_CCM_CCGR_CG_MASK), |
| 170 | MXC_CCM_CCGR4_USB_PHY2(cg)); |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 171 | } |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 172 | #endif |
Wolfgang Grandegger | 8c1e4d3 | 2011-11-11 14:03:34 +0100 | [diff] [blame] | 173 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 174 | /* |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 175 | * Calculate the frequency of PLLn. |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 176 | */ |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 177 | static uint32_t decode_pll(struct mxc_pll_reg *pll, uint32_t infreq) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 178 | { |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 179 | uint32_t ctrl, op, mfd, mfn, mfi, pdf, ret; |
| 180 | uint64_t refclk, temp; |
| 181 | int32_t mfn_abs; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 182 | |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 183 | ctrl = readl(&pll->ctrl); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 184 | |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 185 | if (ctrl & MXC_DPLLC_CTL_HFSM) { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 186 | mfn = readl(&pll->hfs_mfn); |
| 187 | mfd = readl(&pll->hfs_mfd); |
| 188 | op = readl(&pll->hfs_op); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 189 | } else { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 190 | mfn = readl(&pll->mfn); |
| 191 | mfd = readl(&pll->mfd); |
| 192 | op = readl(&pll->op); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | mfd &= MXC_DPLLC_MFD_MFD_MASK; |
| 196 | mfn &= MXC_DPLLC_MFN_MFN_MASK; |
| 197 | pdf = op & MXC_DPLLC_OP_PDF_MASK; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 198 | mfi = MXC_DPLLC_OP_MFI_RD(op); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 199 | |
| 200 | /* 21.2.3 */ |
| 201 | if (mfi < 5) |
| 202 | mfi = 5; |
| 203 | |
| 204 | /* Sign extend */ |
| 205 | if (mfn >= 0x04000000) { |
| 206 | mfn |= 0xfc000000; |
| 207 | mfn_abs = -mfn; |
| 208 | } else |
| 209 | mfn_abs = mfn; |
| 210 | |
| 211 | refclk = infreq * 2; |
| 212 | if (ctrl & MXC_DPLLC_CTL_DPDCK0_2_EN) |
| 213 | refclk *= 2; |
| 214 | |
Simon Glass | 3d55788 | 2011-11-05 04:25:22 +0000 | [diff] [blame] | 215 | do_div(refclk, pdf + 1); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 216 | temp = refclk * mfn_abs; |
| 217 | do_div(temp, mfd + 1); |
| 218 | ret = refclk * mfi; |
| 219 | |
| 220 | if ((int)mfn < 0) |
| 221 | ret -= temp; |
| 222 | else |
| 223 | ret += temp; |
| 224 | |
| 225 | return ret; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Benoît Thébaudeau | 9c05cc7 | 2012-09-27 10:22:37 +0000 | [diff] [blame] | 228 | #ifdef CONFIG_MX51 |
| 229 | /* |
| 230 | * This function returns the Frequency Pre-Multiplier clock. |
| 231 | */ |
| 232 | static u32 get_fpm(void) |
| 233 | { |
| 234 | u32 mult; |
| 235 | u32 ccr = readl(&mxc_ccm->ccr); |
| 236 | |
| 237 | if (ccr & MXC_CCM_CCR_FPM_MULT) |
| 238 | mult = 1024; |
| 239 | else |
| 240 | mult = 512; |
| 241 | |
| 242 | return MXC_CLK32 * mult; |
| 243 | } |
| 244 | #endif |
| 245 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 246 | /* |
Benoît Thébaudeau | 96d9df3 | 2012-09-27 10:22:51 +0000 | [diff] [blame] | 247 | * This function returns the low power audio clock. |
| 248 | */ |
| 249 | static u32 get_lp_apm(void) |
| 250 | { |
| 251 | u32 ret_val = 0; |
| 252 | u32 ccsr = readl(&mxc_ccm->ccsr); |
| 253 | |
| 254 | if (ccsr & MXC_CCM_CCSR_LP_APM) |
| 255 | #if defined(CONFIG_MX51) |
| 256 | ret_val = get_fpm(); |
| 257 | #elif defined(CONFIG_MX53) |
| 258 | ret_val = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK); |
| 259 | #endif |
| 260 | else |
| 261 | ret_val = MXC_HCLK; |
| 262 | |
| 263 | return ret_val; |
| 264 | } |
| 265 | |
| 266 | /* |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 267 | * Get mcu main rate |
| 268 | */ |
| 269 | u32 get_mcu_main_clk(void) |
| 270 | { |
| 271 | u32 reg, freq; |
| 272 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 273 | reg = MXC_CCM_CACRR_ARM_PODF_RD(readl(&mxc_ccm->cacrr)); |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 274 | freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 275 | return freq / (reg + 1); |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Get the rate of peripheral's root clock. |
| 280 | */ |
Fabio Estevam | 6479f51 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 281 | u32 get_periph_clk(void) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 282 | { |
| 283 | u32 reg; |
| 284 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 285 | reg = readl(&mxc_ccm->cbcdr); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 286 | if (!(reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL)) |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 287 | return decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK); |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 288 | reg = readl(&mxc_ccm->cbcmr); |
| 289 | switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(reg)) { |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 290 | case 0: |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 291 | return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 292 | case 1: |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 293 | return decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK); |
Benoît Thébaudeau | 96d9df3 | 2012-09-27 10:22:51 +0000 | [diff] [blame] | 294 | case 2: |
| 295 | return get_lp_apm(); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 296 | default: |
| 297 | return 0; |
| 298 | } |
| 299 | /* NOTREACHED */ |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Get the rate of ipg clock. |
| 304 | */ |
| 305 | static u32 get_ipg_clk(void) |
| 306 | { |
Marek Vasut | 6674bf5 | 2011-09-22 09:20:37 +0000 | [diff] [blame] | 307 | uint32_t freq, reg, div; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 308 | |
Marek Vasut | 6674bf5 | 2011-09-22 09:20:37 +0000 | [diff] [blame] | 309 | freq = get_ahb_clk(); |
| 310 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 311 | reg = readl(&mxc_ccm->cbcdr); |
| 312 | div = MXC_CCM_CBCDR_IPG_PODF_RD(reg) + 1; |
Marek Vasut | 6674bf5 | 2011-09-22 09:20:37 +0000 | [diff] [blame] | 313 | |
| 314 | return freq / div; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Get the rate of ipg_per clock. |
| 319 | */ |
| 320 | static u32 get_ipg_per_clk(void) |
| 321 | { |
Benoît Thébaudeau | 0cef3ae | 2012-09-27 10:23:08 +0000 | [diff] [blame] | 322 | u32 freq, pred1, pred2, podf; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 323 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 324 | if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_IPG_CLK_SEL) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 325 | return get_ipg_clk(); |
Benoît Thébaudeau | 0cef3ae | 2012-09-27 10:23:08 +0000 | [diff] [blame] | 326 | |
| 327 | if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_LP_APM_CLK_SEL) |
| 328 | freq = get_lp_apm(); |
| 329 | else |
| 330 | freq = get_periph_clk(); |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 331 | podf = readl(&mxc_ccm->cbcdr); |
| 332 | pred1 = MXC_CCM_CBCDR_PERCLK_PRED1_RD(podf); |
| 333 | pred2 = MXC_CCM_CBCDR_PERCLK_PRED2_RD(podf); |
| 334 | podf = MXC_CCM_CBCDR_PERCLK_PODF_RD(podf); |
Benoît Thébaudeau | 0cef3ae | 2012-09-27 10:23:08 +0000 | [diff] [blame] | 335 | return freq / ((pred1 + 1) * (pred2 + 1) * (podf + 1)); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 338 | /* Get the output clock rate of a standard PLL MUX for peripherals. */ |
| 339 | static u32 get_standard_pll_sel_clk(u32 clk_sel) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 340 | { |
Stefano Babic | fd375f3 | 2012-10-24 10:06:28 +0200 | [diff] [blame] | 341 | u32 freq = 0; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 342 | |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 343 | switch (clk_sel & 0x3) { |
| 344 | case 0: |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 345 | freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 346 | break; |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 347 | case 1: |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 348 | freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 349 | break; |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 350 | case 2: |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 351 | freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 352 | break; |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 353 | case 3: |
| 354 | freq = get_lp_apm(); |
| 355 | break; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Benoît Thébaudeau | 9aa99c4 | 2012-09-27 10:23:23 +0000 | [diff] [blame] | 358 | return freq; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Get the rate of uart clk. |
| 363 | */ |
| 364 | static u32 get_uart_clk(void) |
| 365 | { |
| 366 | unsigned int clk_sel, freq, reg, pred, podf; |
| 367 | |
| 368 | reg = readl(&mxc_ccm->cscmr1); |
| 369 | clk_sel = MXC_CCM_CSCMR1_UART_CLK_SEL_RD(reg); |
| 370 | freq = get_standard_pll_sel_clk(clk_sel); |
| 371 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 372 | reg = readl(&mxc_ccm->cscdr1); |
| 373 | pred = MXC_CCM_CSCDR1_UART_CLK_PRED_RD(reg); |
| 374 | podf = MXC_CCM_CSCDR1_UART_CLK_PODF_RD(reg); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 375 | freq /= (pred + 1) * (podf + 1); |
| 376 | |
| 377 | return freq; |
| 378 | } |
| 379 | |
| 380 | /* |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 381 | * get cspi clock rate. |
| 382 | */ |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 383 | static u32 imx_get_cspiclk(void) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 384 | { |
Benoît Thébaudeau | 81fb274 | 2012-09-27 10:23:42 +0000 | [diff] [blame] | 385 | u32 ret_val = 0, pdf, pre_pdf, clk_sel, freq; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 386 | u32 cscmr1 = readl(&mxc_ccm->cscmr1); |
| 387 | u32 cscdr2 = readl(&mxc_ccm->cscdr2); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 388 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 389 | pre_pdf = MXC_CCM_CSCDR2_CSPI_CLK_PRED_RD(cscdr2); |
| 390 | pdf = MXC_CCM_CSCDR2_CSPI_CLK_PODF_RD(cscdr2); |
| 391 | clk_sel = MXC_CCM_CSCMR1_CSPI_CLK_SEL_RD(cscmr1); |
Benoît Thébaudeau | 81fb274 | 2012-09-27 10:23:42 +0000 | [diff] [blame] | 392 | freq = get_standard_pll_sel_clk(clk_sel); |
| 393 | ret_val = freq / ((pre_pdf + 1) * (pdf + 1)); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 394 | return ret_val; |
| 395 | } |
| 396 | |
Benoît Thébaudeau | eb9c255 | 2012-09-27 10:24:37 +0000 | [diff] [blame] | 397 | /* |
| 398 | * get esdhc clock rate. |
| 399 | */ |
| 400 | static u32 get_esdhc_clk(u32 port) |
| 401 | { |
| 402 | u32 clk_sel = 0, pred = 0, podf = 0, freq = 0; |
| 403 | u32 cscmr1 = readl(&mxc_ccm->cscmr1); |
| 404 | u32 cscdr1 = readl(&mxc_ccm->cscdr1); |
| 405 | |
| 406 | switch (port) { |
| 407 | case 0: |
| 408 | clk_sel = MXC_CCM_CSCMR1_ESDHC1_MSHC1_CLK_SEL_RD(cscmr1); |
| 409 | pred = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PRED_RD(cscdr1); |
| 410 | podf = MXC_CCM_CSCDR1_ESDHC1_MSHC1_CLK_PODF_RD(cscdr1); |
| 411 | break; |
| 412 | case 1: |
| 413 | clk_sel = MXC_CCM_CSCMR1_ESDHC2_MSHC2_CLK_SEL_RD(cscmr1); |
| 414 | pred = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PRED_RD(cscdr1); |
| 415 | podf = MXC_CCM_CSCDR1_ESDHC2_MSHC2_CLK_PODF_RD(cscdr1); |
| 416 | break; |
| 417 | case 2: |
| 418 | if (cscmr1 & MXC_CCM_CSCMR1_ESDHC3_CLK_SEL) |
| 419 | return get_esdhc_clk(1); |
| 420 | else |
| 421 | return get_esdhc_clk(0); |
| 422 | case 3: |
| 423 | if (cscmr1 & MXC_CCM_CSCMR1_ESDHC4_CLK_SEL) |
| 424 | return get_esdhc_clk(1); |
| 425 | else |
| 426 | return get_esdhc_clk(0); |
| 427 | default: |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | freq = get_standard_pll_sel_clk(clk_sel) / ((pred + 1) * (podf + 1)); |
| 432 | return freq; |
| 433 | } |
| 434 | |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 435 | static u32 get_axi_a_clk(void) |
| 436 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 437 | u32 cbcdr = readl(&mxc_ccm->cbcdr); |
| 438 | u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 439 | |
| 440 | return get_periph_clk() / (pdf + 1); |
| 441 | } |
| 442 | |
| 443 | static u32 get_axi_b_clk(void) |
| 444 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 445 | u32 cbcdr = readl(&mxc_ccm->cbcdr); |
| 446 | u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 447 | |
| 448 | return get_periph_clk() / (pdf + 1); |
| 449 | } |
| 450 | |
| 451 | static u32 get_emi_slow_clk(void) |
| 452 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 453 | u32 cbcdr = readl(&mxc_ccm->cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 454 | u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 455 | u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 456 | |
| 457 | if (emi_clk_sel) |
| 458 | return get_ahb_clk() / (pdf + 1); |
| 459 | |
| 460 | return get_periph_clk() / (pdf + 1); |
| 461 | } |
| 462 | |
| 463 | static u32 get_ddr_clk(void) |
| 464 | { |
| 465 | u32 ret_val = 0; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 466 | u32 cbcmr = readl(&mxc_ccm->cbcmr); |
| 467 | u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 468 | #ifdef CONFIG_MX51 |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 469 | u32 cbcdr = readl(&mxc_ccm->cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 470 | if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 471 | u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 472 | |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 473 | ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 474 | ret_val /= ddr_clk_podf + 1; |
| 475 | |
| 476 | return ret_val; |
| 477 | } |
| 478 | #endif |
| 479 | switch (ddr_clk_sel) { |
| 480 | case 0: |
| 481 | ret_val = get_axi_a_clk(); |
| 482 | break; |
| 483 | case 1: |
| 484 | ret_val = get_axi_b_clk(); |
| 485 | break; |
| 486 | case 2: |
| 487 | ret_val = get_emi_slow_clk(); |
| 488 | break; |
| 489 | case 3: |
| 490 | ret_val = get_ahb_clk(); |
| 491 | break; |
| 492 | default: |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | return ret_val; |
| 497 | } |
| 498 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 499 | /* |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 500 | * The API of get mxc clocks. |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 501 | */ |
| 502 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 503 | { |
| 504 | switch (clk) { |
| 505 | case MXC_ARM_CLK: |
| 506 | return get_mcu_main_clk(); |
| 507 | case MXC_AHB_CLK: |
Marek Vasut | 6674bf5 | 2011-09-22 09:20:37 +0000 | [diff] [blame] | 508 | return get_ahb_clk(); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 509 | case MXC_IPG_CLK: |
| 510 | return get_ipg_clk(); |
| 511 | case MXC_IPG_PERCLK: |
Matthias Weisser | 99ba342 | 2012-09-24 02:46:53 +0000 | [diff] [blame] | 512 | case MXC_I2C_CLK: |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 513 | return get_ipg_per_clk(); |
| 514 | case MXC_UART_CLK: |
| 515 | return get_uart_clk(); |
| 516 | case MXC_CSPI_CLK: |
| 517 | return imx_get_cspiclk(); |
Benoît Thébaudeau | eb9c255 | 2012-09-27 10:24:37 +0000 | [diff] [blame] | 518 | case MXC_ESDHC_CLK: |
| 519 | return get_esdhc_clk(0); |
| 520 | case MXC_ESDHC2_CLK: |
| 521 | return get_esdhc_clk(1); |
| 522 | case MXC_ESDHC3_CLK: |
| 523 | return get_esdhc_clk(2); |
| 524 | case MXC_ESDHC4_CLK: |
| 525 | return get_esdhc_clk(3); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 526 | case MXC_FEC_CLK: |
Benoît Thébaudeau | 743656e | 2012-09-27 10:23:58 +0000 | [diff] [blame] | 527 | return get_ipg_clk(); |
Stefano Babic | d38db76 | 2012-02-22 00:24:36 +0000 | [diff] [blame] | 528 | case MXC_SATA_CLK: |
| 529 | return get_ahb_clk(); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 530 | case MXC_DDR_CLK: |
| 531 | return get_ddr_clk(); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 532 | default: |
| 533 | break; |
| 534 | } |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 535 | return -EINVAL; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | u32 imx_get_uartclk(void) |
| 539 | { |
| 540 | return get_uart_clk(); |
| 541 | } |
| 542 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 543 | u32 imx_get_fecclk(void) |
| 544 | { |
Benoît Thébaudeau | 743656e | 2012-09-27 10:23:58 +0000 | [diff] [blame] | 545 | return get_ipg_clk(); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 548 | static int gcd(int m, int n) |
| 549 | { |
| 550 | int t; |
| 551 | while (m > 0) { |
| 552 | if (n > m) { |
| 553 | t = m; |
| 554 | m = n; |
| 555 | n = t; |
| 556 | } /* swap */ |
| 557 | m -= n; |
| 558 | } |
| 559 | return n; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * This is to calculate various parameters based on reference clock and |
| 564 | * targeted clock based on the equation: |
| 565 | * t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1) |
| 566 | * This calculation is based on a fixed MFD value for simplicity. |
| 567 | */ |
| 568 | static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll) |
| 569 | { |
| 570 | u64 pd, mfi = 1, mfn, mfd, t1; |
| 571 | u32 n_target = target; |
| 572 | u32 n_ref = ref, i; |
| 573 | |
| 574 | /* |
| 575 | * Make sure targeted freq is in the valid range. |
| 576 | * Otherwise the following calculation might be wrong!!! |
| 577 | */ |
| 578 | if (n_target < PLL_FREQ_MIN(ref) || |
| 579 | n_target > PLL_FREQ_MAX(ref)) { |
| 580 | printf("Targeted peripheral clock should be" |
| 581 | "within [%d - %d]\n", |
| 582 | PLL_FREQ_MIN(ref) / SZ_DEC_1M, |
| 583 | PLL_FREQ_MAX(ref) / SZ_DEC_1M); |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) { |
| 588 | if (fixed_mfd[i].ref_clk_hz == ref) { |
| 589 | mfd = fixed_mfd[i].mfd; |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (i == ARRAY_SIZE(fixed_mfd)) |
| 595 | return -EINVAL; |
| 596 | |
| 597 | /* Use n_target and n_ref to avoid overflow */ |
| 598 | for (pd = 1; pd <= PLL_PD_MAX; pd++) { |
| 599 | t1 = n_target * pd; |
| 600 | do_div(t1, (4 * n_ref)); |
| 601 | mfi = t1; |
| 602 | if (mfi > PLL_MFI_MAX) |
| 603 | return -EINVAL; |
| 604 | else if (mfi < 5) |
| 605 | continue; |
| 606 | break; |
| 607 | } |
| 608 | /* |
| 609 | * Now got pd and mfi already |
| 610 | * |
| 611 | * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref; |
| 612 | */ |
| 613 | t1 = n_target * pd; |
| 614 | do_div(t1, 4); |
| 615 | t1 -= n_ref * mfi; |
| 616 | t1 *= mfd; |
| 617 | do_div(t1, n_ref); |
| 618 | mfn = t1; |
| 619 | debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n", |
| 620 | ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd); |
| 621 | i = 1; |
| 622 | if (mfn != 0) |
| 623 | i = gcd(mfd, mfn); |
| 624 | pll->pd = (u32)pd; |
| 625 | pll->mfi = (u32)mfi; |
| 626 | do_div(mfn, i); |
| 627 | pll->mfn = (u32)mfn; |
| 628 | do_div(mfd, i); |
| 629 | pll->mfd = (u32)mfd; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | #define calc_div(tgt_clk, src_clk, limit) ({ \ |
| 635 | u32 v = 0; \ |
| 636 | if (((src_clk) % (tgt_clk)) <= 100) \ |
| 637 | v = (src_clk) / (tgt_clk); \ |
| 638 | else \ |
| 639 | v = ((src_clk) / (tgt_clk)) + 1;\ |
| 640 | if (v > limit) \ |
| 641 | v = limit; \ |
| 642 | (v - 1); \ |
| 643 | }) |
| 644 | |
| 645 | #define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \ |
| 646 | { \ |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 647 | writel(0x1232, &pll->ctrl); \ |
| 648 | writel(0x2, &pll->config); \ |
| 649 | writel((((pd) - 1) << 0) | ((fi) << 4), \ |
| 650 | &pll->op); \ |
| 651 | writel(fn, &(pll->mfn)); \ |
| 652 | writel((fd) - 1, &pll->mfd); \ |
| 653 | writel((((pd) - 1) << 0) | ((fi) << 4), \ |
| 654 | &pll->hfs_op); \ |
| 655 | writel(fn, &pll->hfs_mfn); \ |
| 656 | writel((fd) - 1, &pll->hfs_mfd); \ |
| 657 | writel(0x1232, &pll->ctrl); \ |
| 658 | while (!readl(&pll->ctrl) & 0x1) \ |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 659 | ;\ |
| 660 | } |
| 661 | |
| 662 | static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param) |
| 663 | { |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 664 | u32 ccsr = readl(&mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 665 | struct mxc_pll_reg *pll = mxc_plls[index]; |
| 666 | |
| 667 | switch (index) { |
| 668 | case PLL1_CLOCK: |
| 669 | /* Switch ARM to PLL2 clock */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 670 | writel(ccsr | MXC_CCM_CCSR_PLL1_SW_CLK_SEL, |
| 671 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 672 | CHANGE_PLL_SETTINGS(pll, pll_param->pd, |
| 673 | pll_param->mfi, pll_param->mfn, |
| 674 | pll_param->mfd); |
| 675 | /* Switch back */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 676 | writel(ccsr & ~MXC_CCM_CCSR_PLL1_SW_CLK_SEL, |
| 677 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 678 | break; |
| 679 | case PLL2_CLOCK: |
| 680 | /* Switch to pll2 bypass clock */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 681 | writel(ccsr | MXC_CCM_CCSR_PLL2_SW_CLK_SEL, |
| 682 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 683 | CHANGE_PLL_SETTINGS(pll, pll_param->pd, |
| 684 | pll_param->mfi, pll_param->mfn, |
| 685 | pll_param->mfd); |
| 686 | /* Switch back */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 687 | writel(ccsr & ~MXC_CCM_CCSR_PLL2_SW_CLK_SEL, |
| 688 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 689 | break; |
| 690 | case PLL3_CLOCK: |
| 691 | /* Switch to pll3 bypass clock */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 692 | writel(ccsr | MXC_CCM_CCSR_PLL3_SW_CLK_SEL, |
| 693 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 694 | CHANGE_PLL_SETTINGS(pll, pll_param->pd, |
| 695 | pll_param->mfi, pll_param->mfn, |
| 696 | pll_param->mfd); |
| 697 | /* Switch back */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 698 | writel(ccsr & ~MXC_CCM_CCSR_PLL3_SW_CLK_SEL, |
| 699 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 700 | break; |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 701 | #ifdef CONFIG_MX53 |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 702 | case PLL4_CLOCK: |
| 703 | /* Switch to pll4 bypass clock */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 704 | writel(ccsr | MXC_CCM_CCSR_PLL4_SW_CLK_SEL, |
| 705 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 706 | CHANGE_PLL_SETTINGS(pll, pll_param->pd, |
| 707 | pll_param->mfi, pll_param->mfn, |
| 708 | pll_param->mfd); |
| 709 | /* Switch back */ |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 710 | writel(ccsr & ~MXC_CCM_CCSR_PLL4_SW_CLK_SEL, |
| 711 | &mxc_ccm->ccsr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 712 | break; |
Benoît Thébaudeau | 3c98eb5 | 2012-09-27 10:22:22 +0000 | [diff] [blame] | 713 | #endif |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 714 | default: |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | /* Config CPU clock */ |
| 722 | static int config_core_clk(u32 ref, u32 freq) |
| 723 | { |
| 724 | int ret = 0; |
| 725 | struct pll_param pll_param; |
| 726 | |
| 727 | memset(&pll_param, 0, sizeof(struct pll_param)); |
| 728 | |
| 729 | /* The case that periph uses PLL1 is not considered here */ |
| 730 | ret = calc_pll_params(ref, freq, &pll_param); |
| 731 | if (ret != 0) { |
| 732 | printf("Error:Can't find pll parameters: %d\n", ret); |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | return config_pll_clk(PLL1_CLOCK, &pll_param); |
| 737 | } |
| 738 | |
| 739 | static int config_nfc_clk(u32 nfc_clk) |
| 740 | { |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 741 | u32 parent_rate = get_emi_slow_clk(); |
| 742 | u32 div = parent_rate / nfc_clk; |
| 743 | |
| 744 | if (nfc_clk <= 0) |
| 745 | return -EINVAL; |
| 746 | if (div == 0) |
| 747 | div++; |
| 748 | if (parent_rate / div > NFC_CLK_MAX) |
| 749 | div++; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 750 | clrsetbits_le32(&mxc_ccm->cbcdr, |
| 751 | MXC_CCM_CBCDR_NFC_PODF_MASK, |
| 752 | MXC_CCM_CBCDR_NFC_PODF(div - 1)); |
| 753 | while (readl(&mxc_ccm->cdhipr) != 0) |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 754 | ; |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* Config main_bus_clock for periphs */ |
| 759 | static int config_periph_clk(u32 ref, u32 freq) |
| 760 | { |
| 761 | int ret = 0; |
| 762 | struct pll_param pll_param; |
| 763 | |
| 764 | memset(&pll_param, 0, sizeof(struct pll_param)); |
| 765 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 766 | if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) { |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 767 | ret = calc_pll_params(ref, freq, &pll_param); |
| 768 | if (ret != 0) { |
| 769 | printf("Error:Can't find pll parameters: %d\n", |
| 770 | ret); |
| 771 | return ret; |
| 772 | } |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 773 | switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD( |
| 774 | readl(&mxc_ccm->cbcmr))) { |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 775 | case 0: |
| 776 | return config_pll_clk(PLL1_CLOCK, &pll_param); |
| 777 | break; |
| 778 | case 1: |
| 779 | return config_pll_clk(PLL3_CLOCK, &pll_param); |
| 780 | break; |
| 781 | default: |
| 782 | return -EINVAL; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static int config_ddr_clk(u32 emi_clk) |
| 790 | { |
| 791 | u32 clk_src; |
| 792 | s32 shift = 0, clk_sel, div = 1; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 793 | u32 cbcmr = readl(&mxc_ccm->cbcmr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 794 | |
| 795 | if (emi_clk > MAX_DDR_CLK) { |
| 796 | printf("Warning:DDR clock should not exceed %d MHz\n", |
| 797 | MAX_DDR_CLK / SZ_DEC_1M); |
| 798 | emi_clk = MAX_DDR_CLK; |
| 799 | } |
| 800 | |
| 801 | clk_src = get_periph_clk(); |
| 802 | /* Find DDR clock input */ |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 803 | clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 804 | switch (clk_sel) { |
| 805 | case 0: |
| 806 | shift = 16; |
| 807 | break; |
| 808 | case 1: |
| 809 | shift = 19; |
| 810 | break; |
| 811 | case 2: |
| 812 | shift = 22; |
| 813 | break; |
| 814 | case 3: |
| 815 | shift = 10; |
| 816 | break; |
| 817 | default: |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | |
| 821 | if ((clk_src % emi_clk) < 10000000) |
| 822 | div = clk_src / emi_clk; |
| 823 | else |
| 824 | div = (clk_src / emi_clk) + 1; |
| 825 | if (div > 8) |
| 826 | div = 8; |
| 827 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 828 | clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift); |
| 829 | while (readl(&mxc_ccm->cdhipr) != 0) |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 830 | ; |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 831 | writel(0x0, &mxc_ccm->ccdr); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * This function assumes the expected core clock has to be changed by |
| 838 | * modifying the PLL. This is NOT true always but for most of the times, |
| 839 | * it is. So it assumes the PLL output freq is the same as the expected |
| 840 | * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN. |
| 841 | * In the latter case, it will try to increase the presc value until |
| 842 | * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to |
| 843 | * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based |
| 844 | * on the targeted PLL and reference input clock to the PLL. Lastly, |
| 845 | * it sets the register based on these values along with the dividers. |
| 846 | * Note 1) There is no value checking for the passed-in divider values |
| 847 | * so the caller has to make sure those values are sensible. |
| 848 | * 2) Also adjust the NFC divider such that the NFC clock doesn't |
| 849 | * exceed NFC_CLK_MAX. |
| 850 | * 3) IPU HSP clock is independent of AHB clock. Even it can go up to |
| 851 | * 177MHz for higher voltage, this function fixes the max to 133MHz. |
| 852 | * 4) This function should not have allowed diag_printf() calls since |
| 853 | * the serial driver has been stoped. But leave then here to allow |
| 854 | * easy debugging by NOT calling the cyg_hal_plf_serial_stop(). |
| 855 | */ |
| 856 | int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk) |
| 857 | { |
| 858 | freq *= SZ_DEC_1M; |
| 859 | |
| 860 | switch (clk) { |
| 861 | case MXC_ARM_CLK: |
| 862 | if (config_core_clk(ref, freq)) |
| 863 | return -EINVAL; |
| 864 | break; |
| 865 | case MXC_PERIPH_CLK: |
| 866 | if (config_periph_clk(ref, freq)) |
| 867 | return -EINVAL; |
| 868 | break; |
| 869 | case MXC_DDR_CLK: |
| 870 | if (config_ddr_clk(freq)) |
| 871 | return -EINVAL; |
| 872 | break; |
| 873 | case MXC_NFC_CLK: |
| 874 | if (config_nfc_clk(freq)) |
| 875 | return -EINVAL; |
| 876 | break; |
| 877 | default: |
| 878 | printf("Warning:Unsupported or invalid clock type\n"); |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
Stefano Babic | c378abf | 2012-02-22 00:24:38 +0000 | [diff] [blame] | 884 | #ifdef CONFIG_MX53 |
| 885 | /* |
| 886 | * The clock for the external interface can be set to use internal clock |
| 887 | * if fuse bank 4, row 3, bit 2 is set. |
| 888 | * This is an undocumented feature and it was confirmed by Freescale's support: |
| 889 | * Fuses (but not pins) may be used to configure SATA clocks. |
| 890 | * Particularly the i.MX53 Fuse_Map contains the next information |
| 891 | * about configuring SATA clocks : SATA_ALT_REF_CLK[1:0] (offset 0x180C) |
| 892 | * '00' - 100MHz (External) |
| 893 | * '01' - 50MHz (External) |
| 894 | * '10' - 120MHz, internal (USB PHY) |
| 895 | * '11' - Reserved |
| 896 | */ |
| 897 | void mxc_set_sata_internal_clock(void) |
| 898 | { |
| 899 | u32 *tmp_base = |
| 900 | (u32 *)(IIM_BASE_ADDR + 0x180c); |
| 901 | |
Benoît Thébaudeau | f2f0038 | 2012-09-28 07:09:03 +0000 | [diff] [blame] | 902 | set_usb_phy_clk(); |
Stefano Babic | c378abf | 2012-02-22 00:24:38 +0000 | [diff] [blame] | 903 | |
Benoît Thébaudeau | e5a1017 | 2012-09-27 10:20:33 +0000 | [diff] [blame] | 904 | clrsetbits_le32(tmp_base, 0x6, 0x4); |
Stefano Babic | c378abf | 2012-02-22 00:24:38 +0000 | [diff] [blame] | 905 | } |
| 906 | #endif |
| 907 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 908 | /* |
| 909 | * Dump some core clockes. |
| 910 | */ |
Stefano Babic | 6eb9010 | 2010-10-28 11:08:52 +0200 | [diff] [blame] | 911 | int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 912 | { |
| 913 | u32 freq; |
| 914 | |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 915 | freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK); |
Marek Vasut | 421bf45 | 2011-09-14 14:09:04 +0000 | [diff] [blame] | 916 | printf("PLL1 %8d MHz\n", freq / 1000000); |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 917 | freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK); |
Marek Vasut | 421bf45 | 2011-09-14 14:09:04 +0000 | [diff] [blame] | 918 | printf("PLL2 %8d MHz\n", freq / 1000000); |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 919 | freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK); |
Marek Vasut | 421bf45 | 2011-09-14 14:09:04 +0000 | [diff] [blame] | 920 | printf("PLL3 %8d MHz\n", freq / 1000000); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 921 | #ifdef CONFIG_MX53 |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 922 | freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK); |
Marek Vasut | 421bf45 | 2011-09-14 14:09:04 +0000 | [diff] [blame] | 923 | printf("PLL4 %8d MHz\n", freq / 1000000); |
Marek Vasut | 3c844f3 | 2011-09-23 11:43:47 +0200 | [diff] [blame] | 924 | #endif |
Marek Vasut | 421bf45 | 2011-09-14 14:09:04 +0000 | [diff] [blame] | 925 | |
| 926 | printf("\n"); |
| 927 | printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000); |
| 928 | printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000); |
| 929 | printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000); |
Fabio Estevam | b412101 | 2012-04-30 08:12:02 +0000 | [diff] [blame] | 930 | printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000); |
Fabio Estevam | a82fee0 | 2012-11-15 11:23:22 +0000 | [diff] [blame] | 931 | #ifdef CONFIG_MXC_SPI |
| 932 | printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000); |
| 933 | #endif |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | /***************************************************/ |
| 938 | |
| 939 | U_BOOT_CMD( |
Stefano Babic | c8a02c3 | 2011-08-17 17:52:40 +0200 | [diff] [blame] | 940 | clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks, |
| 941 | "display clocks", |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 942 | "" |
| 943 | ); |