blob: 94dbeb9437d9472ca3e469a741c82156bf44c896 [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
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500200void k3_sysfw_loader(void (*config_pm_done_callback)(void))
201{
202 struct spl_image_info spl_image = { 0 };
203 struct spl_boot_device bootdev = { 0 };
204 struct ti_sci_handle *ti_sci;
205 int ret;
206
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);
213
214 debug("%s: allocated %u bytes at 0x%p\n", __func__,
215 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
216
217 /* Set load address for legacy modes that bypass spl_get_load_buffer */
218 spl_image.load_addr = (uintptr_t)sysfw_load_address;
219
220 bootdev.boot_device = spl_boot_device();
221
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,
231#else
232 NULL,
233#endif
234#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
235 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
236#else
237 0,
238#endif
239#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
240 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
241#else
242 0);
243#endif
244 break;
245#endif
Andreas Dannenberg921b3252019-08-15 15:55:29 -0500246#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
247 case BOOT_DEVICE_UART:
248#ifdef CONFIG_K3_EARLY_CONS
249 /*
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.
257 */
258 if (!gd->have_console)
259 early_console_init();
260#endif
261 ret = spl_ymodem_load_image(&spl_image, &bootdev);
262 break;
263#endif
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530264#if CONFIG_IS_ENABLED(DFU)
265 case BOOT_DEVICE_DFU:
266 ret = k3_sysfw_dfu_download(sysfw_load_address);
267 break;
268#endif
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500269 default:
270 panic("Loading SYSFW image from device %u not supported!\n",
271 bootdev.boot_device);
272 }
273
274 if (ret)
275 panic("Error %d occurred during loading SYSFW image!\n", ret);
276
277 /*
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.
280 */
281 sysfw_loaded = true;
282
283 /* Ensure the SYSFW image is in FIT format */
284 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
285 FDT_MAGIC)
286 panic("SYSFW image not in FIT format!\n");
287
288 /* Extract and start SYSFW */
289 k3_sysfw_load_using_fit(sysfw_load_address);
290
291 /* Get handle for accessing SYSFW services */
292 ti_sci = get_ti_sci_handle();
293
294 /* Parse and apply the different SYSFW configuration fragments */
295 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
296
297 /*
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.
301 */
302 if (config_pm_done_callback)
303 config_pm_done_callback();
304
Andreas Dannenberg0805fe12019-08-05 13:46:23 -0500305 /*
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.
312 */
313 char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
314
315 strncpy(fw_desc, ti_sci->version.firmware_description,
316 sizeof(ti_sci->version.firmware_description));
317 fw_desc[sizeof(fw_desc) - 1] = '\0';
318
319 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500320 ti_sci->version.abi_major, ti_sci->version.abi_minor,
Andreas Dannenberg0805fe12019-08-05 13:46:23 -0500321 ti_sci->version.firmware_revision, fw_desc);
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500322}