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"
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
, "timeout wait pmu cmd\n");
28 pmu_write(LIMA_PMU_INT_CLEAR
, LIMA_PMU_INT_CMD_MASK
);
32 static u32
lima_pmu_get_ip_mask(struct lima_ip
*ip
)
34 struct lima_device
*dev
= ip
->dev
;
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
);
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
;
55 for (i
= lima_ip_pp4
; i
<= lima_ip_pp7
; i
++) {
56 if (dev
->ip
[i
].present
) {
57 ret
|= LIMA450_PMU_POWER_PP47_MASK
;
66 static int lima_pmu_hw_init(struct lima_ip
*ip
)
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
);
83 pmu_write(LIMA_PMU_POWER_UP
, stat
);
84 err
= lima_pmu_wait_cmd(ip
);
91 static void lima_pmu_hw_fini(struct lima_ip
*ip
)
96 ip
->data
.mask
= lima_pmu_get_ip_mask(ip
);
98 stat
= ~pmu_read(LIMA_PMU_STATUS
) & ip
->data
.mask
;
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
106 if (ip
->dev
->id
== lima_gpu_mali400
)
107 pmu_write(LIMA_PMU_INT_CLEAR
, LIMA_PMU_INT_CMD_MASK
);
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
);