blackfin-cpufreq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* .driver_data 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. .driver_data = 0,
  25. },
  26. {
  27. .frequency = CPUFREQ_TABLE_END,
  28. .driver_data = 1,
  29. },
  30. {
  31. .frequency = CPUFREQ_TABLE_END,
  32. .driver_data = 2,
  33. },
  34. {
  35. .frequency = CPUFREQ_TABLE_END,
  36. .driver_data = 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 *policy, unsigned int index)
  113. {
  114. #ifndef CONFIG_BF60x
  115. unsigned int plldiv;
  116. #endif
  117. static unsigned long lpj_ref;
  118. static unsigned int lpj_ref_freq;
  119. unsigned int old_freq, new_freq;
  120. int ret = 0;
  121. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  122. cycles_t cycles;
  123. #endif
  124. old_freq = bfin_getfreq_khz(0);
  125. new_freq = bfin_freq_table[index].frequency;
  126. #ifndef CONFIG_BF60x
  127. plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
  128. bfin_write_PLL_DIV(plldiv);
  129. #else
  130. ret = cpu_set_cclk(policy->cpu, new_freq * 1000);
  131. if (ret != 0) {
  132. WARN_ONCE(ret, "cpufreq set freq failed %d\n", ret);
  133. return ret;
  134. }
  135. #endif
  136. on_each_cpu(bfin_adjust_core_timer, &index, 1);
  137. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  138. cycles = get_cycles();
  139. SSYNC();
  140. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  141. __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index);
  142. __bfin_cycles_mod = index;
  143. #endif
  144. if (!lpj_ref_freq) {
  145. lpj_ref = loops_per_jiffy;
  146. lpj_ref_freq = old_freq;
  147. }
  148. if (new_freq != old_freq) {
  149. loops_per_jiffy = cpufreq_scale(lpj_ref,
  150. lpj_ref_freq, new_freq);
  151. }
  152. return ret;
  153. }
  154. static int __bfin_cpu_init(struct cpufreq_policy *policy)
  155. {
  156. unsigned long cclk, sclk;
  157. cclk = get_cclk() / 1000;
  158. sclk = get_sclk() / 1000;
  159. if (policy->cpu == CPUFREQ_CPU)
  160. bfin_init_tables(cclk, sclk);
  161. policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
  162. return cpufreq_table_validate_and_show(policy, bfin_freq_table);
  163. }
  164. static struct cpufreq_driver bfin_driver = {
  165. .verify = cpufreq_generic_frequency_table_verify,
  166. .target_index = bfin_target,
  167. .get = bfin_getfreq_khz,
  168. .init = __bfin_cpu_init,
  169. .name = "bfin cpufreq",
  170. .attr = cpufreq_generic_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);