2 * Copyright (C) 2014 Freescale Semiconductor
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/system.h>
11 #include <asm/arch-fsl-lsch3/immap_lsch3.h>
13 #include <fsl_debug_server.h>
15 DECLARE_GLOBAL_DATA_PTR;
16 static int debug_server_ver_info_maj, debug_server_ver_info_min;
19 * Copying Debug Server firmware to DDR
21 static int debug_server_copy_image(const char *title, u64 image_addr,
22 u32 image_size, u64 debug_server_ram_addr)
24 debug("%s copied to address %p\n", title,
25 (void *)debug_server_ram_addr);
26 memcpy((void *)debug_server_ram_addr, (void *)image_addr, image_size);
32 * Debug Server FIT image parser checks if the image is in FIT
33 * format, verifies integrity of the image and calculates
34 * raw image address and size values.
36 * Returns 0 if success and -1 if any of the above mentioned
39 int debug_server_parse_firmware_fit_image(const void **raw_image_addr,
40 size_t *raw_image_size)
47 const char *uname = "firmware";
49 char *debug_server_ver_info;
50 char *debug_server_ver_info_major, *debug_server_ver_info_minor;
52 /* Check if the image is in NOR flash */
53 #ifdef CONFIG_SYS_DEBUG_SERVER_FW_IN_NOR
54 fit_hdr = (void *)CONFIG_SYS_DEBUG_SERVER_FW_ADDR;
56 #error "CONFIG_SYS_DEBUG_SERVER_FW_IN_NOR not defined"
59 /* Check if Image is in FIT format */
60 format = genimg_get_format(fit_hdr);
61 if (format != IMAGE_FORMAT_FIT) {
62 printf("Error! Not a FIT image\n");
66 if (!fit_check_format(fit_hdr)) {
67 printf("Error! Bad FIT image format\n");
71 node_offset = fit_image_get_node(fit_hdr, uname);
72 if (node_offset < 0) {
73 printf("Error! Can not find %s subimage\n", uname);
77 /* Verify Debug Server firmware image */
78 if (!fit_image_verify(fit_hdr, node_offset)) {
79 printf("Error! Bad Debug Server firmware hash");
83 if (fit_get_desc(fit_hdr, node_offset, &desc) < 0) {
84 printf("Error! Failed to get Debug Server fw description");
88 debug_server_ver_info = strstr(desc, "Version");
89 debug_server_ver_info_major = strtok(debug_server_ver_info, ".");
90 debug_server_ver_info_minor = strtok(NULL, ".");
92 debug_server_ver_info_maj =
93 simple_strtoul(debug_server_ver_info_major, NULL, 10);
94 debug_server_ver_info_min =
95 simple_strtoul(debug_server_ver_info_minor, NULL, 10);
97 /* Debug server version checking */
98 if ((debug_server_ver_info_maj < DEBUG_SERVER_VER_MAJOR) ||
99 (debug_server_ver_info_min < DEBUG_SERVER_VER_MINOR)) {
100 printf("Debug server FW mismatches the min version required\n");
101 printf("Expected:%d.%d, Got %d.%d\n",
102 DEBUG_SERVER_VER_MAJOR, DEBUG_SERVER_VER_MINOR,
103 debug_server_ver_info_maj,
104 debug_server_ver_info_min);
108 /* Get address and size of raw image */
109 fit_image_get_data(fit_hdr, node_offset, &data, &size);
111 *raw_image_addr = data;
112 *raw_image_size = size;
121 * Return the actual size of the Debug Server private DRAM block.
123 * NOTE: For now this function always returns the minimum required size,
124 * However, in the future, the actual size may be obtained from an environment
127 unsigned long debug_server_get_dram_block_size(void)
129 return CONFIG_SYS_DEBUG_SERVER_DRAM_BLOCK_MIN_SIZE;
132 int debug_server_init(void)
134 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
135 int error, timeout = CONFIG_SYS_DEBUG_SERVER_TIMEOUT;
136 int debug_server_boot_status;
137 u64 debug_server_ram_addr, debug_server_ram_size;
138 const void *raw_image_addr;
139 size_t raw_image_size = 0;
141 debug("debug_server_init called\n");
143 * The Debug Server private DRAM block was already carved at the end of
144 * DRAM by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
146 debug_server_ram_size = debug_server_get_dram_block_size();
147 if (gd->bd->bi_dram[1].start)
148 debug_server_ram_addr =
149 gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
151 debug_server_ram_addr =
152 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
154 error = debug_server_parse_firmware_fit_image(&raw_image_addr,
159 debug("debug server (ram addr = 0x%llx, ram size = 0x%llx)\n",
160 debug_server_ram_addr, debug_server_ram_size);
162 * Load the Debug Server FW at the beginning of the Debug Server
163 * private DRAM block:
165 debug_server_copy_image("Debug Server Firmware",
166 (u64)raw_image_addr, raw_image_size,
167 debug_server_ram_addr);
170 flush_dcache_range((unsigned long)debug_server_ram_addr,
171 (unsigned long)debug_server_ram_addr +
172 (unsigned long)debug_server_ram_size);
175 * Tell SP that the Debug Server FW is about to be launched. Before that
176 * populate the following:
177 * 1. Write the size allocated to SP Memory region into Bits {31:16} of
179 * 2. Write the start address of the SP memory regions into
180 * SCRATCHRW5 (Bits {15:0}, contain most significant bits, Bits
181 * {47:32} of the SP Memory Region physical start address
182 * (SoC address)) and SCRATCHRW6 (Bits {31:0}).
183 * 3. To know the Debug Server FW boot status, set bit 0 of SCRATCHRW11
184 * to 1. The Debug Server sets this to 0 to indicate a
186 * 4. Wakeup SP by writing 0x1F to VSG GIC reg VIGR2.
190 out_le32(&gur->scratchrw[5 - 1],
191 (u32)((u64)debug_server_ram_addr >> 32) | (0x000D << 16));
192 out_le32(&gur->scratchrw[6 - 1],
193 ((u32)debug_server_ram_addr) & 0xFFFFFFFF);
195 out_le32(&gur->scratchrw[11 - 1], DEBUG_SERVER_INIT_STATUS);
196 /* Allow the changes to reflect in GUR block */
200 * Program VGIC to raise an interrupt to SP
202 out_le32(CONFIG_SYS_FSL_SP_VSG_GIC_VIGR2, 0x1F);
203 /* Allow the changes to reflect in VIGR2 */
207 debug("Polling for Debug server to launch ...\n");
210 debug_server_boot_status = in_le32(&gur->scratchrw[11 - 1]);
211 if (!(debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK))
214 udelay(1); /* throttle polling */
220 printf("Debug Server FW timed out (boot status: 0x%x)\n",
221 debug_server_boot_status);
226 if (debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK) {
227 printf("Debug server FW error'ed out (boot status: 0x%x)\n",
228 debug_server_boot_status);
233 printf("Debug server booted\n");
234 printf("Detected firmware %d.%d, (boot status: 0x0%x)\n",
235 debug_server_ver_info_maj, debug_server_ver_info_min,
236 debug_server_boot_status);
240 debug_server_boot_status = -error;
242 debug_server_boot_status = 0;
244 return debug_server_boot_status;