blob: 95a1e71b5d710df0b4876aa67c9f2040d6774d43 [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 */
44#define HALT_CHECK_MAX_LOOPS 100
45/* 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 Wagantall0625ea02011-07-13 18:51:56 -0700489static int _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);
497
498 return 0;
499}
500
Matt Wagantall0625ea02011-07-13 18:51:56 -0700501static void _rcg_clk_disable(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502{
503 unsigned long flags;
504
505 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0625ea02011-07-13 18:51:56 -0700506 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 clk->enabled = false;
508 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
509}
510
511/* Enable a clock and any related power rail. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700512int rcg_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513{
514 int rc;
515 struct rcg_clk *clk = to_rcg_clk(c);
516
517 rc = local_vote_sys_vdd(clk->current_freq->sys_vdd);
518 if (rc)
519 goto err_vdd;
520 rc = clk_enable(clk->depends);
521 if (rc)
522 goto err_dep;
Matt Wagantall0625ea02011-07-13 18:51:56 -0700523 rc = _rcg_clk_enable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 if (rc)
525 goto err_enable;
526 return rc;
527
528err_enable:
529 clk_disable(clk->depends);
530err_dep:
531 local_unvote_sys_vdd(clk->current_freq->sys_vdd);
532err_vdd:
533 return rc;
534}
535
536/* Disable a clock and any related power rail. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700537void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538{
539 struct rcg_clk *clk = to_rcg_clk(c);
540
Matt Wagantall0625ea02011-07-13 18:51:56 -0700541 _rcg_clk_disable(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 clk_disable(clk->depends);
543 local_unvote_sys_vdd(clk->current_freq->sys_vdd);
544}
545
546/* Turn off a clock at boot, without checking refcounts or disabling depends. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700547void rcg_clk_auto_off(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548{
Matt Wagantall0625ea02011-07-13 18:51:56 -0700549 _rcg_clk_disable(to_rcg_clk(c));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550}
551
552/*
553 * Frequency-related functions
554 */
555
556/* Set a clock's frequency. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700557static int _rcg_clk_set_rate(struct rcg_clk *clk, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558{
559 struct clk_freq_tbl *cf;
560 int rc = 0;
561 struct clk *chld;
562 unsigned long flags;
563
564 spin_lock_irqsave(&clk->c.lock, flags);
565
566 /* Check if frequency is actually changed. */
567 cf = clk->current_freq;
568 if (nf == cf)
569 goto unlock;
570
571 if (clk->enabled) {
572 /* Vote for voltage and source for new freq. */
573 rc = local_vote_sys_vdd(nf->sys_vdd);
574 if (rc)
575 goto unlock;
576 rc = clk_enable(nf->src_clk);
577 if (rc) {
578 local_unvote_sys_vdd(nf->sys_vdd);
579 goto unlock;
580 }
581 }
582
583 spin_lock(&local_clock_reg_lock);
584
585 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
586 if (clk->bank_masks == NULL) {
587 /* Disable all branches to prevent glitches. */
588 list_for_each_entry(chld, &clk->c.children, siblings) {
589 struct branch_clk *x = to_branch_clk(chld);
590 /*
591 * We don't need to grab the child's lock because
592 * we hold the local_clock_reg_lock and 'enabled' is
593 * only modified within lock.
594 */
595 if (x->enabled)
596 __branch_clk_disable_reg(&x->b, x->c.dbg_name);
597 }
598 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700599 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 }
601
602 /* Perform clock-specific frequency switch operations. */
603 BUG_ON(!clk->set_rate);
604 clk->set_rate(clk, nf);
605
606 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700607 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 * is called to make sure the MNCNTR_EN bit is set correctly.
609 */
610 clk->current_freq = nf;
611
612 /* Enable any clocks that were disabled. */
613 if (clk->bank_masks == NULL) {
614 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700615 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 /* Enable only branches that were ON before. */
617 list_for_each_entry(chld, &clk->c.children, siblings) {
618 struct branch_clk *x = to_branch_clk(chld);
619 if (x->enabled)
620 __branch_clk_enable_reg(&x->b, x->c.dbg_name);
621 }
622 }
623
624 spin_unlock(&local_clock_reg_lock);
625
626 /* Release requirements of the old freq. */
627 if (clk->enabled) {
628 clk_disable(cf->src_clk);
629 local_unvote_sys_vdd(cf->sys_vdd);
630 }
631unlock:
632 spin_unlock_irqrestore(&clk->c.lock, flags);
633
634 return rc;
635}
636
637/* Set a clock to an exact rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700638int rcg_clk_set_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639{
640 struct rcg_clk *clk = to_rcg_clk(c);
641 struct clk_freq_tbl *nf;
642
643 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
644 && nf->freq_hz != rate; nf++)
645 ;
646
647 if (nf->freq_hz == FREQ_END)
648 return -EINVAL;
649
Matt Wagantall0625ea02011-07-13 18:51:56 -0700650 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651}
652
653/* Set a clock to a rate greater than some minimum. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700654int rcg_clk_set_min_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655{
656 struct rcg_clk *clk = to_rcg_clk(c);
657 struct clk_freq_tbl *nf;
658
659 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
660 && nf->freq_hz < rate; nf++)
661 ;
662
663 if (nf->freq_hz == FREQ_END)
664 return -EINVAL;
665
Matt Wagantall0625ea02011-07-13 18:51:56 -0700666 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667}
668
669/* Set a clock to a maximum rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700670int rcg_clk_set_max_rate(struct clk *clk, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671{
672 return -EPERM;
673}
674
675/* Get the currently-set rate of a clock in Hz. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700676unsigned rcg_clk_get_rate(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677{
678 struct rcg_clk *clk = to_rcg_clk(c);
679 unsigned long flags;
680 unsigned ret = 0;
681
682 spin_lock_irqsave(&local_clock_reg_lock, flags);
683 ret = clk->current_freq->freq_hz;
684 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
685
686 /*
687 * Return 0 if the rate has never been set. Might not be correct,
688 * but it's good enough.
689 */
690 if (ret == FREQ_END)
691 ret = 0;
692
693 return ret;
694}
695
696/* Check if a clock is currently enabled. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700697int rcg_clk_is_enabled(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698{
699 return to_rcg_clk(clk)->enabled;
700}
701
702/* Return a supported rate that's at least the specified rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700703long rcg_clk_round_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704{
705 struct rcg_clk *clk = to_rcg_clk(c);
706 struct clk_freq_tbl *f;
707
708 for (f = clk->freq_tbl; f->freq_hz != FREQ_END; f++)
709 if (f->freq_hz >= rate)
710 return f->freq_hz;
711
712 return -EPERM;
713}
714
715bool local_clk_is_local(struct clk *clk)
716{
717 return true;
718}
719
720/* Return the nth supported frequency for a given clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700721int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722{
723 struct rcg_clk *clk = to_rcg_clk(c);
724
725 if (!clk->freq_tbl || clk->freq_tbl->freq_hz == FREQ_END)
726 return -ENXIO;
727
728 return (clk->freq_tbl + n)->freq_hz;
729}
730
Matt Wagantall0625ea02011-07-13 18:51:56 -0700731struct clk *rcg_clk_get_parent(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732{
733 return to_rcg_clk(clk)->current_freq->src_clk;
734}
735
736static int pll_vote_clk_enable(struct clk *clk)
737{
738 u32 ena;
739 unsigned long flags;
740 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
741
742 spin_lock_irqsave(&local_clock_reg_lock, flags);
743 ena = readl_relaxed(pll->en_reg);
744 ena |= pll->en_mask;
745 writel_relaxed(ena, pll->en_reg);
746 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
747
748 /* Wait until PLL is enabled */
749 while ((readl_relaxed(pll->status_reg) & BIT(16)) == 0)
750 cpu_relax();
751
752 return 0;
753}
754
755static void pll_vote_clk_disable(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
768static unsigned pll_vote_clk_get_rate(struct clk *clk)
769{
770 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
771 return pll->rate;
772}
773
774static struct clk *pll_vote_clk_get_parent(struct clk *clk)
775{
776 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
777 return pll->parent;
778}
779
780static int pll_vote_clk_is_enabled(struct clk *clk)
781{
782 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
783 return !!(readl_relaxed(pll->status_reg) & BIT(16));
784}
785
786struct clk_ops clk_ops_pll_vote = {
787 .enable = pll_vote_clk_enable,
788 .disable = pll_vote_clk_disable,
789 .is_enabled = pll_vote_clk_is_enabled,
790 .get_rate = pll_vote_clk_get_rate,
791 .get_parent = pll_vote_clk_get_parent,
792 .is_local = local_clk_is_local,
793};
794
795static int pll_clk_enable(struct clk *clk)
796{
797 u32 mode;
798 unsigned long flags;
799 struct pll_clk *pll = to_pll_clk(clk);
800
801 spin_lock_irqsave(&local_clock_reg_lock, flags);
802 mode = readl_relaxed(pll->mode_reg);
803 /* Disable PLL bypass mode. */
804 mode |= BIT(1);
805 writel_relaxed(mode, pll->mode_reg);
806
807 /*
808 * H/W requires a 5us delay between disabling the bypass and
809 * de-asserting the reset. Delay 10us just to be safe.
810 */
811 mb();
812 udelay(10);
813
814 /* De-assert active-low PLL reset. */
815 mode |= BIT(2);
816 writel_relaxed(mode, pll->mode_reg);
817
818 /* Wait until PLL is locked. */
819 mb();
820 udelay(50);
821
822 /* Enable PLL output. */
823 mode |= BIT(0);
824 writel_relaxed(mode, pll->mode_reg);
825
826 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
827 return 0;
828}
829
830static void pll_clk_disable(struct clk *clk)
831{
832 u32 mode;
833 unsigned long flags;
834 struct pll_clk *pll = to_pll_clk(clk);
835
836 /*
837 * Disable the PLL output, disable test mode, enable
838 * the bypass mode, and assert the reset.
839 */
840 spin_lock_irqsave(&local_clock_reg_lock, flags);
841 mode = readl_relaxed(pll->mode_reg);
842 mode &= ~BM(3, 0);
843 writel_relaxed(mode, pll->mode_reg);
844 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
845}
846
847static unsigned pll_clk_get_rate(struct clk *clk)
848{
849 struct pll_clk *pll = to_pll_clk(clk);
850 return pll->rate;
851}
852
853static struct clk *pll_clk_get_parent(struct clk *clk)
854{
855 struct pll_clk *pll = to_pll_clk(clk);
856 return pll->parent;
857}
858
859struct clk_ops clk_ops_pll = {
860 .enable = pll_clk_enable,
861 .disable = pll_clk_disable,
862 .get_rate = pll_clk_get_rate,
863 .get_parent = pll_clk_get_parent,
864 .is_local = local_clk_is_local,
865};
866
867struct clk_ops clk_ops_gnd = {
868 .get_rate = fixed_clk_get_rate,
869 .is_local = local_clk_is_local,
870};
871
872struct fixed_clk gnd_clk = {
873 .c = {
874 .dbg_name = "ground_clk",
875 .ops = &clk_ops_gnd,
876 CLK_INIT(gnd_clk.c),
877 },
878};
879
880struct clk_ops clk_ops_measure = {
881 .is_local = local_clk_is_local,
882};
883
884int branch_clk_enable(struct clk *clk)
885{
886 unsigned long flags;
887 struct branch_clk *branch = to_branch_clk(clk);
888
889 spin_lock_irqsave(&local_clock_reg_lock, flags);
890 __branch_clk_enable_reg(&branch->b, branch->c.dbg_name);
891 branch->enabled = true;
892 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
893
894 return 0;
895}
896
897void branch_clk_disable(struct clk *clk)
898{
899 unsigned long flags;
900 struct branch_clk *branch = to_branch_clk(clk);
901
902 spin_lock_irqsave(&local_clock_reg_lock, flags);
903 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
904 branch->enabled = false;
905 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
906}
907
908struct clk *branch_clk_get_parent(struct clk *clk)
909{
910 struct branch_clk *branch = to_branch_clk(clk);
911 return branch->parent;
912}
913
914int branch_clk_set_parent(struct clk *clk, struct clk *parent)
915{
916 /*
917 * We setup the parent pointer at init time in msm_clock_init().
918 * This check is to make sure drivers can't change the parent.
919 */
920 if (parent && list_empty(&clk->siblings)) {
921 list_add(&clk->siblings, &parent->children);
922 return 0;
923 }
924 return -EINVAL;
925}
926
927int branch_clk_is_enabled(struct clk *clk)
928{
929 struct branch_clk *branch = to_branch_clk(clk);
930 return branch->enabled;
931}
932
933void branch_clk_auto_off(struct clk *clk)
934{
935 struct branch_clk *branch = to_branch_clk(clk);
936 unsigned long flags;
937
938 spin_lock_irqsave(&local_clock_reg_lock, flags);
939 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
940 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
941}
942
943int branch_reset(struct branch *clk, enum clk_reset_action action)
944{
945 int ret = 0;
946 u32 reg_val;
947 unsigned long flags;
948
949 if (!clk->reset_reg)
950 return -EPERM;
951
952 spin_lock_irqsave(&local_clock_reg_lock, flags);
953
954 reg_val = readl_relaxed(clk->reset_reg);
955 switch (action) {
956 case CLK_RESET_ASSERT:
957 reg_val |= clk->reset_mask;
958 break;
959 case CLK_RESET_DEASSERT:
960 reg_val &= ~(clk->reset_mask);
961 break;
962 default:
963 ret = -EINVAL;
964 }
965 writel_relaxed(reg_val, clk->reset_reg);
966
967 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
968
969 /* Make sure write is issued before returning. */
970 mb();
971
972 return ret;
973}
974
975int branch_clk_reset(struct clk *clk, enum clk_reset_action action)
976{
977 return branch_reset(&to_branch_clk(clk)->b, action);
978}