blob: 355a6d30480b529978cddf515df3ae49dff5478c [file] [log] [blame]
Vikram Mulukutla8810e342011-10-20 20:26:53 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/err.h>
19#include <linux/ctype.h>
20#include <linux/bitops.h>
21#include <linux/io.h>
22#include <linux/spinlock.h>
23#include <linux/delay.h>
24#include <linux/clk.h>
25
26#include <mach/clk.h>
27
28#include "clock.h"
29#include "clock-local2.h"
30
31/*
32 * When enabling/disabling a clock, check the halt bit up to this number
33 * number of times (with a 1 us delay in between) before continuing.
34 */
35#define HALT_CHECK_MAX_LOOPS 200
36/* For clock without halt checking, wait this long after enables/disables. */
37#define HALT_CHECK_DELAY_US 10
38
39/*
40 * When updating an RCG configuration, check the update bit up to this number
41 * number of times (with a 1 us delay in between) before continuing.
42 */
43#define UPDATE_CHECK_MAX_LOOPS 200
44
45DEFINE_SPINLOCK(local_clock_reg_lock);
46struct clk_freq_tbl rcg_dummy_freq = F_END;
47
48#define CMD_RCGR_REG(x) (*(x)->base + (x)->cmd_rcgr_reg)
49#define CFG_RCGR_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x4)
50#define M_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x8)
51#define N_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0xC)
52#define D_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x10)
53#define CBCR_REG(x) (*(x)->base + (x)->cbcr_reg)
54#define BCR_REG(x) (*(x)->base + (x)->bcr_reg)
55#define VOTE_REG(x) (*(x)->base + (x)->vote_reg)
56
57/*
58 * Important clock bit positions and masks
59 */
60#define CMD_RCGR_ROOT_ENABLE_BIT BIT(1)
61#define CBCR_BRANCH_ENABLE_BIT BIT(0)
62#define CBCR_BRANCH_OFF_BIT BIT(31)
63#define CMD_RCGR_CONFIG_UPDATE_BIT BIT(0)
64#define CMD_RCGR_ROOT_STATUS_BIT BIT(31)
65#define BCR_BLK_ARES_BIT BIT(0)
66#define CBCR_HW_CTL_BIT BIT(1)
67#define CFG_RCGR_DIV_MASK BM(4, 0)
68#define CFG_RCGR_SRC_SEL_MASK BM(10, 8)
69#define MND_MODE_MASK BM(13, 12)
70#define MND_DUAL_EDGE_MODE_BVAL BVAL(13, 12, 0x2)
71#define CMD_RCGR_CONFIG_DIRTY_MASK BM(7, 4)
72#define CBCR_BRANCH_CDIV_MASK BM(24, 16)
73#define CBCR_BRANCH_CDIV_MASKED(val) BVAL(24, 16, (val));
74
75enum branch_state {
76 BRANCH_ON,
77 BRANCH_OFF,
78};
79
80/*
81 * RCG functions
82 */
83
84/*
85 * Update an RCG with a new configuration. This may include a new M, N, or D
86 * value, source selection or pre-divider value.
87 *
88 */
89static void rcg_update_config(struct rcg_clk *rcg)
90{
91 u32 cmd_rcgr_regval, count;
92
93 cmd_rcgr_regval = readl_relaxed(CMD_RCGR_REG(rcg));
94 cmd_rcgr_regval |= CMD_RCGR_CONFIG_UPDATE_BIT;
95 writel_relaxed(cmd_rcgr_regval, CMD_RCGR_REG(rcg));
96
97 /* Wait for update to take effect */
98 for (count = UPDATE_CHECK_MAX_LOOPS; count > 0; count--) {
99 if (!(readl_relaxed(CMD_RCGR_REG(rcg)) &
100 CMD_RCGR_CONFIG_UPDATE_BIT))
101 return;
102 udelay(1);
103 }
104
105 WARN(count == 0, "%s: rcg didn't update its configuration.",
106 rcg->c.dbg_name);
107}
108
109/* RCG set rate function for clocks with Half Integer Dividers. */
110void set_rate_hid(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
111{
112 u32 cfg_regval;
113
114 cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg));
115 cfg_regval &= ~(CFG_RCGR_DIV_MASK | CFG_RCGR_SRC_SEL_MASK);
116 cfg_regval |= nf->div_src_val;
117 writel_relaxed(cfg_regval, CFG_RCGR_REG(rcg));
118
119 rcg_update_config(rcg);
120}
121
122/* RCG set rate function for clocks with MND & Half Integer Dividers. */
123void set_rate_mnd(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
124{
125 u32 cfg_regval;
126
127 writel_relaxed(nf->m_val, M_REG(rcg));
128 writel_relaxed(nf->n_val, N_REG(rcg));
129 writel_relaxed(nf->d_val, D_REG(rcg));
130
131 cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg));
132 cfg_regval &= ~(CFG_RCGR_DIV_MASK | CFG_RCGR_SRC_SEL_MASK);
133 cfg_regval |= nf->div_src_val;
134
135 /* Activate or disable the M/N:D divider as necessary */
136 cfg_regval &= ~MND_MODE_MASK;
137 if (nf->n_val != 0)
138 cfg_regval |= MND_DUAL_EDGE_MODE_BVAL;
139 writel_relaxed(cfg_regval, CFG_RCGR_REG(rcg));
140
141 rcg_update_config(rcg);
142}
143
144static int rcg_clk_enable(struct clk *c)
145{
146 struct rcg_clk *rcg = to_rcg_clk(c);
147
148 WARN(rcg->current_freq == &rcg_dummy_freq,
149 "Attempting to enable %s before setting its rate. "
150 "Set the rate first!\n", rcg->c.dbg_name);
151
152 return 0;
153}
154
155static int rcg_clk_set_rate(struct clk *c, unsigned long rate)
156{
157 struct clk_freq_tbl *cf, *nf;
158 struct rcg_clk *rcg = to_rcg_clk(c);
159 int rc = 0;
160 unsigned long flags;
161
162 for (nf = rcg->freq_tbl; nf->freq_hz != FREQ_END
163 && nf->freq_hz != rate; nf++)
164 ;
165
166 if (nf->freq_hz == FREQ_END)
167 return -EINVAL;
168
169 /* Check if frequency is actually changed. */
170 cf = rcg->current_freq;
171 if (nf == cf)
172 return 0;
173
174 if (rcg->c.count) {
175 /* TODO: Modify to use the prepare API */
176 /* Enable source clock dependency for the new freq. */
177 rc = clk_enable(nf->src_clk);
178 if (rc)
179 goto out;
180 }
181
182 BUG_ON(!rcg->set_rate);
183
184 spin_lock_irqsave(&local_clock_reg_lock, flags);
185
186 /* Perform clock-specific frequency switch operations. */
187 rcg->set_rate(rcg, nf);
188
189 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
190
191 /* Release source requirements of the old freq. */
192 if (rcg->c.count)
193 clk_disable(cf->src_clk);
194
195 rcg->current_freq = nf;
196out:
197 return rc;
198}
199
200/* Return a supported rate that's at least the specified rate. */
201static long rcg_clk_round_rate(struct clk *c, unsigned long rate)
202{
203 struct rcg_clk *rcg = to_rcg_clk(c);
204 struct clk_freq_tbl *f;
205
206 for (f = rcg->freq_tbl; f->freq_hz != FREQ_END; f++)
207 if (f->freq_hz >= rate)
208 return f->freq_hz;
209
210 return -EPERM;
211}
212
213/* Return the nth supported frequency for a given clock. */
214static int rcg_clk_list_rate(struct clk *c, unsigned n)
215{
216 struct rcg_clk *rcg = to_rcg_clk(c);
217
218 if (!rcg->freq_tbl || rcg->freq_tbl->freq_hz == FREQ_END)
219 return -ENXIO;
220
221 return (rcg->freq_tbl + n)->freq_hz;
222}
223
224static struct clk *rcg_clk_get_parent(struct clk *c)
225{
226 return to_rcg_clk(c)->current_freq->src_clk;
227}
228
229static enum handoff _rcg_clk_handoff(struct rcg_clk *rcg, int has_mnd)
230{
231 u32 n_regval = 0, m_regval = 0, d_regval = 0;
232 u32 cfg_regval;
233 struct clk_freq_tbl *freq;
234 u32 cmd_rcgr_regval;
235
236 /* Is the root enabled? */
237 cmd_rcgr_regval = readl_relaxed(CMD_RCGR_REG(rcg));
238 if ((cmd_rcgr_regval & CMD_RCGR_ROOT_STATUS_BIT))
239 return HANDOFF_DISABLED_CLK;
240
241 /* Is there a pending configuration? */
242 if (cmd_rcgr_regval & CMD_RCGR_CONFIG_DIRTY_MASK)
243 return HANDOFF_UNKNOWN_RATE;
244
245 /* Get values of m, n, d, div and src_sel registers. */
246 if (has_mnd) {
247 m_regval = readl_relaxed(M_REG(rcg));
248 n_regval = readl_relaxed(N_REG(rcg));
249 d_regval = readl_relaxed(D_REG(rcg));
250
251 /*
252 * The n and d values stored in the frequency tables are sign
253 * extended to 32 bits. The n and d values in the registers are
254 * sign extended to 8 or 16 bits. Sign extend the values read
255 * from the registers so that they can be compared to the
256 * values in the frequency tables.
257 */
258 n_regval |= (n_regval >> 8) ? BM(31, 16) : BM(31, 8);
259 d_regval |= (d_regval >> 8) ? BM(31, 16) : BM(31, 8);
260 }
261
262 cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg));
263 cfg_regval &= CFG_RCGR_SRC_SEL_MASK | CFG_RCGR_DIV_MASK
264 | MND_MODE_MASK;
265
266 /* If mnd counter is present, check if it's in use. */
267 has_mnd = (has_mnd) &&
268 ((cfg_regval & MND_MODE_MASK) == MND_DUAL_EDGE_MODE_BVAL);
269
270 /*
271 * Clear out the mn counter mode bits since we now want to compare only
272 * the source mux selection and pre-divider values in the registers.
273 */
274 cfg_regval &= ~MND_MODE_MASK;
275
276 /* Figure out what rate the rcg is running at */
277 for (freq = rcg->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
278 if (freq->div_src_val != cfg_regval)
279 continue;
280 if (has_mnd) {
281 if (freq->m_val != m_regval)
282 continue;
283 if (freq->n_val != n_regval)
284 continue;
285 if (freq->d_val != d_regval)
286 continue;
287 }
288 pr_info("%s rate=%lu\n", rcg->c.dbg_name, freq->freq_hz);
289 break;
290 }
291
292 /* No known frequency found */
293 if (freq->freq_hz == FREQ_END)
294 return HANDOFF_UNKNOWN_RATE;
295
296 rcg->current_freq = freq;
297 rcg->c.rate = freq->freq_hz;
298
299 return HANDOFF_ENABLED_CLK;
300}
301
302static enum handoff rcg_mnd_clk_handoff(struct clk *c)
303{
304 return _rcg_clk_handoff(to_rcg_clk(c), 1);
305}
306
307static enum handoff rcg_clk_handoff(struct clk *c)
308{
309 return _rcg_clk_handoff(to_rcg_clk(c), 0);
310}
311
312/*
313 * Branch clock functions
314 */
315static void branch_clk_halt_check(u32 halt_check, const char *clk_name,
316 void __iomem *cbcr_reg,
317 enum branch_state br_status)
318{
Vikram Mulukutla86b9fa62012-05-02 16:39:14 -0700319 char *status_str = (br_status == BRANCH_ON) ? "off" : "on";
Vikram Mulukutla8810e342011-10-20 20:26:53 -0700320
321 /*
322 * Use a memory barrier since some halt status registers are
323 * not within the same 1K segment as the branch/root enable
324 * registers. It's also needed in the udelay() case to ensure
325 * the delay starts after the branch disable.
326 */
327 mb();
328
329 if (halt_check == DELAY || halt_check == HALT_VOTED) {
330 udelay(HALT_CHECK_DELAY_US);
331 } else if (halt_check == HALT) {
332 int count;
333 for (count = HALT_CHECK_MAX_LOOPS; count > 0; count--) {
334 if (br_status == BRANCH_ON
335 && !(readl_relaxed(cbcr_reg)
336 & CBCR_BRANCH_OFF_BIT))
337 return;
338 if (br_status == BRANCH_OFF
339 && (readl_relaxed(cbcr_reg)
340 & CBCR_BRANCH_OFF_BIT))
341 return;
342 udelay(1);
343 }
344 WARN(count == 0, "%s status stuck %s", clk_name, status_str);
345 }
346}
347
348static int branch_clk_enable(struct clk *c)
349{
350 unsigned long flags;
351 u32 cbcr_val;
352 struct branch_clk *branch = to_branch_clk(c);
353
354 spin_lock_irqsave(&local_clock_reg_lock, flags);
355 cbcr_val = readl_relaxed(CBCR_REG(branch));
356 cbcr_val |= CBCR_BRANCH_ENABLE_BIT;
357 writel_relaxed(cbcr_val, CBCR_REG(branch));
358 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
359
360 /* Wait for clock to enable before continuing. */
361 branch_clk_halt_check(branch->halt_check, branch->c.dbg_name,
362 CBCR_REG(branch), BRANCH_ON);
363
364 return 0;
365}
366
367static void branch_clk_disable(struct clk *c)
368{
369 unsigned long flags;
370 struct branch_clk *branch = to_branch_clk(c);
371 u32 reg_val;
372
373 spin_lock_irqsave(&local_clock_reg_lock, flags);
374 reg_val = readl_relaxed(CBCR_REG(branch));
375 reg_val &= ~CBCR_BRANCH_ENABLE_BIT;
376 writel_relaxed(reg_val, CBCR_REG(branch));
377 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
378
379 /* Wait for clock to disable before continuing. */
380 branch_clk_halt_check(branch->halt_check, branch->c.dbg_name,
381 CBCR_REG(branch), BRANCH_OFF);
382}
383
384static int branch_cdiv_set_rate(struct branch_clk *branch, unsigned long rate)
385{
386 unsigned long flags;
387 u32 regval;
388
389 if (rate > branch->max_div)
390 return -EINVAL;
391
392 spin_lock_irqsave(&local_clock_reg_lock, flags);
393 regval = readl_relaxed(CBCR_REG(branch));
394 regval &= ~CBCR_BRANCH_CDIV_MASK;
395 regval |= CBCR_BRANCH_CDIV_MASKED(rate);
396 writel_relaxed(regval, CBCR_REG(branch));
397 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
398
399 return 0;
400}
401
402static int branch_clk_set_rate(struct clk *c, unsigned long rate)
403{
404 struct branch_clk *branch = to_branch_clk(c);
405
406 if (branch->max_div)
407 return branch_cdiv_set_rate(branch, rate);
408
409 if (!branch->has_sibling)
410 return clk_set_rate(branch->parent, rate);
411
412 return -EPERM;
413}
414
415static unsigned long branch_clk_get_rate(struct clk *c)
416{
417 struct branch_clk *branch = to_branch_clk(c);
418
419 if (branch->max_div)
420 return branch->c.rate;
421
422 if (!branch->has_sibling)
423 return clk_get_rate(branch->parent);
424
425 return 0;
426}
427
428static struct clk *branch_clk_get_parent(struct clk *c)
429{
430 return to_branch_clk(c)->parent;
431}
432
433static int branch_clk_list_rate(struct clk *c, unsigned n)
434{
435 struct branch_clk *branch = to_branch_clk(c);
436
437 if (branch->has_sibling == 1)
438 return -ENXIO;
439
440 if (branch->parent)
441 return rcg_clk_list_rate(branch->parent, n);
442 else
443 return 0;
444}
445
446static enum handoff branch_clk_handoff(struct clk *c)
447{
448 struct branch_clk *branch = to_branch_clk(c);
449 u32 cbcr_regval;
450
451 cbcr_regval = readl_relaxed(CBCR_REG(branch));
452 if ((cbcr_regval & CBCR_BRANCH_OFF_BIT))
453 return HANDOFF_DISABLED_CLK;
454 pr_info("%s enabled.\n", branch->c.dbg_name);
455
456 if (branch->parent) {
457 if (branch->parent->ops->handoff)
458 return branch->parent->ops->handoff(branch->parent);
459 }
460
461 return HANDOFF_ENABLED_CLK;
462}
463
464static int __branch_clk_reset(void __iomem *bcr_reg,
465 enum clk_reset_action action)
466{
467 int ret = 0;
468 unsigned long flags;
469 u32 reg_val;
470
471 if (!bcr_reg)
472 return -EPERM;
473
474 spin_lock_irqsave(&local_clock_reg_lock, flags);
475 reg_val = readl_relaxed(bcr_reg);
476 switch (action) {
477 case CLK_RESET_ASSERT:
478 reg_val |= BCR_BLK_ARES_BIT;
479 break;
480 case CLK_RESET_DEASSERT:
481 reg_val &= ~BCR_BLK_ARES_BIT;
482 break;
483 default:
484 ret = -EINVAL;
485 }
486 writel_relaxed(reg_val, bcr_reg);
487 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
488
489 /* Make sure write is issued before returning. */
490 mb();
491
492 return ret;
493}
494
495static int branch_clk_reset(struct clk *c, enum clk_reset_action action)
496{
497 struct branch_clk *branch = to_branch_clk(c);
498 return __branch_clk_reset(BCR_REG(branch), action);
499}
500
501/*
502 * Voteable clock functions
503 */
504static int local_vote_clk_reset(struct clk *c, enum clk_reset_action action)
505{
Vikram Mulukutla27784c02012-06-06 13:37:36 -0700506 struct local_vote_clk *vclk = to_local_vote_clk(c);
Vikram Mulukutla8810e342011-10-20 20:26:53 -0700507 return __branch_clk_reset(BCR_REG(vclk), action);
508}
509
510static int local_vote_clk_enable(struct clk *c)
511{
512 unsigned long flags;
513 u32 ena;
514 struct local_vote_clk *vclk = to_local_vote_clk(c);
515
516 spin_lock_irqsave(&local_clock_reg_lock, flags);
517 ena = readl_relaxed(VOTE_REG(vclk));
518 ena |= vclk->en_mask;
519 writel_relaxed(ena, VOTE_REG(vclk));
520 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
521
522 branch_clk_halt_check(vclk->halt_check, c->dbg_name, CBCR_REG(vclk),
523 BRANCH_ON);
524
525 return 0;
526}
527
528static void local_vote_clk_disable(struct clk *c)
529{
530 unsigned long flags;
531 u32 ena;
532 struct local_vote_clk *vclk = to_local_vote_clk(c);
533
534 spin_lock_irqsave(&local_clock_reg_lock, flags);
535 ena = readl_relaxed(VOTE_REG(vclk));
536 ena &= ~vclk->en_mask;
537 writel_relaxed(ena, VOTE_REG(vclk));
538 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
539}
540
541static enum handoff local_vote_clk_handoff(struct clk *c)
542{
543 struct local_vote_clk *vclk = to_local_vote_clk(c);
544 u32 vote_regval;
545
546 /* Is the branch voted on by apps? */
547 vote_regval = readl_relaxed(VOTE_REG(vclk));
548 if (!(vote_regval & vclk->en_mask))
549 return HANDOFF_DISABLED_CLK;
550 pr_info("%s enabled.\n", vclk->c.dbg_name);
551
552 return HANDOFF_ENABLED_CLK;
553}
554
555struct clk_ops clk_ops_rcg = {
556 .enable = rcg_clk_enable,
557 .set_rate = rcg_clk_set_rate,
558 .list_rate = rcg_clk_list_rate,
559 .round_rate = rcg_clk_round_rate,
560 .get_parent = rcg_clk_get_parent,
561 .handoff = rcg_clk_handoff,
562};
563
564struct clk_ops clk_ops_rcg_mnd = {
565 .enable = rcg_clk_enable,
566 .set_rate = rcg_clk_set_rate,
567 .list_rate = rcg_clk_list_rate,
568 .round_rate = rcg_clk_round_rate,
569 .get_parent = rcg_clk_get_parent,
570 .handoff = rcg_mnd_clk_handoff,
571};
572
573struct clk_ops clk_ops_branch = {
574 .enable = branch_clk_enable,
575 .disable = branch_clk_disable,
576 .auto_off = branch_clk_disable,
577 .set_rate = branch_clk_set_rate,
578 .get_rate = branch_clk_get_rate,
579 .list_rate = branch_clk_list_rate,
580 .reset = branch_clk_reset,
581 .get_parent = branch_clk_get_parent,
582 .handoff = branch_clk_handoff,
583};
584
585struct clk_ops clk_ops_vote = {
586 .enable = local_vote_clk_enable,
587 .disable = local_vote_clk_disable,
588 .auto_off = local_vote_clk_disable,
589 .reset = local_vote_clk_reset,
590 .handoff = local_vote_clk_handoff,
591};