1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 #include <linux/iopoll.h>
5 #include <linux/device.h>
7 #include "lima_device.h"
11 #define pmu_write(reg, data) writel(data, ip->iomem + reg)
12 #define pmu_read(reg) readl(ip->iomem + reg)
14 static int lima_pmu_wait_cmd(struct lima_ip *ip)
16 struct lima_device *dev = ip->dev;
20 err = readl_poll_timeout(ip->iomem + LIMA_PMU_INT_RAWSTAT,
21 v, v & LIMA_PMU_INT_CMD_MASK,
24 dev_err(dev->dev, "%s timeout wait pmu cmd\n",
29 pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
33 static u32 lima_pmu_get_ip_mask(struct lima_ip *ip)
35 struct lima_device *dev = ip->dev;
39 ret |= LIMA_PMU_POWER_GP0_MASK;
41 if (dev->id == lima_gpu_mali400) {
42 ret |= LIMA_PMU_POWER_L2_MASK;
43 for (i = 0; i < 4; i++) {
44 if (dev->ip[lima_ip_pp0 + i].present)
45 ret |= LIMA_PMU_POWER_PP_MASK(i);
48 if (dev->ip[lima_ip_pp0].present)
49 ret |= LIMA450_PMU_POWER_PP0_MASK;
50 for (i = lima_ip_pp1; i <= lima_ip_pp3; i++) {
51 if (dev->ip[i].present) {
52 ret |= LIMA450_PMU_POWER_PP13_MASK;
56 for (i = lima_ip_pp4; i <= lima_ip_pp7; i++) {
57 if (dev->ip[i].present) {
58 ret |= LIMA450_PMU_POWER_PP47_MASK;
67 static int lima_pmu_hw_init(struct lima_ip *ip)
72 pmu_write(LIMA_PMU_INT_MASK, 0);
74 /* If this value is too low, when in high GPU clk freq,
75 * GPU will be in unstable state.
77 pmu_write(LIMA_PMU_SW_DELAY, 0xffff);
79 /* status reg 1=off 0=on */
80 stat = pmu_read(LIMA_PMU_STATUS);
84 pmu_write(LIMA_PMU_POWER_UP, stat);
85 err = lima_pmu_wait_cmd(ip);
92 static void lima_pmu_hw_fini(struct lima_ip *ip)
97 ip->data.mask = lima_pmu_get_ip_mask(ip);
99 stat = ~pmu_read(LIMA_PMU_STATUS) & ip->data.mask;
101 pmu_write(LIMA_PMU_POWER_DOWN, stat);
103 /* Don't wait for interrupt on Mali400 if all domains are
104 * powered off because the HW won't generate an interrupt
107 if (ip->dev->id == lima_gpu_mali400)
108 pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
110 lima_pmu_wait_cmd(ip);
114 int lima_pmu_resume(struct lima_ip *ip)
116 return lima_pmu_hw_init(ip);
119 void lima_pmu_suspend(struct lima_ip *ip)
121 lima_pmu_hw_fini(ip);
124 int lima_pmu_init(struct lima_ip *ip)
126 return lima_pmu_hw_init(ip);
129 void lima_pmu_fini(struct lima_ip *ip)
131 lima_pmu_hw_fini(ip);