]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clock24xx.c
9e1328f1902e89ad740cb231a9dba40a043c5567
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / clock24xx.c
1 /*
2  *  linux/arch/arm/mach-omap2/clock.c
3  *
4  *  Copyright (C) 2005-2008 Texas Instruments, Inc.
5  *  Copyright (C) 2004-2008 Nokia Corporation
6  *
7  *  Contacts:
8  *  Richard Woodruff <r-woodruff2@ti.com>
9  *  Paul Walmsley
10  *
11  *  Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
12  *  Gordon McNutt and RidgeRun, Inc.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  */
18 #undef DEBUG
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/errno.h>
25 #include <linux/delay.h>
26 #include <linux/clk.h>
27 #include <linux/io.h>
28 #include <linux/cpufreq.h>
29 #include <linux/bitops.h>
30
31 #include <mach/common.h>
32 #include <mach/clock.h>
33 #include <mach/sram.h>
34 #include <asm/div64.h>
35
36 #include <mach/sdrc.h>
37 #include "clock.h"
38 #include "clock24xx.h"
39 #include "prm.h"
40 #include "prm-regbits-24xx.h"
41 #include "cm.h"
42 #include "cm-regbits-24xx.h"
43
44 /* CM_CLKEN_PLL.EN_{54,96}M_PLL options (24XX) */
45 #define EN_APLL_STOPPED                 0
46 #define EN_APLL_LOCKED                  3
47
48 /* CM_CLKSEL1_PLL.APLLS_CLKIN options (24XX) */
49 #define APLLS_CLKIN_19_2MHZ             0
50 #define APLLS_CLKIN_13MHZ               2
51 #define APLLS_CLKIN_12MHZ               3
52
53 /* #define DOWN_VARIABLE_DPLL 1 */              /* Experimental */
54
55 static struct prcm_config *curr_prcm_set;
56 static struct clk *vclk;
57 static struct clk *sclk;
58
59 /*-------------------------------------------------------------------------
60  * Omap24xx specific clock functions
61  *-------------------------------------------------------------------------*/
62
63 /**
64  * omap2xxx_clk_get_core_rate - return the CORE_CLK rate
65  * @clk: pointer to the combined dpll_ck + core_ck (currently "dpll_ck")
66  *
67  * Returns the CORE_CLK rate.  CORE_CLK can have one of three rate
68  * sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz
69  * (the latter is unusual).  This currently should be called with
70  * struct clk *dpll_ck, which is a composite clock of dpll_ck and
71  * core_ck.
72  */
73 static u32 omap2xxx_clk_get_core_rate(struct clk *clk)
74 {
75         long long core_clk;
76         u32 v;
77
78         core_clk = omap2_get_dpll_rate(clk);
79
80         v = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
81         v &= OMAP24XX_CORE_CLK_SRC_MASK;
82
83         if (v == CORE_CLK_SRC_32K)
84                 core_clk = 32768;
85         else
86                 core_clk *= v;
87
88         return core_clk;
89 }
90
91 static int omap2_enable_osc_ck(struct clk *clk)
92 {
93         prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK, 0,
94                         OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
95
96         return 0;
97 }
98
99 static void omap2_disable_osc_ck(struct clk *clk)
100 {
101         prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK, OMAP_AUTOEXTCLKMODE_MASK,
102                         OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
103 }
104
105 /* Enable an APLL if off */
106 static int omap2_clk_fixed_enable(struct clk *clk)
107 {
108         u32 cval, apll_mask;
109
110         apll_mask = EN_APLL_LOCKED << clk->enable_bit;
111
112         cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
113
114         if ((cval & apll_mask) == apll_mask)
115                 return 0;   /* apll already enabled */
116
117         cval &= ~apll_mask;
118         cval |= apll_mask;
119         cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
120
121         if (clk == &apll96_ck)
122                 cval = OMAP24XX_ST_96M_APLL;
123         else if (clk == &apll54_ck)
124                 cval = OMAP24XX_ST_54M_APLL;
125
126         omap2_wait_clock_ready(PLL_MOD, CM_IDLEST, cval, clk->name);
127
128         /*
129          * REVISIT: Should we return an error code if omap2_wait_clock_ready()
130          * fails?
131          */
132         return 0;
133 }
134
135 /* Stop APLL */
136 static void omap2_clk_fixed_disable(struct clk *clk)
137 {
138         u32 cval;
139
140         cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
141         cval &= ~(EN_APLL_LOCKED << clk->enable_bit);
142         cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
143 }
144
145 /*
146  * Uses the current prcm set to tell if a rate is valid.
147  * You can go slower, but not faster within a given rate set.
148  */
149 static long omap2_dpllcore_round_rate(unsigned long target_rate)
150 {
151         u32 high, low, core_clk_src;
152
153         core_clk_src = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
154         core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK;
155
156         if (core_clk_src == CORE_CLK_SRC_DPLL) {        /* DPLL clockout */
157                 high = curr_prcm_set->dpll_speed * 2;
158                 low = curr_prcm_set->dpll_speed;
159         } else {                                /* DPLL clockout x 2 */
160                 high = curr_prcm_set->dpll_speed;
161                 low = curr_prcm_set->dpll_speed / 2;
162         }
163
164 #ifdef DOWN_VARIABLE_DPLL
165         if (target_rate > high)
166                 return high;
167         else
168                 return target_rate;
169 #else
170         if (target_rate > low)
171                 return high;
172         else
173                 return low;
174 #endif
175
176 }
177
178 static void omap2_dpllcore_recalc(struct clk *clk)
179 {
180         clk->rate = omap2xxx_clk_get_core_rate(clk);
181 }
182
183 static int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate)
184 {
185         u32 cur_rate, low, mult, div, valid_rate, done_rate;
186         u32 bypass = 0;
187         struct prcm_config tmpset;
188         const struct dpll_data *dd;
189         unsigned long flags;
190         int ret = -EINVAL;
191
192         local_irq_save(flags);
193         cur_rate = omap2xxx_clk_get_core_rate(&dpll_ck);
194         mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
195         mult &= OMAP24XX_CORE_CLK_SRC_MASK;
196
197         if ((rate == (cur_rate / 2)) && (mult == 2)) {
198                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
199         } else if ((rate == (cur_rate * 2)) && (mult == 1)) {
200                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
201         } else if (rate != cur_rate) {
202                 valid_rate = omap2_dpllcore_round_rate(rate);
203                 if (valid_rate != rate)
204                         goto dpll_exit;
205
206                 if (mult == 1)
207                         low = curr_prcm_set->dpll_speed;
208                 else
209                         low = curr_prcm_set->dpll_speed / 2;
210
211                 dd = clk->dpll_data;
212                 if (!dd)
213                         goto dpll_exit;
214
215                 tmpset.cm_clksel1_pll = cm_read_mod_reg(clk->prcm_mod,
216                                                         dd->mult_div1_reg);
217                 tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
218                                            dd->div1_mask);
219                 div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
220                 tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
221                 tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
222                 if (rate > low) {
223                         tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
224                         mult = ((rate / 2) / 1000000);
225                         done_rate = CORE_CLK_SRC_DPLL_X2;
226                 } else {
227                         tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
228                         mult = (rate / 1000000);
229                         done_rate = CORE_CLK_SRC_DPLL;
230                 }
231                 tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
232                 tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
233
234                 /* Worst case */
235                 tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
236
237                 if (rate == curr_prcm_set->xtal_speed)  /* If asking for 1-1 */
238                         bypass = 1;
239
240                 /* For omap2xxx_sdrc_init_params() */
241                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
242
243                 /* Force dll lock mode */
244                 omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
245                                bypass);
246
247                 /* Errata: ret dll entry state */
248                 omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
249                 omap2xxx_sdrc_reprogram(done_rate, 0);
250         }
251         ret = 0;
252
253 dpll_exit:
254         local_irq_restore(flags);
255         return(ret);
256 }
257
258 /**
259  * omap2_table_mpu_recalc - just return the MPU speed
260  * @clk: virt_prcm_set struct clk
261  *
262  * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.
263  */
264 static void omap2_table_mpu_recalc(struct clk *clk)
265 {
266         clk->rate = curr_prcm_set->mpu_speed;
267 }
268
269 /*
270  * Look for a rate equal or less than the target rate given a configuration set.
271  *
272  * What's not entirely clear is "which" field represents the key field.
273  * Some might argue L3-DDR, others ARM, others IVA. This code is simple and
274  * just uses the ARM rates.
275  */
276 static long omap2_round_to_table_rate(struct clk *clk, unsigned long rate)
277 {
278         struct prcm_config *ptr;
279         long highest_rate;
280
281         if (clk != &virt_prcm_set)
282                 return -EINVAL;
283
284         highest_rate = -EINVAL;
285
286         for (ptr = rate_table; ptr->mpu_speed; ptr++) {
287                 if (!(ptr->flags & cpu_mask))
288                         continue;
289                 if (ptr->xtal_speed != sys_ck.rate)
290                         continue;
291
292                 highest_rate = ptr->mpu_speed;
293
294                 /* Can check only after xtal frequency check */
295                 if (ptr->mpu_speed <= rate)
296                         break;
297         }
298         return highest_rate;
299 }
300
301 /* Sets basic clocks based on the specified rate */
302 static int omap2_select_table_rate(struct clk *clk, unsigned long rate)
303 {
304         u32 cur_rate, done_rate, bypass = 0, tmp;
305         struct prcm_config *prcm;
306         unsigned long found_speed = 0;
307         unsigned long flags;
308
309         if (clk != &virt_prcm_set)
310                 return -EINVAL;
311
312         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
313                 if (!(prcm->flags & cpu_mask))
314                         continue;
315
316                 if (prcm->xtal_speed != sys_ck.rate)
317                         continue;
318
319                 if (prcm->mpu_speed <= rate) {
320                         found_speed = prcm->mpu_speed;
321                         break;
322                 }
323         }
324
325         if (!found_speed) {
326                 printk(KERN_INFO "Could not set MPU rate to %luMHz\n",
327                        rate / 1000000);
328                 return -EINVAL;
329         }
330
331         curr_prcm_set = prcm;
332         cur_rate = omap2xxx_clk_get_core_rate(&dpll_ck);
333
334         if (prcm->dpll_speed == cur_rate / 2) {
335                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
336         } else if (prcm->dpll_speed == cur_rate * 2) {
337                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
338         } else if (prcm->dpll_speed != cur_rate) {
339                 local_irq_save(flags);
340
341                 if (prcm->dpll_speed == prcm->xtal_speed)
342                         bypass = 1;
343
344                 if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
345                     CORE_CLK_SRC_DPLL_X2)
346                         done_rate = CORE_CLK_SRC_DPLL_X2;
347                 else
348                         done_rate = CORE_CLK_SRC_DPLL;
349
350                 /* MPU divider */
351                 cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL);
352
353                 /* dsp + iva1 div(2420), iva2.1(2430) */
354                 cm_write_mod_reg(prcm->cm_clksel_dsp,
355                                  OMAP24XX_DSP_MOD, CM_CLKSEL);
356
357                 cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL);
358
359                 /* Major subsystem dividers */
360                 tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK;
361                 cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD,
362                                  CM_CLKSEL1);
363
364                 if (cpu_is_omap2430())
365                         cm_write_mod_reg(prcm->cm_clksel_mdm,
366                                          OMAP2430_MDM_MOD, CM_CLKSEL);
367
368                 /* x2 to enter omap2xxx_sdrc_init_params() */
369                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
370
371                 omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,
372                                bypass);
373
374                 omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
375                 omap2xxx_sdrc_reprogram(done_rate, 0);
376
377                 local_irq_restore(flags);
378         }
379
380         return 0;
381 }
382
383 #ifdef CONFIG_CPU_FREQ
384 /*
385  * Walk PRCM rate table and fillout cpufreq freq_table
386  */
387 static struct cpufreq_frequency_table freq_table[ARRAY_SIZE(rate_table)];
388
389 void omap2_clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
390 {
391         struct prcm_config *prcm;
392         int i = 0;
393
394         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
395                 if (!(prcm->flags & cpu_mask))
396                         continue;
397                 if (prcm->xtal_speed != sys_ck.rate)
398                         continue;
399
400                 /* don't put bypass rates in table */
401                 if (prcm->dpll_speed == prcm->xtal_speed)
402                         continue;
403
404                 freq_table[i].index = i;
405                 freq_table[i].frequency = prcm->mpu_speed / 1000;
406                 i++;
407         }
408
409         if (i == 0) {
410                 printk(KERN_WARNING "%s: failed to initialize frequency "
411                        "table\n", __func__);
412                 return;
413         }
414
415         freq_table[i].index = i;
416         freq_table[i].frequency = CPUFREQ_TABLE_END;
417
418         *table = &freq_table[0];
419 }
420 #endif
421
422 static struct clk_functions omap2_clk_functions = {
423         .clk_enable             = omap2_clk_enable,
424         .clk_disable            = omap2_clk_disable,
425         .clk_round_rate         = omap2_clk_round_rate,
426         .clk_set_rate           = omap2_clk_set_rate,
427         .clk_set_parent         = omap2_clk_set_parent,
428         .clk_get_parent         = omap2_clk_get_parent,
429         .clk_disable_unused     = omap2_clk_disable_unused,
430 #ifdef  CONFIG_CPU_FREQ
431         .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
432 #endif
433 };
434
435 static u32 omap2_get_apll_clkin(void)
436 {
437         u32 aplls, srate = 0;
438
439         aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1);
440         aplls &= OMAP24XX_APLLS_CLKIN_MASK;
441         aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT;
442
443         if (aplls == APLLS_CLKIN_19_2MHZ)
444                 srate = 19200000;
445         else if (aplls == APLLS_CLKIN_13MHZ)
446                 srate = 13000000;
447         else if (aplls == APLLS_CLKIN_12MHZ)
448                 srate = 12000000;
449
450         return srate;
451 }
452
453 static u32 omap2_get_sysclkdiv(void)
454 {
455         u32 div;
456
457         div = prm_read_mod_reg(OMAP24XX_GR_MOD,
458                                 OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
459         div &= OMAP_SYSCLKDIV_MASK;
460         div >>= OMAP_SYSCLKDIV_SHIFT;
461
462         return div;
463 }
464
465 static void omap2_osc_clk_recalc(struct clk *clk)
466 {
467         clk->rate = omap2_get_apll_clkin() * omap2_get_sysclkdiv();
468 }
469
470 static void omap2_sys_clk_recalc(struct clk *clk)
471 {
472         clk->rate = clk->parent->rate / omap2_get_sysclkdiv();
473 }
474
475 /*
476  * Set clocks for bypass mode for reboot to work.
477  */
478 void omap2_clk_prepare_for_reboot(void)
479 {
480         u32 rate;
481
482         if (vclk == NULL || sclk == NULL)
483                 return;
484
485         rate = clk_get_rate(sclk);
486         clk_set_rate(vclk, rate);
487 }
488
489 /*
490  * Switch the MPU rate if specified on cmdline.
491  * We cannot do this early until cmdline is parsed.
492  */
493 static int __init omap2_clk_arch_init(void)
494 {
495         if (!mpurate)
496                 return -EINVAL;
497
498         if (clk_set_rate(&virt_prcm_set, mpurate))
499                 printk(KERN_ERR "Could not find matching MPU rate\n");
500
501         recalculate_root_clocks();
502
503         printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL/MPU): "
504                "%ld.%01ld/%ld/%ld MHz\n",
505                (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
506                (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;
507
508         return 0;
509 }
510 arch_initcall(omap2_clk_arch_init);
511
512 int __init omap2_clk_init(void)
513 {
514         struct prcm_config *prcm;
515         struct clk **clkp;
516         u32 clkrate;
517
518         if (cpu_is_omap242x())
519                 cpu_mask = RATE_IN_242X;
520         else if (cpu_is_omap2430())
521                 cpu_mask = RATE_IN_243X;
522
523         clk_init(&omap2_clk_functions);
524
525         omap2_osc_clk_recalc(&osc_ck);
526         omap2_sys_clk_recalc(&sys_ck);
527
528         for (clkp = onchip_24xx_clks;
529              clkp < onchip_24xx_clks + ARRAY_SIZE(onchip_24xx_clks);
530              clkp++) {
531
532                 if ((*clkp)->flags & CLOCK_IN_OMAP242X && cpu_is_omap2420()) {
533                         clk_register(*clkp);
534                         omap2_init_clk_clkdm(*clkp);
535                         continue;
536                 }
537
538                 if ((*clkp)->flags & CLOCK_IN_OMAP243X && cpu_is_omap2430()) {
539                         clk_register(*clkp);
540                         omap2_init_clk_clkdm(*clkp);
541                         continue;
542                 }
543         }
544
545         /* Check the MPU rate set by bootloader */
546         clkrate = omap2xxx_clk_get_core_rate(&dpll_ck);
547         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
548                 if (!(prcm->flags & cpu_mask))
549                         continue;
550                 if (prcm->xtal_speed != sys_ck.rate)
551                         continue;
552                 if (prcm->dpll_speed <= clkrate)
553                          break;
554         }
555         curr_prcm_set = prcm;
556
557         recalculate_root_clocks();
558
559         printk(KERN_INFO "Clocking rate (Crystal/DPLL/MPU): "
560                "%ld.%01ld/%ld/%ld MHz\n",
561                (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
562                (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;
563
564         /*
565          * Only enable those clocks we will need, let the drivers
566          * enable other clocks as necessary
567          */
568         clk_enable_init_clocks();
569
570         /* Avoid sleeping sleeping during omap2_clk_prepare_for_reboot() */
571         vclk = clk_get(NULL, "virt_prcm_set");
572         sclk = clk_get(NULL, "sys_ck");
573
574         return 0;
575 }