blob: 39e0959b61bd64cadbaaea22506077c0ffdbd560 [file] [log] [blame]
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301/*
Peter De Schrijverdba40722013-04-03 17:40:36 +03002 * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved.
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05303 *
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
Peter De Schrijver642fb0c2013-09-26 18:30:01 +030080#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)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053097
Peter De Schrijverc1d19392013-04-03 17:40:41 +030098#define PLLE_AUX_PLLP_SEL BIT(2)
Jim Lin2cfe1672014-05-14 17:32:57 -070099#define PLLE_AUX_USE_LOCKDET BIT(3)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300100#define PLLE_AUX_ENABLE_SWCTL BIT(4)
Jim Lin2cfe1672014-05-14 17:32:57 -0700101#define PLLE_AUX_SS_SWCTL BIT(6)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300102#define PLLE_AUX_SEQ_ENABLE BIT(24)
Jim Lin2cfe1672014-05-14 17:32:57 -0700103#define PLLE_AUX_SEQ_START_STATE BIT(25)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300104#define PLLE_AUX_PLLRE_SEL BIT(28)
105
Jim Lin2cfe1672014-05-14 17:32:57 -0700106#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)
112
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300113#define PLLE_MISC_PLLE_PTS BIT(8)
114#define PLLE_MISC_IDDQ_SW_VALUE BIT(13)
115#define PLLE_MISC_IDDQ_SW_CTRL BIT(14)
116#define PLLE_MISC_VREG_BG_CTRL_SHIFT 4
117#define PLLE_MISC_VREG_BG_CTRL_MASK (3 << PLLE_MISC_VREG_BG_CTRL_SHIFT)
118#define PLLE_MISC_VREG_CTRL_SHIFT 2
119#define PLLE_MISC_VREG_CTRL_MASK (2 << PLLE_MISC_VREG_CTRL_SHIFT)
120
121#define PLLCX_MISC_STROBE BIT(31)
122#define PLLCX_MISC_RESET BIT(30)
123#define PLLCX_MISC_SDM_DIV_SHIFT 28
124#define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
125#define PLLCX_MISC_FILT_DIV_SHIFT 26
126#define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
127#define PLLCX_MISC_ALPHA_SHIFT 18
128#define PLLCX_MISC_DIV_LOW_RANGE \
129 ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \
130 (0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
131#define PLLCX_MISC_DIV_HIGH_RANGE \
132 ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \
133 (0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
134#define PLLCX_MISC_COEF_LOW_RANGE \
135 ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT))
136#define PLLCX_MISC_KA_SHIFT 2
137#define PLLCX_MISC_KB_SHIFT 9
138#define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \
139 (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
140 PLLCX_MISC_DIV_LOW_RANGE | \
141 PLLCX_MISC_RESET)
142#define PLLCX_MISC1_DEFAULT 0x000d2308
143#define PLLCX_MISC2_DEFAULT 0x30211200
144#define PLLCX_MISC3_DEFAULT 0x200
145
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530146#define PMC_SATA_PWRGT 0x1ac
147#define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5)
148#define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4)
149
Peter De Schrijver798e9102013-09-09 13:22:55 +0300150#define PLLSS_MISC_KCP 0
151#define PLLSS_MISC_KVCO 0
152#define PLLSS_MISC_SETUP 0
153#define PLLSS_EN_SDM 0
154#define PLLSS_EN_SSC 0
155#define PLLSS_EN_DITHER2 0
156#define PLLSS_EN_DITHER 1
157#define PLLSS_SDM_RESET 0
158#define PLLSS_CLAMP 0
159#define PLLSS_SDM_SSC_MAX 0
160#define PLLSS_SDM_SSC_MIN 0
161#define PLLSS_SDM_SSC_STEP 0
162#define PLLSS_SDM_DIN 0
163#define PLLSS_MISC_DEFAULT ((PLLSS_MISC_KCP << 25) | \
164 (PLLSS_MISC_KVCO << 24) | \
165 PLLSS_MISC_SETUP)
166#define PLLSS_CFG_DEFAULT ((PLLSS_EN_SDM << 31) | \
167 (PLLSS_EN_SSC << 30) | \
168 (PLLSS_EN_DITHER2 << 29) | \
169 (PLLSS_EN_DITHER << 28) | \
170 (PLLSS_SDM_RESET) << 27 | \
171 (PLLSS_CLAMP << 22))
172#define PLLSS_CTRL1_DEFAULT \
173 ((PLLSS_SDM_SSC_MAX << 16) | PLLSS_SDM_SSC_MIN)
174#define PLLSS_CTRL2_DEFAULT \
175 ((PLLSS_SDM_SSC_STEP << 16) | PLLSS_SDM_DIN)
176#define PLLSS_LOCK_OVERRIDE BIT(24)
177#define PLLSS_REF_SRC_SEL_SHIFT 25
178#define PLLSS_REF_SRC_SEL_MASK (3 << PLLSS_REF_SRC_SEL_SHIFT)
179
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530180#define pll_readl(offset, p) readl_relaxed(p->clk_base + offset)
181#define pll_readl_base(p) pll_readl(p->params->base_reg, p)
182#define pll_readl_misc(p) pll_readl(p->params->misc_reg, p)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300183#define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530184
185#define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
186#define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
187#define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300188#define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530189
190#define mask(w) ((1 << (w)) - 1)
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300191#define divm_mask(p) mask(p->params->div_nmp->divm_width)
192#define divn_mask(p) mask(p->params->div_nmp->divn_width)
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300193#define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300194 mask(p->params->div_nmp->divp_width))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530195
196#define divm_max(p) (divm_mask(p))
197#define divn_max(p) (divn_mask(p))
198#define divp_max(p) (1 << (divp_mask(p)))
199
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300200static struct div_nmp default_nmp = {
201 .divn_shift = PLL_BASE_DIVN_SHIFT,
202 .divn_width = PLL_BASE_DIVN_WIDTH,
203 .divm_shift = PLL_BASE_DIVM_SHIFT,
204 .divm_width = PLL_BASE_DIVM_WIDTH,
205 .divp_shift = PLL_BASE_DIVP_SHIFT,
206 .divp_width = PLL_BASE_DIVP_WIDTH,
207};
208
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530209static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
210{
211 u32 val;
212
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300213 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530214 return;
215
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300216 if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
Peter De Schrijver7ba28812013-04-03 17:40:38 +0300217 return;
218
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530219 val = pll_readl_misc(pll);
220 val |= BIT(pll->params->lock_enable_bit_idx);
221 pll_writel_misc(val, pll);
222}
223
Peter De Schrijverdba40722013-04-03 17:40:36 +0300224static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530225{
226 int i;
Peter De Schrijver3e727712013-04-03 17:40:40 +0300227 u32 val, lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300228 void __iomem *lock_addr;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530229
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300230 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530231 udelay(pll->params->lock_delay);
232 return 0;
233 }
234
Peter De Schrijverdba40722013-04-03 17:40:36 +0300235 lock_addr = pll->clk_base;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300236 if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300237 lock_addr += pll->params->misc_reg;
238 else
239 lock_addr += pll->params->base_reg;
240
Peter De Schrijver3e727712013-04-03 17:40:40 +0300241 lock_mask = pll->params->lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300242
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530243 for (i = 0; i < pll->params->lock_delay; i++) {
244 val = readl_relaxed(lock_addr);
Peter De Schrijver3e727712013-04-03 17:40:40 +0300245 if ((val & lock_mask) == lock_mask) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530246 udelay(PLL_POST_LOCK_DELAY);
247 return 0;
248 }
249 udelay(2); /* timeout = 2 * lock time */
250 }
251
252 pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
253 __clk_get_name(pll->hw.clk));
254
255 return -1;
256}
257
258static int clk_pll_is_enabled(struct clk_hw *hw)
259{
260 struct tegra_clk_pll *pll = to_clk_pll(hw);
261 u32 val;
262
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300263 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530264 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
265 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
266 return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
267 }
268
269 val = pll_readl_base(pll);
270
271 return val & PLL_BASE_ENABLE ? 1 : 0;
272}
273
Peter De Schrijverdba40722013-04-03 17:40:36 +0300274static void _clk_pll_enable(struct clk_hw *hw)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530275{
276 struct tegra_clk_pll *pll = to_clk_pll(hw);
277 u32 val;
278
279 clk_pll_enable_lock(pll);
280
281 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300282 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300283 val &= ~PLL_BASE_BYPASS;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530284 val |= PLL_BASE_ENABLE;
285 pll_writel_base(val, pll);
286
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300287 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530288 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
289 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
290 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
291 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530292}
293
294static void _clk_pll_disable(struct clk_hw *hw)
295{
296 struct tegra_clk_pll *pll = to_clk_pll(hw);
297 u32 val;
298
299 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300300 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300301 val &= ~PLL_BASE_BYPASS;
302 val &= ~PLL_BASE_ENABLE;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530303 pll_writel_base(val, pll);
304
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300305 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530306 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
307 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
308 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
309 }
310}
311
312static int clk_pll_enable(struct clk_hw *hw)
313{
314 struct tegra_clk_pll *pll = to_clk_pll(hw);
315 unsigned long flags = 0;
316 int ret;
317
318 if (pll->lock)
319 spin_lock_irqsave(pll->lock, flags);
320
Peter De Schrijverdba40722013-04-03 17:40:36 +0300321 _clk_pll_enable(hw);
322
323 ret = clk_pll_wait_for_lock(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530324
325 if (pll->lock)
326 spin_unlock_irqrestore(pll->lock, flags);
327
328 return ret;
329}
330
331static void clk_pll_disable(struct clk_hw *hw)
332{
333 struct tegra_clk_pll *pll = to_clk_pll(hw);
334 unsigned long flags = 0;
335
336 if (pll->lock)
337 spin_lock_irqsave(pll->lock, flags);
338
339 _clk_pll_disable(hw);
340
341 if (pll->lock)
342 spin_unlock_irqrestore(pll->lock, flags);
343}
344
Peter De Schrijver053b5252013-06-05 15:56:41 +0300345static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
346{
347 struct tegra_clk_pll *pll = to_clk_pll(hw);
348 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
349
350 if (p_tohw) {
351 while (p_tohw->pdiv) {
352 if (p_div <= p_tohw->pdiv)
353 return p_tohw->hw_val;
354 p_tohw++;
355 }
356 return -EINVAL;
357 }
358 return -EINVAL;
359}
360
361static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
362{
363 struct tegra_clk_pll *pll = to_clk_pll(hw);
364 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
365
366 if (p_tohw) {
367 while (p_tohw->pdiv) {
368 if (p_div_hw == p_tohw->hw_val)
369 return p_tohw->pdiv;
370 p_tohw++;
371 }
372 return -EINVAL;
373 }
374
375 return 1 << p_div_hw;
376}
377
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530378static int _get_table_rate(struct clk_hw *hw,
379 struct tegra_clk_pll_freq_table *cfg,
380 unsigned long rate, unsigned long parent_rate)
381{
382 struct tegra_clk_pll *pll = to_clk_pll(hw);
383 struct tegra_clk_pll_freq_table *sel;
384
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300385 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530386 if (sel->input_rate == parent_rate &&
387 sel->output_rate == rate)
388 break;
389
390 if (sel->input_rate == 0)
391 return -EINVAL;
392
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530393 cfg->input_rate = sel->input_rate;
394 cfg->output_rate = sel->output_rate;
395 cfg->m = sel->m;
396 cfg->n = sel->n;
397 cfg->p = sel->p;
398 cfg->cpcon = sel->cpcon;
399
400 return 0;
401}
402
403static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
404 unsigned long rate, unsigned long parent_rate)
405{
406 struct tegra_clk_pll *pll = to_clk_pll(hw);
407 unsigned long cfreq;
408 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300409 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530410
411 switch (parent_rate) {
412 case 12000000:
413 case 26000000:
414 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
415 break;
416 case 13000000:
417 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
418 break;
419 case 16800000:
420 case 19200000:
421 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
422 break;
423 case 9600000:
424 case 28800000:
425 /*
426 * PLL_P_OUT1 rate is not listed in PLLA table
427 */
428 cfreq = parent_rate/(parent_rate/1000000);
429 break;
430 default:
431 pr_err("%s Unexpected reference rate %lu\n",
432 __func__, parent_rate);
433 BUG();
434 }
435
436 /* Raise VCO to guarantee 0.5% accuracy */
437 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
438 cfg->output_rate <<= 1)
439 p_div++;
440
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530441 cfg->m = parent_rate / cfreq;
442 cfg->n = cfg->output_rate / cfreq;
443 cfg->cpcon = OUT_OF_TABLE_CPCON;
444
445 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300446 (1 << p_div) > divp_max(pll)
447 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530448 return -EINVAL;
449 }
450
Thierry Reding00c674e2013-11-18 16:11:35 +0100451 cfg->output_rate >>= p_div;
452
Peter De Schrijver053b5252013-06-05 15:56:41 +0300453 if (pll->params->pdiv_tohw) {
454 ret = _p_div_to_hw(hw, 1 << p_div);
455 if (ret < 0)
456 return ret;
457 else
458 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300459 } else
460 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300461
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530462 return 0;
463}
464
Peter De Schrijverdba40722013-04-03 17:40:36 +0300465static void _update_pll_mnp(struct tegra_clk_pll *pll,
466 struct tegra_clk_pll_freq_table *cfg)
467{
468 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300469 struct tegra_clk_pll_params *params = pll->params;
470 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300471
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300472 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300473 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
474 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
475 val = pll_override_readl(params->pmc_divp_reg, pll);
476 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
477 val |= cfg->p << div_nmp->override_divp_shift;
478 pll_override_writel(val, params->pmc_divp_reg, pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300479
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300480 val = pll_override_readl(params->pmc_divnm_reg, pll);
481 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
482 ~(divn_mask(pll) << div_nmp->override_divn_shift);
483 val |= (cfg->m << div_nmp->override_divm_shift) |
484 (cfg->n << div_nmp->override_divn_shift);
485 pll_override_writel(val, params->pmc_divnm_reg, pll);
486 } else {
487 val = pll_readl_base(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300488
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300489 val &= ~((divm_mask(pll) << div_nmp->divm_shift) |
490 (divn_mask(pll) << div_nmp->divn_shift) |
491 (divp_mask(pll) << div_nmp->divp_shift));
492
493 val |= ((cfg->m << div_nmp->divm_shift) |
494 (cfg->n << div_nmp->divn_shift) |
495 (cfg->p << div_nmp->divp_shift));
496
497 pll_writel_base(val, pll);
498 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300499}
500
501static void _get_pll_mnp(struct tegra_clk_pll *pll,
502 struct tegra_clk_pll_freq_table *cfg)
503{
504 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300505 struct tegra_clk_pll_params *params = pll->params;
506 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300507
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300508 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300509 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
510 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
511 val = pll_override_readl(params->pmc_divp_reg, pll);
512 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300513
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300514 val = pll_override_readl(params->pmc_divnm_reg, pll);
515 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
516 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
517 } else {
518 val = pll_readl_base(pll);
519
520 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
521 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
522 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
523 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300524}
525
526static void _update_pll_cpcon(struct tegra_clk_pll *pll,
527 struct tegra_clk_pll_freq_table *cfg,
528 unsigned long rate)
529{
530 u32 val;
531
532 val = pll_readl_misc(pll);
533
534 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
535 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
536
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300537 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300538 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
539 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
540 val |= 1 << PLL_MISC_LFCON_SHIFT;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300541 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300542 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
543 if (rate >= (pll->params->vco_max >> 1))
544 val |= 1 << PLL_MISC_DCCON_SHIFT;
545 }
546
547 pll_writel_misc(val, pll);
548}
549
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530550static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
551 unsigned long rate)
552{
553 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300554 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530555
556 state = clk_pll_is_enabled(hw);
557
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530558 if (state)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300559 _clk_pll_disable(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530560
Peter De Schrijverdba40722013-04-03 17:40:36 +0300561 _update_pll_mnp(pll, cfg);
562
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300563 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300564 _update_pll_cpcon(pll, cfg, rate);
565
566 if (state) {
567 _clk_pll_enable(hw);
568 ret = clk_pll_wait_for_lock(pll);
569 }
570
571 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530572}
573
574static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
575 unsigned long parent_rate)
576{
577 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300578 struct tegra_clk_pll_freq_table cfg, old_cfg;
579 unsigned long flags = 0;
580 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530581
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300582 if (pll->params->flags & TEGRA_PLL_FIXED) {
583 if (rate != pll->params->fixed_rate) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530584 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
585 __func__, __clk_get_name(hw->clk),
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300586 pll->params->fixed_rate, rate);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530587 return -EINVAL;
588 }
589 return 0;
590 }
591
592 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Peter De Schrijver053b5252013-06-05 15:56:41 +0300593 _calc_rate(hw, &cfg, rate, parent_rate)) {
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100594 pr_err("%s: Failed to set %s rate %lu\n", __func__,
595 __clk_get_name(hw->clk), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300596 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530597 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300598 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300599 if (pll->lock)
600 spin_lock_irqsave(pll->lock, flags);
601
602 _get_pll_mnp(pll, &old_cfg);
603
604 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
605 ret = _program_pll(hw, &cfg, rate);
606
607 if (pll->lock)
608 spin_unlock_irqrestore(pll->lock, flags);
609
610 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530611}
612
613static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
614 unsigned long *prate)
615{
616 struct tegra_clk_pll *pll = to_clk_pll(hw);
617 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530618
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300619 if (pll->params->flags & TEGRA_PLL_FIXED)
620 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530621
622 /* PLLM is used for memory; we do not change rate */
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300623 if (pll->params->flags & TEGRA_PLLM)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530624 return __clk_get_rate(hw->clk);
625
626 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100627 _calc_rate(hw, &cfg, rate, *prate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530628 return -EINVAL;
629
Peter De Schrijver053b5252013-06-05 15:56:41 +0300630 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530631}
632
633static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
634 unsigned long parent_rate)
635{
636 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300637 struct tegra_clk_pll_freq_table cfg;
638 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530639 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300640 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530641
Peter De Schrijverdba40722013-04-03 17:40:36 +0300642 val = pll_readl_base(pll);
643
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300644 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530645 return parent_rate;
646
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300647 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
648 !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530649 struct tegra_clk_pll_freq_table sel;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300650 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
651 parent_rate)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530652 pr_err("Clock %s has unknown fixed frequency\n",
653 __clk_get_name(hw->clk));
654 BUG();
655 }
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300656 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530657 }
658
Peter De Schrijverdba40722013-04-03 17:40:36 +0300659 _get_pll_mnp(pll, &cfg);
660
Peter De Schrijver053b5252013-06-05 15:56:41 +0300661 pdiv = _hw_to_p_div(hw, cfg.p);
662 if (pdiv < 0) {
663 WARN_ON(1);
664 pdiv = 1;
665 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300666
667 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530668
Peter De Schrijverdba40722013-04-03 17:40:36 +0300669 rate *= cfg.n;
670 do_div(rate, cfg.m);
671
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530672 return rate;
673}
674
675static int clk_plle_training(struct tegra_clk_pll *pll)
676{
677 u32 val;
678 unsigned long timeout;
679
680 if (!pll->pmc)
681 return -ENOSYS;
682
683 /*
684 * PLLE is already disabled, and setup cleared;
685 * create falling edge on PLLE IDDQ input.
686 */
687 val = readl(pll->pmc + PMC_SATA_PWRGT);
688 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
689 writel(val, pll->pmc + PMC_SATA_PWRGT);
690
691 val = readl(pll->pmc + PMC_SATA_PWRGT);
692 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
693 writel(val, pll->pmc + PMC_SATA_PWRGT);
694
695 val = readl(pll->pmc + PMC_SATA_PWRGT);
696 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
697 writel(val, pll->pmc + PMC_SATA_PWRGT);
698
699 val = pll_readl_misc(pll);
700
701 timeout = jiffies + msecs_to_jiffies(100);
702 while (1) {
703 val = pll_readl_misc(pll);
704 if (val & PLLE_MISC_READY)
705 break;
706 if (time_after(jiffies, timeout)) {
707 pr_err("%s: timeout waiting for PLLE\n", __func__);
708 return -EBUSY;
709 }
710 udelay(300);
711 }
712
713 return 0;
714}
715
716static int clk_plle_enable(struct clk_hw *hw)
717{
718 struct tegra_clk_pll *pll = to_clk_pll(hw);
719 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
720 struct tegra_clk_pll_freq_table sel;
721 u32 val;
722 int err;
723
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300724 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530725 return -EINVAL;
726
727 clk_pll_disable(hw);
728
729 val = pll_readl_misc(pll);
730 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
731 pll_writel_misc(val, pll);
732
733 val = pll_readl_misc(pll);
734 if (!(val & PLLE_MISC_READY)) {
735 err = clk_plle_training(pll);
736 if (err)
737 return err;
738 }
739
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300740 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530741 /* configure dividers */
742 val = pll_readl_base(pll);
743 val &= ~(divm_mask(pll) | divn_mask(pll) | divp_mask(pll));
744 val &= ~(PLLE_BASE_DIVCML_WIDTH << PLLE_BASE_DIVCML_SHIFT);
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300745 val |= sel.m << pll->params->div_nmp->divm_shift;
746 val |= sel.n << pll->params->div_nmp->divn_shift;
747 val |= sel.p << pll->params->div_nmp->divp_shift;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530748 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
749 pll_writel_base(val, pll);
750 }
751
752 val = pll_readl_misc(pll);
753 val |= PLLE_MISC_SETUP_VALUE;
754 val |= PLLE_MISC_LOCK_ENABLE;
755 pll_writel_misc(val, pll);
756
757 val = readl(pll->clk_base + PLLE_SS_CTRL);
758 val |= PLLE_SS_DISABLE;
759 writel(val, pll->clk_base + PLLE_SS_CTRL);
760
761 val |= pll_readl_base(pll);
762 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
763 pll_writel_base(val, pll);
764
Peter De Schrijverdba40722013-04-03 17:40:36 +0300765 clk_pll_wait_for_lock(pll);
766
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530767 return 0;
768}
769
770static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
771 unsigned long parent_rate)
772{
773 struct tegra_clk_pll *pll = to_clk_pll(hw);
774 u32 val = pll_readl_base(pll);
775 u32 divn = 0, divm = 0, divp = 0;
776 u64 rate = parent_rate;
777
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300778 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
779 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
780 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530781 divm *= divp;
782
783 rate *= divn;
784 do_div(rate, divm);
785 return rate;
786}
787
788const struct clk_ops tegra_clk_pll_ops = {
789 .is_enabled = clk_pll_is_enabled,
790 .enable = clk_pll_enable,
791 .disable = clk_pll_disable,
792 .recalc_rate = clk_pll_recalc_rate,
793 .round_rate = clk_pll_round_rate,
794 .set_rate = clk_pll_set_rate,
795};
796
797const struct clk_ops tegra_clk_plle_ops = {
798 .recalc_rate = clk_plle_recalc_rate,
799 .is_enabled = clk_pll_is_enabled,
800 .disable = clk_pll_disable,
801 .enable = clk_plle_enable,
802};
803
Peter De Schrijver798e9102013-09-09 13:22:55 +0300804#if defined(CONFIG_ARCH_TEGRA_114_SOC) || defined(CONFIG_ARCH_TEGRA_124_SOC)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300805
806static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
807 unsigned long parent_rate)
808{
809 if (parent_rate > pll_params->cf_max)
810 return 2;
811 else
812 return 1;
813}
814
Peter De Schrijver04edb092013-09-06 14:37:37 +0300815static unsigned long _clip_vco_min(unsigned long vco_min,
816 unsigned long parent_rate)
817{
818 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
819}
820
821static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
822 void __iomem *clk_base,
823 unsigned long parent_rate)
824{
825 u32 val;
826 u32 step_a, step_b;
827
828 switch (parent_rate) {
829 case 12000000:
830 case 13000000:
831 case 26000000:
832 step_a = 0x2B;
833 step_b = 0x0B;
834 break;
835 case 16800000:
836 step_a = 0x1A;
837 step_b = 0x09;
838 break;
839 case 19200000:
840 step_a = 0x12;
841 step_b = 0x08;
842 break;
843 default:
844 pr_err("%s: Unexpected reference rate %lu\n",
845 __func__, parent_rate);
846 WARN_ON(1);
847 return -EINVAL;
848 }
849
850 val = step_a << pll_params->stepa_shift;
851 val |= step_b << pll_params->stepb_shift;
852 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
853
854 return 0;
855}
856
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300857static int clk_pll_iddq_enable(struct clk_hw *hw)
858{
859 struct tegra_clk_pll *pll = to_clk_pll(hw);
860 unsigned long flags = 0;
861
862 u32 val;
863 int ret;
864
865 if (pll->lock)
866 spin_lock_irqsave(pll->lock, flags);
867
868 val = pll_readl(pll->params->iddq_reg, pll);
869 val &= ~BIT(pll->params->iddq_bit_idx);
870 pll_writel(val, pll->params->iddq_reg, pll);
871 udelay(2);
872
873 _clk_pll_enable(hw);
874
875 ret = clk_pll_wait_for_lock(pll);
876
877 if (pll->lock)
878 spin_unlock_irqrestore(pll->lock, flags);
879
880 return 0;
881}
882
883static void clk_pll_iddq_disable(struct clk_hw *hw)
884{
885 struct tegra_clk_pll *pll = to_clk_pll(hw);
886 unsigned long flags = 0;
887 u32 val;
888
889 if (pll->lock)
890 spin_lock_irqsave(pll->lock, flags);
891
892 _clk_pll_disable(hw);
893
894 val = pll_readl(pll->params->iddq_reg, pll);
895 val |= BIT(pll->params->iddq_bit_idx);
896 pll_writel(val, pll->params->iddq_reg, pll);
897 udelay(2);
898
899 if (pll->lock)
900 spin_unlock_irqrestore(pll->lock, flags);
901}
902
903static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
904 struct tegra_clk_pll_freq_table *cfg,
905 unsigned long rate, unsigned long parent_rate)
906{
907 struct tegra_clk_pll *pll = to_clk_pll(hw);
908 unsigned int p;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300909 int p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300910
911 if (!rate)
912 return -EINVAL;
913
914 p = DIV_ROUND_UP(pll->params->vco_min, rate);
915 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300916 cfg->output_rate = rate * p;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300917 cfg->n = cfg->output_rate * cfg->m / parent_rate;
918
Peter De Schrijver053b5252013-06-05 15:56:41 +0300919 p_div = _p_div_to_hw(hw, p);
920 if (p_div < 0)
921 return p_div;
922 else
923 cfg->p = p_div;
924
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300925 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
926 return -EINVAL;
927
928 return 0;
929}
930
931static int _pll_ramp_calc_pll(struct clk_hw *hw,
932 struct tegra_clk_pll_freq_table *cfg,
933 unsigned long rate, unsigned long parent_rate)
934{
935 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300936 int err = 0, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300937
938 err = _get_table_rate(hw, cfg, rate, parent_rate);
939 if (err < 0)
940 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300941 else {
942 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300943 WARN_ON(1);
944 err = -EINVAL;
945 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300946 }
947 p_div = _p_div_to_hw(hw, cfg->p);
948 if (p_div < 0)
949 return p_div;
950 else
951 cfg->p = p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300952 }
953
Peter De Schrijver053b5252013-06-05 15:56:41 +0300954 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300955 err = -EINVAL;
956
957out:
958 return err;
959}
960
961static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
962 unsigned long parent_rate)
963{
964 struct tegra_clk_pll *pll = to_clk_pll(hw);
965 struct tegra_clk_pll_freq_table cfg, old_cfg;
966 unsigned long flags = 0;
967 int ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300968
969 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
970 if (ret < 0)
971 return ret;
972
973 if (pll->lock)
974 spin_lock_irqsave(pll->lock, flags);
975
976 _get_pll_mnp(pll, &old_cfg);
977
Peter De Schrijver053b5252013-06-05 15:56:41 +0300978 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300979 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300980
981 if (pll->lock)
982 spin_unlock_irqrestore(pll->lock, flags);
983
984 return ret;
985}
986
987static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
988 unsigned long *prate)
989{
990 struct tegra_clk_pll_freq_table cfg;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300991 int ret = 0, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300992 u64 output_rate = *prate;
993
994 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
995 if (ret < 0)
996 return ret;
997
Peter De Schrijver053b5252013-06-05 15:56:41 +0300998 p_div = _hw_to_p_div(hw, cfg.p);
999 if (p_div < 0)
1000 return p_div;
1001
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001002 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001003 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001004
1005 return output_rate;
1006}
1007
1008static int clk_pllm_set_rate(struct clk_hw *hw, unsigned long rate,
1009 unsigned long parent_rate)
1010{
1011 struct tegra_clk_pll_freq_table cfg;
1012 struct tegra_clk_pll *pll = to_clk_pll(hw);
1013 unsigned long flags = 0;
1014 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001015
1016 if (pll->lock)
1017 spin_lock_irqsave(pll->lock, flags);
1018
1019 state = clk_pll_is_enabled(hw);
1020 if (state) {
1021 if (rate != clk_get_rate(hw->clk)) {
1022 pr_err("%s: Cannot change active PLLM\n", __func__);
1023 ret = -EINVAL;
1024 goto out;
1025 }
1026 goto out;
1027 }
1028
1029 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1030 if (ret < 0)
1031 goto out;
1032
Peter De Schrijver408a24f2013-06-06 13:47:31 +03001033 _update_pll_mnp(pll, &cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001034
1035out:
1036 if (pll->lock)
1037 spin_unlock_irqrestore(pll->lock, flags);
1038
1039 return ret;
1040}
1041
1042static void _pllcx_strobe(struct tegra_clk_pll *pll)
1043{
1044 u32 val;
1045
1046 val = pll_readl_misc(pll);
1047 val |= PLLCX_MISC_STROBE;
1048 pll_writel_misc(val, pll);
1049 udelay(2);
1050
1051 val &= ~PLLCX_MISC_STROBE;
1052 pll_writel_misc(val, pll);
1053}
1054
1055static int clk_pllc_enable(struct clk_hw *hw)
1056{
1057 struct tegra_clk_pll *pll = to_clk_pll(hw);
1058 u32 val;
1059 int ret = 0;
1060 unsigned long flags = 0;
1061
1062 if (pll->lock)
1063 spin_lock_irqsave(pll->lock, flags);
1064
1065 _clk_pll_enable(hw);
1066 udelay(2);
1067
1068 val = pll_readl_misc(pll);
1069 val &= ~PLLCX_MISC_RESET;
1070 pll_writel_misc(val, pll);
1071 udelay(2);
1072
1073 _pllcx_strobe(pll);
1074
1075 ret = clk_pll_wait_for_lock(pll);
1076
1077 if (pll->lock)
1078 spin_unlock_irqrestore(pll->lock, flags);
1079
1080 return ret;
1081}
1082
1083static void _clk_pllc_disable(struct clk_hw *hw)
1084{
1085 struct tegra_clk_pll *pll = to_clk_pll(hw);
1086 u32 val;
1087
1088 _clk_pll_disable(hw);
1089
1090 val = pll_readl_misc(pll);
1091 val |= PLLCX_MISC_RESET;
1092 pll_writel_misc(val, pll);
1093 udelay(2);
1094}
1095
1096static void clk_pllc_disable(struct clk_hw *hw)
1097{
1098 struct tegra_clk_pll *pll = to_clk_pll(hw);
1099 unsigned long flags = 0;
1100
1101 if (pll->lock)
1102 spin_lock_irqsave(pll->lock, flags);
1103
1104 _clk_pllc_disable(hw);
1105
1106 if (pll->lock)
1107 spin_unlock_irqrestore(pll->lock, flags);
1108}
1109
1110static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1111 unsigned long input_rate, u32 n)
1112{
1113 u32 val, n_threshold;
1114
1115 switch (input_rate) {
1116 case 12000000:
1117 n_threshold = 70;
1118 break;
1119 case 13000000:
1120 case 26000000:
1121 n_threshold = 71;
1122 break;
1123 case 16800000:
1124 n_threshold = 55;
1125 break;
1126 case 19200000:
1127 n_threshold = 48;
1128 break;
1129 default:
1130 pr_err("%s: Unexpected reference rate %lu\n",
1131 __func__, input_rate);
1132 return -EINVAL;
1133 }
1134
1135 val = pll_readl_misc(pll);
1136 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1137 val |= n <= n_threshold ?
1138 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1139 pll_writel_misc(val, pll);
1140
1141 return 0;
1142}
1143
1144static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1145 unsigned long parent_rate)
1146{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001147 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001148 struct tegra_clk_pll *pll = to_clk_pll(hw);
1149 unsigned long flags = 0;
1150 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001151
1152 if (pll->lock)
1153 spin_lock_irqsave(pll->lock, flags);
1154
1155 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1156 if (ret < 0)
1157 goto out;
1158
Peter De Schrijver053b5252013-06-05 15:56:41 +03001159 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001160
Peter De Schrijver053b5252013-06-05 15:56:41 +03001161 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001162 WARN_ON(1);
1163 goto out;
1164 }
1165
Peter De Schrijver053b5252013-06-05 15:56:41 +03001166 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001167 goto out;
1168
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001169 state = clk_pll_is_enabled(hw);
1170 if (state)
1171 _clk_pllc_disable(hw);
1172
1173 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1174 if (ret < 0)
1175 goto out;
1176
1177 _update_pll_mnp(pll, &cfg);
1178
1179 if (state)
1180 ret = clk_pllc_enable(hw);
1181
1182out:
1183 if (pll->lock)
1184 spin_unlock_irqrestore(pll->lock, flags);
1185
1186 return ret;
1187}
1188
1189static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1190 struct tegra_clk_pll_freq_table *cfg,
1191 unsigned long rate, unsigned long parent_rate)
1192{
1193 u16 m, n;
1194 u64 output_rate = parent_rate;
1195
1196 m = _pll_fixed_mdiv(pll->params, parent_rate);
1197 n = rate * m / parent_rate;
1198
1199 output_rate *= n;
1200 do_div(output_rate, m);
1201
1202 if (cfg) {
1203 cfg->m = m;
1204 cfg->n = n;
1205 }
1206
1207 return output_rate;
1208}
1209static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1210 unsigned long parent_rate)
1211{
1212 struct tegra_clk_pll_freq_table cfg, old_cfg;
1213 struct tegra_clk_pll *pll = to_clk_pll(hw);
1214 unsigned long flags = 0;
1215 int state, ret = 0;
1216
1217 if (pll->lock)
1218 spin_lock_irqsave(pll->lock, flags);
1219
1220 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1221 _get_pll_mnp(pll, &old_cfg);
1222 cfg.p = old_cfg.p;
1223
1224 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1225 state = clk_pll_is_enabled(hw);
1226 if (state)
1227 _clk_pll_disable(hw);
1228
1229 _update_pll_mnp(pll, &cfg);
1230
1231 if (state) {
1232 _clk_pll_enable(hw);
1233 ret = clk_pll_wait_for_lock(pll);
1234 }
1235 }
1236
1237 if (pll->lock)
1238 spin_unlock_irqrestore(pll->lock, flags);
1239
1240 return ret;
1241}
1242
1243static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1244 unsigned long parent_rate)
1245{
1246 struct tegra_clk_pll_freq_table cfg;
1247 struct tegra_clk_pll *pll = to_clk_pll(hw);
1248 u64 rate = parent_rate;
1249
1250 _get_pll_mnp(pll, &cfg);
1251
1252 rate *= cfg.n;
1253 do_div(rate, cfg.m);
1254
1255 return rate;
1256}
1257
1258static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1259 unsigned long *prate)
1260{
1261 struct tegra_clk_pll *pll = to_clk_pll(hw);
1262
1263 return _pllre_calc_rate(pll, NULL, rate, *prate);
1264}
1265
1266static int clk_plle_tegra114_enable(struct clk_hw *hw)
1267{
1268 struct tegra_clk_pll *pll = to_clk_pll(hw);
1269 struct tegra_clk_pll_freq_table sel;
1270 u32 val;
1271 int ret;
1272 unsigned long flags = 0;
1273 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1274
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001275 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001276 return -EINVAL;
1277
1278 if (pll->lock)
1279 spin_lock_irqsave(pll->lock, flags);
1280
1281 val = pll_readl_base(pll);
1282 val &= ~BIT(29); /* Disable lock override */
1283 pll_writel_base(val, pll);
1284
1285 val = pll_readl(pll->params->aux_reg, pll);
1286 val |= PLLE_AUX_ENABLE_SWCTL;
1287 val &= ~PLLE_AUX_SEQ_ENABLE;
1288 pll_writel(val, pll->params->aux_reg, pll);
1289 udelay(1);
1290
1291 val = pll_readl_misc(pll);
1292 val |= PLLE_MISC_LOCK_ENABLE;
1293 val |= PLLE_MISC_IDDQ_SW_CTRL;
1294 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1295 val |= PLLE_MISC_PLLE_PTS;
1296 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1297 pll_writel_misc(val, pll);
1298 udelay(5);
1299
1300 val = pll_readl(PLLE_SS_CTRL, pll);
1301 val |= PLLE_SS_DISABLE;
1302 pll_writel(val, PLLE_SS_CTRL, pll);
1303
1304 val = pll_readl_base(pll);
1305 val &= ~(divm_mask(pll) | divn_mask(pll) | divp_mask(pll));
1306 val &= ~(PLLE_BASE_DIVCML_WIDTH << PLLE_BASE_DIVCML_SHIFT);
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001307 val |= sel.m << pll->params->div_nmp->divm_shift;
1308 val |= sel.n << pll->params->div_nmp->divn_shift;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001309 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1310 pll_writel_base(val, pll);
1311 udelay(1);
1312
1313 _clk_pll_enable(hw);
1314 ret = clk_pll_wait_for_lock(pll);
1315
1316 if (ret < 0)
1317 goto out;
1318
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001319 val = pll_readl(PLLE_SS_CTRL, pll);
1320 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1321 val &= ~PLLE_SS_COEFFICIENTS_MASK;
1322 val |= PLLE_SS_COEFFICIENTS_VAL;
1323 pll_writel(val, PLLE_SS_CTRL, pll);
1324 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1325 pll_writel(val, PLLE_SS_CTRL, pll);
1326 udelay(1);
1327 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1328 pll_writel(val, PLLE_SS_CTRL, pll);
1329 udelay(1);
1330
Jim Lin2cfe1672014-05-14 17:32:57 -07001331 /* Enable hw control of xusb brick pll */
1332 val = pll_readl_misc(pll);
1333 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1334 pll_writel_misc(val, pll);
1335
1336 val = pll_readl(pll->params->aux_reg, pll);
1337 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1338 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1339 pll_writel(val, pll->params->aux_reg, pll);
1340 udelay(1);
1341 val |= PLLE_AUX_SEQ_ENABLE;
1342 pll_writel(val, pll->params->aux_reg, pll);
1343
1344 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1345 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1346 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1347 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1348 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1349 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1350 udelay(1);
1351 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1352 pll_writel(val, XUSBIO_PLL_CFG0, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001353
1354out:
1355 if (pll->lock)
1356 spin_unlock_irqrestore(pll->lock, flags);
1357
1358 return ret;
1359}
1360
1361static void clk_plle_tegra114_disable(struct clk_hw *hw)
1362{
1363 struct tegra_clk_pll *pll = to_clk_pll(hw);
1364 unsigned long flags = 0;
1365 u32 val;
1366
1367 if (pll->lock)
1368 spin_lock_irqsave(pll->lock, flags);
1369
1370 _clk_pll_disable(hw);
1371
1372 val = pll_readl_misc(pll);
1373 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1374 pll_writel_misc(val, pll);
1375 udelay(1);
1376
1377 if (pll->lock)
1378 spin_unlock_irqrestore(pll->lock, flags);
1379}
1380#endif
1381
Peter De Schrijverdba40722013-04-03 17:40:36 +03001382static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001383 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1384 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301385{
1386 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301387
1388 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1389 if (!pll)
1390 return ERR_PTR(-ENOMEM);
1391
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301392 pll->clk_base = clk_base;
1393 pll->pmc = pmc;
1394
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301395 pll->params = pll_params;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301396 pll->lock = lock;
1397
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001398 if (!pll_params->div_nmp)
1399 pll_params->div_nmp = &default_nmp;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301400
Peter De Schrijverdba40722013-04-03 17:40:36 +03001401 return pll;
1402}
1403
1404static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1405 const char *name, const char *parent_name, unsigned long flags,
1406 const struct clk_ops *ops)
1407{
1408 struct clk_init_data init;
1409
1410 init.name = name;
1411 init.ops = ops;
1412 init.flags = flags;
1413 init.parent_names = (parent_name ? &parent_name : NULL);
1414 init.num_parents = (parent_name ? 1 : 0);
1415
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301416 /* Data in .init is copied by clk_register(), so stack variable OK */
1417 pll->hw.init = &init;
1418
Peter De Schrijverdba40722013-04-03 17:40:36 +03001419 return clk_register(NULL, &pll->hw);
1420}
1421
1422struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1423 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001424 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1425 spinlock_t *lock)
Peter De Schrijverdba40722013-04-03 17:40:36 +03001426{
1427 struct tegra_clk_pll *pll;
1428 struct clk *clk;
1429
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001430 pll_params->flags |= TEGRA_PLL_BYPASS;
1431 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1432 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001433 if (IS_ERR(pll))
1434 return ERR_CAST(pll);
1435
1436 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1437 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301438 if (IS_ERR(clk))
1439 kfree(pll);
1440
1441 return clk;
1442}
1443
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301444struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1445 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001446 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1447 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301448{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001449 struct tegra_clk_pll *pll;
1450 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001451
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001452 pll_params->flags |= TEGRA_PLL_LOCK_MISC | TEGRA_PLL_BYPASS;
1453 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1454 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001455 if (IS_ERR(pll))
1456 return ERR_CAST(pll);
1457
1458 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1459 &tegra_clk_plle_ops);
1460 if (IS_ERR(clk))
1461 kfree(pll);
1462
1463 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301464}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001465
Peter De Schrijver798e9102013-09-09 13:22:55 +03001466#if defined(CONFIG_ARCH_TEGRA_114_SOC) || defined(CONFIG_ARCH_TEGRA_124_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301467static const struct clk_ops tegra_clk_pllxc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001468 .is_enabled = clk_pll_is_enabled,
1469 .enable = clk_pll_iddq_enable,
1470 .disable = clk_pll_iddq_disable,
1471 .recalc_rate = clk_pll_recalc_rate,
1472 .round_rate = clk_pll_ramp_round_rate,
1473 .set_rate = clk_pllxc_set_rate,
1474};
1475
Sachin Kamate47e12f2013-10-08 16:47:41 +05301476static const struct clk_ops tegra_clk_pllm_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001477 .is_enabled = clk_pll_is_enabled,
1478 .enable = clk_pll_iddq_enable,
1479 .disable = clk_pll_iddq_disable,
1480 .recalc_rate = clk_pll_recalc_rate,
1481 .round_rate = clk_pll_ramp_round_rate,
1482 .set_rate = clk_pllm_set_rate,
1483};
1484
Sachin Kamate47e12f2013-10-08 16:47:41 +05301485static const struct clk_ops tegra_clk_pllc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001486 .is_enabled = clk_pll_is_enabled,
1487 .enable = clk_pllc_enable,
1488 .disable = clk_pllc_disable,
1489 .recalc_rate = clk_pll_recalc_rate,
1490 .round_rate = clk_pll_ramp_round_rate,
1491 .set_rate = clk_pllc_set_rate,
1492};
1493
Sachin Kamate47e12f2013-10-08 16:47:41 +05301494static const struct clk_ops tegra_clk_pllre_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001495 .is_enabled = clk_pll_is_enabled,
1496 .enable = clk_pll_iddq_enable,
1497 .disable = clk_pll_iddq_disable,
1498 .recalc_rate = clk_pllre_recalc_rate,
1499 .round_rate = clk_pllre_round_rate,
1500 .set_rate = clk_pllre_set_rate,
1501};
1502
Sachin Kamate47e12f2013-10-08 16:47:41 +05301503static const struct clk_ops tegra_clk_plle_tegra114_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001504 .is_enabled = clk_pll_is_enabled,
1505 .enable = clk_plle_tegra114_enable,
1506 .disable = clk_plle_tegra114_disable,
1507 .recalc_rate = clk_pll_recalc_rate,
1508};
1509
1510
1511struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1512 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001513 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001514 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001515 spinlock_t *lock)
1516{
1517 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001518 struct clk *clk, *parent;
1519 unsigned long parent_rate;
1520 int err;
1521 u32 val, val_iddq;
1522
1523 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001524 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001525 WARN(1, "parent clk %s of %s must be registered first\n",
1526 name, parent_name);
1527 return ERR_PTR(-EINVAL);
1528 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001529
1530 if (!pll_params->pdiv_tohw)
1531 return ERR_PTR(-EINVAL);
1532
Peter De Schrijver04edb092013-09-06 14:37:37 +03001533 parent_rate = __clk_get_rate(parent);
1534
1535 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1536
1537 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
1538 if (err)
1539 return ERR_PTR(err);
1540
1541 val = readl_relaxed(clk_base + pll_params->base_reg);
1542 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
1543
1544 if (val & PLL_BASE_ENABLE)
1545 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
1546 else {
1547 val_iddq |= BIT(pll_params->iddq_bit_idx);
1548 writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
1549 }
1550
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001551 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1552 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001553 if (IS_ERR(pll))
1554 return ERR_CAST(pll);
1555
1556 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1557 &tegra_clk_pllxc_ops);
1558 if (IS_ERR(clk))
1559 kfree(pll);
1560
1561 return clk;
1562}
1563
1564struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1565 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001566 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001567 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001568 spinlock_t *lock, unsigned long parent_rate)
1569{
1570 u32 val;
1571 struct tegra_clk_pll *pll;
1572 struct clk *clk;
1573
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001574 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE | TEGRA_PLL_LOCK_MISC;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001575
1576 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1577
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001578 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001579 if (IS_ERR(pll))
1580 return ERR_CAST(pll);
1581
1582 /* program minimum rate by default */
1583
1584 val = pll_readl_base(pll);
1585 if (val & PLL_BASE_ENABLE)
1586 WARN_ON(val & pll_params->iddq_bit_idx);
1587 else {
1588 int m;
1589
1590 m = _pll_fixed_mdiv(pll_params, parent_rate);
1591 val = m << PLL_BASE_DIVM_SHIFT;
1592 val |= (pll_params->vco_min / parent_rate)
1593 << PLL_BASE_DIVN_SHIFT;
1594 pll_writel_base(val, pll);
1595 }
1596
1597 /* disable lock override */
1598
1599 val = pll_readl_misc(pll);
1600 val &= ~BIT(29);
1601 pll_writel_misc(val, pll);
1602
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001603 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1604 &tegra_clk_pllre_ops);
1605 if (IS_ERR(clk))
1606 kfree(pll);
1607
1608 return clk;
1609}
1610
1611struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1612 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001613 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001614 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001615 spinlock_t *lock)
1616{
1617 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001618 struct clk *clk, *parent;
1619 unsigned long parent_rate;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001620
1621 if (!pll_params->pdiv_tohw)
1622 return ERR_PTR(-EINVAL);
1623
Peter De Schrijver04edb092013-09-06 14:37:37 +03001624 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001625 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001626 WARN(1, "parent clk %s of %s must be registered first\n",
1627 name, parent_name);
1628 return ERR_PTR(-EINVAL);
1629 }
1630
1631 parent_rate = __clk_get_rate(parent);
1632
1633 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1634
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001635 pll_params->flags |= TEGRA_PLL_BYPASS;
1636 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1637 pll_params->flags |= TEGRA_PLLM;
1638 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001639 if (IS_ERR(pll))
1640 return ERR_CAST(pll);
1641
1642 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1643 &tegra_clk_pllm_ops);
1644 if (IS_ERR(clk))
1645 kfree(pll);
1646
1647 return clk;
1648}
1649
1650struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1651 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001652 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001653 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001654 spinlock_t *lock)
1655{
1656 struct clk *parent, *clk;
1657 struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
1658 struct tegra_clk_pll *pll;
1659 struct tegra_clk_pll_freq_table cfg;
1660 unsigned long parent_rate;
1661
1662 if (!p_tohw)
1663 return ERR_PTR(-EINVAL);
1664
1665 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001666 if (!parent) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001667 WARN(1, "parent clk %s of %s must be registered first\n",
1668 name, parent_name);
1669 return ERR_PTR(-EINVAL);
1670 }
1671
Peter De Schrijver04edb092013-09-06 14:37:37 +03001672 parent_rate = __clk_get_rate(parent);
1673
1674 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1675
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001676 pll_params->flags |= TEGRA_PLL_BYPASS;
1677 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001678 if (IS_ERR(pll))
1679 return ERR_CAST(pll);
1680
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001681 /*
1682 * Most of PLLC register fields are shadowed, and can not be read
1683 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1684 * Initialize PLL to default state: disabled, reset; shadow registers
1685 * loaded with default parameters; dividers are preset for half of
1686 * minimum VCO rate (the latter assured that shadowed divider settings
1687 * are within supported range).
1688 */
1689
1690 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1691 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1692
1693 while (p_tohw->pdiv) {
1694 if (p_tohw->pdiv == 2) {
1695 cfg.p = p_tohw->hw_val;
1696 break;
1697 }
1698 p_tohw++;
1699 }
1700
1701 if (!p_tohw->pdiv) {
1702 WARN_ON(1);
1703 return ERR_PTR(-EINVAL);
1704 }
1705
1706 pll_writel_base(0, pll);
1707 _update_pll_mnp(pll, &cfg);
1708
1709 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1710 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1711 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1712 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1713
1714 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1715
1716 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1717 &tegra_clk_pllc_ops);
1718 if (IS_ERR(clk))
1719 kfree(pll);
1720
1721 return clk;
1722}
1723
1724struct clk *tegra_clk_register_plle_tegra114(const char *name,
1725 const char *parent_name,
1726 void __iomem *clk_base, unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001727 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001728 spinlock_t *lock)
1729{
1730 struct tegra_clk_pll *pll;
1731 struct clk *clk;
1732 u32 val, val_aux;
1733
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001734 pll_params->flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1735 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001736 if (IS_ERR(pll))
1737 return ERR_CAST(pll);
1738
1739 /* ensure parent is set to pll_re_vco */
1740
1741 val = pll_readl_base(pll);
1742 val_aux = pll_readl(pll_params->aux_reg, pll);
1743
1744 if (val & PLL_BASE_ENABLE) {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001745 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
1746 (val_aux & PLLE_AUX_PLLP_SEL))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001747 WARN(1, "pll_e enabled with unsupported parent %s\n",
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001748 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
1749 "pll_re_vco");
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001750 } else {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001751 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
Tuomas Tynkkynend2c834a2014-05-16 16:50:20 +03001752 pll_writel(val_aux, pll_params->aux_reg, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001753 }
1754
1755 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1756 &tegra_clk_plle_tegra114_ops);
1757 if (IS_ERR(clk))
1758 kfree(pll);
1759
1760 return clk;
1761}
1762#endif
Peter De Schrijver798e9102013-09-09 13:22:55 +03001763
1764#ifdef CONFIG_ARCH_TEGRA_124_SOC
Sachin Kamate47e12f2013-10-08 16:47:41 +05301765static const struct clk_ops tegra_clk_pllss_ops = {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001766 .is_enabled = clk_pll_is_enabled,
1767 .enable = clk_pll_iddq_enable,
1768 .disable = clk_pll_iddq_disable,
1769 .recalc_rate = clk_pll_recalc_rate,
1770 .round_rate = clk_pll_ramp_round_rate,
1771 .set_rate = clk_pllxc_set_rate,
1772};
1773
1774struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
1775 void __iomem *clk_base, unsigned long flags,
1776 struct tegra_clk_pll_params *pll_params,
1777 spinlock_t *lock)
1778{
1779 struct tegra_clk_pll *pll;
1780 struct clk *clk, *parent;
1781 struct tegra_clk_pll_freq_table cfg;
1782 unsigned long parent_rate;
1783 u32 val;
1784 int i;
1785
1786 if (!pll_params->div_nmp)
1787 return ERR_PTR(-EINVAL);
1788
1789 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001790 if (!parent) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001791 WARN(1, "parent clk %s of %s must be registered first\n",
1792 name, parent_name);
1793 return ERR_PTR(-EINVAL);
1794 }
1795
1796 pll_params->flags = TEGRA_PLL_HAS_LOCK_ENABLE | TEGRA_PLL_USE_LOCK;
1797 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1798 if (IS_ERR(pll))
1799 return ERR_CAST(pll);
1800
1801 val = pll_readl_base(pll);
1802 val &= ~PLLSS_REF_SRC_SEL_MASK;
1803 pll_writel_base(val, pll);
1804
1805 parent_rate = __clk_get_rate(parent);
1806
1807 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1808
1809 /* initialize PLL to minimum rate */
1810
1811 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1812 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1813
1814 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
1815 ;
1816 if (!i) {
1817 kfree(pll);
1818 return ERR_PTR(-EINVAL);
1819 }
1820
1821 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
1822
1823 _update_pll_mnp(pll, &cfg);
1824
1825 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
1826 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
1827 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
1828 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
1829
1830 val = pll_readl_base(pll);
1831 if (val & PLL_BASE_ENABLE) {
1832 if (val & BIT(pll_params->iddq_bit_idx)) {
1833 WARN(1, "%s is on but IDDQ set\n", name);
1834 kfree(pll);
1835 return ERR_PTR(-EINVAL);
1836 }
1837 } else
1838 val |= BIT(pll_params->iddq_bit_idx);
1839
1840 val &= ~PLLSS_LOCK_OVERRIDE;
1841 pll_writel_base(val, pll);
1842
1843 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1844 &tegra_clk_pllss_ops);
1845
1846 if (IS_ERR(clk))
1847 kfree(pll);
1848
1849 return clk;
1850}
1851#endif