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