modpost: support objects with more than 64k sections
[pandora-kernel.git] / arch / arm / mach-omap2 / dpll.c
1 /*
2  * OMAP3/4 - specific DPLL control functions
3  *
4  * Copyright (C) 2009 Texas Instruments, Inc.
5  * Copyright (C) 2009 Nokia Corporation
6  *
7  * Written by Paul Walmsley
8  * Testing and integration fixes by Jouni Högander
9  *
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
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/list.h>
22 #include <linux/errno.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/io.h>
26 #include <linux/limits.h>
27 #include <linux/bitops.h>
28
29 #include <plat/cpu.h>
30 #include <plat/clock.h>
31 #include <plat/sram.h>
32 #include <asm/div64.h>
33 #include <asm/clkdev.h>
34
35 #include "clock.h"
36 #include "prm.h"
37 #include "prm-regbits-34xx.h"
38 #include "cm.h"
39 #include "cm-regbits-34xx.h"
40
41 /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
42 #define DPLL_AUTOIDLE_DISABLE                   0x0
43 #define DPLL_AUTOIDLE_LOW_POWER_STOP            0x1
44
45 #define MAX_DPLL_WAIT_TRIES             1000000
46
47
48 /**
49  * omap3_dpll_recalc - recalculate DPLL rate
50  * @clk: DPLL struct clk
51  *
52  * Recalculate and propagate the DPLL rate.
53  */
54 unsigned long omap3_dpll_recalc(struct clk *clk)
55 {
56         return omap2_get_dpll_rate(clk);
57 }
58
59 /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
60 static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
61 {
62         const struct dpll_data *dd;
63         u32 v;
64
65         dd = clk->dpll_data;
66
67         v = __raw_readl(dd->control_reg);
68         v &= ~dd->enable_mask;
69         v |= clken_bits << __ffs(dd->enable_mask);
70         __raw_writel(v, dd->control_reg);
71 }
72
73 /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
74 static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
75 {
76         const struct dpll_data *dd;
77         int i = 0;
78         int ret = -EINVAL;
79
80         dd = clk->dpll_data;
81
82         state <<= __ffs(dd->idlest_mask);
83
84         while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) &&
85                i < MAX_DPLL_WAIT_TRIES) {
86                 i++;
87                 udelay(1);
88         }
89
90         if (i == MAX_DPLL_WAIT_TRIES) {
91                 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
92                        clk->name, (state) ? "locked" : "bypassed");
93         } else {
94                 pr_debug("clock: %s transition to '%s' in %d loops\n",
95                          clk->name, (state) ? "locked" : "bypassed", i);
96
97                 ret = 0;
98         }
99
100         return ret;
101 }
102
103 /* From 3430 TRM ES2 4.7.6.2 */
104 static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
105 {
106         unsigned long fint;
107         u16 f = 0;
108
109         fint = clk->dpll_data->clk_ref->rate / n;
110
111         pr_debug("clock: fint is %lu\n", fint);
112
113         if (fint >= 750000 && fint <= 1000000)
114                 f = 0x3;
115         else if (fint > 1000000 && fint <= 1250000)
116                 f = 0x4;
117         else if (fint > 1250000 && fint <= 1500000)
118                 f = 0x5;
119         else if (fint > 1500000 && fint <= 1750000)
120                 f = 0x6;
121         else if (fint > 1750000 && fint <= 2100000)
122                 f = 0x7;
123         else if (fint > 7500000 && fint <= 10000000)
124                 f = 0xB;
125         else if (fint > 10000000 && fint <= 12500000)
126                 f = 0xC;
127         else if (fint > 12500000 && fint <= 15000000)
128                 f = 0xD;
129         else if (fint > 15000000 && fint <= 17500000)
130                 f = 0xE;
131         else if (fint > 17500000 && fint <= 21000000)
132                 f = 0xF;
133         else
134                 pr_debug("clock: unknown freqsel setting for %d\n", n);
135
136         return f;
137 }
138
139 /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
140
141 /*
142  * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
143  * @clk: pointer to a DPLL struct clk
144  *
145  * Instructs a non-CORE DPLL to lock.  Waits for the DPLL to report
146  * readiness before returning.  Will save and restore the DPLL's
147  * autoidle state across the enable, per the CDP code.  If the DPLL
148  * locked successfully, return 0; if the DPLL did not lock in the time
149  * allotted, or DPLL3 was passed in, return -EINVAL.
150  */
151 static int _omap3_noncore_dpll_lock(struct clk *clk)
152 {
153         u8 ai;
154         int r;
155
156         pr_debug("clock: locking DPLL %s\n", clk->name);
157
158         ai = omap3_dpll_autoidle_read(clk);
159
160         omap3_dpll_deny_idle(clk);
161
162         _omap3_dpll_write_clken(clk, DPLL_LOCKED);
163
164         r = _omap3_wait_dpll_status(clk, 1);
165
166         if (ai)
167                 omap3_dpll_allow_idle(clk);
168
169         return r;
170 }
171
172 /*
173  * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
174  * @clk: pointer to a DPLL struct clk
175  *
176  * Instructs a non-CORE DPLL to enter low-power bypass mode.  In
177  * bypass mode, the DPLL's rate is set equal to its parent clock's
178  * rate.  Waits for the DPLL to report readiness before returning.
179  * Will save and restore the DPLL's autoidle state across the enable,
180  * per the CDP code.  If the DPLL entered bypass mode successfully,
181  * return 0; if the DPLL did not enter bypass in the time allotted, or
182  * DPLL3 was passed in, or the DPLL does not support low-power bypass,
183  * return -EINVAL.
184  */
185 static int _omap3_noncore_dpll_bypass(struct clk *clk)
186 {
187         int r;
188         u8 ai;
189
190         if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
191                 return -EINVAL;
192
193         pr_debug("clock: configuring DPLL %s for low-power bypass\n",
194                  clk->name);
195
196         ai = omap3_dpll_autoidle_read(clk);
197
198         _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
199
200         r = _omap3_wait_dpll_status(clk, 0);
201
202         if (ai)
203                 omap3_dpll_allow_idle(clk);
204         else
205                 omap3_dpll_deny_idle(clk);
206
207         return r;
208 }
209
210 /*
211  * _omap3_noncore_dpll_stop - instruct a DPLL to stop
212  * @clk: pointer to a DPLL struct clk
213  *
214  * Instructs a non-CORE DPLL to enter low-power stop. Will save and
215  * restore the DPLL's autoidle state across the stop, per the CDP
216  * code.  If DPLL3 was passed in, or the DPLL does not support
217  * low-power stop, return -EINVAL; otherwise, return 0.
218  */
219 static int _omap3_noncore_dpll_stop(struct clk *clk)
220 {
221         u8 ai;
222
223         if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
224                 return -EINVAL;
225
226         pr_debug("clock: stopping DPLL %s\n", clk->name);
227
228         ai = omap3_dpll_autoidle_read(clk);
229
230         _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
231
232         if (ai)
233                 omap3_dpll_allow_idle(clk);
234         else
235                 omap3_dpll_deny_idle(clk);
236
237         return 0;
238 }
239
240 /**
241  * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
242  * @clk: pointer to a DPLL struct clk
243  *
244  * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
245  * The choice of modes depends on the DPLL's programmed rate: if it is
246  * the same as the DPLL's parent clock, it will enter bypass;
247  * otherwise, it will enter lock.  This code will wait for the DPLL to
248  * indicate readiness before returning, unless the DPLL takes too long
249  * to enter the target state.  Intended to be used as the struct clk's
250  * enable function.  If DPLL3 was passed in, or the DPLL does not
251  * support low-power stop, or if the DPLL took too long to enter
252  * bypass or lock, return -EINVAL; otherwise, return 0.
253  */
254 int omap3_noncore_dpll_enable(struct clk *clk)
255 {
256         int r;
257         struct dpll_data *dd;
258
259         dd = clk->dpll_data;
260         if (!dd)
261                 return -EINVAL;
262
263         if (clk->rate == dd->clk_bypass->rate) {
264                 WARN_ON(clk->parent != dd->clk_bypass);
265                 r = _omap3_noncore_dpll_bypass(clk);
266         } else {
267                 WARN_ON(clk->parent != dd->clk_ref);
268                 r = _omap3_noncore_dpll_lock(clk);
269         }
270         /*
271          *FIXME: this is dubious - if clk->rate has changed, what about
272          * propagating?
273          */
274         if (!r)
275                 clk->rate = omap2_get_dpll_rate(clk);
276
277         return r;
278 }
279
280 /**
281  * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
282  * @clk: pointer to a DPLL struct clk
283  *
284  * Instructs a non-CORE DPLL to enter low-power stop.  This function is
285  * intended for use in struct clkops.  No return value.
286  */
287 void omap3_noncore_dpll_disable(struct clk *clk)
288 {
289         _omap3_noncore_dpll_stop(clk);
290 }
291
292
293 /* Non-CORE DPLL rate set code */
294
295 /*
296  * omap3_noncore_dpll_program - set non-core DPLL M,N values directly
297  * @clk: struct clk * of DPLL to set
298  * @m: DPLL multiplier to set
299  * @n: DPLL divider to set
300  * @freqsel: FREQSEL value to set
301  *
302  * Program the DPLL with the supplied M, N values, and wait for the DPLL to
303  * lock..  Returns -EINVAL upon error, or 0 upon success.
304  */
305 int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
306 {
307         struct dpll_data *dd = clk->dpll_data;
308         u32 v;
309
310         /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
311         _omap3_noncore_dpll_bypass(clk);
312
313         /* Set jitter correction */
314         if (!cpu_is_omap44xx()) {
315                 v = __raw_readl(dd->control_reg);
316                 v &= ~dd->freqsel_mask;
317                 v |= freqsel << __ffs(dd->freqsel_mask);
318                 __raw_writel(v, dd->control_reg);
319         }
320
321         /* Set DPLL multiplier, divider */
322         v = __raw_readl(dd->mult_div1_reg);
323         v &= ~(dd->mult_mask | dd->div1_mask);
324         v |= m << __ffs(dd->mult_mask);
325         v |= (n - 1) << __ffs(dd->div1_mask);
326         __raw_writel(v, dd->mult_div1_reg);
327
328         /* We let the clock framework set the other output dividers later */
329
330         /* REVISIT: Set ramp-up delay? */
331
332         _omap3_noncore_dpll_lock(clk);
333
334         return 0;
335 }
336
337 /**
338  * omap3_noncore_dpll_set_rate - set non-core DPLL rate
339  * @clk: struct clk * of DPLL to set
340  * @rate: rounded target rate
341  *
342  * Set the DPLL CLKOUT to the target rate.  If the DPLL can enter
343  * low-power bypass, and the target rate is the bypass source clock
344  * rate, then configure the DPLL for bypass.  Otherwise, round the
345  * target rate if it hasn't been done already, then program and lock
346  * the DPLL.  Returns -EINVAL upon error, or 0 upon success.
347  */
348 int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
349 {
350         struct clk *new_parent = NULL;
351         u16 freqsel = 0;
352         struct dpll_data *dd;
353         int ret;
354
355         if (!clk || !rate)
356                 return -EINVAL;
357
358         dd = clk->dpll_data;
359         if (!dd)
360                 return -EINVAL;
361
362         if (rate == omap2_get_dpll_rate(clk))
363                 return 0;
364
365         /*
366          * Ensure both the bypass and ref clocks are enabled prior to
367          * doing anything; we need the bypass clock running to reprogram
368          * the DPLL.
369          */
370         omap2_clk_enable(dd->clk_bypass);
371         omap2_clk_enable(dd->clk_ref);
372
373         if (dd->clk_bypass->rate == rate &&
374             (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
375                 pr_debug("clock: %s: set rate: entering bypass.\n", clk->name);
376
377                 ret = _omap3_noncore_dpll_bypass(clk);
378                 if (!ret)
379                         new_parent = dd->clk_bypass;
380         } else {
381                 if (dd->last_rounded_rate != rate)
382                         omap2_dpll_round_rate(clk, rate);
383
384                 if (dd->last_rounded_rate == 0)
385                         return -EINVAL;
386
387                 /* No freqsel on OMAP4 */
388                 if (!cpu_is_omap44xx()) {
389                         freqsel = _omap3_dpll_compute_freqsel(clk,
390                                                 dd->last_rounded_n);
391                         if (!freqsel)
392                                 WARN_ON(1);
393                 }
394
395                 pr_debug("clock: %s: set rate: locking rate to %lu.\n",
396                          clk->name, rate);
397
398                 ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m,
399                                                  dd->last_rounded_n, freqsel);
400                 if (!ret)
401                         new_parent = dd->clk_ref;
402         }
403         if (!ret) {
404                 /*
405                  * Switch the parent clock in the heirarchy, and make sure
406                  * that the new parent's usecount is correct.  Note: we
407                  * enable the new parent before disabling the old to avoid
408                  * any unnecessary hardware disable->enable transitions.
409                  */
410                 if (clk->usecount) {
411                         omap2_clk_enable(new_parent);
412                         omap2_clk_disable(clk->parent);
413                 }
414                 clk_reparent(clk, new_parent);
415                 clk->rate = rate;
416         }
417         omap2_clk_disable(dd->clk_ref);
418         omap2_clk_disable(dd->clk_bypass);
419
420         return 0;
421 }
422
423 /* DPLL autoidle read/set code */
424
425 /**
426  * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
427  * @clk: struct clk * of the DPLL to read
428  *
429  * Return the DPLL's autoidle bits, shifted down to bit 0.  Returns
430  * -EINVAL if passed a null pointer or if the struct clk does not
431  * appear to refer to a DPLL.
432  */
433 u32 omap3_dpll_autoidle_read(struct clk *clk)
434 {
435         const struct dpll_data *dd;
436         u32 v;
437
438         if (!clk || !clk->dpll_data)
439                 return -EINVAL;
440
441         dd = clk->dpll_data;
442
443         v = __raw_readl(dd->autoidle_reg);
444         v &= dd->autoidle_mask;
445         v >>= __ffs(dd->autoidle_mask);
446
447         return v;
448 }
449
450 /**
451  * omap3_dpll_allow_idle - enable DPLL autoidle bits
452  * @clk: struct clk * of the DPLL to operate on
453  *
454  * Enable DPLL automatic idle control.  This automatic idle mode
455  * switching takes effect only when the DPLL is locked, at least on
456  * OMAP3430.  The DPLL will enter low-power stop when its downstream
457  * clocks are gated.  No return value.
458  */
459 void omap3_dpll_allow_idle(struct clk *clk)
460 {
461         const struct dpll_data *dd;
462         u32 v;
463
464         if (!clk || !clk->dpll_data)
465                 return;
466
467         dd = clk->dpll_data;
468
469         /*
470          * REVISIT: CORE DPLL can optionally enter low-power bypass
471          * by writing 0x5 instead of 0x1.  Add some mechanism to
472          * optionally enter this mode.
473          */
474         v = __raw_readl(dd->autoidle_reg);
475         v &= ~dd->autoidle_mask;
476         v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
477         __raw_writel(v, dd->autoidle_reg);
478 }
479
480 /**
481  * omap3_dpll_deny_idle - prevent DPLL from automatically idling
482  * @clk: struct clk * of the DPLL to operate on
483  *
484  * Disable DPLL automatic idle control.  No return value.
485  */
486 void omap3_dpll_deny_idle(struct clk *clk)
487 {
488         const struct dpll_data *dd;
489         u32 v;
490
491         if (!clk || !clk->dpll_data)
492                 return;
493
494         dd = clk->dpll_data;
495
496         v = __raw_readl(dd->autoidle_reg);
497         v &= ~dd->autoidle_mask;
498         v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
499         __raw_writel(v, dd->autoidle_reg);
500
501 }
502
503 /* Clock control for DPLL outputs */
504
505 /**
506  * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
507  * @clk: DPLL output struct clk
508  *
509  * Using parent clock DPLL data, look up DPLL state.  If locked, set our
510  * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
511  */
512 unsigned long omap3_clkoutx2_recalc(struct clk *clk)
513 {
514         const struct dpll_data *dd;
515         unsigned long rate;
516         u32 v;
517         struct clk *pclk;
518
519         /* Walk up the parents of clk, looking for a DPLL */
520         pclk = clk->parent;
521         while (pclk && !pclk->dpll_data)
522                 pclk = pclk->parent;
523
524         /* clk does not have a DPLL as a parent? */
525         WARN_ON(!pclk);
526
527         dd = pclk->dpll_data;
528
529         WARN_ON(!dd->enable_mask);
530
531         v = __raw_readl(dd->control_reg) & dd->enable_mask;
532         v >>= __ffs(dd->enable_mask);
533         if (v != OMAP3XXX_EN_DPLL_LOCKED)
534                 rate = clk->parent->rate;
535         else
536                 rate = clk->parent->rate * 2;
537         return rate;
538 }