cpufreq.c 6.1 KB

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