loongson2_cpufreq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Cpufreq driver for the loongson-2 processors
  3. *
  4. * The 2E revision of loongson processor not support this feature.
  5. *
  6. * Copyright (C) 2006 - 2008 Lemote Inc. & Institute of Computing Technology
  7. * Author: Yanhua, yanh@lemote.com
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/cpufreq.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/idle.h>
  20. #include <asm/mach-loongson2ef/loongson.h>
  21. static uint nowait;
  22. static void (*saved_cpu_wait) (void);
  23. static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
  24. unsigned long val, void *data);
  25. static struct notifier_block loongson2_cpufreq_notifier_block = {
  26. .notifier_call = loongson2_cpu_freq_notifier
  27. };
  28. static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
  29. unsigned long val, void *data)
  30. {
  31. if (val == CPUFREQ_POSTCHANGE)
  32. current_cpu_data.udelay_val = loops_per_jiffy;
  33. return 0;
  34. }
  35. /*
  36. * Here we notify other drivers of the proposed change and the final change.
  37. */
  38. static int loongson2_cpufreq_target(struct cpufreq_policy *policy,
  39. unsigned int index)
  40. {
  41. unsigned int freq;
  42. freq =
  43. ((cpu_clock_freq / 1000) *
  44. loongson2_clockmod_table[index].driver_data) / 8;
  45. /* setting the cpu frequency */
  46. loongson2_cpu_set_rate(freq);
  47. return 0;
  48. }
  49. static int loongson2_cpufreq_cpu_init(struct cpufreq_policy *policy)
  50. {
  51. int i;
  52. unsigned long rate;
  53. int ret;
  54. rate = cpu_clock_freq / 1000;
  55. if (!rate)
  56. return -EINVAL;
  57. /* clock table init */
  58. for (i = 2;
  59. (loongson2_clockmod_table[i].frequency != CPUFREQ_TABLE_END);
  60. i++)
  61. loongson2_clockmod_table[i].frequency = (rate * i) / 8;
  62. ret = loongson2_cpu_set_rate(rate);
  63. if (ret)
  64. return ret;
  65. cpufreq_generic_init(policy, &loongson2_clockmod_table[0], 0);
  66. return 0;
  67. }
  68. static struct cpufreq_driver loongson2_cpufreq_driver = {
  69. .name = "loongson2",
  70. .init = loongson2_cpufreq_cpu_init,
  71. .verify = cpufreq_generic_frequency_table_verify,
  72. .target_index = loongson2_cpufreq_target,
  73. .get = cpufreq_generic_get,
  74. .attr = cpufreq_generic_attr,
  75. };
  76. static const struct platform_device_id platform_device_ids[] = {
  77. {
  78. .name = "loongson2_cpufreq",
  79. },
  80. {}
  81. };
  82. MODULE_DEVICE_TABLE(platform, platform_device_ids);
  83. static struct platform_driver platform_driver = {
  84. .driver = {
  85. .name = "loongson2_cpufreq",
  86. },
  87. .id_table = platform_device_ids,
  88. };
  89. /*
  90. * This is the simple version of Loongson-2 wait, Maybe we need do this in
  91. * interrupt disabled context.
  92. */
  93. static DEFINE_SPINLOCK(loongson2_wait_lock);
  94. static void loongson2_cpu_wait(void)
  95. {
  96. unsigned long flags;
  97. u32 cpu_freq;
  98. spin_lock_irqsave(&loongson2_wait_lock, flags);
  99. cpu_freq = readl(LOONGSON_CHIPCFG);
  100. /* Put CPU into wait mode */
  101. writel(readl(LOONGSON_CHIPCFG) & ~0x7, LOONGSON_CHIPCFG);
  102. /* Restore CPU state */
  103. writel(cpu_freq, LOONGSON_CHIPCFG);
  104. spin_unlock_irqrestore(&loongson2_wait_lock, flags);
  105. local_irq_enable();
  106. }
  107. static int __init cpufreq_init(void)
  108. {
  109. int ret;
  110. /* Register platform stuff */
  111. ret = platform_driver_register(&platform_driver);
  112. if (ret)
  113. return ret;
  114. pr_info("Loongson-2F CPU frequency driver\n");
  115. cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
  116. CPUFREQ_TRANSITION_NOTIFIER);
  117. ret = cpufreq_register_driver(&loongson2_cpufreq_driver);
  118. if (ret) {
  119. platform_driver_unregister(&platform_driver);
  120. } else if (!nowait) {
  121. saved_cpu_wait = cpu_wait;
  122. cpu_wait = loongson2_cpu_wait;
  123. }
  124. return ret;
  125. }
  126. static void __exit cpufreq_exit(void)
  127. {
  128. if (!nowait && saved_cpu_wait)
  129. cpu_wait = saved_cpu_wait;
  130. cpufreq_unregister_driver(&loongson2_cpufreq_driver);
  131. cpufreq_unregister_notifier(&loongson2_cpufreq_notifier_block,
  132. CPUFREQ_TRANSITION_NOTIFIER);
  133. platform_driver_unregister(&platform_driver);
  134. }
  135. module_init(cpufreq_init);
  136. module_exit(cpufreq_exit);
  137. module_param(nowait, uint, 0644);
  138. MODULE_PARM_DESC(nowait, "Disable Loongson-2F specific wait");
  139. MODULE_AUTHOR("Yanhua <yanh@lemote.com>");
  140. MODULE_DESCRIPTION("cpufreq driver for Loongson2F");
  141. MODULE_LICENSE("GPL");