blob: 52698fb4fd040bd76c4b59e72fa0610655e72749 [file] [log] [blame]
Paul Walmsley02e19a92008-03-18 15:09:51 +02001/*
2 * OMAP3-specific clock framework functions
3 *
Paul Walmsley542313c2008-07-03 12:24:45 +03004 * Copyright (C) 2007-2008 Texas Instruments, Inc.
5 * Copyright (C) 2007-2008 Nokia Corporation
Paul Walmsley02e19a92008-03-18 15:09:51 +02006 *
7 * Written by Paul Walmsley
Paul Walmsley542313c2008-07-03 12:24:45 +03008 * Testing and integration fixes by Jouni Högander
Paul Walmsley02e19a92008-03-18 15:09:51 +02009 *
10 * Parts of this code are based on code written by
11 * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
12 *
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.
16 */
17#undef DEBUG
18
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>
26#include <linux/io.h>
Paul Walmsley542313c2008-07-03 12:24:45 +030027#include <linux/limits.h>
Russell Kingfbd3bdb2008-09-06 12:13:59 +010028#include <linux/bitops.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020029
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/clock.h>
31#include <mach/sram.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020032#include <asm/div64.h>
Paul Walmsley02e19a92008-03-18 15:09:51 +020033
34#include "memory.h"
35#include "clock.h"
Paul Walmsley02e19a92008-03-18 15:09:51 +020036#include "prm.h"
37#include "prm-regbits-34xx.h"
38#include "cm.h"
39#include "cm-regbits-34xx.h"
40
Russell King548d8492008-11-04 14:02:46 +000041static const struct clkops clkops_noncore_dpll_ops;
42
43#include "clock34xx.h"
44
Paul Walmsley542313c2008-07-03 12:24:45 +030045/* CM_AUTOIDLE_PLL*.AUTO_* bit values */
46#define DPLL_AUTOIDLE_DISABLE 0x0
47#define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
48
49#define MAX_DPLL_WAIT_TRIES 1000000
Paul Walmsley02e19a92008-03-18 15:09:51 +020050
51/**
52 * omap3_dpll_recalc - recalculate DPLL rate
53 * @clk: DPLL struct clk
54 *
55 * Recalculate and propagate the DPLL rate.
56 */
57static void omap3_dpll_recalc(struct clk *clk)
58{
59 clk->rate = omap2_get_dpll_rate(clk);
Paul Walmsley02e19a92008-03-18 15:09:51 +020060}
61
Paul Walmsley542313c2008-07-03 12:24:45 +030062/* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
63static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
64{
65 const struct dpll_data *dd;
Paul Walmsleyad67ef62008-08-19 11:08:40 +030066 u32 v;
Paul Walmsley542313c2008-07-03 12:24:45 +030067
68 dd = clk->dpll_data;
69
Paul Walmsleyad67ef62008-08-19 11:08:40 +030070 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);
Paul Walmsley542313c2008-07-03 12:24:45 +030074}
75
76/* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
77static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
78{
79 const struct dpll_data *dd;
80 int i = 0;
81 int ret = -EINVAL;
82 u32 idlest_mask;
83
84 dd = clk->dpll_data;
85
86 state <<= dd->idlest_bit;
87 idlest_mask = 1 << dd->idlest_bit;
88
Paul Walmsleyad67ef62008-08-19 11:08:40 +030089 while (((__raw_readl(dd->idlest_reg) & idlest_mask) != state) &&
Paul Walmsley542313c2008-07-03 12:24:45 +030090 i < MAX_DPLL_WAIT_TRIES) {
91 i++;
92 udelay(1);
93 }
94
95 if (i == MAX_DPLL_WAIT_TRIES) {
96 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
97 clk->name, (state) ? "locked" : "bypassed");
98 } else {
99 pr_debug("clock: %s transition to '%s' in %d loops\n",
100 clk->name, (state) ? "locked" : "bypassed", i);
101
102 ret = 0;
103 }
104
105 return ret;
106}
107
108/* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
109
110/*
111 * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
112 * @clk: pointer to a DPLL struct clk
113 *
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.
119 */
120static int _omap3_noncore_dpll_lock(struct clk *clk)
121{
122 u8 ai;
123 int r;
124
125 if (clk == &dpll3_ck)
126 return -EINVAL;
127
128 pr_debug("clock: locking DPLL %s\n", clk->name);
129
130 ai = omap3_dpll_autoidle_read(clk);
131
132 _omap3_dpll_write_clken(clk, DPLL_LOCKED);
133
134 if (ai) {
135 /*
136 * If no downstream clocks are enabled, CM_IDLEST bit
137 * may never become active, so don't wait for DPLL to lock.
138 */
139 r = 0;
140 omap3_dpll_allow_idle(clk);
141 } else {
142 r = _omap3_wait_dpll_status(clk, 1);
143 omap3_dpll_deny_idle(clk);
144 };
145
146 return r;
147}
148
149/*
150 * omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
151 * @clk: pointer to a DPLL struct clk
152 *
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,
160 * return -EINVAL.
161 */
162static int _omap3_noncore_dpll_bypass(struct clk *clk)
163{
164 int r;
165 u8 ai;
166
167 if (clk == &dpll3_ck)
168 return -EINVAL;
169
170 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
171 return -EINVAL;
172
173 pr_debug("clock: configuring DPLL %s for low-power bypass\n",
174 clk->name);
175
176 ai = omap3_dpll_autoidle_read(clk);
177
178 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
179
180 r = _omap3_wait_dpll_status(clk, 0);
181
182 if (ai)
183 omap3_dpll_allow_idle(clk);
184 else
185 omap3_dpll_deny_idle(clk);
186
187 return r;
188}
189
190/*
191 * _omap3_noncore_dpll_stop - instruct a DPLL to stop
192 * @clk: pointer to a DPLL struct clk
193 *
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.
198 */
199static int _omap3_noncore_dpll_stop(struct clk *clk)
200{
201 u8 ai;
202
203 if (clk == &dpll3_ck)
204 return -EINVAL;
205
206 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
207 return -EINVAL;
208
209 pr_debug("clock: stopping DPLL %s\n", clk->name);
210
211 ai = omap3_dpll_autoidle_read(clk);
212
213 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
214
215 if (ai)
216 omap3_dpll_allow_idle(clk);
217 else
218 omap3_dpll_deny_idle(clk);
219
220 return 0;
221}
222
223/**
224 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
225 * @clk: pointer to a DPLL struct clk
226 *
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.
236 */
237static int omap3_noncore_dpll_enable(struct clk *clk)
238{
239 int r;
240
241 if (clk == &dpll3_ck)
242 return -EINVAL;
243
244 if (clk->parent->rate == clk_get_rate(clk))
245 r = _omap3_noncore_dpll_bypass(clk);
246 else
247 r = _omap3_noncore_dpll_lock(clk);
248
249 return r;
250}
251
252/**
253 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
254 * @clk: pointer to a DPLL struct clk
255 *
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.
265 */
266static void omap3_noncore_dpll_disable(struct clk *clk)
267{
268 if (clk == &dpll3_ck)
269 return;
270
271 _omap3_noncore_dpll_stop(clk);
272}
273
Russell King548d8492008-11-04 14:02:46 +0000274static const struct clkops clkops_noncore_dpll_ops = {
275 .enable = &omap3_noncore_dpll_enable,
276 .disable = &omap3_noncore_dpll_disable,
277};
278
Paul Walmsley542313c2008-07-03 12:24:45 +0300279/**
280 * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
281 * @clk: struct clk * of the DPLL to read
282 *
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.
286 */
287static u32 omap3_dpll_autoidle_read(struct clk *clk)
288{
289 const struct dpll_data *dd;
290 u32 v;
291
292 if (!clk || !clk->dpll_data)
293 return -EINVAL;
294
295 dd = clk->dpll_data;
296
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300297 v = __raw_readl(dd->autoidle_reg);
Paul Walmsley542313c2008-07-03 12:24:45 +0300298 v &= dd->autoidle_mask;
299 v >>= __ffs(dd->autoidle_mask);
300
301 return v;
302}
303
304/**
305 * omap3_dpll_allow_idle - enable DPLL autoidle bits
306 * @clk: struct clk * of the DPLL to operate on
307 *
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.
312 */
313static void omap3_dpll_allow_idle(struct clk *clk)
314{
315 const struct dpll_data *dd;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300316 u32 v;
Paul Walmsley542313c2008-07-03 12:24:45 +0300317
318 if (!clk || !clk->dpll_data)
319 return;
320
321 dd = clk->dpll_data;
322
323 /*
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.
327 */
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300328 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);
Paul Walmsley542313c2008-07-03 12:24:45 +0300332}
333
334/**
335 * omap3_dpll_deny_idle - prevent DPLL from automatically idling
336 * @clk: struct clk * of the DPLL to operate on
337 *
338 * Disable DPLL automatic idle control. No return value.
339 */
340static void omap3_dpll_deny_idle(struct clk *clk)
341{
342 const struct dpll_data *dd;
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300343 u32 v;
Paul Walmsley542313c2008-07-03 12:24:45 +0300344
345 if (!clk || !clk->dpll_data)
346 return;
347
348 dd = clk->dpll_data;
349
Paul Walmsleyad67ef62008-08-19 11:08:40 +0300350 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);
Paul Walmsley542313c2008-07-03 12:24:45 +0300354}
355
356/* Clock control for DPLL outputs */
357
Paul Walmsley02e19a92008-03-18 15:09:51 +0200358/**
359 * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
360 * @clk: DPLL output struct clk
361 *
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.
364 */
365static void omap3_clkoutx2_recalc(struct clk *clk)
366{
367 const struct dpll_data *dd;
368 u32 v;
369 struct clk *pclk;
370
371 /* Walk up the parents of clk, looking for a DPLL */
372 pclk = clk->parent;
373 while (pclk && !pclk->dpll_data)
374 pclk = pclk->parent;
375
376 /* clk does not have a DPLL as a parent? */
377 WARN_ON(!pclk);
378
379 dd = pclk->dpll_data;
380
381 WARN_ON(!dd->control_reg || !dd->enable_mask);
382
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;
387 else
388 clk->rate = clk->parent->rate * 2;
Paul Walmsley02e19a92008-03-18 15:09:51 +0200389}
390
Paul Walmsley542313c2008-07-03 12:24:45 +0300391/* Common clock code */
392
Paul Walmsley02e19a92008-03-18 15:09:51 +0200393/*
394 * As it is structured now, this will prevent an OMAP2/3 multiboot
395 * kernel from compiling. This will need further attention.
396 */
397#if defined(CONFIG_ARCH_OMAP3)
398
399static 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,
406};
407
408/*
409 * Set clocks for bypass mode for reboot to work.
410 */
411void omap2_clk_prepare_for_reboot(void)
412{
413 /* REVISIT: Not ready for 343x */
414#if 0
415 u32 rate;
416
417 if (vclk == NULL || sclk == NULL)
418 return;
419
420 rate = clk_get_rate(sclk);
421 clk_set_rate(vclk, rate);
422#endif
423}
424
425/* REVISIT: Move this init stuff out into clock.c */
426
427/*
428 * Switch the MPU rate if specified on cmdline.
429 * We cannot do this early until cmdline is parsed.
430 */
431static int __init omap2_clk_arch_init(void)
432{
433 if (!mpurate)
434 return -EINVAL;
435
436 /* REVISIT: not yet ready for 343x */
437#if 0
438 if (omap2_select_table_rate(&virt_prcm_set, mpurate))
439 printk(KERN_ERR "Could not find matching MPU rate\n");
440#endif
441
442 recalculate_root_clocks();
443
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)) ;
448
449 return 0;
450}
451arch_initcall(omap2_clk_arch_init);
452
453int __init omap2_clk_init(void)
454{
455 /* struct prcm_config *prcm; */
456 struct clk **clkp;
457 /* u32 clkrate; */
458 u32 cpu_clkflg;
459
460 /* REVISIT: Ultimately this will be used for multiboot */
461#if 0
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;
470 }
471#endif
472 if (cpu_is_omap34xx()) {
473 cpu_mask = RATE_IN_343X;
474 cpu_clkflg = CLOCK_IN_OMAP343X;
475 clkp = onchip_34xx_clks;
476
477 /*
478 * Update this if there are further clock changes between ES2
479 * and production parts
480 */
Lauri Leukkunen84a34342008-12-10 17:36:31 -0800481 if (omap_rev() == OMAP3430_REV_ES1_0) {
Paul Walmsley02e19a92008-03-18 15:09:51 +0200482 /* No 3430ES1-only rates exist, so no RATE_IN_3430ES1 */
483 cpu_clkflg |= CLOCK_IN_OMAP3430ES1;
484 } else {
485 cpu_mask |= RATE_IN_3430ES2;
486 cpu_clkflg |= CLOCK_IN_OMAP3430ES2;
487 }
488 }
489
490 clk_init(&omap2_clk_functions);
491
492 for (clkp = onchip_34xx_clks;
493 clkp < onchip_34xx_clks + ARRAY_SIZE(onchip_34xx_clks);
494 clkp++) {
Paul Walmsley333943b2008-08-19 11:08:45 +0300495 if ((*clkp)->flags & cpu_clkflg) {
Paul Walmsley02e19a92008-03-18 15:09:51 +0200496 clk_register(*clkp);
Paul Walmsley333943b2008-08-19 11:08:45 +0300497 omap2_init_clk_clkdm(*clkp);
498 }
Paul Walmsley02e19a92008-03-18 15:09:51 +0200499 }
500
501 /* REVISIT: Not yet ready for OMAP3 */
502#if 0
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))
507 continue;
508 if (prcm->xtal_speed != sys_ck.rate)
509 continue;
510 if (prcm->dpll_speed <= clkrate)
511 break;
512 }
513 curr_prcm_set = prcm;
514#endif
515
516 recalculate_root_clocks();
517
Roman Tereshonkov3760d312008-03-13 21:35:09 +0200518 printk(KERN_INFO "Clocking rate (Crystal/DPLL/ARM core): "
Paul Walmsley02e19a92008-03-18 15:09:51 +0200519 "%ld.%01ld/%ld/%ld MHz\n",
520 (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
Roman Tereshonkov3760d312008-03-13 21:35:09 +0200521 (core_ck.rate / 1000000), (arm_fck.rate / 1000000));
Paul Walmsley02e19a92008-03-18 15:09:51 +0200522
523 /*
524 * Only enable those clocks we will need, let the drivers
525 * enable other clocks as necessary
526 */
527 clk_enable_init_clocks();
528
529 /* Avoid sleeping during omap2_clk_prepare_for_reboot() */
530 /* REVISIT: not yet ready for 343x */
531#if 0
532 vclk = clk_get(NULL, "virt_prcm_set");
533 sclk = clk_get(NULL, "sys_ck");
534#endif
535 return 0;
536}
537
538#endif