pm-r8a7779.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * r8a7779 Power management support
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Copyright (C) 2011 Magnus Damm
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/pm.h>
  12. #include <linux/suspend.h>
  13. #include <linux/err.h>
  14. #include <linux/pm_clock.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/console.h>
  20. #include <asm/system.h>
  21. #include <asm/io.h>
  22. #include <mach/common.h>
  23. #include <mach/r8a7779.h>
  24. static void __iomem *r8a7779_sysc_base;
  25. /* SYSC */
  26. #define SYSCSR 0x00
  27. #define SYSCISR 0x04
  28. #define SYSCISCR 0x08
  29. #define SYSCIER 0x0c
  30. #define SYSCIMR 0x10
  31. #define PWRSR0 0x40
  32. #define PWRSR1 0x80
  33. #define PWRSR2 0xc0
  34. #define PWRSR3 0x100
  35. #define PWRSR4 0x140
  36. #define PWRSR_OFFS 0x00
  37. #define PWROFFCR_OFFS 0x04
  38. #define PWRONCR_OFFS 0x0c
  39. #define PWRER_OFFS 0x14
  40. #define SYSCSR_RETRIES 100
  41. #define SYSCSR_DELAY_US 1
  42. #define SYSCISR_RETRIES 1000
  43. #define SYSCISR_DELAY_US 1
  44. #ifdef CONFIG_PM
  45. static int r8a7779_sysc_pwr_on_off(struct r8a7779_pm_ch *r8a7779_ch,
  46. int sr_bit, int reg_offs)
  47. {
  48. int k;
  49. for (k = 0; k < SYSCSR_RETRIES; k++) {
  50. if (ioread32(r8a7779_sysc_base + SYSCSR) & (1 << sr_bit))
  51. break;
  52. udelay(SYSCSR_DELAY_US);
  53. }
  54. if (k == SYSCSR_RETRIES)
  55. return -EAGAIN;
  56. iowrite32(1 << r8a7779_ch->chan_bit,
  57. r8a7779_sysc_base + r8a7779_ch->chan_offs + reg_offs);
  58. return 0;
  59. }
  60. static int r8a7779_sysc_pwr_off(struct r8a7779_pm_ch *r8a7779_ch)
  61. {
  62. return r8a7779_sysc_pwr_on_off(r8a7779_ch, 0, PWROFFCR_OFFS);
  63. }
  64. static int r8a7779_sysc_pwr_on(struct r8a7779_pm_ch *r8a7779_ch)
  65. {
  66. return r8a7779_sysc_pwr_on_off(r8a7779_ch, 1, PWRONCR_OFFS);
  67. }
  68. static int r8a7779_sysc_update(struct r8a7779_pm_ch *r8a7779_ch,
  69. int (*on_off_fn)(struct r8a7779_pm_ch *))
  70. {
  71. unsigned int isr_mask = 1 << r8a7779_ch->isr_bit;
  72. unsigned int chan_mask = 1 << r8a7779_ch->chan_bit;
  73. unsigned int status;
  74. int ret = 0;
  75. int k;
  76. iowrite32(isr_mask, r8a7779_sysc_base + SYSCISCR);
  77. do {
  78. ret = on_off_fn(r8a7779_ch);
  79. if (ret)
  80. goto out;
  81. status = ioread32(r8a7779_sysc_base +
  82. r8a7779_ch->chan_offs + PWRER_OFFS);
  83. } while (status & chan_mask);
  84. for (k = 0; k < SYSCISR_RETRIES; k++) {
  85. if (ioread32(r8a7779_sysc_base + SYSCISR) & isr_mask)
  86. break;
  87. udelay(SYSCISR_DELAY_US);
  88. }
  89. if (k == SYSCISR_RETRIES)
  90. ret = -EIO;
  91. iowrite32(isr_mask, r8a7779_sysc_base + SYSCISCR);
  92. out:
  93. pr_debug("r8a7779 power domain %d: %02x %02x %02x %02x %02x -> %d\n",
  94. r8a7779_ch->isr_bit, ioread32(r8a7779_sysc_base + PWRSR0),
  95. ioread32(r8a7779_sysc_base + PWRSR1),
  96. ioread32(r8a7779_sysc_base + PWRSR2),
  97. ioread32(r8a7779_sysc_base + PWRSR3),
  98. ioread32(r8a7779_sysc_base + PWRSR4), ret);
  99. return ret;
  100. }
  101. static int r8a7779_sysc_power_down(struct r8a7779_pm_ch *r8a7779_ch)
  102. {
  103. return r8a7779_sysc_update(r8a7779_ch, r8a7779_sysc_pwr_off);
  104. }
  105. static int r8a7779_sysc_power_up(struct r8a7779_pm_ch *r8a7779_ch)
  106. {
  107. return r8a7779_sysc_update(r8a7779_ch, r8a7779_sysc_pwr_on);
  108. }
  109. static void __init r8a7779_sysc_init(void)
  110. {
  111. r8a7779_sysc_base = ioremap_nocache(0xffd85000, PAGE_SIZE);
  112. if (!r8a7779_sysc_base)
  113. panic("unable to ioremap r8a7779 SYSC hardware block\n");
  114. /* enable all interrupt sources, but do not use interrupt handler */
  115. iowrite32(0x0131000e, r8a7779_sysc_base + SYSCIER);
  116. iowrite32(0, r8a7779_sysc_base + SYSCIMR);
  117. }
  118. static int pd_power_down(struct generic_pm_domain *genpd)
  119. {
  120. return r8a7779_sysc_power_down(to_r8a7779_ch(genpd));
  121. }
  122. static int pd_power_up(struct generic_pm_domain *genpd)
  123. {
  124. return r8a7779_sysc_power_up(to_r8a7779_ch(genpd));
  125. }
  126. static bool pd_is_off(struct generic_pm_domain *genpd)
  127. {
  128. struct r8a7779_pm_ch *r8a7779_ch = to_r8a7779_ch(genpd);
  129. unsigned int st;
  130. st = ioread32(r8a7779_sysc_base + r8a7779_ch->chan_offs + PWRSR_OFFS);
  131. if (st & (1 << r8a7779_ch->chan_bit))
  132. return true;
  133. return false;
  134. }
  135. static bool pd_active_wakeup(struct device *dev)
  136. {
  137. return true;
  138. }
  139. void r8a7779_init_pm_domain(struct r8a7779_pm_domain *r8a7779_pd)
  140. {
  141. struct generic_pm_domain *genpd = &r8a7779_pd->genpd;
  142. pm_genpd_init(genpd, NULL, false);
  143. genpd->dev_ops.stop = pm_clk_suspend;
  144. genpd->dev_ops.start = pm_clk_resume;
  145. genpd->dev_ops.active_wakeup = pd_active_wakeup;
  146. genpd->dev_irq_safe = true;
  147. genpd->power_off = pd_power_down;
  148. genpd->power_on = pd_power_up;
  149. if (pd_is_off(&r8a7779_pd->genpd))
  150. pd_power_up(&r8a7779_pd->genpd);
  151. }
  152. void r8a7779_add_device_to_domain(struct r8a7779_pm_domain *r8a7779_pd,
  153. struct platform_device *pdev)
  154. {
  155. struct device *dev = &pdev->dev;
  156. pm_genpd_add_device(&r8a7779_pd->genpd, dev);
  157. if (pm_clk_no_clocks(dev))
  158. pm_clk_add(dev, NULL);
  159. }
  160. struct r8a7779_pm_domain r8a7779_sh4a = {
  161. .ch = {
  162. .chan_offs = 0x80, /* PWRSR1 .. PWRER1 */
  163. .isr_bit = 16, /* SH4A */
  164. }
  165. };
  166. struct r8a7779_pm_domain r8a7779_sgx = {
  167. .ch = {
  168. .chan_offs = 0xc0, /* PWRSR2 .. PWRER2 */
  169. .isr_bit = 20, /* SGX */
  170. }
  171. };
  172. struct r8a7779_pm_domain r8a7779_vdp1 = {
  173. .ch = {
  174. .chan_offs = 0x100, /* PWRSR3 .. PWRER3 */
  175. .isr_bit = 21, /* VDP */
  176. }
  177. };
  178. struct r8a7779_pm_domain r8a7779_impx3 = {
  179. .ch = {
  180. .chan_offs = 0x140, /* PWRSR4 .. PWRER4 */
  181. .isr_bit = 24, /* IMP */
  182. }
  183. };
  184. #else /* CONFIG_PM */
  185. static inline void r8a7779_sysc_init(void) {}
  186. #endif /* CONFIG_PM */
  187. void __init r8a7779_pm_init(void)
  188. {
  189. r8a7779_sysc_init();
  190. }