cpufreq.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 < 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. #else
  75. dpm_state_table[index].csel = csel;
  76. #endif
  77. dpm_state_table[index].tscale = (TIME_SCALE >> index) - 1;
  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 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. #ifndef CONFIG_BF60x
  143. plldiv = (bfin_read_PLL_DIV() & SSEL) |
  144. dpm_state_table[index].csel;
  145. bfin_write_PLL_DIV(plldiv);
  146. #else
  147. ret = cpu_set_cclk(cpu, freqs.new * 1000);
  148. if (ret != 0) {
  149. WARN_ONCE(ret, "cpufreq set freq failed %d\n", ret);
  150. break;
  151. }
  152. #endif
  153. on_each_cpu(bfin_adjust_core_timer, &index, 1);
  154. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  155. cycles = get_cycles();
  156. SSYNC();
  157. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  158. __bfin_cycles_off +=
  159. (cycles << __bfin_cycles_mod) - (cycles << index);
  160. __bfin_cycles_mod = index;
  161. #endif
  162. if (!lpj_ref_freq) {
  163. lpj_ref = loops_per_jiffy;
  164. lpj_ref_freq = freqs.old;
  165. }
  166. if (freqs.new != freqs.old) {
  167. loops_per_jiffy = cpufreq_scale(lpj_ref,
  168. lpj_ref_freq, freqs.new);
  169. }
  170. }
  171. /* TODO: just test case for cycles clock source, remove later */
  172. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  173. }
  174. pr_debug("cpufreq: done\n");
  175. return ret;
  176. }
  177. static int bfin_verify_speed(struct cpufreq_policy *policy)
  178. {
  179. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  180. }
  181. static int __bfin_cpu_init(struct cpufreq_policy *policy)
  182. {
  183. unsigned long cclk, sclk;
  184. cclk = get_cclk() / 1000;
  185. sclk = get_sclk() / 1000;
  186. if (policy->cpu == CPUFREQ_CPU)
  187. bfin_init_tables(cclk, sclk);
  188. policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
  189. policy->cur = cclk;
  190. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  191. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  192. }
  193. static struct freq_attr *bfin_freq_attr[] = {
  194. &cpufreq_freq_attr_scaling_available_freqs,
  195. NULL,
  196. };
  197. static struct cpufreq_driver bfin_driver = {
  198. .verify = bfin_verify_speed,
  199. .target = bfin_target,
  200. .get = bfin_getfreq_khz,
  201. .init = __bfin_cpu_init,
  202. .name = "bfin cpufreq",
  203. .owner = THIS_MODULE,
  204. .attr = bfin_freq_attr,
  205. };
  206. static int __init bfin_cpu_init(void)
  207. {
  208. return cpufreq_register_driver(&bfin_driver);
  209. }
  210. static void __exit bfin_cpu_exit(void)
  211. {
  212. cpufreq_unregister_driver(&bfin_driver);
  213. }
  214. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  215. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  216. MODULE_LICENSE("GPL");
  217. module_init(bfin_cpu_init);
  218. module_exit(bfin_cpu_exit);