blob: cf759000ff74a11307f86c6a9f4729f408060cac [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);
Matt Wagantall84f43fd2011-08-16 23:28:38 -070049struct clk_freq_tbl rcg_dummy_freq = F_END;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
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{
Stephen Boydc78d9a72011-07-20 00:46:24 -0700118 struct bank_masks *banks = clk->bank_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119 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{
Stephen Boydc78d9a72011-07-20 00:46:24 -0700210 struct bank_masks *banks = clk->bank_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 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
Matt Wagantall84f43fd2011-08-16 23:28:38 -0700394 WARN(clk->current_freq == &rcg_dummy_freq,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 "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. */
Stephen Boydc78d9a72011-07-20 00:46:24 -0700571 if (!clk->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 /* 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. */
Stephen Boydc78d9a72011-07-20 00:46:24 -0700598 if (!clk->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 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
Stephen Boydc78d9a72011-07-20 00:46:24 -0700725 if (clk->bank_info) {
726 const struct bank_masks *bank_masks = clk->bank_info;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700727 const struct bank_mask_info *bank_info;
Stephen Boydc78d9a72011-07-20 00:46:24 -0700728 if (!(ctl_val & bank_masks->bank_sel_mask))
729 bank_info = &bank_masks->bank0_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700730 else
Stephen Boydc78d9a72011-07-20 00:46:24 -0700731 bank_info = &bank_masks->bank1_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700732
733 ns_mask = bank_info->ns_mask;
734 md_val = readl_relaxed(bank_info->md_reg);
735 } else {
736 ns_mask = clk->ns_mask;
737 md_val = clk->md_reg ? readl_relaxed(clk->md_reg) : 0;
738 }
739
740 ns_val = readl_relaxed(clk->ns_reg) & ns_mask;
741 for (freq = clk->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
742 if ((freq->ns_val & ns_mask) == ns_val &&
743 (freq->mnd_en_mask || freq->md_val == md_val)) {
744 pr_info("%s rate=%d\n", clk->c.dbg_name, freq->freq_hz);
745 break;
746 }
747 }
748 if (freq->freq_hz == FREQ_END)
749 return;
750
751 clk->current_freq = freq;
752 c->flags |= CLKFLAG_HANDOFF_RATE;
753 clk_enable(c);
754}
755
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756static int pll_vote_clk_enable(struct clk *clk)
757{
758 u32 ena;
759 unsigned long flags;
760 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
761
762 spin_lock_irqsave(&local_clock_reg_lock, flags);
763 ena = readl_relaxed(pll->en_reg);
764 ena |= pll->en_mask;
765 writel_relaxed(ena, pll->en_reg);
766 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
767
768 /* Wait until PLL is enabled */
769 while ((readl_relaxed(pll->status_reg) & BIT(16)) == 0)
770 cpu_relax();
771
772 return 0;
773}
774
775static void pll_vote_clk_disable(struct clk *clk)
776{
777 u32 ena;
778 unsigned long flags;
779 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
780
781 spin_lock_irqsave(&local_clock_reg_lock, flags);
782 ena = readl_relaxed(pll->en_reg);
783 ena &= ~(pll->en_mask);
784 writel_relaxed(ena, pll->en_reg);
785 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
786}
787
788static unsigned pll_vote_clk_get_rate(struct clk *clk)
789{
790 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
791 return pll->rate;
792}
793
794static struct clk *pll_vote_clk_get_parent(struct clk *clk)
795{
796 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
797 return pll->parent;
798}
799
800static int pll_vote_clk_is_enabled(struct clk *clk)
801{
802 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
803 return !!(readl_relaxed(pll->status_reg) & BIT(16));
804}
805
806struct clk_ops clk_ops_pll_vote = {
807 .enable = pll_vote_clk_enable,
808 .disable = pll_vote_clk_disable,
809 .is_enabled = pll_vote_clk_is_enabled,
810 .get_rate = pll_vote_clk_get_rate,
811 .get_parent = pll_vote_clk_get_parent,
812 .is_local = local_clk_is_local,
813};
814
815static int pll_clk_enable(struct clk *clk)
816{
817 u32 mode;
818 unsigned long flags;
819 struct pll_clk *pll = to_pll_clk(clk);
820
821 spin_lock_irqsave(&local_clock_reg_lock, flags);
822 mode = readl_relaxed(pll->mode_reg);
823 /* Disable PLL bypass mode. */
824 mode |= BIT(1);
825 writel_relaxed(mode, pll->mode_reg);
826
827 /*
828 * H/W requires a 5us delay between disabling the bypass and
829 * de-asserting the reset. Delay 10us just to be safe.
830 */
831 mb();
832 udelay(10);
833
834 /* De-assert active-low PLL reset. */
835 mode |= BIT(2);
836 writel_relaxed(mode, pll->mode_reg);
837
838 /* Wait until PLL is locked. */
839 mb();
840 udelay(50);
841
842 /* Enable PLL output. */
843 mode |= BIT(0);
844 writel_relaxed(mode, pll->mode_reg);
845
846 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
847 return 0;
848}
849
850static void pll_clk_disable(struct clk *clk)
851{
852 u32 mode;
853 unsigned long flags;
854 struct pll_clk *pll = to_pll_clk(clk);
855
856 /*
857 * Disable the PLL output, disable test mode, enable
858 * the bypass mode, and assert the reset.
859 */
860 spin_lock_irqsave(&local_clock_reg_lock, flags);
861 mode = readl_relaxed(pll->mode_reg);
862 mode &= ~BM(3, 0);
863 writel_relaxed(mode, pll->mode_reg);
864 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
865}
866
867static unsigned pll_clk_get_rate(struct clk *clk)
868{
869 struct pll_clk *pll = to_pll_clk(clk);
870 return pll->rate;
871}
872
873static struct clk *pll_clk_get_parent(struct clk *clk)
874{
875 struct pll_clk *pll = to_pll_clk(clk);
876 return pll->parent;
877}
878
Vikram Mulukutla489e39e2011-08-31 18:04:05 -0700879int sr_pll_clk_enable(struct clk *clk)
880{
881 u32 mode;
882 unsigned long flags;
883 struct pll_clk *pll = to_pll_clk(clk);
884
885 spin_lock_irqsave(&local_clock_reg_lock, flags);
886 mode = readl_relaxed(pll->mode_reg);
887 /* De-assert active-low PLL reset. */
888 mode |= BIT(2);
889 writel_relaxed(mode, pll->mode_reg);
890
891 /*
892 * H/W requires a 5us delay between disabling the bypass and
893 * de-asserting the reset. Delay 10us just to be safe.
894 */
895 mb();
896 udelay(10);
897
898 /* Disable PLL bypass mode. */
899 mode |= BIT(1);
900 writel_relaxed(mode, pll->mode_reg);
901
902 /* Wait until PLL is locked. */
903 mb();
904 udelay(60);
905
906 /* Enable PLL output. */
907 mode |= BIT(0);
908 writel_relaxed(mode, pll->mode_reg);
909
910 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
911 return 0;
912}
913
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914struct clk_ops clk_ops_pll = {
915 .enable = pll_clk_enable,
916 .disable = pll_clk_disable,
917 .get_rate = pll_clk_get_rate,
918 .get_parent = pll_clk_get_parent,
919 .is_local = local_clk_is_local,
920};
921
922struct clk_ops clk_ops_gnd = {
923 .get_rate = fixed_clk_get_rate,
924 .is_local = local_clk_is_local,
925};
926
927struct fixed_clk gnd_clk = {
928 .c = {
929 .dbg_name = "ground_clk",
930 .ops = &clk_ops_gnd,
931 CLK_INIT(gnd_clk.c),
932 },
933};
934
935struct clk_ops clk_ops_measure = {
936 .is_local = local_clk_is_local,
937};
938
939int branch_clk_enable(struct clk *clk)
940{
941 unsigned long flags;
942 struct branch_clk *branch = to_branch_clk(clk);
943
944 spin_lock_irqsave(&local_clock_reg_lock, flags);
945 __branch_clk_enable_reg(&branch->b, branch->c.dbg_name);
946 branch->enabled = true;
947 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
948
949 return 0;
950}
951
952void branch_clk_disable(struct clk *clk)
953{
954 unsigned long flags;
955 struct branch_clk *branch = to_branch_clk(clk);
956
957 spin_lock_irqsave(&local_clock_reg_lock, flags);
958 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
959 branch->enabled = false;
960 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961}
962
963struct clk *branch_clk_get_parent(struct clk *clk)
964{
965 struct branch_clk *branch = to_branch_clk(clk);
966 return branch->parent;
967}
968
969int branch_clk_set_parent(struct clk *clk, struct clk *parent)
970{
971 /*
972 * We setup the parent pointer at init time in msm_clock_init().
973 * This check is to make sure drivers can't change the parent.
974 */
975 if (parent && list_empty(&clk->siblings)) {
976 list_add(&clk->siblings, &parent->children);
977 return 0;
978 }
979 return -EINVAL;
980}
981
982int branch_clk_is_enabled(struct clk *clk)
983{
984 struct branch_clk *branch = to_branch_clk(clk);
985 return branch->enabled;
986}
987
988void branch_clk_auto_off(struct clk *clk)
989{
990 struct branch_clk *branch = to_branch_clk(clk);
991 unsigned long flags;
992
993 spin_lock_irqsave(&local_clock_reg_lock, flags);
994 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
995 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
996}
997
998int branch_reset(struct branch *clk, enum clk_reset_action action)
999{
1000 int ret = 0;
1001 u32 reg_val;
1002 unsigned long flags;
1003
1004 if (!clk->reset_reg)
1005 return -EPERM;
1006
1007 spin_lock_irqsave(&local_clock_reg_lock, flags);
1008
1009 reg_val = readl_relaxed(clk->reset_reg);
1010 switch (action) {
1011 case CLK_RESET_ASSERT:
1012 reg_val |= clk->reset_mask;
1013 break;
1014 case CLK_RESET_DEASSERT:
1015 reg_val &= ~(clk->reset_mask);
1016 break;
1017 default:
1018 ret = -EINVAL;
1019 }
1020 writel_relaxed(reg_val, clk->reset_reg);
1021
1022 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
1023
1024 /* Make sure write is issued before returning. */
1025 mb();
1026
1027 return ret;
1028}
1029
1030int branch_clk_reset(struct clk *clk, enum clk_reset_action action)
1031{
1032 return branch_reset(&to_branch_clk(clk)->b, action);
1033}