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