blob: c4c5c3711009baef3cec1557e428d7365c2de56f [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>
Simon Glassa00867b2020-07-19 10:15:40 -060010#include <dm.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060011#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Andreas Dannenberg6df87062019-06-04 17:55:47 -050013#include <spl.h>
14#include <malloc.h>
15#include <remoteproc.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Andreas Dannenberg6df87062019-06-04 17:55:47 -050018#include <linux/soc/ti/ti_sci_protocol.h>
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +053019#include <g_dnl.h>
20#include <usb.h>
21#include <dfu.h>
Lokesh Vutla7d0866b2020-02-04 11:09:50 +053022#include <dm/uclass-internal.h>
23#include <spi_flash.h>
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +053024
Vignesh Raghavendra58421632021-12-23 19:26:03 +053025#include <asm/io.h>
Andreas Dannenberg6df87062019-06-04 17:55:47 -050026#include <asm/arch/sys_proto.h>
Andreas Dannenberg921b3252019-08-15 15:55:29 -050027#include "common.h"
28
29DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg6df87062019-06-04 17:55:47 -050030
31/* Name of the FIT image nodes for SYSFW and its config data */
32#define SYSFW_FIRMWARE "sysfw.bin"
33#define SYSFW_CFG_BOARD "board-cfg.bin"
34#define SYSFW_CFG_PM "pm-cfg.bin"
35#define SYSFW_CFG_RM "rm-cfg.bin"
36#define SYSFW_CFG_SEC "sec-cfg.bin"
37
Lokesh Vutladc57a552020-08-05 22:44:18 +053038/*
39 * It is assumed that remoteproc device 0 is the corresponding
40 * system-controller that runs SYSFW. Make sure DT reflects the same.
41 */
42#define K3_SYSTEM_CONTROLLER_RPROC_ID 0
43
Tero Kristo925698d2021-06-11 11:45:22 +030044#define COMMON_HEADER_ADDRESS 0x41cffb00
45#define BOARDCFG_ADDRESS 0x41c80000
46
47#define COMP_TYPE_SBL_DATA 0x11
48#define DESC_TYPE_BOARDCFG_PM_INDEX 0x2
49#define DESC_TYPE_BOARDCFG_RM_INDEX 0x3
50
51#define BOARD_CONFIG_RM_DESC_TYPE 0x000c
52#define BOARD_CONFIG_PM_DESC_TYPE 0x000e
53
54struct extboot_comp {
55 u32 comp_type;
56 u32 boot_core;
57 u32 comp_opts;
58 u64 dest_addr;
59 u32 comp_size;
60};
61
62struct extboot_header {
63 u8 magic[8];
64 u32 num_comps;
65 struct extboot_comp comps[5];
66 u32 reserved;
67};
68
69struct bcfg_desc {
70 u16 type;
71 u16 offset;
72 u16 size;
73 u8 devgrp;
74 u8 reserved;
75} __packed;
76
77struct bcfg_header {
78 u8 num_elems;
79 u8 sw_rev;
80 struct bcfg_desc descs[4];
81 u16 reserved;
82} __packed;
83
Andreas Dannenberg6df87062019-06-04 17:55:47 -050084static bool sysfw_loaded;
85static void *sysfw_load_address;
86
87/*
88 * Populate SPL hook to override the default load address used by the SPL
89 * loader function with a custom address for SYSFW loading.
90 */
Simon Glassf3543e62022-09-06 20:26:52 -060091struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
Andreas Dannenberg6df87062019-06-04 17:55:47 -050092{
93 if (sysfw_loaded)
Simon Glass98463902022-10-20 18:22:39 -060094 return (struct legacy_img_hdr *)(CONFIG_TEXT_BASE + offset);
Andreas Dannenberg6df87062019-06-04 17:55:47 -050095 else if (sysfw_load_address)
96 return sysfw_load_address;
97 else
98 panic("SYSFW load address not defined!");
99}
100
101/*
102 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
103 * during SYSFW loading and return to the calling function so we can perform
104 * our own custom processing.
105 */
106bool spl_load_simple_fit_skip_processing(void)
107{
108 return !sysfw_loaded;
109}
110
111static int fit_get_data_by_name(const void *fit, int images, const char *name,
112 const void **addr, size_t *size)
113{
114 int node_offset;
115
116 node_offset = fdt_subnode_offset(fit, images, name);
117 if (node_offset < 0)
118 return -ENOENT;
119
120 return fit_image_get_data(fit, node_offset, addr, size);
121}
122
Lokesh Vutladc57a552020-08-05 22:44:18 +0530123static void k3_start_system_controller(int rproc_id, bool rproc_loaded,
124 ulong addr, ulong size)
125{
126 int ret;
127
128 ret = rproc_dev_init(rproc_id);
129 if (ret)
130 panic("rproc failed to be initialized (%d)\n", ret);
131
132 if (!rproc_loaded) {
133 ret = rproc_load(rproc_id, addr, size);
134 if (ret)
135 panic("Firmware failed to start on rproc (%d)\n", ret);
136 }
137
138 ret = rproc_start(0);
139 if (ret)
140 panic("Firmware init failed on rproc (%d)\n", ret);
141}
142
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500143static void k3_sysfw_load_using_fit(void *fit)
144{
145 int images;
146 const void *sysfw_addr;
147 size_t sysfw_size;
148 int ret;
149
150 /* Find the node holding the images information */
151 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
152 if (images < 0)
153 panic("Cannot find /images node (%d)\n", images);
154
155 /* Extract System Firmware (SYSFW) image from FIT */
156 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
157 &sysfw_addr, &sysfw_size);
158 if (ret < 0)
159 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
160 ret);
161
Lokesh Vutladc57a552020-08-05 22:44:18 +0530162 /* Start up system controller firmware */
163 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID, false,
164 (ulong)sysfw_addr, (ulong)sysfw_size);
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500165}
166
167static void k3_sysfw_configure_using_fit(void *fit,
168 struct ti_sci_handle *ti_sci)
169{
170 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
171 int images;
172 const void *cfg_fragment_addr;
173 size_t cfg_fragment_size;
174 int ret;
Tero Kristo925698d2021-06-11 11:45:22 +0300175 u8 *buf;
176 struct extboot_header *common_header;
177 struct bcfg_header *bcfg_header;
178 struct extboot_comp *comp;
179 struct bcfg_desc *desc;
180 u32 addr;
181 bool copy_bcfg = false;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500182
183 /* Find the node holding the images information */
184 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
185 if (images < 0)
186 panic("Cannot find /images node (%d)\n", images);
187
188 /* Extract board configuration from FIT */
189 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
190 &cfg_fragment_addr, &cfg_fragment_size);
191 if (ret < 0)
192 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
193 ret);
194
195 /* Apply board configuration to SYSFW */
196 ret = board_ops->board_config(ti_sci,
197 (u64)(u32)cfg_fragment_addr,
198 (u32)cfg_fragment_size);
199 if (ret)
200 panic("Failed to set board configuration (%d)\n", ret);
201
202 /* Extract power/clock (PM) specific configuration from FIT */
203 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
204 &cfg_fragment_addr, &cfg_fragment_size);
205 if (ret < 0)
206 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
207 ret);
208
209 /* Apply power/clock (PM) specific configuration to SYSFW */
Tero Kristo925698d2021-06-11 11:45:22 +0300210 if (!IS_ENABLED(CONFIG_K3_DM_FW)) {
211 ret = board_ops->board_config_pm(ti_sci,
212 (u64)(u32)cfg_fragment_addr,
213 (u32)cfg_fragment_size);
214 if (ret)
215 panic("Failed to set board PM configuration (%d)\n", ret);
216 } else {
217 /* Initialize shared memory boardconfig buffer */
218 buf = (u8 *)COMMON_HEADER_ADDRESS;
219 common_header = (struct extboot_header *)buf;
220
221 /* Check if we have a struct populated by ROM in memory already */
222 if (strcmp((char *)common_header->magic, "EXTBOOT"))
223 copy_bcfg = true;
224
225 if (copy_bcfg) {
226 strcpy((char *)common_header->magic, "EXTBOOT");
227 common_header->num_comps = 1;
228
229 comp = &common_header->comps[0];
230
231 comp->comp_type = COMP_TYPE_SBL_DATA;
232 comp->boot_core = 0x10;
233 comp->comp_opts = 0;
234 addr = (u32)BOARDCFG_ADDRESS;
235 comp->dest_addr = addr;
236 comp->comp_size = sizeof(*bcfg_header);
237
238 bcfg_header = (struct bcfg_header *)addr;
239
240 bcfg_header->num_elems = 2;
241 bcfg_header->sw_rev = 0;
242
243 desc = &bcfg_header->descs[0];
244
245 desc->type = BOARD_CONFIG_PM_DESC_TYPE;
246 desc->offset = sizeof(*bcfg_header);
247 desc->size = cfg_fragment_size;
248 comp->comp_size += desc->size;
249 desc->devgrp = 0;
250 desc->reserved = 0;
251 memcpy((u8 *)bcfg_header + desc->offset,
252 cfg_fragment_addr, cfg_fragment_size);
253
254 bcfg_header->descs[1].offset = desc->offset + desc->size;
255 }
256 }
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500257
258 /* Extract resource management (RM) specific configuration from FIT */
259 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
260 &cfg_fragment_addr, &cfg_fragment_size);
261 if (ret < 0)
262 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
263 ret);
264
Tero Kristo925698d2021-06-11 11:45:22 +0300265 if (copy_bcfg) {
266 desc = &bcfg_header->descs[1];
267
268 desc->type = BOARD_CONFIG_RM_DESC_TYPE;
269 desc->size = cfg_fragment_size;
270 comp->comp_size += desc->size;
271 desc->devgrp = 0;
272 desc->reserved = 0;
273 memcpy((u8 *)bcfg_header + desc->offset, cfg_fragment_addr,
274 cfg_fragment_size);
275 }
276
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500277 /* Apply resource management (RM) configuration to SYSFW */
278 ret = board_ops->board_config_rm(ti_sci,
279 (u64)(u32)cfg_fragment_addr,
280 (u32)cfg_fragment_size);
281 if (ret)
282 panic("Failed to set board RM configuration (%d)\n", ret);
283
284 /* Extract security specific configuration from FIT */
285 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
286 &cfg_fragment_addr, &cfg_fragment_size);
287 if (ret < 0)
288 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
289 ret);
290
291 /* Apply security configuration to SYSFW */
292 ret = board_ops->board_config_security(ti_sci,
293 (u64)(u32)cfg_fragment_addr,
294 (u32)cfg_fragment_size);
295 if (ret)
296 panic("Failed to set board security configuration (%d)\n",
297 ret);
298}
299
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530300#if CONFIG_IS_ENABLED(DFU)
301static int k3_sysfw_dfu_download(void *addr)
302{
303 char dfu_str[50];
304 int ret;
305
306 sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
307 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
308 ret = dfu_config_entities(dfu_str, "ram", "0");
309 if (ret) {
310 dfu_free_entities();
311 goto exit;
312 }
313
314 run_usb_dnl_gadget(0, "usb_dnl_dfu");
315exit:
316 dfu_free_entities();
317 return ret;
318}
319#endif
320
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530321#if CONFIG_IS_ENABLED(SPI_LOAD)
322static void *k3_sysfw_get_spi_addr(void)
323{
324 struct udevice *dev;
325 fdt_addr_t addr;
326 int ret;
Vaishnav Achathc16b4f12022-06-03 11:32:16 +0530327 unsigned int sf_bus = spl_spi_boot_bus();
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530328
Vaishnav Achathc16b4f12022-06-03 11:32:16 +0530329 ret = uclass_find_device_by_seq(UCLASS_SPI, sf_bus, &dev);
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530330 if (ret)
331 return NULL;
332
333 addr = dev_read_addr_index(dev, 1);
334 if (addr == FDT_ADDR_T_NONE)
335 return NULL;
336
337 return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
338}
Vignesh Raghavendra58421632021-12-23 19:26:03 +0530339
340static void k3_sysfw_spi_copy(u32 *dst, u32 *src, size_t len)
341{
342 size_t i;
343
344 for (i = 0; i < len / sizeof(*dst); i++)
345 *dst++ = *src++;
346}
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530347#endif
348
Vaishnav Achath09d14d72022-05-09 11:50:14 +0530349#if CONFIG_IS_ENABLED(NOR_SUPPORT)
350static void *get_sysfw_hf_addr(void)
351{
352 struct udevice *dev;
353 fdt_addr_t addr;
354 int ret;
355
356 ret = uclass_find_first_device(UCLASS_MTD, &dev);
357 if (ret)
358 return NULL;
359
360 addr = dev_read_addr_index(dev, 1);
361 if (addr == FDT_ADDR_T_NONE)
362 return NULL;
363
364 return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
365}
366#endif
367
Lokesh Vutladc57a552020-08-05 22:44:18 +0530368void k3_sysfw_loader(bool rom_loaded_sysfw,
369 void (*config_pm_pre_callback)(void),
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530370 void (*config_pm_done_callback)(void))
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500371{
372 struct spl_image_info spl_image = { 0 };
373 struct spl_boot_device bootdev = { 0 };
374 struct ti_sci_handle *ti_sci;
Vignesh Raghavendra58421632021-12-23 19:26:03 +0530375#if CONFIG_IS_ENABLED(SPI_LOAD)
376 void *sysfw_spi_base;
377#endif
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530378 int ret = 0;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500379
Lokesh Vutladc57a552020-08-05 22:44:18 +0530380 if (rom_loaded_sysfw) {
381 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID,
382 rom_loaded_sysfw, 0, 0);
383 sysfw_loaded = true;
384 return;
385 }
386
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500387 /* Reserve a block of aligned memory for loading the SYSFW image */
388 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
389 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
390 if (!sysfw_load_address)
391 panic("Error allocating %u bytes of memory for SYSFW image\n",
392 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
393
394 debug("%s: allocated %u bytes at 0x%p\n", __func__,
395 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
396
397 /* Set load address for legacy modes that bypass spl_get_load_buffer */
398 spl_image.load_addr = (uintptr_t)sysfw_load_address;
399
400 bootdev.boot_device = spl_boot_device();
401
402 /* Load combined System Controller firmware and config data image */
403 switch (bootdev.boot_device) {
Simon Glass103c5f12021-08-08 12:20:09 -0600404#if CONFIG_IS_ENABLED(MMC)
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500405 case BOOT_DEVICE_MMC1:
406 case BOOT_DEVICE_MMC2:
407 case BOOT_DEVICE_MMC2_2:
408 ret = spl_mmc_load(&spl_image, &bootdev,
409#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
410 CONFIG_K3_SYSFW_IMAGE_NAME,
411#else
412 NULL,
413#endif
414#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
415 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
416#else
417 0,
418#endif
419#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
420 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
421#else
422 0);
423#endif
424 break;
425#endif
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530426#if CONFIG_IS_ENABLED(SPI_LOAD)
427 case BOOT_DEVICE_SPI:
Vignesh Raghavendra58421632021-12-23 19:26:03 +0530428 sysfw_spi_base = k3_sysfw_get_spi_addr();
429 if (!sysfw_spi_base)
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530430 ret = -ENODEV;
Vignesh Raghavendra58421632021-12-23 19:26:03 +0530431 k3_sysfw_spi_copy(sysfw_load_address, sysfw_spi_base,
432 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530433 break;
434#endif
Vaishnav Achath09d14d72022-05-09 11:50:14 +0530435#if CONFIG_IS_ENABLED(NOR_SUPPORT)
436 case BOOT_DEVICE_HYPERFLASH:
437 sysfw_spi_base = get_sysfw_hf_addr();
438 if (!sysfw_spi_base)
439 ret = -ENODEV;
440 k3_sysfw_spi_copy(sysfw_load_address, sysfw_spi_base,
441 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
442 break;
443#endif
Andreas Dannenberg921b3252019-08-15 15:55:29 -0500444#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
445 case BOOT_DEVICE_UART:
446#ifdef CONFIG_K3_EARLY_CONS
447 /*
448 * Establish a serial console if not yet available as required
449 * for UART-based boot. For this use the early console feature
450 * that allows setting up a UART for use before SYSFW has been
451 * brought up. Note that the associated UART module's clocks
452 * must have gotten enabled by the ROM bootcode which will be
453 * the case when continuing to boot serially from the same
454 * UART that the ROM loaded the initial bootloader from.
455 */
456 if (!gd->have_console)
457 early_console_init();
458#endif
459 ret = spl_ymodem_load_image(&spl_image, &bootdev);
460 break;
461#endif
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530462#if CONFIG_IS_ENABLED(DFU)
463 case BOOT_DEVICE_DFU:
464 ret = k3_sysfw_dfu_download(sysfw_load_address);
465 break;
466#endif
Faiz Abbasf5838b12020-08-03 11:35:07 +0530467#if CONFIG_IS_ENABLED(USB_STORAGE)
468 case BOOT_DEVICE_USB:
469 ret = spl_usb_load(&spl_image, &bootdev,
470 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
471#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
472 CONFIG_K3_SYSFW_IMAGE_NAME);
473#else
474 NULL);
475#endif
476#endif
477 break;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500478 default:
479 panic("Loading SYSFW image from device %u not supported!\n",
480 bootdev.boot_device);
481 }
482
483 if (ret)
484 panic("Error %d occurred during loading SYSFW image!\n", ret);
485
486 /*
487 * Now that SYSFW got loaded set helper flag to restore regular SPL
488 * loader behavior so we can later boot into the next stage as expected.
489 */
490 sysfw_loaded = true;
491
492 /* Ensure the SYSFW image is in FIT format */
Simon Glassf3543e62022-09-06 20:26:52 -0600493 if (image_get_magic((const struct legacy_img_hdr *)sysfw_load_address) !=
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500494 FDT_MAGIC)
495 panic("SYSFW image not in FIT format!\n");
496
497 /* Extract and start SYSFW */
498 k3_sysfw_load_using_fit(sysfw_load_address);
499
500 /* Get handle for accessing SYSFW services */
501 ti_sci = get_ti_sci_handle();
502
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530503 if (config_pm_pre_callback)
504 config_pm_pre_callback();
505
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500506 /* Parse and apply the different SYSFW configuration fragments */
507 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
508
509 /*
510 * Now that all clocks and PM aspects are setup, invoke a user-
511 * provided callback function. Usually this callback would be used
512 * to setup or re-configure the U-Boot console UART.
513 */
514 if (config_pm_done_callback)
515 config_pm_done_callback();
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500516}