cpufreq.c 5.4 KB

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