cpufreq.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/cpufreq.h>
  4. #include <hwregs/reg_map.h>
  5. #include <hwregs/reg_rdwr.h>
  6. #include <hwregs/clkgen_defs.h>
  7. #include <hwregs/ddr2_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_clkgen_rw_clk_ctrl clk_ctrl;
  22. clk_ctrl = REG_RD(clkgen, regi_clkgen, 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 = 0;
  28. struct cpufreq_freqs freqs;
  29. reg_clkgen_rw_clk_ctrl clk_ctrl;
  30. clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  31. #ifdef CONFIG_SMP
  32. for_each_present_cpu(i)
  33. #endif
  34. {
  35. freqs.old = cris_freq_get_cpu_frequency(i);
  36. freqs.new = cris_freq_table[state].frequency;
  37. freqs.cpu = i;
  38. }
  39. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  40. local_irq_disable();
  41. /* Even though we may be SMP they will share the same clock
  42. * so all settings are made on CPU0. */
  43. if (cris_freq_table[state].frequency == 200000)
  44. clk_ctrl.pll = 1;
  45. else
  46. clk_ctrl.pll = 0;
  47. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
  48. local_irq_enable();
  49. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  50. };
  51. static int cris_freq_verify(struct cpufreq_policy *policy)
  52. {
  53. return cpufreq_frequency_table_verify(policy, &cris_freq_table[0]);
  54. }
  55. static int cris_freq_target(struct cpufreq_policy *policy,
  56. unsigned int target_freq,
  57. unsigned int relation)
  58. {
  59. unsigned int newstate = 0;
  60. if (cpufreq_frequency_table_target(policy, cris_freq_table,
  61. target_freq, relation, &newstate))
  62. return -EINVAL;
  63. cris_freq_set_cpu_state(newstate);
  64. return 0;
  65. }
  66. static int cris_freq_cpu_init(struct cpufreq_policy *policy)
  67. {
  68. int result;
  69. /* cpuinfo and default policy values */
  70. policy->cpuinfo.transition_latency = 1000000; /* 1ms */
  71. policy->cur = cris_freq_get_cpu_frequency(0);
  72. result = cpufreq_frequency_table_cpuinfo(policy, cris_freq_table);
  73. if (result)
  74. return (result);
  75. cpufreq_frequency_table_get_attr(cris_freq_table, policy->cpu);
  76. return 0;
  77. }
  78. static int cris_freq_cpu_exit(struct cpufreq_policy *policy)
  79. {
  80. cpufreq_frequency_table_put_attr(policy->cpu);
  81. return 0;
  82. }
  83. static struct freq_attr *cris_freq_attr[] = {
  84. &cpufreq_freq_attr_scaling_available_freqs,
  85. NULL,
  86. };
  87. static struct cpufreq_driver cris_freq_driver = {
  88. .get = cris_freq_get_cpu_frequency,
  89. .verify = cris_freq_verify,
  90. .target = cris_freq_target,
  91. .init = cris_freq_cpu_init,
  92. .exit = cris_freq_cpu_exit,
  93. .name = "cris_freq",
  94. .owner = THIS_MODULE,
  95. .attr = cris_freq_attr,
  96. };
  97. static int __init cris_freq_init(void)
  98. {
  99. int ret;
  100. ret = cpufreq_register_driver(&cris_freq_driver);
  101. cpufreq_register_notifier(&cris_sdram_freq_notifier_block,
  102. CPUFREQ_TRANSITION_NOTIFIER);
  103. return ret;
  104. }
  105. static int
  106. cris_sdram_freq_notifier(struct notifier_block *nb, unsigned long val,
  107. void *data)
  108. {
  109. int i;
  110. struct cpufreq_freqs *freqs = data;
  111. if (val == CPUFREQ_PRECHANGE) {
  112. reg_ddr2_rw_cfg cfg =
  113. REG_RD(ddr2, regi_ddr2_ctrl, rw_cfg);
  114. cfg.ref_interval = (freqs->new == 200000 ? 1560 : 46);
  115. if (freqs->new == 200000)
  116. for (i = 0; i < 50000; i++);
  117. REG_WR(bif_core, regi_bif_core, rw_sdram_timing, timing);
  118. }
  119. return 0;
  120. }
  121. module_init(cris_freq_init);