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>
16 /* Name of the FIT image nodes for SYSFW and its config data */
17 #define SYSFW_FIRMWARE "sysfw.bin"
18 #define SYSFW_CFG_BOARD "board-cfg.bin"
19 #define SYSFW_CFG_PM "pm-cfg.bin"
20 #define SYSFW_CFG_RM "rm-cfg.bin"
21 #define SYSFW_CFG_SEC "sec-cfg.bin"
23 static bool sysfw_loaded;
24 static void *sysfw_load_address;
27 * Populate SPL hook to override the default load address used by the SPL
28 * loader function with a custom address for SYSFW loading.
30 struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
33 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
34 else if (sysfw_load_address)
35 return sysfw_load_address;
37 panic("SYSFW load address not defined!");
41 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
42 * during SYSFW loading and return to the calling function so we can perform
43 * our own custom processing.
45 bool spl_load_simple_fit_skip_processing(void)
50 static int fit_get_data_by_name(const void *fit, int images, const char *name,
51 const void **addr, size_t *size)
55 node_offset = fdt_subnode_offset(fit, images, name);
59 return fit_image_get_data(fit, node_offset, addr, size);
62 static void k3_sysfw_load_using_fit(void *fit)
65 const void *sysfw_addr;
69 /* Find the node holding the images information */
70 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
72 panic("Cannot find /images node (%d)\n", images);
74 /* Extract System Firmware (SYSFW) image from FIT */
75 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
76 &sysfw_addr, &sysfw_size);
78 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
82 * Start up system controller firmware
84 * It is assumed that remoteproc device 0 is the corresponding
85 * system-controller that runs SYSFW. Make sure DT reflects the same.
87 ret = rproc_dev_init(0);
89 panic("rproc failed to be initialized (%d)\n", ret);
91 ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
93 panic("Firmware failed to start on rproc (%d)\n", ret);
97 panic("Firmware init failed on rproc (%d)\n", ret);
100 static void k3_sysfw_configure_using_fit(void *fit,
101 struct ti_sci_handle *ti_sci)
103 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
105 const void *cfg_fragment_addr;
106 size_t cfg_fragment_size;
109 /* Find the node holding the images information */
110 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
112 panic("Cannot find /images node (%d)\n", images);
114 /* Extract board configuration from FIT */
115 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
116 &cfg_fragment_addr, &cfg_fragment_size);
118 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
121 /* Apply board configuration to SYSFW */
122 ret = board_ops->board_config(ti_sci,
123 (u64)(u32)cfg_fragment_addr,
124 (u32)cfg_fragment_size);
126 panic("Failed to set board configuration (%d)\n", ret);
128 /* Extract power/clock (PM) specific configuration from FIT */
129 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
130 &cfg_fragment_addr, &cfg_fragment_size);
132 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
135 /* Apply power/clock (PM) specific configuration to SYSFW */
136 ret = board_ops->board_config_pm(ti_sci,
137 (u64)(u32)cfg_fragment_addr,
138 (u32)cfg_fragment_size);
140 panic("Failed to set board PM configuration (%d)\n", ret);
142 /* Extract resource management (RM) specific configuration from FIT */
143 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
144 &cfg_fragment_addr, &cfg_fragment_size);
146 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
149 /* Apply resource management (RM) configuration to SYSFW */
150 ret = board_ops->board_config_rm(ti_sci,
151 (u64)(u32)cfg_fragment_addr,
152 (u32)cfg_fragment_size);
154 panic("Failed to set board RM configuration (%d)\n", ret);
156 /* Extract security specific configuration from FIT */
157 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
158 &cfg_fragment_addr, &cfg_fragment_size);
160 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
163 /* Apply security configuration to SYSFW */
164 ret = board_ops->board_config_security(ti_sci,
165 (u64)(u32)cfg_fragment_addr,
166 (u32)cfg_fragment_size);
168 panic("Failed to set board security configuration (%d)\n",
172 void k3_sysfw_loader(void (*config_pm_done_callback)(void))
174 struct spl_image_info spl_image = { 0 };
175 struct spl_boot_device bootdev = { 0 };
176 struct ti_sci_handle *ti_sci;
179 /* Reserve a block of aligned memory for loading the SYSFW image */
180 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
181 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
182 if (!sysfw_load_address)
183 panic("Error allocating %u bytes of memory for SYSFW image\n",
184 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
186 debug("%s: allocated %u bytes at 0x%p\n", __func__,
187 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
189 /* Set load address for legacy modes that bypass spl_get_load_buffer */
190 spl_image.load_addr = (uintptr_t)sysfw_load_address;
192 bootdev.boot_device = spl_boot_device();
194 /* Load combined System Controller firmware and config data image */
195 switch (bootdev.boot_device) {
196 #if CONFIG_IS_ENABLED(MMC_SUPPORT)
197 case BOOT_DEVICE_MMC1:
198 case BOOT_DEVICE_MMC2:
199 case BOOT_DEVICE_MMC2_2:
200 ret = spl_mmc_load(&spl_image, &bootdev,
201 #ifdef CONFIG_K3_SYSFW_IMAGE_NAME
202 CONFIG_K3_SYSFW_IMAGE_NAME,
206 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
207 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
211 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
212 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
219 panic("Loading SYSFW image from device %u not supported!\n",
220 bootdev.boot_device);
224 panic("Error %d occurred during loading SYSFW image!\n", ret);
227 * Now that SYSFW got loaded set helper flag to restore regular SPL
228 * loader behavior so we can later boot into the next stage as expected.
232 /* Ensure the SYSFW image is in FIT format */
233 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
235 panic("SYSFW image not in FIT format!\n");
237 /* Extract and start SYSFW */
238 k3_sysfw_load_using_fit(sysfw_load_address);
240 /* Get handle for accessing SYSFW services */
241 ti_sci = get_ti_sci_handle();
243 /* Parse and apply the different SYSFW configuration fragments */
244 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
247 * Now that all clocks and PM aspects are setup, invoke a user-
248 * provided callback function. Usually this callback would be used
249 * to setup or re-configure the U-Boot console UART.
251 if (config_pm_done_callback)
252 config_pm_done_callback();
254 /* Output System Firmware version info */
255 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%.*s')\n",
256 ti_sci->version.abi_major, ti_sci->version.abi_minor,
257 ti_sci->version.firmware_revision,
258 sizeof(ti_sci->version.firmware_description),
259 ti_sci->version.firmware_description);