Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 1 | /* |
Jimmy Zhang | 2a544db | 2014-01-24 10:37:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2010-2014, NVIDIA CORPORATION. All rights reserved. |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | /* Tegra SoC common clock control functions */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <asm/io.h> |
| 21 | #include <asm/arch/clock.h> |
| 22 | #include <asm/arch/tegra.h> |
Stephen Warren | 8d1fb31 | 2015-01-19 16:25:52 -0700 | [diff] [blame] | 23 | #include <asm/arch-tegra/ap.h> |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 24 | #include <asm/arch-tegra/clk_rst.h> |
| 25 | #include <asm/arch-tegra/timer.h> |
| 26 | #include <div64.h> |
| 27 | #include <fdtdec.h> |
| 28 | |
| 29 | /* |
| 30 | * This is our record of the current clock rate of each clock. We don't |
| 31 | * fill all of these in since we are only really interested in clocks which |
| 32 | * we use as parents. |
| 33 | */ |
| 34 | static unsigned pll_rate[CLOCK_ID_COUNT]; |
| 35 | |
| 36 | /* |
| 37 | * The oscillator frequency is fixed to one of four set values. Based on this |
| 38 | * the other clocks are set up appropriately. |
| 39 | */ |
| 40 | static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = { |
| 41 | 13000000, |
| 42 | 19200000, |
| 43 | 12000000, |
| 44 | 26000000, |
| 45 | }; |
| 46 | |
| 47 | /* return 1 if a peripheral ID is in range */ |
| 48 | #define clock_type_id_isvalid(id) ((id) >= 0 && \ |
| 49 | (id) < CLOCK_TYPE_COUNT) |
| 50 | |
| 51 | char pllp_valid = 1; /* PLLP is set up correctly */ |
| 52 | |
| 53 | /* return 1 if a periphc_internal_id is in range */ |
| 54 | #define periphc_internal_id_isvalid(id) ((id) >= 0 && \ |
| 55 | (id) < PERIPHC_COUNT) |
| 56 | |
| 57 | /* number of clock outputs of a PLL */ |
| 58 | static const u8 pll_num_clkouts[] = { |
| 59 | 1, /* PLLC */ |
| 60 | 1, /* PLLM */ |
| 61 | 4, /* PLLP */ |
| 62 | 1, /* PLLA */ |
| 63 | 0, /* PLLU */ |
| 64 | 0, /* PLLD */ |
| 65 | }; |
| 66 | |
| 67 | int clock_get_osc_bypass(void) |
| 68 | { |
| 69 | struct clk_rst_ctlr *clkrst = |
| 70 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 71 | u32 reg; |
| 72 | |
| 73 | reg = readl(&clkrst->crc_osc_ctrl); |
| 74 | return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT; |
| 75 | } |
| 76 | |
| 77 | /* Returns a pointer to the registers of the given pll */ |
| 78 | static struct clk_pll *get_pll(enum clock_id clkid) |
| 79 | { |
| 80 | struct clk_rst_ctlr *clkrst = |
| 81 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 82 | |
| 83 | assert(clock_id_is_pll(clkid)); |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 84 | if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) { |
| 85 | debug("%s: Invalid PLL\n", __func__); |
| 86 | return NULL; |
| 87 | } |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 88 | return &clkrst->crc_pll[clkid]; |
| 89 | } |
| 90 | |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 91 | __weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid) |
| 92 | { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 96 | int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn, |
| 97 | u32 *divp, u32 *cpcon, u32 *lfcon) |
| 98 | { |
| 99 | struct clk_pll *pll = get_pll(clkid); |
| 100 | u32 data; |
| 101 | |
| 102 | assert(clkid != CLOCK_ID_USB); |
| 103 | |
| 104 | /* Safety check, adds to code size but is small */ |
| 105 | if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB) |
| 106 | return -1; |
| 107 | data = readl(&pll->pll_base); |
| 108 | *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT; |
| 109 | *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT; |
| 110 | *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT; |
| 111 | data = readl(&pll->pll_misc); |
| 112 | *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT; |
| 113 | *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn, |
| 119 | u32 divp, u32 cpcon, u32 lfcon) |
| 120 | { |
| 121 | struct clk_pll *pll = get_pll(clkid); |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 122 | u32 misc_data, data; |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * We cheat by treating all PLL (except PLLU) in the same fashion. |
| 126 | * This works only because: |
| 127 | * - same fields are always mapped at same offsets, except DCCON |
| 128 | * - DCCON is always 0, doesn't conflict |
| 129 | * - M,N, P of PLLP values are ignored for PLLP |
| 130 | */ |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 131 | misc_data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT); |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 132 | |
| 133 | data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) | |
| 134 | (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT); |
| 135 | |
| 136 | if (clkid == CLOCK_ID_USB) |
| 137 | data |= divp << PLLU_VCO_FREQ_SHIFT; |
| 138 | else |
| 139 | data |= divp << PLL_DIVP_SHIFT; |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 140 | if (pll) { |
| 141 | writel(misc_data, &pll->pll_misc); |
| 142 | writel(data, &pll->pll_base); |
| 143 | } else { |
| 144 | struct clk_pll_simple *pll = clock_get_simple_pll(clkid); |
| 145 | |
| 146 | if (!pll) { |
| 147 | debug("%s: Uknown simple PLL %d\n", __func__, clkid); |
| 148 | return 0; |
| 149 | } |
| 150 | writel(misc_data, &pll->pll_misc); |
| 151 | writel(data, &pll->pll_base); |
| 152 | } |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 153 | |
| 154 | /* calculate the stable time */ |
| 155 | return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US; |
| 156 | } |
| 157 | |
| 158 | void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source, |
| 159 | unsigned divisor) |
| 160 | { |
| 161 | u32 *reg = get_periph_source_reg(periph_id); |
| 162 | u32 value; |
| 163 | |
| 164 | value = readl(reg); |
| 165 | |
Stephen Warren | df5ed45 | 2014-01-24 10:16:19 -0700 | [diff] [blame] | 166 | value &= ~OUT_CLK_SOURCE_31_30_MASK; |
| 167 | value |= source << OUT_CLK_SOURCE_31_30_SHIFT; |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 168 | |
| 169 | value &= ~OUT_CLK_DIVISOR_MASK; |
| 170 | value |= divisor << OUT_CLK_DIVISOR_SHIFT; |
| 171 | |
| 172 | writel(value, reg); |
| 173 | } |
| 174 | |
Simon Glass | d2d1c3f | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 175 | int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits, |
| 176 | unsigned source) |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 177 | { |
| 178 | u32 *reg = get_periph_source_reg(periph_id); |
| 179 | |
Simon Glass | d2d1c3f | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 180 | switch (mux_bits) { |
| 181 | case MASK_BITS_31_30: |
| 182 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK, |
| 183 | source << OUT_CLK_SOURCE_31_30_SHIFT); |
| 184 | break; |
| 185 | |
| 186 | case MASK_BITS_31_29: |
| 187 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK, |
| 188 | source << OUT_CLK_SOURCE_31_29_SHIFT); |
| 189 | break; |
| 190 | |
| 191 | case MASK_BITS_31_28: |
| 192 | clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK, |
| 193 | source << OUT_CLK_SOURCE_31_28_SHIFT); |
| 194 | break; |
| 195 | |
| 196 | default: |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | d2d1c3f | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 203 | void clock_ll_set_source(enum periph_id periph_id, unsigned source) |
| 204 | { |
| 205 | clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source); |
| 206 | } |
| 207 | |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Given the parent's rate and the required rate for the children, this works |
| 210 | * out the peripheral clock divider to use, in 7.1 binary format. |
| 211 | * |
| 212 | * @param divider_bits number of divider bits (8 or 16) |
| 213 | * @param parent_rate clock rate of parent clock in Hz |
| 214 | * @param rate required clock rate for this clock |
| 215 | * @return divider which should be used |
| 216 | */ |
| 217 | static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate, |
| 218 | unsigned long rate) |
| 219 | { |
| 220 | u64 divider = parent_rate * 2; |
| 221 | unsigned max_divider = 1 << divider_bits; |
| 222 | |
| 223 | divider += rate - 1; |
| 224 | do_div(divider, rate); |
| 225 | |
| 226 | if ((s64)divider - 2 < 0) |
| 227 | return 0; |
| 228 | |
| 229 | if ((s64)divider - 2 >= max_divider) |
| 230 | return -1; |
| 231 | |
| 232 | return divider - 2; |
| 233 | } |
| 234 | |
| 235 | int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate) |
| 236 | { |
| 237 | struct clk_pll *pll = get_pll(clkid); |
| 238 | int data = 0, div = 0, offset = 0; |
| 239 | |
| 240 | if (!clock_id_is_pll(clkid)) |
| 241 | return -1; |
| 242 | |
| 243 | if (pllout + 1 > pll_num_clkouts[clkid]) |
| 244 | return -1; |
| 245 | |
| 246 | div = clk_get_divider(8, pll_rate[clkid], rate); |
| 247 | |
| 248 | if (div < 0) |
| 249 | return -1; |
| 250 | |
| 251 | /* out2 and out4 are in the high part of the register */ |
| 252 | if (pllout == PLL_OUT2 || pllout == PLL_OUT4) |
| 253 | offset = 16; |
| 254 | |
| 255 | data = (div << PLL_OUT_RATIO_SHIFT) | |
| 256 | PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN; |
| 257 | clrsetbits_le32(&pll->pll_out[pllout >> 1], |
| 258 | PLL_OUT_RATIO_MASK << offset, data << offset); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Given the parent's rate and the divider in 7.1 format, this works out the |
| 265 | * resulting peripheral clock rate. |
| 266 | * |
| 267 | * @param parent_rate clock rate of parent clock in Hz |
| 268 | * @param divider which should be used in 7.1 format |
| 269 | * @return effective clock rate of peripheral |
| 270 | */ |
| 271 | static unsigned long get_rate_from_divider(unsigned long parent_rate, |
| 272 | int divider) |
| 273 | { |
| 274 | u64 rate; |
| 275 | |
| 276 | rate = (u64)parent_rate * 2; |
| 277 | do_div(rate, divider + 2); |
| 278 | return rate; |
| 279 | } |
| 280 | |
| 281 | unsigned long clock_get_periph_rate(enum periph_id periph_id, |
| 282 | enum clock_id parent) |
| 283 | { |
| 284 | u32 *reg = get_periph_source_reg(periph_id); |
| 285 | |
| 286 | return get_rate_from_divider(pll_rate[parent], |
| 287 | (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Find the best available 7.1 format divisor given a parent clock rate and |
| 292 | * required child clock rate. This function assumes that a second-stage |
| 293 | * divisor is available which can divide by powers of 2 from 1 to 256. |
| 294 | * |
| 295 | * @param divider_bits number of divider bits (8 or 16) |
| 296 | * @param parent_rate clock rate of parent clock in Hz |
| 297 | * @param rate required clock rate for this clock |
| 298 | * @param extra_div value for the second-stage divisor (not set if this |
| 299 | * function returns -1. |
| 300 | * @return divider which should be used, or -1 if nothing is valid |
| 301 | * |
| 302 | */ |
| 303 | static int find_best_divider(unsigned divider_bits, unsigned long parent_rate, |
| 304 | unsigned long rate, int *extra_div) |
| 305 | { |
| 306 | int shift; |
| 307 | int best_divider = -1; |
| 308 | int best_error = rate; |
| 309 | |
| 310 | /* try dividers from 1 to 256 and find closest match */ |
| 311 | for (shift = 0; shift <= 8 && best_error > 0; shift++) { |
| 312 | unsigned divided_parent = parent_rate >> shift; |
| 313 | int divider = clk_get_divider(divider_bits, divided_parent, |
| 314 | rate); |
| 315 | unsigned effective_rate = get_rate_from_divider(divided_parent, |
| 316 | divider); |
| 317 | int error = rate - effective_rate; |
| 318 | |
| 319 | /* Given a valid divider, look for the lowest error */ |
| 320 | if (divider != -1 && error < best_error) { |
| 321 | best_error = error; |
| 322 | *extra_div = 1 << shift; |
| 323 | best_divider = divider; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* return what we found - *extra_div will already be set */ |
| 328 | return best_divider; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Adjust peripheral PLL to use the given divider and source. |
| 333 | * |
| 334 | * @param periph_id peripheral to adjust |
| 335 | * @param source Source number (0-3 or 0-7) |
| 336 | * @param mux_bits Number of mux bits (2 or 4) |
| 337 | * @param divider Required divider in 7.1 or 15.1 format |
| 338 | * @return 0 if ok, -1 on error (requesting a parent clock which is not valid |
| 339 | * for this peripheral) |
| 340 | */ |
| 341 | static int adjust_periph_pll(enum periph_id periph_id, int source, |
| 342 | int mux_bits, unsigned divider) |
| 343 | { |
| 344 | u32 *reg = get_periph_source_reg(periph_id); |
| 345 | |
| 346 | clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK, |
| 347 | divider << OUT_CLK_DIVISOR_SHIFT); |
| 348 | udelay(1); |
| 349 | |
| 350 | /* work out the source clock and set it */ |
| 351 | if (source < 0) |
| 352 | return -1; |
Tom Warren | 2fde44e | 2014-01-24 10:16:22 -0700 | [diff] [blame] | 353 | |
Simon Glass | d2d1c3f | 2015-04-14 21:03:33 -0600 | [diff] [blame] | 354 | clock_ll_set_source_bits(periph_id, mux_bits, source); |
Tom Warren | 2fde44e | 2014-01-24 10:16:22 -0700 | [diff] [blame] | 355 | |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 356 | udelay(2); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | unsigned clock_adjust_periph_pll_div(enum periph_id periph_id, |
| 361 | enum clock_id parent, unsigned rate, int *extra_div) |
| 362 | { |
| 363 | unsigned effective_rate; |
| 364 | int mux_bits, divider_bits, source; |
| 365 | int divider; |
Allen Martin | 810a4e4 | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 366 | int xdiv = 0; |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 367 | |
| 368 | /* work out the source clock and set it */ |
| 369 | source = get_periph_clock_source(periph_id, parent, &mux_bits, |
| 370 | ÷r_bits); |
| 371 | |
Allen Martin | 810a4e4 | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 372 | divider = find_best_divider(divider_bits, pll_rate[parent], |
| 373 | rate, &xdiv); |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 374 | if (extra_div) |
Allen Martin | 810a4e4 | 2013-05-10 16:56:55 +0000 | [diff] [blame] | 375 | *extra_div = xdiv; |
| 376 | |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 377 | assert(divider >= 0); |
| 378 | if (adjust_periph_pll(periph_id, source, mux_bits, divider)) |
| 379 | return -1U; |
| 380 | debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate, |
| 381 | get_periph_source_reg(periph_id), |
| 382 | readl(get_periph_source_reg(periph_id))); |
| 383 | |
| 384 | /* Check what we ended up with. This shouldn't matter though */ |
| 385 | effective_rate = clock_get_periph_rate(periph_id, parent); |
| 386 | if (extra_div) |
| 387 | effective_rate /= *extra_div; |
| 388 | if (rate != effective_rate) |
| 389 | debug("Requested clock rate %u not honored (got %u)\n", |
| 390 | rate, effective_rate); |
| 391 | return effective_rate; |
| 392 | } |
| 393 | |
| 394 | unsigned clock_start_periph_pll(enum periph_id periph_id, |
| 395 | enum clock_id parent, unsigned rate) |
| 396 | { |
| 397 | unsigned effective_rate; |
| 398 | |
| 399 | reset_set_enable(periph_id, 1); |
| 400 | clock_enable(periph_id); |
| 401 | |
| 402 | effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate, |
| 403 | NULL); |
| 404 | |
| 405 | reset_set_enable(periph_id, 0); |
| 406 | return effective_rate; |
| 407 | } |
| 408 | |
| 409 | void clock_enable(enum periph_id clkid) |
| 410 | { |
| 411 | clock_set_enable(clkid, 1); |
| 412 | } |
| 413 | |
| 414 | void clock_disable(enum periph_id clkid) |
| 415 | { |
| 416 | clock_set_enable(clkid, 0); |
| 417 | } |
| 418 | |
| 419 | void reset_periph(enum periph_id periph_id, int us_delay) |
| 420 | { |
| 421 | /* Put peripheral into reset */ |
| 422 | reset_set_enable(periph_id, 1); |
| 423 | udelay(us_delay); |
| 424 | |
| 425 | /* Remove reset */ |
| 426 | reset_set_enable(periph_id, 0); |
| 427 | |
| 428 | udelay(us_delay); |
| 429 | } |
| 430 | |
| 431 | void reset_cmplx_set_enable(int cpu, int which, int reset) |
| 432 | { |
| 433 | struct clk_rst_ctlr *clkrst = |
| 434 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 435 | u32 mask; |
| 436 | |
| 437 | /* Form the mask, which depends on the cpu chosen (2 or 4) */ |
| 438 | assert(cpu >= 0 && cpu < MAX_NUM_CPU); |
| 439 | mask = which << cpu; |
| 440 | |
| 441 | /* either enable or disable those reset for that CPU */ |
| 442 | if (reset) |
| 443 | writel(mask, &clkrst->crc_cpu_cmplx_set); |
| 444 | else |
| 445 | writel(mask, &clkrst->crc_cpu_cmplx_clr); |
| 446 | } |
| 447 | |
| 448 | unsigned clock_get_rate(enum clock_id clkid) |
| 449 | { |
| 450 | struct clk_pll *pll; |
| 451 | u32 base; |
| 452 | u32 divm; |
| 453 | u64 parent_rate; |
| 454 | u64 rate; |
| 455 | |
| 456 | parent_rate = osc_freq[clock_get_osc_freq()]; |
| 457 | if (clkid == CLOCK_ID_OSC) |
| 458 | return parent_rate; |
| 459 | |
| 460 | pll = get_pll(clkid); |
Simon Glass | 6017b9a | 2015-04-14 21:03:32 -0600 | [diff] [blame] | 461 | if (!pll) |
| 462 | return 0; |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 463 | base = readl(&pll->pll_base); |
| 464 | |
| 465 | /* Oh for bf_unpack()... */ |
| 466 | rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT); |
| 467 | divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT; |
| 468 | if (clkid == CLOCK_ID_USB) |
| 469 | divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT; |
| 470 | else |
| 471 | divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT; |
| 472 | do_div(rate, divm); |
| 473 | return rate; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Set the output frequency you want for each PLL clock. |
| 478 | * PLL output frequencies are programmed by setting their N, M and P values. |
| 479 | * The governing equations are: |
| 480 | * VCO = (Fi / m) * n, Fo = VCO / (2^p) |
| 481 | * where Fo is the output frequency from the PLL. |
| 482 | * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi) |
| 483 | * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1 |
| 484 | * Please see Tegra TRM section 5.3 to get the detail for PLL Programming |
| 485 | * |
| 486 | * @param n PLL feedback divider(DIVN) |
| 487 | * @param m PLL input divider(DIVN) |
| 488 | * @param p post divider(DIVP) |
| 489 | * @param cpcon base PLL charge pump(CPCON) |
| 490 | * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot |
| 491 | * be overriden), 1 if PLL is already correct |
| 492 | */ |
| 493 | int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon) |
| 494 | { |
| 495 | u32 base_reg; |
| 496 | u32 misc_reg; |
| 497 | struct clk_pll *pll; |
| 498 | |
| 499 | pll = get_pll(clkid); |
| 500 | |
| 501 | base_reg = readl(&pll->pll_base); |
| 502 | |
| 503 | /* Set BYPASS, m, n and p to PLL_BASE */ |
| 504 | base_reg &= ~PLL_DIVM_MASK; |
| 505 | base_reg |= m << PLL_DIVM_SHIFT; |
| 506 | |
| 507 | base_reg &= ~PLL_DIVN_MASK; |
| 508 | base_reg |= n << PLL_DIVN_SHIFT; |
| 509 | |
| 510 | base_reg &= ~PLL_DIVP_MASK; |
| 511 | base_reg |= p << PLL_DIVP_SHIFT; |
| 512 | |
| 513 | if (clkid == CLOCK_ID_PERIPH) { |
| 514 | /* |
| 515 | * If the PLL is already set up, check that it is correct |
| 516 | * and record this info for clock_verify() to check. |
| 517 | */ |
| 518 | if (base_reg & PLL_BASE_OVRRIDE_MASK) { |
| 519 | base_reg |= PLL_ENABLE_MASK; |
| 520 | if (base_reg != readl(&pll->pll_base)) |
| 521 | pllp_valid = 0; |
| 522 | return pllp_valid ? 1 : -1; |
| 523 | } |
| 524 | base_reg |= PLL_BASE_OVRRIDE_MASK; |
| 525 | } |
| 526 | |
| 527 | base_reg |= PLL_BYPASS_MASK; |
| 528 | writel(base_reg, &pll->pll_base); |
| 529 | |
| 530 | /* Set cpcon to PLL_MISC */ |
| 531 | misc_reg = readl(&pll->pll_misc); |
| 532 | misc_reg &= ~PLL_CPCON_MASK; |
| 533 | misc_reg |= cpcon << PLL_CPCON_SHIFT; |
| 534 | writel(misc_reg, &pll->pll_misc); |
| 535 | |
| 536 | /* Enable PLL */ |
| 537 | base_reg |= PLL_ENABLE_MASK; |
| 538 | writel(base_reg, &pll->pll_base); |
| 539 | |
| 540 | /* Disable BYPASS */ |
| 541 | base_reg &= ~PLL_BYPASS_MASK; |
| 542 | writel(base_reg, &pll->pll_base); |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | void clock_ll_start_uart(enum periph_id periph_id) |
| 548 | { |
| 549 | /* Assert UART reset and enable clock */ |
| 550 | reset_set_enable(periph_id, 1); |
| 551 | clock_enable(periph_id); |
| 552 | clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */ |
| 553 | |
| 554 | /* wait for 2us */ |
| 555 | udelay(2); |
| 556 | |
| 557 | /* De-assert reset to UART */ |
| 558 | reset_set_enable(periph_id, 0); |
| 559 | } |
| 560 | |
| 561 | #ifdef CONFIG_OF_CONTROL |
| 562 | int clock_decode_periph_id(const void *blob, int node) |
| 563 | { |
| 564 | enum periph_id id; |
| 565 | u32 cell[2]; |
| 566 | int err; |
| 567 | |
| 568 | err = fdtdec_get_int_array(blob, node, "clocks", cell, |
| 569 | ARRAY_SIZE(cell)); |
| 570 | if (err) |
| 571 | return -1; |
| 572 | id = clk_id_to_periph_id(cell[1]); |
| 573 | assert(clock_periph_id_isvalid(id)); |
| 574 | return id; |
| 575 | } |
| 576 | #endif /* CONFIG_OF_CONTROL */ |
| 577 | |
| 578 | int clock_verify(void) |
| 579 | { |
| 580 | struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH); |
| 581 | u32 reg = readl(&pll->pll_base); |
| 582 | |
| 583 | if (!pllp_valid) { |
| 584 | printf("Warning: PLLP %x is not correct\n", reg); |
| 585 | return -1; |
| 586 | } |
| 587 | debug("PLLP %x is correct\n", reg); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | void clock_init(void) |
| 592 | { |
| 593 | pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY); |
| 594 | pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH); |
| 595 | pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL); |
Simon Glass | 93a1995 | 2015-04-14 21:03:34 -0600 | [diff] [blame^] | 596 | pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY); |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 597 | pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC); |
| 598 | pll_rate[CLOCK_ID_SFROM32KHZ] = 32768; |
| 599 | pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU); |
| 600 | debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]); |
| 601 | debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]); |
| 602 | debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]); |
| 603 | debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]); |
Simon Glass | 93a1995 | 2015-04-14 21:03:34 -0600 | [diff] [blame^] | 604 | debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]); |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 605 | debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]); |
Tom Warren | fbef355 | 2013-04-01 15:48:54 -0700 | [diff] [blame] | 606 | |
| 607 | /* Do any special system timer/TSC setup */ |
Stephen Warren | 8d1fb31 | 2015-01-19 16:25:52 -0700 | [diff] [blame] | 608 | #if defined(CONFIG_TEGRA_SUPPORT_NON_SECURE) |
| 609 | if (!tegra_cpu_is_non_secure()) |
| 610 | #endif |
| 611 | arch_timer_init(); |
Tom Warren | 795f9d7 | 2013-01-23 14:01:01 -0700 | [diff] [blame] | 612 | } |
Jimmy Zhang | 2a544db | 2014-01-24 10:37:36 -0700 | [diff] [blame] | 613 | |
| 614 | static void set_avp_clock_source(u32 src) |
| 615 | { |
| 616 | struct clk_rst_ctlr *clkrst = |
| 617 | (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 618 | u32 val; |
| 619 | |
| 620 | val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) | |
| 621 | (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) | |
| 622 | (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) | |
| 623 | (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) | |
| 624 | (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT); |
| 625 | writel(val, &clkrst->crc_sclk_brst_pol); |
| 626 | udelay(3); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * This function is useful on Tegra30, and any later SoCs that have compatible |
| 631 | * PLLP configuration registers. |
| 632 | */ |
| 633 | void tegra30_set_up_pllp(void) |
| 634 | { |
| 635 | struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE; |
| 636 | u32 reg; |
| 637 | |
| 638 | /* |
| 639 | * Based on the Tegra TRM, the system clock (which is the AVP clock) can |
| 640 | * run up to 275MHz. On power on, the default sytem clock source is set |
| 641 | * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to |
| 642 | * 408MHz which is beyond system clock's upper limit. |
| 643 | * |
| 644 | * The fix is to set the system clock to CLK_M before initializing PLLP, |
| 645 | * and then switch back to PLLP_OUT4, which has an appropriate divider |
| 646 | * configured, after PLLP has been configured |
| 647 | */ |
| 648 | set_avp_clock_source(SCLK_SOURCE_CLKM); |
| 649 | |
| 650 | /* |
| 651 | * PLLP output frequency set to 408Mhz |
| 652 | * PLLC output frequency set to 228Mhz |
| 653 | */ |
| 654 | switch (clock_get_osc_freq()) { |
| 655 | case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */ |
| 656 | clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8); |
| 657 | clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8); |
| 658 | break; |
| 659 | |
| 660 | case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */ |
| 661 | clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8); |
| 662 | clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8); |
| 663 | break; |
| 664 | |
| 665 | case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */ |
| 666 | clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8); |
| 667 | clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8); |
| 668 | break; |
| 669 | case CLOCK_OSC_FREQ_19_2: |
| 670 | default: |
| 671 | /* |
| 672 | * These are not supported. It is too early to print a |
| 673 | * message and the UART likely won't work anyway due to the |
| 674 | * oscillator being wrong. |
| 675 | */ |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */ |
| 680 | |
| 681 | /* OUT1, 2 */ |
| 682 | /* Assert RSTN before enable */ |
| 683 | reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN; |
| 684 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]); |
| 685 | /* Set divisor and reenable */ |
| 686 | reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO) |
| 687 | | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS |
| 688 | | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO) |
| 689 | | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS; |
| 690 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]); |
| 691 | |
| 692 | /* OUT3, 4 */ |
| 693 | /* Assert RSTN before enable */ |
| 694 | reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN; |
| 695 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]); |
| 696 | /* Set divisor and reenable */ |
| 697 | reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO) |
| 698 | | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS |
| 699 | | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO) |
| 700 | | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS; |
| 701 | writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]); |
| 702 | |
| 703 | set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4); |
| 704 | } |