cpuidle-calxeda.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2012 Calxeda, Inc.
  3. *
  4. * Based on arch/arm/plat-mxc/cpuidle.c:
  5. * Copyright 2012 Freescale Semiconductor, Inc.
  6. * Copyright 2012 Linaro Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/cpuidle.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/time.h>
  25. #include <linux/delay.h>
  26. #include <linux/suspend.h>
  27. #include <asm/cpuidle.h>
  28. #include <asm/proc-fns.h>
  29. #include <asm/smp_scu.h>
  30. #include <asm/suspend.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/cp15.h>
  33. extern void highbank_set_cpu_jump(int cpu, void *jump_addr);
  34. extern void *scu_base_addr;
  35. static struct cpuidle_device __percpu *calxeda_idle_cpuidle_devices;
  36. static inline unsigned int get_auxcr(void)
  37. {
  38. unsigned int val;
  39. asm("mrc p15, 0, %0, c1, c0, 1 @ get AUXCR" : "=r" (val) : : "cc");
  40. return val;
  41. }
  42. static inline void set_auxcr(unsigned int val)
  43. {
  44. asm volatile("mcr p15, 0, %0, c1, c0, 1 @ set AUXCR"
  45. : : "r" (val) : "cc");
  46. isb();
  47. }
  48. static noinline void calxeda_idle_restore(void)
  49. {
  50. set_cr(get_cr() | CR_C);
  51. set_auxcr(get_auxcr() | 0x40);
  52. scu_power_mode(scu_base_addr, SCU_PM_NORMAL);
  53. }
  54. static int calxeda_idle_finish(unsigned long val)
  55. {
  56. /* Already flushed cache, but do it again as the outer cache functions
  57. * dirty the cache with spinlocks */
  58. flush_cache_all();
  59. set_auxcr(get_auxcr() & ~0x40);
  60. set_cr(get_cr() & ~CR_C);
  61. scu_power_mode(scu_base_addr, SCU_PM_DORMANT);
  62. cpu_do_idle();
  63. /* Restore things if we didn't enter power-gating */
  64. calxeda_idle_restore();
  65. return 1;
  66. }
  67. static int calxeda_pwrdown_idle(struct cpuidle_device *dev,
  68. struct cpuidle_driver *drv,
  69. int index)
  70. {
  71. highbank_set_cpu_jump(smp_processor_id(), cpu_resume);
  72. cpu_suspend(0, calxeda_idle_finish);
  73. return index;
  74. }
  75. static void calxeda_idle_cpuidle_devices_uninit(void)
  76. {
  77. int i;
  78. struct cpuidle_device *dev;
  79. for_each_possible_cpu(i) {
  80. dev = per_cpu_ptr(calxeda_idle_cpuidle_devices, i);
  81. cpuidle_unregister_device(dev);
  82. }
  83. free_percpu(calxeda_idle_cpuidle_devices);
  84. }
  85. static struct cpuidle_driver calxeda_idle_driver = {
  86. .name = "calxeda_idle",
  87. .states = {
  88. ARM_CPUIDLE_WFI_STATE,
  89. {
  90. .name = "PG",
  91. .desc = "Power Gate",
  92. .flags = CPUIDLE_FLAG_TIME_VALID,
  93. .exit_latency = 30,
  94. .power_usage = 50,
  95. .target_residency = 200,
  96. .enter = calxeda_pwrdown_idle,
  97. },
  98. },
  99. .state_count = 2,
  100. };
  101. static int __init calxeda_cpuidle_init(void)
  102. {
  103. int cpu_id;
  104. int ret;
  105. struct cpuidle_device *dev;
  106. struct cpuidle_driver *drv = &calxeda_idle_driver;
  107. if (!of_machine_is_compatible("calxeda,highbank"))
  108. return -ENODEV;
  109. ret = cpuidle_register_driver(drv);
  110. if (ret)
  111. return ret;
  112. calxeda_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  113. if (calxeda_idle_cpuidle_devices == NULL) {
  114. ret = -ENOMEM;
  115. goto unregister_drv;
  116. }
  117. /* initialize state data for each cpuidle_device */
  118. for_each_possible_cpu(cpu_id) {
  119. dev = per_cpu_ptr(calxeda_idle_cpuidle_devices, cpu_id);
  120. dev->cpu = cpu_id;
  121. dev->state_count = drv->state_count;
  122. ret = cpuidle_register_device(dev);
  123. if (ret) {
  124. pr_err("Failed to register cpu %u, error: %d\n",
  125. cpu_id, ret);
  126. goto uninit;
  127. }
  128. }
  129. return 0;
  130. uninit:
  131. calxeda_idle_cpuidle_devices_uninit();
  132. unregister_drv:
  133. cpuidle_unregister_driver(drv);
  134. return ret;
  135. }
  136. module_init(calxeda_cpuidle_init);