pm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010-2013
  4. * Author: Rickard Andersson <rickard.andersson@stericsson.com> for
  5. * ST-Ericsson.
  6. * Author: Daniel Lezcano <daniel.lezcano@linaro.org> for Linaro.
  7. * Author: Ulf Hansson <ulf.hansson@linaro.org> for Linaro.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/irqchip/arm-gic.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/suspend.h>
  14. #include <linux/platform_data/arm-ux500-pm.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. /* ARM WFI Standby signal register */
  18. #define PRCM_ARM_WFI_STANDBY (prcmu_base + 0x130)
  19. #define PRCM_ARM_WFI_STANDBY_WFI0 0x08
  20. #define PRCM_ARM_WFI_STANDBY_WFI1 0x10
  21. #define PRCM_IOCR (prcmu_base + 0x310)
  22. #define PRCM_IOCR_IOFORCE 0x1
  23. /* Dual A9 core interrupt management unit registers */
  24. #define PRCM_A9_MASK_REQ (prcmu_base + 0x328)
  25. #define PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ 0x1
  26. #define PRCM_A9_MASK_ACK (prcmu_base + 0x32c)
  27. #define PRCM_ARMITMSK31TO0 (prcmu_base + 0x11c)
  28. #define PRCM_ARMITMSK63TO32 (prcmu_base + 0x120)
  29. #define PRCM_ARMITMSK95TO64 (prcmu_base + 0x124)
  30. #define PRCM_ARMITMSK127TO96 (prcmu_base + 0x128)
  31. #define PRCM_POWER_STATE_VAL (prcmu_base + 0x25C)
  32. #define PRCM_ARMITVAL31TO0 (prcmu_base + 0x260)
  33. #define PRCM_ARMITVAL63TO32 (prcmu_base + 0x264)
  34. #define PRCM_ARMITVAL95TO64 (prcmu_base + 0x268)
  35. #define PRCM_ARMITVAL127TO96 (prcmu_base + 0x26C)
  36. static void __iomem *prcmu_base;
  37. static void __iomem *dist_base;
  38. /* This function decouple the gic from the prcmu */
  39. int prcmu_gic_decouple(void)
  40. {
  41. u32 val = readl(PRCM_A9_MASK_REQ);
  42. /* Set bit 0 register value to 1 */
  43. writel(val | PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ,
  44. PRCM_A9_MASK_REQ);
  45. /* Make sure the register is updated */
  46. readl(PRCM_A9_MASK_REQ);
  47. /* Wait a few cycles for the gic mask completion */
  48. udelay(1);
  49. return 0;
  50. }
  51. /* This function recouple the gic with the prcmu */
  52. int prcmu_gic_recouple(void)
  53. {
  54. u32 val = readl(PRCM_A9_MASK_REQ);
  55. /* Set bit 0 register value to 0 */
  56. writel(val & ~PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ, PRCM_A9_MASK_REQ);
  57. return 0;
  58. }
  59. #define PRCMU_GIC_NUMBER_REGS 5
  60. /*
  61. * This function checks if there are pending irq on the gic. It only
  62. * makes sense if the gic has been decoupled before with the
  63. * db8500_prcmu_gic_decouple function. Disabling an interrupt only
  64. * disables the forwarding of the interrupt to any CPU interface. It
  65. * does not prevent the interrupt from changing state, for example
  66. * becoming pending, or active and pending if it is already
  67. * active. Hence, we have to check the interrupt is pending *and* is
  68. * active.
  69. */
  70. bool prcmu_gic_pending_irq(void)
  71. {
  72. u32 pr; /* Pending register */
  73. u32 er; /* Enable register */
  74. int i;
  75. /* 5 registers. STI & PPI not skipped */
  76. for (i = 0; i < PRCMU_GIC_NUMBER_REGS; i++) {
  77. pr = readl_relaxed(dist_base + GIC_DIST_PENDING_SET + i * 4);
  78. er = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  79. if (pr & er)
  80. return true; /* There is a pending interrupt */
  81. }
  82. return false;
  83. }
  84. /*
  85. * This function checks if there are pending interrupt on the
  86. * prcmu which has been delegated to monitor the irqs with the
  87. * db8500_prcmu_copy_gic_settings function.
  88. */
  89. bool prcmu_pending_irq(void)
  90. {
  91. u32 it, im;
  92. int i;
  93. for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
  94. it = readl(PRCM_ARMITVAL31TO0 + i * 4);
  95. im = readl(PRCM_ARMITMSK31TO0 + i * 4);
  96. if (it & im)
  97. return true; /* There is a pending interrupt */
  98. }
  99. return false;
  100. }
  101. /*
  102. * This function checks if the specified cpu is in WFI. It's usage
  103. * makes sense only if the gic is decoupled with the db8500_prcmu_gic_decouple
  104. * function. Of course passing smp_processor_id() to this function will
  105. * always return false...
  106. */
  107. bool prcmu_is_cpu_in_wfi(int cpu)
  108. {
  109. return readl(PRCM_ARM_WFI_STANDBY) &
  110. (cpu ? PRCM_ARM_WFI_STANDBY_WFI1 : PRCM_ARM_WFI_STANDBY_WFI0);
  111. }
  112. /*
  113. * This function copies the gic SPI settings to the prcmu in order to
  114. * monitor them and abort/finish the retention/off sequence or state.
  115. */
  116. int prcmu_copy_gic_settings(void)
  117. {
  118. u32 er; /* Enable register */
  119. int i;
  120. /* We skip the STI and PPI */
  121. for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
  122. er = readl_relaxed(dist_base +
  123. GIC_DIST_ENABLE_SET + (i + 1) * 4);
  124. writel(er, PRCM_ARMITMSK31TO0 + i * 4);
  125. }
  126. return 0;
  127. }
  128. #ifdef CONFIG_SUSPEND
  129. static int ux500_suspend_enter(suspend_state_t state)
  130. {
  131. cpu_do_idle();
  132. return 0;
  133. }
  134. static int ux500_suspend_valid(suspend_state_t state)
  135. {
  136. return state == PM_SUSPEND_MEM || state == PM_SUSPEND_STANDBY;
  137. }
  138. static const struct platform_suspend_ops ux500_suspend_ops = {
  139. .enter = ux500_suspend_enter,
  140. .valid = ux500_suspend_valid,
  141. };
  142. #define UX500_SUSPEND_OPS (&ux500_suspend_ops)
  143. #else
  144. #define UX500_SUSPEND_OPS NULL
  145. #endif
  146. void __init ux500_pm_init(u32 phy_base, u32 size)
  147. {
  148. struct device_node *np;
  149. prcmu_base = ioremap(phy_base, size);
  150. if (!prcmu_base) {
  151. pr_err("could not remap PRCMU for PM functions\n");
  152. return;
  153. }
  154. np = of_find_compatible_node(NULL, NULL, "arm,cortex-a9-gic");
  155. dist_base = of_iomap(np, 0);
  156. of_node_put(np);
  157. if (!dist_base) {
  158. pr_err("could not remap GIC dist base for PM functions\n");
  159. return;
  160. }
  161. /*
  162. * On watchdog reboot the GIC is in some cases decoupled.
  163. * This will make sure that the GIC is correctly configured.
  164. */
  165. prcmu_gic_recouple();
  166. /* Set up ux500 suspend callbacks. */
  167. suspend_set_ops(UX500_SUSPEND_OPS);
  168. }