cpuidle-calxeda.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. .en_core_tk_irqen = 1,
  88. .states = {
  89. ARM_CPUIDLE_WFI_STATE,
  90. {
  91. .name = "PG",
  92. .desc = "Power Gate",
  93. .flags = CPUIDLE_FLAG_TIME_VALID,
  94. .exit_latency = 30,
  95. .power_usage = 50,
  96. .target_residency = 200,
  97. .enter = calxeda_pwrdown_idle,
  98. },
  99. },
  100. .state_count = 2,
  101. };
  102. static int __init calxeda_cpuidle_init(void)
  103. {
  104. int cpu_id;
  105. int ret;
  106. struct cpuidle_device *dev;
  107. struct cpuidle_driver *drv = &calxeda_idle_driver;
  108. if (!of_machine_is_compatible("calxeda,highbank"))
  109. return -ENODEV;
  110. ret = cpuidle_register_driver(drv);
  111. if (ret)
  112. return ret;
  113. calxeda_idle_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  114. if (calxeda_idle_cpuidle_devices == NULL) {
  115. ret = -ENOMEM;
  116. goto unregister_drv;
  117. }
  118. /* initialize state data for each cpuidle_device */
  119. for_each_possible_cpu(cpu_id) {
  120. dev = per_cpu_ptr(calxeda_idle_cpuidle_devices, cpu_id);
  121. dev->cpu = cpu_id;
  122. dev->state_count = drv->state_count;
  123. ret = cpuidle_register_device(dev);
  124. if (ret) {
  125. pr_err("Failed to register cpu %u, error: %d\n",
  126. cpu_id, ret);
  127. goto uninit;
  128. }
  129. }
  130. return 0;
  131. uninit:
  132. calxeda_idle_cpuidle_devices_uninit();
  133. unregister_drv:
  134. cpuidle_unregister_driver(drv);
  135. return ret;
  136. }
  137. module_init(calxeda_cpuidle_init);