blob: 1f590a036fd46053002c2591aa2499092abd4151 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Matt Wagantalld55b90f2012-02-23 23:27:44 -080027#include <mach/clk-provider.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <mach/clk.h>
29#include <mach/scm-io.h>
30
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#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. */
Matt Wagantallf82f2942012-01-27 13:56:13 -080056void set_rate_mnd(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057{
58 uint32_t ns_reg_val, ctl_reg_val;
59
60 /* Assert MND reset. */
Matt Wagantallf82f2942012-01-27 13:56:13 -080061 ns_reg_val = readl_relaxed(rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 ns_reg_val |= BIT(7);
Matt Wagantallf82f2942012-01-27 13:56:13 -080063 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064
65 /* Program M and D values. */
Matt Wagantallf82f2942012-01-27 13:56:13 -080066 writel_relaxed(nf->md_val, rcg->md_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
68 /* If the clock has a separate CC register, program it. */
Matt Wagantallf82f2942012-01-27 13:56:13 -080069 if (rcg->ns_reg != rcg->b.ctl_reg) {
70 ctl_reg_val = readl_relaxed(rcg->b.ctl_reg);
71 ctl_reg_val &= ~(rcg->ctl_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 ctl_reg_val |= nf->ctl_val;
Matt Wagantallf82f2942012-01-27 13:56:13 -080073 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074 }
75
76 /* Deassert MND reset. */
77 ns_reg_val &= ~BIT(7);
Matt Wagantallf82f2942012-01-27 13:56:13 -080078 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079}
80
Matt Wagantallf82f2942012-01-27 13:56:13 -080081void set_rate_nop(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082{
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
Matt Wagantallf82f2942012-01-27 13:56:13 -080091void set_rate_mnd_8(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092{
93 uint32_t ctl_reg_val;
94
95 /* Assert MND reset. */
Matt Wagantallf82f2942012-01-27 13:56:13 -080096 ctl_reg_val = readl_relaxed(rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097 ctl_reg_val |= BIT(8);
Matt Wagantallf82f2942012-01-27 13:56:13 -080098 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099
100 /* Program M and D values. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800101 writel_relaxed(nf->md_val, rcg->md_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102
103 /* Program MN counter Enable and Mode. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800104 ctl_reg_val &= ~(rcg->ctl_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 ctl_reg_val |= nf->ctl_val;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800106 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107
108 /* Deassert MND reset. */
109 ctl_reg_val &= ~BIT(8);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800110 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111}
112
Matt Wagantallf82f2942012-01-27 13:56:13 -0800113void set_rate_mnd_banked(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800115 struct bank_masks *banks = rcg->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 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800126 ctl_reg_val = readl_relaxed(rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 bank_sel = !!(ctl_reg_val & banks->bank_sel_mask);
128 /* If clock isn't running, don't switch banks. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800129 bank_sel ^= (!rcg->enabled || rcg->current_freq->freq_hz == 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130 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
Matt Wagantallf82f2942012-01-27 13:56:13 -0800138 ns_reg_val = readl_relaxed(rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139
140 /* Assert bank MND reset. */
141 ns_reg_val |= new_bank_masks->rst_mask;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800142 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143
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 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800149 if (rcg->enabled) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 ns_reg_val &= ~(new_bank_masks->ns_mask);
151 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800152 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 }
154
155 writel_relaxed(nf->md_val, new_bank_masks->md_reg);
156
157 /* Enable counter only if clock is enabled. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800158 if (rcg->enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 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);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800165 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166
167 /* Deassert bank MND reset. */
168 ns_reg_val &= ~(new_bank_masks->rst_mask);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800169 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170
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 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800175 if (rcg->enabled && rcg->current_freq->freq_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176 ctl_reg_val ^= banks->bank_sel_mask;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800177 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 /*
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);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800187 writel_relaxed(ctl_reg_val, rcg->b.ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 /* Program old bank to a low-power source and divider. */
190 ns_reg_val &= ~(old_bank_masks->ns_mask);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800191 ns_reg_val |= (rcg->freq_tbl->ns_val & old_bank_masks->ns_mask);
192 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193 }
194
Matt Wagantall07c45472012-02-10 23:27:24 -0800195 /* Update the MND_EN and NS masks to match the current bank. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800196 rcg->mnd_en_mask = new_bank_masks->mnd_en_mask;
197 rcg->ns_mask = new_bank_masks->ns_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198}
199
Matt Wagantallf82f2942012-01-27 13:56:13 -0800200void set_rate_div_banked(struct rcg_clk *rcg, struct clk_freq_tbl *nf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800202 struct bank_masks *banks = rcg->bank_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 const struct bank_mask_info *new_bank_masks;
204 const struct bank_mask_info *old_bank_masks;
205 uint32_t ns_reg_val, bank_sel;
206
207 /*
208 * Determine active bank and program the other one. If the clock is
209 * off, program the active bank since bank switching won't work if
210 * both banks aren't running.
211 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800212 ns_reg_val = readl_relaxed(rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 bank_sel = !!(ns_reg_val & banks->bank_sel_mask);
214 /* If clock isn't running, don't switch banks. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800215 bank_sel ^= (!rcg->enabled || rcg->current_freq->freq_hz == 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 if (bank_sel == 0) {
217 new_bank_masks = &banks->bank1_mask;
218 old_bank_masks = &banks->bank0_mask;
219 } else {
220 new_bank_masks = &banks->bank0_mask;
221 old_bank_masks = &banks->bank1_mask;
222 }
223
224 /*
225 * Program NS only if the clock is enabled, since the NS will be set
226 * as part of the enable procedure and should remain with a low-power
227 * MUX input selected until then.
228 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800229 if (rcg->enabled) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 ns_reg_val &= ~(new_bank_masks->ns_mask);
231 ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800232 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 }
234
235 /*
236 * Switch to the new bank if clock is running. If it isn't, then
237 * no switch is necessary since we programmed the active bank.
238 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800239 if (rcg->enabled && rcg->current_freq->freq_hz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 ns_reg_val ^= banks->bank_sel_mask;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800241 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 /*
243 * Wait at least 6 cycles of slowest bank's clock
244 * for the glitch-free MUX to fully switch sources.
245 */
246 mb();
247 udelay(1);
248
249 /* Program old bank to a low-power source and divider. */
250 ns_reg_val &= ~(old_bank_masks->ns_mask);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800251 ns_reg_val |= (rcg->freq_tbl->ns_val & old_bank_masks->ns_mask);
252 writel_relaxed(ns_reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253 }
254
255 /* Update the NS mask to match the current bank. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800256 rcg->ns_mask = new_bank_masks->ns_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257}
258
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259/*
260 * Clock enable/disable functions
261 */
262
263/* Return non-zero if a clock status registers shows the clock is halted. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800264static int branch_clk_is_halted(const struct branch *b)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800266 int invert = (b->halt_check == ENABLE);
267 int status_bit = readl_relaxed(b->halt_reg) & BIT(b->halt_bit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 return invert ? !status_bit : status_bit;
269}
270
Stephen Boyd409b8b42012-04-10 12:12:56 -0700271static int branch_in_hwcg_mode(const struct branch *b)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800272{
273 if (!b->hwcg_mask)
274 return 0;
275
276 return !!(readl_relaxed(b->hwcg_reg) & b->hwcg_mask);
277}
278
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700279void __branch_enable_reg(const struct branch *b, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280{
281 u32 reg_val;
282
Matt Wagantallf82f2942012-01-27 13:56:13 -0800283 if (b->en_mask) {
284 reg_val = readl_relaxed(b->ctl_reg);
285 reg_val |= b->en_mask;
286 writel_relaxed(reg_val, b->ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 }
288
289 /*
290 * Use a memory barrier since some halt status registers are
291 * not within the same 1K segment as the branch/root enable
292 * registers. It's also needed in the udelay() case to ensure
293 * the delay starts after the branch enable.
294 */
295 mb();
296
Stephen Boyda52d7e32011-11-10 11:59:00 -0800297 /* Skip checking halt bit if the clock is in hardware gated mode */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800298 if (branch_in_hwcg_mode(b))
Stephen Boyda52d7e32011-11-10 11:59:00 -0800299 return;
300
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 /* Wait for clock to enable before returning. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800302 if (b->halt_check == DELAY) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 udelay(HALT_CHECK_DELAY_US);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800304 } else if (b->halt_check == ENABLE || b->halt_check == HALT
305 || b->halt_check == ENABLE_VOTED
306 || b->halt_check == HALT_VOTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 int count;
308
309 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to enable. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800310 for (count = HALT_CHECK_MAX_LOOPS; branch_clk_is_halted(b)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 && count > 0; count--)
312 udelay(1);
313 WARN(count == 0, "%s status stuck at 'off'", name);
314 }
315}
316
317/* Perform any register operations required to enable the clock. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800318static void __rcg_clk_enable_reg(struct rcg_clk *rcg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319{
320 u32 reg_val;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800321 void __iomem *const reg = rcg->b.ctl_reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 /*
324 * Program the NS register, if applicable. NS registers are not
325 * set in the set_rate path because power can be saved by deferring
326 * the selection of a clocked source until the clock is enabled.
327 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800328 if (rcg->ns_mask) {
329 reg_val = readl_relaxed(rcg->ns_reg);
330 reg_val &= ~(rcg->ns_mask);
331 reg_val |= (rcg->current_freq->ns_val & rcg->ns_mask);
332 writel_relaxed(reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 }
334
335 /* Enable MN counter, if applicable. */
336 reg_val = readl_relaxed(reg);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800337 if (rcg->current_freq->md_val) {
338 reg_val |= rcg->mnd_en_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 writel_relaxed(reg_val, reg);
340 }
341 /* Enable root. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800342 if (rcg->root_en_mask) {
343 reg_val |= rcg->root_en_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 writel_relaxed(reg_val, reg);
345 }
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700346 __branch_enable_reg(&rcg->b, rcg->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347}
348
349/* Perform any register operations required to disable the branch. */
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700350u32 __branch_disable_reg(const struct branch *b, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351{
352 u32 reg_val;
353
Matt Wagantalle3508bb2012-07-23 17:18:37 -0700354 reg_val = b->ctl_reg ? readl_relaxed(b->ctl_reg) : 0;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800355 if (b->en_mask) {
356 reg_val &= ~(b->en_mask);
357 writel_relaxed(reg_val, b->ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 }
359
360 /*
361 * Use a memory barrier since some halt status registers are
362 * not within the same K segment as the branch/root enable
363 * registers. It's also needed in the udelay() case to ensure
364 * the delay starts after the branch disable.
365 */
366 mb();
367
Stephen Boyda52d7e32011-11-10 11:59:00 -0800368 /* Skip checking halt bit if the clock is in hardware gated mode */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800369 if (branch_in_hwcg_mode(b))
Stephen Boyda52d7e32011-11-10 11:59:00 -0800370 return reg_val;
371
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 /* Wait for clock to disable before continuing. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800373 if (b->halt_check == DELAY || b->halt_check == ENABLE_VOTED
374 || b->halt_check == HALT_VOTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 udelay(HALT_CHECK_DELAY_US);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800376 } else if (b->halt_check == ENABLE || b->halt_check == HALT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 int count;
378
379 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to disable. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800380 for (count = HALT_CHECK_MAX_LOOPS; !branch_clk_is_halted(b)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 && count > 0; count--)
382 udelay(1);
383 WARN(count == 0, "%s status stuck at 'on'", name);
384 }
385
386 return reg_val;
387}
388
389/* Perform any register operations required to disable the generator. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800390static void __rcg_clk_disable_reg(struct rcg_clk *rcg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800392 void __iomem *const reg = rcg->b.ctl_reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 uint32_t reg_val;
394
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700395 reg_val = __branch_disable_reg(&rcg->b, rcg->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 /* Disable root. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800397 if (rcg->root_en_mask) {
398 reg_val &= ~(rcg->root_en_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 writel_relaxed(reg_val, reg);
400 }
401 /* Disable MN counter, if applicable. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800402 if (rcg->current_freq->md_val) {
403 reg_val &= ~(rcg->mnd_en_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 writel_relaxed(reg_val, reg);
405 }
406 /*
407 * Program NS register to low-power value with an un-clocked or
408 * slowly-clocked source selected.
409 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800410 if (rcg->ns_mask) {
411 reg_val = readl_relaxed(rcg->ns_reg);
412 reg_val &= ~(rcg->ns_mask);
413 reg_val |= (rcg->freq_tbl->ns_val & rcg->ns_mask);
414 writel_relaxed(reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 }
416}
417
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800418static int rcg_clk_prepare(struct clk *c)
419{
420 struct rcg_clk *rcg = to_rcg_clk(c);
421
422 WARN(rcg->current_freq == &rcg_dummy_freq,
423 "Attempting to prepare %s before setting its rate. "
424 "Set the rate first!\n", rcg->c.dbg_name);
425 rcg->prepared = true;
426
427 return 0;
428}
429
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700430/* Enable a rate-settable clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700431static int rcg_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432{
433 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800434 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435
436 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800437 __rcg_clk_enable_reg(rcg);
438 rcg->enabled = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700440
441 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442}
443
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700444/* Disable a rate-settable clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700445static void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446{
447 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800448 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800451 __rcg_clk_disable_reg(rcg);
452 rcg->enabled = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
454}
455
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800456static void rcg_clk_unprepare(struct clk *c)
457{
458 struct rcg_clk *rcg = to_rcg_clk(c);
459 rcg->prepared = false;
460}
461
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462/*
463 * Frequency-related functions
464 */
465
Matt Wagantallab1adce2012-01-24 14:57:24 -0800466/* Set a clock to an exact rate. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700467static int rcg_clk_set_rate(struct clk *c, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800469 struct rcg_clk *rcg = to_rcg_clk(c);
Matt Wagantallab1adce2012-01-24 14:57:24 -0800470 struct clk_freq_tbl *nf, *cf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 struct clk *chld;
Matt Wagantallab1adce2012-01-24 14:57:24 -0800472 int rc = 0;
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800473 unsigned long flags;
Matt Wagantallab1adce2012-01-24 14:57:24 -0800474
Matt Wagantallf82f2942012-01-27 13:56:13 -0800475 for (nf = rcg->freq_tbl; nf->freq_hz != FREQ_END
Matt Wagantallab1adce2012-01-24 14:57:24 -0800476 && nf->freq_hz != rate; nf++)
477 ;
478
479 if (nf->freq_hz == FREQ_END)
480 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
Matt Wagantallf82f2942012-01-27 13:56:13 -0800482 cf = rcg->current_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800484 /* Enable source clock dependency for the new frequency */
485 if (rcg->prepared) {
486 rc = clk_prepare(nf->src_clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700487 if (rc)
488 return rc;
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800489
490 }
491
492 spin_lock_irqsave(&c->lock, flags);
493 if (rcg->enabled) {
494 rc = clk_enable(nf->src_clk);
495 if (rc) {
496 spin_unlock_irqrestore(&c->lock, flags);
497 clk_unprepare(nf->src_clk);
498 return rc;
499 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 }
501
502 spin_lock(&local_clock_reg_lock);
503
504 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800505 if (!rcg->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /* Disable all branches to prevent glitches. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800507 list_for_each_entry(chld, &rcg->c.children, siblings) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 struct branch_clk *x = to_branch_clk(chld);
509 /*
510 * We don't need to grab the child's lock because
511 * we hold the local_clock_reg_lock and 'enabled' is
512 * only modified within lock.
513 */
514 if (x->enabled)
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700515 __branch_disable_reg(&x->b, x->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 }
Matt Wagantallf82f2942012-01-27 13:56:13 -0800517 if (rcg->enabled)
518 __rcg_clk_disable_reg(rcg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 }
520
521 /* Perform clock-specific frequency switch operations. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800522 BUG_ON(!rcg->set_rate);
523 rcg->set_rate(rcg, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700526 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 * is called to make sure the MNCNTR_EN bit is set correctly.
528 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800529 rcg->current_freq = nf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530
531 /* Enable any clocks that were disabled. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800532 if (!rcg->bank_info) {
533 if (rcg->enabled)
534 __rcg_clk_enable_reg(rcg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 /* Enable only branches that were ON before. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800536 list_for_each_entry(chld, &rcg->c.children, siblings) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 struct branch_clk *x = to_branch_clk(chld);
538 if (x->enabled)
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700539 __branch_enable_reg(&x->b, x->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 }
541 }
542
543 spin_unlock(&local_clock_reg_lock);
544
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700545 /* Release source requirements of the old freq. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800546 if (rcg->enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547 clk_disable(cf->src_clk);
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800548 spin_unlock_irqrestore(&c->lock, flags);
549
550 if (rcg->prepared)
551 clk_unprepare(cf->src_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
553 return rc;
554}
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556/* Check if a clock is currently enabled. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800557static int rcg_clk_is_enabled(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800559 return to_rcg_clk(c)->enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560}
561
562/* Return a supported rate that's at least the specified rate. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700563static long rcg_clk_round_rate(struct clk *c, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800565 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 struct clk_freq_tbl *f;
567
Matt Wagantallf82f2942012-01-27 13:56:13 -0800568 for (f = rcg->freq_tbl; f->freq_hz != FREQ_END; f++)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 if (f->freq_hz >= rate)
570 return f->freq_hz;
571
572 return -EPERM;
573}
574
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575/* Return the nth supported frequency for a given clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700576static int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800578 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
Matt Wagantallf82f2942012-01-27 13:56:13 -0800580 if (!rcg->freq_tbl || rcg->freq_tbl->freq_hz == FREQ_END)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 return -ENXIO;
582
Matt Wagantallf82f2942012-01-27 13:56:13 -0800583 return (rcg->freq_tbl + n)->freq_hz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584}
585
Matt Wagantallf82f2942012-01-27 13:56:13 -0800586static struct clk *rcg_clk_get_parent(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800588 return to_rcg_clk(c)->current_freq->src_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589}
590
Stephen Boyda52d7e32011-11-10 11:59:00 -0800591/* Disable hw clock gating if not set at boot */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800592enum handoff branch_handoff(struct branch *b, struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800593{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800594 if (!branch_in_hwcg_mode(b)) {
595 b->hwcg_mask = 0;
Matt Wagantalle3508bb2012-07-23 17:18:37 -0700596 if (b->ctl_reg && readl_relaxed(b->ctl_reg) & b->en_mask)
Matt Wagantalla15833b2012-04-03 11:00:56 -0700597 return HANDOFF_ENABLED_CLK;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800598 }
Matt Wagantalla15833b2012-04-03 11:00:56 -0700599 return HANDOFF_DISABLED_CLK;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800600}
601
Stephen Boyd409b8b42012-04-10 12:12:56 -0700602static enum handoff branch_clk_handoff(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800603{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800604 struct branch_clk *br = to_branch_clk(c);
605 return branch_handoff(&br->b, &br->c);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800606}
607
Stephen Boyd409b8b42012-04-10 12:12:56 -0700608static enum handoff rcg_clk_handoff(struct clk *c)
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700609{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800610 struct rcg_clk *rcg = to_rcg_clk(c);
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700611 uint32_t ctl_val, ns_val, md_val, ns_mask;
612 struct clk_freq_tbl *freq;
Matt Wagantalla15833b2012-04-03 11:00:56 -0700613 enum handoff ret;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800614
Matt Wagantallf82f2942012-01-27 13:56:13 -0800615 ctl_val = readl_relaxed(rcg->b.ctl_reg);
616 ret = branch_handoff(&rcg->b, &rcg->c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700617 if (ret == HANDOFF_DISABLED_CLK)
618 return HANDOFF_DISABLED_CLK;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700619
Matt Wagantallf82f2942012-01-27 13:56:13 -0800620 if (rcg->bank_info) {
621 const struct bank_masks *bank_masks = rcg->bank_info;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700622 const struct bank_mask_info *bank_info;
Stephen Boydc78d9a72011-07-20 00:46:24 -0700623 if (!(ctl_val & bank_masks->bank_sel_mask))
624 bank_info = &bank_masks->bank0_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700625 else
Stephen Boydc78d9a72011-07-20 00:46:24 -0700626 bank_info = &bank_masks->bank1_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700627
628 ns_mask = bank_info->ns_mask;
Tianyi Goue46938b2012-01-31 12:30:12 -0800629 md_val = bank_info->md_reg ?
630 readl_relaxed(bank_info->md_reg) : 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700631 } else {
Matt Wagantallf82f2942012-01-27 13:56:13 -0800632 ns_mask = rcg->ns_mask;
633 md_val = rcg->md_reg ? readl_relaxed(rcg->md_reg) : 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700634 }
Matt Wagantalla15833b2012-04-03 11:00:56 -0700635 if (!ns_mask)
636 return HANDOFF_UNKNOWN_RATE;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800637 ns_val = readl_relaxed(rcg->ns_reg) & ns_mask;
638 for (freq = rcg->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700639 if ((freq->ns_val & ns_mask) == ns_val &&
Matt Wagantall2a59b212012-06-12 19:16:01 -0700640 (!freq->md_val || freq->md_val == md_val))
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700641 break;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700642 }
643 if (freq->freq_hz == FREQ_END)
Matt Wagantalla15833b2012-04-03 11:00:56 -0700644 return HANDOFF_UNKNOWN_RATE;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700645
Matt Wagantallf82f2942012-01-27 13:56:13 -0800646 rcg->current_freq = freq;
Stephen Boyde891ca32012-03-19 12:16:36 -0700647 c->rate = freq->freq_hz;
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700648
Matt Wagantalla15833b2012-04-03 11:00:56 -0700649 return HANDOFF_ENABLED_CLK;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700650}
651
Matt Wagantallae053222012-05-14 19:42:07 -0700652struct clk_ops clk_ops_empty;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653
654struct fixed_clk gnd_clk = {
655 .c = {
656 .dbg_name = "ground_clk",
Matt Wagantallae053222012-05-14 19:42:07 -0700657 .ops = &clk_ops_empty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658 CLK_INIT(gnd_clk.c),
659 },
660};
661
Matt Wagantallf82f2942012-01-27 13:56:13 -0800662static int branch_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663{
664 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800665 struct branch_clk *br = to_branch_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666
667 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700668 __branch_enable_reg(&br->b, br->c.dbg_name);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800669 br->enabled = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
671
672 return 0;
673}
674
Matt Wagantallf82f2942012-01-27 13:56:13 -0800675static void branch_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676{
677 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800678 struct branch_clk *br = to_branch_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679
680 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700681 __branch_disable_reg(&br->b, br->c.dbg_name);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800682 br->enabled = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684}
685
Matt Wagantallf82f2942012-01-27 13:56:13 -0800686static struct clk *branch_clk_get_parent(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800688 return to_branch_clk(c)->parent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689}
690
Matt Wagantallf82f2942012-01-27 13:56:13 -0800691static int branch_clk_is_enabled(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800693 return to_branch_clk(c)->enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694}
695
Stephen Boyda52d7e32011-11-10 11:59:00 -0800696static void branch_enable_hwcg(struct branch *b)
697{
698 unsigned long flags;
699 u32 reg_val;
700
701 spin_lock_irqsave(&local_clock_reg_lock, flags);
702 reg_val = readl_relaxed(b->hwcg_reg);
703 reg_val |= b->hwcg_mask;
704 writel_relaxed(reg_val, b->hwcg_reg);
705 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
706}
707
708static void branch_disable_hwcg(struct branch *b)
709{
710 unsigned long flags;
711 u32 reg_val;
712
713 spin_lock_irqsave(&local_clock_reg_lock, flags);
714 reg_val = readl_relaxed(b->hwcg_reg);
715 reg_val &= ~b->hwcg_mask;
716 writel_relaxed(reg_val, b->hwcg_reg);
717 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
718}
719
Matt Wagantallf82f2942012-01-27 13:56:13 -0800720static void branch_clk_enable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800721{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800722 branch_enable_hwcg(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800723}
724
Matt Wagantallf82f2942012-01-27 13:56:13 -0800725static void branch_clk_disable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800726{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800727 branch_disable_hwcg(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800728}
729
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800730static int branch_set_flags(struct branch *b, unsigned flags)
731{
732 unsigned long irq_flags;
733 u32 reg_val;
734 int ret = 0;
735
736 if (!b->retain_reg)
737 return -EPERM;
738
739 spin_lock_irqsave(&local_clock_reg_lock, irq_flags);
740 reg_val = readl_relaxed(b->retain_reg);
741 switch (flags) {
742 case CLKFLAG_RETAIN:
743 reg_val |= b->retain_mask;
744 break;
745 case CLKFLAG_NORETAIN:
746 reg_val &= ~b->retain_mask;
747 break;
748 default:
749 ret = -EINVAL;
750 }
751 writel_relaxed(reg_val, b->retain_reg);
752 spin_unlock_irqrestore(&local_clock_reg_lock, irq_flags);
753
754 return ret;
755}
756
Stephen Boyd409b8b42012-04-10 12:12:56 -0700757static int branch_clk_set_flags(struct clk *clk, unsigned flags)
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800758{
759 return branch_set_flags(&to_branch_clk(clk)->b, flags);
760}
761
Stephen Boyd409b8b42012-04-10 12:12:56 -0700762static int branch_clk_in_hwcg_mode(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800763{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800764 return branch_in_hwcg_mode(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800765}
766
Matt Wagantallf82f2942012-01-27 13:56:13 -0800767static void rcg_clk_enable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800768{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800769 branch_enable_hwcg(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800770}
771
Matt Wagantallf82f2942012-01-27 13:56:13 -0800772static void rcg_clk_disable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800773{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800774 branch_disable_hwcg(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800775}
776
Stephen Boyd409b8b42012-04-10 12:12:56 -0700777static int rcg_clk_in_hwcg_mode(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800778{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800779 return branch_in_hwcg_mode(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800780}
781
Stephen Boyd409b8b42012-04-10 12:12:56 -0700782static int rcg_clk_set_flags(struct clk *clk, unsigned flags)
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800783{
784 return branch_set_flags(&to_rcg_clk(clk)->b, flags);
785}
786
Stephen Boyda52d7e32011-11-10 11:59:00 -0800787int branch_reset(struct branch *b, enum clk_reset_action action)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788{
789 int ret = 0;
790 u32 reg_val;
791 unsigned long flags;
792
Stephen Boyda52d7e32011-11-10 11:59:00 -0800793 if (!b->reset_reg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 return -EPERM;
795
Stephen Boyda52d7e32011-11-10 11:59:00 -0800796 /* Disable hw gating when asserting a reset */
797 if (b->hwcg_mask && action == CLK_RESET_ASSERT)
798 branch_disable_hwcg(b);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799
Stephen Boyda52d7e32011-11-10 11:59:00 -0800800 spin_lock_irqsave(&local_clock_reg_lock, flags);
801 /* Assert/Deassert reset */
802 reg_val = readl_relaxed(b->reset_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 switch (action) {
804 case CLK_RESET_ASSERT:
Stephen Boyda52d7e32011-11-10 11:59:00 -0800805 reg_val |= b->reset_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 break;
807 case CLK_RESET_DEASSERT:
Stephen Boyda52d7e32011-11-10 11:59:00 -0800808 reg_val &= ~b->reset_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 break;
810 default:
811 ret = -EINVAL;
812 }
Stephen Boyda52d7e32011-11-10 11:59:00 -0800813 writel_relaxed(reg_val, b->reset_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
815
Stephen Boyda52d7e32011-11-10 11:59:00 -0800816 /* Enable hw gating when deasserting a reset */
817 if (b->hwcg_mask && action == CLK_RESET_DEASSERT)
818 branch_enable_hwcg(b);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 /* Make sure write is issued before returning. */
820 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 return ret;
822}
823
Matt Wagantallf82f2942012-01-27 13:56:13 -0800824static int branch_clk_reset(struct clk *c, enum clk_reset_action action)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800826 return branch_reset(&to_branch_clk(c)->b, action);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827}
Stephen Boydb8ad8222011-11-28 12:17:58 -0800828
Stephen Boyd409b8b42012-04-10 12:12:56 -0700829struct clk_ops clk_ops_branch = {
830 .enable = branch_clk_enable,
831 .disable = branch_clk_disable,
832 .enable_hwcg = branch_clk_enable_hwcg,
833 .disable_hwcg = branch_clk_disable_hwcg,
834 .in_hwcg_mode = branch_clk_in_hwcg_mode,
Stephen Boyd409b8b42012-04-10 12:12:56 -0700835 .is_enabled = branch_clk_is_enabled,
836 .reset = branch_clk_reset,
837 .get_parent = branch_clk_get_parent,
838 .handoff = branch_clk_handoff,
839 .set_flags = branch_clk_set_flags,
840};
841
842struct clk_ops clk_ops_reset = {
843 .reset = branch_clk_reset,
844};
845
Matt Wagantallf82f2942012-01-27 13:56:13 -0800846static int rcg_clk_reset(struct clk *c, enum clk_reset_action action)
Stephen Boyd7bf28142011-12-07 00:30:52 -0800847{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800848 return branch_reset(&to_rcg_clk(c)->b, action);
Stephen Boyd7bf28142011-12-07 00:30:52 -0800849}
850
Stephen Boyd409b8b42012-04-10 12:12:56 -0700851struct clk_ops clk_ops_rcg = {
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800852 .prepare = rcg_clk_prepare,
Stephen Boyd409b8b42012-04-10 12:12:56 -0700853 .enable = rcg_clk_enable,
854 .disable = rcg_clk_disable,
Stephen Boyd2c2875f2012-01-24 17:36:34 -0800855 .unprepare = rcg_clk_unprepare,
Stephen Boyd409b8b42012-04-10 12:12:56 -0700856 .enable_hwcg = rcg_clk_enable_hwcg,
857 .disable_hwcg = rcg_clk_disable_hwcg,
858 .in_hwcg_mode = rcg_clk_in_hwcg_mode,
Stephen Boyd409b8b42012-04-10 12:12:56 -0700859 .handoff = rcg_clk_handoff,
860 .set_rate = rcg_clk_set_rate,
861 .list_rate = rcg_clk_list_rate,
862 .is_enabled = rcg_clk_is_enabled,
863 .round_rate = rcg_clk_round_rate,
864 .reset = rcg_clk_reset,
865 .get_parent = rcg_clk_get_parent,
866 .set_flags = rcg_clk_set_flags,
867};
868
Stephen Boydb8ad8222011-11-28 12:17:58 -0800869static int cdiv_clk_enable(struct clk *c)
870{
871 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800872 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800873
874 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700875 __branch_enable_reg(&cdiv->b, cdiv->c.dbg_name);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800876 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
877
878 return 0;
879}
880
881static void cdiv_clk_disable(struct clk *c)
882{
883 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800884 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800885
886 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantall0de1b3f2012-06-05 19:52:43 -0700887 __branch_disable_reg(&cdiv->b, cdiv->c.dbg_name);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800888 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
889}
890
891static int cdiv_clk_set_rate(struct clk *c, unsigned long rate)
892{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800893 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800894 u32 reg_val;
895
Matt Wagantallf82f2942012-01-27 13:56:13 -0800896 if (rate > cdiv->max_div)
Stephen Boydb8ad8222011-11-28 12:17:58 -0800897 return -EINVAL;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800898
899 spin_lock(&local_clock_reg_lock);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800900 reg_val = readl_relaxed(cdiv->ns_reg);
901 reg_val &= ~(cdiv->ext_mask | (cdiv->max_div - 1) << cdiv->div_offset);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800902 /* Non-zero rates mean set a divider, zero means use external input */
903 if (rate)
Matt Wagantallf82f2942012-01-27 13:56:13 -0800904 reg_val |= (rate - 1) << cdiv->div_offset;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800905 else
Matt Wagantallf82f2942012-01-27 13:56:13 -0800906 reg_val |= cdiv->ext_mask;
907 writel_relaxed(reg_val, cdiv->ns_reg);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800908 spin_unlock(&local_clock_reg_lock);
909
Matt Wagantallf82f2942012-01-27 13:56:13 -0800910 cdiv->cur_div = rate;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800911 return 0;
912}
913
914static unsigned long cdiv_clk_get_rate(struct clk *c)
915{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800916 return to_cdiv_clk(c)->cur_div;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800917}
918
919static long cdiv_clk_round_rate(struct clk *c, unsigned long rate)
920{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800921 return rate > to_cdiv_clk(c)->max_div ? -EPERM : rate;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800922}
923
924static int cdiv_clk_list_rate(struct clk *c, unsigned n)
925{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800926 return n > to_cdiv_clk(c)->max_div ? -ENXIO : n;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800927}
928
Matt Wagantalla15833b2012-04-03 11:00:56 -0700929static enum handoff cdiv_clk_handoff(struct clk *c)
Stephen Boydb8ad8222011-11-28 12:17:58 -0800930{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800931 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700932 enum handoff ret;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800933 u32 reg_val;
934
Matt Wagantallf82f2942012-01-27 13:56:13 -0800935 ret = branch_handoff(&cdiv->b, &cdiv->c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700936 if (ret == HANDOFF_DISABLED_CLK)
937 return ret;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800938
Matt Wagantallf82f2942012-01-27 13:56:13 -0800939 reg_val = readl_relaxed(cdiv->ns_reg);
940 if (reg_val & cdiv->ext_mask) {
941 cdiv->cur_div = 0;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800942 } else {
Matt Wagantallf82f2942012-01-27 13:56:13 -0800943 reg_val >>= cdiv->div_offset;
944 cdiv->cur_div = (reg_val & (cdiv->max_div - 1)) + 1;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800945 }
946
Matt Wagantalla15833b2012-04-03 11:00:56 -0700947 return HANDOFF_ENABLED_CLK;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800948}
949
Stephen Boyda52d7e32011-11-10 11:59:00 -0800950static void cdiv_clk_enable_hwcg(struct clk *c)
951{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800952 branch_enable_hwcg(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800953}
954
955static void cdiv_clk_disable_hwcg(struct clk *c)
956{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800957 branch_disable_hwcg(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800958}
959
960static int cdiv_clk_in_hwcg_mode(struct clk *c)
961{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800962 return branch_in_hwcg_mode(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800963}
964
Stephen Boydb8ad8222011-11-28 12:17:58 -0800965struct clk_ops clk_ops_cdiv = {
966 .enable = cdiv_clk_enable,
967 .disable = cdiv_clk_disable,
Stephen Boyda52d7e32011-11-10 11:59:00 -0800968 .in_hwcg_mode = cdiv_clk_in_hwcg_mode,
969 .enable_hwcg = cdiv_clk_enable_hwcg,
970 .disable_hwcg = cdiv_clk_disable_hwcg,
Stephen Boydb8ad8222011-11-28 12:17:58 -0800971 .handoff = cdiv_clk_handoff,
972 .set_rate = cdiv_clk_set_rate,
973 .get_rate = cdiv_clk_get_rate,
974 .list_rate = cdiv_clk_list_rate,
975 .round_rate = cdiv_clk_round_rate,
Stephen Boydb8ad8222011-11-28 12:17:58 -0800976};