summaryrefslogtreecommitdiff
path: root/plat/mediatek/drivers/spm/mt8188/constraints/mt_spm_rc_bus26m.c
blob: 0b792ab1bc08362791cf4b0d5f387969188cf2f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 2023, MediaTek Inc. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <common/debug.h>
#ifndef MTK_PLAT_CIRQ_UNSUPPORT
#include <mtk_cirq.h>
#endif
#include <drivers/spm/mt_spm_resource_req.h>
#include <lib/pm/mtk_pm.h>
#include <lpm/mt_lp_rm.h>
#include <mt_spm.h>
#include <mt_spm_cond.h>
#include <mt_spm_conservation.h>
#include <mt_spm_constraint.h>
#include <mt_spm_idle.h>
#include <mt_spm_internal.h>
#include <mt_spm_notifier.h>
#include "mt_spm_rc_api.h"
#include "mt_spm_rc_internal.h"
#include <mt_spm_reg.h>
#include <mt_spm_suspend.h>

#define CONSTRAINT_BUS26M_ALLOW (MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF | \
				 MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \
				 MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \
				 MT_RM_CONSTRAINT_ALLOW_VCORE_LP | \
				 MT_RM_CONSTRAINT_ALLOW_LVTS_STATE | \
				 MT_RM_CONSTRAINT_ALLOW_BUS26M_OFF)

#define CONSTRAINT_BUS26M_PCM_FLAG (SPM_FLAG_DISABLE_INFRA_PDN | \
				    SPM_FLAG_DISABLE_VCORE_DVS | \
				    SPM_FLAG_DISABLE_VCORE_DFS | \
				    SPM_FLAG_SRAM_SLEEP_CTRL | \
				    SPM_FLAG_ENABLE_LVTS_WORKAROUND | \
				    SPM_FLAG_KEEP_CSYSPWRACK_HIGH | \
				    SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP)

#define CONSTRAINT_BUS26M_PCM_FLAG1 (SPM_FLAG1_DISABLE_PWRAP_CLK_SWITCH)

/* If sspm sram won't enter sleep voltage then vcore couldn't enter low power mode */
#if defined(MTK_PLAT_SPM_SRAM_SLP_UNSUPPORT) && SPM_SRAM_SLEEP_RC_RES_RESTRICT
#define CONSTRAINT_BUS26M_RESOURCE_REQ	(MT_SPM_26M)
#else
#define CONSTRAINT_BUS26M_RESOURCE_REQ	(0)
#endif

static unsigned int bus26m_ext_opand;
static unsigned int bus26m_ext_opand2;

static struct mt_irqremain *refer2remain_irq;

static struct mt_spm_cond_tables cond_bus26m = {
	.table_cg = {
		0xFF5DD002,	/* MTCMOS1 */
		0x0000003C,	/* MTCMOS2 */
		0x27AF8000,	/* INFRA0  */
		0x22010876,	/* INFRA1  */
		0x86000650,	/* INFRA2  */
		0x30008020,	/* INFRA3  */
		0x80000000,	/* INFRA4  */
		0x01002A3B,	/* PERI0   */
		0x00090000,	/* VPPSYS0_0  */
		0x38FF3E69,     /* VPPSYS0_1  */
		0xF0081450,	/* VPPSYS1_0  */
		0x00003000,     /* VPPSYS1_1  */
		0x00000000,	/* VDOSYS0_0  */
		0x00000000,     /* VDOSYS0_1  */
		0x000001FF,	/* VDOSYS1_0  */
		0x000001E0,     /* VDOSYS1_1  */
		0x00FB0007,	/* VDOSYS1_2  */
	},
	.table_pll = (PLL_BIT_UNIVPLL |
		      PLL_BIT_MFGPLL |
		      PLL_BIT_MSDCPLL |
		      PLL_BIT_TVDPLL1 |
		      PLL_BIT_TVDPLL2 |
		      PLL_BIT_MMPLL |
		      PLL_BIT_ETHPLL |
		      PLL_BIT_IMGPLL |
		      PLL_BIT_APLL1 |
		      PLL_BIT_APLL2 |
		      PLL_BIT_APLL3 |
		      PLL_BIT_APLL4 |
		      PLL_BIT_APLL5),
};

static struct mt_spm_cond_tables cond_bus26m_res = {
	.table_cg = { 0U },
	.table_pll = 0U,
};

static struct constraint_status status = {
	.id = MT_RM_CONSTRAINT_ID_BUS26M,
	.is_valid = (MT_SPM_RC_VALID_SW |
		     MT_SPM_RC_VALID_COND_CHECK |
		     MT_SPM_RC_VALID_COND_LATCH |
		     MT_SPM_RC_VALID_TRACE_TIME),
	.is_cond_block = 0U,
	.enter_cnt = 0U,
	.all_pll_dump = 0U,
	.cond_res = &cond_bus26m_res,
	.residency = 0ULL,
};

