Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1 | /* |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 2 | * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved. |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [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 | #include <linux/slab.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/clk-provider.h> |
| 22 | #include <linux/clk.h> |
| 23 | |
| 24 | #include "clk.h" |
| 25 | |
| 26 | #define PLL_BASE_BYPASS BIT(31) |
| 27 | #define PLL_BASE_ENABLE BIT(30) |
| 28 | #define PLL_BASE_REF_ENABLE BIT(29) |
| 29 | #define PLL_BASE_OVERRIDE BIT(28) |
| 30 | |
| 31 | #define PLL_BASE_DIVP_SHIFT 20 |
| 32 | #define PLL_BASE_DIVP_WIDTH 3 |
| 33 | #define PLL_BASE_DIVN_SHIFT 8 |
| 34 | #define PLL_BASE_DIVN_WIDTH 10 |
| 35 | #define PLL_BASE_DIVM_SHIFT 0 |
| 36 | #define PLL_BASE_DIVM_WIDTH 5 |
| 37 | #define PLLU_POST_DIVP_MASK 0x1 |
| 38 | |
| 39 | #define PLL_MISC_DCCON_SHIFT 20 |
| 40 | #define PLL_MISC_CPCON_SHIFT 8 |
| 41 | #define PLL_MISC_CPCON_WIDTH 4 |
| 42 | #define PLL_MISC_CPCON_MASK ((1 << PLL_MISC_CPCON_WIDTH) - 1) |
| 43 | #define PLL_MISC_LFCON_SHIFT 4 |
| 44 | #define PLL_MISC_LFCON_WIDTH 4 |
| 45 | #define PLL_MISC_LFCON_MASK ((1 << PLL_MISC_LFCON_WIDTH) - 1) |
| 46 | #define PLL_MISC_VCOCON_SHIFT 0 |
| 47 | #define PLL_MISC_VCOCON_WIDTH 4 |
| 48 | #define PLL_MISC_VCOCON_MASK ((1 << PLL_MISC_VCOCON_WIDTH) - 1) |
| 49 | |
| 50 | #define OUT_OF_TABLE_CPCON 8 |
| 51 | |
| 52 | #define PMC_PLLP_WB0_OVERRIDE 0xf8 |
| 53 | #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE BIT(12) |
| 54 | #define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE BIT(11) |
| 55 | |
| 56 | #define PLL_POST_LOCK_DELAY 50 |
| 57 | |
| 58 | #define PLLDU_LFCON_SET_DIVN 600 |
| 59 | |
| 60 | #define PLLE_BASE_DIVCML_SHIFT 24 |
| 61 | #define PLLE_BASE_DIVCML_WIDTH 4 |
| 62 | #define PLLE_BASE_DIVP_SHIFT 16 |
| 63 | #define PLLE_BASE_DIVP_WIDTH 7 |
| 64 | #define PLLE_BASE_DIVN_SHIFT 8 |
| 65 | #define PLLE_BASE_DIVN_WIDTH 8 |
| 66 | #define PLLE_BASE_DIVM_SHIFT 0 |
| 67 | #define PLLE_BASE_DIVM_WIDTH 8 |
| 68 | |
| 69 | #define PLLE_MISC_SETUP_BASE_SHIFT 16 |
| 70 | #define PLLE_MISC_SETUP_BASE_MASK (0xffff << PLLE_MISC_SETUP_BASE_SHIFT) |
| 71 | #define PLLE_MISC_LOCK_ENABLE BIT(9) |
| 72 | #define PLLE_MISC_READY BIT(15) |
| 73 | #define PLLE_MISC_SETUP_EX_SHIFT 2 |
| 74 | #define PLLE_MISC_SETUP_EX_MASK (3 << PLLE_MISC_SETUP_EX_SHIFT) |
| 75 | #define PLLE_MISC_SETUP_MASK (PLLE_MISC_SETUP_BASE_MASK | \ |
| 76 | PLLE_MISC_SETUP_EX_MASK) |
| 77 | #define PLLE_MISC_SETUP_VALUE (7 << PLLE_MISC_SETUP_BASE_SHIFT) |
| 78 | |
| 79 | #define PLLE_SS_CTRL 0x68 |
| 80 | #define PLLE_SS_DISABLE (7 << 10) |
| 81 | |
| 82 | #define PMC_SATA_PWRGT 0x1ac |
| 83 | #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5) |
| 84 | #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4) |
| 85 | |
| 86 | #define pll_readl(offset, p) readl_relaxed(p->clk_base + offset) |
| 87 | #define pll_readl_base(p) pll_readl(p->params->base_reg, p) |
| 88 | #define pll_readl_misc(p) pll_readl(p->params->misc_reg, p) |
| 89 | |
| 90 | #define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset) |
| 91 | #define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p) |
| 92 | #define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p) |
| 93 | |
| 94 | #define mask(w) ((1 << (w)) - 1) |
| 95 | #define divm_mask(p) mask(p->divm_width) |
| 96 | #define divn_mask(p) mask(p->divn_width) |
| 97 | #define divp_mask(p) (p->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK : \ |
| 98 | mask(p->divp_width)) |
| 99 | |
| 100 | #define divm_max(p) (divm_mask(p)) |
| 101 | #define divn_max(p) (divn_mask(p)) |
| 102 | #define divp_max(p) (1 << (divp_mask(p))) |
| 103 | |
| 104 | static void clk_pll_enable_lock(struct tegra_clk_pll *pll) |
| 105 | { |
| 106 | u32 val; |
| 107 | |
| 108 | if (!(pll->flags & TEGRA_PLL_USE_LOCK)) |
| 109 | return; |
| 110 | |
| 111 | val = pll_readl_misc(pll); |
| 112 | val |= BIT(pll->params->lock_enable_bit_idx); |
| 113 | pll_writel_misc(val, pll); |
| 114 | } |
| 115 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 116 | static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 117 | { |
| 118 | int i; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 119 | u32 val, lock_bit; |
| 120 | void __iomem *lock_addr; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 121 | |
| 122 | if (!(pll->flags & TEGRA_PLL_USE_LOCK)) { |
| 123 | udelay(pll->params->lock_delay); |
| 124 | return 0; |
| 125 | } |
| 126 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 127 | lock_addr = pll->clk_base; |
| 128 | if (pll->flags & TEGRA_PLL_LOCK_MISC) |
| 129 | lock_addr += pll->params->misc_reg; |
| 130 | else |
| 131 | lock_addr += pll->params->base_reg; |
| 132 | |
| 133 | lock_bit = BIT(pll->params->lock_bit_idx); |
| 134 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 135 | for (i = 0; i < pll->params->lock_delay; i++) { |
| 136 | val = readl_relaxed(lock_addr); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 137 | if (val & lock_bit) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 138 | udelay(PLL_POST_LOCK_DELAY); |
| 139 | return 0; |
| 140 | } |
| 141 | udelay(2); /* timeout = 2 * lock time */ |
| 142 | } |
| 143 | |
| 144 | pr_err("%s: Timed out waiting for pll %s lock\n", __func__, |
| 145 | __clk_get_name(pll->hw.clk)); |
| 146 | |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | static int clk_pll_is_enabled(struct clk_hw *hw) |
| 151 | { |
| 152 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 153 | u32 val; |
| 154 | |
| 155 | if (pll->flags & TEGRA_PLLM) { |
| 156 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 157 | if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) |
| 158 | return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0; |
| 159 | } |
| 160 | |
| 161 | val = pll_readl_base(pll); |
| 162 | |
| 163 | return val & PLL_BASE_ENABLE ? 1 : 0; |
| 164 | } |
| 165 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 166 | static void _clk_pll_enable(struct clk_hw *hw) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 167 | { |
| 168 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 169 | u32 val; |
| 170 | |
| 171 | clk_pll_enable_lock(pll); |
| 172 | |
| 173 | val = pll_readl_base(pll); |
| 174 | val &= ~PLL_BASE_BYPASS; |
| 175 | val |= PLL_BASE_ENABLE; |
| 176 | pll_writel_base(val, pll); |
| 177 | |
| 178 | if (pll->flags & TEGRA_PLLM) { |
| 179 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 180 | val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE; |
| 181 | writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 182 | } |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void _clk_pll_disable(struct clk_hw *hw) |
| 186 | { |
| 187 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 188 | u32 val; |
| 189 | |
| 190 | val = pll_readl_base(pll); |
| 191 | val &= ~(PLL_BASE_BYPASS | PLL_BASE_ENABLE); |
| 192 | pll_writel_base(val, pll); |
| 193 | |
| 194 | if (pll->flags & TEGRA_PLLM) { |
| 195 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 196 | val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE; |
| 197 | writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static int clk_pll_enable(struct clk_hw *hw) |
| 202 | { |
| 203 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 204 | unsigned long flags = 0; |
| 205 | int ret; |
| 206 | |
| 207 | if (pll->lock) |
| 208 | spin_lock_irqsave(pll->lock, flags); |
| 209 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 210 | _clk_pll_enable(hw); |
| 211 | |
| 212 | ret = clk_pll_wait_for_lock(pll); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 213 | |
| 214 | if (pll->lock) |
| 215 | spin_unlock_irqrestore(pll->lock, flags); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static void clk_pll_disable(struct clk_hw *hw) |
| 221 | { |
| 222 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 223 | unsigned long flags = 0; |
| 224 | |
| 225 | if (pll->lock) |
| 226 | spin_lock_irqsave(pll->lock, flags); |
| 227 | |
| 228 | _clk_pll_disable(hw); |
| 229 | |
| 230 | if (pll->lock) |
| 231 | spin_unlock_irqrestore(pll->lock, flags); |
| 232 | } |
| 233 | |
| 234 | static int _get_table_rate(struct clk_hw *hw, |
| 235 | struct tegra_clk_pll_freq_table *cfg, |
| 236 | unsigned long rate, unsigned long parent_rate) |
| 237 | { |
| 238 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 239 | struct tegra_clk_pll_freq_table *sel; |
| 240 | |
| 241 | for (sel = pll->freq_table; sel->input_rate != 0; sel++) |
| 242 | if (sel->input_rate == parent_rate && |
| 243 | sel->output_rate == rate) |
| 244 | break; |
| 245 | |
| 246 | if (sel->input_rate == 0) |
| 247 | return -EINVAL; |
| 248 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 249 | cfg->input_rate = sel->input_rate; |
| 250 | cfg->output_rate = sel->output_rate; |
| 251 | cfg->m = sel->m; |
| 252 | cfg->n = sel->n; |
| 253 | cfg->p = sel->p; |
| 254 | cfg->cpcon = sel->cpcon; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg, |
| 260 | unsigned long rate, unsigned long parent_rate) |
| 261 | { |
| 262 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 263 | unsigned long cfreq; |
| 264 | u32 p_div = 0; |
| 265 | |
| 266 | switch (parent_rate) { |
| 267 | case 12000000: |
| 268 | case 26000000: |
| 269 | cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000; |
| 270 | break; |
| 271 | case 13000000: |
| 272 | cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000; |
| 273 | break; |
| 274 | case 16800000: |
| 275 | case 19200000: |
| 276 | cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000; |
| 277 | break; |
| 278 | case 9600000: |
| 279 | case 28800000: |
| 280 | /* |
| 281 | * PLL_P_OUT1 rate is not listed in PLLA table |
| 282 | */ |
| 283 | cfreq = parent_rate/(parent_rate/1000000); |
| 284 | break; |
| 285 | default: |
| 286 | pr_err("%s Unexpected reference rate %lu\n", |
| 287 | __func__, parent_rate); |
| 288 | BUG(); |
| 289 | } |
| 290 | |
| 291 | /* Raise VCO to guarantee 0.5% accuracy */ |
| 292 | for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq; |
| 293 | cfg->output_rate <<= 1) |
| 294 | p_div++; |
| 295 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 296 | cfg->p = p_div; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 297 | cfg->m = parent_rate / cfreq; |
| 298 | cfg->n = cfg->output_rate / cfreq; |
| 299 | cfg->cpcon = OUT_OF_TABLE_CPCON; |
| 300 | |
| 301 | if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) || |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 302 | (1 << p_div) > divp_max(pll) |
| 303 | || cfg->output_rate > pll->params->vco_max) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 304 | pr_err("%s: Failed to set %s rate %lu\n", |
| 305 | __func__, __clk_get_name(hw->clk), rate); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 309 | if (pll->flags & TEGRA_PLLU) |
| 310 | cfg->p ^= 1; |
| 311 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 315 | static void _update_pll_mnp(struct tegra_clk_pll *pll, |
| 316 | struct tegra_clk_pll_freq_table *cfg) |
| 317 | { |
| 318 | u32 val; |
| 319 | |
| 320 | val = pll_readl_base(pll); |
| 321 | |
| 322 | val &= ~((divm_mask(pll) << pll->divm_shift) | |
| 323 | (divn_mask(pll) << pll->divn_shift) | |
| 324 | (divp_mask(pll) << pll->divp_shift)); |
| 325 | val |= ((cfg->m << pll->divm_shift) | |
| 326 | (cfg->n << pll->divn_shift) | |
| 327 | (cfg->p << pll->divp_shift)); |
| 328 | |
| 329 | pll_writel_base(val, pll); |
| 330 | } |
| 331 | |
| 332 | static void _get_pll_mnp(struct tegra_clk_pll *pll, |
| 333 | struct tegra_clk_pll_freq_table *cfg) |
| 334 | { |
| 335 | u32 val; |
| 336 | |
| 337 | val = pll_readl_base(pll); |
| 338 | |
| 339 | cfg->m = (val >> pll->divm_shift) & (divm_mask(pll)); |
| 340 | cfg->n = (val >> pll->divn_shift) & (divn_mask(pll)); |
| 341 | cfg->p = (val >> pll->divp_shift) & (divp_mask(pll)); |
| 342 | } |
| 343 | |
| 344 | static void _update_pll_cpcon(struct tegra_clk_pll *pll, |
| 345 | struct tegra_clk_pll_freq_table *cfg, |
| 346 | unsigned long rate) |
| 347 | { |
| 348 | u32 val; |
| 349 | |
| 350 | val = pll_readl_misc(pll); |
| 351 | |
| 352 | val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT); |
| 353 | val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT; |
| 354 | |
| 355 | if (pll->flags & TEGRA_PLL_SET_LFCON) { |
| 356 | val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT); |
| 357 | if (cfg->n >= PLLDU_LFCON_SET_DIVN) |
| 358 | val |= 1 << PLL_MISC_LFCON_SHIFT; |
| 359 | } else if (pll->flags & TEGRA_PLL_SET_DCCON) { |
| 360 | val &= ~(1 << PLL_MISC_DCCON_SHIFT); |
| 361 | if (rate >= (pll->params->vco_max >> 1)) |
| 362 | val |= 1 << PLL_MISC_DCCON_SHIFT; |
| 363 | } |
| 364 | |
| 365 | pll_writel_misc(val, pll); |
| 366 | } |
| 367 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 368 | static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg, |
| 369 | unsigned long rate) |
| 370 | { |
| 371 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 372 | int state, ret = 0; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 373 | |
| 374 | state = clk_pll_is_enabled(hw); |
| 375 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 376 | if (state) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 377 | _clk_pll_disable(hw); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 378 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 379 | _update_pll_mnp(pll, cfg); |
| 380 | |
| 381 | if (pll->flags & TEGRA_PLL_HAS_CPCON) |
| 382 | _update_pll_cpcon(pll, cfg, rate); |
| 383 | |
| 384 | if (state) { |
| 385 | _clk_pll_enable(hw); |
| 386 | ret = clk_pll_wait_for_lock(pll); |
| 387 | } |
| 388 | |
| 389 | return ret; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate, |
| 393 | unsigned long parent_rate) |
| 394 | { |
| 395 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 396 | struct tegra_clk_pll_freq_table cfg, old_cfg; |
| 397 | unsigned long flags = 0; |
| 398 | int ret = 0; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 399 | |
| 400 | if (pll->flags & TEGRA_PLL_FIXED) { |
| 401 | if (rate != pll->fixed_rate) { |
| 402 | pr_err("%s: Can not change %s fixed rate %lu to %lu\n", |
| 403 | __func__, __clk_get_name(hw->clk), |
| 404 | pll->fixed_rate, rate); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | if (_get_table_rate(hw, &cfg, rate, parent_rate) && |
| 411 | _calc_rate(hw, &cfg, rate, parent_rate)) |
| 412 | return -EINVAL; |
| 413 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 414 | if (pll->lock) |
| 415 | spin_lock_irqsave(pll->lock, flags); |
| 416 | |
| 417 | _get_pll_mnp(pll, &old_cfg); |
| 418 | |
| 419 | if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p) |
| 420 | ret = _program_pll(hw, &cfg, rate); |
| 421 | |
| 422 | if (pll->lock) |
| 423 | spin_unlock_irqrestore(pll->lock, flags); |
| 424 | |
| 425 | return ret; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate, |
| 429 | unsigned long *prate) |
| 430 | { |
| 431 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 432 | struct tegra_clk_pll_freq_table cfg; |
| 433 | u64 output_rate = *prate; |
| 434 | |
| 435 | if (pll->flags & TEGRA_PLL_FIXED) |
| 436 | return pll->fixed_rate; |
| 437 | |
| 438 | /* PLLM is used for memory; we do not change rate */ |
| 439 | if (pll->flags & TEGRA_PLLM) |
| 440 | return __clk_get_rate(hw->clk); |
| 441 | |
| 442 | if (_get_table_rate(hw, &cfg, rate, *prate) && |
| 443 | _calc_rate(hw, &cfg, rate, *prate)) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | output_rate *= cfg.n; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 447 | do_div(output_rate, cfg.m * (1 << cfg.p)); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 448 | |
| 449 | return output_rate; |
| 450 | } |
| 451 | |
| 452 | static unsigned long clk_pll_recalc_rate(struct clk_hw *hw, |
| 453 | unsigned long parent_rate) |
| 454 | { |
| 455 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 456 | struct tegra_clk_pll_freq_table cfg; |
| 457 | u32 val; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 458 | u64 rate = parent_rate; |
| 459 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 460 | val = pll_readl_base(pll); |
| 461 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 462 | if (val & PLL_BASE_BYPASS) |
| 463 | return parent_rate; |
| 464 | |
| 465 | if ((pll->flags & TEGRA_PLL_FIXED) && !(val & PLL_BASE_OVERRIDE)) { |
| 466 | struct tegra_clk_pll_freq_table sel; |
| 467 | if (_get_table_rate(hw, &sel, pll->fixed_rate, parent_rate)) { |
| 468 | pr_err("Clock %s has unknown fixed frequency\n", |
| 469 | __clk_get_name(hw->clk)); |
| 470 | BUG(); |
| 471 | } |
| 472 | return pll->fixed_rate; |
| 473 | } |
| 474 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 475 | _get_pll_mnp(pll, &cfg); |
| 476 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 477 | if (pll->flags & TEGRA_PLLU) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 478 | cfg.p ^= 1; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 479 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 480 | cfg.m *= 1 << cfg.p; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 481 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 482 | rate *= cfg.n; |
| 483 | do_div(rate, cfg.m); |
| 484 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 485 | return rate; |
| 486 | } |
| 487 | |
| 488 | static int clk_plle_training(struct tegra_clk_pll *pll) |
| 489 | { |
| 490 | u32 val; |
| 491 | unsigned long timeout; |
| 492 | |
| 493 | if (!pll->pmc) |
| 494 | return -ENOSYS; |
| 495 | |
| 496 | /* |
| 497 | * PLLE is already disabled, and setup cleared; |
| 498 | * create falling edge on PLLE IDDQ input. |
| 499 | */ |
| 500 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 501 | val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE; |
| 502 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 503 | |
| 504 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 505 | val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL; |
| 506 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 507 | |
| 508 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 509 | val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE; |
| 510 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 511 | |
| 512 | val = pll_readl_misc(pll); |
| 513 | |
| 514 | timeout = jiffies + msecs_to_jiffies(100); |
| 515 | while (1) { |
| 516 | val = pll_readl_misc(pll); |
| 517 | if (val & PLLE_MISC_READY) |
| 518 | break; |
| 519 | if (time_after(jiffies, timeout)) { |
| 520 | pr_err("%s: timeout waiting for PLLE\n", __func__); |
| 521 | return -EBUSY; |
| 522 | } |
| 523 | udelay(300); |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int clk_plle_enable(struct clk_hw *hw) |
| 530 | { |
| 531 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 532 | unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk)); |
| 533 | struct tegra_clk_pll_freq_table sel; |
| 534 | u32 val; |
| 535 | int err; |
| 536 | |
| 537 | if (_get_table_rate(hw, &sel, pll->fixed_rate, input_rate)) |
| 538 | return -EINVAL; |
| 539 | |
| 540 | clk_pll_disable(hw); |
| 541 | |
| 542 | val = pll_readl_misc(pll); |
| 543 | val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK); |
| 544 | pll_writel_misc(val, pll); |
| 545 | |
| 546 | val = pll_readl_misc(pll); |
| 547 | if (!(val & PLLE_MISC_READY)) { |
| 548 | err = clk_plle_training(pll); |
| 549 | if (err) |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | if (pll->flags & TEGRA_PLLE_CONFIGURE) { |
| 554 | /* configure dividers */ |
| 555 | val = pll_readl_base(pll); |
| 556 | val &= ~(divm_mask(pll) | divn_mask(pll) | divp_mask(pll)); |
| 557 | val &= ~(PLLE_BASE_DIVCML_WIDTH << PLLE_BASE_DIVCML_SHIFT); |
| 558 | val |= sel.m << pll->divm_shift; |
| 559 | val |= sel.n << pll->divn_shift; |
| 560 | val |= sel.p << pll->divp_shift; |
| 561 | val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT; |
| 562 | pll_writel_base(val, pll); |
| 563 | } |
| 564 | |
| 565 | val = pll_readl_misc(pll); |
| 566 | val |= PLLE_MISC_SETUP_VALUE; |
| 567 | val |= PLLE_MISC_LOCK_ENABLE; |
| 568 | pll_writel_misc(val, pll); |
| 569 | |
| 570 | val = readl(pll->clk_base + PLLE_SS_CTRL); |
| 571 | val |= PLLE_SS_DISABLE; |
| 572 | writel(val, pll->clk_base + PLLE_SS_CTRL); |
| 573 | |
| 574 | val |= pll_readl_base(pll); |
| 575 | val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE); |
| 576 | pll_writel_base(val, pll); |
| 577 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 578 | clk_pll_wait_for_lock(pll); |
| 579 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static unsigned long clk_plle_recalc_rate(struct clk_hw *hw, |
| 584 | unsigned long parent_rate) |
| 585 | { |
| 586 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 587 | u32 val = pll_readl_base(pll); |
| 588 | u32 divn = 0, divm = 0, divp = 0; |
| 589 | u64 rate = parent_rate; |
| 590 | |
| 591 | divp = (val >> pll->divp_shift) & (divp_mask(pll)); |
| 592 | divn = (val >> pll->divn_shift) & (divn_mask(pll)); |
| 593 | divm = (val >> pll->divm_shift) & (divm_mask(pll)); |
| 594 | divm *= divp; |
| 595 | |
| 596 | rate *= divn; |
| 597 | do_div(rate, divm); |
| 598 | return rate; |
| 599 | } |
| 600 | |
| 601 | const struct clk_ops tegra_clk_pll_ops = { |
| 602 | .is_enabled = clk_pll_is_enabled, |
| 603 | .enable = clk_pll_enable, |
| 604 | .disable = clk_pll_disable, |
| 605 | .recalc_rate = clk_pll_recalc_rate, |
| 606 | .round_rate = clk_pll_round_rate, |
| 607 | .set_rate = clk_pll_set_rate, |
| 608 | }; |
| 609 | |
| 610 | const struct clk_ops tegra_clk_plle_ops = { |
| 611 | .recalc_rate = clk_plle_recalc_rate, |
| 612 | .is_enabled = clk_pll_is_enabled, |
| 613 | .disable = clk_pll_disable, |
| 614 | .enable = clk_plle_enable, |
| 615 | }; |
| 616 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 617 | static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base, |
| 618 | void __iomem *pmc, unsigned long fixed_rate, |
| 619 | struct tegra_clk_pll_params *pll_params, u32 pll_flags, |
| 620 | struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 621 | { |
| 622 | struct tegra_clk_pll *pll; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 623 | |
| 624 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 625 | if (!pll) |
| 626 | return ERR_PTR(-ENOMEM); |
| 627 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 628 | pll->clk_base = clk_base; |
| 629 | pll->pmc = pmc; |
| 630 | |
| 631 | pll->freq_table = freq_table; |
| 632 | pll->params = pll_params; |
| 633 | pll->fixed_rate = fixed_rate; |
| 634 | pll->flags = pll_flags; |
| 635 | pll->lock = lock; |
| 636 | |
| 637 | pll->divp_shift = PLL_BASE_DIVP_SHIFT; |
| 638 | pll->divp_width = PLL_BASE_DIVP_WIDTH; |
| 639 | pll->divn_shift = PLL_BASE_DIVN_SHIFT; |
| 640 | pll->divn_width = PLL_BASE_DIVN_WIDTH; |
| 641 | pll->divm_shift = PLL_BASE_DIVM_SHIFT; |
| 642 | pll->divm_width = PLL_BASE_DIVM_WIDTH; |
| 643 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 644 | return pll; |
| 645 | } |
| 646 | |
| 647 | static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll, |
| 648 | const char *name, const char *parent_name, unsigned long flags, |
| 649 | const struct clk_ops *ops) |
| 650 | { |
| 651 | struct clk_init_data init; |
| 652 | |
| 653 | init.name = name; |
| 654 | init.ops = ops; |
| 655 | init.flags = flags; |
| 656 | init.parent_names = (parent_name ? &parent_name : NULL); |
| 657 | init.num_parents = (parent_name ? 1 : 0); |
| 658 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 659 | /* Data in .init is copied by clk_register(), so stack variable OK */ |
| 660 | pll->hw.init = &init; |
| 661 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 662 | return clk_register(NULL, &pll->hw); |
| 663 | } |
| 664 | |
| 665 | struct clk *tegra_clk_register_pll(const char *name, const char *parent_name, |
| 666 | void __iomem *clk_base, void __iomem *pmc, |
| 667 | unsigned long flags, unsigned long fixed_rate, |
| 668 | struct tegra_clk_pll_params *pll_params, u32 pll_flags, |
| 669 | struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock) |
| 670 | { |
| 671 | struct tegra_clk_pll *pll; |
| 672 | struct clk *clk; |
| 673 | |
| 674 | pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags, |
| 675 | freq_table, lock); |
| 676 | if (IS_ERR(pll)) |
| 677 | return ERR_CAST(pll); |
| 678 | |
| 679 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 680 | &tegra_clk_pll_ops); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 681 | if (IS_ERR(clk)) |
| 682 | kfree(pll); |
| 683 | |
| 684 | return clk; |
| 685 | } |
| 686 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 687 | struct clk *tegra_clk_register_plle(const char *name, const char *parent_name, |
| 688 | void __iomem *clk_base, void __iomem *pmc, |
| 689 | unsigned long flags, unsigned long fixed_rate, |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 690 | struct tegra_clk_pll_params *pll_params, u32 pll_flags, |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 691 | struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock) |
| 692 | { |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame^] | 693 | struct tegra_clk_pll *pll; |
| 694 | struct clk *clk; |
| 695 | pll_flags |= TEGRA_PLL_LOCK_MISC; |
| 696 | |
| 697 | pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags, |
| 698 | freq_table, lock); |
| 699 | if (IS_ERR(pll)) |
| 700 | return ERR_CAST(pll); |
| 701 | |
| 702 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 703 | &tegra_clk_plle_ops); |
| 704 | if (IS_ERR(clk)) |
| 705 | kfree(pll); |
| 706 | |
| 707 | return clk; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 708 | } |