cpufreq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Blackfin core clock scaling
  3. *
  4. * Copyright 2008-2009 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 <asm/blackfin.h>
  14. #include <asm/time.h>
  15. #include <asm/dpmc.h>
  16. #define CPUFREQ_CPU 0
  17. /* this is the table of CCLK frequencies, in Hz */
  18. /* .index is the entry in the auxillary 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 frequncy 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. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  95. cycles_t cycles;
  96. #endif
  97. for_each_online_cpu(cpu) {
  98. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  99. if (!policy)
  100. continue;
  101. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  102. target_freq, relation, &index))
  103. return -EINVAL;
  104. cclk_hz = bfin_freq_table[index].frequency;
  105. freqs.old = bfin_getfreq_khz(0);
  106. freqs.new = cclk_hz;
  107. freqs.cpu = cpu;
  108. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  109. cclk_hz, target_freq, freqs.old);
  110. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  111. if (cpu == CPUFREQ_CPU) {
  112. local_irq_save_hw(flags);
  113. plldiv = (bfin_read_PLL_DIV() & SSEL) |
  114. dpm_state_table[index].csel;
  115. bfin_write_PLL_DIV(plldiv);
  116. on_each_cpu(bfin_adjust_core_timer, &index, 1);
  117. #if defined(CONFIG_CYCLES_CLOCKSOURCE)
  118. cycles = get_cycles();
  119. SSYNC();
  120. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  121. __bfin_cycles_off +=
  122. (cycles << __bfin_cycles_mod) - (cycles << index);
  123. __bfin_cycles_mod = index;
  124. #endif
  125. local_irq_restore_hw(flags);
  126. }
  127. /* TODO: just test case for cycles clock source, remove later */
  128. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  129. }
  130. pr_debug("cpufreq: done\n");
  131. return 0;
  132. }
  133. static int bfin_verify_speed(struct cpufreq_policy *policy)
  134. {
  135. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  136. }
  137. static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
  138. {
  139. unsigned long cclk, sclk;
  140. cclk = get_cclk() / 1000;
  141. sclk = get_sclk() / 1000;
  142. if (policy->cpu == CPUFREQ_CPU)
  143. bfin_init_tables(cclk, sclk);
  144. policy->cpuinfo.transition_latency = 50000; /* 50us assumed */
  145. policy->cur = cclk;
  146. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  147. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  148. }
  149. static struct freq_attr *bfin_freq_attr[] = {
  150. &cpufreq_freq_attr_scaling_available_freqs,
  151. NULL,
  152. };
  153. static struct cpufreq_driver bfin_driver = {
  154. .verify = bfin_verify_speed,
  155. .target = bfin_target,
  156. .get = bfin_getfreq_khz,
  157. .init = __bfin_cpu_init,
  158. .name = "bfin cpufreq",
  159. .owner = THIS_MODULE,
  160. .attr = bfin_freq_attr,
  161. };
  162. static int __init bfin_cpu_init(void)
  163. {
  164. return cpufreq_register_driver(&bfin_driver);
  165. }
  166. static void __exit bfin_cpu_exit(void)
  167. {
  168. cpufreq_unregister_driver(&bfin_driver);
  169. }
  170. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  171. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  172. MODULE_LICENSE("GPL");
  173. module_init(bfin_cpu_init);
  174. module_exit(bfin_cpu_exit);