cpufreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * arch/sh/kernel/cpufreq.c
  3. *
  4. * cpufreq driver for the SuperH processors.
  5. *
  6. * Copyright (C) 2002, 2003, 2004, 2005 Paul Mundt
  7. * Copyright (C) 2002 M. R. Brown
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/smp.h>
  23. #include <linux/sched.h> /* set_cpus_allowed() */
  24. #include <asm/processor.h>
  25. #include <asm/watchdog.h>
  26. #include <asm/freq.h>
  27. #include <asm/io.h>
  28. /*
  29. * For SuperH, each policy change requires that we change the IFC, BFC, and
  30. * PFC at the same time. Here we define sane values that won't trash the
  31. * system.
  32. *
  33. * Note the max set is computed at runtime, we use the divisors that we booted
  34. * with to setup our maximum operating frequencies.
  35. */
  36. struct clock_set {
  37. unsigned int ifc;
  38. unsigned int bfc;
  39. unsigned int pfc;
  40. } clock_sets[] = {
  41. #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH2)
  42. { 0, 0, 0 }, /* not implemented yet */
  43. #elif defined(CONFIG_CPU_SH4)
  44. { 4, 8, 8 }, /* min - IFC: 1/4, BFC: 1/8, PFC: 1/8 */
  45. { 1, 2, 2 }, /* max - IFC: 1, BFC: 1/2, PFC: 1/2 */
  46. #endif
  47. };
  48. #define MIN_CLOCK_SET 0
  49. #define MAX_CLOCK_SET (ARRAY_SIZE(clock_sets) - 1)
  50. /*
  51. * For the time being, we only support two frequencies, which in turn are
  52. * aimed at the POWERSAVE and PERFORMANCE policies, which in turn are derived
  53. * directly from the respective min/max clock sets. Technically we could
  54. * support a wider range of frequencies, but these vary far too much for each
  55. * CPU subtype (and we'd have to construct a frequency table for each subtype).
  56. *
  57. * Maybe something to implement in the future..
  58. */
  59. #define SH_FREQ_MAX 0
  60. #define SH_FREQ_MIN 1
  61. static struct cpufreq_frequency_table sh_freqs[] = {
  62. { SH_FREQ_MAX, 0 },
  63. { SH_FREQ_MIN, 0 },
  64. { 0, CPUFREQ_TABLE_END },
  65. };
  66. static void sh_cpufreq_update_clocks(unsigned int set)
  67. {
  68. current_cpu_data.cpu_clock = current_cpu_data.master_clock / clock_sets[set].ifc;
  69. current_cpu_data.bus_clock = current_cpu_data.master_clock / clock_sets[set].bfc;
  70. current_cpu_data.module_clock = current_cpu_data.master_clock / clock_sets[set].pfc;
  71. current_cpu_data.loops_per_jiffy = loops_per_jiffy;
  72. }
  73. /* XXX: This needs to be split out per CPU and CPU subtype. */
  74. /*
  75. * Here we notify other drivers of the proposed change and the final change.
  76. */
  77. static int sh_cpufreq_setstate(unsigned int cpu, unsigned int set)
  78. {
  79. unsigned short frqcr = ctrl_inw(FRQCR);
  80. cpumask_t cpus_allowed;
  81. struct cpufreq_freqs freqs;
  82. if (!cpu_online(cpu))
  83. return -ENODEV;
  84. cpus_allowed = current->cpus_allowed;
  85. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  86. BUG_ON(smp_processor_id() != cpu);
  87. freqs.cpu = cpu;
  88. freqs.old = current_cpu_data.cpu_clock / 1000;
  89. freqs.new = (current_cpu_data.master_clock / clock_sets[set].ifc) / 1000;
  90. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  91. #if defined(CONFIG_CPU_SH3)
  92. frqcr |= (newstate & 0x4000) << 14;
  93. frqcr |= (newstate & 0x000c) << 2;
  94. #elif defined(CONFIG_CPU_SH4)
  95. /*
  96. * FRQCR.PLL2EN is 1, we need to allow the PLL to stabilize by
  97. * initializing the WDT.
  98. */
  99. if (frqcr & (1 << 9)) {
  100. __u8 csr;
  101. /*
  102. * Set the overflow period to the highest available,
  103. * in this case a 1/4096 division ratio yields a 5.25ms
  104. * overflow period. See asm-sh/watchdog.h for more
  105. * information and a range of other divisors.
  106. */
  107. csr = sh_wdt_read_csr();
  108. csr |= WTCSR_CKS_4096;
  109. sh_wdt_write_csr(csr);
  110. sh_wdt_write_cnt(0);
  111. }
  112. frqcr &= 0x0e00; /* Clear ifc, bfc, pfc */
  113. frqcr |= get_ifc_value(clock_sets[set].ifc) << 6;
  114. frqcr |= get_bfc_value(clock_sets[set].bfc) << 3;
  115. frqcr |= get_pfc_value(clock_sets[set].pfc);
  116. #endif
  117. ctrl_outw(frqcr, FRQCR);
  118. sh_cpufreq_update_clocks(set);
  119. set_cpus_allowed(current, cpus_allowed);
  120. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  121. return 0;
  122. }
  123. static int sh_cpufreq_cpu_init(struct cpufreq_policy *policy)
  124. {
  125. unsigned int min_freq, max_freq;
  126. unsigned int ifc, bfc, pfc;
  127. if (!cpu_online(policy->cpu))
  128. return -ENODEV;
  129. /* Update our maximum clock set */
  130. get_current_frequency_divisors(&ifc, &bfc, &pfc);
  131. clock_sets[MAX_CLOCK_SET].ifc = ifc;
  132. clock_sets[MAX_CLOCK_SET].bfc = bfc;
  133. clock_sets[MAX_CLOCK_SET].pfc = pfc;
  134. /* Convert from Hz to kHz */
  135. max_freq = current_cpu_data.cpu_clock / 1000;
  136. min_freq = (current_cpu_data.master_clock / clock_sets[MIN_CLOCK_SET].ifc) / 1000;
  137. sh_freqs[SH_FREQ_MAX].frequency = max_freq;
  138. sh_freqs[SH_FREQ_MIN].frequency = min_freq;
  139. /* cpuinfo and default policy values */
  140. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  141. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  142. policy->cur = max_freq;
  143. return cpufreq_frequency_table_cpuinfo(policy, &sh_freqs[0]);
  144. }
  145. static int sh_cpufreq_verify(struct cpufreq_policy *policy)
  146. {
  147. return cpufreq_frequency_table_verify(policy, &sh_freqs[0]);
  148. }
  149. static int sh_cpufreq_target(struct cpufreq_policy *policy,
  150. unsigned int target_freq,
  151. unsigned int relation)
  152. {
  153. unsigned int set, idx = 0;
  154. if (cpufreq_frequency_table_target(policy, &sh_freqs[0], target_freq, relation, &idx))
  155. return -EINVAL;
  156. set = (idx == SH_FREQ_MIN) ? MIN_CLOCK_SET : MAX_CLOCK_SET;
  157. sh_cpufreq_setstate(policy->cpu, set);
  158. return 0;
  159. }
  160. static struct cpufreq_driver sh_cpufreq_driver = {
  161. .owner = THIS_MODULE,
  162. .name = "SH cpufreq",
  163. .init = sh_cpufreq_cpu_init,
  164. .verify = sh_cpufreq_verify,
  165. .target = sh_cpufreq_target,
  166. };
  167. static int __init sh_cpufreq_init(void)
  168. {
  169. if (!current_cpu_data.cpu_clock)
  170. return -EINVAL;
  171. if (cpufreq_register_driver(&sh_cpufreq_driver))
  172. return -EINVAL;
  173. return 0;
  174. }
  175. static void __exit sh_cpufreq_exit(void)
  176. {
  177. cpufreq_unregister_driver(&sh_cpufreq_driver);
  178. }
  179. module_init(sh_cpufreq_init);
  180. module_exit(sh_cpufreq_exit);
  181. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  182. MODULE_DESCRIPTION("cpufreq driver for SuperH");
  183. MODULE_LICENSE("GPL");