]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clock34xx.c
OMAP2/3 clock: Add non-CORE DPLL rate set code and M,N programming
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / clock34xx.c
1 /*
2  * OMAP3-specific clock framework functions
3  *
4  * Copyright (C) 2007-2008 Texas Instruments, Inc.
5  * Copyright (C) 2007-2008 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 #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>
27 #include <linux/limits.h>
28
29 #include <asm/arch/clock.h>
30 #include <asm/arch/sram.h>
31 #include <asm/div64.h>
32 #include <asm/bitops.h>
33
34 #include "memory.h"
35 #include "clock.h"
36 #include "clock34xx.h"
37 #include "prm.h"
38 #include "prm-regbits-34xx.h"
39 #include "cm.h"
40 #include "cm-regbits-34xx.h"
41
42 /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
43 #define DPLL_AUTOIDLE_DISABLE                   0x0
44 #define DPLL_AUTOIDLE_LOW_POWER_STOP            0x1
45
46 #define MAX_DPLL_WAIT_TRIES             1000000
47
48 /**
49  * omap3_dpll_recalc - recalculate DPLL rate
50  * @clk: DPLL struct clk
51  *
52  * Recalculate and propagate the DPLL rate.
53  */
54 static void omap3_dpll_recalc(struct clk *clk)
55 {
56         clk->rate = omap2_get_dpll_rate(clk);
57
58         propagate_rate(clk);
59 }
60
61 /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
62 static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
63 {
64         const struct dpll_data *dd;
65
66         dd = clk->dpll_data;
67
68         cm_rmw_reg_bits(dd->enable_mask, clken_bits << __ffs(dd->enable_mask),
69                         dd->control_reg);
70 }
71
72 /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
73 static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
74 {
75         const struct dpll_data *dd;
76         int i = 0;
77         int ret = -EINVAL;
78         u32 idlest_mask;
79
80         dd = clk->dpll_data;
81
82         state <<= dd->idlest_bit;
83         idlest_mask = 1 << dd->idlest_bit;
84
85         while (((__raw_readl(dd->idlest_reg) & idlest_mask) != state) &&
86                i < MAX_DPLL_WAIT_TRIES) {
87                 i++;
88                 udelay(1);
89         }
90
91         if (i == MAX_DPLL_WAIT_TRIES) {
92                 printk(KERN_ERR "clock: %s failed transition to '%s'\n",
93                        clk->name, (state) ? "locked" : "bypassed");
94         } else {
95                 pr_debug("clock: %s transition to '%s' in %d loops\n",
96                          clk->name, (state) ? "locked" : "bypassed", i);
97
98                 ret = 0;
99         }
100
101         return ret;
102 }
103
104 /* From 3430 TRM ES2 4.7.6.2 */
105 static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n)
106 {
107         unsigned long fint;
108         u16 f = 0;
109
110         fint = clk->parent->rate / (n + 1);
111
112         pr_debug("clock: fint is %lu\n", fint);
113
114         if (fint >= 750000 && fint <= 1000000)
115                 f = 0x3;
116         else if (fint > 1000000 && fint <= 1250000)
117                 f = 0x4;
118         else if (fint > 1250000 && fint <= 1500000)
119                 f = 0x5;
120         else if (fint > 1500000 && fint <= 1750000)
121                 f = 0x6;
122         else if (fint > 1750000 && fint <= 2100000)
123                 f = 0x7;
124         else if (fint > 7500000 && fint <= 10000000)
125                 f = 0xB;
126         else if (fint > 10000000 && fint <= 12500000)
127                 f = 0xC;
128         else if (fint > 12500000 && fint <= 15000000)
129                 f = 0xD;
130         else if (fint > 15000000 && fint <= 17500000)
131                 f = 0xE;
132         else if (fint > 17500000 && fint <= 21000000)
133                 f = 0xF;
134         else
135                 pr_debug("clock: unknown freqsel setting for %d\n", n);
136
137         return f;
138 }
139
140 /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
141
142 /*
143  * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
144  * @clk: pointer to a DPLL struct clk
145  *
146  * Instructs a non-CORE DPLL to lock.  Waits for the DPLL to report
147  * readiness before returning.  Will save and restore the DPLL's
148  * autoidle state across the enable, per the CDP code.  If the DPLL
149  * locked successfully, return 0; if the DPLL did not lock in the time
150  * allotted, or DPLL3 was passed in, return -EINVAL.
151  */
152 static int _omap3_noncore_dpll_lock(struct clk *clk)
153 {
154         u8 ai;
155         int r;
156
157         if (clk == &dpll3_ck)
158                 return -EINVAL;
159
160         pr_debug("clock: locking DPLL %s\n", clk->name);
161
162         ai = omap3_dpll_autoidle_read(clk);
163
164         _omap3_dpll_write_clken(clk, DPLL_LOCKED);
165
166         if (ai) {
167                 /*
168                  * If no downstream clocks are enabled, CM_IDLEST bit
169                  * may never become active, so don't wait for DPLL to lock.
170                  */
171                 r = 0;
172                 omap3_dpll_allow_idle(clk);
173         } else {
174                 r = _omap3_wait_dpll_status(clk, 1);
175                 omap3_dpll_deny_idle(clk);
176         };
177
178         return r;
179 }
180
181 /*
182  * omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
183  * @clk: pointer to a DPLL struct clk
184  *
185  * Instructs a non-CORE DPLL to enter low-power bypass mode.  In
186  * bypass mode, the DPLL's rate is set equal to its parent clock's
187  * rate.  Waits for the DPLL to report readiness before returning.
188  * Will save and restore the DPLL's autoidle state across the enable,
189  * per the CDP code.  If the DPLL entered bypass mode successfully,
190  * return 0; if the DPLL did not enter bypass in the time allotted, or
191  * DPLL3 was passed in, or the DPLL does not support low-power bypass,
192  * return -EINVAL.
193  */
194 static int _omap3_noncore_dpll_bypass(struct clk *clk)
195 {
196         int r;
197         u8 ai;
198
199         if (clk == &dpll3_ck)
200                 return -EINVAL;
201
202         if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
203                 return -EINVAL;
204
205         pr_debug("clock: configuring DPLL %s for low-power bypass\n",
206                  clk->name);
207
208         ai = omap3_dpll_autoidle_read(clk);
209
210         _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
211
212         r = _omap3_wait_dpll_status(clk, 0);
213
214         if (ai)
215                 omap3_dpll_allow_idle(clk);
216         else
217                 omap3_dpll_deny_idle(clk);
218
219         return r;
220 }
221
222 /*
223  * _omap3_noncore_dpll_stop - instruct a DPLL to stop
224  * @clk: pointer to a DPLL struct clk
225  *
226  * Instructs a non-CORE DPLL to enter low-power stop. Will save and
227  * restore the DPLL's autoidle state across the stop, per the CDP
228  * code.  If DPLL3 was passed in, or the DPLL does not support
229  * low-power stop, return -EINVAL; otherwise, return 0.
230  */
231 static int _omap3_noncore_dpll_stop(struct clk *clk)
232 {
233         u8 ai;
234
235         if (clk == &dpll3_ck)
236                 return -EINVAL;
237
238         if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
239                 return -EINVAL;
240
241         pr_debug("clock: stopping DPLL %s\n", clk->name);
242
243         ai = omap3_dpll_autoidle_read(clk);
244
245         _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
246
247         if (ai)
248                 omap3_dpll_allow_idle(clk);
249         else
250                 omap3_dpll_deny_idle(clk);
251
252         return 0;
253 }
254
255 /**
256  * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
257  * @clk: pointer to a DPLL struct clk
258  *
259  * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
260  * The choice of modes depends on the DPLL's programmed rate: if it is
261  * the same as the DPLL's parent clock, it will enter bypass;
262  * otherwise, it will enter lock.  This code will wait for the DPLL to
263  * indicate readiness before returning, unless the DPLL takes too long
264  * to enter the target state.  Intended to be used as the struct clk's
265  * enable function.  If DPLL3 was passed in, or the DPLL does not
266  * support low-power stop, or if the DPLL took too long to enter
267  * bypass or lock, return -EINVAL; otherwise, return 0.
268  */
269 static int omap3_noncore_dpll_enable(struct clk *clk)
270 {
271         int r;
272
273         if (clk == &dpll3_ck)
274                 return -EINVAL;
275
276         if (clk->parent->rate == clk_get_rate(clk))
277                 r = _omap3_noncore_dpll_bypass(clk);
278         else
279                 r = _omap3_noncore_dpll_lock(clk);
280
281         return r;
282 }
283
284 /**
285  * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
286  * @clk: pointer to a DPLL struct clk
287  *
288  * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
289  * The choice of modes depends on the DPLL's programmed rate: if it is
290  * the same as the DPLL's parent clock, it will enter bypass;
291  * otherwise, it will enter lock.  This code will wait for the DPLL to
292  * indicate readiness before returning, unless the DPLL takes too long
293  * to enter the target state.  Intended to be used as the struct clk's
294  * enable function.  If DPLL3 was passed in, or the DPLL does not
295  * support low-power stop, or if the DPLL took too long to enter
296  * bypass or lock, return -EINVAL; otherwise, return 0.
297  */
298 static void omap3_noncore_dpll_disable(struct clk *clk)
299 {
300         if (clk == &dpll3_ck)
301                 return;
302
303         _omap3_noncore_dpll_stop(clk);
304 }
305
306
307 /* Non-CORE DPLL rate set code */
308
309 /*
310  * omap3_noncore_dpll_program - set non-core DPLL M,N values directly
311  * @clk: struct clk * of DPLL to set
312  * @m: DPLL multiplier to set
313  * @n: DPLL divider to set
314  * @freqsel: FREQSEL value to set
315  *
316  * Program the DPLL with the supplied M, N values, and wait for the DPLL to
317  * lock..  Returns -EINVAL upon error, or 0 upon success.
318  */
319 static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel)
320 {
321         struct dpll_data *dd;
322         u32 v;
323
324         if (!clk)
325                 return -EINVAL;
326
327         dd = clk->dpll_data;
328         if (!dd)
329                 return -EINVAL;
330
331         /*
332          * According to the 12-5 CDP code from TI, "Limitation 2.5"
333          * on 3430ES1 prevents us from changing DPLL multipliers or dividers
334          * on DPLL4.
335          */
336         if (is_sil_rev_equal_to(OMAP3430_REV_ES1_0) &&
337             !strcmp("dpll4_ck", clk->name)) {
338                 printk(KERN_ERR "clock: DPLL4 cannot change rate due to "
339                        "silicon 'Limitation 2.5' on 3430ES1.\n");
340                 return -EINVAL;
341         }
342
343         /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
344         _omap3_noncore_dpll_bypass(clk);
345
346         v = __raw_readl(dd->mult_div1_reg);
347         v &= ~(dd->mult_mask | dd->div1_mask);
348
349         /* Set mult (M), div1 (N), freqsel */
350         v |= m << __ffs(dd->mult_mask);
351         v |= n << __ffs(dd->div1_mask);
352         v |= freqsel << __ffs(dd->freqsel_mask);
353
354         __raw_writel(v, dd->mult_div1_reg);
355
356         /* We let the clock framework set the other output dividers later */
357
358         /* REVISIT: Set ramp-up delay? */
359
360         _omap3_noncore_dpll_lock(clk);
361
362         return 0;
363 }
364
365 /**
366  * omap3_noncore_dpll_set_rate - set non-core DPLL rate
367  * @clk: struct clk * of DPLL to set
368  * @rate: rounded target rate
369  *
370  * Program the DPLL with the rounded target rate.  Returns -EINVAL upon
371  * error, or 0 upon success.
372  */
373 static int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate)
374 {
375         u16 freqsel;
376         struct dpll_data *dd;
377
378         if (!clk || !rate)
379                 return -EINVAL;
380
381         dd = clk->dpll_data;
382         if (!dd)
383                 return -EINVAL;
384
385         if (rate == omap2_get_dpll_rate(clk))
386                 return 0;
387
388         if (dd->last_rounded_rate != rate)
389                 omap2_dpll_round_rate(clk, rate);
390
391         if (dd->last_rounded_rate == 0)
392                 return -EINVAL;
393
394         freqsel = _omap3_dpll_compute_freqsel(clk, dd->last_rounded_n);
395         if (!freqsel)
396                 WARN_ON(1);
397
398         omap3_noncore_dpll_program(clk, dd->last_rounded_m, dd->last_rounded_n,
399                                    freqsel);
400
401         omap3_dpll_recalc(clk);
402
403         return 0;
404 }
405
406 /* DPLL autoidle read/set code */
407
408
409 /**
410  * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
411  * @clk: struct clk * of the DPLL to read
412  *
413  * Return the DPLL's autoidle bits, shifted down to bit 0.  Returns
414  * -EINVAL if passed a null pointer or if the struct clk does not
415  * appear to refer to a DPLL.
416  */
417 static u32 omap3_dpll_autoidle_read(struct clk *clk)
418 {
419         const struct dpll_data *dd;
420         u32 v;
421
422         if (!clk || !clk->dpll_data)
423                 return -EINVAL;
424
425         dd = clk->dpll_data;
426
427         v = __raw_readl(dd->autoidle_reg);
428         v &= dd->autoidle_mask;
429         v >>= __ffs(dd->autoidle_mask);
430
431         return v;
432 }
433
434 /**
435  * omap3_dpll_allow_idle - enable DPLL autoidle bits
436  * @clk: struct clk * of the DPLL to operate on
437  *
438  * Enable DPLL automatic idle control.  This automatic idle mode
439  * switching takes effect only when the DPLL is locked, at least on
440  * OMAP3430.  The DPLL will enter low-power stop when its downstream
441  * clocks are gated.  No return value.
442  */
443 static void omap3_dpll_allow_idle(struct clk *clk)
444 {
445         const struct dpll_data *dd;
446
447         if (!clk || !clk->dpll_data)
448                 return;
449
450         dd = clk->dpll_data;
451
452         /*
453          * REVISIT: CORE DPLL can optionally enter low-power bypass
454          * by writing 0x5 instead of 0x1.  Add some mechanism to
455          * optionally enter this mode.
456          */
457         cm_rmw_reg_bits(dd->autoidle_mask,
458                         DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask),
459                         dd->autoidle_reg);
460 }
461
462 /**
463  * omap3_dpll_deny_idle - prevent DPLL from automatically idling
464  * @clk: struct clk * of the DPLL to operate on
465  *
466  * Disable DPLL automatic idle control.  No return value.
467  */
468 static void omap3_dpll_deny_idle(struct clk *clk)
469 {
470         const struct dpll_data *dd;
471
472         if (!clk || !clk->dpll_data)
473                 return;
474
475         dd = clk->dpll_data;
476
477         cm_rmw_reg_bits(dd->autoidle_mask,
478                         DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask),
479                         dd->autoidle_reg);
480 }
481
482 /* Clock control for DPLL outputs */
483
484 /**
485  * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
486  * @clk: DPLL output struct clk
487  *
488  * Using parent clock DPLL data, look up DPLL state.  If locked, set our
489  * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
490  */
491 static void omap3_clkoutx2_recalc(struct clk *clk)
492 {
493         const struct dpll_data *dd;
494         u32 v;
495         struct clk *pclk;
496
497         /* Walk up the parents of clk, looking for a DPLL */
498         pclk = clk->parent;
499         while (pclk && !pclk->dpll_data)
500                 pclk = pclk->parent;
501
502         /* clk does not have a DPLL as a parent? */
503         WARN_ON(!pclk);
504
505         dd = pclk->dpll_data;
506
507         WARN_ON(!dd->control_reg || !dd->enable_mask);
508
509         v = __raw_readl(dd->control_reg) & dd->enable_mask;
510         v >>= __ffs(dd->enable_mask);
511         if (v != DPLL_LOCKED)
512                 clk->rate = clk->parent->rate;
513         else
514                 clk->rate = clk->parent->rate * 2;
515
516         if (clk->flags & RATE_PROPAGATES)
517                 propagate_rate(clk);
518 }
519
520 /* Common clock code */
521
522 /*
523  * As it is structured now, this will prevent an OMAP2/3 multiboot
524  * kernel from compiling.  This will need further attention.
525  */
526 #if defined(CONFIG_ARCH_OMAP3)
527
528 static struct clk_functions omap2_clk_functions = {
529         .clk_enable             = omap2_clk_enable,
530         .clk_disable            = omap2_clk_disable,
531         .clk_round_rate         = omap2_clk_round_rate,
532         .clk_set_rate           = omap2_clk_set_rate,
533         .clk_set_parent         = omap2_clk_set_parent,
534         .clk_disable_unused     = omap2_clk_disable_unused,
535 };
536
537 /*
538  * Set clocks for bypass mode for reboot to work.
539  */
540 void omap2_clk_prepare_for_reboot(void)
541 {
542         /* REVISIT: Not ready for 343x */
543 #if 0
544         u32 rate;
545
546         if (vclk == NULL || sclk == NULL)
547                 return;
548
549         rate = clk_get_rate(sclk);
550         clk_set_rate(vclk, rate);
551 #endif
552 }
553
554 /* REVISIT: Move this init stuff out into clock.c */
555
556 /*
557  * Switch the MPU rate if specified on cmdline.
558  * We cannot do this early until cmdline is parsed.
559  */
560 static int __init omap2_clk_arch_init(void)
561 {
562         if (!mpurate)
563                 return -EINVAL;
564
565         /* REVISIT: not yet ready for 343x */
566 #if 0
567         if (omap2_select_table_rate(&virt_prcm_set, mpurate))
568                 printk(KERN_ERR "Could not find matching MPU rate\n");
569 #endif
570
571         recalculate_root_clocks();
572
573         printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL3/MPU): "
574                "%ld.%01ld/%ld/%ld MHz\n",
575                (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
576                (core_ck.rate / 1000000), (dpll1_fck.rate / 1000000)) ;
577
578         return 0;
579 }
580 arch_initcall(omap2_clk_arch_init);
581
582 int __init omap2_clk_init(void)
583 {
584         /* struct prcm_config *prcm; */
585         struct clk **clkp;
586         /* u32 clkrate; */
587         u32 cpu_clkflg;
588
589         /* REVISIT: Ultimately this will be used for multiboot */
590 #if 0
591         if (cpu_is_omap242x()) {
592                 cpu_mask = RATE_IN_242X;
593                 cpu_clkflg = CLOCK_IN_OMAP242X;
594                 clkp = onchip_24xx_clks;
595         } else if (cpu_is_omap2430()) {
596                 cpu_mask = RATE_IN_243X;
597                 cpu_clkflg = CLOCK_IN_OMAP243X;
598                 clkp = onchip_24xx_clks;
599         }
600 #endif
601         if (cpu_is_omap34xx()) {
602                 cpu_mask = RATE_IN_343X;
603                 cpu_clkflg = CLOCK_IN_OMAP343X;
604                 clkp = onchip_34xx_clks;
605
606                 /*
607                  * Update this if there are further clock changes between ES2
608                  * and production parts
609                  */
610                 if (is_sil_rev_equal_to(OMAP3430_REV_ES1_0)) {
611                         /* No 3430ES1-only rates exist, so no RATE_IN_3430ES1 */
612                         cpu_clkflg |= CLOCK_IN_OMAP3430ES1;
613                 } else {
614                         cpu_mask |= RATE_IN_3430ES2;
615                         cpu_clkflg |= CLOCK_IN_OMAP3430ES2;
616                 }
617         }
618
619         clk_init(&omap2_clk_functions);
620
621         for (clkp = onchip_34xx_clks;
622              clkp < onchip_34xx_clks + ARRAY_SIZE(onchip_34xx_clks);
623              clkp++) {
624                 if ((*clkp)->flags & cpu_clkflg)
625                         clk_register(*clkp);
626         }
627
628         /* REVISIT: Not yet ready for OMAP3 */
629 #if 0
630         /* Check the MPU rate set by bootloader */
631         clkrate = omap2_get_dpll_rate_24xx(&dpll_ck);
632         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
633                 if (!(prcm->flags & cpu_mask))
634                         continue;
635                 if (prcm->xtal_speed != sys_ck.rate)
636                         continue;
637                 if (prcm->dpll_speed <= clkrate)
638                          break;
639         }
640         curr_prcm_set = prcm;
641 #endif
642
643         recalculate_root_clocks();
644
645         printk(KERN_INFO "Clocking rate (Crystal/DPLL/ARM core): "
646                "%ld.%01ld/%ld/%ld MHz\n",
647                (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
648                (core_ck.rate / 1000000), (arm_fck.rate / 1000000));
649
650         /*
651          * Only enable those clocks we will need, let the drivers
652          * enable other clocks as necessary
653          */
654         clk_enable_init_clocks();
655
656         /* Avoid sleeping during omap2_clk_prepare_for_reboot() */
657         /* REVISIT: not yet ready for 343x */
658 #if 0
659         vclk = clk_get(NULL, "virt_prcm_set");
660         sclk = clk_get(NULL, "sys_ck");
661 #endif
662         return 0;
663 }
664
665 #endif