Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * K3: System Firmware Loader |
| 4 | * |
| 5 | * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * Andreas Dannenberg <dannenberg@ti.com> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 7130b95 | 2020-07-19 10:15:40 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 11 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 13 | #include <spl.h> |
| 14 | #include <malloc.h> |
| 15 | #include <remoteproc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <asm/cache.h> |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 17 | #include <linux/soc/ti/ti_sci_protocol.h> |
Vignesh Raghavendra | 1b9d927 | 2020-01-27 17:59:24 +0530 | [diff] [blame] | 18 | #include <g_dnl.h> |
| 19 | #include <usb.h> |
| 20 | #include <dfu.h> |
Lokesh Vutla | a5f2756 | 2020-02-04 11:09:50 +0530 | [diff] [blame] | 21 | #include <dm/uclass-internal.h> |
| 22 | #include <spi_flash.h> |
Vignesh Raghavendra | 1b9d927 | 2020-01-27 17:59:24 +0530 | [diff] [blame] | 23 | |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 24 | #include <asm/arch/sys_proto.h> |
Andreas Dannenberg | 6da45de | 2019-08-15 15:55:29 -0500 | [diff] [blame] | 25 | #include "common.h" |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 28 | |
| 29 | /* Name of the FIT image nodes for SYSFW and its config data */ |
| 30 | #define SYSFW_FIRMWARE "sysfw.bin" |
| 31 | #define SYSFW_CFG_BOARD "board-cfg.bin" |
| 32 | #define SYSFW_CFG_PM "pm-cfg.bin" |
| 33 | #define SYSFW_CFG_RM "rm-cfg.bin" |
| 34 | #define SYSFW_CFG_SEC "sec-cfg.bin" |
| 35 | |
| 36 | static bool sysfw_loaded; |
| 37 | static void *sysfw_load_address; |
| 38 | |
| 39 | /* |
| 40 | * Populate SPL hook to override the default load address used by the SPL |
| 41 | * loader function with a custom address for SYSFW loading. |
| 42 | */ |
| 43 | struct image_header *spl_get_load_buffer(ssize_t offset, size_t size) |
| 44 | { |
| 45 | if (sysfw_loaded) |
| 46 | return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset); |
| 47 | else if (sysfw_load_address) |
| 48 | return sysfw_load_address; |
| 49 | else |
| 50 | panic("SYSFW load address not defined!"); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Populate SPL hook to skip the default SPL loader FIT post-processing steps |
| 55 | * during SYSFW loading and return to the calling function so we can perform |
| 56 | * our own custom processing. |
| 57 | */ |
| 58 | bool spl_load_simple_fit_skip_processing(void) |
| 59 | { |
| 60 | return !sysfw_loaded; |
| 61 | } |
| 62 | |
| 63 | static int fit_get_data_by_name(const void *fit, int images, const char *name, |
| 64 | const void **addr, size_t *size) |
| 65 | { |
| 66 | int node_offset; |
| 67 | |
| 68 | node_offset = fdt_subnode_offset(fit, images, name); |
| 69 | if (node_offset < 0) |
| 70 | return -ENOENT; |
| 71 | |
| 72 | return fit_image_get_data(fit, node_offset, addr, size); |
| 73 | } |
| 74 | |
| 75 | static void k3_sysfw_load_using_fit(void *fit) |
| 76 | { |
| 77 | int images; |
| 78 | const void *sysfw_addr; |
| 79 | size_t sysfw_size; |
| 80 | int ret; |
| 81 | |
| 82 | /* Find the node holding the images information */ |
| 83 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 84 | if (images < 0) |
| 85 | panic("Cannot find /images node (%d)\n", images); |
| 86 | |
| 87 | /* Extract System Firmware (SYSFW) image from FIT */ |
| 88 | ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE, |
| 89 | &sysfw_addr, &sysfw_size); |
| 90 | if (ret < 0) |
| 91 | panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE, |
| 92 | ret); |
| 93 | |
| 94 | /* |
| 95 | * Start up system controller firmware |
| 96 | * |
| 97 | * It is assumed that remoteproc device 0 is the corresponding |
| 98 | * system-controller that runs SYSFW. Make sure DT reflects the same. |
| 99 | */ |
| 100 | ret = rproc_dev_init(0); |
| 101 | if (ret) |
| 102 | panic("rproc failed to be initialized (%d)\n", ret); |
| 103 | |
| 104 | ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size); |
| 105 | if (ret) |
| 106 | panic("Firmware failed to start on rproc (%d)\n", ret); |
| 107 | |
| 108 | ret = rproc_start(0); |
| 109 | if (ret) |
| 110 | panic("Firmware init failed on rproc (%d)\n", ret); |
| 111 | } |
| 112 | |
| 113 | static void k3_sysfw_configure_using_fit(void *fit, |
| 114 | struct ti_sci_handle *ti_sci) |
| 115 | { |
| 116 | struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops; |
| 117 | int images; |
| 118 | const void *cfg_fragment_addr; |
| 119 | size_t cfg_fragment_size; |
| 120 | int ret; |
| 121 | |
| 122 | /* Find the node holding the images information */ |
| 123 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 124 | if (images < 0) |
| 125 | panic("Cannot find /images node (%d)\n", images); |
| 126 | |
| 127 | /* Extract board configuration from FIT */ |
| 128 | ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD, |
| 129 | &cfg_fragment_addr, &cfg_fragment_size); |
| 130 | if (ret < 0) |
| 131 | panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD, |
| 132 | ret); |
| 133 | |
| 134 | /* Apply board configuration to SYSFW */ |
| 135 | ret = board_ops->board_config(ti_sci, |
| 136 | (u64)(u32)cfg_fragment_addr, |
| 137 | (u32)cfg_fragment_size); |
| 138 | if (ret) |
| 139 | panic("Failed to set board configuration (%d)\n", ret); |
| 140 | |
| 141 | /* Extract power/clock (PM) specific configuration from FIT */ |
| 142 | ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM, |
| 143 | &cfg_fragment_addr, &cfg_fragment_size); |
| 144 | if (ret < 0) |
| 145 | panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM, |
| 146 | ret); |
| 147 | |
| 148 | /* Apply power/clock (PM) specific configuration to SYSFW */ |
| 149 | ret = board_ops->board_config_pm(ti_sci, |
| 150 | (u64)(u32)cfg_fragment_addr, |
| 151 | (u32)cfg_fragment_size); |
| 152 | if (ret) |
| 153 | panic("Failed to set board PM configuration (%d)\n", ret); |
| 154 | |
| 155 | /* Extract resource management (RM) specific configuration from FIT */ |
| 156 | ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM, |
| 157 | &cfg_fragment_addr, &cfg_fragment_size); |
| 158 | if (ret < 0) |
| 159 | panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM, |
| 160 | ret); |
| 161 | |
| 162 | /* Apply resource management (RM) configuration to SYSFW */ |
| 163 | ret = board_ops->board_config_rm(ti_sci, |
| 164 | (u64)(u32)cfg_fragment_addr, |
| 165 | (u32)cfg_fragment_size); |
| 166 | if (ret) |
| 167 | panic("Failed to set board RM configuration (%d)\n", ret); |
| 168 | |
| 169 | /* Extract security specific configuration from FIT */ |
| 170 | ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC, |
| 171 | &cfg_fragment_addr, &cfg_fragment_size); |
| 172 | if (ret < 0) |
| 173 | panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC, |
| 174 | ret); |
| 175 | |
| 176 | /* Apply security configuration to SYSFW */ |
| 177 | ret = board_ops->board_config_security(ti_sci, |
| 178 | (u64)(u32)cfg_fragment_addr, |
| 179 | (u32)cfg_fragment_size); |
| 180 | if (ret) |
| 181 | panic("Failed to set board security configuration (%d)\n", |
| 182 | ret); |
| 183 | } |
Vignesh Raghavendra | 1b9d927 | 2020-01-27 17:59:24 +0530 | [diff] [blame] | 184 | |
| 185 | #if CONFIG_IS_ENABLED(DFU) |
| 186 | static int k3_sysfw_dfu_download(void *addr) |
| 187 | { |
| 188 | char dfu_str[50]; |
| 189 | int ret; |
| 190 | |
| 191 | sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr, |
| 192 | CONFIG_K3_SYSFW_IMAGE_SIZE_MAX); |
| 193 | ret = dfu_config_entities(dfu_str, "ram", "0"); |
| 194 | if (ret) { |
| 195 | dfu_free_entities(); |
| 196 | goto exit; |
| 197 | } |
| 198 | |
| 199 | run_usb_dnl_gadget(0, "usb_dnl_dfu"); |
| 200 | exit: |
| 201 | dfu_free_entities(); |
| 202 | return ret; |
| 203 | } |
| 204 | #endif |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 205 | |
Lokesh Vutla | a5f2756 | 2020-02-04 11:09:50 +0530 | [diff] [blame] | 206 | #if CONFIG_IS_ENABLED(SPI_LOAD) |
| 207 | static void *k3_sysfw_get_spi_addr(void) |
| 208 | { |
| 209 | struct udevice *dev; |
| 210 | fdt_addr_t addr; |
| 211 | int ret; |
| 212 | |
| 213 | ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS, |
| 214 | true, &dev); |
| 215 | if (ret) |
| 216 | return NULL; |
| 217 | |
| 218 | addr = dev_read_addr_index(dev, 1); |
| 219 | if (addr == FDT_ADDR_T_NONE) |
| 220 | return NULL; |
| 221 | |
| 222 | return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS); |
| 223 | } |
| 224 | #endif |
| 225 | |
Faiz Abbas | 6839321 | 2020-02-26 13:44:36 +0530 | [diff] [blame] | 226 | void k3_sysfw_loader(void (*config_pm_pre_callback) (void), |
| 227 | void (*config_pm_done_callback)(void)) |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 228 | { |
| 229 | struct spl_image_info spl_image = { 0 }; |
| 230 | struct spl_boot_device bootdev = { 0 }; |
| 231 | struct ti_sci_handle *ti_sci; |
Lokesh Vutla | a5f2756 | 2020-02-04 11:09:50 +0530 | [diff] [blame] | 232 | int ret = 0; |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 233 | |
| 234 | /* Reserve a block of aligned memory for loading the SYSFW image */ |
| 235 | sysfw_load_address = memalign(ARCH_DMA_MINALIGN, |
| 236 | CONFIG_K3_SYSFW_IMAGE_SIZE_MAX); |
| 237 | if (!sysfw_load_address) |
| 238 | panic("Error allocating %u bytes of memory for SYSFW image\n", |
| 239 | CONFIG_K3_SYSFW_IMAGE_SIZE_MAX); |
| 240 | |
| 241 | debug("%s: allocated %u bytes at 0x%p\n", __func__, |
| 242 | CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address); |
| 243 | |
| 244 | /* Set load address for legacy modes that bypass spl_get_load_buffer */ |
| 245 | spl_image.load_addr = (uintptr_t)sysfw_load_address; |
| 246 | |
| 247 | bootdev.boot_device = spl_boot_device(); |
| 248 | |
| 249 | /* Load combined System Controller firmware and config data image */ |
| 250 | switch (bootdev.boot_device) { |
| 251 | #if CONFIG_IS_ENABLED(MMC_SUPPORT) |
| 252 | case BOOT_DEVICE_MMC1: |
| 253 | case BOOT_DEVICE_MMC2: |
| 254 | case BOOT_DEVICE_MMC2_2: |
| 255 | ret = spl_mmc_load(&spl_image, &bootdev, |
| 256 | #ifdef CONFIG_K3_SYSFW_IMAGE_NAME |
| 257 | CONFIG_K3_SYSFW_IMAGE_NAME, |
| 258 | #else |
| 259 | NULL, |
| 260 | #endif |
| 261 | #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART |
| 262 | CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART, |
| 263 | #else |
| 264 | 0, |
| 265 | #endif |
| 266 | #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT |
| 267 | CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT); |
| 268 | #else |
| 269 | 0); |
| 270 | #endif |
Andreas Dannenberg | 6da45de | 2019-08-15 15:55:29 -0500 | [diff] [blame] | 271 | break; |
| 272 | #endif |
Lokesh Vutla | a5f2756 | 2020-02-04 11:09:50 +0530 | [diff] [blame] | 273 | #if CONFIG_IS_ENABLED(SPI_LOAD) |
| 274 | case BOOT_DEVICE_SPI: |
| 275 | sysfw_load_address = k3_sysfw_get_spi_addr(); |
| 276 | if (!sysfw_load_address) |
| 277 | ret = -ENODEV; |
Andreas Dannenberg | 6da45de | 2019-08-15 15:55:29 -0500 | [diff] [blame] | 278 | break; |
| 279 | #endif |
| 280 | #if CONFIG_IS_ENABLED(YMODEM_SUPPORT) |
| 281 | case BOOT_DEVICE_UART: |
| 282 | #ifdef CONFIG_K3_EARLY_CONS |
| 283 | /* |
| 284 | * Establish a serial console if not yet available as required |
| 285 | * for UART-based boot. For this use the early console feature |
| 286 | * that allows setting up a UART for use before SYSFW has been |
| 287 | * brought up. Note that the associated UART module's clocks |
| 288 | * must have gotten enabled by the ROM bootcode which will be |
| 289 | * the case when continuing to boot serially from the same |
| 290 | * UART that the ROM loaded the initial bootloader from. |
| 291 | */ |
| 292 | if (!gd->have_console) |
| 293 | early_console_init(); |
| 294 | #endif |
| 295 | ret = spl_ymodem_load_image(&spl_image, &bootdev); |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 296 | break; |
| 297 | #endif |
Vignesh Raghavendra | 1b9d927 | 2020-01-27 17:59:24 +0530 | [diff] [blame] | 298 | #if CONFIG_IS_ENABLED(DFU) |
| 299 | case BOOT_DEVICE_DFU: |
| 300 | ret = k3_sysfw_dfu_download(sysfw_load_address); |
| 301 | break; |
| 302 | #endif |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 303 | default: |
| 304 | panic("Loading SYSFW image from device %u not supported!\n", |
| 305 | bootdev.boot_device); |
| 306 | } |
| 307 | |
| 308 | if (ret) |
| 309 | panic("Error %d occurred during loading SYSFW image!\n", ret); |
| 310 | |
| 311 | /* |
| 312 | * Now that SYSFW got loaded set helper flag to restore regular SPL |
| 313 | * loader behavior so we can later boot into the next stage as expected. |
| 314 | */ |
| 315 | sysfw_loaded = true; |
| 316 | |
| 317 | /* Ensure the SYSFW image is in FIT format */ |
| 318 | if (image_get_magic((const image_header_t *)sysfw_load_address) != |
| 319 | FDT_MAGIC) |
| 320 | panic("SYSFW image not in FIT format!\n"); |
| 321 | |
| 322 | /* Extract and start SYSFW */ |
| 323 | k3_sysfw_load_using_fit(sysfw_load_address); |
| 324 | |
| 325 | /* Get handle for accessing SYSFW services */ |
| 326 | ti_sci = get_ti_sci_handle(); |
| 327 | |
Faiz Abbas | 6839321 | 2020-02-26 13:44:36 +0530 | [diff] [blame] | 328 | if (config_pm_pre_callback) |
| 329 | config_pm_pre_callback(); |
| 330 | |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 331 | /* Parse and apply the different SYSFW configuration fragments */ |
| 332 | k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci); |
| 333 | |
| 334 | /* |
| 335 | * Now that all clocks and PM aspects are setup, invoke a user- |
| 336 | * provided callback function. Usually this callback would be used |
| 337 | * to setup or re-configure the U-Boot console UART. |
| 338 | */ |
| 339 | if (config_pm_done_callback) |
| 340 | config_pm_done_callback(); |
Andreas Dannenberg | 04e5ea8 | 2019-06-04 17:55:47 -0500 | [diff] [blame] | 341 | } |