blob: d213e06afbb1c345ba7c3efec7960b27e41675c2 [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
Andreas Dannenberg6df87062019-06-04 17:55:47 -050025#include <asm/arch/sys_proto.h>
Andreas Dannenberg921b3252019-08-15 15:55:29 -050026#include "common.h"
27
28DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg6df87062019-06-04 17:55:47 -050029
30/* Name of the FIT image nodes for SYSFW and its config data */
31#define SYSFW_FIRMWARE "sysfw.bin"
32#define SYSFW_CFG_BOARD "board-cfg.bin"
33#define SYSFW_CFG_PM "pm-cfg.bin"
34#define SYSFW_CFG_RM "rm-cfg.bin"
35#define SYSFW_CFG_SEC "sec-cfg.bin"
36
Lokesh Vutladc57a552020-08-05 22:44:18 +053037/*
38 * It is assumed that remoteproc device 0 is the corresponding
39 * system-controller that runs SYSFW. Make sure DT reflects the same.
40 */
41#define K3_SYSTEM_CONTROLLER_RPROC_ID 0
42
Tero Kristo925698d2021-06-11 11:45:22 +030043#define COMMON_HEADER_ADDRESS 0x41cffb00
44#define BOARDCFG_ADDRESS 0x41c80000
45
46#define COMP_TYPE_SBL_DATA 0x11
47#define DESC_TYPE_BOARDCFG_PM_INDEX 0x2
48#define DESC_TYPE_BOARDCFG_RM_INDEX 0x3
49
50#define BOARD_CONFIG_RM_DESC_TYPE 0x000c
51#define BOARD_CONFIG_PM_DESC_TYPE 0x000e
52
53struct extboot_comp {
54 u32 comp_type;
55 u32 boot_core;
56 u32 comp_opts;
57 u64 dest_addr;
58 u32 comp_size;
59};
60
61struct extboot_header {
62 u8 magic[8];
63 u32 num_comps;
64 struct extboot_comp comps[5];
65 u32 reserved;
66};
67
68struct bcfg_desc {
69 u16 type;
70 u16 offset;
71 u16 size;
72 u8 devgrp;
73 u8 reserved;
74} __packed;
75
76struct bcfg_header {
77 u8 num_elems;
78 u8 sw_rev;
79 struct bcfg_desc descs[4];
80 u16 reserved;
81} __packed;
82
Andreas Dannenberg6df87062019-06-04 17:55:47 -050083static bool sysfw_loaded;
84static void *sysfw_load_address;
85
86/*
87 * Populate SPL hook to override the default load address used by the SPL
88 * loader function with a custom address for SYSFW loading.
89 */
90struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
91{
92 if (sysfw_loaded)
93 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
94 else if (sysfw_load_address)
95 return sysfw_load_address;
96 else
97 panic("SYSFW load address not defined!");
98}
99
100/*
101 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
102 * during SYSFW loading and return to the calling function so we can perform
103 * our own custom processing.
104 */
105bool spl_load_simple_fit_skip_processing(void)
106{
107 return !sysfw_loaded;
108}
109
110static int fit_get_data_by_name(const void *fit, int images, const char *name,
111 const void **addr, size_t *size)
112{
113 int node_offset;
114
115 node_offset = fdt_subnode_offset(fit, images, name);
116 if (node_offset < 0)
117 return -ENOENT;
118
119 return fit_image_get_data(fit, node_offset, addr, size);
120}
121
Lokesh Vutladc57a552020-08-05 22:44:18 +0530122static void k3_start_system_controller(int rproc_id, bool rproc_loaded,
123 ulong addr, ulong size)
124{
125 int ret;
126
127 ret = rproc_dev_init(rproc_id);
128 if (ret)
129 panic("rproc failed to be initialized (%d)\n", ret);
130
131 if (!rproc_loaded) {
132 ret = rproc_load(rproc_id, addr, size);
133 if (ret)
134 panic("Firmware failed to start on rproc (%d)\n", ret);
135 }
136
137 ret = rproc_start(0);
138 if (ret)
139 panic("Firmware init failed on rproc (%d)\n", ret);
140}
141
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500142static void k3_sysfw_load_using_fit(void *fit)
143{
144 int images;
145 const void *sysfw_addr;
146 size_t sysfw_size;
147 int ret;
148
149 /* Find the node holding the images information */
150 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
151 if (images < 0)
152 panic("Cannot find /images node (%d)\n", images);
153
154 /* Extract System Firmware (SYSFW) image from FIT */
155 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
156 &sysfw_addr, &sysfw_size);
157 if (ret < 0)
158 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
159 ret);
160
Lokesh Vutladc57a552020-08-05 22:44:18 +0530161 /* Start up system controller firmware */
162 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID, false,
163 (ulong)sysfw_addr, (ulong)sysfw_size);
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500164}
165
166static void k3_sysfw_configure_using_fit(void *fit,
167 struct ti_sci_handle *ti_sci)
168{
169 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
170 int images;
171 const void *cfg_fragment_addr;
172 size_t cfg_fragment_size;
173 int ret;
Tero Kristo925698d2021-06-11 11:45:22 +0300174 u8 *buf;
175 struct extboot_header *common_header;
176 struct bcfg_header *bcfg_header;
177 struct extboot_comp *comp;
178 struct bcfg_desc *desc;
179 u32 addr;
180 bool copy_bcfg = false;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500181
182 /* Find the node holding the images information */
183 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
184 if (images < 0)
185 panic("Cannot find /images node (%d)\n", images);
186
187 /* Extract board configuration from FIT */
188 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
189 &cfg_fragment_addr, &cfg_fragment_size);
190 if (ret < 0)
191 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
192 ret);
193
194 /* Apply board configuration to SYSFW */
195 ret = board_ops->board_config(ti_sci,
196 (u64)(u32)cfg_fragment_addr,
197 (u32)cfg_fragment_size);
198 if (ret)
199 panic("Failed to set board configuration (%d)\n", ret);
200
201 /* Extract power/clock (PM) specific configuration from FIT */
202 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
203 &cfg_fragment_addr, &cfg_fragment_size);
204 if (ret < 0)
205 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
206 ret);
207
208 /* Apply power/clock (PM) specific configuration to SYSFW */
Tero Kristo925698d2021-06-11 11:45:22 +0300209 if (!IS_ENABLED(CONFIG_K3_DM_FW)) {
210 ret = board_ops->board_config_pm(ti_sci,
211 (u64)(u32)cfg_fragment_addr,
212 (u32)cfg_fragment_size);
213 if (ret)
214 panic("Failed to set board PM configuration (%d)\n", ret);
215 } else {
216 /* Initialize shared memory boardconfig buffer */
217 buf = (u8 *)COMMON_HEADER_ADDRESS;
218 common_header = (struct extboot_header *)buf;
219
220 /* Check if we have a struct populated by ROM in memory already */
221 if (strcmp((char *)common_header->magic, "EXTBOOT"))
222 copy_bcfg = true;
223
224 if (copy_bcfg) {
225 strcpy((char *)common_header->magic, "EXTBOOT");
226 common_header->num_comps = 1;
227
228 comp = &common_header->comps[0];
229
230 comp->comp_type = COMP_TYPE_SBL_DATA;
231 comp->boot_core = 0x10;
232 comp->comp_opts = 0;
233 addr = (u32)BOARDCFG_ADDRESS;
234 comp->dest_addr = addr;
235 comp->comp_size = sizeof(*bcfg_header);
236
237 bcfg_header = (struct bcfg_header *)addr;
238
239 bcfg_header->num_elems = 2;
240 bcfg_header->sw_rev = 0;
241
242 desc = &bcfg_header->descs[0];
243
244 desc->type = BOARD_CONFIG_PM_DESC_TYPE;
245 desc->offset = sizeof(*bcfg_header);
246 desc->size = cfg_fragment_size;
247 comp->comp_size += desc->size;
248 desc->devgrp = 0;
249 desc->reserved = 0;
250 memcpy((u8 *)bcfg_header + desc->offset,
251 cfg_fragment_addr, cfg_fragment_size);
252
253 bcfg_header->descs[1].offset = desc->offset + desc->size;
254 }
255 }
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500256
257 /* Extract resource management (RM) specific configuration from FIT */
258 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
259 &cfg_fragment_addr, &cfg_fragment_size);
260 if (ret < 0)
261 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
262 ret);
263
Tero Kristo925698d2021-06-11 11:45:22 +0300264 if (copy_bcfg) {
265 desc = &bcfg_header->descs[1];
266
267 desc->type = BOARD_CONFIG_RM_DESC_TYPE;
268 desc->size = cfg_fragment_size;
269 comp->comp_size += desc->size;
270 desc->devgrp = 0;
271 desc->reserved = 0;
272 memcpy((u8 *)bcfg_header + desc->offset, cfg_fragment_addr,
273 cfg_fragment_size);
274 }
275
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500276 /* Apply resource management (RM) configuration to SYSFW */
277 ret = board_ops->board_config_rm(ti_sci,
278 (u64)(u32)cfg_fragment_addr,
279 (u32)cfg_fragment_size);
280 if (ret)
281 panic("Failed to set board RM configuration (%d)\n", ret);
282
283 /* Extract security specific configuration from FIT */
284 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
285 &cfg_fragment_addr, &cfg_fragment_size);
286 if (ret < 0)
287 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
288 ret);
289
290 /* Apply security configuration to SYSFW */
291 ret = board_ops->board_config_security(ti_sci,
292 (u64)(u32)cfg_fragment_addr,
293 (u32)cfg_fragment_size);
294 if (ret)
295 panic("Failed to set board security configuration (%d)\n",
296 ret);
297}
298
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530299#if CONFIG_IS_ENABLED(DFU)
300static int k3_sysfw_dfu_download(void *addr)
301{
302 char dfu_str[50];
303 int ret;
304
305 sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
306 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
307 ret = dfu_config_entities(dfu_str, "ram", "0");
308 if (ret) {
309 dfu_free_entities();
310 goto exit;
311 }
312
313 run_usb_dnl_gadget(0, "usb_dnl_dfu");
314exit:
315 dfu_free_entities();
316 return ret;
317}
318#endif
319
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530320#if CONFIG_IS_ENABLED(SPI_LOAD)
321static void *k3_sysfw_get_spi_addr(void)
322{
323 struct udevice *dev;
324 fdt_addr_t addr;
325 int ret;
326
327 ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS,
Simon Glass99175912020-12-16 21:20:29 -0700328 &dev);
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530329 if (ret)
330 return NULL;
331
332 addr = dev_read_addr_index(dev, 1);
333 if (addr == FDT_ADDR_T_NONE)
334 return NULL;
335
336 return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
337}
338#endif
339
Lokesh Vutladc57a552020-08-05 22:44:18 +0530340void k3_sysfw_loader(bool rom_loaded_sysfw,
341 void (*config_pm_pre_callback)(void),
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530342 void (*config_pm_done_callback)(void))
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500343{
344 struct spl_image_info spl_image = { 0 };
345 struct spl_boot_device bootdev = { 0 };
346 struct ti_sci_handle *ti_sci;
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530347 int ret = 0;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500348
Lokesh Vutladc57a552020-08-05 22:44:18 +0530349 if (rom_loaded_sysfw) {
350 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID,
351 rom_loaded_sysfw, 0, 0);
352 sysfw_loaded = true;
353 return;
354 }
355
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500356 /* Reserve a block of aligned memory for loading the SYSFW image */
357 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
358 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
359 if (!sysfw_load_address)
360 panic("Error allocating %u bytes of memory for SYSFW image\n",
361 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
362
363 debug("%s: allocated %u bytes at 0x%p\n", __func__,
364 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
365
366 /* Set load address for legacy modes that bypass spl_get_load_buffer */
367 spl_image.load_addr = (uintptr_t)sysfw_load_address;
368
369 bootdev.boot_device = spl_boot_device();
370
371 /* Load combined System Controller firmware and config data image */
372 switch (bootdev.boot_device) {
373#if CONFIG_IS_ENABLED(MMC_SUPPORT)
374 case BOOT_DEVICE_MMC1:
375 case BOOT_DEVICE_MMC2:
376 case BOOT_DEVICE_MMC2_2:
377 ret = spl_mmc_load(&spl_image, &bootdev,
378#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
379 CONFIG_K3_SYSFW_IMAGE_NAME,
380#else
381 NULL,
382#endif
383#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
384 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
385#else
386 0,
387#endif
388#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
389 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
390#else
391 0);
392#endif
393 break;
394#endif
Lokesh Vutla7d0866b2020-02-04 11:09:50 +0530395#if CONFIG_IS_ENABLED(SPI_LOAD)
396 case BOOT_DEVICE_SPI:
397 sysfw_load_address = k3_sysfw_get_spi_addr();
398 if (!sysfw_load_address)
399 ret = -ENODEV;
400 break;
401#endif
Andreas Dannenberg921b3252019-08-15 15:55:29 -0500402#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
403 case BOOT_DEVICE_UART:
404#ifdef CONFIG_K3_EARLY_CONS
405 /*
406 * Establish a serial console if not yet available as required
407 * for UART-based boot. For this use the early console feature
408 * that allows setting up a UART for use before SYSFW has been
409 * brought up. Note that the associated UART module's clocks
410 * must have gotten enabled by the ROM bootcode which will be
411 * the case when continuing to boot serially from the same
412 * UART that the ROM loaded the initial bootloader from.
413 */
414 if (!gd->have_console)
415 early_console_init();
416#endif
417 ret = spl_ymodem_load_image(&spl_image, &bootdev);
418 break;
419#endif
Vignesh Raghavendrae15b6e32020-01-27 17:59:24 +0530420#if CONFIG_IS_ENABLED(DFU)
421 case BOOT_DEVICE_DFU:
422 ret = k3_sysfw_dfu_download(sysfw_load_address);
423 break;
424#endif
Faiz Abbasf5838b12020-08-03 11:35:07 +0530425#if CONFIG_IS_ENABLED(USB_STORAGE)
426 case BOOT_DEVICE_USB:
427 ret = spl_usb_load(&spl_image, &bootdev,
428 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
429#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
430 CONFIG_K3_SYSFW_IMAGE_NAME);
431#else
432 NULL);
433#endif
434#endif
435 break;
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500436 default:
437 panic("Loading SYSFW image from device %u not supported!\n",
438 bootdev.boot_device);
439 }
440
441 if (ret)
442 panic("Error %d occurred during loading SYSFW image!\n", ret);
443
444 /*
445 * Now that SYSFW got loaded set helper flag to restore regular SPL
446 * loader behavior so we can later boot into the next stage as expected.
447 */
448 sysfw_loaded = true;
449
450 /* Ensure the SYSFW image is in FIT format */
451 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
452 FDT_MAGIC)
453 panic("SYSFW image not in FIT format!\n");
454
455 /* Extract and start SYSFW */
456 k3_sysfw_load_using_fit(sysfw_load_address);
457
458 /* Get handle for accessing SYSFW services */
459 ti_sci = get_ti_sci_handle();
460
Faiz Abbasd45ffb72020-02-26 13:44:36 +0530461 if (config_pm_pre_callback)
462 config_pm_pre_callback();
463
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500464 /* Parse and apply the different SYSFW configuration fragments */
465 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
466
467 /*
468 * Now that all clocks and PM aspects are setup, invoke a user-
469 * provided callback function. Usually this callback would be used
470 * to setup or re-configure the U-Boot console UART.
471 */
472 if (config_pm_done_callback)
473 config_pm_done_callback();
Andreas Dannenberg6df87062019-06-04 17:55:47 -0500474}