mc.c 8.4 KB

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