blob: 731c6857c895cd8739c98ef3d74af214f33fd345 [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
Rhyland Kleindd322f02015-06-18 17:28:28 -040068#define PLLE_BASE_ENABLE BIT(31)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053069
70#define PLLE_MISC_SETUP_BASE_SHIFT 16
71#define PLLE_MISC_SETUP_BASE_MASK (0xffff << PLLE_MISC_SETUP_BASE_SHIFT)
72#define PLLE_MISC_LOCK_ENABLE BIT(9)
73#define PLLE_MISC_READY BIT(15)
74#define PLLE_MISC_SETUP_EX_SHIFT 2
75#define PLLE_MISC_SETUP_EX_MASK (3 << PLLE_MISC_SETUP_EX_SHIFT)
76#define PLLE_MISC_SETUP_MASK (PLLE_MISC_SETUP_BASE_MASK | \
77 PLLE_MISC_SETUP_EX_MASK)
78#define PLLE_MISC_SETUP_VALUE (7 << PLLE_MISC_SETUP_BASE_SHIFT)
79
80#define PLLE_SS_CTRL 0x68
Peter De Schrijver642fb0c2013-09-26 18:30:01 +030081#define PLLE_SS_CNTL_BYPASS_SS BIT(10)
82#define PLLE_SS_CNTL_INTERP_RESET BIT(11)
83#define PLLE_SS_CNTL_SSC_BYP BIT(12)
84#define PLLE_SS_CNTL_CENTER BIT(14)
85#define PLLE_SS_CNTL_INVERT BIT(15)
86#define PLLE_SS_DISABLE (PLLE_SS_CNTL_BYPASS_SS | PLLE_SS_CNTL_INTERP_RESET |\
87 PLLE_SS_CNTL_SSC_BYP)
88#define PLLE_SS_MAX_MASK 0x1ff
89#define PLLE_SS_MAX_VAL 0x25
90#define PLLE_SS_INC_MASK (0xff << 16)
91#define PLLE_SS_INC_VAL (0x1 << 16)
92#define PLLE_SS_INCINTRV_MASK (0x3f << 24)
93#define PLLE_SS_INCINTRV_VAL (0x20 << 24)
94#define PLLE_SS_COEFFICIENTS_MASK \
95 (PLLE_SS_MAX_MASK | PLLE_SS_INC_MASK | PLLE_SS_INCINTRV_MASK)
96#define PLLE_SS_COEFFICIENTS_VAL \
97 (PLLE_SS_MAX_VAL | PLLE_SS_INC_VAL | PLLE_SS_INCINTRV_VAL)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053098
Peter De Schrijverc1d19392013-04-03 17:40:41 +030099#define PLLE_AUX_PLLP_SEL BIT(2)
Jim Lin2cfe1672014-05-14 17:32:57 -0700100#define PLLE_AUX_USE_LOCKDET BIT(3)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300101#define PLLE_AUX_ENABLE_SWCTL BIT(4)
Jim Lin2cfe1672014-05-14 17:32:57 -0700102#define PLLE_AUX_SS_SWCTL BIT(6)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300103#define PLLE_AUX_SEQ_ENABLE BIT(24)
Jim Lin2cfe1672014-05-14 17:32:57 -0700104#define PLLE_AUX_SEQ_START_STATE BIT(25)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300105#define PLLE_AUX_PLLRE_SEL BIT(28)
Rhyland Kleindd322f02015-06-18 17:28:28 -0400106#define PLLE_AUX_SS_SEQ_INCLUDE BIT(31)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300107
Jim Lin2cfe1672014-05-14 17:32:57 -0700108#define XUSBIO_PLL_CFG0 0x51c
109#define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
110#define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL BIT(2)
111#define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET BIT(6)
112#define XUSBIO_PLL_CFG0_SEQ_ENABLE BIT(24)
113#define XUSBIO_PLL_CFG0_SEQ_START_STATE BIT(25)
114
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300115#define SATA_PLL_CFG0 0x490
116#define SATA_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +0200117#define SATA_PLL_CFG0_PADPLL_USE_LOCKDET BIT(2)
118#define SATA_PLL_CFG0_SEQ_ENABLE BIT(24)
119#define SATA_PLL_CFG0_SEQ_START_STATE BIT(25)
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300120
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300121#define PLLE_MISC_PLLE_PTS BIT(8)
122#define PLLE_MISC_IDDQ_SW_VALUE BIT(13)
123#define PLLE_MISC_IDDQ_SW_CTRL BIT(14)
124#define PLLE_MISC_VREG_BG_CTRL_SHIFT 4
125#define PLLE_MISC_VREG_BG_CTRL_MASK (3 << PLLE_MISC_VREG_BG_CTRL_SHIFT)
126#define PLLE_MISC_VREG_CTRL_SHIFT 2
127#define PLLE_MISC_VREG_CTRL_MASK (2 << PLLE_MISC_VREG_CTRL_SHIFT)
128
129#define PLLCX_MISC_STROBE BIT(31)
130#define PLLCX_MISC_RESET BIT(30)
131#define PLLCX_MISC_SDM_DIV_SHIFT 28
132#define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
133#define PLLCX_MISC_FILT_DIV_SHIFT 26
134#define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
135#define PLLCX_MISC_ALPHA_SHIFT 18
136#define PLLCX_MISC_DIV_LOW_RANGE \
137 ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \
138 (0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
139#define PLLCX_MISC_DIV_HIGH_RANGE \
140 ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \
141 (0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
142#define PLLCX_MISC_COEF_LOW_RANGE \
143 ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT))
144#define PLLCX_MISC_KA_SHIFT 2
145#define PLLCX_MISC_KB_SHIFT 9
146#define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \
147 (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
148 PLLCX_MISC_DIV_LOW_RANGE | \
149 PLLCX_MISC_RESET)
150#define PLLCX_MISC1_DEFAULT 0x000d2308
151#define PLLCX_MISC2_DEFAULT 0x30211200
152#define PLLCX_MISC3_DEFAULT 0x200
153
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530154#define PMC_SATA_PWRGT 0x1ac
155#define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5)
156#define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4)
157
Peter De Schrijver798e9102013-09-09 13:22:55 +0300158#define PLLSS_MISC_KCP 0
159#define PLLSS_MISC_KVCO 0
160#define PLLSS_MISC_SETUP 0
161#define PLLSS_EN_SDM 0
162#define PLLSS_EN_SSC 0
163#define PLLSS_EN_DITHER2 0
164#define PLLSS_EN_DITHER 1
165#define PLLSS_SDM_RESET 0
166#define PLLSS_CLAMP 0
167#define PLLSS_SDM_SSC_MAX 0
168#define PLLSS_SDM_SSC_MIN 0
169#define PLLSS_SDM_SSC_STEP 0
170#define PLLSS_SDM_DIN 0
171#define PLLSS_MISC_DEFAULT ((PLLSS_MISC_KCP << 25) | \
172 (PLLSS_MISC_KVCO << 24) | \
173 PLLSS_MISC_SETUP)
174#define PLLSS_CFG_DEFAULT ((PLLSS_EN_SDM << 31) | \
175 (PLLSS_EN_SSC << 30) | \
176 (PLLSS_EN_DITHER2 << 29) | \
177 (PLLSS_EN_DITHER << 28) | \
178 (PLLSS_SDM_RESET) << 27 | \
179 (PLLSS_CLAMP << 22))
180#define PLLSS_CTRL1_DEFAULT \
181 ((PLLSS_SDM_SSC_MAX << 16) | PLLSS_SDM_SSC_MIN)
182#define PLLSS_CTRL2_DEFAULT \
183 ((PLLSS_SDM_SSC_STEP << 16) | PLLSS_SDM_DIN)
184#define PLLSS_LOCK_OVERRIDE BIT(24)
185#define PLLSS_REF_SRC_SEL_SHIFT 25
186#define PLLSS_REF_SRC_SEL_MASK (3 << PLLSS_REF_SRC_SEL_SHIFT)
187
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530188#define pll_readl(offset, p) readl_relaxed(p->clk_base + offset)
189#define pll_readl_base(p) pll_readl(p->params->base_reg, p)
190#define pll_readl_misc(p) pll_readl(p->params->misc_reg, p)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300191#define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400192#define pll_readl_sdm_din(p) pll_readl(p->params->sdm_din_reg, p)
193#define pll_readl_sdm_ctrl(p) pll_readl(p->params->sdm_ctrl_reg, p)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530194
195#define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
196#define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
197#define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300198#define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400199#define pll_writel_sdm_din(val, p) pll_writel(val, p->params->sdm_din_reg, p)
200#define pll_writel_sdm_ctrl(val, p) pll_writel(val, p->params->sdm_ctrl_reg, p)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530201
202#define mask(w) ((1 << (w)) - 1)
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300203#define divm_mask(p) mask(p->params->div_nmp->divm_width)
204#define divn_mask(p) mask(p->params->div_nmp->divn_width)
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300205#define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300206 mask(p->params->div_nmp->divp_width))
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400207#define sdm_din_mask(p) p->params->sdm_din_mask
208#define sdm_en_mask(p) p->params->sdm_ctrl_en_mask
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530209
Thierry Redingc61e4e72014-04-04 15:55:14 +0200210#define divm_shift(p) (p)->params->div_nmp->divm_shift
211#define divn_shift(p) (p)->params->div_nmp->divn_shift
212#define divp_shift(p) (p)->params->div_nmp->divp_shift
213
214#define divm_mask_shifted(p) (divm_mask(p) << divm_shift(p))
215#define divn_mask_shifted(p) (divn_mask(p) << divn_shift(p))
216#define divp_mask_shifted(p) (divp_mask(p) << divp_shift(p))
217
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530218#define divm_max(p) (divm_mask(p))
219#define divn_max(p) (divn_mask(p))
220#define divp_max(p) (1 << (divp_mask(p)))
221
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400222#define sdin_din_to_data(din) ((u16)((din) ? : 0xFFFFU))
223#define sdin_data_to_din(dat) (((dat) == 0xFFFFU) ? 0 : (s16)dat)
224
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300225static struct div_nmp default_nmp = {
226 .divn_shift = PLL_BASE_DIVN_SHIFT,
227 .divn_width = PLL_BASE_DIVN_WIDTH,
228 .divm_shift = PLL_BASE_DIVM_SHIFT,
229 .divm_width = PLL_BASE_DIVM_WIDTH,
230 .divp_shift = PLL_BASE_DIVP_SHIFT,
231 .divp_width = PLL_BASE_DIVP_WIDTH,
232};
233
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530234static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
235{
236 u32 val;
237
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300238 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530239 return;
240
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300241 if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
Peter De Schrijver7ba28812013-04-03 17:40:38 +0300242 return;
243
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530244 val = pll_readl_misc(pll);
245 val |= BIT(pll->params->lock_enable_bit_idx);
246 pll_writel_misc(val, pll);
247}
248
Peter De Schrijverdba40722013-04-03 17:40:36 +0300249static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530250{
251 int i;
Peter De Schrijver3e727712013-04-03 17:40:40 +0300252 u32 val, lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300253 void __iomem *lock_addr;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530254
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300255 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530256 udelay(pll->params->lock_delay);
257 return 0;
258 }
259
Peter De Schrijverdba40722013-04-03 17:40:36 +0300260 lock_addr = pll->clk_base;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300261 if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300262 lock_addr += pll->params->misc_reg;
263 else
264 lock_addr += pll->params->base_reg;
265
Peter De Schrijver3e727712013-04-03 17:40:40 +0300266 lock_mask = pll->params->lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300267
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530268 for (i = 0; i < pll->params->lock_delay; i++) {
269 val = readl_relaxed(lock_addr);
Peter De Schrijver3e727712013-04-03 17:40:40 +0300270 if ((val & lock_mask) == lock_mask) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530271 udelay(PLL_POST_LOCK_DELAY);
272 return 0;
273 }
274 udelay(2); /* timeout = 2 * lock time */
275 }
276
277 pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700278 clk_hw_get_name(&pll->hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530279
280 return -1;
281}
282
Rhyland Klein6583a632015-06-18 17:28:19 -0400283int tegra_pll_wait_for_lock(struct tegra_clk_pll *pll)
284{
285 return clk_pll_wait_for_lock(pll);
286}
287
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530288static int clk_pll_is_enabled(struct clk_hw *hw)
289{
290 struct tegra_clk_pll *pll = to_clk_pll(hw);
291 u32 val;
292
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300293 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530294 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
295 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
296 return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
297 }
298
299 val = pll_readl_base(pll);
300
301 return val & PLL_BASE_ENABLE ? 1 : 0;
302}
303
Peter De Schrijverdba40722013-04-03 17:40:36 +0300304static void _clk_pll_enable(struct clk_hw *hw)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530305{
306 struct tegra_clk_pll *pll = to_clk_pll(hw);
307 u32 val;
308
Rhyland Klein7db864c2015-06-18 17:28:20 -0400309 if (pll->params->iddq_reg) {
310 val = pll_readl(pll->params->iddq_reg, pll);
311 val &= ~BIT(pll->params->iddq_bit_idx);
312 pll_writel(val, pll->params->iddq_reg, pll);
313 udelay(2);
314 }
315
Bill Huangfde207e2015-06-18 17:28:26 -0400316 if (pll->params->reset_reg) {
317 val = pll_readl(pll->params->reset_reg, pll);
318 val &= ~BIT(pll->params->reset_bit_idx);
319 pll_writel(val, pll->params->reset_reg, pll);
320 }
321
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530322 clk_pll_enable_lock(pll);
323
324 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300325 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300326 val &= ~PLL_BASE_BYPASS;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530327 val |= PLL_BASE_ENABLE;
328 pll_writel_base(val, pll);
329
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300330 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530331 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
332 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
333 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
334 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530335}
336
337static void _clk_pll_disable(struct clk_hw *hw)
338{
339 struct tegra_clk_pll *pll = to_clk_pll(hw);
340 u32 val;
341
342 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300343 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300344 val &= ~PLL_BASE_BYPASS;
345 val &= ~PLL_BASE_ENABLE;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530346 pll_writel_base(val, pll);
347
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300348 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530349 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
350 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
351 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
352 }
Rhyland Klein7db864c2015-06-18 17:28:20 -0400353
Bill Huangfde207e2015-06-18 17:28:26 -0400354 if (pll->params->reset_reg) {
355 val = pll_readl(pll->params->reset_reg, pll);
356 val |= BIT(pll->params->reset_bit_idx);
357 pll_writel(val, pll->params->reset_reg, pll);
358 }
359
Rhyland Klein7db864c2015-06-18 17:28:20 -0400360 if (pll->params->iddq_reg) {
361 val = pll_readl(pll->params->iddq_reg, pll);
362 val |= BIT(pll->params->iddq_bit_idx);
363 pll_writel(val, pll->params->iddq_reg, pll);
364 udelay(2);
365 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530366}
367
368static int clk_pll_enable(struct clk_hw *hw)
369{
370 struct tegra_clk_pll *pll = to_clk_pll(hw);
371 unsigned long flags = 0;
372 int ret;
373
374 if (pll->lock)
375 spin_lock_irqsave(pll->lock, flags);
376
Peter De Schrijverdba40722013-04-03 17:40:36 +0300377 _clk_pll_enable(hw);
378
379 ret = clk_pll_wait_for_lock(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530380
381 if (pll->lock)
382 spin_unlock_irqrestore(pll->lock, flags);
383
384 return ret;
385}
386
387static void clk_pll_disable(struct clk_hw *hw)
388{
389 struct tegra_clk_pll *pll = to_clk_pll(hw);
390 unsigned long flags = 0;
391
392 if (pll->lock)
393 spin_lock_irqsave(pll->lock, flags);
394
395 _clk_pll_disable(hw);
396
397 if (pll->lock)
398 spin_unlock_irqrestore(pll->lock, flags);
399}
400
Peter De Schrijver053b5252013-06-05 15:56:41 +0300401static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
402{
403 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100404 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300405
406 if (p_tohw) {
407 while (p_tohw->pdiv) {
408 if (p_div <= p_tohw->pdiv)
409 return p_tohw->hw_val;
410 p_tohw++;
411 }
412 return -EINVAL;
413 }
414 return -EINVAL;
415}
416
Rhyland Klein6b301a02015-06-18 17:28:36 -0400417int tegra_pll_p_div_to_hw(struct tegra_clk_pll *pll, u8 p_div)
418{
419 return _p_div_to_hw(&pll->hw, p_div);
420}
421
Peter De Schrijver053b5252013-06-05 15:56:41 +0300422static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
423{
424 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100425 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300426
427 if (p_tohw) {
428 while (p_tohw->pdiv) {
429 if (p_div_hw == p_tohw->hw_val)
430 return p_tohw->pdiv;
431 p_tohw++;
432 }
433 return -EINVAL;
434 }
435
436 return 1 << p_div_hw;
437}
438
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530439static int _get_table_rate(struct clk_hw *hw,
440 struct tegra_clk_pll_freq_table *cfg,
441 unsigned long rate, unsigned long parent_rate)
442{
443 struct tegra_clk_pll *pll = to_clk_pll(hw);
444 struct tegra_clk_pll_freq_table *sel;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400445 int p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530446
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300447 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530448 if (sel->input_rate == parent_rate &&
449 sel->output_rate == rate)
450 break;
451
452 if (sel->input_rate == 0)
453 return -EINVAL;
454
Rhyland Klein86c679a2015-06-18 17:28:34 -0400455 if (pll->params->pdiv_tohw) {
456 p = _p_div_to_hw(hw, sel->p);
457 if (p < 0)
458 return p;
459 } else {
460 p = ilog2(sel->p);
461 }
462
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530463 cfg->input_rate = sel->input_rate;
464 cfg->output_rate = sel->output_rate;
465 cfg->m = sel->m;
466 cfg->n = sel->n;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400467 cfg->p = p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530468 cfg->cpcon = sel->cpcon;
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400469 cfg->sdm_data = sel->sdm_data;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530470
471 return 0;
472}
473
474static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
475 unsigned long rate, unsigned long parent_rate)
476{
477 struct tegra_clk_pll *pll = to_clk_pll(hw);
478 unsigned long cfreq;
479 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300480 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530481
482 switch (parent_rate) {
483 case 12000000:
484 case 26000000:
485 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
486 break;
487 case 13000000:
488 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
489 break;
490 case 16800000:
491 case 19200000:
492 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
493 break;
494 case 9600000:
495 case 28800000:
496 /*
497 * PLL_P_OUT1 rate is not listed in PLLA table
498 */
Thierry Redinge52d7c02015-11-18 14:04:20 +0100499 cfreq = parent_rate / (parent_rate / 1000000);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530500 break;
501 default:
502 pr_err("%s Unexpected reference rate %lu\n",
503 __func__, parent_rate);
504 BUG();
505 }
506
507 /* Raise VCO to guarantee 0.5% accuracy */
508 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
509 cfg->output_rate <<= 1)
510 p_div++;
511
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530512 cfg->m = parent_rate / cfreq;
513 cfg->n = cfg->output_rate / cfreq;
514 cfg->cpcon = OUT_OF_TABLE_CPCON;
515
516 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300517 (1 << p_div) > divp_max(pll)
518 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530519 return -EINVAL;
520 }
521
Thierry Reding00c674e2013-11-18 16:11:35 +0100522 cfg->output_rate >>= p_div;
523
Peter De Schrijver053b5252013-06-05 15:56:41 +0300524 if (pll->params->pdiv_tohw) {
525 ret = _p_div_to_hw(hw, 1 << p_div);
526 if (ret < 0)
527 return ret;
528 else
529 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300530 } else
531 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300532
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530533 return 0;
534}
535
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400536/*
537 * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number
538 * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as
539 * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used
540 * to indicate that SDM is disabled.
541 *
542 * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13
543 */
544static void clk_pll_set_sdm_data(struct clk_hw *hw,
545 struct tegra_clk_pll_freq_table *cfg)
546{
547 struct tegra_clk_pll *pll = to_clk_pll(hw);
548 u32 val;
549 bool enabled;
550
551 if (!pll->params->sdm_din_reg)
552 return;
553
554 if (cfg->sdm_data) {
555 val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll));
556 val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll);
557 pll_writel_sdm_din(val, pll);
558 }
559
560 val = pll_readl_sdm_ctrl(pll);
561 enabled = (val & sdm_en_mask(pll));
562
563 if (cfg->sdm_data == 0 && enabled)
564 val &= ~pll->params->sdm_ctrl_en_mask;
565
566 if (cfg->sdm_data != 0 && !enabled)
567 val |= pll->params->sdm_ctrl_en_mask;
568
569 pll_writel_sdm_ctrl(val, pll);
570}
571
Peter De Schrijverdba40722013-04-03 17:40:36 +0300572static void _update_pll_mnp(struct tegra_clk_pll *pll,
573 struct tegra_clk_pll_freq_table *cfg)
574{
575 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300576 struct tegra_clk_pll_params *params = pll->params;
577 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300578
Rhyland Klein69297152015-06-18 17:28:29 -0400579 if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300580 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
581 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
582 val = pll_override_readl(params->pmc_divp_reg, pll);
583 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
584 val |= cfg->p << div_nmp->override_divp_shift;
585 pll_override_writel(val, params->pmc_divp_reg, pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300586
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300587 val = pll_override_readl(params->pmc_divnm_reg, pll);
588 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
589 ~(divn_mask(pll) << div_nmp->override_divn_shift);
590 val |= (cfg->m << div_nmp->override_divm_shift) |
591 (cfg->n << div_nmp->override_divn_shift);
592 pll_override_writel(val, params->pmc_divnm_reg, pll);
593 } else {
594 val = pll_readl_base(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300595
Thierry Redingc61e4e72014-04-04 15:55:14 +0200596 val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
597 divp_mask_shifted(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300598
Thierry Redingc61e4e72014-04-04 15:55:14 +0200599 val |= (cfg->m << divm_shift(pll)) |
600 (cfg->n << divn_shift(pll)) |
601 (cfg->p << divp_shift(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300602
603 pll_writel_base(val, pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400604
605 clk_pll_set_sdm_data(&pll->hw, cfg);
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300606 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300607}
608
609static void _get_pll_mnp(struct tegra_clk_pll *pll,
610 struct tegra_clk_pll_freq_table *cfg)
611{
612 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300613 struct tegra_clk_pll_params *params = pll->params;
614 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300615
Rhyland Klein69297152015-06-18 17:28:29 -0400616 if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300617 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
618 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
619 val = pll_override_readl(params->pmc_divp_reg, pll);
620 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300621
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300622 val = pll_override_readl(params->pmc_divnm_reg, pll);
623 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
624 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
625 } else {
626 val = pll_readl_base(pll);
627
628 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
629 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
630 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400631
632 if (pll->params->sdm_din_reg) {
633 if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) {
634 val = pll_readl_sdm_din(pll);
635 val &= sdm_din_mask(pll);
636 cfg->sdm_data = sdin_din_to_data(val);
637 }
638 }
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300639 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300640}
641
642static void _update_pll_cpcon(struct tegra_clk_pll *pll,
643 struct tegra_clk_pll_freq_table *cfg,
644 unsigned long rate)
645{
646 u32 val;
647
648 val = pll_readl_misc(pll);
649
650 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
651 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
652
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300653 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300654 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
655 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
656 val |= 1 << PLL_MISC_LFCON_SHIFT;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300657 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300658 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
659 if (rate >= (pll->params->vco_max >> 1))
660 val |= 1 << PLL_MISC_DCCON_SHIFT;
661 }
662
663 pll_writel_misc(val, pll);
664}
665
Bill Huang0ef9db62015-06-18 17:28:33 -0400666static void pll_clk_start_ss(struct tegra_clk_pll *pll)
667{
668 if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
669 u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
670
671 val |= pll->params->ssc_ctrl_en_mask;
672 pll_writel(val, pll->params->ssc_ctrl_reg, pll);
673 }
674}
675
676static void pll_clk_stop_ss(struct tegra_clk_pll *pll)
677{
678 if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
679 u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
680
681 val &= ~pll->params->ssc_ctrl_en_mask;
682 pll_writel(val, pll->params->ssc_ctrl_reg, pll);
683 }
684}
685
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530686static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
687 unsigned long rate)
688{
689 struct tegra_clk_pll *pll = to_clk_pll(hw);
Bill Huangb9851142015-06-18 17:28:31 -0400690 struct tegra_clk_pll_freq_table old_cfg;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300691 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530692
693 state = clk_pll_is_enabled(hw);
694
Bill Huangb9851142015-06-18 17:28:31 -0400695 _get_pll_mnp(pll, &old_cfg);
696
Rhyland Klein17e92732015-06-18 17:28:32 -0400697 if (state && pll->params->defaults_set && pll->params->dyn_ramp &&
698 (cfg->m == old_cfg.m) && (cfg->p == old_cfg.p)) {
699 ret = pll->params->dyn_ramp(pll, cfg);
700 if (!ret)
701 return 0;
702 }
703
Bill Huang0ef9db62015-06-18 17:28:33 -0400704 if (state) {
705 pll_clk_stop_ss(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300706 _clk_pll_disable(hw);
Bill Huang0ef9db62015-06-18 17:28:33 -0400707 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530708
Bill Huangb9851142015-06-18 17:28:31 -0400709 if (!pll->params->defaults_set && pll->params->set_defaults)
710 pll->params->set_defaults(pll);
711
Peter De Schrijverdba40722013-04-03 17:40:36 +0300712 _update_pll_mnp(pll, cfg);
713
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300714 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300715 _update_pll_cpcon(pll, cfg, rate);
716
717 if (state) {
718 _clk_pll_enable(hw);
719 ret = clk_pll_wait_for_lock(pll);
Bill Huang0ef9db62015-06-18 17:28:33 -0400720 pll_clk_start_ss(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300721 }
722
723 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530724}
725
726static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
727 unsigned long parent_rate)
728{
729 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300730 struct tegra_clk_pll_freq_table cfg, old_cfg;
731 unsigned long flags = 0;
732 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530733
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300734 if (pll->params->flags & TEGRA_PLL_FIXED) {
735 if (rate != pll->params->fixed_rate) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530736 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700737 __func__, clk_hw_get_name(hw),
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300738 pll->params->fixed_rate, rate);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530739 return -EINVAL;
740 }
741 return 0;
742 }
743
744 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400745 pll->params->calc_rate(hw, &cfg, rate, parent_rate)) {
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100746 pr_err("%s: Failed to set %s rate %lu\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700747 clk_hw_get_name(hw), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300748 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530749 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300750 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300751 if (pll->lock)
752 spin_lock_irqsave(pll->lock, flags);
753
754 _get_pll_mnp(pll, &old_cfg);
Andrew Brestickerafff4552015-06-18 17:28:37 -0400755 if (pll->params->flags & TEGRA_PLL_VCO_OUT)
756 cfg.p = old_cfg.p;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300757
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400758 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p ||
759 old_cfg.sdm_data != cfg.sdm_data)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300760 ret = _program_pll(hw, &cfg, rate);
761
762 if (pll->lock)
763 spin_unlock_irqrestore(pll->lock, flags);
764
765 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530766}
767
768static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
769 unsigned long *prate)
770{
771 struct tegra_clk_pll *pll = to_clk_pll(hw);
772 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530773
Danny Huang267b62a2015-06-18 17:28:27 -0400774 if (pll->params->flags & TEGRA_PLL_FIXED) {
Rhyland Klein69297152015-06-18 17:28:29 -0400775 /* PLLM/MB are used for memory; we do not change rate */
776 if (pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB))
Danny Huang267b62a2015-06-18 17:28:27 -0400777 return clk_hw_get_rate(hw);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300778 return pll->params->fixed_rate;
Danny Huang267b62a2015-06-18 17:28:27 -0400779 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530780
781 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400782 pll->params->calc_rate(hw, &cfg, rate, *prate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530783 return -EINVAL;
784
Peter De Schrijver053b5252013-06-05 15:56:41 +0300785 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530786}
787
788static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
789 unsigned long parent_rate)
790{
791 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300792 struct tegra_clk_pll_freq_table cfg;
793 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530794 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300795 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530796
Peter De Schrijverdba40722013-04-03 17:40:36 +0300797 val = pll_readl_base(pll);
798
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300799 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530800 return parent_rate;
801
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300802 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
Rhyland Klein69297152015-06-18 17:28:29 -0400803 !(pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300804 !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530805 struct tegra_clk_pll_freq_table sel;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300806 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
807 parent_rate)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530808 pr_err("Clock %s has unknown fixed frequency\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700809 clk_hw_get_name(hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530810 BUG();
811 }
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300812 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530813 }
814
Peter De Schrijverdba40722013-04-03 17:40:36 +0300815 _get_pll_mnp(pll, &cfg);
816
Andrew Brestickerafff4552015-06-18 17:28:37 -0400817 if (pll->params->flags & TEGRA_PLL_VCO_OUT) {
Peter De Schrijver053b5252013-06-05 15:56:41 +0300818 pdiv = 1;
Andrew Brestickerafff4552015-06-18 17:28:37 -0400819 } else {
820 pdiv = _hw_to_p_div(hw, cfg.p);
821 if (pdiv < 0) {
822 WARN(1, "Clock %s has invalid pdiv value : 0x%x\n",
823 clk_hw_get_name(hw), cfg.p);
824 pdiv = 1;
825 }
Peter De Schrijver053b5252013-06-05 15:56:41 +0300826 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300827
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400828 if (pll->params->set_gain)
829 pll->params->set_gain(&cfg);
830
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300831 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530832
Peter De Schrijverdba40722013-04-03 17:40:36 +0300833 rate *= cfg.n;
834 do_div(rate, cfg.m);
835
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530836 return rate;
837}
838
839static int clk_plle_training(struct tegra_clk_pll *pll)
840{
841 u32 val;
842 unsigned long timeout;
843
844 if (!pll->pmc)
845 return -ENOSYS;
846
847 /*
848 * PLLE is already disabled, and setup cleared;
849 * create falling edge on PLLE IDDQ input.
850 */
851 val = readl(pll->pmc + PMC_SATA_PWRGT);
852 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
853 writel(val, pll->pmc + PMC_SATA_PWRGT);
854
855 val = readl(pll->pmc + PMC_SATA_PWRGT);
856 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
857 writel(val, pll->pmc + PMC_SATA_PWRGT);
858
859 val = readl(pll->pmc + PMC_SATA_PWRGT);
860 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
861 writel(val, pll->pmc + PMC_SATA_PWRGT);
862
863 val = pll_readl_misc(pll);
864
865 timeout = jiffies + msecs_to_jiffies(100);
866 while (1) {
867 val = pll_readl_misc(pll);
868 if (val & PLLE_MISC_READY)
869 break;
870 if (time_after(jiffies, timeout)) {
871 pr_err("%s: timeout waiting for PLLE\n", __func__);
872 return -EBUSY;
873 }
874 udelay(300);
875 }
876
877 return 0;
878}
879
880static int clk_plle_enable(struct clk_hw *hw)
881{
882 struct tegra_clk_pll *pll = to_clk_pll(hw);
883 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
884 struct tegra_clk_pll_freq_table sel;
885 u32 val;
886 int err;
887
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300888 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530889 return -EINVAL;
890
891 clk_pll_disable(hw);
892
893 val = pll_readl_misc(pll);
894 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
895 pll_writel_misc(val, pll);
896
897 val = pll_readl_misc(pll);
898 if (!(val & PLLE_MISC_READY)) {
899 err = clk_plle_training(pll);
900 if (err)
901 return err;
902 }
903
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300904 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530905 /* configure dividers */
906 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200907 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
908 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200909 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200910 val |= sel.m << divm_shift(pll);
911 val |= sel.n << divn_shift(pll);
912 val |= sel.p << divp_shift(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530913 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
914 pll_writel_base(val, pll);
915 }
916
917 val = pll_readl_misc(pll);
918 val |= PLLE_MISC_SETUP_VALUE;
919 val |= PLLE_MISC_LOCK_ENABLE;
920 pll_writel_misc(val, pll);
921
922 val = readl(pll->clk_base + PLLE_SS_CTRL);
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200923 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530924 val |= PLLE_SS_DISABLE;
925 writel(val, pll->clk_base + PLLE_SS_CTRL);
926
Thierry Reding4ccc4022014-04-04 15:55:15 +0200927 val = pll_readl_base(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530928 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
929 pll_writel_base(val, pll);
930
Peter De Schrijverdba40722013-04-03 17:40:36 +0300931 clk_pll_wait_for_lock(pll);
932
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530933 return 0;
934}
935
936static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
937 unsigned long parent_rate)
938{
939 struct tegra_clk_pll *pll = to_clk_pll(hw);
940 u32 val = pll_readl_base(pll);
941 u32 divn = 0, divm = 0, divp = 0;
942 u64 rate = parent_rate;
943
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300944 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
945 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
946 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530947 divm *= divp;
948
949 rate *= divn;
950 do_div(rate, divm);
951 return rate;
952}
953
954const struct clk_ops tegra_clk_pll_ops = {
955 .is_enabled = clk_pll_is_enabled,
956 .enable = clk_pll_enable,
957 .disable = clk_pll_disable,
958 .recalc_rate = clk_pll_recalc_rate,
959 .round_rate = clk_pll_round_rate,
960 .set_rate = clk_pll_set_rate,
961};
962
963const struct clk_ops tegra_clk_plle_ops = {
964 .recalc_rate = clk_plle_recalc_rate,
965 .is_enabled = clk_pll_is_enabled,
966 .disable = clk_pll_disable,
967 .enable = clk_plle_enable,
968};
969
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300970static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
971 unsigned long parent_rate)
972{
Rhyland Klein407254d2015-06-18 17:28:25 -0400973 u16 mdiv = parent_rate / pll_params->cf_min;
974
975 if (pll_params->flags & TEGRA_MDIV_NEW)
976 return (!pll_params->mdiv_default ? mdiv :
977 min(mdiv, pll_params->mdiv_default));
978
979 if (pll_params->mdiv_default)
980 return pll_params->mdiv_default;
981
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300982 if (parent_rate > pll_params->cf_max)
983 return 2;
984 else
985 return 1;
986}
987
Rhyland Klein86c679a2015-06-18 17:28:34 -0400988static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
989 struct tegra_clk_pll_freq_table *cfg,
990 unsigned long rate, unsigned long parent_rate)
991{
992 struct tegra_clk_pll *pll = to_clk_pll(hw);
993 unsigned int p;
994 int p_div;
995
996 if (!rate)
997 return -EINVAL;
998
999 p = DIV_ROUND_UP(pll->params->vco_min, rate);
1000 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
1001 cfg->output_rate = rate * p;
1002 cfg->n = cfg->output_rate * cfg->m / parent_rate;
1003 cfg->input_rate = parent_rate;
1004
1005 p_div = _p_div_to_hw(hw, p);
1006 if (p_div < 0)
1007 return p_div;
1008
1009 cfg->p = p_div;
1010
1011 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
1012 return -EINVAL;
1013
1014 return 0;
1015}
1016
1017#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1018 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
Rhyland Kleindd322f02015-06-18 17:28:28 -04001019 defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1020 defined(CONFIG_ARCH_TEGRA_210_SOC)
Rhyland Klein86c679a2015-06-18 17:28:34 -04001021
Rhyland Klein407254d2015-06-18 17:28:25 -04001022u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate)
1023{
1024 struct tegra_clk_pll *pll = to_clk_pll(hw);
1025
1026 return (u16)_pll_fixed_mdiv(pll->params, input_rate);
1027}
1028
Peter De Schrijver04edb092013-09-06 14:37:37 +03001029static unsigned long _clip_vco_min(unsigned long vco_min,
1030 unsigned long parent_rate)
1031{
1032 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
1033}
1034
1035static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
1036 void __iomem *clk_base,
1037 unsigned long parent_rate)
1038{
1039 u32 val;
1040 u32 step_a, step_b;
1041
1042 switch (parent_rate) {
1043 case 12000000:
1044 case 13000000:
1045 case 26000000:
1046 step_a = 0x2B;
1047 step_b = 0x0B;
1048 break;
1049 case 16800000:
1050 step_a = 0x1A;
1051 step_b = 0x09;
1052 break;
1053 case 19200000:
1054 step_a = 0x12;
1055 step_b = 0x08;
1056 break;
1057 default:
1058 pr_err("%s: Unexpected reference rate %lu\n",
1059 __func__, parent_rate);
1060 WARN_ON(1);
1061 return -EINVAL;
1062 }
1063
1064 val = step_a << pll_params->stepa_shift;
1065 val |= step_b << pll_params->stepb_shift;
1066 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
1067
1068 return 0;
1069}
1070
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001071static int _pll_ramp_calc_pll(struct clk_hw *hw,
1072 struct tegra_clk_pll_freq_table *cfg,
1073 unsigned long rate, unsigned long parent_rate)
1074{
1075 struct tegra_clk_pll *pll = to_clk_pll(hw);
Rhyland Klein86c679a2015-06-18 17:28:34 -04001076 int err = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001077
1078 err = _get_table_rate(hw, cfg, rate, parent_rate);
1079 if (err < 0)
1080 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +03001081 else {
1082 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001083 WARN_ON(1);
1084 err = -EINVAL;
1085 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001086 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001087 }
1088
Peter De Schrijver053b5252013-06-05 15:56:41 +03001089 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001090 err = -EINVAL;
1091
1092out:
1093 return err;
1094}
1095
1096static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
1097 unsigned long parent_rate)
1098{
1099 struct tegra_clk_pll *pll = to_clk_pll(hw);
1100 struct tegra_clk_pll_freq_table cfg, old_cfg;
1101 unsigned long flags = 0;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001102 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001103
1104 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1105 if (ret < 0)
1106 return ret;
1107
1108 if (pll->lock)
1109 spin_lock_irqsave(pll->lock, flags);
1110
1111 _get_pll_mnp(pll, &old_cfg);
Andrew Brestickerafff4552015-06-18 17:28:37 -04001112 if (pll->params->flags & TEGRA_PLL_VCO_OUT)
1113 cfg.p = old_cfg.p;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001114
Peter De Schrijver053b5252013-06-05 15:56:41 +03001115 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001116 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001117
1118 if (pll->lock)
1119 spin_unlock_irqrestore(pll->lock, flags);
1120
1121 return ret;
1122}
1123
1124static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1125 unsigned long *prate)
1126{
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001127 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001128 struct tegra_clk_pll_freq_table cfg;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001129 int ret, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001130 u64 output_rate = *prate;
1131
1132 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1133 if (ret < 0)
1134 return ret;
1135
Peter De Schrijver053b5252013-06-05 15:56:41 +03001136 p_div = _hw_to_p_div(hw, cfg.p);
1137 if (p_div < 0)
1138 return p_div;
1139
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001140 if (pll->params->set_gain)
1141 pll->params->set_gain(&cfg);
1142
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001143 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001144 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001145
1146 return output_rate;
1147}
1148
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001149static void _pllcx_strobe(struct tegra_clk_pll *pll)
1150{
1151 u32 val;
1152
1153 val = pll_readl_misc(pll);
1154 val |= PLLCX_MISC_STROBE;
1155 pll_writel_misc(val, pll);
1156 udelay(2);
1157
1158 val &= ~PLLCX_MISC_STROBE;
1159 pll_writel_misc(val, pll);
1160}
1161
1162static int clk_pllc_enable(struct clk_hw *hw)
1163{
1164 struct tegra_clk_pll *pll = to_clk_pll(hw);
1165 u32 val;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001166 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001167 unsigned long flags = 0;
1168
1169 if (pll->lock)
1170 spin_lock_irqsave(pll->lock, flags);
1171
1172 _clk_pll_enable(hw);
1173 udelay(2);
1174
1175 val = pll_readl_misc(pll);
1176 val &= ~PLLCX_MISC_RESET;
1177 pll_writel_misc(val, pll);
1178 udelay(2);
1179
1180 _pllcx_strobe(pll);
1181
1182 ret = clk_pll_wait_for_lock(pll);
1183
1184 if (pll->lock)
1185 spin_unlock_irqrestore(pll->lock, flags);
1186
1187 return ret;
1188}
1189
1190static void _clk_pllc_disable(struct clk_hw *hw)
1191{
1192 struct tegra_clk_pll *pll = to_clk_pll(hw);
1193 u32 val;
1194
1195 _clk_pll_disable(hw);
1196
1197 val = pll_readl_misc(pll);
1198 val |= PLLCX_MISC_RESET;
1199 pll_writel_misc(val, pll);
1200 udelay(2);
1201}
1202
1203static void clk_pllc_disable(struct clk_hw *hw)
1204{
1205 struct tegra_clk_pll *pll = to_clk_pll(hw);
1206 unsigned long flags = 0;
1207
1208 if (pll->lock)
1209 spin_lock_irqsave(pll->lock, flags);
1210
1211 _clk_pllc_disable(hw);
1212
1213 if (pll->lock)
1214 spin_unlock_irqrestore(pll->lock, flags);
1215}
1216
1217static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1218 unsigned long input_rate, u32 n)
1219{
1220 u32 val, n_threshold;
1221
1222 switch (input_rate) {
1223 case 12000000:
1224 n_threshold = 70;
1225 break;
1226 case 13000000:
1227 case 26000000:
1228 n_threshold = 71;
1229 break;
1230 case 16800000:
1231 n_threshold = 55;
1232 break;
1233 case 19200000:
1234 n_threshold = 48;
1235 break;
1236 default:
1237 pr_err("%s: Unexpected reference rate %lu\n",
1238 __func__, input_rate);
1239 return -EINVAL;
1240 }
1241
1242 val = pll_readl_misc(pll);
1243 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1244 val |= n <= n_threshold ?
1245 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1246 pll_writel_misc(val, pll);
1247
1248 return 0;
1249}
1250
1251static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1252 unsigned long parent_rate)
1253{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001254 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001255 struct tegra_clk_pll *pll = to_clk_pll(hw);
1256 unsigned long flags = 0;
1257 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001258
1259 if (pll->lock)
1260 spin_lock_irqsave(pll->lock, flags);
1261
1262 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1263 if (ret < 0)
1264 goto out;
1265
Peter De Schrijver053b5252013-06-05 15:56:41 +03001266 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001267
Peter De Schrijver053b5252013-06-05 15:56:41 +03001268 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001269 WARN_ON(1);
1270 goto out;
1271 }
1272
Peter De Schrijver053b5252013-06-05 15:56:41 +03001273 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001274 goto out;
1275
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001276 state = clk_pll_is_enabled(hw);
1277 if (state)
1278 _clk_pllc_disable(hw);
1279
1280 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1281 if (ret < 0)
1282 goto out;
1283
1284 _update_pll_mnp(pll, &cfg);
1285
1286 if (state)
1287 ret = clk_pllc_enable(hw);
1288
1289out:
1290 if (pll->lock)
1291 spin_unlock_irqrestore(pll->lock, flags);
1292
1293 return ret;
1294}
1295
1296static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1297 struct tegra_clk_pll_freq_table *cfg,
1298 unsigned long rate, unsigned long parent_rate)
1299{
1300 u16 m, n;
1301 u64 output_rate = parent_rate;
1302
1303 m = _pll_fixed_mdiv(pll->params, parent_rate);
1304 n = rate * m / parent_rate;
1305
1306 output_rate *= n;
1307 do_div(output_rate, m);
1308
1309 if (cfg) {
1310 cfg->m = m;
1311 cfg->n = n;
1312 }
1313
1314 return output_rate;
1315}
Thierry Reding6bb18c52014-08-01 10:44:20 +02001316
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001317static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1318 unsigned long parent_rate)
1319{
1320 struct tegra_clk_pll_freq_table cfg, old_cfg;
1321 struct tegra_clk_pll *pll = to_clk_pll(hw);
1322 unsigned long flags = 0;
1323 int state, ret = 0;
1324
1325 if (pll->lock)
1326 spin_lock_irqsave(pll->lock, flags);
1327
1328 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1329 _get_pll_mnp(pll, &old_cfg);
1330 cfg.p = old_cfg.p;
1331
1332 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1333 state = clk_pll_is_enabled(hw);
1334 if (state)
1335 _clk_pll_disable(hw);
1336
1337 _update_pll_mnp(pll, &cfg);
1338
1339 if (state) {
1340 _clk_pll_enable(hw);
1341 ret = clk_pll_wait_for_lock(pll);
1342 }
1343 }
1344
1345 if (pll->lock)
1346 spin_unlock_irqrestore(pll->lock, flags);
1347
1348 return ret;
1349}
1350
1351static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1352 unsigned long parent_rate)
1353{
1354 struct tegra_clk_pll_freq_table cfg;
1355 struct tegra_clk_pll *pll = to_clk_pll(hw);
1356 u64 rate = parent_rate;
1357
1358 _get_pll_mnp(pll, &cfg);
1359
1360 rate *= cfg.n;
1361 do_div(rate, cfg.m);
1362
1363 return rate;
1364}
1365
1366static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1367 unsigned long *prate)
1368{
1369 struct tegra_clk_pll *pll = to_clk_pll(hw);
1370
1371 return _pllre_calc_rate(pll, NULL, rate, *prate);
1372}
1373
1374static int clk_plle_tegra114_enable(struct clk_hw *hw)
1375{
1376 struct tegra_clk_pll *pll = to_clk_pll(hw);
1377 struct tegra_clk_pll_freq_table sel;
1378 u32 val;
1379 int ret;
1380 unsigned long flags = 0;
1381 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1382
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001383 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001384 return -EINVAL;
1385
1386 if (pll->lock)
1387 spin_lock_irqsave(pll->lock, flags);
1388
1389 val = pll_readl_base(pll);
1390 val &= ~BIT(29); /* Disable lock override */
1391 pll_writel_base(val, pll);
1392
1393 val = pll_readl(pll->params->aux_reg, pll);
1394 val |= PLLE_AUX_ENABLE_SWCTL;
1395 val &= ~PLLE_AUX_SEQ_ENABLE;
1396 pll_writel(val, pll->params->aux_reg, pll);
1397 udelay(1);
1398
1399 val = pll_readl_misc(pll);
1400 val |= PLLE_MISC_LOCK_ENABLE;
1401 val |= PLLE_MISC_IDDQ_SW_CTRL;
1402 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1403 val |= PLLE_MISC_PLLE_PTS;
1404 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1405 pll_writel_misc(val, pll);
1406 udelay(5);
1407
1408 val = pll_readl(PLLE_SS_CTRL, pll);
1409 val |= PLLE_SS_DISABLE;
1410 pll_writel(val, PLLE_SS_CTRL, pll);
1411
1412 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001413 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1414 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001415 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001416 val |= sel.m << divm_shift(pll);
1417 val |= sel.n << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001418 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1419 pll_writel_base(val, pll);
1420 udelay(1);
1421
1422 _clk_pll_enable(hw);
1423 ret = clk_pll_wait_for_lock(pll);
1424
1425 if (ret < 0)
1426 goto out;
1427
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001428 val = pll_readl(PLLE_SS_CTRL, pll);
1429 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1430 val &= ~PLLE_SS_COEFFICIENTS_MASK;
1431 val |= PLLE_SS_COEFFICIENTS_VAL;
1432 pll_writel(val, PLLE_SS_CTRL, pll);
1433 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1434 pll_writel(val, PLLE_SS_CTRL, pll);
1435 udelay(1);
1436 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1437 pll_writel(val, PLLE_SS_CTRL, pll);
1438 udelay(1);
1439
Jim Lin2cfe1672014-05-14 17:32:57 -07001440 /* Enable hw control of xusb brick pll */
1441 val = pll_readl_misc(pll);
1442 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1443 pll_writel_misc(val, pll);
1444
1445 val = pll_readl(pll->params->aux_reg, pll);
1446 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1447 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1448 pll_writel(val, pll->params->aux_reg, pll);
1449 udelay(1);
1450 val |= PLLE_AUX_SEQ_ENABLE;
1451 pll_writel(val, pll->params->aux_reg, pll);
1452
1453 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1454 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1455 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1456 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1457 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1458 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1459 udelay(1);
1460 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1461 pll_writel(val, XUSBIO_PLL_CFG0, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001462
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001463 /* Enable hw control of SATA pll */
1464 val = pll_readl(SATA_PLL_CFG0, pll);
1465 val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +02001466 val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1467 val |= SATA_PLL_CFG0_SEQ_START_STATE;
1468 pll_writel(val, SATA_PLL_CFG0, pll);
1469
1470 udelay(1);
1471
1472 val = pll_readl(SATA_PLL_CFG0, pll);
1473 val |= SATA_PLL_CFG0_SEQ_ENABLE;
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001474 pll_writel(val, SATA_PLL_CFG0, pll);
1475
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001476out:
1477 if (pll->lock)
1478 spin_unlock_irqrestore(pll->lock, flags);
1479
1480 return ret;
1481}
1482
1483static void clk_plle_tegra114_disable(struct clk_hw *hw)
1484{
1485 struct tegra_clk_pll *pll = to_clk_pll(hw);
1486 unsigned long flags = 0;
1487 u32 val;
1488
1489 if (pll->lock)
1490 spin_lock_irqsave(pll->lock, flags);
1491
1492 _clk_pll_disable(hw);
1493
1494 val = pll_readl_misc(pll);
1495 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1496 pll_writel_misc(val, pll);
1497 udelay(1);
1498
1499 if (pll->lock)
1500 spin_unlock_irqrestore(pll->lock, flags);
1501}
1502#endif
1503
Peter De Schrijverdba40722013-04-03 17:40:36 +03001504static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001505 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1506 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301507{
1508 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301509
1510 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1511 if (!pll)
1512 return ERR_PTR(-ENOMEM);
1513
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301514 pll->clk_base = clk_base;
1515 pll->pmc = pmc;
1516
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301517 pll->params = pll_params;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301518 pll->lock = lock;
1519
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001520 if (!pll_params->div_nmp)
1521 pll_params->div_nmp = &default_nmp;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301522
Peter De Schrijverdba40722013-04-03 17:40:36 +03001523 return pll;
1524}
1525
1526static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1527 const char *name, const char *parent_name, unsigned long flags,
1528 const struct clk_ops *ops)
1529{
1530 struct clk_init_data init;
1531
1532 init.name = name;
1533 init.ops = ops;
1534 init.flags = flags;
1535 init.parent_names = (parent_name ? &parent_name : NULL);
1536 init.num_parents = (parent_name ? 1 : 0);
1537
Rhyland Klein407254d2015-06-18 17:28:25 -04001538 /* Default to _calc_rate if unspecified */
Rhyland Klein86c679a2015-06-18 17:28:34 -04001539 if (!pll->params->calc_rate) {
1540 if (pll->params->flags & TEGRA_PLLM)
1541 pll->params->calc_rate = _calc_dynamic_ramp_rate;
1542 else
1543 pll->params->calc_rate = _calc_rate;
1544 }
Rhyland Klein407254d2015-06-18 17:28:25 -04001545
Bill Huangb9851142015-06-18 17:28:31 -04001546 if (pll->params->set_defaults)
1547 pll->params->set_defaults(pll);
1548
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301549 /* Data in .init is copied by clk_register(), so stack variable OK */
1550 pll->hw.init = &init;
1551
Peter De Schrijverdba40722013-04-03 17:40:36 +03001552 return clk_register(NULL, &pll->hw);
1553}
1554
1555struct clk *tegra_clk_register_pll(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)
Peter De Schrijverdba40722013-04-03 17:40:36 +03001559{
1560 struct tegra_clk_pll *pll;
1561 struct clk *clk;
1562
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001563 pll_params->flags |= TEGRA_PLL_BYPASS;
Rhyland Klein3706b432015-06-18 17:28:23 -04001564
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001565 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001566 if (IS_ERR(pll))
1567 return ERR_CAST(pll);
1568
1569 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1570 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301571 if (IS_ERR(clk))
1572 kfree(pll);
1573
1574 return clk;
1575}
1576
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001577static struct div_nmp pll_e_nmp = {
1578 .divn_shift = PLLE_BASE_DIVN_SHIFT,
1579 .divn_width = PLLE_BASE_DIVN_WIDTH,
1580 .divm_shift = PLLE_BASE_DIVM_SHIFT,
1581 .divm_width = PLLE_BASE_DIVM_WIDTH,
1582 .divp_shift = PLLE_BASE_DIVP_SHIFT,
1583 .divp_width = PLLE_BASE_DIVP_WIDTH,
1584};
1585
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301586struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1587 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001588 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1589 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301590{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001591 struct tegra_clk_pll *pll;
1592 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001593
Rhyland Klein3706b432015-06-18 17:28:23 -04001594 pll_params->flags |= TEGRA_PLL_BYPASS;
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001595
1596 if (!pll_params->div_nmp)
1597 pll_params->div_nmp = &pll_e_nmp;
1598
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001599 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001600 if (IS_ERR(pll))
1601 return ERR_CAST(pll);
1602
1603 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1604 &tegra_clk_plle_ops);
1605 if (IS_ERR(clk))
1606 kfree(pll);
1607
1608 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301609}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001610
Paul Walmsley08acae32014-12-16 12:38:29 -08001611#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1612 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
Rhyland Kleindd322f02015-06-18 17:28:28 -04001613 defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1614 defined(CONFIG_ARCH_TEGRA_210_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301615static const struct clk_ops tegra_clk_pllxc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001616 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001617 .enable = clk_pll_enable,
1618 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001619 .recalc_rate = clk_pll_recalc_rate,
1620 .round_rate = clk_pll_ramp_round_rate,
1621 .set_rate = clk_pllxc_set_rate,
1622};
1623
Sachin Kamate47e12f2013-10-08 16:47:41 +05301624static const struct clk_ops tegra_clk_pllc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001625 .is_enabled = clk_pll_is_enabled,
1626 .enable = clk_pllc_enable,
1627 .disable = clk_pllc_disable,
1628 .recalc_rate = clk_pll_recalc_rate,
1629 .round_rate = clk_pll_ramp_round_rate,
1630 .set_rate = clk_pllc_set_rate,
1631};
1632
Sachin Kamate47e12f2013-10-08 16:47:41 +05301633static const struct clk_ops tegra_clk_pllre_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001634 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001635 .enable = clk_pll_enable,
1636 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001637 .recalc_rate = clk_pllre_recalc_rate,
1638 .round_rate = clk_pllre_round_rate,
1639 .set_rate = clk_pllre_set_rate,
1640};
1641
Sachin Kamate47e12f2013-10-08 16:47:41 +05301642static const struct clk_ops tegra_clk_plle_tegra114_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001643 .is_enabled = clk_pll_is_enabled,
1644 .enable = clk_plle_tegra114_enable,
1645 .disable = clk_plle_tegra114_disable,
1646 .recalc_rate = clk_pll_recalc_rate,
1647};
1648
1649
1650struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1651 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001652 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001653 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001654 spinlock_t *lock)
1655{
1656 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001657 struct clk *clk, *parent;
1658 unsigned long parent_rate;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001659 u32 val, val_iddq;
1660
1661 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001662 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001663 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001664 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001665 return ERR_PTR(-EINVAL);
1666 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001667
1668 if (!pll_params->pdiv_tohw)
1669 return ERR_PTR(-EINVAL);
1670
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001671 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001672
1673 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1674
Bill Huangb5512b42015-06-18 17:28:30 -04001675 if (pll_params->adjust_vco)
1676 pll_params->vco_min = pll_params->adjust_vco(pll_params,
1677 parent_rate);
1678
Bill Huangb9851142015-06-18 17:28:31 -04001679 /*
1680 * If the pll has a set_defaults callback, it will take care of
1681 * configuring dynamic ramping and setting IDDQ in that path.
1682 */
1683 if (!pll_params->set_defaults) {
1684 int err;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001685
Bill Huangb9851142015-06-18 17:28:31 -04001686 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
1687 if (err)
1688 return ERR_PTR(err);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001689
Bill Huangb9851142015-06-18 17:28:31 -04001690 val = readl_relaxed(clk_base + pll_params->base_reg);
1691 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
1692
1693 if (val & PLL_BASE_ENABLE)
1694 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
1695 else {
1696 val_iddq |= BIT(pll_params->iddq_bit_idx);
1697 writel_relaxed(val_iddq,
1698 clk_base + pll_params->iddq_reg);
1699 }
Peter De Schrijver04edb092013-09-06 14:37:37 +03001700 }
1701
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001702 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001703 if (IS_ERR(pll))
1704 return ERR_CAST(pll);
1705
1706 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1707 &tegra_clk_pllxc_ops);
1708 if (IS_ERR(clk))
1709 kfree(pll);
1710
1711 return clk;
1712}
1713
1714struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1715 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001716 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001717 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001718 spinlock_t *lock, unsigned long parent_rate)
1719{
1720 u32 val;
1721 struct tegra_clk_pll *pll;
1722 struct clk *clk;
1723
Peter De Schrijver04edb092013-09-06 14:37:37 +03001724 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1725
Bill Huangb5512b42015-06-18 17:28:30 -04001726 if (pll_params->adjust_vco)
1727 pll_params->vco_min = pll_params->adjust_vco(pll_params,
1728 parent_rate);
1729
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001730 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001731 if (IS_ERR(pll))
1732 return ERR_CAST(pll);
1733
1734 /* program minimum rate by default */
1735
1736 val = pll_readl_base(pll);
1737 if (val & PLL_BASE_ENABLE)
1738 WARN_ON(val & pll_params->iddq_bit_idx);
1739 else {
1740 int m;
1741
1742 m = _pll_fixed_mdiv(pll_params, parent_rate);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001743 val = m << divm_shift(pll);
1744 val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001745 pll_writel_base(val, pll);
1746 }
1747
1748 /* disable lock override */
1749
1750 val = pll_readl_misc(pll);
1751 val &= ~BIT(29);
1752 pll_writel_misc(val, pll);
1753
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001754 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1755 &tegra_clk_pllre_ops);
1756 if (IS_ERR(clk))
1757 kfree(pll);
1758
1759 return clk;
1760}
1761
1762struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1763 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001764 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001765 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001766 spinlock_t *lock)
1767{
1768 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001769 struct clk *clk, *parent;
1770 unsigned long parent_rate;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001771
1772 if (!pll_params->pdiv_tohw)
1773 return ERR_PTR(-EINVAL);
1774
Peter De Schrijver04edb092013-09-06 14:37:37 +03001775 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001776 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001777 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001778 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001779 return ERR_PTR(-EINVAL);
1780 }
1781
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001782 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001783
1784 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1785
Bill Huangb5512b42015-06-18 17:28:30 -04001786 if (pll_params->adjust_vco)
1787 pll_params->vco_min = pll_params->adjust_vco(pll_params,
1788 parent_rate);
1789
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001790 pll_params->flags |= TEGRA_PLL_BYPASS;
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001791 pll_params->flags |= TEGRA_PLLM;
1792 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001793 if (IS_ERR(pll))
1794 return ERR_CAST(pll);
1795
1796 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
Danny Huang267b62a2015-06-18 17:28:27 -04001797 &tegra_clk_pll_ops);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001798 if (IS_ERR(clk))
1799 kfree(pll);
1800
1801 return clk;
1802}
1803
1804struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1805 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001806 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001807 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001808 spinlock_t *lock)
1809{
1810 struct clk *parent, *clk;
Thierry Reding385f9ad2015-11-19 16:34:06 +01001811 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001812 struct tegra_clk_pll *pll;
1813 struct tegra_clk_pll_freq_table cfg;
1814 unsigned long parent_rate;
1815
1816 if (!p_tohw)
1817 return ERR_PTR(-EINVAL);
1818
1819 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001820 if (!parent) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001821 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001822 parent_name, name);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001823 return ERR_PTR(-EINVAL);
1824 }
1825
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001826 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001827
1828 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1829
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001830 pll_params->flags |= TEGRA_PLL_BYPASS;
1831 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001832 if (IS_ERR(pll))
1833 return ERR_CAST(pll);
1834
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001835 /*
1836 * Most of PLLC register fields are shadowed, and can not be read
1837 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1838 * Initialize PLL to default state: disabled, reset; shadow registers
1839 * loaded with default parameters; dividers are preset for half of
1840 * minimum VCO rate (the latter assured that shadowed divider settings
1841 * are within supported range).
1842 */
1843
1844 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1845 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1846
1847 while (p_tohw->pdiv) {
1848 if (p_tohw->pdiv == 2) {
1849 cfg.p = p_tohw->hw_val;
1850 break;
1851 }
1852 p_tohw++;
1853 }
1854
1855 if (!p_tohw->pdiv) {
1856 WARN_ON(1);
1857 return ERR_PTR(-EINVAL);
1858 }
1859
1860 pll_writel_base(0, pll);
1861 _update_pll_mnp(pll, &cfg);
1862
1863 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1864 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1865 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1866 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1867
1868 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1869
1870 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1871 &tegra_clk_pllc_ops);
1872 if (IS_ERR(clk))
1873 kfree(pll);
1874
1875 return clk;
1876}
1877
1878struct clk *tegra_clk_register_plle_tegra114(const char *name,
1879 const char *parent_name,
1880 void __iomem *clk_base, unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001881 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001882 spinlock_t *lock)
1883{
1884 struct tegra_clk_pll *pll;
1885 struct clk *clk;
1886 u32 val, val_aux;
1887
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001888 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001889 if (IS_ERR(pll))
1890 return ERR_CAST(pll);
1891
1892 /* ensure parent is set to pll_re_vco */
1893
1894 val = pll_readl_base(pll);
1895 val_aux = pll_readl(pll_params->aux_reg, pll);
1896
1897 if (val & PLL_BASE_ENABLE) {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001898 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
1899 (val_aux & PLLE_AUX_PLLP_SEL))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001900 WARN(1, "pll_e enabled with unsupported parent %s\n",
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001901 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
1902 "pll_re_vco");
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001903 } else {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02001904 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
Tuomas Tynkkynend2c834a2014-05-16 16:50:20 +03001905 pll_writel(val_aux, pll_params->aux_reg, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001906 }
1907
1908 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1909 &tegra_clk_plle_tegra114_ops);
1910 if (IS_ERR(clk))
1911 kfree(pll);
1912
1913 return clk;
1914}
1915#endif
Peter De Schrijver798e9102013-09-09 13:22:55 +03001916
Paul Walmsley08acae32014-12-16 12:38:29 -08001917#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301918static const struct clk_ops tegra_clk_pllss_ops = {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001919 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001920 .enable = clk_pll_enable,
1921 .disable = clk_pll_disable,
Peter De Schrijver798e9102013-09-09 13:22:55 +03001922 .recalc_rate = clk_pll_recalc_rate,
1923 .round_rate = clk_pll_ramp_round_rate,
1924 .set_rate = clk_pllxc_set_rate,
1925};
1926
1927struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
1928 void __iomem *clk_base, unsigned long flags,
1929 struct tegra_clk_pll_params *pll_params,
1930 spinlock_t *lock)
1931{
1932 struct tegra_clk_pll *pll;
1933 struct clk *clk, *parent;
1934 struct tegra_clk_pll_freq_table cfg;
1935 unsigned long parent_rate;
1936 u32 val;
1937 int i;
1938
1939 if (!pll_params->div_nmp)
1940 return ERR_PTR(-EINVAL);
1941
1942 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001943 if (!parent) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03001944 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001945 parent_name, name);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001946 return ERR_PTR(-EINVAL);
1947 }
1948
Peter De Schrijver798e9102013-09-09 13:22:55 +03001949 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1950 if (IS_ERR(pll))
1951 return ERR_CAST(pll);
1952
1953 val = pll_readl_base(pll);
1954 val &= ~PLLSS_REF_SRC_SEL_MASK;
1955 pll_writel_base(val, pll);
1956
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001957 parent_rate = clk_get_rate(parent);
Peter De Schrijver798e9102013-09-09 13:22:55 +03001958
1959 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1960
1961 /* initialize PLL to minimum rate */
1962
1963 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1964 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1965
1966 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
1967 ;
1968 if (!i) {
1969 kfree(pll);
1970 return ERR_PTR(-EINVAL);
1971 }
1972
1973 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
1974
1975 _update_pll_mnp(pll, &cfg);
1976
1977 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
1978 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
1979 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
1980 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
1981
1982 val = pll_readl_base(pll);
1983 if (val & PLL_BASE_ENABLE) {
1984 if (val & BIT(pll_params->iddq_bit_idx)) {
1985 WARN(1, "%s is on but IDDQ set\n", name);
1986 kfree(pll);
1987 return ERR_PTR(-EINVAL);
1988 }
1989 } else
1990 val |= BIT(pll_params->iddq_bit_idx);
1991
1992 val &= ~PLLSS_LOCK_OVERRIDE;
1993 pll_writel_base(val, pll);
1994
1995 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1996 &tegra_clk_pllss_ops);
1997
1998 if (IS_ERR(clk))
1999 kfree(pll);
2000
2001 return clk;
2002}
2003#endif
Rhyland Kleindd322f02015-06-18 17:28:28 -04002004
2005#if defined(CONFIG_ARCH_TEGRA_210_SOC)
2006static int clk_plle_tegra210_enable(struct clk_hw *hw)
2007{
2008 struct tegra_clk_pll *pll = to_clk_pll(hw);
2009 struct tegra_clk_pll_freq_table sel;
2010 u32 val;
2011 int ret;
2012 unsigned long flags = 0;
2013 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
2014
2015 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
2016 return -EINVAL;
2017
2018 if (pll->lock)
2019 spin_lock_irqsave(pll->lock, flags);
2020
2021 val = pll_readl_base(pll);
2022 val &= ~BIT(30); /* Disable lock override */
2023 pll_writel_base(val, pll);
2024
2025 val = pll_readl(pll->params->aux_reg, pll);
2026 val |= PLLE_AUX_ENABLE_SWCTL;
2027 val &= ~PLLE_AUX_SEQ_ENABLE;
2028 pll_writel(val, pll->params->aux_reg, pll);
2029 udelay(1);
2030
2031 val = pll_readl_misc(pll);
2032 val |= PLLE_MISC_LOCK_ENABLE;
2033 val |= PLLE_MISC_IDDQ_SW_CTRL;
2034 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
2035 val |= PLLE_MISC_PLLE_PTS;
2036 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
2037 pll_writel_misc(val, pll);
2038 udelay(5);
2039
2040 val = pll_readl(PLLE_SS_CTRL, pll);
2041 val |= PLLE_SS_DISABLE;
2042 pll_writel(val, PLLE_SS_CTRL, pll);
2043
2044 val = pll_readl_base(pll);
2045 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
2046 divm_mask_shifted(pll));
2047 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
2048 val |= sel.m << divm_shift(pll);
2049 val |= sel.n << divn_shift(pll);
2050 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
2051 pll_writel_base(val, pll);
2052 udelay(1);
2053
2054 val = pll_readl_base(pll);
2055 val |= PLLE_BASE_ENABLE;
2056 pll_writel_base(val, pll);
2057
2058 ret = clk_pll_wait_for_lock(pll);
2059
2060 if (ret < 0)
2061 goto out;
2062
2063 val = pll_readl(PLLE_SS_CTRL, pll);
2064 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
2065 val &= ~PLLE_SS_COEFFICIENTS_MASK;
2066 val |= PLLE_SS_COEFFICIENTS_VAL;
2067 pll_writel(val, PLLE_SS_CTRL, pll);
2068 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
2069 pll_writel(val, PLLE_SS_CTRL, pll);
2070 udelay(1);
2071 val &= ~PLLE_SS_CNTL_INTERP_RESET;
2072 pll_writel(val, PLLE_SS_CTRL, pll);
2073 udelay(1);
2074
2075 val = pll_readl_misc(pll);
2076 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
2077 pll_writel_misc(val, pll);
2078
2079 val = pll_readl(pll->params->aux_reg, pll);
2080 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SS_SEQ_INCLUDE);
2081 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
2082 pll_writel(val, pll->params->aux_reg, pll);
2083 udelay(1);
2084 val |= PLLE_AUX_SEQ_ENABLE;
2085 pll_writel(val, pll->params->aux_reg, pll);
2086
2087out:
2088 if (pll->lock)
2089 spin_unlock_irqrestore(pll->lock, flags);
2090
2091 return ret;
2092}
2093
2094static void clk_plle_tegra210_disable(struct clk_hw *hw)
2095{
2096 struct tegra_clk_pll *pll = to_clk_pll(hw);
2097 unsigned long flags = 0;
2098 u32 val;
2099
2100 if (pll->lock)
2101 spin_lock_irqsave(pll->lock, flags);
2102
2103 val = pll_readl_base(pll);
2104 val &= ~PLLE_BASE_ENABLE;
2105 pll_writel_base(val, pll);
2106
2107 val = pll_readl_misc(pll);
2108 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
2109 pll_writel_misc(val, pll);
2110 udelay(1);
2111
2112 if (pll->lock)
2113 spin_unlock_irqrestore(pll->lock, flags);
2114}
2115
2116static int clk_plle_tegra210_is_enabled(struct clk_hw *hw)
2117{
2118 struct tegra_clk_pll *pll = to_clk_pll(hw);
2119 u32 val;
2120
2121 val = pll_readl_base(pll);
2122
2123 return val & PLLE_BASE_ENABLE ? 1 : 0;
2124}
2125
2126static const struct clk_ops tegra_clk_plle_tegra210_ops = {
2127 .is_enabled = clk_plle_tegra210_is_enabled,
2128 .enable = clk_plle_tegra210_enable,
2129 .disable = clk_plle_tegra210_disable,
2130 .recalc_rate = clk_pll_recalc_rate,
2131};
2132
2133struct clk *tegra_clk_register_plle_tegra210(const char *name,
2134 const char *parent_name,
2135 void __iomem *clk_base, unsigned long flags,
2136 struct tegra_clk_pll_params *pll_params,
2137 spinlock_t *lock)
2138{
2139 struct tegra_clk_pll *pll;
2140 struct clk *clk;
2141 u32 val, val_aux;
2142
2143 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2144 if (IS_ERR(pll))
2145 return ERR_CAST(pll);
2146
2147 /* ensure parent is set to pll_re_vco */
2148
2149 val = pll_readl_base(pll);
2150 val_aux = pll_readl(pll_params->aux_reg, pll);
2151
2152 if (val & PLLE_BASE_ENABLE) {
2153 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
2154 (val_aux & PLLE_AUX_PLLP_SEL))
2155 WARN(1, "pll_e enabled with unsupported parent %s\n",
2156 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
2157 "pll_re_vco");
2158 } else {
2159 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
2160 pll_writel(val_aux, pll_params->aux_reg, pll);
2161 }
2162
2163 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2164 &tegra_clk_plle_tegra210_ops);
2165 if (IS_ERR(clk))
2166 kfree(pll);
2167
2168 return clk;
2169}
2170
2171struct clk *tegra_clk_register_pllc_tegra210(const char *name,
2172 const char *parent_name, void __iomem *clk_base,
2173 void __iomem *pmc, unsigned long flags,
2174 struct tegra_clk_pll_params *pll_params,
2175 spinlock_t *lock)
2176{
2177 struct clk *parent, *clk;
2178 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
2179 struct tegra_clk_pll *pll;
2180 unsigned long parent_rate;
2181
2182 if (!p_tohw)
2183 return ERR_PTR(-EINVAL);
2184
2185 parent = __clk_lookup(parent_name);
2186 if (!parent) {
2187 WARN(1, "parent clk %s of %s must be registered first\n",
2188 name, parent_name);
2189 return ERR_PTR(-EINVAL);
2190 }
2191
2192 parent_rate = clk_get_rate(parent);
2193
2194 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2195
Bill Huangb5512b42015-06-18 17:28:30 -04002196 if (pll_params->adjust_vco)
2197 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2198 parent_rate);
2199
Rhyland Kleindd322f02015-06-18 17:28:28 -04002200 pll_params->flags |= TEGRA_PLL_BYPASS;
2201 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2202 if (IS_ERR(pll))
2203 return ERR_CAST(pll);
2204
2205 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2206 &tegra_clk_pll_ops);
2207 if (IS_ERR(clk))
2208 kfree(pll);
2209
2210 return clk;
2211}
2212
2213struct clk *tegra_clk_register_pllxc_tegra210(const char *name,
2214 const char *parent_name, void __iomem *clk_base,
2215 void __iomem *pmc, unsigned long flags,
2216 struct tegra_clk_pll_params *pll_params,
2217 spinlock_t *lock)
2218{
2219 struct tegra_clk_pll *pll;
2220 struct clk *clk, *parent;
2221 unsigned long parent_rate;
2222
2223 parent = __clk_lookup(parent_name);
2224 if (!parent) {
2225 WARN(1, "parent clk %s of %s must be registered first\n",
2226 name, parent_name);
2227 return ERR_PTR(-EINVAL);
2228 }
2229
2230 if (!pll_params->pdiv_tohw)
2231 return ERR_PTR(-EINVAL);
2232
2233 parent_rate = clk_get_rate(parent);
2234
2235 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2236
Bill Huangb5512b42015-06-18 17:28:30 -04002237 if (pll_params->adjust_vco)
2238 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2239 parent_rate);
2240
Rhyland Kleindd322f02015-06-18 17:28:28 -04002241 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2242 if (IS_ERR(pll))
2243 return ERR_CAST(pll);
2244
2245 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2246 &tegra_clk_pll_ops);
2247 if (IS_ERR(clk))
2248 kfree(pll);
2249
2250 return clk;
2251}
2252
2253struct clk *tegra_clk_register_pllss_tegra210(const char *name,
2254 const char *parent_name, void __iomem *clk_base,
2255 unsigned long flags,
2256 struct tegra_clk_pll_params *pll_params,
2257 spinlock_t *lock)
2258{
2259 struct tegra_clk_pll *pll;
2260 struct clk *clk, *parent;
2261 struct tegra_clk_pll_freq_table cfg;
2262 unsigned long parent_rate;
2263 u32 val;
2264 int i;
2265
2266 if (!pll_params->div_nmp)
2267 return ERR_PTR(-EINVAL);
2268
2269 parent = __clk_lookup(parent_name);
2270 if (!parent) {
2271 WARN(1, "parent clk %s of %s must be registered first\n",
2272 name, parent_name);
2273 return ERR_PTR(-EINVAL);
2274 }
2275
2276 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2277 if (IS_ERR(pll))
2278 return ERR_CAST(pll);
2279
2280 val = pll_readl_base(pll);
2281 val &= ~PLLSS_REF_SRC_SEL_MASK;
2282 pll_writel_base(val, pll);
2283
2284 parent_rate = clk_get_rate(parent);
2285
2286 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2287
Bill Huangb5512b42015-06-18 17:28:30 -04002288 if (pll_params->adjust_vco)
2289 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2290 parent_rate);
2291
Rhyland Kleindd322f02015-06-18 17:28:28 -04002292 /* initialize PLL to minimum rate */
2293
2294 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2295 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2296
2297 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
2298 ;
2299 if (!i) {
2300 kfree(pll);
2301 return ERR_PTR(-EINVAL);
2302 }
2303
2304 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
2305
2306 _update_pll_mnp(pll, &cfg);
2307
2308 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
2309
2310 val = pll_readl_base(pll);
2311 if (val & PLL_BASE_ENABLE) {
2312 if (val & BIT(pll_params->iddq_bit_idx)) {
2313 WARN(1, "%s is on but IDDQ set\n", name);
2314 kfree(pll);
2315 return ERR_PTR(-EINVAL);
2316 }
2317 } else
2318 val |= BIT(pll_params->iddq_bit_idx);
2319
2320 val &= ~PLLSS_LOCK_OVERRIDE;
2321 pll_writel_base(val, pll);
2322
2323 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2324 &tegra_clk_pll_ops);
2325
2326 if (IS_ERR(clk))
2327 kfree(pll);
2328
2329 return clk;
2330}
Rhyland Klein69297152015-06-18 17:28:29 -04002331
2332struct clk *tegra_clk_register_pllmb(const char *name, const char *parent_name,
2333 void __iomem *clk_base, void __iomem *pmc,
2334 unsigned long flags,
2335 struct tegra_clk_pll_params *pll_params,
2336 spinlock_t *lock)
2337{
2338 struct tegra_clk_pll *pll;
2339 struct clk *clk, *parent;
2340 unsigned long parent_rate;
2341
2342 if (!pll_params->pdiv_tohw)
2343 return ERR_PTR(-EINVAL);
2344
2345 parent = __clk_lookup(parent_name);
2346 if (!parent) {
2347 WARN(1, "parent clk %s of %s must be registered first\n",
2348 parent_name, name);
2349 return ERR_PTR(-EINVAL);
2350 }
2351
2352 parent_rate = clk_get_rate(parent);
2353
2354 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2355
Bill Huangb5512b42015-06-18 17:28:30 -04002356 if (pll_params->adjust_vco)
2357 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2358 parent_rate);
2359
Rhyland Klein69297152015-06-18 17:28:29 -04002360 pll_params->flags |= TEGRA_PLL_BYPASS;
2361 pll_params->flags |= TEGRA_PLLMB;
2362 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2363 if (IS_ERR(pll))
2364 return ERR_CAST(pll);
2365
2366 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2367 &tegra_clk_pll_ops);
2368 if (IS_ERR(clk))
2369 kfree(pll);
2370
2371 return clk;
2372}
Rhyland Kleindd322f02015-06-18 17:28:28 -04002373#endif