xref: /openbmc/u-boot/common/spl/spl_ram.c (revision 0da90255083681a02b24528f80da9d4062ff634a)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
222802f4eSStefan Agner /*
322802f4eSStefan Agner  * (C) Copyright 2016
422802f4eSStefan Agner  * Xilinx, Inc.
522802f4eSStefan Agner  *
622802f4eSStefan Agner  * (C) Copyright 2016
722802f4eSStefan Agner  * Toradex AG
822802f4eSStefan Agner  *
922802f4eSStefan Agner  * Michal Simek <michal.simek@xilinx.com>
1022802f4eSStefan Agner  * Stefan Agner <stefan.agner@toradex.com>
1122802f4eSStefan Agner  */
1222802f4eSStefan Agner #include <common.h>
13dfce1799SSimon Glass #include <binman_sym.h>
14dfce1799SSimon Glass #include <mapmem.h>
1522802f4eSStefan Agner #include <spl.h>
16b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
1722802f4eSStefan Agner 
1822802f4eSStefan Agner #ifndef CONFIG_SPL_LOAD_FIT_ADDRESS
1922802f4eSStefan Agner # define CONFIG_SPL_LOAD_FIT_ADDRESS	0
2022802f4eSStefan Agner #endif
2122802f4eSStefan Agner 
spl_ram_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)2222802f4eSStefan Agner static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
2322802f4eSStefan Agner 			       ulong count, void *buf)
2422802f4eSStefan Agner {
2522802f4eSStefan Agner 	debug("%s: sector %lx, count %lx, buf %lx\n",
2622802f4eSStefan Agner 	      __func__, sector, count, (ulong)buf);
2722802f4eSStefan Agner 	memcpy(buf, (void *)(CONFIG_SPL_LOAD_FIT_ADDRESS + sector), count);
2822802f4eSStefan Agner 	return count;
2922802f4eSStefan Agner }
3022802f4eSStefan Agner 
spl_ram_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)3122802f4eSStefan Agner static int spl_ram_load_image(struct spl_image_info *spl_image,
3222802f4eSStefan Agner 			      struct spl_boot_device *bootdev)
3322802f4eSStefan Agner {
3422802f4eSStefan Agner 	struct image_header *header;
3522802f4eSStefan Agner 
3622802f4eSStefan Agner 	header = (struct image_header *)CONFIG_SPL_LOAD_FIT_ADDRESS;
3722802f4eSStefan Agner 
38*6536ca4dSAndrew F. Davis #if CONFIG_IS_ENABLED(DFU)
3922802f4eSStefan Agner 	if (bootdev->boot_device == BOOT_DEVICE_DFU)
4022802f4eSStefan Agner 		spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0");
4122802f4eSStefan Agner #endif
4222802f4eSStefan Agner 
4322802f4eSStefan Agner 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
4422802f4eSStefan Agner 	    image_get_magic(header) == FDT_MAGIC) {
4522802f4eSStefan Agner 		struct spl_load_info load;
4622802f4eSStefan Agner 
4722802f4eSStefan Agner 		debug("Found FIT\n");
4822802f4eSStefan Agner 		load.bl_len = 1;
4922802f4eSStefan Agner 		load.read = spl_ram_load_read;
5022802f4eSStefan Agner 		spl_load_simple_fit(spl_image, &load, 0, header);
5122802f4eSStefan Agner 	} else {
52dbf6be9fSSimon Glass 		ulong u_boot_pos = binman_sym(ulong, u_boot_any, image_pos);
53dfce1799SSimon Glass 
5422802f4eSStefan Agner 		debug("Legacy image\n");
5522802f4eSStefan Agner 		/*
5622802f4eSStefan Agner 		 * Get the header.  It will point to an address defined by
5722802f4eSStefan Agner 		 * handoff which will tell where the image located inside
58dfce1799SSimon Glass 		 * the flash.
5922802f4eSStefan Agner 		 */
60dfce1799SSimon Glass 		debug("u_boot_pos = %lx\n", u_boot_pos);
61dfce1799SSimon Glass 		if (u_boot_pos == BINMAN_SYM_MISSING) {
62dfce1799SSimon Glass 			/*
63dfce1799SSimon Glass 			 * No binman support or no information. For now, fix it
64dfce1799SSimon Glass 			 * to the address pointed to by U-Boot.
65dfce1799SSimon Glass 			 */
6683a64560SMichal Simek 			u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header),
6704ce5427SMarek Vasut 								sizeof(*header));
68dfce1799SSimon Glass 		}
69dfce1799SSimon Glass 		header = (struct image_header *)map_sysmem(u_boot_pos, 0);
7022802f4eSStefan Agner 
7122802f4eSStefan Agner 		spl_parse_image_header(spl_image, header);
7222802f4eSStefan Agner 	}
7322802f4eSStefan Agner 
7422802f4eSStefan Agner 	return 0;
7522802f4eSStefan Agner }
7649132610SMarek Vasut #if CONFIG_IS_ENABLED(RAM_DEVICE)
7722802f4eSStefan Agner SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image);
7822802f4eSStefan Agner #endif
79*6536ca4dSAndrew F. Davis #if CONFIG_IS_ENABLED(DFU)
8022802f4eSStefan Agner SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image);
8122802f4eSStefan Agner #endif
8222802f4eSStefan Agner 
8322802f4eSStefan Agner 
84