2 * Copyright (c) 2006-2008 Simtec Electronics
3 * http://armlinux.simtec.co.uk/
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C24XX CPU Frequency scaling
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/cpufreq.h>
18 #include <linux/cpu.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/device.h>
23 #include <linux/sysfs.h>
24 #include <linux/slab.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach/map.h>
30 #include <plat/cpu-freq-core.h>
32 #include <mach/regs-clock.h>
34 /* note, cpufreq support deals in kHz, no Hz */
36 static struct cpufreq_driver s3c24xx_driver;
37 static struct s3c_cpufreq_config cpu_cur;
38 static struct s3c_iotimings s3c24xx_iotiming;
39 static struct cpufreq_frequency_table *pll_reg;
40 static unsigned int last_target = ~0;
41 static unsigned int ftab_size;
42 static struct cpufreq_frequency_table *ftab;
44 static struct clk *_clk_mpll;
45 static struct clk *_clk_xtal;
46 static struct clk *clk_fclk;
47 static struct clk *clk_hclk;
48 static struct clk *clk_pclk;
49 static struct clk *clk_arm;
51 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ_DEBUGFS
52 struct s3c_cpufreq_config *s3c_cpufreq_getconfig(void)
57 struct s3c_iotimings *s3c_cpufreq_getiotimings(void)
59 return &s3c24xx_iotiming;
61 #endif /* CONFIG_ARM_S3C24XX_CPUFREQ_DEBUGFS */
63 static void s3c_cpufreq_getcur(struct s3c_cpufreq_config *cfg)
65 unsigned long fclk, pclk, hclk, armclk;
67 cfg->freq.fclk = fclk = clk_get_rate(clk_fclk);
68 cfg->freq.hclk = hclk = clk_get_rate(clk_hclk);
69 cfg->freq.pclk = pclk = clk_get_rate(clk_pclk);
70 cfg->freq.armclk = armclk = clk_get_rate(clk_arm);
72 cfg->pll.driver_data = __raw_readl(S3C2410_MPLLCON);
73 cfg->pll.frequency = fclk;
75 cfg->freq.hclk_tns = 1000000000 / (cfg->freq.hclk / 10);
77 cfg->divs.h_divisor = fclk / hclk;
78 cfg->divs.p_divisor = fclk / pclk;
81 static inline void s3c_cpufreq_calc(struct s3c_cpufreq_config *cfg)
83 unsigned long pll = cfg->pll.frequency;
86 cfg->freq.hclk = pll / cfg->divs.h_divisor;
87 cfg->freq.pclk = pll / cfg->divs.p_divisor;
89 /* convert hclk into 10ths of nanoseconds for io calcs */
90 cfg->freq.hclk_tns = 1000000000 / (cfg->freq.hclk / 10);
93 static inline int closer(unsigned int target, unsigned int n, unsigned int c)
95 int diff_cur = abs(target - c);
96 int diff_new = abs(target - n);
98 return (diff_new < diff_cur);
101 static void s3c_cpufreq_show(const char *pfx,
102 struct s3c_cpufreq_config *cfg)
104 s3c_freq_dbg("%s: Fvco=%u, F=%lu, A=%lu, H=%lu (%u), P=%lu (%u)\n",
105 pfx, cfg->pll.frequency, cfg->freq.fclk, cfg->freq.armclk,
106 cfg->freq.hclk, cfg->divs.h_divisor,
107 cfg->freq.pclk, cfg->divs.p_divisor);
110 /* functions to wrapper the driver info calls to do the cpu specific work */
112 static void s3c_cpufreq_setio(struct s3c_cpufreq_config *cfg)
114 if (cfg->info->set_iotiming)
115 (cfg->info->set_iotiming)(cfg, &s3c24xx_iotiming);
118 static int s3c_cpufreq_calcio(struct s3c_cpufreq_config *cfg)
120 if (cfg->info->calc_iotiming)
121 return (cfg->info->calc_iotiming)(cfg, &s3c24xx_iotiming);
126 static void s3c_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
128 (cfg->info->set_refresh)(cfg);
131 static void s3c_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
133 (cfg->info->set_divs)(cfg);
136 static int s3c_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
138 return (cfg->info->calc_divs)(cfg);
141 static void s3c_cpufreq_setfvco(struct s3c_cpufreq_config *cfg)
143 cfg->mpll = _clk_mpll;
144 (cfg->info->set_fvco)(cfg);
147 static inline void s3c_cpufreq_updateclk(struct clk *clk,
150 clk_set_rate(clk, freq);
153 static int s3c_cpufreq_settarget(struct cpufreq_policy *policy,
154 unsigned int target_freq,
155 struct cpufreq_frequency_table *pll)
157 struct s3c_cpufreq_freqs freqs;
158 struct s3c_cpufreq_config cpu_new;
161 cpu_new = cpu_cur; /* copy new from current */
163 s3c_cpufreq_show("cur", &cpu_cur);
165 /* TODO - check for DMA currently outstanding */
167 cpu_new.pll = pll ? *pll : cpu_cur.pll;
170 freqs.pll_changing = 1;
172 /* update our frequencies */
174 cpu_new.freq.armclk = target_freq;
175 cpu_new.freq.fclk = cpu_new.pll.frequency;
177 if (s3c_cpufreq_calcdivs(&cpu_new) < 0) {
178 printk(KERN_ERR "no divisors for %d\n", target_freq);
179 goto err_notpossible;
182 s3c_freq_dbg("%s: got divs\n", __func__);
184 s3c_cpufreq_calc(&cpu_new);
186 s3c_freq_dbg("%s: calculated frequencies for new\n", __func__);
188 if (cpu_new.freq.hclk != cpu_cur.freq.hclk) {
189 if (s3c_cpufreq_calcio(&cpu_new) < 0) {
190 printk(KERN_ERR "%s: no IO timings\n", __func__);
191 goto err_notpossible;
195 s3c_cpufreq_show("new", &cpu_new);
197 /* setup our cpufreq parameters */
199 freqs.old = cpu_cur.freq;
200 freqs.new = cpu_new.freq;
202 freqs.freqs.old = cpu_cur.freq.armclk / 1000;
203 freqs.freqs.new = cpu_new.freq.armclk / 1000;
205 /* update f/h/p clock settings before we issue the change
206 * notification, so that drivers do not need to do anything
207 * special if they want to recalculate on CPUFREQ_PRECHANGE. */
209 s3c_cpufreq_updateclk(_clk_mpll, cpu_new.pll.frequency);
210 s3c_cpufreq_updateclk(clk_fclk, cpu_new.freq.fclk);
211 s3c_cpufreq_updateclk(clk_hclk, cpu_new.freq.hclk);
212 s3c_cpufreq_updateclk(clk_pclk, cpu_new.freq.pclk);
214 /* start the frequency change */
215 cpufreq_freq_transition_begin(policy, &freqs.freqs);
217 /* If hclk is staying the same, then we do not need to
218 * re-write the IO or the refresh timings whilst we are changing
221 local_irq_save(flags);
223 /* is our memory clock slowing down? */
224 if (cpu_new.freq.hclk < cpu_cur.freq.hclk) {
225 s3c_cpufreq_setrefresh(&cpu_new);
226 s3c_cpufreq_setio(&cpu_new);
229 if (cpu_new.freq.fclk == cpu_cur.freq.fclk) {
230 /* not changing PLL, just set the divisors */
232 s3c_cpufreq_setdivs(&cpu_new);
234 if (cpu_new.freq.fclk < cpu_cur.freq.fclk) {
235 /* slow the cpu down, then set divisors */
237 s3c_cpufreq_setfvco(&cpu_new);
238 s3c_cpufreq_setdivs(&cpu_new);
240 /* set the divisors, then speed up */
242 s3c_cpufreq_setdivs(&cpu_new);
243 s3c_cpufreq_setfvco(&cpu_new);
247 /* did our memory clock speed up */
248 if (cpu_new.freq.hclk > cpu_cur.freq.hclk) {
249 s3c_cpufreq_setrefresh(&cpu_new);
250 s3c_cpufreq_setio(&cpu_new);
253 /* update our current settings */
256 local_irq_restore(flags);
258 /* notify everyone we've done this */
259 cpufreq_freq_transition_end(policy, &freqs.freqs, 0);
261 s3c_freq_dbg("%s: finished\n", __func__);
265 printk(KERN_ERR "no compatible settings for %d\n", target_freq);
269 /* s3c_cpufreq_target
271 * called by the cpufreq core to adjust the frequency that the CPU
272 * is currently running at.
275 static int s3c_cpufreq_target(struct cpufreq_policy *policy,
276 unsigned int target_freq,
277 unsigned int relation)
279 struct cpufreq_frequency_table *pll;
282 /* avoid repeated calls which cause a needless amout of duplicated
283 * logging output (and CPU time as the calculation process is
285 if (target_freq == last_target)
288 last_target = target_freq;
290 s3c_freq_dbg("%s: policy %p, target %u, relation %u\n",
291 __func__, policy, target_freq, relation);
294 if (cpufreq_frequency_table_target(policy, ftab,
295 target_freq, relation,
297 s3c_freq_dbg("%s: table failed\n", __func__);
301 s3c_freq_dbg("%s: adjust %d to entry %d (%u)\n", __func__,
302 target_freq, index, ftab[index].frequency);
303 target_freq = ftab[index].frequency;
306 target_freq *= 1000; /* convert target to Hz */
308 /* find the settings for our new frequency */
310 if (!pll_reg || cpu_cur.lock_pll) {
311 /* either we've not got any PLL values, or we've locked
312 * to the current one. */
315 struct cpufreq_policy tmp_policy;
318 /* we keep the cpu pll table in Hz, to ensure we get an
319 * accurate value for the PLL output. */
321 tmp_policy.min = policy->min * 1000;
322 tmp_policy.max = policy->max * 1000;
323 tmp_policy.cpu = policy->cpu;
325 /* cpufreq_frequency_table_target uses a pointer to 'index'
326 * which is the number of the table entry, not the value of
327 * the table entry's index field. */
329 ret = cpufreq_frequency_table_target(&tmp_policy, pll_reg,
330 target_freq, relation,
334 printk(KERN_ERR "%s: no PLL available\n", __func__);
335 goto err_notpossible;
338 pll = pll_reg + index;
340 s3c_freq_dbg("%s: target %u => %u\n",
341 __func__, target_freq, pll->frequency);
343 target_freq = pll->frequency;
346 return s3c_cpufreq_settarget(policy, target_freq, pll);
349 printk(KERN_ERR "no compatible settings for %d\n", target_freq);
353 struct clk *s3c_cpufreq_clk_get(struct device *dev, const char *name)
357 clk = clk_get(dev, name);
359 printk(KERN_ERR "cpufreq: failed to get clock '%s'\n", name);
364 static int s3c_cpufreq_init(struct cpufreq_policy *policy)
366 policy->clk = clk_arm;
368 policy->cpuinfo.transition_latency = cpu_cur.info->latency;
371 return cpufreq_table_validate_and_show(policy, ftab);
376 static int __init s3c_cpufreq_initclks(void)
378 _clk_mpll = s3c_cpufreq_clk_get(NULL, "mpll");
379 _clk_xtal = s3c_cpufreq_clk_get(NULL, "xtal");
380 clk_fclk = s3c_cpufreq_clk_get(NULL, "fclk");
381 clk_hclk = s3c_cpufreq_clk_get(NULL, "hclk");
382 clk_pclk = s3c_cpufreq_clk_get(NULL, "pclk");
383 clk_arm = s3c_cpufreq_clk_get(NULL, "armclk");
385 if (IS_ERR(clk_fclk) || IS_ERR(clk_hclk) || IS_ERR(clk_pclk) ||
386 IS_ERR(_clk_mpll) || IS_ERR(clk_arm) || IS_ERR(_clk_xtal)) {
387 printk(KERN_ERR "%s: could not get clock(s)\n", __func__);
391 printk(KERN_INFO "%s: clocks f=%lu,h=%lu,p=%lu,a=%lu\n", __func__,
392 clk_get_rate(clk_fclk) / 1000,
393 clk_get_rate(clk_hclk) / 1000,
394 clk_get_rate(clk_pclk) / 1000,
395 clk_get_rate(clk_arm) / 1000);
401 static struct cpufreq_frequency_table suspend_pll;
402 static unsigned int suspend_freq;
404 static int s3c_cpufreq_suspend(struct cpufreq_policy *policy)
406 suspend_pll.frequency = clk_get_rate(_clk_mpll);
407 suspend_pll.driver_data = __raw_readl(S3C2410_MPLLCON);
408 suspend_freq = clk_get_rate(clk_arm);
413 static int s3c_cpufreq_resume(struct cpufreq_policy *policy)
417 s3c_freq_dbg("%s: resuming with policy %p\n", __func__, policy);
419 last_target = ~0; /* invalidate last_target setting */
421 /* whilst we will be called later on, we try and re-set the
422 * cpu frequencies as soon as possible so that we do not end
423 * up resuming devices and then immediately having to re-set
424 * a number of settings once these devices have restarted.
426 * as a note, it is expected devices are not used until they
427 * have been un-suspended and at that time they should have
428 * used the updated clock settings.
431 ret = s3c_cpufreq_settarget(NULL, suspend_freq, &suspend_pll);
433 printk(KERN_ERR "%s: failed to reset pll/freq\n", __func__);
440 #define s3c_cpufreq_resume NULL
441 #define s3c_cpufreq_suspend NULL
444 static struct cpufreq_driver s3c24xx_driver = {
445 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
446 .target = s3c_cpufreq_target,
447 .get = cpufreq_generic_get,
448 .init = s3c_cpufreq_init,
449 .suspend = s3c_cpufreq_suspend,
450 .resume = s3c_cpufreq_resume,
455 int s3c_cpufreq_register(struct s3c_cpufreq_info *info)
457 if (!info || !info->name) {
458 printk(KERN_ERR "%s: failed to pass valid information\n",
463 printk(KERN_INFO "S3C24XX CPU Frequency driver, %s cpu support\n",
466 /* check our driver info has valid data */
468 BUG_ON(info->set_refresh == NULL);
469 BUG_ON(info->set_divs == NULL);
470 BUG_ON(info->calc_divs == NULL);
472 /* info->set_fvco is optional, depending on whether there
473 * is a need to set the clock code. */
477 /* Note, driver registering should probably update locktime */
482 int __init s3c_cpufreq_setboard(struct s3c_cpufreq_board *board)
484 struct s3c_cpufreq_board *ours;
487 printk(KERN_INFO "%s: no board data\n", __func__);
491 /* Copy the board information so that each board can make this
494 ours = kzalloc(sizeof(*ours), GFP_KERNEL);
496 printk(KERN_ERR "%s: no memory\n", __func__);
501 cpu_cur.board = ours;
506 static int __init s3c_cpufreq_auto_io(void)
510 if (!cpu_cur.info->get_iotiming) {
511 printk(KERN_ERR "%s: get_iotiming undefined\n", __func__);
515 printk(KERN_INFO "%s: working out IO settings\n", __func__);
517 ret = (cpu_cur.info->get_iotiming)(&cpu_cur, &s3c24xx_iotiming);
519 printk(KERN_ERR "%s: failed to get timings\n", __func__);
524 /* if one or is zero, then return the other, otherwise return the min */
525 #define do_min(_a, _b) ((_a) == 0 ? (_b) : (_b) == 0 ? (_a) : min(_a, _b))
528 * s3c_cpufreq_freq_min - find the minimum settings for the given freq.
529 * @dst: The destination structure
531 * @b: The other argument.
533 * Create a minimum of each frequency entry in the 'struct s3c_freq',
534 * unless the entry is zero when it is ignored and the non-zero argument
537 static void s3c_cpufreq_freq_min(struct s3c_freq *dst,
538 struct s3c_freq *a, struct s3c_freq *b)
540 dst->fclk = do_min(a->fclk, b->fclk);
541 dst->hclk = do_min(a->hclk, b->hclk);
542 dst->pclk = do_min(a->pclk, b->pclk);
543 dst->armclk = do_min(a->armclk, b->armclk);
546 static inline u32 calc_locktime(u32 freq, u32 time_us)
550 result = freq * time_us;
551 result = DIV_ROUND_UP(result, 1000 * 1000);
556 static void s3c_cpufreq_update_loctkime(void)
558 unsigned int bits = cpu_cur.info->locktime_bits;
559 u32 rate = (u32)clk_get_rate(_clk_xtal);
567 val = calc_locktime(rate, cpu_cur.info->locktime_u) << bits;
568 val |= calc_locktime(rate, cpu_cur.info->locktime_m);
570 printk(KERN_INFO "%s: new locktime is 0x%08x\n", __func__, val);
571 __raw_writel(val, S3C2410_LOCKTIME);
574 static int s3c_cpufreq_build_freq(void)
578 if (!cpu_cur.info->calc_freqtable)
584 size = cpu_cur.info->calc_freqtable(&cpu_cur, NULL, 0);
587 ftab = kzalloc(sizeof(*ftab) * size, GFP_KERNEL);
589 printk(KERN_ERR "%s: no memory for tables\n", __func__);
595 ret = cpu_cur.info->calc_freqtable(&cpu_cur, ftab, size);
596 s3c_cpufreq_addfreq(ftab, ret, size, CPUFREQ_TABLE_END);
601 static int __init s3c_cpufreq_initcall(void)
605 if (cpu_cur.info && cpu_cur.board) {
606 ret = s3c_cpufreq_initclks();
610 /* get current settings */
611 s3c_cpufreq_getcur(&cpu_cur);
612 s3c_cpufreq_show("cur", &cpu_cur);
614 if (cpu_cur.board->auto_io) {
615 ret = s3c_cpufreq_auto_io();
617 printk(KERN_ERR "%s: failed to get io timing\n",
623 if (cpu_cur.board->need_io && !cpu_cur.info->set_iotiming) {
624 printk(KERN_ERR "%s: no IO support registered\n",
630 if (!cpu_cur.info->need_pll)
631 cpu_cur.lock_pll = 1;
633 s3c_cpufreq_update_loctkime();
635 s3c_cpufreq_freq_min(&cpu_cur.max, &cpu_cur.board->max,
638 if (cpu_cur.info->calc_freqtable)
639 s3c_cpufreq_build_freq();
641 ret = cpufreq_register_driver(&s3c24xx_driver);
648 late_initcall(s3c_cpufreq_initcall);
651 * s3c_plltab_register - register CPU PLL table.
652 * @plls: The list of PLL entries.
653 * @plls_no: The size of the PLL entries @plls.
655 * Register the given set of PLLs with the system.
657 int s3c_plltab_register(struct cpufreq_frequency_table *plls,
658 unsigned int plls_no)
660 struct cpufreq_frequency_table *vals;
663 size = sizeof(*vals) * (plls_no + 1);
665 vals = kzalloc(size, GFP_KERNEL);
667 memcpy(vals, plls, size);
670 /* write a terminating entry, we don't store it in the
671 * table that is stored in the kernel */
673 vals->frequency = CPUFREQ_TABLE_END;
675 printk(KERN_INFO "cpufreq: %d PLL entries\n", plls_no);
677 printk(KERN_ERR "cpufreq: no memory for PLL tables\n");
679 return vals ? 0 : -ENOMEM;