cpuidle.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * based on arch/arm/mach-kirkwood/cpuidle.c
  3. *
  4. * CPU idle support for AT91 SoC
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. *
  10. * The cpu idle uses wait-for-interrupt and RAM self refresh in order
  11. * to implement two idle states -
  12. * #1 wait-for-interrupt
  13. * #2 wait-for-interrupt and RAM self refresh
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/cpuidle.h>
  19. #include <asm/proc-fns.h>
  20. #include <linux/io.h>
  21. #include "pm.h"
  22. #define AT91_MAX_STATES 2
  23. static DEFINE_PER_CPU(struct cpuidle_device, at91_cpuidle_device);
  24. static struct cpuidle_driver at91_idle_driver = {
  25. .name = "at91_idle",
  26. .owner = THIS_MODULE,
  27. };
  28. /* Actual code that puts the SoC in different idle states */
  29. static int at91_enter_idle(struct cpuidle_device *dev,
  30. struct cpuidle_state *state)
  31. {
  32. struct timeval before, after;
  33. int idle_time;
  34. u32 saved_lpr;
  35. local_irq_disable();
  36. do_gettimeofday(&before);
  37. if (state == &dev->states[0])
  38. /* Wait for interrupt state */
  39. cpu_do_idle();
  40. else if (state == &dev->states[1]) {
  41. asm("b 1f; .align 5; 1:");
  42. asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
  43. saved_lpr = sdram_selfrefresh_enable();
  44. cpu_do_idle();
  45. sdram_selfrefresh_disable(saved_lpr);
  46. }
  47. do_gettimeofday(&after);
  48. local_irq_enable();
  49. idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
  50. (after.tv_usec - before.tv_usec);
  51. return idle_time;
  52. }
  53. /* Initialize CPU idle by registering the idle states */
  54. static int at91_init_cpuidle(void)
  55. {
  56. struct cpuidle_device *device;
  57. cpuidle_register_driver(&at91_idle_driver);
  58. device = &per_cpu(at91_cpuidle_device, smp_processor_id());
  59. device->state_count = AT91_MAX_STATES;
  60. /* Wait for interrupt state */
  61. device->states[0].enter = at91_enter_idle;
  62. device->states[0].exit_latency = 1;
  63. device->states[0].target_residency = 10000;
  64. device->states[0].flags = CPUIDLE_FLAG_TIME_VALID;
  65. strcpy(device->states[0].name, "WFI");
  66. strcpy(device->states[0].desc, "Wait for interrupt");
  67. /* Wait for interrupt and RAM self refresh state */
  68. device->states[1].enter = at91_enter_idle;
  69. device->states[1].exit_latency = 10;
  70. device->states[1].target_residency = 10000;
  71. device->states[1].flags = CPUIDLE_FLAG_TIME_VALID;
  72. strcpy(device->states[1].name, "RAM_SR");
  73. strcpy(device->states[1].desc, "WFI and RAM Self Refresh");
  74. if (cpuidle_register_device(device)) {
  75. printk(KERN_ERR "at91_init_cpuidle: Failed registering\n");
  76. return -EIO;
  77. }
  78. return 0;
  79. }
  80. device_initcall(at91_init_cpuidle);