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