pm-r8a7779.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/io.h>
  21. #include <mach/common.h>
  22. #include <mach/r8a7779.h>
  23. static void __iomem *r8a7779_sysc_base;
  24. /* SYSC */
  25. #define SYSCSR 0x00
  26. #define SYSCISR 0x04
  27. #define SYSCISCR 0x08
  28. #define SYSCIER 0x0c
  29. #define SYSCIMR 0x10
  30. #define PWRSR0 0x40
  31. #define PWRSR1 0x80
  32. #define PWRSR2 0xc0
  33. #define PWRSR3 0x100
  34. #define PWRSR4 0x140
  35. #define PWRSR_OFFS 0x00
  36. #define PWROFFCR_OFFS 0x04
  37. #define PWRONCR_OFFS 0x0c
  38. #define PWRER_OFFS 0x14
  39. #define SYSCSR_RETRIES 100
  40. #define SYSCSR_DELAY_US 1
  41. #define SYSCISR_RETRIES 1000
  42. #define SYSCISR_DELAY_US 1
  43. #if defined(CONFIG_PM) || defined(CONFIG_SMP)
  44. static DEFINE_SPINLOCK(r8a7779_sysc_lock); /* SMP CPUs + I/O devices */
  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. unsigned long flags;
  75. int ret = 0;
  76. int k;
  77. spin_lock_irqsave(&r8a7779_sysc_lock, flags);
  78. iowrite32(isr_mask, r8a7779_sysc_base + SYSCISCR);
  79. do {
  80. ret = on_off_fn(r8a7779_ch);
  81. if (ret)
  82. goto out;
  83. status = ioread32(r8a7779_sysc_base +
  84. r8a7779_ch->chan_offs + PWRER_OFFS);
  85. } while (status & chan_mask);
  86. for (k = 0; k < SYSCISR_RETRIES; k++) {
  87. if (ioread32(r8a7779_sysc_base + SYSCISR) & isr_mask)
  88. break;
  89. udelay(SYSCISR_DELAY_US);
  90. }
  91. if (k == SYSCISR_RETRIES)
  92. ret = -EIO;
  93. iowrite32(isr_mask, r8a7779_sysc_base + SYSCISCR);
  94. out:
  95. spin_unlock_irqrestore(&r8a7779_sysc_lock, flags);
  96. pr_debug("r8a7779 power domain %d: %02x %02x %02x %02x %02x -> %d\n",
  97. r8a7779_ch->isr_bit, ioread32(r8a7779_sysc_base + PWRSR0),
  98. ioread32(r8a7779_sysc_base + PWRSR1),
  99. ioread32(r8a7779_sysc_base + PWRSR2),
  100. ioread32(r8a7779_sysc_base + PWRSR3),
  101. ioread32(r8a7779_sysc_base + PWRSR4), ret);
  102. return ret;
  103. }
  104. int r8a7779_sysc_power_down(struct r8a7779_pm_ch *r8a7779_ch)
  105. {
  106. return r8a7779_sysc_update(r8a7779_ch, r8a7779_sysc_pwr_off);
  107. }
  108. int r8a7779_sysc_power_up(struct r8a7779_pm_ch *r8a7779_ch)
  109. {
  110. return r8a7779_sysc_update(r8a7779_ch, r8a7779_sysc_pwr_on);
  111. }
  112. static void __init r8a7779_sysc_init(void)
  113. {
  114. r8a7779_sysc_base = ioremap_nocache(0xffd85000, PAGE_SIZE);
  115. if (!r8a7779_sysc_base)
  116. panic("unable to ioremap r8a7779 SYSC hardware block\n");
  117. /* enable all interrupt sources, but do not use interrupt handler */
  118. iowrite32(0x0131000e, r8a7779_sysc_base + SYSCIER);
  119. iowrite32(0, r8a7779_sysc_base + SYSCIMR);
  120. }
  121. #else /* CONFIG_PM || CONFIG_SMP */
  122. static inline void r8a7779_sysc_init(void) {}
  123. #endif /* CONFIG_PM || CONFIG_SMP */
  124. #ifdef CONFIG_PM
  125. static int pd_power_down(struct generic_pm_domain *genpd)
  126. {
  127. return r8a7779_sysc_power_down(to_r8a7779_ch(genpd));
  128. }
  129. static int pd_power_up(struct generic_pm_domain *genpd)
  130. {
  131. return r8a7779_sysc_power_up(to_r8a7779_ch(genpd));
  132. }
  133. static bool pd_is_off(struct generic_pm_domain *genpd)
  134. {
  135. struct r8a7779_pm_ch *r8a7779_ch = to_r8a7779_ch(genpd);
  136. unsigned int st;
  137. st = ioread32(r8a7779_sysc_base + r8a7779_ch->chan_offs + PWRSR_OFFS);
  138. if (st & (1 << r8a7779_ch->chan_bit))
  139. return true;
  140. return false;
  141. }
  142. static bool pd_active_wakeup(struct device *dev)
  143. {
  144. return true;
  145. }
  146. void r8a7779_init_pm_domain(struct r8a7779_pm_domain *r8a7779_pd)
  147. {
  148. struct generic_pm_domain *genpd = &r8a7779_pd->genpd;
  149. pm_genpd_init(genpd, NULL, false);
  150. genpd->dev_ops.stop = pm_clk_suspend;
  151. genpd->dev_ops.start = pm_clk_resume;
  152. genpd->dev_ops.active_wakeup = pd_active_wakeup;
  153. genpd->dev_irq_safe = true;
  154. genpd->power_off = pd_power_down;
  155. genpd->power_on = pd_power_up;
  156. if (pd_is_off(&r8a7779_pd->genpd))
  157. pd_power_up(&r8a7779_pd->genpd);
  158. }
  159. void r8a7779_add_device_to_domain(struct r8a7779_pm_domain *r8a7779_pd,
  160. struct platform_device *pdev)
  161. {
  162. struct device *dev = &pdev->dev;
  163. pm_genpd_add_device(&r8a7779_pd->genpd, dev);
  164. if (pm_clk_no_clocks(dev))
  165. pm_clk_add(dev, NULL);
  166. }
  167. struct r8a7779_pm_domain r8a7779_sh4a = {
  168. .ch = {
  169. .chan_offs = 0x80, /* PWRSR1 .. PWRER1 */
  170. .isr_bit = 16, /* SH4A */
  171. }
  172. };
  173. struct r8a7779_pm_domain r8a7779_sgx = {
  174. .ch = {
  175. .chan_offs = 0xc0, /* PWRSR2 .. PWRER2 */
  176. .isr_bit = 20, /* SGX */
  177. }
  178. };
  179. struct r8a7779_pm_domain r8a7779_vdp1 = {
  180. .ch = {
  181. .chan_offs = 0x100, /* PWRSR3 .. PWRER3 */
  182. .isr_bit = 21, /* VDP */
  183. }
  184. };
  185. struct r8a7779_pm_domain r8a7779_impx3 = {
  186. .ch = {
  187. .chan_offs = 0x140, /* PWRSR4 .. PWRER4 */
  188. .isr_bit = 24, /* IMP */
  189. }
  190. };
  191. #endif /* CONFIG_PM */
  192. void __init r8a7779_pm_init(void)
  193. {
  194. static int once;
  195. if (!once++)
  196. r8a7779_sysc_init();
  197. }