ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / clk / tegra / clk-pll.c
blobc7c6d8fb32fbb14bfc0727024bdd91ccab6009b8
1 /*
2 * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved.
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.
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.
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/>.
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>
24 #include "clk.h"
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)
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
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)
50 #define OUT_OF_TABLE_CPCON 8
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)
56 #define PLL_POST_LOCK_DELAY 50
58 #define PLLDU_LFCON_SET_DIVN 600
60 #define PLLE_BASE_DIVCML_SHIFT 24
61 #define PLLE_BASE_DIVCML_MASK 0xf
62 #define PLLE_BASE_DIVP_SHIFT 16
63 #define PLLE_BASE_DIVP_WIDTH 6
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
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)
79 #define PLLE_SS_CTRL 0x68
80 #define PLLE_SS_CNTL_BYPASS_SS BIT(10)
81 #define PLLE_SS_CNTL_INTERP_RESET BIT(11)
82 #define PLLE_SS_CNTL_SSC_BYP BIT(12)
83 #define PLLE_SS_CNTL_CENTER BIT(14)
84 #define PLLE_SS_CNTL_INVERT BIT(15)
85 #define PLLE_SS_DISABLE (PLLE_SS_CNTL_BYPASS_SS | PLLE_SS_CNTL_INTERP_RESET |\
86 PLLE_SS_CNTL_SSC_BYP)
87 #define PLLE_SS_MAX_MASK 0x1ff
88 #define PLLE_SS_MAX_VAL 0x25
89 #define PLLE_SS_INC_MASK (0xff << 16)
90 #define PLLE_SS_INC_VAL (0x1 << 16)
91 #define PLLE_SS_INCINTRV_MASK (0x3f << 24)
92 #define PLLE_SS_INCINTRV_VAL (0x20 << 24)
93 #define PLLE_SS_COEFFICIENTS_MASK \
94 (PLLE_SS_MAX_MASK | PLLE_SS_INC_MASK | PLLE_SS_INCINTRV_MASK)
95 #define PLLE_SS_COEFFICIENTS_VAL \
96 (PLLE_SS_MAX_VAL | PLLE_SS_INC_VAL | PLLE_SS_INCINTRV_VAL)
98 #define PLLE_AUX_PLLP_SEL BIT(2)
99 #define PLLE_AUX_USE_LOCKDET BIT(3)
100 #define PLLE_AUX_ENABLE_SWCTL BIT(4)
101 #define PLLE_AUX_SS_SWCTL BIT(6)
102 #define PLLE_AUX_SEQ_ENABLE BIT(24)
103 #define PLLE_AUX_SEQ_START_STATE BIT(25)
104 #define PLLE_AUX_PLLRE_SEL BIT(28)
106 #define XUSBIO_PLL_CFG0 0x51c
107 #define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
108 #define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL BIT(2)
109 #define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET BIT(6)
110 #define XUSBIO_PLL_CFG0_SEQ_ENABLE BIT(24)
111 #define XUSBIO_PLL_CFG0_SEQ_START_STATE BIT(25)
113 #define SATA_PLL_CFG0 0x490
114 #define SATA_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
115 #define SATA_PLL_CFG0_PADPLL_USE_LOCKDET BIT(2)
116 #define SATA_PLL_CFG0_SEQ_ENABLE BIT(24)
117 #define SATA_PLL_CFG0_SEQ_START_STATE BIT(25)
119 #define PLLE_MISC_PLLE_PTS BIT(8)
120 #define PLLE_MISC_IDDQ_SW_VALUE BIT(13)
121 #define PLLE_MISC_IDDQ_SW_CTRL BIT(14)
122 #define PLLE_MISC_VREG_BG_CTRL_SHIFT 4
123 #define PLLE_MISC_VREG_BG_CTRL_MASK (3 << PLLE_MISC_VREG_BG_CTRL_SHIFT)
124 #define PLLE_MISC_VREG_CTRL_SHIFT 2
125 #define PLLE_MISC_VREG_CTRL_MASK (2 << PLLE_MISC_VREG_CTRL_SHIFT)
127 #define PLLCX_MISC_STROBE BIT(31)
128 #define PLLCX_MISC_RESET BIT(30)
129 #define PLLCX_MISC_SDM_DIV_SHIFT 28
130 #define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
131 #define PLLCX_MISC_FILT_DIV_SHIFT 26
132 #define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
133 #define PLLCX_MISC_ALPHA_SHIFT 18
134 #define PLLCX_MISC_DIV_LOW_RANGE \
135 ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \
136 (0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
137 #define PLLCX_MISC_DIV_HIGH_RANGE \
138 ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \
139 (0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
140 #define PLLCX_MISC_COEF_LOW_RANGE \
141 ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT))
142 #define PLLCX_MISC_KA_SHIFT 2
143 #define PLLCX_MISC_KB_SHIFT 9
144 #define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \
145 (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
146 PLLCX_MISC_DIV_LOW_RANGE | \
147 PLLCX_MISC_RESET)
148 #define PLLCX_MISC1_DEFAULT 0x000d2308
149 #define PLLCX_MISC2_DEFAULT 0x30211200
150 #define PLLCX_MISC3_DEFAULT 0x200
152 #define PMC_SATA_PWRGT 0x1ac
153 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5)
154 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4)
156 #define PLLSS_MISC_KCP 0
157 #define PLLSS_MISC_KVCO 0
158 #define PLLSS_MISC_SETUP 0
159 #define PLLSS_EN_SDM 0
160 #define PLLSS_EN_SSC 0
161 #define PLLSS_EN_DITHER2 0
162 #define PLLSS_EN_DITHER 1
163 #define PLLSS_SDM_RESET 0
164 #define PLLSS_CLAMP 0
165 #define PLLSS_SDM_SSC_MAX 0
166 #define PLLSS_SDM_SSC_MIN 0
167 #define PLLSS_SDM_SSC_STEP 0
168 #define PLLSS_SDM_DIN 0
169 #define PLLSS_MISC_DEFAULT ((PLLSS_MISC_KCP << 25) | \
170 (PLLSS_MISC_KVCO << 24) | \
171 PLLSS_MISC_SETUP)
172 #define PLLSS_CFG_DEFAULT ((PLLSS_EN_SDM << 31) | \
173 (PLLSS_EN_SSC << 30) | \
174 (PLLSS_EN_DITHER2 << 29) | \
175 (PLLSS_EN_DITHER << 28) | \
176 (PLLSS_SDM_RESET) << 27 | \
177 (PLLSS_CLAMP << 22))
178 #define PLLSS_CTRL1_DEFAULT \
179 ((PLLSS_SDM_SSC_MAX << 16) | PLLSS_SDM_SSC_MIN)
180 #define PLLSS_CTRL2_DEFAULT \
181 ((PLLSS_SDM_SSC_STEP << 16) | PLLSS_SDM_DIN)
182 #define PLLSS_LOCK_OVERRIDE BIT(24)
183 #define PLLSS_REF_SRC_SEL_SHIFT 25
184 #define PLLSS_REF_SRC_SEL_MASK (3 << PLLSS_REF_SRC_SEL_SHIFT)
186 #define pll_readl(offset, p) readl_relaxed(p->clk_base + offset)
187 #define pll_readl_base(p) pll_readl(p->params->base_reg, p)
188 #define pll_readl_misc(p) pll_readl(p->params->misc_reg, p)
189 #define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
191 #define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
192 #define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
193 #define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
194 #define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
196 #define mask(w) ((1 << (w)) - 1)
197 #define divm_mask(p) mask(p->params->div_nmp->divm_width)
198 #define divn_mask(p) mask(p->params->div_nmp->divn_width)
199 #define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
200 mask(p->params->div_nmp->divp_width))
202 #define divm_shift(p) (p)->params->div_nmp->divm_shift
203 #define divn_shift(p) (p)->params->div_nmp->divn_shift
204 #define divp_shift(p) (p)->params->div_nmp->divp_shift
206 #define divm_mask_shifted(p) (divm_mask(p) << divm_shift(p))
207 #define divn_mask_shifted(p) (divn_mask(p) << divn_shift(p))
208 #define divp_mask_shifted(p) (divp_mask(p) << divp_shift(p))
210 #define divm_max(p) (divm_mask(p))
211 #define divn_max(p) (divn_mask(p))
212 #define divp_max(p) (1 << (divp_mask(p)))
214 static struct div_nmp default_nmp = {
215 .divn_shift = PLL_BASE_DIVN_SHIFT,
216 .divn_width = PLL_BASE_DIVN_WIDTH,
217 .divm_shift = PLL_BASE_DIVM_SHIFT,
218 .divm_width = PLL_BASE_DIVM_WIDTH,
219 .divp_shift = PLL_BASE_DIVP_SHIFT,
220 .divp_width = PLL_BASE_DIVP_WIDTH,
223 static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
225 u32 val;
227 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
228 return;
230 if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
231 return;
233 val = pll_readl_misc(pll);
234 val |= BIT(pll->params->lock_enable_bit_idx);
235 pll_writel_misc(val, pll);
238 static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
240 int i;
241 u32 val, lock_mask;
242 void __iomem *lock_addr;
244 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
245 udelay(pll->params->lock_delay);
246 return 0;
249 lock_addr = pll->clk_base;
250 if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
251 lock_addr += pll->params->misc_reg;
252 else
253 lock_addr += pll->params->base_reg;
255 lock_mask = pll->params->lock_mask;
257 for (i = 0; i < pll->params->lock_delay; i++) {
258 val = readl_relaxed(lock_addr);
259 if ((val & lock_mask) == lock_mask) {
260 udelay(PLL_POST_LOCK_DELAY);
261 return 0;
263 udelay(2); /* timeout = 2 * lock time */
266 pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
267 __clk_get_name(pll->hw.clk));
269 return -1;
272 static int clk_pll_is_enabled(struct clk_hw *hw)
274 struct tegra_clk_pll *pll = to_clk_pll(hw);
275 u32 val;
277 if (pll->params->flags & TEGRA_PLLM) {
278 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
279 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
280 return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
283 val = pll_readl_base(pll);
285 return val & PLL_BASE_ENABLE ? 1 : 0;
288 static void _clk_pll_enable(struct clk_hw *hw)
290 struct tegra_clk_pll *pll = to_clk_pll(hw);
291 u32 val;
293 clk_pll_enable_lock(pll);
295 val = pll_readl_base(pll);
296 if (pll->params->flags & TEGRA_PLL_BYPASS)
297 val &= ~PLL_BASE_BYPASS;
298 val |= PLL_BASE_ENABLE;
299 pll_writel_base(val, pll);
301 if (pll->params->flags & TEGRA_PLLM) {
302 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
303 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
304 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
308 static void _clk_pll_disable(struct clk_hw *hw)
310 struct tegra_clk_pll *pll = to_clk_pll(hw);
311 u32 val;
313 val = pll_readl_base(pll);
314 if (pll->params->flags & TEGRA_PLL_BYPASS)
315 val &= ~PLL_BASE_BYPASS;
316 val &= ~PLL_BASE_ENABLE;
317 pll_writel_base(val, pll);
319 if (pll->params->flags & TEGRA_PLLM) {
320 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
321 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
322 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
326 static int clk_pll_enable(struct clk_hw *hw)
328 struct tegra_clk_pll *pll = to_clk_pll(hw);
329 unsigned long flags = 0;
330 int ret;
332 if (pll->lock)
333 spin_lock_irqsave(pll->lock, flags);
335 _clk_pll_enable(hw);
337 ret = clk_pll_wait_for_lock(pll);
339 if (pll->lock)
340 spin_unlock_irqrestore(pll->lock, flags);
342 return ret;
345 static void clk_pll_disable(struct clk_hw *hw)
347 struct tegra_clk_pll *pll = to_clk_pll(hw);
348 unsigned long flags = 0;
350 if (pll->lock)
351 spin_lock_irqsave(pll->lock, flags);
353 _clk_pll_disable(hw);
355 if (pll->lock)
356 spin_unlock_irqrestore(pll->lock, flags);
359 static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
361 struct tegra_clk_pll *pll = to_clk_pll(hw);
362 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
364 if (p_tohw) {
365 while (p_tohw->pdiv) {
366 if (p_div <= p_tohw->pdiv)
367 return p_tohw->hw_val;
368 p_tohw++;
370 return -EINVAL;
372 return -EINVAL;
375 static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
377 struct tegra_clk_pll *pll = to_clk_pll(hw);
378 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
380 if (p_tohw) {
381 while (p_tohw->pdiv) {
382 if (p_div_hw == p_tohw->hw_val)
383 return p_tohw->pdiv;
384 p_tohw++;
386 return -EINVAL;
389 return 1 << p_div_hw;
392 static int _get_table_rate(struct clk_hw *hw,
393 struct tegra_clk_pll_freq_table *cfg,
394 unsigned long rate, unsigned long parent_rate)
396 struct tegra_clk_pll *pll = to_clk_pll(hw);
397 struct tegra_clk_pll_freq_table *sel;
399 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
400 if (sel->input_rate == parent_rate &&
401 sel->output_rate == rate)
402 break;
404 if (sel->input_rate == 0)
405 return -EINVAL;
407 cfg->input_rate = sel->input_rate;
408 cfg->output_rate = sel->output_rate;
409 cfg->m = sel->m;
410 cfg->n = sel->n;
411 cfg->p = sel->p;
412 cfg->cpcon = sel->cpcon;
414 return 0;
417 static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
418 unsigned long rate, unsigned long parent_rate)
420 struct tegra_clk_pll *pll = to_clk_pll(hw);
421 unsigned long cfreq;
422 u32 p_div = 0;
423 int ret;
425 switch (parent_rate) {
426 case 12000000:
427 case 26000000:
428 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
429 break;
430 case 13000000:
431 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
432 break;
433 case 16800000:
434 case 19200000:
435 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
436 break;
437 case 9600000:
438 case 28800000:
440 * PLL_P_OUT1 rate is not listed in PLLA table
442 cfreq = parent_rate/(parent_rate/1000000);
443 break;
444 default:
445 pr_err("%s Unexpected reference rate %lu\n",
446 __func__, parent_rate);
447 BUG();
450 /* Raise VCO to guarantee 0.5% accuracy */
451 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
452 cfg->output_rate <<= 1)
453 p_div++;
455 cfg->m = parent_rate / cfreq;
456 cfg->n = cfg->output_rate / cfreq;
457 cfg->cpcon = OUT_OF_TABLE_CPCON;
459 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
460 (1 << p_div) > divp_max(pll)
461 || cfg->output_rate > pll->params->vco_max) {
462 return -EINVAL;
465 cfg->output_rate >>= p_div;
467 if (pll->params->pdiv_tohw) {
468 ret = _p_div_to_hw(hw, 1 << p_div);
469 if (ret < 0)
470 return ret;
471 else
472 cfg->p = ret;
473 } else
474 cfg->p = p_div;
476 return 0;
479 static void _update_pll_mnp(struct tegra_clk_pll *pll,
480 struct tegra_clk_pll_freq_table *cfg)
482 u32 val;
483 struct tegra_clk_pll_params *params = pll->params;
484 struct div_nmp *div_nmp = params->div_nmp;
486 if ((params->flags & TEGRA_PLLM) &&
487 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
488 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
489 val = pll_override_readl(params->pmc_divp_reg, pll);
490 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
491 val |= cfg->p << div_nmp->override_divp_shift;
492 pll_override_writel(val, params->pmc_divp_reg, pll);
494 val = pll_override_readl(params->pmc_divnm_reg, pll);
495 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
496 ~(divn_mask(pll) << div_nmp->override_divn_shift);
497 val |= (cfg->m << div_nmp->override_divm_shift) |
498 (cfg->n << div_nmp->override_divn_shift);
499 pll_override_writel(val, params->pmc_divnm_reg, pll);
500 } else {
501 val = pll_readl_base(pll);
503 val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
504 divp_mask_shifted(pll));
506 val |= (cfg->m << divm_shift(pll)) |
507 (cfg->n << divn_shift(pll)) |
508 (cfg->p << divp_shift(pll));
510 pll_writel_base(val, pll);
514 static void _get_pll_mnp(struct tegra_clk_pll *pll,
515 struct tegra_clk_pll_freq_table *cfg)
517 u32 val;
518 struct tegra_clk_pll_params *params = pll->params;
519 struct div_nmp *div_nmp = params->div_nmp;
521 if ((params->flags & TEGRA_PLLM) &&
522 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
523 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
524 val = pll_override_readl(params->pmc_divp_reg, pll);
525 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
527 val = pll_override_readl(params->pmc_divnm_reg, pll);
528 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
529 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
530 } else {
531 val = pll_readl_base(pll);
533 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
534 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
535 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
539 static void _update_pll_cpcon(struct tegra_clk_pll *pll,
540 struct tegra_clk_pll_freq_table *cfg,
541 unsigned long rate)
543 u32 val;
545 val = pll_readl_misc(pll);
547 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
548 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
550 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
551 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
552 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
553 val |= 1 << PLL_MISC_LFCON_SHIFT;
554 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
555 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
556 if (rate >= (pll->params->vco_max >> 1))
557 val |= 1 << PLL_MISC_DCCON_SHIFT;
560 pll_writel_misc(val, pll);
563 static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
564 unsigned long rate)
566 struct tegra_clk_pll *pll = to_clk_pll(hw);
567 int state, ret = 0;
569 state = clk_pll_is_enabled(hw);
571 if (state)
572 _clk_pll_disable(hw);
574 _update_pll_mnp(pll, cfg);
576 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
577 _update_pll_cpcon(pll, cfg, rate);
579 if (state) {
580 _clk_pll_enable(hw);
581 ret = clk_pll_wait_for_lock(pll);
584 return ret;
587 static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
588 unsigned long parent_rate)
590 struct tegra_clk_pll *pll = to_clk_pll(hw);
591 struct tegra_clk_pll_freq_table cfg, old_cfg;
592 unsigned long flags = 0;
593 int ret = 0;
595 if (pll->params->flags & TEGRA_PLL_FIXED) {
596 if (rate != pll->params->fixed_rate) {
597 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
598 __func__, __clk_get_name(hw->clk),
599 pll->params->fixed_rate, rate);
600 return -EINVAL;
602 return 0;
605 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
606 _calc_rate(hw, &cfg, rate, parent_rate)) {
607 pr_err("%s: Failed to set %s rate %lu\n", __func__,
608 __clk_get_name(hw->clk), rate);
609 WARN_ON(1);
610 return -EINVAL;
612 if (pll->lock)
613 spin_lock_irqsave(pll->lock, flags);
615 _get_pll_mnp(pll, &old_cfg);
617 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
618 ret = _program_pll(hw, &cfg, rate);
620 if (pll->lock)
621 spin_unlock_irqrestore(pll->lock, flags);
623 return ret;
626 static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
627 unsigned long *prate)
629 struct tegra_clk_pll *pll = to_clk_pll(hw);
630 struct tegra_clk_pll_freq_table cfg;
632 if (pll->params->flags & TEGRA_PLL_FIXED)
633 return pll->params->fixed_rate;
635 /* PLLM is used for memory; we do not change rate */
636 if (pll->params->flags & TEGRA_PLLM)
637 return __clk_get_rate(hw->clk);
639 if (_get_table_rate(hw, &cfg, rate, *prate) &&
640 _calc_rate(hw, &cfg, rate, *prate))
641 return -EINVAL;
643 return cfg.output_rate;
646 static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
647 unsigned long parent_rate)
649 struct tegra_clk_pll *pll = to_clk_pll(hw);
650 struct tegra_clk_pll_freq_table cfg;
651 u32 val;
652 u64 rate = parent_rate;
653 int pdiv;
655 val = pll_readl_base(pll);
657 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
658 return parent_rate;
660 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
661 !(val & PLL_BASE_OVERRIDE)) {
662 struct tegra_clk_pll_freq_table sel;
663 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
664 parent_rate)) {
665 pr_err("Clock %s has unknown fixed frequency\n",
666 __clk_get_name(hw->clk));
667 BUG();
669 return pll->params->fixed_rate;
672 _get_pll_mnp(pll, &cfg);
674 pdiv = _hw_to_p_div(hw, cfg.p);
675 if (pdiv < 0) {
676 WARN_ON(1);
677 pdiv = 1;
680 cfg.m *= pdiv;
682 rate *= cfg.n;
683 do_div(rate, cfg.m);
685 return rate;
688 static int clk_plle_training(struct tegra_clk_pll *pll)
690 u32 val;
691 unsigned long timeout;
693 if (!pll->pmc)
694 return -ENOSYS;
697 * PLLE is already disabled, and setup cleared;
698 * create falling edge on PLLE IDDQ input.
700 val = readl(pll->pmc + PMC_SATA_PWRGT);
701 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
702 writel(val, pll->pmc + PMC_SATA_PWRGT);
704 val = readl(pll->pmc + PMC_SATA_PWRGT);
705 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
706 writel(val, pll->pmc + PMC_SATA_PWRGT);
708 val = readl(pll->pmc + PMC_SATA_PWRGT);
709 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
710 writel(val, pll->pmc + PMC_SATA_PWRGT);
712 val = pll_readl_misc(pll);
714 timeout = jiffies + msecs_to_jiffies(100);
715 while (1) {
716 val = pll_readl_misc(pll);
717 if (val & PLLE_MISC_READY)
718 break;
719 if (time_after(jiffies, timeout)) {
720 pr_err("%s: timeout waiting for PLLE\n", __func__);
721 return -EBUSY;
723 udelay(300);
726 return 0;
729 static int clk_plle_enable(struct clk_hw *hw)
731 struct tegra_clk_pll *pll = to_clk_pll(hw);
732 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
733 struct tegra_clk_pll_freq_table sel;
734 u32 val;
735 int err;
737 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
738 return -EINVAL;
740 clk_pll_disable(hw);
742 val = pll_readl_misc(pll);
743 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
744 pll_writel_misc(val, pll);
746 val = pll_readl_misc(pll);
747 if (!(val & PLLE_MISC_READY)) {
748 err = clk_plle_training(pll);
749 if (err)
750 return err;
753 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
754 /* configure dividers */
755 val = pll_readl_base(pll);
756 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
757 divm_mask_shifted(pll));
758 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
759 val |= sel.m << divm_shift(pll);
760 val |= sel.n << divn_shift(pll);
761 val |= sel.p << divp_shift(pll);
762 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
763 pll_writel_base(val, pll);
766 val = pll_readl_misc(pll);
767 val |= PLLE_MISC_SETUP_VALUE;
768 val |= PLLE_MISC_LOCK_ENABLE;
769 pll_writel_misc(val, pll);
771 val = readl(pll->clk_base + PLLE_SS_CTRL);
772 val &= ~PLLE_SS_COEFFICIENTS_MASK;
773 val |= PLLE_SS_DISABLE;
774 writel(val, pll->clk_base + PLLE_SS_CTRL);
776 val = pll_readl_base(pll);
777 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
778 pll_writel_base(val, pll);
780 clk_pll_wait_for_lock(pll);
782 return 0;
785 static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
786 unsigned long parent_rate)
788 struct tegra_clk_pll *pll = to_clk_pll(hw);
789 u32 val = pll_readl_base(pll);
790 u32 divn = 0, divm = 0, divp = 0;
791 u64 rate = parent_rate;
793 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
794 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
795 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
796 divm *= divp;
798 rate *= divn;
799 do_div(rate, divm);
800 return rate;
803 const struct clk_ops tegra_clk_pll_ops = {
804 .is_enabled = clk_pll_is_enabled,
805 .enable = clk_pll_enable,
806 .disable = clk_pll_disable,
807 .recalc_rate = clk_pll_recalc_rate,
808 .round_rate = clk_pll_round_rate,
809 .set_rate = clk_pll_set_rate,
812 const struct clk_ops tegra_clk_plle_ops = {
813 .recalc_rate = clk_plle_recalc_rate,
814 .is_enabled = clk_pll_is_enabled,
815 .disable = clk_pll_disable,
816 .enable = clk_plle_enable,
819 #if defined(CONFIG_ARCH_TEGRA_114_SOC) || defined(CONFIG_ARCH_TEGRA_124_SOC)
821 static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
822 unsigned long parent_rate)
824 if (parent_rate > pll_params->cf_max)
825 return 2;
826 else
827 return 1;
830 static unsigned long _clip_vco_min(unsigned long vco_min,
831 unsigned long parent_rate)
833 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
836 static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
837 void __iomem *clk_base,
838 unsigned long parent_rate)
840 u32 val;
841 u32 step_a, step_b;
843 switch (parent_rate) {
844 case 12000000:
845 case 13000000:
846 case 26000000:
847 step_a = 0x2B;
848 step_b = 0x0B;
849 break;
850 case 16800000:
851 step_a = 0x1A;
852 step_b = 0x09;
853 break;
854 case 19200000:
855 step_a = 0x12;
856 step_b = 0x08;
857 break;
858 default:
859 pr_err("%s: Unexpected reference rate %lu\n",
860 __func__, parent_rate);
861 WARN_ON(1);
862 return -EINVAL;
865 val = step_a << pll_params->stepa_shift;
866 val |= step_b << pll_params->stepb_shift;
867 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
869 return 0;
872 static int clk_pll_iddq_enable(struct clk_hw *hw)
874 struct tegra_clk_pll *pll = to_clk_pll(hw);
875 unsigned long flags = 0;
877 u32 val;
878 int ret;
880 if (pll->lock)
881 spin_lock_irqsave(pll->lock, flags);
883 val = pll_readl(pll->params->iddq_reg, pll);
884 val &= ~BIT(pll->params->iddq_bit_idx);
885 pll_writel(val, pll->params->iddq_reg, pll);
886 udelay(2);
888 _clk_pll_enable(hw);
890 ret = clk_pll_wait_for_lock(pll);
892 if (pll->lock)
893 spin_unlock_irqrestore(pll->lock, flags);
895 return 0;
898 static void clk_pll_iddq_disable(struct clk_hw *hw)
900 struct tegra_clk_pll *pll = to_clk_pll(hw);
901 unsigned long flags = 0;
902 u32 val;
904 if (pll->lock)
905 spin_lock_irqsave(pll->lock, flags);
907 _clk_pll_disable(hw);
909 val = pll_readl(pll->params->iddq_reg, pll);
910 val |= BIT(pll->params->iddq_bit_idx);
911 pll_writel(val, pll->params->iddq_reg, pll);
912 udelay(2);
914 if (pll->lock)
915 spin_unlock_irqrestore(pll->lock, flags);
918 static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
919 struct tegra_clk_pll_freq_table *cfg,
920 unsigned long rate, unsigned long parent_rate)
922 struct tegra_clk_pll *pll = to_clk_pll(hw);
923 unsigned int p;
924 int p_div;
926 if (!rate)
927 return -EINVAL;
929 p = DIV_ROUND_UP(pll->params->vco_min, rate);
930 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
931 cfg->output_rate = rate * p;
932 cfg->n = cfg->output_rate * cfg->m / parent_rate;
934 p_div = _p_div_to_hw(hw, p);
935 if (p_div < 0)
936 return p_div;
937 else
938 cfg->p = p_div;
940 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
941 return -EINVAL;
943 return 0;
946 static int _pll_ramp_calc_pll(struct clk_hw *hw,
947 struct tegra_clk_pll_freq_table *cfg,
948 unsigned long rate, unsigned long parent_rate)
950 struct tegra_clk_pll *pll = to_clk_pll(hw);
951 int err = 0, p_div;
953 err = _get_table_rate(hw, cfg, rate, parent_rate);
954 if (err < 0)
955 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
956 else {
957 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
958 WARN_ON(1);
959 err = -EINVAL;
960 goto out;
962 p_div = _p_div_to_hw(hw, cfg->p);
963 if (p_div < 0)
964 return p_div;
965 else
966 cfg->p = p_div;
969 if (cfg->p > pll->params->max_p)
970 err = -EINVAL;
972 out:
973 return err;
976 static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
977 unsigned long parent_rate)
979 struct tegra_clk_pll *pll = to_clk_pll(hw);
980 struct tegra_clk_pll_freq_table cfg, old_cfg;
981 unsigned long flags = 0;
982 int ret = 0;
984 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
985 if (ret < 0)
986 return ret;
988 if (pll->lock)
989 spin_lock_irqsave(pll->lock, flags);
991 _get_pll_mnp(pll, &old_cfg);
993 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
994 ret = _program_pll(hw, &cfg, rate);
996 if (pll->lock)
997 spin_unlock_irqrestore(pll->lock, flags);
999 return ret;
1002 static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1003 unsigned long *prate)
1005 struct tegra_clk_pll_freq_table cfg;
1006 int ret = 0, p_div;
1007 u64 output_rate = *prate;
1009 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1010 if (ret < 0)
1011 return ret;
1013 p_div = _hw_to_p_div(hw, cfg.p);
1014 if (p_div < 0)
1015 return p_div;
1017 output_rate *= cfg.n;
1018 do_div(output_rate, cfg.m * p_div);
1020 return output_rate;
1023 static int clk_pllm_set_rate(struct clk_hw *hw, unsigned long rate,
1024 unsigned long parent_rate)
1026 struct tegra_clk_pll_freq_table cfg;
1027 struct tegra_clk_pll *pll = to_clk_pll(hw);
1028 unsigned long flags = 0;
1029 int state, ret = 0;
1031 if (pll->lock)
1032 spin_lock_irqsave(pll->lock, flags);
1034 state = clk_pll_is_enabled(hw);
1035 if (state) {
1036 if (rate != clk_get_rate(hw->clk)) {
1037 pr_err("%s: Cannot change active PLLM\n", __func__);
1038 ret = -EINVAL;
1039 goto out;
1041 goto out;
1044 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1045 if (ret < 0)
1046 goto out;
1048 _update_pll_mnp(pll, &cfg);
1050 out:
1051 if (pll->lock)
1052 spin_unlock_irqrestore(pll->lock, flags);
1054 return ret;
1057 static void _pllcx_strobe(struct tegra_clk_pll *pll)
1059 u32 val;
1061 val = pll_readl_misc(pll);
1062 val |= PLLCX_MISC_STROBE;
1063 pll_writel_misc(val, pll);
1064 udelay(2);
1066 val &= ~PLLCX_MISC_STROBE;
1067 pll_writel_misc(val, pll);
1070 static int clk_pllc_enable(struct clk_hw *hw)
1072 struct tegra_clk_pll *pll = to_clk_pll(hw);
1073 u32 val;
1074 int ret = 0;
1075 unsigned long flags = 0;
1077 if (pll->lock)
1078 spin_lock_irqsave(pll->lock, flags);
1080 _clk_pll_enable(hw);
1081 udelay(2);
1083 val = pll_readl_misc(pll);
1084 val &= ~PLLCX_MISC_RESET;
1085 pll_writel_misc(val, pll);
1086 udelay(2);
1088 _pllcx_strobe(pll);
1090 ret = clk_pll_wait_for_lock(pll);
1092 if (pll->lock)
1093 spin_unlock_irqrestore(pll->lock, flags);
1095 return ret;
1098 static void _clk_pllc_disable(struct clk_hw *hw)
1100 struct tegra_clk_pll *pll = to_clk_pll(hw);
1101 u32 val;
1103 _clk_pll_disable(hw);
1105 val = pll_readl_misc(pll);
1106 val |= PLLCX_MISC_RESET;
1107 pll_writel_misc(val, pll);
1108 udelay(2);
1111 static void clk_pllc_disable(struct clk_hw *hw)
1113 struct tegra_clk_pll *pll = to_clk_pll(hw);
1114 unsigned long flags = 0;
1116 if (pll->lock)
1117 spin_lock_irqsave(pll->lock, flags);
1119 _clk_pllc_disable(hw);
1121 if (pll->lock)
1122 spin_unlock_irqrestore(pll->lock, flags);
1125 static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1126 unsigned long input_rate, u32 n)
1128 u32 val, n_threshold;
1130 switch (input_rate) {
1131 case 12000000:
1132 n_threshold = 70;
1133 break;
1134 case 13000000:
1135 case 26000000:
1136 n_threshold = 71;
1137 break;
1138 case 16800000:
1139 n_threshold = 55;
1140 break;
1141 case 19200000:
1142 n_threshold = 48;
1143 break;
1144 default:
1145 pr_err("%s: Unexpected reference rate %lu\n",
1146 __func__, input_rate);
1147 return -EINVAL;
1150 val = pll_readl_misc(pll);
1151 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1152 val |= n <= n_threshold ?
1153 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1154 pll_writel_misc(val, pll);
1156 return 0;
1159 static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1160 unsigned long parent_rate)
1162 struct tegra_clk_pll_freq_table cfg, old_cfg;
1163 struct tegra_clk_pll *pll = to_clk_pll(hw);
1164 unsigned long flags = 0;
1165 int state, ret = 0;
1167 if (pll->lock)
1168 spin_lock_irqsave(pll->lock, flags);
1170 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1171 if (ret < 0)
1172 goto out;
1174 _get_pll_mnp(pll, &old_cfg);
1176 if (cfg.m != old_cfg.m) {
1177 WARN_ON(1);
1178 goto out;
1181 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
1182 goto out;
1184 state = clk_pll_is_enabled(hw);
1185 if (state)
1186 _clk_pllc_disable(hw);
1188 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1189 if (ret < 0)
1190 goto out;
1192 _update_pll_mnp(pll, &cfg);
1194 if (state)
1195 ret = clk_pllc_enable(hw);
1197 out:
1198 if (pll->lock)
1199 spin_unlock_irqrestore(pll->lock, flags);
1201 return ret;
1204 static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1205 struct tegra_clk_pll_freq_table *cfg,
1206 unsigned long rate, unsigned long parent_rate)
1208 u16 m, n;
1209 u64 output_rate = parent_rate;
1211 m = _pll_fixed_mdiv(pll->params, parent_rate);
1212 n = rate * m / parent_rate;
1214 output_rate *= n;
1215 do_div(output_rate, m);
1217 if (cfg) {
1218 cfg->m = m;
1219 cfg->n = n;
1222 return output_rate;
1224 static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1225 unsigned long parent_rate)
1227 struct tegra_clk_pll_freq_table cfg, old_cfg;
1228 struct tegra_clk_pll *pll = to_clk_pll(hw);
1229 unsigned long flags = 0;
1230 int state, ret = 0;
1232 if (pll->lock)
1233 spin_lock_irqsave(pll->lock, flags);
1235 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1236 _get_pll_mnp(pll, &old_cfg);
1237 cfg.p = old_cfg.p;
1239 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1240 state = clk_pll_is_enabled(hw);
1241 if (state)
1242 _clk_pll_disable(hw);
1244 _update_pll_mnp(pll, &cfg);
1246 if (state) {
1247 _clk_pll_enable(hw);
1248 ret = clk_pll_wait_for_lock(pll);
1252 if (pll->lock)
1253 spin_unlock_irqrestore(pll->lock, flags);
1255 return ret;
1258 static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1259 unsigned long parent_rate)
1261 struct tegra_clk_pll_freq_table cfg;
1262 struct tegra_clk_pll *pll = to_clk_pll(hw);
1263 u64 rate = parent_rate;
1265 _get_pll_mnp(pll, &cfg);
1267 rate *= cfg.n;
1268 do_div(rate, cfg.m);
1270 return rate;
1273 static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1274 unsigned long *prate)
1276 struct tegra_clk_pll *pll = to_clk_pll(hw);
1278 return _pllre_calc_rate(pll, NULL, rate, *prate);
1281 static int clk_plle_tegra114_enable(struct clk_hw *hw)
1283 struct tegra_clk_pll *pll = to_clk_pll(hw);
1284 struct tegra_clk_pll_freq_table sel;
1285 u32 val;
1286 int ret;
1287 unsigned long flags = 0;
1288 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1290 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
1291 return -EINVAL;
1293 if (pll->lock)
1294 spin_lock_irqsave(pll->lock, flags);
1296 val = pll_readl_base(pll);
1297 val &= ~BIT(29); /* Disable lock override */
1298 pll_writel_base(val, pll);
1300 val = pll_readl(pll->params->aux_reg, pll);
1301 val |= PLLE_AUX_ENABLE_SWCTL;
1302 val &= ~PLLE_AUX_SEQ_ENABLE;
1303 pll_writel(val, pll->params->aux_reg, pll);
1304 udelay(1);
1306 val = pll_readl_misc(pll);
1307 val |= PLLE_MISC_LOCK_ENABLE;
1308 val |= PLLE_MISC_IDDQ_SW_CTRL;
1309 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1310 val |= PLLE_MISC_PLLE_PTS;
1311 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1312 pll_writel_misc(val, pll);
1313 udelay(5);
1315 val = pll_readl(PLLE_SS_CTRL, pll);
1316 val |= PLLE_SS_DISABLE;
1317 pll_writel(val, PLLE_SS_CTRL, pll);
1319 val = pll_readl_base(pll);
1320 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1321 divm_mask_shifted(pll));
1322 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
1323 val |= sel.m << divm_shift(pll);
1324 val |= sel.n << divn_shift(pll);
1325 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1326 pll_writel_base(val, pll);
1327 udelay(1);
1329 _clk_pll_enable(hw);
1330 ret = clk_pll_wait_for_lock(pll);
1332 if (ret < 0)
1333 goto out;
1335 val = pll_readl(PLLE_SS_CTRL, pll);
1336 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1337 val &= ~PLLE_SS_COEFFICIENTS_MASK;
1338 val |= PLLE_SS_COEFFICIENTS_VAL;
1339 pll_writel(val, PLLE_SS_CTRL, pll);
1340 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1341 pll_writel(val, PLLE_SS_CTRL, pll);
1342 udelay(1);
1343 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1344 pll_writel(val, PLLE_SS_CTRL, pll);
1345 udelay(1);
1347 /* Enable hw control of xusb brick pll */
1348 val = pll_readl_misc(pll);
1349 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1350 pll_writel_misc(val, pll);
1352 val = pll_readl(pll->params->aux_reg, pll);
1353 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1354 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1355 pll_writel(val, pll->params->aux_reg, pll);
1356 udelay(1);
1357 val |= PLLE_AUX_SEQ_ENABLE;
1358 pll_writel(val, pll->params->aux_reg, pll);
1360 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1361 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1362 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1363 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1364 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1365 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1366 udelay(1);
1367 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1368 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1370 /* Enable hw control of SATA pll */
1371 val = pll_readl(SATA_PLL_CFG0, pll);
1372 val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
1373 val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1374 val |= SATA_PLL_CFG0_SEQ_START_STATE;
1375 pll_writel(val, SATA_PLL_CFG0, pll);
1377 udelay(1);
1379 val = pll_readl(SATA_PLL_CFG0, pll);
1380 val |= SATA_PLL_CFG0_SEQ_ENABLE;
1381 pll_writel(val, SATA_PLL_CFG0, pll);
1383 out:
1384 if (pll->lock)
1385 spin_unlock_irqrestore(pll->lock, flags);
1387 return ret;
1390 static void clk_plle_tegra114_disable(struct clk_hw *hw)
1392 struct tegra_clk_pll *pll = to_clk_pll(hw);
1393 unsigned long flags = 0;
1394 u32 val;
1396 if (pll->lock)
1397 spin_lock_irqsave(pll->lock, flags);
1399 _clk_pll_disable(hw);
1401 val = pll_readl_misc(pll);
1402 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1403 pll_writel_misc(val, pll);
1404 udelay(1);
1406 if (pll->lock)
1407 spin_unlock_irqrestore(pll->lock, flags);
1409 #endif
1411 static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
1412 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1413 spinlock_t *lock)
1415 struct tegra_clk_pll *pll;
1417 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1418 if (!pll)
1419 return ERR_PTR(-ENOMEM);
1421 pll->clk_base = clk_base;
1422 pll->pmc = pmc;
1424 pll->params = pll_params;
1425 pll->lock = lock;
1427 if (!pll_params->div_nmp)
1428 pll_params->div_nmp = &default_nmp;
1430 return pll;
1433 static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1434 const char *name, const char *parent_name, unsigned long flags,
1435 const struct clk_ops *ops)
1437 struct clk_init_data init;
1439 init.name = name;
1440 init.ops = ops;
1441 init.flags = flags;
1442 init.parent_names = (parent_name ? &parent_name : NULL);
1443 init.num_parents = (parent_name ? 1 : 0);
1445 /* Data in .init is copied by clk_register(), so stack variable OK */
1446 pll->hw.init = &init;
1448 return clk_register(NULL, &pll->hw);
1451 struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1452 void __iomem *clk_base, void __iomem *pmc,
1453 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1454 spinlock_t *lock)
1456 struct tegra_clk_pll *pll;
1457 struct clk *clk;
1459 pll_params->flags |= TEGRA_PLL_BYPASS;
1460 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1461 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1462 if (IS_ERR(pll))
1463 return ERR_CAST(pll);
1465 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1466 &tegra_clk_pll_ops);
1467 if (IS_ERR(clk))
1468 kfree(pll);
1470 return clk;
1473 static struct div_nmp pll_e_nmp = {
1474 .divn_shift = PLLE_BASE_DIVN_SHIFT,
1475 .divn_width = PLLE_BASE_DIVN_WIDTH,
1476 .divm_shift = PLLE_BASE_DIVM_SHIFT,
1477 .divm_width = PLLE_BASE_DIVM_WIDTH,
1478 .divp_shift = PLLE_BASE_DIVP_SHIFT,
1479 .divp_width = PLLE_BASE_DIVP_WIDTH,
1482 struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1483 void __iomem *clk_base, void __iomem *pmc,
1484 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1485 spinlock_t *lock)
1487 struct tegra_clk_pll *pll;
1488 struct clk *clk;
1490 pll_params->flags |= TEGRA_PLL_LOCK_MISC | TEGRA_PLL_BYPASS;
1491 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1493 if (!pll_params->div_nmp)
1494 pll_params->div_nmp = &pll_e_nmp;
1496 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1497 if (IS_ERR(pll))
1498 return ERR_CAST(pll);
1500 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1501 &tegra_clk_plle_ops);
1502 if (IS_ERR(clk))
1503 kfree(pll);
1505 return clk;
1508 #if defined(CONFIG_ARCH_TEGRA_114_SOC) || defined(CONFIG_ARCH_TEGRA_124_SOC)
1509 static const struct clk_ops tegra_clk_pllxc_ops = {
1510 .is_enabled = clk_pll_is_enabled,
1511 .enable = clk_pll_iddq_enable,
1512 .disable = clk_pll_iddq_disable,
1513 .recalc_rate = clk_pll_recalc_rate,
1514 .round_rate = clk_pll_ramp_round_rate,
1515 .set_rate = clk_pllxc_set_rate,
1518 static const struct clk_ops tegra_clk_pllm_ops = {
1519 .is_enabled = clk_pll_is_enabled,
1520 .enable = clk_pll_iddq_enable,
1521 .disable = clk_pll_iddq_disable,
1522 .recalc_rate = clk_pll_recalc_rate,
1523 .round_rate = clk_pll_ramp_round_rate,
1524 .set_rate = clk_pllm_set_rate,
1527 static const struct clk_ops tegra_clk_pllc_ops = {
1528 .is_enabled = clk_pll_is_enabled,
1529 .enable = clk_pllc_enable,
1530 .disable = clk_pllc_disable,
1531 .recalc_rate = clk_pll_recalc_rate,
1532 .round_rate = clk_pll_ramp_round_rate,
1533 .set_rate = clk_pllc_set_rate,
1536 static const struct clk_ops tegra_clk_pllre_ops = {
1537 .is_enabled = clk_pll_is_enabled,
1538 .enable = clk_pll_iddq_enable,
1539 .disable = clk_pll_iddq_disable,
1540 .recalc_rate = clk_pllre_recalc_rate,
1541 .round_rate = clk_pllre_round_rate,
1542 .set_rate = clk_pllre_set_rate,
1545 static const struct clk_ops tegra_clk_plle_tegra114_ops = {
1546 .is_enabled = clk_pll_is_enabled,
1547 .enable = clk_plle_tegra114_enable,
1548 .disable = clk_plle_tegra114_disable,
1549 .recalc_rate = clk_pll_recalc_rate,
1553 struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1554 void __iomem *clk_base, void __iomem *pmc,
1555 unsigned long flags,
1556 struct tegra_clk_pll_params *pll_params,
1557 spinlock_t *lock)
1559 struct tegra_clk_pll *pll;
1560 struct clk *clk, *parent;
1561 unsigned long parent_rate;
1562 int err;
1563 u32 val, val_iddq;
1565 parent = __clk_lookup(parent_name);
1566 if (!parent) {
1567 WARN(1, "parent clk %s of %s must be registered first\n",
1568 name, parent_name);
1569 return ERR_PTR(-EINVAL);
1572 if (!pll_params->pdiv_tohw)
1573 return ERR_PTR(-EINVAL);
1575 parent_rate = __clk_get_rate(parent);
1577 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1579 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
1580 if (err)
1581 return ERR_PTR(err);
1583 val = readl_relaxed(clk_base + pll_params->base_reg);
1584 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
1586 if (val & PLL_BASE_ENABLE)
1587 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
1588 else {
1589 val_iddq |= BIT(pll_params->iddq_bit_idx);
1590 writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
1593 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1594 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1595 if (IS_ERR(pll))
1596 return ERR_CAST(pll);
1598 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1599 &tegra_clk_pllxc_ops);
1600 if (IS_ERR(clk))
1601 kfree(pll);
1603 return clk;
1606 struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1607 void __iomem *clk_base, void __iomem *pmc,
1608 unsigned long flags,
1609 struct tegra_clk_pll_params *pll_params,
1610 spinlock_t *lock, unsigned long parent_rate)
1612 u32 val;
1613 struct tegra_clk_pll *pll;
1614 struct clk *clk;
1616 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE | TEGRA_PLL_LOCK_MISC;
1618 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1620 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1621 if (IS_ERR(pll))
1622 return ERR_CAST(pll);
1624 /* program minimum rate by default */
1626 val = pll_readl_base(pll);
1627 if (val & PLL_BASE_ENABLE)
1628 WARN_ON(val & pll_params->iddq_bit_idx);
1629 else {
1630 int m;
1632 m = _pll_fixed_mdiv(pll_params, parent_rate);
1633 val = m << divm_shift(pll);
1634 val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
1635 pll_writel_base(val, pll);
1638 /* disable lock override */
1640 val = pll_readl_misc(pll);
1641 val &= ~BIT(29);
1642 pll_writel_misc(val, pll);
1644 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1645 &tegra_clk_pllre_ops);
1646 if (IS_ERR(clk))
1647 kfree(pll);
1649 return clk;
1652 struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1653 void __iomem *clk_base, void __iomem *pmc,
1654 unsigned long flags,
1655 struct tegra_clk_pll_params *pll_params,
1656 spinlock_t *lock)
1658 struct tegra_clk_pll *pll;
1659 struct clk *clk, *parent;
1660 unsigned long parent_rate;
1662 if (!pll_params->pdiv_tohw)
1663 return ERR_PTR(-EINVAL);
1665 parent = __clk_lookup(parent_name);
1666 if (!parent) {
1667 WARN(1, "parent clk %s of %s must be registered first\n",
1668 name, parent_name);
1669 return ERR_PTR(-EINVAL);
1672 parent_rate = __clk_get_rate(parent);
1674 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1676 pll_params->flags |= TEGRA_PLL_BYPASS;
1677 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1678 pll_params->flags |= TEGRA_PLLM;
1679 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1680 if (IS_ERR(pll))
1681 return ERR_CAST(pll);
1683 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1684 &tegra_clk_pllm_ops);
1685 if (IS_ERR(clk))
1686 kfree(pll);
1688 return clk;
1691 struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1692 void __iomem *clk_base, void __iomem *pmc,
1693 unsigned long flags,
1694 struct tegra_clk_pll_params *pll_params,
1695 spinlock_t *lock)
1697 struct clk *parent, *clk;
1698 struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
1699 struct tegra_clk_pll *pll;
1700 struct tegra_clk_pll_freq_table cfg;
1701 unsigned long parent_rate;
1703 if (!p_tohw)
1704 return ERR_PTR(-EINVAL);
1706 parent = __clk_lookup(parent_name);
1707 if (!parent) {
1708 WARN(1, "parent clk %s of %s must be registered first\n",
1709 name, parent_name);
1710 return ERR_PTR(-EINVAL);
1713 parent_rate = __clk_get_rate(parent);
1715 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1717 pll_params->flags |= TEGRA_PLL_BYPASS;
1718 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1719 if (IS_ERR(pll))
1720 return ERR_CAST(pll);
1723 * Most of PLLC register fields are shadowed, and can not be read
1724 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1725 * Initialize PLL to default state: disabled, reset; shadow registers
1726 * loaded with default parameters; dividers are preset for half of
1727 * minimum VCO rate (the latter assured that shadowed divider settings
1728 * are within supported range).
1731 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1732 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1734 while (p_tohw->pdiv) {
1735 if (p_tohw->pdiv == 2) {
1736 cfg.p = p_tohw->hw_val;
1737 break;
1739 p_tohw++;
1742 if (!p_tohw->pdiv) {
1743 WARN_ON(1);
1744 return ERR_PTR(-EINVAL);
1747 pll_writel_base(0, pll);
1748 _update_pll_mnp(pll, &cfg);
1750 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1751 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1752 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1753 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1755 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1757 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1758 &tegra_clk_pllc_ops);
1759 if (IS_ERR(clk))
1760 kfree(pll);
1762 return clk;
1765 struct clk *tegra_clk_register_plle_tegra114(const char *name,
1766 const char *parent_name,
1767 void __iomem *clk_base, unsigned long flags,
1768 struct tegra_clk_pll_params *pll_params,
1769 spinlock_t *lock)
1771 struct tegra_clk_pll *pll;
1772 struct clk *clk;
1773 u32 val, val_aux;
1775 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1776 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1777 if (IS_ERR(pll))
1778 return ERR_CAST(pll);
1780 /* ensure parent is set to pll_re_vco */
1782 val = pll_readl_base(pll);
1783 val_aux = pll_readl(pll_params->aux_reg, pll);
1785 if (val & PLL_BASE_ENABLE) {
1786 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
1787 (val_aux & PLLE_AUX_PLLP_SEL))
1788 WARN(1, "pll_e enabled with unsupported parent %s\n",
1789 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
1790 "pll_re_vco");
1791 } else {
1792 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
1793 pll_writel(val_aux, pll_params->aux_reg, pll);
1796 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1797 &tegra_clk_plle_tegra114_ops);
1798 if (IS_ERR(clk))
1799 kfree(pll);
1801 return clk;
1803 #endif
1805 #ifdef CONFIG_ARCH_TEGRA_124_SOC
1806 static const struct clk_ops tegra_clk_pllss_ops = {
1807 .is_enabled = clk_pll_is_enabled,
1808 .enable = clk_pll_iddq_enable,
1809 .disable = clk_pll_iddq_disable,
1810 .recalc_rate = clk_pll_recalc_rate,
1811 .round_rate = clk_pll_ramp_round_rate,
1812 .set_rate = clk_pllxc_set_rate,
1815 struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
1816 void __iomem *clk_base, unsigned long flags,
1817 struct tegra_clk_pll_params *pll_params,
1818 spinlock_t *lock)
1820 struct tegra_clk_pll *pll;
1821 struct clk *clk, *parent;
1822 struct tegra_clk_pll_freq_table cfg;
1823 unsigned long parent_rate;
1824 u32 val;
1825 int i;
1827 if (!pll_params->div_nmp)
1828 return ERR_PTR(-EINVAL);
1830 parent = __clk_lookup(parent_name);
1831 if (!parent) {
1832 WARN(1, "parent clk %s of %s must be registered first\n",
1833 name, parent_name);
1834 return ERR_PTR(-EINVAL);
1837 pll_params->flags = TEGRA_PLL_HAS_LOCK_ENABLE | TEGRA_PLL_USE_LOCK;
1838 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1839 if (IS_ERR(pll))
1840 return ERR_CAST(pll);
1842 val = pll_readl_base(pll);
1843 val &= ~PLLSS_REF_SRC_SEL_MASK;
1844 pll_writel_base(val, pll);
1846 parent_rate = __clk_get_rate(parent);
1848 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1850 /* initialize PLL to minimum rate */
1852 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1853 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1855 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
1857 if (!i) {
1858 kfree(pll);
1859 return ERR_PTR(-EINVAL);
1862 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
1864 _update_pll_mnp(pll, &cfg);
1866 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
1867 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
1868 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
1869 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
1871 val = pll_readl_base(pll);
1872 if (val & PLL_BASE_ENABLE) {
1873 if (val & BIT(pll_params->iddq_bit_idx)) {
1874 WARN(1, "%s is on but IDDQ set\n", name);
1875 kfree(pll);
1876 return ERR_PTR(-EINVAL);
1878 } else
1879 val |= BIT(pll_params->iddq_bit_idx);
1881 val &= ~PLLSS_LOCK_OVERRIDE;
1882 pll_writel_base(val, pll);
1884 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1885 &tegra_clk_pllss_ops);
1887 if (IS_ERR(clk))
1888 kfree(pll);
1890 return clk;
1892 #endif