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