blob: ca25802e3edd7cdf0c36c93b436be0bf15377f1f [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051/*
52 * Common Set-Rate Functions
53 */
54
55/* For clocks with MND dividers. */
56void set_rate_mnd(struct rcg_clk *clk, struct clk_freq_tbl *nf)
57{
58 uint32_t ns_reg_val, ctl_reg_val;
59
60 /* Assert MND reset. */
61 ns_reg_val = readl_relaxed(clk->ns_reg);
62 ns_reg_val |= BIT(7);
63 writel_relaxed(ns_reg_val, clk->ns_reg);
64
65 /* Program M and D values. */
66 writel_relaxed(nf->md_val, clk->md_reg);
67
68 /* If the clock has a separate CC register, program it. */
69 if (clk->ns_reg != clk->b.ctl_reg) {
70 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
71 ctl_reg_val &= ~(clk->ctl_mask);
72 ctl_reg_val |= nf->ctl_val;
73 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
74 }
75
76 /* Deassert MND reset. */
77 ns_reg_val &= ~BIT(7);
78 writel_relaxed(ns_reg_val, clk->ns_reg);
79}
80
81void set_rate_nop(struct rcg_clk *clk, struct clk_freq_tbl *nf)
82{
83 /*
84 * Nothing to do for fixed-rate or integer-divider clocks. Any settings
85 * in NS registers are applied in the enable path, since power can be
86 * saved by leaving an un-clocked or slowly-clocked source selected
87 * until the clock is enabled.
88 */
89}
90
91void set_rate_mnd_8(struct rcg_clk *clk, struct clk_freq_tbl *nf)
92{
93 uint32_t ctl_reg_val;
94
95 /* Assert MND reset. */
96 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
97 ctl_reg_val |= BIT(8);
98 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
99
100 /* Program M and D values. */
101 writel_relaxed(nf->md_val, clk->md_reg);
102
103 /* Program MN counter Enable and Mode. */
104 ctl_reg_val &= ~(clk->ctl_mask);
105 ctl_reg_val |= nf->ctl_val;
106 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
107
108 /* Deassert MND reset. */
109 ctl_reg_val &= ~BIT(8);
110 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
111}
112
113void set_rate_mnd_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf)
114{
Stephen Boydc78d9a72011-07-20 00:46:24 -0700115 struct bank_masks *banks = clk->bank_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116 const struct bank_mask_info *new_bank_masks;
117 const struct bank_mask_info *old_bank_masks;
118 uint32_t ns_reg_val, ctl_reg_val;
119 uint32_t bank_sel;
120
121 /*
122 * Determine active bank and program the other one. If the clock is
123 * off, program the active bank since bank switching won't work if
124 * both banks aren't running.
125 */
126 ctl_reg_val = readl_relaxed(clk->b.ctl_reg);
127 bank_sel = !!(ctl_reg_val & banks->bank_sel_mask);
128 /* If clock isn't running, don't switch banks. */
129 bank_sel ^= (!clk->enabled || clk->current_freq->freq_hz == 0);
130 if (bank_sel == 0) {
131 new_bank_masks = &banks->bank1_mask;
132 old_bank_masks = &banks->bank0_mask;
133 } else {
134 new_bank_masks = &banks->bank0_mask;
135 old_bank_masks = &banks->bank1_mask;
136 }
137
138 ns_reg_val = readl_relaxed(clk->ns_reg);
139
140 /* Assert bank MND reset. */
141 ns_reg_val |= new_bank_masks->rst_mask;
142 writel_relaxed(ns_reg_val, clk->ns_reg);
143
144 /*
145 * Program NS only if the clock is enabled, since the NS will be set
146 * as part of the enable procedure and should remain with a low-power
147 * MUX input selected until then.
148 */
149 if (clk->enabled) {
150 ns_reg_val &= ~(new_bank_masks->ns_mask);
151 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
152 writel_relaxed(ns_reg_val, clk->ns_reg);
153 }
154
155 writel_relaxed(nf->md_val, new_bank_masks->md_reg);
156
157 /* Enable counter only if clock is enabled. */
158 if (clk->enabled)
159 ctl_reg_val |= new_bank_masks->mnd_en_mask;
160 else
161 ctl_reg_val &= ~(new_bank_masks->mnd_en_mask);
162
163 ctl_reg_val &= ~(new_bank_masks->mode_mask);
164 ctl_reg_val |= (nf->ctl_val & new_bank_masks->mode_mask);
165 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
166
167 /* Deassert bank MND reset. */
168 ns_reg_val &= ~(new_bank_masks->rst_mask);
169 writel_relaxed(ns_reg_val, clk->ns_reg);
170
171 /*
172 * Switch to the new bank if clock is running. If it isn't, then
173 * no switch is necessary since we programmed the active bank.
174 */
175 if (clk->enabled && clk->current_freq->freq_hz) {
176 ctl_reg_val ^= banks->bank_sel_mask;
177 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
178 /*
179 * Wait at least 6 cycles of slowest bank's clock
180 * for the glitch-free MUX to fully switch sources.
181 */
182 mb();
183 udelay(1);
184
185 /* Disable old bank's MN counter. */
186 ctl_reg_val &= ~(old_bank_masks->mnd_en_mask);
187 writel_relaxed(ctl_reg_val, clk->b.ctl_reg);
188
189 /* Program old bank to a low-power source and divider. */
190 ns_reg_val &= ~(old_bank_masks->ns_mask);
191 ns_reg_val |= (clk->freq_tbl->ns_val & old_bank_masks->ns_mask);
192 writel_relaxed(ns_reg_val, clk->ns_reg);
193 }
194
195 /*
196 * If this freq requires the MN counter to be enabled,
197 * update the enable mask to match the current bank.
198 */
199 if (nf->mnd_en_mask)
200 nf->mnd_en_mask = new_bank_masks->mnd_en_mask;
201 /* Update the NS mask to match the current bank. */
202 clk->ns_mask = new_bank_masks->ns_mask;
203}
204
205void set_rate_div_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf)
206{
Stephen Boydc78d9a72011-07-20 00:46:24 -0700207 struct bank_masks *banks = clk->bank_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 const struct bank_mask_info *new_bank_masks;
209 const struct bank_mask_info *old_bank_masks;
210 uint32_t ns_reg_val, bank_sel;
211
212 /*
213 * Determine active bank and program the other one. If the clock is
214 * off, program the active bank since bank switching won't work if
215 * both banks aren't running.
216 */
217 ns_reg_val = readl_relaxed(clk->ns_reg);
218 bank_sel = !!(ns_reg_val & banks->bank_sel_mask);
219 /* If clock isn't running, don't switch banks. */
220 bank_sel ^= (!clk->enabled || clk->current_freq->freq_hz == 0);
221 if (bank_sel == 0) {
222 new_bank_masks = &banks->bank1_mask;
223 old_bank_masks = &banks->bank0_mask;
224 } else {
225 new_bank_masks = &banks->bank0_mask;
226 old_bank_masks = &banks->bank1_mask;
227 }
228
229 /*
230 * Program NS only if the clock is enabled, since the NS will be set
231 * as part of the enable procedure and should remain with a low-power
232 * MUX input selected until then.
233 */
234 if (clk->enabled) {
235 ns_reg_val &= ~(new_bank_masks->ns_mask);
236 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
237 writel_relaxed(ns_reg_val, clk->ns_reg);
238 }
239
240 /*
241 * Switch to the new bank if clock is running. If it isn't, then
242 * no switch is necessary since we programmed the active bank.
243 */
244 if (clk->enabled && clk->current_freq->freq_hz) {
245 ns_reg_val ^= banks->bank_sel_mask;
246 writel_relaxed(ns_reg_val, clk->ns_reg);
247 /*
248 * Wait at least 6 cycles of slowest bank's clock
249 * for the glitch-free MUX to fully switch sources.
250 */
251 mb();
252 udelay(1);
253
254 /* Program old bank to a low-power source and divider. */
255 ns_reg_val &= ~(old_bank_masks->ns_mask);
256 ns_reg_val |= (clk->freq_tbl->ns_val & old_bank_masks->ns_mask);
257 writel_relaxed(ns_reg_val, clk->ns_reg);
258 }
259
260 /* Update the NS mask to match the current bank. */
261 clk->ns_mask = new_bank_masks->ns_mask;
262}
263
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264/*
265 * Clock enable/disable functions
266 */
267
268/* Return non-zero if a clock status registers shows the clock is halted. */
269static int branch_clk_is_halted(const struct branch *clk)
270{
271 int invert = (clk->halt_check == ENABLE);
272 int status_bit = readl_relaxed(clk->halt_reg) & BIT(clk->halt_bit);
273 return invert ? !status_bit : status_bit;
274}
275
Stephen Boyd092fd182011-10-21 15:56:30 -0700276void __branch_clk_enable_reg(const struct branch *clk, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277{
278 u32 reg_val;
279
280 if (clk->en_mask) {
281 reg_val = readl_relaxed(clk->ctl_reg);
282 reg_val |= clk->en_mask;
283 writel_relaxed(reg_val, clk->ctl_reg);
284 }
285
286 /*
287 * Use a memory barrier since some halt status registers are
288 * not within the same 1K segment as the branch/root enable
289 * registers. It's also needed in the udelay() case to ensure
290 * the delay starts after the branch enable.
291 */
292 mb();
293
294 /* Wait for clock to enable before returning. */
295 if (clk->halt_check == DELAY)
296 udelay(HALT_CHECK_DELAY_US);
297 else if (clk->halt_check == ENABLE || clk->halt_check == HALT
298 || clk->halt_check == ENABLE_VOTED
299 || clk->halt_check == HALT_VOTED) {
300 int count;
301
302 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to enable. */
303 for (count = HALT_CHECK_MAX_LOOPS; branch_clk_is_halted(clk)
304 && count > 0; count--)
305 udelay(1);
306 WARN(count == 0, "%s status stuck at 'off'", name);
307 }
308}
309
310/* Perform any register operations required to enable the clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700311static void __rcg_clk_enable_reg(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312{
313 u32 reg_val;
314 void __iomem *const reg = clk->b.ctl_reg;
315
Matt Wagantall84f43fd2011-08-16 23:28:38 -0700316 WARN(clk->current_freq == &rcg_dummy_freq,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 "Attempting to enable %s before setting its rate. "
318 "Set the rate first!\n", clk->c.dbg_name);
319
320 /*
321 * Program the NS register, if applicable. NS registers are not
322 * set in the set_rate path because power can be saved by deferring
323 * the selection of a clocked source until the clock is enabled.
324 */
325 if (clk->ns_mask) {
326 reg_val = readl_relaxed(clk->ns_reg);
327 reg_val &= ~(clk->ns_mask);
328 reg_val |= (clk->current_freq->ns_val & clk->ns_mask);
329 writel_relaxed(reg_val, clk->ns_reg);
330 }
331
332 /* Enable MN counter, if applicable. */
333 reg_val = readl_relaxed(reg);
334 if (clk->current_freq->mnd_en_mask) {
335 reg_val |= clk->current_freq->mnd_en_mask;
336 writel_relaxed(reg_val, reg);
337 }
338 /* Enable root. */
339 if (clk->root_en_mask) {
340 reg_val |= clk->root_en_mask;
341 writel_relaxed(reg_val, reg);
342 }
343 __branch_clk_enable_reg(&clk->b, clk->c.dbg_name);
344}
345
346/* Perform any register operations required to disable the branch. */
Stephen Boyd092fd182011-10-21 15:56:30 -0700347u32 __branch_clk_disable_reg(const struct branch *clk, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348{
349 u32 reg_val;
350
351 reg_val = readl_relaxed(clk->ctl_reg);
352 if (clk->en_mask) {
353 reg_val &= ~(clk->en_mask);
354 writel_relaxed(reg_val, clk->ctl_reg);
355 }
356
357 /*
358 * Use a memory barrier since some halt status registers are
359 * not within the same K segment as the branch/root enable
360 * registers. It's also needed in the udelay() case to ensure
361 * the delay starts after the branch disable.
362 */
363 mb();
364
365 /* Wait for clock to disable before continuing. */
366 if (clk->halt_check == DELAY || clk->halt_check == ENABLE_VOTED
367 || clk->halt_check == HALT_VOTED)
368 udelay(HALT_CHECK_DELAY_US);
369 else if (clk->halt_check == ENABLE || clk->halt_check == HALT) {
370 int count;
371
372 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to disable. */
373 for (count = HALT_CHECK_MAX_LOOPS; !branch_clk_is_halted(clk)
374 && count > 0; count--)
375 udelay(1);
376 WARN(count == 0, "%s status stuck at 'on'", name);
377 }
378
379 return reg_val;
380}
381
382/* Perform any register operations required to disable the generator. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700383static void __rcg_clk_disable_reg(struct rcg_clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384{
385 void __iomem *const reg = clk->b.ctl_reg;
386 uint32_t reg_val;
387
388 reg_val = __branch_clk_disable_reg(&clk->b, clk->c.dbg_name);
389 /* Disable root. */
390 if (clk->root_en_mask) {
391 reg_val &= ~(clk->root_en_mask);
392 writel_relaxed(reg_val, reg);
393 }
394 /* Disable MN counter, if applicable. */
395 if (clk->current_freq->mnd_en_mask) {
396 reg_val &= ~(clk->current_freq->mnd_en_mask);
397 writel_relaxed(reg_val, reg);
398 }
399 /*
400 * Program NS register to low-power value with an un-clocked or
401 * slowly-clocked source selected.
402 */
403 if (clk->ns_mask) {
404 reg_val = readl_relaxed(clk->ns_reg);
405 reg_val &= ~(clk->ns_mask);
406 reg_val |= (clk->freq_tbl->ns_val & clk->ns_mask);
407 writel_relaxed(reg_val, clk->ns_reg);
408 }
409}
410
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700411/* Enable a rate-settable clock. */
412int rcg_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413{
414 unsigned long flags;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700415 struct rcg_clk *clk = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0625ea02011-07-13 18:51:56 -0700418 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 clk->enabled = true;
420 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700421
422 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423}
424
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700425/* Disable a rate-settable clock. */
426void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427{
428 unsigned long flags;
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700429 struct rcg_clk *clk = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430
431 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0625ea02011-07-13 18:51:56 -0700432 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 clk->enabled = false;
434 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
435}
436
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437/*
438 * Frequency-related functions
439 */
440
441/* Set a clock's frequency. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700442static int _rcg_clk_set_rate(struct rcg_clk *clk, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443{
444 struct clk_freq_tbl *cf;
445 int rc = 0;
446 struct clk *chld;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447
448 /* Check if frequency is actually changed. */
449 cf = clk->current_freq;
450 if (nf == cf)
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700451 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
453 if (clk->enabled) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700454 /* Enable source clock dependency for the new freq. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 rc = clk_enable(nf->src_clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700456 if (rc)
457 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 }
459
460 spin_lock(&local_clock_reg_lock);
461
462 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
Stephen Boydc78d9a72011-07-20 00:46:24 -0700463 if (!clk->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 /* Disable all branches to prevent glitches. */
465 list_for_each_entry(chld, &clk->c.children, siblings) {
466 struct branch_clk *x = to_branch_clk(chld);
467 /*
468 * We don't need to grab the child's lock because
469 * we hold the local_clock_reg_lock and 'enabled' is
470 * only modified within lock.
471 */
472 if (x->enabled)
473 __branch_clk_disable_reg(&x->b, x->c.dbg_name);
474 }
475 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700476 __rcg_clk_disable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 }
478
479 /* Perform clock-specific frequency switch operations. */
480 BUG_ON(!clk->set_rate);
481 clk->set_rate(clk, nf);
482
483 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700484 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 * is called to make sure the MNCNTR_EN bit is set correctly.
486 */
487 clk->current_freq = nf;
488
489 /* Enable any clocks that were disabled. */
Stephen Boydc78d9a72011-07-20 00:46:24 -0700490 if (!clk->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 if (clk->enabled)
Matt Wagantall0625ea02011-07-13 18:51:56 -0700492 __rcg_clk_enable_reg(clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 /* Enable only branches that were ON before. */
494 list_for_each_entry(chld, &clk->c.children, siblings) {
495 struct branch_clk *x = to_branch_clk(chld);
496 if (x->enabled)
497 __branch_clk_enable_reg(&x->b, x->c.dbg_name);
498 }
499 }
500
501 spin_unlock(&local_clock_reg_lock);
502
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700503 /* Release source requirements of the old freq. */
504 if (clk->enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 clk_disable(cf->src_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506
507 return rc;
508}
509
510/* Set a clock to an exact rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700511int rcg_clk_set_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512{
513 struct rcg_clk *clk = to_rcg_clk(c);
514 struct clk_freq_tbl *nf;
515
516 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
517 && nf->freq_hz != rate; nf++)
518 ;
519
520 if (nf->freq_hz == FREQ_END)
521 return -EINVAL;
522
Matt Wagantall0625ea02011-07-13 18:51:56 -0700523 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524}
525
526/* Set a clock to a rate greater than some minimum. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700527int rcg_clk_set_min_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528{
529 struct rcg_clk *clk = to_rcg_clk(c);
530 struct clk_freq_tbl *nf;
531
532 for (nf = clk->freq_tbl; nf->freq_hz != FREQ_END
533 && nf->freq_hz < rate; nf++)
534 ;
535
536 if (nf->freq_hz == FREQ_END)
537 return -EINVAL;
538
Matt Wagantall0625ea02011-07-13 18:51:56 -0700539 return _rcg_clk_set_rate(clk, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540}
541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542/* Get the currently-set rate of a clock in Hz. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700543unsigned rcg_clk_get_rate(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544{
545 struct rcg_clk *clk = to_rcg_clk(c);
546 unsigned long flags;
547 unsigned ret = 0;
548
549 spin_lock_irqsave(&local_clock_reg_lock, flags);
550 ret = clk->current_freq->freq_hz;
551 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
552
553 /*
554 * Return 0 if the rate has never been set. Might not be correct,
555 * but it's good enough.
556 */
557 if (ret == FREQ_END)
558 ret = 0;
559
560 return ret;
561}
562
563/* Check if a clock is currently enabled. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700564int rcg_clk_is_enabled(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565{
566 return to_rcg_clk(clk)->enabled;
567}
568
569/* Return a supported rate that's at least the specified rate. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700570long rcg_clk_round_rate(struct clk *c, unsigned rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571{
572 struct rcg_clk *clk = to_rcg_clk(c);
573 struct clk_freq_tbl *f;
574
575 for (f = clk->freq_tbl; f->freq_hz != FREQ_END; f++)
576 if (f->freq_hz >= rate)
577 return f->freq_hz;
578
579 return -EPERM;
580}
581
582bool local_clk_is_local(struct clk *clk)
583{
584 return true;
585}
586
587/* Return the nth supported frequency for a given clock. */
Matt Wagantall0625ea02011-07-13 18:51:56 -0700588int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589{
590 struct rcg_clk *clk = to_rcg_clk(c);
591
592 if (!clk->freq_tbl || clk->freq_tbl->freq_hz == FREQ_END)
593 return -ENXIO;
594
595 return (clk->freq_tbl + n)->freq_hz;
596}
597
Matt Wagantall0625ea02011-07-13 18:51:56 -0700598struct clk *rcg_clk_get_parent(struct clk *clk)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599{
600 return to_rcg_clk(clk)->current_freq->src_clk;
601}
602
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700603int rcg_clk_handoff(struct clk *c)
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700604{
605 struct rcg_clk *clk = to_rcg_clk(c);
606 uint32_t ctl_val, ns_val, md_val, ns_mask;
607 struct clk_freq_tbl *freq;
608
609 ctl_val = readl_relaxed(clk->b.ctl_reg);
610 if (!(ctl_val & clk->root_en_mask))
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700611 return 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700612
Stephen Boydc78d9a72011-07-20 00:46:24 -0700613 if (clk->bank_info) {
614 const struct bank_masks *bank_masks = clk->bank_info;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700615 const struct bank_mask_info *bank_info;
Stephen Boydc78d9a72011-07-20 00:46:24 -0700616 if (!(ctl_val & bank_masks->bank_sel_mask))
617 bank_info = &bank_masks->bank0_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700618 else
Stephen Boydc78d9a72011-07-20 00:46:24 -0700619 bank_info = &bank_masks->bank1_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700620
621 ns_mask = bank_info->ns_mask;
622 md_val = readl_relaxed(bank_info->md_reg);
623 } else {
624 ns_mask = clk->ns_mask;
625 md_val = clk->md_reg ? readl_relaxed(clk->md_reg) : 0;
626 }
627
628 ns_val = readl_relaxed(clk->ns_reg) & ns_mask;
629 for (freq = clk->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
630 if ((freq->ns_val & ns_mask) == ns_val &&
631 (freq->mnd_en_mask || freq->md_val == md_val)) {
632 pr_info("%s rate=%d\n", clk->c.dbg_name, freq->freq_hz);
633 break;
634 }
635 }
636 if (freq->freq_hz == FREQ_END)
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700637 return 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700638
639 clk->current_freq = freq;
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700640
641 return 1;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700642}
643
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644static int pll_vote_clk_enable(struct clk *clk)
645{
646 u32 ena;
647 unsigned long flags;
648 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
649
650 spin_lock_irqsave(&local_clock_reg_lock, flags);
651 ena = readl_relaxed(pll->en_reg);
652 ena |= pll->en_mask;
653 writel_relaxed(ena, pll->en_reg);
654 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
655
656 /* Wait until PLL is enabled */
657 while ((readl_relaxed(pll->status_reg) & BIT(16)) == 0)
658 cpu_relax();
659
660 return 0;
661}
662
663static void pll_vote_clk_disable(struct clk *clk)
664{
665 u32 ena;
666 unsigned long flags;
667 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
668
669 spin_lock_irqsave(&local_clock_reg_lock, flags);
670 ena = readl_relaxed(pll->en_reg);
671 ena &= ~(pll->en_mask);
672 writel_relaxed(ena, pll->en_reg);
673 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
674}
675
676static unsigned pll_vote_clk_get_rate(struct clk *clk)
677{
678 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
679 return pll->rate;
680}
681
682static struct clk *pll_vote_clk_get_parent(struct clk *clk)
683{
684 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
685 return pll->parent;
686}
687
688static int pll_vote_clk_is_enabled(struct clk *clk)
689{
690 struct pll_vote_clk *pll = to_pll_vote_clk(clk);
691 return !!(readl_relaxed(pll->status_reg) & BIT(16));
692}
693
694struct clk_ops clk_ops_pll_vote = {
695 .enable = pll_vote_clk_enable,
696 .disable = pll_vote_clk_disable,
697 .is_enabled = pll_vote_clk_is_enabled,
698 .get_rate = pll_vote_clk_get_rate,
699 .get_parent = pll_vote_clk_get_parent,
700 .is_local = local_clk_is_local,
701};
702
703static int pll_clk_enable(struct clk *clk)
704{
705 u32 mode;
706 unsigned long flags;
707 struct pll_clk *pll = to_pll_clk(clk);
708
709 spin_lock_irqsave(&local_clock_reg_lock, flags);
710 mode = readl_relaxed(pll->mode_reg);
711 /* Disable PLL bypass mode. */
712 mode |= BIT(1);
713 writel_relaxed(mode, pll->mode_reg);
714
715 /*
716 * H/W requires a 5us delay between disabling the bypass and
717 * de-asserting the reset. Delay 10us just to be safe.
718 */
719 mb();
720 udelay(10);
721
722 /* De-assert active-low PLL reset. */
723 mode |= BIT(2);
724 writel_relaxed(mode, pll->mode_reg);
725
726 /* Wait until PLL is locked. */
727 mb();
728 udelay(50);
729
730 /* Enable PLL output. */
731 mode |= BIT(0);
732 writel_relaxed(mode, pll->mode_reg);
733
734 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
735 return 0;
736}
737
738static void pll_clk_disable(struct clk *clk)
739{
740 u32 mode;
741 unsigned long flags;
742 struct pll_clk *pll = to_pll_clk(clk);
743
744 /*
745 * Disable the PLL output, disable test mode, enable
746 * the bypass mode, and assert the reset.
747 */
748 spin_lock_irqsave(&local_clock_reg_lock, flags);
749 mode = readl_relaxed(pll->mode_reg);
750 mode &= ~BM(3, 0);
751 writel_relaxed(mode, pll->mode_reg);
752 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
753}
754
755static unsigned pll_clk_get_rate(struct clk *clk)
756{
757 struct pll_clk *pll = to_pll_clk(clk);
758 return pll->rate;
759}
760
761static struct clk *pll_clk_get_parent(struct clk *clk)
762{
763 struct pll_clk *pll = to_pll_clk(clk);
764 return pll->parent;
765}
766
Vikram Mulukutla489e39e2011-08-31 18:04:05 -0700767int sr_pll_clk_enable(struct clk *clk)
768{
769 u32 mode;
770 unsigned long flags;
771 struct pll_clk *pll = to_pll_clk(clk);
772
773 spin_lock_irqsave(&local_clock_reg_lock, flags);
774 mode = readl_relaxed(pll->mode_reg);
775 /* De-assert active-low PLL reset. */
776 mode |= BIT(2);
777 writel_relaxed(mode, pll->mode_reg);
778
779 /*
780 * H/W requires a 5us delay between disabling the bypass and
781 * de-asserting the reset. Delay 10us just to be safe.
782 */
783 mb();
784 udelay(10);
785
786 /* Disable PLL bypass mode. */
787 mode |= BIT(1);
788 writel_relaxed(mode, pll->mode_reg);
789
790 /* Wait until PLL is locked. */
791 mb();
792 udelay(60);
793
794 /* Enable PLL output. */
795 mode |= BIT(0);
796 writel_relaxed(mode, pll->mode_reg);
797
798 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
799 return 0;
800}
801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802struct clk_ops clk_ops_pll = {
803 .enable = pll_clk_enable,
804 .disable = pll_clk_disable,
805 .get_rate = pll_clk_get_rate,
806 .get_parent = pll_clk_get_parent,
807 .is_local = local_clk_is_local,
808};
809
810struct clk_ops clk_ops_gnd = {
811 .get_rate = fixed_clk_get_rate,
812 .is_local = local_clk_is_local,
813};
814
815struct fixed_clk gnd_clk = {
816 .c = {
817 .dbg_name = "ground_clk",
818 .ops = &clk_ops_gnd,
819 CLK_INIT(gnd_clk.c),
820 },
821};
822
823struct clk_ops clk_ops_measure = {
824 .is_local = local_clk_is_local,
825};
826
827int branch_clk_enable(struct clk *clk)
828{
829 unsigned long flags;
830 struct branch_clk *branch = to_branch_clk(clk);
831
832 spin_lock_irqsave(&local_clock_reg_lock, flags);
833 __branch_clk_enable_reg(&branch->b, branch->c.dbg_name);
834 branch->enabled = true;
835 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
836
837 return 0;
838}
839
840void branch_clk_disable(struct clk *clk)
841{
842 unsigned long flags;
843 struct branch_clk *branch = to_branch_clk(clk);
844
845 spin_lock_irqsave(&local_clock_reg_lock, flags);
846 __branch_clk_disable_reg(&branch->b, branch->c.dbg_name);
847 branch->enabled = false;
848 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849}
850
851struct clk *branch_clk_get_parent(struct clk *clk)
852{
853 struct branch_clk *branch = to_branch_clk(clk);
854 return branch->parent;
855}
856
857int branch_clk_set_parent(struct clk *clk, struct clk *parent)
858{
859 /*
860 * We setup the parent pointer at init time in msm_clock_init().
861 * This check is to make sure drivers can't change the parent.
862 */
863 if (parent && list_empty(&clk->siblings)) {
864 list_add(&clk->siblings, &parent->children);
865 return 0;
866 }
867 return -EINVAL;
868}
869
870int branch_clk_is_enabled(struct clk *clk)
871{
872 struct branch_clk *branch = to_branch_clk(clk);
873 return branch->enabled;
874}
875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876int branch_reset(struct branch *clk, enum clk_reset_action action)
877{
878 int ret = 0;
879 u32 reg_val;
880 unsigned long flags;
881
882 if (!clk->reset_reg)
883 return -EPERM;
884
885 spin_lock_irqsave(&local_clock_reg_lock, flags);
886
887 reg_val = readl_relaxed(clk->reset_reg);
888 switch (action) {
889 case CLK_RESET_ASSERT:
890 reg_val |= clk->reset_mask;
891 break;
892 case CLK_RESET_DEASSERT:
893 reg_val &= ~(clk->reset_mask);
894 break;
895 default:
896 ret = -EINVAL;
897 }
898 writel_relaxed(reg_val, clk->reset_reg);
899
900 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
901
902 /* Make sure write is issued before returning. */
903 mb();
904
905 return ret;
906}
907
908int branch_clk_reset(struct clk *clk, enum clk_reset_action action)
909{
910 return branch_reset(&to_branch_clk(clk)->b, action);
911}