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