blob: 5e48c36ccd58cf79c7a045699e25d37413e001df [file] [log] [blame]
Andreas Dannenberg04e5ea82019-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 Glass7130b952020-07-19 10:15:40 -060010#include <dm.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050013#include <spl.h>
14#include <malloc.h>
15#include <remoteproc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050018#include <linux/soc/ti/ti_sci_protocol.h>
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +053019#include <g_dnl.h>
20#include <usb.h>
21#include <dfu.h>
Lokesh Vutlaa5f27562020-02-04 11:09:50 +053022#include <dm/uclass-internal.h>
23#include <spi_flash.h>
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +053024
Vignesh Raghavendra1d341972021-12-23 19:26:03 +053025#include <asm/io.h>
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050026#include <asm/arch/sys_proto.h>
Andreas Dannenberg6da45de2019-08-15 15:55:29 -050027#include "common.h"
28
29DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg04e5ea82019-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 Vutla17951b72020-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 Kristo20e4b552021-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 Dannenberg04e5ea82019-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 */
91struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
92{
93 if (sysfw_loaded)
94 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
95 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 Vutla17951b72020-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 Dannenberg04e5ea82019-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 Vutla17951b72020-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 Dannenberg04e5ea82019-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 Kristo20e4b552021-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 Dannenberg04e5ea82019-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 Kristo20e4b552021-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 Dannenberg04e5ea82019-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 Kristo20e4b552021-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 Dannenberg04e5ea82019-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}
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530299
300#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
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500320
Lokesh Vutlaa5f27562020-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;
327
328 ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS,
Simon Glass07e13382020-12-16 21:20:29 -0700329 &dev);
Lokesh Vutlaa5f27562020-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 Raghavendra1d341972021-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 Vutlaa5f27562020-02-04 11:09:50 +0530347#endif
348
Lokesh Vutla17951b72020-08-05 22:44:18 +0530349void k3_sysfw_loader(bool rom_loaded_sysfw,
350 void (*config_pm_pre_callback)(void),
Faiz Abbas68393212020-02-26 13:44:36 +0530351 void (*config_pm_done_callback)(void))
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500352{
353 struct spl_image_info spl_image = { 0 };
354 struct spl_boot_device bootdev = { 0 };
355 struct ti_sci_handle *ti_sci;
Vignesh Raghavendra1d341972021-12-23 19:26:03 +0530356#if CONFIG_IS_ENABLED(SPI_LOAD)
357 void *sysfw_spi_base;
358#endif
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530359 int ret = 0;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500360
Lokesh Vutla17951b72020-08-05 22:44:18 +0530361 if (rom_loaded_sysfw) {
362 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID,
363 rom_loaded_sysfw, 0, 0);
364 sysfw_loaded = true;
365 return;
366 }
367
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500368 /* Reserve a block of aligned memory for loading the SYSFW image */
369 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
370 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
371 if (!sysfw_load_address)
372 panic("Error allocating %u bytes of memory for SYSFW image\n",
373 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
374
375 debug("%s: allocated %u bytes at 0x%p\n", __func__,
376 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
377
378 /* Set load address for legacy modes that bypass spl_get_load_buffer */
379 spl_image.load_addr = (uintptr_t)sysfw_load_address;
380
381 bootdev.boot_device = spl_boot_device();
382
383 /* Load combined System Controller firmware and config data image */
384 switch (bootdev.boot_device) {
Simon Glassb58bfe02021-08-08 12:20:09 -0600385#if CONFIG_IS_ENABLED(MMC)
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500386 case BOOT_DEVICE_MMC1:
387 case BOOT_DEVICE_MMC2:
388 case BOOT_DEVICE_MMC2_2:
389 ret = spl_mmc_load(&spl_image, &bootdev,
390#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
391 CONFIG_K3_SYSFW_IMAGE_NAME,
392#else
393 NULL,
394#endif
395#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
396 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
397#else
398 0,
399#endif
400#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
401 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
402#else
403 0);
404#endif
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500405 break;
406#endif
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530407#if CONFIG_IS_ENABLED(SPI_LOAD)
408 case BOOT_DEVICE_SPI:
Vignesh Raghavendra1d341972021-12-23 19:26:03 +0530409 sysfw_spi_base = k3_sysfw_get_spi_addr();
410 if (!sysfw_spi_base)
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530411 ret = -ENODEV;
Vignesh Raghavendra1d341972021-12-23 19:26:03 +0530412 k3_sysfw_spi_copy(sysfw_load_address, sysfw_spi_base,
413 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500414 break;
415#endif
416#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
417 case BOOT_DEVICE_UART:
418#ifdef CONFIG_K3_EARLY_CONS
419 /*
420 * Establish a serial console if not yet available as required
421 * for UART-based boot. For this use the early console feature
422 * that allows setting up a UART for use before SYSFW has been
423 * brought up. Note that the associated UART module's clocks
424 * must have gotten enabled by the ROM bootcode which will be
425 * the case when continuing to boot serially from the same
426 * UART that the ROM loaded the initial bootloader from.
427 */
428 if (!gd->have_console)
429 early_console_init();
430#endif
431 ret = spl_ymodem_load_image(&spl_image, &bootdev);
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500432 break;
433#endif
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530434#if CONFIG_IS_ENABLED(DFU)
435 case BOOT_DEVICE_DFU:
436 ret = k3_sysfw_dfu_download(sysfw_load_address);
437 break;
438#endif
Faiz Abbas5365ecf2020-08-03 11:35:07 +0530439#if CONFIG_IS_ENABLED(USB_STORAGE)
440 case BOOT_DEVICE_USB:
441 ret = spl_usb_load(&spl_image, &bootdev,
442 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
443#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
444 CONFIG_K3_SYSFW_IMAGE_NAME);
445#else
446 NULL);
447#endif
448#endif
449 break;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500450 default:
451 panic("Loading SYSFW image from device %u not supported!\n",
452 bootdev.boot_device);
453 }
454
455 if (ret)
456 panic("Error %d occurred during loading SYSFW image!\n", ret);
457
458 /*
459 * Now that SYSFW got loaded set helper flag to restore regular SPL
460 * loader behavior so we can later boot into the next stage as expected.
461 */
462 sysfw_loaded = true;
463
464 /* Ensure the SYSFW image is in FIT format */
465 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
466 FDT_MAGIC)
467 panic("SYSFW image not in FIT format!\n");
468
469 /* Extract and start SYSFW */
470 k3_sysfw_load_using_fit(sysfw_load_address);
471
472 /* Get handle for accessing SYSFW services */
473 ti_sci = get_ti_sci_handle();
474
Faiz Abbas68393212020-02-26 13:44:36 +0530475 if (config_pm_pre_callback)
476 config_pm_pre_callback();
477
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500478 /* Parse and apply the different SYSFW configuration fragments */
479 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
480
481 /*
482 * Now that all clocks and PM aspects are setup, invoke a user-
483 * provided callback function. Usually this callback would be used
484 * to setup or re-configure the U-Boot console UART.
485 */
486 if (config_pm_done_callback)
487 config_pm_done_callback();
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500488}