loongson2_cpufreq.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, [email protected]
  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 int loongson2_cpufreq_exit(struct cpufreq_policy *policy)
  69. {
  70. return 0;
  71. }
  72. static struct cpufreq_driver loongson2_cpufreq_driver = {
  73. .name = "loongson2",
  74. .init = loongson2_cpufreq_cpu_init,
  75. .verify = cpufreq_generic_frequency_table_verify,
  76. .target_index = loongson2_cpufreq_target,
  77. .get = cpufreq_generic_get,
  78. .exit = loongson2_cpufreq_exit,
  79. .attr = cpufreq_generic_attr,
  80. };
  81. static const struct platform_device_id platform_device_ids[] = {
  82. {
  83. .name = "loongson2_cpufreq",
  84. },
  85. {}
  86. };
  87. MODULE_DEVICE_TABLE(platform, platform_device_ids);
  88. static struct platform_driver platform_driver = {
  89. .driver = {
  90. .name = "loongson2_cpufreq",
  91. },
  92. .id_table = platform_device_ids,
  93. };
  94. /*
  95. * This is the simple version of Loongson-2 wait, Maybe we need do this in
  96. * interrupt disabled context.
  97. */
  98. static DEFINE_SPINLOCK(loongson2_wait_lock);
  99. static void loongson2_cpu_wait(void)
  100. {
  101. unsigned long flags;
  102. u32 cpu_freq;
  103. spin_lock_irqsave(&loongson2_wait_lock, flags);
  104. cpu_freq = readl(LOONGSON_CHIPCFG);
  105. /* Put CPU into wait mode */
  106. writel(readl(LOONGSON_CHIPCFG) & ~0x7, LOONGSON_CHIPCFG);
  107. /* Restore CPU state */
  108. writel(cpu_freq, LOONGSON_CHIPCFG);
  109. spin_unlock_irqrestore(&loongson2_wait_lock, flags);
  110. local_irq_enable();
  111. }
  112. static int __init cpufreq_init(void)
  113. {
  114. int ret;
  115. /* Register platform stuff */
  116. ret = platform_driver_register(&platform_driver);
  117. if (ret)
  118. return ret;
  119. pr_info("Loongson-2F CPU frequency driver\n");
  120. cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
  121. CPUFREQ_TRANSITION_NOTIFIER);
  122. ret = cpufreq_register_driver(&loongson2_cpufreq_driver);
  123. if (!ret && !nowait) {
  124. saved_cpu_wait = cpu_wait;
  125. cpu_wait = loongson2_cpu_wait;
  126. }
  127. return ret;
  128. }
  129. static void __exit cpufreq_exit(void)
  130. {
  131. if (!nowait && saved_cpu_wait)
  132. cpu_wait = saved_cpu_wait;
  133. cpufreq_unregister_driver(&loongson2_cpufreq_driver);
  134. cpufreq_unregister_notifier(&loongson2_cpufreq_notifier_block,
  135. CPUFREQ_TRANSITION_NOTIFIER);
  136. platform_driver_unregister(&platform_driver);
  137. }
  138. module_init(cpufreq_init);
  139. module_exit(cpufreq_exit);
  140. module_param(nowait, uint, 0644);
  141. MODULE_PARM_DESC(nowait, "Disable Loongson-2F specific wait");
  142. MODULE_AUTHOR("Yanhua <[email protected]>");
  143. MODULE_DESCRIPTION("cpufreq driver for Loongson2F");
  144. MODULE_LICENSE("GPL");