blob: b8b3fc6dc39b0f58d40938e79971e9a546c42e32 [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>
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053021#include <linux/clk.h>
Stephen Boyd584ac4e2015-06-19 15:00:46 -070022#include <linux/clk-provider.h>
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053023
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
Thierry Redingd0f02ce2014-04-04 15:55:13 +020061#define PLLE_BASE_DIVCML_MASK 0xf
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053062#define PLLE_BASE_DIVP_SHIFT 16
Thierry Redingd0f02ce2014-04-04 15:55:13 +020063#define PLLE_BASE_DIVP_WIDTH 6
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053064#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
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300113#define SATA_PLL_CFG0 0x490
114#define SATA_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +0200115#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)
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300118
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300119#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)
126
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
151
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530152#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)
155
Peter De Schrijver798e9102013-09-09 13:22:55 +0300156#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)
185
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530186#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)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300189#define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400190#define pll_readl_sdm_din(p) pll_readl(p->params->sdm_din_reg, p)
191#define pll_readl_sdm_ctrl(p) pll_readl(p->params->sdm_ctrl_reg, p)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530192
193#define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
194#define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
195#define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300196#define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400197#define pll_writel_sdm_din(val, p) pll_writel(val, p->params->sdm_din_reg, p)
198#define pll_writel_sdm_ctrl(val, p) pll_writel(val, p->params->sdm_ctrl_reg, p)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530199
200#define mask(w) ((1 << (w)) - 1)
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300201#define divm_mask(p) mask(p->params->div_nmp->divm_width)
202#define divn_mask(p) mask(p->params->div_nmp->divn_width)
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300203#define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300204 mask(p->params->div_nmp->divp_width))
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400205#define sdm_din_mask(p) p->params->sdm_din_mask
206#define sdm_en_mask(p) p->params->sdm_ctrl_en_mask
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530207
Thierry Redingc61e4e72014-04-04 15:55:14 +0200208#define divm_shift(p) (p)->params->div_nmp->divm_shift
209#define divn_shift(p) (p)->params->div_nmp->divn_shift
210#define divp_shift(p) (p)->params->div_nmp->divp_shift
211
212#define divm_mask_shifted(p) (divm_mask(p) << divm_shift(p))
213#define divn_mask_shifted(p) (divn_mask(p) << divn_shift(p))
214#define divp_mask_shifted(p) (divp_mask(p) << divp_shift(p))
215
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530216#define divm_max(p) (divm_mask(p))
217#define divn_max(p) (divn_mask(p))
218#define divp_max(p) (1 << (divp_mask(p)))
219
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400220#define sdin_din_to_data(din) ((u16)((din) ? : 0xFFFFU))
221#define sdin_data_to_din(dat) (((dat) == 0xFFFFU) ? 0 : (s16)dat)
222
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300223static struct div_nmp default_nmp = {
224 .divn_shift = PLL_BASE_DIVN_SHIFT,
225 .divn_width = PLL_BASE_DIVN_WIDTH,
226 .divm_shift = PLL_BASE_DIVM_SHIFT,
227 .divm_width = PLL_BASE_DIVM_WIDTH,
228 .divp_shift = PLL_BASE_DIVP_SHIFT,
229 .divp_width = PLL_BASE_DIVP_WIDTH,
230};
231
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530232static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
233{
234 u32 val;
235
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300236 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530237 return;
238
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300239 if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
Peter De Schrijver7ba28812013-04-03 17:40:38 +0300240 return;
241
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530242 val = pll_readl_misc(pll);
243 val |= BIT(pll->params->lock_enable_bit_idx);
244 pll_writel_misc(val, pll);
245}
246
Peter De Schrijverdba40722013-04-03 17:40:36 +0300247static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530248{
249 int i;
Peter De Schrijver3e727712013-04-03 17:40:40 +0300250 u32 val, lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300251 void __iomem *lock_addr;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530252
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300253 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530254 udelay(pll->params->lock_delay);
255 return 0;
256 }
257
Peter De Schrijverdba40722013-04-03 17:40:36 +0300258 lock_addr = pll->clk_base;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300259 if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300260 lock_addr += pll->params->misc_reg;
261 else
262 lock_addr += pll->params->base_reg;
263
Peter De Schrijver3e727712013-04-03 17:40:40 +0300264 lock_mask = pll->params->lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300265
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530266 for (i = 0; i < pll->params->lock_delay; i++) {
267 val = readl_relaxed(lock_addr);
Peter De Schrijver3e727712013-04-03 17:40:40 +0300268 if ((val & lock_mask) == lock_mask) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530269 udelay(PLL_POST_LOCK_DELAY);
270 return 0;
271 }
272 udelay(2); /* timeout = 2 * lock time */
273 }
274
275 pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700276 clk_hw_get_name(&pll->hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530277
278 return -1;
279}
280
Rhyland Klein6583a632015-06-18 17:28:19 -0400281int tegra_pll_wait_for_lock(struct tegra_clk_pll *pll)
282{
283 return clk_pll_wait_for_lock(pll);
284}
285
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530286static int clk_pll_is_enabled(struct clk_hw *hw)
287{
288 struct tegra_clk_pll *pll = to_clk_pll(hw);
289 u32 val;
290
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300291 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530292 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
293 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
294 return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
295 }
296
297 val = pll_readl_base(pll);
298
299 return val & PLL_BASE_ENABLE ? 1 : 0;
300}
301
Peter De Schrijverdba40722013-04-03 17:40:36 +0300302static void _clk_pll_enable(struct clk_hw *hw)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530303{
304 struct tegra_clk_pll *pll = to_clk_pll(hw);
305 u32 val;
306
Rhyland Klein7db864c2015-06-18 17:28:20 -0400307 if (pll->params->iddq_reg) {
308 val = pll_readl(pll->params->iddq_reg, pll);
309 val &= ~BIT(pll->params->iddq_bit_idx);
310 pll_writel(val, pll->params->iddq_reg, pll);
311 udelay(2);
312 }
313
Bill Huangfde207e2015-06-18 17:28:26 -0400314 if (pll->params->reset_reg) {
315 val = pll_readl(pll->params->reset_reg, pll);
316 val &= ~BIT(pll->params->reset_bit_idx);
317 pll_writel(val, pll->params->reset_reg, pll);
318 }
319
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530320 clk_pll_enable_lock(pll);
321
322 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300323 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300324 val &= ~PLL_BASE_BYPASS;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530325 val |= PLL_BASE_ENABLE;
326 pll_writel_base(val, pll);
327
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300328 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530329 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
330 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
331 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
332 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530333}
334
335static void _clk_pll_disable(struct clk_hw *hw)
336{
337 struct tegra_clk_pll *pll = to_clk_pll(hw);
338 u32 val;
339
340 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300341 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300342 val &= ~PLL_BASE_BYPASS;
343 val &= ~PLL_BASE_ENABLE;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530344 pll_writel_base(val, pll);
345
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300346 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530347 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
348 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
349 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
350 }
Rhyland Klein7db864c2015-06-18 17:28:20 -0400351
Bill Huangfde207e2015-06-18 17:28:26 -0400352 if (pll->params->reset_reg) {
353 val = pll_readl(pll->params->reset_reg, pll);
354 val |= BIT(pll->params->reset_bit_idx);
355 pll_writel(val, pll->params->reset_reg, pll);
356 }
357
Rhyland Klein7db864c2015-06-18 17:28:20 -0400358 if (pll->params->iddq_reg) {
359 val = pll_readl(pll->params->iddq_reg, pll);
360 val |= BIT(pll->params->iddq_bit_idx);
361 pll_writel(val, pll->params->iddq_reg, pll);
362 udelay(2);
363 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530364}
365
366static int clk_pll_enable(struct clk_hw *hw)
367{
368 struct tegra_clk_pll *pll = to_clk_pll(hw);
369 unsigned long flags = 0;
370 int ret;
371
372 if (pll->lock)
373 spin_lock_irqsave(pll->lock, flags);
374
Peter De Schrijverdba40722013-04-03 17:40:36 +0300375 _clk_pll_enable(hw);
376
377 ret = clk_pll_wait_for_lock(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530378
379 if (pll->lock)
380 spin_unlock_irqrestore(pll->lock, flags);
381
382 return ret;
383}
384
385static void clk_pll_disable(struct clk_hw *hw)
386{
387 struct tegra_clk_pll *pll = to_clk_pll(hw);
388 unsigned long flags = 0;
389
390 if (pll->lock)
391 spin_lock_irqsave(pll->lock, flags);
392
393 _clk_pll_disable(hw);
394
395 if (pll->lock)
396 spin_unlock_irqrestore(pll->lock, flags);
397}
398
Peter De Schrijver053b5252013-06-05 15:56:41 +0300399static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
400{
401 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100402 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300403
404 if (p_tohw) {
405 while (p_tohw->pdiv) {
406 if (p_div <= p_tohw->pdiv)
407 return p_tohw->hw_val;
408 p_tohw++;
409 }
410 return -EINVAL;
411 }
412 return -EINVAL;
413}
414
415static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
416{
417 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100418 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300419
420 if (p_tohw) {
421 while (p_tohw->pdiv) {
422 if (p_div_hw == p_tohw->hw_val)
423 return p_tohw->pdiv;
424 p_tohw++;
425 }
426 return -EINVAL;
427 }
428
429 return 1 << p_div_hw;
430}
431
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530432static int _get_table_rate(struct clk_hw *hw,
433 struct tegra_clk_pll_freq_table *cfg,
434 unsigned long rate, unsigned long parent_rate)
435{
436 struct tegra_clk_pll *pll = to_clk_pll(hw);
437 struct tegra_clk_pll_freq_table *sel;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400438 int p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530439
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300440 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530441 if (sel->input_rate == parent_rate &&
442 sel->output_rate == rate)
443 break;
444
445 if (sel->input_rate == 0)
446 return -EINVAL;
447
Rhyland Klein86c679a2015-06-18 17:28:34 -0400448 if (pll->params->pdiv_tohw) {
449 p = _p_div_to_hw(hw, sel->p);
450 if (p < 0)
451 return p;
452 } else {
453 p = ilog2(sel->p);
454 }
455
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530456 cfg->input_rate = sel->input_rate;
457 cfg->output_rate = sel->output_rate;
458 cfg->m = sel->m;
459 cfg->n = sel->n;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400460 cfg->p = p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530461 cfg->cpcon = sel->cpcon;
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400462 cfg->sdm_data = sel->sdm_data;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530463
464 return 0;
465}
466
467static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
468 unsigned long rate, unsigned long parent_rate)
469{
470 struct tegra_clk_pll *pll = to_clk_pll(hw);
471 unsigned long cfreq;
472 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300473 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530474
475 switch (parent_rate) {
476 case 12000000:
477 case 26000000:
478 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
479 break;
480 case 13000000:
481 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
482 break;
483 case 16800000:
484 case 19200000:
485 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
486 break;
487 case 9600000:
488 case 28800000:
489 /*
490 * PLL_P_OUT1 rate is not listed in PLLA table
491 */
Thierry Redinge52d7c02015-11-18 14:04:20 +0100492 cfreq = parent_rate / (parent_rate / 1000000);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530493 break;
494 default:
495 pr_err("%s Unexpected reference rate %lu\n",
496 __func__, parent_rate);
497 BUG();
498 }
499
500 /* Raise VCO to guarantee 0.5% accuracy */
501 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
502 cfg->output_rate <<= 1)
503 p_div++;
504
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530505 cfg->m = parent_rate / cfreq;
506 cfg->n = cfg->output_rate / cfreq;
507 cfg->cpcon = OUT_OF_TABLE_CPCON;
508
509 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300510 (1 << p_div) > divp_max(pll)
511 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530512 return -EINVAL;
513 }
514
Thierry Reding00c674e2013-11-18 16:11:35 +0100515 cfg->output_rate >>= p_div;
516
Peter De Schrijver053b5252013-06-05 15:56:41 +0300517 if (pll->params->pdiv_tohw) {
518 ret = _p_div_to_hw(hw, 1 << p_div);
519 if (ret < 0)
520 return ret;
521 else
522 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300523 } else
524 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300525
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530526 return 0;
527}
528
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400529/*
530 * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number
531 * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as
532 * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used
533 * to indicate that SDM is disabled.
534 *
535 * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13
536 */
537static void clk_pll_set_sdm_data(struct clk_hw *hw,
538 struct tegra_clk_pll_freq_table *cfg)
539{
540 struct tegra_clk_pll *pll = to_clk_pll(hw);
541 u32 val;
542 bool enabled;
543
544 if (!pll->params->sdm_din_reg)
545 return;
546
547 if (cfg->sdm_data) {
548 val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll));
549 val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll);
550 pll_writel_sdm_din(val, pll);
551 }
552
553 val = pll_readl_sdm_ctrl(pll);
554 enabled = (val & sdm_en_mask(pll));
555
556 if (cfg->sdm_data == 0 && enabled)
557 val &= ~pll->params->sdm_ctrl_en_mask;
558
559 if (cfg->sdm_data != 0 && !enabled)
560 val |= pll->params->sdm_ctrl_en_mask;
561
562 pll_writel_sdm_ctrl(val, pll);
563}
564
Peter De Schrijverdba40722013-04-03 17:40:36 +0300565static void _update_pll_mnp(struct tegra_clk_pll *pll,
566 struct tegra_clk_pll_freq_table *cfg)
567{
568 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300569 struct tegra_clk_pll_params *params = pll->params;
570 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300571
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300572 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300573 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
574 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
575 val = pll_override_readl(params->pmc_divp_reg, pll);
576 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
577 val |= cfg->p << div_nmp->override_divp_shift;
578 pll_override_writel(val, params->pmc_divp_reg, pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300579
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300580 val = pll_override_readl(params->pmc_divnm_reg, pll);
581 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
582 ~(divn_mask(pll) << div_nmp->override_divn_shift);
583 val |= (cfg->m << div_nmp->override_divm_shift) |
584 (cfg->n << div_nmp->override_divn_shift);
585 pll_override_writel(val, params->pmc_divnm_reg, pll);
586 } else {
587 val = pll_readl_base(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300588
Thierry Redingc61e4e72014-04-04 15:55:14 +0200589 val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
590 divp_mask_shifted(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300591
Thierry Redingc61e4e72014-04-04 15:55:14 +0200592 val |= (cfg->m << divm_shift(pll)) |
593 (cfg->n << divn_shift(pll)) |
594 (cfg->p << divp_shift(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300595
596 pll_writel_base(val, pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400597
598 clk_pll_set_sdm_data(&pll->hw, cfg);
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300599 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300600}
601
602static void _get_pll_mnp(struct tegra_clk_pll *pll,
603 struct tegra_clk_pll_freq_table *cfg)
604{
605 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300606 struct tegra_clk_pll_params *params = pll->params;
607 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300608
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300609 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300610 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
611 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
612 val = pll_override_readl(params->pmc_divp_reg, pll);
613 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300614
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300615 val = pll_override_readl(params->pmc_divnm_reg, pll);
616 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
617 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
618 } else {
619 val = pll_readl_base(pll);
620
621 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
622 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
623 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400624
625 if (pll->params->sdm_din_reg) {
626 if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) {
627 val = pll_readl_sdm_din(pll);
628 val &= sdm_din_mask(pll);
629 cfg->sdm_data = sdin_din_to_data(val);
630 }
631 }
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300632 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300633}
634
635static void _update_pll_cpcon(struct tegra_clk_pll *pll,
636 struct tegra_clk_pll_freq_table *cfg,
637 unsigned long rate)
638{
639 u32 val;
640
641 val = pll_readl_misc(pll);
642
643 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
644 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
645
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300646 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300647 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
648 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
649 val |= 1 << PLL_MISC_LFCON_SHIFT;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300650 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300651 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
652 if (rate >= (pll->params->vco_max >> 1))
653 val |= 1 << PLL_MISC_DCCON_SHIFT;
654 }
655
656 pll_writel_misc(val, pll);
657}
658
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530659static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
660 unsigned long rate)
661{
662 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300663 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530664
665 state = clk_pll_is_enabled(hw);
666
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530667 if (state)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300668 _clk_pll_disable(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530669
Peter De Schrijverdba40722013-04-03 17:40:36 +0300670 _update_pll_mnp(pll, cfg);
671
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300672 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300673 _update_pll_cpcon(pll, cfg, rate);
674
675 if (state) {
676 _clk_pll_enable(hw);
677 ret = clk_pll_wait_for_lock(pll);
678 }
679
680 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530681}
682
683static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
684 unsigned long parent_rate)
685{
686 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300687 struct tegra_clk_pll_freq_table cfg, old_cfg;
688 unsigned long flags = 0;
689 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530690
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300691 if (pll->params->flags & TEGRA_PLL_FIXED) {
692 if (rate != pll->params->fixed_rate) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530693 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700694 __func__, clk_hw_get_name(hw),
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300695 pll->params->fixed_rate, rate);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530696 return -EINVAL;
697 }
698 return 0;
699 }
700
701 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400702 pll->params->calc_rate(hw, &cfg, rate, parent_rate)) {
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100703 pr_err("%s: Failed to set %s rate %lu\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700704 clk_hw_get_name(hw), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300705 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530706 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300707 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300708 if (pll->lock)
709 spin_lock_irqsave(pll->lock, flags);
710
711 _get_pll_mnp(pll, &old_cfg);
712
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400713 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p ||
714 old_cfg.sdm_data != cfg.sdm_data)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300715 ret = _program_pll(hw, &cfg, rate);
716
717 if (pll->lock)
718 spin_unlock_irqrestore(pll->lock, flags);
719
720 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530721}
722
723static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
724 unsigned long *prate)
725{
726 struct tegra_clk_pll *pll = to_clk_pll(hw);
727 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530728
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300729 if (pll->params->flags & TEGRA_PLL_FIXED)
730 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530731
732 /* PLLM is used for memory; we do not change rate */
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300733 if (pll->params->flags & TEGRA_PLLM)
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -0700734 return clk_hw_get_rate(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530735
736 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400737 pll->params->calc_rate(hw, &cfg, rate, *prate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530738 return -EINVAL;
739
Peter De Schrijver053b5252013-06-05 15:56:41 +0300740 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530741}
742
743static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
744 unsigned long parent_rate)
745{
746 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300747 struct tegra_clk_pll_freq_table cfg;
748 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530749 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300750 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530751
Peter De Schrijverdba40722013-04-03 17:40:36 +0300752 val = pll_readl_base(pll);
753
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300754 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530755 return parent_rate;
756
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300757 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
758 !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530759 struct tegra_clk_pll_freq_table sel;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300760 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
761 parent_rate)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530762 pr_err("Clock %s has unknown fixed frequency\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700763 clk_hw_get_name(hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530764 BUG();
765 }
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300766 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530767 }
768
Peter De Schrijverdba40722013-04-03 17:40:36 +0300769 _get_pll_mnp(pll, &cfg);
770
Peter De Schrijver053b5252013-06-05 15:56:41 +0300771 pdiv = _hw_to_p_div(hw, cfg.p);
772 if (pdiv < 0) {
Rhyland Klein204c85d2015-06-18 17:28:21 -0400773 WARN(1, "Clock %s has invalid pdiv value : 0x%x\n",
774 __clk_get_name(hw->clk), cfg.p);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300775 pdiv = 1;
776 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300777
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400778 if (pll->params->set_gain)
779 pll->params->set_gain(&cfg);
780
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300781 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530782
Peter De Schrijverdba40722013-04-03 17:40:36 +0300783 rate *= cfg.n;
784 do_div(rate, cfg.m);
785
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530786 return rate;
787}
788
789static int clk_plle_training(struct tegra_clk_pll *pll)
790{
791 u32 val;
792 unsigned long timeout;
793
794 if (!pll->pmc)
795 return -ENOSYS;
796
797 /*
798 * PLLE is already disabled, and setup cleared;
799 * create falling edge on PLLE IDDQ input.
800 */
801 val = readl(pll->pmc + PMC_SATA_PWRGT);
802 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
803 writel(val, pll->pmc + PMC_SATA_PWRGT);
804
805 val = readl(pll->pmc + PMC_SATA_PWRGT);
806 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
807 writel(val, pll->pmc + PMC_SATA_PWRGT);
808
809 val = readl(pll->pmc + PMC_SATA_PWRGT);
810 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
811 writel(val, pll->pmc + PMC_SATA_PWRGT);
812
813 val = pll_readl_misc(pll);
814
815 timeout = jiffies + msecs_to_jiffies(100);
816 while (1) {
817 val = pll_readl_misc(pll);
818 if (val & PLLE_MISC_READY)
819 break;
820 if (time_after(jiffies, timeout)) {
821 pr_err("%s: timeout waiting for PLLE\n", __func__);
822 return -EBUSY;
823 }
824 udelay(300);
825 }
826
827 return 0;
828}
829
830static int clk_plle_enable(struct clk_hw *hw)
831{
832 struct tegra_clk_pll *pll = to_clk_pll(hw);
833 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
834 struct tegra_clk_pll_freq_table sel;
835 u32 val;
836 int err;
837
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300838 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530839 return -EINVAL;
840
841 clk_pll_disable(hw);
842
843 val = pll_readl_misc(pll);
844 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
845 pll_writel_misc(val, pll);
846
847 val = pll_readl_misc(pll);
848 if (!(val & PLLE_MISC_READY)) {
849 err = clk_plle_training(pll);
850 if (err)
851 return err;
852 }
853
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300854 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530855 /* configure dividers */
856 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200857 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
858 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200859 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200860 val |= sel.m << divm_shift(pll);
861 val |= sel.n << divn_shift(pll);
862 val |= sel.p << divp_shift(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530863 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
864 pll_writel_base(val, pll);
865 }
866
867 val = pll_readl_misc(pll);
868 val |= PLLE_MISC_SETUP_VALUE;
869 val |= PLLE_MISC_LOCK_ENABLE;
870 pll_writel_misc(val, pll);
871
872 val = readl(pll->clk_base + PLLE_SS_CTRL);
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200873 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530874 val |= PLLE_SS_DISABLE;
875 writel(val, pll->clk_base + PLLE_SS_CTRL);
876
Thierry Reding4ccc4022014-04-04 15:55:15 +0200877 val = pll_readl_base(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530878 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
879 pll_writel_base(val, pll);
880
Peter De Schrijverdba40722013-04-03 17:40:36 +0300881 clk_pll_wait_for_lock(pll);
882
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530883 return 0;
884}
885
886static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
887 unsigned long parent_rate)
888{
889 struct tegra_clk_pll *pll = to_clk_pll(hw);
890 u32 val = pll_readl_base(pll);
891 u32 divn = 0, divm = 0, divp = 0;
892 u64 rate = parent_rate;
893
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300894 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
895 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
896 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530897 divm *= divp;
898
899 rate *= divn;
900 do_div(rate, divm);
901 return rate;
902}
903
904const struct clk_ops tegra_clk_pll_ops = {
905 .is_enabled = clk_pll_is_enabled,
906 .enable = clk_pll_enable,
907 .disable = clk_pll_disable,
908 .recalc_rate = clk_pll_recalc_rate,
909 .round_rate = clk_pll_round_rate,
910 .set_rate = clk_pll_set_rate,
911};
912
913const struct clk_ops tegra_clk_plle_ops = {
914 .recalc_rate = clk_plle_recalc_rate,
915 .is_enabled = clk_pll_is_enabled,
916 .disable = clk_pll_disable,
917 .enable = clk_plle_enable,
918};
919
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300920static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
921 unsigned long parent_rate)
922{
Rhyland Klein407254d2015-06-18 17:28:25 -0400923 u16 mdiv = parent_rate / pll_params->cf_min;
924
925 if (pll_params->flags & TEGRA_MDIV_NEW)
926 return (!pll_params->mdiv_default ? mdiv :
927 min(mdiv, pll_params->mdiv_default));
928
929 if (pll_params->mdiv_default)
930 return pll_params->mdiv_default;
931
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300932 if (parent_rate > pll_params->cf_max)
933 return 2;
934 else
935 return 1;
936}
937
Rhyland Klein86c679a2015-06-18 17:28:34 -0400938static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
939 struct tegra_clk_pll_freq_table *cfg,
940 unsigned long rate, unsigned long parent_rate)
941{
942 struct tegra_clk_pll *pll = to_clk_pll(hw);
943 unsigned int p;
944 int p_div;
945
946 if (!rate)
947 return -EINVAL;
948
949 p = DIV_ROUND_UP(pll->params->vco_min, rate);
950 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
951 cfg->output_rate = rate * p;
952 cfg->n = cfg->output_rate * cfg->m / parent_rate;
953 cfg->input_rate = parent_rate;
954
955 p_div = _p_div_to_hw(hw, p);
956 if (p_div < 0)
957 return p_div;
958
959 cfg->p = p_div;
960
961 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
962 return -EINVAL;
963
964 return 0;
965}
966
967#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
968 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
969 defined(CONFIG_ARCH_TEGRA_132_SOC)
970
Rhyland Klein407254d2015-06-18 17:28:25 -0400971u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate)
972{
973 struct tegra_clk_pll *pll = to_clk_pll(hw);
974
975 return (u16)_pll_fixed_mdiv(pll->params, input_rate);
976}
977
Peter De Schrijver04edb092013-09-06 14:37:37 +0300978static unsigned long _clip_vco_min(unsigned long vco_min,
979 unsigned long parent_rate)
980{
981 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
982}
983
984static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
985 void __iomem *clk_base,
986 unsigned long parent_rate)
987{
988 u32 val;
989 u32 step_a, step_b;
990
991 switch (parent_rate) {
992 case 12000000:
993 case 13000000:
994 case 26000000:
995 step_a = 0x2B;
996 step_b = 0x0B;
997 break;
998 case 16800000:
999 step_a = 0x1A;
1000 step_b = 0x09;
1001 break;
1002 case 19200000:
1003 step_a = 0x12;
1004 step_b = 0x08;
1005 break;
1006 default:
1007 pr_err("%s: Unexpected reference rate %lu\n",
1008 __func__, parent_rate);
1009 WARN_ON(1);
1010 return -EINVAL;
1011 }
1012
1013 val = step_a << pll_params->stepa_shift;
1014 val |= step_b << pll_params->stepb_shift;
1015 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
1016
1017 return 0;
1018}
1019
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001020static int _pll_ramp_calc_pll(struct clk_hw *hw,
1021 struct tegra_clk_pll_freq_table *cfg,
1022 unsigned long rate, unsigned long parent_rate)
1023{
1024 struct tegra_clk_pll *pll = to_clk_pll(hw);
Rhyland Klein86c679a2015-06-18 17:28:34 -04001025 int err = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001026
1027 err = _get_table_rate(hw, cfg, rate, parent_rate);
1028 if (err < 0)
1029 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +03001030 else {
1031 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001032 WARN_ON(1);
1033 err = -EINVAL;
1034 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001035 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001036 }
1037
Peter De Schrijver053b5252013-06-05 15:56:41 +03001038 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001039 err = -EINVAL;
1040
1041out:
1042 return err;
1043}
1044
1045static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
1046 unsigned long parent_rate)
1047{
1048 struct tegra_clk_pll *pll = to_clk_pll(hw);
1049 struct tegra_clk_pll_freq_table cfg, old_cfg;
1050 unsigned long flags = 0;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001051 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001052
1053 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1054 if (ret < 0)
1055 return ret;
1056
1057 if (pll->lock)
1058 spin_lock_irqsave(pll->lock, flags);
1059
1060 _get_pll_mnp(pll, &old_cfg);
1061
Peter De Schrijver053b5252013-06-05 15:56:41 +03001062 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001063 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001064
1065 if (pll->lock)
1066 spin_unlock_irqrestore(pll->lock, flags);
1067
1068 return ret;
1069}
1070
1071static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1072 unsigned long *prate)
1073{
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001074 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001075 struct tegra_clk_pll_freq_table cfg;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001076 int ret, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001077 u64 output_rate = *prate;
1078
1079 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1080 if (ret < 0)
1081 return ret;
1082
Peter De Schrijver053b5252013-06-05 15:56:41 +03001083 p_div = _hw_to_p_div(hw, cfg.p);
1084 if (p_div < 0)
1085 return p_div;
1086
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001087 if (pll->params->set_gain)
1088 pll->params->set_gain(&cfg);
1089
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001090 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001091 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001092
1093 return output_rate;
1094}
1095
1096static int clk_pllm_set_rate(struct clk_hw *hw, unsigned long rate,
1097 unsigned long parent_rate)
1098{
1099 struct tegra_clk_pll_freq_table cfg;
1100 struct tegra_clk_pll *pll = to_clk_pll(hw);
1101 unsigned long flags = 0;
1102 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001103
1104 if (pll->lock)
1105 spin_lock_irqsave(pll->lock, flags);
1106
1107 state = clk_pll_is_enabled(hw);
1108 if (state) {
1109 if (rate != clk_get_rate(hw->clk)) {
1110 pr_err("%s: Cannot change active PLLM\n", __func__);
1111 ret = -EINVAL;
1112 goto out;
1113 }
1114 goto out;
1115 }
1116
1117 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1118 if (ret < 0)
1119 goto out;
1120
Peter De Schrijver408a24f2013-06-06 13:47:31 +03001121 _update_pll_mnp(pll, &cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001122
1123out:
1124 if (pll->lock)
1125 spin_unlock_irqrestore(pll->lock, flags);
1126
1127 return ret;
1128}
1129
1130static void _pllcx_strobe(struct tegra_clk_pll *pll)
1131{
1132 u32 val;
1133
1134 val = pll_readl_misc(pll);
1135 val |= PLLCX_MISC_STROBE;
1136 pll_writel_misc(val, pll);
1137 udelay(2);
1138
1139 val &= ~PLLCX_MISC_STROBE;
1140 pll_writel_misc(val, pll);
1141}
1142
1143static int clk_pllc_enable(struct clk_hw *hw)
1144{
1145 struct tegra_clk_pll *pll = to_clk_pll(hw);
1146 u32 val;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001147 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001148 unsigned long flags = 0;
1149
1150 if (pll->lock)
1151 spin_lock_irqsave(pll->lock, flags);
1152
1153 _clk_pll_enable(hw);
1154 udelay(2);
1155
1156 val = pll_readl_misc(pll);
1157 val &= ~PLLCX_MISC_RESET;
1158 pll_writel_misc(val, pll);
1159 udelay(2);
1160
1161 _pllcx_strobe(pll);
1162
1163 ret = clk_pll_wait_for_lock(pll);
1164
1165 if (pll->lock)
1166 spin_unlock_irqrestore(pll->lock, flags);
1167
1168 return ret;
1169}
1170
1171static void _clk_pllc_disable(struct clk_hw *hw)
1172{
1173 struct tegra_clk_pll *pll = to_clk_pll(hw);
1174 u32 val;
1175
1176 _clk_pll_disable(hw);
1177
1178 val = pll_readl_misc(pll);
1179 val |= PLLCX_MISC_RESET;
1180 pll_writel_misc(val, pll);
1181 udelay(2);
1182}
1183
1184static void clk_pllc_disable(struct clk_hw *hw)
1185{
1186 struct tegra_clk_pll *pll = to_clk_pll(hw);
1187 unsigned long flags = 0;
1188
1189 if (pll->lock)
1190 spin_lock_irqsave(pll->lock, flags);
1191
1192 _clk_pllc_disable(hw);
1193
1194 if (pll->lock)
1195 spin_unlock_irqrestore(pll->lock, flags);
1196}
1197
1198static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1199 unsigned long input_rate, u32 n)
1200{
1201 u32 val, n_threshold;
1202
1203 switch (input_rate) {
1204 case 12000000:
1205 n_threshold = 70;
1206 break;
1207 case 13000000:
1208 case 26000000:
1209 n_threshold = 71;
1210 break;
1211 case 16800000:
1212 n_threshold = 55;
1213 break;
1214 case 19200000:
1215 n_threshold = 48;
1216 break;
1217 default:
1218 pr_err("%s: Unexpected reference rate %lu\n",
1219 __func__, input_rate);
1220 return -EINVAL;
1221 }
1222
1223 val = pll_readl_misc(pll);
1224 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1225 val |= n <= n_threshold ?
1226 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1227 pll_writel_misc(val, pll);
1228
1229 return 0;
1230}
1231
1232static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1233 unsigned long parent_rate)
1234{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001235 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001236 struct tegra_clk_pll *pll = to_clk_pll(hw);
1237 unsigned long flags = 0;
1238 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001239
1240 if (pll->lock)
1241 spin_lock_irqsave(pll->lock, flags);
1242
1243 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1244 if (ret < 0)
1245 goto out;
1246
Peter De Schrijver053b5252013-06-05 15:56:41 +03001247 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001248
Peter De Schrijver053b5252013-06-05 15:56:41 +03001249 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001250 WARN_ON(1);
1251 goto out;
1252 }
1253
Peter De Schrijver053b5252013-06-05 15:56:41 +03001254 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001255 goto out;
1256
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001257 state = clk_pll_is_enabled(hw);
1258 if (state)
1259 _clk_pllc_disable(hw);
1260
1261 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1262 if (ret < 0)
1263 goto out;
1264
1265 _update_pll_mnp(pll, &cfg);
1266
1267 if (state)
1268 ret = clk_pllc_enable(hw);
1269
1270out:
1271 if (pll->lock)
1272 spin_unlock_irqrestore(pll->lock, flags);
1273
1274 return ret;
1275}
1276
1277static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1278 struct tegra_clk_pll_freq_table *cfg,
1279 unsigned long rate, unsigned long parent_rate)
1280{
1281 u16 m, n;
1282 u64 output_rate = parent_rate;
1283
1284 m = _pll_fixed_mdiv(pll->params, parent_rate);
1285 n = rate * m / parent_rate;
1286
1287 output_rate *= n;
1288 do_div(output_rate, m);
1289
1290 if (cfg) {
1291 cfg->m = m;
1292 cfg->n = n;
1293 }
1294
1295 return output_rate;
1296}
Thierry Reding6bb18c52014-08-01 10:44:20 +02001297
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001298static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1299 unsigned long parent_rate)
1300{
1301 struct tegra_clk_pll_freq_table cfg, old_cfg;
1302 struct tegra_clk_pll *pll = to_clk_pll(hw);
1303 unsigned long flags = 0;
1304 int state, ret = 0;
1305
1306 if (pll->lock)
1307 spin_lock_irqsave(pll->lock, flags);
1308
1309 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1310 _get_pll_mnp(pll, &old_cfg);
1311 cfg.p = old_cfg.p;
1312
1313 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1314 state = clk_pll_is_enabled(hw);
1315 if (state)
1316 _clk_pll_disable(hw);
1317
1318 _update_pll_mnp(pll, &cfg);
1319
1320 if (state) {
1321 _clk_pll_enable(hw);
1322 ret = clk_pll_wait_for_lock(pll);
1323 }
1324 }
1325
1326 if (pll->lock)
1327 spin_unlock_irqrestore(pll->lock, flags);
1328
1329 return ret;
1330}
1331
1332static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1333 unsigned long parent_rate)
1334{
1335 struct tegra_clk_pll_freq_table cfg;
1336 struct tegra_clk_pll *pll = to_clk_pll(hw);
1337 u64 rate = parent_rate;
1338
1339 _get_pll_mnp(pll, &cfg);
1340
1341 rate *= cfg.n;
1342 do_div(rate, cfg.m);
1343
1344 return rate;
1345}
1346
1347static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1348 unsigned long *prate)
1349{
1350 struct tegra_clk_pll *pll = to_clk_pll(hw);
1351
1352 return _pllre_calc_rate(pll, NULL, rate, *prate);
1353}
1354
1355static int clk_plle_tegra114_enable(struct clk_hw *hw)
1356{
1357 struct tegra_clk_pll *pll = to_clk_pll(hw);
1358 struct tegra_clk_pll_freq_table sel;
1359 u32 val;
1360 int ret;
1361 unsigned long flags = 0;
1362 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1363
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001364 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001365 return -EINVAL;
1366
1367 if (pll->lock)
1368 spin_lock_irqsave(pll->lock, flags);
1369
1370 val = pll_readl_base(pll);
1371 val &= ~BIT(29); /* Disable lock override */
1372 pll_writel_base(val, pll);
1373
1374 val = pll_readl(pll->params->aux_reg, pll);
1375 val |= PLLE_AUX_ENABLE_SWCTL;
1376 val &= ~PLLE_AUX_SEQ_ENABLE;
1377 pll_writel(val, pll->params->aux_reg, pll);
1378 udelay(1);
1379
1380 val = pll_readl_misc(pll);
1381 val |= PLLE_MISC_LOCK_ENABLE;
1382 val |= PLLE_MISC_IDDQ_SW_CTRL;
1383 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1384 val |= PLLE_MISC_PLLE_PTS;
1385 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1386 pll_writel_misc(val, pll);
1387 udelay(5);
1388
1389 val = pll_readl(PLLE_SS_CTRL, pll);
1390 val |= PLLE_SS_DISABLE;
1391 pll_writel(val, PLLE_SS_CTRL, pll);
1392
1393 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001394 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1395 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001396 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001397 val |= sel.m << divm_shift(pll);
1398 val |= sel.n << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001399 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1400 pll_writel_base(val, pll);
1401 udelay(1);
1402
1403 _clk_pll_enable(hw);
1404 ret = clk_pll_wait_for_lock(pll);
1405
1406 if (ret < 0)
1407 goto out;
1408
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001409 val = pll_readl(PLLE_SS_CTRL, pll);
1410 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1411 val &= ~PLLE_SS_COEFFICIENTS_MASK;
1412 val |= PLLE_SS_COEFFICIENTS_VAL;
1413 pll_writel(val, PLLE_SS_CTRL, pll);
1414 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1415 pll_writel(val, PLLE_SS_CTRL, pll);
1416 udelay(1);
1417 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1418 pll_writel(val, PLLE_SS_CTRL, pll);
1419 udelay(1);
1420
Jim Lin2cfe1672014-05-14 17:32:57 -07001421 /* Enable hw control of xusb brick pll */
1422 val = pll_readl_misc(pll);
1423 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1424 pll_writel_misc(val, pll);
1425
1426 val = pll_readl(pll->params->aux_reg, pll);
1427 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1428 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1429 pll_writel(val, pll->params->aux_reg, pll);
1430 udelay(1);
1431 val |= PLLE_AUX_SEQ_ENABLE;
1432 pll_writel(val, pll->params->aux_reg, pll);
1433
1434 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1435 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1436 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1437 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1438 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1439 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1440 udelay(1);
1441 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1442 pll_writel(val, XUSBIO_PLL_CFG0, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001443
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001444 /* Enable hw control of SATA pll */
1445 val = pll_readl(SATA_PLL_CFG0, pll);
1446 val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +02001447 val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1448 val |= SATA_PLL_CFG0_SEQ_START_STATE;
1449 pll_writel(val, SATA_PLL_CFG0, pll);
1450
1451 udelay(1);
1452
1453 val = pll_readl(SATA_PLL_CFG0, pll);
1454 val |= SATA_PLL_CFG0_SEQ_ENABLE;
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001455 pll_writel(val, SATA_PLL_CFG0, pll);
1456
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001457out:
1458 if (pll->lock)
1459 spin_unlock_irqrestore(pll->lock, flags);
1460
1461 return ret;
1462}
1463
1464static void clk_plle_tegra114_disable(struct clk_hw *hw)
1465{
1466 struct tegra_clk_pll *pll = to_clk_pll(hw);
1467 unsigned long flags = 0;
1468 u32 val;
1469
1470 if (pll->lock)
1471 spin_lock_irqsave(pll->lock, flags);
1472
1473 _clk_pll_disable(hw);
1474
1475 val = pll_readl_misc(pll);
1476 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1477 pll_writel_misc(val, pll);
1478 udelay(1);
1479
1480 if (pll->lock)
1481 spin_unlock_irqrestore(pll->lock, flags);
1482}
1483#endif
1484
Peter De Schrijverdba40722013-04-03 17:40:36 +03001485static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001486 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1487 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301488{
1489 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301490
1491 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1492 if (!pll)
1493 return ERR_PTR(-ENOMEM);
1494
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301495 pll->clk_base = clk_base;
1496 pll->pmc = pmc;
1497
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301498 pll->params = pll_params;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301499 pll->lock = lock;
1500
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001501 if (!pll_params->div_nmp)
1502 pll_params->div_nmp = &default_nmp;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301503
Peter De Schrijverdba40722013-04-03 17:40:36 +03001504 return pll;
1505}
1506
1507static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1508 const char *name, const char *parent_name, unsigned long flags,
1509 const struct clk_ops *ops)
1510{
1511 struct clk_init_data init;
1512
1513 init.name = name;
1514 init.ops = ops;
1515 init.flags = flags;
1516 init.parent_names = (parent_name ? &parent_name : NULL);
1517 init.num_parents = (parent_name ? 1 : 0);
1518
Rhyland Klein407254d2015-06-18 17:28:25 -04001519 /* Default to _calc_rate if unspecified */
Rhyland Klein86c679a2015-06-18 17:28:34 -04001520 if (!pll->params->calc_rate) {
1521 if (pll->params->flags & TEGRA_PLLM)
1522 pll->params->calc_rate = _calc_dynamic_ramp_rate;
1523 else
1524 pll->params->calc_rate = _calc_rate;
1525 }
Rhyland Klein407254d2015-06-18 17:28:25 -04001526
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301527 /* Data in .init is copied by clk_register(), so stack variable OK */
1528 pll->hw.init = &init;
1529
Peter De Schrijverdba40722013-04-03 17:40:36 +03001530 return clk_register(NULL, &pll->hw);
1531}
1532
1533struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1534 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001535 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1536 spinlock_t *lock)
Peter De Schrijverdba40722013-04-03 17:40:36 +03001537{
1538 struct tegra_clk_pll *pll;
1539 struct clk *clk;
1540
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001541 pll_params->flags |= TEGRA_PLL_BYPASS;
Rhyland Klein3706b432015-06-18 17:28:23 -04001542
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001543 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001544 if (IS_ERR(pll))
1545 return ERR_CAST(pll);
1546
1547 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1548 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301549 if (IS_ERR(clk))
1550 kfree(pll);
1551
1552 return clk;
1553}
1554
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001555static struct div_nmp pll_e_nmp = {
1556 .divn_shift = PLLE_BASE_DIVN_SHIFT,
1557 .divn_width = PLLE_BASE_DIVN_WIDTH,
1558 .divm_shift = PLLE_BASE_DIVM_SHIFT,
1559 .divm_width = PLLE_BASE_DIVM_WIDTH,
1560 .divp_shift = PLLE_BASE_DIVP_SHIFT,
1561 .divp_width = PLLE_BASE_DIVP_WIDTH,
1562};
1563
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301564struct clk *tegra_clk_register_plle(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, struct tegra_clk_pll_params *pll_params,
1567 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301568{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001569 struct tegra_clk_pll *pll;
1570 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001571
Rhyland Klein3706b432015-06-18 17:28:23 -04001572 pll_params->flags |= TEGRA_PLL_BYPASS;
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001573
1574 if (!pll_params->div_nmp)
1575 pll_params->div_nmp = &pll_e_nmp;
1576
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001577 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001578 if (IS_ERR(pll))
1579 return ERR_CAST(pll);
1580
1581 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1582 &tegra_clk_plle_ops);
1583 if (IS_ERR(clk))
1584 kfree(pll);
1585
1586 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301587}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001588
Paul Walmsley08acae32014-12-16 12:38:29 -08001589#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1590 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
1591 defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301592static const struct clk_ops tegra_clk_pllxc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001593 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001594 .enable = clk_pll_enable,
1595 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001596 .recalc_rate = clk_pll_recalc_rate,
1597 .round_rate = clk_pll_ramp_round_rate,
1598 .set_rate = clk_pllxc_set_rate,
1599};
1600
Sachin Kamate47e12f2013-10-08 16:47:41 +05301601static const struct clk_ops tegra_clk_pllm_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001602 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001603 .enable = clk_pll_enable,
1604 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001605 .recalc_rate = clk_pll_recalc_rate,
1606 .round_rate = clk_pll_ramp_round_rate,
1607 .set_rate = clk_pllm_set_rate,
1608};
1609
Sachin Kamate47e12f2013-10-08 16:47:41 +05301610static const struct clk_ops tegra_clk_pllc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001611 .is_enabled = clk_pll_is_enabled,
1612 .enable = clk_pllc_enable,
1613 .disable = clk_pllc_disable,
1614 .recalc_rate = clk_pll_recalc_rate,
1615 .round_rate = clk_pll_ramp_round_rate,
1616 .set_rate = clk_pllc_set_rate,
1617};
1618
Sachin Kamate47e12f2013-10-08 16:47:41 +05301619static const struct clk_ops tegra_clk_pllre_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001620 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001621 .enable = clk_pll_enable,
1622 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001623 .recalc_rate = clk_pllre_recalc_rate,
1624 .round_rate = clk_pllre_round_rate,
1625 .set_rate = clk_pllre_set_rate,
1626};
1627
Sachin Kamate47e12f2013-10-08 16:47:41 +05301628static const struct clk_ops tegra_clk_plle_tegra114_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001629 .is_enabled = clk_pll_is_enabled,
1630 .enable = clk_plle_tegra114_enable,
1631 .disable = clk_plle_tegra114_disable,
1632 .recalc_rate = clk_pll_recalc_rate,
1633};
1634
1635
1636struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1637 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001638 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001639 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001640 spinlock_t *lock)
1641{
1642 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001643 struct clk *clk, *parent;
1644 unsigned long parent_rate;
1645 int err;
1646 u32 val, val_iddq;
1647
1648 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001649 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001650 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001651 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001652 return ERR_PTR(-EINVAL);
1653 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001654
1655 if (!pll_params->pdiv_tohw)
1656 return ERR_PTR(-EINVAL);
1657
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001658 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001659
1660 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1661
1662 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
1663 if (err)
1664 return ERR_PTR(err);
1665
1666 val = readl_relaxed(clk_base + pll_params->base_reg);
1667 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
1668
1669 if (val & PLL_BASE_ENABLE)
1670 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
1671 else {
1672 val_iddq |= BIT(pll_params->iddq_bit_idx);
1673 writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
1674 }
1675
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001676 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001677 if (IS_ERR(pll))
1678 return ERR_CAST(pll);
1679
1680 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1681 &tegra_clk_pllxc_ops);
1682 if (IS_ERR(clk))
1683 kfree(pll);
1684
1685 return clk;
1686}
1687
1688struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1689 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001690 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001691 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001692 spinlock_t *lock, unsigned long parent_rate)
1693{
1694 u32 val;
1695 struct tegra_clk_pll *pll;
1696 struct clk *clk;
1697
Peter De Schrijver04edb092013-09-06 14:37:37 +03001698 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1699
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001700 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001701 if (IS_ERR(pll))
1702 return ERR_CAST(pll);
1703
1704 /* program minimum rate by default */
1705
1706 val = pll_readl_base(pll);
1707 if (val & PLL_BASE_ENABLE)
1708 WARN_ON(val & pll_params->iddq_bit_idx);
1709 else {
1710 int m;
1711
1712 m = _pll_fixed_mdiv(pll_params, parent_rate);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001713 val = m << divm_shift(pll);
1714 val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001715 pll_writel_base(val, pll);
1716 }
1717
1718 /* disable lock override */
1719
1720 val = pll_readl_misc(pll);
1721 val &= ~BIT(29);
1722 pll_writel_misc(val, pll);
1723
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001724 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1725 &tegra_clk_pllre_ops);
1726 if (IS_ERR(clk))
1727 kfree(pll);
1728
1729 return clk;
1730}
1731
1732struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1733 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001734 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001735 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001736 spinlock_t *lock)
1737{
1738 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001739 struct clk *clk, *parent;
1740 unsigned long parent_rate;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001741
1742 if (!pll_params->pdiv_tohw)
1743 return ERR_PTR(-EINVAL);
1744
Peter De Schrijver04edb092013-09-06 14:37:37 +03001745 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001746 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001747 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001748 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001749 return ERR_PTR(-EINVAL);
1750 }
1751
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001752 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001753
1754 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1755
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001756 pll_params->flags |= TEGRA_PLL_BYPASS;
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001757 pll_params->flags |= TEGRA_PLLM;
1758 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001759 if (IS_ERR(pll))
1760 return ERR_CAST(pll);
1761
1762 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1763 &tegra_clk_pllm_ops);
1764 if (IS_ERR(clk))
1765 kfree(pll);
1766
1767 return clk;
1768}
1769
1770struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1771 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001772 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001773 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001774 spinlock_t *lock)
1775{
1776 struct clk *parent, *clk;
Thierry Reding385f9ad2015-11-19 16:34:06 +01001777 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001778 struct tegra_clk_pll *pll;
1779 struct tegra_clk_pll_freq_table cfg;
1780 unsigned long parent_rate;
1781
1782 if (!p_tohw)
1783 return ERR_PTR(-EINVAL);
1784
1785 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001786 if (!parent) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001787 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001788 parent_name, name);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001789 return ERR_PTR(-EINVAL);
1790 }
1791
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001792 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001793
1794 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1795
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001796 pll_params->flags |= TEGRA_PLL_BYPASS;
1797 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001798 if (IS_ERR(pll))
1799 return ERR_CAST(pll);
1800
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001801 /*
1802 * Most of PLLC register fields are shadowed, and can not be read
1803 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1804 * Initialize PLL to default state: disabled, reset; shadow registers
1805 * loaded with default parameters; dividers are preset for half of
1806 * minimum VCO rate (the latter assured that shadowed divider settings
1807 * are within supported range).
1808 */
1809
1810 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1811 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1812
1813 while (p_tohw->pdiv) {
1814 if (p_tohw->pdiv == 2) {
1815 cfg.p = p_tohw->hw_val;
1816 break;
1817 }
1818 p_tohw++;
1819 }
1820
1821 if (!p_tohw->pdiv) {
1822 WARN_ON(1);
1823 return ERR_PTR(-EINVAL);
1824 }
1825
1826 pll_writel_base(0, pll);
1827 _update_pll_mnp(pll, &cfg);
1828
1829 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1830 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1831 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1832 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1833
1834 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1835
1836 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1837 &tegra_clk_pllc_ops);
1838 if (IS_ERR(clk))
1839 kfree(pll);
1840
1841 return clk;
1842}
1843
1844struct clk *tegra_clk_register_plle_tegra114(const char *name,
1845 const char *parent_name,
1846 void __iomem *clk_base, unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001847 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001848 spinlock_t *lock)
1849{
1850 struct tegra_clk_pll *pll;
1851 struct clk *clk;
1852 u32 val, val_aux;
1853
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001854 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001855 if (IS_ERR(pll))
1856 return ERR_CAST(pll);
1857
1858 /* ensure parent is set to pll_re_vco */
1859
1860 val = pll_readl_base(pll);
1861 val_aux = pll_readl(pll_params->aux_reg, pll);
1862
1863 if (val & PLL_BASE_ENABLE) {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001864 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
1865 (val_aux & PLLE_AUX_PLLP_SEL))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001866 WARN(1, "pll_e enabled with unsupported parent %s\n",
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001867 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
1868 "pll_re_vco");
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001869 } else {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001870 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
Tuomas Tynkkynend2c834a2014-05-16 16:50:20 +03001871 pll_writel(val_aux, pll_params->aux_reg, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001872 }
1873
1874 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1875 &tegra_clk_plle_tegra114_ops);
1876 if (IS_ERR(clk))
1877 kfree(pll);
1878
1879 return clk;
1880}
1881#endif
Peter De Schrijver798e9102013-09-09 13:22:55 +03001882
Paul Walmsley08acae32014-12-16 12:38:29 -08001883#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301884static const struct clk_ops tegra_clk_pllss_ops = {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001885 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001886 .enable = clk_pll_enable,
1887 .disable = clk_pll_disable,
Peter De Schrijver798e9102013-09-09 13:22:55 +03001888 .recalc_rate = clk_pll_recalc_rate,
1889 .round_rate = clk_pll_ramp_round_rate,
1890 .set_rate = clk_pllxc_set_rate,
1891};
1892
1893struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
1894 void __iomem *clk_base, unsigned long flags,
1895 struct tegra_clk_pll_params *pll_params,
1896 spinlock_t *lock)
1897{
1898 struct tegra_clk_pll *pll;
1899 struct clk *clk, *parent;
1900 struct tegra_clk_pll_freq_table cfg;
1901 unsigned long parent_rate;
1902 u32 val;
1903 int i;
1904
1905 if (!pll_params->div_nmp)
1906 return ERR_PTR(-EINVAL);
1907
1908 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001909 if (!parent) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001910 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001911 parent_name, name);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001912 return ERR_PTR(-EINVAL);
1913 }
1914
Peter De Schrijver798e9102013-09-09 13:22:55 +03001915 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1916 if (IS_ERR(pll))
1917 return ERR_CAST(pll);
1918
1919 val = pll_readl_base(pll);
1920 val &= ~PLLSS_REF_SRC_SEL_MASK;
1921 pll_writel_base(val, pll);
1922
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001923 parent_rate = clk_get_rate(parent);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001924
1925 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1926
1927 /* initialize PLL to minimum rate */
1928
1929 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1930 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1931
1932 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
1933 ;
1934 if (!i) {
1935 kfree(pll);
1936 return ERR_PTR(-EINVAL);
1937 }
1938
1939 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
1940
1941 _update_pll_mnp(pll, &cfg);
1942
1943 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
1944 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
1945 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
1946 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
1947
1948 val = pll_readl_base(pll);
1949 if (val & PLL_BASE_ENABLE) {
1950 if (val & BIT(pll_params->iddq_bit_idx)) {
1951 WARN(1, "%s is on but IDDQ set\n", name);
1952 kfree(pll);
1953 return ERR_PTR(-EINVAL);
1954 }
1955 } else
1956 val |= BIT(pll_params->iddq_bit_idx);
1957
1958 val &= ~PLLSS_LOCK_OVERRIDE;
1959 pll_writel_base(val, pll);
1960
1961 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1962 &tegra_clk_pllss_ops);
1963
1964 if (IS_ERR(clk))
1965 kfree(pll);
1966
1967 return clk;
1968}
1969#endif