pm-rmobile.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * rmobile power management support
  3. *
  4. * Copyright (C) 2012 Renesas Solutions Corp.
  5. * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * based on pm-sh7372.c
  8. * Copyright (C) 2011 Magnus Damm
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/console.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/pm_clock.h>
  19. #include <asm/io.h>
  20. #include <mach/pm-rmobile.h>
  21. /* SYSC */
  22. #define SPDCR IOMEM(0xe6180008)
  23. #define SWUCR IOMEM(0xe6180014)
  24. #define PSTR IOMEM(0xe6180080)
  25. #define PSTR_RETRIES 100
  26. #define PSTR_DELAY_US 10
  27. #ifdef CONFIG_PM
  28. static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
  29. {
  30. struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
  31. unsigned int mask = 1 << rmobile_pd->bit_shift;
  32. if (rmobile_pd->suspend) {
  33. int ret = rmobile_pd->suspend();
  34. if (ret)
  35. return ret;
  36. }
  37. if (__raw_readl(PSTR) & mask) {
  38. unsigned int retry_count;
  39. __raw_writel(mask, SPDCR);
  40. for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
  41. if (!(__raw_readl(SPDCR) & mask))
  42. break;
  43. cpu_relax();
  44. }
  45. }
  46. if (!rmobile_pd->no_debug)
  47. pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
  48. genpd->name, mask, __raw_readl(PSTR));
  49. return 0;
  50. }
  51. static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd,
  52. bool do_resume)
  53. {
  54. unsigned int mask = 1 << rmobile_pd->bit_shift;
  55. unsigned int retry_count;
  56. int ret = 0;
  57. if (__raw_readl(PSTR) & mask)
  58. goto out;
  59. __raw_writel(mask, SWUCR);
  60. for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
  61. if (!(__raw_readl(SWUCR) & mask))
  62. break;
  63. if (retry_count > PSTR_RETRIES)
  64. udelay(PSTR_DELAY_US);
  65. else
  66. cpu_relax();
  67. }
  68. if (!retry_count)
  69. ret = -EIO;
  70. if (!rmobile_pd->no_debug)
  71. pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
  72. rmobile_pd->genpd.name, mask, __raw_readl(PSTR));
  73. out:
  74. if (ret == 0 && rmobile_pd->resume && do_resume)
  75. rmobile_pd->resume();
  76. return ret;
  77. }
  78. static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
  79. {
  80. return __rmobile_pd_power_up(to_rmobile_pd(genpd), true);
  81. }
  82. static bool rmobile_pd_active_wakeup(struct device *dev)
  83. {
  84. bool (*active_wakeup)(struct device *dev);
  85. active_wakeup = dev_gpd_data(dev)->ops.active_wakeup;
  86. return active_wakeup ? active_wakeup(dev) : true;
  87. }
  88. static int rmobile_pd_stop_dev(struct device *dev)
  89. {
  90. int (*stop)(struct device *dev);
  91. stop = dev_gpd_data(dev)->ops.stop;
  92. if (stop) {
  93. int ret = stop(dev);
  94. if (ret)
  95. return ret;
  96. }
  97. return pm_clk_suspend(dev);
  98. }
  99. static int rmobile_pd_start_dev(struct device *dev)
  100. {
  101. int (*start)(struct device *dev);
  102. int ret;
  103. ret = pm_clk_resume(dev);
  104. if (ret)
  105. return ret;
  106. start = dev_gpd_data(dev)->ops.start;
  107. if (start)
  108. ret = start(dev);
  109. return ret;
  110. }
  111. static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
  112. {
  113. struct generic_pm_domain *genpd = &rmobile_pd->genpd;
  114. struct dev_power_governor *gov = rmobile_pd->gov;
  115. pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
  116. genpd->dev_ops.stop = rmobile_pd_stop_dev;
  117. genpd->dev_ops.start = rmobile_pd_start_dev;
  118. genpd->dev_ops.active_wakeup = rmobile_pd_active_wakeup;
  119. genpd->dev_irq_safe = true;
  120. genpd->power_off = rmobile_pd_power_down;
  121. genpd->power_on = rmobile_pd_power_up;
  122. __rmobile_pd_power_up(rmobile_pd, false);
  123. }
  124. void rmobile_init_domains(struct rmobile_pm_domain domains[], int num)
  125. {
  126. int j;
  127. for (j = 0; j < num; j++)
  128. rmobile_init_pm_domain(&domains[j]);
  129. }
  130. void rmobile_add_device_to_domain_td(const char *domain_name,
  131. struct platform_device *pdev,
  132. struct gpd_timing_data *td)
  133. {
  134. struct device *dev = &pdev->dev;
  135. __pm_genpd_name_add_device(domain_name, dev, td);
  136. if (pm_clk_no_clocks(dev))
  137. pm_clk_add(dev, NULL);
  138. }
  139. void rmobile_add_devices_to_domains(struct pm_domain_device data[],
  140. int size)
  141. {
  142. struct gpd_timing_data latencies = {
  143. .stop_latency_ns = DEFAULT_DEV_LATENCY_NS,
  144. .start_latency_ns = DEFAULT_DEV_LATENCY_NS,
  145. .save_state_latency_ns = DEFAULT_DEV_LATENCY_NS,
  146. .restore_state_latency_ns = DEFAULT_DEV_LATENCY_NS,
  147. };
  148. int j;
  149. for (j = 0; j < size; j++)
  150. rmobile_add_device_to_domain_td(data[j].domain_name,
  151. data[j].pdev, &latencies);
  152. }
  153. #endif /* CONFIG_PM */