pmu_com.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef PMU_COM_H
  7. #define PMU_COM_H
  8. #ifndef CHECK_CPU_WFIE_BASE
  9. #define CHECK_CPU_WFIE_BASE (PMU_BASE + PMU_CORE_PWR_ST)
  10. #endif
  11. /*
  12. * Use this macro to instantiate lock before it is used in below
  13. * rockchip_pd_lock_xxx() macros
  14. */
  15. DECLARE_BAKERY_LOCK(rockchip_pd_lock);
  16. /*
  17. * These are wrapper macros to the powe domain Bakery Lock API.
  18. */
  19. #define rockchip_pd_lock_init() bakery_lock_init(&rockchip_pd_lock)
  20. #define rockchip_pd_lock_get() bakery_lock_get(&rockchip_pd_lock)
  21. #define rockchip_pd_lock_rls() bakery_lock_release(&rockchip_pd_lock)
  22. /*****************************************************************************
  23. * power domain on or off
  24. *****************************************************************************/
  25. enum pmu_pd_state {
  26. pmu_pd_on = 0,
  27. pmu_pd_off = 1
  28. };
  29. #pragma weak plat_ic_get_pending_interrupt_id
  30. #pragma weak pmu_power_domain_ctr
  31. #pragma weak check_cpu_wfie
  32. static inline uint32_t pmu_power_domain_st(uint32_t pd)
  33. {
  34. uint32_t pwrdn_st = mmio_read_32(PMU_BASE + PMU_PWRDN_ST) & BIT(pd);
  35. if (pwrdn_st)
  36. return pmu_pd_off;
  37. else
  38. return pmu_pd_on;
  39. }
  40. static int pmu_power_domain_ctr(uint32_t pd, uint32_t pd_state)
  41. {
  42. uint32_t val;
  43. uint32_t loop = 0;
  44. int ret = 0;
  45. rockchip_pd_lock_get();
  46. val = mmio_read_32(PMU_BASE + PMU_PWRDN_CON);
  47. if (pd_state == pmu_pd_off)
  48. val |= BIT(pd);
  49. else
  50. val &= ~BIT(pd);
  51. mmio_write_32(PMU_BASE + PMU_PWRDN_CON, val);
  52. dsb();
  53. while ((pmu_power_domain_st(pd) != pd_state) && (loop < PD_CTR_LOOP)) {
  54. udelay(1);
  55. loop++;
  56. }
  57. if (pmu_power_domain_st(pd) != pd_state) {
  58. WARN("%s: %d, %d, error!\n", __func__, pd, pd_state);
  59. ret = -EINVAL;
  60. }
  61. rockchip_pd_lock_rls();
  62. return ret;
  63. }
  64. static int check_cpu_wfie(uint32_t cpu_id, uint32_t wfie_msk)
  65. {
  66. uint32_t cluster_id, loop = 0;
  67. if (cpu_id >= PLATFORM_CLUSTER0_CORE_COUNT) {
  68. cluster_id = 1;
  69. cpu_id -= PLATFORM_CLUSTER0_CORE_COUNT;
  70. } else {
  71. cluster_id = 0;
  72. }
  73. /*
  74. * wfe/wfi tracking not possible, hopefully the host
  75. * was successful in enabling wfe/wfi.
  76. * We'll give a bit of additional time, like the kernel does.
  77. */
  78. if ((cluster_id && clstb_cpu_wfe < 0) ||
  79. (!cluster_id && clstl_cpu_wfe < 0)) {
  80. mdelay(1);
  81. return 0;
  82. }
  83. if (cluster_id)
  84. wfie_msk <<= (clstb_cpu_wfe + cpu_id);
  85. else
  86. wfie_msk <<= (clstl_cpu_wfe + cpu_id);
  87. while (!(mmio_read_32(CHECK_CPU_WFIE_BASE) & wfie_msk) &&
  88. (loop < CHK_CPU_LOOP)) {
  89. udelay(1);
  90. loop++;
  91. }
  92. if ((mmio_read_32(CHECK_CPU_WFIE_BASE) & wfie_msk) == 0) {
  93. WARN("%s: %d, %d, %d, error!\n", __func__,
  94. cluster_id, cpu_id, wfie_msk);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. #endif /* PMU_COM_H */