blob: 85bec1dd87d809d342421cb73af51723c2d2bd5a [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>
21#include <linux/clk-provider.h>
22#include <linux/clk.h>
23
24#include "clk.h"
25
26#define PLL_BASE_BYPASS BIT(31)
27#define PLL_BASE_ENABLE BIT(30)
28#define PLL_BASE_REF_ENABLE BIT(29)
29#define PLL_BASE_OVERRIDE BIT(28)
30
31#define PLL_BASE_DIVP_SHIFT 20
32#define PLL_BASE_DIVP_WIDTH 3
33#define PLL_BASE_DIVN_SHIFT 8
34#define PLL_BASE_DIVN_WIDTH 10
35#define PLL_BASE_DIVM_SHIFT 0
36#define PLL_BASE_DIVM_WIDTH 5
37#define PLLU_POST_DIVP_MASK 0x1
38
39#define PLL_MISC_DCCON_SHIFT 20
40#define PLL_MISC_CPCON_SHIFT 8
41#define PLL_MISC_CPCON_WIDTH 4
42#define PLL_MISC_CPCON_MASK ((1 << PLL_MISC_CPCON_WIDTH) - 1)
43#define PLL_MISC_LFCON_SHIFT 4
44#define PLL_MISC_LFCON_WIDTH 4
45#define PLL_MISC_LFCON_MASK ((1 << PLL_MISC_LFCON_WIDTH) - 1)
46#define PLL_MISC_VCOCON_SHIFT 0
47#define PLL_MISC_VCOCON_WIDTH 4
48#define PLL_MISC_VCOCON_MASK ((1 << PLL_MISC_VCOCON_WIDTH) - 1)
49
50#define OUT_OF_TABLE_CPCON 8
51
52#define PMC_PLLP_WB0_OVERRIDE 0xf8
53#define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE BIT(12)
54#define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE BIT(11)
55
56#define PLL_POST_LOCK_DELAY 50
57
58#define PLLDU_LFCON_SET_DIVN 600
59
60#define PLLE_BASE_DIVCML_SHIFT 24
61#define PLLE_BASE_DIVCML_WIDTH 4
62#define PLLE_BASE_DIVP_SHIFT 16
63#define PLLE_BASE_DIVP_WIDTH 7
64#define PLLE_BASE_DIVN_SHIFT 8
65#define PLLE_BASE_DIVN_WIDTH 8
66#define PLLE_BASE_DIVM_SHIFT 0
67#define PLLE_BASE_DIVM_WIDTH 8
68
69#define PLLE_MISC_SETUP_BASE_SHIFT 16
70#define PLLE_MISC_SETUP_BASE_MASK (0xffff << PLLE_MISC_SETUP_BASE_SHIFT)
71#define PLLE_MISC_LOCK_ENABLE BIT(9)
72#define PLLE_MISC_READY BIT(15)
73#define PLLE_MISC_SETUP_EX_SHIFT 2
74#define PLLE_MISC_SETUP_EX_MASK (3 << PLLE_MISC_SETUP_EX_SHIFT)
75#define PLLE_MISC_SETUP_MASK (PLLE_MISC_SETUP_BASE_MASK | \
76 PLLE_MISC_SETUP_EX_MASK)
77#define PLLE_MISC_SETUP_VALUE (7 << PLLE_MISC_SETUP_BASE_SHIFT)
78
79#define PLLE_SS_CTRL 0x68
80#define PLLE_SS_DISABLE (7 << 10)
81
Peter De Schrijverc1d19392013-04-03 17:40:41 +030082#define PLLE_AUX_PLLP_SEL BIT(2)
83#define PLLE_AUX_ENABLE_SWCTL BIT(4)
84#define PLLE_AUX_SEQ_ENABLE BIT(24)
85#define PLLE_AUX_PLLRE_SEL BIT(28)
86
87#define PLLE_MISC_PLLE_PTS BIT(8)
88#define PLLE_MISC_IDDQ_SW_VALUE BIT(13)
89#define PLLE_MISC_IDDQ_SW_CTRL BIT(14)
90#define PLLE_MISC_VREG_BG_CTRL_SHIFT 4
91#define PLLE_MISC_VREG_BG_CTRL_MASK (3 << PLLE_MISC_VREG_BG_CTRL_SHIFT)
92#define PLLE_MISC_VREG_CTRL_SHIFT 2
93#define PLLE_MISC_VREG_CTRL_MASK (2 << PLLE_MISC_VREG_CTRL_SHIFT)
94
95#define PLLCX_MISC_STROBE BIT(31)
96#define PLLCX_MISC_RESET BIT(30)
97#define PLLCX_MISC_SDM_DIV_SHIFT 28
98#define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT)
99#define PLLCX_MISC_FILT_DIV_SHIFT 26
100#define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT)
101#define PLLCX_MISC_ALPHA_SHIFT 18
102#define PLLCX_MISC_DIV_LOW_RANGE \
103 ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \
104 (0x1 << PLLCX_MISC_FILT_DIV_SHIFT))
105#define PLLCX_MISC_DIV_HIGH_RANGE \
106 ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \
107 (0x2 << PLLCX_MISC_FILT_DIV_SHIFT))
108#define PLLCX_MISC_COEF_LOW_RANGE \
109 ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT))
110#define PLLCX_MISC_KA_SHIFT 2
111#define PLLCX_MISC_KB_SHIFT 9
112#define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \
113 (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \
114 PLLCX_MISC_DIV_LOW_RANGE | \
115 PLLCX_MISC_RESET)
116#define PLLCX_MISC1_DEFAULT 0x000d2308
117#define PLLCX_MISC2_DEFAULT 0x30211200
118#define PLLCX_MISC3_DEFAULT 0x200
119
120#define PMC_PLLM_WB0_OVERRIDE 0x1dc
121#define PMC_PLLM_WB0_OVERRIDE_2 0x2b0
122#define PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK BIT(27)
123
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530124#define PMC_SATA_PWRGT 0x1ac
125#define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5)
126#define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4)
127
128#define pll_readl(offset, p) readl_relaxed(p->clk_base + offset)
129#define pll_readl_base(p) pll_readl(p->params->base_reg, p)
130#define pll_readl_misc(p) pll_readl(p->params->misc_reg, p)
131
132#define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset)
133#define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p)
134#define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p)
135
136#define mask(w) ((1 << (w)) - 1)
137#define divm_mask(p) mask(p->divm_width)
138#define divn_mask(p) mask(p->divn_width)
139#define divp_mask(p) (p->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK : \
140 mask(p->divp_width))
141
142#define divm_max(p) (divm_mask(p))
143#define divn_max(p) (divn_mask(p))
144#define divp_max(p) (1 << (divp_mask(p)))
145
146static void clk_pll_enable_lock(struct tegra_clk_pll *pll)
147{
148 u32 val;
149
150 if (!(pll->flags & TEGRA_PLL_USE_LOCK))
151 return;
152
Peter De Schrijver7ba28812013-04-03 17:40:38 +0300153 if (!(pll->flags & TEGRA_PLL_HAS_LOCK_ENABLE))
154 return;
155
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530156 val = pll_readl_misc(pll);
157 val |= BIT(pll->params->lock_enable_bit_idx);
158 pll_writel_misc(val, pll);
159}
160
Peter De Schrijverdba40722013-04-03 17:40:36 +0300161static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530162{
163 int i;
Peter De Schrijver3e727712013-04-03 17:40:40 +0300164 u32 val, lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300165 void __iomem *lock_addr;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530166
167 if (!(pll->flags & TEGRA_PLL_USE_LOCK)) {
168 udelay(pll->params->lock_delay);
169 return 0;
170 }
171
Peter De Schrijverdba40722013-04-03 17:40:36 +0300172 lock_addr = pll->clk_base;
173 if (pll->flags & TEGRA_PLL_LOCK_MISC)
174 lock_addr += pll->params->misc_reg;
175 else
176 lock_addr += pll->params->base_reg;
177
Peter De Schrijver3e727712013-04-03 17:40:40 +0300178 lock_mask = pll->params->lock_mask;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300179
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530180 for (i = 0; i < pll->params->lock_delay; i++) {
181 val = readl_relaxed(lock_addr);
Peter De Schrijver3e727712013-04-03 17:40:40 +0300182 if ((val & lock_mask) == lock_mask) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530183 udelay(PLL_POST_LOCK_DELAY);
184 return 0;
185 }
186 udelay(2); /* timeout = 2 * lock time */
187 }
188
189 pr_err("%s: Timed out waiting for pll %s lock\n", __func__,
190 __clk_get_name(pll->hw.clk));
191
192 return -1;
193}
194
195static int clk_pll_is_enabled(struct clk_hw *hw)
196{
197 struct tegra_clk_pll *pll = to_clk_pll(hw);
198 u32 val;
199
200 if (pll->flags & TEGRA_PLLM) {
201 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
202 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)
203 return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0;
204 }
205
206 val = pll_readl_base(pll);
207
208 return val & PLL_BASE_ENABLE ? 1 : 0;
209}
210
Peter De Schrijverdba40722013-04-03 17:40:36 +0300211static void _clk_pll_enable(struct clk_hw *hw)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530212{
213 struct tegra_clk_pll *pll = to_clk_pll(hw);
214 u32 val;
215
216 clk_pll_enable_lock(pll);
217
218 val = pll_readl_base(pll);
Peter De Schrijverdd935872013-04-03 17:40:37 +0300219 if (pll->flags & TEGRA_PLL_BYPASS)
220 val &= ~PLL_BASE_BYPASS;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530221 val |= PLL_BASE_ENABLE;
222 pll_writel_base(val, pll);
223
224 if (pll->flags & TEGRA_PLLM) {
225 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
226 val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
227 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
228 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530229}
230
231static void _clk_pll_disable(struct clk_hw *hw)
232{
233 struct tegra_clk_pll *pll = to_clk_pll(hw);
234 u32 val;
235
236 val = pll_readl_base(pll);
Peter De Schrijverdd935872013-04-03 17:40:37 +0300237 if (pll->flags & TEGRA_PLL_BYPASS)
238 val &= ~PLL_BASE_BYPASS;
239 val &= ~PLL_BASE_ENABLE;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530240 pll_writel_base(val, pll);
241
242 if (pll->flags & TEGRA_PLLM) {
243 val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE);
244 val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE;
245 writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE);
246 }
247}
248
249static int clk_pll_enable(struct clk_hw *hw)
250{
251 struct tegra_clk_pll *pll = to_clk_pll(hw);
252 unsigned long flags = 0;
253 int ret;
254
255 if (pll->lock)
256 spin_lock_irqsave(pll->lock, flags);
257
Peter De Schrijverdba40722013-04-03 17:40:36 +0300258 _clk_pll_enable(hw);
259
260 ret = clk_pll_wait_for_lock(pll);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530261
262 if (pll->lock)
263 spin_unlock_irqrestore(pll->lock, flags);
264
265 return ret;
266}
267
268static void clk_pll_disable(struct clk_hw *hw)
269{
270 struct tegra_clk_pll *pll = to_clk_pll(hw);
271 unsigned long flags = 0;
272
273 if (pll->lock)
274 spin_lock_irqsave(pll->lock, flags);
275
276 _clk_pll_disable(hw);
277
278 if (pll->lock)
279 spin_unlock_irqrestore(pll->lock, flags);
280}
281
Peter De Schrijver053b5252013-06-05 15:56:41 +0300282static int _p_div_to_hw(struct clk_hw *hw, u8 p_div)
283{
284 struct tegra_clk_pll *pll = to_clk_pll(hw);
285 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
286
287 if (p_tohw) {
288 while (p_tohw->pdiv) {
289 if (p_div <= p_tohw->pdiv)
290 return p_tohw->hw_val;
291 p_tohw++;
292 }
293 return -EINVAL;
294 }
295 return -EINVAL;
296}
297
298static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw)
299{
300 struct tegra_clk_pll *pll = to_clk_pll(hw);
301 struct pdiv_map *p_tohw = pll->params->pdiv_tohw;
302
303 if (p_tohw) {
304 while (p_tohw->pdiv) {
305 if (p_div_hw == p_tohw->hw_val)
306 return p_tohw->pdiv;
307 p_tohw++;
308 }
309 return -EINVAL;
310 }
311
312 return 1 << p_div_hw;
313}
314
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530315static int _get_table_rate(struct clk_hw *hw,
316 struct tegra_clk_pll_freq_table *cfg,
317 unsigned long rate, unsigned long parent_rate)
318{
319 struct tegra_clk_pll *pll = to_clk_pll(hw);
320 struct tegra_clk_pll_freq_table *sel;
321
322 for (sel = pll->freq_table; sel->input_rate != 0; sel++)
323 if (sel->input_rate == parent_rate &&
324 sel->output_rate == rate)
325 break;
326
327 if (sel->input_rate == 0)
328 return -EINVAL;
329
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530330 cfg->input_rate = sel->input_rate;
331 cfg->output_rate = sel->output_rate;
332 cfg->m = sel->m;
333 cfg->n = sel->n;
334 cfg->p = sel->p;
335 cfg->cpcon = sel->cpcon;
336
337 return 0;
338}
339
340static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
341 unsigned long rate, unsigned long parent_rate)
342{
343 struct tegra_clk_pll *pll = to_clk_pll(hw);
344 unsigned long cfreq;
345 u32 p_div = 0;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300346 int ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530347
348 switch (parent_rate) {
349 case 12000000:
350 case 26000000:
351 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000;
352 break;
353 case 13000000:
354 cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000;
355 break;
356 case 16800000:
357 case 19200000:
358 cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000;
359 break;
360 case 9600000:
361 case 28800000:
362 /*
363 * PLL_P_OUT1 rate is not listed in PLLA table
364 */
365 cfreq = parent_rate/(parent_rate/1000000);
366 break;
367 default:
368 pr_err("%s Unexpected reference rate %lu\n",
369 __func__, parent_rate);
370 BUG();
371 }
372
373 /* Raise VCO to guarantee 0.5% accuracy */
374 for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq;
375 cfg->output_rate <<= 1)
376 p_div++;
377
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530378 cfg->m = parent_rate / cfreq;
379 cfg->n = cfg->output_rate / cfreq;
380 cfg->cpcon = OUT_OF_TABLE_CPCON;
381
382 if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) ||
Peter De Schrijverdba40722013-04-03 17:40:36 +0300383 (1 << p_div) > divp_max(pll)
384 || cfg->output_rate > pll->params->vco_max) {
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530385 pr_err("%s: Failed to set %s rate %lu\n",
386 __func__, __clk_get_name(hw->clk), rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300387 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530388 return -EINVAL;
389 }
390
Peter De Schrijver053b5252013-06-05 15:56:41 +0300391 if (pll->params->pdiv_tohw) {
392 ret = _p_div_to_hw(hw, 1 << p_div);
393 if (ret < 0)
394 return ret;
395 else
396 cfg->p = ret;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300397 } else
398 cfg->p = p_div;
Peter De Schrijverdba40722013-04-03 17:40:36 +0300399
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530400 return 0;
401}
402
Peter De Schrijverdba40722013-04-03 17:40:36 +0300403static void _update_pll_mnp(struct tegra_clk_pll *pll,
404 struct tegra_clk_pll_freq_table *cfg)
405{
406 u32 val;
407
408 val = pll_readl_base(pll);
409
410 val &= ~((divm_mask(pll) << pll->divm_shift) |
411 (divn_mask(pll) << pll->divn_shift) |
412 (divp_mask(pll) << pll->divp_shift));
413 val |= ((cfg->m << pll->divm_shift) |
414 (cfg->n << pll->divn_shift) |
415 (cfg->p << pll->divp_shift));
416
417 pll_writel_base(val, pll);
418}
419
420static void _get_pll_mnp(struct tegra_clk_pll *pll,
421 struct tegra_clk_pll_freq_table *cfg)
422{
423 u32 val;
424
425 val = pll_readl_base(pll);
426
427 cfg->m = (val >> pll->divm_shift) & (divm_mask(pll));
428 cfg->n = (val >> pll->divn_shift) & (divn_mask(pll));
429 cfg->p = (val >> pll->divp_shift) & (divp_mask(pll));
430}
431
432static void _update_pll_cpcon(struct tegra_clk_pll *pll,
433 struct tegra_clk_pll_freq_table *cfg,
434 unsigned long rate)
435{
436 u32 val;
437
438 val = pll_readl_misc(pll);
439
440 val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT);
441 val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT;
442
443 if (pll->flags & TEGRA_PLL_SET_LFCON) {
444 val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT);
445 if (cfg->n >= PLLDU_LFCON_SET_DIVN)
446 val |= 1 << PLL_MISC_LFCON_SHIFT;
447 } else if (pll->flags & TEGRA_PLL_SET_DCCON) {
448 val &= ~(1 << PLL_MISC_DCCON_SHIFT);
449 if (rate >= (pll->params->vco_max >> 1))
450 val |= 1 << PLL_MISC_DCCON_SHIFT;
451 }
452
453 pll_writel_misc(val, pll);
454}
455
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530456static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg,
457 unsigned long rate)
458{
459 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300460 int state, ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530461
462 state = clk_pll_is_enabled(hw);
463
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530464 if (state)
Peter De Schrijverdba40722013-04-03 17:40:36 +0300465 _clk_pll_disable(hw);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530466
Peter De Schrijverdba40722013-04-03 17:40:36 +0300467 _update_pll_mnp(pll, cfg);
468
469 if (pll->flags & TEGRA_PLL_HAS_CPCON)
470 _update_pll_cpcon(pll, cfg, rate);
471
472 if (state) {
473 _clk_pll_enable(hw);
474 ret = clk_pll_wait_for_lock(pll);
475 }
476
477 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530478}
479
480static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
481 unsigned long parent_rate)
482{
483 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300484 struct tegra_clk_pll_freq_table cfg, old_cfg;
485 unsigned long flags = 0;
486 int ret = 0;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530487
488 if (pll->flags & TEGRA_PLL_FIXED) {
489 if (rate != pll->fixed_rate) {
490 pr_err("%s: Can not change %s fixed rate %lu to %lu\n",
491 __func__, __clk_get_name(hw->clk),
492 pll->fixed_rate, rate);
493 return -EINVAL;
494 }
495 return 0;
496 }
497
498 if (_get_table_rate(hw, &cfg, rate, parent_rate) &&
Peter De Schrijver053b5252013-06-05 15:56:41 +0300499 _calc_rate(hw, &cfg, rate, parent_rate)) {
500 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530501 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300502 }
Peter De Schrijverdba40722013-04-03 17:40:36 +0300503 if (pll->lock)
504 spin_lock_irqsave(pll->lock, flags);
505
506 _get_pll_mnp(pll, &old_cfg);
507
508 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
509 ret = _program_pll(hw, &cfg, rate);
510
511 if (pll->lock)
512 spin_unlock_irqrestore(pll->lock, flags);
513
514 return ret;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530515}
516
517static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
518 unsigned long *prate)
519{
520 struct tegra_clk_pll *pll = to_clk_pll(hw);
521 struct tegra_clk_pll_freq_table cfg;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530522
523 if (pll->flags & TEGRA_PLL_FIXED)
524 return pll->fixed_rate;
525
526 /* PLLM is used for memory; we do not change rate */
527 if (pll->flags & TEGRA_PLLM)
528 return __clk_get_rate(hw->clk);
529
530 if (_get_table_rate(hw, &cfg, rate, *prate) &&
Peter De Schrijver053b5252013-06-05 15:56:41 +0300531 _calc_rate(hw, &cfg, rate, *prate)) {
532 WARN_ON(1);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530533 return -EINVAL;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300534 }
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530535
Peter De Schrijver053b5252013-06-05 15:56:41 +0300536 return cfg.output_rate;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530537}
538
539static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
540 unsigned long parent_rate)
541{
542 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijverdba40722013-04-03 17:40:36 +0300543 struct tegra_clk_pll_freq_table cfg;
544 u32 val;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530545 u64 rate = parent_rate;
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300546 int pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530547
Peter De Schrijverdba40722013-04-03 17:40:36 +0300548 val = pll_readl_base(pll);
549
Peter De Schrijverdd935872013-04-03 17:40:37 +0300550 if ((pll->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS))
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530551 return parent_rate;
552
553 if ((pll->flags & TEGRA_PLL_FIXED) && !(val & PLL_BASE_OVERRIDE)) {
554 struct tegra_clk_pll_freq_table sel;
555 if (_get_table_rate(hw, &sel, pll->fixed_rate, parent_rate)) {
556 pr_err("Clock %s has unknown fixed frequency\n",
557 __clk_get_name(hw->clk));
558 BUG();
559 }
560 return pll->fixed_rate;
561 }
562
Peter De Schrijverdba40722013-04-03 17:40:36 +0300563 _get_pll_mnp(pll, &cfg);
564
Peter De Schrijver053b5252013-06-05 15:56:41 +0300565 pdiv = _hw_to_p_div(hw, cfg.p);
566 if (pdiv < 0) {
567 WARN_ON(1);
568 pdiv = 1;
569 }
Peter De Schrijver0b6525a2013-04-03 17:40:39 +0300570
571 cfg.m *= pdiv;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530572
Peter De Schrijverdba40722013-04-03 17:40:36 +0300573 rate *= cfg.n;
574 do_div(rate, cfg.m);
575
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530576 return rate;
577}
578
579static int clk_plle_training(struct tegra_clk_pll *pll)
580{
581 u32 val;
582 unsigned long timeout;
583
584 if (!pll->pmc)
585 return -ENOSYS;
586
587 /*
588 * PLLE is already disabled, and setup cleared;
589 * create falling edge on PLLE IDDQ input.
590 */
591 val = readl(pll->pmc + PMC_SATA_PWRGT);
592 val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
593 writel(val, pll->pmc + PMC_SATA_PWRGT);
594
595 val = readl(pll->pmc + PMC_SATA_PWRGT);
596 val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL;
597 writel(val, pll->pmc + PMC_SATA_PWRGT);
598
599 val = readl(pll->pmc + PMC_SATA_PWRGT);
600 val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE;
601 writel(val, pll->pmc + PMC_SATA_PWRGT);
602
603 val = pll_readl_misc(pll);
604
605 timeout = jiffies + msecs_to_jiffies(100);
606 while (1) {
607 val = pll_readl_misc(pll);
608 if (val & PLLE_MISC_READY)
609 break;
610 if (time_after(jiffies, timeout)) {
611 pr_err("%s: timeout waiting for PLLE\n", __func__);
612 return -EBUSY;
613 }
614 udelay(300);
615 }
616
617 return 0;
618}
619
620static int clk_plle_enable(struct clk_hw *hw)
621{
622 struct tegra_clk_pll *pll = to_clk_pll(hw);
623 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
624 struct tegra_clk_pll_freq_table sel;
625 u32 val;
626 int err;
627
628 if (_get_table_rate(hw, &sel, pll->fixed_rate, input_rate))
629 return -EINVAL;
630
631 clk_pll_disable(hw);
632
633 val = pll_readl_misc(pll);
634 val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK);
635 pll_writel_misc(val, pll);
636
637 val = pll_readl_misc(pll);
638 if (!(val & PLLE_MISC_READY)) {
639 err = clk_plle_training(pll);
640 if (err)
641 return err;
642 }
643
644 if (pll->flags & TEGRA_PLLE_CONFIGURE) {
645 /* configure dividers */
646 val = pll_readl_base(pll);
647 val &= ~(divm_mask(pll) | divn_mask(pll) | divp_mask(pll));
648 val &= ~(PLLE_BASE_DIVCML_WIDTH << PLLE_BASE_DIVCML_SHIFT);
649 val |= sel.m << pll->divm_shift;
650 val |= sel.n << pll->divn_shift;
651 val |= sel.p << pll->divp_shift;
652 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
653 pll_writel_base(val, pll);
654 }
655
656 val = pll_readl_misc(pll);
657 val |= PLLE_MISC_SETUP_VALUE;
658 val |= PLLE_MISC_LOCK_ENABLE;
659 pll_writel_misc(val, pll);
660
661 val = readl(pll->clk_base + PLLE_SS_CTRL);
662 val |= PLLE_SS_DISABLE;
663 writel(val, pll->clk_base + PLLE_SS_CTRL);
664
665 val |= pll_readl_base(pll);
666 val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE);
667 pll_writel_base(val, pll);
668
Peter De Schrijverdba40722013-04-03 17:40:36 +0300669 clk_pll_wait_for_lock(pll);
670
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +0530671 return 0;
672}
673
674static unsigned long clk_plle_recalc_rate(struct clk_hw *hw,
675 unsigned long parent_rate)
676{
677 struct tegra_clk_pll *pll = to_clk_pll(hw);
678 u32 val = pll_readl_base(pll);
679 u32 divn = 0, divm = 0, divp = 0;
680 u64 rate = parent_rate;
681
682 divp = (val >> pll->divp_shift) & (divp_mask(pll));
683 divn = (val >> pll->divn_shift) & (divn_mask(pll));
684 divm = (val >> pll->divm_shift) & (divm_mask(pll));
685 divm *= divp;
686
687 rate *= divn;
688 do_div(rate, divm);
689 return rate;
690}
691
692const struct clk_ops tegra_clk_pll_ops = {
693 .is_enabled = clk_pll_is_enabled,
694 .enable = clk_pll_enable,
695 .disable = clk_pll_disable,
696 .recalc_rate = clk_pll_recalc_rate,
697 .round_rate = clk_pll_round_rate,
698 .set_rate = clk_pll_set_rate,
699};
700
701const struct clk_ops tegra_clk_plle_ops = {
702 .recalc_rate = clk_plle_recalc_rate,
703 .is_enabled = clk_pll_is_enabled,
704 .disable = clk_pll_disable,
705 .enable = clk_plle_enable,
706};
707
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300708#ifdef CONFIG_ARCH_TEGRA_114_SOC
709
710static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params,
711 unsigned long parent_rate)
712{
713 if (parent_rate > pll_params->cf_max)
714 return 2;
715 else
716 return 1;
717}
718
719static int clk_pll_iddq_enable(struct clk_hw *hw)
720{
721 struct tegra_clk_pll *pll = to_clk_pll(hw);
722 unsigned long flags = 0;
723
724 u32 val;
725 int ret;
726
727 if (pll->lock)
728 spin_lock_irqsave(pll->lock, flags);
729
730 val = pll_readl(pll->params->iddq_reg, pll);
731 val &= ~BIT(pll->params->iddq_bit_idx);
732 pll_writel(val, pll->params->iddq_reg, pll);
733 udelay(2);
734
735 _clk_pll_enable(hw);
736
737 ret = clk_pll_wait_for_lock(pll);
738
739 if (pll->lock)
740 spin_unlock_irqrestore(pll->lock, flags);
741
742 return 0;
743}
744
745static void clk_pll_iddq_disable(struct clk_hw *hw)
746{
747 struct tegra_clk_pll *pll = to_clk_pll(hw);
748 unsigned long flags = 0;
749 u32 val;
750
751 if (pll->lock)
752 spin_lock_irqsave(pll->lock, flags);
753
754 _clk_pll_disable(hw);
755
756 val = pll_readl(pll->params->iddq_reg, pll);
757 val |= BIT(pll->params->iddq_bit_idx);
758 pll_writel(val, pll->params->iddq_reg, pll);
759 udelay(2);
760
761 if (pll->lock)
762 spin_unlock_irqrestore(pll->lock, flags);
763}
764
765static int _calc_dynamic_ramp_rate(struct clk_hw *hw,
766 struct tegra_clk_pll_freq_table *cfg,
767 unsigned long rate, unsigned long parent_rate)
768{
769 struct tegra_clk_pll *pll = to_clk_pll(hw);
770 unsigned int p;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300771 int p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300772
773 if (!rate)
774 return -EINVAL;
775
776 p = DIV_ROUND_UP(pll->params->vco_min, rate);
777 cfg->m = _pll_fixed_mdiv(pll->params, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300778 cfg->output_rate = rate * p;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300779 cfg->n = cfg->output_rate * cfg->m / parent_rate;
780
Peter De Schrijver053b5252013-06-05 15:56:41 +0300781 p_div = _p_div_to_hw(hw, p);
782 if (p_div < 0)
783 return p_div;
784 else
785 cfg->p = p_div;
786
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300787 if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max)
788 return -EINVAL;
789
790 return 0;
791}
792
793static int _pll_ramp_calc_pll(struct clk_hw *hw,
794 struct tegra_clk_pll_freq_table *cfg,
795 unsigned long rate, unsigned long parent_rate)
796{
797 struct tegra_clk_pll *pll = to_clk_pll(hw);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300798 int err = 0, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300799
800 err = _get_table_rate(hw, cfg, rate, parent_rate);
801 if (err < 0)
802 err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate);
Peter De Schrijver053b5252013-06-05 15:56:41 +0300803 else {
804 if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300805 WARN_ON(1);
806 err = -EINVAL;
807 goto out;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300808 }
809 p_div = _p_div_to_hw(hw, cfg->p);
810 if (p_div < 0)
811 return p_div;
812 else
813 cfg->p = p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300814 }
815
Peter De Schrijver053b5252013-06-05 15:56:41 +0300816 if (cfg->p > pll->params->max_p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300817 err = -EINVAL;
818
819out:
820 return err;
821}
822
823static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate,
824 unsigned long parent_rate)
825{
826 struct tegra_clk_pll *pll = to_clk_pll(hw);
827 struct tegra_clk_pll_freq_table cfg, old_cfg;
828 unsigned long flags = 0;
829 int ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300830
831 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
832 if (ret < 0)
833 return ret;
834
835 if (pll->lock)
836 spin_lock_irqsave(pll->lock, flags);
837
838 _get_pll_mnp(pll, &old_cfg);
839
Peter De Schrijver053b5252013-06-05 15:56:41 +0300840 if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300841 ret = _program_pll(hw, &cfg, rate);
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300842
843 if (pll->lock)
844 spin_unlock_irqrestore(pll->lock, flags);
845
846 return ret;
847}
848
849static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate,
850 unsigned long *prate)
851{
852 struct tegra_clk_pll_freq_table cfg;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300853 int ret = 0, p_div;
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300854 u64 output_rate = *prate;
855
856 ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate);
857 if (ret < 0)
858 return ret;
859
Peter De Schrijver053b5252013-06-05 15:56:41 +0300860 p_div = _hw_to_p_div(hw, cfg.p);
861 if (p_div < 0)
862 return p_div;
863
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300864 output_rate *= cfg.n;
Peter De Schrijver053b5252013-06-05 15:56:41 +0300865 do_div(output_rate, cfg.m * p_div);
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300866
867 return output_rate;
868}
869
870static int clk_pllm_set_rate(struct clk_hw *hw, unsigned long rate,
871 unsigned long parent_rate)
872{
873 struct tegra_clk_pll_freq_table cfg;
874 struct tegra_clk_pll *pll = to_clk_pll(hw);
875 unsigned long flags = 0;
876 int state, ret = 0;
877 u32 val;
878
879 if (pll->lock)
880 spin_lock_irqsave(pll->lock, flags);
881
882 state = clk_pll_is_enabled(hw);
883 if (state) {
884 if (rate != clk_get_rate(hw->clk)) {
885 pr_err("%s: Cannot change active PLLM\n", __func__);
886 ret = -EINVAL;
887 goto out;
888 }
889 goto out;
890 }
891
892 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
893 if (ret < 0)
894 goto out;
895
Peter De Schrijverc1d19392013-04-03 17:40:41 +0300896 val = readl_relaxed(pll->pmc + PMC_PLLM_WB0_OVERRIDE);
897 if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) {
898 val = readl_relaxed(pll->pmc + PMC_PLLM_WB0_OVERRIDE_2);
899 val = cfg.p ? (val | PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK) :
900 (val & ~PMC_PLLM_WB0_OVERRIDE_2_DIVP_MASK);
901 writel_relaxed(val, pll->pmc + PMC_PLLM_WB0_OVERRIDE_2);
902
903 val = readl_relaxed(pll->pmc + PMC_PLLM_WB0_OVERRIDE);
904 val &= ~(divn_mask(pll) | divm_mask(pll));
905 val |= (cfg.m << pll->divm_shift) | (cfg.n << pll->divn_shift);
906 writel_relaxed(val, pll->pmc + PMC_PLLM_WB0_OVERRIDE);
907 } else
908 _update_pll_mnp(pll, &cfg);
909
910
911out:
912 if (pll->lock)
913 spin_unlock_irqrestore(pll->lock, flags);
914
915 return ret;
916}
917
918static void _pllcx_strobe(struct tegra_clk_pll *pll)
919{
920 u32 val;
921
922 val = pll_readl_misc(pll);
923 val |= PLLCX_MISC_STROBE;
924 pll_writel_misc(val, pll);
925 udelay(2);
926
927 val &= ~PLLCX_MISC_STROBE;
928 pll_writel_misc(val, pll);
929}
930
931static int clk_pllc_enable(struct clk_hw *hw)
932{
933 struct tegra_clk_pll *pll = to_clk_pll(hw);
934 u32 val;
935 int ret = 0;
936 unsigned long flags = 0;
937
938 if (pll->lock)
939 spin_lock_irqsave(pll->lock, flags);
940
941 _clk_pll_enable(hw);
942 udelay(2);
943
944 val = pll_readl_misc(pll);
945 val &= ~PLLCX_MISC_RESET;
946 pll_writel_misc(val, pll);
947 udelay(2);
948
949 _pllcx_strobe(pll);
950
951 ret = clk_pll_wait_for_lock(pll);
952
953 if (pll->lock)
954 spin_unlock_irqrestore(pll->lock, flags);
955
956 return ret;
957}
958
959static void _clk_pllc_disable(struct clk_hw *hw)
960{
961 struct tegra_clk_pll *pll = to_clk_pll(hw);
962 u32 val;
963
964 _clk_pll_disable(hw);
965
966 val = pll_readl_misc(pll);
967 val |= PLLCX_MISC_RESET;
968 pll_writel_misc(val, pll);
969 udelay(2);
970}
971
972static void clk_pllc_disable(struct clk_hw *hw)
973{
974 struct tegra_clk_pll *pll = to_clk_pll(hw);
975 unsigned long flags = 0;
976
977 if (pll->lock)
978 spin_lock_irqsave(pll->lock, flags);
979
980 _clk_pllc_disable(hw);
981
982 if (pll->lock)
983 spin_unlock_irqrestore(pll->lock, flags);
984}
985
986static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll,
987 unsigned long input_rate, u32 n)
988{
989 u32 val, n_threshold;
990
991 switch (input_rate) {
992 case 12000000:
993 n_threshold = 70;
994 break;
995 case 13000000:
996 case 26000000:
997 n_threshold = 71;
998 break;
999 case 16800000:
1000 n_threshold = 55;
1001 break;
1002 case 19200000:
1003 n_threshold = 48;
1004 break;
1005 default:
1006 pr_err("%s: Unexpected reference rate %lu\n",
1007 __func__, input_rate);
1008 return -EINVAL;
1009 }
1010
1011 val = pll_readl_misc(pll);
1012 val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK);
1013 val |= n <= n_threshold ?
1014 PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE;
1015 pll_writel_misc(val, pll);
1016
1017 return 0;
1018}
1019
1020static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate,
1021 unsigned long parent_rate)
1022{
Peter De Schrijver053b5252013-06-05 15:56:41 +03001023 struct tegra_clk_pll_freq_table cfg, old_cfg;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001024 struct tegra_clk_pll *pll = to_clk_pll(hw);
1025 unsigned long flags = 0;
1026 int state, ret = 0;
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001027
1028 if (pll->lock)
1029 spin_lock_irqsave(pll->lock, flags);
1030
1031 ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate);
1032 if (ret < 0)
1033 goto out;
1034
Peter De Schrijver053b5252013-06-05 15:56:41 +03001035 _get_pll_mnp(pll, &old_cfg);
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001036
Peter De Schrijver053b5252013-06-05 15:56:41 +03001037 if (cfg.m != old_cfg.m) {
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001038 WARN_ON(1);
1039 goto out;
1040 }
1041
Peter De Schrijver053b5252013-06-05 15:56:41 +03001042 if (old_cfg.n == cfg.n && old_cfg.p == cfg.p)
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001043 goto out;
1044
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001045 state = clk_pll_is_enabled(hw);
1046 if (state)
1047 _clk_pllc_disable(hw);
1048
1049 ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1050 if (ret < 0)
1051 goto out;
1052
1053 _update_pll_mnp(pll, &cfg);
1054
1055 if (state)
1056 ret = clk_pllc_enable(hw);
1057
1058out:
1059 if (pll->lock)
1060 spin_unlock_irqrestore(pll->lock, flags);
1061
1062 return ret;
1063}
1064
1065static long _pllre_calc_rate(struct tegra_clk_pll *pll,
1066 struct tegra_clk_pll_freq_table *cfg,
1067 unsigned long rate, unsigned long parent_rate)
1068{
1069 u16 m, n;
1070 u64 output_rate = parent_rate;
1071
1072 m = _pll_fixed_mdiv(pll->params, parent_rate);
1073 n = rate * m / parent_rate;
1074
1075 output_rate *= n;
1076 do_div(output_rate, m);
1077
1078 if (cfg) {
1079 cfg->m = m;
1080 cfg->n = n;
1081 }
1082
1083 return output_rate;
1084}
1085static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate,
1086 unsigned long parent_rate)
1087{
1088 struct tegra_clk_pll_freq_table cfg, old_cfg;
1089 struct tegra_clk_pll *pll = to_clk_pll(hw);
1090 unsigned long flags = 0;
1091 int state, ret = 0;
1092
1093 if (pll->lock)
1094 spin_lock_irqsave(pll->lock, flags);
1095
1096 _pllre_calc_rate(pll, &cfg, rate, parent_rate);
1097 _get_pll_mnp(pll, &old_cfg);
1098 cfg.p = old_cfg.p;
1099
1100 if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) {
1101 state = clk_pll_is_enabled(hw);
1102 if (state)
1103 _clk_pll_disable(hw);
1104
1105 _update_pll_mnp(pll, &cfg);
1106
1107 if (state) {
1108 _clk_pll_enable(hw);
1109 ret = clk_pll_wait_for_lock(pll);
1110 }
1111 }
1112
1113 if (pll->lock)
1114 spin_unlock_irqrestore(pll->lock, flags);
1115
1116 return ret;
1117}
1118
1119static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw,
1120 unsigned long parent_rate)
1121{
1122 struct tegra_clk_pll_freq_table cfg;
1123 struct tegra_clk_pll *pll = to_clk_pll(hw);
1124 u64 rate = parent_rate;
1125
1126 _get_pll_mnp(pll, &cfg);
1127
1128 rate *= cfg.n;
1129 do_div(rate, cfg.m);
1130
1131 return rate;
1132}
1133
1134static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate,
1135 unsigned long *prate)
1136{
1137 struct tegra_clk_pll *pll = to_clk_pll(hw);
1138
1139 return _pllre_calc_rate(pll, NULL, rate, *prate);
1140}
1141
1142static int clk_plle_tegra114_enable(struct clk_hw *hw)
1143{
1144 struct tegra_clk_pll *pll = to_clk_pll(hw);
1145 struct tegra_clk_pll_freq_table sel;
1146 u32 val;
1147 int ret;
1148 unsigned long flags = 0;
1149 unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk));
1150
1151 if (_get_table_rate(hw, &sel, pll->fixed_rate, input_rate))
1152 return -EINVAL;
1153
1154 if (pll->lock)
1155 spin_lock_irqsave(pll->lock, flags);
1156
1157 val = pll_readl_base(pll);
1158 val &= ~BIT(29); /* Disable lock override */
1159 pll_writel_base(val, pll);
1160
1161 val = pll_readl(pll->params->aux_reg, pll);
1162 val |= PLLE_AUX_ENABLE_SWCTL;
1163 val &= ~PLLE_AUX_SEQ_ENABLE;
1164 pll_writel(val, pll->params->aux_reg, pll);
1165 udelay(1);
1166
1167 val = pll_readl_misc(pll);
1168 val |= PLLE_MISC_LOCK_ENABLE;
1169 val |= PLLE_MISC_IDDQ_SW_CTRL;
1170 val &= ~PLLE_MISC_IDDQ_SW_VALUE;
1171 val |= PLLE_MISC_PLLE_PTS;
1172 val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK;
1173 pll_writel_misc(val, pll);
1174 udelay(5);
1175
1176 val = pll_readl(PLLE_SS_CTRL, pll);
1177 val |= PLLE_SS_DISABLE;
1178 pll_writel(val, PLLE_SS_CTRL, pll);
1179
1180 val = pll_readl_base(pll);
1181 val &= ~(divm_mask(pll) | divn_mask(pll) | divp_mask(pll));
1182 val &= ~(PLLE_BASE_DIVCML_WIDTH << PLLE_BASE_DIVCML_SHIFT);
1183 val |= sel.m << pll->divm_shift;
1184 val |= sel.n << pll->divn_shift;
1185 val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT;
1186 pll_writel_base(val, pll);
1187 udelay(1);
1188
1189 _clk_pll_enable(hw);
1190 ret = clk_pll_wait_for_lock(pll);
1191
1192 if (ret < 0)
1193 goto out;
1194
1195 /* TODO: enable hw control of xusb brick pll */
1196
1197out:
1198 if (pll->lock)
1199 spin_unlock_irqrestore(pll->lock, flags);
1200
1201 return ret;
1202}
1203
1204static void clk_plle_tegra114_disable(struct clk_hw *hw)
1205{
1206 struct tegra_clk_pll *pll = to_clk_pll(hw);
1207 unsigned long flags = 0;
1208 u32 val;
1209
1210 if (pll->lock)
1211 spin_lock_irqsave(pll->lock, flags);
1212
1213 _clk_pll_disable(hw);
1214
1215 val = pll_readl_misc(pll);
1216 val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE;
1217 pll_writel_misc(val, pll);
1218 udelay(1);
1219
1220 if (pll->lock)
1221 spin_unlock_irqrestore(pll->lock, flags);
1222}
1223#endif
1224
Peter De Schrijverdba40722013-04-03 17:40:36 +03001225static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base,
1226 void __iomem *pmc, unsigned long fixed_rate,
1227 struct tegra_clk_pll_params *pll_params, u32 pll_flags,
1228 struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock)
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301229{
1230 struct tegra_clk_pll *pll;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301231
1232 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1233 if (!pll)
1234 return ERR_PTR(-ENOMEM);
1235
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301236 pll->clk_base = clk_base;
1237 pll->pmc = pmc;
1238
1239 pll->freq_table = freq_table;
1240 pll->params = pll_params;
1241 pll->fixed_rate = fixed_rate;
1242 pll->flags = pll_flags;
1243 pll->lock = lock;
1244
1245 pll->divp_shift = PLL_BASE_DIVP_SHIFT;
1246 pll->divp_width = PLL_BASE_DIVP_WIDTH;
1247 pll->divn_shift = PLL_BASE_DIVN_SHIFT;
1248 pll->divn_width = PLL_BASE_DIVN_WIDTH;
1249 pll->divm_shift = PLL_BASE_DIVM_SHIFT;
1250 pll->divm_width = PLL_BASE_DIVM_WIDTH;
1251
Peter De Schrijverdba40722013-04-03 17:40:36 +03001252 return pll;
1253}
1254
1255static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll,
1256 const char *name, const char *parent_name, unsigned long flags,
1257 const struct clk_ops *ops)
1258{
1259 struct clk_init_data init;
1260
1261 init.name = name;
1262 init.ops = ops;
1263 init.flags = flags;
1264 init.parent_names = (parent_name ? &parent_name : NULL);
1265 init.num_parents = (parent_name ? 1 : 0);
1266
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301267 /* Data in .init is copied by clk_register(), so stack variable OK */
1268 pll->hw.init = &init;
1269
Peter De Schrijverdba40722013-04-03 17:40:36 +03001270 return clk_register(NULL, &pll->hw);
1271}
1272
1273struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
1274 void __iomem *clk_base, void __iomem *pmc,
1275 unsigned long flags, unsigned long fixed_rate,
1276 struct tegra_clk_pll_params *pll_params, u32 pll_flags,
1277 struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock)
1278{
1279 struct tegra_clk_pll *pll;
1280 struct clk *clk;
1281
Peter De Schrijverdd935872013-04-03 17:40:37 +03001282 pll_flags |= TEGRA_PLL_BYPASS;
Peter De Schrijver7ba28812013-04-03 17:40:38 +03001283 pll_flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001284 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1285 freq_table, lock);
1286 if (IS_ERR(pll))
1287 return ERR_CAST(pll);
1288
1289 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1290 &tegra_clk_pll_ops);
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301291 if (IS_ERR(clk))
1292 kfree(pll);
1293
1294 return clk;
1295}
1296
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301297struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
1298 void __iomem *clk_base, void __iomem *pmc,
1299 unsigned long flags, unsigned long fixed_rate,
Peter De Schrijverdba40722013-04-03 17:40:36 +03001300 struct tegra_clk_pll_params *pll_params, u32 pll_flags,
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301301 struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock)
1302{
Peter De Schrijverdba40722013-04-03 17:40:36 +03001303 struct tegra_clk_pll *pll;
1304 struct clk *clk;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001305
Peter De Schrijverdd935872013-04-03 17:40:37 +03001306 pll_flags |= TEGRA_PLL_LOCK_MISC | TEGRA_PLL_BYPASS;
Peter De Schrijver7ba28812013-04-03 17:40:38 +03001307 pll_flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
Peter De Schrijverdba40722013-04-03 17:40:36 +03001308 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1309 freq_table, lock);
1310 if (IS_ERR(pll))
1311 return ERR_CAST(pll);
1312
1313 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1314 &tegra_clk_plle_ops);
1315 if (IS_ERR(clk))
1316 kfree(pll);
1317
1318 return clk;
Prashant Gaikwad8f8f4842013-01-11 13:16:20 +05301319}
Peter De Schrijverc1d19392013-04-03 17:40:41 +03001320
1321#ifdef CONFIG_ARCH_TEGRA_114_SOC
1322const struct clk_ops tegra_clk_pllxc_ops = {
1323 .is_enabled = clk_pll_is_enabled,
1324 .enable = clk_pll_iddq_enable,
1325 .disable = clk_pll_iddq_disable,
1326 .recalc_rate = clk_pll_recalc_rate,
1327 .round_rate = clk_pll_ramp_round_rate,
1328 .set_rate = clk_pllxc_set_rate,
1329};
1330
1331const struct clk_ops tegra_clk_pllm_ops = {
1332 .is_enabled = clk_pll_is_enabled,
1333 .enable = clk_pll_iddq_enable,
1334 .disable = clk_pll_iddq_disable,
1335 .recalc_rate = clk_pll_recalc_rate,
1336 .round_rate = clk_pll_ramp_round_rate,
1337 .set_rate = clk_pllm_set_rate,
1338};
1339
1340const struct clk_ops tegra_clk_pllc_ops = {
1341 .is_enabled = clk_pll_is_enabled,
1342 .enable = clk_pllc_enable,
1343 .disable = clk_pllc_disable,
1344 .recalc_rate = clk_pll_recalc_rate,
1345 .round_rate = clk_pll_ramp_round_rate,
1346 .set_rate = clk_pllc_set_rate,
1347};
1348
1349const struct clk_ops tegra_clk_pllre_ops = {
1350 .is_enabled = clk_pll_is_enabled,
1351 .enable = clk_pll_iddq_enable,
1352 .disable = clk_pll_iddq_disable,
1353 .recalc_rate = clk_pllre_recalc_rate,
1354 .round_rate = clk_pllre_round_rate,
1355 .set_rate = clk_pllre_set_rate,
1356};
1357
1358const struct clk_ops tegra_clk_plle_tegra114_ops = {
1359 .is_enabled = clk_pll_is_enabled,
1360 .enable = clk_plle_tegra114_enable,
1361 .disable = clk_plle_tegra114_disable,
1362 .recalc_rate = clk_pll_recalc_rate,
1363};
1364
1365
1366struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name,
1367 void __iomem *clk_base, void __iomem *pmc,
1368 unsigned long flags, unsigned long fixed_rate,
1369 struct tegra_clk_pll_params *pll_params,
1370 u32 pll_flags,
1371 struct tegra_clk_pll_freq_table *freq_table,
1372 spinlock_t *lock)
1373{
1374 struct tegra_clk_pll *pll;
1375 struct clk *clk;
1376
1377 if (!pll_params->pdiv_tohw)
1378 return ERR_PTR(-EINVAL);
1379
1380 pll_flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1381 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1382 freq_table, lock);
1383 if (IS_ERR(pll))
1384 return ERR_CAST(pll);
1385
1386 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1387 &tegra_clk_pllxc_ops);
1388 if (IS_ERR(clk))
1389 kfree(pll);
1390
1391 return clk;
1392}
1393
1394struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name,
1395 void __iomem *clk_base, void __iomem *pmc,
1396 unsigned long flags, unsigned long fixed_rate,
1397 struct tegra_clk_pll_params *pll_params,
1398 u32 pll_flags,
1399 struct tegra_clk_pll_freq_table *freq_table,
1400 spinlock_t *lock, unsigned long parent_rate)
1401{
1402 u32 val;
1403 struct tegra_clk_pll *pll;
1404 struct clk *clk;
1405
1406 pll_flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1407 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1408 freq_table, lock);
1409 if (IS_ERR(pll))
1410 return ERR_CAST(pll);
1411
1412 /* program minimum rate by default */
1413
1414 val = pll_readl_base(pll);
1415 if (val & PLL_BASE_ENABLE)
1416 WARN_ON(val & pll_params->iddq_bit_idx);
1417 else {
1418 int m;
1419
1420 m = _pll_fixed_mdiv(pll_params, parent_rate);
1421 val = m << PLL_BASE_DIVM_SHIFT;
1422 val |= (pll_params->vco_min / parent_rate)
1423 << PLL_BASE_DIVN_SHIFT;
1424 pll_writel_base(val, pll);
1425 }
1426
1427 /* disable lock override */
1428
1429 val = pll_readl_misc(pll);
1430 val &= ~BIT(29);
1431 pll_writel_misc(val, pll);
1432
1433 pll_flags |= TEGRA_PLL_LOCK_MISC;
1434 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1435 &tegra_clk_pllre_ops);
1436 if (IS_ERR(clk))
1437 kfree(pll);
1438
1439 return clk;
1440}
1441
1442struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name,
1443 void __iomem *clk_base, void __iomem *pmc,
1444 unsigned long flags, unsigned long fixed_rate,
1445 struct tegra_clk_pll_params *pll_params,
1446 u32 pll_flags,
1447 struct tegra_clk_pll_freq_table *freq_table,
1448 spinlock_t *lock)
1449{
1450 struct tegra_clk_pll *pll;
1451 struct clk *clk;
1452
1453 if (!pll_params->pdiv_tohw)
1454 return ERR_PTR(-EINVAL);
1455
1456 pll_flags |= TEGRA_PLL_BYPASS;
1457 pll_flags |= TEGRA_PLL_HAS_LOCK_ENABLE;
1458 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1459 freq_table, lock);
1460 if (IS_ERR(pll))
1461 return ERR_CAST(pll);
1462
1463 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1464 &tegra_clk_pllm_ops);
1465 if (IS_ERR(clk))
1466 kfree(pll);
1467
1468 return clk;
1469}
1470
1471struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name,
1472 void __iomem *clk_base, void __iomem *pmc,
1473 unsigned long flags, unsigned long fixed_rate,
1474 struct tegra_clk_pll_params *pll_params,
1475 u32 pll_flags,
1476 struct tegra_clk_pll_freq_table *freq_table,
1477 spinlock_t *lock)
1478{
1479 struct clk *parent, *clk;
1480 struct pdiv_map *p_tohw = pll_params->pdiv_tohw;
1481 struct tegra_clk_pll *pll;
1482 struct tegra_clk_pll_freq_table cfg;
1483 unsigned long parent_rate;
1484
1485 if (!p_tohw)
1486 return ERR_PTR(-EINVAL);
1487
1488 parent = __clk_lookup(parent_name);
1489 if (IS_ERR(parent)) {
1490 WARN(1, "parent clk %s of %s must be registered first\n",
1491 name, parent_name);
1492 return ERR_PTR(-EINVAL);
1493 }
1494
1495 pll_flags |= TEGRA_PLL_BYPASS;
1496 pll = _tegra_init_pll(clk_base, pmc, fixed_rate, pll_params, pll_flags,
1497 freq_table, lock);
1498 if (IS_ERR(pll))
1499 return ERR_CAST(pll);
1500
1501 parent_rate = __clk_get_rate(parent);
1502
1503 /*
1504 * Most of PLLC register fields are shadowed, and can not be read
1505 * directly from PLL h/w. Hence, actual PLLC boot state is unknown.
1506 * Initialize PLL to default state: disabled, reset; shadow registers
1507 * loaded with default parameters; dividers are preset for half of
1508 * minimum VCO rate (the latter assured that shadowed divider settings
1509 * are within supported range).
1510 */
1511
1512 cfg.m = _pll_fixed_mdiv(pll_params, parent_rate);
1513 cfg.n = cfg.m * pll_params->vco_min / parent_rate;
1514
1515 while (p_tohw->pdiv) {
1516 if (p_tohw->pdiv == 2) {
1517 cfg.p = p_tohw->hw_val;
1518 break;
1519 }
1520 p_tohw++;
1521 }
1522
1523 if (!p_tohw->pdiv) {
1524 WARN_ON(1);
1525 return ERR_PTR(-EINVAL);
1526 }
1527
1528 pll_writel_base(0, pll);
1529 _update_pll_mnp(pll, &cfg);
1530
1531 pll_writel_misc(PLLCX_MISC_DEFAULT, pll);
1532 pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll);
1533 pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll);
1534 pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll);
1535
1536 _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n);
1537
1538 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1539 &tegra_clk_pllc_ops);
1540 if (IS_ERR(clk))
1541 kfree(pll);
1542
1543 return clk;
1544}
1545
1546struct clk *tegra_clk_register_plle_tegra114(const char *name,
1547 const char *parent_name,
1548 void __iomem *clk_base, unsigned long flags,
1549 unsigned long fixed_rate,
1550 struct tegra_clk_pll_params *pll_params,
1551 struct tegra_clk_pll_freq_table *freq_table,
1552 spinlock_t *lock)
1553{
1554 struct tegra_clk_pll *pll;
1555 struct clk *clk;
1556 u32 val, val_aux;
1557
1558 pll = _tegra_init_pll(clk_base, NULL, fixed_rate, pll_params,
1559 TEGRA_PLL_HAS_LOCK_ENABLE, freq_table, lock);
1560 if (IS_ERR(pll))
1561 return ERR_CAST(pll);
1562
1563 /* ensure parent is set to pll_re_vco */
1564
1565 val = pll_readl_base(pll);
1566 val_aux = pll_readl(pll_params->aux_reg, pll);
1567
1568 if (val & PLL_BASE_ENABLE) {
1569 if (!(val_aux & PLLE_AUX_PLLRE_SEL))
1570 WARN(1, "pll_e enabled with unsupported parent %s\n",
1571 (val & PLLE_AUX_PLLP_SEL) ? "pllp_out0" : "pll_ref");
1572 } else {
1573 val_aux |= PLLE_AUX_PLLRE_SEL;
1574 pll_writel(val, pll_params->aux_reg, pll);
1575 }
1576
1577 clk = _tegra_clk_register_pll(pll, name, parent_name, flags,
1578 &tegra_clk_plle_tegra114_ops);
1579 if (IS_ERR(clk))
1580 kfree(pll);
1581
1582 return clk;
1583}
1584#endif