blob: b3855360d6bc0d64121abc127f50e58411b67db7 [file] [log] [blame]
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301/*
Peter De Schrijverdba40722013-04-03 17:40:36 +03002 * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved.
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05303 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/slab.h>
18#include <linux/io.h>
19#include <linux/delay.h>
20#include <linux/err.h>
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053021#include <linux/clk.h>
Stephen Boyd584ac4e2015-06-19 15:00:46 -070022#include <linux/clk-provider.h>
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053023
24#include "clk.h"
25
26#define PLL_BASE_BYPASS BIT(31)
27#define PLL_BASE_ENABLE BIT(30)
28#define PLL_BASE_REF_ENABLE BIT(29)
29#define PLL_BASE_OVERRIDE BIT(28)
30
31#define PLL_BASE_DIVP_SHIFT 20
32#define PLL_BASE_DIVP_WIDTH 3
33#define PLL_BASE_DIVN_SHIFT 8
34#define PLL_BASE_DIVN_WIDTH 10
35#define PLL_BASE_DIVM_SHIFT 0
36#define PLL_BASE_DIVM_WIDTH 5
37#define PLLU_POST_DIVP_MASK 0x1
38
39#define PLL_MISC_DCCON_SHIFT 20
40#define PLL_MISC_CPCON_SHIFT 8
41#define PLL_MISC_CPCON_WIDTH 4
42#define PLL_MISC_CPCON_MASK ((1 << PLL_MISC_CPCON_WIDTH) - 1)
43#define PLL_MISC_LFCON_SHIFT 4
44#define PLL_MISC_LFCON_WIDTH 4
45#define PLL_MISC_LFCON_MASK ((1 << PLL_MISC_LFCON_WIDTH) - 1)
46#define PLL_MISC_VCOCON_SHIFT 0
47#define PLL_MISC_VCOCON_WIDTH 4
48#define PLL_MISC_VCOCON_MASK ((1 << PLL_MISC_VCOCON_WIDTH) - 1)
49
50#define OUT_OF_TABLE_CPCON 8
51
52#define PMC_PLLP_WB0_OVERRIDE 0xf8
53#define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE BIT(12)
54#define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE BIT(11)
55
56#define PLL_POST_LOCK_DELAY 50
57
58#define PLLDU_LFCON_SET_DIVN 600
59
60#define PLLE_BASE_DIVCML_SHIFT 24
Thierry Redingd0f02ce2014-04-04 15:55:13 +020061#define PLLE_BASE_DIVCML_MASK 0xf
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053062#define PLLE_BASE_DIVP_SHIFT 16
Thierry Redingd0f02ce2014-04-04 15:55:13 +020063#define PLLE_BASE_DIVP_WIDTH 6
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053064#define PLLE_BASE_DIVN_SHIFT 8
65#define PLLE_BASE_DIVN_WIDTH 8
66#define PLLE_BASE_DIVM_SHIFT 0
67#define PLLE_BASE_DIVM_WIDTH 8
Rhyland Kleindd322f02015-06-18 17:28:28 -040068#define PLLE_BASE_ENABLE BIT(31)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +053069
70#define PLLE_MISC_SETUP_BASE_SHIFT 16
71#define PLLE_MISC_SETUP_BASE_MASK (0xffff << PLLE_MISC_SETUP_BASE_SHIFT)
72#define PLLE_MISC_LOCK_ENABLE BIT(9)
73#define PLLE_MISC_READY BIT(15)
74#define PLLE_MISC_SETUP_EX_SHIFT 2
75#define PLLE_MISC_SETUP_EX_MASK (3 << PLLE_MISC_SETUP_EX_SHIFT)
76#define PLLE_MISC_SETUP_MASK (PLLE_MISC_SETUP_BASE_MASK | \
77 PLLE_MISC_SETUP_EX_MASK)
78#define PLLE_MISC_SETUP_VALUE (7 << PLLE_MISC_SETUP_BASE_SHIFT)
79
80#define PLLE_SS_CTRL 0x68
Peter De Schrijver642fb0c2013-09-26 18:30:01 +030081#define PLLE_SS_CNTL_BYPASS_SS BIT(10)
82#define PLLE_SS_CNTL_INTERP_RESET BIT(11)
83#define PLLE_SS_CNTL_SSC_BYP BIT(12)
84#define PLLE_SS_CNTL_CENTER BIT(14)
85#define PLLE_SS_CNTL_INVERT BIT(15)
86#define PLLE_SS_DISABLE (PLLE_SS_CNTL_BYPASS_SS | PLLE_SS_CNTL_INTERP_RESET |\
87 PLLE_SS_CNTL_SSC_BYP)
88#define PLLE_SS_MAX_MASK 0x1ff
Mark Kuo442f53f2016-01-14 14:26:42 -050089#define PLLE_SS_MAX_VAL_TEGRA114 0x25
90#define PLLE_SS_MAX_VAL_TEGRA210 0x21
Peter De Schrijver642fb0c2013-09-26 18:30:01 +030091#define PLLE_SS_INC_MASK (0xff << 16)
92#define PLLE_SS_INC_VAL (0x1 << 16)
93#define PLLE_SS_INCINTRV_MASK (0x3f << 24)
Mark Kuo442f53f2016-01-14 14:26:42 -050094#define PLLE_SS_INCINTRV_VAL_TEGRA114 (0x20 << 24)
95#define PLLE_SS_INCINTRV_VAL_TEGRA210 (0x23 << 24)
Peter De Schrijver642fb0c2013-09-26 18:30:01 +030096#define PLLE_SS_COEFFICIENTS_MASK \
97 (PLLE_SS_MAX_MASK | PLLE_SS_INC_MASK | PLLE_SS_INCINTRV_MASK)
Mark Kuo442f53f2016-01-14 14:26:42 -050098#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)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530104
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300105#define PLLE_AUX_PLLP_SEL BIT(2)
Jim Lin2cfe1672014-05-14 17:32:57 -0700106#define PLLE_AUX_USE_LOCKDET BIT(3)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300107#define PLLE_AUX_ENABLE_SWCTL BIT(4)
Jim Lin2cfe1672014-05-14 17:32:57 -0700108#define PLLE_AUX_SS_SWCTL BIT(6)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300109#define PLLE_AUX_SEQ_ENABLE BIT(24)
Jim Lin2cfe1672014-05-14 17:32:57 -0700110#define PLLE_AUX_SEQ_START_STATE BIT(25)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300111#define PLLE_AUX_PLLRE_SEL BIT(28)
Rhyland Kleindd322f02015-06-18 17:28:28 -0400112#define PLLE_AUX_SS_SEQ_INCLUDE BIT(31)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300113
Jim Lin2cfe1672014-05-14 17:32:57 -0700114#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
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300121#define SATA_PLL_CFG0 0x490
122#define SATA_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0)
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +0200123#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)
Mikko Perttunen37ab3662014-06-18 17:23:23 +0300126
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300127#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
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530160#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
Peter De Schrijver798e9102013-09-09 13:22:55 +0300164#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
Andrew Bresticker15d68e82016-05-26 12:41:31 -0400194#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
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530241#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)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300244#define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400245#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)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530247
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)
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300251#define pll_override_writel(val, offset, p) writel(val, p->pmc + offset)
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400252#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)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530254
255#define mask(w) ((1 << (w)) - 1)
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300256#define divm_mask(p) mask(p->params->div_nmp->divm_width)
257#define divn_mask(p) mask(p->params->div_nmp->divn_width)
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300258#define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300259 mask(p->params->div_nmp->divp_width))
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400260#define sdm_din_mask(p) p->params->sdm_din_mask
261#define sdm_en_mask(p) p->params->sdm_ctrl_en_mask
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530262
Thierry Redingc61e4e72014-04-04 15:55:14 +0200263#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
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530271#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
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400275#define sdin_din_to_data(din) ((u16)((din) ? : 0xFFFFU))
276#define sdin_data_to_din(dat) (((dat) == 0xFFFFU) ? 0 : (s16)dat)
277
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300278static 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
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530287static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
288{
289 u32 val;
290
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300291 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530292 return;
293
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300294 if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
Peter De Schrijver7ba28812013-04-03 17:40:38 +0300295 return;
296
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530297 val = pll_readl_misc(pll);
298 val |= BIT(pll->params->lock_enable_bit_idx);
299 pll_writel_misc(val, pll);
300}
301
Peter De Schrijverdba40722013-04-03 17:40:36 +0300302static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530303{
304 int i;
Peter De Schrijver3e727712013-04-03 17:40:40 +0300305 u32 val, lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300306 void __iomem *lock_addr;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530307
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300308 if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530309 udelay(pll->params->lock_delay);
310 return 0;
311 }
312
Peter De Schrijverdba40722013-04-03 17:40:36 +0300313 lock_addr = pll->clk_base;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300314 if (pll->params->flags & TEGRA_PLL_LOCK_MISC)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300315 lock_addr += pll->params->misc_reg;
316 else
317 lock_addr += pll->params->base_reg;
318
Peter De Schrijver3e727712013-04-03 17:40:40 +0300319 lock_mask = pll->params->lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300320
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530321 for (i = 0; i < pll->params->lock_delay; i++) {
322 val = readl_relaxed(lock_addr);
Peter De Schrijver3e727712013-04-03 17:40:40 +0300323 if ((val & lock_mask) == lock_mask) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530324 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__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700331 clk_hw_get_name(&pll->hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530332
333 return -1;
334}
335
Rhyland Klein6583a632015-06-18 17:28:19 -0400336int tegra_pll_wait_for_lock(struct tegra_clk_pll *pll)
337{
338 return clk_pll_wait_for_lock(pll);
339}
340
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530341static int clk_pll_is_enabled(struct clk_hw *hw)
342{
343 struct tegra_clk_pll *pll = to_clk_pll(hw);
344 u32 val;
345
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300346 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530347 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
348 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
Peter De Schrijverdba40722013-04-03 17:40:36 +0300357static void _clk_pll_enable(struct clk_hw *hw)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530358{
359 struct tegra_clk_pll *pll = to_clk_pll(hw);
360 u32 val;
361
Rhyland Klein7db864c2015-06-18 17:28:20 -0400362 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(2);
367 }
368
Bill Huangfde207e2015-06-18 17:28:26 -0400369 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
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530375 clk_pll_enable_lock(pll);
376
377 val = pll_readl_base(pll);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300378 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300379 val &= ~PLL_BASE_BYPASS;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530380 val |= PLL_BASE_ENABLE;
381 pll_writel_base(val, pll);
382
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300383 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530384 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 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530388}
389
390static 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);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300396 if (pll->params->flags & TEGRA_PLL_BYPASS)
Peter De Schrijverdd935872013-04-03 17:40:37 +0300397 val &= ~PLL_BASE_BYPASS;
398 val &= ~PLL_BASE_ENABLE;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530399 pll_writel_base(val, pll);
400
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300401 if (pll->params->flags & TEGRA_PLLM) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530402 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 }
Rhyland Klein7db864c2015-06-18 17:28:20 -0400406
Bill Huangfde207e2015-06-18 17:28:26 -0400407 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
Rhyland Klein7db864c2015-06-18 17:28:20 -0400413 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 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530419}
420
421static int clk_pll_enable(struct clk_hw *hw)
422{
423 struct tegra_clk_pll *pll = to_clk_pll(hw);
424 unsigned long flags = 0;
425 int ret;
426
427 if (pll->lock)
428 spin_lock_irqsave(pll->lock, flags);
429
Peter De Schrijverdba40722013-04-03 17:40:36 +0300430 _clk_pll_enable(hw);
431
432 ret = clk_pll_wait_for_lock(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530433
434 if (pll->lock)
435 spin_unlock_irqrestore(pll->lock, flags);
436
437 return ret;
438}
439
440static void clk_pll_disable(struct clk_hw *hw)
441{
442 struct tegra_clk_pll *pll = to_clk_pll(hw);
443 unsigned long flags = 0;
444
445 if (pll->lock)
446 spin_lock_irqsave(pll->lock, flags);
447
448 _clk_pll_disable(hw);
449
450 if (pll->lock)
451 spin_unlock_irqrestore(pll->lock, flags);
452}
453
Peter De Schrijver053b5252013-06-05 15:56:41 +0300454static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
455{
456 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100457 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300458
459 if (p_tohw) {
460 while (p_tohw->pdiv) {
461 if (p_div <= p_tohw->pdiv)
462 return p_tohw->hw_val;
463 p_tohw++;
464 }
465 return -EINVAL;
466 }
467 return -EINVAL;
468}
469
Rhyland Klein6b301a02015-06-18 17:28:36 -0400470int tegra_pll_p_div_to_hw(struct tegra_clk_pll *pll, u8 p_div)
471{
472 return _p_div_to_hw(&pll->hw, p_div);
473}
474
Peter De Schrijver053b5252013-06-05 15:56:41 +0300475static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
476{
477 struct tegra_clk_pll *pll = to_clk_pll(hw);
Thierry Reding385f9ad2015-11-19 16:34:06 +0100478 const struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300479
480 if (p_tohw) {
481 while (p_tohw->pdiv) {
482 if (p_div_hw == p_tohw->hw_val)
483 return p_tohw->pdiv;
484 p_tohw++;
485 }
486 return -EINVAL;
487 }
488
489 return 1 << p_div_hw;
490}
491
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530492static int _get_table_rate(struct clk_hw *hw,
493 struct tegra_clk_pll_freq_table *cfg,
494 unsigned long rate, unsigned long parent_rate)
495{
496 struct tegra_clk_pll *pll = to_clk_pll(hw);
497 struct tegra_clk_pll_freq_table *sel;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400498 int p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530499
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300500 for (sel = pll->params->freq_table; sel->input_rate != 0; sel++)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530501 if (sel->input_rate == parent_rate &&
502 sel->output_rate == rate)
503 break;
504
505 if (sel->input_rate == 0)
506 return -EINVAL;
507
Rhyland Klein86c679a2015-06-18 17:28:34 -0400508 if (pll->params->pdiv_tohw) {
509 p = _p_div_to_hw(hw, sel->p);
510 if (p < 0)
511 return p;
512 } else {
513 p = ilog2(sel->p);
514 }
515
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530516 cfg->input_rate = sel->input_rate;
517 cfg->output_rate = sel->output_rate;
518 cfg->m = sel->m;
519 cfg->n = sel->n;
Rhyland Klein86c679a2015-06-18 17:28:34 -0400520 cfg->p = p;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530521 cfg->cpcon = sel->cpcon;
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400522 cfg->sdm_data = sel->sdm_data;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530523
524 return 0;
525}
526
527static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
528 unsigned long rate, unsigned long parent_rate)
529{
530 struct tegra_clk_pll *pll = to_clk_pll(hw);
531 unsigned long cfreq;
532 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300533 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530534
535 switch (parent_rate) {
536 case 12000000:
537 case 26000000:
538 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
539 break;
540 case 13000000:
541 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
542 break;
543 case 16800000:
544 case 19200000:
545 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
546 break;
547 case 9600000:
548 case 28800000:
549 /*
550 * PLL_P_OUT1 rate is not listed in PLLA table
551 */
Thierry Redinge52d7c02015-11-18 14:04:20 +0100552 cfreq = parent_rate / (parent_rate / 1000000);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530553 break;
554 default:
555 pr_err("%s Unexpected reference rate %lu\n",
556 __func__, parent_rate);
557 BUG();
558 }
559
560 /* Raise VCO to guarantee 0.5% accuracy */
561 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
562 cfg->output_rate <<= 1)
563 p_div++;
564
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530565 cfg->m = parent_rate / cfreq;
566 cfg->n = cfg->output_rate / cfreq;
567 cfg->cpcon = OUT_OF_TABLE_CPCON;
568
569 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300570 (1 << p_div) > divp_max(pll)
571 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530572 return -EINVAL;
573 }
574
Thierry Reding00c674e2013-11-18 16:11:35 +0100575 cfg->output_rate >>= p_div;
576
Peter De Schrijver053b5252013-06-05 15:56:41 +0300577 if (pll->params->pdiv_tohw) {
578 ret = _p_div_to_hw(hw, 1 << p_div);
579 if (ret < 0)
580 return ret;
581 else
582 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300583 } else
584 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300585
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530586 return 0;
587}
588
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400589/*
590 * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number
591 * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as
592 * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used
593 * to indicate that SDM is disabled.
594 *
595 * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13
596 */
597static void clk_pll_set_sdm_data(struct clk_hw *hw,
598 struct tegra_clk_pll_freq_table *cfg)
599{
600 struct tegra_clk_pll *pll = to_clk_pll(hw);
601 u32 val;
602 bool enabled;
603
604 if (!pll->params->sdm_din_reg)
605 return;
606
607 if (cfg->sdm_data) {
608 val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll));
609 val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll);
610 pll_writel_sdm_din(val, pll);
611 }
612
613 val = pll_readl_sdm_ctrl(pll);
614 enabled = (val & sdm_en_mask(pll));
615
616 if (cfg->sdm_data == 0 && enabled)
617 val &= ~pll->params->sdm_ctrl_en_mask;
618
619 if (cfg->sdm_data != 0 && !enabled)
620 val |= pll->params->sdm_ctrl_en_mask;
621
622 pll_writel_sdm_ctrl(val, pll);
623}
624
Peter De Schrijverdba40722013-04-03 17:40:36 +0300625static void _update_pll_mnp(struct tegra_clk_pll *pll,
626 struct tegra_clk_pll_freq_table *cfg)
627{
628 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300629 struct tegra_clk_pll_params *params = pll->params;
630 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300631
Rhyland Klein69297152015-06-18 17:28:29 -0400632 if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300633 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
634 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
635 val = pll_override_readl(params->pmc_divp_reg, pll);
636 val &= ~(divp_mask(pll) << div_nmp->override_divp_shift);
637 val |= cfg->p << div_nmp->override_divp_shift;
638 pll_override_writel(val, params->pmc_divp_reg, pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300639
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300640 val = pll_override_readl(params->pmc_divnm_reg, pll);
641 val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) |
642 ~(divn_mask(pll) << div_nmp->override_divn_shift);
643 val |= (cfg->m << div_nmp->override_divm_shift) |
644 (cfg->n << div_nmp->override_divn_shift);
645 pll_override_writel(val, params->pmc_divnm_reg, pll);
646 } else {
647 val = pll_readl_base(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300648
Thierry Redingc61e4e72014-04-04 15:55:14 +0200649 val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) |
650 divp_mask_shifted(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300651
Thierry Redingc61e4e72014-04-04 15:55:14 +0200652 val |= (cfg->m << divm_shift(pll)) |
653 (cfg->n << divn_shift(pll)) |
654 (cfg->p << divp_shift(pll));
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300655
656 pll_writel_base(val, pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400657
658 clk_pll_set_sdm_data(&pll->hw, cfg);
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300659 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300660}
661
662static void _get_pll_mnp(struct tegra_clk_pll *pll,
663 struct tegra_clk_pll_freq_table *cfg)
664{
665 u32 val;
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300666 struct tegra_clk_pll_params *params = pll->params;
667 struct div_nmp *div_nmp = params->div_nmp;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300668
Rhyland Klein69297152015-06-18 17:28:29 -0400669 if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300670 (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) &
671 PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) {
672 val = pll_override_readl(params->pmc_divp_reg, pll);
673 cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300674
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300675 val = pll_override_readl(params->pmc_divnm_reg, pll);
676 cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll);
677 cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll);
678 } else {
679 val = pll_readl_base(pll);
680
681 cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll);
682 cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll);
683 cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll);
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400684
685 if (pll->params->sdm_din_reg) {
686 if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) {
687 val = pll_readl_sdm_din(pll);
688 val &= sdm_din_mask(pll);
689 cfg->sdm_data = sdin_din_to_data(val);
690 }
691 }
Peter De Schrijver408a24f2013-06-06 13:47:31 +0300692 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300693}
694
695static void _update_pll_cpcon(struct tegra_clk_pll *pll,
696 struct tegra_clk_pll_freq_table *cfg,
697 unsigned long rate)
698{
699 u32 val;
700
701 val = pll_readl_misc(pll);
702
703 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
704 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
705
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300706 if (pll->params->flags & TEGRA_PLL_SET_LFCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300707 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
708 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
709 val |= 1 << PLL_MISC_LFCON_SHIFT;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300710 } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) {
Peter De Schrijverdba40722013-04-03 17:40:36 +0300711 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
712 if (rate >= (pll->params->vco_max >> 1))
713 val |= 1 << PLL_MISC_DCCON_SHIFT;
714 }
715
716 pll_writel_misc(val, pll);
717}
718
Bill Huang0ef9db62015-06-18 17:28:33 -0400719static void pll_clk_start_ss(struct tegra_clk_pll *pll)
720{
721 if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
722 u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
723
724 val |= pll->params->ssc_ctrl_en_mask;
725 pll_writel(val, pll->params->ssc_ctrl_reg, pll);
726 }
727}
728
729static void pll_clk_stop_ss(struct tegra_clk_pll *pll)
730{
731 if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) {
732 u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll);
733
734 val &= ~pll->params->ssc_ctrl_en_mask;
735 pll_writel(val, pll->params->ssc_ctrl_reg, pll);
736 }
737}
738
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530739static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
740 unsigned long rate)
741{
742 struct tegra_clk_pll *pll = to_clk_pll(hw);
Bill Huangb9851142015-06-18 17:28:31 -0400743 struct tegra_clk_pll_freq_table old_cfg;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300744 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530745
746 state = clk_pll_is_enabled(hw);
747
Bill Huangb9851142015-06-18 17:28:31 -0400748 _get_pll_mnp(pll, &old_cfg);
749
Rhyland Klein17e92732015-06-18 17:28:32 -0400750 if (state && pll->params->defaults_set && pll->params->dyn_ramp &&
751 (cfg->m == old_cfg.m) && (cfg->p == old_cfg.p)) {
752 ret = pll->params->dyn_ramp(pll, cfg);
753 if (!ret)
754 return 0;
755 }
756
Bill Huang0ef9db62015-06-18 17:28:33 -0400757 if (state) {
758 pll_clk_stop_ss(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300759 _clk_pll_disable(hw);
Bill Huang0ef9db62015-06-18 17:28:33 -0400760 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530761
Bill Huangb9851142015-06-18 17:28:31 -0400762 if (!pll->params->defaults_set && pll->params->set_defaults)
763 pll->params->set_defaults(pll);
764
Peter De Schrijverdba40722013-04-03 17:40:36 +0300765 _update_pll_mnp(pll, cfg);
766
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300767 if (pll->params->flags & TEGRA_PLL_HAS_CPCON)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300768 _update_pll_cpcon(pll, cfg, rate);
769
770 if (state) {
771 _clk_pll_enable(hw);
772 ret = clk_pll_wait_for_lock(pll);
Bill Huang0ef9db62015-06-18 17:28:33 -0400773 pll_clk_start_ss(pll);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300774 }
775
776 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530777}
778
779static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
780 unsigned long parent_rate)
781{
782 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300783 struct tegra_clk_pll_freq_table cfg, old_cfg;
784 unsigned long flags = 0;
785 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530786
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300787 if (pll->params->flags & TEGRA_PLL_FIXED) {
788 if (rate != pll->params->fixed_rate) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530789 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700790 __func__, clk_hw_get_name(hw),
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300791 pll->params->fixed_rate, rate);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530792 return -EINVAL;
793 }
794 return 0;
795 }
796
797 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400798 pll->params->calc_rate(hw, &cfg, rate, parent_rate)) {
Thierry Reding8ba4b3b2013-11-27 17:26:03 +0100799 pr_err("%s: Failed to set %s rate %lu\n", __func__,
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700800 clk_hw_get_name(hw), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300801 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530802 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300803 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300804 if (pll->lock)
805 spin_lock_irqsave(pll->lock, flags);
806
807 _get_pll_mnp(pll, &old_cfg);
Andrew Brestickerafff4552015-06-18 17:28:37 -0400808 if (pll->params->flags & TEGRA_PLL_VCO_OUT)
809 cfg.p = old_cfg.p;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300810
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400811 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p ||
812 old_cfg.sdm_data != cfg.sdm_data)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300813 ret = _program_pll(hw, &cfg, rate);
814
815 if (pll->lock)
816 spin_unlock_irqrestore(pll->lock, flags);
817
818 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530819}
820
821static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
822 unsigned long *prate)
823{
824 struct tegra_clk_pll *pll = to_clk_pll(hw);
825 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530826
Danny Huang267b62a2015-06-18 17:28:27 -0400827 if (pll->params->flags & TEGRA_PLL_FIXED) {
Rhyland Klein69297152015-06-18 17:28:29 -0400828 /* PLLM/MB are used for memory; we do not change rate */
829 if (pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB))
Danny Huang267b62a2015-06-18 17:28:27 -0400830 return clk_hw_get_rate(hw);
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300831 return pll->params->fixed_rate;
Danny Huang267b62a2015-06-18 17:28:27 -0400832 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530833
834 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Rhyland Klein407254d2015-06-18 17:28:25 -0400835 pll->params->calc_rate(hw, &cfg, rate, *prate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530836 return -EINVAL;
837
Peter De Schrijver053b5252013-06-05 15:56:41 +0300838 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530839}
840
841static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
842 unsigned long parent_rate)
843{
844 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300845 struct tegra_clk_pll_freq_table cfg;
846 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530847 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300848 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530849
Peter De Schrijverdba40722013-04-03 17:40:36 +0300850 val = pll_readl_base(pll);
851
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300852 if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530853 return parent_rate;
854
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300855 if ((pll->params->flags & TEGRA_PLL_FIXED) &&
Rhyland Klein69297152015-06-18 17:28:29 -0400856 !(pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) &&
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300857 !(val & PLL_BASE_OVERRIDE)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530858 struct tegra_clk_pll_freq_table sel;
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300859 if (_get_table_rate(hw, &sel, pll->params->fixed_rate,
860 parent_rate)) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530861 pr_err("Clock %s has unknown fixed frequency\n",
Stephen Boyd836ee0f2015-08-12 11:42:23 -0700862 clk_hw_get_name(hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530863 BUG();
864 }
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300865 return pll->params->fixed_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530866 }
867
Peter De Schrijverdba40722013-04-03 17:40:36 +0300868 _get_pll_mnp(pll, &cfg);
869
Andrew Brestickerafff4552015-06-18 17:28:37 -0400870 if (pll->params->flags & TEGRA_PLL_VCO_OUT) {
Peter De Schrijver053b5252013-06-05 15:56:41 +0300871 pdiv = 1;
Andrew Brestickerafff4552015-06-18 17:28:37 -0400872 } else {
873 pdiv = _hw_to_p_div(hw, cfg.p);
874 if (pdiv < 0) {
875 WARN(1, "Clock %s has invalid pdiv value : 0x%x\n",
876 clk_hw_get_name(hw), cfg.p);
877 pdiv = 1;
878 }
Peter De Schrijver053b5252013-06-05 15:56:41 +0300879 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300880
Rhyland Kleind907f4b2015-06-18 17:28:24 -0400881 if (pll->params->set_gain)
882 pll->params->set_gain(&cfg);
883
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300884 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530885
Peter De Schrijverdba40722013-04-03 17:40:36 +0300886 rate *= cfg.n;
887 do_div(rate, cfg.m);
888
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530889 return rate;
890}
891
892static int clk_plle_training(struct tegra_clk_pll *pll)
893{
894 u32 val;
895 unsigned long timeout;
896
897 if (!pll->pmc)
898 return -ENOSYS;
899
900 /*
901 * PLLE is already disabled, and setup cleared;
902 * create falling edge on PLLE IDDQ input.
903 */
904 val = readl(pll->pmc + PMC_SATA_PWRGT);
905 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
906 writel(val, pll->pmc + PMC_SATA_PWRGT);
907
908 val = readl(pll->pmc + PMC_SATA_PWRGT);
909 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
910 writel(val, pll->pmc + PMC_SATA_PWRGT);
911
912 val = readl(pll->pmc + PMC_SATA_PWRGT);
913 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
914 writel(val, pll->pmc + PMC_SATA_PWRGT);
915
916 val = pll_readl_misc(pll);
917
918 timeout = jiffies + msecs_to_jiffies(100);
919 while (1) {
920 val = pll_readl_misc(pll);
921 if (val & PLLE_MISC_READY)
922 break;
923 if (time_after(jiffies, timeout)) {
924 pr_err("%s: timeout waiting for PLLE\n", __func__);
925 return -EBUSY;
926 }
927 udelay(300);
928 }
929
930 return 0;
931}
932
933static int clk_plle_enable(struct clk_hw *hw)
934{
935 struct tegra_clk_pll *pll = to_clk_pll(hw);
Andrew Bresticker3eb61562016-01-14 14:24:34 -0500936 unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530937 struct tegra_clk_pll_freq_table sel;
938 u32 val;
939 int err;
940
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300941 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530942 return -EINVAL;
943
944 clk_pll_disable(hw);
945
946 val = pll_readl_misc(pll);
947 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
948 pll_writel_misc(val, pll);
949
950 val = pll_readl_misc(pll);
951 if (!(val & PLLE_MISC_READY)) {
952 err = clk_plle_training(pll);
953 if (err)
954 return err;
955 }
956
Peter De Schrijverebe142b2013-10-04 17:28:34 +0300957 if (pll->params->flags & TEGRA_PLLE_CONFIGURE) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530958 /* configure dividers */
959 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200960 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
961 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200962 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +0200963 val |= sel.m << divm_shift(pll);
964 val |= sel.n << divn_shift(pll);
965 val |= sel.p << divp_shift(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530966 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
967 pll_writel_base(val, pll);
968 }
969
970 val = pll_readl_misc(pll);
971 val |= PLLE_MISC_SETUP_VALUE;
972 val |= PLLE_MISC_LOCK_ENABLE;
973 pll_writel_misc(val, pll);
974
975 val = readl(pll->clk_base + PLLE_SS_CTRL);
Thierry Redingd0f02ce2014-04-04 15:55:13 +0200976 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530977 val |= PLLE_SS_DISABLE;
978 writel(val, pll->clk_base + PLLE_SS_CTRL);
979
Thierry Reding4ccc4022014-04-04 15:55:15 +0200980 val = pll_readl_base(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530981 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
982 pll_writel_base(val, pll);
983
Peter De Schrijverdba40722013-04-03 17:40:36 +0300984 clk_pll_wait_for_lock(pll);
985
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530986 return 0;
987}
988
989static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
990 unsigned long parent_rate)
991{
992 struct tegra_clk_pll *pll = to_clk_pll(hw);
993 u32 val = pll_readl_base(pll);
994 u32 divn = 0, divm = 0, divp = 0;
995 u64 rate = parent_rate;
996
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +0300997 divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll));
998 divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll));
999 divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll));
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301000 divm *= divp;
1001
1002 rate *= divn;
1003 do_div(rate, divm);
1004 return rate;
1005}
1006
1007const struct clk_ops tegra_clk_pll_ops = {
1008 .is_enabled = clk_pll_is_enabled,
1009 .enable = clk_pll_enable,
1010 .disable = clk_pll_disable,
1011 .recalc_rate = clk_pll_recalc_rate,
1012 .round_rate = clk_pll_round_rate,
1013 .set_rate = clk_pll_set_rate,
1014};
1015
1016const struct clk_ops tegra_clk_plle_ops = {
1017 .recalc_rate = clk_plle_recalc_rate,
1018 .is_enabled = clk_pll_is_enabled,
1019 .disable = clk_pll_disable,
1020 .enable = clk_plle_enable,
1021};
1022
Andrew Bresticker15d68e82016-05-26 12:41:31 -04001023/*
1024 * Structure defining the fields for USB UTMI clocks Parameters.
1025 */
1026struct utmi_clk_param {
1027 /* Oscillator Frequency in Hz */
1028 u32 osc_frequency;
1029 /* UTMIP PLL Enable Delay Count */
1030 u8 enable_delay_count;
1031 /* UTMIP PLL Stable count */
1032 u8 stable_count;
1033 /* UTMIP PLL Active delay count */
1034 u8 active_delay_count;
1035 /* UTMIP PLL Xtal frequency count */
1036 u8 xtal_freq_count;
1037};
1038
1039static const struct utmi_clk_param utmi_parameters[] = {
1040 {
1041 .osc_frequency = 13000000, .enable_delay_count = 0x02,
1042 .stable_count = 0x33, .active_delay_count = 0x05,
1043 .xtal_freq_count = 0x7f
1044 }, {
1045 .osc_frequency = 19200000, .enable_delay_count = 0x03,
1046 .stable_count = 0x4b, .active_delay_count = 0x06,
1047 .xtal_freq_count = 0xbb
1048 }, {
1049 .osc_frequency = 12000000, .enable_delay_count = 0x02,
1050 .stable_count = 0x2f, .active_delay_count = 0x04,
1051 .xtal_freq_count = 0x76
1052 }, {
1053 .osc_frequency = 26000000, .enable_delay_count = 0x04,
1054 .stable_count = 0x66, .active_delay_count = 0x09,
1055 .xtal_freq_count = 0xfe
1056 }, {
1057 .osc_frequency = 16800000, .enable_delay_count = 0x03,
1058 .stable_count = 0x41, .active_delay_count = 0x0a,
1059 .xtal_freq_count = 0xa4
1060 }, {
1061 .osc_frequency = 38400000, .enable_delay_count = 0x0,
1062 .stable_count = 0x0, .active_delay_count = 0x6,
1063 .xtal_freq_count = 0x80
1064 },
1065};
1066
1067static int clk_pllu_enable(struct clk_hw *hw)
1068{
1069 struct tegra_clk_pll *pll = to_clk_pll(hw);
1070 struct clk_hw *pll_ref = clk_hw_get_parent(hw);
1071 struct clk_hw *osc = clk_hw_get_parent(pll_ref);
1072 const struct utmi_clk_param *params = NULL;
1073 unsigned long flags = 0, input_rate;
1074 unsigned int i;
1075 int ret = 0;
1076 u32 value;
1077
1078 if (!osc) {
1079 pr_err("%s: failed to get OSC clock\n", __func__);
1080 return -EINVAL;
1081 }
1082
1083 input_rate = clk_hw_get_rate(osc);
1084
1085 if (pll->lock)
1086 spin_lock_irqsave(pll->lock, flags);
1087
1088 _clk_pll_enable(hw);
1089
1090 ret = clk_pll_wait_for_lock(pll);
1091 if (ret < 0)
1092 goto out;
1093
1094 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1095 if (input_rate == utmi_parameters[i].osc_frequency) {
1096 params = &utmi_parameters[i];
1097 break;
1098 }
1099 }
1100
1101 if (!params) {
1102 pr_err("%s: unexpected input rate %lu Hz\n", __func__,
1103 input_rate);
1104 ret = -EINVAL;
1105 goto out;
1106 }
1107
1108 value = pll_readl_base(pll);
1109 value &= ~PLLU_BASE_OVERRIDE;
1110 pll_writel_base(value, pll);
1111
1112 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
1113 /* Program UTMIP PLL stable and active counts */
1114 value &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1115 value |= UTMIP_PLL_CFG2_STABLE_COUNT(params->stable_count);
1116 value &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1117 value |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(params->active_delay_count);
1118 /* Remove power downs from UTMIP PLL control bits */
1119 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1120 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1121 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1122 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
1123
1124 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1125 /* Program UTMIP PLL delay and oscillator frequency counts */
1126 value &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1127 value |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(params->enable_delay_count);
1128 value &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1129 value |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(params->xtal_freq_count);
1130 /* Remove power downs from UTMIP PLL control bits */
1131 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1132 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1133 value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1134 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1135
1136out:
1137 if (pll->lock)
1138 spin_unlock_irqrestore(pll->lock, flags);
1139
1140 return ret;
1141}
1142
1143static const struct clk_ops tegra_clk_pllu_ops = {
1144 .is_enabled = clk_pll_is_enabled,
1145 .enable = clk_pllu_enable,
1146 .disable = clk_pll_disable,
1147 .recalc_rate = clk_pll_recalc_rate,
1148};
1149
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001150static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
1151 unsigned long parent_rate)
1152{
Rhyland Klein407254d2015-06-18 17:28:25 -04001153 u16 mdiv = parent_rate / pll_params->cf_min;
1154
1155 if (pll_params->flags & TEGRA_MDIV_NEW)
1156 return (!pll_params->mdiv_default ? mdiv :
1157 min(mdiv, pll_params->mdiv_default));
1158
1159 if (pll_params->mdiv_default)
1160 return pll_params->mdiv_default;
1161
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001162 if (parent_rate > pll_params->cf_max)
1163 return 2;
1164 else
1165 return 1;
1166}
1167
Rhyland Klein86c679a2015-06-18 17:28:34 -04001168static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
1169 struct tegra_clk_pll_freq_table *cfg,
1170 unsigned long rate, unsigned long parent_rate)
1171{
1172 struct tegra_clk_pll *pll = to_clk_pll(hw);
1173 unsigned int p;
1174 int p_div;
1175
1176 if (!rate)
1177 return -EINVAL;
1178
1179 p = DIV_ROUND_UP(pll->params->vco_min, rate);
1180 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
1181 cfg->output_rate = rate * p;
1182 cfg->n = cfg->output_rate * cfg->m / parent_rate;
1183 cfg->input_rate = parent_rate;
1184
1185 p_div = _p_div_to_hw(hw, p);
1186 if (p_div < 0)
1187 return p_div;
1188
1189 cfg->p = p_div;
1190
1191 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
1192 return -EINVAL;
1193
1194 return 0;
1195}
1196
1197#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1198 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
Rhyland Kleindd322f02015-06-18 17:28:28 -04001199 defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1200 defined(CONFIG_ARCH_TEGRA_210_SOC)
Rhyland Klein86c679a2015-06-18 17:28:34 -04001201
Rhyland Klein407254d2015-06-18 17:28:25 -04001202u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate)
1203{
1204 struct tegra_clk_pll *pll = to_clk_pll(hw);
1205
1206 return (u16)_pll_fixed_mdiv(pll->params, input_rate);
1207}
1208
Peter De Schrijver04edb092013-09-06 14:37:37 +03001209static unsigned long _clip_vco_min(unsigned long vco_min,
1210 unsigned long parent_rate)
1211{
1212 return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate;
1213}
1214
1215static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params,
1216 void __iomem *clk_base,
1217 unsigned long parent_rate)
1218{
1219 u32 val;
1220 u32 step_a, step_b;
1221
1222 switch (parent_rate) {
1223 case 12000000:
1224 case 13000000:
1225 case 26000000:
1226 step_a = 0x2B;
1227 step_b = 0x0B;
1228 break;
1229 case 16800000:
1230 step_a = 0x1A;
1231 step_b = 0x09;
1232 break;
1233 case 19200000:
1234 step_a = 0x12;
1235 step_b = 0x08;
1236 break;
1237 default:
1238 pr_err("%s: Unexpected reference rate %lu\n",
1239 __func__, parent_rate);
1240 WARN_ON(1);
1241 return -EINVAL;
1242 }
1243
1244 val = step_a << pll_params->stepa_shift;
1245 val |= step_b << pll_params->stepb_shift;
1246 writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg);
1247
1248 return 0;
1249}
1250
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001251static int _pll_ramp_calc_pll(struct clk_hw *hw,
1252 struct tegra_clk_pll_freq_table *cfg,
1253 unsigned long rate, unsigned long parent_rate)
1254{
1255 struct tegra_clk_pll *pll = to_clk_pll(hw);
Rhyland Klein86c679a2015-06-18 17:28:34 -04001256 int err = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001257
1258 err = _get_table_rate(hw, cfg, rate, parent_rate);
1259 if (err < 0)
1260 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +03001261 else {
1262 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001263 WARN_ON(1);
1264 err = -EINVAL;
1265 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001266 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001267 }
1268
Peter De Schrijver053b5252013-06-05 15:56:41 +03001269 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001270 err = -EINVAL;
1271
1272out:
1273 return err;
1274}
1275
1276static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
1277 unsigned long parent_rate)
1278{
1279 struct tegra_clk_pll *pll = to_clk_pll(hw);
1280 struct tegra_clk_pll_freq_table cfg, old_cfg;
1281 unsigned long flags = 0;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001282 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001283
1284 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1285 if (ret < 0)
1286 return ret;
1287
1288 if (pll->lock)
1289 spin_lock_irqsave(pll->lock, flags);
1290
1291 _get_pll_mnp(pll, &old_cfg);
Andrew Brestickerafff4552015-06-18 17:28:37 -04001292 if (pll->params->flags & TEGRA_PLL_VCO_OUT)
1293 cfg.p = old_cfg.p;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001294
Peter De Schrijver053b5252013-06-05 15:56:41 +03001295 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001296 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001297
1298 if (pll->lock)
1299 spin_unlock_irqrestore(pll->lock, flags);
1300
1301 return ret;
1302}
1303
1304static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
1305 unsigned long *prate)
1306{
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001307 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001308 struct tegra_clk_pll_freq_table cfg;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001309 int ret, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001310 u64 output_rate = *prate;
1311
1312 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
1313 if (ret < 0)
1314 return ret;
1315
Peter De Schrijver053b5252013-06-05 15:56:41 +03001316 p_div = _hw_to_p_div(hw, cfg.p);
1317 if (p_div < 0)
1318 return p_div;
1319
Rhyland Kleind907f4b2015-06-18 17:28:24 -04001320 if (pll->params->set_gain)
1321 pll->params->set_gain(&cfg);
1322
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001323 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +03001324 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001325
1326 return output_rate;
1327}
1328
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001329static void _pllcx_strobe(struct tegra_clk_pll *pll)
1330{
1331 u32 val;
1332
1333 val = pll_readl_misc(pll);
1334 val |= PLLCX_MISC_STROBE;
1335 pll_writel_misc(val, pll);
1336 udelay(2);
1337
1338 val &= ~PLLCX_MISC_STROBE;
1339 pll_writel_misc(val, pll);
1340}
1341
1342static int clk_pllc_enable(struct clk_hw *hw)
1343{
1344 struct tegra_clk_pll *pll = to_clk_pll(hw);
1345 u32 val;
Thierry Reding44a6f3db2015-02-18 16:25:16 +01001346 int ret;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001347 unsigned long flags = 0;
1348
1349 if (pll->lock)
1350 spin_lock_irqsave(pll->lock, flags);
1351
1352 _clk_pll_enable(hw);
1353 udelay(2);
1354
1355 val = pll_readl_misc(pll);
1356 val &= ~PLLCX_MISC_RESET;
1357 pll_writel_misc(val, pll);
1358 udelay(2);
1359
1360 _pllcx_strobe(pll);
1361
1362 ret = clk_pll_wait_for_lock(pll);
1363
1364 if (pll->lock)
1365 spin_unlock_irqrestore(pll->lock, flags);
1366
1367 return ret;
1368}
1369
1370static void _clk_pllc_disable(struct clk_hw *hw)
1371{
1372 struct tegra_clk_pll *pll = to_clk_pll(hw);
1373 u32 val;
1374
1375 _clk_pll_disable(hw);
1376
1377 val = pll_readl_misc(pll);
1378 val |= PLLCX_MISC_RESET;
1379 pll_writel_misc(val, pll);
1380 udelay(2);
1381}
1382
1383static void clk_pllc_disable(struct clk_hw *hw)
1384{
1385 struct tegra_clk_pll *pll = to_clk_pll(hw);
1386 unsigned long flags = 0;
1387
1388 if (pll->lock)
1389 spin_lock_irqsave(pll->lock, flags);
1390
1391 _clk_pllc_disable(hw);
1392
1393 if (pll->lock)
1394 spin_unlock_irqrestore(pll->lock, flags);
1395}
1396
1397static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
1398 unsigned long input_rate, u32 n)
1399{
1400 u32 val, n_threshold;
1401
1402 switch (input_rate) {
1403 case 12000000:
1404 n_threshold = 70;
1405 break;
1406 case 13000000:
1407 case 26000000:
1408 n_threshold = 71;
1409 break;
1410 case 16800000:
1411 n_threshold = 55;
1412 break;
1413 case 19200000:
1414 n_threshold = 48;
1415 break;
1416 default:
1417 pr_err("%s: Unexpected reference rate %lu\n",
1418 __func__, input_rate);
1419 return -EINVAL;
1420 }
1421
1422 val = pll_readl_misc(pll);
1423 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1424 val |= n <= n_threshold ?
1425 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1426 pll_writel_misc(val, pll);
1427
1428 return 0;
1429}
1430
1431static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1432 unsigned long parent_rate)
1433{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001434 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001435 struct tegra_clk_pll *pll = to_clk_pll(hw);
1436 unsigned long flags = 0;
1437 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001438
1439 if (pll->lock)
1440 spin_lock_irqsave(pll->lock, flags);
1441
1442 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1443 if (ret < 0)
1444 goto out;
1445
Peter De Schrijver053b5252013-06-05 15:56:41 +03001446 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001447
Peter De Schrijver053b5252013-06-05 15:56:41 +03001448 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001449 WARN_ON(1);
1450 goto out;
1451 }
1452
Peter De Schrijver053b5252013-06-05 15:56:41 +03001453 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001454 goto out;
1455
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001456 state = clk_pll_is_enabled(hw);
1457 if (state)
1458 _clk_pllc_disable(hw);
1459
1460 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1461 if (ret < 0)
1462 goto out;
1463
1464 _update_pll_mnp(pll, &cfg);
1465
1466 if (state)
1467 ret = clk_pllc_enable(hw);
1468
1469out:
1470 if (pll->lock)
1471 spin_unlock_irqrestore(pll->lock, flags);
1472
1473 return ret;
1474}
1475
1476static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1477 struct tegra_clk_pll_freq_table *cfg,
1478 unsigned long rate, unsigned long parent_rate)
1479{
1480 u16 m, n;
1481 u64 output_rate = parent_rate;
1482
1483 m = _pll_fixed_mdiv(pll->params, parent_rate);
1484 n = rate * m / parent_rate;
1485
1486 output_rate *= n;
1487 do_div(output_rate, m);
1488
1489 if (cfg) {
1490 cfg->m = m;
1491 cfg->n = n;
1492 }
1493
1494 return output_rate;
1495}
Thierry Reding6bb18c52014-08-01 10:44:20 +02001496
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001497static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1498 unsigned long parent_rate)
1499{
1500 struct tegra_clk_pll_freq_table cfg, old_cfg;
1501 struct tegra_clk_pll *pll = to_clk_pll(hw);
1502 unsigned long flags = 0;
1503 int state, ret = 0;
1504
1505 if (pll->lock)
1506 spin_lock_irqsave(pll->lock, flags);
1507
1508 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1509 _get_pll_mnp(pll, &old_cfg);
1510 cfg.p = old_cfg.p;
1511
1512 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1513 state = clk_pll_is_enabled(hw);
1514 if (state)
1515 _clk_pll_disable(hw);
1516
1517 _update_pll_mnp(pll, &cfg);
1518
1519 if (state) {
1520 _clk_pll_enable(hw);
1521 ret = clk_pll_wait_for_lock(pll);
1522 }
1523 }
1524
1525 if (pll->lock)
1526 spin_unlock_irqrestore(pll->lock, flags);
1527
1528 return ret;
1529}
1530
1531static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1532 unsigned long parent_rate)
1533{
1534 struct tegra_clk_pll_freq_table cfg;
1535 struct tegra_clk_pll *pll = to_clk_pll(hw);
1536 u64 rate = parent_rate;
1537
1538 _get_pll_mnp(pll, &cfg);
1539
1540 rate *= cfg.n;
1541 do_div(rate, cfg.m);
1542
1543 return rate;
1544}
1545
1546static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1547 unsigned long *prate)
1548{
1549 struct tegra_clk_pll *pll = to_clk_pll(hw);
1550
1551 return _pllre_calc_rate(pll, NULL, rate, *prate);
1552}
1553
1554static int clk_plle_tegra114_enable(struct clk_hw *hw)
1555{
1556 struct tegra_clk_pll *pll = to_clk_pll(hw);
1557 struct tegra_clk_pll_freq_table sel;
1558 u32 val;
1559 int ret;
1560 unsigned long flags = 0;
Andrew Bresticker3eb61562016-01-14 14:24:34 -05001561 unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001562
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001563 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001564 return -EINVAL;
1565
1566 if (pll->lock)
1567 spin_lock_irqsave(pll->lock, flags);
1568
1569 val = pll_readl_base(pll);
1570 val &= ~BIT(29); /* Disable lock override */
1571 pll_writel_base(val, pll);
1572
1573 val = pll_readl(pll->params->aux_reg, pll);
1574 val |= PLLE_AUX_ENABLE_SWCTL;
1575 val &= ~PLLE_AUX_SEQ_ENABLE;
1576 pll_writel(val, pll->params->aux_reg, pll);
1577 udelay(1);
1578
1579 val = pll_readl_misc(pll);
1580 val |= PLLE_MISC_LOCK_ENABLE;
1581 val |= PLLE_MISC_IDDQ_SW_CTRL;
1582 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1583 val |= PLLE_MISC_PLLE_PTS;
Rhyland Kleinfd2963b2016-01-14 14:24:37 -05001584 val &= ~(PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001585 pll_writel_misc(val, pll);
1586 udelay(5);
1587
1588 val = pll_readl(PLLE_SS_CTRL, pll);
1589 val |= PLLE_SS_DISABLE;
1590 pll_writel(val, PLLE_SS_CTRL, pll);
1591
1592 val = pll_readl_base(pll);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001593 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
1594 divm_mask_shifted(pll));
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001595 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
Thierry Redingc61e4e72014-04-04 15:55:14 +02001596 val |= sel.m << divm_shift(pll);
1597 val |= sel.n << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001598 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1599 pll_writel_base(val, pll);
1600 udelay(1);
1601
1602 _clk_pll_enable(hw);
1603 ret = clk_pll_wait_for_lock(pll);
1604
1605 if (ret < 0)
1606 goto out;
1607
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001608 val = pll_readl(PLLE_SS_CTRL, pll);
1609 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
1610 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Mark Kuo442f53f2016-01-14 14:26:42 -05001611 val |= PLLE_SS_COEFFICIENTS_VAL_TEGRA114;
Peter De Schrijver642fb0c2013-09-26 18:30:01 +03001612 pll_writel(val, PLLE_SS_CTRL, pll);
1613 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
1614 pll_writel(val, PLLE_SS_CTRL, pll);
1615 udelay(1);
1616 val &= ~PLLE_SS_CNTL_INTERP_RESET;
1617 pll_writel(val, PLLE_SS_CTRL, pll);
1618 udelay(1);
1619
Jim Lin2cfe1672014-05-14 17:32:57 -07001620 /* Enable hw control of xusb brick pll */
1621 val = pll_readl_misc(pll);
1622 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
1623 pll_writel_misc(val, pll);
1624
1625 val = pll_readl(pll->params->aux_reg, pll);
1626 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE);
1627 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
1628 pll_writel(val, pll->params->aux_reg, pll);
1629 udelay(1);
1630 val |= PLLE_AUX_SEQ_ENABLE;
1631 pll_writel(val, pll->params->aux_reg, pll);
1632
1633 val = pll_readl(XUSBIO_PLL_CFG0, pll);
1634 val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET |
1635 XUSBIO_PLL_CFG0_SEQ_START_STATE);
1636 val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL |
1637 XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL);
1638 pll_writel(val, XUSBIO_PLL_CFG0, pll);
1639 udelay(1);
1640 val |= XUSBIO_PLL_CFG0_SEQ_ENABLE;
1641 pll_writel(val, XUSBIO_PLL_CFG0, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001642
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001643 /* Enable hw control of SATA pll */
1644 val = pll_readl(SATA_PLL_CFG0, pll);
1645 val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL;
Mikko Perttunen0e548d50b2014-07-08 09:30:15 +02001646 val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET;
1647 val |= SATA_PLL_CFG0_SEQ_START_STATE;
1648 pll_writel(val, SATA_PLL_CFG0, pll);
1649
1650 udelay(1);
1651
1652 val = pll_readl(SATA_PLL_CFG0, pll);
1653 val |= SATA_PLL_CFG0_SEQ_ENABLE;
Mikko Perttunen37ab3662014-06-18 17:23:23 +03001654 pll_writel(val, SATA_PLL_CFG0, pll);
1655
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001656out:
1657 if (pll->lock)
1658 spin_unlock_irqrestore(pll->lock, flags);
1659
1660 return ret;
1661}
1662
1663static void clk_plle_tegra114_disable(struct clk_hw *hw)
1664{
1665 struct tegra_clk_pll *pll = to_clk_pll(hw);
1666 unsigned long flags = 0;
1667 u32 val;
1668
1669 if (pll->lock)
1670 spin_lock_irqsave(pll->lock, flags);
1671
1672 _clk_pll_disable(hw);
1673
1674 val = pll_readl_misc(pll);
1675 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1676 pll_writel_misc(val, pll);
1677 udelay(1);
1678
1679 if (pll->lock)
1680 spin_unlock_irqrestore(pll->lock, flags);
1681}
Andrew Bresticker15d68e82016-05-26 12:41:31 -04001682
1683static int clk_pllu_tegra114_enable(struct clk_hw *hw)
1684{
1685 struct tegra_clk_pll *pll = to_clk_pll(hw);
1686 const struct utmi_clk_param *params = NULL;
1687 struct clk *osc = __clk_lookup("osc");
1688 unsigned long flags = 0, input_rate;
1689 unsigned int i;
1690 int ret = 0;
1691 u32 value;
1692
1693 if (!osc) {
1694 pr_err("%s: failed to get OSC clock\n", __func__);
1695 return -EINVAL;
1696 }
1697
1698 input_rate = clk_hw_get_rate(__clk_get_hw(osc));
1699
1700 if (pll->lock)
1701 spin_lock_irqsave(pll->lock, flags);
1702
1703 _clk_pll_enable(hw);
1704
1705 ret = clk_pll_wait_for_lock(pll);
1706 if (ret < 0)
1707 goto out;
1708
1709 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
1710 if (input_rate == utmi_parameters[i].osc_frequency) {
1711 params = &utmi_parameters[i];
1712 break;
1713 }
1714 }
1715
1716 if (!params) {
1717 pr_err("%s: unexpected input rate %lu Hz\n", __func__,
1718 input_rate);
1719 ret = -EINVAL;
1720 goto out;
1721 }
1722
1723 value = pll_readl_base(pll);
1724 value &= ~PLLU_BASE_OVERRIDE;
1725 pll_writel_base(value, pll);
1726
1727 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
1728 /* Program UTMIP PLL stable and active counts */
1729 value &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
1730 value |= UTMIP_PLL_CFG2_STABLE_COUNT(params->stable_count);
1731 value &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
1732 value |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(params->active_delay_count);
1733 /* Remove power downs from UTMIP PLL control bits */
1734 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
1735 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
1736 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_C_POWERDOWN;
1737 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
1738
1739 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1740 /* Program UTMIP PLL delay and oscillator frequency counts */
1741 value &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
1742 value |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(params->enable_delay_count);
1743 value &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
1744 value |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(params->xtal_freq_count);
1745 /* Remove power downs from UTMIP PLL control bits */
1746 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1747 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ACTIVE_POWERDOWN;
1748 value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERUP;
1749 value &= ~UTMIP_PLL_CFG1_FORCE_PLLU_POWERDOWN;
1750 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1751
1752 /* Setup HW control of UTMIPLL */
1753 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1754 value |= UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET;
1755 value &= ~UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL;
1756 value |= UTMIPLL_HW_PWRDN_CFG0_SEQ_START_STATE;
1757 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1758
1759 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
1760 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
1761 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
1762 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
1763
1764 udelay(1);
1765
1766 /*
1767 * Setup SW override of UTMIPLL assuming USB2.0 ports are assigned
1768 * to USB2
1769 */
1770 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1771 value |= UTMIPLL_HW_PWRDN_CFG0_IDDQ_SWCTL;
1772 value &= ~UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE;
1773 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1774
1775 udelay(1);
1776
1777 /* Enable HW control of UTMIPLL */
1778 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1779 value |= UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE;
1780 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
1781
1782out:
1783 if (pll->lock)
1784 spin_unlock_irqrestore(pll->lock, flags);
1785
1786 return ret;
1787}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001788#endif
1789
Peter De Schrijverdba40722013-04-03 17:40:36 +03001790static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001791 void __iomem *pmc, struct tegra_clk_pll_params *pll_params,
1792 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301793{
1794 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301795
1796 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1797 if (!pll)
1798 return ERR_PTR(-ENOMEM);
1799
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301800 pll->clk_base = clk_base;
1801 pll->pmc = pmc;
1802
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301803 pll->params = pll_params;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301804 pll->lock = lock;
1805
Peter De Schrijveraa6fefd2013-06-05 16:51:25 +03001806 if (!pll_params->div_nmp)
1807 pll_params->div_nmp = &default_nmp;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301808
Peter De Schrijverdba40722013-04-03 17:40:36 +03001809 return pll;
1810}
1811
1812static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1813 const char *name, const char *parent_name, unsigned long flags,
1814 const struct clk_ops *ops)
1815{
1816 struct clk_init_data init;
1817
1818 init.name = name;
1819 init.ops = ops;
1820 init.flags = flags;
1821 init.parent_names = (parent_name ? &parent_name : NULL);
1822 init.num_parents = (parent_name ? 1 : 0);
1823
Rhyland Klein407254d2015-06-18 17:28:25 -04001824 /* Default to _calc_rate if unspecified */
Rhyland Klein86c679a2015-06-18 17:28:34 -04001825 if (!pll->params->calc_rate) {
1826 if (pll->params->flags & TEGRA_PLLM)
1827 pll->params->calc_rate = _calc_dynamic_ramp_rate;
1828 else
1829 pll->params->calc_rate = _calc_rate;
1830 }
Rhyland Klein407254d2015-06-18 17:28:25 -04001831
Bill Huangb9851142015-06-18 17:28:31 -04001832 if (pll->params->set_defaults)
1833 pll->params->set_defaults(pll);
1834
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301835 /* Data in .init is copied by clk_register(), so stack variable OK */
1836 pll->hw.init = &init;
1837
Peter De Schrijverdba40722013-04-03 17:40:36 +03001838 return clk_register(NULL, &pll->hw);
1839}
1840
1841struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1842 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001843 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1844 spinlock_t *lock)
Peter De Schrijverdba40722013-04-03 17:40:36 +03001845{
1846 struct tegra_clk_pll *pll;
1847 struct clk *clk;
1848
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001849 pll_params->flags |= TEGRA_PLL_BYPASS;
Rhyland Klein3706b432015-06-18 17:28:23 -04001850
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001851 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001852 if (IS_ERR(pll))
1853 return ERR_CAST(pll);
1854
1855 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1856 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301857 if (IS_ERR(clk))
1858 kfree(pll);
1859
1860 return clk;
1861}
1862
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001863static struct div_nmp pll_e_nmp = {
1864 .divn_shift = PLLE_BASE_DIVN_SHIFT,
1865 .divn_width = PLLE_BASE_DIVN_WIDTH,
1866 .divm_shift = PLLE_BASE_DIVM_SHIFT,
1867 .divm_width = PLLE_BASE_DIVM_WIDTH,
1868 .divp_shift = PLLE_BASE_DIVP_SHIFT,
1869 .divp_width = PLLE_BASE_DIVP_WIDTH,
1870};
1871
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301872struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1873 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001874 unsigned long flags, struct tegra_clk_pll_params *pll_params,
1875 spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301876{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001877 struct tegra_clk_pll *pll;
1878 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001879
Rhyland Klein3706b432015-06-18 17:28:23 -04001880 pll_params->flags |= TEGRA_PLL_BYPASS;
Thierry Redingd0f02ce2014-04-04 15:55:13 +02001881
1882 if (!pll_params->div_nmp)
1883 pll_params->div_nmp = &pll_e_nmp;
1884
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001885 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverdba40722013-04-03 17:40:36 +03001886 if (IS_ERR(pll))
1887 return ERR_CAST(pll);
1888
1889 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1890 &tegra_clk_plle_ops);
1891 if (IS_ERR(clk))
1892 kfree(pll);
1893
1894 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301895}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001896
Andrew Bresticker15d68e82016-05-26 12:41:31 -04001897struct clk *tegra_clk_register_pllu(const char *name, const char *parent_name,
1898 void __iomem *clk_base, unsigned long flags,
1899 struct tegra_clk_pll_params *pll_params, spinlock_t *lock)
1900{
1901 struct tegra_clk_pll *pll;
1902 struct clk *clk;
1903
1904 pll_params->flags |= TEGRA_PLLU;
1905
1906 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
1907 if (IS_ERR(pll))
1908 return ERR_CAST(pll);
1909
1910 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1911 &tegra_clk_pllu_ops);
1912 if (IS_ERR(clk))
1913 kfree(pll);
1914
1915 return clk;
1916}
1917
Paul Walmsley08acae32014-12-16 12:38:29 -08001918#if defined(CONFIG_ARCH_TEGRA_114_SOC) || \
1919 defined(CONFIG_ARCH_TEGRA_124_SOC) || \
Rhyland Kleindd322f02015-06-18 17:28:28 -04001920 defined(CONFIG_ARCH_TEGRA_132_SOC) || \
1921 defined(CONFIG_ARCH_TEGRA_210_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05301922static const struct clk_ops tegra_clk_pllxc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001923 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001924 .enable = clk_pll_enable,
1925 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001926 .recalc_rate = clk_pll_recalc_rate,
1927 .round_rate = clk_pll_ramp_round_rate,
1928 .set_rate = clk_pllxc_set_rate,
1929};
1930
Sachin Kamate47e12f2013-10-08 16:47:41 +05301931static const struct clk_ops tegra_clk_pllc_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001932 .is_enabled = clk_pll_is_enabled,
1933 .enable = clk_pllc_enable,
1934 .disable = clk_pllc_disable,
1935 .recalc_rate = clk_pll_recalc_rate,
1936 .round_rate = clk_pll_ramp_round_rate,
1937 .set_rate = clk_pllc_set_rate,
1938};
1939
Sachin Kamate47e12f2013-10-08 16:47:41 +05301940static const struct clk_ops tegra_clk_pllre_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001941 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04001942 .enable = clk_pll_enable,
1943 .disable = clk_pll_disable,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001944 .recalc_rate = clk_pllre_recalc_rate,
1945 .round_rate = clk_pllre_round_rate,
1946 .set_rate = clk_pllre_set_rate,
1947};
1948
Sachin Kamate47e12f2013-10-08 16:47:41 +05301949static const struct clk_ops tegra_clk_plle_tegra114_ops = {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001950 .is_enabled = clk_pll_is_enabled,
1951 .enable = clk_plle_tegra114_enable,
1952 .disable = clk_plle_tegra114_disable,
1953 .recalc_rate = clk_pll_recalc_rate,
1954};
1955
Andrew Bresticker15d68e82016-05-26 12:41:31 -04001956static const struct clk_ops tegra_clk_pllu_tegra114_ops = {
1957 .is_enabled = clk_pll_is_enabled,
1958 .enable = clk_pllu_tegra114_enable,
1959 .disable = clk_pll_disable,
1960 .recalc_rate = clk_pll_recalc_rate,
1961};
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001962
1963struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1964 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03001965 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001966 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001967 spinlock_t *lock)
1968{
1969 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001970 struct clk *clk, *parent;
1971 unsigned long parent_rate;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001972 u32 val, val_iddq;
1973
1974 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01001975 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03001976 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02001977 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001978 return ERR_PTR(-EINVAL);
1979 }
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001980
1981 if (!pll_params->pdiv_tohw)
1982 return ERR_PTR(-EINVAL);
1983
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07001984 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03001985
1986 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
1987
Bill Huangb5512b42015-06-18 17:28:30 -04001988 if (pll_params->adjust_vco)
1989 pll_params->vco_min = pll_params->adjust_vco(pll_params,
1990 parent_rate);
1991
Bill Huangb9851142015-06-18 17:28:31 -04001992 /*
1993 * If the pll has a set_defaults callback, it will take care of
1994 * configuring dynamic ramping and setting IDDQ in that path.
1995 */
1996 if (!pll_params->set_defaults) {
1997 int err;
Peter De Schrijver04edb092013-09-06 14:37:37 +03001998
Bill Huangb9851142015-06-18 17:28:31 -04001999 err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate);
2000 if (err)
2001 return ERR_PTR(err);
Peter De Schrijver04edb092013-09-06 14:37:37 +03002002
Bill Huangb9851142015-06-18 17:28:31 -04002003 val = readl_relaxed(clk_base + pll_params->base_reg);
2004 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
2005
2006 if (val & PLL_BASE_ENABLE)
2007 WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx));
2008 else {
2009 val_iddq |= BIT(pll_params->iddq_bit_idx);
2010 writel_relaxed(val_iddq,
2011 clk_base + pll_params->iddq_reg);
2012 }
Peter De Schrijver04edb092013-09-06 14:37:37 +03002013 }
2014
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002015 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002016 if (IS_ERR(pll))
2017 return ERR_CAST(pll);
2018
2019 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2020 &tegra_clk_pllxc_ops);
2021 if (IS_ERR(clk))
2022 kfree(pll);
2023
2024 return clk;
2025}
2026
2027struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
2028 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002029 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002030 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002031 spinlock_t *lock, unsigned long parent_rate)
2032{
2033 u32 val;
2034 struct tegra_clk_pll *pll;
2035 struct clk *clk;
2036
Peter De Schrijver04edb092013-09-06 14:37:37 +03002037 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2038
Bill Huangb5512b42015-06-18 17:28:30 -04002039 if (pll_params->adjust_vco)
2040 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2041 parent_rate);
2042
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002043 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002044 if (IS_ERR(pll))
2045 return ERR_CAST(pll);
2046
2047 /* program minimum rate by default */
2048
2049 val = pll_readl_base(pll);
2050 if (val & PLL_BASE_ENABLE)
Bill Huanga4ca2b22015-06-18 17:28:38 -04002051 WARN_ON(readl_relaxed(clk_base + pll_params->iddq_reg) &
2052 BIT(pll_params->iddq_bit_idx));
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002053 else {
2054 int m;
2055
2056 m = _pll_fixed_mdiv(pll_params, parent_rate);
Thierry Redingc61e4e72014-04-04 15:55:14 +02002057 val = m << divm_shift(pll);
2058 val |= (pll_params->vco_min / parent_rate) << divn_shift(pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002059 pll_writel_base(val, pll);
2060 }
2061
2062 /* disable lock override */
2063
2064 val = pll_readl_misc(pll);
2065 val &= ~BIT(29);
2066 pll_writel_misc(val, pll);
2067
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002068 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2069 &tegra_clk_pllre_ops);
2070 if (IS_ERR(clk))
2071 kfree(pll);
2072
2073 return clk;
2074}
2075
2076struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
2077 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002078 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002079 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002080 spinlock_t *lock)
2081{
2082 struct tegra_clk_pll *pll;
Peter De Schrijver04edb092013-09-06 14:37:37 +03002083 struct clk *clk, *parent;
2084 unsigned long parent_rate;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002085
2086 if (!pll_params->pdiv_tohw)
2087 return ERR_PTR(-EINVAL);
2088
Peter De Schrijver04edb092013-09-06 14:37:37 +03002089 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01002090 if (!parent) {
Peter De Schrijver04edb092013-09-06 14:37:37 +03002091 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02002092 parent_name, name);
Peter De Schrijver04edb092013-09-06 14:37:37 +03002093 return ERR_PTR(-EINVAL);
2094 }
2095
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07002096 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03002097
2098 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2099
Bill Huangb5512b42015-06-18 17:28:30 -04002100 if (pll_params->adjust_vco)
2101 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2102 parent_rate);
2103
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002104 pll_params->flags |= TEGRA_PLL_BYPASS;
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002105 pll_params->flags |= TEGRA_PLLM;
2106 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002107 if (IS_ERR(pll))
2108 return ERR_CAST(pll);
2109
2110 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
Danny Huang267b62a2015-06-18 17:28:27 -04002111 &tegra_clk_pll_ops);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002112 if (IS_ERR(clk))
2113 kfree(pll);
2114
2115 return clk;
2116}
2117
2118struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
2119 void __iomem *clk_base, void __iomem *pmc,
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002120 unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002121 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002122 spinlock_t *lock)
2123{
2124 struct clk *parent, *clk;
Thierry Reding385f9ad2015-11-19 16:34:06 +01002125 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002126 struct tegra_clk_pll *pll;
2127 struct tegra_clk_pll_freq_table cfg;
2128 unsigned long parent_rate;
2129
2130 if (!p_tohw)
2131 return ERR_PTR(-EINVAL);
2132
2133 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01002134 if (!parent) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002135 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02002136 parent_name, name);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002137 return ERR_PTR(-EINVAL);
2138 }
2139
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07002140 parent_rate = clk_get_rate(parent);
Peter De Schrijver04edb092013-09-06 14:37:37 +03002141
2142 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2143
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002144 pll_params->flags |= TEGRA_PLL_BYPASS;
2145 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002146 if (IS_ERR(pll))
2147 return ERR_CAST(pll);
2148
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002149 /*
2150 * Most of PLLC register fields are shadowed, and can not be read
2151 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
2152 * Initialize PLL to default state: disabled, reset; shadow registers
2153 * loaded with default parameters; dividers are preset for half of
2154 * minimum VCO rate (the latter assured that shadowed divider settings
2155 * are within supported range).
2156 */
2157
2158 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2159 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2160
2161 while (p_tohw->pdiv) {
2162 if (p_tohw->pdiv == 2) {
2163 cfg.p = p_tohw->hw_val;
2164 break;
2165 }
2166 p_tohw++;
2167 }
2168
2169 if (!p_tohw->pdiv) {
2170 WARN_ON(1);
2171 return ERR_PTR(-EINVAL);
2172 }
2173
2174 pll_writel_base(0, pll);
2175 _update_pll_mnp(pll, &cfg);
2176
2177 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
2178 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
2179 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
2180 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
2181
2182 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
2183
2184 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2185 &tegra_clk_pllc_ops);
2186 if (IS_ERR(clk))
2187 kfree(pll);
2188
2189 return clk;
2190}
2191
2192struct clk *tegra_clk_register_plle_tegra114(const char *name,
2193 const char *parent_name,
2194 void __iomem *clk_base, unsigned long flags,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002195 struct tegra_clk_pll_params *pll_params,
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002196 spinlock_t *lock)
2197{
2198 struct tegra_clk_pll *pll;
2199 struct clk *clk;
2200 u32 val, val_aux;
2201
Peter De Schrijverebe142b2013-10-04 17:28:34 +03002202 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002203 if (IS_ERR(pll))
2204 return ERR_CAST(pll);
2205
2206 /* ensure parent is set to pll_re_vco */
2207
2208 val = pll_readl_base(pll);
2209 val_aux = pll_readl(pll_params->aux_reg, pll);
2210
2211 if (val & PLL_BASE_ENABLE) {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02002212 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
2213 (val_aux & PLLE_AUX_PLLP_SEL))
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002214 WARN(1, "pll_e enabled with unsupported parent %s\n",
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02002215 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
2216 "pll_re_vco");
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002217 } else {
Peter De Schrijver8e9cc802013-11-25 14:44:13 +02002218 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
Tuomas Tynkkynend2c834a2014-05-16 16:50:20 +03002219 pll_writel(val_aux, pll_params->aux_reg, pll);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002220 }
2221
2222 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2223 &tegra_clk_plle_tegra114_ops);
2224 if (IS_ERR(clk))
2225 kfree(pll);
2226
2227 return clk;
2228}
Andrew Bresticker15d68e82016-05-26 12:41:31 -04002229
2230struct clk *
2231tegra_clk_register_pllu_tegra114(const char *name, const char *parent_name,
2232 void __iomem *clk_base, unsigned long flags,
2233 struct tegra_clk_pll_params *pll_params,
2234 spinlock_t *lock)
2235{
2236 struct tegra_clk_pll *pll;
2237 struct clk *clk;
2238
2239 pll_params->flags |= TEGRA_PLLU;
2240
2241 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2242 if (IS_ERR(pll))
2243 return ERR_CAST(pll);
2244
2245 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2246 &tegra_clk_pllu_tegra114_ops);
2247 if (IS_ERR(clk))
2248 kfree(pll);
2249
2250 return clk;
2251}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03002252#endif
Peter De Schrijver798e9102013-09-09 13:22:55 +03002253
Paul Walmsley08acae32014-12-16 12:38:29 -08002254#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
Sachin Kamate47e12f2013-10-08 16:47:41 +05302255static const struct clk_ops tegra_clk_pllss_ops = {
Peter De Schrijver798e9102013-09-09 13:22:55 +03002256 .is_enabled = clk_pll_is_enabled,
Rhyland Klein7db864c2015-06-18 17:28:20 -04002257 .enable = clk_pll_enable,
2258 .disable = clk_pll_disable,
Peter De Schrijver798e9102013-09-09 13:22:55 +03002259 .recalc_rate = clk_pll_recalc_rate,
2260 .round_rate = clk_pll_ramp_round_rate,
2261 .set_rate = clk_pllxc_set_rate,
2262};
2263
2264struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name,
2265 void __iomem *clk_base, unsigned long flags,
2266 struct tegra_clk_pll_params *pll_params,
2267 spinlock_t *lock)
2268{
2269 struct tegra_clk_pll *pll;
2270 struct clk *clk, *parent;
2271 struct tegra_clk_pll_freq_table cfg;
2272 unsigned long parent_rate;
Bill Huang2d7f61f2015-06-18 17:28:39 -04002273 u32 val, val_iddq;
Peter De Schrijver798e9102013-09-09 13:22:55 +03002274 int i;
2275
2276 if (!pll_params->div_nmp)
2277 return ERR_PTR(-EINVAL);
2278
2279 parent = __clk_lookup(parent_name);
Wei Yongjun62ce7cd2013-10-29 03:07:57 +01002280 if (!parent) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03002281 WARN(1, "parent clk %s of %s must be registered first\n",
Tomeu Vizosoca036b22014-09-30 09:22:00 +02002282 parent_name, name);
Peter De Schrijver798e9102013-09-09 13:22:55 +03002283 return ERR_PTR(-EINVAL);
2284 }
2285
Peter De Schrijver798e9102013-09-09 13:22:55 +03002286 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2287 if (IS_ERR(pll))
2288 return ERR_CAST(pll);
2289
2290 val = pll_readl_base(pll);
2291 val &= ~PLLSS_REF_SRC_SEL_MASK;
2292 pll_writel_base(val, pll);
2293
Stephen Boyd5cdb1dc2015-07-30 17:20:57 -07002294 parent_rate = clk_get_rate(parent);
Peter De Schrijver798e9102013-09-09 13:22:55 +03002295
2296 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2297
2298 /* initialize PLL to minimum rate */
2299
2300 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2301 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2302
2303 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
2304 ;
2305 if (!i) {
2306 kfree(pll);
2307 return ERR_PTR(-EINVAL);
2308 }
2309
2310 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
2311
2312 _update_pll_mnp(pll, &cfg);
2313
2314 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
2315 pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll);
2316 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll);
2317 pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll);
2318
2319 val = pll_readl_base(pll);
Bill Huang2d7f61f2015-06-18 17:28:39 -04002320 val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg);
Peter De Schrijver798e9102013-09-09 13:22:55 +03002321 if (val & PLL_BASE_ENABLE) {
Bill Huang2d7f61f2015-06-18 17:28:39 -04002322 if (val_iddq & BIT(pll_params->iddq_bit_idx)) {
Peter De Schrijver798e9102013-09-09 13:22:55 +03002323 WARN(1, "%s is on but IDDQ set\n", name);
2324 kfree(pll);
2325 return ERR_PTR(-EINVAL);
2326 }
Bill Huang2d7f61f2015-06-18 17:28:39 -04002327 } else {
2328 val_iddq |= BIT(pll_params->iddq_bit_idx);
2329 writel_relaxed(val_iddq, clk_base + pll_params->iddq_reg);
2330 }
Peter De Schrijver798e9102013-09-09 13:22:55 +03002331
2332 val &= ~PLLSS_LOCK_OVERRIDE;
2333 pll_writel_base(val, pll);
2334
2335 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2336 &tegra_clk_pllss_ops);
2337
2338 if (IS_ERR(clk))
2339 kfree(pll);
2340
2341 return clk;
2342}
2343#endif
Rhyland Kleindd322f02015-06-18 17:28:28 -04002344
2345#if defined(CONFIG_ARCH_TEGRA_210_SOC)
Rhyland Klein926655f2016-03-21 15:58:52 -04002346struct clk *tegra_clk_register_pllre_tegra210(const char *name,
2347 const char *parent_name, void __iomem *clk_base,
2348 void __iomem *pmc, unsigned long flags,
2349 struct tegra_clk_pll_params *pll_params,
2350 spinlock_t *lock, unsigned long parent_rate)
2351{
2352 u32 val;
2353 struct tegra_clk_pll *pll;
2354 struct clk *clk;
2355
2356 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2357
2358 if (pll_params->adjust_vco)
2359 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2360 parent_rate);
2361
2362 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2363 if (IS_ERR(pll))
2364 return ERR_CAST(pll);
2365
2366 /* program minimum rate by default */
2367
2368 val = pll_readl_base(pll);
2369 if (val & PLL_BASE_ENABLE)
2370 WARN_ON(readl_relaxed(clk_base + pll_params->iddq_reg) &
2371 BIT(pll_params->iddq_bit_idx));
2372 else {
2373 val = 0x4 << divm_shift(pll);
2374 val |= 0x41 << divn_shift(pll);
2375 pll_writel_base(val, pll);
2376 }
2377
2378 /* disable lock override */
2379
2380 val = pll_readl_misc(pll);
2381 val &= ~BIT(29);
2382 pll_writel_misc(val, pll);
2383
2384 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2385 &tegra_clk_pllre_ops);
2386 if (IS_ERR(clk))
2387 kfree(pll);
2388
2389 return clk;
2390}
2391
Rhyland Kleindd322f02015-06-18 17:28:28 -04002392static int clk_plle_tegra210_enable(struct clk_hw *hw)
2393{
2394 struct tegra_clk_pll *pll = to_clk_pll(hw);
2395 struct tegra_clk_pll_freq_table sel;
2396 u32 val;
Mark Kuof59b0162016-01-14 14:24:36 -05002397 int ret = 0;
Rhyland Kleindd322f02015-06-18 17:28:28 -04002398 unsigned long flags = 0;
Andrew Bresticker3eb61562016-01-14 14:24:34 -05002399 unsigned long input_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
Rhyland Kleindd322f02015-06-18 17:28:28 -04002400
2401 if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate))
2402 return -EINVAL;
2403
2404 if (pll->lock)
2405 spin_lock_irqsave(pll->lock, flags);
2406
Mark Kuof59b0162016-01-14 14:24:36 -05002407 val = pll_readl(pll->params->aux_reg, pll);
2408 if (val & PLLE_AUX_SEQ_ENABLE)
2409 goto out;
2410
Rhyland Kleindd322f02015-06-18 17:28:28 -04002411 val = pll_readl_base(pll);
2412 val &= ~BIT(30); /* Disable lock override */
2413 pll_writel_base(val, pll);
2414
Rhyland Kleindd322f02015-06-18 17:28:28 -04002415 val = pll_readl_misc(pll);
2416 val |= PLLE_MISC_LOCK_ENABLE;
2417 val |= PLLE_MISC_IDDQ_SW_CTRL;
2418 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
2419 val |= PLLE_MISC_PLLE_PTS;
Rhyland Kleinfd2963b2016-01-14 14:24:37 -05002420 val &= ~(PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK);
Rhyland Kleindd322f02015-06-18 17:28:28 -04002421 pll_writel_misc(val, pll);
2422 udelay(5);
2423
2424 val = pll_readl(PLLE_SS_CTRL, pll);
2425 val |= PLLE_SS_DISABLE;
2426 pll_writel(val, PLLE_SS_CTRL, pll);
2427
2428 val = pll_readl_base(pll);
2429 val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) |
2430 divm_mask_shifted(pll));
2431 val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT);
2432 val |= sel.m << divm_shift(pll);
2433 val |= sel.n << divn_shift(pll);
2434 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
2435 pll_writel_base(val, pll);
2436 udelay(1);
2437
2438 val = pll_readl_base(pll);
2439 val |= PLLE_BASE_ENABLE;
2440 pll_writel_base(val, pll);
2441
2442 ret = clk_pll_wait_for_lock(pll);
2443
2444 if (ret < 0)
2445 goto out;
2446
2447 val = pll_readl(PLLE_SS_CTRL, pll);
2448 val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT);
2449 val &= ~PLLE_SS_COEFFICIENTS_MASK;
Mark Kuo442f53f2016-01-14 14:26:42 -05002450 val |= PLLE_SS_COEFFICIENTS_VAL_TEGRA210;
Rhyland Kleindd322f02015-06-18 17:28:28 -04002451 pll_writel(val, PLLE_SS_CTRL, pll);
2452 val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS);
2453 pll_writel(val, PLLE_SS_CTRL, pll);
2454 udelay(1);
2455 val &= ~PLLE_SS_CNTL_INTERP_RESET;
2456 pll_writel(val, PLLE_SS_CTRL, pll);
2457 udelay(1);
2458
2459 val = pll_readl_misc(pll);
2460 val &= ~PLLE_MISC_IDDQ_SW_CTRL;
2461 pll_writel_misc(val, pll);
2462
2463 val = pll_readl(pll->params->aux_reg, pll);
2464 val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SS_SEQ_INCLUDE);
2465 val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL);
2466 pll_writel(val, pll->params->aux_reg, pll);
2467 udelay(1);
2468 val |= PLLE_AUX_SEQ_ENABLE;
2469 pll_writel(val, pll->params->aux_reg, pll);
2470
2471out:
2472 if (pll->lock)
2473 spin_unlock_irqrestore(pll->lock, flags);
2474
2475 return ret;
2476}
2477
2478static void clk_plle_tegra210_disable(struct clk_hw *hw)
2479{
2480 struct tegra_clk_pll *pll = to_clk_pll(hw);
2481 unsigned long flags = 0;
2482 u32 val;
2483
2484 if (pll->lock)
2485 spin_lock_irqsave(pll->lock, flags);
2486
Mark Kuof59b0162016-01-14 14:24:36 -05002487 /* If PLLE HW sequencer is enabled, SW should not disable PLLE */
2488 val = pll_readl(pll->params->aux_reg, pll);
2489 if (val & PLLE_AUX_SEQ_ENABLE)
2490 goto out;
2491
Rhyland Kleindd322f02015-06-18 17:28:28 -04002492 val = pll_readl_base(pll);
2493 val &= ~PLLE_BASE_ENABLE;
2494 pll_writel_base(val, pll);
2495
Mark Kuof59b0162016-01-14 14:24:36 -05002496 val = pll_readl(pll->params->aux_reg, pll);
2497 val |= PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL;
2498 pll_writel(val, pll->params->aux_reg, pll);
2499
Rhyland Kleindd322f02015-06-18 17:28:28 -04002500 val = pll_readl_misc(pll);
2501 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
2502 pll_writel_misc(val, pll);
2503 udelay(1);
2504
Mark Kuof59b0162016-01-14 14:24:36 -05002505out:
Rhyland Kleindd322f02015-06-18 17:28:28 -04002506 if (pll->lock)
2507 spin_unlock_irqrestore(pll->lock, flags);
2508}
2509
2510static int clk_plle_tegra210_is_enabled(struct clk_hw *hw)
2511{
2512 struct tegra_clk_pll *pll = to_clk_pll(hw);
2513 u32 val;
2514
2515 val = pll_readl_base(pll);
2516
2517 return val & PLLE_BASE_ENABLE ? 1 : 0;
2518}
2519
Andrew Bresticker15d68e82016-05-26 12:41:31 -04002520static int clk_pllu_tegra210_enable(struct clk_hw *hw)
2521{
2522 struct tegra_clk_pll *pll = to_clk_pll(hw);
2523 struct clk_hw *pll_ref = clk_hw_get_parent(hw);
2524 struct clk_hw *osc = clk_hw_get_parent(pll_ref);
2525 const struct utmi_clk_param *params = NULL;
2526 unsigned long flags = 0, input_rate;
2527 unsigned int i;
2528 int ret = 0;
2529 u32 value;
2530
2531 if (!osc) {
2532 pr_err("%s: failed to get OSC clock\n", __func__);
2533 return -EINVAL;
2534 }
2535
2536 input_rate = clk_hw_get_rate(osc);
2537
2538 if (pll->lock)
2539 spin_lock_irqsave(pll->lock, flags);
2540
2541 _clk_pll_enable(hw);
2542
2543 ret = clk_pll_wait_for_lock(pll);
2544 if (ret < 0)
2545 goto out;
2546
2547 for (i = 0; i < ARRAY_SIZE(utmi_parameters); i++) {
2548 if (input_rate == utmi_parameters[i].osc_frequency) {
2549 params = &utmi_parameters[i];
2550 break;
2551 }
2552 }
2553
2554 if (!params) {
2555 pr_err("%s: unexpected input rate %lu Hz\n", __func__,
2556 input_rate);
2557 ret = -EINVAL;
2558 goto out;
2559 }
2560
2561 value = pll_readl_base(pll);
2562 value &= ~PLLU_BASE_OVERRIDE;
2563 pll_writel_base(value, pll);
2564
2565 /* Put PLLU under HW control */
2566 value = readl_relaxed(pll->clk_base + PLLU_HW_PWRDN_CFG0);
2567 value |= PLLU_HW_PWRDN_CFG0_IDDQ_PD_INCLUDE |
2568 PLLU_HW_PWRDN_CFG0_USE_SWITCH_DETECT |
2569 PLLU_HW_PWRDN_CFG0_USE_LOCKDET;
2570 value &= ~(PLLU_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL |
2571 PLLU_HW_PWRDN_CFG0_CLK_SWITCH_SWCTL);
2572 writel_relaxed(value, pll->clk_base + PLLU_HW_PWRDN_CFG0);
2573
2574 value = readl_relaxed(pll->clk_base + XUSB_PLL_CFG0);
2575 value &= ~XUSB_PLL_CFG0_PLLU_LOCK_DLY;
2576 writel_relaxed(value, pll->clk_base + XUSB_PLL_CFG0);
2577
2578 udelay(1);
2579
2580 value = readl_relaxed(pll->clk_base + PLLU_HW_PWRDN_CFG0);
2581 value |= PLLU_HW_PWRDN_CFG0_SEQ_ENABLE;
2582 writel_relaxed(value, pll->clk_base + PLLU_HW_PWRDN_CFG0);
2583
2584 udelay(1);
2585
2586 /* Disable PLLU clock branch to UTMIPLL since it uses OSC */
2587 value = pll_readl_base(pll);
2588 value &= ~PLLU_BASE_CLKENABLE_USB;
2589 pll_writel_base(value, pll);
2590
2591 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2592 if (value & UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE) {
2593 pr_debug("UTMIPLL already enabled\n");
2594 goto out;
2595 }
2596
2597 value &= ~UTMIPLL_HW_PWRDN_CFG0_IDDQ_OVERRIDE;
2598 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2599
2600 /* Program UTMIP PLL stable and active counts */
2601 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
2602 value &= ~UTMIP_PLL_CFG2_STABLE_COUNT(~0);
2603 value |= UTMIP_PLL_CFG2_STABLE_COUNT(params->stable_count);
2604 value &= ~UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(~0);
2605 value |= UTMIP_PLL_CFG2_ACTIVE_DLY_COUNT(params->active_delay_count);
2606 value |= UTMIP_PLL_CFG2_PHY_XTAL_CLOCKEN;
2607 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
2608
2609 /* Program UTMIP PLL delay and oscillator frequency counts */
2610 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
2611 value &= ~UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(~0);
2612 value |= UTMIP_PLL_CFG1_ENABLE_DLY_COUNT(params->enable_delay_count);
2613 value &= ~UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(~0);
2614 value |= UTMIP_PLL_CFG1_XTAL_FREQ_COUNT(params->xtal_freq_count);
2615 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
2616
2617 /* Remove power downs from UTMIP PLL control bits */
2618 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
2619 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2620 value |= UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
2621 writel(value, pll->clk_base + UTMIP_PLL_CFG1);
2622
2623 udelay(1);
2624
2625 /* Enable samplers for SNPS, XUSB_HOST, XUSB_DEV */
2626 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG2);
2627 value |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERUP;
2628 value |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERUP;
2629 value |= UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERUP;
2630 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_A_POWERDOWN;
2631 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_B_POWERDOWN;
2632 value &= ~UTMIP_PLL_CFG2_FORCE_PD_SAMP_D_POWERDOWN;
2633 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG2);
2634
2635 /* Setup HW control of UTMIPLL */
2636 value = readl_relaxed(pll->clk_base + UTMIP_PLL_CFG1);
2637 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERUP;
2638 value &= ~UTMIP_PLL_CFG1_FORCE_PLL_ENABLE_POWERDOWN;
2639 writel_relaxed(value, pll->clk_base + UTMIP_PLL_CFG1);
2640
2641 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2642 value |= UTMIPLL_HW_PWRDN_CFG0_USE_LOCKDET;
2643 value &= ~UTMIPLL_HW_PWRDN_CFG0_CLK_ENABLE_SWCTL;
2644 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2645
2646 udelay(1);
2647
2648 value = readl_relaxed(pll->clk_base + XUSB_PLL_CFG0);
2649 value &= ~XUSB_PLL_CFG0_UTMIPLL_LOCK_DLY;
2650 writel_relaxed(value, pll->clk_base + XUSB_PLL_CFG0);
2651
2652 udelay(1);
2653
2654 /* Enable HW control of UTMIPLL */
2655 value = readl_relaxed(pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2656 value |= UTMIPLL_HW_PWRDN_CFG0_SEQ_ENABLE;
2657 writel_relaxed(value, pll->clk_base + UTMIPLL_HW_PWRDN_CFG0);
2658
2659out:
2660 if (pll->lock)
2661 spin_unlock_irqrestore(pll->lock, flags);
2662
2663 return ret;
2664}
2665
Rhyland Kleindd322f02015-06-18 17:28:28 -04002666static const struct clk_ops tegra_clk_plle_tegra210_ops = {
2667 .is_enabled = clk_plle_tegra210_is_enabled,
2668 .enable = clk_plle_tegra210_enable,
2669 .disable = clk_plle_tegra210_disable,
2670 .recalc_rate = clk_pll_recalc_rate,
2671};
2672
Andrew Bresticker15d68e82016-05-26 12:41:31 -04002673static const struct clk_ops tegra_clk_pllu_tegra210_ops = {
2674 .is_enabled = clk_pll_is_enabled,
2675 .enable = clk_pllu_tegra210_enable,
2676 .disable = clk_pll_disable,
2677 .recalc_rate = clk_pllre_recalc_rate,
2678};
2679
Rhyland Kleindd322f02015-06-18 17:28:28 -04002680struct clk *tegra_clk_register_plle_tegra210(const char *name,
2681 const char *parent_name,
2682 void __iomem *clk_base, unsigned long flags,
2683 struct tegra_clk_pll_params *pll_params,
2684 spinlock_t *lock)
2685{
2686 struct tegra_clk_pll *pll;
2687 struct clk *clk;
2688 u32 val, val_aux;
2689
2690 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2691 if (IS_ERR(pll))
2692 return ERR_CAST(pll);
2693
2694 /* ensure parent is set to pll_re_vco */
2695
2696 val = pll_readl_base(pll);
2697 val_aux = pll_readl(pll_params->aux_reg, pll);
2698
2699 if (val & PLLE_BASE_ENABLE) {
2700 if ((val_aux & PLLE_AUX_PLLRE_SEL) ||
2701 (val_aux & PLLE_AUX_PLLP_SEL))
2702 WARN(1, "pll_e enabled with unsupported parent %s\n",
2703 (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" :
2704 "pll_re_vco");
2705 } else {
2706 val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL);
2707 pll_writel(val_aux, pll_params->aux_reg, pll);
2708 }
2709
2710 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2711 &tegra_clk_plle_tegra210_ops);
2712 if (IS_ERR(clk))
2713 kfree(pll);
2714
2715 return clk;
2716}
2717
2718struct clk *tegra_clk_register_pllc_tegra210(const char *name,
2719 const char *parent_name, void __iomem *clk_base,
2720 void __iomem *pmc, unsigned long flags,
2721 struct tegra_clk_pll_params *pll_params,
2722 spinlock_t *lock)
2723{
2724 struct clk *parent, *clk;
2725 const struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
2726 struct tegra_clk_pll *pll;
2727 unsigned long parent_rate;
2728
2729 if (!p_tohw)
2730 return ERR_PTR(-EINVAL);
2731
2732 parent = __clk_lookup(parent_name);
2733 if (!parent) {
2734 WARN(1, "parent clk %s of %s must be registered first\n",
2735 name, parent_name);
2736 return ERR_PTR(-EINVAL);
2737 }
2738
2739 parent_rate = clk_get_rate(parent);
2740
2741 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2742
Bill Huangb5512b42015-06-18 17:28:30 -04002743 if (pll_params->adjust_vco)
2744 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2745 parent_rate);
2746
Rhyland Kleindd322f02015-06-18 17:28:28 -04002747 pll_params->flags |= TEGRA_PLL_BYPASS;
2748 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2749 if (IS_ERR(pll))
2750 return ERR_CAST(pll);
2751
2752 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2753 &tegra_clk_pll_ops);
2754 if (IS_ERR(clk))
2755 kfree(pll);
2756
2757 return clk;
2758}
2759
2760struct clk *tegra_clk_register_pllxc_tegra210(const char *name,
2761 const char *parent_name, void __iomem *clk_base,
2762 void __iomem *pmc, unsigned long flags,
2763 struct tegra_clk_pll_params *pll_params,
2764 spinlock_t *lock)
2765{
2766 struct tegra_clk_pll *pll;
2767 struct clk *clk, *parent;
2768 unsigned long parent_rate;
2769
2770 parent = __clk_lookup(parent_name);
2771 if (!parent) {
2772 WARN(1, "parent clk %s of %s must be registered first\n",
2773 name, parent_name);
2774 return ERR_PTR(-EINVAL);
2775 }
2776
2777 if (!pll_params->pdiv_tohw)
2778 return ERR_PTR(-EINVAL);
2779
2780 parent_rate = clk_get_rate(parent);
2781
2782 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2783
Bill Huangb5512b42015-06-18 17:28:30 -04002784 if (pll_params->adjust_vco)
2785 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2786 parent_rate);
2787
Rhyland Kleindd322f02015-06-18 17:28:28 -04002788 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2789 if (IS_ERR(pll))
2790 return ERR_CAST(pll);
2791
2792 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2793 &tegra_clk_pll_ops);
2794 if (IS_ERR(clk))
2795 kfree(pll);
2796
2797 return clk;
2798}
2799
2800struct clk *tegra_clk_register_pllss_tegra210(const char *name,
2801 const char *parent_name, void __iomem *clk_base,
2802 unsigned long flags,
2803 struct tegra_clk_pll_params *pll_params,
2804 spinlock_t *lock)
2805{
2806 struct tegra_clk_pll *pll;
2807 struct clk *clk, *parent;
2808 struct tegra_clk_pll_freq_table cfg;
2809 unsigned long parent_rate;
2810 u32 val;
2811 int i;
2812
2813 if (!pll_params->div_nmp)
2814 return ERR_PTR(-EINVAL);
2815
2816 parent = __clk_lookup(parent_name);
2817 if (!parent) {
2818 WARN(1, "parent clk %s of %s must be registered first\n",
2819 name, parent_name);
2820 return ERR_PTR(-EINVAL);
2821 }
2822
2823 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2824 if (IS_ERR(pll))
2825 return ERR_CAST(pll);
2826
2827 val = pll_readl_base(pll);
2828 val &= ~PLLSS_REF_SRC_SEL_MASK;
2829 pll_writel_base(val, pll);
2830
2831 parent_rate = clk_get_rate(parent);
2832
2833 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2834
Bill Huangb5512b42015-06-18 17:28:30 -04002835 if (pll_params->adjust_vco)
2836 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2837 parent_rate);
2838
Rhyland Kleindd322f02015-06-18 17:28:28 -04002839 /* initialize PLL to minimum rate */
2840
2841 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
2842 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
2843
2844 for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++)
2845 ;
2846 if (!i) {
2847 kfree(pll);
2848 return ERR_PTR(-EINVAL);
2849 }
2850
2851 cfg.p = pll_params->pdiv_tohw[i-1].hw_val;
2852
2853 _update_pll_mnp(pll, &cfg);
2854
2855 pll_writel_misc(PLLSS_MISC_DEFAULT, pll);
2856
2857 val = pll_readl_base(pll);
2858 if (val & PLL_BASE_ENABLE) {
2859 if (val & BIT(pll_params->iddq_bit_idx)) {
2860 WARN(1, "%s is on but IDDQ set\n", name);
2861 kfree(pll);
2862 return ERR_PTR(-EINVAL);
2863 }
2864 } else
2865 val |= BIT(pll_params->iddq_bit_idx);
2866
2867 val &= ~PLLSS_LOCK_OVERRIDE;
2868 pll_writel_base(val, pll);
2869
2870 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2871 &tegra_clk_pll_ops);
2872
2873 if (IS_ERR(clk))
2874 kfree(pll);
2875
2876 return clk;
2877}
Rhyland Klein69297152015-06-18 17:28:29 -04002878
2879struct clk *tegra_clk_register_pllmb(const char *name, const char *parent_name,
2880 void __iomem *clk_base, void __iomem *pmc,
2881 unsigned long flags,
2882 struct tegra_clk_pll_params *pll_params,
2883 spinlock_t *lock)
2884{
2885 struct tegra_clk_pll *pll;
2886 struct clk *clk, *parent;
2887 unsigned long parent_rate;
2888
2889 if (!pll_params->pdiv_tohw)
2890 return ERR_PTR(-EINVAL);
2891
2892 parent = __clk_lookup(parent_name);
2893 if (!parent) {
2894 WARN(1, "parent clk %s of %s must be registered first\n",
2895 parent_name, name);
2896 return ERR_PTR(-EINVAL);
2897 }
2898
2899 parent_rate = clk_get_rate(parent);
2900
2901 pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate);
2902
Bill Huangb5512b42015-06-18 17:28:30 -04002903 if (pll_params->adjust_vco)
2904 pll_params->vco_min = pll_params->adjust_vco(pll_params,
2905 parent_rate);
2906
Rhyland Klein69297152015-06-18 17:28:29 -04002907 pll_params->flags |= TEGRA_PLL_BYPASS;
2908 pll_params->flags |= TEGRA_PLLMB;
2909 pll = _tegra_init_pll(clk_base, pmc, pll_params, lock);
2910 if (IS_ERR(pll))
2911 return ERR_CAST(pll);
2912
2913 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2914 &tegra_clk_pll_ops);
2915 if (IS_ERR(clk))
2916 kfree(pll);
2917
2918 return clk;
2919}
Andrew Bresticker15d68e82016-05-26 12:41:31 -04002920
2921struct clk *tegra_clk_register_pllu_tegra210(const char *name,
2922 const char *parent_name, void __iomem *clk_base,
2923 unsigned long flags, struct tegra_clk_pll_params *pll_params,
2924 spinlock_t *lock)
2925{
2926 struct tegra_clk_pll *pll;
2927 struct clk *clk;
2928
2929 pll_params->flags |= TEGRA_PLLU;
2930
2931 pll = _tegra_init_pll(clk_base, NULL, pll_params, lock);
2932 if (IS_ERR(pll))
2933 return ERR_CAST(pll);
2934
2935 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
2936 &tegra_clk_pllu_tegra210_ops);
2937 if (IS_ERR(clk))
2938 kfree(pll);
2939
2940 return clk;
2941}
Rhyland Kleindd322f02015-06-18 17:28:28 -04002942#endif