#ifdef MTK_PLAT_CIRQ_UNSUPPORT
#define do_irqs_delivery()
#else
static void mt_spm_irq_remain_dump(struct mt_irqremain *irqs,
				   unsigned int irq_index,
				   struct wake_status *wakeup)
{
	if ((irqs == NULL) || (wakeup == NULL)) {
		return;
	}

	INFO("[SPM] r12=0x%08x(0x%08x), flag=0x%08x 0x%08x 0x%08x, irq:%u(0x%08x) set pending\n",
	     wakeup->tr.comm.r12,
	     wakeup->md32pcm_wakeup_sta,
	     wakeup->tr.comm.debug_flag,
	     wakeup->tr.comm.b_sw_flag0,
	     wakeup->tr.comm.b_sw_flag1,
	     irqs->wakeupsrc[irq_index],
	     irqs->irqs[irq_index]);
}

static void do_irqs_delivery(void)
{
	unsigned int idx;
	struct wake_status *wakeup = NULL;
	struct mt_irqremain *irqs = refer2remain_irq;

	if (irqs == NULL) {
		return;
	}

	if (spm_conservation_get_result(&wakeup) == 0) {
		if (wakeup != NULL) {
			for (idx = 0; idx < irqs->count; idx++) {
				if (((wakeup->tr.comm.r12 & irqs->wakeupsrc[idx]) != 0U) ||
				    ((wakeup->tr.comm.raw_sta & irqs->wakeupsrc[idx]) != 0U)) {
					if ((irqs->wakeupsrc_cat[idx] &
					     MT_IRQ_REMAIN_CAT_LOG) != 0U) {
						mt_spm_irq_remain_dump(irqs, idx, wakeup);
					}
					mt_irq_set_pending(irqs->irqs[idx]);
				}
			}
		}
	}
}
#endif

int spm_bus26m_conduct(int state_id, struct spm_lp_scen *spm_lp, unsigned int *resource_req)
{
	unsigned int res_req = CONSTRAINT_BUS26M_RESOURCE_REQ;

	if ((spm_lp == NULL) || (resource_req == NULL)) {
		return -1;
	}

	spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG;
	spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG1;

	*resource_req |= res_req;
	return 0;
}

bool spm_is_valid_rc_bus26m(unsigned int cpu, int state_id)
{
	return (!(status.is_cond_block && (status.is_valid & MT_SPM_RC_VALID_COND_CHECK) > 0) &&
		IS_MT_RM_RC_READY(status.is_valid) &&
		(IS_PLAT_SUSPEND_ID(state_id) || (state_id == MT_PLAT_PWR_STATE_SYSTEM_BUS)));
}

static int update_rc_condition(const void *val)
{
	const struct mt_spm_cond_tables *tlb = (const struct mt_spm_cond_tables *)val;
	const struct mt_spm_cond_tables *tlb_check =
		(const struct mt_spm_cond_tables *)&cond_bus26m;

	if (tlb == NULL) {
		return MT_RM_STATUS_BAD;
	}

	status.is_cond_block = mt_spm_cond_check(tlb, tlb_check,
						 (status.is_valid & MT_SPM_RC_VALID_COND_LATCH) ?
						 &cond_bus26m_res : NULL);
	status.all_pll_dump = mt_spm_dump_all_pll(tlb, tlb_check,
						  (status.is_valid & MT_SPM_RC_VALID_COND_LATCH) ?
						  &cond_bus26m_res : NULL);
	return MT_RM_STATUS_OK;
}

static void update_rc_remain_irqs(const void *val)
{
	refer2remain_irq = (struct mt_irqremain *)val;
}

static void update_rc_fmaudio_adsp(int type, const void *val)
{
	int *flag = (int *)val;
	unsigned int ext_op = (type == PLAT_RC_IS_ADSP) ?
			      (MT_SPM_EX_OP_SET_IS_ADSP | MT_SPM_EX_OP_SET_SUSPEND_MODE) :
			      MT_SPM_EX_OP_SET_SUSPEND_MODE;

	if (flag == NULL) {
		return;
	}

	if (*flag != 0) {
		SPM_RC_BITS_SET(bus26m_ext_opand, ext_op);
	} else {
		SPM_RC_BITS_CLR(bus26m_ext_opand, ext_op);
	}
}

static void update_rc_usb_peri(const void *val)
{
	int *flag = (int *)val;

	if (flag == NULL) {
		return;
	}

	if (*flag != 0) {
		SPM_RC_BITS_SET(bus26m_ext_opand2, MT_SPM_EX_OP_PERI_ON);
	} else {
		SPM_RC_BITS_CLR(bus26m_ext_opand2, MT_SPM_EX_OP_PERI_ON);
	}
}

static void update_rc_usb_infra(const void *val)
{
	int *flag = (int *)val;

	if (flag == NULL) {
		return;
	}

	if (*flag != 0) {
		SPM_RC_BITS_SET(bus26m_ext_opand2, MT_SPM_EX_OP_INFRA_ON);
	} else {
		SPM_RC_BITS_CLR(bus26m_ext_opand2, MT_SPM_EX_OP_INFRA_ON);
	}
}

