Peng Fan | 684ccd9 | 2017-02-22 16:21:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <div64.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <errno.h> |
| 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/sys_proto.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
| 16 | int get_clocks(void) |
| 17 | { |
| 18 | #ifdef CONFIG_FSL_ESDHC |
| 19 | #if CONFIG_SYS_FSL_ESDHC_ADDR == USDHC0_RBASE |
| 20 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK); |
| 21 | #elif CONFIG_SYS_FSL_ESDHC_ADDR == USDHC1_RBASE |
| 22 | gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK); |
| 23 | #endif |
| 24 | #endif |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static u32 get_fast_plat_clk(void) |
| 29 | { |
| 30 | return scg_clk_get_rate(SCG_NIC0_CLK); |
| 31 | } |
| 32 | |
| 33 | static u32 get_slow_plat_clk(void) |
| 34 | { |
| 35 | return scg_clk_get_rate(SCG_NIC1_CLK); |
| 36 | } |
| 37 | |
| 38 | static u32 get_ipg_clk(void) |
| 39 | { |
| 40 | return scg_clk_get_rate(SCG_NIC1_BUS_CLK); |
| 41 | } |
| 42 | |
| 43 | u32 get_lpuart_clk(void) |
| 44 | { |
| 45 | int index = 0; |
| 46 | |
| 47 | const u32 lpuart_array[] = { |
| 48 | LPUART0_RBASE, |
| 49 | LPUART1_RBASE, |
| 50 | LPUART2_RBASE, |
| 51 | LPUART3_RBASE, |
| 52 | LPUART4_RBASE, |
| 53 | LPUART5_RBASE, |
| 54 | LPUART6_RBASE, |
| 55 | LPUART7_RBASE, |
| 56 | }; |
| 57 | |
| 58 | const enum pcc_clk lpuart_pcc_clks[] = { |
| 59 | PER_CLK_LPUART4, |
| 60 | PER_CLK_LPUART5, |
| 61 | PER_CLK_LPUART6, |
| 62 | PER_CLK_LPUART7, |
| 63 | }; |
| 64 | |
| 65 | for (index = 0; index < 8; index++) { |
| 66 | if (lpuart_array[index] == LPUART_BASE) |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | if (index < 4 || index > 7) |
| 71 | return 0; |
| 72 | |
| 73 | return pcc_clock_get_rate(lpuart_pcc_clks[index - 4]); |
| 74 | } |
| 75 | |
| 76 | unsigned int mxc_get_clock(enum mxc_clock clk) |
| 77 | { |
| 78 | switch (clk) { |
| 79 | case MXC_ARM_CLK: |
| 80 | return scg_clk_get_rate(SCG_CORE_CLK); |
| 81 | case MXC_AXI_CLK: |
| 82 | return get_fast_plat_clk(); |
| 83 | case MXC_AHB_CLK: |
| 84 | return get_slow_plat_clk(); |
| 85 | case MXC_IPG_CLK: |
| 86 | return get_ipg_clk(); |
| 87 | case MXC_I2C_CLK: |
| 88 | return pcc_clock_get_rate(PER_CLK_LPI2C4); |
| 89 | case MXC_UART_CLK: |
| 90 | return get_lpuart_clk(); |
| 91 | case MXC_ESDHC_CLK: |
| 92 | return pcc_clock_get_rate(PER_CLK_USDHC0); |
| 93 | case MXC_ESDHC2_CLK: |
| 94 | return pcc_clock_get_rate(PER_CLK_USDHC1); |
| 95 | case MXC_DDR_CLK: |
| 96 | return scg_clk_get_rate(SCG_DDR_CLK); |
| 97 | default: |
| 98 | printf("Unsupported mxc_clock %d\n", clk); |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | void init_clk_usdhc(u32 index) |
| 106 | { |
| 107 | switch (index) { |
| 108 | case 0: |
| 109 | /*Disable the clock before configure it */ |
| 110 | pcc_clock_enable(PER_CLK_USDHC0, false); |
| 111 | |
| 112 | /* 158MHz / 1 = 158MHz */ |
| 113 | pcc_clock_sel(PER_CLK_USDHC0, SCG_NIC1_CLK); |
| 114 | pcc_clock_div_config(PER_CLK_USDHC0, false, 1); |
| 115 | pcc_clock_enable(PER_CLK_USDHC0, true); |
| 116 | break; |
| 117 | case 1: |
| 118 | /*Disable the clock before configure it */ |
| 119 | pcc_clock_enable(PER_CLK_USDHC1, false); |
| 120 | |
| 121 | /* 158MHz / 1 = 158MHz */ |
| 122 | pcc_clock_sel(PER_CLK_USDHC1, SCG_NIC1_CLK); |
| 123 | pcc_clock_div_config(PER_CLK_USDHC1, false, 1); |
| 124 | pcc_clock_enable(PER_CLK_USDHC1, true); |
| 125 | break; |
| 126 | default: |
| 127 | printf("Invalid index for USDHC %d\n", index); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #ifdef CONFIG_MXC_OCOTP |
| 133 | |
| 134 | #define OCOTP_CTRL_PCC1_SLOT (38) |
| 135 | #define OCOTP_CTRL_HIGH4K_PCC1_SLOT (39) |
| 136 | |
| 137 | void enable_ocotp_clk(unsigned char enable) |
| 138 | { |
| 139 | u32 val; |
| 140 | |
| 141 | /* |
| 142 | * Seems the OCOTP CLOCKs have been enabled at default, |
| 143 | * check its inuse flag |
| 144 | */ |
| 145 | |
| 146 | val = readl(PCC1_RBASE + 4 * OCOTP_CTRL_PCC1_SLOT); |
| 147 | if (!(val & PCC_INUSE_MASK)) |
| 148 | writel(PCC_CGC_MASK, (PCC1_RBASE + 4 * OCOTP_CTRL_PCC1_SLOT)); |
| 149 | |
| 150 | val = readl(PCC1_RBASE + 4 * OCOTP_CTRL_HIGH4K_PCC1_SLOT); |
| 151 | if (!(val & PCC_INUSE_MASK)) |
| 152 | writel(PCC_CGC_MASK, |
| 153 | (PCC1_RBASE + 4 * OCOTP_CTRL_HIGH4K_PCC1_SLOT)); |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | void enable_usboh3_clk(unsigned char enable) |
| 158 | { |
| 159 | if (enable) { |
| 160 | pcc_clock_enable(PER_CLK_USB0, false); |
| 161 | pcc_clock_sel(PER_CLK_USB0, SCG_NIC1_BUS_CLK); |
| 162 | pcc_clock_enable(PER_CLK_USB0, true); |
| 163 | |
| 164 | #ifdef CONFIG_USB_MAX_CONTROLLER_COUNT |
| 165 | if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1) { |
| 166 | pcc_clock_enable(PER_CLK_USB1, false); |
| 167 | pcc_clock_sel(PER_CLK_USB1, SCG_NIC1_BUS_CLK); |
| 168 | pcc_clock_enable(PER_CLK_USB1, true); |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | pcc_clock_enable(PER_CLK_USB_PHY, true); |
| 173 | pcc_clock_enable(PER_CLK_USB_PL301, true); |
| 174 | } else { |
| 175 | pcc_clock_enable(PER_CLK_USB0, false); |
| 176 | pcc_clock_enable(PER_CLK_USB1, false); |
| 177 | pcc_clock_enable(PER_CLK_USB_PHY, false); |
| 178 | pcc_clock_enable(PER_CLK_USB_PL301, false); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static void lpuart_set_clk(uint32_t index, enum scg_clk clk) |
| 183 | { |
| 184 | const enum pcc_clk lpuart_pcc_clks[] = { |
| 185 | PER_CLK_LPUART4, |
| 186 | PER_CLK_LPUART5, |
| 187 | PER_CLK_LPUART6, |
| 188 | PER_CLK_LPUART7, |
| 189 | }; |
| 190 | |
| 191 | if (index < 4 || index > 7) |
| 192 | return; |
| 193 | |
| 194 | #ifndef CONFIG_CLK_DEBUG |
| 195 | pcc_clock_enable(lpuart_pcc_clks[index - 4], false); |
| 196 | #endif |
| 197 | pcc_clock_sel(lpuart_pcc_clks[index - 4], clk); |
| 198 | pcc_clock_enable(lpuart_pcc_clks[index - 4], true); |
| 199 | } |
| 200 | |
| 201 | static void init_clk_lpuart(void) |
| 202 | { |
| 203 | u32 index = 0, i; |
| 204 | |
| 205 | const u32 lpuart_array[] = { |
| 206 | LPUART0_RBASE, |
| 207 | LPUART1_RBASE, |
| 208 | LPUART2_RBASE, |
| 209 | LPUART3_RBASE, |
| 210 | LPUART4_RBASE, |
| 211 | LPUART5_RBASE, |
| 212 | LPUART6_RBASE, |
| 213 | LPUART7_RBASE, |
| 214 | }; |
| 215 | |
| 216 | for (i = 0; i < 8; i++) { |
| 217 | if (lpuart_array[i] == LPUART_BASE) { |
| 218 | index = i; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | lpuart_set_clk(index, SCG_SOSC_DIV2_CLK); |
| 224 | } |
| 225 | |
| 226 | static void init_clk_rgpio2p(void) |
| 227 | { |
| 228 | /*Enable RGPIO2P1 clock */ |
| 229 | pcc_clock_enable(PER_CLK_RGPIO2P1, true); |
| 230 | |
| 231 | /* |
| 232 | * Hard code to enable RGPIO2P0 clock since it is not |
| 233 | * in clock frame for A7 domain |
| 234 | */ |
| 235 | writel(PCC_CGC_MASK, (PCC0_RBASE + 0x3C)); |
| 236 | } |
| 237 | |
| 238 | /* Configure PLL/PFD freq */ |
| 239 | void clock_init(void) |
| 240 | { |
| 241 | /* |
| 242 | * ROM has enabled clocks: |
| 243 | * A4 side: SIRC 16Mhz (DIV1-3 off), FIRC 48Mhz (DIV1-2 on), |
| 244 | * Non-LP-boot: SOSC, SPLL PFD0 (scs selected) |
| 245 | * A7 side: SPLL PFD0 (scs selected, 413Mhz), |
| 246 | * APLL PFD0 (352Mhz), DDRCLK, all NIC clocks |
| 247 | * A7 Plat0 (NIC0) = 176Mhz, Plat1 (NIC1) = 176Mhz, |
| 248 | * IP BUS (NIC1_BUS) = 58.6Mhz |
| 249 | * |
| 250 | * In u-boot: |
| 251 | * 1. Enable PFD1-3 of APLL for A7 side. Enable FIRC and DIVs. |
| 252 | * 2. Enable USB PLL |
| 253 | * 3. Init the clocks of peripherals used in u-boot bu |
| 254 | * without set rate interface.The clocks for these |
| 255 | * peripherals are enabled in this intialization. |
| 256 | * 4.Other peripherals with set clock rate interface |
| 257 | * does not be set in this function. |
| 258 | */ |
| 259 | |
| 260 | scg_a7_firc_init(); |
| 261 | |
| 262 | scg_a7_soscdiv_init(); |
| 263 | |
| 264 | /* APLL PFD1 = 270Mhz, PFD2=480Mhz, PFD3=800Mhz */ |
| 265 | scg_enable_pll_pfd(SCG_APLL_PFD1_CLK, 35); |
| 266 | scg_enable_pll_pfd(SCG_APLL_PFD2_CLK, 20); |
| 267 | scg_enable_pll_pfd(SCG_APLL_PFD3_CLK, 12); |
| 268 | |
| 269 | init_clk_lpuart(); |
| 270 | |
| 271 | init_clk_rgpio2p(); |
| 272 | |
| 273 | enable_usboh3_clk(1); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Dump some core clockes. |
| 278 | */ |
| 279 | int do_mx7_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 280 | { |
| 281 | u32 addr = 0; |
| 282 | u32 freq; |
| 283 | freq = decode_pll(PLL_A7_SPLL); |
| 284 | printf("PLL_A7_SPLL %8d MHz\n", freq / 1000000); |
| 285 | |
| 286 | freq = decode_pll(PLL_A7_APLL); |
| 287 | printf("PLL_A7_APLL %8d MHz\n", freq / 1000000); |
| 288 | |
| 289 | freq = decode_pll(PLL_USB); |
| 290 | printf("PLL_USB %8d MHz\n", freq / 1000000); |
| 291 | |
| 292 | printf("\n"); |
| 293 | |
| 294 | printf("CORE %8d kHz\n", scg_clk_get_rate(SCG_CORE_CLK) / 1000); |
| 295 | printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000); |
| 296 | printf("UART %8d kHz\n", mxc_get_clock(MXC_UART_CLK) / 1000); |
| 297 | printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000); |
| 298 | printf("AXI %8d kHz\n", mxc_get_clock(MXC_AXI_CLK) / 1000); |
| 299 | printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000); |
| 300 | printf("USDHC1 %8d kHz\n", mxc_get_clock(MXC_ESDHC_CLK) / 1000); |
| 301 | printf("USDHC2 %8d kHz\n", mxc_get_clock(MXC_ESDHC2_CLK) / 1000); |
| 302 | printf("I2C4 %8d kHz\n", mxc_get_clock(MXC_I2C_CLK) / 1000); |
| 303 | |
| 304 | addr = (u32) clock_init; |
| 305 | printf("[%s] addr = 0x%08X\r\n", __func__, addr); |
| 306 | scg_a7_info(); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | U_BOOT_CMD( |
| 312 | clocks, CONFIG_SYS_MAXARGS, 1, do_mx7_showclocks, |
| 313 | "display clocks", |
| 314 | "" |
| 315 | ); |