Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 1 | /* |
Carlo Caione | 4948832 | 2019-08-24 17:28:23 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Carlo Caione | 4948832 | 2019-08-24 17:28:23 +0100 | [diff] [blame] | 8 | #include <crypto/sha_dma.h> |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 9 | #include <lib/mmio.h> |
| 10 | #include <plat/common/platform.h> |
| 11 | #include <platform_def.h> |
| 12 | #include <string.h> |
| 13 | |
Carlo Caione | 4948832 | 2019-08-24 17:28:23 +0100 | [diff] [blame] | 14 | #include "aml_private.h" |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 15 | |
| 16 | #define SIZE_SHIFT 20 |
| 17 | #define SIZE_MASK 0x1FF |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 18 | #define SIZE_FWBLK 0x200UL |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Note: The Amlogic SCP firmware uses the legacy SCPI protocol. |
| 22 | */ |
| 23 | #define SCPI_CMD_SET_CSS_POWER_STATE 0x04 |
| 24 | #define SCPI_CMD_SET_SYS_POWER_STATE 0x08 |
| 25 | |
| 26 | #define SCPI_CMD_JTAG_SET_STATE 0xC0 |
| 27 | #define SCPI_CMD_EFUSE_READ 0xC2 |
Carlo Caione | f7c4f9b | 2019-09-16 12:13:49 +0100 | [diff] [blame] | 28 | #define SCPI_CMD_CHIP_ID 0xC6 |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 29 | |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 30 | #define SCPI_CMD_COPY_FW 0xd4 |
| 31 | #define SCPI_CMD_SET_FW_ADDR 0xd3 |
| 32 | #define SCPI_CMD_FW_SIZE 0xd2 |
| 33 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 34 | static inline uint32_t aml_scpi_cmd(uint32_t command, uint32_t size) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 35 | { |
| 36 | return command | (size << SIZE_SHIFT); |
| 37 | } |
| 38 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 39 | static void aml_scpi_secure_message_send(uint32_t command, uint32_t size) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 40 | { |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 41 | aml_mhu_secure_message_send(aml_scpi_cmd(command, size)); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 44 | static uint32_t aml_scpi_secure_message_receive(void **message_out, size_t *size_out) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 45 | { |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 46 | uint32_t response = aml_mhu_secure_message_wait(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 47 | |
| 48 | size_t size = (response >> SIZE_SHIFT) & SIZE_MASK; |
| 49 | |
| 50 | response &= ~(SIZE_MASK << SIZE_SHIFT); |
| 51 | |
| 52 | if (size_out != NULL) |
| 53 | *size_out = size; |
| 54 | |
| 55 | if (message_out != NULL) |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 56 | *message_out = (void *)AML_MHU_SECURE_SCP_TO_AP_PAYLOAD; |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 57 | |
| 58 | return response; |
| 59 | } |
| 60 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 61 | void aml_scpi_set_css_power_state(u_register_t mpidr, uint32_t cpu_state, |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 62 | uint32_t cluster_state, uint32_t css_state) |
| 63 | { |
| 64 | uint32_t state = (mpidr & 0x0F) | /* CPU ID */ |
| 65 | ((mpidr & 0xF00) >> 4) | /* Cluster ID */ |
| 66 | (cpu_state << 8) | |
| 67 | (cluster_state << 12) | |
| 68 | (css_state << 16); |
| 69 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 70 | aml_mhu_secure_message_start(); |
| 71 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, state); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 72 | aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_SET_CSS_POWER_STATE, 4)); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 73 | aml_mhu_secure_message_wait(); |
| 74 | aml_mhu_secure_message_end(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 77 | uint32_t aml_scpi_sys_power_state(uint64_t system_state) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 78 | { |
| 79 | uint32_t *response; |
| 80 | size_t size; |
| 81 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 82 | aml_mhu_secure_message_start(); |
| 83 | mmio_write_8(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, system_state); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 84 | aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_SET_SYS_POWER_STATE, 1)); |
| 85 | aml_scpi_secure_message_receive((void *)&response, &size); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 86 | aml_mhu_secure_message_end(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 87 | |
| 88 | return *response; |
| 89 | } |
| 90 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 91 | void aml_scpi_jtag_set_state(uint32_t state, uint8_t select) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 92 | { |
Carlo Caione | 107df3e | 2019-08-26 13:04:12 +0100 | [diff] [blame] | 93 | assert(state <= AML_JTAG_STATE_OFF); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 94 | |
Carlo Caione | 107df3e | 2019-08-26 13:04:12 +0100 | [diff] [blame] | 95 | if (select > AML_JTAG_A53_EE) { |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 96 | WARN("BL31: Invalid JTAG select (0x%x).\n", select); |
| 97 | return; |
| 98 | } |
| 99 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 100 | aml_mhu_secure_message_start(); |
| 101 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 102 | (state << 8) | (uint32_t)select); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 103 | aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_JTAG_SET_STATE, 4)); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 104 | aml_mhu_secure_message_wait(); |
| 105 | aml_mhu_secure_message_end(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 108 | uint32_t aml_scpi_efuse_read(void *dst, uint32_t base, uint32_t size) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 109 | { |
| 110 | uint32_t *response; |
| 111 | size_t resp_size; |
| 112 | |
| 113 | if (size > 0x1FC) |
| 114 | return 0; |
| 115 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 116 | aml_mhu_secure_message_start(); |
| 117 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, base); |
| 118 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 4, size); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 119 | aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_EFUSE_READ, 8)); |
| 120 | aml_scpi_secure_message_receive((void *)&response, &resp_size); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 121 | aml_mhu_secure_message_end(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * response[0] is the size of the response message. |
| 125 | * response[1 ... N] are the contents. |
| 126 | */ |
| 127 | if (*response != 0) |
| 128 | memcpy(dst, response + 1, *response); |
| 129 | |
| 130 | return *response; |
| 131 | } |
| 132 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 133 | void aml_scpi_unknown_thermal(uint32_t arg0, uint32_t arg1, |
| 134 | uint32_t arg2, uint32_t arg3) |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 135 | { |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 136 | aml_mhu_secure_message_start(); |
| 137 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x0, arg0); |
| 138 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x4, arg1); |
| 139 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0x8, arg2); |
| 140 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD + 0xC, arg3); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 141 | aml_mhu_secure_message_send(aml_scpi_cmd(0xC3, 16)); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 142 | aml_mhu_secure_message_wait(); |
| 143 | aml_mhu_secure_message_end(); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Carlo Caione | f7c4f9b | 2019-09-16 12:13:49 +0100 | [diff] [blame] | 146 | uint32_t aml_scpi_get_chip_id(uint8_t *obuff, uint32_t osize) |
| 147 | { |
| 148 | uint32_t *response; |
| 149 | size_t resp_size; |
| 150 | |
| 151 | if ((osize != 16) && (osize != 12)) |
| 152 | return 0; |
| 153 | |
| 154 | aml_mhu_secure_message_start(); |
| 155 | aml_mhu_secure_message_send(aml_scpi_cmd(SCPI_CMD_CHIP_ID, osize)); |
| 156 | aml_scpi_secure_message_receive((void *)&response, &resp_size); |
| 157 | aml_mhu_secure_message_end(); |
| 158 | |
| 159 | if (!((resp_size == 16) && (osize == 16)) && |
| 160 | !((resp_size == 0) && (osize == 12))) |
| 161 | return 0; |
| 162 | |
| 163 | memcpy((void *)obuff, (const void *)response, osize); |
| 164 | |
| 165 | return osize; |
| 166 | } |
| 167 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 168 | static inline void aml_scpi_copy_scp_data(uint8_t *data, size_t len) |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 169 | { |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 170 | void *dst = (void *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD; |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 171 | size_t sz; |
| 172 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 173 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, len); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 174 | aml_scpi_secure_message_send(SCPI_CMD_FW_SIZE, len); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 175 | aml_mhu_secure_message_wait(); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 176 | |
| 177 | for (sz = 0; sz < len; sz += SIZE_FWBLK) { |
| 178 | memcpy(dst, data + sz, MIN(SIZE_FWBLK, len - sz)); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 179 | aml_mhu_secure_message_send(SCPI_CMD_COPY_FW); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 183 | static inline void aml_scpi_set_scp_addr(uint64_t addr, size_t len) |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 184 | { |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 185 | volatile uint64_t *dst = (uint64_t *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD; |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 186 | |
| 187 | /* |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 188 | * It is ok as AML_MHU_SECURE_AP_TO_SCP_PAYLOAD is mapped as |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 189 | * non cachable |
| 190 | */ |
| 191 | *dst = addr; |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 192 | aml_scpi_secure_message_send(SCPI_CMD_SET_FW_ADDR, sizeof(addr)); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 193 | aml_mhu_secure_message_wait(); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 194 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 195 | mmio_write_32(AML_MHU_SECURE_AP_TO_SCP_PAYLOAD, len); |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 196 | aml_scpi_secure_message_send(SCPI_CMD_FW_SIZE, len); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 197 | aml_mhu_secure_message_wait(); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 200 | static inline void aml_scpi_send_fw_hash(uint8_t hash[], size_t len) |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 201 | { |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 202 | void *dst = (void *)AML_MHU_SECURE_AP_TO_SCP_PAYLOAD; |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 203 | |
| 204 | memcpy(dst, hash, len); |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 205 | aml_mhu_secure_message_send(0xd0); |
| 206 | aml_mhu_secure_message_send(0xd1); |
| 207 | aml_mhu_secure_message_send(0xd5); |
| 208 | aml_mhu_secure_message_end(); |
Antonio Nino Diaz | 7298c1f | 2018-12-05 00:09:30 +0000 | [diff] [blame] | 209 | } |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * Upload a FW to SCP. |
| 213 | * |
| 214 | * @param addr: firmware data address |
| 215 | * @param size: size of firmware |
| 216 | * @param send: If set, actually copy the firmware in SCP memory otherwise only |
| 217 | * send the firmware address. |
| 218 | */ |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 219 | void aml_scpi_upload_scp_fw(uintptr_t addr, size_t size, int send) |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 220 | { |
| 221 | struct asd_ctx ctx; |
| 222 | |
| 223 | asd_sha_init(&ctx, ASM_SHA256); |
| 224 | asd_sha_update(&ctx, (void *)addr, size); |
| 225 | asd_sha_finalize(&ctx); |
| 226 | |
Carlo Caione | 9c85f25 | 2019-08-28 09:46:18 +0100 | [diff] [blame] | 227 | aml_mhu_secure_message_start(); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 228 | if (send == 0) |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 229 | aml_scpi_set_scp_addr(addr, size); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 230 | else |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 231 | aml_scpi_copy_scp_data((void *)addr, size); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 232 | |
Carlo Caione | 7bb8302 | 2019-08-28 10:08:24 +0100 | [diff] [blame] | 233 | aml_scpi_send_fw_hash(ctx.digest, sizeof(ctx.digest)); |
Remi Pommarel | 1a13b22 | 2019-03-28 20:55:13 +0100 | [diff] [blame] | 234 | } |