mc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2014 Freescale Semiconductor
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <errno.h>
  7. #include <asm/io.h>
  8. #include <fsl_mc.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. static int mc_boot_status;
  11. /**
  12. * Copying MC firmware or DPL image to DDR
  13. */
  14. static int mc_copy_image(const char *title,
  15. u64 image_addr, u32 image_size, u64 mc_ram_addr)
  16. {
  17. debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
  18. memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
  19. return 0;
  20. }
  21. /**
  22. * MC firmware FIT image parser checks if the image is in FIT
  23. * format, verifies integrity of the image and calculates
  24. * raw image address and size values.
  25. * Returns 0 if success and 1 if any of the above mentioned
  26. * task fail.
  27. **/
  28. int parse_mc_firmware_fit_image(const void **raw_image_addr,
  29. size_t *raw_image_size)
  30. {
  31. int format;
  32. void *fit_hdr;
  33. int node_offset;
  34. const void *data;
  35. size_t size;
  36. const char *uname = "firmware";
  37. /* Check if the image is in NOR flash*/
  38. #ifdef CONFIG_SYS_LS_MC_FW_IN_NOR
  39. fit_hdr = (void *)CONFIG_SYS_LS_MC_FW_ADDR;
  40. #else
  41. #error "No CONFIG_SYS_LS_MC_FW_IN_xxx defined"
  42. #endif
  43. /* Check if Image is in FIT format */
  44. format = genimg_get_format(fit_hdr);
  45. if (format != IMAGE_FORMAT_FIT) {
  46. debug("Not a FIT image\n");
  47. return 1;
  48. }
  49. if (!fit_check_format(fit_hdr)) {
  50. debug("Bad FIT image format\n");
  51. return 1;
  52. }
  53. node_offset = fit_image_get_node(fit_hdr, uname);
  54. if (node_offset < 0) {
  55. debug("Can not find %s subimage\n", uname);
  56. return 1;
  57. }
  58. /* Verify MC firmware image */
  59. if (!(fit_image_verify(fit_hdr, node_offset))) {
  60. debug("Bad MC firmware hash");
  61. return 1;
  62. }
  63. /* Get address and size of raw image */
  64. fit_image_get_data(fit_hdr, node_offset, &data, &size);
  65. *raw_image_addr = data;
  66. *raw_image_size = size;
  67. return 0;
  68. }
  69. int mc_init(bd_t *bis)
  70. {
  71. int error = 0;
  72. int timeout = 200000;
  73. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  74. u64 mc_ram_addr;
  75. u64 mc_dpl_offset;
  76. u32 reg_gsr;
  77. u32 mc_fw_boot_status;
  78. void *fdt_hdr;
  79. int dpl_size;
  80. const void *raw_image_addr;
  81. size_t raw_image_size = 0;
  82. BUILD_BUG_ON(CONFIG_SYS_LS_MC_FW_LENGTH % 4 != 0);
  83. /*
  84. * The MC private DRAM block was already carved at the end of DRAM
  85. * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
  86. */
  87. if (gd->bd->bi_dram[1].start) {
  88. mc_ram_addr =
  89. gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
  90. } else {
  91. mc_ram_addr =
  92. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
  93. }
  94. /*
  95. * Management Complex cores should be held at reset out of POR.
  96. * U-boot should be the first software to touch MC. To be safe,
  97. * we reset all cores again by setting GCR1 to 0. It doesn't do
  98. * anything if they are held at reset. After we setup the firmware
  99. * we kick off MC by deasserting the reset bit for core 0, and
  100. * deasserting the reset bits for Command Portal Managers.
  101. * The stop bits are not touched here. They are used to stop the
  102. * cores when they are active. Setting stop bits doesn't stop the
  103. * cores from fetching instructions when they are released from
  104. * reset.
  105. */
  106. out_le32(&mc_ccsr_regs->reg_gcr1, 0);
  107. dmb();
  108. error = parse_mc_firmware_fit_image(&raw_image_addr, &raw_image_size);
  109. if (error != 0)
  110. goto out;
  111. /*
  112. * Load the MC FW at the beginning of the MC private DRAM block:
  113. */
  114. mc_copy_image(
  115. "MC Firmware",
  116. (u64)raw_image_addr,
  117. raw_image_size,
  118. mc_ram_addr);
  119. /*
  120. * Calculate offset in the MC private DRAM block at which the MC DPL
  121. * blob is to be placed:
  122. */
  123. #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
  124. BUILD_BUG_ON(
  125. (CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
  126. CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
  127. mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
  128. #else
  129. mc_dpl_offset = mc_get_dram_block_size() -
  130. roundup(CONFIG_SYS_LS_MC_DPL_LENGTH, 4096);
  131. if ((mc_dpl_offset & 0x3) != 0 || mc_dpl_offset > 0xffffffff) {
  132. printf("%s: Invalid MC DPL offset: %llu\n",
  133. __func__, mc_dpl_offset);
  134. error = -EINVAL;
  135. goto out;
  136. }
  137. #endif
  138. /* Check if DPL image is in NOR flash */
  139. #ifdef CONFIG_SYS_LS_MC_DPL_IN_NOR
  140. fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPL_ADDR;
  141. #else
  142. #error "No CONFIG_SYS_LS_MC_DPL_IN_xxx defined"
  143. #endif
  144. dpl_size = fdt_totalsize(fdt_hdr);
  145. /*
  146. * Load the MC DPL blob at the far end of the MC private DRAM block:
  147. */
  148. mc_copy_image(
  149. "MC DPL blob",
  150. (u64)fdt_hdr,
  151. dpl_size,
  152. mc_ram_addr + mc_dpl_offset);
  153. debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
  154. /*
  155. * Tell MC where the MC Firmware image was loaded in DDR:
  156. */
  157. out_le32(&mc_ccsr_regs->reg_mcfbalr, (u32)mc_ram_addr);
  158. out_le32(&mc_ccsr_regs->reg_mcfbahr, (u32)((u64)mc_ram_addr >> 32));
  159. out_le32(&mc_ccsr_regs->reg_mcfapr, MCFAPR_BYPASS_ICID_MASK);
  160. /*
  161. * Tell MC where the DPL blob was loaded in DDR, by indicating
  162. * its offset relative to the beginning of the DDR block
  163. * allocated to the MC firmware. The MC firmware is responsible
  164. * for checking that there is no overlap between the DPL blob
  165. * and the runtime heap and stack of the MC firmware itself.
  166. *
  167. * NOTE: bits [31:2] of this offset need to be stored in bits [29:0] of
  168. * the GSR MC CCSR register. So, this offset is assumed to be 4-byte
  169. * aligned.
  170. * Care must be taken not to write 1s into bits 31 and 30 of the GSR in
  171. * this case as the SoC COP or PIC will be signaled.
  172. */
  173. out_le32(&mc_ccsr_regs->reg_gsr, (u32)(mc_dpl_offset >> 2));
  174. /*
  175. * Deassert reset and release MC core 0 to run
  176. */
  177. out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
  178. dmb();
  179. debug("Polling mc_ccsr_regs->reg_gsr ...\n");
  180. for (;;) {
  181. reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
  182. mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
  183. if (mc_fw_boot_status & 0x1)
  184. break;
  185. udelay(1000); /* throttle polling */
  186. if (timeout-- <= 0)
  187. break;
  188. }
  189. if (timeout <= 0) {
  190. printf("%s: timeout booting management complex firmware\n",
  191. __func__);
  192. /* TODO: Get an error status from an MC CCSR register */
  193. error = -ETIMEDOUT;
  194. goto out;
  195. }
  196. printf("Management complex booted (boot status: %#x)\n",
  197. mc_fw_boot_status);
  198. if (mc_fw_boot_status != 0x1) {
  199. /*
  200. * TODO: Identify critical errors from the GSR register's FS
  201. * field and for those errors, set error to -ENODEV or other
  202. * appropriate errno, so that the status property is set to
  203. * failure in the fsl,dprc device tree node.
  204. */
  205. }
  206. out:
  207. if (error != 0)
  208. mc_boot_status = -error;
  209. else
  210. mc_boot_status = 0;
  211. return error;
  212. }
  213. int get_mc_boot_status(void)
  214. {
  215. return mc_boot_status;
  216. }
  217. /**
  218. * Return the actual size of the MC private DRAM block.
  219. *
  220. * NOTE: For now this function always returns the minimum required size,
  221. * However, in the future, the actual size may be obtained from an environment
  222. * variable.
  223. */
  224. unsigned long mc_get_dram_block_size(void)
  225. {
  226. return CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  227. }