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