aml_scpi.c 6.11 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8
#include <crypto/sha_dma.h>
9
10
11
12
13
#include <lib/mmio.h>
#include <plat/common/platform.h>
#include <platform_def.h>
#include <string.h>

14
#include "aml_private.h"
15
16
17

#define SIZE_SHIFT	20
#define SIZE_MASK	0x1FF
18
#define SIZE_FWBLK	0x200UL
19
20
21
22
23
24
25
26
27

/*
 * Note: The Amlogic SCP firmware uses the legacy SCPI protocol.
 */
#define SCPI_CMD_SET_CSS_POWER_STATE	0x04
#define SCPI_CMD_SET_SYS_POWER_STATE	0x08

#define SCPI_CMD_JTAG_SET_STATE		0xC0
#define SCPI_CMD_EFUSE_READ		0xC2
28
#define SCPI_CMD_CHIP_ID		0xC6
29

30
31
32
33
#define SCPI_CMD_COPY_FW 0xd4
#define SCPI_CMD_SET_FW_ADDR 0xd3
#define SCPI_CMD_FW_SIZE 0xd2

34
static inline uint32_t aml_scpi_cmd(uint32_t command, uint32_t size)
35
36
37
38
{
	return command | (size << SIZE_SHIFT);
}

39
static void aml_scpi_secure_message_send(uint32_t command, uint32_t size)
40
{
41
	aml_mhu_secure_message_send(aml_scpi_cmd(command, size));
42
43
}

44
static uint32_t aml_scpi_secure_message_receive(void **message_out, size_t *size_out)
45
{
46
	uint32_t response = aml_mhu_secure_message_wait();
47
48
49
50
51
52
53
54
55

	size_t size = (response >> SIZE_SHIFT) & SIZE_MASK;

	response &= ~(SIZE_MASK << SIZE_SHIFT);

	if (size_out != NULL)
		*size_out = size;

	if (message_out != NULL)
56
		*message_out = (void *)AML_MHU_SECURE_SCP_TO_AP_PAYLOAD;
57
58
59
60

	return response;
}

61
void aml_scpi_set_css_power_state(u_register_t mpidr, uint32_t cpu_state,
62
63
64
65
66
67
68
69
			      uint32_t cluster_state, uint32_t css_state)
{
	uint32_t state = (mpidr & 0x0F) | /* CPU ID */
			 ((mpidr & 0xF00) >> 4) | /* Cluster ID */
			 (cpu_state << 8) |
			 (cluster_state << 12) |
			 (css_state << 16);

70
71
	aml_mhu_secure_message_start();
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, state);
72
	aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_SET_CSS_POWER_STATE, 4));
73
74
	aml_mhu_secure_message_wait();
	aml_mhu_secure_message_end();
75
76
}

77
uint32_t aml_scpi_sys_power_state(uint64_t system_state)
78
79
80
81
{
	uint32_t *response;
	size_t size;

82
83
	aml_mhu_secure_message_start();
	mmio_write_8(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, system_state);
84
85
	aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_SET_SYS_POWER_STATE, 1));
	aml_scpi_secure_message_receive((void *)&response, &size);
86
	aml_mhu_secure_message_end();
87
88
89
90

	return *response;
}

91
void aml_scpi_jtag_set_state(uint32_t state, uint8_t select)
92
{
93
	assert(state <= AML_JTAG_STATE_OFF);
94

95
	if (select > AML_JTAG_A53_EE) {
96
97
98
99
		WARN("BL31: Invalid JTAG select (0x%x).\n", select);
		return;
	}

100
101
	aml_mhu_secure_message_start();
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD,
102
		      (state << 8) | (uint32_t)select);
103
	aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_JTAG_SET_STATE, 4));
104
105
	aml_mhu_secure_message_wait();
	aml_mhu_secure_message_end();
106
107
}

108
uint32_t aml_scpi_efuse_read(void *dst, uint32_t base, uint32_t size)
109
110
111
112
113
114
115
{
	uint32_t *response;
	size_t resp_size;

	if (size > 0x1FC)
		return 0;

116
117
118
	aml_mhu_secure_message_start();
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, base);
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 4, size);
119
120
	aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_EFUSE_READ, 8));
	aml_scpi_secure_message_receive((void *)&response, &resp_size);
