hisi_mcu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <drivers/console.h>
  13. #include <lib/mmio.h>
  14. #include <plat/common/platform.h>
  15. #include <hi6220.h>
  16. #define MCU_SECTION_MAX 30
  17. enum MCU_IMAGE_SEC_TYPE_ENUM {
  18. MCU_IMAGE_SEC_TYPE_TEXT = 0, /* text section */
  19. MCU_IMAGE_SEC_TYPE_DATA, /* data section */
  20. MCU_IMAGE_SEC_TYPE_BUTT
  21. };
  22. enum MCU_IMAGE_SEC_LOAD_ENUM {
  23. MCU_IMAGE_SEC_LOAD_STATIC = 0,
  24. MCU_IMAGE_SEC_LOAD_DYNAMIC,
  25. MCU_IMAGE_SEC_LOAD_BUFFER,
  26. MCU_IMAGE_SEC_LOAD_MODEM_ENTRY,
  27. MCU_IMAGE_SEC_LOAD_BUTT
  28. };
  29. struct mcu_image_sec {
  30. unsigned short serial;
  31. char type;
  32. char load_attr;
  33. uint32_t src_offset; /* offset in image */
  34. uint32_t dst_offset; /* offset in memory */
  35. uint32_t size;
  36. };
  37. struct mcu_image_head {
  38. char time_stamp[24];
  39. uint32_t image_size;
  40. uint32_t secs_num;
  41. struct mcu_image_sec secs[MCU_SECTION_MAX];
  42. };
  43. #define SOC_SRAM_M3_BASE_ADDR (0xF6000000)
  44. #define MCU_SRAM_SIZE (0x0000C000)
  45. #define MCU_CACHE_SIZE (0x00004000)
  46. #define MCU_CODE_SIZE (MCU_SRAM_SIZE - MCU_CACHE_SIZE)
  47. #define MCU_SYS_MEM_ADDR (0x05E00000)
  48. #define MCU_SYS_MEM_SIZE (0x00100000)
  49. static uint32_t mcu2ap_addr(uint32_t mcu_addr)
  50. {
  51. if (mcu_addr < MCU_CODE_SIZE)
  52. return (mcu_addr + SOC_SRAM_M3_BASE_ADDR);
  53. else if ((mcu_addr >= MCU_SRAM_SIZE) &&
  54. (mcu_addr < MCU_SRAM_SIZE + MCU_SYS_MEM_SIZE))
  55. return mcu_addr - MCU_SRAM_SIZE + MCU_SYS_MEM_ADDR;
  56. else
  57. return mcu_addr;
  58. }
  59. static int is_binary_header_invalid(struct mcu_image_head *head,
  60. unsigned int length)
  61. {
  62. /* invalid cases */
  63. if ((head->image_size == 0) ||
  64. (head->image_size > length) ||
  65. (head->secs_num > MCU_SECTION_MAX) ||
  66. (head->secs_num == 0))
  67. return 1;
  68. return 0;
  69. }
  70. static int is_binary_section_invalid(struct mcu_image_sec *sec,
  71. struct mcu_image_head *head)
  72. {
  73. unsigned long ap_dst_offset = 0;
  74. if ((sec->serial >= head->secs_num) ||
  75. (sec->src_offset + sec->size > head->image_size))
  76. return 1;
  77. if ((sec->type >= MCU_IMAGE_SEC_TYPE_BUTT) ||
  78. (sec->load_attr >= MCU_IMAGE_SEC_LOAD_BUTT))
  79. return 1;
  80. ap_dst_offset = mcu2ap_addr(sec->dst_offset);
  81. if ((ap_dst_offset >= SOC_SRAM_M3_BASE_ADDR) &&
  82. (ap_dst_offset < SOC_SRAM_M3_BASE_ADDR + 0x20000 - sec->size))
  83. return 0;
  84. else if ((ap_dst_offset >= MCU_SYS_MEM_ADDR) &&
  85. (ap_dst_offset < MCU_SYS_MEM_ADDR + MCU_SYS_MEM_SIZE - sec->size))
  86. return 0;
  87. else if ((ap_dst_offset >= 0xfff8e000) &&
  88. (ap_dst_offset < 0xfff91c00 - sec->size))
  89. return 0;
  90. ERROR("%s: mcu destination address invalid.\n", __func__);
  91. ERROR("%s: number=%d, dst offset=%d size=%d\n",
  92. __func__, sec->serial, sec->dst_offset, sec->size);
  93. return 1;
  94. }
  95. void hisi_mcu_enable_sram(void)
  96. {
  97. mmio_write_32(AO_SC_PERIPH_CLKEN4,
  98. AO_SC_PERIPH_CLKEN4_HCLK_IPC_S |
  99. AO_SC_PERIPH_CLKEN4_HCLK_IPC_NS);
  100. /* set register to enable dvfs which is used by mcu */
  101. mmio_write_32(PERI_SC_RESERVED8_ADDR, 0x0A001022);
  102. /* mcu mem is powered on, need de-assert reset */
  103. mmio_write_32(AO_SC_PERIPH_RSTDIS4,
  104. AO_SC_PERIPH_RSTDIS4_RESET_MCU_ECTR_N);
  105. /* enable mcu hclk */
  106. mmio_write_32(AO_SC_PERIPH_CLKEN4,
  107. AO_SC_PERIPH_CLKEN4_HCLK_MCU |
  108. AO_SC_PERIPH_CLKEN4_CLK_MCU_DAP);
  109. }
  110. void hisi_mcu_start_run(void)
  111. {
  112. unsigned int val;
  113. /* set mcu ddr remap configuration */
  114. mmio_write_32(AO_SC_MCU_SUBSYS_CTRL2, MCU_SYS_MEM_ADDR);
  115. /* de-assert reset for mcu and to run */
  116. mmio_write_32(AO_SC_PERIPH_RSTDIS4,
  117. AO_SC_PERIPH_RSTDIS4_RESET_MCU_ECTR_N |
  118. AO_SC_PERIPH_RSTDIS4_RESET_MCU_SYS_N |
  119. AO_SC_PERIPH_RSTDIS4_RESET_MCU_POR_N |
  120. AO_SC_PERIPH_RSTDIS4_RESET_MCU_DAP_N);
  121. val = mmio_read_32(AO_SC_SYS_CTRL2);
  122. mmio_write_32(AO_SC_SYS_CTRL2,
  123. val | AO_SC_SYS_CTRL2_GLB_SRST_STAT_CLEAR);
  124. INFO("%s: AO_SC_SYS_CTRL2=%x\n", __func__,
  125. mmio_read_32(AO_SC_SYS_CTRL2));
  126. }
  127. int hisi_mcu_load_image(uintptr_t image_base, uint32_t image_size)
  128. {
  129. unsigned int i;
  130. struct mcu_image_head *head;
  131. char *buf;
  132. head = (struct mcu_image_head *)image_base;
  133. if (is_binary_header_invalid(head, image_size)) {
  134. ERROR("Invalid %s image header.\n", head->time_stamp);
  135. return -1;
  136. }
  137. buf = (char *)head;
  138. for (i = 0; i < head->secs_num; i++) {
  139. int *src, *dst;
  140. /* check the sections */
  141. if (is_binary_section_invalid(&head->secs[i], head)) {
  142. ERROR("Invalid mcu section.\n");
  143. return -1;
  144. }
  145. /* check if the section is static-loaded */
  146. if (head->secs[i].load_attr != MCU_IMAGE_SEC_LOAD_STATIC)
  147. continue;
  148. /* copy the sections */
  149. src = (int *)(intptr_t)(buf + head->secs[i].src_offset);
  150. dst = (int *)(intptr_t)mcu2ap_addr(head->secs[i].dst_offset);
  151. memcpy((void *)dst, (void *)src, head->secs[i].size);
  152. INFO("%s: mcu sections %d:\n", __func__, i);
  153. INFO("%s: src = 0x%x\n",
  154. __func__, (unsigned int)(uintptr_t)src);
  155. INFO("%s: dst = 0x%x\n",
  156. __func__, (unsigned int)(uintptr_t)dst);
  157. INFO("%s: size = %d\n", __func__, head->secs[i].size);
  158. INFO("%s: [SRC 0x%x] 0x%x 0x%x 0x%x 0x%x\n",
  159. __func__, (unsigned int)(uintptr_t)src,
  160. src[0], src[1], src[2], src[3]);
  161. INFO("%s: [DST 0x%x] 0x%x 0x%x 0x%x 0x%x\n",
  162. __func__, (unsigned int)(uintptr_t)dst,
  163. dst[0], dst[1], dst[2], dst[3]);
  164. }
  165. return 0;
  166. }