cpufreq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /* this is the table of CCLK frequencies, in Hz */
  16. /* .index is the entry in the auxillary dpm_state_table[] */
  17. static struct cpufreq_frequency_table bfin_freq_table[] = {
  18. {
  19. .frequency = CPUFREQ_TABLE_END,
  20. .index = 0,
  21. },
  22. {
  23. .frequency = CPUFREQ_TABLE_END,
  24. .index = 1,
  25. },
  26. {
  27. .frequency = CPUFREQ_TABLE_END,
  28. .index = 2,
  29. },
  30. {
  31. .frequency = CPUFREQ_TABLE_END,
  32. .index = 0,
  33. },
  34. };
  35. static struct bfin_dpm_state {
  36. unsigned int csel; /* system clock divider */
  37. unsigned int tscale; /* change the divider on the core timer interrupt */
  38. } dpm_state_table[3];
  39. /*
  40. normalized to maximum frequncy offset for CYCLES,
  41. used in time-ts cycles clock source, but could be used
  42. somewhere also.
  43. */
  44. unsigned long long __bfin_cycles_off;
  45. unsigned int __bfin_cycles_mod;
  46. /**************************************************************************/
  47. static unsigned int bfin_getfreq_khz(unsigned int cpu)
  48. {
  49. /* The driver only support single cpu */
  50. if (cpu != 0)
  51. return -1;
  52. return get_cclk() / 1000;
  53. }
  54. static int bfin_target(struct cpufreq_policy *policy,
  55. unsigned int target_freq, unsigned int relation)
  56. {
  57. unsigned int index, plldiv, tscale;
  58. unsigned long flags, cclk_hz;
  59. struct cpufreq_freqs freqs;
  60. cycles_t cycles;
  61. if (cpufreq_frequency_table_target(policy, bfin_freq_table,
  62. target_freq, relation, &index))
  63. return -EINVAL;
  64. cclk_hz = bfin_freq_table[index].frequency;
  65. freqs.old = bfin_getfreq_khz(0);
  66. freqs.new = cclk_hz;
  67. freqs.cpu = 0;
  68. pr_debug("cpufreq: changing cclk to %lu; target = %u, oldfreq = %u\n",
  69. cclk_hz, target_freq, freqs.old);
  70. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  71. local_irq_save_hw(flags);
  72. plldiv = (bfin_read_PLL_DIV() & SSEL) | dpm_state_table[index].csel;
  73. tscale = dpm_state_table[index].tscale;
  74. bfin_write_PLL_DIV(plldiv);
  75. /* we have to adjust the core timer, because it is using cclk */
  76. bfin_write_TSCALE(tscale);
  77. cycles = get_cycles();
  78. SSYNC();
  79. cycles += 10; /* ~10 cycles we lose after get_cycles() */
  80. __bfin_cycles_off += (cycles << __bfin_cycles_mod) - (cycles << index);
  81. __bfin_cycles_mod = index;
  82. local_irq_restore_hw(flags);
  83. /* TODO: just test case for cycles clock source, remove later */
  84. pr_debug("cpufreq: done\n");
  85. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  86. return 0;
  87. }
  88. static int bfin_verify_speed(struct cpufreq_policy *policy)
  89. {
  90. return cpufreq_frequency_table_verify(policy, bfin_freq_table);
  91. }
  92. static int __init __bfin_cpu_init(struct cpufreq_policy *policy)
  93. {
  94. unsigned long cclk, sclk, csel, min_cclk;
  95. int index;
  96. if (policy->cpu != 0)
  97. return -EINVAL;
  98. cclk = get_cclk() / 1000;
  99. sclk = get_sclk() / 1000;
  100. #if ANOMALY_05000273 || ANOMALY_05000274 || \
  101. (!defined(CONFIG_BF54x) && defined(CONFIG_BFIN_EXTMEM_DCACHEABLE))
  102. min_cclk = sclk * 2;
  103. #else
  104. min_cclk = sclk;
  105. #endif
  106. csel = ((bfin_read_PLL_DIV() & CSEL) >> 4);
  107. for (index = 0; (cclk >> index) >= min_cclk && csel <= 3; index++, csel++) {
  108. bfin_freq_table[index].frequency = cclk >> index;
  109. dpm_state_table[index].csel = csel << 4; /* Shift now into PLL_DIV bitpos */
  110. dpm_state_table[index].tscale = (TIME_SCALE / (1 << csel)) - 1;
  111. pr_debug("cpufreq: freq:%d csel:0x%x tscale:%d\n",
  112. bfin_freq_table[index].frequency,
  113. dpm_state_table[index].csel,
  114. dpm_state_table[index].tscale);
  115. }
  116. policy->cpuinfo.transition_latency = (bfin_read_PLL_LOCKCNT() / (sclk / 1000000)) * 1000;
  117. /*Now ,only support one cpu */
  118. policy->cur = cclk;
  119. cpufreq_frequency_table_get_attr(bfin_freq_table, policy->cpu);
  120. return cpufreq_frequency_table_cpuinfo(policy, bfin_freq_table);
  121. }
  122. static struct freq_attr *bfin_freq_attr[] = {
  123. &cpufreq_freq_attr_scaling_available_freqs,
  124. NULL,
  125. };
  126. static struct cpufreq_driver bfin_driver = {
  127. .verify = bfin_verify_speed,
  128. .target = bfin_target,
  129. .get = bfin_getfreq_khz,
  130. .init = __bfin_cpu_init,
  131. .name = "bfin cpufreq",
  132. .owner = THIS_MODULE,
  133. .attr = bfin_freq_attr,
  134. };
  135. static int __init bfin_cpu_init(void)
  136. {
  137. return cpufreq_register_driver(&bfin_driver);
  138. }
  139. static void __exit bfin_cpu_exit(void)
  140. {
  141. cpufreq_unregister_driver(&bfin_driver);
  142. }
  143. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  144. MODULE_DESCRIPTION("cpufreq driver for Blackfin");
  145. MODULE_LICENSE("GPL");
  146. module_init(bfin_cpu_init);
  147. module_exit(bfin_cpu_exit);