blob: 502281183d415938de48cc937407f21427ab7933 [file] [log] [blame]
Pankaj Kumar3912c982011-12-07 16:59:03 +05301/*
2 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/remote_spinlock.h>
19
Vikram Mulukutla681d8682012-03-09 23:56:20 -080020#include <mach/scm-io.h>
Pankaj Kumar3912c982011-12-07 16:59:03 +053021#include <mach/msm_iomap.h>
22
23#include "clock.h"
24#include "clock-pll.h"
25#include "smd_private.h"
26
Vikram Mulukutla681d8682012-03-09 23:56:20 -080027#ifdef CONFIG_MSM_SECURE_IO
28#undef readl_relaxed
29#undef writel_relaxed
30#define readl_relaxed secure_readl
31#define writel_relaxed secure_writel
32#endif
33
34#define PLL_OUTCTRL BIT(0)
35#define PLL_BYPASSNL BIT(1)
36#define PLL_RESET_N BIT(2)
37#define PLL_MODE_MASK BM(3, 0)
38
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -070039#define PLL_EN_REG(x) ((x)->base ? (*(x)->base + (u32)((x)->en_reg)) : \
40 ((x)->en_reg))
41#define PLL_STATUS_REG(x) ((x)->base ? (*(x)->base + (u32)((x)->status_reg)) : \
42 ((x)->status_reg))
43#define PLL_MODE_REG(x) ((x)->base ? (*(x)->base + (u32)((x)->mode_reg)) : \
44 ((x)->mode_reg))
45
Vikram Mulukutla681d8682012-03-09 23:56:20 -080046static DEFINE_SPINLOCK(pll_reg_lock);
47
Vikram Mulukutla7b953b12012-04-09 13:56:26 -070048#define ENABLE_WAIT_MAX_LOOPS 200
49
Vikram Mulukutla681d8682012-03-09 23:56:20 -080050int pll_vote_clk_enable(struct clk *clk)
51{
Vikram Mulukutla7b953b12012-04-09 13:56:26 -070052 u32 ena, count;
Vikram Mulukutla681d8682012-03-09 23:56:20 -080053 unsigned long flags;
54 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
55
56 spin_lock_irqsave(&pll_reg_lock, flags);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -070057 ena = readl_relaxed(PLL_EN_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -080058 ena |= pll->en_mask;
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -070059 writel_relaxed(ena, PLL_EN_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -080060 spin_unlock_irqrestore(&pll_reg_lock, flags);
61
Vikram Mulukutla7b953b12012-04-09 13:56:26 -070062 /*
63 * Use a memory barrier since some PLL status registers are
64 * not within the same 1K segment as the voting registers.
65 */
66 mb();
Vikram Mulukutla681d8682012-03-09 23:56:20 -080067
Vikram Mulukutla7b953b12012-04-09 13:56:26 -070068 /* Wait for pll to enable. */
69 for (count = ENABLE_WAIT_MAX_LOOPS; count > 0; count--) {
70 if (readl_relaxed(pll->status_reg) & pll->status_mask)
71 return 0;
72 udelay(1);
73 }
74
75 WARN("PLL %s didn't enable after voting for it!\n", clk->dbg_name);
76
77 return -ETIMEDOUT;
Vikram Mulukutla681d8682012-03-09 23:56:20 -080078}
79
80void pll_vote_clk_disable(struct clk *clk)
81{
82 u32 ena;
83 unsigned long flags;
84 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
85
86 spin_lock_irqsave(&pll_reg_lock, flags);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -070087 ena = readl_relaxed(PLL_EN_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -080088 ena &= ~(pll->en_mask);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -070089 writel_relaxed(ena, PLL_EN_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -080090 spin_unlock_irqrestore(&pll_reg_lock, flags);
91}
92
93struct clk *pll_vote_clk_get_parent(struct clk *clk)
94{
95 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
96 return pll->parent;
97}
98
99int pll_vote_clk_is_enabled(struct clk *clk)
100{
101 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700102 return !!(readl_relaxed(PLL_STATUS_REG(pll)) & pll->status_mask);
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800103}
104
105struct clk_ops clk_ops_pll_vote = {
106 .enable = pll_vote_clk_enable,
107 .disable = pll_vote_clk_disable,
108 .auto_off = pll_vote_clk_disable,
109 .is_enabled = pll_vote_clk_is_enabled,
110 .get_parent = pll_vote_clk_get_parent,
111};
112
113static void __pll_clk_enable_reg(void __iomem *mode_reg)
114{
115 u32 mode = readl_relaxed(mode_reg);
116 /* Disable PLL bypass mode. */
117 mode |= PLL_BYPASSNL;
118 writel_relaxed(mode, mode_reg);
119
120 /*
121 * H/W requires a 5us delay between disabling the bypass and
122 * de-asserting the reset. Delay 10us just to be safe.
123 */
124 mb();
125 udelay(10);
126
127 /* De-assert active-low PLL reset. */
128 mode |= PLL_RESET_N;
129 writel_relaxed(mode, mode_reg);
130
131 /* Wait until PLL is locked. */
132 mb();
133 udelay(50);
134
135 /* Enable PLL output. */
136 mode |= PLL_OUTCTRL;
137 writel_relaxed(mode, mode_reg);
138
139 /* Ensure that the write above goes through before returning. */
140 mb();
141}
142
143static int local_pll_clk_enable(struct clk *clk)
144{
145 unsigned long flags;
146 struct pll_clk *pll = to_pll_clk(clk);
147
148 spin_lock_irqsave(&pll_reg_lock, flags);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700149 __pll_clk_enable_reg(PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800150 spin_unlock_irqrestore(&pll_reg_lock, flags);
151
152 return 0;
153}
154
155static void __pll_clk_disable_reg(void __iomem *mode_reg)
156{
157 u32 mode = readl_relaxed(mode_reg);
158 mode &= ~PLL_MODE_MASK;
159 writel_relaxed(mode, mode_reg);
160}
161
162static void local_pll_clk_disable(struct clk *clk)
163{
164 unsigned long flags;
165 struct pll_clk *pll = to_pll_clk(clk);
166
167 /*
168 * Disable the PLL output, disable test mode, enable
169 * the bypass mode, and assert the reset.
170 */
171 spin_lock_irqsave(&pll_reg_lock, flags);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700172 __pll_clk_disable_reg(PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800173 spin_unlock_irqrestore(&pll_reg_lock, flags);
174}
175
176static struct clk *local_pll_clk_get_parent(struct clk *clk)
177{
178 struct pll_clk *pll = to_pll_clk(clk);
179 return pll->parent;
180}
181
182int sr_pll_clk_enable(struct clk *clk)
183{
184 u32 mode;
185 unsigned long flags;
186 struct pll_clk *pll = to_pll_clk(clk);
187
188 spin_lock_irqsave(&pll_reg_lock, flags);
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700189 mode = readl_relaxed(PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800190 /* De-assert active-low PLL reset. */
191 mode |= PLL_RESET_N;
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700192 writel_relaxed(mode, PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800193
194 /*
195 * H/W requires a 5us delay between disabling the bypass and
196 * de-asserting the reset. Delay 10us just to be safe.
197 */
198 mb();
199 udelay(10);
200
201 /* Disable PLL bypass mode. */
202 mode |= PLL_BYPASSNL;
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700203 writel_relaxed(mode, PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800204
205 /* Wait until PLL is locked. */
206 mb();
207 udelay(60);
208
209 /* Enable PLL output. */
210 mode |= PLL_OUTCTRL;
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700211 writel_relaxed(mode, PLL_MODE_REG(pll));
Vikram Mulukutla681d8682012-03-09 23:56:20 -0800212
213 /* Ensure that the write above goes through before returning. */
214 mb();
215
216 spin_unlock_irqrestore(&pll_reg_lock, flags);
217
218 return 0;
219}
220
221struct clk_ops clk_ops_local_pll = {
222 .enable = local_pll_clk_enable,
223 .disable = local_pll_clk_disable,
224 .auto_off = local_pll_clk_disable,
225 .get_parent = local_pll_clk_get_parent,
226};
227
Pankaj Kumar3912c982011-12-07 16:59:03 +0530228struct pll_rate {
229 unsigned int lvalue;
230 unsigned long rate;
231};
232
233static struct pll_rate pll_l_rate[] = {
234 {10, 196000000},
235 {12, 245760000},
236 {30, 589820000},
237 {38, 737280000},
238 {41, 800000000},
239 {50, 960000000},
240 {52, 1008000000},
241 {62, 1200000000},
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530242 {63, 1209600000},
Pankaj Kumar3912c982011-12-07 16:59:03 +0530243 {0, 0},
244};
245
246#define PLL_BASE 7
247
248struct shared_pll_control {
249 uint32_t version;
250 struct {
251 /*
252 * Denotes if the PLL is ON. Technically, this can be read
253 * directly from the PLL registers, but this feild is here,
254 * so let's use it.
255 */
256 uint32_t on;
257 /*
258 * One bit for each processor core. The application processor
259 * is allocated bit position 1. All other bits should be
260 * considered as votes from other processors.
261 */
262 uint32_t votes;
263 } pll[PLL_BASE + PLL_END];
264};
265
266static remote_spinlock_t pll_lock;
267static struct shared_pll_control *pll_control;
268
269void __init msm_shared_pll_control_init(void)
270{
271#define PLL_REMOTE_SPINLOCK_ID "S:7"
272 unsigned smem_size;
273
274 remote_spin_lock_init(&pll_lock, PLL_REMOTE_SPINLOCK_ID);
275
276 pll_control = smem_get_entry(SMEM_CLKREGIM_SOURCES, &smem_size);
277 if (!pll_control) {
278 pr_err("Can't find shared PLL control data structure!\n");
279 BUG();
280 /*
281 * There might be more PLLs than what the application processor knows
282 * about. But the index used for each PLL is guaranteed to remain the
283 * same.
284 */
285 } else if (smem_size < sizeof(struct shared_pll_control)) {
286 pr_err("Shared PLL control data"
287 "structure too small!\n");
288 BUG();
289 } else if (pll_control->version != 0xCCEE0001) {
290 pr_err("Shared PLL control version mismatch!\n");
291 BUG();
292 } else {
293 pr_info("Shared PLL control available.\n");
294 return;
295 }
296
297}
298
Pankaj Kumar3912c982011-12-07 16:59:03 +0530299static int pll_clk_enable(struct clk *clk)
300{
301 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
302 unsigned int pll_id = pll->id;
303
304 remote_spin_lock(&pll_lock);
305
306 pll_control->pll[PLL_BASE + pll_id].votes |= BIT(1);
307 if (!pll_control->pll[PLL_BASE + pll_id].on) {
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700308 __pll_clk_enable_reg(PLL_MODE_REG(pll));
Pankaj Kumar3912c982011-12-07 16:59:03 +0530309 pll_control->pll[PLL_BASE + pll_id].on = 1;
310 }
311
312 remote_spin_unlock(&pll_lock);
313 return 0;
314}
315
316static void pll_clk_disable(struct clk *clk)
317{
318 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
319 unsigned int pll_id = pll->id;
320
321 remote_spin_lock(&pll_lock);
322
323 pll_control->pll[PLL_BASE + pll_id].votes &= ~BIT(1);
324 if (pll_control->pll[PLL_BASE + pll_id].on
325 && !pll_control->pll[PLL_BASE + pll_id].votes) {
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700326 __pll_clk_disable_reg(PLL_MODE_REG(pll));
Pankaj Kumar3912c982011-12-07 16:59:03 +0530327 pll_control->pll[PLL_BASE + pll_id].on = 0;
328 }
329
330 remote_spin_unlock(&pll_lock);
331}
332
333static int pll_clk_is_enabled(struct clk *clk)
334{
335 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
336
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700337 return readl_relaxed(PLL_MODE_REG(pll)) & BIT(0);
Pankaj Kumar3912c982011-12-07 16:59:03 +0530338}
339
Matt Wagantalla15833b2012-04-03 11:00:56 -0700340static enum handoff pll_clk_handoff(struct clk *clk)
Pankaj Kumar3912c982011-12-07 16:59:03 +0530341{
342 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
343 unsigned int pll_lval;
344 struct pll_rate *l;
345
346 /*
347 * Wait for the PLLs to be initialized and then read their frequency.
348 */
349 do {
Vikram Mulukutla4d6caa82012-04-10 18:04:55 -0700350 pll_lval = readl_relaxed(PLL_MODE_REG(pll) + 4) & 0x3ff;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530351 cpu_relax();
352 udelay(50);
353 } while (pll_lval == 0);
354
355 /* Convert PLL L values to PLL Output rate */
356 for (l = pll_l_rate; l->rate != 0; l++) {
357 if (l->lvalue == pll_lval) {
358 clk->rate = l->rate;
359 break;
360 }
361 }
362
363 if (!clk->rate) {
364 pr_crit("Unknown PLL's L value!\n");
365 BUG();
366 }
367
Matt Wagantalla15833b2012-04-03 11:00:56 -0700368 return HANDOFF_ENABLED_CLK;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530369}
370
371struct clk_ops clk_pll_ops = {
372 .enable = pll_clk_enable,
373 .disable = pll_clk_disable,
374 .handoff = pll_clk_handoff,
Pankaj Kumar3912c982011-12-07 16:59:03 +0530375 .is_enabled = pll_clk_is_enabled,
376};