]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clock24xx.c
Merge current mainline tree into linux-omap tree
[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/bitops.h>
28 #include <linux/io.h>
29 #include <linux/cpufreq.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 /* This actually returns the rate of core_ck, not dpll_ck. */
64 static u32 omap2_get_dpll_rate_24xx(struct clk *tclk)
65 {
66         long long dpll_clk;
67         u8 amult;
68
69         dpll_clk = omap2_get_dpll_rate(tclk);
70
71         amult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
72         amult &= OMAP24XX_CORE_CLK_SRC_MASK;
73         dpll_clk *= amult;
74
75         return dpll_clk;
76 }
77
78 static int omap2_enable_osc_ck(struct clk *clk)
79 {
80         prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK, 0,
81                         OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
82
83         return 0;
84 }
85
86 static void omap2_disable_osc_ck(struct clk *clk)
87 {
88         prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK, OMAP_AUTOEXTCLKMODE_MASK,
89                         OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
90 }
91
92 /* Enable an APLL if off */
93 static int omap2_clk_fixed_enable(struct clk *clk)
94 {
95         u32 cval, apll_mask;
96         void __iomem *idlest;
97
98         apll_mask = EN_APLL_LOCKED << clk->enable_bit;
99
100         cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
101
102         if ((cval & apll_mask) == apll_mask)
103                 return 0;   /* apll already enabled */
104
105         cval &= ~apll_mask;
106         cval |= apll_mask;
107         cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
108
109         if (clk == &apll96_ck)
110                 cval = OMAP24XX_ST_96M_APLL;
111         else if (clk == &apll54_ck)
112                 cval = OMAP24XX_ST_54M_APLL;
113
114         if (cpu_is_omap242x())
115                 idlest = (__force void __iomem *)OMAP2420_CM_REGADDR(PLL_MOD,
116                                                                 CM_IDLEST);
117         else
118                 idlest = (__force void __iomem *)OMAP2430_CM_REGADDR(PLL_MOD,
119                                                                 CM_IDLEST);
120
121         omap2_wait_clock_ready(idlest, cval, clk->name);
122
123         /*
124          * REVISIT: Should we return an error code if omap2_wait_clock_ready()
125          * fails?
126          */
127         return 0;
128 }
129
130 /* Stop APLL */
131 static void omap2_clk_fixed_disable(struct clk *clk)
132 {
133         u32 cval;
134
135         cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN);
136         cval &= ~(EN_APLL_LOCKED << clk->enable_bit);
137         cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN);
138 }
139
140 /*
141  * Uses the current prcm set to tell if a rate is valid.
142  * You can go slower, but not faster within a given rate set.
143  */
144 static long omap2_dpllcore_round_rate(unsigned long target_rate)
145 {
146         u32 high, low, core_clk_src;
147
148         core_clk_src = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
149         core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK;
150
151         if (core_clk_src == CORE_CLK_SRC_DPLL) {        /* DPLL clockout */
152                 high = curr_prcm_set->dpll_speed * 2;
153                 low = curr_prcm_set->dpll_speed;
154         } else {                                /* DPLL clockout x 2 */
155                 high = curr_prcm_set->dpll_speed;
156                 low = curr_prcm_set->dpll_speed / 2;
157         }
158
159 #ifdef DOWN_VARIABLE_DPLL
160         if (target_rate > high)
161                 return high;
162         else
163                 return target_rate;
164 #else
165         if (target_rate > low)
166                 return high;
167         else
168                 return low;
169 #endif
170
171 }
172
173 static void omap2_dpllcore_recalc(struct clk *clk)
174 {
175         clk->rate = omap2_get_dpll_rate_24xx(clk);
176
177         propagate_rate(clk);
178 }
179
180 static int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate)
181 {
182         u32 cur_rate, low, mult, div, valid_rate, done_rate;
183         u32 bypass = 0;
184         struct prcm_config tmpset;
185         const struct dpll_data *dd;
186         unsigned long flags;
187         int ret = -EINVAL;
188
189         local_irq_save(flags);
190         cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck);
191         mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
192         mult &= OMAP24XX_CORE_CLK_SRC_MASK;
193
194         if ((rate == (cur_rate / 2)) && (mult == 2)) {
195                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
196         } else if ((rate == (cur_rate * 2)) && (mult == 1)) {
197                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
198         } else if (rate != cur_rate) {
199                 valid_rate = omap2_dpllcore_round_rate(rate);
200                 if (valid_rate != rate)
201                         goto dpll_exit;
202
203                 if (mult == 1)
204                         low = curr_prcm_set->dpll_speed;
205                 else
206                         low = curr_prcm_set->dpll_speed / 2;
207
208                 dd = clk->dpll_data;
209                 if (!dd)
210                         goto dpll_exit;
211
212                 tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg);
213                 tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
214                                            dd->div1_mask);
215                 div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
216                 tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
217                 tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
218                 if (rate > low) {
219                         tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
220                         mult = ((rate / 2) / 1000000);
221                         done_rate = CORE_CLK_SRC_DPLL_X2;
222                 } else {
223                         tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
224                         mult = (rate / 1000000);
225                         done_rate = CORE_CLK_SRC_DPLL;
226                 }
227                 tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
228                 tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
229
230                 /* Worst case */
231                 tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
232
233                 if (rate == curr_prcm_set->xtal_speed)  /* If asking for 1-1 */
234                         bypass = 1;
235
236                 /* For omap2xxx_sdrc_init_params() */
237                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
238
239                 /* Force dll lock mode */
240                 omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
241                                bypass);
242
243                 /* Errata: ret dll entry state */
244                 omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
245                 omap2xxx_sdrc_reprogram(done_rate, 0);
246         }
247         omap2_dpllcore_recalc(&dpll_ck);
248         ret = 0;
249
250 dpll_exit:
251         local_irq_restore(flags);
252         return(ret);
253 }
254
255 /**
256  * omap2_table_mpu_recalc - just return the MPU speed
257  * @clk: virt_prcm_set struct clk
258  *
259  * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.
260  */
261 static void omap2_table_mpu_recalc(struct clk *clk)
262 {
263         clk->rate = curr_prcm_set->mpu_speed;
264 }
265
266 /*
267  * Look for a rate equal or less than the target rate given a configuration set.
268  *
269  * What's not entirely clear is "which" field represents the key field.
270  * Some might argue L3-DDR, others ARM, others IVA. This code is simple and
271  * just uses the ARM rates.
272  */
273 static long omap2_round_to_table_rate(struct clk *clk, unsigned long rate)
274 {
275         struct prcm_config *ptr;
276         long highest_rate;
277
278         if (clk != &virt_prcm_set)
279                 return -EINVAL;
280
281         highest_rate = -EINVAL;
282
283         for (ptr = rate_table; ptr->mpu_speed; ptr++) {
284                 if (!(ptr->flags & cpu_mask))
285                         continue;
286                 if (ptr->xtal_speed != sys_ck.rate)
287                         continue;
288
289                 highest_rate = ptr->mpu_speed;
290
291                 /* Can check only after xtal frequency check */
292                 if (ptr->mpu_speed <= rate)
293                         break;
294         }
295         return highest_rate;
296 }
297
298 /* Sets basic clocks based on the specified rate */
299 static int omap2_select_table_rate(struct clk *clk, unsigned long rate)
300 {
301         u32 cur_rate, done_rate, bypass = 0, tmp;
302         struct prcm_config *prcm;
303         unsigned long found_speed = 0;
304         unsigned long flags;
305
306         if (clk != &virt_prcm_set)
307                 return -EINVAL;
308
309         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
310                 if (!(prcm->flags & cpu_mask))
311                         continue;
312
313                 if (prcm->xtal_speed != sys_ck.rate)
314                         continue;
315
316                 if (prcm->mpu_speed <= rate) {
317                         found_speed = prcm->mpu_speed;
318                         break;
319                 }
320         }
321
322         if (!found_speed) {
323                 printk(KERN_INFO "Could not set MPU rate to %luMHz\n",
324                        rate / 1000000);
325                 return -EINVAL;
326         }
327
328         curr_prcm_set = prcm;
329         cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck);
330
331         if (prcm->dpll_speed == cur_rate / 2) {
332                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
333         } else if (prcm->dpll_speed == cur_rate * 2) {
334                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
335         } else if (prcm->dpll_speed != cur_rate) {
336                 local_irq_save(flags);
337
338                 if (prcm->dpll_speed == prcm->xtal_speed)
339                         bypass = 1;
340
341                 if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
342                     CORE_CLK_SRC_DPLL_X2)
343                         done_rate = CORE_CLK_SRC_DPLL_X2;
344                 else
345                         done_rate = CORE_CLK_SRC_DPLL;
346
347                 /* MPU divider */
348                 cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL);
349
350                 /* dsp + iva1 div(2420), iva2.1(2430) */
351                 cm_write_mod_reg(prcm->cm_clksel_dsp,
352                                  OMAP24XX_DSP_MOD, CM_CLKSEL);
353
354                 cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL);
355
356                 /* Major subsystem dividers */
357                 tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK;
358                 cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD,
359                                  CM_CLKSEL1);
360
361                 if (cpu_is_omap2430())
362                         cm_write_mod_reg(prcm->cm_clksel_mdm,
363                                          OMAP2430_MDM_MOD, CM_CLKSEL);
364
365                 /* x2 to enter omap2xxx_sdrc_init_params() */
366                 omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
367
368                 omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,
369                                bypass);
370
371                 omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
372                 omap2xxx_sdrc_reprogram(done_rate, 0);
373
374                 local_irq_restore(flags);
375         }
376         omap2_dpllcore_recalc(&dpll_ck);
377
378         return 0;
379 }
380
381 #ifdef CONFIG_CPU_FREQ
382 /*
383  * Walk PRCM rate table and fillout cpufreq freq_table
384  */
385 static struct cpufreq_frequency_table freq_table[ARRAY_SIZE(rate_table)];
386
387 void omap2_clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
388 {
389         struct prcm_config *prcm;
390         int i = 0;
391
392         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
393                 if (!(prcm->flags & cpu_mask))
394                         continue;
395                 if (prcm->xtal_speed != sys_ck.rate)
396                         continue;
397
398                 /* don't put bypass rates in table */
399                 if (prcm->dpll_speed == prcm->xtal_speed)
400                         continue;
401
402                 freq_table[i].index = i;
403                 freq_table[i].frequency = prcm->mpu_speed / 1000;
404                 i++;
405         }
406
407         if (i == 0) {
408                 printk(KERN_WARNING "%s: failed to initialize frequency "
409                        "table\n", __func__);
410                 return;
411         }
412
413         freq_table[i].index = i;
414         freq_table[i].frequency = CPUFREQ_TABLE_END;
415
416         *table = &freq_table[0];
417 }
418 #endif
419
420 static struct clk_functions omap2_clk_functions = {
421         .clk_enable             = omap2_clk_enable,
422         .clk_disable            = omap2_clk_disable,
423         .clk_round_rate         = omap2_clk_round_rate,
424         .clk_set_rate           = omap2_clk_set_rate,
425         .clk_set_parent         = omap2_clk_set_parent,
426         .clk_disable_unused     = omap2_clk_disable_unused,
427 #ifdef  CONFIG_CPU_FREQ
428         .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
429 #endif
430 };
431
432 static u32 omap2_get_apll_clkin(void)
433 {
434         u32 aplls, srate = 0;
435
436         aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1);
437         aplls &= OMAP24XX_APLLS_CLKIN_MASK;
438         aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT;
439
440         if (aplls == APLLS_CLKIN_19_2MHZ)
441                 srate = 19200000;
442         else if (aplls == APLLS_CLKIN_13MHZ)
443                 srate = 13000000;
444         else if (aplls == APLLS_CLKIN_12MHZ)
445                 srate = 12000000;
446
447         return srate;
448 }
449
450 static u32 omap2_get_sysclkdiv(void)
451 {
452         u32 div;
453
454         div = prm_read_mod_reg(OMAP24XX_GR_MOD,
455                                 OMAP24XX_PRCM_CLKSRC_CTRL_OFFSET);
456         div &= OMAP_SYSCLKDIV_MASK;
457         div >>= OMAP_SYSCLKDIV_SHIFT;
458
459         return div;
460 }
461
462 static void omap2_osc_clk_recalc(struct clk *clk)
463 {
464         clk->rate = omap2_get_apll_clkin() * omap2_get_sysclkdiv();
465         propagate_rate(clk);
466 }
467
468 static void omap2_sys_clk_recalc(struct clk *clk)
469 {
470         clk->rate = clk->parent->rate / omap2_get_sysclkdiv();
471         propagate_rate(clk);
472 }
473
474 /*
475  * Set clocks for bypass mode for reboot to work.
476  */
477 void omap2_clk_prepare_for_reboot(void)
478 {
479         u32 rate;
480
481         if (vclk == NULL || sclk == NULL)
482                 return;
483
484         rate = clk_get_rate(sclk);
485         clk_set_rate(vclk, rate);
486 }
487
488 /*
489  * Switch the MPU rate if specified on cmdline.
490  * We cannot do this early until cmdline is parsed.
491  */
492 static int __init omap2_clk_arch_init(void)
493 {
494         if (!mpurate)
495                 return -EINVAL;
496
497         if (omap2_select_table_rate(&virt_prcm_set, mpurate))
498                 printk(KERN_ERR "Could not find matching MPU rate\n");
499
500         recalculate_root_clocks();
501
502         printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL/MPU): "
503                "%ld.%01ld/%ld/%ld MHz\n",
504                (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
505                (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;
506
507         return 0;
508 }
509 arch_initcall(omap2_clk_arch_init);
510
511 static u32 prm_base;
512 static u32 cm_base;
513
514 /*
515  * Since we share clock data for 242x and 243x, we need to rewrite some
516  * some register base offsets. Assume offset is at prm_base if flagged,
517  * else assume it's cm_base.
518  */
519 static inline void omap2_clk_check_reg(u32 flags, void __iomem **reg)
520 {
521         u32 tmp = (__force u32)*reg;
522
523         if ((tmp >> 24) != 0)
524                 return;
525
526         if (flags & OFFSET_GR_MOD)
527                 tmp += prm_base;
528         else
529                 tmp += cm_base;
530
531         *reg = (__force void __iomem *)tmp;
532 }
533
534 static void __init omap2_clk_rewrite_base(struct clk *clk)
535 {
536         omap2_clk_check_reg(clk->flags, &clk->clksel_reg);
537         omap2_clk_check_reg(clk->flags, &clk->enable_reg);
538         if (clk->dpll_data)
539                 omap2_clk_check_reg(0, &clk->dpll_data->mult_div1_reg);
540 }
541
542 int __init omap2_clk_init(void)
543 {
544         struct prcm_config *prcm;
545         struct clk **clkp;
546         u32 clkrate;
547
548         if (cpu_is_omap242x())
549                 cpu_mask = RATE_IN_242X;
550         else if (cpu_is_omap2430())
551                 cpu_mask = RATE_IN_243X;
552
553         for (clkp = onchip_24xx_clks;
554              clkp < onchip_24xx_clks + ARRAY_SIZE(onchip_24xx_clks);
555              clkp++) {
556                         omap2_clk_rewrite_base(*clkp);
557         }
558
559         clk_init(&omap2_clk_functions);
560
561         omap2_osc_clk_recalc(&osc_ck);
562         omap2_sys_clk_recalc(&sys_ck);
563
564         for (clkp = onchip_24xx_clks;
565              clkp < onchip_24xx_clks + ARRAY_SIZE(onchip_24xx_clks);
566              clkp++) {
567
568                 if ((*clkp)->flags & CLOCK_IN_OMAP242X && cpu_is_omap2420()) {
569                         clk_register(*clkp);
570                         omap2_init_clk_clkdm(*clkp);
571                         continue;
572                 }
573
574                 if ((*clkp)->flags & CLOCK_IN_OMAP243X && cpu_is_omap2430()) {
575                         clk_register(*clkp);
576                         omap2_init_clk_clkdm(*clkp);
577                         continue;
578                 }
579         }
580
581         /* Check the MPU rate set by bootloader */
582         clkrate = omap2_get_dpll_rate_24xx(&dpll_ck);
583         for (prcm = rate_table; prcm->mpu_speed; prcm++) {
584                 if (!(prcm->flags & cpu_mask))
585                         continue;
586                 if (prcm->xtal_speed != sys_ck.rate)
587                         continue;
588                 if (prcm->dpll_speed <= clkrate)
589                          break;
590         }
591         curr_prcm_set = prcm;
592
593         recalculate_root_clocks();
594
595         printk(KERN_INFO "Clocking rate (Crystal/DPLL/MPU): "
596                "%ld.%01ld/%ld/%ld MHz\n",
597                (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10,
598                (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ;
599
600         /*
601          * Only enable those clocks we will need, let the drivers
602          * enable other clocks as necessary
603          */
604         clk_enable_init_clocks();
605
606         /* Avoid sleeping sleeping during omap2_clk_prepare_for_reboot() */
607         vclk = clk_get(NULL, "virt_prcm_set");
608         sclk = clk_get(NULL, "sys_ck");
609
610         return 0;
611 }
612
613 void __init omap2_set_globals_clock24xx(struct omap_globals *omap2_globals)
614 {
615         prm_base = (__force u32)omap2_globals->prm;
616         cm_base = (__force u32)omap2_globals->cm;
617 }