Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / lima / lima_pmu.c
blobe397e1146e96314808b4964576fc419988b11053
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
4 #include <linux/iopoll.h>
5 #include <linux/device.h>
7 #include "lima_device.h"
8 #include "lima_pmu.h"
9 #include "lima_regs.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;
17 int err;
18 u32 v;
20 err = readl_poll_timeout(ip->iomem + LIMA_PMU_INT_RAWSTAT,
21 v, v & LIMA_PMU_INT_CMD_MASK,
22 100, 100000);
23 if (err) {
24 dev_err(dev->dev, "timeout wait pmu cmd\n");
25 return err;
28 pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
29 return 0;
32 static u32 lima_pmu_get_ip_mask(struct lima_ip *ip)
34 struct lima_device *dev = ip->dev;
35 u32 ret = 0;
36 int i;
38 ret |= LIMA_PMU_POWER_GP0_MASK;
40 if (dev->id == lima_gpu_mali400) {
41 ret |= LIMA_PMU_POWER_L2_MASK;
42 for (i = 0; i < 4; i++) {
43 if (dev->ip[lima_ip_pp0 + i].present)
44 ret |= LIMA_PMU_POWER_PP_MASK(i);
46 } else {
47 if (dev->ip[lima_ip_pp0].present)
48 ret |= LIMA450_PMU_POWER_PP0_MASK;
49 for (i = lima_ip_pp1; i <= lima_ip_pp3; i++) {
50 if (dev->ip[i].present) {
51 ret |= LIMA450_PMU_POWER_PP13_MASK;
52 break;
55 for (i = lima_ip_pp4; i <= lima_ip_pp7; i++) {
56 if (dev->ip[i].present) {
57 ret |= LIMA450_PMU_POWER_PP47_MASK;
58 break;
63 return ret;
66 static int lima_pmu_hw_init(struct lima_ip *ip)
68 int err;
69 u32 stat;
71 pmu_write(LIMA_PMU_INT_MASK, 0);
73 /* If this value is too low, when in high GPU clk freq,
74 * GPU will be in unstable state.
76 pmu_write(LIMA_PMU_SW_DELAY, 0xffff);
78 /* status reg 1=off 0=on */
79 stat = pmu_read(LIMA_PMU_STATUS);
81 /* power up all ip */
82 if (stat) {
83 pmu_write(LIMA_PMU_POWER_UP, stat);
84 err = lima_pmu_wait_cmd(ip);
85 if (err)
86 return err;
88 return 0;
91 static void lima_pmu_hw_fini(struct lima_ip *ip)
93 u32 stat;
95 if (!ip->data.mask)
96 ip->data.mask = lima_pmu_get_ip_mask(ip);
98 stat = ~pmu_read(LIMA_PMU_STATUS) & ip->data.mask;
99 if (stat) {
100 pmu_write(LIMA_PMU_POWER_DOWN, stat);
102 /* Don't wait for interrupt on Mali400 if all domains are
103 * powered off because the HW won't generate an interrupt
104 * in this case.
106 if (ip->dev->id == lima_gpu_mali400)
107 pmu_write(LIMA_PMU_INT_CLEAR, LIMA_PMU_INT_CMD_MASK);
108 else
109 lima_pmu_wait_cmd(ip);
113 int lima_pmu_resume(struct lima_ip *ip)
115 return lima_pmu_hw_init(ip);
118 void lima_pmu_suspend(struct lima_ip *ip)
120 lima_pmu_hw_fini(ip);
123 int lima_pmu_init(struct lima_ip *ip)
125 return lima_pmu_hw_init(ip);
128 void lima_pmu_fini(struct lima_ip *ip)
130 lima_pmu_hw_fini(ip);