blob: 19a980956d446d87ae281083d876e6b770084e5f [file] [log] [blame]
Paul Walmsleydf791b32010-01-26 20:13:04 -07001/*
2 * clkt_clksel.c - OMAP2/3/4 clksel clock functions
3 *
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2010 Nokia Corporation
6 *
7 * Contacts:
8 * Richard Woodruff <r-woodruff2@ti.com>
9 * Paul Walmsley
10 *
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.
14 *
Paul Walmsley435699d2010-05-18 18:40:24 -060015 *
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.
19 *
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.
26 *
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
31 * match the hardware.
32 *
33 * (The name "clksel" comes from the name of the corresponding
34 * register field in the OMAP2/3 family of SoCs.)
Paul Walmsleydf791b32010-01-26 20:13:04 -070035 *
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
38 * mechanism.
39 */
40#undef DEBUG
41
42#include <linux/kernel.h>
43#include <linux/errno.h>
44#include <linux/clk.h>
45#include <linux/io.h>
Tony Lindgrend9a5f4d2012-03-07 17:28:01 -080046#include <linux/bug.h>
Paul Walmsleydf791b32010-01-26 20:13:04 -070047
48#include <plat/clock.h>
49
50#include "clock.h"
Paul Walmsleydf791b32010-01-26 20:13:04 -070051
52/* Private functions */
53
54/**
Paul Walmsley435699d2010-05-18 18:40:24 -060055 * _get_clksel_by_parent() - return clksel struct for a given clk & parent
Paul Walmsleydf791b32010-01-26 20:13:04 -070056 * @clk: OMAP struct clk ptr to inspect
57 * @src_clk: OMAP struct clk ptr of the parent clk to search for
58 *
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.
62 */
Paul Walmsley435699d2010-05-18 18:40:24 -060063static const struct clksel *_get_clksel_by_parent(struct clk *clk,
64 struct clk *src_clk)
Paul Walmsleydf791b32010-01-26 20:13:04 -070065{
66 const struct clksel *clks;
67
Paul Walmsley435699d2010-05-18 18:40:24 -060068 for (clks = clk->clksel; clks->parent; clks++)
Paul Walmsleydf791b32010-01-26 20:13:04 -070069 if (clks->parent == src_clk)
70 break; /* Found the requested parent */
Paul Walmsleydf791b32010-01-26 20:13:04 -070071
72 if (!clks->parent) {
Paul Walmsley435699d2010-05-18 18:40:24 -060073 /* This indicates a data problem */
Paul Walmsley7852ec02012-07-26 00:54:26 -060074 WARN(1, "clock: %s: could not find parent clock %s in clksel array\n",
75 clk->name, src_clk->name);
Paul Walmsleydf791b32010-01-26 20:13:04 -070076 return NULL;
77 }
78
79 return clks;
80}
81
Paul Walmsleyd74b4942010-05-18 18:40:24 -060082/**
Paul Walmsley435699d2010-05-18 18:40:24 -060083 * _get_div_and_fieldval() - find the new clksel divisor and field value to use
Paul Walmsleyd74b4942010-05-18 18:40:24 -060084 * @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
87 *
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)
Paul Walmsleydf791b32010-01-26 20:13:04 -070096 */
Paul Walmsley435699d2010-05-18 18:40:24 -060097static u8 _get_div_and_fieldval(struct clk *src_clk, struct clk *clk,
98 u32 *field_val)
Paul Walmsleydf791b32010-01-26 20:13:04 -070099{
100 const struct clksel *clks;
Felipe Balbi405505c2011-01-16 13:22:03 +0200101 const struct clksel_rate *clkr, *max_clkr = NULL;
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600102 u8 max_div = 0;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700103
Paul Walmsley435699d2010-05-18 18:40:24 -0600104 clks = _get_clksel_by_parent(clk, src_clk);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700105 if (!clks)
106 return 0;
107
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600108 /*
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
115 * highest divisor.
116 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700117 for (clkr = clks->rates; clkr->div; clkr++) {
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600118 if (!(clkr->flags & cpu_mask))
119 continue;
120
121 if (clkr->div > max_div) {
122 max_div = clkr->div;
123 max_clkr = clkr;
124 }
Paul Walmsleydf791b32010-01-26 20:13:04 -0700125 }
126
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600127 if (max_div == 0) {
Paul Walmsley435699d2010-05-18 18:40:24 -0600128 /* This indicates an error in the clksel data */
Paul Walmsley7852ec02012-07-26 00:54:26 -0600129 WARN(1, "clock: %s: could not find divisor for parent %s\n",
130 clk->name, src_clk->parent->name);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700131 return 0;
132 }
133
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600134 *field_val = max_clkr->val;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700135
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600136 return max_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700137}
138
Paul Walmsley435699d2010-05-18 18:40:24 -0600139/**
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)
143 *
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
150 * clock source.
151 */
152static void _write_clksel_reg(struct clk *clk, u32 field_val)
153{
154 u32 v;
155
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);
160
161 v = __raw_readl(clk->clksel_reg); /* OCP barrier */
162}
163
164/**
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
168 *
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
173 * upon success.
174 */
175static u32 _clksel_to_divisor(struct clk *clk, u32 field_val)
176{
177 const struct clksel *clks;
178 const struct clksel_rate *clkr;
179
180 clks = _get_clksel_by_parent(clk, clk->parent);
181 if (!clks)
182 return 0;
183
184 for (clkr = clks->rates; clkr->div; clkr++) {
185 if (!(clkr->flags & cpu_mask))
186 continue;
187
188 if (clkr->val == field_val)
189 break;
190 }
191
192 if (!clkr->div) {
193 /* This indicates a data error */
Paul Walmsley7852ec02012-07-26 00:54:26 -0600194 WARN(1, "clock: %s: could not find fieldval %d parent %s\n",
195 clk->name, field_val, clk->parent->name);
Paul Walmsley435699d2010-05-18 18:40:24 -0600196 return 0;
197 }
198
199 return clkr->div;
200}
201
202/**
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
206 *
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.
211 */
212static u32 _divisor_to_clksel(struct clk *clk, u32 div)
213{
214 const struct clksel *clks;
215 const struct clksel_rate *clkr;
216
217 /* should never happen */
218 WARN_ON(div == 0);
219
220 clks = _get_clksel_by_parent(clk, clk->parent);
221 if (!clks)
222 return ~0;
223
224 for (clkr = clks->rates; clkr->div; clkr++) {
225 if (!(clkr->flags & cpu_mask))
226 continue;
227
228 if (clkr->div == div)
229 break;
230 }
231
232 if (!clkr->div) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600233 pr_err("clock: %s: could not find divisor %d parent %s\n",
234 clk->name, div, clk->parent->name);
Paul Walmsley435699d2010-05-18 18:40:24 -0600235 return ~0;
236 }
237
238 return clkr->val;
239}
240
241/**
242 * _read_divisor() - get current divisor applied to parent clock (from hdwr)
243 * @clk: OMAP struct clk to use.
244 *
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.
248 */
249static u32 _read_divisor(struct clk *clk)
250{
251 u32 v;
252
253 if (!clk->clksel || !clk->clksel_mask)
254 return 0;
255
256 v = __raw_readl(clk->clksel_reg);
257 v &= clk->clksel_mask;
258 v >>= __ffs(clk->clksel_mask);
259
260 return _clksel_to_divisor(clk, v);
261}
Paul Walmsleydf791b32010-01-26 20:13:04 -0700262
263/* Public functions */
264
265/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600266 * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
Paul Walmsleydf791b32010-01-26 20:13:04 -0700267 * @clk: OMAP struct clk to use
268 * @target_rate: desired clock rate
269 * @new_div: ptr to where we should store the divisor
270 *
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.
Paul Walmsley435699d2010-05-18 18:40:24 -0600273 * This function is also used by the DPLL3 M2 divider code.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700274 *
275 * Returns the rounded clock rate or returns 0xffffffff on error.
276 */
277u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
278 u32 *new_div)
279{
280 unsigned long test_rate;
281 const struct clksel *clks;
282 const struct clksel_rate *clkr;
283 u32 last_div = 0;
284
Paul Walmsley435699d2010-05-18 18:40:24 -0600285 if (!clk->clksel || !clk->clksel_mask)
286 return ~0;
287
Paul Walmsleydf791b32010-01-26 20:13:04 -0700288 pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
289 clk->name, target_rate);
290
291 *new_div = 1;
292
Paul Walmsley435699d2010-05-18 18:40:24 -0600293 clks = _get_clksel_by_parent(clk, clk->parent);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700294 if (!clks)
295 return ~0;
296
297 for (clkr = clks->rates; clkr->div; clkr++) {
298 if (!(clkr->flags & cpu_mask))
299 continue;
300
301 /* Sanity check */
302 if (clkr->div <= last_div)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600303 pr_err("clock: %s: clksel_rate table not sorted",
304 clk->name);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700305
306 last_div = clkr->div;
307
308 test_rate = clk->parent->rate / clkr->div;
309
310 if (test_rate <= target_rate)
311 break; /* found it */
312 }
313
314 if (!clkr->div) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600315 pr_err("clock: %s: could not find divisor for target rate %ld parent %s\n",
316 clk->name, target_rate, clk->parent->name);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700317 return ~0;
318 }
319
320 *new_div = clkr->div;
321
322 pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
323 (clk->parent->rate / clkr->div));
324
325 return clk->parent->rate / clkr->div;
326}
327
Paul Walmsley435699d2010-05-18 18:40:24 -0600328/*
329 * Clocktype interface functions to the OMAP clock code
330 * (i.e., those used in struct clk field function pointers, etc.)
331 */
332
Paul Walmsleydf791b32010-01-26 20:13:04 -0700333/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600334 * omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr
335 * @clk: OMAP clock struct ptr to use
336 *
337 * Given a pointer @clk to a source-selectable struct clk, read the
338 * hardware register and determine what its parent is currently set
339 * to. Update @clk's .parent field with the appropriate clk ptr. No
340 * return value.
341 */
342void omap2_init_clksel_parent(struct clk *clk)
343{
344 const struct clksel *clks;
345 const struct clksel_rate *clkr;
346 u32 r, found = 0;
347
348 if (!clk->clksel || !clk->clksel_mask)
349 return;
350
351 r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
352 r >>= __ffs(clk->clksel_mask);
353
354 for (clks = clk->clksel; clks->parent && !found; clks++) {
355 for (clkr = clks->rates; clkr->div && !found; clkr++) {
356 if (!(clkr->flags & cpu_mask))
357 continue;
358
359 if (clkr->val == r) {
360 if (clk->parent != clks->parent) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600361 pr_debug("clock: %s: inited parent to %s (was %s)\n",
Paul Walmsley435699d2010-05-18 18:40:24 -0600362 clk->name, clks->parent->name,
363 ((clk->parent) ?
364 clk->parent->name : "NULL"));
365 clk_reparent(clk, clks->parent);
366 };
367 found = 1;
368 }
369 }
370 }
371
372 /* This indicates a data error */
373 WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
374 clk->name, r);
375
376 return;
377}
378
379/**
380 * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
381 * @clk: struct clk *
382 *
383 * This function is intended to be called only by the clock framework.
384 * Each clksel clock should have its struct clk .recalc field set to this
385 * function. Returns the clock's current rate, based on its parent's rate
386 * and its current divisor setting in the hardware.
387 */
388unsigned long omap2_clksel_recalc(struct clk *clk)
389{
390 unsigned long rate;
391 u32 div = 0;
392
393 div = _read_divisor(clk);
394 if (div == 0)
395 return clk->rate;
396
397 rate = clk->parent->rate / div;
398
399 pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n", clk->name,
400 rate, div);
401
402 return rate;
403}
404
405/**
406 * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
Paul Walmsleydf791b32010-01-26 20:13:04 -0700407 * @clk: OMAP struct clk to use
408 * @target_rate: desired clock rate
409 *
Paul Walmsley435699d2010-05-18 18:40:24 -0600410 * This function is intended to be called only by the clock framework.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700411 * Finds best target rate based on the source clock and possible dividers.
412 * rates. The divider array must be sorted with smallest divider first.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700413 *
414 * Returns the rounded clock rate or returns 0xffffffff on error.
415 */
416long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
417{
418 u32 new_div;
419
420 return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
421}
422
Paul Walmsleydf791b32010-01-26 20:13:04 -0700423/**
Paul Walmsley435699d2010-05-18 18:40:24 -0600424 * omap2_clksel_set_rate() - program clock rate in hardware
425 * @clk: struct clk * to program rate
426 * @rate: target rate to program
Paul Walmsleydf791b32010-01-26 20:13:04 -0700427 *
Paul Walmsley435699d2010-05-18 18:40:24 -0600428 * This function is intended to be called only by the clock framework.
429 * Program @clk's rate to @rate in the hardware. The clock can be
430 * either enabled or disabled when this happens, although if the clock
431 * is enabled, some downstream devices may glitch or behave
432 * unpredictably when the clock rate is changed - this depends on the
433 * hardware. This function does not currently check the usecount of
434 * the clock, so if multiple drivers are using the clock, and the rate
435 * is changed, they will all be affected without any notification.
436 * Returns -EINVAL upon error, or 0 upon success.
Paul Walmsleydf791b32010-01-26 20:13:04 -0700437 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700438int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
439{
Paul Walmsley435699d2010-05-18 18:40:24 -0600440 u32 field_val, validrate, new_div = 0;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700441
Paul Walmsley435699d2010-05-18 18:40:24 -0600442 if (!clk->clksel || !clk->clksel_mask)
Paul Walmsleydf791b32010-01-26 20:13:04 -0700443 return -EINVAL;
444
445 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
446 if (validrate != rate)
447 return -EINVAL;
448
Paul Walmsley435699d2010-05-18 18:40:24 -0600449 field_val = _divisor_to_clksel(clk, new_div);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700450 if (field_val == ~0)
451 return -EINVAL;
452
Paul Walmsley435699d2010-05-18 18:40:24 -0600453 _write_clksel_reg(clk, field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700454
455 clk->rate = clk->parent->rate / new_div;
456
Paul Walmsley435699d2010-05-18 18:40:24 -0600457 pr_debug("clock: %s: set rate to %ld\n", clk->name, clk->rate);
458
Paul Walmsleydf791b32010-01-26 20:13:04 -0700459 return 0;
460}
461
Paul Walmsley435699d2010-05-18 18:40:24 -0600462/*
463 * Clksel parent setting function - not passed in struct clk function
464 * pointer - instead, the OMAP clock code currently assumes that any
465 * parent-setting clock is a clksel clock, and calls
466 * omap2_clksel_set_parent() by default
467 */
468
469/**
470 * omap2_clksel_set_parent() - change a clock's parent clock
471 * @clk: struct clk * of the child clock
472 * @new_parent: struct clk * of the new parent clock
473 *
474 * This function is intended to be called only by the clock framework.
475 * Change the parent clock of clock @clk to @new_parent. This is
476 * intended to be used while @clk is disabled. This function does not
477 * currently check the usecount of the clock, so if multiple drivers
478 * are using the clock, and the parent is changed, they will all be
479 * affected without any notification. Returns -EINVAL upon error, or
480 * 0 upon success.
481 */
Paul Walmsleydf791b32010-01-26 20:13:04 -0700482int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
483{
Paul Walmsley435699d2010-05-18 18:40:24 -0600484 u32 field_val = 0;
485 u32 parent_div;
Paul Walmsleydf791b32010-01-26 20:13:04 -0700486
Paul Walmsleyd74b4942010-05-18 18:40:24 -0600487 if (!clk->clksel || !clk->clksel_mask)
Paul Walmsleydf791b32010-01-26 20:13:04 -0700488 return -EINVAL;
489
Paul Walmsley435699d2010-05-18 18:40:24 -0600490 parent_div = _get_div_and_fieldval(new_parent, clk, &field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700491 if (!parent_div)
492 return -EINVAL;
493
Paul Walmsley435699d2010-05-18 18:40:24 -0600494 _write_clksel_reg(clk, field_val);
Paul Walmsleydf791b32010-01-26 20:13:04 -0700495
Paul Walmsleydf791b32010-01-26 20:13:04 -0700496 clk_reparent(clk, new_parent);
497
498 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
499 clk->rate = new_parent->rate;
500
501 if (parent_div > 0)
502 clk->rate /= parent_div;
503
Paul Walmsley435699d2010-05-18 18:40:24 -0600504 pr_debug("clock: %s: set parent to %s (new rate %ld)\n",
Paul Walmsleydf791b32010-01-26 20:13:04 -0700505 clk->name, clk->parent->name, clk->rate);
506
507 return 0;
508}