cpufreq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * arch/sh/kernel/cpufreq.c
  3. *
  4. * cpufreq driver for the SuperH processors.
  5. *
  6. * Copyright (C) 2002 - 2007 Paul Mundt
  7. * Copyright (C) 2002 M. R. Brown
  8. *
  9. * Clock framework bits from arch/avr32/mach-at32ap/cpufreq.c
  10. *
  11. * Copyright (C) 2004-2007 Atmel Corporation
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/smp.h>
  25. #include <linux/sched.h> /* set_cpus_allowed() */
  26. #include <linux/io.h>
  27. #include <linux/clk.h>
  28. #include <asm/processor.h>
  29. #include <asm/watchdog.h>
  30. #include <asm/freq.h>
  31. #include <asm/clock.h>
  32. static struct clk *cpuclk;
  33. static unsigned int sh_cpufreq_get(unsigned int cpu)
  34. {
  35. return (clk_get_rate(cpuclk) + 500) / 1000;
  36. }
  37. /*
  38. * Here we notify other drivers of the proposed change and the final change.
  39. */
  40. static int sh_cpufreq_target(struct cpufreq_policy *policy,
  41. unsigned int target_freq,
  42. unsigned int relation)
  43. {
  44. unsigned int cpu = policy->cpu;
  45. cpumask_t cpus_allowed;
  46. struct cpufreq_freqs freqs;
  47. long freq;
  48. if (!cpu_online(cpu))
  49. return -ENODEV;
  50. cpus_allowed = current->cpus_allowed;
  51. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  52. BUG_ON(smp_processor_id() != cpu);
  53. /* Convert target_freq from kHz to Hz */
  54. freq = clk_round_rate(cpuclk, target_freq * 1000);
  55. if (freq < (policy->min * 1000) || freq > (policy->max * 1000))
  56. return -EINVAL;
  57. pr_debug("cpufreq: requested frequency %u Hz\n", target_freq * 1000);
  58. freqs.cpu = cpu;
  59. freqs.old = sh_cpufreq_get(cpu);
  60. freqs.new = (freq + 500) / 1000;
  61. freqs.flags = 0;
  62. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  63. set_cpus_allowed(current, cpus_allowed);
  64. clk_set_rate(cpuclk, freq);
  65. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  66. pr_debug("cpufreq: set frequency %lu Hz\n", freq);
  67. return 0;
  68. }
  69. static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  70. {
  71. printk(KERN_INFO "cpufreq: SuperH CPU frequency driver.\n");
  72. if (!cpu_online(policy->cpu))
  73. return -ENODEV;
  74. cpuclk = clk_get(NULL, "cpu_clk");
  75. if (IS_ERR(cpuclk)) {
  76. printk(KERN_ERR "cpufreq: couldn't get CPU clk\n");
  77. return PTR_ERR(cpuclk);
  78. }
  79. /* cpuinfo and default policy values */
  80. policy->cpuinfo.min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000;
  81. policy->cpuinfo.max_freq = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000;
  82. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  83. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  84. policy->cur = sh_cpufreq_get(policy->cpu);
  85. policy->min = policy->cpuinfo.min_freq;
  86. policy->max = policy->cpuinfo.max_freq;
  87. /*
  88. * Catch the cases where the clock framework hasn't been wired up
  89. * properly to support scaling.
  90. */
  91. if (unlikely(policy->min == policy->max)) {
  92. printk(KERN_ERR "cpufreq: clock framework rate rounding "
  93. "not supported on this CPU.\n");
  94. clk_put(cpuclk);
  95. return -EINVAL;
  96. }
  97. printk(KERN_INFO "cpufreq: Frequencies - Minimum %u.%03u MHz, "
  98. "Maximum %u.%03u MHz.\n",
  99. policy->min / 1000, policy->min % 1000,
  100. policy->max / 1000, policy->max % 1000);
  101. return 0;
  102. }
  103. static int sh_cpufreq_verify(struct cpufreq_policy *policy)
  104. {
  105. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  106. policy->cpuinfo.max_freq);
  107. return 0;
  108. }
  109. static int sh_cpufreq_exit(struct cpufreq_policy *policy)
  110. {
  111. clk_put(cpuclk);
  112. return 0;
  113. }
  114. static struct cpufreq_driver sh_cpufreq_driver = {
  115. .owner = THIS_MODULE,
  116. .name = "sh",
  117. .init = sh_cpufreq_cpu_init,
  118. .verify = sh_cpufreq_verify,
  119. .target = sh_cpufreq_target,
  120. .get = sh_cpufreq_get,
  121. .exit = sh_cpufreq_exit,
  122. };
  123. static int __init sh_cpufreq_module_init(void)
  124. {
  125. return cpufreq_register_driver(&sh_cpufreq_driver);
  126. }
  127. static void __exit sh_cpufreq_module_exit(void)
  128. {
  129. cpufreq_unregister_driver(&sh_cpufreq_driver);
  130. }
  131. module_init(sh_cpufreq_module_init);
  132. module_exit(sh_cpufreq_module_exit);
  133. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  134. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  135. MODULE_LICENSE("GPL");