121
	aml_mhu_secure_message_end();
122
123
124
125
126
127
128
129
130
131
132

	/*
	 * response[0] is the size of the response message.
	 * response[1 ... N] are the contents.
	 */
	if (*response != 0)
		memcpy(dst, response + 1, *response);

	return *response;
}

133
134
void aml_scpi_unknown_thermal(uint32_t arg0, uint32_t arg1,
			      uint32_t arg2, uint32_t arg3)
135
{
136
137
138
139
140
	aml_mhu_secure_message_start();
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x0, arg0);
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x4, arg1);
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x8, arg2);
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0xC, arg3);
141
	aml_mhu_secure_message_send(aml_scpi_cmd(0xC3, 16));
142
143
	aml_mhu_secure_message_wait();
	aml_mhu_secure_message_end();
144
}
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
uint32_t aml_scpi_get_chip_id(uint8_t *obuff, uint32_t osize)
{
	uint32_t *response;
	size_t resp_size;

	if ((osize != 16) && (osize != 12))
		return 0;

	aml_mhu_secure_message_start();
	aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_CHIP_ID, osize));
	aml_scpi_secure_message_receive((void *)&response, &resp_size);
	aml_mhu_secure_message_end();

	if (!((resp_size == 16) && (osize == 16)) &&
	    !((resp_size == 0) && (osize == 12)))
		return 0;

	memcpy((void *)obuff, (const void *)response, osize);

	return osize;
}

168
static inline void aml_scpi_copy_scp_data(uint8_t *data, size_t len)
169
{
170
	void *dst = (void *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD;
171
172
	size_t sz;

173
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, len);
174
	aml_scpi_secure_message_send(SCPI_CMD_FW_SIZE, len);
175
	aml_mhu_secure_message_wait();
176
177
178

	for (sz = 0; sz < len; sz += SIZE_FWBLK) {
		memcpy(dst, data + sz, MIN(SIZE_FWBLK, len - sz));
179
		aml_mhu_secure_message_send(SCPI_CMD_COPY_FW);
180
181
182
	}
}

183
static inline void aml_scpi_set_scp_addr(uint64_t addr, size_t len)
184
{
185
	volatile uint64_t *dst = (uint64_t *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD;
186
187

	/*
188
	 * It is ok as AML_MHU_SECURE_AP_TO_SCP_PAYLOAD is mapped as
189
190
191
	 * non cachable
	 */
	*dst = addr;
192
	aml_scpi_secure_message_send(SCPI_CMD_SET_FW_ADDR, sizeof(addr));
193
	aml_mhu_secure_message_wait();
194

195
	mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, len);
196
	aml_scpi_secure_message_send(SCPI_CMD_FW_SIZE, len);
197
	aml_mhu_secure_message_wait();
198
199
}

200
static inline void aml_scpi_send_fw_hash(uint8_t hash[], size_t len)
201
{
202
	void *dst = (void *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD;
203
204

	memcpy(dst, hash, len);
205
206
207
208
	aml_mhu_secure_message_send(0xd0);
	aml_mhu_secure_message_send(0xd1);
	aml_mhu_secure_message_send(0xd5);
	aml_mhu_secure_message_end();
209
210
211
212
213
214
215
216
217
218
}

/**
 * Upload a FW to SCP.
 *
 * @param addr: firmware data address
 * @param size: size of firmware
 * @param send: If set, actually copy the firmware in SCP memory otherwise only
 *  send the firmware address.
 */
219
void aml_scpi_upload_scp_fw(uintptr_t addr, size_t size, int send)
220
221
222
223
224
225
226
{
	struct asd_ctx ctx;

	asd_sha_init(&ctx, ASM_SHA256);
	asd_sha_update(&ctx, (void *)addr, size);
	asd_sha_finalize(&ctx);

227
	aml_mhu_secure_message_start();
228
	if (send == 0)
229
		aml_scpi_set_scp_addr(addr, size);
230
	else
231
		aml_scpi_copy_scp_data((void *)addr, size);
232

233
	aml_scpi_send_fw_hash(ctx.digest, sizeof(ctx.digest));
234
}