cpuidle.c 3.6 KB

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