cpuidle.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * CPU idle for DaVinci SoCs
  3. *
  4. * Copyright (C) 2009 Texas Instruments Incorporated. http://www.ti.com/
  5. *
  6. * Derived from Marvell Kirkwood CPU idle code
  7. * (arch/arm/mach-kirkwood/cpuidle.c)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/io.h>
  18. #include <asm/proc-fns.h>
  19. #include <mach/cpuidle.h>
  20. #include <mach/memory.h>
  21. #define DAVINCI_CPUIDLE_MAX_STATES 2
  22. struct davinci_ops {
  23. void (*enter) (u32 flags);
  24. void (*exit) (u32 flags);
  25. u32 flags;
  26. };
  27. /* fields in davinci_ops.flags */
  28. #define DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN BIT(0)
  29. static struct cpuidle_driver davinci_idle_driver = {
  30. .name = "cpuidle-davinci",
  31. .owner = THIS_MODULE,
  32. };
  33. static DEFINE_PER_CPU(struct cpuidle_device, davinci_cpuidle_device);
  34. static void __iomem *ddr2_reg_base;
  35. static void davinci_save_ddr_power(int enter, bool pdown)
  36. {
  37. u32 val;
  38. val = __raw_readl(ddr2_reg_base + DDR2_SDRCR_OFFSET);
  39. if (enter) {
  40. if (pdown)
  41. val |= DDR2_SRPD_BIT;
  42. else
  43. val &= ~DDR2_SRPD_BIT;
  44. val |= DDR2_LPMODEN_BIT;
  45. } else {
  46. val &= ~(DDR2_SRPD_BIT | DDR2_LPMODEN_BIT);
  47. }
  48. __raw_writel(val, ddr2_reg_base + DDR2_SDRCR_OFFSET);
  49. }
  50. static void davinci_c2state_enter(u32 flags)
  51. {
  52. davinci_save_ddr_power(1, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
  53. }
  54. static void davinci_c2state_exit(u32 flags)
  55. {
  56. davinci_save_ddr_power(0, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
  57. }
  58. static struct davinci_ops davinci_states[DAVINCI_CPUIDLE_MAX_STATES] = {
  59. [1] = {
  60. .enter = davinci_c2state_enter,
  61. .exit = davinci_c2state_exit,
  62. },
  63. };
  64. /* Actual code that puts the SoC in different idle states */
  65. static int davinci_enter_idle(struct cpuidle_device *dev,
  66. struct cpuidle_state *state)
  67. {
  68. struct davinci_ops *ops = cpuidle_get_statedata(state);
  69. struct timeval before, after;
  70. int idle_time;
  71. local_irq_disable();
  72. do_gettimeofday(&before);
  73. if (ops && ops->enter)
  74. ops->enter(ops->flags);
  75. /* Wait for interrupt state */
  76. cpu_do_idle();
  77. if (ops && ops->exit)
  78. ops->exit(ops->flags);
  79. do_gettimeofday(&after);
  80. local_irq_enable();
  81. idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
  82. (after.tv_usec - before.tv_usec);
  83. return idle_time;
  84. }
  85. static int __init davinci_cpuidle_probe(struct platform_device *pdev)
  86. {
  87. int ret;
  88. struct cpuidle_device *device;
  89. struct davinci_cpuidle_config *pdata = pdev->dev.platform_data;
  90. device = &per_cpu(davinci_cpuidle_device, smp_processor_id());
  91. if (!pdata) {
  92. dev_err(&pdev->dev, "cannot get platform data\n");
  93. return -ENOENT;
  94. }
  95. ddr2_reg_base = pdata->ddr2_ctlr_base;
  96. ret = cpuidle_register_driver(&davinci_idle_driver);
  97. if (ret) {
  98. dev_err(&pdev->dev, "failed to register driver\n");
  99. return ret;
  100. }
  101. /* Wait for interrupt state */
  102. device->states[0].enter = davinci_enter_idle;
  103. device->states[0].exit_latency = 1;
  104. device->states[0].target_residency = 10000;
  105. device->states[0].flags = CPUIDLE_FLAG_TIME_VALID;
  106. strcpy(device->states[0].name, "WFI");
  107. strcpy(device->states[0].desc, "Wait for interrupt");
  108. /* Wait for interrupt and DDR self refresh state */
  109. device->states[1].enter = davinci_enter_idle;
  110. device->states[1].exit_latency = 10;
  111. device->states[1].target_residency = 10000;
  112. device->states[1].flags = CPUIDLE_FLAG_TIME_VALID;
  113. strcpy(device->states[1].name, "DDR SR");
  114. strcpy(device->states[1].desc, "WFI and DDR Self Refresh");
  115. if (pdata->ddr2_pdown)
  116. davinci_states[1].flags |= DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN;
  117. cpuidle_set_statedata(&device->states[1], &davinci_states[1]);
  118. device->state_count = DAVINCI_CPUIDLE_MAX_STATES;
  119. ret = cpuidle_register_device(device);
  120. if (ret) {
  121. dev_err(&pdev->dev, "failed to register device\n");
  122. cpuidle_unregister_driver(&davinci_idle_driver);
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static struct platform_driver davinci_cpuidle_driver = {
  128. .driver = {
  129. .name = "cpuidle-davinci",
  130. .owner = THIS_MODULE,
  131. },
  132. };
  133. static int __init davinci_cpuidle_init(void)
  134. {
  135. return platform_driver_probe(&davinci_cpuidle_driver,
  136. davinci_cpuidle_probe);
  137. }
  138. device_initcall(davinci_cpuidle_init);