1*06cb65efSGarmin.Chang /*
2*06cb65efSGarmin.Chang * Copyright (c) 2021, MediaTek Inc. All rights reserved.
3*06cb65efSGarmin.Chang *
4*06cb65efSGarmin.Chang * SPDX-License-Identifier: BSD-3-Clause
5*06cb65efSGarmin.Chang */
6*06cb65efSGarmin.Chang
7*06cb65efSGarmin.Chang #include <string.h>
8*06cb65efSGarmin.Chang
9*06cb65efSGarmin.Chang #include <drivers/delay_timer.h>
10*06cb65efSGarmin.Chang
11*06cb65efSGarmin.Chang #include <mt_cpu_pm_cpc.h>
12*06cb65efSGarmin.Chang #include <mt_timer.h>
13*06cb65efSGarmin.Chang
14*06cb65efSGarmin.Chang struct mtk_cpc_dev {
15*06cb65efSGarmin.Chang int auto_off;
16*06cb65efSGarmin.Chang unsigned int auto_thres_tick;
17*06cb65efSGarmin.Chang };
18*06cb65efSGarmin.Chang
19*06cb65efSGarmin.Chang static struct mtk_cpc_dev cpc;
20*06cb65efSGarmin.Chang
mtk_cpc_last_core_prot(uint32_t prot_req,uint32_t resp_reg,uint32_t resp_ofs)21*06cb65efSGarmin.Chang static int mtk_cpc_last_core_prot(uint32_t prot_req,
22*06cb65efSGarmin.Chang uint32_t resp_reg, uint32_t resp_ofs)
23*06cb65efSGarmin.Chang {
24*06cb65efSGarmin.Chang uint32_t sta, retry;
25*06cb65efSGarmin.Chang
26*06cb65efSGarmin.Chang retry = 0U;
27*06cb65efSGarmin.Chang
28*06cb65efSGarmin.Chang while (retry++ < RETRY_CNT_MAX) {
29*06cb65efSGarmin.Chang
30*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_LAST_CORE_REQ, prot_req);
31*06cb65efSGarmin.Chang
32*06cb65efSGarmin.Chang udelay(1U);
33*06cb65efSGarmin.Chang
34*06cb65efSGarmin.Chang sta = (mmio_read_32(resp_reg) >> resp_ofs) & CPC_PROT_RESP_MASK;
35*06cb65efSGarmin.Chang
36*06cb65efSGarmin.Chang if (sta == PROT_SUCCESS) {
37*06cb65efSGarmin.Chang return CPC_SUCCESS;
38*06cb65efSGarmin.Chang } else if (sta == PROT_GIVEUP) {
39*06cb65efSGarmin.Chang return CPC_ERR_FAIL;
40*06cb65efSGarmin.Chang }
41*06cb65efSGarmin.Chang }
42*06cb65efSGarmin.Chang
43*06cb65efSGarmin.Chang return CPC_ERR_TIMEOUT;
44*06cb65efSGarmin.Chang }
45*06cb65efSGarmin.Chang
mtk_cpu_pm_mcusys_prot_aquire(void)46*06cb65efSGarmin.Chang int mtk_cpu_pm_mcusys_prot_aquire(void)
47*06cb65efSGarmin.Chang {
48*06cb65efSGarmin.Chang return mtk_cpc_last_core_prot(
49*06cb65efSGarmin.Chang MCUSYS_PROT_SET,
50*06cb65efSGarmin.Chang CPC_MCUSYS_LAST_CORE_RESP,
51*06cb65efSGarmin.Chang MCUSYS_RESP_OFS);
52*06cb65efSGarmin.Chang }
53*06cb65efSGarmin.Chang
mtk_cpu_pm_mcusys_prot_release(void)54*06cb65efSGarmin.Chang void mtk_cpu_pm_mcusys_prot_release(void)
55*06cb65efSGarmin.Chang {
56*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_PWR_ON_MASK, MCUSYS_PROT_CLR);
57*06cb65efSGarmin.Chang }
58*06cb65efSGarmin.Chang
mtk_cpu_pm_cluster_prot_aquire(unsigned int cluster)59*06cb65efSGarmin.Chang int mtk_cpu_pm_cluster_prot_aquire(unsigned int cluster)
60*06cb65efSGarmin.Chang {
61*06cb65efSGarmin.Chang return mtk_cpc_last_core_prot(
62*06cb65efSGarmin.Chang CPUSYS_PROT_SET,
63*06cb65efSGarmin.Chang CPC_MCUSYS_MP_LAST_CORE_RESP,
64*06cb65efSGarmin.Chang CPUSYS_RESP_OFS);
65*06cb65efSGarmin.Chang }
66*06cb65efSGarmin.Chang
mtk_cpu_pm_cluster_prot_release(unsigned int cluster)67*06cb65efSGarmin.Chang void mtk_cpu_pm_cluster_prot_release(unsigned int cluster)
68*06cb65efSGarmin.Chang {
69*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_PWR_ON_MASK, CPUSYS_PROT_CLR);
70*06cb65efSGarmin.Chang }
71*06cb65efSGarmin.Chang
mtk_cpc_cluster_cnt_backup(void)72*06cb65efSGarmin.Chang static void mtk_cpc_cluster_cnt_backup(void)
73*06cb65efSGarmin.Chang {
74*06cb65efSGarmin.Chang uint32_t backup_cnt;
75*06cb65efSGarmin.Chang uint32_t curr_cnt;
76*06cb65efSGarmin.Chang uint32_t cnt_mask = GENMASK(14, 0);
77*06cb65efSGarmin.Chang uint32_t clr_mask = GENMASK(1, 0);
78*06cb65efSGarmin.Chang
79*06cb65efSGarmin.Chang /* Single Cluster */
80*06cb65efSGarmin.Chang backup_cnt = mmio_read_32(CPC_CLUSTER_CNT_BACKUP);
81*06cb65efSGarmin.Chang curr_cnt = mmio_read_32(CPC_MCUSYS_CLUSTER_COUNTER);
82*06cb65efSGarmin.Chang
83*06cb65efSGarmin.Chang /* Get off count if dormant count is 0 */
84*06cb65efSGarmin.Chang if ((curr_cnt & cnt_mask) == 0U) {
85*06cb65efSGarmin.Chang curr_cnt = (curr_cnt >> 16) & cnt_mask;
86*06cb65efSGarmin.Chang } else {
87*06cb65efSGarmin.Chang curr_cnt = curr_cnt & cnt_mask;
88*06cb65efSGarmin.Chang }
89*06cb65efSGarmin.Chang
90*06cb65efSGarmin.Chang mmio_write_32(CPC_CLUSTER_CNT_BACKUP, backup_cnt + curr_cnt);
91*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CLUSTER_COUNTER_CLR, clr_mask);
92*06cb65efSGarmin.Chang }
93*06cb65efSGarmin.Chang
mtk_cpc_mcusys_off_en(void)94*06cb65efSGarmin.Chang static inline void mtk_cpc_mcusys_off_en(void)
95*06cb65efSGarmin.Chang {
96*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_PWR_CTRL, 1U);
97*06cb65efSGarmin.Chang }
98*06cb65efSGarmin.Chang
mtk_cpc_mcusys_off_dis(void)99*06cb65efSGarmin.Chang static inline void mtk_cpc_mcusys_off_dis(void)
100*06cb65efSGarmin.Chang {
101*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_PWR_CTRL, 0U);
102*06cb65efSGarmin.Chang }
103*06cb65efSGarmin.Chang
mtk_cpc_mcusys_off_reflect(void)104*06cb65efSGarmin.Chang void mtk_cpc_mcusys_off_reflect(void)
105*06cb65efSGarmin.Chang {
106*06cb65efSGarmin.Chang mtk_cpc_mcusys_off_dis();
107*06cb65efSGarmin.Chang mtk_cpu_pm_mcusys_prot_release();
108*06cb65efSGarmin.Chang }
109*06cb65efSGarmin.Chang
mtk_cpc_mcusys_off_prepare(void)110*06cb65efSGarmin.Chang int mtk_cpc_mcusys_off_prepare(void)
111*06cb65efSGarmin.Chang {
112*06cb65efSGarmin.Chang if (mtk_cpu_pm_mcusys_prot_aquire() != CPC_SUCCESS) {
113*06cb65efSGarmin.Chang return CPC_ERR_FAIL;
114*06cb65efSGarmin.Chang }
115*06cb65efSGarmin.Chang
116*06cb65efSGarmin.Chang mtk_cpc_cluster_cnt_backup();
117*06cb65efSGarmin.Chang mtk_cpc_mcusys_off_en();
118*06cb65efSGarmin.Chang
119*06cb65efSGarmin.Chang return CPC_SUCCESS;
120*06cb65efSGarmin.Chang }
121*06cb65efSGarmin.Chang
mtk_cpc_core_on_hint_set(unsigned int cpu)122*06cb65efSGarmin.Chang void mtk_cpc_core_on_hint_set(unsigned int cpu)
123*06cb65efSGarmin.Chang {
124*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPU_ON_SW_HINT_SET, BIT(cpu));
125*06cb65efSGarmin.Chang }
126*06cb65efSGarmin.Chang
mtk_cpc_core_on_hint_clr(unsigned int cpu)127*06cb65efSGarmin.Chang void mtk_cpc_core_on_hint_clr(unsigned int cpu)
128*06cb65efSGarmin.Chang {
129*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPU_ON_SW_HINT_CLR, BIT(cpu));
130*06cb65efSGarmin.Chang }
131*06cb65efSGarmin.Chang
mtk_cpc_dump_timestamp(void)132*06cb65efSGarmin.Chang static void mtk_cpc_dump_timestamp(void)
133*06cb65efSGarmin.Chang {
134*06cb65efSGarmin.Chang uint32_t id;
135*06cb65efSGarmin.Chang
136*06cb65efSGarmin.Chang for (id = 0U; id < CPC_TRACE_ID_NUM; id++) {
137*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_TRACE_SEL, id);
138*06cb65efSGarmin.Chang
139*06cb65efSGarmin.Chang memcpy((void *)(uintptr_t)CPC_TRACE_SRAM(id),
140*06cb65efSGarmin.Chang (const void *)(uintptr_t)CPC_MCUSYS_TRACE_DATA,
141*06cb65efSGarmin.Chang CPC_TRACE_SIZE);
142*06cb65efSGarmin.Chang }
143*06cb65efSGarmin.Chang }
144*06cb65efSGarmin.Chang
mtk_cpc_time_sync(void)145*06cb65efSGarmin.Chang void mtk_cpc_time_sync(void)
146*06cb65efSGarmin.Chang {
147*06cb65efSGarmin.Chang uint64_t kt;
148*06cb65efSGarmin.Chang uint32_t systime_l, systime_h;
149*06cb65efSGarmin.Chang
150*06cb65efSGarmin.Chang kt = sched_clock();
151*06cb65efSGarmin.Chang systime_l = mmio_read_32(CNTSYS_L_REG);
152*06cb65efSGarmin.Chang systime_h = mmio_read_32(CNTSYS_H_REG);
153*06cb65efSGarmin.Chang
154*06cb65efSGarmin.Chang /* sync kernel timer to cpc */
155*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_KERNEL_TIME_L_BASE, (uint32_t)kt);
156*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_KERNEL_TIME_H_BASE, (uint32_t)(kt >> 32));
157*06cb65efSGarmin.Chang /* sync system timer to cpc */
158*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_SYSTEM_TIME_L_BASE, systime_l);
159*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_SYSTEM_TIME_H_BASE, systime_h);
160*06cb65efSGarmin.Chang }
161*06cb65efSGarmin.Chang
mtk_cpc_config(uint32_t cfg,uint32_t data)162*06cb65efSGarmin.Chang static void mtk_cpc_config(uint32_t cfg, uint32_t data)
163*06cb65efSGarmin.Chang {
164*06cb65efSGarmin.Chang uint32_t val;
165*06cb65efSGarmin.Chang uint32_t reg = 0U;
166*06cb65efSGarmin.Chang
167*06cb65efSGarmin.Chang switch (cfg) {
168*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_PROF:
169*06cb65efSGarmin.Chang reg = CPC_MCUSYS_CPC_DBG_SETTING;
170*06cb65efSGarmin.Chang val = mmio_read_32(reg);
171*06cb65efSGarmin.Chang val = (data != 0U) ? (val | CPC_PROF_EN) : (val & ~CPC_PROF_EN);
172*06cb65efSGarmin.Chang break;
173*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_AUTO_OFF:
174*06cb65efSGarmin.Chang reg = CPC_MCUSYS_CPC_FLOW_CTRL_CFG;
175*06cb65efSGarmin.Chang val = mmio_read_32(reg);
176*06cb65efSGarmin.Chang if (data != 0U) {
177*06cb65efSGarmin.Chang val |= CPC_AUTO_OFF_EN;
178*06cb65efSGarmin.Chang cpc.auto_off = 1;
179*06cb65efSGarmin.Chang } else {
180*06cb65efSGarmin.Chang val &= ~CPC_AUTO_OFF_EN;
181*06cb65efSGarmin.Chang cpc.auto_off = 0;
182*06cb65efSGarmin.Chang }
183*06cb65efSGarmin.Chang break;
184*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_AUTO_OFF_THRES:
185*06cb65efSGarmin.Chang reg = CPC_MCUSYS_CPC_OFF_THRES;
186*06cb65efSGarmin.Chang cpc.auto_thres_tick = us_to_ticks(data);
187*06cb65efSGarmin.Chang val = cpc.auto_thres_tick;
188*06cb65efSGarmin.Chang break;
189*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_CNT_CLR:
190*06cb65efSGarmin.Chang reg = CPC_MCUSYS_CLUSTER_COUNTER_CLR;
191*06cb65efSGarmin.Chang val = GENMASK(1, 0); /* clr_mask */
192*06cb65efSGarmin.Chang break;
193*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_TIME_SYNC:
194*06cb65efSGarmin.Chang mtk_cpc_time_sync();
195*06cb65efSGarmin.Chang break;
196*06cb65efSGarmin.Chang default:
197*06cb65efSGarmin.Chang break;
198*06cb65efSGarmin.Chang }
199*06cb65efSGarmin.Chang
200*06cb65efSGarmin.Chang if (reg != 0U) {
201*06cb65efSGarmin.Chang mmio_write_32(reg, val);
202*06cb65efSGarmin.Chang }
203*06cb65efSGarmin.Chang }
204*06cb65efSGarmin.Chang
mtk_cpc_read_config(uint32_t cfg)205*06cb65efSGarmin.Chang static uint32_t mtk_cpc_read_config(uint32_t cfg)
206*06cb65efSGarmin.Chang {
207*06cb65efSGarmin.Chang uint32_t res = 0U;
208*06cb65efSGarmin.Chang
209*06cb65efSGarmin.Chang switch (cfg) {
210*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_PROF:
211*06cb65efSGarmin.Chang res = (mmio_read_32(CPC_MCUSYS_CPC_DBG_SETTING) & CPC_PROF_EN) ?
212*06cb65efSGarmin.Chang 1U : 0U;
213*06cb65efSGarmin.Chang break;
214*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_AUTO_OFF:
215*06cb65efSGarmin.Chang res = cpc.auto_off;
216*06cb65efSGarmin.Chang break;
217*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_AUTO_OFF_THRES:
218*06cb65efSGarmin.Chang res = ticks_to_us(cpc.auto_thres_tick);
219*06cb65efSGarmin.Chang break;
220*06cb65efSGarmin.Chang case CPC_SMC_CONFIG_CNT_CLR:
221*06cb65efSGarmin.Chang break;
222*06cb65efSGarmin.Chang default:
223*06cb65efSGarmin.Chang break;
224*06cb65efSGarmin.Chang }
225*06cb65efSGarmin.Chang
226*06cb65efSGarmin.Chang return res;
227*06cb65efSGarmin.Chang }
228*06cb65efSGarmin.Chang
mtk_cpc_handler(uint64_t act,uint64_t arg1,uint64_t arg2)229*06cb65efSGarmin.Chang uint64_t mtk_cpc_handler(uint64_t act, uint64_t arg1, uint64_t arg2)
230*06cb65efSGarmin.Chang {
231*06cb65efSGarmin.Chang uint64_t res = 0ULL;
232*06cb65efSGarmin.Chang
233*06cb65efSGarmin.Chang switch (act) {
234*06cb65efSGarmin.Chang case CPC_SMC_EVENT_DUMP_TRACE_DATA:
235*06cb65efSGarmin.Chang mtk_cpc_dump_timestamp();
236*06cb65efSGarmin.Chang break;
237*06cb65efSGarmin.Chang case CPC_SMC_EVENT_GIC_DPG_SET:
238*06cb65efSGarmin.Chang /* isolated_status = x2; */
239*06cb65efSGarmin.Chang break;
240*06cb65efSGarmin.Chang case CPC_SMC_EVENT_CPC_CONFIG:
241*06cb65efSGarmin.Chang mtk_cpc_config((uint32_t)arg1, (uint32_t)arg2);
242*06cb65efSGarmin.Chang break;
243*06cb65efSGarmin.Chang case CPC_SMC_EVENT_READ_CONFIG:
244*06cb65efSGarmin.Chang res = mtk_cpc_read_config((uint32_t)arg1);
245*06cb65efSGarmin.Chang break;
246*06cb65efSGarmin.Chang default:
247*06cb65efSGarmin.Chang break;
248*06cb65efSGarmin.Chang }
249*06cb65efSGarmin.Chang
250*06cb65efSGarmin.Chang return res;
251*06cb65efSGarmin.Chang }
252*06cb65efSGarmin.Chang
mtk_cpc_init(void)253*06cb65efSGarmin.Chang void mtk_cpc_init(void)
254*06cb65efSGarmin.Chang {
255*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_DBG_SETTING,
256*06cb65efSGarmin.Chang mmio_read_32(CPC_MCUSYS_CPC_DBG_SETTING)
257*06cb65efSGarmin.Chang | CPC_DBG_EN
258*06cb65efSGarmin.Chang | CPC_CALC_EN);
259*06cb65efSGarmin.Chang
260*06cb65efSGarmin.Chang cpc.auto_off = 1;
261*06cb65efSGarmin.Chang cpc.auto_thres_tick = us_to_ticks(8000);
262*06cb65efSGarmin.Chang
263*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_FLOW_CTRL_CFG,
264*06cb65efSGarmin.Chang mmio_read_32(CPC_MCUSYS_CPC_FLOW_CTRL_CFG)
265*06cb65efSGarmin.Chang | CPC_OFF_PRE_EN
266*06cb65efSGarmin.Chang | (cpc.auto_off ? CPC_AUTO_OFF_EN : 0U));
267*06cb65efSGarmin.Chang
268*06cb65efSGarmin.Chang mmio_write_32(CPC_MCUSYS_CPC_OFF_THRES, cpc.auto_thres_tick);
269*06cb65efSGarmin.Chang }
270