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