cpufreq.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Blackfin core clock scaling
  3. *
  4. * Copyright 2008-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/init.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/fs.h>
  14. #include <linux/delay.h>
  15. #include <asm/blackfin.h>
  16. #include <asm/time.h>
  17. #include <asm/dpmc.h>
  18. /* this is the table of CCLK frequencies, in Hz */
  19. /* .index is the entry in the auxiliary dpm_state_table[] */
  20. static struct cpufreq_frequency_table bfin_freq_table[] = {
  21. {
  22. .frequency = CPUFREQ_TABLE_END,
  23. .index = 0,
  24. },
  25. {
  26. .frequency = CPUFREQ_TABLE_END,
  27. .index = 1,
  28. },
  29. {
  30. .frequency = CPUFREQ_TABLE_END,
  31. .index = 2,
  32. },
  33. {
  34. .frequency = CPUFREQ_TABLE_END,
  35. .index = 0,
  36. },
  37. };
  38. static struct bfin_dpm_state {
  39. unsigned int csel; /* system clock divider */
  40. unsigned int tscale; /* change the divider on the core timer interrupt */
  41. } dpm_state_table[3];
  42. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  43. /*
  44. * normalized to maximum frequency offset for CYCLES,
  45. * used in time-ts cycles clock source, but could be used
  46. * somewhere also.
  47. */
  48. unsigned long long __bfin_cycles_off;
  49. unsigned int __bfin_cycles_mod;
  50. #endif
  51. /**************************************************************************/
  52. static void __init bfin_init_tables(unsigned long cclk, unsigned long sclk)
  53. {
  54. unsigned long csel, min_cclk;
  55. int index;
  56. /* Anomaly 273 seems to still exist on non-BF54x w/dcache turned on */
  57. #if ANOMALY_05000273 || ANOMALY_05000274 || \
  58. (!defined(CONFIG_BF54x) && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE))
  59. min_cclk = sclk * 2;
  60. #else
  61. min_cclk = sclk;
  62. #endif
  63. csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
  64. for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
  65. bfin_freq_table[index].frequency = cclk >> index;
  66. dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
  67. dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
  68. pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
  69. bfin_freq_table[index].frequency,
  70. dpm_state_table[index].csel,
  71. dpm_state_table[index].tscale);
  72. }
  73. return;
  74. }
  75. static void bfin_adjust_core_timer(void *info)
  76. {
  77. unsigned int tscale;
  78. unsigned int index = *(unsigned int *)info;
  79. /* we have to adjust the core timer, because it is using cclk */
  80. tscale = dpm_state_table[index].tscale;
  81. bfin_write_TSCALE(tscale);
  82. return;
  83. }
  84. static unsigned int bfin_getfreq_khz(unsigned int cpu)
  85. {
  86. /* Both CoreA/B have the same core clock */
  87. return get_cclk() / 1000;
  88. }
  89. static int bfin_target(struct cpufreq_policy *poli,
  90. unsigned int target_freq, unsigned int relation)
  91. {
  92. unsigned int index, plldiv, cpu;
  93. unsigned long flags, cclk_hz;
  94. struct cpufreq_freqs freqs;
  95. static unsigned long lpj_ref;
  96. static unsigned int lpj_ref_freq;
  97. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  98. cycles_t cycles;
  99. #endif
  100. for_each_online_cpu(cpu) {
  101. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  102. if (!policy)
  103. continue;
  104. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  105. target_freq, relation, &index))
  106. return -EINVAL;
  107. cclk_hz = bfin_freq_table[index].frequency;
  108. freqs.old = bfin_getfreq_khz(0);
  109. freqs.new = cclk_hz;
  110. freqs.cpu = cpu;
  111. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  112. cclk_hz, target_freq, freqs.old);
  113. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  114. if (cpu == CPUFREQ_CPU) {
  115. flags = hard_local_irq_save();
  116. plldiv = (bfin_read_PLL_DIV() & SSEL) |
  117. dpm_state_table[index].csel;
  118. bfin_write_PLL_DIV(plldiv);
  119. on_each_cpu(bfin_adjust_core_timer, &index, 1);
  120. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  121. cycles = get_cycles();
  122. SSYNC();
  123. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  124. __bfin_cycles_off +=
  125. (cycles << __bfin_cycles_mod) - (cycles << index);
  126. __bfin_cycles_mod = index;
  127. #endif
  128. if (!lpj_ref_freq) {
  129. lpj_ref = loops_per_jiffy;
  130. lpj_ref_freq = freqs.old;
  131. }
  132. if (freqs.new != freqs.old) {
  133. loops_per_jiffy = cpufreq_scale(lpj_ref,
  134. lpj_ref_freq, freqs.new);
  135. }
  136. hard_local_irq_restore(flags);
  137. }
  138. /* TODO: just test case for cycles clock source, remove later */
  139. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  140. }
  141. pr_debug("cpufreq: done\n");
  142. return 0;
  143. }
  144. static int bfin_verify_speed(struct cpufreq_policy *policy)
  145. {
  146. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  147. }
  148. static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
  149. {
  150. unsigned long cclk, sclk;
  151. cclk = get_cclk() / 1000;
  152. sclk = get_sclk() / 1000;
  153. if (policy->cpu == CPUFREQ_CPU)
  154. bfin_init_tables(cclk, sclk);
  155. policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
  156. policy->cur = cclk;
  157. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  158. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  159. }
  160. static struct freq_attr *bfin_freq_attr[] = {
  161. &cpufreq_freq_attr_scaling_available_freqs,
  162. NULL,
  163. };
  164. static struct cpufreq_driver bfin_driver = {
  165. .verify = bfin_verify_speed,
  166. .target = bfin_target,
  167. .get = bfin_getfreq_khz,
  168. .init = __bfin_cpu_init,
  169. .name = "bfin cpufreq",
  170. .owner = THIS_MODULE,
  171. .attr = bfin_freq_attr,
  172. };
  173. static int __init bfin_cpu_init(void)
  174. {
  175. return cpufreq_register_driver(&bfin_driver);
  176. }
  177. static void __exit bfin_cpu_exit(void)
  178. {
  179. cpufreq_unregister_driver(&bfin_driver);
  180. }
  181. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  182. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  183. MODULE_LICENSE("GPL");
  184. module_init(bfin_cpu_init);
  185. module_exit(bfin_cpu_exit);