blob: db02607b171ee52076edd98d8cecc6bd121a5aa0 [file] [log] [blame]
Andreas Dannenberg6df87062019-06-04 17:55:47 -05001// 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>
10#include <spl.h>
11#include <malloc.h>
12#include <remoteproc.h>
13#include <linux/soc/ti/ti_sci_protocol.h>
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +053014#include <g_dnl.h>
15#include <usb.h>
16#include <dfu.h>
17
Andreas Dannenberg6df87062019-06-04 17:55:47 -050018#include <asm/arch/sys_proto.h>
Andreas Dannenberg921b3252019-08-15 15:55:29 -050019#include "common.h"
20
21DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg6df87062019-06-04 17:55:47 -050022
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"
29
30static bool sysfw_loaded;
31static void *sysfw_load_address;
32
33/*
34 * Populate SPL hook to override the default load address used by the SPL
35 * loader function with a custom address for SYSFW loading.
36 */
37struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
38{
39 if (sysfw_loaded)
40 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
41 else if (sysfw_load_address)
42 return sysfw_load_address;
43 else
44 panic("SYSFW load address not defined!");
45}
46
47/*
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.
51 */
52bool spl_load_simple_fit_skip_processing(void)
53{
54 return !sysfw_loaded;
55}
56
57static int fit_get_data_by_name(const void *fit, int images, const char *name,
58 const void **addr, size_t *size)
59{
60 int node_offset;
61
62 node_offset = fdt_subnode_offset(fit, images, name);
63 if (node_offset < 0)
64 return -ENOENT;
65
66 return fit_image_get_data(fit, node_offset, addr, size);
67}
68
69static void k3_sysfw_load_using_fit(void *fit)
70{
71 int images;
72 const void *sysfw_addr;
73 size_t sysfw_size;
74 int ret;
75
76 /* Find the node holding the images information */
77 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
78 if (images < 0)
79 panic("Cannot find /images node (%d)\n", images);
80
81 /* Extract System Firmware (SYSFW) image from FIT */
82 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
83 &sysfw_addr, &sysfw_size);
84 if (ret < 0)
85 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
86 ret);
87
88 /*
89 * Start up system controller firmware
90 *
91 * It is assumed that remoteproc device 0 is the corresponding
92 * system-controller that runs SYSFW. Make sure DT reflects the same.
93 */
94 ret = rproc_dev_init(0);
95 if (ret)
96 panic("rproc failed to be initialized (%d)\n", ret);
97
98 ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
99 if (ret)
100 panic("Firmware failed to start on rproc (%d)\n", ret);
101
102 ret = rproc_start(0);
103 if (ret)
104 panic("Firmware init failed on rproc (%d)\n", ret);
105}
106
107static void k3_sysfw_configure_using_fit(void *fit,
108 struct ti_sci_handle *ti_sci)
109{
110 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
111 int images;
112 const void *cfg_fragment_addr;
113 size_t cfg_fragment_size;
114 int ret;
115
116 /* Find the node holding the images information */
117 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
118 if (images < 0)
119 panic("Cannot find /images node (%d)\n", images);
120
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);
124 if (ret < 0)
125 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
126 ret);
127
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);
132 if (ret)
133 panic("Failed to set board configuration (%d)\n", ret);
134
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);
138 if (ret < 0)
139 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
140 ret);
141
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);
146 if (ret)
147 panic("Failed to set board PM configuration (%d)\n", ret);
148
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);
152 if (ret < 0)
153 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
154 ret);
155
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);
160 if (ret)
161 panic("Failed to set board RM configuration (%d)\n", ret);
162
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);
166 if (ret < 0)
167 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
168 ret);
169
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);
174 if (ret)
175 panic("Failed to set board security configuration (%d)\n",
176 ret);
177}
178
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530179#if CONFIG_IS_ENABLED(DFU)
180static int k3_sysfw_dfu_download(void *addr)
181{
182 char dfu_str[50];
183 int ret;
184
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");
188 if (ret) {
189 dfu_free_entities();
190 goto exit;
191 }
192
193 run_usb_dnl_gadget(0, "usb_dnl_dfu");
194exit:
195 dfu_free_entities();
196 return ret;
197}
198#endif
199
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530200void k3_sysfw_loader(void (*config_pm_pre_callback) (void),
201 void (*config_pm_done_callback)(void))
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500202{
203 struct spl_image_info spl_image = { 0 };
204 struct spl_boot_device bootdev = { 0 };
205 struct ti_sci_handle *ti_sci;
206 int ret;
207
208 /* Reserve a block of aligned memory for loading the SYSFW image */
209 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
210 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
211 if (!sysfw_load_address)
212 panic("Error allocating %u bytes of memory for SYSFW image\n",
213 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
214
215 debug("%s: allocated %u bytes at 0x%p\n", __func__,
216 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
217
218 /* Set load address for legacy modes that bypass spl_get_load_buffer */
219 spl_image.load_addr = (uintptr_t)sysfw_load_address;
220
221 bootdev.boot_device = spl_boot_device();
222
223 /* Load combined System Controller firmware and config data image */
224 switch (bootdev.boot_device) {
225#if CONFIG_IS_ENABLED(MMC_SUPPORT)
226 case BOOT_DEVICE_MMC1:
227 case BOOT_DEVICE_MMC2:
228 case BOOT_DEVICE_MMC2_2:
229 ret = spl_mmc_load(&spl_image, &bootdev,
230#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
231 CONFIG_K3_SYSFW_IMAGE_NAME,
232#else
233 NULL,
234#endif
235#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
236 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
237#else
238 0,
239#endif
240#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
241 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
242#else
243 0);
244#endif
245 break;
246#endif
Andreas Dannenberg921b3252019-08-15 15:55:29 -0500247#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
248 case BOOT_DEVICE_UART:
249#ifdef CONFIG_K3_EARLY_CONS
250 /*
251 * Establish a serial console if not yet available as required
252 * for UART-based boot. For this use the early console feature
253 * that allows setting up a UART for use before SYSFW has been
254 * brought up. Note that the associated UART module's clocks
255 * must have gotten enabled by the ROM bootcode which will be
256 * the case when continuing to boot serially from the same
257 * UART that the ROM loaded the initial bootloader from.
258 */
259 if (!gd->have_console)
260 early_console_init();
261#endif
262 ret = spl_ymodem_load_image(&spl_image, &bootdev);
263 break;
264#endif
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530265#if CONFIG_IS_ENABLED(DFU)
266 case BOOT_DEVICE_DFU:
267 ret = k3_sysfw_dfu_download(sysfw_load_address);
268 break;
269#endif
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500270 default:
271 panic("Loading SYSFW image from device %u not supported!\n",
272 bootdev.boot_device);
273 }
274
275 if (ret)
276 panic("Error %d occurred during loading SYSFW image!\n", ret);
277
278 /*
279 * Now that SYSFW got loaded set helper flag to restore regular SPL
280 * loader behavior so we can later boot into the next stage as expected.
281 */
282 sysfw_loaded = true;
283
284 /* Ensure the SYSFW image is in FIT format */
285 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
286 FDT_MAGIC)
287 panic("SYSFW image not in FIT format!\n");
288
289 /* Extract and start SYSFW */
290 k3_sysfw_load_using_fit(sysfw_load_address);
291
292 /* Get handle for accessing SYSFW services */
293 ti_sci = get_ti_sci_handle();
294
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530295 if (config_pm_pre_callback)
296 config_pm_pre_callback();
297
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500298 /* Parse and apply the different SYSFW configuration fragments */
299 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
300
301 /*
302 * Now that all clocks and PM aspects are setup, invoke a user-
303 * provided callback function. Usually this callback would be used
304 * to setup or re-configure the U-Boot console UART.
305 */
306 if (config_pm_done_callback)
307 config_pm_done_callback();
308
Andreas Dannenberg0805fe12019-08-05 13:46:23 -0500309 /*
310 * Output System Firmware version info. Note that since the
311 * 'firmware_description' field is not guaranteed to be zero-
312 * terminated we manually add a \0 terminator if needed. Further
313 * note that we intentionally no longer rely on the extended
314 * printf() formatter '%.*s' to not having to require a more
315 * full-featured printf() implementation.
316 */
317 char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
318
319 strncpy(fw_desc, ti_sci->version.firmware_description,
320 sizeof(ti_sci->version.firmware_description));
321 fw_desc[sizeof(fw_desc) - 1] = '\0';
322
323 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500324 ti_sci->version.abi_major, ti_sci->version.abi_minor,
Andreas Dannenberg0805fe12019-08-05 13:46:23 -0500325 ti_sci->version.firmware_revision, fw_desc);
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500326}