blob: 98d9fbe534c3bd9511c4f2dfd4a4be3957b4241c [file] [log] [blame]
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -07001/*
2 * Copyright (C) 2014 Freescale Semiconductor
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <errno.h>
9#include <asm/io.h>
10#include <asm/system.h>
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070011
Bhupesh Sharma26c18642015-05-28 14:54:12 +053012#include <fsl-mc/fsl_mc.h>
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070013#include <fsl_debug_server.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16static int debug_server_ver_info_maj, debug_server_ver_info_min;
17
18/**
19 * Copying Debug Server firmware to DDR
20 */
21static int debug_server_copy_image(const char *title, u64 image_addr,
22 u32 image_size, u64 debug_server_ram_addr)
23{
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);
27
28 return 0;
29}
30
31/**
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.
35 *
36 * Returns 0 if success and -1 if any of the above mentioned
37 * task fail.
38 **/
39int debug_server_parse_firmware_fit_image(const void **raw_image_addr,
40 size_t *raw_image_size)
41{
42 int format;
43 void *fit_hdr;
44 int node_offset;
45 const void *data;
46 size_t size;
47 const char *uname = "firmware";
48 char *desc;
49 char *debug_server_ver_info;
50 char *debug_server_ver_info_major, *debug_server_ver_info_minor;
51
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;
55#else
56#error "CONFIG_SYS_DEBUG_SERVER_FW_IN_NOR not defined"
57#endif
58
59 /* Check if Image is in FIT format */
60 format = genimg_get_format(fit_hdr);
61 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwaha4586fe82015-08-10 20:03:03 +053062 printf("Debug Server FW: Not a FIT image\n");
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070063 goto out_error;
64 }
65
66 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwaha4586fe82015-08-10 20:03:03 +053067 printf("Debug Server FW: Bad FIT image format\n");
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070068 goto out_error;
69 }
70
71 node_offset = fit_image_get_node(fit_hdr, uname);
72 if (node_offset < 0) {
Prabhakar Kushwaha4586fe82015-08-10 20:03:03 +053073 printf("Debug Server FW:Can not find %s subimage\n", uname);
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070074 goto out_error;
75 }
76
77 /* Verify Debug Server firmware image */
78 if (!fit_image_verify(fit_hdr, node_offset)) {
Prabhakar Kushwaha4586fe82015-08-10 20:03:03 +053079 printf("Debug Server FW: Bad Debug Server firmware hash");
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070080 goto out_error;
81 }
82
83 if (fit_get_desc(fit_hdr, node_offset, &desc) < 0) {
Prabhakar Kushwaha4586fe82015-08-10 20:03:03 +053084 printf("Debug Server FW: Failed to get FW description");
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -070085 goto out_error;
86 }
87
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, ".");
91
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);
96
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);
105 goto out_error;
106 }
107
108 /* Get address and size of raw image */
109 fit_image_get_data(fit_hdr, node_offset, &data, &size);
110
111 *raw_image_addr = data;
112 *raw_image_size = size;
113
114 return 0;
115
116out_error:
117 return -1;
118}
119
120/**
121 * Return the actual size of the Debug Server private DRAM block.
122 *
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
125 * variable.
126 */
127unsigned long debug_server_get_dram_block_size(void)
128{
129 return CONFIG_SYS_DEBUG_SERVER_DRAM_BLOCK_MIN_SIZE;
130}
131
132int debug_server_init(void)
133{
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;
140
141 debug("debug_server_init called\n");
142 /*
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:
145 */
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;
150 else
151 debug_server_ram_addr =
152 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
153
Bhupesh Sharma26c18642015-05-28 14:54:12 +0530154#ifdef CONFIG_FSL_MC_ENET
155 debug_server_ram_addr += mc_get_dram_block_size();
156#endif
157
Bhupesh Sharma25b8efe2015-03-19 09:20:43 -0700158 error = debug_server_parse_firmware_fit_image(&raw_image_addr,
159 &raw_image_size);
160 if (error != 0)
161 goto out;
162
163 debug("debug server (ram addr = 0x%llx, ram size = 0x%llx)\n",
164 debug_server_ram_addr, debug_server_ram_size);
165 /*
166 * Load the Debug Server FW at the beginning of the Debug Server
167 * private DRAM block:
168 */
169 debug_server_copy_image("Debug Server Firmware",
170 (u64)raw_image_addr, raw_image_size,
171 debug_server_ram_addr);
172
173 /* flush dcache */
174 flush_dcache_range((unsigned long)debug_server_ram_addr,
175 (unsigned long)debug_server_ram_addr +
176 (unsigned long)debug_server_ram_size);
177
178 /*
179 * Tell SP that the Debug Server FW is about to be launched. Before that
180 * populate the following:
181 * 1. Write the size allocated to SP Memory region into Bits {31:16} of
182 * SCRATCHRW5.
183 * 2. Write the start address of the SP memory regions into
184 * SCRATCHRW5 (Bits {15:0}, contain most significant bits, Bits
185 * {47:32} of the SP Memory Region physical start address
186 * (SoC address)) and SCRATCHRW6 (Bits {31:0}).
187 * 3. To know the Debug Server FW boot status, set bit 0 of SCRATCHRW11
188 * to 1. The Debug Server sets this to 0 to indicate a
189 * successul boot.
190 * 4. Wakeup SP by writing 0x1F to VSG GIC reg VIGR2.
191 */
192
193 /* 512 MB */
194 out_le32(&gur->scratchrw[5 - 1],
195 (u32)((u64)debug_server_ram_addr >> 32) | (0x000D << 16));
196 out_le32(&gur->scratchrw[6 - 1],
197 ((u32)debug_server_ram_addr) & 0xFFFFFFFF);
198
199 out_le32(&gur->scratchrw[11 - 1], DEBUG_SERVER_INIT_STATUS);
200 /* Allow the changes to reflect in GUR block */
201 mb();
202
203 /*
204 * Program VGIC to raise an interrupt to SP
205 */
206 out_le32(CONFIG_SYS_FSL_SP_VSG_GIC_VIGR2, 0x1F);
207 /* Allow the changes to reflect in VIGR2 */
208 mb();
209
210 dmb();
211 debug("Polling for Debug server to launch ...\n");
212
213 while (1) {
214 debug_server_boot_status = in_le32(&gur->scratchrw[11 - 1]);
215 if (!(debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK))
216 break;
217
218 udelay(1); /* throttle polling */
219 if (timeout-- <= 0)
220 break;
221 }
222
223 if (timeout <= 0) {
224 printf("Debug Server FW timed out (boot status: 0x%x)\n",
225 debug_server_boot_status);
226 error = -ETIMEDOUT;
227 goto out;
228 }
229
230 if (debug_server_boot_status & DEBUG_SERVER_INIT_STATUS_MASK) {
231 printf("Debug server FW error'ed out (boot status: 0x%x)\n",
232 debug_server_boot_status);
233 error = -ENODEV;
234 goto out;
235 }
236
237 printf("Debug server booted\n");
238 printf("Detected firmware %d.%d, (boot status: 0x0%x)\n",
239 debug_server_ver_info_maj, debug_server_ver_info_min,
240 debug_server_boot_status);
241
242out:
243 if (error != 0)
244 debug_server_boot_status = -error;
245 else
246 debug_server_boot_status = 0;
247
248 return debug_server_boot_status;
249}
250