cpuidle.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <linux/export.h>
  19. #include <asm/proc-fns.h>
  20. #include <asm/cpuidle.h>
  21. #include <mach/cpuidle.h>
  22. #include <mach/ddr2.h>
  23. #define DAVINCI_CPUIDLE_MAX_STATES 2
  24. struct davinci_ops {
  25. void (*enter) (u32 flags);
  26. void (*exit) (u32 flags);
  27. u32 flags;
  28. };
  29. /* Actual code that puts the SoC in different idle states */
  30. static int davinci_enter_idle(struct cpuidle_device *dev,
  31. struct cpuidle_driver *drv,
  32. int index)
  33. {
  34. struct cpuidle_state_usage *state_usage = &dev->states_usage[index];
  35. struct davinci_ops *ops = cpuidle_get_statedata(state_usage);
  36. if (ops && ops->enter)
  37. ops->enter(ops->flags);
  38. index = cpuidle_wrap_enter(dev, drv, index,
  39. arm_cpuidle_simple_enter);
  40. if (ops && ops->exit)
  41. ops->exit(ops->flags);
  42. return index;
  43. }
  44. /* fields in davinci_ops.flags */
  45. #define DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN BIT(0)
  46. static struct cpuidle_driver davinci_idle_driver = {
  47. .name = "cpuidle-davinci",
  48. .owner = THIS_MODULE,
  49. .en_core_tk_irqen = 1,
  50. .states[0] = ARM_CPUIDLE_WFI_STATE,
  51. .states[1] = {
  52. .enter = davinci_enter_idle,
  53. .exit_latency = 10,
  54. .target_residency = 100000,
  55. .flags = CPUIDLE_FLAG_TIME_VALID,
  56. .name = "DDR SR",
  57. .desc = "WFI and DDR Self Refresh",
  58. },
  59. .state_count = DAVINCI_CPUIDLE_MAX_STATES,
  60. };
  61. static DEFINE_PER_CPU(struct cpuidle_device, davinci_cpuidle_device);
  62. static void __iomem *ddr2_reg_base;
  63. static void davinci_save_ddr_power(int enter, bool pdown)
  64. {
  65. u32 val;
  66. val = __raw_readl(ddr2_reg_base + DDR2_SDRCR_OFFSET);
  67. if (enter) {
  68. if (pdown)
  69. val |= DDR2_SRPD_BIT;
  70. else
  71. val &= ~DDR2_SRPD_BIT;
  72. val |= DDR2_LPMODEN_BIT;
  73. } else {
  74. val &= ~(DDR2_SRPD_BIT | DDR2_LPMODEN_BIT);
  75. }
  76. __raw_writel(val, ddr2_reg_base + DDR2_SDRCR_OFFSET);
  77. }
  78. static void davinci_c2state_enter(u32 flags)
  79. {
  80. davinci_save_ddr_power(1, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
  81. }
  82. static void davinci_c2state_exit(u32 flags)
  83. {
  84. davinci_save_ddr_power(0, !!(flags & DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN));
  85. }
  86. static struct davinci_ops davinci_states[DAVINCI_CPUIDLE_MAX_STATES] = {
  87. [1] = {
  88. .enter = davinci_c2state_enter,
  89. .exit = davinci_c2state_exit,
  90. },
  91. };
  92. static int __init davinci_cpuidle_probe(struct platform_device *pdev)
  93. {
  94. int ret;
  95. struct cpuidle_device *device;
  96. struct davinci_cpuidle_config *pdata = pdev->dev.platform_data;
  97. device = &per_cpu(davinci_cpuidle_device, smp_processor_id());
  98. if (!pdata) {
  99. dev_err(&pdev->dev, "cannot get platform data\n");
  100. return -ENOENT;
  101. }
  102. ddr2_reg_base = pdata->ddr2_ctlr_base;
  103. if (pdata->ddr2_pdown)
  104. davinci_states[1].flags |= DAVINCI_CPUIDLE_FLAGS_DDR2_PWDN;
  105. cpuidle_set_statedata(&device->states_usage[1], &davinci_states[1]);
  106. device->state_count = DAVINCI_CPUIDLE_MAX_STATES;
  107. ret = cpuidle_register_driver(&davinci_idle_driver);
  108. if (ret) {
  109. dev_err(&pdev->dev, "failed to register driver\n");
  110. return ret;
  111. }
  112. ret = cpuidle_register_device(device);
  113. if (ret) {
  114. dev_err(&pdev->dev, "failed to register device\n");
  115. cpuidle_unregister_driver(&davinci_idle_driver);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static struct platform_driver davinci_cpuidle_driver = {
  121. .driver = {
  122. .name = "cpuidle-davinci",
  123. .owner = THIS_MODULE,
  124. },
  125. };
  126. static int __init davinci_cpuidle_init(void)
  127. {
  128. return platform_driver_probe(&davinci_cpuidle_driver,
  129. davinci_cpuidle_probe);
  130. }
  131. device_initcall(davinci_cpuidle_init);