Jason Liu | dec1112 | 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 | 6479f51 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 27 | #include <asm/arch/crm_regs.h> |
Jason Liu | dec1112 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 28 | #include <asm/arch/clock.h> |
Fabio Estevam | 6479f51 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 29 | #include <asm/arch/sys_proto.h> |
Jason Liu | dec1112 | 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 | 6479f51 | 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 | dec1112 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 39 | |
Wolfgang Grandegger | 1859b70 | 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 | dec1112 | 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 | 6479f51 | 2012-04-29 08:11:13 +0000 | [diff] [blame] | 96 | u32 get_periph_clk(void) |
Jason Liu | dec1112 | 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 | dec1112 | 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 | 92aa90b | 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 | |
Eric Nelson | fdba076 | 2012-03-27 09:52:21 +0000 | [diff] [blame^] | 295 | int enable_sata_clock(void) |
| 296 | { |
| 297 | u32 reg = 0; |
| 298 | s32 timeout = 100000; |
| 299 | struct mxc_ccm_reg *const imx_ccm |
| 300 | = (struct mxc_ccm_reg *) CCM_BASE_ADDR; |
| 301 | |
| 302 | /* Enable sata clock */ |
| 303 | reg = readl(&imx_ccm->CCGR5); /* CCGR5 */ |
| 304 | reg |= MXC_CCM_CCGR5_CG2_MASK; |
| 305 | writel(reg, &imx_ccm->CCGR5); |
| 306 | |
| 307 | /* Enable PLLs */ |
| 308 | reg = readl(&imx_ccm->analog_pll_enet); |
| 309 | reg &= ~BM_ANADIG_PLL_SYS_POWERDOWN; |
| 310 | writel(reg, &imx_ccm->analog_pll_enet); |
| 311 | reg |= BM_ANADIG_PLL_SYS_ENABLE; |
| 312 | while (timeout--) { |
| 313 | if (readl(&imx_ccm->analog_pll_enet) & BM_ANADIG_PLL_SYS_LOCK) |
| 314 | break; |
| 315 | } |
| 316 | if (timeout <= 0) |
| 317 | return -EIO; |
| 318 | reg &= ~BM_ANADIG_PLL_SYS_BYPASS; |
| 319 | writel(reg, &imx_ccm->analog_pll_enet); |
| 320 | reg |= BM_ANADIG_PLL_ENET_ENABLE_SATA; |
| 321 | writel(reg, &imx_ccm->analog_pll_enet); |
| 322 | |
| 323 | return 0 ; |
| 324 | } |
| 325 | |
Jason Liu | dec1112 | 2011-11-25 00:18:02 +0000 | [diff] [blame] | 326 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 327 | { |
| 328 | switch (clk) { |
| 329 | case MXC_ARM_CLK: |
| 330 | return get_mcu_main_clk(); |
| 331 | case MXC_PER_CLK: |
| 332 | return get_periph_clk(); |
| 333 | case MXC_AHB_CLK: |
| 334 | return get_ahb_clk(); |
| 335 | case MXC_IPG_CLK: |
| 336 | return get_ipg_clk(); |
| 337 | case MXC_IPG_PERCLK: |
| 338 | return get_ipg_per_clk(); |
| 339 | case MXC_UART_CLK: |
| 340 | return get_uart_clk(); |
| 341 | case MXC_CSPI_CLK: |
| 342 | return get_cspi_clk(); |
| 343 | case MXC_AXI_CLK: |
| 344 | return get_axi_clk(); |
| 345 | case MXC_EMI_SLOW_CLK: |
| 346 | return get_emi_slow_clk(); |
| 347 | case MXC_DDR_CLK: |
| 348 | return get_mmdc_ch0_clk(); |
| 349 | case MXC_ESDHC_CLK: |
| 350 | return get_usdhc_clk(0); |
| 351 | case MXC_ESDHC2_CLK: |
| 352 | return get_usdhc_clk(1); |
| 353 | case MXC_ESDHC3_CLK: |
| 354 | return get_usdhc_clk(2); |
| 355 | case MXC_ESDHC4_CLK: |
| 356 | return get_usdhc_clk(3); |
| 357 | case MXC_SATA_CLK: |
| 358 | return get_ahb_clk(); |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Dump some core clockes. |
| 368 | */ |
| 369 | int do_mx6_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 370 | { |
| 371 | u32 freq; |
| 372 | freq = decode_pll(PLL_SYS, CONFIG_SYS_MX6_HCLK); |
| 373 | printf("PLL_SYS %8d MHz\n", freq / 1000000); |
| 374 | freq = decode_pll(PLL_BUS, CONFIG_SYS_MX6_HCLK); |
| 375 | printf("PLL_BUS %8d MHz\n", freq / 1000000); |
| 376 | freq = decode_pll(PLL_USBOTG, CONFIG_SYS_MX6_HCLK); |
| 377 | printf("PLL_OTG %8d MHz\n", freq / 1000000); |
| 378 | freq = decode_pll(PLL_ENET, CONFIG_SYS_MX6_HCLK); |
| 379 | printf("PLL_NET %8d MHz\n", freq / 1000000); |
| 380 | |
| 381 | printf("\n"); |
| 382 | printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000); |
| 383 | printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000); |
| 384 | printf("CSPI %8d kHz\n", mxc_get_clock(MXC_CSPI_CLK) / 1000); |
| 385 | printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000); |
| 386 | printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000); |
| 387 | printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000); |
| 388 | printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000); |
| 389 | printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000); |
| 390 | printf("USDHC3 %8d kHz\n", mxc_get_clock(MXC_ESDHC3_CLK) / 1000); |
| 391 | printf("USDHC4 %8d kHz\n", mxc_get_clock(MXC_ESDHC4_CLK) / 1000); |
| 392 | printf("EMI SLOW %8d kHz\n", mxc_get_clock(MXC_EMI_SLOW_CLK) / 1000); |
| 393 | printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /***************************************************/ |
| 399 | |
| 400 | U_BOOT_CMD( |
| 401 | clocks, CONFIG_SYS_MAXARGS, 1, do_mx6_showclocks, |
| 402 | "display clocks", |
| 403 | "" |
| 404 | ); |