blob: 0bacfc4d0770bea0bd34aaa8d4c43dfc49c3bb48 [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
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050025#include <asm/arch/sys_proto.h>
Andreas Dannenberg6da45de2019-08-15 15:55:29 -050026#include "common.h"
27
28DECLARE_GLOBAL_DATA_PTR;
Andreas Dannenberg04e5ea82019-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 Vutla17951b72020-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
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -050043static bool sysfw_loaded;
44static void *sysfw_load_address;
45
46/*
47 * Populate SPL hook to override the default load address used by the SPL
48 * loader function with a custom address for SYSFW loading.
49 */
50struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
51{
52 if (sysfw_loaded)
53 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
54 else if (sysfw_load_address)
55 return sysfw_load_address;
56 else
57 panic("SYSFW load address not defined!");
58}
59
60/*
61 * Populate SPL hook to skip the default SPL loader FIT post-processing steps
62 * during SYSFW loading and return to the calling function so we can perform
63 * our own custom processing.
64 */
65bool spl_load_simple_fit_skip_processing(void)
66{
67 return !sysfw_loaded;
68}
69
70static int fit_get_data_by_name(const void *fit, int images, const char *name,
71 const void **addr, size_t *size)
72{
73 int node_offset;
74
75 node_offset = fdt_subnode_offset(fit, images, name);
76 if (node_offset < 0)
77 return -ENOENT;
78
79 return fit_image_get_data(fit, node_offset, addr, size);
80}
81
Lokesh Vutla17951b72020-08-05 22:44:18 +053082static void k3_start_system_controller(int rproc_id, bool rproc_loaded,
83 ulong addr, ulong size)
84{
85 int ret;
86
87 ret = rproc_dev_init(rproc_id);
88 if (ret)
89 panic("rproc failed to be initialized (%d)\n", ret);
90
91 if (!rproc_loaded) {
92 ret = rproc_load(rproc_id, addr, size);
93 if (ret)
94 panic("Firmware failed to start on rproc (%d)\n", ret);
95 }
96
97 ret = rproc_start(0);
98 if (ret)
99 panic("Firmware init failed on rproc (%d)\n", ret);
100}
101
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500102static void k3_sysfw_load_using_fit(void *fit)
103{
104 int images;
105 const void *sysfw_addr;
106 size_t sysfw_size;
107 int ret;
108
109 /* Find the node holding the images information */
110 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
111 if (images < 0)
112 panic("Cannot find /images node (%d)\n", images);
113
114 /* Extract System Firmware (SYSFW) image from FIT */
115 ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
116 &sysfw_addr, &sysfw_size);
117 if (ret < 0)
118 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
119 ret);
120
Lokesh Vutla17951b72020-08-05 22:44:18 +0530121 /* Start up system controller firmware */
122 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID, false,
123 (ulong)sysfw_addr, (ulong)sysfw_size);
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500124}
125
126static void k3_sysfw_configure_using_fit(void *fit,
127 struct ti_sci_handle *ti_sci)
128{
129 struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
130 int images;
131 const void *cfg_fragment_addr;
132 size_t cfg_fragment_size;
133 int ret;
134
135 /* Find the node holding the images information */
136 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
137 if (images < 0)
138 panic("Cannot find /images node (%d)\n", images);
139
140 /* Extract board configuration from FIT */
141 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
142 &cfg_fragment_addr, &cfg_fragment_size);
143 if (ret < 0)
144 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
145 ret);
146
147 /* Apply board configuration to SYSFW */
148 ret = board_ops->board_config(ti_sci,
149 (u64)(u32)cfg_fragment_addr,
150 (u32)cfg_fragment_size);
151 if (ret)
152 panic("Failed to set board configuration (%d)\n", ret);
153
154 /* Extract power/clock (PM) specific configuration from FIT */
155 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
156 &cfg_fragment_addr, &cfg_fragment_size);
157 if (ret < 0)
158 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
159 ret);
160
161 /* Apply power/clock (PM) specific configuration to SYSFW */
162 ret = board_ops->board_config_pm(ti_sci,
163 (u64)(u32)cfg_fragment_addr,
164 (u32)cfg_fragment_size);
165 if (ret)
166 panic("Failed to set board PM configuration (%d)\n", ret);
167
168 /* Extract resource management (RM) specific configuration from FIT */
169 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
170 &cfg_fragment_addr, &cfg_fragment_size);
171 if (ret < 0)
172 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
173 ret);
174
175 /* Apply resource management (RM) configuration to SYSFW */
176 ret = board_ops->board_config_rm(ti_sci,
177 (u64)(u32)cfg_fragment_addr,
178 (u32)cfg_fragment_size);
179 if (ret)
180 panic("Failed to set board RM configuration (%d)\n", ret);
181
182 /* Extract security specific configuration from FIT */
183 ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
184 &cfg_fragment_addr, &cfg_fragment_size);
185 if (ret < 0)
186 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
187 ret);
188
189 /* Apply security configuration to SYSFW */
190 ret = board_ops->board_config_security(ti_sci,
191 (u64)(u32)cfg_fragment_addr,
192 (u32)cfg_fragment_size);
193 if (ret)
194 panic("Failed to set board security configuration (%d)\n",
195 ret);
196}
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530197
198#if CONFIG_IS_ENABLED(DFU)
199static int k3_sysfw_dfu_download(void *addr)
200{
201 char dfu_str[50];
202 int ret;
203
204 sprintf(dfu_str, "sysfw.itb ram 0x%p 0x%x", addr,
205 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
206 ret = dfu_config_entities(dfu_str, "ram", "0");
207 if (ret) {
208 dfu_free_entities();
209 goto exit;
210 }
211
212 run_usb_dnl_gadget(0, "usb_dnl_dfu");
213exit:
214 dfu_free_entities();
215 return ret;
216}
217#endif
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500218
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530219#if CONFIG_IS_ENABLED(SPI_LOAD)
220static void *k3_sysfw_get_spi_addr(void)
221{
222 struct udevice *dev;
223 fdt_addr_t addr;
224 int ret;
225
226 ret = uclass_find_device_by_seq(UCLASS_SPI, CONFIG_SF_DEFAULT_BUS,
Simon Glass07e13382020-12-16 21:20:29 -0700227 &dev);
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530228 if (ret)
229 return NULL;
230
231 addr = dev_read_addr_index(dev, 1);
232 if (addr == FDT_ADDR_T_NONE)
233 return NULL;
234
235 return (void *)(addr + CONFIG_K3_SYSFW_IMAGE_SPI_OFFS);
236}
237#endif
238
Lokesh Vutla17951b72020-08-05 22:44:18 +0530239void k3_sysfw_loader(bool rom_loaded_sysfw,
240 void (*config_pm_pre_callback)(void),
Faiz Abbas68393212020-02-26 13:44:36 +0530241 void (*config_pm_done_callback)(void))
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500242{
243 struct spl_image_info spl_image = { 0 };
244 struct spl_boot_device bootdev = { 0 };
245 struct ti_sci_handle *ti_sci;
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530246 int ret = 0;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500247
Lokesh Vutla17951b72020-08-05 22:44:18 +0530248 if (rom_loaded_sysfw) {
249 k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID,
250 rom_loaded_sysfw, 0, 0);
251 sysfw_loaded = true;
252 return;
253 }
254
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500255 /* Reserve a block of aligned memory for loading the SYSFW image */
256 sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
257 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
258 if (!sysfw_load_address)
259 panic("Error allocating %u bytes of memory for SYSFW image\n",
260 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
261
262 debug("%s: allocated %u bytes at 0x%p\n", __func__,
263 CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
264
265 /* Set load address for legacy modes that bypass spl_get_load_buffer */
266 spl_image.load_addr = (uintptr_t)sysfw_load_address;
267
268 bootdev.boot_device = spl_boot_device();
269
270 /* Load combined System Controller firmware and config data image */
271 switch (bootdev.boot_device) {
272#if CONFIG_IS_ENABLED(MMC_SUPPORT)
273 case BOOT_DEVICE_MMC1:
274 case BOOT_DEVICE_MMC2:
275 case BOOT_DEVICE_MMC2_2:
276 ret = spl_mmc_load(&spl_image, &bootdev,
277#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
278 CONFIG_K3_SYSFW_IMAGE_NAME,
279#else
280 NULL,
281#endif
282#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
283 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
284#else
285 0,
286#endif
287#ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
288 CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
289#else
290 0);
291#endif
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500292 break;
293#endif
Lokesh Vutlaa5f27562020-02-04 11:09:50 +0530294#if CONFIG_IS_ENABLED(SPI_LOAD)
295 case BOOT_DEVICE_SPI:
296 sysfw_load_address = k3_sysfw_get_spi_addr();
297 if (!sysfw_load_address)
298 ret = -ENODEV;
Andreas Dannenberg6da45de2019-08-15 15:55:29 -0500299 break;
300#endif
301#if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
302 case BOOT_DEVICE_UART:
303#ifdef CONFIG_K3_EARLY_CONS
304 /*
305 * Establish a serial console if not yet available as required
306 * for UART-based boot. For this use the early console feature
307 * that allows setting up a UART for use before SYSFW has been
308 * brought up. Note that the associated UART module's clocks
309 * must have gotten enabled by the ROM bootcode which will be
310 * the case when continuing to boot serially from the same
311 * UART that the ROM loaded the initial bootloader from.
312 */
313 if (!gd->have_console)
314 early_console_init();
315#endif
316 ret = spl_ymodem_load_image(&spl_image, &bootdev);
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500317 break;
318#endif
Vignesh Raghavendra1b9d9272020-01-27 17:59:24 +0530319#if CONFIG_IS_ENABLED(DFU)
320 case BOOT_DEVICE_DFU:
321 ret = k3_sysfw_dfu_download(sysfw_load_address);
322 break;
323#endif
Faiz Abbas5365ecf2020-08-03 11:35:07 +0530324#if CONFIG_IS_ENABLED(USB_STORAGE)
325 case BOOT_DEVICE_USB:
326 ret = spl_usb_load(&spl_image, &bootdev,
327 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
328#ifdef CONFIG_K3_SYSFW_IMAGE_NAME
329 CONFIG_K3_SYSFW_IMAGE_NAME);
330#else
331 NULL);
332#endif
333#endif
334 break;
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500335 default:
336 panic("Loading SYSFW image from device %u not supported!\n",
337 bootdev.boot_device);
338 }
339
340 if (ret)
341 panic("Error %d occurred during loading SYSFW image!\n", ret);
342
343 /*
344 * Now that SYSFW got loaded set helper flag to restore regular SPL
345 * loader behavior so we can later boot into the next stage as expected.
346 */
347 sysfw_loaded = true;
348
349 /* Ensure the SYSFW image is in FIT format */
350 if (image_get_magic((const image_header_t *)sysfw_load_address) !=
351 FDT_MAGIC)
352 panic("SYSFW image not in FIT format!\n");
353
354 /* Extract and start SYSFW */
355 k3_sysfw_load_using_fit(sysfw_load_address);
356
357 /* Get handle for accessing SYSFW services */
358 ti_sci = get_ti_sci_handle();
359
Faiz Abbas68393212020-02-26 13:44:36 +0530360 if (config_pm_pre_callback)
361 config_pm_pre_callback();
362
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500363 /* Parse and apply the different SYSFW configuration fragments */
364 k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
365
366 /*
367 * Now that all clocks and PM aspects are setup, invoke a user-
368 * provided callback function. Usually this callback would be used
369 * to setup or re-configure the U-Boot console UART.
370 */
371 if (config_pm_done_callback)
372 config_pm_done_callback();
Andreas Dannenberg04e5ea82019-06-04 17:55:47 -0500373}