2 * clkt_clksel.c - OMAP2/3/4 clksel clock functions
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2010 Nokia Corporation
8 * Richard Woodruff <r-woodruff2@ti.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * XXX At some point these clksel clocks should be split into
16 * "divider" clocks and "mux" clocks to better match the hardware.
18 * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
19 * many of the OMAP1 clocks should be convertible to use this
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/clk.h>
29 #include <plat/clock.h>
33 #include "cm-regbits-24xx.h"
34 #include "cm-regbits-34xx.h"
36 /* Private functions */
39 * _omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
40 * @clk: OMAP struct clk ptr to inspect
41 * @src_clk: OMAP struct clk ptr of the parent clk to search for
43 * Scan the struct clksel array associated with the clock to find
44 * the element associated with the supplied parent clock address.
45 * Returns a pointer to the struct clksel on success or NULL on error.
47 static const struct clksel *_omap2_get_clksel_by_parent(struct clk *clk,
50 const struct clksel *clks;
55 for (clks = clk->clksel; clks->parent; clks++) {
56 if (clks->parent == src_clk)
57 break; /* Found the requested parent */
61 printk(KERN_ERR "clock: Could not find parent clock %s in "
62 "clksel array of clock %s\n", src_clk->name,
71 * _omap2_clksel_get_src_field - find the new clksel divisor to use
72 * @src_clk: planned new parent struct clk *
73 * @clk: struct clk * that is being reparented
74 * @field_val: pointer to a u32 to contain the register data for the divisor
76 * Given an intended new parent struct clk * @src_clk, and the struct
77 * clk * @clk to the clock that is being reparented, find the
78 * appropriate rate divisor for the new clock (returned as the return
79 * value), and the corresponding register bitfield data to program to
80 * reach that divisor (returned in the u32 pointed to by @field_val).
81 * Returns 0 on error, or returns the newly-selected divisor upon
82 * success (in this latter case, the corresponding register bitfield
83 * value is passed back in the variable pointed to by @field_val)
85 static u8 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
88 const struct clksel *clks;
89 const struct clksel_rate *clkr, *max_clkr;
92 clks = _omap2_get_clksel_by_parent(clk, src_clk);
97 * Find the highest divisor (e.g., the one resulting in the
98 * lowest rate) to use as the default. This should avoid
99 * clock rates that are too high for the device. XXX A better
100 * solution here would be to try to determine if there is a
101 * divisor matching the original clock rate before the parent
102 * switch, and if it cannot be found, to fall back to the
105 for (clkr = clks->rates; clkr->div; clkr++) {
106 if (!(clkr->flags & cpu_mask))
109 if (clkr->div > max_div) {
116 WARN(1, "clock: Could not find divisor for "
117 "clock %s parent %s\n", clk->name,
118 src_clk->parent->name);
122 *field_val = max_clkr->val;
128 /* Public functions */
131 * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
132 * @clk: OMAP clock struct ptr to use
134 * Given a pointer to a source-selectable struct clk, read the hardware
135 * register and determine what its parent is currently set to. Update the
136 * clk->parent field with the appropriate clk ptr.
138 void omap2_init_clksel_parent(struct clk *clk)
140 const struct clksel *clks;
141 const struct clksel_rate *clkr;
147 r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
148 r >>= __ffs(clk->clksel_mask);
150 for (clks = clk->clksel; clks->parent && !found; clks++) {
151 for (clkr = clks->rates; clkr->div && !found; clkr++) {
152 if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
153 if (clk->parent != clks->parent) {
154 pr_debug("clock: inited %s parent "
156 clk->name, clks->parent->name,
158 clk->parent->name : "NULL"));
159 clk_reparent(clk, clks->parent);
167 printk(KERN_ERR "clock: init parent: could not find "
168 "regval %0x for clock %s\n", r, clk->name);
174 * Used for clocks that are part of CLKSEL_xyz governed clocks.
175 * REVISIT: Maybe change to use clk->enable() functions like on omap1?
177 unsigned long omap2_clksel_recalc(struct clk *clk)
182 pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
184 div = omap2_clksel_get_divisor(clk);
188 rate = clk->parent->rate / div;
190 pr_debug("clock: new clock rate is %ld (div %d)\n", rate, div);
196 * omap2_clksel_round_rate_div - find divisor for the given clock and rate
197 * @clk: OMAP struct clk to use
198 * @target_rate: desired clock rate
199 * @new_div: ptr to where we should store the divisor
201 * Finds 'best' divider value in an array based on the source and target
202 * rates. The divider array must be sorted with smallest divider first.
204 * Returns the rounded clock rate or returns 0xffffffff on error.
206 u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
209 unsigned long test_rate;
210 const struct clksel *clks;
211 const struct clksel_rate *clkr;
214 pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
215 clk->name, target_rate);
219 clks = _omap2_get_clksel_by_parent(clk, clk->parent);
223 for (clkr = clks->rates; clkr->div; clkr++) {
224 if (!(clkr->flags & cpu_mask))
228 if (clkr->div <= last_div)
229 pr_err("clock: clksel_rate table not sorted "
230 "for clock %s", clk->name);
232 last_div = clkr->div;
234 test_rate = clk->parent->rate / clkr->div;
236 if (test_rate <= target_rate)
237 break; /* found it */
241 pr_err("clock: Could not find divisor for target "
242 "rate %ld for clock %s parent %s\n", target_rate,
243 clk->name, clk->parent->name);
247 *new_div = clkr->div;
249 pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
250 (clk->parent->rate / clkr->div));
252 return clk->parent->rate / clkr->div;
256 * omap2_clksel_round_rate - find rounded rate for the given clock and rate
257 * @clk: OMAP struct clk to use
258 * @target_rate: desired clock rate
260 * Compatibility wrapper for OMAP clock framework
261 * Finds best target rate based on the source clock and possible dividers.
262 * rates. The divider array must be sorted with smallest divider first.
263 * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
264 * they are only settable as part of virtual_prcm set.
266 * Returns the rounded clock rate or returns 0xffffffff on error.
268 long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
272 return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
276 /* Given a clock and a rate apply a clock specific rounding function */
277 long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
280 return clk->round_rate(clk, rate);
286 * omap2_clksel_to_divisor() - turn clksel field value into integer divider
287 * @clk: OMAP struct clk to use
288 * @field_val: register field value to find
290 * Given a struct clk of a rate-selectable clksel clock, and a register field
291 * value to search for, find the corresponding clock divisor. The register
292 * field value should be pre-masked and shifted down so the LSB is at bit 0
293 * before calling. Returns 0 on error
295 u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
297 const struct clksel *clks;
298 const struct clksel_rate *clkr;
300 clks = _omap2_get_clksel_by_parent(clk, clk->parent);
304 for (clkr = clks->rates; clkr->div; clkr++) {
305 if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
310 printk(KERN_ERR "clock: Could not find fieldval %d for "
311 "clock %s parent %s\n", field_val, clk->name,
320 * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
321 * @clk: OMAP struct clk to use
322 * @div: integer divisor to search for
324 * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
325 * find the corresponding register field value. The return register value is
326 * the value before left-shifting. Returns ~0 on error
328 u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
330 const struct clksel *clks;
331 const struct clksel_rate *clkr;
333 /* should never happen */
336 clks = _omap2_get_clksel_by_parent(clk, clk->parent);
340 for (clkr = clks->rates; clkr->div; clkr++) {
341 if ((clkr->flags & cpu_mask) && (clkr->div == div))
346 printk(KERN_ERR "clock: Could not find divisor %d for "
347 "clock %s parent %s\n", div, clk->name,
356 * omap2_clksel_get_divisor - get current divider applied to parent clock.
357 * @clk: OMAP struct clk to use.
359 * Returns the integer divisor upon success or 0 on error.
361 u32 omap2_clksel_get_divisor(struct clk *clk)
365 if (!clk->clksel_mask)
368 v = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
369 v >>= __ffs(clk->clksel_mask);
371 return omap2_clksel_to_divisor(clk, v);
374 int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
376 u32 v, field_val, validrate, new_div = 0;
378 if (!clk->clksel_mask)
381 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
382 if (validrate != rate)
385 field_val = omap2_divisor_to_clksel(clk, new_div);
389 v = __raw_readl(clk->clksel_reg);
390 v &= ~clk->clksel_mask;
391 v |= field_val << __ffs(clk->clksel_mask);
392 __raw_writel(v, clk->clksel_reg);
393 v = __raw_readl(clk->clksel_reg); /* OCP barrier */
395 clk->rate = clk->parent->rate / new_div;
400 int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
402 u32 field_val, v, parent_div;
404 if (!clk->clksel || !clk->clksel_mask)
407 parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
411 /* Set new source value (previous dividers if any in effect) */
412 v = __raw_readl(clk->clksel_reg);
413 v &= ~clk->clksel_mask;
414 v |= field_val << __ffs(clk->clksel_mask);
415 __raw_writel(v, clk->clksel_reg);
416 v = __raw_readl(clk->clksel_reg); /* OCP barrier */
418 clk_reparent(clk, new_parent);
420 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
421 clk->rate = new_parent->rate;
424 clk->rate /= parent_div;
426 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
427 clk->name, clk->parent->name, clk->rate);