Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 1 | /* |
Tom Warren | 7aaa5a6 | 2015-03-04 16:36:00 -0700 | [diff] [blame] | 2 | * Copyright (c) 2010-2015, NVIDIA CORPORATION. All rights reserved. |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 3 | * |
Tom Rini | 5b8031c | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0 |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* Tegra SoC common clock control functions */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 746dc76 | 2015-06-05 14:39:36 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/clock.h> |
| 13 | #include <asm/arch/tegra.h> |
Stephen Warren | 73c3893 | 2015-01-19 16:25:52 -0700 | [diff] [blame] | 14 | #include <asm/arch-tegra/ap.h> |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 15 | #include <asm/arch-tegra/clk_rst.h> |
Simon Glass | 746dc76 | 2015-06-05 14:39:36 -0600 | [diff] [blame] | 16 | #include <asm/arch-tegra/pmc.h> |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 17 | #include <asm/arch-tegra/timer.h> |
| 18 | #include <div64.h> |
| 19 | #include <fdtdec.h> |
| 20 | |
| 21 | /* |
| 22 | * This is our record of the current clock rate of each clock. We don't |
| 23 | * fill all of these in since we are only really interested in clocks which |
| 24 | * we use as parents. |
| 25 | */ |
| 26 | static unsigned pll_rate[CLOCK_ID_COUNT]; |
| 27 | |
| 28 | /* |
| 29 | * The oscillator frequency is fixed to one of four set values. Based on this |
| 30 | * the other clocks are set up appropriately. |
| 31 | */ |
| 32 | static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = { |
| 33 | 13000000, |
| 34 | 19200000, |
| 35 | 12000000, |
| 36 | 26000000, |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 37 | 38400000, |
| 38 | 48000000, |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* return 1 if a peripheral ID is in range */ |
| 42 | #define clock_type_id_isvalid(id) ((id) >= 0 && \ |
| 43 | (id) < CLOCK_TYPE_COUNT) |
| 44 | |
| 45 | char pllp_valid = 1; /* PLLP is set up correctly */ |
| 46 | |
| 47 | /* return 1 if a periphc_internal_id is in range */ |
| 48 | #define periphc_internal_id_isvalid(id) ((id) >= 0 && \ |
| 49 | (id) < PERIPHC_COUNT) |
| 50 | |
| 51 | /* number of clock outputs of a PLL */ |
| 52 | static const u8 pll_num_clkouts[] = { |
| 53 | 1, /* PLLC */ |
| 54 | 1, /* PLLM */ |
| 55 | 4, /* PLLP */ |
| 56 | 1, /* PLLA */ |
| 57 | 0, /* PLLU */ |
| 58 | 0, /* PLLD */ |
| 59 | }; |
| 60 | |
| 61 | int clock_get_osc_bypass(void) |
| 62 | { |
| 63 | struct clk_rst_ctlr *clkrst = |
| 64 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 65 | u32 reg; |
| 66 | |
| 67 | reg = readl(&clkrst->crc_osc_ctrl); |
| 68 | return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT; |
| 69 | } |
| 70 | |
| 71 | /* Returns a pointer to the registers of the given pll */ |
| 72 | static struct clk_pll *get_pll(enum clock_id clkid) |
| 73 | { |
| 74 | struct clk_rst_ctlr *clkrst = |
| 75 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 76 | |
| 77 | assert(clock_id_is_pll(clkid)); |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 78 | if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) { |
Simon Glass | cd3c676 | 2015-06-05 14:39:37 -0600 | [diff] [blame] | 79 | debug("%s: Invalid PLL %d\n", __func__, clkid); |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 82 | return &clkrst->crc_pll[clkid]; |
| 83 | } |
| 84 | |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 85 | __weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid) |
| 86 | { |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 90 | int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn, |
| 91 | u32 *divp, u32 *cpcon, u32 *lfcon) |
| 92 | { |
| 93 | struct clk_pll *pll = get_pll(clkid); |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 94 | struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid]; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 95 | u32 data; |
| 96 | |
| 97 | assert(clkid != CLOCK_ID_USB); |
| 98 | |
| 99 | /* Safety check, adds to code size but is small */ |
| 100 | if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB) |
| 101 | return -1; |
| 102 | data = readl(&pll->pll_base); |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 103 | *divm = (data >> pllinfo->m_shift) & pllinfo->m_mask; |
| 104 | *divn = (data >> pllinfo->n_shift) & pllinfo->n_mask; |
| 105 | *divp = (data >> pllinfo->p_shift) & pllinfo->p_mask; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 106 | data = readl(&pll->pll_misc); |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 107 | /* NOTE: On T210, cpcon/lfcon no longer exist, moved to KCP/KVCO */ |
| 108 | *cpcon = (data >> pllinfo->kcp_shift) & pllinfo->kcp_mask; |
| 109 | *lfcon = (data >> pllinfo->kvco_shift) & pllinfo->kvco_mask; |
| 110 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn, |
| 115 | u32 divp, u32 cpcon, u32 lfcon) |
| 116 | { |
Simon Glass | cd3c676 | 2015-06-05 14:39:37 -0600 | [diff] [blame] | 117 | struct clk_pll *pll = NULL; |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 118 | struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid]; |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 119 | struct clk_pll_simple *simple_pll = NULL; |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 120 | u32 misc_data, data; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 121 | |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 122 | if (clkid < (enum clock_id)TEGRA_CLK_PLLS) { |
Simon Glass | cd3c676 | 2015-06-05 14:39:37 -0600 | [diff] [blame] | 123 | pll = get_pll(clkid); |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 124 | } else { |
| 125 | simple_pll = clock_get_simple_pll(clkid); |
| 126 | if (!simple_pll) { |
| 127 | debug("%s: Uknown simple PLL %d\n", __func__, clkid); |
| 128 | return 0; |
| 129 | } |
| 130 | } |
Simon Glass | cd3c676 | 2015-06-05 14:39:37 -0600 | [diff] [blame] | 131 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 132 | /* |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 133 | * pllinfo has the m/n/p and kcp/kvco mask and shift |
| 134 | * values for all of the PLLs used in U-Boot, with any |
| 135 | * SoC differences accounted for. |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 136 | * |
| 137 | * Preserve EN_LOCKDET, etc. |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 138 | */ |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 139 | if (pll) |
| 140 | misc_data = readl(&pll->pll_misc); |
| 141 | else |
| 142 | misc_data = readl(&simple_pll->pll_misc); |
| 143 | misc_data &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift); |
| 144 | misc_data |= cpcon << pllinfo->kcp_shift; |
| 145 | misc_data &= ~(pllinfo->kvco_mask << pllinfo->kvco_shift); |
| 146 | misc_data |= lfcon << pllinfo->kvco_shift; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 147 | |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 148 | data = (divm << pllinfo->m_shift) | (divn << pllinfo->n_shift); |
| 149 | data |= divp << pllinfo->p_shift; |
| 150 | data |= (1 << PLL_ENABLE_SHIFT); /* BYPASS s/b 0 already */ |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 151 | |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 152 | if (pll) { |
| 153 | writel(misc_data, &pll->pll_misc); |
| 154 | writel(data, &pll->pll_base); |
| 155 | } else { |
Simon Glass | 5a30cee | 2015-08-10 07:14:36 -0600 | [diff] [blame] | 156 | writel(misc_data, &simple_pll->pll_misc); |
| 157 | writel(data, &simple_pll->pll_base); |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 158 | } |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 159 | |
| 160 | /* calculate the stable time */ |
| 161 | return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US; |
| 162 | } |
| 163 | |
| 164 | void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source, |
| 165 | unsigned divisor) |
| 166 | { |
| 167 | u32 *reg = get_periph_source_reg(periph_id); |
| 168 | u32 value; |
| 169 | |
| 170 | value = readl(reg); |
| 171 | |
Stephen Warren | 9cb0c6d | 2014-01-24 10:16:19 -0700 | [diff] [blame] | 172 | value &= ~OUT_CLK_SOURCE_31_30_MASK; |
| 173 | value |= source << OUT_CLK_SOURCE_31_30_SHIFT; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 174 | |
| 175 | value &= ~OUT_CLK_DIVISOR_MASK; |
| 176 | value |= divisor << OUT_CLK_DIVISOR_SHIFT; |
| 177 | |
| 178 | writel(value, reg); |
| 179 | } |
| 180 | |
Simon Glass | 7bb6199 | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 181 | int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits, |
| 182 | unsigned source) |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 183 | { |
| 184 | u32 *reg = get_periph_source_reg(periph_id); |
| 185 | |
Simon Glass | 7bb6199 | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 186 | switch (mux_bits) { |
| 187 | case MASK_BITS_31_30: |
| 188 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK, |
| 189 | source << OUT_CLK_SOURCE_31_30_SHIFT); |
| 190 | break; |
| 191 | |
| 192 | case MASK_BITS_31_29: |
| 193 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK, |
| 194 | source << OUT_CLK_SOURCE_31_29_SHIFT); |
| 195 | break; |
| 196 | |
| 197 | case MASK_BITS_31_28: |
| 198 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK, |
| 199 | source << OUT_CLK_SOURCE_31_28_SHIFT); |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Stephen Warren | d0ad8a5 | 2016-09-13 10:45:56 -0600 | [diff] [blame] | 209 | static int clock_ll_get_source_bits(enum periph_id periph_id, int mux_bits) |
| 210 | { |
| 211 | u32 *reg = get_periph_source_reg(periph_id); |
| 212 | u32 val = readl(reg); |
| 213 | |
| 214 | switch (mux_bits) { |
| 215 | case MASK_BITS_31_30: |
| 216 | val >>= OUT_CLK_SOURCE_31_30_SHIFT; |
| 217 | val &= OUT_CLK_SOURCE_31_30_MASK; |
| 218 | return val; |
| 219 | case MASK_BITS_31_29: |
| 220 | val >>= OUT_CLK_SOURCE_31_29_SHIFT; |
| 221 | val &= OUT_CLK_SOURCE_31_29_MASK; |
| 222 | return val; |
| 223 | case MASK_BITS_31_28: |
| 224 | val >>= OUT_CLK_SOURCE_31_28_SHIFT; |
| 225 | val &= OUT_CLK_SOURCE_31_28_MASK; |
| 226 | return val; |
| 227 | default: |
| 228 | return -1; |
| 229 | } |
| 230 | } |
| 231 | |
Simon Glass | 7bb6199 | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 232 | void clock_ll_set_source(enum periph_id periph_id, unsigned source) |
| 233 | { |
| 234 | clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Given the parent's rate and the required rate for the children, this works |
| 239 | * out the peripheral clock divider to use, in 7.1 binary format. |
| 240 | * |
| 241 | * @param divider_bits number of divider bits (8 or 16) |
| 242 | * @param parent_rate clock rate of parent clock in Hz |
| 243 | * @param rate required clock rate for this clock |
| 244 | * @return divider which should be used |
| 245 | */ |
| 246 | static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate, |
| 247 | unsigned long rate) |
| 248 | { |
| 249 | u64 divider = parent_rate * 2; |
| 250 | unsigned max_divider = 1 << divider_bits; |
| 251 | |
| 252 | divider += rate - 1; |
| 253 | do_div(divider, rate); |
| 254 | |
| 255 | if ((s64)divider - 2 < 0) |
| 256 | return 0; |
| 257 | |
| 258 | if ((s64)divider - 2 >= max_divider) |
| 259 | return -1; |
| 260 | |
| 261 | return divider - 2; |
| 262 | } |
| 263 | |
| 264 | int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate) |
| 265 | { |
| 266 | struct clk_pll *pll = get_pll(clkid); |
| 267 | int data = 0, div = 0, offset = 0; |
| 268 | |
| 269 | if (!clock_id_is_pll(clkid)) |
| 270 | return -1; |
| 271 | |
| 272 | if (pllout + 1 > pll_num_clkouts[clkid]) |
| 273 | return -1; |
| 274 | |
| 275 | div = clk_get_divider(8, pll_rate[clkid], rate); |
| 276 | |
| 277 | if (div < 0) |
| 278 | return -1; |
| 279 | |
| 280 | /* out2 and out4 are in the high part of the register */ |
| 281 | if (pllout == PLL_OUT2 || pllout == PLL_OUT4) |
| 282 | offset = 16; |
| 283 | |
| 284 | data = (div << PLL_OUT_RATIO_SHIFT) | |
| 285 | PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN; |
| 286 | clrsetbits_le32(&pll->pll_out[pllout >> 1], |
| 287 | PLL_OUT_RATIO_MASK << offset, data << offset); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Given the parent's rate and the divider in 7.1 format, this works out the |
| 294 | * resulting peripheral clock rate. |
| 295 | * |
| 296 | * @param parent_rate clock rate of parent clock in Hz |
| 297 | * @param divider which should be used in 7.1 format |
| 298 | * @return effective clock rate of peripheral |
| 299 | */ |
| 300 | static unsigned long get_rate_from_divider(unsigned long parent_rate, |
| 301 | int divider) |
| 302 | { |
| 303 | u64 rate; |
| 304 | |
| 305 | rate = (u64)parent_rate * 2; |
| 306 | do_div(rate, divider + 2); |
| 307 | return rate; |
| 308 | } |
| 309 | |
| 310 | unsigned long clock_get_periph_rate(enum periph_id periph_id, |
| 311 | enum clock_id parent) |
| 312 | { |
| 313 | u32 *reg = get_periph_source_reg(periph_id); |
Stephen Warren | 7468676 | 2016-09-23 16:44:51 -0600 | [diff] [blame^] | 314 | unsigned parent_rate = pll_rate[parent]; |
| 315 | int div = (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 316 | |
Stephen Warren | 7468676 | 2016-09-23 16:44:51 -0600 | [diff] [blame^] | 317 | switch (periph_id) { |
| 318 | case PERIPH_ID_UART1: |
| 319 | case PERIPH_ID_UART2: |
| 320 | case PERIPH_ID_UART3: |
| 321 | case PERIPH_ID_UART4: |
| 322 | case PERIPH_ID_UART5: |
| 323 | #ifdef CONFIG_TEGRA20 |
| 324 | /* There's no divider for these clocks in this SoC. */ |
| 325 | return parent_rate; |
| 326 | #else |
| 327 | /* |
| 328 | * This undoes the +2 in get_rate_from_divider() which I |
| 329 | * believe is incorrect. Ideally we would fix |
| 330 | * get_rate_from_divider(), but... Removing the +2 from |
| 331 | * get_rate_from_divider() would probably require remove the -2 |
| 332 | * from the tail of clk_get_divider() since I believe that's |
| 333 | * only there to invert get_rate_from_divider()'s +2. Observe |
| 334 | * how find_best_divider() uses those two functions together. |
| 335 | * However, doing so breaks other stuff, such as Seaboard's |
| 336 | * display, likely due to clock_set_pllout()'s call to |
| 337 | * clk_get_divider(). Attempting to fix that by making |
| 338 | * clock_set_pllout() subtract 2 from clk_get_divider()'s |
| 339 | * return value doesn't help. In summary this clock driver is |
| 340 | * quite broken but I'm afraid I have no idea how to fix it |
| 341 | * without completely replacing it. |
| 342 | */ |
| 343 | div -= 2; |
| 344 | break; |
| 345 | #endif |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | return get_rate_from_divider(parent_rate, div); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Find the best available 7.1 format divisor given a parent clock rate and |
| 355 | * required child clock rate. This function assumes that a second-stage |
| 356 | * divisor is available which can divide by powers of 2 from 1 to 256. |
| 357 | * |
| 358 | * @param divider_bits number of divider bits (8 or 16) |
| 359 | * @param parent_rate clock rate of parent clock in Hz |
| 360 | * @param rate required clock rate for this clock |
| 361 | * @param extra_div value for the second-stage divisor (not set if this |
| 362 | * function returns -1. |
| 363 | * @return divider which should be used, or -1 if nothing is valid |
| 364 | * |
| 365 | */ |
| 366 | static int find_best_divider(unsigned divider_bits, unsigned long parent_rate, |
| 367 | unsigned long rate, int *extra_div) |
| 368 | { |
| 369 | int shift; |
| 370 | int best_divider = -1; |
| 371 | int best_error = rate; |
| 372 | |
| 373 | /* try dividers from 1 to 256 and find closest match */ |
| 374 | for (shift = 0; shift <= 8 && best_error > 0; shift++) { |
| 375 | unsigned divided_parent = parent_rate >> shift; |
| 376 | int divider = clk_get_divider(divider_bits, divided_parent, |
| 377 | rate); |
| 378 | unsigned effective_rate = get_rate_from_divider(divided_parent, |
| 379 | divider); |
| 380 | int error = rate - effective_rate; |
| 381 | |
| 382 | /* Given a valid divider, look for the lowest error */ |
| 383 | if (divider != -1 && error < best_error) { |
| 384 | best_error = error; |
| 385 | *extra_div = 1 << shift; |
| 386 | best_divider = divider; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* return what we found - *extra_div will already be set */ |
| 391 | return best_divider; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Adjust peripheral PLL to use the given divider and source. |
| 396 | * |
| 397 | * @param periph_id peripheral to adjust |
| 398 | * @param source Source number (0-3 or 0-7) |
| 399 | * @param mux_bits Number of mux bits (2 or 4) |
| 400 | * @param divider Required divider in 7.1 or 15.1 format |
| 401 | * @return 0 if ok, -1 on error (requesting a parent clock which is not valid |
| 402 | * for this peripheral) |
| 403 | */ |
| 404 | static int adjust_periph_pll(enum periph_id periph_id, int source, |
| 405 | int mux_bits, unsigned divider) |
| 406 | { |
| 407 | u32 *reg = get_periph_source_reg(periph_id); |
| 408 | |
| 409 | clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK, |
| 410 | divider << OUT_CLK_DIVISOR_SHIFT); |
| 411 | udelay(1); |
| 412 | |
| 413 | /* work out the source clock and set it */ |
| 414 | if (source < 0) |
| 415 | return -1; |
Tom Warren | c82014d | 2014-01-24 10:16:22 -0700 | [diff] [blame] | 416 | |
Simon Glass | 7bb6199 | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 417 | clock_ll_set_source_bits(periph_id, mux_bits, source); |
Tom Warren | c82014d | 2014-01-24 10:16:22 -0700 | [diff] [blame] | 418 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 419 | udelay(2); |
| 420 | return 0; |
| 421 | } |
| 422 | |
Stephen Warren | d0ad8a5 | 2016-09-13 10:45:56 -0600 | [diff] [blame] | 423 | enum clock_id clock_get_periph_parent(enum periph_id periph_id) |
| 424 | { |
| 425 | int err, mux_bits, divider_bits, type; |
| 426 | int source; |
| 427 | |
| 428 | err = get_periph_clock_info(periph_id, &mux_bits, ÷r_bits, &type); |
| 429 | if (err) |
| 430 | return CLOCK_ID_NONE; |
| 431 | |
| 432 | source = clock_ll_get_source_bits(periph_id, mux_bits); |
| 433 | |
| 434 | return get_periph_clock_id(periph_id, source); |
| 435 | } |
| 436 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 437 | unsigned clock_adjust_periph_pll_div(enum periph_id periph_id, |
| 438 | enum clock_id parent, unsigned rate, int *extra_div) |
| 439 | { |
| 440 | unsigned effective_rate; |
| 441 | int mux_bits, divider_bits, source; |
| 442 | int divider; |
Allen Martin | a51f7de | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 443 | int xdiv = 0; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 444 | |
| 445 | /* work out the source clock and set it */ |
| 446 | source = get_periph_clock_source(periph_id, parent, &mux_bits, |
| 447 | ÷r_bits); |
| 448 | |
Allen Martin | a51f7de | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 449 | divider = find_best_divider(divider_bits, pll_rate[parent], |
| 450 | rate, &xdiv); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 451 | if (extra_div) |
Allen Martin | a51f7de | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 452 | *extra_div = xdiv; |
| 453 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 454 | assert(divider >= 0); |
| 455 | if (adjust_periph_pll(periph_id, source, mux_bits, divider)) |
| 456 | return -1U; |
| 457 | debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate, |
| 458 | get_periph_source_reg(periph_id), |
| 459 | readl(get_periph_source_reg(periph_id))); |
| 460 | |
| 461 | /* Check what we ended up with. This shouldn't matter though */ |
| 462 | effective_rate = clock_get_periph_rate(periph_id, parent); |
| 463 | if (extra_div) |
| 464 | effective_rate /= *extra_div; |
| 465 | if (rate != effective_rate) |
| 466 | debug("Requested clock rate %u not honored (got %u)\n", |
| 467 | rate, effective_rate); |
| 468 | return effective_rate; |
| 469 | } |
| 470 | |
| 471 | unsigned clock_start_periph_pll(enum periph_id periph_id, |
| 472 | enum clock_id parent, unsigned rate) |
| 473 | { |
| 474 | unsigned effective_rate; |
| 475 | |
| 476 | reset_set_enable(periph_id, 1); |
| 477 | clock_enable(periph_id); |
| 478 | |
| 479 | effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate, |
| 480 | NULL); |
| 481 | |
| 482 | reset_set_enable(periph_id, 0); |
| 483 | return effective_rate; |
| 484 | } |
| 485 | |
| 486 | void clock_enable(enum periph_id clkid) |
| 487 | { |
| 488 | clock_set_enable(clkid, 1); |
| 489 | } |
| 490 | |
| 491 | void clock_disable(enum periph_id clkid) |
| 492 | { |
| 493 | clock_set_enable(clkid, 0); |
| 494 | } |
| 495 | |
| 496 | void reset_periph(enum periph_id periph_id, int us_delay) |
| 497 | { |
| 498 | /* Put peripheral into reset */ |
| 499 | reset_set_enable(periph_id, 1); |
| 500 | udelay(us_delay); |
| 501 | |
| 502 | /* Remove reset */ |
| 503 | reset_set_enable(periph_id, 0); |
| 504 | |
| 505 | udelay(us_delay); |
| 506 | } |
| 507 | |
| 508 | void reset_cmplx_set_enable(int cpu, int which, int reset) |
| 509 | { |
| 510 | struct clk_rst_ctlr *clkrst = |
| 511 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 512 | u32 mask; |
| 513 | |
| 514 | /* Form the mask, which depends on the cpu chosen (2 or 4) */ |
| 515 | assert(cpu >= 0 && cpu < MAX_NUM_CPU); |
| 516 | mask = which << cpu; |
| 517 | |
| 518 | /* either enable or disable those reset for that CPU */ |
| 519 | if (reset) |
| 520 | writel(mask, &clkrst->crc_cpu_cmplx_set); |
| 521 | else |
| 522 | writel(mask, &clkrst->crc_cpu_cmplx_clr); |
| 523 | } |
| 524 | |
Thierry Reding | c043c02 | 2015-08-20 11:42:19 +0200 | [diff] [blame] | 525 | unsigned int __weak clk_m_get_rate(unsigned int parent_rate) |
| 526 | { |
| 527 | return parent_rate; |
| 528 | } |
| 529 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 530 | unsigned clock_get_rate(enum clock_id clkid) |
| 531 | { |
| 532 | struct clk_pll *pll; |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 533 | u32 base, divm; |
| 534 | u64 parent_rate, rate; |
| 535 | struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid]; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 536 | |
| 537 | parent_rate = osc_freq[clock_get_osc_freq()]; |
| 538 | if (clkid == CLOCK_ID_OSC) |
| 539 | return parent_rate; |
| 540 | |
Thierry Reding | c043c02 | 2015-08-20 11:42:19 +0200 | [diff] [blame] | 541 | if (clkid == CLOCK_ID_CLK_M) |
| 542 | return clk_m_get_rate(parent_rate); |
| 543 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 544 | pll = get_pll(clkid); |
Simon Glass | 801b05c | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 545 | if (!pll) |
| 546 | return 0; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 547 | base = readl(&pll->pll_base); |
| 548 | |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 549 | rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask); |
| 550 | divm = (base >> pllinfo->m_shift) & pllinfo->m_mask; |
| 551 | /* |
| 552 | * PLLU uses p_mask/p_shift for VCO on all but T210, |
| 553 | * T210 uses normal DIVP. Handled in pllinfo table. |
| 554 | */ |
Stephen Warren | 6c7dc62 | 2015-08-19 17:03:59 -0600 | [diff] [blame] | 555 | #ifdef CONFIG_TEGRA210 |
| 556 | /* |
| 557 | * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is |
| 558 | * not applied. pllP_out2 does have divp applied. All other pllP_outN |
| 559 | * are divided down from pllP_out0. We only support pllP_out0 in |
| 560 | * U-Boot at the time of writing this comment. |
| 561 | */ |
| 562 | if (clkid != CLOCK_ID_PERIPH) |
| 563 | #endif |
| 564 | divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 565 | do_div(rate, divm); |
| 566 | return rate; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Set the output frequency you want for each PLL clock. |
| 571 | * PLL output frequencies are programmed by setting their N, M and P values. |
| 572 | * The governing equations are: |
| 573 | * VCO = (Fi / m) * n, Fo = VCO / (2^p) |
| 574 | * where Fo is the output frequency from the PLL. |
| 575 | * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi) |
| 576 | * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1 |
| 577 | * Please see Tegra TRM section 5.3 to get the detail for PLL Programming |
| 578 | * |
| 579 | * @param n PLL feedback divider(DIVN) |
| 580 | * @param m PLL input divider(DIVN) |
| 581 | * @param p post divider(DIVP) |
| 582 | * @param cpcon base PLL charge pump(CPCON) |
| 583 | * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot |
Robert P. J. Day | 62a3b7d | 2016-07-15 13:44:45 -0400 | [diff] [blame] | 584 | * be overridden), 1 if PLL is already correct |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 585 | */ |
| 586 | int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon) |
| 587 | { |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 588 | u32 base_reg, misc_reg; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 589 | struct clk_pll *pll; |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 590 | struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid]; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 591 | |
| 592 | pll = get_pll(clkid); |
| 593 | |
| 594 | base_reg = readl(&pll->pll_base); |
| 595 | |
| 596 | /* Set BYPASS, m, n and p to PLL_BASE */ |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 597 | base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift); |
| 598 | base_reg |= m << pllinfo->m_shift; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 599 | |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 600 | base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift); |
| 601 | base_reg |= n << pllinfo->n_shift; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 602 | |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 603 | base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift); |
| 604 | base_reg |= p << pllinfo->p_shift; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 605 | |
| 606 | if (clkid == CLOCK_ID_PERIPH) { |
| 607 | /* |
| 608 | * If the PLL is already set up, check that it is correct |
| 609 | * and record this info for clock_verify() to check. |
| 610 | */ |
| 611 | if (base_reg & PLL_BASE_OVRRIDE_MASK) { |
| 612 | base_reg |= PLL_ENABLE_MASK; |
| 613 | if (base_reg != readl(&pll->pll_base)) |
| 614 | pllp_valid = 0; |
| 615 | return pllp_valid ? 1 : -1; |
| 616 | } |
| 617 | base_reg |= PLL_BASE_OVRRIDE_MASK; |
| 618 | } |
| 619 | |
| 620 | base_reg |= PLL_BYPASS_MASK; |
| 621 | writel(base_reg, &pll->pll_base); |
| 622 | |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 623 | /* Set cpcon (KCP) to PLL_MISC */ |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 624 | misc_reg = readl(&pll->pll_misc); |
Tom Warren | 722e000 | 2015-06-25 09:50:44 -0700 | [diff] [blame] | 625 | misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift); |
| 626 | misc_reg |= cpcon << pllinfo->kcp_shift; |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 627 | writel(misc_reg, &pll->pll_misc); |
| 628 | |
| 629 | /* Enable PLL */ |
| 630 | base_reg |= PLL_ENABLE_MASK; |
| 631 | writel(base_reg, &pll->pll_base); |
| 632 | |
| 633 | /* Disable BYPASS */ |
| 634 | base_reg &= ~PLL_BYPASS_MASK; |
| 635 | writel(base_reg, &pll->pll_base); |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | void clock_ll_start_uart(enum periph_id periph_id) |
| 641 | { |
| 642 | /* Assert UART reset and enable clock */ |
| 643 | reset_set_enable(periph_id, 1); |
| 644 | clock_enable(periph_id); |
| 645 | clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */ |
| 646 | |
| 647 | /* wait for 2us */ |
| 648 | udelay(2); |
| 649 | |
| 650 | /* De-assert reset to UART */ |
| 651 | reset_set_enable(periph_id, 0); |
| 652 | } |
| 653 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 654 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 655 | int clock_decode_periph_id(const void *blob, int node) |
| 656 | { |
| 657 | enum periph_id id; |
| 658 | u32 cell[2]; |
| 659 | int err; |
| 660 | |
| 661 | err = fdtdec_get_int_array(blob, node, "clocks", cell, |
| 662 | ARRAY_SIZE(cell)); |
| 663 | if (err) |
| 664 | return -1; |
| 665 | id = clk_id_to_periph_id(cell[1]); |
| 666 | assert(clock_periph_id_isvalid(id)); |
| 667 | return id; |
| 668 | } |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 669 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 670 | |
| 671 | int clock_verify(void) |
| 672 | { |
| 673 | struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH); |
| 674 | u32 reg = readl(&pll->pll_base); |
| 675 | |
| 676 | if (!pllp_valid) { |
| 677 | printf("Warning: PLLP %x is not correct\n", reg); |
| 678 | return -1; |
| 679 | } |
| 680 | debug("PLLP %x is correct\n", reg); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | void clock_init(void) |
| 685 | { |
Stephen Warren | 6dbcc96 | 2016-09-13 10:45:55 -0600 | [diff] [blame] | 686 | int i; |
| 687 | |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 688 | pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 689 | pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY); |
| 690 | pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH); |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 691 | pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB); |
Simon Glass | 96e82a2 | 2015-04-14 21:03:34 -0600 | [diff] [blame] | 692 | pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 693 | pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU); |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 694 | pll_rate[CLOCK_ID_SFROM32KHZ] = 32768; |
| 695 | pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC); |
Thierry Reding | c043c02 | 2015-08-20 11:42:19 +0200 | [diff] [blame] | 696 | pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M); |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 697 | |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 698 | debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]); |
Thierry Reding | c043c02 | 2015-08-20 11:42:19 +0200 | [diff] [blame] | 699 | debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]); |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 700 | debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 701 | debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]); |
| 702 | debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]); |
Tom Warren | 3e8650c | 2015-06-22 13:03:44 -0700 | [diff] [blame] | 703 | debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]); |
Simon Glass | 96e82a2 | 2015-04-14 21:03:34 -0600 | [diff] [blame] | 704 | debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]); |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 705 | debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]); |
Stephen Warren | 6dbcc96 | 2016-09-13 10:45:55 -0600 | [diff] [blame] | 706 | |
| 707 | for (i = 0; periph_clk_init_table[i].periph_id != -1; i++) { |
| 708 | enum periph_id periph_id; |
| 709 | enum clock_id parent; |
| 710 | int source, mux_bits, divider_bits; |
| 711 | |
| 712 | periph_id = periph_clk_init_table[i].periph_id; |
| 713 | parent = periph_clk_init_table[i].parent_clock_id; |
| 714 | |
| 715 | source = get_periph_clock_source(periph_id, parent, &mux_bits, |
| 716 | ÷r_bits); |
| 717 | clock_ll_set_source_bits(periph_id, mux_bits, source); |
| 718 | } |
Tom Warren | f29f086 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 719 | } |
Jimmy Zhang | b9dd621 | 2014-01-24 10:37:36 -0700 | [diff] [blame] | 720 | |
| 721 | static void set_avp_clock_source(u32 src) |
| 722 | { |
| 723 | struct clk_rst_ctlr *clkrst = |
| 724 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 725 | u32 val; |
| 726 | |
| 727 | val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) | |
| 728 | (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) | |
| 729 | (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) | |
| 730 | (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) | |
| 731 | (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT); |
| 732 | writel(val, &clkrst->crc_sclk_brst_pol); |
| 733 | udelay(3); |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * This function is useful on Tegra30, and any later SoCs that have compatible |
| 738 | * PLLP configuration registers. |
Tom Warren | 7aaa5a6 | 2015-03-04 16:36:00 -0700 | [diff] [blame] | 739 | * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c |
Jimmy Zhang | b9dd621 | 2014-01-24 10:37:36 -0700 | [diff] [blame] | 740 | */ |
| 741 | void tegra30_set_up_pllp(void) |
| 742 | { |
| 743 | struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 744 | u32 reg; |
| 745 | |
| 746 | /* |
| 747 | * Based on the Tegra TRM, the system clock (which is the AVP clock) can |
| 748 | * run up to 275MHz. On power on, the default sytem clock source is set |
| 749 | * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to |
| 750 | * 408MHz which is beyond system clock's upper limit. |
| 751 | * |
| 752 | * The fix is to set the system clock to CLK_M before initializing PLLP, |
| 753 | * and then switch back to PLLP_OUT4, which has an appropriate divider |
| 754 | * configured, after PLLP has been configured |
| 755 | */ |
| 756 | set_avp_clock_source(SCLK_SOURCE_CLKM); |
| 757 | |
| 758 | /* |
| 759 | * PLLP output frequency set to 408Mhz |
| 760 | * PLLC output frequency set to 228Mhz |
| 761 | */ |
| 762 | switch (clock_get_osc_freq()) { |
| 763 | case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */ |
| 764 | clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8); |
| 765 | clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8); |
| 766 | break; |
| 767 | |
| 768 | case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */ |
| 769 | clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8); |
| 770 | clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8); |
| 771 | break; |
| 772 | |
| 773 | case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */ |
| 774 | clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8); |
| 775 | clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8); |
| 776 | break; |
| 777 | case CLOCK_OSC_FREQ_19_2: |
| 778 | default: |
| 779 | /* |
| 780 | * These are not supported. It is too early to print a |
| 781 | * message and the UART likely won't work anyway due to the |
| 782 | * oscillator being wrong. |
| 783 | */ |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */ |
| 788 | |
| 789 | /* OUT1, 2 */ |
| 790 | /* Assert RSTN before enable */ |
| 791 | reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN; |
| 792 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]); |
| 793 | /* Set divisor and reenable */ |
| 794 | reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO) |
| 795 | | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS |
| 796 | | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO) |
| 797 | | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS; |
| 798 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]); |
| 799 | |
| 800 | /* OUT3, 4 */ |
| 801 | /* Assert RSTN before enable */ |
| 802 | reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN; |
| 803 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]); |
| 804 | /* Set divisor and reenable */ |
| 805 | reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO) |
| 806 | | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS |
| 807 | | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO) |
| 808 | | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS; |
| 809 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]); |
| 810 | |
| 811 | set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4); |
| 812 | } |
Simon Glass | 746dc76 | 2015-06-05 14:39:36 -0600 | [diff] [blame] | 813 | |
| 814 | int clock_external_output(int clk_id) |
| 815 | { |
| 816 | struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE; |
| 817 | |
| 818 | if (clk_id >= 1 && clk_id <= 3) { |
| 819 | setbits_le32(&pmc->pmc_clk_out_cntrl, |
| 820 | 1 << (2 + (clk_id - 1) * 8)); |
| 821 | } else { |
| 822 | printf("%s: Unknown output clock id %d\n", __func__, clk_id); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | } |