cpufreq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/cpufreq.h>
  4. #include <hwregs/reg_map.h>
  5. #include <arch/hwregs/reg_rdwr.h>
  6. #include <arch/hwregs/config_defs.h>
  7. #include <arch/hwregs/bif_core_defs.h>
  8. static int
  9. cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
  10. void *data);
  11. static struct notifier_block cris_sdram_freq_notifier_block = {
  12. .notifier_call = cris_sdram_freq_notifier
  13. };
  14. static struct cpufreq_frequency_table cris_freq_table[] = {
  15. {0x01, 6000},
  16. {0x02, 200000},
  17. {0, CPUFREQ_TABLE_END},
  18. };
  19. static unsigned int cris_freq_get_cpu_frequency(unsigned int cpu)
  20. {
  21. reg_config_rw_clk_ctrl clk_ctrl;
  22. clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
  23. return clk_ctrl.pll ? 200000 : 6000;
  24. }
  25. static void cris_freq_set_cpu_state(unsigned int state)
  26. {
  27. int i;
  28. struct cpufreq_freqs freqs;
  29. reg_config_rw_clk_ctrl clk_ctrl;
  30. clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
  31. for_each_possible_cpu(i) {
  32. freqs.old = cris_freq_get_cpu_frequency(i);
  33. freqs.new = cris_freq_table[state].frequency;
  34. freqs.cpu = i;
  35. }
  36. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  37. local_irq_disable();
  38. /* Even though we may be SMP they will share the same clock
  39. * so all settings are made on CPU0. */
  40. if (cris_freq_table[state].frequency == 200000)
  41. clk_ctrl.pll = 1;
  42. else
  43. clk_ctrl.pll = 0;
  44. REG_WR(config, regi_config, rw_clk_ctrl, clk_ctrl);
  45. local_irq_enable();
  46. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  47. };
  48. static int cris_freq_verify(struct cpufreq_policy *policy)
  49. {
  50. return cpufreq_frequency_table_verify(policy, &cris_freq_table[0]);
  51. }
  52. static int cris_freq_target(struct cpufreq_policy *policy,
  53. unsigned int target_freq, unsigned int relation)
  54. {
  55. unsigned int newstate = 0;
  56. if (cpufreq_frequency_table_target
  57. (policy, cris_freq_table, target_freq, relation, &newstate))
  58. return -EINVAL;
  59. cris_freq_set_cpu_state(newstate);
  60. return 0;
  61. }
  62. static int cris_freq_cpu_init(struct cpufreq_policy *policy)
  63. {
  64. int result;
  65. /* cpuinfo and default policy values */
  66. policy->cpuinfo.transition_latency = 1000000; /* 1ms */
  67. policy->cur = cris_freq_get_cpu_frequency(0);
  68. result = cpufreq_frequency_table_cpuinfo(policy, cris_freq_table);
  69. if (result)
  70. return (result);
  71. cpufreq_frequency_table_get_attr(cris_freq_table, policy->cpu);
  72. return 0;
  73. }
  74. static int cris_freq_cpu_exit(struct cpufreq_policy *policy)
  75. {
  76. cpufreq_frequency_table_put_attr(policy->cpu);
  77. return 0;
  78. }
  79. static struct freq_attr *cris_freq_attr[] = {
  80. &cpufreq_freq_attr_scaling_available_freqs,
  81. NULL,
  82. };
  83. static struct cpufreq_driver cris_freq_driver = {
  84. .get = cris_freq_get_cpu_frequency,
  85. .verify = cris_freq_verify,
  86. .target = cris_freq_target,
  87. .init = cris_freq_cpu_init,
  88. .exit = cris_freq_cpu_exit,
  89. .name = "cris_freq",
  90. .owner = THIS_MODULE,
  91. .attr = cris_freq_attr,
  92. };
  93. static int __init cris_freq_init(void)
  94. {
  95. int ret;
  96. ret = cpufreq_register_driver(&cris_freq_driver);
  97. cpufreq_register_notifier(&cris_sdram_freq_notifier_block,
  98. CPUFREQ_TRANSITION_NOTIFIER);
  99. return ret;
  100. }
  101. static int
  102. cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
  103. void *data)
  104. {
  105. int i;
  106. struct cpufreq_freqs *freqs = data;
  107. if (val == CPUFREQ_PRECHANGE) {
  108. reg_bif_core_rw_sdram_timing timing =
  109. REG_RD(bif_core, regi_bif_core, rw_sdram_timing);
  110. timing.cpd = (freqs->new == 200000 ? 0 : 1);
  111. if (freqs->new == 200000)
  112. for (i = 0; i < 50000; i++) ;
  113. REG_WR(bif_core, regi_bif_core, rw_sdram_timing, timing);
  114. }
  115. return 0;
  116. }
  117. module_init(cris_freq_init);