blob: 472b5085ed83ca45726865152947ecb01e08cc99 [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 Glass2dc9c342020-05-10 11:40:01 -060010#include <image.h>
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050011#include <spl.h>
12#include <malloc.h>
13#include <remoteproc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050015#include <linux/soc/ti/ti_sci_protocol.h>
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +053016#include <g_dnl.h>
17#include <usb.h>
18#include <dfu.h>
Lokesh Vutlaa5f27562020-02-04 11:09:50 +053019#include <dm/uclass-internal.h>
20#include <spi_flash.h>
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +053021
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050022#include <asm/arch/sys_proto.h>
Andreas Dannenberg6da45de2019-08-15 15:55:29 -050023#include "common.h"
24
25DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050026
27/* Name of the FIT image nodes for SYSFW and its config data */
28#define SYSFW_FIRMWARE "sysfw.bin"
29#define SYSFW_CFG_BOARD "board-cfg.bin"
30#define SYSFW_CFG_PM "pm-cfg.bin"
31#define SYSFW_CFG_RM "rm-cfg.bin"
32#define SYSFW_CFG_SEC "sec-cfg.bin"
33
34static bool sysfw_loaded;
35static void *sysfw_load_address;
36
37/*
38 * Populate SPL hook to override the default load address used by the SPL
39 * loader function with a custom address for SYSFW loading.
40 */
41struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
42{
43 if (sysfw_loaded)
44 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
45 else if (sysfw_load_address)
46 return sysfw_load_address;
47 else
48 panic("SYSFW load address not defined!");
49}
50
51/*
52 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
53 * during SYSFW loading and return to the calling function so we can perform
54 * our own custom processing.
55 */
56bool spl_load_simple_fit_skip_processing(void)
57{
58 return !sysfw_loaded;
59}
60
61static int fit_get_data_by_name(const void *fit, int images, const char *name,
62 const void **addr, size_t *size)
63{
64 int node_offset;
65
66 node_offset = fdt_subnode_offset(fit, images, name);
67 if (node_offset < 0)
68 return -ENOENT;
69
70 return fit_image_get_data(fit, node_offset, addr, size);
71}
72
73static void k3_sysfw_load_using_fit(void *fit)
74{
75 int images;
76 const void *sysfw_addr;
77 size_t sysfw_size;
78 int ret;
79
80 /* Find the node holding the images information */
81 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
82 if (images < 0)
83 panic("Cannot find /images node (%d)\n", images);
84
85 /* Extract System Firmware (SYSFW) image from FIT */
86 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
87 &sysfw_addr, &sysfw_size);
88 if (ret < 0)
89 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
90 ret);
91
92 /*
93 * Start up system controller firmware
94 *
95 * It is assumed that remoteproc device 0 is the corresponding
96 * system-controller that runs SYSFW. Make sure DT reflects the same.
97 */
98 ret = rproc_dev_init(0);
99 if (ret)
100 panic("rproc failed to be initialized (%d)\n", ret);
101
102 ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
103 if (ret)
104 panic("Firmware failed to start on rproc (%d)\n", ret);
105
106 ret = rproc_start(0);
107 if (ret)
108 panic("Firmware init failed on rproc (%d)\n", ret);
109}
110
111static void k3_sysfw_configure_using_fit(void *fit,
112 struct ti_sci_handle *ti_sci)
113{
114 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
115 int images;
116 const void *cfg_fragment_addr;
117 size_t cfg_fragment_size;
118 int ret;
119
120 /* Find the node holding the images information */
121 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
122 if (images < 0)
123 panic("Cannot find /images node (%d)\n", images);
124
125 /* Extract board configuration from FIT */
126 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
127 &cfg_fragment_addr, &cfg_fragment_size);
128 if (ret < 0)
129 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
130 ret);
131
132 /* Apply board configuration to SYSFW */
133 ret = board_ops->board_config(ti_sci,
134 (u64)(u32)cfg_fragment_addr,
135 (u32)cfg_fragment_size);
136 if (ret)
137 panic("Failed to set board configuration (%d)\n", ret);
138
139 /* Extract power/clock (PM) specific configuration from FIT */
140 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
141 &cfg_fragment_addr, &cfg_fragment_size);
142 if (ret < 0)
143 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
144 ret);
145
146 /* Apply power/clock (PM) specific configuration to SYSFW */
147 ret = board_ops->board_config_pm(ti_sci,
148 (u64)(u32)cfg_fragment_addr,
149 (u32)cfg_fragment_size);
150 if (ret)
151 panic("Failed to set board PM configuration (%d)\n", ret);
152
153 /* Extract resource management (RM) specific configuration from FIT */
154 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
155 &cfg_fragment_addr, &cfg_fragment_size);
156 if (ret < 0)
157 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
158 ret);
159
160 /* Apply resource management (RM) configuration to SYSFW */
161 ret = board_ops->board_config_rm(ti_sci,
162 (u64)(u32)cfg_fragment_addr,
163 (u32)cfg_fragment_size);
164 if (ret)
165 panic("Failed to set board RM configuration (%d)\n", ret);
166
167 /* Extract security specific configuration from FIT */
168 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
169 &cfg_fragment_addr, &cfg_fragment_size);
170 if (ret < 0)
171 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
172 ret);
173
174 /* Apply security configuration to SYSFW */
175 ret = board_ops->board_config_security(ti_sci,
176 (u64)(u32)cfg_fragment_addr,
177 (u32)cfg_fragment_size);
178 if (ret)
179 panic("Failed to set board security configuration (%d)\n",
180 ret);
181}
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530182
183#if CONFIG_IS_ENABLED(DFU)
184static int k3_sysfw_dfu_download(void *addr)
185{
186 char dfu_str[50];
187 int ret;
188
189 sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
190 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
191 ret = dfu_config_entities(dfu_str, "ram", "0");
192 if (ret) {
193 dfu_free_entities();
194 goto exit;
195 }
196
197 run_usb_dnl_gadget(0, "usb_dnl_dfu");
198exit:
199 dfu_free_entities();
200 return ret;
201}
202#endif
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500203
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530204#if CONFIG_IS_ENABLED(SPI_LOAD)
205static void *k3_sysfw_get_spi_addr(void)
206{
207 struct udevice *dev;
208 fdt_addr_t addr;
209 int ret;
210
211 ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS,
212 true, &dev);
213 if (ret)
214 return NULL;
215
216 addr = dev_read_addr_index(dev, 1);
217 if (addr == FDT_ADDR_T_NONE)
218 return NULL;
219
220 return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
221}
222#endif
223
Faiz Abbas68393212020-02-26 13:44:36 +0530224void k3_sysfw_loader(void (*config_pm_pre_callback) (void),
225 void (*config_pm_done_callback)(void))
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500226{
227 struct spl_image_info spl_image = { 0 };
228 struct spl_boot_device bootdev = { 0 };
229 struct ti_sci_handle *ti_sci;
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530230 int ret = 0;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500231
232 /* Reserve a block of aligned memory for loading the SYSFW image */
233 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
234 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
235 if (!sysfw_load_address)
236 panic("Error allocating %u bytes of memory for SYSFW image\n",
237 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
238
239 debug("%s: allocated %u bytes at 0x%p\n", __func__,
240 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
241
242 /* Set load address for legacy modes that bypass spl_get_load_buffer */
243 spl_image.load_addr = (uintptr_t)sysfw_load_address;
244
245 bootdev.boot_device = spl_boot_device();
246
247 /* Load combined System Controller firmware and config data image */
248 switch (bootdev.boot_device) {
249#if CONFIG_IS_ENABLED(MMC_SUPPORT)
250 case BOOT_DEVICE_MMC1:
251 case BOOT_DEVICE_MMC2:
252 case BOOT_DEVICE_MMC2_2:
253 ret = spl_mmc_load(&spl_image, &bootdev,
254#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
255 CONFIG_K3_SYSFW_IMAGE_NAME,
256#else
257 NULL,
258#endif
259#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
260 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
261#else
262 0,
263#endif
264#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
265 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
266#else
267 0);
268#endif
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500269 break;
270#endif
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530271#if CONFIG_IS_ENABLED(SPI_LOAD)
272 case BOOT_DEVICE_SPI:
273 sysfw_load_address = k3_sysfw_get_spi_addr();
274 if (!sysfw_load_address)
275 ret = -ENODEV;
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500276 break;
277#endif
278#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
279 case BOOT_DEVICE_UART:
280#ifdef CONFIG_K3_EARLY_CONS
281 /*
282 * Establish a serial console if not yet available as required
283 * for UART-based boot. For this use the early console feature
284 * that allows setting up a UART for use before SYSFW has been
285 * brought up. Note that the associated UART module's clocks
286 * must have gotten enabled by the ROM bootcode which will be
287 * the case when continuing to boot serially from the same
288 * UART that the ROM loaded the initial bootloader from.
289 */
290 if (!gd->have_console)
291 early_console_init();
292#endif
293 ret = spl_ymodem_load_image(&spl_image, &bootdev);
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500294 break;
295#endif
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530296#if CONFIG_IS_ENABLED(DFU)
297 case BOOT_DEVICE_DFU:
298 ret = k3_sysfw_dfu_download(sysfw_load_address);
299 break;
300#endif
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500301 default:
302 panic("Loading SYSFW image from device %u not supported!\n",
303 bootdev.boot_device);
304 }
305
306 if (ret)
307 panic("Error %d occurred during loading SYSFW image!\n", ret);
308
309 /*
310 * Now that SYSFW got loaded set helper flag to restore regular SPL
311 * loader behavior so we can later boot into the next stage as expected.
312 */
313 sysfw_loaded = true;
314
315 /* Ensure the SYSFW image is in FIT format */
316 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
317 FDT_MAGIC)
318 panic("SYSFW image not in FIT format!\n");
319
320 /* Extract and start SYSFW */
321 k3_sysfw_load_using_fit(sysfw_load_address);
322
323 /* Get handle for accessing SYSFW services */
324 ti_sci = get_ti_sci_handle();
325
Faiz Abbas68393212020-02-26 13:44:36 +0530326 if (config_pm_pre_callback)
327 config_pm_pre_callback();
328
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500329 /* Parse and apply the different SYSFW configuration fragments */
330 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
331
332 /*
333 * Now that all clocks and PM aspects are setup, invoke a user-
334 * provided callback function. Usually this callback would be used
335 * to setup or re-configure the U-Boot console UART.
336 */
337 if (config_pm_done_callback)
338 config_pm_done_callback();
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500339}