lima_pmu.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /* Copyright 2017-2019 Qiang Yu <[email protected]> */
  3. #include <linux/iopoll.h>
  4. #include <linux/device.h>
  5. #include "lima_device.h"
  6. #include "lima_pmu.h"
  7. #include "lima_regs.h"
  8. #define pmu_write(reg, data) writel(data, ip->iomem + reg)
  9. #define pmu_read(reg) readl(ip->iomem + reg)
  10. static int lima_pmu_wait_cmd(struct lima_ip *ip)
  11. {
  12. struct lima_device *dev = ip->dev;
  13. int err;
  14. u32 v;
  15. err = readl_poll_timeout(ip->iomem + LIMA_PMU_INT_RAWSTAT,
  16. v, v & LIMA_PMU_INT_CMD_MASK,
  17. 100, 100000);
  18. if (err) {
  19. dev_err(dev->dev, "timeout wait pmu cmd\n");
  20. return err;
  21. }
  22. pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
  23. return 0;
  24. }
  25. static u32 lima_pmu_get_ip_mask(struct lima_ip *ip)
  26. {
  27. struct lima_device *dev = ip->dev;
  28. u32 ret = 0;
  29. int i;
  30. ret |= LIMA_PMU_POWER_GP0_MASK;
  31. if (dev->id == lima_gpu_mali400) {
  32. ret |= LIMA_PMU_POWER_L2_MASK;
  33. for (i = 0; i < 4; i++) {
  34. if (dev->ip[lima_ip_pp0 + i].present)
  35. ret |= LIMA_PMU_POWER_PP_MASK(i);
  36. }
  37. } else {
  38. if (dev->ip[lima_ip_pp0].present)
  39. ret |= LIMA450_PMU_POWER_PP0_MASK;
  40. for (i = lima_ip_pp1; i <= lima_ip_pp3; i++) {
  41. if (dev->ip[i].present) {
  42. ret |= LIMA450_PMU_POWER_PP13_MASK;
  43. break;
  44. }
  45. }
  46. for (i = lima_ip_pp4; i <= lima_ip_pp7; i++) {
  47. if (dev->ip[i].present) {
  48. ret |= LIMA450_PMU_POWER_PP47_MASK;
  49. break;
  50. }
  51. }
  52. }
  53. return ret;
  54. }
  55. static int lima_pmu_hw_init(struct lima_ip *ip)
  56. {
  57. int err;
  58. u32 stat;
  59. pmu_write(LIMA_PMU_INT_MASK, 0);
  60. /* If this value is too low, when in high GPU clk freq,
  61. * GPU will be in unstable state.
  62. */
  63. pmu_write(LIMA_PMU_SW_DELAY, 0xffff);
  64. /* status reg 1=off 0=on */
  65. stat = pmu_read(LIMA_PMU_STATUS);
  66. /* power up all ip */
  67. if (stat) {
  68. pmu_write(LIMA_PMU_POWER_UP, stat);
  69. err = lima_pmu_wait_cmd(ip);
  70. if (err)
  71. return err;
  72. }
  73. return 0;
  74. }
  75. static void lima_pmu_hw_fini(struct lima_ip *ip)
  76. {
  77. u32 stat;
  78. if (!ip->data.mask)
  79. ip->data.mask = lima_pmu_get_ip_mask(ip);
  80. stat = ~pmu_read(LIMA_PMU_STATUS) & ip->data.mask;
  81. if (stat) {
  82. pmu_write(LIMA_PMU_POWER_DOWN, stat);
  83. /* Don't wait for interrupt on Mali400 if all domains are
  84. * powered off because the HW won't generate an interrupt
  85. * in this case.
  86. */
  87. if (ip->dev->id == lima_gpu_mali400)
  88. pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
  89. else
  90. lima_pmu_wait_cmd(ip);
  91. }
  92. }
  93. int lima_pmu_resume(struct lima_ip *ip)
  94. {
  95. return lima_pmu_hw_init(ip);
  96. }
  97. void lima_pmu_suspend(struct lima_ip *ip)
  98. {
  99. lima_pmu_hw_fini(ip);
  100. }
  101. int lima_pmu_init(struct lima_ip *ip)
  102. {
  103. return lima_pmu_hw_init(ip);
  104. }
  105. void lima_pmu_fini(struct lima_ip *ip)
  106. {
  107. lima_pmu_hw_fini(ip);
  108. }