loongson2_cpufreq.c 4.3 KB

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