cbe_cpufreq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * cpufreq driver for the cell processor
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Christian Krafft <krafft@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/cpufreq.h>
  23. #include <linux/timer.h>
  24. #include <asm/hw_irq.h>
  25. #include <asm/io.h>
  26. #include <asm/processor.h>
  27. #include <asm/prom.h>
  28. #include <asm/time.h>
  29. #include "cbe_regs.h"
  30. static DEFINE_MUTEX(cbe_switch_mutex);
  31. /* the CBE supports an 8 step frequency scaling */
  32. static struct cpufreq_frequency_table cbe_freqs[] = {
  33. {1, 0},
  34. {2, 0},
  35. {3, 0},
  36. {4, 0},
  37. {5, 0},
  38. {6, 0},
  39. {8, 0},
  40. {10, 0},
  41. {0, CPUFREQ_TABLE_END},
  42. };
  43. /* to write to MIC register */
  44. static u64 MIC_Slow_Fast_Timer_table[] = {
  45. [0 ... 7] = 0x007fc00000000000ull,
  46. };
  47. /* more values for the MIC */
  48. static u64 MIC_Slow_Next_Timer_table[] = {
  49. 0x0000240000000000ull,
  50. 0x0000268000000000ull,
  51. 0x000029C000000000ull,
  52. 0x00002D0000000000ull,
  53. 0x0000300000000000ull,
  54. 0x0000334000000000ull,
  55. 0x000039C000000000ull,
  56. 0x00003FC000000000ull,
  57. };
  58. /*
  59. * hardware specific functions
  60. */
  61. static int get_pmode(int cpu)
  62. {
  63. int ret;
  64. struct cbe_pmd_regs __iomem *pmd_regs;
  65. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  66. ret = in_be64(&pmd_regs->pmsr) & 0x07;
  67. return ret;
  68. }
  69. static int set_pmode(int cpu, unsigned int pmode)
  70. {
  71. struct cbe_pmd_regs __iomem *pmd_regs;
  72. struct cbe_mic_tm_regs __iomem *mic_tm_regs;
  73. u64 flags;
  74. u64 value;
  75. local_irq_save(flags);
  76. mic_tm_regs = cbe_get_cpu_mic_tm_regs(cpu);
  77. pmd_regs = cbe_get_cpu_pmd_regs(cpu);
  78. pr_debug("pm register is mapped at %p\n", &pmd_regs->pmcr);
  79. pr_debug("mic register is mapped at %p\n", &mic_tm_regs->slow_fast_timer_0);
  80. out_be64(&mic_tm_regs->slow_fast_timer_0, MIC_Slow_Fast_Timer_table[pmode]);
  81. out_be64(&mic_tm_regs->slow_fast_timer_1, MIC_Slow_Fast_Timer_table[pmode]);
  82. out_be64(&mic_tm_regs->slow_next_timer_0, MIC_Slow_Next_Timer_table[pmode]);
  83. out_be64(&mic_tm_regs->slow_next_timer_1, MIC_Slow_Next_Timer_table[pmode]);
  84. value = in_be64(&pmd_regs->pmcr);
  85. /* set bits to zero */
  86. value &= 0xFFFFFFFFFFFFFFF8ull;
  87. /* set bits to next pmode */
  88. value |= pmode;
  89. out_be64(&pmd_regs->pmcr, value);
  90. /* wait until new pmode appears in status register */
  91. value = in_be64(&pmd_regs->pmsr) & 0x07;
  92. while(value != pmode) {
  93. cpu_relax();
  94. value = in_be64(&pmd_regs->pmsr) & 0x07;
  95. }
  96. local_irq_restore(flags);
  97. return 0;
  98. }
  99. /*
  100. * cpufreq functions
  101. */
  102. static int cbe_cpufreq_cpu_init (struct cpufreq_policy *policy)
  103. {
  104. u32 *max_freq;
  105. int i, cur_pmode;
  106. struct device_node *cpu;
  107. cpu = of_get_cpu_node(policy->cpu, NULL);
  108. if(!cpu)
  109. return -ENODEV;
  110. pr_debug("init cpufreq on CPU %d\n", policy->cpu);
  111. max_freq = (u32*) get_property(cpu, "clock-frequency", NULL);
  112. if(!max_freq)
  113. return -EINVAL;
  114. // we need the freq in kHz
  115. *max_freq /= 1000;
  116. pr_debug("max clock-frequency is at %u kHz\n", *max_freq);
  117. pr_debug("initializing frequency table\n");
  118. // initialize frequency table
  119. for (i=0; cbe_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
  120. cbe_freqs[i].frequency = *max_freq / cbe_freqs[i].index;
  121. pr_debug("%d: %d\n", i, cbe_freqs[i].frequency);
  122. }
  123. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  124. /* if DEBUG is enabled set_pmode() measures the correct latency of a transition */
  125. policy->cpuinfo.transition_latency = 25000;
  126. cur_pmode = get_pmode(policy->cpu);
  127. pr_debug("current pmode is at %d\n",cur_pmode);
  128. policy->cur = cbe_freqs[cur_pmode].frequency;
  129. #ifdef CONFIG_SMP
  130. policy->cpus = cpu_sibling_map[policy->cpu];
  131. #endif
  132. cpufreq_frequency_table_get_attr (cbe_freqs, policy->cpu);
  133. /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max are set correctly */
  134. return cpufreq_frequency_table_cpuinfo (policy, cbe_freqs);
  135. }
  136. static int cbe_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  137. {
  138. cpufreq_frequency_table_put_attr(policy->cpu);
  139. return 0;
  140. }
  141. static int cbe_cpufreq_verify(struct cpufreq_policy *policy)
  142. {
  143. return cpufreq_frequency_table_verify(policy, cbe_freqs);
  144. }
  145. static int cbe_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq,
  146. unsigned int relation)
  147. {
  148. int rc;
  149. struct cpufreq_freqs freqs;
  150. int cbe_pmode_new;
  151. cpufreq_frequency_table_target(policy,
  152. cbe_freqs,
  153. target_freq,
  154. relation,
  155. &cbe_pmode_new);
  156. freqs.old = policy->cur;
  157. freqs.new = cbe_freqs[cbe_pmode_new].frequency;
  158. freqs.cpu = policy->cpu;
  159. mutex_lock (&cbe_switch_mutex);
  160. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  161. pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
  162. policy->cpu,
  163. cbe_freqs[cbe_pmode_new].frequency,
  164. cbe_freqs[cbe_pmode_new].index);
  165. rc = set_pmode(policy->cpu, cbe_pmode_new);
  166. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  167. mutex_unlock(&cbe_switch_mutex);
  168. return rc;
  169. }
  170. static struct cpufreq_driver cbe_cpufreq_driver = {
  171. .verify = cbe_cpufreq_verify,
  172. .target = cbe_cpufreq_target,
  173. .init = cbe_cpufreq_cpu_init,
  174. .exit = cbe_cpufreq_cpu_exit,
  175. .name = "cbe-cpufreq",
  176. .owner = THIS_MODULE,
  177. .flags = CPUFREQ_CONST_LOOPS,
  178. };
  179. /*
  180. * module init and destoy
  181. */
  182. static int __init cbe_cpufreq_init(void)
  183. {
  184. return cpufreq_register_driver(&cbe_cpufreq_driver);
  185. }
  186. static void __exit cbe_cpufreq_exit(void)
  187. {
  188. cpufreq_unregister_driver(&cbe_cpufreq_driver);
  189. }
  190. module_init(cbe_cpufreq_init);
  191. module_exit(cbe_cpufreq_exit);
  192. MODULE_LICENSE("GPL");
  193. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");