summaryrefslogtreecommitdiffstats
path: root/plat/mediatek/mt8183/drivers/mcdi/mtk_mcdi.c
blob: 29eebcb98f91b5fbb2eca6846dc465ebc415bd0b (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
/*
 * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <lib/mmio.h>
#include <sspm_reg.h>
#include <mtk_mcdi.h>

static inline uint32_t mcdi_mbox_read(uint32_t id)
{
	return mmio_read_32(SSPM_MBOX_3_BASE + (id << 2));
}

static inline void mcdi_mbox_write(uint32_t id, uint32_t val)
{
	mmio_write_32(SSPM_MBOX_3_BASE + (id << 2), val);
}

void sspm_set_bootaddr(uint32_t bootaddr)
{
	mcdi_mbox_write(MCDI_MBOX_BOOTADDR, bootaddr);
}

void sspm_cluster_pwr_off_notify(uint32_t cluster)
{
	mcdi_mbox_write(MCDI_MBOX_CLUSTER_0_ATF_ACTION_DONE + cluster, 1);
}

void sspm_cluster_pwr_on_notify(uint32_t cluster)
{
	mcdi_mbox_write(MCDI_MBOX_CLUSTER_0_ATF_ACTION_DONE + cluster, 0);
}

void sspm_standbywfi_irq_enable(uint32_t cpu_idx)
{
	mmio_write_32(SSPM_CFGREG_ACAO_INT_SET, STANDBYWFI_EN(cpu_idx));
}

uint32_t mcdi_avail_cpu_mask_read(void)
{
	return mcdi_mbox_read(MCDI_MBOX_AVAIL_CPU_MASK);
}

uint32_t mcdi_avail_cpu_mask_write(uint32_t mask)
{
	mcdi_mbox_write(MCDI_MBOX_AVAIL_CPU_MASK, mask);

	return mask;
}

uint32_t mcdi_avail_cpu_mask_set(uint32_t mask)
{
	uint32_t m;

	m = mcdi_mbox_read(MCDI_MBOX_AVAIL_CPU_MASK);
	m |= mask;
	mcdi_mbox_write(MCDI_MBOX_AVAIL_CPU_MASK, m);

	return m;
}

uint32_t mcdi_avail_cpu_mask_clr(uint32_t mask)
{
	uint32_t m;

	m = mcdi_mbox_read(MCDI_MBOX_AVAIL_CPU_MASK);
	m &= ~mask;
	mcdi_mbox_write(MCDI_MBOX_AVAIL_CPU_MASK, m);

	return m;
}

uint32_t mcdi_cpu_cluster_pwr_stat_read(void)
{
	return mcdi_mbox_read(MCDI_MBOX_CPU_CLUSTER_PWR_STAT);
}

#define PAUSE_BIT		1
#define CLUSTER_OFF_OFS		20
#define CPU_OFF_OFS		24
#define CLUSTER_ON_OFS		4
#define CPU_ON_OFS		8

static uint32_t target_mask(int cluster, int cpu_idx, bool on)
{
	uint32_t t = 0;

	if (on) {
		if (cluster >= 0)
			t |= BIT(cluster + CLUSTER_ON_OFS);

		if (cpu_idx >= 0)
			t |= BIT(cpu_idx + CPU_ON_OFS);
	} else {
		if (cluster >= 0)
			t |= BIT(cluster + CLUSTER_OFF_OFS);

		if (cpu_idx >= 0)
			t |= BIT(cpu_idx + CPU_OFF_OFS);
	}

	return t;
}

void mcdi_pause_clr(int cluster, int cpu_idx, bool on)
{
	uint32_t tgt = target_mask(cluster, cpu_idx, on);
	uint32_t m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION);

	m &= ~tgt;
	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);
}

void mcdi_pause_set(int cluster, int cpu_idx, bool on)
{
	uint32_t tgt = target_mask(cluster, cpu_idx, on);
	uint32_t m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION);
	uint32_t tgtn = target_mask(-1, cpu_idx, !on);

	/* request on and off at the same time to ensure it can be paused */
	m |= tgt | tgtn;
	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);

	/* wait pause_ack */
	while (!mcdi_mbox_read(MCDI_MBOX_PAUSE_ACK))
		;

	/* clear non-requested operation */
	m &= ~tgtn;
	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);
}

