cpufreq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * File: arch/blackfin/mach-common/cpufreq.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: Blackfin core clock scaling
  8. *
  9. * Modified:
  10. * Copyright 2004-2008 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/init.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/fs.h>
  34. #include <asm/blackfin.h>
  35. #include <asm/time.h>
  36. /* this is the table of CCLK frequencies, in Hz */
  37. /* .index is the entry in the auxillary dpm_state_table[] */
  38. static struct cpufreq_frequency_table bfin_freq_table[] = {
  39. {
  40. .frequency = CPUFREQ_TABLE_END,
  41. .index = 0,
  42. },
  43. {
  44. .frequency = CPUFREQ_TABLE_END,
  45. .index = 1,
  46. },
  47. {
  48. .frequency = CPUFREQ_TABLE_END,
  49. .index = 2,
  50. },
  51. {
  52. .frequency = CPUFREQ_TABLE_END,
  53. .index = 0,
  54. },
  55. };
  56. static struct bfin_dpm_state {
  57. unsigned int csel; /* system clock divider */
  58. unsigned int tscale; /* change the divider on the core timer interrupt */
  59. } dpm_state_table[3];
  60. /**************************************************************************/
  61. static unsigned int bfin_getfreq(unsigned int cpu)
  62. {
  63. /* The driver only support single cpu */
  64. if (cpu != 0)
  65. return -1;
  66. return get_cclk();
  67. }
  68. static int bfin_target(struct cpufreq_policy *policy,
  69. unsigned int target_freq, unsigned int relation)
  70. {
  71. unsigned int index, plldiv, tscale;
  72. unsigned long flags, cclk_hz;
  73. struct cpufreq_freqs freqs;
  74. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  75. target_freq, relation, &index))
  76. return -EINVAL;
  77. cclk_hz = bfin_freq_table[index].frequency;
  78. freqs.old = bfin_getfreq(0);
  79. freqs.new = cclk_hz;
  80. freqs.cpu = 0;
  81. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  82. cclk_hz, target_freq, freqs.old);
  83. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  84. local_irq_save(flags);
  85. plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
  86. tscale = dpm_state_table[index].tscale;
  87. bfin_write_PLL_DIV(plldiv);
  88. /* we have to adjust the core timer, because it is using cclk */
  89. bfin_write_TSCALE(tscale);
  90. SSYNC();
  91. local_irq_restore(flags);
  92. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  93. return 0;
  94. }
  95. static int bfin_verify_speed(struct cpufreq_policy *policy)
  96. {
  97. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  98. }
  99. static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
  100. {
  101. unsigned long cclk, sclk, csel, min_cclk;
  102. int index;
  103. #ifdef CONFIG_CYCLES_CLOCKSOURCE
  104. /*
  105. * Clocksource CYCLES is still CONTINUOUS but not longer with a constant tick rate in case we enable
  106. * CPU frequency scaling, since CYCLES runs off Core Clock.
  107. */
  108. printk(KERN_WARNING "CPU frequency scaling not supported: Clocksource not suitable\n"
  109. return -ENODEV;
  110. #endif
  111. if (policy->cpu != 0)
  112. return -EINVAL;
  113. cclk = get_cclk();
  114. sclk = get_sclk();
  115. #if ANOMALY_05000273 || (!defined(CONFIG_BF54x) && defined(CONFIG_BFIN_DCACHE))
  116. min_cclk = sclk * 2;
  117. #else
  118. min_cclk = sclk;
  119. #endif
  120. csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
  121. for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
  122. bfin_freq_table[index].frequency = cclk >> index;
  123. dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
  124. dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
  125. pr_debug("cpufreq: freq:%d csel:%d tscale:%d\n",
  126. bfin_freq_table[index].frequency,
  127. dpm_state_table[index].csel,
  128. dpm_state_table[index].tscale);
  129. }
  130. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  131. policy->cpuinfo.transition_latency = (bfin_read_PLL_LOCKCNT() / (sclk / 1000000)) * 1000;
  132. /*Now ,only support one cpu */
  133. policy->cur = cclk;
  134. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  135. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  136. }
  137. static struct freq_attr *bfin_freq_attr[] = {
  138. &cpufreq_freq_attr_scaling_available_freqs,
  139. NULL,
  140. };
  141. static struct cpufreq_driver bfin_driver = {
  142. .verify = bfin_verify_speed,
  143. .target = bfin_target,
  144. .get = bfin_getfreq,
  145. .init = __bfin_cpu_init,
  146. .name = "bfin cpufreq",
  147. .owner = THIS_MODULE,
  148. .attr = bfin_freq_attr,
  149. };
  150. static int __init bfin_cpu_init(void)
  151. {
  152. return cpufreq_register_driver(&bfin_driver);
  153. }
  154. static void __exit bfin_cpu_exit(void)
  155. {
  156. cpufreq_unregister_driver(&bfin_driver);
  157. }
  158. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  159. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  160. MODULE_LICENSE("GPL");
  161. module_init(bfin_cpu_init);
  162. module_exit(bfin_cpu_exit);