Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/errno.h> |
| 26 | #include <asm/arch/imx-regs.h> |
Fabio Estevam | 6a37604 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 27 | #include <asm/arch/crm_regs.h> |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 28 | #include <asm/arch/clock.h> |
Fabio Estevam | 6a37604 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 29 | #include <asm/arch/sys_proto.h> |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 30 | |
| 31 | enum pll_clocks { |
| 32 | PLL_SYS, /* System PLL */ |
| 33 | PLL_BUS, /* System Bus PLL*/ |
| 34 | PLL_USBOTG, /* OTG USB PLL */ |
| 35 | PLL_ENET, /* ENET PLL */ |
| 36 | }; |
| 37 | |
Fabio Estevam | 6a37604 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 38 | struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR; |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 39 | |
Wolfgang Grandegger | 3f46752 | 2012-02-08 22:33:25 +0000 | [diff] [blame] | 40 | void enable_usboh3_clk(unsigned char enable) |
| 41 | { |
| 42 | u32 reg; |
| 43 | |
| 44 | reg = __raw_readl(&imx_ccm->CCGR6); |
| 45 | if (enable) |
| 46 | reg |= MXC_CCM_CCGR_CG_MASK << MXC_CCM_CCGR0_CG0_OFFSET; |
| 47 | else |
| 48 | reg &= ~(MXC_CCM_CCGR_CG_MASK << MXC_CCM_CCGR0_CG0_OFFSET); |
| 49 | __raw_writel(reg, &imx_ccm->CCGR6); |
| 50 | |
| 51 | } |
| 52 | |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 53 | static u32 decode_pll(enum pll_clocks pll, u32 infreq) |
| 54 | { |
| 55 | u32 div; |
| 56 | |
| 57 | switch (pll) { |
| 58 | case PLL_SYS: |
| 59 | div = __raw_readl(&imx_ccm->analog_pll_sys); |
| 60 | div &= BM_ANADIG_PLL_SYS_DIV_SELECT; |
| 61 | |
| 62 | return infreq * (div >> 1); |
| 63 | case PLL_BUS: |
| 64 | div = __raw_readl(&imx_ccm->analog_pll_528); |
| 65 | div &= BM_ANADIG_PLL_528_DIV_SELECT; |
| 66 | |
| 67 | return infreq * (20 + (div << 1)); |
| 68 | case PLL_USBOTG: |
| 69 | div = __raw_readl(&imx_ccm->analog_usb1_pll_480_ctrl); |
| 70 | div &= BM_ANADIG_USB1_PLL_480_CTRL_DIV_SELECT; |
| 71 | |
| 72 | return infreq * (20 + (div << 1)); |
| 73 | case PLL_ENET: |
| 74 | div = __raw_readl(&imx_ccm->analog_pll_enet); |
| 75 | div &= BM_ANADIG_PLL_ENET_DIV_SELECT; |
| 76 | |
| 77 | return (div == 3 ? 125000000 : 25000000 * (div << 1)); |
| 78 | default: |
| 79 | return 0; |
| 80 | } |
| 81 | /* NOTREACHED */ |
| 82 | } |
| 83 | |
| 84 | static u32 get_mcu_main_clk(void) |
| 85 | { |
| 86 | u32 reg, freq; |
| 87 | |
| 88 | reg = __raw_readl(&imx_ccm->cacrr); |
| 89 | reg &= MXC_CCM_CACRR_ARM_PODF_MASK; |
| 90 | reg >>= MXC_CCM_CACRR_ARM_PODF_OFFSET; |
| 91 | freq = decode_pll(PLL_SYS, CONFIG_SYS_MX6_HCLK); |
| 92 | |
| 93 | return freq / (reg + 1); |
| 94 | } |
| 95 | |
Fabio Estevam | 6a37604 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 96 | u32 get_periph_clk(void) |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 97 | { |
| 98 | u32 reg, freq = 0; |
| 99 | |
| 100 | reg = __raw_readl(&imx_ccm->cbcdr); |
| 101 | if (reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL) { |
| 102 | reg = __raw_readl(&imx_ccm->cbcmr); |
| 103 | reg &= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_MASK; |
| 104 | reg >>= MXC_CCM_CBCMR_PERIPH_CLK2_SEL_OFFSET; |
| 105 | |
| 106 | switch (reg) { |
| 107 | case 0: |
| 108 | freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK); |
| 109 | break; |
| 110 | case 1: |
| 111 | case 2: |
| 112 | freq = CONFIG_SYS_MX6_HCLK; |
| 113 | break; |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } else { |
| 118 | reg = __raw_readl(&imx_ccm->cbcmr); |
| 119 | reg &= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK; |
| 120 | reg >>= MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET; |
| 121 | |
| 122 | switch (reg) { |
| 123 | case 0: |
| 124 | freq = decode_pll(PLL_BUS, CONFIG_SYS_MX6_HCLK); |
| 125 | break; |
| 126 | case 1: |
| 127 | freq = PLL2_PFD2_FREQ; |
| 128 | break; |
| 129 | case 2: |
| 130 | freq = PLL2_PFD0_FREQ; |
| 131 | break; |
| 132 | case 3: |
| 133 | freq = PLL2_PFD2_DIV_FREQ; |
| 134 | break; |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return freq; |
| 141 | } |
| 142 | |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 143 | static u32 get_ipg_clk(void) |
| 144 | { |
| 145 | u32 reg, ipg_podf; |
| 146 | |
| 147 | reg = __raw_readl(&imx_ccm->cbcdr); |
| 148 | reg &= MXC_CCM_CBCDR_IPG_PODF_MASK; |
| 149 | ipg_podf = reg >> MXC_CCM_CBCDR_IPG_PODF_OFFSET; |
| 150 | |
| 151 | return get_ahb_clk() / (ipg_podf + 1); |
| 152 | } |
| 153 | |
| 154 | static u32 get_ipg_per_clk(void) |
| 155 | { |
| 156 | u32 reg, perclk_podf; |
| 157 | |
| 158 | reg = __raw_readl(&imx_ccm->cscmr1); |
| 159 | perclk_podf = reg & MXC_CCM_CSCMR1_PERCLK_PODF_MASK; |
| 160 | |
| 161 | return get_ipg_clk() / (perclk_podf + 1); |
| 162 | } |
| 163 | |
| 164 | static u32 get_uart_clk(void) |
| 165 | { |
| 166 | u32 reg, uart_podf; |
| 167 | |
| 168 | reg = __raw_readl(&imx_ccm->cscdr1); |
| 169 | reg &= MXC_CCM_CSCDR1_UART_CLK_PODF_MASK; |
| 170 | uart_podf = reg >> MXC_CCM_CSCDR1_UART_CLK_PODF_OFFSET; |
| 171 | |
| 172 | return PLL3_80M / (uart_podf + 1); |
| 173 | } |
| 174 | |
| 175 | static u32 get_cspi_clk(void) |
| 176 | { |
| 177 | u32 reg, cspi_podf; |
| 178 | |
| 179 | reg = __raw_readl(&imx_ccm->cscdr2); |
| 180 | reg &= MXC_CCM_CSCDR2_ECSPI_CLK_PODF_MASK; |
| 181 | cspi_podf = reg >> MXC_CCM_CSCDR2_ECSPI_CLK_PODF_OFFSET; |
| 182 | |
| 183 | return PLL3_60M / (cspi_podf + 1); |
| 184 | } |
| 185 | |
| 186 | static u32 get_axi_clk(void) |
| 187 | { |
| 188 | u32 root_freq, axi_podf; |
| 189 | u32 cbcdr = __raw_readl(&imx_ccm->cbcdr); |
| 190 | |
| 191 | axi_podf = cbcdr & MXC_CCM_CBCDR_AXI_PODF_MASK; |
| 192 | axi_podf >>= MXC_CCM_CBCDR_AXI_PODF_OFFSET; |
| 193 | |
| 194 | if (cbcdr & MXC_CCM_CBCDR_AXI_SEL) { |
| 195 | if (cbcdr & MXC_CCM_CBCDR_AXI_ALT_SEL) |
| 196 | root_freq = PLL2_PFD2_FREQ; |
| 197 | else |
| 198 | root_freq = PLL3_PFD1_FREQ; |
| 199 | } else |
| 200 | root_freq = get_periph_clk(); |
| 201 | |
| 202 | return root_freq / (axi_podf + 1); |
| 203 | } |
| 204 | |
| 205 | static u32 get_emi_slow_clk(void) |
| 206 | { |
| 207 | u32 emi_clk_sel, emi_slow_pof, cscmr1, root_freq = 0; |
| 208 | |
| 209 | cscmr1 = __raw_readl(&imx_ccm->cscmr1); |
| 210 | emi_clk_sel = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_MASK; |
| 211 | emi_clk_sel >>= MXC_CCM_CSCMR1_ACLK_EMI_SLOW_OFFSET; |
| 212 | emi_slow_pof = cscmr1 & MXC_CCM_CSCMR1_ACLK_EMI_SLOW_PODF_MASK; |
| 213 | emi_slow_pof >>= MXC_CCM_CSCMR1_ACLK_EMI_PODF_OFFSET; |
| 214 | |
| 215 | switch (emi_clk_sel) { |
| 216 | case 0: |
| 217 | root_freq = get_axi_clk(); |
| 218 | break; |
| 219 | case 1: |
| 220 | root_freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK); |
| 221 | break; |
| 222 | case 2: |
| 223 | root_freq = PLL2_PFD2_FREQ; |
| 224 | break; |
| 225 | case 3: |
| 226 | root_freq = PLL2_PFD0_FREQ; |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | return root_freq / (emi_slow_pof + 1); |
| 231 | } |
| 232 | |
| 233 | static u32 get_mmdc_ch0_clk(void) |
| 234 | { |
| 235 | u32 cbcdr = __raw_readl(&imx_ccm->cbcdr); |
| 236 | u32 mmdc_ch0_podf = (cbcdr & MXC_CCM_CBCDR_MMDC_CH0_PODF_MASK) >> |
| 237 | MXC_CCM_CBCDR_MMDC_CH0_PODF_OFFSET; |
| 238 | |
| 239 | return get_periph_clk() / (mmdc_ch0_podf + 1); |
| 240 | } |
| 241 | |
| 242 | static u32 get_usdhc_clk(u32 port) |
| 243 | { |
| 244 | u32 root_freq = 0, usdhc_podf = 0, clk_sel = 0; |
| 245 | u32 cscmr1 = __raw_readl(&imx_ccm->cscmr1); |
| 246 | u32 cscdr1 = __raw_readl(&imx_ccm->cscdr1); |
| 247 | |
| 248 | switch (port) { |
| 249 | case 0: |
| 250 | usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC1_PODF_MASK) >> |
| 251 | MXC_CCM_CSCDR1_USDHC1_PODF_OFFSET; |
| 252 | clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC1_CLK_SEL; |
| 253 | |
| 254 | break; |
| 255 | case 1: |
| 256 | usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC2_PODF_MASK) >> |
| 257 | MXC_CCM_CSCDR1_USDHC2_PODF_OFFSET; |
| 258 | clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC2_CLK_SEL; |
| 259 | |
| 260 | break; |
| 261 | case 2: |
| 262 | usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC3_PODF_MASK) >> |
| 263 | MXC_CCM_CSCDR1_USDHC3_PODF_OFFSET; |
| 264 | clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC3_CLK_SEL; |
| 265 | |
| 266 | break; |
| 267 | case 3: |
| 268 | usdhc_podf = (cscdr1 & MXC_CCM_CSCDR1_USDHC4_PODF_MASK) >> |
| 269 | MXC_CCM_CSCDR1_USDHC4_PODF_OFFSET; |
| 270 | clk_sel = cscmr1 & MXC_CCM_CSCMR1_USDHC4_CLK_SEL; |
| 271 | |
| 272 | break; |
| 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | if (clk_sel) |
| 278 | root_freq = PLL2_PFD0_FREQ; |
| 279 | else |
| 280 | root_freq = PLL2_PFD2_FREQ; |
| 281 | |
| 282 | return root_freq / (usdhc_podf + 1); |
| 283 | } |
| 284 | |
| 285 | u32 imx_get_uartclk(void) |
| 286 | { |
| 287 | return get_uart_clk(); |
| 288 | } |
| 289 | |
Jason Liu | ff167df | 2011-12-16 05:17:06 +0000 | [diff] [blame] | 290 | u32 imx_get_fecclk(void) |
| 291 | { |
| 292 | return decode_pll(PLL_ENET, CONFIG_SYS_MX6_HCLK); |
| 293 | } |
| 294 | |
Jason Liu | 23608e2 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 295 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 296 | { |
| 297 | switch (clk) { |
| 298 | case MXC_ARM_CLK: |
| 299 | return get_mcu_main_clk(); |
| 300 | case MXC_PER_CLK: |
| 301 | return get_periph_clk(); |
| 302 | case MXC_AHB_CLK: |
| 303 | return get_ahb_clk(); |
| 304 | case MXC_IPG_CLK: |
| 305 | return get_ipg_clk(); |
| 306 | case MXC_IPG_PERCLK: |
| 307 | return get_ipg_per_clk(); |
| 308 | case MXC_UART_CLK: |
| 309 | return get_uart_clk(); |
| 310 | case MXC_CSPI_CLK: |
| 311 | return get_cspi_clk(); |
| 312 | case MXC_AXI_CLK: |
| 313 | return get_axi_clk(); |
| 314 | case MXC_EMI_SLOW_CLK: |
| 315 | return get_emi_slow_clk(); |
| 316 | case MXC_DDR_CLK: |
| 317 | return get_mmdc_ch0_clk(); |
| 318 | case MXC_ESDHC_CLK: |
| 319 | return get_usdhc_clk(0); |
| 320 | case MXC_ESDHC2_CLK: |
| 321 | return get_usdhc_clk(1); |
| 322 | case MXC_ESDHC3_CLK: |
| 323 | return get_usdhc_clk(2); |
| 324 | case MXC_ESDHC4_CLK: |
| 325 | return get_usdhc_clk(3); |
| 326 | case MXC_SATA_CLK: |
| 327 | return get_ahb_clk(); |
| 328 | default: |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Dump some core clockes. |
| 337 | */ |
| 338 | int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 339 | { |
| 340 | u32 freq; |
| 341 | freq = decode_pll(PLL_SYS, CONFIG_SYS_MX6_HCLK); |
| 342 | printf("PLL_SYS %8d MHz\n", freq / 1000000); |
| 343 | freq = decode_pll(PLL_BUS, CONFIG_SYS_MX6_HCLK); |
| 344 | printf("PLL_BUS %8d MHz\n", freq / 1000000); |
| 345 | freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK); |
| 346 | printf("PLL_OTG %8d MHz\n", freq / 1000000); |
| 347 | freq = decode_pll(PLL_ENET, CONFIG_SYS_MX6_HCLK); |
| 348 | printf("PLL_NET %8d MHz\n", freq / 1000000); |
| 349 | |
| 350 | printf("\n"); |
| 351 | printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000); |
| 352 | printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000); |
| 353 | printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000); |
| 354 | printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000); |
| 355 | printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000); |
| 356 | printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000); |
| 357 | printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000); |
| 358 | printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000); |
| 359 | printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000); |
| 360 | printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000); |
| 361 | printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000); |
| 362 | printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /***************************************************/ |
| 368 | |
| 369 | U_BOOT_CMD( |
| 370 | clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks, |
| 371 | "display clocks", |
| 372 | "" |
| 373 | ); |