blob: b7542c828e72709d7ccf737c581fe4000ea00935 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2009-2011, 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/msm_iomap.h>
27#include <mach/clk.h>
28#include <mach/scm-io.h>
29
30#include "clock.h"
31#include "clock-local.h"
32
33#ifdef CONFIG_MSM_SECURE_IO
34#undef readl_relaxed
35#undef writel_relaxed
36#define readl_relaxed secure_readl
37#define writel_relaxed secure_writel
38#endif
39
40/*
41 * When enabling/disabling a clock, check the halt bit up to this number
42 * number of times (with a 1 us delay in between) before continuing.
43 */
Stephen Boyd138da0e2011-08-05 13:25:57 -070044#define HALT_CHECK_MAX_LOOPS 200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045/* For clock without halt checking, wait this long after enables/disables. */
46#define HALT_CHECK_DELAY_US 10
47
48DEFINE_SPINLOCK(local_clock_reg_lock);
49struct clk_freq_tbl local_dummy_freq = F_END;
50
51unsigned local_sys_vdd_votes[NUM_SYS_VDD_LEVELS];
52static DEFINE_SPINLOCK(sys_vdd_vote_lock);
53
54/*
55 * Common Set-Rate Functions
56 */
57
58/* For clocks with MND dividers. */
59void set_rate_mnd(struct rcg_clk *clk, struct clk_freq_tbl *nf)
60{
61 uint32_t ns_reg_val, ctl_reg_val;
62
63 /* Assert MND reset. */
64 ns_reg_val = readl_relaxed(clk->ns_reg);
65 ns_reg_val |= BIT(7);
66 writel_relaxed(ns_reg_val, clk->ns_reg);
67
68 /* Program M and D values. */
69 writel_relaxed(nf->md_val, clk->md_reg);
70
71 /* If the clock has a separate CC register, program it. */
72 if (clk->ns_reg != clk->b.ctl_reg) {
73 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
74 ctl_reg_val &= ~(clk->ctl_mask);
75 ctl_reg_val |= nf->ctl_val;
76 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
77 }
78
79 /* Deassert MND reset. */
80 ns_reg_val &= ~BIT(7);
81 writel_relaxed(ns_reg_val, clk->ns_reg);
82}
83
84void set_rate_nop(struct rcg_clk *clk, struct clk_freq_tbl *nf)
85{
86 /*
87 * Nothing to do for fixed-rate or integer-divider clocks. Any settings
88 * in NS registers are applied in the enable path, since power can be
89 * saved by leaving an un-clocked or slowly-clocked source selected
90 * until the clock is enabled.
91 */
92}
93
94void set_rate_mnd_8(struct rcg_clk *clk, struct clk_freq_tbl *nf)
95{
96 uint32_t ctl_reg_val;
97
98 /* Assert MND reset. */
99 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
100 ctl_reg_val |= BIT(8);
101 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
102
103 /* Program M and D values. */
104 writel_relaxed(nf->md_val, clk->md_reg);
105
106 /* Program MN counter Enable and Mode. */
107 ctl_reg_val &= ~(clk->ctl_mask);
108 ctl_reg_val |= nf->ctl_val;
109 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
110
111 /* Deassert MND reset. */
112 ctl_reg_val &= ~BIT(8);
113 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
114}
115
116void set_rate_mnd_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf)
117{
118 struct bank_masks *banks = clk->bank_masks;
119 const struct bank_mask_info *new_bank_masks;
120 const struct bank_mask_info *old_bank_masks;
121 uint32_t ns_reg_val, ctl_reg_val;
122 uint32_t bank_sel;
123
124 /*
125 * Determine active bank and program the other one. If the clock is
126 * off, program the active bank since bank switching won't work if
127 * both banks aren't running.
128 */
129 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
130 bank_sel = !!(ctl_reg_val & banks->bank_sel_mask);
131 /* If clock isn't running, don't switch banks. */
132 bank_sel ^= (!clk->enabled || clk->current_freq->freq_hz == 0);
133 if (bank_sel == 0) {
134 new_bank_masks = &banks->bank1_mask;
135 old_bank_masks = &banks->bank0_mask;
136 } else {
137 new_bank_masks = &banks->bank0_mask;
138 old_bank_masks = &banks->bank1_mask;
139 }
140
141 ns_reg_val = readl_relaxed(clk->ns_reg);
142
143 /* Assert bank MND reset. */
144 ns_reg_val |= new_bank_masks->rst_mask;
145 writel_relaxed(ns_reg_val, clk->ns_reg);
146
147 /*
148 * Program NS only if the clock is enabled, since the NS will be set
149 * as part of the enable procedure and should remain with a low-power
150 * MUX input selected until then.
151 */
152 if (clk->enabled) {
153 ns_reg_val &= ~(new_bank_masks->ns_mask);
154 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
155 writel_relaxed(ns_reg_val, clk->ns_reg);
156 }
157
158 writel_relaxed(nf->md_val, new_bank_masks->md_reg);
159
160 /* Enable counter only if clock is enabled. */
161 if (clk->enabled)
162 ctl_reg_val |= new_bank_masks->mnd_en_mask;
163 else
164 ctl_reg_val &= ~(new_bank_masks->mnd_en_mask);
165
166 ctl_reg_val &= ~(new_bank_masks->mode_mask);
167 ctl_reg_val |= (nf->ctl_val & new_bank_masks->mode_mask);
168 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
169
170 /* Deassert bank MND reset. */
171 ns_reg_val &= ~(new_bank_masks->rst_mask);
172 writel_relaxed(ns_reg_val, clk->ns_reg);
173
174 /*
175 * Switch to the new bank if clock is running. If it isn't, then
176 * no switch is necessary since we programmed the active bank.
177 */
178 if (clk->enabled && clk->current_freq->freq_hz) {
179 ctl_reg_val ^= banks->bank_sel_mask;
180 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
181 /*
182 * Wait at least 6 cycles of slowest bank's clock
183 * for the glitch-free MUX to fully switch sources.
184 */
185 mb();
186 udelay(1);
187
188 /* Disable old bank's MN counter. */
189 ctl_reg_val &= ~(old_bank_masks->mnd_en_mask);
190 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
191
192 /* Program old bank to a low-power source and divider. */
193 ns_reg_val &= ~(old_bank_masks->ns_mask);
194 ns_reg_val |= (clk->freq_tbl->ns_val & old_bank_masks->ns_mask);
195 writel_relaxed(ns_reg_val, clk->ns_reg);
196 }
197
198 /*
199 * If this freq requires the MN counter to be enabled,
200 * update the enable mask to match the current bank.
201 */
202 if (nf->mnd_en_mask)
203 nf->mnd_en_mask = new_bank_masks->mnd_en_mask;
204 /* Update the NS mask to match the current bank. */
205 clk->ns_mask = new_bank_masks->ns_mask;
206}
207
208void set_rate_div_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf)
209{
210 struct bank_masks *banks = clk->bank_masks;
211 const struct bank_mask_info *new_bank_masks;
212 const struct bank_mask_info *old_bank_masks;
213 uint32_t ns_reg_val, bank_sel;
214
215 /*
216 * Determine active bank and program the other one. If the clock is
217 * off, program the active bank since bank switching won't work if
218 * both banks aren't running.
219 */
220 ns_reg_val = readl_relaxed(clk->ns_reg);
221 bank_sel = !!(ns_reg_val & banks->bank_sel_mask);
222 /* If clock isn't running, don't switch banks. */
223 bank_sel ^= (!clk->enabled || clk->current_freq->freq_hz == 0);
224 if (bank_sel == 0) {
225 new_bank_masks = &banks->bank1_mask;
226 old_bank_masks = &banks->bank0_mask;
227 } else {
228 new_bank_masks = &banks->bank0_mask;
229 old_bank_masks = &banks->bank1_mask;
230 }
231
232 /*
233 * Program NS only if the clock is enabled, since the NS will be set
234 * as part of the enable procedure and should remain with a low-power
235 * MUX input selected until then.
236 */
237 if (clk->enabled) {
238 ns_reg_val &= ~(new_bank_masks->ns_mask);
239 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
240 writel_relaxed(ns_reg_val, clk->ns_reg);
241 }
242
243 /*
244 * Switch to the new bank if clock is running. If it isn't, then
245 * no switch is necessary since we programmed the active bank.
246 */
247 if (clk->enabled && clk->current_freq->freq_hz) {
248 ns_reg_val ^= banks->bank_sel_mask;
249 writel_relaxed(ns_reg_val, clk->ns_reg);
250 /*
251 * Wait at least 6 cycles of slowest bank's clock
252 * for the glitch-free MUX to fully switch sources.
253 */
254 mb();
255 udelay(1);
256
257 /* Program old bank to a low-power source and divider. */
258 ns_reg_val &= ~(old_bank_masks->ns_mask);
259 ns_reg_val |= (clk->freq_tbl->ns_val & old_bank_masks->ns_mask);
260 writel_relaxed(ns_reg_val, clk->ns_reg);
261 }
262
263 /* Update the NS mask to match the current bank. */
264 clk->ns_mask = new_bank_masks->ns_mask;
265}
266
267int (*soc_update_sys_vdd)(enum sys_vdd_level level);
268
269/*
270 * SYS_VDD voting functions
271 */
272
273/* Update system voltage level given the current votes. */
274static int local_update_sys_vdd(void)
275{
276 static int cur_level = NUM_SYS_VDD_LEVELS;
277 int level, rc = 0;
278
279 if (local_sys_vdd_votes[HIGH])
280 level = HIGH;
281 else if (local_sys_vdd_votes[NOMINAL])
282 level = NOMINAL;
283 else if (local_sys_vdd_votes[LOW])
284 level = LOW;
285 else
286 level = NONE;
287
288 if (level == cur_level)
289 return rc;
290
291 rc = soc_update_sys_vdd(level);
292 if (!rc)
293 cur_level = level;
294
295 return rc;
296}
297
298/* Vote for a system voltage level. */
299int local_vote_sys_vdd(unsigned level)
300{
301 int rc = 0;
302 unsigned long flags;
303
304 /* Bounds checking. */
305 if (level >= ARRAY_SIZE(local_sys_vdd_votes))
306 return -EINVAL;
307
308 spin_lock_irqsave(&sys_vdd_vote_lock, flags);
309 local_sys_vdd_votes[level]++;
310 rc = local_update_sys_vdd();
311 if (rc)
312 local_sys_vdd_votes[level]--;
313 spin_unlock_irqrestore(&sys_vdd_vote_lock, flags);
314
315 return rc;
316}
317
318/* Remove vote for a system voltage level. */
319int local_unvote_sys_vdd(unsigned level)
320{
321 int rc = 0;
322 unsigned long flags;
323
324 /* Bounds checking. */
325 if (level >= ARRAY_SIZE(local_sys_vdd_votes))
326 return -EINVAL;
327
328 spin_lock_irqsave(&sys_vdd_vote_lock, flags);
329
330 if (WARN(!local_sys_vdd_votes[level],
331 "Reference counts are incorrect for level %d!\n", level))
332 goto out;
333
334 local_sys_vdd_votes[level]--;
335 rc = local_update_sys_vdd();
336 if (rc)
337 local_sys_vdd_votes[level]++;
338out:
339 spin_unlock_irqrestore(&sys_vdd_vote_lock, flags);
340 return rc;
341}
342/*
343 * Clock enable/disable functions
344 */
345
346/* Return non-zero if a clock status registers shows the clock is halted. */
347static int branch_clk_is_halted(const struct branch *clk)
348{
349 int invert = (clk->halt_check == ENABLE);
350 int status_bit = readl_relaxed(clk->halt_reg) & BIT(clk->halt_bit);
351 return invert ? !status_bit : status_bit;
352}
353
354static void __branch_clk_enable_reg(const struct branch *clk, const char *name)
355{
356 u32 reg_val;
357
358 if (clk->en_mask) {
359 reg_val = readl_relaxed(clk->ctl_reg);
360 reg_val |= clk->en_mask;
361 writel_relaxed(reg_val, clk->ctl_reg);
362 }
363
364 /*
365 * Use a memory barrier since some halt status registers are
366 * not within the same 1K segment as the branch/root enable
367 * registers. It's also needed in the udelay() case to ensure
368 * the delay starts after the branch enable.
369 */
370 mb();
371
372 /* Wait for clock to enable before returning. */
373 if (clk->halt_check == DELAY)
374 udelay(HALT_CHECK_DELAY_US);
375 else if (clk->halt_check == ENABLE || clk->halt_check == HALT
376 || clk->halt_check == ENABLE_VOTED
377 || clk->halt_check == HALT_VOTED) {
378 int count;
379
380 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to enable. */
381 for (count = HALT_CHECK_MAX_LOOPS; branch_clk_is_halted(clk)
382 && count > 0; count--)
383 udelay(1);
384 WARN(count == 0, "%s status stuck at 'off'", name);
385 }
386}
387
388/* Perform any register operations required to enable the clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700389static void __rcg_clk_enable_reg(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390{
391 u32 reg_val;
392 void __iomem *const reg = clk->b.ctl_reg;
393
394 WARN(clk->current_freq == &local_dummy_freq,
395 "Attempting to enable %s before setting its rate. "
396 "Set the rate first!\n", clk->c.dbg_name);
397
398 /*
399 * Program the NS register, if applicable. NS registers are not
400 * set in the set_rate path because power can be saved by deferring
401 * the selection of a clocked source until the clock is enabled.
402 */
403 if (clk->ns_mask) {
404 reg_val = readl_relaxed(clk->ns_reg);
405 reg_val &= ~(clk->ns_mask);
406 reg_val |= (clk->current_freq->ns_val & clk->ns_mask);
407 writel_relaxed(reg_val, clk->ns_reg);
408 }
409
410 /* Enable MN counter, if applicable. */
411 reg_val = readl_relaxed(reg);
412 if (clk->current_freq->mnd_en_mask) {
413 reg_val |= clk->current_freq->mnd_en_mask;
414 writel_relaxed(reg_val, reg);
415 }
416 /* Enable root. */
417 if (clk->root_en_mask) {
418 reg_val |= clk->root_en_mask;
419 writel_relaxed(reg_val, reg);
420 }
421 __branch_clk_enable_reg(&clk->b, clk->c.dbg_name);
422}
423
424/* Perform any register operations required to disable the branch. */
425static u32 __branch_clk_disable_reg(const struct branch *clk, const char *name)
426{
427 u32 reg_val;
428
429 reg_val = readl_relaxed(clk->ctl_reg);
430 if (clk->en_mask) {
431 reg_val &= ~(clk->en_mask);
432 writel_relaxed(reg_val, clk->ctl_reg);
433 }
434
435 /*
436 * Use a memory barrier since some halt status registers are
437 * not within the same K segment as the branch/root enable
438 * registers. It's also needed in the udelay() case to ensure
439 * the delay starts after the branch disable.
440 */
441 mb();
442
443 /* Wait for clock to disable before continuing. */
444 if (clk->halt_check == DELAY || clk->halt_check == ENABLE_VOTED
445 || clk->halt_check == HALT_VOTED)
446 udelay(HALT_CHECK_DELAY_US);
447 else if (clk->halt_check == ENABLE || clk->halt_check == HALT) {
448 int count;
449
450 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to disable. */
451 for (count = HALT_CHECK_MAX_LOOPS; !branch_clk_is_halted(clk)
452 && count > 0; count--)
453 udelay(1);
454 WARN(count == 0, "%s status stuck at 'on'", name);
455 }
456
457 return reg_val;
458}
459
460/* Perform any register operations required to disable the generator. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700461static void __rcg_clk_disable_reg(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462{
463 void __iomem *const reg = clk->b.ctl_reg;
464 uint32_t reg_val;
465
466 reg_val = __branch_clk_disable_reg(&clk->b, clk->c.dbg_name);
467 /* Disable root. */
468 if (clk->root_en_mask) {
469 reg_val &= ~(clk->root_en_mask);
470 writel_relaxed(reg_val, reg);
471 }
472 /* Disable MN counter, if applicable. */
473 if (clk->current_freq->mnd_en_mask) {
474 reg_val &= ~(clk->current_freq->mnd_en_mask);
475 writel_relaxed(reg_val, reg);
476 }
477 /*
478 * Program NS register to low-power value with an un-clocked or
479 * slowly-clocked source selected.
480 */
481 if (clk->ns_mask) {
482 reg_val = readl_relaxed(clk->ns_reg);
483 reg_val &= ~(clk->ns_mask);
484 reg_val |= (clk->freq_tbl->ns_val & clk->ns_mask);
485 writel_relaxed(reg_val, clk->ns_reg);
486 }
487}
488
Matt Wagantall54a3d692011-07-13 19:30:09 -0700489static void _rcg_clk_enable(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0625ea02011-07-13 18:51:56 -0700494 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 clk->enabled = true;
496 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497}
498
Matt Wagantall0625ea02011-07-13 18:51:56 -0700499static void _rcg_clk_disable(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500{
501 unsigned long flags;
502
503 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0625ea02011-07-13 18:51:56 -0700504 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 clk->enabled = false;
506 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
507}
508
509/* Enable a clock and any related power rail. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700510int rcg_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511{
512 int rc;
513 struct rcg_clk *clk = to_rcg_clk(c);
514
515 rc = local_vote_sys_vdd(clk->current_freq->sys_vdd);
516 if (rc)
Stephen Boyd7fa26742011-08-11 23:22:29 -0700517 return rc;
Matt Wagantall54a3d692011-07-13 19:30:09 -0700518 _rcg_clk_enable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520}
521
522/* Disable a clock and any related power rail. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700523void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524{
525 struct rcg_clk *clk = to_rcg_clk(c);
526
Matt Wagantall0625ea02011-07-13 18:51:56 -0700527 _rcg_clk_disable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 local_unvote_sys_vdd(clk->current_freq->sys_vdd);
529}
530
Stephen Boyd7fa26742011-08-11 23:22:29 -0700531/* Turn off a clock at boot, without checking refcounts. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700532void rcg_clk_auto_off(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533{
Matt Wagantall0625ea02011-07-13 18:51:56 -0700534 _rcg_clk_disable(to_rcg_clk(c));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535}
536
537/*
538 * Frequency-related functions
539 */
540
541/* Set a clock's frequency. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700542static int _rcg_clk_set_rate(struct rcg_clk *clk, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543{
544 struct clk_freq_tbl *cf;
545 int rc = 0;
546 struct clk *chld;
547 unsigned long flags;
548
549 spin_lock_irqsave(&clk->c.lock, flags);
550
551 /* Check if frequency is actually changed. */
552 cf = clk->current_freq;
553 if (nf == cf)
554 goto unlock;
555
556 if (clk->enabled) {
557 /* Vote for voltage and source for new freq. */
558 rc = local_vote_sys_vdd(nf->sys_vdd);
559 if (rc)
560 goto unlock;
561 rc = clk_enable(nf->src_clk);
562 if (rc) {
563 local_unvote_sys_vdd(nf->sys_vdd);
564 goto unlock;
565 }
566 }
567
568 spin_lock(&local_clock_reg_lock);
569
570 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
571 if (clk->bank_masks == NULL) {
572 /* Disable all branches to prevent glitches. */
573 list_for_each_entry(chld, &clk->c.children, siblings) {
574 struct branch_clk *x = to_branch_clk(chld);
575 /*
576 * We don't need to grab the child's lock because
577 * we hold the local_clock_reg_lock and 'enabled' is
578 * only modified within lock.
579 */
580 if (x->enabled)
581 __branch_clk_disable_reg(&x->b, x->c.dbg_name);
582 }
583 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700584 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 }
586
587 /* Perform clock-specific frequency switch operations. */
588 BUG_ON(!clk->set_rate);
589 clk->set_rate(clk, nf);
590
591 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700592 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 * is called to make sure the MNCNTR_EN bit is set correctly.
594 */
595 clk->current_freq = nf;
596
597 /* Enable any clocks that were disabled. */
598 if (clk->bank_masks == NULL) {
599 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700600 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 /* Enable only branches that were ON before. */
602 list_for_each_entry(chld, &clk->c.children, siblings) {
603 struct branch_clk *x = to_branch_clk(chld);
604 if (x->enabled)
605 __branch_clk_enable_reg(&x->b, x->c.dbg_name);
606 }
607 }
608
609 spin_unlock(&local_clock_reg_lock);
610
611 /* Release requirements of the old freq. */
612 if (clk->enabled) {
613 clk_disable(cf->src_clk);
614 local_unvote_sys_vdd(cf->sys_vdd);
615 }
616unlock:
617 spin_unlock_irqrestore(&clk->c.lock, flags);
618
619 return rc;
620}
621
622/* Set a clock to an exact rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700623int rcg_clk_set_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624{
625 struct rcg_clk *clk = to_rcg_clk(c);
626 struct clk_freq_tbl *nf;
627
628 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
629 && nf->freq_hz != rate; nf++)
630 ;
631
632 if (nf->freq_hz == FREQ_END)
633 return -EINVAL;
634
Matt Wagantall0625ea02011-07-13 18:51:56 -0700635 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636}
637
638/* Set a clock to a rate greater than some minimum. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700639int rcg_clk_set_min_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640{
641 struct rcg_clk *clk = to_rcg_clk(c);
642 struct clk_freq_tbl *nf;
643
644 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
645 && nf->freq_hz < rate; nf++)
646 ;
647
648 if (nf->freq_hz == FREQ_END)
649 return -EINVAL;
650
Matt Wagantall0625ea02011-07-13 18:51:56 -0700651 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652}
653
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654/* Get the currently-set rate of a clock in Hz. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700655unsigned rcg_clk_get_rate(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656{
657 struct rcg_clk *clk = to_rcg_clk(c);
658 unsigned long flags;
659 unsigned ret = 0;
660
661 spin_lock_irqsave(&local_clock_reg_lock, flags);
662 ret = clk->current_freq->freq_hz;
663 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
664
665 /*
666 * Return 0 if the rate has never been set. Might not be correct,
667 * but it's good enough.
668 */
669 if (ret == FREQ_END)
670 ret = 0;
671
672 return ret;
673}
674
675/* Check if a clock is currently enabled. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700676int rcg_clk_is_enabled(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677{
678 return to_rcg_clk(clk)->enabled;
679}
680
681/* Return a supported rate that's at least the specified rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700682long rcg_clk_round_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683{
684 struct rcg_clk *clk = to_rcg_clk(c);
685 struct clk_freq_tbl *f;
686
687 for (f = clk->freq_tbl; f->freq_hz != FREQ_END; f++)
688 if (f->freq_hz >= rate)
689 return f->freq_hz;
690
691 return -EPERM;
692}
693
694bool local_clk_is_local(struct clk *clk)
695{
696 return true;
697}
698
699/* Return the nth supported frequency for a given clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700700int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701{
702 struct rcg_clk *clk = to_rcg_clk(c);
703
704 if (!clk->freq_tbl || clk->freq_tbl->freq_hz == FREQ_END)
705 return -ENXIO;
706
707 return (clk->freq_tbl + n)->freq_hz;
708}
709
Matt Wagantall0625ea02011-07-13 18:51:56 -0700710struct clk *rcg_clk_get_parent(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711{
712 return to_rcg_clk(clk)->current_freq->src_clk;
713}
714
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700715void rcg_clk_handoff(struct clk *c)
716{
717 struct rcg_clk *clk = to_rcg_clk(c);
718 uint32_t ctl_val, ns_val, md_val, ns_mask;
719 struct clk_freq_tbl *freq;
720
721 ctl_val = readl_relaxed(clk->b.ctl_reg);
722 if (!(ctl_val & clk->root_en_mask))
723 return;
724
725 if (clk->bank_masks) {
726 const struct bank_mask_info *bank_info;
727 if (!(ctl_val & clk->bank_masks->bank_sel_mask))
728 bank_info = &clk->bank_masks->bank0_mask;
729 else
730 bank_info = &clk->bank_masks->bank1_mask;
731
732 ns_mask = bank_info->ns_mask;
733 md_val = readl_relaxed(bank_info->md_reg);
734 } else {
735 ns_mask = clk->ns_mask;
736 md_val = clk->md_reg ? readl_relaxed(clk->md_reg) : 0;
737 }
738
739 ns_val = readl_relaxed(clk->ns_reg) & ns_mask;
740 for (freq = clk->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
741 if ((freq->ns_val & ns_mask) == ns_val &&
742 (freq->mnd_en_mask || freq->md_val == md_val)) {
743 pr_info("%s rate=%d\n", clk->c.dbg_name, freq->freq_hz);
744 break;
745 }
746 }
747 if (freq->freq_hz == FREQ_END)
748 return;
749
750 clk->current_freq = freq;
751 c->flags |= CLKFLAG_HANDOFF_RATE;
752 clk_enable(c);
753}
754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755static int pll_vote_clk_enable(struct clk *clk)
756{
757 u32 ena;
758 unsigned long flags;
759 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
760
761 spin_lock_irqsave(&local_clock_reg_lock, flags);
762 ena = readl_relaxed(pll->en_reg);
763 ena |= pll->en_mask;
764 writel_relaxed(ena, pll->en_reg);
765 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
766
767 /* Wait until PLL is enabled */
768 while ((readl_relaxed(pll->status_reg) & BIT(16)) == 0)
769 cpu_relax();
770
771 return 0;
772}
773
774static void pll_vote_clk_disable(struct clk *clk)
775{
776 u32 ena;
777 unsigned long flags;
778 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
779
780 spin_lock_irqsave(&local_clock_reg_lock, flags);
781 ena = readl_relaxed(pll->en_reg);
782 ena &= ~(pll->en_mask);
783 writel_relaxed(ena, pll->en_reg);
784 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
785}
786
787static unsigned pll_vote_clk_get_rate(struct clk *clk)
788{
789 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
790 return pll->rate;
791}
792
793static struct clk *pll_vote_clk_get_parent(struct clk *clk)
794{
795 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
796 return pll->parent;
797}
798
799static int pll_vote_clk_is_enabled(struct clk *clk)
800{
801 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
802 return !!(readl_relaxed(pll->status_reg) & BIT(16));
803}
804
805struct clk_ops clk_ops_pll_vote = {
806 .enable = pll_vote_clk_enable,
807 .disable = pll_vote_clk_disable,
808 .is_enabled = pll_vote_clk_is_enabled,
809 .get_rate = pll_vote_clk_get_rate,
810 .get_parent = pll_vote_clk_get_parent,
811 .is_local = local_clk_is_local,
812};
813
814static int pll_clk_enable(struct clk *clk)
815{
816 u32 mode;
817 unsigned long flags;
818 struct pll_clk *pll = to_pll_clk(clk);
819
820 spin_lock_irqsave(&local_clock_reg_lock, flags);
821 mode = readl_relaxed(pll->mode_reg);
822 /* Disable PLL bypass mode. */
823 mode |= BIT(1);
824 writel_relaxed(mode, pll->mode_reg);
825
826 /*
827 * H/W requires a 5us delay between disabling the bypass and
828 * de-asserting the reset. Delay 10us just to be safe.
829 */
830 mb();
831 udelay(10);
832
833 /* De-assert active-low PLL reset. */
834 mode |= BIT(2);
835 writel_relaxed(mode, pll->mode_reg);
836
837 /* Wait until PLL is locked. */
838 mb();
839 udelay(50);
840
841 /* Enable PLL output. */
842 mode |= BIT(0);
843 writel_relaxed(mode, pll->mode_reg);
844
845 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
846 return 0;
847}
848
849static void pll_clk_disable(struct clk *clk)
850{
851 u32 mode;
852 unsigned long flags;
853 struct pll_clk *pll = to_pll_clk(clk);
854
855 /*
856 * Disable the PLL output, disable test mode, enable
857 * the bypass mode, and assert the reset.
858 */
859 spin_lock_irqsave(&local_clock_reg_lock, flags);
860 mode = readl_relaxed(pll->mode_reg);
861 mode &= ~BM(3, 0);
862 writel_relaxed(mode, pll->mode_reg);
863 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
864}
865
866static unsigned pll_clk_get_rate(struct clk *clk)
867{
868 struct pll_clk *pll = to_pll_clk(clk);
869 return pll->rate;
870}
871
872static struct clk *pll_clk_get_parent(struct clk *clk)
873{
874 struct pll_clk *pll = to_pll_clk(clk);
875 return pll->parent;
876}
877
878struct clk_ops clk_ops_pll = {
879 .enable = pll_clk_enable,
880 .disable = pll_clk_disable,
881 .get_rate = pll_clk_get_rate,
882 .get_parent = pll_clk_get_parent,
883 .is_local = local_clk_is_local,
884};
885
886struct clk_ops clk_ops_gnd = {
887 .get_rate = fixed_clk_get_rate,
888 .is_local = local_clk_is_local,
889};
890
891struct fixed_clk gnd_clk = {
892 .c = {
893 .dbg_name = "ground_clk",
894 .ops = &clk_ops_gnd,
895 CLK_INIT(gnd_clk.c),
896 },
897};
898
899struct clk_ops clk_ops_measure = {
900 .is_local = local_clk_is_local,
901};
902
903int branch_clk_enable(struct clk *clk)
904{
905 unsigned long flags;
906 struct branch_clk *branch = to_branch_clk(clk);
907
908 spin_lock_irqsave(&local_clock_reg_lock, flags);
909 __branch_clk_enable_reg(&branch->b, branch->c.dbg_name);
910 branch->enabled = true;
911 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
912
913 return 0;
914}
915
916void branch_clk_disable(struct clk *clk)
917{
918 unsigned long flags;
919 struct branch_clk *branch = to_branch_clk(clk);
920
921 spin_lock_irqsave(&local_clock_reg_lock, flags);
922 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
923 branch->enabled = false;
924 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925}
926
927struct clk *branch_clk_get_parent(struct clk *clk)
928{
929 struct branch_clk *branch = to_branch_clk(clk);
930 return branch->parent;
931}
932
933int branch_clk_set_parent(struct clk *clk, struct clk *parent)
934{
935 /*
936 * We setup the parent pointer at init time in msm_clock_init().
937 * This check is to make sure drivers can't change the parent.
938 */
939 if (parent && list_empty(&clk->siblings)) {
940 list_add(&clk->siblings, &parent->children);
941 return 0;
942 }
943 return -EINVAL;
944}
945
946int branch_clk_is_enabled(struct clk *clk)
947{
948 struct branch_clk *branch = to_branch_clk(clk);
949 return branch->enabled;
950}
951
952void branch_clk_auto_off(struct clk *clk)
953{
954 struct branch_clk *branch = to_branch_clk(clk);
955 unsigned long flags;
956
957 spin_lock_irqsave(&local_clock_reg_lock, flags);
958 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
959 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
960}
961
962int branch_reset(struct branch *clk, enum clk_reset_action action)
963{
964 int ret = 0;
965 u32 reg_val;
966 unsigned long flags;
967
968 if (!clk->reset_reg)
969 return -EPERM;
970
971 spin_lock_irqsave(&local_clock_reg_lock, flags);
972
973 reg_val = readl_relaxed(clk->reset_reg);
974 switch (action) {
975 case CLK_RESET_ASSERT:
976 reg_val |= clk->reset_mask;
977 break;
978 case CLK_RESET_DEASSERT:
979 reg_val &= ~(clk->reset_mask);
980 break;
981 default:
982 ret = -EINVAL;
983 }
984 writel_relaxed(reg_val, clk->reset_reg);
985
986 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
987
988 /* Make sure write is issued before returning. */
989 mb();
990
991 return ret;
992}
993
994int branch_clk_reset(struct clk *clk, enum clk_reset_action action)
995{
996 return branch_reset(&to_branch_clk(clk)->b, action);
997}