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>
14 #include <asm/arch/sys_proto.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* Name of the FIT image nodes for SYSFW and its config data */
20 #define SYSFW_FIRMWARE "sysfw.bin"
21 #define SYSFW_CFG_BOARD "board-cfg.bin"
22 #define SYSFW_CFG_PM "pm-cfg.bin"
23 #define SYSFW_CFG_RM "rm-cfg.bin"
24 #define SYSFW_CFG_SEC "sec-cfg.bin"
26 static bool sysfw_loaded;
27 static void *sysfw_load_address;
30 * Populate SPL hook to override the default load address used by the SPL
31 * loader function with a custom address for SYSFW loading.
33 struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
36 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
37 else if (sysfw_load_address)
38 return sysfw_load_address;
40 panic("SYSFW load address not defined!");
44 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
45 * during SYSFW loading and return to the calling function so we can perform
46 * our own custom processing.
48 bool spl_load_simple_fit_skip_processing(void)
53 static int fit_get_data_by_name(const void *fit, int images, const char *name,
54 const void **addr, size_t *size)
58 node_offset = fdt_subnode_offset(fit, images, name);
62 return fit_image_get_data(fit, node_offset, addr, size);
65 static void k3_sysfw_load_using_fit(void *fit)
68 const void *sysfw_addr;
72 /* Find the node holding the images information */
73 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
75 panic("Cannot find /images node (%d)\n", images);
77 /* Extract System Firmware (SYSFW) image from FIT */
78 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
79 &sysfw_addr, &sysfw_size);
81 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
85 * Start up system controller firmware
87 * It is assumed that remoteproc device 0 is the corresponding
88 * system-controller that runs SYSFW. Make sure DT reflects the same.
90 ret = rproc_dev_init(0);
92 panic("rproc failed to be initialized (%d)\n", ret);
94 ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
96 panic("Firmware failed to start on rproc (%d)\n", ret);
100 panic("Firmware init failed on rproc (%d)\n", ret);
103 static void k3_sysfw_configure_using_fit(void *fit,
104 struct ti_sci_handle *ti_sci)
106 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
108 const void *cfg_fragment_addr;
109 size_t cfg_fragment_size;
112 /* Find the node holding the images information */
113 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
115 panic("Cannot find /images node (%d)\n", images);
117 /* Extract board configuration from FIT */
118 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
119 &cfg_fragment_addr, &cfg_fragment_size);
121 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
124 /* Apply board configuration to SYSFW */
125 ret = board_ops->board_config(ti_sci,
126 (u64)(u32)cfg_fragment_addr,
127 (u32)cfg_fragment_size);
129 panic("Failed to set board configuration (%d)\n", ret);
131 /* Extract power/clock (PM) specific configuration from FIT */
132 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
133 &cfg_fragment_addr, &cfg_fragment_size);
135 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
138 /* Apply power/clock (PM) specific configuration to SYSFW */
139 ret = board_ops->board_config_pm(ti_sci,
140 (u64)(u32)cfg_fragment_addr,
141 (u32)cfg_fragment_size);
143 panic("Failed to set board PM configuration (%d)\n", ret);
145 /* Extract resource management (RM) specific configuration from FIT */
146 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
147 &cfg_fragment_addr, &cfg_fragment_size);
149 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
152 /* Apply resource management (RM) configuration to SYSFW */
153 ret = board_ops->board_config_rm(ti_sci,
154 (u64)(u32)cfg_fragment_addr,
155 (u32)cfg_fragment_size);
157 panic("Failed to set board RM configuration (%d)\n", ret);
159 /* Extract security specific configuration from FIT */
160 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
161 &cfg_fragment_addr, &cfg_fragment_size);
163 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
166 /* Apply security configuration to SYSFW */
167 ret = board_ops->board_config_security(ti_sci,
168 (u64)(u32)cfg_fragment_addr,
169 (u32)cfg_fragment_size);
171 panic("Failed to set board security configuration (%d)\n",
175 void k3_sysfw_loader(void (*config_pm_done_callback)(void))
177 struct spl_image_info spl_image = { 0 };
178 struct spl_boot_device bootdev = { 0 };
179 struct ti_sci_handle *ti_sci;
182 /* Reserve a block of aligned memory for loading the SYSFW image */
183 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
184 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
185 if (!sysfw_load_address)
186 panic("Error allocating %u bytes of memory for SYSFW image\n",
187 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
189 debug("%s: allocated %u bytes at 0x%p\n", __func__,
190 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
192 /* Set load address for legacy modes that bypass spl_get_load_buffer */
193 spl_image.load_addr = (uintptr_t)sysfw_load_address;
195 bootdev.boot_device = spl_boot_device();
197 /* Load combined System Controller firmware and config data image */
198 switch (bootdev.boot_device) {
199 #if CONFIG_IS_ENABLED(MMC_SUPPORT)
200 case BOOT_DEVICE_MMC1:
201 case BOOT_DEVICE_MMC2:
202 case BOOT_DEVICE_MMC2_2:
203 ret = spl_mmc_load(&spl_image, &bootdev,
204 #ifdef CONFIG_K3_SYSFW_IMAGE_NAME
205 CONFIG_K3_SYSFW_IMAGE_NAME,
209 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
210 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
214 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
215 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
221 #if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
222 case BOOT_DEVICE_UART:
223 #ifdef CONFIG_K3_EARLY_CONS
225 * Establish a serial console if not yet available as required
226 * for UART-based boot. For this use the early console feature
227 * that allows setting up a UART for use before SYSFW has been
228 * brought up. Note that the associated UART module's clocks
229 * must have gotten enabled by the ROM bootcode which will be
230 * the case when continuing to boot serially from the same
231 * UART that the ROM loaded the initial bootloader from.
233 if (!gd->have_console)
234 early_console_init();
236 ret = spl_ymodem_load_image(&spl_image, &bootdev);
240 panic("Loading SYSFW image from device %u not supported!\n",
241 bootdev.boot_device);
245 panic("Error %d occurred during loading SYSFW image!\n", ret);
248 * Now that SYSFW got loaded set helper flag to restore regular SPL
249 * loader behavior so we can later boot into the next stage as expected.
253 /* Ensure the SYSFW image is in FIT format */
254 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
256 panic("SYSFW image not in FIT format!\n");
258 /* Extract and start SYSFW */
259 k3_sysfw_load_using_fit(sysfw_load_address);
261 /* Get handle for accessing SYSFW services */
262 ti_sci = get_ti_sci_handle();
264 /* Parse and apply the different SYSFW configuration fragments */
265 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
268 * Now that all clocks and PM aspects are setup, invoke a user-
269 * provided callback function. Usually this callback would be used
270 * to setup or re-configure the U-Boot console UART.
272 if (config_pm_done_callback)
273 config_pm_done_callback();
276 * Output System Firmware version info. Note that since the
277 * 'firmware_description' field is not guaranteed to be zero-
278 * terminated we manually add a \0 terminator if needed. Further
279 * note that we intentionally no longer rely on the extended
280 * printf() formatter '%.*s' to not having to require a more
281 * full-featured printf() implementation.
283 char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
285 strncpy(fw_desc, ti_sci->version.firmware_description,
286 sizeof(ti_sci->version.firmware_description));
287 fw_desc[sizeof(fw_desc) - 1] = '\0';
289 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
290 ti_sci->version.abi_major, ti_sci->version.abi_minor,
291 ti_sci->version.firmware_revision, fw_desc);