fsl_debug_server.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2014 Freescale Semiconductor
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <asm/io.h>
  9. #include <asm/system.h>
  10. #include <asm/arch-fsl-lsch3/immap_lsch3.h>
  11. #include <fsl-mc/fsl_mc.h>
  12. #include <fsl_debug_server.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static int debug_server_ver_info_maj, debug_server_ver_info_min;
  15. /**
  16. * Copying Debug Server firmware to DDR
  17. */
  18. static int debug_server_copy_image(const char *title, u64 image_addr,
  19. u32 image_size, u64 debug_server_ram_addr)
  20. {
  21. debug("%s copied to address %p\n", title,
  22. (void *)debug_server_ram_addr);
  23. memcpy((void *)debug_server_ram_addr, (void *)image_addr, image_size);
  24. return 0;
  25. }
  26. /**
  27. * Debug Server FIT image parser checks if the image is in FIT
  28. * format, verifies integrity of the image and calculates
  29. * raw image address and size values.
  30. *
  31. * Returns 0 if success and -1 if any of the above mentioned
  32. * task fail.
  33. **/
  34. int debug_server_parse_firmware_fit_image(const void **raw_image_addr,
  35. size_t *raw_image_size)
  36. {
  37. int format;
  38. void *fit_hdr;
  39. int node_offset;
  40. const void *data;
  41. size_t size;
  42. const char *uname = "firmware";
  43. char *desc;
  44. char *debug_server_ver_info;
  45. char *debug_server_ver_info_major, *debug_server_ver_info_minor;
  46. /* Check if the image is in NOR flash */
  47. #ifdef CONFIG_SYS_DEBUG_SERVER_FW_IN_NOR
  48. fit_hdr = (void *)CONFIG_SYS_DEBUG_SERVER_FW_ADDR;
  49. #else
  50. #error "CONFIG_SYS_DEBUG_SERVER_FW_IN_NOR not defined"
  51. #endif
  52. /* Check if Image is in FIT format */
  53. format = genimg_get_format(fit_hdr);
  54. if (format != IMAGE_FORMAT_FIT) {
  55. printf("Error! Not a FIT image\n");
  56. goto out_error;
  57. }
  58. if (!fit_check_format(fit_hdr)) {
  59. printf("Error! Bad FIT image format\n");
  60. goto out_error;
  61. }
  62. node_offset = fit_image_get_node(fit_hdr, uname);
  63. if (node_offset < 0) {
  64. printf("Error! Can not find %s subimage\n", uname);
  65. goto out_error;
  66. }
  67. /* Verify Debug Server firmware image */
  68. if (!fit_image_verify(fit_hdr, node_offset)) {
  69. printf("Error! Bad Debug Server firmware hash");
  70. goto out_error;
  71. }
  72. if (fit_get_desc(fit_hdr, node_offset, &desc) < 0) {
  73. printf("Error! Failed to get Debug Server fw description");
  74. goto out_error;
  75. }
  76. debug_server_ver_info = strstr(desc, "Version");
  77. debug_server_ver_info_major = strtok(debug_server_ver_info, ".");
  78. debug_server_ver_info_minor = strtok(NULL, ".");
  79. debug_server_ver_info_maj =
  80. simple_strtoul(debug_server_ver_info_major, NULL, 10);
  81. debug_server_ver_info_min =
  82. simple_strtoul(debug_server_ver_info_minor, NULL, 10);
  83. /* Debug server version checking */
  84. if ((debug_server_ver_info_maj < DEBUG_SERVER_VER_MAJOR) ||
  85. (debug_server_ver_info_min < DEBUG_SERVER_VER_MINOR)) {
  86. printf("Debug server FW mismatches the min version required\n");
  87. printf("Expected:%d.%d, Got %d.%d\n",
  88. DEBUG_SERVER_VER_MAJOR, DEBUG_SERVER_VER_MINOR,
  89. debug_server_ver_info_maj,
  90. debug_server_ver_info_min);
  91. goto out_error;
  92. }
  93. /* Get address and size of raw image */
  94. fit_image_get_data(fit_hdr, node_offset, &data, &size);
  95. *raw_image_addr = data;
  96. *raw_image_size = size;
  97. return 0;
  98. out_error:
  99. return -1;
  100. }
  101. /**
  102. * Return the actual size of the Debug Server private DRAM block.
  103. *
  104. * NOTE: For now this function always returns the minimum required size,
  105. * However, in the future, the actual size may be obtained from an environment
  106. * variable.
  107. */
  108. unsigned long debug_server_get_dram_block_size(void)
  109. {
  110. return CONFIG_SYS_DEBUG_SERVER_DRAM_BLOCK_MIN_SIZE;
  111. }
  112. int debug_server_init(void)
  113. {
  114. struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  115. int error, timeout = CONFIG_SYS_DEBUG_SERVER_TIMEOUT;
  116. int debug_server_boot_status;
  117. u64 debug_server_ram_addr, debug_server_ram_size;
  118. const void *raw_image_addr;
  119. size_t raw_image_size = 0;
  120. debug("debug_server_init called\n");
  121. /*
  122. * The Debug Server private DRAM block was already carved at the end of
  123. * DRAM by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
  124. */
  125. debug_server_ram_size = debug_server_get_dram_block_size();
  126. if (gd->bd->bi_dram[1].start)
  127. debug_server_ram_addr =
  128. gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
  129. else
  130. debug_server_ram_addr =
  131. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
  132. #ifdef CONFIG_FSL_MC_ENET
  133. debug_server_ram_addr += mc_get_dram_block_size();
  134. #endif
  135. error = debug_server_parse_firmware_fit_image(&raw_image_addr,
  136. &raw_image_size);
  137. if (error != 0)
  138. goto out;
  139. debug("debug server (ram addr = 0x%llx, ram size = 0x%llx)\n",
  140. debug_server_ram_addr, debug_server_ram_size);
  141. /*
  142. * Load the Debug Server FW at the beginning of the Debug Server
  143. * private DRAM block:
  144. */
  145. debug_server_copy_image("Debug Server Firmware",
  146. (u64)raw_image_addr, raw_image_size,
  147. debug_server_ram_addr);
  148. /* flush dcache */
  149. flush_dcache_range((unsigned long)debug_server_ram_addr,
  150. (unsigned long)debug_server_ram_addr +
  151. (unsigned long)debug_server_ram_size);
  152. /*
  153. * Tell SP that the Debug Server FW is about to be launched. Before that
  154. * populate the following:
  155. * 1. Write the size allocated to SP Memory region into Bits {31:16} of
  156. * SCRATCHRW5.
  157. * 2. Write the start address of the SP memory regions into
  158. * SCRATCHRW5 (Bits {15:0}, contain most significant bits, Bits
  159. * {47:32} of the SP Memory Region physical start address
  160. * (SoC address)) and SCRATCHRW6 (Bits {31:0}).
  161. * 3. To know the Debug Server FW boot status, set bit 0 of SCRATCHRW11
  162. * to 1. The Debug Server sets this to 0 to indicate a
  163. * successul boot.
  164. * 4. Wakeup SP by writing 0x1F to VSG GIC reg VIGR2.
  165. */
  166. /* 512 MB */
  167. out_le32(&gur->scratchrw[5 - 1],
  168. (u32)((u64)debug_server_ram_addr >> 32) | (0x000D << 16));
  169. out_le32(&gur->scratchrw[6 - 1],
  170. ((u32)debug_server_ram_addr) & 0xFFFFFFFF);
  171. out_le32(&gur->scratchrw[11 - 1], DEBUG_SERVER_INIT_STATUS);
  172. /* Allow the changes to reflect in GUR block */
  173. mb();
  174. /*
  175. * Program VGIC to raise an interrupt to SP
  176. */
  177. out_le32(CONFIG_SYS_FSL_SP_VSG_GIC_VIGR2, 0x1F);
  178. /* Allow the changes to reflect in VIGR2 */
  179. mb();
  180. dmb();
  181. debug("Polling for Debug server to launch ...\n");
  182. while (1) {
  183. debug_server_boot_status = in_le32(&gur->scratchrw[11 - 1]);
  184. if (!(debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK))
  185. break;
  186. udelay(1); /* throttle polling */
  187. if (timeout-- <= 0)
  188. break;
  189. }
  190. if (timeout <= 0) {
  191. printf("Debug Server FW timed out (boot status: 0x%x)\n",
  192. debug_server_boot_status);
  193. error = -ETIMEDOUT;
  194. goto out;
  195. }
  196. if (debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK) {
  197. printf("Debug server FW error'ed out (boot status: 0x%x)\n",
  198. debug_server_boot_status);
  199. error = -ENODEV;
  200. goto out;
  201. }
  202. printf("Debug server booted\n");
  203. printf("Detected firmware %d.%d, (boot status: 0x0%x)\n",
  204. debug_server_ver_info_maj, debug_server_ver_info_min,
  205. debug_server_boot_status);
  206. out:
  207. if (error != 0)
  208. debug_server_boot_status = -error;
  209. else
  210. debug_server_boot_status = 0;
  211. return debug_server_boot_status;
  212. }