cpufreq.c 4.5 KB

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