static void update_rc_status(const void *val)
{
	const struct rc_common_state *st;

	st = (const struct rc_common_state *)val;

	if (st == NULL) {
		return;
	}

	if (st->type == CONSTRAINT_UPDATE_COND_CHECK) {
		struct mt_spm_cond_tables * const tlb = &cond_bus26m;

		spm_rc_condition_modifier(st->id, st->act, st->value,
					  MT_RM_CONSTRAINT_ID_BUS26M, tlb);
	} else if ((st->type == CONSTRAINT_UPDATE_VALID) ||
		   (st->type == CONSTRAINT_RESIDNECY)) {
		spm_rc_constraint_status_set(st->id, st->type, st->act,
					     MT_RM_CONSTRAINT_ID_BUS26M,
					     (struct constraint_status * const)st->value,
					     (struct constraint_status * const)&status);
	} else {
		INFO("[%s:%d] - Unknown type: 0x%x\n", __func__, __LINE__, st->type);
	}
}

int spm_update_rc_bus26m(int state_id, int type, const void *val)
{
	int res = MT_RM_STATUS_OK;

	switch (type) {
	case PLAT_RC_UPDATE_CONDITION:
		res = update_rc_condition(val);
		break;
	case PLAT_RC_UPDATE_REMAIN_IRQS:
		update_rc_remain_irqs(val);
		break;
	case PLAT_RC_IS_FMAUDIO:
	case PLAT_RC_IS_ADSP:
		update_rc_fmaudio_adsp(type, val);
		break;
	case PLAT_RC_IS_USB_PERI:
		update_rc_usb_peri(val);
		break;
	case PLAT_RC_IS_USB_INFRA:
		update_rc_usb_infra(val);
		break;
	case PLAT_RC_STATUS:
		update_rc_status(val);
		break;
	default:
		INFO("[%s:%d] - Do nothing for type: %d\n", __func__, __LINE__, type);
		break;
	}
	return res;
}

unsigned int spm_allow_rc_bus26m(int state_id)
{
	return CONSTRAINT_BUS26M_ALLOW;
}

int spm_run_rc_bus26m(unsigned int cpu, int state_id)
{
	unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;

#ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
	mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, CONSTRAINT_BUS26M_ALLOW |
			       (IS_PLAT_SUSPEND_ID(state_id) ?
				MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0));
#endif
	if (status.is_valid & MT_SPM_RC_VALID_TRACE_TIME) {
		ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN;
	}

	if (IS_PLAT_SUSPEND_ID(state_id)) {
		mt_spm_suspend_enter(state_id,
				     (MT_SPM_EX_OP_CLR_26M_RECORD |
				      MT_SPM_EX_OP_SET_WDT |
				      MT_SPM_EX_OP_HW_S1_DETECT |
				      bus26m_ext_opand |
				      bus26m_ext_opand2),
				     CONSTRAINT_BUS26M_RESOURCE_REQ);
	} else {
		mt_spm_idle_generic_enter(state_id, ext_op, spm_bus26m_conduct);
	}
	return MT_RM_STATUS_OK;
}

int spm_reset_rc_bus26m(unsigned int cpu, int state_id)
{
	unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;

#ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
	mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, 0);
#endif
	if (status.is_valid & MT_SPM_RC_VALID_TRACE_TIME) {
		ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN;
	}

	if (IS_PLAT_SUSPEND_ID(state_id)) {
		mt_spm_suspend_resume(state_id,
				      (bus26m_ext_opand | bus26m_ext_opand2 |
				       MT_SPM_EX_OP_SET_WDT | ext_op),
				      NULL);
		bus26m_ext_opand = 0;
	} else {
		struct wake_status *waken = NULL;

		if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_TRACE_EVENT)) {
			ext_op |= MT_SPM_EX_OP_TRACE_LP;
		}

		mt_spm_idle_generic_resume(state_id, ext_op, &waken, NULL);
		status.enter_cnt++;

		if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_RESIDNECY)) {
			status.residency += (waken != NULL) ? waken->tr.comm.timer_out : 0;
		}
	}

	do_irqs_delivery();

	return MT_RM_STATUS_OK;
}

int spm_get_status_rc_bus26m(unsigned int type, void *priv)
{
	int ret = MT_RM_STATUS_OK;

	if (type == PLAT_RC_STATUS) {
		int res = 0;
		struct rc_common_state *st = (struct rc_common_state *)priv;

		if (st == NULL) {
			return MT_RM_STATUS_BAD;
		}

		res = spm_rc_constraint_status_get(st->id, st->type,
						   st->act, MT_RM_CONSTRAINT_ID_BUS26M,
						   (struct constraint_status * const)&status,
						   (struct constraint_status * const)st->value);
		if ((res == 0) && (st->id != MT_RM_CONSTRAINT_ID_ALL)) {
			ret = MT_RM_STATUS_STOP;
		}
	}
	return ret;
}