blob: 6f298038f15aa8a6ce70de697a2f91228498e233 [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)
517 goto err_vdd;
518 rc = clk_enable(clk->depends);
519 if (rc)
520 goto err_dep;
Matt Wagantall54a3d692011-07-13 19:30:09 -0700521 _rcg_clk_enable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 return rc;
523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524err_dep:
525 local_unvote_sys_vdd(clk->current_freq->sys_vdd);
526err_vdd:
527 return rc;
528}
529
530/* Disable a clock and any related power rail. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700531void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532{
533 struct rcg_clk *clk = to_rcg_clk(c);
534
Matt Wagantall0625ea02011-07-13 18:51:56 -0700535 _rcg_clk_disable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 clk_disable(clk->depends);
537 local_unvote_sys_vdd(clk->current_freq->sys_vdd);
538}
539
540/* Turn off a clock at boot, without checking refcounts or disabling depends. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700541void rcg_clk_auto_off(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542{
Matt Wagantall0625ea02011-07-13 18:51:56 -0700543 _rcg_clk_disable(to_rcg_clk(c));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544}
545
546/*
547 * Frequency-related functions
548 */
549
550/* Set a clock's frequency. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700551static int _rcg_clk_set_rate(struct rcg_clk *clk, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552{
553 struct clk_freq_tbl *cf;
554 int rc = 0;
555 struct clk *chld;
556 unsigned long flags;
557
558 spin_lock_irqsave(&clk->c.lock, flags);
559
560 /* Check if frequency is actually changed. */
561 cf = clk->current_freq;
562 if (nf == cf)
563 goto unlock;
564
565 if (clk->enabled) {
566 /* Vote for voltage and source for new freq. */
567 rc = local_vote_sys_vdd(nf->sys_vdd);
568 if (rc)
569 goto unlock;
570 rc = clk_enable(nf->src_clk);
571 if (rc) {
572 local_unvote_sys_vdd(nf->sys_vdd);
573 goto unlock;
574 }
575 }
576
577 spin_lock(&local_clock_reg_lock);
578
579 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
580 if (clk->bank_masks == NULL) {
581 /* Disable all branches to prevent glitches. */
582 list_for_each_entry(chld, &clk->c.children, siblings) {
583 struct branch_clk *x = to_branch_clk(chld);
584 /*
585 * We don't need to grab the child's lock because
586 * we hold the local_clock_reg_lock and 'enabled' is
587 * only modified within lock.
588 */
589 if (x->enabled)
590 __branch_clk_disable_reg(&x->b, x->c.dbg_name);
591 }
592 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700593 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 }
595
596 /* Perform clock-specific frequency switch operations. */
597 BUG_ON(!clk->set_rate);
598 clk->set_rate(clk, nf);
599
600 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700601 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 * is called to make sure the MNCNTR_EN bit is set correctly.
603 */
604 clk->current_freq = nf;
605
606 /* Enable any clocks that were disabled. */
607 if (clk->bank_masks == NULL) {
608 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700609 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 /* Enable only branches that were ON before. */
611 list_for_each_entry(chld, &clk->c.children, siblings) {
612 struct branch_clk *x = to_branch_clk(chld);
613 if (x->enabled)
614 __branch_clk_enable_reg(&x->b, x->c.dbg_name);
615 }
616 }
617
618 spin_unlock(&local_clock_reg_lock);
619
620 /* Release requirements of the old freq. */
621 if (clk->enabled) {
622 clk_disable(cf->src_clk);
623 local_unvote_sys_vdd(cf->sys_vdd);
624 }
625unlock:
626 spin_unlock_irqrestore(&clk->c.lock, flags);
627
628 return rc;
629}
630
631/* Set a clock to an exact rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700632int rcg_clk_set_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633{
634 struct rcg_clk *clk = to_rcg_clk(c);
635 struct clk_freq_tbl *nf;
636
637 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
638 && nf->freq_hz != rate; nf++)
639 ;
640
641 if (nf->freq_hz == FREQ_END)
642 return -EINVAL;
643
Matt Wagantall0625ea02011-07-13 18:51:56 -0700644 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645}
646
647/* Set a clock to a rate greater than some minimum. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700648int rcg_clk_set_min_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649{
650 struct rcg_clk *clk = to_rcg_clk(c);
651 struct clk_freq_tbl *nf;
652
653 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
654 && nf->freq_hz < rate; nf++)
655 ;
656
657 if (nf->freq_hz == FREQ_END)
658 return -EINVAL;
659
Matt Wagantall0625ea02011-07-13 18:51:56 -0700660 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661}
662
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663/* Get the currently-set rate of a clock in Hz. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700664unsigned rcg_clk_get_rate(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665{
666 struct rcg_clk *clk = to_rcg_clk(c);
667 unsigned long flags;
668 unsigned ret = 0;
669
670 spin_lock_irqsave(&local_clock_reg_lock, flags);
671 ret = clk->current_freq->freq_hz;
672 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
673
674 /*
675 * Return 0 if the rate has never been set. Might not be correct,
676 * but it's good enough.
677 */
678 if (ret == FREQ_END)
679 ret = 0;
680
681 return ret;
682}
683
684/* Check if a clock is currently enabled. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700685int rcg_clk_is_enabled(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686{
687 return to_rcg_clk(clk)->enabled;
688}
689
690/* Return a supported rate that's at least the specified rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700691long rcg_clk_round_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692{
693 struct rcg_clk *clk = to_rcg_clk(c);
694 struct clk_freq_tbl *f;
695
696 for (f = clk->freq_tbl; f->freq_hz != FREQ_END; f++)
697 if (f->freq_hz >= rate)
698 return f->freq_hz;
699
700 return -EPERM;
701}
702
703bool local_clk_is_local(struct clk *clk)
704{
705 return true;
706}
707
708/* Return the nth supported frequency for a given clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700709int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710{
711 struct rcg_clk *clk = to_rcg_clk(c);
712
713 if (!clk->freq_tbl || clk->freq_tbl->freq_hz == FREQ_END)
714 return -ENXIO;
715
716 return (clk->freq_tbl + n)->freq_hz;
717}
718
Matt Wagantall0625ea02011-07-13 18:51:56 -0700719struct clk *rcg_clk_get_parent(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720{
721 return to_rcg_clk(clk)->current_freq->src_clk;
722}
723
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700724void rcg_clk_handoff(struct clk *c)
725{
726 struct rcg_clk *clk = to_rcg_clk(c);
727 uint32_t ctl_val, ns_val, md_val, ns_mask;
728 struct clk_freq_tbl *freq;
729
730 ctl_val = readl_relaxed(clk->b.ctl_reg);
731 if (!(ctl_val & clk->root_en_mask))
732 return;
733
734 if (clk->bank_masks) {
735 const struct bank_mask_info *bank_info;
736 if (!(ctl_val & clk->bank_masks->bank_sel_mask))
737 bank_info = &clk->bank_masks->bank0_mask;
738 else
739 bank_info = &clk->bank_masks->bank1_mask;
740
741 ns_mask = bank_info->ns_mask;
742 md_val = readl_relaxed(bank_info->md_reg);
743 } else {
744 ns_mask = clk->ns_mask;
745 md_val = clk->md_reg ? readl_relaxed(clk->md_reg) : 0;
746 }
747
748 ns_val = readl_relaxed(clk->ns_reg) & ns_mask;
749 for (freq = clk->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
750 if ((freq->ns_val & ns_mask) == ns_val &&
751 (freq->mnd_en_mask || freq->md_val == md_val)) {
752 pr_info("%s rate=%d\n", clk->c.dbg_name, freq->freq_hz);
753 break;
754 }
755 }
756 if (freq->freq_hz == FREQ_END)
757 return;
758
759 clk->current_freq = freq;
760 c->flags |= CLKFLAG_HANDOFF_RATE;
761 clk_enable(c);
762}
763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764static int pll_vote_clk_enable(struct clk *clk)
765{
766 u32 ena;
767 unsigned long flags;
768 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
769
770 spin_lock_irqsave(&local_clock_reg_lock, flags);
771 ena = readl_relaxed(pll->en_reg);
772 ena |= pll->en_mask;
773 writel_relaxed(ena, pll->en_reg);
774 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
775
776 /* Wait until PLL is enabled */
777 while ((readl_relaxed(pll->status_reg) & BIT(16)) == 0)
778 cpu_relax();
779
780 return 0;
781}
782
783static void pll_vote_clk_disable(struct clk *clk)
784{
785 u32 ena;
786 unsigned long flags;
787 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
788
789 spin_lock_irqsave(&local_clock_reg_lock, flags);
790 ena = readl_relaxed(pll->en_reg);
791 ena &= ~(pll->en_mask);
792 writel_relaxed(ena, pll->en_reg);
793 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
794}
795
796static unsigned pll_vote_clk_get_rate(struct clk *clk)
797{
798 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
799 return pll->rate;
800}
801
802static struct clk *pll_vote_clk_get_parent(struct clk *clk)
803{
804 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
805 return pll->parent;
806}
807
808static int pll_vote_clk_is_enabled(struct clk *clk)
809{
810 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
811 return !!(readl_relaxed(pll->status_reg) & BIT(16));
812}
813
814struct clk_ops clk_ops_pll_vote = {
815 .enable = pll_vote_clk_enable,
816 .disable = pll_vote_clk_disable,
817 .is_enabled = pll_vote_clk_is_enabled,
818 .get_rate = pll_vote_clk_get_rate,
819 .get_parent = pll_vote_clk_get_parent,
820 .is_local = local_clk_is_local,
821};
822
823static int pll_clk_enable(struct clk *clk)
824{
825 u32 mode;
826 unsigned long flags;
827 struct pll_clk *pll = to_pll_clk(clk);
828
829 spin_lock_irqsave(&local_clock_reg_lock, flags);
830 mode = readl_relaxed(pll->mode_reg);
831 /* Disable PLL bypass mode. */
832 mode |= BIT(1);
833 writel_relaxed(mode, pll->mode_reg);
834
835 /*
836 * H/W requires a 5us delay between disabling the bypass and
837 * de-asserting the reset. Delay 10us just to be safe.
838 */
839 mb();
840 udelay(10);
841
842 /* De-assert active-low PLL reset. */
843 mode |= BIT(2);
844 writel_relaxed(mode, pll->mode_reg);
845
846 /* Wait until PLL is locked. */
847 mb();
848 udelay(50);
849
850 /* Enable PLL output. */
851 mode |= BIT(0);
852 writel_relaxed(mode, pll->mode_reg);
853
854 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
855 return 0;
856}
857
858static void pll_clk_disable(struct clk *clk)
859{
860 u32 mode;
861 unsigned long flags;
862 struct pll_clk *pll = to_pll_clk(clk);
863
864 /*
865 * Disable the PLL output, disable test mode, enable
866 * the bypass mode, and assert the reset.
867 */
868 spin_lock_irqsave(&local_clock_reg_lock, flags);
869 mode = readl_relaxed(pll->mode_reg);
870 mode &= ~BM(3, 0);
871 writel_relaxed(mode, pll->mode_reg);
872 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
873}
874
875static unsigned pll_clk_get_rate(struct clk *clk)
876{
877 struct pll_clk *pll = to_pll_clk(clk);
878 return pll->rate;
879}
880
881static struct clk *pll_clk_get_parent(struct clk *clk)
882{
883 struct pll_clk *pll = to_pll_clk(clk);
884 return pll->parent;
885}
886
887struct clk_ops clk_ops_pll = {
888 .enable = pll_clk_enable,
889 .disable = pll_clk_disable,
890 .get_rate = pll_clk_get_rate,
891 .get_parent = pll_clk_get_parent,
892 .is_local = local_clk_is_local,
893};
894
895struct clk_ops clk_ops_gnd = {
896 .get_rate = fixed_clk_get_rate,
897 .is_local = local_clk_is_local,
898};
899
900struct fixed_clk gnd_clk = {
901 .c = {
902 .dbg_name = "ground_clk",
903 .ops = &clk_ops_gnd,
904 CLK_INIT(gnd_clk.c),
905 },
906};
907
908struct clk_ops clk_ops_measure = {
909 .is_local = local_clk_is_local,
910};
911
912int branch_clk_enable(struct clk *clk)
913{
Matt Wagantall2faaef02011-07-14 11:59:57 -0700914 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 unsigned long flags;
916 struct branch_clk *branch = to_branch_clk(clk);
917
Matt Wagantall2faaef02011-07-14 11:59:57 -0700918 rc = clk_enable(branch->depends);
919 if (rc)
920 return rc;
921
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 spin_lock_irqsave(&local_clock_reg_lock, flags);
923 __branch_clk_enable_reg(&branch->b, branch->c.dbg_name);
924 branch->enabled = true;
925 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
926
927 return 0;
928}
929
930void branch_clk_disable(struct clk *clk)
931{
932 unsigned long flags;
933 struct branch_clk *branch = to_branch_clk(clk);
934
935 spin_lock_irqsave(&local_clock_reg_lock, flags);
936 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
937 branch->enabled = false;
938 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Matt Wagantall2faaef02011-07-14 11:59:57 -0700939
940 clk_disable(branch->depends);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941}
942
943struct clk *branch_clk_get_parent(struct clk *clk)
944{
945 struct branch_clk *branch = to_branch_clk(clk);
946 return branch->parent;
947}
948
949int branch_clk_set_parent(struct clk *clk, struct clk *parent)
950{
951 /*
952 * We setup the parent pointer at init time in msm_clock_init().
953 * This check is to make sure drivers can't change the parent.
954 */
955 if (parent && list_empty(&clk->siblings)) {
956 list_add(&clk->siblings, &parent->children);
957 return 0;
958 }
959 return -EINVAL;
960}
961
962int branch_clk_is_enabled(struct clk *clk)
963{
964 struct branch_clk *branch = to_branch_clk(clk);
965 return branch->enabled;
966}
967
968void branch_clk_auto_off(struct clk *clk)
969{
970 struct branch_clk *branch = to_branch_clk(clk);
971 unsigned long flags;
972
973 spin_lock_irqsave(&local_clock_reg_lock, flags);
974 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
975 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
976}
977
978int branch_reset(struct branch *clk, enum clk_reset_action action)
979{
980 int ret = 0;
981 u32 reg_val;
982 unsigned long flags;
983
984 if (!clk->reset_reg)
985 return -EPERM;
986
987 spin_lock_irqsave(&local_clock_reg_lock, flags);
988
989 reg_val = readl_relaxed(clk->reset_reg);
990 switch (action) {
991 case CLK_RESET_ASSERT:
992 reg_val |= clk->reset_mask;
993 break;
994 case CLK_RESET_DEASSERT:
995 reg_val &= ~(clk->reset_mask);
996 break;
997 default:
998 ret = -EINVAL;
999 }
1000 writel_relaxed(reg_val, clk->reset_reg);
1001
1002 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
1003
1004 /* Make sure write is issued before returning. */
1005 mb();
1006
1007 return ret;
1008}
1009
1010int branch_clk_reset(struct clk *clk, enum clk_reset_action action)
1011{
1012 return branch_reset(&to_branch_clk(clk)->b, action);
1013}