drm/i915: Mark the context and address space as closed
[linux/fpc-iii.git] / drivers / cpufreq / s3c24xx-cpufreq.c
blobae8eaed77b70748e961aaac9f4fb4b8e7ff6447d
1 /*
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/cpufreq.h>
20 #include <linux/cpu.h>
21 #include <linux/clk.h>
22 #include <linux/err.h>
23 #include <linux/io.h>
24 #include <linux/device.h>
25 #include <linux/sysfs.h>
26 #include <linux/slab.h>
28 #include <asm/mach/arch.h>
29 #include <asm/mach/map.h>
31 #include <plat/cpu.h>
32 #include <plat/cpu-freq-core.h>
34 #include <mach/regs-clock.h>
36 /* note, cpufreq support deals in kHz, no Hz */
38 static struct cpufreq_driver s3c24xx_driver;
39 static struct s3c_cpufreq_config cpu_cur;
40 static struct s3c_iotimings s3c24xx_iotiming;
41 static struct cpufreq_frequency_table *pll_reg;
42 static unsigned int last_target = ~0;
43 static unsigned int ftab_size;
44 static struct cpufreq_frequency_table *ftab;
46 static struct clk *_clk_mpll;
47 static struct clk *_clk_xtal;
48 static struct clk *clk_fclk;
49 static struct clk *clk_hclk;
50 static struct clk *clk_pclk;
51 static struct clk *clk_arm;
53 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ_DEBUGFS
54 struct s3c_cpufreq_config *s3c_cpufreq_getconfig(void)
56 return &cpu_cur;
59 struct s3c_iotimings *s3c_cpufreq_getiotimings(void)
61 return &s3c24xx_iotiming;
63 #endif /* CONFIG_ARM_S3C24XX_CPUFREQ_DEBUGFS */
65 static void s3c_cpufreq_getcur(struct s3c_cpufreq_config *cfg)
67 unsigned long fclk, pclk, hclk, armclk;
69 cfg->freq.fclk = fclk = clk_get_rate(clk_fclk);
70 cfg->freq.hclk = hclk = clk_get_rate(clk_hclk);
71 cfg->freq.pclk = pclk = clk_get_rate(clk_pclk);
72 cfg->freq.armclk = armclk = clk_get_rate(clk_arm);
74 cfg->pll.driver_data = __raw_readl(S3C2410_MPLLCON);
75 cfg->pll.frequency = fclk;
77 cfg->freq.hclk_tns = 1000000000 / (cfg->freq.hclk / 10);
79 cfg->divs.h_divisor = fclk / hclk;
80 cfg->divs.p_divisor = fclk / pclk;
83 static inline void s3c_cpufreq_calc(struct s3c_cpufreq_config *cfg)
85 unsigned long pll = cfg->pll.frequency;
87 cfg->freq.fclk = pll;
88 cfg->freq.hclk = pll / cfg->divs.h_divisor;
89 cfg->freq.pclk = pll / cfg->divs.p_divisor;
91 /* convert hclk into 10ths of nanoseconds for io calcs */
92 cfg->freq.hclk_tns = 1000000000 / (cfg->freq.hclk / 10);
95 static inline int closer(unsigned int target, unsigned int n, unsigned int c)
97 int diff_cur = abs(target - c);
98 int diff_new = abs(target - n);
100 return (diff_new < diff_cur);
103 static void s3c_cpufreq_show(const char *pfx,
104 struct s3c_cpufreq_config *cfg)
106 s3c_freq_dbg("%s: Fvco=%u, F=%lu, A=%lu, H=%lu (%u), P=%lu (%u)\n",
107 pfx, cfg->pll.frequency, cfg->freq.fclk, cfg->freq.armclk,
108 cfg->freq.hclk, cfg->divs.h_divisor,
109 cfg->freq.pclk, cfg->divs.p_divisor);
112 /* functions to wrapper the driver info calls to do the cpu specific work */
114 static void s3c_cpufreq_setio(struct s3c_cpufreq_config *cfg)
116 if (cfg->info->set_iotiming)
117 (cfg->info->set_iotiming)(cfg, &s3c24xx_iotiming);
120 static int s3c_cpufreq_calcio(struct s3c_cpufreq_config *cfg)
122 if (cfg->info->calc_iotiming)
123 return (cfg->info->calc_iotiming)(cfg, &s3c24xx_iotiming);
125 return 0;
128 static void s3c_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
130 (cfg->info->set_refresh)(cfg);
133 static void s3c_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
135 (cfg->info->set_divs)(cfg);
138 static int s3c_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
140 return (cfg->info->calc_divs)(cfg);
143 static void s3c_cpufreq_setfvco(struct s3c_cpufreq_config *cfg)
145 cfg->mpll = _clk_mpll;
146 (cfg->info->set_fvco)(cfg);
149 static inline void s3c_cpufreq_updateclk(struct clk *clk,
150 unsigned int freq)
152 clk_set_rate(clk, freq);
155 static int s3c_cpufreq_settarget(struct cpufreq_policy *policy,
156 unsigned int target_freq,
157 struct cpufreq_frequency_table *pll)
159 struct s3c_cpufreq_freqs freqs;
160 struct s3c_cpufreq_config cpu_new;
161 unsigned long flags;
163 cpu_new = cpu_cur; /* copy new from current */
165 s3c_cpufreq_show("cur", &cpu_cur);
167 /* TODO - check for DMA currently outstanding */
169 cpu_new.pll = pll ? *pll : cpu_cur.pll;
171 if (pll)
172 freqs.pll_changing = 1;
174 /* update our frequencies */
176 cpu_new.freq.armclk = target_freq;
177 cpu_new.freq.fclk = cpu_new.pll.frequency;
179 if (s3c_cpufreq_calcdivs(&cpu_new) < 0) {
180 pr_err("no divisors for %d\n", target_freq);
181 goto err_notpossible;
184 s3c_freq_dbg("%s: got divs\n", __func__);
186 s3c_cpufreq_calc(&cpu_new);
188 s3c_freq_dbg("%s: calculated frequencies for new\n", __func__);
190 if (cpu_new.freq.hclk != cpu_cur.freq.hclk) {
191 if (s3c_cpufreq_calcio(&cpu_new) < 0) {
192 pr_err("%s: no IO timings\n", __func__);
193 goto err_notpossible;
197 s3c_cpufreq_show("new", &cpu_new);
199 /* setup our cpufreq parameters */
201 freqs.old = cpu_cur.freq;
202 freqs.new = cpu_new.freq;
204 freqs.freqs.old = cpu_cur.freq.armclk / 1000;
205 freqs.freqs.new = cpu_new.freq.armclk / 1000;
207 /* update f/h/p clock settings before we issue the change
208 * notification, so that drivers do not need to do anything
209 * special if they want to recalculate on CPUFREQ_PRECHANGE. */
211 s3c_cpufreq_updateclk(_clk_mpll, cpu_new.pll.frequency);
212 s3c_cpufreq_updateclk(clk_fclk, cpu_new.freq.fclk);
213 s3c_cpufreq_updateclk(clk_hclk, cpu_new.freq.hclk);
214 s3c_cpufreq_updateclk(clk_pclk, cpu_new.freq.pclk);
216 /* start the frequency change */
217 cpufreq_freq_transition_begin(policy, &freqs.freqs);
219 /* If hclk is staying the same, then we do not need to
220 * re-write the IO or the refresh timings whilst we are changing
221 * speed. */
223 local_irq_save(flags);
225 /* is our memory clock slowing down? */
226 if (cpu_new.freq.hclk < cpu_cur.freq.hclk) {
227 s3c_cpufreq_setrefresh(&cpu_new);
228 s3c_cpufreq_setio(&cpu_new);
231 if (cpu_new.freq.fclk == cpu_cur.freq.fclk) {
232 /* not changing PLL, just set the divisors */
234 s3c_cpufreq_setdivs(&cpu_new);
235 } else {
236 if (cpu_new.freq.fclk < cpu_cur.freq.fclk) {
237 /* slow the cpu down, then set divisors */
239 s3c_cpufreq_setfvco(&cpu_new);
240 s3c_cpufreq_setdivs(&cpu_new);
241 } else {
242 /* set the divisors, then speed up */
244 s3c_cpufreq_setdivs(&cpu_new);
245 s3c_cpufreq_setfvco(&cpu_new);
249 /* did our memory clock speed up */
250 if (cpu_new.freq.hclk > cpu_cur.freq.hclk) {
251 s3c_cpufreq_setrefresh(&cpu_new);
252 s3c_cpufreq_setio(&cpu_new);
255 /* update our current settings */
256 cpu_cur = cpu_new;
258 local_irq_restore(flags);
260 /* notify everyone we've done this */
261 cpufreq_freq_transition_end(policy, &freqs.freqs, 0);
263 s3c_freq_dbg("%s: finished\n", __func__);
264 return 0;
266 err_notpossible:
267 pr_err("no compatible settings for %d\n", target_freq);
268 return -EINVAL;
271 /* s3c_cpufreq_target
273 * called by the cpufreq core to adjust the frequency that the CPU
274 * is currently running at.
277 static int s3c_cpufreq_target(struct cpufreq_policy *policy,
278 unsigned int target_freq,
279 unsigned int relation)
281 struct cpufreq_frequency_table *pll;
282 unsigned int index;
284 /* avoid repeated calls which cause a needless amout of duplicated
285 * logging output (and CPU time as the calculation process is
286 * done) */
287 if (target_freq == last_target)
288 return 0;
290 last_target = target_freq;
292 s3c_freq_dbg("%s: policy %p, target %u, relation %u\n",
293 __func__, policy, target_freq, relation);
295 if (ftab) {
296 if (cpufreq_frequency_table_target(policy, ftab,
297 target_freq, relation,
298 &index)) {
299 s3c_freq_dbg("%s: table failed\n", __func__);
300 return -EINVAL;
303 s3c_freq_dbg("%s: adjust %d to entry %d (%u)\n", __func__,
304 target_freq, index, ftab[index].frequency);
305 target_freq = ftab[index].frequency;
308 target_freq *= 1000; /* convert target to Hz */
310 /* find the settings for our new frequency */
312 if (!pll_reg || cpu_cur.lock_pll) {
313 /* either we've not got any PLL values, or we've locked
314 * to the current one. */
315 pll = NULL;
316 } else {
317 struct cpufreq_policy tmp_policy;
318 int ret;
320 /* we keep the cpu pll table in Hz, to ensure we get an
321 * accurate value for the PLL output. */
323 tmp_policy.min = policy->min * 1000;
324 tmp_policy.max = policy->max * 1000;
325 tmp_policy.cpu = policy->cpu;
327 /* cpufreq_frequency_table_target uses a pointer to 'index'
328 * which is the number of the table entry, not the value of
329 * the table entry's index field. */
331 ret = cpufreq_frequency_table_target(&tmp_policy, pll_reg,
332 target_freq, relation,
333 &index);
335 if (ret < 0) {
336 pr_err("%s: no PLL available\n", __func__);
337 goto err_notpossible;
340 pll = pll_reg + index;
342 s3c_freq_dbg("%s: target %u => %u\n",
343 __func__, target_freq, pll->frequency);
345 target_freq = pll->frequency;
348 return s3c_cpufreq_settarget(policy, target_freq, pll);
350 err_notpossible:
351 pr_err("no compatible settings for %d\n", target_freq);
352 return -EINVAL;
355 struct clk *s3c_cpufreq_clk_get(struct device *dev, const char *name)
357 struct clk *clk;
359 clk = clk_get(dev, name);
360 if (IS_ERR(clk))
361 pr_err("failed to get clock '%s'\n", name);
363 return clk;
366 static int s3c_cpufreq_init(struct cpufreq_policy *policy)
368 policy->clk = clk_arm;
369 return cpufreq_generic_init(policy, ftab, cpu_cur.info->latency);
372 static int __init s3c_cpufreq_initclks(void)
374 _clk_mpll = s3c_cpufreq_clk_get(NULL, "mpll");
375 _clk_xtal = s3c_cpufreq_clk_get(NULL, "xtal");
376 clk_fclk = s3c_cpufreq_clk_get(NULL, "fclk");
377 clk_hclk = s3c_cpufreq_clk_get(NULL, "hclk");
378 clk_pclk = s3c_cpufreq_clk_get(NULL, "pclk");
379 clk_arm = s3c_cpufreq_clk_get(NULL, "armclk");
381 if (IS_ERR(clk_fclk) || IS_ERR(clk_hclk) || IS_ERR(clk_pclk) ||
382 IS_ERR(_clk_mpll) || IS_ERR(clk_arm) || IS_ERR(_clk_xtal)) {
383 pr_err("%s: could not get clock(s)\n", __func__);
384 return -ENOENT;
387 pr_info("%s: clocks f=%lu,h=%lu,p=%lu,a=%lu\n",
388 __func__,
389 clk_get_rate(clk_fclk) / 1000,
390 clk_get_rate(clk_hclk) / 1000,
391 clk_get_rate(clk_pclk) / 1000,
392 clk_get_rate(clk_arm) / 1000);
394 return 0;
397 #ifdef CONFIG_PM
398 static struct cpufreq_frequency_table suspend_pll;
399 static unsigned int suspend_freq;
401 static int s3c_cpufreq_suspend(struct cpufreq_policy *policy)
403 suspend_pll.frequency = clk_get_rate(_clk_mpll);
404 suspend_pll.driver_data = __raw_readl(S3C2410_MPLLCON);
405 suspend_freq = clk_get_rate(clk_arm);
407 return 0;
410 static int s3c_cpufreq_resume(struct cpufreq_policy *policy)
412 int ret;
414 s3c_freq_dbg("%s: resuming with policy %p\n", __func__, policy);
416 last_target = ~0; /* invalidate last_target setting */
418 /* whilst we will be called later on, we try and re-set the
419 * cpu frequencies as soon as possible so that we do not end
420 * up resuming devices and then immediately having to re-set
421 * a number of settings once these devices have restarted.
423 * as a note, it is expected devices are not used until they
424 * have been un-suspended and at that time they should have
425 * used the updated clock settings.
428 ret = s3c_cpufreq_settarget(NULL, suspend_freq, &suspend_pll);
429 if (ret) {
430 pr_err("%s: failed to reset pll/freq\n", __func__);
431 return ret;
434 return 0;
436 #else
437 #define s3c_cpufreq_resume NULL
438 #define s3c_cpufreq_suspend NULL
439 #endif
441 static struct cpufreq_driver s3c24xx_driver = {
442 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
443 .target = s3c_cpufreq_target,
444 .get = cpufreq_generic_get,
445 .init = s3c_cpufreq_init,
446 .suspend = s3c_cpufreq_suspend,
447 .resume = s3c_cpufreq_resume,
448 .name = "s3c24xx",
452 int s3c_cpufreq_register(struct s3c_cpufreq_info *info)
454 if (!info || !info->name) {
455 pr_err("%s: failed to pass valid information\n", __func__);
456 return -EINVAL;
459 pr_info("S3C24XX CPU Frequency driver, %s cpu support\n",
460 info->name);
462 /* check our driver info has valid data */
464 BUG_ON(info->set_refresh == NULL);
465 BUG_ON(info->set_divs == NULL);
466 BUG_ON(info->calc_divs == NULL);
468 /* info->set_fvco is optional, depending on whether there
469 * is a need to set the clock code. */
471 cpu_cur.info = info;
473 /* Note, driver registering should probably update locktime */
475 return 0;
478 int __init s3c_cpufreq_setboard(struct s3c_cpufreq_board *board)
480 struct s3c_cpufreq_board *ours;
482 if (!board) {
483 pr_info("%s: no board data\n", __func__);
484 return -EINVAL;
487 /* Copy the board information so that each board can make this
488 * initdata. */
490 ours = kzalloc(sizeof(*ours), GFP_KERNEL);
491 if (ours == NULL) {
492 pr_err("%s: no memory\n", __func__);
493 return -ENOMEM;
496 *ours = *board;
497 cpu_cur.board = ours;
499 return 0;
502 static int __init s3c_cpufreq_auto_io(void)
504 int ret;
506 if (!cpu_cur.info->get_iotiming) {
507 pr_err("%s: get_iotiming undefined\n", __func__);
508 return -ENOENT;
511 pr_info("%s: working out IO settings\n", __func__);
513 ret = (cpu_cur.info->get_iotiming)(&cpu_cur, &s3c24xx_iotiming);
514 if (ret)
515 pr_err("%s: failed to get timings\n", __func__);
517 return ret;
520 /* if one or is zero, then return the other, otherwise return the min */
521 #define do_min(_a, _b) ((_a) == 0 ? (_b) : (_b) == 0 ? (_a) : min(_a, _b))
524 * s3c_cpufreq_freq_min - find the minimum settings for the given freq.
525 * @dst: The destination structure
526 * @a: One argument.
527 * @b: The other argument.
529 * Create a minimum of each frequency entry in the 'struct s3c_freq',
530 * unless the entry is zero when it is ignored and the non-zero argument
531 * used.
533 static void s3c_cpufreq_freq_min(struct s3c_freq *dst,
534 struct s3c_freq *a, struct s3c_freq *b)
536 dst->fclk = do_min(a->fclk, b->fclk);
537 dst->hclk = do_min(a->hclk, b->hclk);
538 dst->pclk = do_min(a->pclk, b->pclk);
539 dst->armclk = do_min(a->armclk, b->armclk);
542 static inline u32 calc_locktime(u32 freq, u32 time_us)
544 u32 result;
546 result = freq * time_us;
547 result = DIV_ROUND_UP(result, 1000 * 1000);
549 return result;
552 static void s3c_cpufreq_update_loctkime(void)
554 unsigned int bits = cpu_cur.info->locktime_bits;
555 u32 rate = (u32)clk_get_rate(_clk_xtal);
556 u32 val;
558 if (bits == 0) {
559 WARN_ON(1);
560 return;
563 val = calc_locktime(rate, cpu_cur.info->locktime_u) << bits;
564 val |= calc_locktime(rate, cpu_cur.info->locktime_m);
566 pr_info("%s: new locktime is 0x%08x\n", __func__, val);
567 __raw_writel(val, S3C2410_LOCKTIME);
570 static int s3c_cpufreq_build_freq(void)
572 int size, ret;
574 if (!cpu_cur.info->calc_freqtable)
575 return -EINVAL;
577 kfree(ftab);
578 ftab = NULL;
580 size = cpu_cur.info->calc_freqtable(&cpu_cur, NULL, 0);
581 size++;
583 ftab = kzalloc(sizeof(*ftab) * size, GFP_KERNEL);
584 if (!ftab) {
585 pr_err("%s: no memory for tables\n", __func__);
586 return -ENOMEM;
589 ftab_size = size;
591 ret = cpu_cur.info->calc_freqtable(&cpu_cur, ftab, size);
592 s3c_cpufreq_addfreq(ftab, ret, size, CPUFREQ_TABLE_END);
594 return 0;
597 static int __init s3c_cpufreq_initcall(void)
599 int ret = 0;
601 if (cpu_cur.info && cpu_cur.board) {
602 ret = s3c_cpufreq_initclks();
603 if (ret)
604 goto out;
606 /* get current settings */
607 s3c_cpufreq_getcur(&cpu_cur);
608 s3c_cpufreq_show("cur", &cpu_cur);
610 if (cpu_cur.board->auto_io) {
611 ret = s3c_cpufreq_auto_io();
612 if (ret) {
613 pr_err("%s: failed to get io timing\n",
614 __func__);
615 goto out;
619 if (cpu_cur.board->need_io && !cpu_cur.info->set_iotiming) {
620 pr_err("%s: no IO support registered\n", __func__);
621 ret = -EINVAL;
622 goto out;
625 if (!cpu_cur.info->need_pll)
626 cpu_cur.lock_pll = 1;
628 s3c_cpufreq_update_loctkime();
630 s3c_cpufreq_freq_min(&cpu_cur.max, &cpu_cur.board->max,
631 &cpu_cur.info->max);
633 if (cpu_cur.info->calc_freqtable)
634 s3c_cpufreq_build_freq();
636 ret = cpufreq_register_driver(&s3c24xx_driver);
639 out:
640 return ret;
643 late_initcall(s3c_cpufreq_initcall);
646 * s3c_plltab_register - register CPU PLL table.
647 * @plls: The list of PLL entries.
648 * @plls_no: The size of the PLL entries @plls.
650 * Register the given set of PLLs with the system.
652 int s3c_plltab_register(struct cpufreq_frequency_table *plls,
653 unsigned int plls_no)
655 struct cpufreq_frequency_table *vals;
656 unsigned int size;
658 size = sizeof(*vals) * (plls_no + 1);
660 vals = kzalloc(size, GFP_KERNEL);
661 if (vals) {
662 memcpy(vals, plls, size);
663 pll_reg = vals;
665 /* write a terminating entry, we don't store it in the
666 * table that is stored in the kernel */
667 vals += plls_no;
668 vals->frequency = CPUFREQ_TABLE_END;
670 pr_info("%d PLL entries\n", plls_no);
671 } else
672 pr_err("no memory for PLL tables\n");
674 return vals ? 0 : -ENOMEM;