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 <dm.h>
11 #include <image.h>
12 #include <log.h>
13 #include <spl.h>
14 #include <malloc.h>
15 #include <remoteproc.h>
16 #include <asm/cache.h>
17 #include <asm/global_data.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
19 #include <g_dnl.h>
20 #include <usb.h>
21 #include <dfu.h>
22 #include <dm/uclass-internal.h>
23 #include <spi_flash.h>
24 
25 #include <asm/arch/sys_proto.h>
26 #include "common.h"
27 
28 DECLARE_GLOBAL_DATA_PTR;
29 
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 
37 /*
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 
43 static bool sysfw_loaded;
44 static 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  */
spl_get_load_buffer(ssize_t offset,size_t size)50 struct 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  */
spl_load_simple_fit_skip_processing(void)65 bool spl_load_simple_fit_skip_processing(void)
66 {
67 	return !sysfw_loaded;
68 }
69 
fit_get_data_by_name(const void * fit,int images,const char * name,const void ** addr,size_t * size)70 static 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 
k3_start_system_controller(int rproc_id,bool rproc_loaded,ulong addr,ulong size)82 static 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 
k3_sysfw_load_using_fit(void * fit)102 static 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 
121 	/* Start up system controller firmware */
122 	k3_start_system_controller(K3_SYSTEM_CONTROLLER_RPROC_ID, false,
123 				   (ulong)sysfw_addr, (ulong)sysfw_size);
124 }
125 
k3_sysfw_configure_using_fit(void * fit,struct ti_sci_handle * ti_sci)126 static 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 }
197 
198 #if CONFIG_IS_ENABLED(DFU)
k3_sysfw_dfu_download(void * addr)199 static 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");
213 exit:
214 	dfu_free_entities();
215 	return ret;
216 }
217 #endif
218 
219 #if CONFIG_IS_ENABLED(SPI_LOAD)
k3_sysfw_get_spi_addr(void)220 static 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,
227 					&dev);
228 	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 
k3_sysfw_loader(bool rom_loaded_sysfw,void (* config_pm_pre_callback)(void),void (* config_pm_done_callback)(void))239 void k3_sysfw_loader(bool rom_loaded_sysfw,
240 		     void (*config_pm_pre_callback)(void),
241 		     void (*config_pm_done_callback)(void))
242 {
243 	struct spl_image_info spl_image = { 0 };
244 	struct spl_boot_device bootdev = { 0 };
245 	struct ti_sci_handle *ti_sci;
246 	int ret = 0;
247 
248 	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 
255 	/* 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
292 		break;
293 #endif
294 #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;
299 		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);
317 		break;
318 #endif
319 #if CONFIG_IS_ENABLED(DFU)
320 	case BOOT_DEVICE_DFU:
321 		ret = k3_sysfw_dfu_download(sysfw_load_address);
322 		break;
323 #endif
324 #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;
335 	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 
360 	if (config_pm_pre_callback)
361 		config_pm_pre_callback();
362 
363 	/* 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();
373 }
374