mc.c 8.5 KB

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