xref: /openbmc/linux/drivers/clk/tegra/clk-pll.c (revision ccb01374)
1 /*
2  * Copyright (c) 2012, 2013, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/slab.h>
18 #include <linux/io.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 
24 #include "clk.h"
25 
26 #define PLL_BASE_BYPASS BIT(31)
27 #define PLL_BASE_ENABLE BIT(30)
28 #define PLL_BASE_REF_ENABLE BIT(29)
29 #define PLL_BASE_OVERRIDE BIT(28)
30 
31 #define PLL_BASE_DIVP_SHIFT 20
32 #define PLL_BASE_DIVP_WIDTH 3
33 #define PLL_BASE_DIVN_SHIFT 8
34 #define PLL_BASE_DIVN_WIDTH 10
35 #define PLL_BASE_DIVM_SHIFT 0
36 #define PLL_BASE_DIVM_WIDTH 5
37 #define PLLU_POST_DIVP_MASK 0x1
38 
39 #define PLL_MISC_DCCON_SHIFT 20
40 #define PLL_MISC_CPCON_SHIFT 8
41 #define PLL_MISC_CPCON_WIDTH 4
42 #define PLL_MISC_CPCON_MASK ((1 << PLL_MISC_CPCON_WIDTH) - 1)
43 #define PLL_MISC_LFCON_SHIFT 4
44 #define PLL_MISC_LFCON_WIDTH 4
45 #define PLL_MISC_LFCON_MASK ((1 << PLL_MISC_LFCON_WIDTH) - 1)
46 #define PLL_MISC_VCOCON_SHIFT 0
47 #define PLL_MISC_VCOCON_WIDTH 4
48 #define PLL_MISC_VCOCON_MASK ((1 << PLL_MISC_VCOCON_WIDTH) - 1)
49 
50 #define OUT_OF_TABLE_CPCON 8
51 
52 #define PMC_PLLP_WB0_OVERRIDE 0xf8
53 #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE BIT(12)
54 #define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE BIT(11)
55 
56 #define PLL_POST_LOCK_DELAY 50
57 
58 #define PLLDU_LFCON_SET_DIVN 600
59 
60 #define PLLE_BASE_DIVCML_SHIFT 24
61 #define PLLE_BASE_DIVCML_MASK 0xf
62 #define PLLE_BASE_DIVP_SHIFT 16
63 #define PLLE_BASE_DIVP_WIDTH 6
64 #define PLLE_BASE_DIVN_SHIFT 8
65 #define PLLE_BASE_DIVN_WIDTH 8
66 #define PLLE_BASE_DIVM_SHIFT 0
67 #define PLLE_BASE_DIVM_WIDTH 8
68 #define PLLE_BASE_ENABLE BIT(31)
69 
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
81 #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_TEGRA114 0x25
90 #define PLLE_SS_MAX_VAL_TEGRA210 0x21
91 #define PLLE_SS_INC_MASK (0xff << 16)
92 #define PLLE_SS_INC_VAL (0x1 << 16)
93 #define PLLE_SS_INCINTRV_MASK (0x3f << 24)
94 #define PLLE_SS_INCINTRV_VAL_TEGRA114 (0x20 << 24)
95 #define PLLE_SS_INCINTRV_VAL_TEGRA210 (0x23 << 24)
96 #define PLLE_SS_COEFFICIENTS_MASK \
97 	(PLLE_SS_MAX_MASK | PLLE_SS_INC_MASK | PLLE_SS_INCINTRV_MASK)
98 #define PLLE_SS_COEFFICIENTS_VAL_TEGRA114 \
99 	(PLLE_SS_MAX_VAL_TEGRA114 | PLLE_SS_INC_VAL |\
100 	 PLLE_SS_INCINTRV_VAL_TEGRA114)
101 #define PLLE_SS_COEFFICIENTS_VAL_TEGRA210 \
102 	(PLLE_SS_MAX_VAL_TEGRA210 | PLLE_SS_INC_VAL |\
103 	 PLLE_SS_INCINTRV_VAL_TEGRA210)
104 
105 #define PLLE_AUX_PLLP_SEL	BIT(2)
106 #define PLLE_AUX_USE_LOCKDET	BIT(3)
107 #define PLLE_AUX_ENABLE_SWCTL	BIT(4)
108 #define PLLE_AUX_SS_SWCTL	BIT(6)
109 #define PLLE_AUX_SEQ_ENABLE	BIT(24)
110 #define PLLE_AUX_SEQ_START_STATE BIT(25)
111 #define PLLE_AUX_PLLRE_SEL	BIT(28)
112 #define PLLE_AUX_SS_SEQ_INCLUDE	BIT(31)
113 
114 #define XUSBIO_PLL_CFG0		0x51c
115 #define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL	BIT(0)
116 #define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL	BIT(2)
117 #define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET	BIT(6)
118 #define XUSBIO_PLL_CFG0_SEQ_ENABLE		BIT(24)
119 #define XUSBIO_PLL_CFG0_SEQ_START_STATE		BIT(25)
120 
121 #define SATA_PLL_CFG0		0x490
122 #define SATA_PLL_CFG0_PADPLL_RESET_SWCTL	BIT(0)
123 #define SATA_PLL_CFG0_PADPLL_USE_LOCKDET	BIT(2)
124 #define SATA_PLL_CFG0_SEQ_ENABLE		BIT(24)
125 #define SATA_PLL_CFG0_SEQ_START_STATE		BIT(25)
126 
127 #define PLLE_MISC_PLLE_PTS	BIT(8)
128 #define PLLE_MISC_IDDQ_SW_VALUE	BIT(13)
129 #define PLLE_MISC_IDDQ_SW_CTRL	BIT(14)
130 #define PLLE_MISC_VREG_BG_CTRL_SHIFT	4
131 #define PLLE_MISC_VREG_BG_CTRL_MASK	(3 << PLLE_MISC_VREG_BG_CTRL_SHIFT)
132 #define PLLE_MISC_VREG_CTRL_SHIFT	2
133 #define PLLE_MISC_VREG_CTRL_MASK	(2 << PLLE_MISC_VREG_CTRL_SHIFT)
134 
135 #define PLLCX_MISC_STROBE	BIT(31)
136 #define PLLCX_MISC_RESET	BIT(30)
137 #define PLLCX_MISC_SDM_DIV_SHIFT 28
138 #define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
139 #define PLLCX_MISC_FILT_DIV_SHIFT 26
140 #define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
141 #define PLLCX_MISC_ALPHA_SHIFT 18
142 #define PLLCX_MISC_DIV_LOW_RANGE \
143 		((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \
144 		(0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
145 #define PLLCX_MISC_DIV_HIGH_RANGE \
146 		((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \
147 		(0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
148 #define PLLCX_MISC_COEF_LOW_RANGE \
149 		((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT))
150 #define PLLCX_MISC_KA_SHIFT 2
151 #define PLLCX_MISC_KB_SHIFT 9
152 #define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \
153 			    (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
154 			    PLLCX_MISC_DIV_LOW_RANGE | \
155 			    PLLCX_MISC_RESET)
156 #define PLLCX_MISC1_DEFAULT 0x000d2308
157 #define PLLCX_MISC2_DEFAULT 0x30211200
158 #define PLLCX_MISC3_DEFAULT 0x200
159 
160 #define PMC_SATA_PWRGT 0x1ac
161 #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5)
162 #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4)
163 
164 #define PLLSS_MISC_KCP		0
165 #define PLLSS_MISC_KVCO		0
166 #define PLLSS_MISC_SETUP	0
167 #define PLLSS_EN_SDM		0
168 #define PLLSS_EN_SSC		0
169 #define PLLSS_EN_DITHER2	0
170 #define PLLSS_EN_DITHER		1
171 #define PLLSS_SDM_RESET		0
172 #define PLLSS_CLAMP		0
173 #define PLLSS_SDM_SSC_MAX	0
174 #define PLLSS_SDM_SSC_MIN	0
175 #define PLLSS_SDM_SSC_STEP	0
176 #define PLLSS_SDM_DIN		0
177 #define PLLSS_MISC_DEFAULT ((PLLSS_MISC_KCP << 25) | \
178 			    (PLLSS_MISC_KVCO << 24) | \
179 			    PLLSS_MISC_SETUP)
180 #define PLLSS_CFG_DEFAULT ((PLLSS_EN_SDM << 31) | \
181 			   (PLLSS_EN_SSC << 30) | \
182 			   (PLLSS_EN_DITHER2 << 29) | \
183 			   (PLLSS_EN_DITHER << 28) | \
184 			   (PLLSS_SDM_RESET) << 27 | \
185 			   (PLLSS_CLAMP << 22))
186 #define PLLSS_CTRL1_DEFAULT \
187 			((PLLSS_SDM_SSC_MAX << 16) | PLLSS_SDM_SSC_MIN)
188 #define PLLSS_CTRL2_DEFAULT \
189 			((PLLSS_SDM_SSC_STEP << 16) | PLLSS_SDM_DIN)
190 #define PLLSS_LOCK_OVERRIDE	BIT(24)
191 #define PLLSS_REF_SRC_SEL_SHIFT	25
192 #define PLLSS_REF_SRC_SEL_MASK	(3 << PLLSS_REF_SRC_SEL_SHIFT)
193 
194 #define UTMIP_PLL_CFG1 0x484
195 #define UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
196 #define UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
197 #define UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN BIT(12)
198 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN BIT(14)
199 #define UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP BIT(15)
200 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN BIT(16)
201 #define UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP BIT(17)
202 
203 #define UTMIP_PLL_CFG2 0x488
204 #define UTMIP_PLL_CFG2_STABLE_COUNT(x) (((x) & 0xfff) << 6)
205 #define UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(x) (((x) & 0x3f) << 18)
206 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN BIT(0)
207 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP BIT(1)
208 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN BIT(2)
209 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP BIT(3)
210 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN BIT(4)
211 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERUP BIT(5)
212 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN BIT(24)
213 #define UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP BIT(25)
214 #define UTMIP_PLL_CFG2_PHY_XTAL_CLOCKEN BIT(30)
215 
216 #define UTMIPLL_HW_PWRDN_CFG0 0x52c
217 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL BIT(0)
218 #define UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE BIT(1)
219 #define UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL BIT(2)
220 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_IN_SWCTL BIT(4)
221 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_RESET_INPUT_VALUE BIT(5)
222 #define UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET BIT(6)
223 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE BIT(24)
224 #define UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE BIT(25)
225 
226 #define PLLU_HW_PWRDN_CFG0 0x530
227 #define PLLU_HW_PWRDN_CFG0_CLK_SWITCH_SWCTL BIT(0)
228 #define PLLU_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL BIT(2)
229 #define PLLU_HW_PWRDN_CFG0_USE_LOCKDET BIT(6)
230 #define PLLU_HW_PWRDN_CFG0_USE_SWITCH_DETECT BIT(7)
231 #define PLLU_HW_PWRDN_CFG0_SEQ_ENABLE BIT(24)
232 #define PLLU_HW_PWRDN_CFG0_IDDQ_PD_INCLUDE BIT(28)
233 
234 #define XUSB_PLL_CFG0 0x534
235 #define XUSB_PLL_CFG0_UTMIPLL_LOCK_DLY 0x3ff
236 #define XUSB_PLL_CFG0_PLLU_LOCK_DLY (0x3ff << 14)
237 
238 #define PLLU_BASE_CLKENABLE_USB BIT(21)
239 #define PLLU_BASE_OVERRIDE BIT(24)
240 
241 #define pll_readl(offset, p) readl_relaxed(p->clk_base + offset)
242 #define pll_readl_base(p) pll_readl(p->params->base_reg, p)
243 #define pll_readl_misc(p) pll_readl(p->params->misc_reg, p)
244 #define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
245 #define pll_readl_sdm_din(p) pll_readl(p->params->sdm_din_reg, p)
246 #define pll_readl_sdm_ctrl(p) pll_readl(p->params->sdm_ctrl_reg, p)
247 
248 #define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
249 #define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
250 #define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
251 #define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
252 #define pll_writel_sdm_din(val, p) pll_writel(val, p->params->sdm_din_reg, p)
253 #define pll_writel_sdm_ctrl(val, p) pll_writel(val, p->params->sdm_ctrl_reg, p)
254 
255 #define mask(w) ((1 << (w)) - 1)
256 #define divm_mask(p) mask(p->params->div_nmp->divm_width)
257 #define divn_mask(p) mask(p->params->div_nmp->divn_width)
258 #define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
259 		      mask(p->params->div_nmp->divp_width))
260 #define sdm_din_mask(p) p->params->sdm_din_mask
261 #define sdm_en_mask(p) p->params->sdm_ctrl_en_mask
262 
263 #define divm_shift(p) (p)->params->div_nmp->divm_shift
264 #define divn_shift(p) (p)->params->div_nmp->divn_shift
265 #define divp_shift(p) (p)->params->div_nmp->divp_shift
266 
267 #define divm_mask_shifted(p) (divm_mask(p) << divm_shift(p))
268 #define divn_mask_shifted(p) (divn_mask(p) << divn_shift(p))
269 #define divp_mask_shifted(p) (divp_mask(p) << divp_shift(p))
270 
271 #define divm_max(p) (divm_mask(p))
272 #define divn_max(p) (divn_mask(p))
273 #define divp_max(p) (1 << (divp_mask(p)))
274 
275 #define sdin_din_to_data(din)	((u16)((din) ? : 0xFFFFU))
276 #define sdin_data_to_din(dat)	(((dat) == 0xFFFFU) ? 0 : (s16)dat)
277 
278 static struct div_nmp default_nmp = {
279 	.divn_shift = PLL_BASE_DIVN_SHIFT,
280 	.divn_width = PLL_BASE_DIVN_WIDTH,
281 	.divm_shift = PLL_BASE_DIVM_SHIFT,
282 	.divm_width = PLL_BASE_DIVM_WIDTH,
283 	.divp_shift = PLL_BASE_DIVP_SHIFT,
284 	.divp_width = PLL_BASE_DIVP_WIDTH,
285 };
286 
287 static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
288 {
289 	u32 val;
290 
291 	if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
292 		return;
293 
294 	if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
295 		return;
296 
297 	val = pll_readl_misc(pll);
298 	val |= BIT(pll->params->lock_enable_bit_idx);
299 	pll_writel_misc(val, pll);
300 }
301 
302 static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
303 {
304 	int i;
305 	u32 val, lock_mask;
306 	void __iomem *lock_addr;
307 
308 	if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
309 		udelay(pll->params->lock_delay);
310 		return 0;
311 	}
312 
313 	lock_addr = pll->clk_base;
314 	if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
315 		lock_addr += pll->params->misc_reg;
316 	else
317 		lock_addr += pll->params->base_reg;
318 
319 	lock_mask = pll->params->lock_mask;
320 
321 	for (i = 0; i < pll->params->lock_delay; i++) {
322 		val = readl_relaxed(lock_addr);
323 		if ((val & lock_mask) == lock_mask) {
324 			udelay(PLL_POST_LOCK_DELAY);
325 			return 0;
326 		}
327 		udelay(2); /* timeout = 2 * lock time */
328 	}
329 
330 	pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
331 	       clk_hw_get_name(&pll->hw));
332 
333 	return -1;
334 }
335 
336 int tegra_pll_wait_for_lock(struct tegra_clk_pll *pll)
337 {
338 	return clk_pll_wait_for_lock(pll);
339 }
340 
341 static int clk_pll_is_enabled(struct clk_hw *hw)
342 {
343 	struct tegra_clk_pll *pll = to_clk_pll(hw);
344 	u32 val;
345 
346 	if (pll->params->flags & TEGRA_PLLM) {
347 		val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
348 		if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
349 			return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
350 	}
351 
352 	val = pll_readl_base(pll);
353 
354 	return val & PLL_BASE_ENABLE ? 1 : 0;
355 }
356 
357 static void _clk_pll_enable(struct clk_hw *hw)
358 {
359 	struct tegra_clk_pll *pll = to_clk_pll(hw);
360 	u32 val;
361 
362 	if (pll->params->iddq_reg) {
363 		val = pll_readl(pll->params->iddq_reg, pll);
364 		val &= ~BIT(pll->params->iddq_bit_idx);
365 		pll_writel(val, pll->params->iddq_reg, pll);
366 		udelay(5);
367 	}
368 
369 	if (pll->params->reset_reg) {
370 		val = pll_readl(pll->params->reset_reg, pll);
371 		val &= ~BIT(pll->params->reset_bit_idx);
372 		pll_writel(val, pll->params->reset_reg, pll);
373 	}
374 
375 	clk_pll_enable_lock(pll);
376 
377 	val = pll_readl_base(pll);
378 	if (pll->params->flags & TEGRA_PLL_BYPASS)
379 		val &= ~PLL_BASE_BYPASS;
380 	val |= PLL_BASE_ENABLE;
381 	pll_writel_base(val, pll);
382 
383 	if (pll->params->flags & TEGRA_PLLM) {
384 		val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
385 		val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
386 		writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
387 	}
388 }
389 
390 static void _clk_pll_disable(struct clk_hw *hw)
391 {
392 	struct tegra_clk_pll *pll = to_clk_pll(hw);
393 	u32 val;
394 
395 	val = pll_readl_base(pll);
396 	if (pll->params->flags & TEGRA_PLL_BYPASS)
397 		val &= ~PLL_BASE_BYPASS;
398 	val &= ~PLL_BASE_ENABLE;
399 	pll_writel_base(val, pll);
400 
401 	if (pll->params->flags & TEGRA_PLLM) {
402 		val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
403 		val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
404 		writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
405 	}
406 
407 	if (pll->params->reset_reg) {
408 		val = pll_readl(pll->params->reset_reg, pll);
409 		val |= BIT(pll->params->reset_bit_idx);
410 		pll_writel(val, pll->params->reset_reg, pll);
411 	}
412 
413 	if (pll->params->iddq_reg) {
414 		val = pll_readl(pll->params->iddq_reg, pll);
415 		val |= BIT(pll->params->iddq_bit_idx);
416 		pll_writel(val, pll->params->iddq_reg, pll);
417 		udelay(2);
418 	}
419 }
420 
421 static void pll_clk_start_ss(struct tegra_clk_pll *pll)
422 {
423 	if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
424 		u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
425 
426 		val |= pll->params->ssc_ctrl_en_mask;
427 		pll_writel(val, pll->params->ssc_ctrl_reg, pll);
428 	}
429 }
430 
431 static void pll_clk_stop_ss(struct tegra_clk_pll *pll)
432 {
433 	if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
434 		u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
435 
436 		val &= ~pll->params->ssc_ctrl_en_mask;
437 		pll_writel(val, pll->params->ssc_ctrl_reg, pll);
438 	}
439 }
440 
441 static int clk_pll_enable(struct clk_hw *hw)
442 {
443 	struct tegra_clk_pll *pll = to_clk_pll(hw);
444 	unsigned long flags = 0;
445 	int ret;
446 
447 	if (pll->lock)
448 		spin_lock_irqsave(pll->lock, flags);
449 
450 	_clk_pll_enable(hw);
451 
452 	ret = clk_pll_wait_for_lock(pll);
453 
454 	pll_clk_start_ss(pll);
455 
456 	if (pll->lock)
457 		spin_unlock_irqrestore(pll->lock, flags);
458 
459 	return ret;
460 }
461 
462 static void clk_pll_disable(struct clk_hw *hw)
463 {
464 	struct tegra_clk_pll *pll = to_clk_pll(hw);
465 	unsigned long flags = 0;
466 
467 	if (pll->lock)
468 		spin_lock_irqsave(pll->lock, flags);
469 
470 	pll_clk_stop_ss(pll);
471 
472 	_clk_pll_disable(hw);
473 
474 	if (pll->lock)
475 		spin_unlock_irqrestore(pll->lock, flags);
476 }
477 
478 static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
479 {
480 	struct tegra_clk_pll *pll = to_clk_pll(hw);
481 	const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
482 
483 	if (p_tohw) {
484 		while (p_tohw->pdiv) {
485 			if (p_div <= p_tohw->pdiv)
486 				return p_tohw->hw_val;
487 			p_tohw++;
488 		}
489 		return -EINVAL;
490 	}
491 	return -EINVAL;
492 }
493 
494 int tegra_pll_p_div_to_hw(struct tegra_clk_pll *pll, u8 p_div)
495 {
496 	return _p_div_to_hw(&pll->hw, p_div);
497 }
498 
499 static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
500 {
501 	struct tegra_clk_pll *pll = to_clk_pll(hw);
502 	const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
503 
504 	if (p_tohw) {
505 		while (p_tohw->pdiv) {
506 			if (p_div_hw == p_tohw->hw_val)
507 				return p_tohw->pdiv;
508 			p_tohw++;
509 		}
510 		return -EINVAL;
511 	}
512 
513 	return 1 << p_div_hw;
514 }
515 
516 static int _get_table_rate(struct clk_hw *hw,
517 			   struct tegra_clk_pll_freq_table *cfg,
518 			   unsigned long rate, unsigned long parent_rate)
519 {
520 	struct tegra_clk_pll *pll = to_clk_pll(hw);
521 	struct tegra_clk_pll_freq_table *sel;
522 	int p;
523 
524 	for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
525 		if (sel->input_rate == parent_rate &&
526 		    sel->output_rate == rate)
527 			break;
528 
529 	if (sel->input_rate == 0)
530 		return -EINVAL;
531 
532 	if (pll->params->pdiv_tohw) {
533 		p = _p_div_to_hw(hw, sel->p);
534 		if (p < 0)
535 			return p;
536 	} else {
537 		p = ilog2(sel->p);
538 	}
539 
540 	cfg->input_rate = sel->input_rate;
541 	cfg->output_rate = sel->output_rate;
542 	cfg->m = sel->m;
543 	cfg->n = sel->n;
544 	cfg->p = p;
545 	cfg->cpcon = sel->cpcon;
546 	cfg->sdm_data = sel->sdm_data;
547 
548 	return 0;
549 }
550 
551 static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
552 		      unsigned long rate, unsigned long parent_rate)
553 {
554 	struct tegra_clk_pll *pll = to_clk_pll(hw);
555 	unsigned long cfreq;
556 	u32 p_div = 0;
557 	int ret;
558 
559 	switch (parent_rate) {
560 	case 12000000:
561 	case 26000000:
562 		cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
563 		break;
564 	case 13000000:
565 		cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
566 		break;
567 	case 16800000:
568 	case 19200000:
569 		cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
570 		break;
571 	case 9600000:
572 	case 28800000:
573 		/*
574 		 * PLL_P_OUT1 rate is not listed in PLLA table
575 		 */
576 		cfreq = parent_rate / (parent_rate / 1000000);
577 		break;
578 	default:
579 		pr_err("%s Unexpected reference rate %lu\n",
580 		       __func__, parent_rate);
581 		BUG();
582 	}
583 
584 	/* Raise VCO to guarantee 0.5% accuracy */
585 	for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
586 	     cfg->output_rate <<= 1)
587 		p_div++;
588 
589 	cfg->m = parent_rate / cfreq;
590 	cfg->n = cfg->output_rate / cfreq;
591 	cfg->cpcon = OUT_OF_TABLE_CPCON;
592 
593 	if (cfg->m == 0 || cfg->m > divm_max(pll) ||
594 	    cfg->n > divn_max(pll) || (1 << p_div) > divp_max(pll) ||
595 	    cfg->output_rate > pll->params->vco_max) {
596 		return -EINVAL;
597 	}
598 
599 	cfg->output_rate = cfg->n * DIV_ROUND_UP(parent_rate, cfg->m);
600 	cfg->output_rate >>= p_div;
601 
602 	if (pll->params->pdiv_tohw) {
603 		ret = _p_div_to_hw(hw, 1 << p_div);
604 		if (ret < 0)
605 			return ret;
606 		else
607 			cfg->p = ret;
608 	} else
609 		cfg->p = p_div;
610 
611 	return 0;
612 }
613 
614 /*
615  * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number
616  * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as
617  * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used
618  * to indicate that SDM is disabled.
619  *
620  * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13
621  */
622 static void clk_pll_set_sdm_data(struct clk_hw *hw,
623 				 struct tegra_clk_pll_freq_table *cfg)
624 {
625 	struct tegra_clk_pll *pll = to_clk_pll(hw);
626 	u32 val;
627 	bool enabled;
628 
629 	if (!pll->params->sdm_din_reg)
630 		return;
631 
632 	if (cfg->sdm_data) {
633 		val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll));
634 		val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll);
635 		pll_writel_sdm_din(val, pll);
636 	}
637 
638 	val = pll_readl_sdm_ctrl(pll);
639 	enabled = (val & sdm_en_mask(pll));
640 
641 	if (cfg->sdm_data == 0 && enabled)
642 		val &= ~pll->params->sdm_ctrl_en_mask;
643 
644 	if (cfg->sdm_data != 0 && !enabled)
645 		val |= pll->params->sdm_ctrl_en_mask;
646 
647 	pll_writel_sdm_ctrl(val, pll);
648 }
649 
650 static void _update_pll_mnp(struct tegra_clk_pll *pll,
651 			    struct tegra_clk_pll_freq_table *cfg)
652 {
653 	u32 val;
654 	struct tegra_clk_pll_params *params = pll->params;
655 	struct div_nmp *div_nmp = params->div_nmp;
656 
657 	if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
658 		(pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
659 			PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
660 		val = pll_override_readl(params->pmc_divp_reg, pll);
661 		val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
662 		val |= cfg->p << div_nmp->override_divp_shift;
663 		pll_override_writel(val, params->pmc_divp_reg, pll);
664 
665 		val = pll_override_readl(params->pmc_divnm_reg, pll);
666 		val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
667 			~(divn_mask(pll) << div_nmp->override_divn_shift);
668 		val |= (cfg->m << div_nmp->override_divm_shift) |
669 			(cfg->n << div_nmp->override_divn_shift);
670 		pll_override_writel(val, params->pmc_divnm_reg, pll);
671 	} else {
672 		val = pll_readl_base(pll);
673 
674 		val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
675 			 divp_mask_shifted(pll));
676 
677 		val |= (cfg->m << divm_shift(pll)) |
678 		       (cfg->n << divn_shift(pll)) |
679 		       (cfg->p << divp_shift(pll));
680 
681 		pll_writel_base(val, pll);
682 
683 		clk_pll_set_sdm_data(&pll->hw, cfg);
684 	}
685 }
686 
687 static void _get_pll_mnp(struct tegra_clk_pll *pll,
688 			 struct tegra_clk_pll_freq_table *cfg)
689 {
690 	u32 val;
691 	struct tegra_clk_pll_params *params = pll->params;
692 	struct div_nmp *div_nmp = params->div_nmp;
693 
694 	*cfg = (struct tegra_clk_pll_freq_table) { };
695 
696 	if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
697 		(pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
698 			PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
699 		val = pll_override_readl(params->pmc_divp_reg, pll);
700 		cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
701 
702 		val = pll_override_readl(params->pmc_divnm_reg, pll);
703 		cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
704 		cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
705 	}  else {
706 		val = pll_readl_base(pll);
707 
708 		cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
709 		cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
710 		cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
711 
712 		if (pll->params->sdm_din_reg) {
713 			if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) {
714 				val = pll_readl_sdm_din(pll);
715 				val &= sdm_din_mask(pll);
716 				cfg->sdm_data = sdin_din_to_data(val);
717 			}
718 		}
719 	}
720 }
721 
722 static void _update_pll_cpcon(struct tegra_clk_pll *pll,
723 			      struct tegra_clk_pll_freq_table *cfg,
724 			      unsigned long rate)
725 {
726 	u32 val;
727 
728 	val = pll_readl_misc(pll);
729 
730 	val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
731 	val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
732 
733 	if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
734 		val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
735 		if (cfg->n >= PLLDU_LFCON_SET_DIVN)
736 			val |= 1 << PLL_MISC_LFCON_SHIFT;
737 	} else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
738 		val &= ~(1 << PLL_MISC_DCCON_SHIFT);
739 		if (rate >= (pll->params->vco_max >> 1))
740 			val |= 1 << PLL_MISC_DCCON_SHIFT;
741 	}
742 
743 	pll_writel_misc(val, pll);
744 }
745 
746 static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
747 			unsigned long rate)
748 {
749 	struct tegra_clk_pll *pll = to_clk_pll(hw);
750 	struct tegra_clk_pll_freq_table old_cfg;
751 	int state, ret = 0;
752 
753 	state = clk_pll_is_enabled(hw);
754 
755 	_get_pll_mnp(pll, &old_cfg);
756 
757 	if (state && pll->params->defaults_set && pll->params->dyn_ramp &&
758 			(cfg->m == old_cfg.m) && (cfg->p == old_cfg.p)) {
759 		ret = pll->params->dyn_ramp(pll, cfg);
760 		if (!ret)
761 			return 0;
762 	}
763 
764 	if (state) {
765 		pll_clk_stop_ss(pll);
766 		_clk_pll_disable(hw);
767 	}
768 
769 	if (!pll->params->defaults_set && pll->params->set_defaults)
770 		pll->params->set_defaults(pll);
771 
772 	_update_pll_mnp(pll, cfg);
773 
774 	if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
775 		_update_pll_cpcon(pll, cfg, rate);
776 
777 	if (state) {
778 		_clk_pll_enable(hw);
779 		ret = clk_pll_wait_for_lock(pll);
780 		pll_clk_start_ss(pll);
781 	}
782 
783 	return ret;
784 }
785 
786 static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
787 			unsigned long parent_rate)
788 {
789 	struct tegra_clk_pll *pll = to_clk_pll(hw);
790 	struct tegra_clk_pll_freq_table cfg, old_cfg;
791 	unsigned long flags = 0;
792 	int ret = 0;
793 
794 	if (pll->params->flags & TEGRA_PLL_FIXED) {
795 		if (rate != pll->params->fixed_rate) {
796 			pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
797 				__func__, clk_hw_get_name(hw),
798 				pll->params->fixed_rate, rate);
799 			return -EINVAL;
800 		}
801 		return 0;
802 	}
803 
804 	if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
805 	    pll->params->calc_rate(hw, &cfg, rate, parent_rate)) {
806 		pr_err("%s: Failed to set %s rate %lu\n", __func__,
807 		       clk_hw_get_name(hw), rate);
808 		WARN_ON(1);
809 		return -EINVAL;
810 	}
811 	if (pll->lock)
812 		spin_lock_irqsave(pll->lock, flags);
813 
814 	_get_pll_mnp(pll, &old_cfg);
815 	if (pll->params->flags & TEGRA_PLL_VCO_OUT)
816 		cfg.p = old_cfg.p;
817 
818 	if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p ||
819 		old_cfg.sdm_data != cfg.sdm_data)
820 		ret = _program_pll(hw, &cfg, rate);
821 
822 	if (pll->lock)
823 		spin_unlock_irqrestore(pll->lock, flags);
824 
825 	return ret;
826 }
827 
828 static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
829 			unsigned long *prate)
830 {
831 	struct tegra_clk_pll *pll = to_clk_pll(hw);
832 	struct tegra_clk_pll_freq_table cfg;
833 
834 	if (pll->params->flags & TEGRA_PLL_FIXED) {
835 		/* PLLM/MB are used for memory; we do not change rate */
836 		if (pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB))
837 			return clk_hw_get_rate(hw);
838 		return pll->params->fixed_rate;
839 	}
840 
841 	if (_get_table_rate(hw, &cfg, rate, *prate) &&
842 	    pll->params->calc_rate(hw, &cfg, rate, *prate))
843 		return -EINVAL;
844 
845 	return cfg.output_rate;
846 }
847 
848 static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
849 					 unsigned long parent_rate)
850 {
851 	struct tegra_clk_pll *pll = to_clk_pll(hw);
852 	struct tegra_clk_pll_freq_table cfg;
853 	u32 val;
854 	u64 rate = parent_rate;
855 	int pdiv;
856 
857 	val = pll_readl_base(pll);
858 
859 	if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
860 		return parent_rate;
861 
862 	if ((pll->params->flags & TEGRA_PLL_FIXED) &&
863 	    !(pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
864 			!(val & PLL_BASE_OVERRIDE)) {
865 		struct tegra_clk_pll_freq_table sel;
866 		if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
867 					parent_rate)) {
868 			pr_err("Clock %s has unknown fixed frequency\n",
869 			       clk_hw_get_name(hw));
870 			BUG();
871 		}
872 		return pll->params->fixed_rate;
873 	}
874 
875 	_get_pll_mnp(pll, &cfg);
876 
877 	if (pll->params->flags & TEGRA_PLL_VCO_OUT) {
878 		pdiv = 1;
879 	} else {
880 		pdiv = _hw_to_p_div(hw, cfg.p);
881 		if (pdiv < 0) {
882 			WARN(1, "Clock %s has invalid pdiv value : 0x%x\n",
883 			     clk_hw_get_name(hw), cfg.p);
884 			pdiv = 1;
885 		}
886 	}
887 
888 	if (pll->params->set_gain)
889 		pll->params->set_gain(&cfg);
890 
891 	cfg.m *= pdiv;
892 
893 	rate *= cfg.n;
894 	do_div(rate, cfg.m);
895 
896 	return rate;
897 }
898 
899 static int clk_plle_training(struct tegra_clk_pll *pll)
900 {
901 	u32 val;
902 	unsigned long timeout;
903 
904 	if (!pll->pmc)
905 		return -ENOSYS;
906 
907 	/*
908 	 * PLLE is already disabled, and setup cleared;
909 	 * create falling edge on PLLE IDDQ input.
910 	 */
911 	val = readl(pll->pmc + PMC_SATA_PWRGT);
912 	val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
913 	writel(val, pll->pmc + PMC_SATA_PWRGT);
914 
915 	val = readl(pll->pmc + PMC_SATA_PWRGT);
916 	val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
917 	writel(val, pll->pmc + PMC_SATA_PWRGT);
918 
919 	val = readl(pll->pmc + PMC_SATA_PWRGT);
920 	val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
921 	writel(val, pll->pmc + PMC_SATA_PWRGT);
922 
923 	val = pll_readl_misc(pll);
924 
925 	timeout = jiffies + msecs_to_jiffies(100);
926 	while (1) {
927 		val = pll_readl_misc(pll);
928 		if (val & PLLE_MISC_READY)
929 			break;
930 		if (time_after(jiffies, timeout)) {
931 			pr_err("%s: timeout waiting for PLLE\n", __func__);
932 			return -EBUSY;
933 		}
934 		udelay(300);
935 	}
936 
937 	return 0;
938 }
939 
940 static int clk_plle_enable(struct clk_hw *hw)
941 {
942 	struct tegra_clk_pll *pll = to_clk_pll(hw);
943 	unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
944 	struct tegra_clk_pll_freq_table sel;
945 	u32 val;
946 	int err;
947 
948 	if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
949 		return -EINVAL;
950 
951 	clk_pll_disable(hw);
952 
953 	val = pll_readl_misc(pll);
954 	val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
955 	pll_writel_misc(val, pll);
956 
957 	val = pll_readl_misc(pll);
958 	if (!(val & PLLE_MISC_READY)) {
959 		err = clk_plle_training(pll);
960 		if (err)
961 			return err;
962 	}
963 
964 	if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
965 		/* configure dividers */
966 		val = pll_readl_base(pll);
967 		val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
968 			 divm_mask_shifted(pll));
969 		val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
970 		val |= sel.m << divm_shift(pll);
971 		val |= sel.n << divn_shift(pll);
972 		val |= sel.p << divp_shift(pll);
973 		val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
974 		pll_writel_base(val, pll);
975 	}
976 
977 	val = pll_readl_misc(pll);
978 	val |= PLLE_MISC_SETUP_VALUE;
979 	val |= PLLE_MISC_LOCK_ENABLE;
980 	pll_writel_misc(val, pll);
981 
982 	val = readl(pll->clk_base + PLLE_SS_CTRL);
983 	val &= ~PLLE_SS_COEFFICIENTS_MASK;
984 	val |= PLLE_SS_DISABLE;
985 	writel(val, pll->clk_base + PLLE_SS_CTRL);
986 
987 	val = pll_readl_base(pll);
988 	val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
989 	pll_writel_base(val, pll);
990 
991 	clk_pll_wait_for_lock(pll);
992 
993 	return 0;
994 }
995 
996 static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
997 					 unsigned long parent_rate)
998 {
999 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1000 	u32 val = pll_readl_base(pll);
1001 	u32 divn = 0, divm = 0, divp = 0;
1002 	u64 rate = parent_rate;
1003 
1004 	divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
1005 	divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
1006 	divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
1007 	divm *= divp;
1008 
1009 	rate *= divn;
1010 	do_div(rate, divm);
1011 	return rate;
1012 }
1013 
1014 const struct clk_ops tegra_clk_pll_ops = {
1015 	.is_enabled = clk_pll_is_enabled,
1016 	.enable = clk_pll_enable,
1017 	.disable = clk_pll_disable,
1018 	.recalc_rate = clk_pll_recalc_rate,
1019 	.round_rate = clk_pll_round_rate,
1020 	.set_rate = clk_pll_set_rate,
1021 };
1022 
1023 const struct clk_ops tegra_clk_plle_ops = {
1024 	.recalc_rate = clk_plle_recalc_rate,
1025 	.is_enabled = clk_pll_is_enabled,
1026 	.disable = clk_pll_disable,
1027 	.enable = clk_plle_enable,
1028 };
1029 
1030 /*
1031  * Structure defining the fields for USB UTMI clocks Parameters.
1032  */
1033 struct utmi_clk_param {
1034 	/* Oscillator Frequency in Hz */
1035 	u32 osc_frequency;
1036 	/* UTMIP PLL Enable Delay Count  */
1037 	u8 enable_delay_count;
1038 	/* UTMIP PLL Stable count */
1039 	u8 stable_count;
1040 	/*  UTMIP PLL Active delay count */
1041 	u8 active_delay_count;
1042 	/* UTMIP PLL Xtal frequency count */
1043 	u8 xtal_freq_count;
1044 };
1045 
1046 static const struct utmi_clk_param utmi_parameters[] = {
1047 	{
1048 		.osc_frequency = 13000000, .enable_delay_count = 0x02,
1049 		.stable_count = 0x33, .active_delay_count = 0x05,
1050 		.xtal_freq_count = 0x7f
1051 	}, {
1052 		.osc_frequency = 19200000, .enable_delay_count = 0x03,
1053 		.stable_count = 0x4b, .active_delay_count = 0x06,
1054 		.xtal_freq_count = 0xbb
1055 	}, {
1056 		.osc_frequency = 12000000, .enable_delay_count = 0x02,
1057 		.stable_count = 0x2f, .active_delay_count = 0x04,
1058 		.xtal_freq_count = 0x76
1059 	}, {
1060 		.osc_frequency = 26000000, .enable_delay_count = 0x04,
1061 		.stable_count = 0x66, .active_delay_count = 0x09,
1062 		.xtal_freq_count = 0xfe
1063 	}, {
1064 		.osc_frequency = 16800000, .enable_delay_count = 0x03,
1065 		.stable_count = 0x41, .active_delay_count = 0x0a,
1066 		.xtal_freq_count = 0xa4
1067 	}, {
1068 		.osc_frequency = 38400000, .enable_delay_count = 0x0,
1069 		.stable_count = 0x0, .active_delay_count = 0x6,
1070 		.xtal_freq_count = 0x80
1071 	},
1072 };
1073 
1074 static int clk_pllu_enable(struct clk_hw *hw)
1075 {
1076 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1077 	struct clk_hw *pll_ref = clk_hw_get_parent(hw);
1078 	struct clk_hw *osc = clk_hw_get_parent(pll_ref);
1079 	const struct utmi_clk_param *params = NULL;
1080 	unsigned long flags = 0, input_rate;
1081 	unsigned int i;
1082 	int ret = 0;
1083 	u32 value;
1084 
1085 	if (!osc) {
1086 		pr_err("%s: failed to get OSC clock\n", __func__);
1087 		return -EINVAL;
1088 	}
1089 
1090 	input_rate = clk_hw_get_rate(osc);
1091 
1092 	if (pll->lock)
1093 		spin_lock_irqsave(pll->lock, flags);
1094 
1095 	_clk_pll_enable(hw);
1096 
1097 	ret = clk_pll_wait_for_lock(pll);
1098 	if (ret < 0)
1099 		goto out;
1100 
1101 	for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1102 		if (input_rate == utmi_parameters[i].osc_frequency) {
1103 			params = &utmi_parameters[i];
1104 			break;
1105 		}
1106 	}
1107 
1108 	if (!params) {
1109 		pr_err("%s: unexpected input rate %lu Hz\n", __func__,
1110 		       input_rate);
1111 		ret = -EINVAL;
1112 		goto out;
1113 	}
1114 
1115 	value = pll_readl_base(pll);
1116 	value &= ~PLLU_BASE_OVERRIDE;
1117 	pll_writel_base(value, pll);
1118 
1119 	value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
1120 	/* Program UTMIP PLL stable and active counts */
1121 	value &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1122 	value |= UTMIP_PLL_CFG2_STABLE_COUNT(params->stable_count);
1123 	value &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1124 	value |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(params->active_delay_count);
1125 	/* Remove power downs from UTMIP PLL control bits */
1126 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1127 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1128 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1129 	writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
1130 
1131 	value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1132 	/* Program UTMIP PLL delay and oscillator frequency counts */
1133 	value &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1134 	value |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(params->enable_delay_count);
1135 	value &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1136 	value |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(params->xtal_freq_count);
1137 	/* Remove power downs from UTMIP PLL control bits */
1138 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1139 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1140 	value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1141 	writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1142 
1143 out:
1144 	if (pll->lock)
1145 		spin_unlock_irqrestore(pll->lock, flags);
1146 
1147 	return ret;
1148 }
1149 
1150 static const struct clk_ops tegra_clk_pllu_ops = {
1151 	.is_enabled = clk_pll_is_enabled,
1152 	.enable = clk_pllu_enable,
1153 	.disable = clk_pll_disable,
1154 	.recalc_rate = clk_pll_recalc_rate,
1155 	.round_rate = clk_pll_round_rate,
1156 	.set_rate = clk_pll_set_rate,
1157 };
1158 
1159 static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
1160 			   unsigned long parent_rate)
1161 {
1162 	u16 mdiv = parent_rate / pll_params->cf_min;
1163 
1164 	if (pll_params->flags & TEGRA_MDIV_NEW)
1165 		return (!pll_params->mdiv_default ? mdiv :
1166 			min(mdiv, pll_params->mdiv_default));
1167 
1168 	if (pll_params->mdiv_default)
1169 		return pll_params->mdiv_default;
1170 
1171 	if (parent_rate > pll_params->cf_max)
1172 		return 2;
1173 	else
1174 		return 1;
1175 }
1176 
1177 static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
1178 				struct tegra_clk_pll_freq_table *cfg,
1179 				unsigned long rate, unsigned long parent_rate)
1180 {
1181 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1182 	unsigned int p;
1183 	int p_div;
1184 
1185 	if (!rate)
1186 		return -EINVAL;
1187 
1188 	p = DIV_ROUND_UP(pll->params->vco_min, rate);
1189 	cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
1190 	cfg->output_rate = rate * p;
1191 	cfg->n = cfg->output_rate * cfg->m / parent_rate;
1192 	cfg->input_rate = parent_rate;
1193 
1194 	p_div = _p_div_to_hw(hw, p);
1195 	if (p_div < 0)
1196 		return p_div;
1197 
1198 	cfg->p = p_div;
1199 
1200 	if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
1201 		return -EINVAL;
1202 
1203 	return 0;
1204 }
1205 
1206 #if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1207 	defined(CONFIG_ARCH_TEGRA_124_SOC) || \
1208 	defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1209 	defined(CONFIG_ARCH_TEGRA_210_SOC)
1210 
1211 u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate)
1212 {
1213 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1214 
1215 	return (u16)_pll_fixed_mdiv(pll->params, input_rate);
1216 }
1217 
1218 static unsigned long _clip_vco_min(unsigned long vco_min,
1219 				   unsigned long parent_rate)
1220 {
1221 	return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
1222 }
1223 
1224 static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
1225 			       void __iomem *clk_base,
1226 			       unsigned long parent_rate)
1227 {
1228 	u32 val;
1229 	u32 step_a, step_b;
1230 
1231 	switch (parent_rate) {
1232 	case 12000000:
1233 	case 13000000:
1234 	case 26000000:
1235 		step_a = 0x2B;
1236 		step_b = 0x0B;
1237 		break;
1238 	case 16800000:
1239 		step_a = 0x1A;
1240 		step_b = 0x09;
1241 		break;
1242 	case 19200000:
1243 		step_a = 0x12;
1244 		step_b = 0x08;
1245 		break;
1246 	default:
1247 		pr_err("%s: Unexpected reference rate %lu\n",
1248 			__func__, parent_rate);
1249 		WARN_ON(1);
1250 		return -EINVAL;
1251 	}
1252 
1253 	val = step_a << pll_params->stepa_shift;
1254 	val |= step_b << pll_params->stepb_shift;
1255 	writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
1256 
1257 	return 0;
1258 }
1259 
1260 static int _pll_ramp_calc_pll(struct clk_hw *hw,
1261 			      struct tegra_clk_pll_freq_table *cfg,
1262 			      unsigned long rate, unsigned long parent_rate)
1263 {
1264 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1265 	int err = 0;
1266 
1267 	err = _get_table_rate(hw, cfg, rate, parent_rate);
1268 	if (err < 0)
1269 		err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
1270 	else {
1271 		if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
1272 			WARN_ON(1);
1273 			err = -EINVAL;
1274 			goto out;
1275 		}
1276 	}
1277 
1278 	if (cfg->p >  pll->params->max_p)
1279 		err = -EINVAL;
1280 
1281 out:
1282 	return err;
1283 }
1284 
1285 static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
1286 				unsigned long parent_rate)
1287 {
1288 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1289 	struct tegra_clk_pll_freq_table cfg, old_cfg;
1290 	unsigned long flags = 0;
1291 	int ret;
1292 
1293 	ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1294 	if (ret < 0)
1295 		return ret;
1296 
1297 	if (pll->lock)
1298 		spin_lock_irqsave(pll->lock, flags);
1299 
1300 	_get_pll_mnp(pll, &old_cfg);
1301 	if (pll->params->flags & TEGRA_PLL_VCO_OUT)
1302 		cfg.p = old_cfg.p;
1303 
1304 	if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
1305 		ret = _program_pll(hw, &cfg, rate);
1306 
1307 	if (pll->lock)
1308 		spin_unlock_irqrestore(pll->lock, flags);
1309 
1310 	return ret;
1311 }
1312 
1313 static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1314 				unsigned long *prate)
1315 {
1316 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1317 	struct tegra_clk_pll_freq_table cfg;
1318 	int ret, p_div;
1319 	u64 output_rate = *prate;
1320 
1321 	ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1322 	if (ret < 0)
1323 		return ret;
1324 
1325 	p_div = _hw_to_p_div(hw, cfg.p);
1326 	if (p_div < 0)
1327 		return p_div;
1328 
1329 	if (pll->params->set_gain)
1330 		pll->params->set_gain(&cfg);
1331 
1332 	output_rate *= cfg.n;
1333 	do_div(output_rate, cfg.m * p_div);
1334 
1335 	return output_rate;
1336 }
1337 
1338 static void _pllcx_strobe(struct tegra_clk_pll *pll)
1339 {
1340 	u32 val;
1341 
1342 	val = pll_readl_misc(pll);
1343 	val |= PLLCX_MISC_STROBE;
1344 	pll_writel_misc(val, pll);
1345 	udelay(2);
1346 
1347 	val &= ~PLLCX_MISC_STROBE;
1348 	pll_writel_misc(val, pll);
1349 }
1350 
1351 static int clk_pllc_enable(struct clk_hw *hw)
1352 {
1353 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1354 	u32 val;
1355 	int ret;
1356 	unsigned long flags = 0;
1357 
1358 	if (pll->lock)
1359 		spin_lock_irqsave(pll->lock, flags);
1360 
1361 	_clk_pll_enable(hw);
1362 	udelay(2);
1363 
1364 	val = pll_readl_misc(pll);
1365 	val &= ~PLLCX_MISC_RESET;
1366 	pll_writel_misc(val, pll);
1367 	udelay(2);
1368 
1369 	_pllcx_strobe(pll);
1370 
1371 	ret = clk_pll_wait_for_lock(pll);
1372 
1373 	if (pll->lock)
1374 		spin_unlock_irqrestore(pll->lock, flags);
1375 
1376 	return ret;
1377 }
1378 
1379 static void _clk_pllc_disable(struct clk_hw *hw)
1380 {
1381 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1382 	u32 val;
1383 
1384 	_clk_pll_disable(hw);
1385 
1386 	val = pll_readl_misc(pll);
1387 	val |= PLLCX_MISC_RESET;
1388 	pll_writel_misc(val, pll);
1389 	udelay(2);
1390 }
1391 
1392 static void clk_pllc_disable(struct clk_hw *hw)
1393 {
1394 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1395 	unsigned long flags = 0;
1396 
1397 	if (pll->lock)
1398 		spin_lock_irqsave(pll->lock, flags);
1399 
1400 	_clk_pllc_disable(hw);
1401 
1402 	if (pll->lock)
1403 		spin_unlock_irqrestore(pll->lock, flags);
1404 }
1405 
1406 static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1407 					unsigned long input_rate, u32 n)
1408 {
1409 	u32 val, n_threshold;
1410 
1411 	switch (input_rate) {
1412 	case 12000000:
1413 		n_threshold = 70;
1414 		break;
1415 	case 13000000:
1416 	case 26000000:
1417 		n_threshold = 71;
1418 		break;
1419 	case 16800000:
1420 		n_threshold = 55;
1421 		break;
1422 	case 19200000:
1423 		n_threshold = 48;
1424 		break;
1425 	default:
1426 		pr_err("%s: Unexpected reference rate %lu\n",
1427 			__func__, input_rate);
1428 		return -EINVAL;
1429 	}
1430 
1431 	val = pll_readl_misc(pll);
1432 	val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1433 	val |= n <= n_threshold ?
1434 		PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1435 	pll_writel_misc(val, pll);
1436 
1437 	return 0;
1438 }
1439 
1440 static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1441 				unsigned long parent_rate)
1442 {
1443 	struct tegra_clk_pll_freq_table cfg, old_cfg;
1444 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1445 	unsigned long flags = 0;
1446 	int state, ret = 0;
1447 
1448 	if (pll->lock)
1449 		spin_lock_irqsave(pll->lock, flags);
1450 
1451 	ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1452 	if (ret < 0)
1453 		goto out;
1454 
1455 	_get_pll_mnp(pll, &old_cfg);
1456 
1457 	if (cfg.m != old_cfg.m) {
1458 		WARN_ON(1);
1459 		goto out;
1460 	}
1461 
1462 	if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
1463 		goto out;
1464 
1465 	state = clk_pll_is_enabled(hw);
1466 	if (state)
1467 		_clk_pllc_disable(hw);
1468 
1469 	ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1470 	if (ret < 0)
1471 		goto out;
1472 
1473 	_update_pll_mnp(pll, &cfg);
1474 
1475 	if (state)
1476 		ret = clk_pllc_enable(hw);
1477 
1478 out:
1479 	if (pll->lock)
1480 		spin_unlock_irqrestore(pll->lock, flags);
1481 
1482 	return ret;
1483 }
1484 
1485 static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1486 			     struct tegra_clk_pll_freq_table *cfg,
1487 			     unsigned long rate, unsigned long parent_rate)
1488 {
1489 	u16 m, n;
1490 	u64 output_rate = parent_rate;
1491 
1492 	m = _pll_fixed_mdiv(pll->params, parent_rate);
1493 	n = rate * m / parent_rate;
1494 
1495 	output_rate *= n;
1496 	do_div(output_rate, m);
1497 
1498 	if (cfg) {
1499 		cfg->m = m;
1500 		cfg->n = n;
1501 	}
1502 
1503 	return output_rate;
1504 }
1505 
1506 static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1507 				unsigned long parent_rate)
1508 {
1509 	struct tegra_clk_pll_freq_table cfg, old_cfg;
1510 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1511 	unsigned long flags = 0;
1512 	int state, ret = 0;
1513 
1514 	if (pll->lock)
1515 		spin_lock_irqsave(pll->lock, flags);
1516 
1517 	_pllre_calc_rate(pll, &cfg, rate, parent_rate);
1518 	_get_pll_mnp(pll, &old_cfg);
1519 	cfg.p = old_cfg.p;
1520 
1521 	if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1522 		state = clk_pll_is_enabled(hw);
1523 		if (state)
1524 			_clk_pll_disable(hw);
1525 
1526 		_update_pll_mnp(pll, &cfg);
1527 
1528 		if (state) {
1529 			_clk_pll_enable(hw);
1530 			ret = clk_pll_wait_for_lock(pll);
1531 		}
1532 	}
1533 
1534 	if (pll->lock)
1535 		spin_unlock_irqrestore(pll->lock, flags);
1536 
1537 	return ret;
1538 }
1539 
1540 static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1541 					 unsigned long parent_rate)
1542 {
1543 	struct tegra_clk_pll_freq_table cfg;
1544 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1545 	u64 rate = parent_rate;
1546 
1547 	_get_pll_mnp(pll, &cfg);
1548 
1549 	rate *= cfg.n;
1550 	do_div(rate, cfg.m);
1551 
1552 	return rate;
1553 }
1554 
1555 static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1556 				 unsigned long *prate)
1557 {
1558 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1559 
1560 	return _pllre_calc_rate(pll, NULL, rate, *prate);
1561 }
1562 
1563 static int clk_plle_tegra114_enable(struct clk_hw *hw)
1564 {
1565 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1566 	struct tegra_clk_pll_freq_table sel;
1567 	u32 val;
1568 	int ret;
1569 	unsigned long flags = 0;
1570 	unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
1571 
1572 	if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
1573 		return -EINVAL;
1574 
1575 	if (pll->lock)
1576 		spin_lock_irqsave(pll->lock, flags);
1577 
1578 	val = pll_readl_base(pll);
1579 	val &= ~BIT(29); /* Disable lock override */
1580 	pll_writel_base(val, pll);
1581 
1582 	val = pll_readl(pll->params->aux_reg, pll);
1583 	val |= PLLE_AUX_ENABLE_SWCTL;
1584 	val &= ~PLLE_AUX_SEQ_ENABLE;
1585 	pll_writel(val, pll->params->aux_reg, pll);
1586 	udelay(1);
1587 
1588 	val = pll_readl_misc(pll);
1589 	val |= PLLE_MISC_LOCK_ENABLE;
1590 	val |= PLLE_MISC_IDDQ_SW_CTRL;
1591 	val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1592 	val |= PLLE_MISC_PLLE_PTS;
1593 	val &= ~(PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK);
1594 	pll_writel_misc(val, pll);
1595 	udelay(5);
1596 
1597 	val = pll_readl(PLLE_SS_CTRL, pll);
1598 	val |= PLLE_SS_DISABLE;
1599 	pll_writel(val, PLLE_SS_CTRL, pll);
1600 
1601 	val = pll_readl_base(pll);
1602 	val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1603 		 divm_mask_shifted(pll));
1604 	val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
1605 	val |= sel.m << divm_shift(pll);
1606 	val |= sel.n << divn_shift(pll);
1607 	val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1608 	pll_writel_base(val, pll);
1609 	udelay(1);
1610 
1611 	_clk_pll_enable(hw);
1612 	ret = clk_pll_wait_for_lock(pll);
1613 
1614 	if (ret < 0)
1615 		goto out;
1616 
1617 	val = pll_readl(PLLE_SS_CTRL, pll);
1618 	val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1619 	val &= ~PLLE_SS_COEFFICIENTS_MASK;
1620 	val |= PLLE_SS_COEFFICIENTS_VAL_TEGRA114;
1621 	pll_writel(val, PLLE_SS_CTRL, pll);
1622 	val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1623 	pll_writel(val, PLLE_SS_CTRL, pll);
1624 	udelay(1);
1625 	val &= ~PLLE_SS_CNTL_INTERP_RESET;
1626 	pll_writel(val, PLLE_SS_CTRL, pll);
1627 	udelay(1);
1628 
1629 	/* Enable hw control of xusb brick pll */
1630 	val = pll_readl_misc(pll);
1631 	val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1632 	pll_writel_misc(val, pll);
1633 
1634 	val = pll_readl(pll->params->aux_reg, pll);
1635 	val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1636 	val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1637 	pll_writel(val, pll->params->aux_reg, pll);
1638 	udelay(1);
1639 	val |= PLLE_AUX_SEQ_ENABLE;
1640 	pll_writel(val, pll->params->aux_reg, pll);
1641 
1642 	val = pll_readl(XUSBIO_PLL_CFG0, pll);
1643 	val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1644 		XUSBIO_PLL_CFG0_SEQ_START_STATE);
1645 	val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1646 		 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1647 	pll_writel(val, XUSBIO_PLL_CFG0, pll);
1648 	udelay(1);
1649 	val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1650 	pll_writel(val, XUSBIO_PLL_CFG0, pll);
1651 
1652 	/* Enable hw control of SATA pll */
1653 	val = pll_readl(SATA_PLL_CFG0, pll);
1654 	val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
1655 	val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1656 	val |= SATA_PLL_CFG0_SEQ_START_STATE;
1657 	pll_writel(val, SATA_PLL_CFG0, pll);
1658 
1659 	udelay(1);
1660 
1661 	val = pll_readl(SATA_PLL_CFG0, pll);
1662 	val |= SATA_PLL_CFG0_SEQ_ENABLE;
1663 	pll_writel(val, SATA_PLL_CFG0, pll);
1664 
1665 out:
1666 	if (pll->lock)
1667 		spin_unlock_irqrestore(pll->lock, flags);
1668 
1669 	return ret;
1670 }
1671 
1672 static void clk_plle_tegra114_disable(struct clk_hw *hw)
1673 {
1674 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1675 	unsigned long flags = 0;
1676 	u32 val;
1677 
1678 	if (pll->lock)
1679 		spin_lock_irqsave(pll->lock, flags);
1680 
1681 	_clk_pll_disable(hw);
1682 
1683 	val = pll_readl_misc(pll);
1684 	val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1685 	pll_writel_misc(val, pll);
1686 	udelay(1);
1687 
1688 	if (pll->lock)
1689 		spin_unlock_irqrestore(pll->lock, flags);
1690 }
1691 
1692 static int clk_pllu_tegra114_enable(struct clk_hw *hw)
1693 {
1694 	struct tegra_clk_pll *pll = to_clk_pll(hw);
1695 	const struct utmi_clk_param *params = NULL;
1696 	struct clk *osc = __clk_lookup("osc");
1697 	unsigned long flags = 0, input_rate;
1698 	unsigned int i;
1699 	int ret = 0;
1700 	u32 value;
1701 
1702 	if (!osc) {
1703 		pr_err("%s: failed to get OSC clock\n", __func__);
1704 		return -EINVAL;
1705 	}
1706 
1707 	input_rate = clk_hw_get_rate(__clk_get_hw(osc));
1708 
1709 	if (pll->lock)
1710 		spin_lock_irqsave(pll->lock, flags);
1711 
1712 	_clk_pll_enable(hw);
1713 
1714 	ret = clk_pll_wait_for_lock(pll);
1715 	if (ret < 0)
1716 		goto out;
1717 
1718 	for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1719 		if (input_rate == utmi_parameters[i].osc_frequency) {
1720 			params = &utmi_parameters[i];
1721 			break;
1722 		}
1723 	}
1724 
1725 	if (!params) {
1726 		pr_err("%s: unexpected input rate %lu Hz\n", __func__,
1727 		       input_rate);
1728 		ret = -EINVAL;
1729 		goto out;
1730 	}
1731 
1732 	value = pll_readl_base(pll);
1733 	value &= ~PLLU_BASE_OVERRIDE;
1734 	pll_writel_base(value, pll);
1735 
1736 	value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
1737 	/* Program UTMIP PLL stable and active counts */
1738 	value &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1739 	value |= UTMIP_PLL_CFG2_STABLE_COUNT(params->stable_count);
1740 	value &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1741 	value |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(params->active_delay_count);
1742 	/* Remove power downs from UTMIP PLL control bits */
1743 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1744 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1745 	value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1746 	writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
1747 
1748 	value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1749 	/* Program UTMIP PLL delay and oscillator frequency counts */
1750 	value &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1751 	value |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(params->enable_delay_count);
1752 	value &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1753 	value |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(params->xtal_freq_count);
1754 	/* Remove power downs from UTMIP PLL control bits */
1755 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1756 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1757 	value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP;
1758 	value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1759 	writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1760 
1761 	/* Setup HW control of UTMIPLL */
1762 	value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1763 	value |= UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET;
1764 	value &= ~UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL;
1765 	value |= UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE;
1766 	writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1767 
1768 	value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1769 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
1770 	value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1771 	writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1772 
1773 	udelay(1);
1774 
1775 	/*
1776 	 * Setup SW override of UTMIPLL assuming USB2.0 ports are assigned
1777 	 * to USB2
1778 	 */
1779 	value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1780 	value |= UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL;
1781 	value &= ~UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE;
1782 	writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1783 
1784 	udelay(1);
1785 
1786 	/* Enable HW control of UTMIPLL */
1787 	value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1788 	value |= UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE;
1789 	writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1790 
1791 out:
1792 	if (pll->lock)
1793 		spin_unlock_irqrestore(pll->lock, flags);
1794 
1795 	return ret;
1796 }
1797 #endif
1798 
1799 static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
1800 		void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1801 		spinlock_t *lock)
1802 {
1803 	struct tegra_clk_pll *pll;
1804 
1805 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1806 	if (!pll)
1807 		return ERR_PTR(-ENOMEM);
1808 
1809 	pll->clk_base = clk_base;
1810 	pll->pmc = pmc;
1811 
1812 	pll->params = pll_params;
1813 	pll->lock = lock;
1814 
1815 	if (!pll_params->div_nmp)
1816 		pll_params->div_nmp = &default_nmp;
1817 
1818 	return pll;
1819 }
1820 
1821 static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1822 		const char *name, const char *parent_name, unsigned long flags,
1823 		const struct clk_ops *ops)
1824 {
1825 	struct clk_init_data init;
1826 
1827 	init.name = name;
1828 	init.ops = ops;
1829 	init.flags = flags;
1830 	init.parent_names = (parent_name ? &parent_name : NULL);
1831 	init.num_parents = (parent_name ? 1 : 0);
1832 
1833 	/* Default to _calc_rate if unspecified */
1834 	if (!pll->params->calc_rate) {
1835 		if (pll->params->flags & TEGRA_PLLM)
1836 			pll->params->calc_rate = _calc_dynamic_ramp_rate;
1837 		else
1838 			pll->params->calc_rate = _calc_rate;
1839 	}
1840 
1841 	if (pll->params->set_defaults)
1842 		pll->params->set_defaults(pll);
1843 
1844 	/* Data in .init is copied by clk_register(), so stack variable OK */
1845 	pll->hw.init = &init;
1846 
1847 	return clk_register(NULL, &pll->hw);
1848 }
1849 
1850 struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1851 		void __iomem *clk_base, void __iomem *pmc,
1852 		unsigned long flags, struct tegra_clk_pll_params *pll_params,
1853 		spinlock_t *lock)
1854 {
1855 	struct tegra_clk_pll *pll;
1856 	struct clk *clk;
1857 
1858 	pll_params->flags |= TEGRA_PLL_BYPASS;
1859 
1860 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1861 	if (IS_ERR(pll))
1862 		return ERR_CAST(pll);
1863 
1864 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1865 				      &tegra_clk_pll_ops);
1866 	if (IS_ERR(clk))
1867 		kfree(pll);
1868 
1869 	return clk;
1870 }
1871 
1872 static struct div_nmp pll_e_nmp = {
1873 	.divn_shift = PLLE_BASE_DIVN_SHIFT,
1874 	.divn_width = PLLE_BASE_DIVN_WIDTH,
1875 	.divm_shift = PLLE_BASE_DIVM_SHIFT,
1876 	.divm_width = PLLE_BASE_DIVM_WIDTH,
1877 	.divp_shift = PLLE_BASE_DIVP_SHIFT,
1878 	.divp_width = PLLE_BASE_DIVP_WIDTH,
1879 };
1880 
1881 struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1882 		void __iomem *clk_base, void __iomem *pmc,
1883 		unsigned long flags, struct tegra_clk_pll_params *pll_params,
1884 		spinlock_t *lock)
1885 {
1886 	struct tegra_clk_pll *pll;
1887 	struct clk *clk;
1888 
1889 	pll_params->flags |= TEGRA_PLL_BYPASS;
1890 
1891 	if (!pll_params->div_nmp)
1892 		pll_params->div_nmp = &pll_e_nmp;
1893 
1894 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
1895 	if (IS_ERR(pll))
1896 		return ERR_CAST(pll);
1897 
1898 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1899 				      &tegra_clk_plle_ops);
1900 	if (IS_ERR(clk))
1901 		kfree(pll);
1902 
1903 	return clk;
1904 }
1905 
1906 struct clk *tegra_clk_register_pllu(const char *name, const char *parent_name,
1907 		void __iomem *clk_base, unsigned long flags,
1908 		struct tegra_clk_pll_params *pll_params, spinlock_t *lock)
1909 {
1910 	struct tegra_clk_pll *pll;
1911 	struct clk *clk;
1912 
1913 	pll_params->flags |= TEGRA_PLLU;
1914 
1915 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1916 	if (IS_ERR(pll))
1917 		return ERR_CAST(pll);
1918 
1919 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1920 				      &tegra_clk_pllu_ops);
1921 	if (IS_ERR(clk))
1922 		kfree(pll);
1923 
1924 	return clk;
1925 }
1926 
1927 #if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1928 	defined(CONFIG_ARCH_TEGRA_124_SOC) || \
1929 	defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1930 	defined(CONFIG_ARCH_TEGRA_210_SOC)
1931 static const struct clk_ops tegra_clk_pllxc_ops = {
1932 	.is_enabled = clk_pll_is_enabled,
1933 	.enable = clk_pll_enable,
1934 	.disable = clk_pll_disable,
1935 	.recalc_rate = clk_pll_recalc_rate,
1936 	.round_rate = clk_pll_ramp_round_rate,
1937 	.set_rate = clk_pllxc_set_rate,
1938 };
1939 
1940 static const struct clk_ops tegra_clk_pllc_ops = {
1941 	.is_enabled = clk_pll_is_enabled,
1942 	.enable = clk_pllc_enable,
1943 	.disable = clk_pllc_disable,
1944 	.recalc_rate = clk_pll_recalc_rate,
1945 	.round_rate = clk_pll_ramp_round_rate,
1946 	.set_rate = clk_pllc_set_rate,
1947 };
1948 
1949 static const struct clk_ops tegra_clk_pllre_ops = {
1950 	.is_enabled = clk_pll_is_enabled,
1951 	.enable = clk_pll_enable,
1952 	.disable = clk_pll_disable,
1953 	.recalc_rate = clk_pllre_recalc_rate,
1954 	.round_rate = clk_pllre_round_rate,
1955 	.set_rate = clk_pllre_set_rate,
1956 };
1957 
1958 static const struct clk_ops tegra_clk_plle_tegra114_ops = {
1959 	.is_enabled =  clk_pll_is_enabled,
1960 	.enable = clk_plle_tegra114_enable,
1961 	.disable = clk_plle_tegra114_disable,
1962 	.recalc_rate = clk_pll_recalc_rate,
1963 };
1964 
1965 static const struct clk_ops tegra_clk_pllu_tegra114_ops = {
1966 	.is_enabled =  clk_pll_is_enabled,
1967 	.enable = clk_pllu_tegra114_enable,
1968 	.disable = clk_pll_disable,
1969 	.recalc_rate = clk_pll_recalc_rate,
1970 };
1971 
1972 struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1973 			  void __iomem *clk_base, void __iomem *pmc,
1974 			  unsigned long flags,
1975 			  struct tegra_clk_pll_params *pll_params,
1976 			  spinlock_t *lock)
1977 {
1978 	struct tegra_clk_pll *pll;
1979 	struct clk *clk, *parent;
1980 	unsigned long parent_rate;
1981 	u32 val, val_iddq;
1982 
1983 	parent = __clk_lookup(parent_name);
1984 	if (!parent) {
1985 		WARN(1, "parent clk %s of %s must be registered first\n",
1986 			parent_name, name);
1987 		return ERR_PTR(-EINVAL);
1988 	}
1989 
1990 	if (!pll_params->pdiv_tohw)
1991 		return ERR_PTR(-EINVAL);
1992 
1993 	parent_rate = clk_get_rate(parent);
1994 
1995 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1996 
1997 	if (pll_params->adjust_vco)
1998 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
1999 							     parent_rate);
2000 
2001 	/*
2002 	 * If the pll has a set_defaults callback, it will take care of
2003 	 * configuring dynamic ramping and setting IDDQ in that path.
2004 	 */
2005 	if (!pll_params->set_defaults) {
2006 		int err;
2007 
2008 		err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
2009 		if (err)
2010 			return ERR_PTR(err);
2011 
2012 		val = readl_relaxed(clk_base + pll_params->base_reg);
2013 		val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
2014 
2015 		if (val & PLL_BASE_ENABLE)
2016 			WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
2017 		else {
2018 			val_iddq |= BIT(pll_params->iddq_bit_idx);
2019 			writel_relaxed(val_iddq,
2020 				       clk_base + pll_params->iddq_reg);
2021 		}
2022 	}
2023 
2024 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2025 	if (IS_ERR(pll))
2026 		return ERR_CAST(pll);
2027 
2028 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2029 				      &tegra_clk_pllxc_ops);
2030 	if (IS_ERR(clk))
2031 		kfree(pll);
2032 
2033 	return clk;
2034 }
2035 
2036 struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
2037 			  void __iomem *clk_base, void __iomem *pmc,
2038 			  unsigned long flags,
2039 			  struct tegra_clk_pll_params *pll_params,
2040 			  spinlock_t *lock, unsigned long parent_rate)
2041 {
2042 	u32 val;
2043 	struct tegra_clk_pll *pll;
2044 	struct clk *clk;
2045 
2046 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2047 
2048 	if (pll_params->adjust_vco)
2049 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2050 							     parent_rate);
2051 
2052 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2053 	if (IS_ERR(pll))
2054 		return ERR_CAST(pll);
2055 
2056 	/* program minimum rate by default */
2057 
2058 	val = pll_readl_base(pll);
2059 	if (val & PLL_BASE_ENABLE)
2060 		WARN_ON(readl_relaxed(clk_base + pll_params->iddq_reg) &
2061 				BIT(pll_params->iddq_bit_idx));
2062 	else {
2063 		int m;
2064 
2065 		m = _pll_fixed_mdiv(pll_params, parent_rate);
2066 		val = m << divm_shift(pll);
2067 		val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
2068 		pll_writel_base(val, pll);
2069 	}
2070 
2071 	/* disable lock override */
2072 
2073 	val = pll_readl_misc(pll);
2074 	val &= ~BIT(29);
2075 	pll_writel_misc(val, pll);
2076 
2077 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2078 				      &tegra_clk_pllre_ops);
2079 	if (IS_ERR(clk))
2080 		kfree(pll);
2081 
2082 	return clk;
2083 }
2084 
2085 struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
2086 			  void __iomem *clk_base, void __iomem *pmc,
2087 			  unsigned long flags,
2088 			  struct tegra_clk_pll_params *pll_params,
2089 			  spinlock_t *lock)
2090 {
2091 	struct tegra_clk_pll *pll;
2092 	struct clk *clk, *parent;
2093 	unsigned long parent_rate;
2094 
2095 	if (!pll_params->pdiv_tohw)
2096 		return ERR_PTR(-EINVAL);
2097 
2098 	parent = __clk_lookup(parent_name);
2099 	if (!parent) {
2100 		WARN(1, "parent clk %s of %s must be registered first\n",
2101 			parent_name, name);
2102 		return ERR_PTR(-EINVAL);
2103 	}
2104 
2105 	parent_rate = clk_get_rate(parent);
2106 
2107 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2108 
2109 	if (pll_params->adjust_vco)
2110 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2111 							     parent_rate);
2112 
2113 	pll_params->flags |= TEGRA_PLL_BYPASS;
2114 	pll_params->flags |= TEGRA_PLLM;
2115 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2116 	if (IS_ERR(pll))
2117 		return ERR_CAST(pll);
2118 
2119 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2120 				      &tegra_clk_pll_ops);
2121 	if (IS_ERR(clk))
2122 		kfree(pll);
2123 
2124 	return clk;
2125 }
2126 
2127 struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
2128 			  void __iomem *clk_base, void __iomem *pmc,
2129 			  unsigned long flags,
2130 			  struct tegra_clk_pll_params *pll_params,
2131 			  spinlock_t *lock)
2132 {
2133 	struct clk *parent, *clk;
2134 	const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
2135 	struct tegra_clk_pll *pll;
2136 	struct tegra_clk_pll_freq_table cfg;
2137 	unsigned long parent_rate;
2138 
2139 	if (!p_tohw)
2140 		return ERR_PTR(-EINVAL);
2141 
2142 	parent = __clk_lookup(parent_name);
2143 	if (!parent) {
2144 		WARN(1, "parent clk %s of %s must be registered first\n",
2145 			parent_name, name);
2146 		return ERR_PTR(-EINVAL);
2147 	}
2148 
2149 	parent_rate = clk_get_rate(parent);
2150 
2151 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2152 
2153 	pll_params->flags |= TEGRA_PLL_BYPASS;
2154 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2155 	if (IS_ERR(pll))
2156 		return ERR_CAST(pll);
2157 
2158 	/*
2159 	 * Most of PLLC register fields are shadowed, and can not be read
2160 	 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
2161 	 * Initialize PLL to default state: disabled, reset; shadow registers
2162 	 * loaded with default parameters; dividers are preset for half of
2163 	 * minimum VCO rate (the latter assured that shadowed divider settings
2164 	 * are within supported range).
2165 	 */
2166 
2167 	cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2168 	cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2169 
2170 	while (p_tohw->pdiv) {
2171 		if (p_tohw->pdiv == 2) {
2172 			cfg.p = p_tohw->hw_val;
2173 			break;
2174 		}
2175 		p_tohw++;
2176 	}
2177 
2178 	if (!p_tohw->pdiv) {
2179 		WARN_ON(1);
2180 		return ERR_PTR(-EINVAL);
2181 	}
2182 
2183 	pll_writel_base(0, pll);
2184 	_update_pll_mnp(pll, &cfg);
2185 
2186 	pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
2187 	pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
2188 	pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
2189 	pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
2190 
2191 	_pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
2192 
2193 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2194 				      &tegra_clk_pllc_ops);
2195 	if (IS_ERR(clk))
2196 		kfree(pll);
2197 
2198 	return clk;
2199 }
2200 
2201 struct clk *tegra_clk_register_plle_tegra114(const char *name,
2202 				const char *parent_name,
2203 				void __iomem *clk_base, unsigned long flags,
2204 				struct tegra_clk_pll_params *pll_params,
2205 				spinlock_t *lock)
2206 {
2207 	struct tegra_clk_pll *pll;
2208 	struct clk *clk;
2209 	u32 val, val_aux;
2210 
2211 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2212 	if (IS_ERR(pll))
2213 		return ERR_CAST(pll);
2214 
2215 	/* ensure parent is set to pll_re_vco */
2216 
2217 	val = pll_readl_base(pll);
2218 	val_aux = pll_readl(pll_params->aux_reg, pll);
2219 
2220 	if (val & PLL_BASE_ENABLE) {
2221 		if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
2222 			(val_aux & PLLE_AUX_PLLP_SEL))
2223 			WARN(1, "pll_e enabled with unsupported parent %s\n",
2224 			  (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
2225 					"pll_re_vco");
2226 	} else {
2227 		val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
2228 		pll_writel(val_aux, pll_params->aux_reg, pll);
2229 	}
2230 
2231 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2232 				      &tegra_clk_plle_tegra114_ops);
2233 	if (IS_ERR(clk))
2234 		kfree(pll);
2235 
2236 	return clk;
2237 }
2238 
2239 struct clk *
2240 tegra_clk_register_pllu_tegra114(const char *name, const char *parent_name,
2241 				 void __iomem *clk_base, unsigned long flags,
2242 				 struct tegra_clk_pll_params *pll_params,
2243 				 spinlock_t *lock)
2244 {
2245 	struct tegra_clk_pll *pll;
2246 	struct clk *clk;
2247 
2248 	pll_params->flags |= TEGRA_PLLU;
2249 
2250 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2251 	if (IS_ERR(pll))
2252 		return ERR_CAST(pll);
2253 
2254 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2255 				      &tegra_clk_pllu_tegra114_ops);
2256 	if (IS_ERR(clk))
2257 		kfree(pll);
2258 
2259 	return clk;
2260 }
2261 #endif
2262 
2263 #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC) || defined(CONFIG_ARCH_TEGRA_210_SOC)
2264 static const struct clk_ops tegra_clk_pllss_ops = {
2265 	.is_enabled = clk_pll_is_enabled,
2266 	.enable = clk_pll_enable,
2267 	.disable = clk_pll_disable,
2268 	.recalc_rate = clk_pll_recalc_rate,
2269 	.round_rate = clk_pll_ramp_round_rate,
2270 	.set_rate = clk_pllxc_set_rate,
2271 };
2272 
2273 struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
2274 				void __iomem *clk_base, unsigned long flags,
2275 				struct tegra_clk_pll_params *pll_params,
2276 				spinlock_t *lock)
2277 {
2278 	struct tegra_clk_pll *pll;
2279 	struct clk *clk, *parent;
2280 	struct tegra_clk_pll_freq_table cfg;
2281 	unsigned long parent_rate;
2282 	u32 val, val_iddq;
2283 	int i;
2284 
2285 	if (!pll_params->div_nmp)
2286 		return ERR_PTR(-EINVAL);
2287 
2288 	parent = __clk_lookup(parent_name);
2289 	if (!parent) {
2290 		WARN(1, "parent clk %s of %s must be registered first\n",
2291 			parent_name, name);
2292 		return ERR_PTR(-EINVAL);
2293 	}
2294 
2295 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2296 	if (IS_ERR(pll))
2297 		return ERR_CAST(pll);
2298 
2299 	val = pll_readl_base(pll);
2300 	val &= ~PLLSS_REF_SRC_SEL_MASK;
2301 	pll_writel_base(val, pll);
2302 
2303 	parent_rate = clk_get_rate(parent);
2304 
2305 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2306 
2307 	/* initialize PLL to minimum rate */
2308 
2309 	cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2310 	cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2311 
2312 	for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
2313 		;
2314 	if (!i) {
2315 		kfree(pll);
2316 		return ERR_PTR(-EINVAL);
2317 	}
2318 
2319 	cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
2320 
2321 	_update_pll_mnp(pll, &cfg);
2322 
2323 	pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
2324 	pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
2325 	pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
2326 	pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
2327 
2328 	val = pll_readl_base(pll);
2329 	val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
2330 	if (val & PLL_BASE_ENABLE) {
2331 		if (val_iddq & BIT(pll_params->iddq_bit_idx)) {
2332 			WARN(1, "%s is on but IDDQ set\n", name);
2333 			kfree(pll);
2334 			return ERR_PTR(-EINVAL);
2335 		}
2336 	} else {
2337 		val_iddq |= BIT(pll_params->iddq_bit_idx);
2338 		writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
2339 	}
2340 
2341 	val &= ~PLLSS_LOCK_OVERRIDE;
2342 	pll_writel_base(val, pll);
2343 
2344 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2345 					&tegra_clk_pllss_ops);
2346 
2347 	if (IS_ERR(clk))
2348 		kfree(pll);
2349 
2350 	return clk;
2351 }
2352 #endif
2353 
2354 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
2355 struct clk *tegra_clk_register_pllre_tegra210(const char *name,
2356 			  const char *parent_name, void __iomem *clk_base,
2357 			  void __iomem *pmc, unsigned long flags,
2358 			  struct tegra_clk_pll_params *pll_params,
2359 			  spinlock_t *lock, unsigned long parent_rate)
2360 {
2361 	struct tegra_clk_pll *pll;
2362 	struct clk *clk;
2363 
2364 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2365 
2366 	if (pll_params->adjust_vco)
2367 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2368 							     parent_rate);
2369 
2370 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2371 	if (IS_ERR(pll))
2372 		return ERR_CAST(pll);
2373 
2374 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2375 				      &tegra_clk_pll_ops);
2376 	if (IS_ERR(clk))
2377 		kfree(pll);
2378 
2379 	return clk;
2380 }
2381 
2382 static int clk_plle_tegra210_enable(struct clk_hw *hw)
2383 {
2384 	struct tegra_clk_pll *pll = to_clk_pll(hw);
2385 	struct tegra_clk_pll_freq_table sel;
2386 	u32 val;
2387 	int ret = 0;
2388 	unsigned long flags = 0;
2389 	unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
2390 
2391 	if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
2392 		return -EINVAL;
2393 
2394 	if (pll->lock)
2395 		spin_lock_irqsave(pll->lock, flags);
2396 
2397 	val = pll_readl(pll->params->aux_reg, pll);
2398 	if (val & PLLE_AUX_SEQ_ENABLE)
2399 		goto out;
2400 
2401 	val = pll_readl_base(pll);
2402 	val &= ~BIT(30); /* Disable lock override */
2403 	pll_writel_base(val, pll);
2404 
2405 	val = pll_readl_misc(pll);
2406 	val |= PLLE_MISC_LOCK_ENABLE;
2407 	val |= PLLE_MISC_IDDQ_SW_CTRL;
2408 	val &= ~PLLE_MISC_IDDQ_SW_VALUE;
2409 	val |= PLLE_MISC_PLLE_PTS;
2410 	val &= ~(PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK);
2411 	pll_writel_misc(val, pll);
2412 	udelay(5);
2413 
2414 	val = pll_readl(PLLE_SS_CTRL, pll);
2415 	val |= PLLE_SS_DISABLE;
2416 	pll_writel(val, PLLE_SS_CTRL, pll);
2417 
2418 	val = pll_readl_base(pll);
2419 	val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
2420 		 divm_mask_shifted(pll));
2421 	val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
2422 	val |= sel.m << divm_shift(pll);
2423 	val |= sel.n << divn_shift(pll);
2424 	val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
2425 	pll_writel_base(val, pll);
2426 	udelay(1);
2427 
2428 	val = pll_readl_base(pll);
2429 	val |= PLLE_BASE_ENABLE;
2430 	pll_writel_base(val, pll);
2431 
2432 	ret = clk_pll_wait_for_lock(pll);
2433 
2434 	if (ret < 0)
2435 		goto out;
2436 
2437 	val = pll_readl(PLLE_SS_CTRL, pll);
2438 	val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
2439 	val &= ~PLLE_SS_COEFFICIENTS_MASK;
2440 	val |= PLLE_SS_COEFFICIENTS_VAL_TEGRA210;
2441 	pll_writel(val, PLLE_SS_CTRL, pll);
2442 	val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
2443 	pll_writel(val, PLLE_SS_CTRL, pll);
2444 	udelay(1);
2445 	val &= ~PLLE_SS_CNTL_INTERP_RESET;
2446 	pll_writel(val, PLLE_SS_CTRL, pll);
2447 	udelay(1);
2448 
2449 	val = pll_readl_misc(pll);
2450 	val &= ~PLLE_MISC_IDDQ_SW_CTRL;
2451 	pll_writel_misc(val, pll);
2452 
2453 	val = pll_readl(pll->params->aux_reg, pll);
2454 	val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SS_SEQ_INCLUDE);
2455 	val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
2456 	pll_writel(val, pll->params->aux_reg, pll);
2457 	udelay(1);
2458 	val |= PLLE_AUX_SEQ_ENABLE;
2459 	pll_writel(val, pll->params->aux_reg, pll);
2460 
2461 out:
2462 	if (pll->lock)
2463 		spin_unlock_irqrestore(pll->lock, flags);
2464 
2465 	return ret;
2466 }
2467 
2468 static void clk_plle_tegra210_disable(struct clk_hw *hw)
2469 {
2470 	struct tegra_clk_pll *pll = to_clk_pll(hw);
2471 	unsigned long flags = 0;
2472 	u32 val;
2473 
2474 	if (pll->lock)
2475 		spin_lock_irqsave(pll->lock, flags);
2476 
2477 	/* If PLLE HW sequencer is enabled, SW should not disable PLLE */
2478 	val = pll_readl(pll->params->aux_reg, pll);
2479 	if (val & PLLE_AUX_SEQ_ENABLE)
2480 		goto out;
2481 
2482 	val = pll_readl_base(pll);
2483 	val &= ~PLLE_BASE_ENABLE;
2484 	pll_writel_base(val, pll);
2485 
2486 	val = pll_readl(pll->params->aux_reg, pll);
2487 	val |= PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL;
2488 	pll_writel(val, pll->params->aux_reg, pll);
2489 
2490 	val = pll_readl_misc(pll);
2491 	val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
2492 	pll_writel_misc(val, pll);
2493 	udelay(1);
2494 
2495 out:
2496 	if (pll->lock)
2497 		spin_unlock_irqrestore(pll->lock, flags);
2498 }
2499 
2500 static int clk_plle_tegra210_is_enabled(struct clk_hw *hw)
2501 {
2502 	struct tegra_clk_pll *pll = to_clk_pll(hw);
2503 	u32 val;
2504 
2505 	val = pll_readl_base(pll);
2506 
2507 	return val & PLLE_BASE_ENABLE ? 1 : 0;
2508 }
2509 
2510 static const struct clk_ops tegra_clk_plle_tegra210_ops = {
2511 	.is_enabled =  clk_plle_tegra210_is_enabled,
2512 	.enable = clk_plle_tegra210_enable,
2513 	.disable = clk_plle_tegra210_disable,
2514 	.recalc_rate = clk_pll_recalc_rate,
2515 };
2516 
2517 struct clk *tegra_clk_register_plle_tegra210(const char *name,
2518 				const char *parent_name,
2519 				void __iomem *clk_base, unsigned long flags,
2520 				struct tegra_clk_pll_params *pll_params,
2521 				spinlock_t *lock)
2522 {
2523 	struct tegra_clk_pll *pll;
2524 	struct clk *clk;
2525 	u32 val, val_aux;
2526 
2527 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2528 	if (IS_ERR(pll))
2529 		return ERR_CAST(pll);
2530 
2531 	/* ensure parent is set to pll_re_vco */
2532 
2533 	val = pll_readl_base(pll);
2534 	val_aux = pll_readl(pll_params->aux_reg, pll);
2535 
2536 	if (val & PLLE_BASE_ENABLE) {
2537 		if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
2538 			(val_aux & PLLE_AUX_PLLP_SEL))
2539 			WARN(1, "pll_e enabled with unsupported parent %s\n",
2540 			  (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
2541 					"pll_re_vco");
2542 	} else {
2543 		val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
2544 		pll_writel(val_aux, pll_params->aux_reg, pll);
2545 	}
2546 
2547 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2548 				      &tegra_clk_plle_tegra210_ops);
2549 	if (IS_ERR(clk))
2550 		kfree(pll);
2551 
2552 	return clk;
2553 }
2554 
2555 struct clk *tegra_clk_register_pllc_tegra210(const char *name,
2556 			const char *parent_name, void __iomem *clk_base,
2557 			void __iomem *pmc, unsigned long flags,
2558 			struct tegra_clk_pll_params *pll_params,
2559 			spinlock_t *lock)
2560 {
2561 	struct clk *parent, *clk;
2562 	const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
2563 	struct tegra_clk_pll *pll;
2564 	unsigned long parent_rate;
2565 
2566 	if (!p_tohw)
2567 		return ERR_PTR(-EINVAL);
2568 
2569 	parent = __clk_lookup(parent_name);
2570 	if (!parent) {
2571 		WARN(1, "parent clk %s of %s must be registered first\n",
2572 			name, parent_name);
2573 		return ERR_PTR(-EINVAL);
2574 	}
2575 
2576 	parent_rate = clk_get_rate(parent);
2577 
2578 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2579 
2580 	if (pll_params->adjust_vco)
2581 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2582 							     parent_rate);
2583 
2584 	pll_params->flags |= TEGRA_PLL_BYPASS;
2585 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2586 	if (IS_ERR(pll))
2587 		return ERR_CAST(pll);
2588 
2589 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2590 				      &tegra_clk_pll_ops);
2591 	if (IS_ERR(clk))
2592 		kfree(pll);
2593 
2594 	return clk;
2595 }
2596 
2597 struct clk *tegra_clk_register_pllss_tegra210(const char *name,
2598 				const char *parent_name, void __iomem *clk_base,
2599 				unsigned long flags,
2600 				struct tegra_clk_pll_params *pll_params,
2601 				spinlock_t *lock)
2602 {
2603 	struct tegra_clk_pll *pll;
2604 	struct clk *clk, *parent;
2605 	unsigned long parent_rate;
2606 	u32 val;
2607 
2608 	if (!pll_params->div_nmp)
2609 		return ERR_PTR(-EINVAL);
2610 
2611 	parent = __clk_lookup(parent_name);
2612 	if (!parent) {
2613 		WARN(1, "parent clk %s of %s must be registered first\n",
2614 			name, parent_name);
2615 		return ERR_PTR(-EINVAL);
2616 	}
2617 
2618 	val = readl_relaxed(clk_base + pll_params->base_reg);
2619 	if (val & PLLSS_REF_SRC_SEL_MASK) {
2620 		WARN(1, "not supported reference clock for %s\n", name);
2621 		return ERR_PTR(-EINVAL);
2622 	}
2623 
2624 	parent_rate = clk_get_rate(parent);
2625 
2626 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2627 
2628 	if (pll_params->adjust_vco)
2629 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2630 							     parent_rate);
2631 
2632 	pll_params->flags |= TEGRA_PLL_BYPASS;
2633 	pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2634 	if (IS_ERR(pll))
2635 		return ERR_CAST(pll);
2636 
2637 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2638 					&tegra_clk_pll_ops);
2639 
2640 	if (IS_ERR(clk))
2641 		kfree(pll);
2642 
2643 	return clk;
2644 }
2645 
2646 struct clk *tegra_clk_register_pllmb(const char *name, const char *parent_name,
2647 			  void __iomem *clk_base, void __iomem *pmc,
2648 			  unsigned long flags,
2649 			  struct tegra_clk_pll_params *pll_params,
2650 			  spinlock_t *lock)
2651 {
2652 	struct tegra_clk_pll *pll;
2653 	struct clk *clk, *parent;
2654 	unsigned long parent_rate;
2655 
2656 	if (!pll_params->pdiv_tohw)
2657 		return ERR_PTR(-EINVAL);
2658 
2659 	parent = __clk_lookup(parent_name);
2660 	if (!parent) {
2661 		WARN(1, "parent clk %s of %s must be registered first\n",
2662 			parent_name, name);
2663 		return ERR_PTR(-EINVAL);
2664 	}
2665 
2666 	parent_rate = clk_get_rate(parent);
2667 
2668 	pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2669 
2670 	if (pll_params->adjust_vco)
2671 		pll_params->vco_min = pll_params->adjust_vco(pll_params,
2672 							     parent_rate);
2673 
2674 	pll_params->flags |= TEGRA_PLL_BYPASS;
2675 	pll_params->flags |= TEGRA_PLLMB;
2676 	pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2677 	if (IS_ERR(pll))
2678 		return ERR_CAST(pll);
2679 
2680 	clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2681 				      &tegra_clk_pll_ops);
2682 	if (IS_ERR(clk))
2683 		kfree(pll);
2684 
2685 	return clk;
2686 }
2687 
2688 #endif
2689