2 * OMAP3-specific clock framework functions
4 * Copyright (C) 2007-2008 Texas Instruments, Inc.
5 * Copyright (C) 2007-2008 Nokia Corporation
7 * Written by Paul Walmsley
8 * Testing and integration fixes by Jouni Högander
10 * Parts of this code are based on code written by
11 * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/errno.h>
24 #include <linux/delay.h>
25 #include <linux/clk.h>
27 #include <linux/limits.h>
28 #include <linux/bitops.h>
30 #include <mach/clock.h>
31 #include <mach/sram.h>
32 #include <asm/div64.h>
37 #include "prm-regbits-34xx.h"
39 #include "cm-regbits-34xx.h"
41 static const struct clkops clkops_noncore_dpll_ops;
43 #include "clock34xx.h"
45 /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
46 #define DPLL_AUTOIDLE_DISABLE 0x0
47 #define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
49 #define MAX_DPLL_WAIT_TRIES 1000000
52 * omap3_dpll_recalc - recalculate DPLL rate
53 * @clk: DPLL struct clk
55 * Recalculate and propagate the DPLL rate.
57 static void omap3_dpll_recalc(struct clk *clk)
59 clk->rate = omap2_get_dpll_rate(clk);
62 /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
63 static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
65 const struct dpll_data *dd;
70 v = __raw_readl(dd->control_reg);
71 v &= ~dd->enable_mask;
72 v |= clken_bits << __ffs(dd->enable_mask);
73 __raw_writel(v, dd->control_reg);
76 /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
77 static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
79 const struct dpll_data *dd;
86 state <<= dd->idlest_bit;
87 idlest_mask = 1 << dd->idlest_bit;
89 while (((__raw_readl(dd->idlest_reg) & idlest_mask) != state) &&
90 i < MAX_DPLL_WAIT_TRIES) {
95 if (i == MAX_DPLL_WAIT_TRIES) {
96 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
97 clk->name, (state) ? "locked" : "bypassed");
99 pr_debug("clock: %s transition to '%s' in %d loops\n",
100 clk->name, (state) ? "locked" : "bypassed", i);
108 /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
111 * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
112 * @clk: pointer to a DPLL struct clk
114 * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
115 * readiness before returning. Will save and restore the DPLL's
116 * autoidle state across the enable, per the CDP code. If the DPLL
117 * locked successfully, return 0; if the DPLL did not lock in the time
118 * allotted, or DPLL3 was passed in, return -EINVAL.
120 static int _omap3_noncore_dpll_lock(struct clk *clk)
125 if (clk == &dpll3_ck)
128 pr_debug("clock: locking DPLL %s\n", clk->name);
130 ai = omap3_dpll_autoidle_read(clk);
132 _omap3_dpll_write_clken(clk, DPLL_LOCKED);
136 * If no downstream clocks are enabled, CM_IDLEST bit
137 * may never become active, so don't wait for DPLL to lock.
140 omap3_dpll_allow_idle(clk);
142 r = _omap3_wait_dpll_status(clk, 1);
143 omap3_dpll_deny_idle(clk);
150 * omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
151 * @clk: pointer to a DPLL struct clk
153 * Instructs a non-CORE DPLL to enter low-power bypass mode. In
154 * bypass mode, the DPLL's rate is set equal to its parent clock's
155 * rate. Waits for the DPLL to report readiness before returning.
156 * Will save and restore the DPLL's autoidle state across the enable,
157 * per the CDP code. If the DPLL entered bypass mode successfully,
158 * return 0; if the DPLL did not enter bypass in the time allotted, or
159 * DPLL3 was passed in, or the DPLL does not support low-power bypass,
162 static int _omap3_noncore_dpll_bypass(struct clk *clk)
167 if (clk == &dpll3_ck)
170 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
173 pr_debug("clock: configuring DPLL %s for low-power bypass\n",
176 ai = omap3_dpll_autoidle_read(clk);
178 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
180 r = _omap3_wait_dpll_status(clk, 0);
183 omap3_dpll_allow_idle(clk);
185 omap3_dpll_deny_idle(clk);
191 * _omap3_noncore_dpll_stop - instruct a DPLL to stop
192 * @clk: pointer to a DPLL struct clk
194 * Instructs a non-CORE DPLL to enter low-power stop. Will save and
195 * restore the DPLL's autoidle state across the stop, per the CDP
196 * code. If DPLL3 was passed in, or the DPLL does not support
197 * low-power stop, return -EINVAL; otherwise, return 0.
199 static int _omap3_noncore_dpll_stop(struct clk *clk)
203 if (clk == &dpll3_ck)
206 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
209 pr_debug("clock: stopping DPLL %s\n", clk->name);
211 ai = omap3_dpll_autoidle_read(clk);
213 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
216 omap3_dpll_allow_idle(clk);
218 omap3_dpll_deny_idle(clk);
224 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
225 * @clk: pointer to a DPLL struct clk
227 * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
228 * The choice of modes depends on the DPLL's programmed rate: if it is
229 * the same as the DPLL's parent clock, it will enter bypass;
230 * otherwise, it will enter lock. This code will wait for the DPLL to
231 * indicate readiness before returning, unless the DPLL takes too long
232 * to enter the target state. Intended to be used as the struct clk's
233 * enable function. If DPLL3 was passed in, or the DPLL does not
234 * support low-power stop, or if the DPLL took too long to enter
235 * bypass or lock, return -EINVAL; otherwise, return 0.
237 static int omap3_noncore_dpll_enable(struct clk *clk)
241 if (clk == &dpll3_ck)
244 if (clk->parent->rate == clk_get_rate(clk))
245 r = _omap3_noncore_dpll_bypass(clk);
247 r = _omap3_noncore_dpll_lock(clk);
253 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
254 * @clk: pointer to a DPLL struct clk
256 * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
257 * The choice of modes depends on the DPLL's programmed rate: if it is
258 * the same as the DPLL's parent clock, it will enter bypass;
259 * otherwise, it will enter lock. This code will wait for the DPLL to
260 * indicate readiness before returning, unless the DPLL takes too long
261 * to enter the target state. Intended to be used as the struct clk's
262 * enable function. If DPLL3 was passed in, or the DPLL does not
263 * support low-power stop, or if the DPLL took too long to enter
264 * bypass or lock, return -EINVAL; otherwise, return 0.
266 static void omap3_noncore_dpll_disable(struct clk *clk)
268 if (clk == &dpll3_ck)
271 _omap3_noncore_dpll_stop(clk);
274 static const struct clkops clkops_noncore_dpll_ops = {
275 .enable = &omap3_noncore_dpll_enable,
276 .disable = &omap3_noncore_dpll_disable,
280 * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
281 * @clk: struct clk * of the DPLL to read
283 * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
284 * -EINVAL if passed a null pointer or if the struct clk does not
285 * appear to refer to a DPLL.
287 static u32 omap3_dpll_autoidle_read(struct clk *clk)
289 const struct dpll_data *dd;
292 if (!clk || !clk->dpll_data)
297 v = __raw_readl(dd->autoidle_reg);
298 v &= dd->autoidle_mask;
299 v >>= __ffs(dd->autoidle_mask);
305 * omap3_dpll_allow_idle - enable DPLL autoidle bits
306 * @clk: struct clk * of the DPLL to operate on
308 * Enable DPLL automatic idle control. This automatic idle mode
309 * switching takes effect only when the DPLL is locked, at least on
310 * OMAP3430. The DPLL will enter low-power stop when its downstream
311 * clocks are gated. No return value.
313 static void omap3_dpll_allow_idle(struct clk *clk)
315 const struct dpll_data *dd;
318 if (!clk || !clk->dpll_data)
324 * REVISIT: CORE DPLL can optionally enter low-power bypass
325 * by writing 0x5 instead of 0x1. Add some mechanism to
326 * optionally enter this mode.
328 v = __raw_readl(dd->autoidle_reg);
329 v &= ~dd->autoidle_mask;
330 v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
331 __raw_writel(v, dd->autoidle_reg);
335 * omap3_dpll_deny_idle - prevent DPLL from automatically idling
336 * @clk: struct clk * of the DPLL to operate on
338 * Disable DPLL automatic idle control. No return value.
340 static void omap3_dpll_deny_idle(struct clk *clk)
342 const struct dpll_data *dd;
345 if (!clk || !clk->dpll_data)
350 v = __raw_readl(dd->autoidle_reg);
351 v &= ~dd->autoidle_mask;
352 v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
353 __raw_writel(v, dd->autoidle_reg);
356 /* Clock control for DPLL outputs */
359 * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
360 * @clk: DPLL output struct clk
362 * Using parent clock DPLL data, look up DPLL state. If locked, set our
363 * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
365 static void omap3_clkoutx2_recalc(struct clk *clk)
367 const struct dpll_data *dd;
371 /* Walk up the parents of clk, looking for a DPLL */
373 while (pclk && !pclk->dpll_data)
376 /* clk does not have a DPLL as a parent? */
379 dd = pclk->dpll_data;
381 WARN_ON(!dd->control_reg || !dd->enable_mask);
383 v = __raw_readl(dd->control_reg) & dd->enable_mask;
384 v >>= __ffs(dd->enable_mask);
385 if (v != DPLL_LOCKED)
386 clk->rate = clk->parent->rate;
388 clk->rate = clk->parent->rate * 2;
391 /* Common clock code */
394 * As it is structured now, this will prevent an OMAP2/3 multiboot
395 * kernel from compiling. This will need further attention.
397 #if defined(CONFIG_ARCH_OMAP3)
399 static struct clk_functions omap2_clk_functions = {
400 .clk_enable = omap2_clk_enable,
401 .clk_disable = omap2_clk_disable,
402 .clk_round_rate = omap2_clk_round_rate,
403 .clk_set_rate = omap2_clk_set_rate,
404 .clk_set_parent = omap2_clk_set_parent,
405 .clk_disable_unused = omap2_clk_disable_unused,
409 * Set clocks for bypass mode for reboot to work.
411 void omap2_clk_prepare_for_reboot(void)
413 /* REVISIT: Not ready for 343x */
417 if (vclk == NULL || sclk == NULL)
420 rate = clk_get_rate(sclk);
421 clk_set_rate(vclk, rate);
425 /* REVISIT: Move this init stuff out into clock.c */
428 * Switch the MPU rate if specified on cmdline.
429 * We cannot do this early until cmdline is parsed.
431 static int __init omap2_clk_arch_init(void)
436 /* REVISIT: not yet ready for 343x */
438 if (omap2_select_table_rate(&virt_prcm_set, mpurate))
439 printk(KERN_ERR "Could not find matching MPU rate\n");
442 recalculate_root_clocks();
444 printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL3/MPU): "
445 "%ld.%01ld/%ld/%ld MHz\n",
446 (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
447 (core_ck.rate / 1000000), (dpll1_fck.rate / 1000000)) ;
451 arch_initcall(omap2_clk_arch_init);
453 int __init omap2_clk_init(void)
455 /* struct prcm_config *prcm; */
460 /* REVISIT: Ultimately this will be used for multiboot */
462 if (cpu_is_omap242x()) {
463 cpu_mask = RATE_IN_242X;
464 cpu_clkflg = CLOCK_IN_OMAP242X;
465 clkp = onchip_24xx_clks;
466 } else if (cpu_is_omap2430()) {
467 cpu_mask = RATE_IN_243X;
468 cpu_clkflg = CLOCK_IN_OMAP243X;
469 clkp = onchip_24xx_clks;
472 if (cpu_is_omap34xx()) {
473 cpu_mask = RATE_IN_343X;
474 cpu_clkflg = CLOCK_IN_OMAP343X;
475 clkp = onchip_34xx_clks;
478 * Update this if there are further clock changes between ES2
479 * and production parts
481 if (omap_rev() == OMAP3430_REV_ES1_0) {
482 /* No 3430ES1-only rates exist, so no RATE_IN_3430ES1 */
483 cpu_clkflg |= CLOCK_IN_OMAP3430ES1;
485 cpu_mask |= RATE_IN_3430ES2;
486 cpu_clkflg |= CLOCK_IN_OMAP3430ES2;
490 clk_init(&omap2_clk_functions);
492 for (clkp = onchip_34xx_clks;
493 clkp < onchip_34xx_clks + ARRAY_SIZE(onchip_34xx_clks);
495 if ((*clkp)->flags & cpu_clkflg) {
497 omap2_init_clk_clkdm(*clkp);
501 /* REVISIT: Not yet ready for OMAP3 */
503 /* Check the MPU rate set by bootloader */
504 clkrate = omap2_get_dpll_rate_24xx(&dpll_ck);
505 for (prcm = rate_table; prcm->mpu_speed; prcm++) {
506 if (!(prcm->flags & cpu_mask))
508 if (prcm->xtal_speed != sys_ck.rate)
510 if (prcm->dpll_speed <= clkrate)
513 curr_prcm_set = prcm;
516 recalculate_root_clocks();
518 printk(KERN_INFO "Clocking rate (Crystal/DPLL/ARM core): "
519 "%ld.%01ld/%ld/%ld MHz\n",
520 (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
521 (core_ck.rate / 1000000), (arm_fck.rate / 1000000));
524 * Only enable those clocks we will need, let the drivers
525 * enable other clocks as necessary
527 clk_enable_init_clocks();
529 /* Avoid sleeping during omap2_clk_prepare_for_reboot() */
530 /* REVISIT: not yet ready for 343x */
532 vclk = clk_get(NULL, "virt_prcm_set");
533 sclk = clk_get(NULL, "sys_ck");