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