Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[oweals/u-boot.git] / arch / arm / mach-k3 / sysfw-loader.c
1 // 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>
10 #include <spl.h>
11 #include <malloc.h>
12 #include <remoteproc.h>
13 #include <linux/soc/ti/ti_sci_protocol.h>
14 #include <asm/arch/sys_proto.h>
15 #include "common.h"
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 /* Name of the FIT image nodes for SYSFW and its config data */
20 #define SYSFW_FIRMWARE                  "sysfw.bin"
21 #define SYSFW_CFG_BOARD                 "board-cfg.bin"
22 #define SYSFW_CFG_PM                    "pm-cfg.bin"
23 #define SYSFW_CFG_RM                    "rm-cfg.bin"
24 #define SYSFW_CFG_SEC                   "sec-cfg.bin"
25
26 static bool sysfw_loaded;
27 static void *sysfw_load_address;
28
29 /*
30  * Populate SPL hook to override the default load address used by the SPL
31  * loader function with a custom address for SYSFW loading.
32  */
33 struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
34 {
35         if (sysfw_loaded)
36                 return (struct image_header *)(CONFIG_SYS_TEXT_BASE + offset);
37         else if (sysfw_load_address)
38                 return sysfw_load_address;
39         else
40                 panic("SYSFW load address not defined!");
41 }
42
43 /*
44  * Populate SPL hook to skip the default SPL loader FIT post-processing steps
45  * during SYSFW loading and return to the calling function so we can perform
46  * our own custom processing.
47  */
48 bool spl_load_simple_fit_skip_processing(void)
49 {
50         return !sysfw_loaded;
51 }
52
53 static int fit_get_data_by_name(const void *fit, int images, const char *name,
54                                 const void **addr, size_t *size)
55 {
56         int node_offset;
57
58         node_offset = fdt_subnode_offset(fit, images, name);
59         if (node_offset < 0)
60                 return -ENOENT;
61
62         return fit_image_get_data(fit, node_offset, addr, size);
63 }
64
65 static void k3_sysfw_load_using_fit(void *fit)
66 {
67         int images;
68         const void *sysfw_addr;
69         size_t sysfw_size;
70         int ret;
71
72         /* Find the node holding the images information */
73         images = fdt_path_offset(fit, FIT_IMAGES_PATH);
74         if (images < 0)
75                 panic("Cannot find /images node (%d)\n", images);
76
77         /* Extract System Firmware (SYSFW) image from FIT */
78         ret = fit_get_data_by_name(fit, images, SYSFW_FIRMWARE,
79                                    &sysfw_addr, &sysfw_size);
80         if (ret < 0)
81                 panic("Error accessing %s node in FIT (%d)\n", SYSFW_FIRMWARE,
82                       ret);
83
84         /*
85          * Start up system controller firmware
86          *
87          * It is assumed that remoteproc device 0 is the corresponding
88          * system-controller that runs SYSFW. Make sure DT reflects the same.
89          */
90         ret = rproc_dev_init(0);
91         if (ret)
92                 panic("rproc failed to be initialized (%d)\n", ret);
93
94         ret = rproc_load(0, (ulong)sysfw_addr, (ulong)sysfw_size);
95         if (ret)
96                 panic("Firmware failed to start on rproc (%d)\n", ret);
97
98         ret = rproc_start(0);
99         if (ret)
100                 panic("Firmware init failed on rproc (%d)\n", ret);
101 }
102
103 static void k3_sysfw_configure_using_fit(void *fit,
104                                          struct ti_sci_handle *ti_sci)
105 {
106         struct ti_sci_board_ops *board_ops = &ti_sci->ops.board_ops;
107         int images;
108         const void *cfg_fragment_addr;
109         size_t cfg_fragment_size;
110         int ret;
111
112         /* Find the node holding the images information */
113         images = fdt_path_offset(fit, FIT_IMAGES_PATH);
114         if (images < 0)
115                 panic("Cannot find /images node (%d)\n", images);
116
117         /* Extract board configuration from FIT */
118         ret = fit_get_data_by_name(fit, images, SYSFW_CFG_BOARD,
119                                    &cfg_fragment_addr, &cfg_fragment_size);
120         if (ret < 0)
121                 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_BOARD,
122                       ret);
123
124         /* Apply board configuration to SYSFW */
125         ret = board_ops->board_config(ti_sci,
126                                       (u64)(u32)cfg_fragment_addr,
127                                       (u32)cfg_fragment_size);
128         if (ret)
129                 panic("Failed to set board configuration (%d)\n", ret);
130
131         /* Extract power/clock (PM) specific configuration from FIT */
132         ret = fit_get_data_by_name(fit, images, SYSFW_CFG_PM,
133                                    &cfg_fragment_addr, &cfg_fragment_size);
134         if (ret < 0)
135                 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_PM,
136                       ret);
137
138         /* Apply power/clock (PM) specific configuration to SYSFW */
139         ret = board_ops->board_config_pm(ti_sci,
140                                          (u64)(u32)cfg_fragment_addr,
141                                          (u32)cfg_fragment_size);
142         if (ret)
143                 panic("Failed to set board PM configuration (%d)\n", ret);
144
145         /* Extract resource management (RM) specific configuration from FIT */
146         ret = fit_get_data_by_name(fit, images, SYSFW_CFG_RM,
147                                    &cfg_fragment_addr, &cfg_fragment_size);
148         if (ret < 0)
149                 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_RM,
150                       ret);
151
152         /* Apply resource management (RM) configuration to SYSFW */
153         ret = board_ops->board_config_rm(ti_sci,
154                                          (u64)(u32)cfg_fragment_addr,
155                                          (u32)cfg_fragment_size);
156         if (ret)
157                 panic("Failed to set board RM configuration (%d)\n", ret);
158
159         /* Extract security specific configuration from FIT */
160         ret = fit_get_data_by_name(fit, images, SYSFW_CFG_SEC,
161                                    &cfg_fragment_addr, &cfg_fragment_size);
162         if (ret < 0)
163                 panic("Error accessing %s node in FIT (%d)\n", SYSFW_CFG_SEC,
164                       ret);
165
166         /* Apply security configuration to SYSFW */
167         ret = board_ops->board_config_security(ti_sci,
168                                                (u64)(u32)cfg_fragment_addr,
169                                                (u32)cfg_fragment_size);
170         if (ret)
171                 panic("Failed to set board security configuration (%d)\n",
172                       ret);
173 }
174
175 void k3_sysfw_loader(void (*config_pm_done_callback)(void))
176 {
177         struct spl_image_info spl_image = { 0 };
178         struct spl_boot_device bootdev = { 0 };
179         struct ti_sci_handle *ti_sci;
180         int ret;
181
182         /* Reserve a block of aligned memory for loading the SYSFW image */
183         sysfw_load_address = memalign(ARCH_DMA_MINALIGN,
184                                       CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
185         if (!sysfw_load_address)
186                 panic("Error allocating %u bytes of memory for SYSFW image\n",
187                       CONFIG_K3_SYSFW_IMAGE_SIZE_MAX);
188
189         debug("%s: allocated %u bytes at 0x%p\n", __func__,
190               CONFIG_K3_SYSFW_IMAGE_SIZE_MAX, sysfw_load_address);
191
192         /* Set load address for legacy modes that bypass spl_get_load_buffer */
193         spl_image.load_addr = (uintptr_t)sysfw_load_address;
194
195         bootdev.boot_device = spl_boot_device();
196
197         /* Load combined System Controller firmware and config data image */
198         switch (bootdev.boot_device) {
199 #if CONFIG_IS_ENABLED(MMC_SUPPORT)
200         case BOOT_DEVICE_MMC1:
201         case BOOT_DEVICE_MMC2:
202         case BOOT_DEVICE_MMC2_2:
203                 ret = spl_mmc_load(&spl_image, &bootdev,
204 #ifdef CONFIG_K3_SYSFW_IMAGE_NAME
205                                    CONFIG_K3_SYSFW_IMAGE_NAME,
206 #else
207                                    NULL,
208 #endif
209 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART
210                                    CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_PART,
211 #else
212                                    0,
213 #endif
214 #ifdef CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT
215                                    CONFIG_K3_SYSFW_IMAGE_MMCSD_RAW_MODE_SECT);
216 #else
217                                    0);
218 #endif
219                 break;
220 #endif
221 #if CONFIG_IS_ENABLED(YMODEM_SUPPORT)
222         case BOOT_DEVICE_UART:
223 #ifdef CONFIG_K3_EARLY_CONS
224                 /*
225                  * Establish a serial console if not yet available as required
226                  * for UART-based boot. For this use the early console feature
227                  * that allows setting up a UART for use before SYSFW has been
228                  * brought up. Note that the associated UART module's clocks
229                  * must have gotten enabled by the ROM bootcode which will be
230                  * the case when continuing to boot serially from the same
231                  * UART that the ROM loaded the initial bootloader from.
232                  */
233                 if (!gd->have_console)
234                         early_console_init();
235 #endif
236                 ret = spl_ymodem_load_image(&spl_image, &bootdev);
237                 break;
238 #endif
239         default:
240                 panic("Loading SYSFW image from device %u not supported!\n",
241                       bootdev.boot_device);
242         }
243
244         if (ret)
245                 panic("Error %d occurred during loading SYSFW image!\n", ret);
246
247         /*
248          * Now that SYSFW got loaded set helper flag to restore regular SPL
249          * loader behavior so we can later boot into the next stage as expected.
250          */
251         sysfw_loaded = true;
252
253         /* Ensure the SYSFW image is in FIT format */
254         if (image_get_magic((const image_header_t *)sysfw_load_address) !=
255             FDT_MAGIC)
256                 panic("SYSFW image not in FIT format!\n");
257
258         /* Extract and start SYSFW */
259         k3_sysfw_load_using_fit(sysfw_load_address);
260
261         /* Get handle for accessing SYSFW services */
262         ti_sci = get_ti_sci_handle();
263
264         /* Parse and apply the different SYSFW configuration fragments */
265         k3_sysfw_configure_using_fit(sysfw_load_address, ti_sci);
266
267         /*
268          * Now that all clocks and PM aspects are setup, invoke a user-
269          * provided callback function. Usually this callback would be used
270          * to setup or re-configure the U-Boot console UART.
271          */
272         if (config_pm_done_callback)
273                 config_pm_done_callback();
274
275         /*
276          * Output System Firmware version info. Note that since the
277          * 'firmware_description' field is not guaranteed to be zero-
278          * terminated we manually add a \0 terminator if needed. Further
279          * note that we intentionally no longer rely on the extended
280          * printf() formatter '%.*s' to not having to require a more
281          * full-featured printf() implementation.
282          */
283         char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
284
285         strncpy(fw_desc, ti_sci->version.firmware_description,
286                 sizeof(ti_sci->version.firmware_description));
287         fw_desc[sizeof(fw_desc) - 1] = '\0';
288
289         printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
290                ti_sci->version.abi_major, ti_sci->version.abi_minor,
291                ti_sci->version.firmware_revision, fw_desc);
292 }