aml_scpi.c 6.1 KB

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