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