cpufreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_BF60x)) \
  60. && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE))
  61. min_cclk = sclk * 2;
  62. #else
  63. min_cclk = sclk;
  64. #endif
  65. #ifndef CONFIG_BF60x
  66. csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
  67. #else
  68. csel = bfin_read32(CGU0_DIV) & 0x1F;
  69. #endif
  70. for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
  71. bfin_freq_table[index].frequency = cclk >> index;
  72. #ifndef CONFIG_BF60x
  73. dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
  74. dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
  75. #else
  76. dpm_state_table[index].csel = csel;
  77. dpm_state_table[index].tscale = TIME_SCALE >> index;
  78. #endif
  79. pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
  80. bfin_freq_table[index].frequency,
  81. dpm_state_table[index].csel,
  82. dpm_state_table[index].tscale);
  83. }
  84. return;
  85. }
  86. static void bfin_adjust_core_timer(void *info)
  87. {
  88. unsigned int tscale;
  89. unsigned int index = *(unsigned int *)info;
  90. /* we have to adjust the core timer, because it is using cclk */
  91. tscale = dpm_state_table[index].tscale;
  92. bfin_write_TSCALE(tscale);
  93. return;
  94. }
  95. static unsigned int bfin_getfreq_khz(unsigned int cpu)
  96. {
  97. /* Both CoreA/B have the same core clock */
  98. return get_cclk() / 1000;
  99. }
  100. #ifdef CONFIG_BF60x
  101. unsigned long cpu_set_cclk(int cpu, unsigned long new)
  102. {
  103. struct clk *clk;
  104. int ret;
  105. clk = clk_get(NULL, "CCLK");
  106. if (IS_ERR(clk))
  107. return -ENODEV;
  108. ret = clk_set_rate(clk, new);
  109. clk_put(clk);
  110. return ret;
  111. }
  112. #endif
  113. static int bfin_target(struct cpufreq_policy *poli,
  114. unsigned int target_freq, unsigned int relation)
  115. {
  116. #ifndef CONFIG_BF60x
  117. unsigned int plldiv;
  118. #endif
  119. unsigned int index, cpu;
  120. unsigned long flags, cclk_hz;
  121. struct cpufreq_freqs freqs;
  122. static unsigned long lpj_ref;
  123. static unsigned int lpj_ref_freq;
  124. int ret = 0;
  125. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  126. cycles_t cycles;
  127. #endif
  128. for_each_online_cpu(cpu) {
  129. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  130. if (!policy)
  131. continue;
  132. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  133. target_freq, relation, &index))
  134. return -EINVAL;
  135. cclk_hz = bfin_freq_table[index].frequency;
  136. freqs.old = bfin_getfreq_khz(0);
  137. freqs.new = cclk_hz;
  138. freqs.cpu = cpu;
  139. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  140. cclk_hz, target_freq, freqs.old);
  141. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  142. if (cpu == CPUFREQ_CPU) {
  143. flags = hard_local_irq_save();
  144. #ifndef CONFIG_BF60x
  145. plldiv = (bfin_read_PLL_DIV() & SSEL) |
  146. dpm_state_table[index].csel;
  147. bfin_write_PLL_DIV(plldiv);
  148. #else
  149. ret = cpu_set_cclk(cpu, freqs.new * 1000);
  150. if (ret != 0) {
  151. WARN_ONCE(ret, "cpufreq set freq failed %d\n", ret);
  152. break;
  153. }
  154. #endif
  155. on_each_cpu(bfin_adjust_core_timer, &index, 1);
  156. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  157. cycles = get_cycles();
  158. SSYNC();
  159. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  160. __bfin_cycles_off +=
  161. (cycles << __bfin_cycles_mod) - (cycles << index);
  162. __bfin_cycles_mod = index;
  163. #endif
  164. if (!lpj_ref_freq) {
  165. lpj_ref = loops_per_jiffy;
  166. lpj_ref_freq = freqs.old;
  167. }
  168. if (freqs.new != freqs.old) {
  169. loops_per_jiffy = cpufreq_scale(lpj_ref,
  170. lpj_ref_freq, freqs.new);
  171. }
  172. hard_local_irq_restore(flags);
  173. }
  174. /* TODO: just test case for cycles clock source, remove later */
  175. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  176. }
  177. pr_debug("cpufreq: done\n");
  178. return ret;
  179. }
  180. static int bfin_verify_speed(struct cpufreq_policy *policy)
  181. {
  182. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  183. }
  184. static int __bfin_cpu_init(struct cpufreq_policy *policy)
  185. {
  186. unsigned long cclk, sclk;
  187. cclk = get_cclk() / 1000;
  188. sclk = get_sclk() / 1000;
  189. if (policy->cpu == CPUFREQ_CPU)
  190. bfin_init_tables(cclk, sclk);
  191. policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
  192. policy->cur = cclk;
  193. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  194. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  195. }
  196. static struct freq_attr *bfin_freq_attr[] = {
  197. &cpufreq_freq_attr_scaling_available_freqs,
  198. NULL,
  199. };
  200. static struct cpufreq_driver bfin_driver = {
  201. .verify = bfin_verify_speed,
  202. .target = bfin_target,
  203. .get = bfin_getfreq_khz,
  204. .init = __bfin_cpu_init,
  205. .name = "bfin cpufreq",
  206. .owner = THIS_MODULE,
  207. .attr = bfin_freq_attr,
  208. };
  209. static int __init bfin_cpu_init(void)
  210. {
  211. return cpufreq_register_driver(&bfin_driver);
  212. }
  213. static void __exit bfin_cpu_exit(void)
  214. {
  215. cpufreq_unregister_driver(&bfin_driver);
  216. }
  217. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  218. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  219. MODULE_LICENSE("GPL");
  220. module_init(bfin_cpu_init);
  221. module_exit(bfin_cpu_exit);