void mcdi_pause(void)
{
	uint32_t m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION) | BIT(PAUSE_BIT);

	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);

	/* wait pause_ack */
	while (!mcdi_mbox_read(MCDI_MBOX_PAUSE_ACK))
		;
}

void mcdi_unpause(void)
{
	uint32_t m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION) & ~BIT(PAUSE_BIT);

	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);
}

void mcdi_hotplug_wait_ack(int cluster, int cpu_idx, bool on)
{
	uint32_t tgt = target_mask(cluster, cpu_idx, on);
	uint32_t ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);

	/* wait until ack */
	while (!(ack & tgt))
		ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);
}

void mcdi_hotplug_clr(int cluster, int cpu_idx, bool on)
{
	uint32_t tgt = target_mask(cluster, cpu_idx, on);
	uint32_t tgt_cpu = target_mask(-1, cpu_idx, on);
	uint32_t cmd = mcdi_mbox_read(MCDI_MBOX_HP_CMD);
	uint32_t ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);

	if (!(cmd & tgt))
		return;

	/* wait until ack */
	while (!(ack & tgt_cpu))
		ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);

	cmd &= ~tgt;
	mcdi_mbox_write(MCDI_MBOX_HP_CMD, cmd);
}

void mcdi_hotplug_set(int cluster, int cpu_idx, bool on)
{
	uint32_t tgt = target_mask(cluster, cpu_idx, on);
	uint32_t tgt_cpu = target_mask(-1, cpu_idx, on);
	uint32_t cmd = mcdi_mbox_read(MCDI_MBOX_HP_CMD);
	uint32_t ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);

	if ((cmd & tgt) == tgt)
		return;

	/* wait until ack clear */
	while (ack & tgt_cpu)
		ack = mcdi_mbox_read(MCDI_MBOX_HP_ACK);

	cmd |= tgt;
	mcdi_mbox_write(MCDI_MBOX_HP_CMD, cmd);
}

bool check_mcdi_ctl_stat(void)
{
	uint32_t clk_regs[] = {0x100010ac, 0x100010c8};
	uint32_t clk_mask[] = {0x00028000, 0x00000018};
	uint32_t tgt = target_mask(0, 0, true);
	uint32_t m;
	int i;

	/* check clk status */
	for (i = 0; i < ARRAY_SIZE(clk_regs); i++) {
		if (mmio_read_32(clk_regs[i]) & clk_mask[i]) {
			WARN("mcdi: clk check fail.\n");
			return false;
		}
	}

	/* check mcdi cmd handling */
	m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION) | BIT(PAUSE_BIT);
	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);

	i = 500;
	while (!mcdi_mbox_read(MCDI_MBOX_PAUSE_ACK) && --i > 0)
		udelay(10);

	m = mcdi_mbox_read(MCDI_MBOX_PAUSE_ACTION) & ~BIT(PAUSE_BIT);
	mcdi_mbox_write(MCDI_MBOX_PAUSE_ACTION, m);

	if (i == 0) {
		WARN("mcdi: pause_action fail.\n");
		return false;
	}

	/* check mcdi cmd handling */
	if (mcdi_mbox_read(MCDI_MBOX_HP_CMD) ||
			mcdi_mbox_read(MCDI_MBOX_HP_ACK)) {
		WARN("mcdi: hp_cmd fail.\n");
		return false;
	}

	mcdi_mbox_write(MCDI_MBOX_HP_CMD, tgt);

	i = 500;
	while ((mcdi_mbox_read(MCDI_MBOX_HP_ACK) & tgt) != tgt && --i > 0)
		udelay(10);

	mcdi_mbox_write(MCDI_MBOX_HP_CMD, 0);

	if (i == 0) {
		WARN("mcdi: hp_ack fail.\n");
		return false;
	}

	return true;
}

void mcdi_init(void)
{
	mcdi_avail_cpu_mask_write(0x01); /* cpu0 default on */
}