1 // SPDX-License-Identifier: GPL-2.0+
3 * K3: System Firmware Loader
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Andreas Dannenberg <dannenberg@ti.com>
12 #include <remoteproc.h>
13 #include <linux/soc/ti/ti_sci_protocol.h>
18 #include <asm/arch/sys_proto.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* Name of the FIT image nodes for SYSFW and its config data */
24 #define SYSFW_FIRMWARE "sysfw.bin"
25 #define SYSFW_CFG_BOARD "board-cfg.bin"
26 #define SYSFW_CFG_PM "pm-cfg.bin"
27 #define SYSFW_CFG_RM "rm-cfg.bin"
28 #define SYSFW_CFG_SEC "sec-cfg.bin"
30 static bool sysfw_loaded;
31 static void *sysfw_load_address;
34 * Populate SPL hook to override the default load address used by the SPL
35 * loader function with a custom address for SYSFW loading.
37 struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
40 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
41 else if (sysfw_load_address)
42 return sysfw_load_address;
44 panic("SYSFW load address not defined!");
48 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
49 * during SYSFW loading and return to the calling function so we can perform
50 * our own custom processing.
52 bool spl_load_simple_fit_skip_processing(void)
57 static int fit_get_data_by_name(const void *fit, int images, const char *name,
58 const void **addr, size_t *size)
62 node_offset = fdt_subnode_offset(fit, images, name);
66 return fit_image_get_data(fit, node_offset, addr, size);
69 static void k3_sysfw_load_using_fit(void *fit)
72 const void *sysfw_addr;
76 /* Find the node holding the images information */
77 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
79 panic("Cannot find /images node (%d)\n", images);
81 /* Extract System Firmware (SYSFW) image from FIT */
82 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
83 &sysfw_addr, &sysfw_size);
85 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
89 * Start up system controller firmware
91 * It is assumed that remoteproc device 0 is the corresponding
92 * system-controller that runs SYSFW. Make sure DT reflects the same.
94 ret = rproc_dev_init(0);
96 panic("rproc failed to be initialized (%d)\n", ret);
98 ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
100 panic("Firmware failed to start on rproc (%d)\n", ret);
102 ret = rproc_start(0);
104 panic("Firmware init failed on rproc (%d)\n", ret);
107 static void k3_sysfw_configure_using_fit(void *fit,
108 struct ti_sci_handle *ti_sci)
110 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
112 const void *cfg_fragment_addr;
113 size_t cfg_fragment_size;
116 /* Find the node holding the images information */
117 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
119 panic("Cannot find /images node (%d)\n", images);
121 /* Extract board configuration from FIT */
122 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
123 &cfg_fragment_addr, &cfg_fragment_size);
125 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
128 /* Apply board configuration to SYSFW */
129 ret = board_ops->board_config(ti_sci,
130 (u64)(u32)cfg_fragment_addr,
131 (u32)cfg_fragment_size);
133 panic("Failed to set board configuration (%d)\n", ret);
135 /* Extract power/clock (PM) specific configuration from FIT */
136 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
137 &cfg_fragment_addr, &cfg_fragment_size);
139 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
142 /* Apply power/clock (PM) specific configuration to SYSFW */
143 ret = board_ops->board_config_pm(ti_sci,
144 (u64)(u32)cfg_fragment_addr,
145 (u32)cfg_fragment_size);
147 panic("Failed to set board PM configuration (%d)\n", ret);
149 /* Extract resource management (RM) specific configuration from FIT */
150 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
151 &cfg_fragment_addr, &cfg_fragment_size);
153 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
156 /* Apply resource management (RM) configuration to SYSFW */
157 ret = board_ops->board_config_rm(ti_sci,
158 (u64)(u32)cfg_fragment_addr,
159 (u32)cfg_fragment_size);
161 panic("Failed to set board RM configuration (%d)\n", ret);
163 /* Extract security specific configuration from FIT */
164 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
165 &cfg_fragment_addr, &cfg_fragment_size);
167 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
170 /* Apply security configuration to SYSFW */
171 ret = board_ops->board_config_security(ti_sci,
172 (u64)(u32)cfg_fragment_addr,
173 (u32)cfg_fragment_size);
175 panic("Failed to set board security configuration (%d)\n",
179 #if CONFIG_IS_ENABLED(DFU)
180 static int k3_sysfw_dfu_download(void *addr)
185 sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
186 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
187 ret = dfu_config_entities(dfu_str, "ram", "0");
193 run_usb_dnl_gadget(0, "usb_dnl_dfu");
200 void k3_sysfw_loader(void (*config_pm_done_callback)(void))
202 struct spl_image_info spl_image = { 0 };
203 struct spl_boot_device bootdev = { 0 };
204 struct ti_sci_handle *ti_sci;
207 /* Reserve a block of aligned memory for loading the SYSFW image */
208 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
209 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
210 if (!sysfw_load_address)
211 panic("Error allocating %u bytes of memory for SYSFW image\n",
212 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
214 debug("%s: allocated %u bytes at 0x%p\n", __func__,
215 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
217 /* Set load address for legacy modes that bypass spl_get_load_buffer */
218 spl_image.load_addr = (uintptr_t)sysfw_load_address;
220 bootdev.boot_device = spl_boot_device();
222 /* Load combined System Controller firmware and config data image */
223 switch (bootdev.boot_device) {
224 #if CONFIG_IS_ENABLED(MMC_SUPPORT)
225 case BOOT_DEVICE_MMC1:
226 case BOOT_DEVICE_MMC2:
227 case BOOT_DEVICE_MMC2_2:
228 ret = spl_mmc_load(&spl_image, &bootdev,
229 #ifdef CONFIG_K3_SYSFW_IMAGE_NAME
230 CONFIG_K3_SYSFW_IMAGE_NAME,
234 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
235 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
239 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
240 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
246 #if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
247 case BOOT_DEVICE_UART:
248 #ifdef CONFIG_K3_EARLY_CONS
250 * Establish a serial console if not yet available as required
251 * for UART-based boot. For this use the early console feature
252 * that allows setting up a UART for use before SYSFW has been
253 * brought up. Note that the associated UART module's clocks
254 * must have gotten enabled by the ROM bootcode which will be
255 * the case when continuing to boot serially from the same
256 * UART that the ROM loaded the initial bootloader from.
258 if (!gd->have_console)
259 early_console_init();
261 ret = spl_ymodem_load_image(&spl_image, &bootdev);
264 #if CONFIG_IS_ENABLED(DFU)
265 case BOOT_DEVICE_DFU:
266 ret = k3_sysfw_dfu_download(sysfw_load_address);
270 panic("Loading SYSFW image from device %u not supported!\n",
271 bootdev.boot_device);
275 panic("Error %d occurred during loading SYSFW image!\n", ret);
278 * Now that SYSFW got loaded set helper flag to restore regular SPL
279 * loader behavior so we can later boot into the next stage as expected.
283 /* Ensure the SYSFW image is in FIT format */
284 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
286 panic("SYSFW image not in FIT format!\n");
288 /* Extract and start SYSFW */
289 k3_sysfw_load_using_fit(sysfw_load_address);
291 /* Get handle for accessing SYSFW services */
292 ti_sci = get_ti_sci_handle();
294 /* Parse and apply the different SYSFW configuration fragments */
295 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
298 * Now that all clocks and PM aspects are setup, invoke a user-
299 * provided callback function. Usually this callback would be used
300 * to setup or re-configure the U-Boot console UART.
302 if (config_pm_done_callback)
303 config_pm_done_callback();
306 * Output System Firmware version info. Note that since the
307 * 'firmware_description' field is not guaranteed to be zero-
308 * terminated we manually add a \0 terminator if needed. Further
309 * note that we intentionally no longer rely on the extended
310 * printf() formatter '%.*s' to not having to require a more
311 * full-featured printf() implementation.
313 char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
315 strncpy(fw_desc, ti_sci->version.firmware_description,
316 sizeof(ti_sci->version.firmware_description));
317 fw_desc[sizeof(fw_desc) - 1] = '\0';
319 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
320 ti_sci->version.abi_major, ti_sci->version.abi_minor,
321 ti_sci->version.firmware_revision, fw_desc);