cpufreq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. normalized to maximum frequncy offset for CYCLES,
  62. used in time-ts cycles clock source, but could be used
  63. somewhere also.
  64. */
  65. unsigned long long __bfin_cycles_off;
  66. unsigned int __bfin_cycles_mod;
  67. /**************************************************************************/
  68. static unsigned int bfin_getfreq_khz(unsigned int cpu)
  69. {
  70. /* The driver only support single cpu */
  71. if (cpu != 0)
  72. return -1;
  73. return get_cclk() / 1000;
  74. }
  75. static int bfin_target(struct cpufreq_policy *policy,
  76. unsigned int target_freq, unsigned int relation)
  77. {
  78. unsigned int index, plldiv, tscale;
  79. unsigned long flags, cclk_hz;
  80. struct cpufreq_freqs freqs;
  81. cycles_t cycles;
  82. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  83. target_freq, relation, &index))
  84. return -EINVAL;
  85. cclk_hz = bfin_freq_table[index].frequency;
  86. freqs.old = bfin_getfreq_khz(0);
  87. freqs.new = cclk_hz;
  88. freqs.cpu = 0;
  89. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  90. cclk_hz, target_freq, freqs.old);
  91. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  92. local_irq_save_hw(flags);
  93. plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
  94. tscale = dpm_state_table[index].tscale;
  95. bfin_write_PLL_DIV(plldiv);
  96. /* we have to adjust the core timer, because it is using cclk */
  97. bfin_write_TSCALE(tscale);
  98. cycles = get_cycles();
  99. SSYNC();
  100. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  101. __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index);
  102. __bfin_cycles_mod = index;
  103. local_irq_restore_hw(flags);
  104. /* TODO: just test case for cycles clock source, remove later */
  105. pr_debug("cpufreq: done\n");
  106. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  107. return 0;
  108. }
  109. static int bfin_verify_speed(struct cpufreq_policy *policy)
  110. {
  111. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  112. }
  113. static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
  114. {
  115. unsigned long cclk, sclk, csel, min_cclk;
  116. int index;
  117. if (policy->cpu != 0)
  118. return -EINVAL;
  119. cclk = get_cclk() / 1000;
  120. sclk = get_sclk() / 1000;
  121. #if ANOMALY_05000273 || (!defined(CONFIG_BF54x) && defined(CONFIG_BFIN_DCACHE))
  122. min_cclk = sclk * 2;
  123. #else
  124. min_cclk = sclk;
  125. #endif
  126. csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
  127. for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
  128. bfin_freq_table[index].frequency = cclk >> index;
  129. dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
  130. dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
  131. pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
  132. bfin_freq_table[index].frequency,
  133. dpm_state_table[index].csel,
  134. dpm_state_table[index].tscale);
  135. }
  136. policy->cpuinfo.transition_latency = (bfin_read_PLL_LOCKCNT() / (sclk / 1000000)) * 1000;
  137. /*Now ,only support one cpu */
  138. policy->cur = cclk;
  139. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  140. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  141. }
  142. static struct freq_attr *bfin_freq_attr[] = {
  143. &cpufreq_freq_attr_scaling_available_freqs,
  144. NULL,
  145. };
  146. static struct cpufreq_driver bfin_driver = {
  147. .verify = bfin_verify_speed,
  148. .target = bfin_target,
  149. .get = bfin_getfreq_khz,
  150. .init = __bfin_cpu_init,
  151. .name = "bfin cpufreq",
  152. .owner = THIS_MODULE,
  153. .attr = bfin_freq_attr,
  154. };
  155. static int __init bfin_cpu_init(void)
  156. {
  157. return cpufreq_register_driver(&bfin_driver);
  158. }
  159. static void __exit bfin_cpu_exit(void)
  160. {
  161. cpufreq_unregister_driver(&bfin_driver);
  162. }
  163. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  164. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  165. MODULE_LICENSE("GPL");
  166. module_init(bfin_cpu_init);
  167. module_exit(bfin_cpu_exit);