blob: c645a899deba5a5787f03be62ace94386a0f2b6b [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;
438
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300439 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530440 if (sel->input_rate == parent_rate &&
441 sel->output_rate == rate)
442 break;
443
444 if (sel->input_rate == 0)
445 return -EINVAL;
446
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530447 cfg->input_rate = sel->input_rate;
448 cfg->output_rate = sel->output_rate;
449 cfg->m = sel->m;
450 cfg->n = sel->n;
451 cfg->p = sel->p;
452 cfg->cpcon = sel->cpcon;
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400453 cfg->sdm_data = sel->sdm_data;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530454
455 return 0;
456}
457
458static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
459 unsigned long rate, unsigned long parent_rate)
460{
461 struct tegra_clk_pll *pll = to_clk_pll(hw);
462 unsigned long cfreq;
463 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300464 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530465
466 switch (parent_rate) {
467 case 12000000:
468 case 26000000:
469 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
470 break;
471 case 13000000:
472 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
473 break;
474 case 16800000:
475 case 19200000:
476 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
477 break;
478 case 9600000:
479 case 28800000:
480 /*
481 * PLL_P_OUT1 rate is not listed in PLLA table
482 */
Thierry Redinge52d7c02015-11-18 14:04:20 +0100483 cfreq = parent_rate / (parent_rate / 1000000);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530484 break;
485 default:
486 pr_err("%s Unexpected reference rate %lu\n",
487 __func__, parent_rate);
488 BUG();
489 }
490
491 /* Raise VCO to guarantee 0.5% accuracy */
492 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
493 cfg->output_rate <<= 1)
494 p_div++;
495
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530496 cfg->m = parent_rate / cfreq;
497 cfg->n = cfg->output_rate / cfreq;
498 cfg->cpcon = OUT_OF_TABLE_CPCON;
499
500 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300501 (1 << p_div) > divp_max(pll)
502 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530503 return -EINVAL;
504 }
505
Thierry Reding00c674e2013-11-18 16:11:35 +0100506 cfg->output_rate >>= p_div;
507
Peter De Schrijver053b5252013-06-05 15:56:41 +0300508 if (pll->params->pdiv_tohw) {
509 ret = _p_div_to_hw(hw, 1 << p_div);
510 if (ret < 0)
511 return ret;
512 else
513 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300514 } else
515 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300516
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530517 return 0;
518}
519
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400520/*
521 * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number
522 * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as
523 * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used
524 * to indicate that SDM is disabled.
525 *
526 * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13
527 */
528static void clk_pll_set_sdm_data(struct clk_hw *hw,
529 struct tegra_clk_pll_freq_table *cfg)
530{
531 struct tegra_clk_pll *pll = to_clk_pll(hw);
532 u32 val;
533 bool enabled;
534
535 if (!pll->params->sdm_din_reg)
536 return;
537
538 if (cfg->sdm_data) {
539 val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll));
540 val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll);
541 pll_writel_sdm_din(val, pll);
542 }
543
544 val = pll_readl_sdm_ctrl(pll);
545 enabled = (val & sdm_en_mask(pll));
546
547 if (cfg->sdm_data == 0 && enabled)
548 val &= ~pll->params->sdm_ctrl_en_mask;
549
550 if (cfg->sdm_data != 0 && !enabled)
551 val |= pll->params->sdm_ctrl_en_mask;
552
553 pll_writel_sdm_ctrl(val, pll);
554}
555
Peter De Schrijverdba40722013-04-03 17:40:36 +0300556static void _update_pll_mnp(struct tegra_clk_pll *pll,
557 struct tegra_clk_pll_freq_table *cfg)
558{
559 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300560 struct tegra_clk_pll_params *params = pll->params;
561 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300562
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300563 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300564 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
565 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
566 val = pll_override_readl(params->pmc_divp_reg, pll);
567 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
568 val |= cfg->p << div_nmp->override_divp_shift;
569 pll_override_writel(val, params->pmc_divp_reg, pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300570
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300571 val = pll_override_readl(params->pmc_divnm_reg, pll);
572 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
573 ~(divn_mask(pll) << div_nmp->override_divn_shift);
574 val |= (cfg->m << div_nmp->override_divm_shift) |
575 (cfg->n << div_nmp->override_divn_shift);
576 pll_override_writel(val, params->pmc_divnm_reg, pll);
577 } else {
578 val = pll_readl_base(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300579
Thierry Redingc61e4e72014-04-04 15:55:14 +0200580 val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
581 divp_mask_shifted(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300582
Thierry Redingc61e4e72014-04-04 15:55:14 +0200583 val |= (cfg->m << divm_shift(pll)) |
584 (cfg->n << divn_shift(pll)) |
585 (cfg->p << divp_shift(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300586
587 pll_writel_base(val, pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400588
589 clk_pll_set_sdm_data(&pll->hw, cfg);
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300590 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300591}
592
593static void _get_pll_mnp(struct tegra_clk_pll *pll,
594 struct tegra_clk_pll_freq_table *cfg)
595{
596 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300597 struct tegra_clk_pll_params *params = pll->params;
598 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300599
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300600 if ((params->flags & TEGRA_PLLM) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300601 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
602 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
603 val = pll_override_readl(params->pmc_divp_reg, pll);
604 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300605
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300606 val = pll_override_readl(params->pmc_divnm_reg, pll);
607 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
608 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
609 } else {
610 val = pll_readl_base(pll);
611
612 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
613 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
614 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400615
616 if (pll->params->sdm_din_reg) {
617 if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) {
618 val = pll_readl_sdm_din(pll);
619 val &= sdm_din_mask(pll);
620 cfg->sdm_data = sdin_din_to_data(val);
621 }
622 }
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300623 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300624}
625
626static void _update_pll_cpcon(struct tegra_clk_pll *pll,
627 struct tegra_clk_pll_freq_table *cfg,
628 unsigned long rate)
629{
630 u32 val;
631
632 val = pll_readl_misc(pll);
633
634 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
635 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
636
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300637 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300638 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
639 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
640 val |= 1 << PLL_MISC_LFCON_SHIFT;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300641 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300642 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
643 if (rate >= (pll->params->vco_max >> 1))
644 val |= 1 << PLL_MISC_DCCON_SHIFT;
645 }
646
647 pll_writel_misc(val, pll);
648}
649
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530650static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
651 unsigned long rate)
652{
653 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300654 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530655
656 state = clk_pll_is_enabled(hw);
657
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530658 if (state)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300659 _clk_pll_disable(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530660
Peter De Schrijverdba40722013-04-03 17:40:36 +0300661 _update_pll_mnp(pll, cfg);
662
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300663 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300664 _update_pll_cpcon(pll, cfg, rate);
665
666 if (state) {
667 _clk_pll_enable(hw);
668 ret = clk_pll_wait_for_lock(pll);
669 }
670
671 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530672}
673
674static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
675 unsigned long parent_rate)
676{
677 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300678 struct tegra_clk_pll_freq_table cfg, old_cfg;
679 unsigned long flags = 0;
680 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530681
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300682 if (pll->params->flags & TEGRA_PLL_FIXED) {
683 if (rate != pll->params->fixed_rate) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530684 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700685 __func__, clk_hw_get_name(hw),
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300686 pll->params->fixed_rate, rate);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530687 return -EINVAL;
688 }
689 return 0;
690 }
691
692 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400693 pll->params->calc_rate(hw, &cfg, rate, parent_rate)) {
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100694 pr_err("%s: Failed to set %s rate %lu\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700695 clk_hw_get_name(hw), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300696 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530697 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300698 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300699 if (pll->lock)
700 spin_lock_irqsave(pll->lock, flags);
701
702 _get_pll_mnp(pll, &old_cfg);
703
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400704 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p ||
705 old_cfg.sdm_data != cfg.sdm_data)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300706 ret = _program_pll(hw, &cfg, rate);
707
708 if (pll->lock)
709 spin_unlock_irqrestore(pll->lock, flags);
710
711 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530712}
713
714static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
715 unsigned long *prate)
716{
717 struct tegra_clk_pll *pll = to_clk_pll(hw);
718 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530719
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300720 if (pll->params->flags & TEGRA_PLL_FIXED)
721 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530722
723 /* PLLM is used for memory; we do not change rate */
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300724 if (pll->params->flags & TEGRA_PLLM)
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -0700725 return clk_hw_get_rate(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530726
727 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400728 pll->params->calc_rate(hw, &cfg, rate, *prate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530729 return -EINVAL;
730
Peter De Schrijver053b5252013-06-05 15:56:41 +0300731 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530732}
733
734static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
735 unsigned long parent_rate)
736{
737 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300738 struct tegra_clk_pll_freq_table cfg;
739 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530740 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300741 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530742
Peter De Schrijverdba40722013-04-03 17:40:36 +0300743 val = pll_readl_base(pll);
744
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300745 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530746 return parent_rate;
747
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300748 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
749 !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530750 struct tegra_clk_pll_freq_table sel;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300751 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
752 parent_rate)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530753 pr_err("Clock %s has unknown fixed frequency\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700754 clk_hw_get_name(hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530755 BUG();
756 }
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300757 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530758 }
759
Peter De Schrijverdba40722013-04-03 17:40:36 +0300760 _get_pll_mnp(pll, &cfg);
761
Peter De Schrijver053b5252013-06-05 15:56:41 +0300762 pdiv = _hw_to_p_div(hw, cfg.p);
763 if (pdiv < 0) {
Rhyland Klein204c85d2015-06-18 17:28:21 -0400764 WARN(1, "Clock %s has invalid pdiv value : 0x%x\n",
765 __clk_get_name(hw->clk), cfg.p);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300766 pdiv = 1;
767 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300768
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400769 if (pll->params->set_gain)
770 pll->params->set_gain(&cfg);
771
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300772 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530773
Peter De Schrijverdba40722013-04-03 17:40:36 +0300774 rate *= cfg.n;
775 do_div(rate, cfg.m);
776
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530777 return rate;
778}
779
780static int clk_plle_training(struct tegra_clk_pll *pll)
781{
782 u32 val;
783 unsigned long timeout;
784
785 if (!pll->pmc)
786 return -ENOSYS;
787
788 /*
789 * PLLE is already disabled, and setup cleared;
790 * create falling edge on PLLE IDDQ input.
791 */
792 val = readl(pll->pmc + PMC_SATA_PWRGT);
793 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
794 writel(val, pll->pmc + PMC_SATA_PWRGT);
795
796 val = readl(pll->pmc + PMC_SATA_PWRGT);
797 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
798 writel(val, pll->pmc + PMC_SATA_PWRGT);
799
800 val = readl(pll->pmc + PMC_SATA_PWRGT);
801 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
802 writel(val, pll->pmc + PMC_SATA_PWRGT);
803
804 val = pll_readl_misc(pll);
805
806 timeout = jiffies + msecs_to_jiffies(100);
807 while (1) {
808 val = pll_readl_misc(pll);
809 if (val & PLLE_MISC_READY)
810 break;
811 if (time_after(jiffies, timeout)) {
812 pr_err("%s: timeout waiting for PLLE\n", __func__);
813 return -EBUSY;
814 }
815 udelay(300);
816 }
817
818 return 0;
819}
820
821static int clk_plle_enable(struct clk_hw *hw)
822{
823 struct tegra_clk_pll *pll = to_clk_pll(hw);
824 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
825 struct tegra_clk_pll_freq_table sel;
826 u32 val;
827 int err;
828
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300829 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530830 return -EINVAL;
831
832 clk_pll_disable(hw);
833
834 val = pll_readl_misc(pll);
835 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
836 pll_writel_misc(val, pll);
837
838 val = pll_readl_misc(pll);
839 if (!(val & PLLE_MISC_READY)) {
840 err = clk_plle_training(pll);
841 if (err)
842 return err;
843 }
844
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300845 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530846 /* configure dividers */
847 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200848 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
849 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200850 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200851 val |= sel.m << divm_shift(pll);
852 val |= sel.n << divn_shift(pll);
853 val |= sel.p << divp_shift(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530854 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
855 pll_writel_base(val, pll);
856 }
857
858 val = pll_readl_misc(pll);
859 val |= PLLE_MISC_SETUP_VALUE;
860 val |= PLLE_MISC_LOCK_ENABLE;
861 pll_writel_misc(val, pll);
862
863 val = readl(pll->clk_base + PLLE_SS_CTRL);
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200864 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530865 val |= PLLE_SS_DISABLE;
866 writel(val, pll->clk_base + PLLE_SS_CTRL);
867
Thierry Reding4ccc4022014-04-04 15:55:15 +0200868 val = pll_readl_base(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530869 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
870 pll_writel_base(val, pll);
871
Peter De Schrijverdba40722013-04-03 17:40:36 +0300872 clk_pll_wait_for_lock(pll);
873
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530874 return 0;
875}
876
877static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
878 unsigned long parent_rate)
879{
880 struct tegra_clk_pll *pll = to_clk_pll(hw);
881 u32 val = pll_readl_base(pll);
882 u32 divn = 0, divm = 0, divp = 0;
883 u64 rate = parent_rate;
884
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300885 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
886 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
887 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530888 divm *= divp;
889
890 rate *= divn;
891 do_div(rate, divm);
892 return rate;
893}
894
895const struct clk_ops tegra_clk_pll_ops = {
896 .is_enabled = clk_pll_is_enabled,
897 .enable = clk_pll_enable,
898 .disable = clk_pll_disable,
899 .recalc_rate = clk_pll_recalc_rate,
900 .round_rate = clk_pll_round_rate,
901 .set_rate = clk_pll_set_rate,
902};
903
904const struct clk_ops tegra_clk_plle_ops = {
905 .recalc_rate = clk_plle_recalc_rate,
906 .is_enabled = clk_pll_is_enabled,
907 .disable = clk_pll_disable,
908 .enable = clk_plle_enable,
909};
910
Paul Walmsley08acae32014-12-16 12:38:29 -0800911#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
912 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
913 defined(CONFIG_ARCH_TEGRA_132_SOC)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300914
915static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
916 unsigned long parent_rate)
917{
Rhyland Klein407254d2015-06-18 17:28:25 -0400918 u16 mdiv = parent_rate / pll_params->cf_min;
919
920 if (pll_params->flags & TEGRA_MDIV_NEW)
921 return (!pll_params->mdiv_default ? mdiv :
922 min(mdiv, pll_params->mdiv_default));
923
924 if (pll_params->mdiv_default)
925 return pll_params->mdiv_default;
926
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300927 if (parent_rate > pll_params->cf_max)
928 return 2;
929 else
930 return 1;
931}
932
Rhyland Klein407254d2015-06-18 17:28:25 -0400933u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate)
934{
935 struct tegra_clk_pll *pll = to_clk_pll(hw);
936
937 return (u16)_pll_fixed_mdiv(pll->params, input_rate);
938}
939
Peter De Schrijver04edb092013-09-06 14:37:37 +0300940static unsigned long _clip_vco_min(unsigned long vco_min,
941 unsigned long parent_rate)
942{
943 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
944}
945
946static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
947 void __iomem *clk_base,
948 unsigned long parent_rate)
949{
950 u32 val;
951 u32 step_a, step_b;
952
953 switch (parent_rate) {
954 case 12000000:
955 case 13000000:
956 case 26000000:
957 step_a = 0x2B;
958 step_b = 0x0B;
959 break;
960 case 16800000:
961 step_a = 0x1A;
962 step_b = 0x09;
963 break;
964 case 19200000:
965 step_a = 0x12;
966 step_b = 0x08;
967 break;
968 default:
969 pr_err("%s: Unexpected reference rate %lu\n",
970 __func__, parent_rate);
971 WARN_ON(1);
972 return -EINVAL;
973 }
974
975 val = step_a << pll_params->stepa_shift;
976 val |= step_b << pll_params->stepb_shift;
977 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
978
979 return 0;
980}
981
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300982static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
983 struct tegra_clk_pll_freq_table *cfg,
984 unsigned long rate, unsigned long parent_rate)
985{
986 struct tegra_clk_pll *pll = to_clk_pll(hw);
987 unsigned int p;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300988 int p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300989
990 if (!rate)
991 return -EINVAL;
992
993 p = DIV_ROUND_UP(pll->params->vco_min, rate);
994 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300995 cfg->output_rate = rate * p;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300996 cfg->n = cfg->output_rate * cfg->m / parent_rate;
997
Peter De Schrijver053b5252013-06-05 15:56:41 +0300998 p_div = _p_div_to_hw(hw, p);
999 if (p_div < 0)
1000 return p_div;
Thierry Redinge52d7c02015-11-18 14:04:20 +01001001
1002 cfg->p = p_div;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001003
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001004 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
1005 return -EINVAL;
1006
1007 return 0;
1008}
1009
1010static int _pll_ramp_calc_pll(struct clk_hw *hw,
1011 struct tegra_clk_pll_freq_table *cfg,
1012 unsigned long rate, unsigned long parent_rate)
1013{
1014 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijver053b5252013-06-05 15:56:41 +03001015 int err = 0, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001016
1017 err = _get_table_rate(hw, cfg, rate, parent_rate);
1018 if (err < 0)
1019 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +03001020 else {
1021 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001022 WARN_ON(1);
1023 err = -EINVAL;
1024 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001025 }
1026 p_div = _p_div_to_hw(hw, cfg->p);
1027 if (p_div < 0)
1028 return p_div;
1029 else
1030 cfg->p = p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001031 }
1032
Peter De Schrijver053b5252013-06-05 15:56:41 +03001033 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001034 err = -EINVAL;
1035
1036out:
1037 return err;
1038}
1039
1040static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
1041 unsigned long parent_rate)
1042{
1043 struct tegra_clk_pll *pll = to_clk_pll(hw);
1044 struct tegra_clk_pll_freq_table cfg, old_cfg;
1045 unsigned long flags = 0;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001046 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001047
1048 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1049 if (ret < 0)
1050 return ret;
1051
1052 if (pll->lock)
1053 spin_lock_irqsave(pll->lock, flags);
1054
1055 _get_pll_mnp(pll, &old_cfg);
1056
Peter De Schrijver053b5252013-06-05 15:56:41 +03001057 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001058 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001059
1060 if (pll->lock)
1061 spin_unlock_irqrestore(pll->lock, flags);
1062
1063 return ret;
1064}
1065
1066static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1067 unsigned long *prate)
1068{
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001069 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001070 struct tegra_clk_pll_freq_table cfg;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001071 int ret, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001072 u64 output_rate = *prate;
1073
1074 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1075 if (ret < 0)
1076 return ret;
1077
Peter De Schrijver053b5252013-06-05 15:56:41 +03001078 p_div = _hw_to_p_div(hw, cfg.p);
1079 if (p_div < 0)
1080 return p_div;
1081
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001082 if (pll->params->set_gain)
1083 pll->params->set_gain(&cfg);
1084
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001085 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001086 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001087
1088 return output_rate;
1089}
1090
1091static int clk_pllm_set_rate(struct clk_hw *hw, unsigned long rate,
1092 unsigned long parent_rate)
1093{
1094 struct tegra_clk_pll_freq_table cfg;
1095 struct tegra_clk_pll *pll = to_clk_pll(hw);
1096 unsigned long flags = 0;
1097 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001098
1099 if (pll->lock)
1100 spin_lock_irqsave(pll->lock, flags);
1101
1102 state = clk_pll_is_enabled(hw);
1103 if (state) {
1104 if (rate != clk_get_rate(hw->clk)) {
1105 pr_err("%s: Cannot change active PLLM\n", __func__);
1106 ret = -EINVAL;
1107 goto out;
1108 }
1109 goto out;
1110 }
1111
1112 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1113 if (ret < 0)
1114 goto out;
1115
Peter De Schrijver408a24f2013-06-06 13:47:31 +03001116 _update_pll_mnp(pll, &cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001117
1118out:
1119 if (pll->lock)
1120 spin_unlock_irqrestore(pll->lock, flags);
1121
1122 return ret;
1123}
1124
1125static void _pllcx_strobe(struct tegra_clk_pll *pll)
1126{
1127 u32 val;
1128
1129 val = pll_readl_misc(pll);
1130 val |= PLLCX_MISC_STROBE;
1131 pll_writel_misc(val, pll);
1132 udelay(2);
1133
1134 val &= ~PLLCX_MISC_STROBE;
1135 pll_writel_misc(val, pll);
1136}
1137
1138static int clk_pllc_enable(struct clk_hw *hw)
1139{
1140 struct tegra_clk_pll *pll = to_clk_pll(hw);
1141 u32 val;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001142 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001143 unsigned long flags = 0;
1144
1145 if (pll->lock)
1146 spin_lock_irqsave(pll->lock, flags);
1147
1148 _clk_pll_enable(hw);
1149 udelay(2);
1150
1151 val = pll_readl_misc(pll);
1152 val &= ~PLLCX_MISC_RESET;
1153 pll_writel_misc(val, pll);
1154 udelay(2);
1155
1156 _pllcx_strobe(pll);
1157
1158 ret = clk_pll_wait_for_lock(pll);
1159
1160 if (pll->lock)
1161 spin_unlock_irqrestore(pll->lock, flags);
1162
1163 return ret;
1164}
1165
1166static void _clk_pllc_disable(struct clk_hw *hw)
1167{
1168 struct tegra_clk_pll *pll = to_clk_pll(hw);
1169 u32 val;
1170
1171 _clk_pll_disable(hw);
1172
1173 val = pll_readl_misc(pll);
1174 val |= PLLCX_MISC_RESET;
1175 pll_writel_misc(val, pll);
1176 udelay(2);
1177}
1178
1179static void clk_pllc_disable(struct clk_hw *hw)
1180{
1181 struct tegra_clk_pll *pll = to_clk_pll(hw);
1182 unsigned long flags = 0;
1183
1184 if (pll->lock)
1185 spin_lock_irqsave(pll->lock, flags);
1186
1187 _clk_pllc_disable(hw);
1188
1189 if (pll->lock)
1190 spin_unlock_irqrestore(pll->lock, flags);
1191}
1192
1193static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1194 unsigned long input_rate, u32 n)
1195{
1196 u32 val, n_threshold;
1197
1198 switch (input_rate) {
1199 case 12000000:
1200 n_threshold = 70;
1201 break;
1202 case 13000000:
1203 case 26000000:
1204 n_threshold = 71;
1205 break;
1206 case 16800000:
1207 n_threshold = 55;
1208 break;
1209 case 19200000:
1210 n_threshold = 48;
1211 break;
1212 default:
1213 pr_err("%s: Unexpected reference rate %lu\n",
1214 __func__, input_rate);
1215 return -EINVAL;
1216 }
1217
1218 val = pll_readl_misc(pll);
1219 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1220 val |= n <= n_threshold ?
1221 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1222 pll_writel_misc(val, pll);
1223
1224 return 0;
1225}
1226
1227static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1228 unsigned long parent_rate)
1229{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001230 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001231 struct tegra_clk_pll *pll = to_clk_pll(hw);
1232 unsigned long flags = 0;
1233 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001234
1235 if (pll->lock)
1236 spin_lock_irqsave(pll->lock, flags);
1237
1238 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1239 if (ret < 0)
1240 goto out;
1241
Peter De Schrijver053b5252013-06-05 15:56:41 +03001242 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001243
Peter De Schrijver053b5252013-06-05 15:56:41 +03001244 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001245 WARN_ON(1);
1246 goto out;
1247 }
1248
Peter De Schrijver053b5252013-06-05 15:56:41 +03001249 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001250 goto out;
1251
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001252 state = clk_pll_is_enabled(hw);
1253 if (state)
1254 _clk_pllc_disable(hw);
1255
1256 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1257 if (ret < 0)
1258 goto out;
1259
1260 _update_pll_mnp(pll, &cfg);
1261
1262 if (state)
1263 ret = clk_pllc_enable(hw);
1264
1265out:
1266 if (pll->lock)
1267 spin_unlock_irqrestore(pll->lock, flags);
1268
1269 return ret;
1270}
1271
1272static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1273 struct tegra_clk_pll_freq_table *cfg,
1274 unsigned long rate, unsigned long parent_rate)
1275{
1276 u16 m, n;
1277 u64 output_rate = parent_rate;
1278
1279 m = _pll_fixed_mdiv(pll->params, parent_rate);
1280 n = rate * m / parent_rate;
1281
1282 output_rate *= n;
1283 do_div(output_rate, m);
1284
1285 if (cfg) {
1286 cfg->m = m;
1287 cfg->n = n;
1288 }
1289
1290 return output_rate;
1291}
Thierry Reding6bb18c52014-08-01 10:44:20 +02001292
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001293static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1294 unsigned long parent_rate)
1295{
1296 struct tegra_clk_pll_freq_table cfg, old_cfg;
1297 struct tegra_clk_pll *pll = to_clk_pll(hw);
1298 unsigned long flags = 0;
1299 int state, ret = 0;
1300
1301 if (pll->lock)
1302 spin_lock_irqsave(pll->lock, flags);
1303
1304 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1305 _get_pll_mnp(pll, &old_cfg);
1306 cfg.p = old_cfg.p;
1307
1308 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1309 state = clk_pll_is_enabled(hw);
1310 if (state)
1311 _clk_pll_disable(hw);
1312
1313 _update_pll_mnp(pll, &cfg);
1314
1315 if (state) {
1316 _clk_pll_enable(hw);
1317 ret = clk_pll_wait_for_lock(pll);
1318 }
1319 }
1320
1321 if (pll->lock)
1322 spin_unlock_irqrestore(pll->lock, flags);
1323
1324 return ret;
1325}
1326
1327static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1328 unsigned long parent_rate)
1329{
1330 struct tegra_clk_pll_freq_table cfg;
1331 struct tegra_clk_pll *pll = to_clk_pll(hw);
1332 u64 rate = parent_rate;
1333
1334 _get_pll_mnp(pll, &cfg);
1335
1336 rate *= cfg.n;
1337 do_div(rate, cfg.m);
1338
1339 return rate;
1340}
1341
1342static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1343 unsigned long *prate)
1344{
1345 struct tegra_clk_pll *pll = to_clk_pll(hw);
1346
1347 return _pllre_calc_rate(pll, NULL, rate, *prate);
1348}
1349
1350static int clk_plle_tegra114_enable(struct clk_hw *hw)
1351{
1352 struct tegra_clk_pll *pll = to_clk_pll(hw);
1353 struct tegra_clk_pll_freq_table sel;
1354 u32 val;
1355 int ret;
1356 unsigned long flags = 0;
1357 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1358
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001359 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001360 return -EINVAL;
1361
1362 if (pll->lock)
1363 spin_lock_irqsave(pll->lock, flags);
1364
1365 val = pll_readl_base(pll);
1366 val &= ~BIT(29); /* Disable lock override */
1367 pll_writel_base(val, pll);
1368
1369 val = pll_readl(pll->params->aux_reg, pll);
1370 val |= PLLE_AUX_ENABLE_SWCTL;
1371 val &= ~PLLE_AUX_SEQ_ENABLE;
1372 pll_writel(val, pll->params->aux_reg, pll);
1373 udelay(1);
1374
1375 val = pll_readl_misc(pll);
1376 val |= PLLE_MISC_LOCK_ENABLE;
1377 val |= PLLE_MISC_IDDQ_SW_CTRL;
1378 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1379 val |= PLLE_MISC_PLLE_PTS;
1380 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1381 pll_writel_misc(val, pll);
1382 udelay(5);
1383
1384 val = pll_readl(PLLE_SS_CTRL, pll);
1385 val |= PLLE_SS_DISABLE;
1386 pll_writel(val, PLLE_SS_CTRL, pll);
1387
1388 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001389 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1390 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001391 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001392 val |= sel.m << divm_shift(pll);
1393 val |= sel.n << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001394 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1395 pll_writel_base(val, pll);
1396 udelay(1);
1397
1398 _clk_pll_enable(hw);
1399 ret = clk_pll_wait_for_lock(pll);
1400
1401 if (ret < 0)
1402 goto out;
1403
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001404 val = pll_readl(PLLE_SS_CTRL, pll);
1405 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1406 val &= ~PLLE_SS_COEFFICIENTS_MASK;
1407 val |= PLLE_SS_COEFFICIENTS_VAL;
1408 pll_writel(val, PLLE_SS_CTRL, pll);
1409 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1410 pll_writel(val, PLLE_SS_CTRL, pll);
1411 udelay(1);
1412 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1413 pll_writel(val, PLLE_SS_CTRL, pll);
1414 udelay(1);
1415
Jim Lin2cfe1672014-05-14 17:32:57 -07001416 /* Enable hw control of xusb brick pll */
1417 val = pll_readl_misc(pll);
1418 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1419 pll_writel_misc(val, pll);
1420
1421 val = pll_readl(pll->params->aux_reg, pll);
1422 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1423 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1424 pll_writel(val, pll->params->aux_reg, pll);
1425 udelay(1);
1426 val |= PLLE_AUX_SEQ_ENABLE;
1427 pll_writel(val, pll->params->aux_reg, pll);
1428
1429 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1430 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1431 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1432 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1433 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1434 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1435 udelay(1);
1436 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1437 pll_writel(val, XUSBIO_PLL_CFG0, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001438
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001439 /* Enable hw control of SATA pll */
1440 val = pll_readl(SATA_PLL_CFG0, pll);
1441 val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +02001442 val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1443 val |= SATA_PLL_CFG0_SEQ_START_STATE;
1444 pll_writel(val, SATA_PLL_CFG0, pll);
1445
1446 udelay(1);
1447
1448 val = pll_readl(SATA_PLL_CFG0, pll);
1449 val |= SATA_PLL_CFG0_SEQ_ENABLE;
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001450 pll_writel(val, SATA_PLL_CFG0, pll);
1451
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001452out:
1453 if (pll->lock)
1454 spin_unlock_irqrestore(pll->lock, flags);
1455
1456 return ret;
1457}
1458
1459static void clk_plle_tegra114_disable(struct clk_hw *hw)
1460{
1461 struct tegra_clk_pll *pll = to_clk_pll(hw);
1462 unsigned long flags = 0;
1463 u32 val;
1464
1465 if (pll->lock)
1466 spin_lock_irqsave(pll->lock, flags);
1467
1468 _clk_pll_disable(hw);
1469
1470 val = pll_readl_misc(pll);
1471 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1472 pll_writel_misc(val, pll);
1473 udelay(1);
1474
1475 if (pll->lock)
1476 spin_unlock_irqrestore(pll->lock, flags);
1477}
1478#endif
1479
Peter De Schrijverdba40722013-04-03 17:40:36 +03001480static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001481 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1482 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301483{
1484 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301485
1486 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1487 if (!pll)
1488 return ERR_PTR(-ENOMEM);
1489
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301490 pll->clk_base = clk_base;
1491 pll->pmc = pmc;
1492
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301493 pll->params = pll_params;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301494 pll->lock = lock;
1495
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001496 if (!pll_params->div_nmp)
1497 pll_params->div_nmp = &default_nmp;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301498
Peter De Schrijverdba40722013-04-03 17:40:36 +03001499 return pll;
1500}
1501
1502static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1503 const char *name, const char *parent_name, unsigned long flags,
1504 const struct clk_ops *ops)
1505{
1506 struct clk_init_data init;
1507
1508 init.name = name;
1509 init.ops = ops;
1510 init.flags = flags;
1511 init.parent_names = (parent_name ? &parent_name : NULL);
1512 init.num_parents = (parent_name ? 1 : 0);
1513
Rhyland Klein407254d2015-06-18 17:28:25 -04001514 /* Default to _calc_rate if unspecified */
1515 if (!pll->params->calc_rate)
1516 pll->params->calc_rate = _calc_rate;
1517
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301518 /* Data in .init is copied by clk_register(), so stack variable OK */
1519 pll->hw.init = &init;
1520
Peter De Schrijverdba40722013-04-03 17:40:36 +03001521 return clk_register(NULL, &pll->hw);
1522}
1523
1524struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1525 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001526 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1527 spinlock_t *lock)
Peter De Schrijverdba40722013-04-03 17:40:36 +03001528{
1529 struct tegra_clk_pll *pll;
1530 struct clk *clk;
1531
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001532 pll_params->flags |= TEGRA_PLL_BYPASS;
Rhyland Klein3706b432015-06-18 17:28:23 -04001533
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001534 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001535 if (IS_ERR(pll))
1536 return ERR_CAST(pll);
1537
1538 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1539 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301540 if (IS_ERR(clk))
1541 kfree(pll);
1542
1543 return clk;
1544}
1545
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001546static struct div_nmp pll_e_nmp = {
1547 .divn_shift = PLLE_BASE_DIVN_SHIFT,
1548 .divn_width = PLLE_BASE_DIVN_WIDTH,
1549 .divm_shift = PLLE_BASE_DIVM_SHIFT,
1550 .divm_width = PLLE_BASE_DIVM_WIDTH,
1551 .divp_shift = PLLE_BASE_DIVP_SHIFT,
1552 .divp_width = PLLE_BASE_DIVP_WIDTH,
1553};
1554
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301555struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1556 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001557 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1558 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301559{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001560 struct tegra_clk_pll *pll;
1561 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001562
Rhyland Klein3706b432015-06-18 17:28:23 -04001563 pll_params->flags |= TEGRA_PLL_BYPASS;
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001564
1565 if (!pll_params->div_nmp)
1566 pll_params->div_nmp = &pll_e_nmp;
1567
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001568 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001569 if (IS_ERR(pll))
1570 return ERR_CAST(pll);
1571
1572 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1573 &tegra_clk_plle_ops);
1574 if (IS_ERR(clk))
1575 kfree(pll);
1576
1577 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301578}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001579
Paul Walmsley08acae32014-12-16 12:38:29 -08001580#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1581 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
1582 defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301583static const struct clk_ops tegra_clk_pllxc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001584 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001585 .enable = clk_pll_enable,
1586 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001587 .recalc_rate = clk_pll_recalc_rate,
1588 .round_rate = clk_pll_ramp_round_rate,
1589 .set_rate = clk_pllxc_set_rate,
1590};
1591
Sachin Kamate47e12f2013-10-08 16:47:41 +05301592static const struct clk_ops tegra_clk_pllm_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_pllm_set_rate,
1599};
1600
Sachin Kamate47e12f2013-10-08 16:47:41 +05301601static const struct clk_ops tegra_clk_pllc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001602 .is_enabled = clk_pll_is_enabled,
1603 .enable = clk_pllc_enable,
1604 .disable = clk_pllc_disable,
1605 .recalc_rate = clk_pll_recalc_rate,
1606 .round_rate = clk_pll_ramp_round_rate,
1607 .set_rate = clk_pllc_set_rate,
1608};
1609
Sachin Kamate47e12f2013-10-08 16:47:41 +05301610static const struct clk_ops tegra_clk_pllre_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001611 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001612 .enable = clk_pll_enable,
1613 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001614 .recalc_rate = clk_pllre_recalc_rate,
1615 .round_rate = clk_pllre_round_rate,
1616 .set_rate = clk_pllre_set_rate,
1617};
1618
Sachin Kamate47e12f2013-10-08 16:47:41 +05301619static const struct clk_ops tegra_clk_plle_tegra114_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001620 .is_enabled = clk_pll_is_enabled,
1621 .enable = clk_plle_tegra114_enable,
1622 .disable = clk_plle_tegra114_disable,
1623 .recalc_rate = clk_pll_recalc_rate,
1624};
1625
1626
1627struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1628 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001629 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001630 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001631 spinlock_t *lock)
1632{
1633 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001634 struct clk *clk, *parent;
1635 unsigned long parent_rate;
1636 int err;
1637 u32 val, val_iddq;
1638
1639 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001640 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001641 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001642 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001643 return ERR_PTR(-EINVAL);
1644 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001645
1646 if (!pll_params->pdiv_tohw)
1647 return ERR_PTR(-EINVAL);
1648
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001649 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001650
1651 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1652
1653 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
1654 if (err)
1655 return ERR_PTR(err);
1656
1657 val = readl_relaxed(clk_base + pll_params->base_reg);
1658 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
1659
1660 if (val & PLL_BASE_ENABLE)
1661 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
1662 else {
1663 val_iddq |= BIT(pll_params->iddq_bit_idx);
1664 writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
1665 }
1666
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001667 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001668 if (IS_ERR(pll))
1669 return ERR_CAST(pll);
1670
1671 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1672 &tegra_clk_pllxc_ops);
1673 if (IS_ERR(clk))
1674 kfree(pll);
1675
1676 return clk;
1677}
1678
1679struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1680 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001681 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001682 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001683 spinlock_t *lock, unsigned long parent_rate)
1684{
1685 u32 val;
1686 struct tegra_clk_pll *pll;
1687 struct clk *clk;
1688
Peter De Schrijver04edb092013-09-06 14:37:37 +03001689 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1690
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001691 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001692 if (IS_ERR(pll))
1693 return ERR_CAST(pll);
1694
1695 /* program minimum rate by default */
1696
1697 val = pll_readl_base(pll);
1698 if (val & PLL_BASE_ENABLE)
1699 WARN_ON(val & pll_params->iddq_bit_idx);
1700 else {
1701 int m;
1702
1703 m = _pll_fixed_mdiv(pll_params, parent_rate);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001704 val = m << divm_shift(pll);
1705 val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001706 pll_writel_base(val, pll);
1707 }
1708
1709 /* disable lock override */
1710
1711 val = pll_readl_misc(pll);
1712 val &= ~BIT(29);
1713 pll_writel_misc(val, pll);
1714
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001715 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1716 &tegra_clk_pllre_ops);
1717 if (IS_ERR(clk))
1718 kfree(pll);
1719
1720 return clk;
1721}
1722
1723struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1724 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001725 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001726 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001727 spinlock_t *lock)
1728{
1729 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001730 struct clk *clk, *parent;
1731 unsigned long parent_rate;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001732
1733 if (!pll_params->pdiv_tohw)
1734 return ERR_PTR(-EINVAL);
1735
Peter De Schrijver04edb092013-09-06 14:37:37 +03001736 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001737 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001738 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001739 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001740 return ERR_PTR(-EINVAL);
1741 }
1742
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001743 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001744
1745 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1746
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001747 pll_params->flags |= TEGRA_PLL_BYPASS;
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001748 pll_params->flags |= TEGRA_PLLM;
1749 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001750 if (IS_ERR(pll))
1751 return ERR_CAST(pll);
1752
1753 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1754 &tegra_clk_pllm_ops);
1755 if (IS_ERR(clk))
1756 kfree(pll);
1757
1758 return clk;
1759}
1760
1761struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1762 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001763 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001764 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001765 spinlock_t *lock)
1766{
1767 struct clk *parent, *clk;
Thierry Reding385f9ad2015-11-19 16:34:06 +01001768 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001769 struct tegra_clk_pll *pll;
1770 struct tegra_clk_pll_freq_table cfg;
1771 unsigned long parent_rate;
1772
1773 if (!p_tohw)
1774 return ERR_PTR(-EINVAL);
1775
1776 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001777 if (!parent) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001778 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001779 parent_name, name);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001780 return ERR_PTR(-EINVAL);
1781 }
1782
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001783 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001784
1785 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1786
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001787 pll_params->flags |= TEGRA_PLL_BYPASS;
1788 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001789 if (IS_ERR(pll))
1790 return ERR_CAST(pll);
1791
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001792 /*
1793 * Most of PLLC register fields are shadowed, and can not be read
1794 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1795 * Initialize PLL to default state: disabled, reset; shadow registers
1796 * loaded with default parameters; dividers are preset for half of
1797 * minimum VCO rate (the latter assured that shadowed divider settings
1798 * are within supported range).
1799 */
1800
1801 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1802 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1803
1804 while (p_tohw->pdiv) {
1805 if (p_tohw->pdiv == 2) {
1806 cfg.p = p_tohw->hw_val;
1807 break;
1808 }
1809 p_tohw++;
1810 }
1811
1812 if (!p_tohw->pdiv) {
1813 WARN_ON(1);
1814 return ERR_PTR(-EINVAL);
1815 }
1816
1817 pll_writel_base(0, pll);
1818 _update_pll_mnp(pll, &cfg);
1819
1820 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1821 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1822 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1823 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1824
1825 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1826
1827 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1828 &tegra_clk_pllc_ops);
1829 if (IS_ERR(clk))
1830 kfree(pll);
1831
1832 return clk;
1833}
1834
1835struct clk *tegra_clk_register_plle_tegra114(const char *name,
1836 const char *parent_name,
1837 void __iomem *clk_base, unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001838 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001839 spinlock_t *lock)
1840{
1841 struct tegra_clk_pll *pll;
1842 struct clk *clk;
1843 u32 val, val_aux;
1844
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001845 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001846 if (IS_ERR(pll))
1847 return ERR_CAST(pll);
1848
1849 /* ensure parent is set to pll_re_vco */
1850
1851 val = pll_readl_base(pll);
1852 val_aux = pll_readl(pll_params->aux_reg, pll);
1853
1854 if (val & PLL_BASE_ENABLE) {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001855 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
1856 (val_aux & PLLE_AUX_PLLP_SEL))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001857 WARN(1, "pll_e enabled with unsupported parent %s\n",
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001858 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
1859 "pll_re_vco");
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001860 } else {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001861 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
Tuomas Tynkkynend2c834a2014-05-16 16:50:20 +03001862 pll_writel(val_aux, pll_params->aux_reg, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001863 }
1864
1865 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1866 &tegra_clk_plle_tegra114_ops);
1867 if (IS_ERR(clk))
1868 kfree(pll);
1869
1870 return clk;
1871}
1872#endif
Peter De Schrijver798e9102013-09-09 13:22:55 +03001873
Paul Walmsley08acae32014-12-16 12:38:29 -08001874#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301875static const struct clk_ops tegra_clk_pllss_ops = {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001876 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001877 .enable = clk_pll_enable,
1878 .disable = clk_pll_disable,
Peter De Schrijver798e9102013-09-09 13:22:55 +03001879 .recalc_rate = clk_pll_recalc_rate,
1880 .round_rate = clk_pll_ramp_round_rate,
1881 .set_rate = clk_pllxc_set_rate,
1882};
1883
1884struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
1885 void __iomem *clk_base, unsigned long flags,
1886 struct tegra_clk_pll_params *pll_params,
1887 spinlock_t *lock)
1888{
1889 struct tegra_clk_pll *pll;
1890 struct clk *clk, *parent;
1891 struct tegra_clk_pll_freq_table cfg;
1892 unsigned long parent_rate;
1893 u32 val;
1894 int i;
1895
1896 if (!pll_params->div_nmp)
1897 return ERR_PTR(-EINVAL);
1898
1899 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001900 if (!parent) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001901 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001902 parent_name, name);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001903 return ERR_PTR(-EINVAL);
1904 }
1905
Peter De Schrijver798e9102013-09-09 13:22:55 +03001906 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1907 if (IS_ERR(pll))
1908 return ERR_CAST(pll);
1909
1910 val = pll_readl_base(pll);
1911 val &= ~PLLSS_REF_SRC_SEL_MASK;
1912 pll_writel_base(val, pll);
1913
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001914 parent_rate = clk_get_rate(parent);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001915
1916 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1917
1918 /* initialize PLL to minimum rate */
1919
1920 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1921 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1922
1923 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
1924 ;
1925 if (!i) {
1926 kfree(pll);
1927 return ERR_PTR(-EINVAL);
1928 }
1929
1930 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
1931
1932 _update_pll_mnp(pll, &cfg);
1933
1934 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
1935 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
1936 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
1937 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
1938
1939 val = pll_readl_base(pll);
1940 if (val & PLL_BASE_ENABLE) {
1941 if (val & BIT(pll_params->iddq_bit_idx)) {
1942 WARN(1, "%s is on but IDDQ set\n", name);
1943 kfree(pll);
1944 return ERR_PTR(-EINVAL);
1945 }
1946 } else
1947 val |= BIT(pll_params->iddq_bit_idx);
1948
1949 val &= ~PLLSS_LOCK_OVERRIDE;
1950 pll_writel_base(val, pll);
1951
1952 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1953 &tegra_clk_pllss_ops);
1954
1955 if (IS_ERR(clk))
1956 kfree(pll);
1957
1958 return clk;
1959}
1960#endif