blob: 8222e8748758be844d494546e3d4655e4556efd0 [file] [log] [blame]
Matt Wagantallab1adce2012-01-24 14:57:24 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. 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>
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. */
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 Wagantallf82f2942012-01-27 13:56:13 -0800279void __branch_clk_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
Matt Wagantallf82f2942012-01-27 13:56:13 -0800323 WARN(rcg->current_freq == &rcg_dummy_freq,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 "Attempting to enable %s before setting its rate. "
Matt Wagantallf82f2942012-01-27 13:56:13 -0800325 "Set the rate first!\n", rcg->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326
327 /*
328 * Program the NS register, if applicable. NS registers are not
329 * set in the set_rate path because power can be saved by deferring
330 * the selection of a clocked source until the clock is enabled.
331 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800332 if (rcg->ns_mask) {
333 reg_val = readl_relaxed(rcg->ns_reg);
334 reg_val &= ~(rcg->ns_mask);
335 reg_val |= (rcg->current_freq->ns_val & rcg->ns_mask);
336 writel_relaxed(reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 }
338
339 /* Enable MN counter, if applicable. */
340 reg_val = readl_relaxed(reg);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800341 if (rcg->current_freq->md_val) {
342 reg_val |= rcg->mnd_en_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 writel_relaxed(reg_val, reg);
344 }
345 /* Enable root. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800346 if (rcg->root_en_mask) {
347 reg_val |= rcg->root_en_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 writel_relaxed(reg_val, reg);
349 }
Matt Wagantallf82f2942012-01-27 13:56:13 -0800350 __branch_clk_enable_reg(&rcg->b, rcg->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351}
352
353/* Perform any register operations required to disable the branch. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800354u32 __branch_clk_disable_reg(const struct branch *b, const char *name)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355{
356 u32 reg_val;
357
Matt Wagantallf82f2942012-01-27 13:56:13 -0800358 reg_val = readl_relaxed(b->ctl_reg);
359 if (b->en_mask) {
360 reg_val &= ~(b->en_mask);
361 writel_relaxed(reg_val, b->ctl_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 }
363
364 /*
365 * Use a memory barrier since some halt status registers are
366 * not within the same K 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 disable.
369 */
370 mb();
371
Stephen Boyda52d7e32011-11-10 11:59:00 -0800372 /* Skip checking halt bit if the clock is in hardware gated mode */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800373 if (branch_in_hwcg_mode(b))
Stephen Boyda52d7e32011-11-10 11:59:00 -0800374 return reg_val;
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376 /* Wait for clock to disable before continuing. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800377 if (b->halt_check == DELAY || b->halt_check == ENABLE_VOTED
378 || b->halt_check == HALT_VOTED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 udelay(HALT_CHECK_DELAY_US);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800380 } else if (b->halt_check == ENABLE || b->halt_check == HALT) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 int count;
382
383 /* Wait up to HALT_CHECK_MAX_LOOPS for clock to disable. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800384 for (count = HALT_CHECK_MAX_LOOPS; !branch_clk_is_halted(b)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 && count > 0; count--)
386 udelay(1);
387 WARN(count == 0, "%s status stuck at 'on'", name);
388 }
389
390 return reg_val;
391}
392
393/* Perform any register operations required to disable the generator. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800394static void __rcg_clk_disable_reg(struct rcg_clk *rcg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800396 void __iomem *const reg = rcg->b.ctl_reg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 uint32_t reg_val;
398
Matt Wagantallf82f2942012-01-27 13:56:13 -0800399 reg_val = __branch_clk_disable_reg(&rcg->b, rcg->c.dbg_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 /* Disable root. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800401 if (rcg->root_en_mask) {
402 reg_val &= ~(rcg->root_en_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 writel_relaxed(reg_val, reg);
404 }
405 /* Disable MN counter, if applicable. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800406 if (rcg->current_freq->md_val) {
407 reg_val &= ~(rcg->mnd_en_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 writel_relaxed(reg_val, reg);
409 }
410 /*
411 * Program NS register to low-power value with an un-clocked or
412 * slowly-clocked source selected.
413 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800414 if (rcg->ns_mask) {
415 reg_val = readl_relaxed(rcg->ns_reg);
416 reg_val &= ~(rcg->ns_mask);
417 reg_val |= (rcg->freq_tbl->ns_val & rcg->ns_mask);
418 writel_relaxed(reg_val, rcg->ns_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 }
420}
421
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700422/* Enable a rate-settable clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700423static int rcg_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424{
425 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800426 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
428 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800429 __rcg_clk_enable_reg(rcg);
430 rcg->enabled = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700432
433 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434}
435
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700436/* Disable a rate-settable clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700437static void rcg_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438{
439 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800440 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
442 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800443 __rcg_clk_disable_reg(rcg);
444 rcg->enabled = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
446}
447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448/*
449 * Frequency-related functions
450 */
451
Matt Wagantallab1adce2012-01-24 14:57:24 -0800452/* Set a clock to an exact rate. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700453static int rcg_clk_set_rate(struct clk *c, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800455 struct rcg_clk *rcg = to_rcg_clk(c);
Matt Wagantallab1adce2012-01-24 14:57:24 -0800456 struct clk_freq_tbl *nf, *cf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 struct clk *chld;
Matt Wagantallab1adce2012-01-24 14:57:24 -0800458 int rc = 0;
459
Matt Wagantallf82f2942012-01-27 13:56:13 -0800460 for (nf = rcg->freq_tbl; nf->freq_hz != FREQ_END
Matt Wagantallab1adce2012-01-24 14:57:24 -0800461 && nf->freq_hz != rate; nf++)
462 ;
463
464 if (nf->freq_hz == FREQ_END)
465 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466
Matt Wagantallf82f2942012-01-27 13:56:13 -0800467 cf = rcg->current_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468
Matt Wagantallf82f2942012-01-27 13:56:13 -0800469 if (rcg->enabled) {
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700470 /* Enable source clock dependency for the new freq. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 rc = clk_enable(nf->src_clk);
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700472 if (rc)
473 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 }
475
476 spin_lock(&local_clock_reg_lock);
477
478 /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800479 if (!rcg->bank_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 /* Disable all branches to prevent glitches. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800481 list_for_each_entry(chld, &rcg->c.children, siblings) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 struct branch_clk *x = to_branch_clk(chld);
483 /*
484 * We don't need to grab the child's lock because
485 * we hold the local_clock_reg_lock and 'enabled' is
486 * only modified within lock.
487 */
488 if (x->enabled)
489 __branch_clk_disable_reg(&x->b, x->c.dbg_name);
490 }
Matt Wagantallf82f2942012-01-27 13:56:13 -0800491 if (rcg->enabled)
492 __rcg_clk_disable_reg(rcg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 }
494
495 /* Perform clock-specific frequency switch operations. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800496 BUG_ON(!rcg->set_rate);
497 rcg->set_rate(rcg, nf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
499 /*
Matt Wagantall0625ea02011-07-13 18:51:56 -0700500 * Current freq must be updated before __rcg_clk_enable_reg()
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 * is called to make sure the MNCNTR_EN bit is set correctly.
502 */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800503 rcg->current_freq = nf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504
505 /* Enable any clocks that were disabled. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800506 if (!rcg->bank_info) {
507 if (rcg->enabled)
508 __rcg_clk_enable_reg(rcg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 /* Enable only branches that were ON before. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800510 list_for_each_entry(chld, &rcg->c.children, siblings) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511 struct branch_clk *x = to_branch_clk(chld);
512 if (x->enabled)
513 __branch_clk_enable_reg(&x->b, x->c.dbg_name);
514 }
515 }
516
517 spin_unlock(&local_clock_reg_lock);
518
Matt Wagantalle18bbc82011-10-06 10:07:28 -0700519 /* Release source requirements of the old freq. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800520 if (rcg->enabled)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521 clk_disable(cf->src_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
523 return rc;
524}
525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526/* Check if a clock is currently enabled. */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800527static int rcg_clk_is_enabled(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800529 return to_rcg_clk(c)->enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530}
531
532/* Return a supported rate that's at least the specified rate. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700533static long rcg_clk_round_rate(struct clk *c, unsigned long rate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800535 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 struct clk_freq_tbl *f;
537
Matt Wagantallf82f2942012-01-27 13:56:13 -0800538 for (f = rcg->freq_tbl; f->freq_hz != FREQ_END; f++)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 if (f->freq_hz >= rate)
540 return f->freq_hz;
541
542 return -EPERM;
543}
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545/* Return the nth supported frequency for a given clock. */
Stephen Boyd409b8b42012-04-10 12:12:56 -0700546static int rcg_clk_list_rate(struct clk *c, unsigned n)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800548 struct rcg_clk *rcg = to_rcg_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549
Matt Wagantallf82f2942012-01-27 13:56:13 -0800550 if (!rcg->freq_tbl || rcg->freq_tbl->freq_hz == FREQ_END)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 return -ENXIO;
552
Matt Wagantallf82f2942012-01-27 13:56:13 -0800553 return (rcg->freq_tbl + n)->freq_hz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554}
555
Matt Wagantallf82f2942012-01-27 13:56:13 -0800556static struct clk *rcg_clk_get_parent(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800558 return to_rcg_clk(c)->current_freq->src_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559}
560
Stephen Boyda52d7e32011-11-10 11:59:00 -0800561/* Disable hw clock gating if not set at boot */
Matt Wagantallf82f2942012-01-27 13:56:13 -0800562enum handoff branch_handoff(struct branch *b, struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800563{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800564 if (!branch_in_hwcg_mode(b)) {
565 b->hwcg_mask = 0;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800566 c->flags &= ~CLKFLAG_HWCG;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800567 if (readl_relaxed(b->ctl_reg) & b->en_mask)
Matt Wagantalla15833b2012-04-03 11:00:56 -0700568 return HANDOFF_ENABLED_CLK;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800569 } else {
570 c->flags |= CLKFLAG_HWCG;
571 }
Matt Wagantalla15833b2012-04-03 11:00:56 -0700572 return HANDOFF_DISABLED_CLK;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800573}
574
Stephen Boyd409b8b42012-04-10 12:12:56 -0700575static enum handoff branch_clk_handoff(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800576{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800577 struct branch_clk *br = to_branch_clk(c);
578 return branch_handoff(&br->b, &br->c);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800579}
580
Stephen Boyd409b8b42012-04-10 12:12:56 -0700581static enum handoff rcg_clk_handoff(struct clk *c)
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700582{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800583 struct rcg_clk *rcg = to_rcg_clk(c);
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700584 uint32_t ctl_val, ns_val, md_val, ns_mask;
585 struct clk_freq_tbl *freq;
Matt Wagantalla15833b2012-04-03 11:00:56 -0700586 enum handoff ret;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800587
Matt Wagantallf82f2942012-01-27 13:56:13 -0800588 ctl_val = readl_relaxed(rcg->b.ctl_reg);
589 ret = branch_handoff(&rcg->b, &rcg->c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700590 if (ret == HANDOFF_DISABLED_CLK)
591 return HANDOFF_DISABLED_CLK;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700592
Matt Wagantallf82f2942012-01-27 13:56:13 -0800593 if (rcg->bank_info) {
594 const struct bank_masks *bank_masks = rcg->bank_info;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700595 const struct bank_mask_info *bank_info;
Stephen Boydc78d9a72011-07-20 00:46:24 -0700596 if (!(ctl_val & bank_masks->bank_sel_mask))
597 bank_info = &bank_masks->bank0_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700598 else
Stephen Boydc78d9a72011-07-20 00:46:24 -0700599 bank_info = &bank_masks->bank1_mask;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700600
601 ns_mask = bank_info->ns_mask;
Tianyi Goue46938b2012-01-31 12:30:12 -0800602 md_val = bank_info->md_reg ?
603 readl_relaxed(bank_info->md_reg) : 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700604 } else {
Matt Wagantallf82f2942012-01-27 13:56:13 -0800605 ns_mask = rcg->ns_mask;
606 md_val = rcg->md_reg ? readl_relaxed(rcg->md_reg) : 0;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700607 }
Matt Wagantalla15833b2012-04-03 11:00:56 -0700608 if (!ns_mask)
609 return HANDOFF_UNKNOWN_RATE;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800610 ns_val = readl_relaxed(rcg->ns_reg) & ns_mask;
611 for (freq = rcg->freq_tbl; freq->freq_hz != FREQ_END; freq++) {
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700612 if ((freq->ns_val & ns_mask) == ns_val &&
Matt Wagantall2a59b212012-06-12 19:16:01 -0700613 (!freq->md_val || freq->md_val == md_val))
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700614 break;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700615 }
616 if (freq->freq_hz == FREQ_END)
Matt Wagantalla15833b2012-04-03 11:00:56 -0700617 return HANDOFF_UNKNOWN_RATE;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700618
Matt Wagantallf82f2942012-01-27 13:56:13 -0800619 rcg->current_freq = freq;
Stephen Boyde891ca32012-03-19 12:16:36 -0700620 c->rate = freq->freq_hz;
Matt Wagantall271a6cd2011-09-20 16:06:31 -0700621
Matt Wagantalla15833b2012-04-03 11:00:56 -0700622 return HANDOFF_ENABLED_CLK;
Matt Wagantall14dc2af2011-08-12 13:16:06 -0700623}
624
Matt Wagantallae053222012-05-14 19:42:07 -0700625struct clk_ops clk_ops_empty;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626
627struct fixed_clk gnd_clk = {
628 .c = {
629 .dbg_name = "ground_clk",
Matt Wagantallae053222012-05-14 19:42:07 -0700630 .ops = &clk_ops_empty,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 CLK_INIT(gnd_clk.c),
632 },
633};
634
Matt Wagantallf82f2942012-01-27 13:56:13 -0800635static int branch_clk_enable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636{
637 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800638 struct branch_clk *br = to_branch_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639
640 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800641 __branch_clk_enable_reg(&br->b, br->c.dbg_name);
642 br->enabled = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
644
645 return 0;
646}
647
Matt Wagantallf82f2942012-01-27 13:56:13 -0800648static void branch_clk_disable(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649{
650 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800651 struct branch_clk *br = to_branch_clk(c);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652
653 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800654 __branch_clk_disable_reg(&br->b, br->c.dbg_name);
655 br->enabled = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657}
658
Matt Wagantallf82f2942012-01-27 13:56:13 -0800659static struct clk *branch_clk_get_parent(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800661 return to_branch_clk(c)->parent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662}
663
Matt Wagantallf82f2942012-01-27 13:56:13 -0800664static int branch_clk_is_enabled(struct clk *c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800666 return to_branch_clk(c)->enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667}
668
Stephen Boyda52d7e32011-11-10 11:59:00 -0800669static void branch_enable_hwcg(struct branch *b)
670{
671 unsigned long flags;
672 u32 reg_val;
673
674 spin_lock_irqsave(&local_clock_reg_lock, flags);
675 reg_val = readl_relaxed(b->hwcg_reg);
676 reg_val |= b->hwcg_mask;
677 writel_relaxed(reg_val, b->hwcg_reg);
678 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
679}
680
681static void branch_disable_hwcg(struct branch *b)
682{
683 unsigned long flags;
684 u32 reg_val;
685
686 spin_lock_irqsave(&local_clock_reg_lock, flags);
687 reg_val = readl_relaxed(b->hwcg_reg);
688 reg_val &= ~b->hwcg_mask;
689 writel_relaxed(reg_val, b->hwcg_reg);
690 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
691}
692
Matt Wagantallf82f2942012-01-27 13:56:13 -0800693static void branch_clk_enable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800694{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800695 branch_enable_hwcg(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800696}
697
Matt Wagantallf82f2942012-01-27 13:56:13 -0800698static void branch_clk_disable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800699{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800700 branch_disable_hwcg(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800701}
702
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800703static int branch_set_flags(struct branch *b, unsigned flags)
704{
705 unsigned long irq_flags;
706 u32 reg_val;
707 int ret = 0;
708
709 if (!b->retain_reg)
710 return -EPERM;
711
712 spin_lock_irqsave(&local_clock_reg_lock, irq_flags);
713 reg_val = readl_relaxed(b->retain_reg);
714 switch (flags) {
715 case CLKFLAG_RETAIN:
716 reg_val |= b->retain_mask;
717 break;
718 case CLKFLAG_NORETAIN:
719 reg_val &= ~b->retain_mask;
720 break;
721 default:
722 ret = -EINVAL;
723 }
724 writel_relaxed(reg_val, b->retain_reg);
725 spin_unlock_irqrestore(&local_clock_reg_lock, irq_flags);
726
727 return ret;
728}
729
Stephen Boyd409b8b42012-04-10 12:12:56 -0700730static int branch_clk_set_flags(struct clk *clk, unsigned flags)
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800731{
732 return branch_set_flags(&to_branch_clk(clk)->b, flags);
733}
734
Stephen Boyd409b8b42012-04-10 12:12:56 -0700735static int branch_clk_in_hwcg_mode(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800736{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800737 return branch_in_hwcg_mode(&to_branch_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800738}
739
Matt Wagantallf82f2942012-01-27 13:56:13 -0800740static void rcg_clk_enable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800741{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800742 branch_enable_hwcg(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800743}
744
Matt Wagantallf82f2942012-01-27 13:56:13 -0800745static void rcg_clk_disable_hwcg(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800746{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800747 branch_disable_hwcg(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800748}
749
Stephen Boyd409b8b42012-04-10 12:12:56 -0700750static int rcg_clk_in_hwcg_mode(struct clk *c)
Stephen Boyda52d7e32011-11-10 11:59:00 -0800751{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800752 return branch_in_hwcg_mode(&to_rcg_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800753}
754
Stephen Boyd409b8b42012-04-10 12:12:56 -0700755static int rcg_clk_set_flags(struct clk *clk, unsigned flags)
Matt Wagantall7e0b6c92012-01-20 18:48:05 -0800756{
757 return branch_set_flags(&to_rcg_clk(clk)->b, flags);
758}
759
Stephen Boyda52d7e32011-11-10 11:59:00 -0800760int branch_reset(struct branch *b, enum clk_reset_action action)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761{
762 int ret = 0;
763 u32 reg_val;
764 unsigned long flags;
765
Stephen Boyda52d7e32011-11-10 11:59:00 -0800766 if (!b->reset_reg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 return -EPERM;
768
Stephen Boyda52d7e32011-11-10 11:59:00 -0800769 /* Disable hw gating when asserting a reset */
770 if (b->hwcg_mask && action == CLK_RESET_ASSERT)
771 branch_disable_hwcg(b);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772
Stephen Boyda52d7e32011-11-10 11:59:00 -0800773 spin_lock_irqsave(&local_clock_reg_lock, flags);
774 /* Assert/Deassert reset */
775 reg_val = readl_relaxed(b->reset_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 switch (action) {
777 case CLK_RESET_ASSERT:
Stephen Boyda52d7e32011-11-10 11:59:00 -0800778 reg_val |= b->reset_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 break;
780 case CLK_RESET_DEASSERT:
Stephen Boyda52d7e32011-11-10 11:59:00 -0800781 reg_val &= ~b->reset_mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 break;
783 default:
784 ret = -EINVAL;
785 }
Stephen Boyda52d7e32011-11-10 11:59:00 -0800786 writel_relaxed(reg_val, b->reset_reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
788
Stephen Boyda52d7e32011-11-10 11:59:00 -0800789 /* Enable hw gating when deasserting a reset */
790 if (b->hwcg_mask && action == CLK_RESET_DEASSERT)
791 branch_enable_hwcg(b);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 /* Make sure write is issued before returning. */
793 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 return ret;
795}
796
Matt Wagantallf82f2942012-01-27 13:56:13 -0800797static int branch_clk_reset(struct clk *c, enum clk_reset_action action)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800799 return branch_reset(&to_branch_clk(c)->b, action);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800}
Stephen Boydb8ad8222011-11-28 12:17:58 -0800801
Stephen Boyd409b8b42012-04-10 12:12:56 -0700802struct clk_ops clk_ops_branch = {
803 .enable = branch_clk_enable,
804 .disable = branch_clk_disable,
805 .enable_hwcg = branch_clk_enable_hwcg,
806 .disable_hwcg = branch_clk_disable_hwcg,
807 .in_hwcg_mode = branch_clk_in_hwcg_mode,
808 .auto_off = branch_clk_disable,
809 .is_enabled = branch_clk_is_enabled,
810 .reset = branch_clk_reset,
811 .get_parent = branch_clk_get_parent,
812 .handoff = branch_clk_handoff,
813 .set_flags = branch_clk_set_flags,
814};
815
816struct clk_ops clk_ops_reset = {
817 .reset = branch_clk_reset,
818};
819
Matt Wagantallf82f2942012-01-27 13:56:13 -0800820static int rcg_clk_reset(struct clk *c, enum clk_reset_action action)
Stephen Boyd7bf28142011-12-07 00:30:52 -0800821{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800822 return branch_reset(&to_rcg_clk(c)->b, action);
Stephen Boyd7bf28142011-12-07 00:30:52 -0800823}
824
Stephen Boyd409b8b42012-04-10 12:12:56 -0700825struct clk_ops clk_ops_rcg = {
826 .enable = rcg_clk_enable,
827 .disable = rcg_clk_disable,
828 .enable_hwcg = rcg_clk_enable_hwcg,
829 .disable_hwcg = rcg_clk_disable_hwcg,
830 .in_hwcg_mode = rcg_clk_in_hwcg_mode,
831 .auto_off = rcg_clk_disable,
832 .handoff = rcg_clk_handoff,
833 .set_rate = rcg_clk_set_rate,
834 .list_rate = rcg_clk_list_rate,
835 .is_enabled = rcg_clk_is_enabled,
836 .round_rate = rcg_clk_round_rate,
837 .reset = rcg_clk_reset,
838 .get_parent = rcg_clk_get_parent,
839 .set_flags = rcg_clk_set_flags,
840};
841
Stephen Boydb8ad8222011-11-28 12:17:58 -0800842static int cdiv_clk_enable(struct clk *c)
843{
844 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800845 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800846
847 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800848 __branch_clk_enable_reg(&cdiv->b, cdiv->c.dbg_name);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800849 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
850
851 return 0;
852}
853
854static void cdiv_clk_disable(struct clk *c)
855{
856 unsigned long flags;
Matt Wagantallf82f2942012-01-27 13:56:13 -0800857 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800858
859 spin_lock_irqsave(&local_clock_reg_lock, flags);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800860 __branch_clk_disable_reg(&cdiv->b, cdiv->c.dbg_name);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800861 spin_unlock_irqrestore(&local_clock_reg_lock, flags);
862}
863
864static int cdiv_clk_set_rate(struct clk *c, unsigned long rate)
865{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800866 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800867 u32 reg_val;
868
Matt Wagantallf82f2942012-01-27 13:56:13 -0800869 if (rate > cdiv->max_div)
Stephen Boydb8ad8222011-11-28 12:17:58 -0800870 return -EINVAL;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800871
872 spin_lock(&local_clock_reg_lock);
Matt Wagantallf82f2942012-01-27 13:56:13 -0800873 reg_val = readl_relaxed(cdiv->ns_reg);
874 reg_val &= ~(cdiv->ext_mask | (cdiv->max_div - 1) << cdiv->div_offset);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800875 /* Non-zero rates mean set a divider, zero means use external input */
876 if (rate)
Matt Wagantallf82f2942012-01-27 13:56:13 -0800877 reg_val |= (rate - 1) << cdiv->div_offset;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800878 else
Matt Wagantallf82f2942012-01-27 13:56:13 -0800879 reg_val |= cdiv->ext_mask;
880 writel_relaxed(reg_val, cdiv->ns_reg);
Stephen Boydb8ad8222011-11-28 12:17:58 -0800881 spin_unlock(&local_clock_reg_lock);
882
Matt Wagantallf82f2942012-01-27 13:56:13 -0800883 cdiv->cur_div = rate;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800884 return 0;
885}
886
887static unsigned long cdiv_clk_get_rate(struct clk *c)
888{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800889 return to_cdiv_clk(c)->cur_div;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800890}
891
892static long cdiv_clk_round_rate(struct clk *c, unsigned long rate)
893{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800894 return rate > to_cdiv_clk(c)->max_div ? -EPERM : rate;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800895}
896
897static int cdiv_clk_list_rate(struct clk *c, unsigned n)
898{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800899 return n > to_cdiv_clk(c)->max_div ? -ENXIO : n;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800900}
901
Matt Wagantalla15833b2012-04-03 11:00:56 -0700902static enum handoff cdiv_clk_handoff(struct clk *c)
Stephen Boydb8ad8222011-11-28 12:17:58 -0800903{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800904 struct cdiv_clk *cdiv = to_cdiv_clk(c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700905 enum handoff ret;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800906 u32 reg_val;
907
Matt Wagantallf82f2942012-01-27 13:56:13 -0800908 ret = branch_handoff(&cdiv->b, &cdiv->c);
Matt Wagantalla15833b2012-04-03 11:00:56 -0700909 if (ret == HANDOFF_DISABLED_CLK)
910 return ret;
Stephen Boyda52d7e32011-11-10 11:59:00 -0800911
Matt Wagantallf82f2942012-01-27 13:56:13 -0800912 reg_val = readl_relaxed(cdiv->ns_reg);
913 if (reg_val & cdiv->ext_mask) {
914 cdiv->cur_div = 0;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800915 } else {
Matt Wagantallf82f2942012-01-27 13:56:13 -0800916 reg_val >>= cdiv->div_offset;
917 cdiv->cur_div = (reg_val & (cdiv->max_div - 1)) + 1;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800918 }
919
Matt Wagantalla15833b2012-04-03 11:00:56 -0700920 return HANDOFF_ENABLED_CLK;
Stephen Boydb8ad8222011-11-28 12:17:58 -0800921}
922
Stephen Boyda52d7e32011-11-10 11:59:00 -0800923static void cdiv_clk_enable_hwcg(struct clk *c)
924{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800925 branch_enable_hwcg(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800926}
927
928static void cdiv_clk_disable_hwcg(struct clk *c)
929{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800930 branch_disable_hwcg(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800931}
932
933static int cdiv_clk_in_hwcg_mode(struct clk *c)
934{
Matt Wagantallf82f2942012-01-27 13:56:13 -0800935 return branch_in_hwcg_mode(&to_cdiv_clk(c)->b);
Stephen Boyda52d7e32011-11-10 11:59:00 -0800936}
937
Stephen Boydb8ad8222011-11-28 12:17:58 -0800938struct clk_ops clk_ops_cdiv = {
939 .enable = cdiv_clk_enable,
940 .disable = cdiv_clk_disable,
Stephen Boyda52d7e32011-11-10 11:59:00 -0800941 .in_hwcg_mode = cdiv_clk_in_hwcg_mode,
942 .enable_hwcg = cdiv_clk_enable_hwcg,
943 .disable_hwcg = cdiv_clk_disable_hwcg,
Stephen Boydb8ad8222011-11-28 12:17:58 -0800944 .auto_off = cdiv_clk_disable,
945 .handoff = cdiv_clk_handoff,
946 .set_rate = cdiv_clk_set_rate,
947 .get_rate = cdiv_clk_get_rate,
948 .list_rate = cdiv_clk_list_rate,
949 .round_rate = cdiv_clk_round_rate,
Stephen Boydb8ad8222011-11-28 12:17:58 -0800950};