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.
16 * clksel clocks are clocks that do not have a fixed parent, or that
17 * can divide their parent's rate, or possibly both at the same time, based
18 * on the contents of a hardware register bitfield.
20 * All of the various mux and divider settings can be encoded into
21 * struct clksel* data structures, and then these can be autogenerated
22 * from some hardware database for each new chip generation. This
23 * should avoid the need to write, review, and validate a lot of new
24 * clock code for each new chip, since it can be exported from the SoC
25 * design flow. This is now done on OMAP4.
27 * The fusion of mux and divider clocks is a software creation. In
28 * hardware reality, the multiplexer (parent selection) and the
29 * divider exist separately. XXX At some point these clksel clocks
30 * should be split into "divider" clocks and "mux" clocks to better
33 * (The name "clksel" comes from the name of the corresponding
34 * register field in the OMAP2/3 family of SoCs.)
36 * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
37 * many of the OMAP1 clocks should be convertible to use this
42 #include <linux/kernel.h>
43 #include <linux/errno.h>
44 #include <linux/clk.h>
46 #include <linux/bug.h>
48 #include <plat/clock.h>
52 /* Private functions */
55 * _get_clksel_by_parent() - return clksel struct for a given clk & parent
56 * @clk: OMAP struct clk ptr to inspect
57 * @src_clk: OMAP struct clk ptr of the parent clk to search for
59 * Scan the struct clksel array associated with the clock to find
60 * the element associated with the supplied parent clock address.
61 * Returns a pointer to the struct clksel on success or NULL on error.
63 static const struct clksel
*_get_clksel_by_parent(struct clk
*clk
,
66 const struct clksel
*clks
;
68 for (clks
= clk
->clksel
; clks
->parent
; clks
++)
69 if (clks
->parent
== src_clk
)
70 break; /* Found the requested parent */
73 /* This indicates a data problem */
74 WARN(1, "clock: Could not find parent clock %s in clksel array "
75 "of clock %s\n", src_clk
->name
, clk
->name
);
83 * _get_div_and_fieldval() - find the new clksel divisor and field value to use
84 * @src_clk: planned new parent struct clk *
85 * @clk: struct clk * that is being reparented
86 * @field_val: pointer to a u32 to contain the register data for the divisor
88 * Given an intended new parent struct clk * @src_clk, and the struct
89 * clk * @clk to the clock that is being reparented, find the
90 * appropriate rate divisor for the new clock (returned as the return
91 * value), and the corresponding register bitfield data to program to
92 * reach that divisor (returned in the u32 pointed to by @field_val).
93 * Returns 0 on error, or returns the newly-selected divisor upon
94 * success (in this latter case, the corresponding register bitfield
95 * value is passed back in the variable pointed to by @field_val)
97 static u8
_get_div_and_fieldval(struct clk
*src_clk
, struct clk
*clk
,
100 const struct clksel
*clks
;
101 const struct clksel_rate
*clkr
, *max_clkr
= NULL
;
104 clks
= _get_clksel_by_parent(clk
, src_clk
);
109 * Find the highest divisor (e.g., the one resulting in the
110 * lowest rate) to use as the default. This should avoid
111 * clock rates that are too high for the device. XXX A better
112 * solution here would be to try to determine if there is a
113 * divisor matching the original clock rate before the parent
114 * switch, and if it cannot be found, to fall back to the
117 for (clkr
= clks
->rates
; clkr
->div
; clkr
++) {
118 if (!(clkr
->flags
& cpu_mask
))
121 if (clkr
->div
> max_div
) {
128 /* This indicates an error in the clksel data */
129 WARN(1, "clock: Could not find divisor for clock %s parent %s"
130 "\n", clk
->name
, src_clk
->parent
->name
);
134 *field_val
= max_clkr
->val
;
140 * _write_clksel_reg() - program a clock's clksel register in hardware
141 * @clk: struct clk * to program
142 * @v: clksel bitfield value to program (with LSB at bit 0)
144 * Shift the clksel register bitfield value @v to its appropriate
145 * location in the clksel register and write it in. This function
146 * will ensure that the write to the clksel_reg reaches its
147 * destination before returning -- important since PRM and CM register
148 * accesses can be quite slow compared to ARM cycles -- but does not
149 * take into account any time the hardware might take to switch the
152 static void _write_clksel_reg(struct clk
*clk
, u32 field_val
)
156 v
= __raw_readl(clk
->clksel_reg
);
157 v
&= ~clk
->clksel_mask
;
158 v
|= field_val
<< __ffs(clk
->clksel_mask
);
159 __raw_writel(v
, clk
->clksel_reg
);
161 v
= __raw_readl(clk
->clksel_reg
); /* OCP barrier */
165 * _clksel_to_divisor() - turn clksel field value into integer divider
166 * @clk: OMAP struct clk to use
167 * @field_val: register field value to find
169 * Given a struct clk of a rate-selectable clksel clock, and a register field
170 * value to search for, find the corresponding clock divisor. The register
171 * field value should be pre-masked and shifted down so the LSB is at bit 0
172 * before calling. Returns 0 on error or returns the actual integer divisor
175 static u32
_clksel_to_divisor(struct clk
*clk
, u32 field_val
)
177 const struct clksel
*clks
;
178 const struct clksel_rate
*clkr
;
180 clks
= _get_clksel_by_parent(clk
, clk
->parent
);
184 for (clkr
= clks
->rates
; clkr
->div
; clkr
++) {
185 if (!(clkr
->flags
& cpu_mask
))
188 if (clkr
->val
== field_val
)
193 /* This indicates a data error */
194 WARN(1, "clock: Could not find fieldval %d for clock %s parent "
195 "%s\n", field_val
, clk
->name
, clk
->parent
->name
);
203 * _divisor_to_clksel() - turn clksel integer divisor into a field value
204 * @clk: OMAP struct clk to use
205 * @div: integer divisor to search for
207 * Given a struct clk of a rate-selectable clksel clock, and a clock
208 * divisor, find the corresponding register field value. Returns the
209 * register field value _before_ left-shifting (i.e., LSB is at bit
210 * 0); or returns 0xFFFFFFFF (~0) upon error.
212 static u32
_divisor_to_clksel(struct clk
*clk
, u32 div
)
214 const struct clksel
*clks
;
215 const struct clksel_rate
*clkr
;
217 /* should never happen */
220 clks
= _get_clksel_by_parent(clk
, clk
->parent
);
224 for (clkr
= clks
->rates
; clkr
->div
; clkr
++) {
225 if (!(clkr
->flags
& cpu_mask
))
228 if (clkr
->div
== div
)
233 pr_err("clock: Could not find divisor %d for clock %s parent "
234 "%s\n", div
, clk
->name
, clk
->parent
->name
);
242 * _read_divisor() - get current divisor applied to parent clock (from hdwr)
243 * @clk: OMAP struct clk to use.
245 * Read the current divisor register value for @clk that is programmed
246 * into the hardware, convert it into the actual divisor value, and
247 * return it; or return 0 on error.
249 static u32
_read_divisor(struct clk
*clk
)
253 if (!clk
->clksel
|| !clk
->clksel_mask
)
256 v
= __raw_readl(clk
->clksel_reg
);
257 v
&= clk
->clksel_mask
;
258 v
>>= __ffs(clk
->clksel_mask
);
260 return _clksel_to_divisor(clk
, v
);
263 /* Public functions */
266 * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
267 * @clk: OMAP struct clk to use
268 * @target_rate: desired clock rate
269 * @new_div: ptr to where we should store the divisor
271 * Finds 'best' divider value in an array based on the source and target
272 * rates. The divider array must be sorted with smallest divider first.
273 * This function is also used by the DPLL3 M2 divider code.
275 * Returns the rounded clock rate or returns 0xffffffff on error.
277 u32
omap2_clksel_round_rate_div(struct clk
*clk
, unsigned long target_rate
,
280 unsigned long test_rate
;
281 const struct clksel
*clks
;
282 const struct clksel_rate
*clkr
;
285 if (!clk
->clksel
|| !clk
->clksel_mask
)
288 pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
289 clk
->name
, target_rate
);
293 clks
= _get_clksel_by_parent(clk
, clk
->parent
);
297 for (clkr
= clks
->rates
; clkr
->div
; clkr
++) {
298 if (!(clkr
->flags
& cpu_mask
))
302 if (clkr
->div
<= last_div
)
303 pr_err("clock: clksel_rate table not sorted "
304 "for clock %s", clk
->name
);
306 last_div
= clkr
->div
;
308 test_rate
= clk
->parent
->rate
/ clkr
->div
;
310 if (test_rate
<= target_rate
)
311 break; /* found it */
315 pr_err("clock: Could not find divisor for target "
316 "rate %ld for clock %s parent %s\n", target_rate
,
317 clk
->name
, clk
->parent
->name
);
321 *new_div
= clkr
->div
;
323 pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div
,
324 (clk
->parent
->rate
/ clkr
->div
));
326 return clk
->parent
->rate
/ clkr
->div
;
330 * Clocktype interface functions to the OMAP clock code
331 * (i.e., those used in struct clk field function pointers, etc.)
335 * omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr
336 * @clk: OMAP clock struct ptr to use
338 * Given a pointer @clk to a source-selectable struct clk, read the
339 * hardware register and determine what its parent is currently set
340 * to. Update @clk's .parent field with the appropriate clk ptr. No
343 void omap2_init_clksel_parent(struct clk
*clk
)
345 const struct clksel
*clks
;
346 const struct clksel_rate
*clkr
;
349 if (!clk
->clksel
|| !clk
->clksel_mask
)
352 r
= __raw_readl(clk
->clksel_reg
) & clk
->clksel_mask
;
353 r
>>= __ffs(clk
->clksel_mask
);
355 for (clks
= clk
->clksel
; clks
->parent
&& !found
; clks
++) {
356 for (clkr
= clks
->rates
; clkr
->div
&& !found
; clkr
++) {
357 if (!(clkr
->flags
& cpu_mask
))
360 if (clkr
->val
== r
) {
361 if (clk
->parent
!= clks
->parent
) {
362 pr_debug("clock: inited %s parent "
364 clk
->name
, clks
->parent
->name
,
366 clk
->parent
->name
: "NULL"));
367 clk_reparent(clk
, clks
->parent
);
374 /* This indicates a data error */
375 WARN(!found
, "clock: %s: init parent: could not find regval %0x\n",
382 * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
385 * This function is intended to be called only by the clock framework.
386 * Each clksel clock should have its struct clk .recalc field set to this
387 * function. Returns the clock's current rate, based on its parent's rate
388 * and its current divisor setting in the hardware.
390 unsigned long omap2_clksel_recalc(struct clk
*clk
)
395 div
= _read_divisor(clk
);
399 rate
= clk
->parent
->rate
/ div
;
401 pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n", clk
->name
,
408 * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
409 * @clk: OMAP struct clk to use
410 * @target_rate: desired clock rate
412 * This function is intended to be called only by the clock framework.
413 * Finds best target rate based on the source clock and possible dividers.
414 * rates. The divider array must be sorted with smallest divider first.
416 * Returns the rounded clock rate or returns 0xffffffff on error.
418 long omap2_clksel_round_rate(struct clk
*clk
, unsigned long target_rate
)
422 return omap2_clksel_round_rate_div(clk
, target_rate
, &new_div
);
426 * omap2_clksel_set_rate() - program clock rate in hardware
427 * @clk: struct clk * to program rate
428 * @rate: target rate to program
430 * This function is intended to be called only by the clock framework.
431 * Program @clk's rate to @rate in the hardware. The clock can be
432 * either enabled or disabled when this happens, although if the clock
433 * is enabled, some downstream devices may glitch or behave
434 * unpredictably when the clock rate is changed - this depends on the
435 * hardware. This function does not currently check the usecount of
436 * the clock, so if multiple drivers are using the clock, and the rate
437 * is changed, they will all be affected without any notification.
438 * Returns -EINVAL upon error, or 0 upon success.
440 int omap2_clksel_set_rate(struct clk
*clk
, unsigned long rate
)
442 u32 field_val
, validrate
, new_div
= 0;
444 if (!clk
->clksel
|| !clk
->clksel_mask
)
447 validrate
= omap2_clksel_round_rate_div(clk
, rate
, &new_div
);
448 if (validrate
!= rate
)
451 field_val
= _divisor_to_clksel(clk
, new_div
);
455 _write_clksel_reg(clk
, field_val
);
457 clk
->rate
= clk
->parent
->rate
/ new_div
;
459 pr_debug("clock: %s: set rate to %ld\n", clk
->name
, clk
->rate
);
465 * Clksel parent setting function - not passed in struct clk function
466 * pointer - instead, the OMAP clock code currently assumes that any
467 * parent-setting clock is a clksel clock, and calls
468 * omap2_clksel_set_parent() by default
472 * omap2_clksel_set_parent() - change a clock's parent clock
473 * @clk: struct clk * of the child clock
474 * @new_parent: struct clk * of the new parent clock
476 * This function is intended to be called only by the clock framework.
477 * Change the parent clock of clock @clk to @new_parent. This is
478 * intended to be used while @clk is disabled. This function does not
479 * currently check the usecount of the clock, so if multiple drivers
480 * are using the clock, and the parent is changed, they will all be
481 * affected without any notification. Returns -EINVAL upon error, or
484 int omap2_clksel_set_parent(struct clk
*clk
, struct clk
*new_parent
)
489 if (!clk
->clksel
|| !clk
->clksel_mask
)
492 parent_div
= _get_div_and_fieldval(new_parent
, clk
, &field_val
);
496 _write_clksel_reg(clk
, field_val
);
498 clk_reparent(clk
, new_parent
);
500 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
501 clk
->rate
= new_parent
->rate
;
504 clk
->rate
/= parent_div
;
506 pr_debug("clock: %s: set parent to %s (new rate %ld)\n",
507 clk
->name
, clk
->parent
->name
, clk
->rate
);