xref: /rk3399_ARM-atf/plat/st/common/bl2_io_storage.c (revision 95e4908e17fbb44aed1f8612fefdd6d21fef8f49)
1c9d75b3cSYann Gautier /*
2f87de907SNicolas Toromanoff  * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved.
3c9d75b3cSYann Gautier  *
4c9d75b3cSYann Gautier  * SPDX-License-Identifier: BSD-3-Clause
5c9d75b3cSYann Gautier  */
6c9d75b3cSYann Gautier 
7c9d75b3cSYann Gautier #include <assert.h>
8c9d75b3cSYann Gautier #include <string.h>
9c9d75b3cSYann Gautier 
10c9d75b3cSYann Gautier #include <arch_helpers.h>
11c9d75b3cSYann Gautier #include <common/debug.h>
1218b415beSYann Gautier #include <common/desc_image_load.h>
138dd75531SSughosh Ganu #include <drivers/fwu/fwu.h>
148dd75531SSughosh Ganu #include <drivers/fwu/fwu_metadata.h>
15c9d75b3cSYann Gautier #include <drivers/io/io_block.h>
16c9d75b3cSYann Gautier #include <drivers/io/io_driver.h>
171d204ee4SYann Gautier #include <drivers/io/io_fip.h>
18fa92fef0SPatrick Delaunay #include <drivers/io/io_memmap.h>
1912e21dfdSLionel Debieve #include <drivers/io/io_mtd.h>
20c9d75b3cSYann Gautier #include <drivers/io/io_storage.h>
21c9d75b3cSYann Gautier #include <drivers/mmc.h>
228dd75531SSughosh Ganu #include <drivers/partition/efi.h>
23c9d75b3cSYann Gautier #include <drivers/partition/partition.h>
2412e21dfdSLionel Debieve #include <drivers/raw_nand.h>
2557044228SLionel Debieve #include <drivers/spi_nand.h>
26b1b218fbSLionel Debieve #include <drivers/spi_nor.h>
27c9d75b3cSYann Gautier #include <drivers/st/io_mmc.h>
2812e21dfdSLionel Debieve #include <drivers/st/stm32_fmc2_nand.h>
2957044228SLionel Debieve #include <drivers/st/stm32_qspi.h>
30c9d75b3cSYann Gautier #include <drivers/st/stm32_sdmmc2.h>
31fa92fef0SPatrick Delaunay #include <drivers/usb_device.h>
32d5a84eeaSYann Gautier #include <lib/fconf/fconf.h>
33c9d75b3cSYann Gautier #include <lib/mmio.h>
34c9d75b3cSYann Gautier #include <lib/utils.h>
35c9d75b3cSYann Gautier #include <plat/common/platform.h>
361d204ee4SYann Gautier #include <tools_share/firmware_image_package.h>
371d204ee4SYann Gautier 
381d204ee4SYann Gautier #include <platform_def.h>
39fa92fef0SPatrick Delaunay #include <stm32cubeprogrammer.h>
40d5a84eeaSYann Gautier #include <stm32mp_fconf_getter.h>
41b1391b29SYann Gautier #include <stm32mp_io_storage.h>
42fa92fef0SPatrick Delaunay #include <usb_dfu.h>
43c9d75b3cSYann Gautier 
44c9d75b3cSYann Gautier /* IO devices */
451d204ee4SYann Gautier uintptr_t fip_dev_handle;
461d204ee4SYann Gautier uintptr_t storage_dev_handle;
47c9d75b3cSYann Gautier 
481d204ee4SYann Gautier static const io_dev_connector_t *fip_dev_con;
49c9d75b3cSYann Gautier 
5046554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC
51cddf1bd7SYann Gautier static struct mmc_device_info mmc_info;
52c9d75b3cSYann Gautier 
53c9d75b3cSYann Gautier static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
54c9d75b3cSYann Gautier 
5518b415beSYann Gautier static io_block_dev_spec_t mmc_block_dev_spec = {
56c9d75b3cSYann Gautier 	/* It's used as temp buffer in block driver */
57c9d75b3cSYann Gautier 	.buffer = {
58c9d75b3cSYann Gautier 		.offset = (size_t)&block_buffer,
59c9d75b3cSYann Gautier 		.length = MMC_BLOCK_SIZE,
60c9d75b3cSYann Gautier 	},
61c9d75b3cSYann Gautier 	.ops = {
62c9d75b3cSYann Gautier 		.read = mmc_read_blocks,
63c9d75b3cSYann Gautier 		.write = NULL,
64c9d75b3cSYann Gautier 	},
65c9d75b3cSYann Gautier 	.block_size = MMC_BLOCK_SIZE,
66c9d75b3cSYann Gautier };
67c9d75b3cSYann Gautier 
68c9d75b3cSYann Gautier static const io_dev_connector_t *mmc_dev_con;
6946554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */
70c9d75b3cSYann Gautier 
71b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
72b1b218fbSLionel Debieve static io_mtd_dev_spec_t spi_nor_dev_spec = {
73b1b218fbSLionel Debieve 	.ops = {
74b1b218fbSLionel Debieve 		.init = spi_nor_init,
75b1b218fbSLionel Debieve 		.read = spi_nor_read,
76b1b218fbSLionel Debieve 	},
77b1b218fbSLionel Debieve };
78b1b218fbSLionel Debieve #endif
79b1b218fbSLionel Debieve 
8012e21dfdSLionel Debieve #if STM32MP_RAW_NAND
8112e21dfdSLionel Debieve static io_mtd_dev_spec_t nand_dev_spec = {
8212e21dfdSLionel Debieve 	.ops = {
8312e21dfdSLionel Debieve 		.init = nand_raw_init,
8412e21dfdSLionel Debieve 		.read = nand_read,
851d204ee4SYann Gautier 		.seek = nand_seek_bb
8612e21dfdSLionel Debieve 	},
8712e21dfdSLionel Debieve };
8812e21dfdSLionel Debieve 
8912e21dfdSLionel Debieve static const io_dev_connector_t *nand_dev_con;
9012e21dfdSLionel Debieve #endif
9112e21dfdSLionel Debieve 
9257044228SLionel Debieve #if STM32MP_SPI_NAND
9357044228SLionel Debieve static io_mtd_dev_spec_t spi_nand_dev_spec = {
9457044228SLionel Debieve 	.ops = {
9557044228SLionel Debieve 		.init = spi_nand_init,
9657044228SLionel Debieve 		.read = nand_read,
971d204ee4SYann Gautier 		.seek = nand_seek_bb
9857044228SLionel Debieve 	},
9957044228SLionel Debieve };
100b1b218fbSLionel Debieve #endif
10157044228SLionel Debieve 
102b1b218fbSLionel Debieve #if STM32MP_SPI_NAND || STM32MP_SPI_NOR
10357044228SLionel Debieve static const io_dev_connector_t *spi_dev_con;
10457044228SLionel Debieve #endif
10557044228SLionel Debieve 
1069083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
107fa92fef0SPatrick Delaunay static const io_dev_connector_t *memmap_dev_con;
108fa92fef0SPatrick Delaunay #endif
109fa92fef0SPatrick Delaunay 
110d5a84eeaSYann Gautier io_block_spec_t image_block_spec = {
1111d204ee4SYann Gautier 	.offset = 0U,
1121d204ee4SYann Gautier 	.length = 0U,
113c9d75b3cSYann Gautier };
114c9d75b3cSYann Gautier 
115d5a84eeaSYann Gautier int open_fip(const uintptr_t spec)
116c9d75b3cSYann Gautier {
1171d204ee4SYann Gautier 	return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
118c9d75b3cSYann Gautier }
119c9d75b3cSYann Gautier 
120d5a84eeaSYann Gautier int open_storage(const uintptr_t spec)
121c9d75b3cSYann Gautier {
122c9d75b3cSYann Gautier 	return io_dev_init(storage_dev_handle, 0);
123c9d75b3cSYann Gautier }
124c9d75b3cSYann Gautier 
125*95e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
126*95e4908eSAhmad Fatoum static uint32_t get_boot_part_fip_header(void)
127*95e4908eSAhmad Fatoum {
128*95e4908eSAhmad Fatoum 	io_block_spec_t emmc_boot_fip_block_spec = {
129*95e4908eSAhmad Fatoum 		.offset = STM32MP_EMMC_BOOT_FIP_OFFSET,
130*95e4908eSAhmad Fatoum 		.length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */
131*95e4908eSAhmad Fatoum 	};
132*95e4908eSAhmad Fatoum 	uint32_t magic = 0U;
133*95e4908eSAhmad Fatoum 	int io_result;
134*95e4908eSAhmad Fatoum 	size_t bytes_read;
135*95e4908eSAhmad Fatoum 	uintptr_t fip_hdr_handle;
136*95e4908eSAhmad Fatoum 
137*95e4908eSAhmad Fatoum 	io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_fip_block_spec,
138*95e4908eSAhmad Fatoum 			    &fip_hdr_handle);
139*95e4908eSAhmad Fatoum 	assert(io_result == 0);
140*95e4908eSAhmad Fatoum 
141*95e4908eSAhmad Fatoum 	io_result = io_read(fip_hdr_handle, (uintptr_t)&magic, sizeof(magic),
142*95e4908eSAhmad Fatoum 			    &bytes_read);
143*95e4908eSAhmad Fatoum 	if ((io_result != 0) || (bytes_read != sizeof(magic))) {
144*95e4908eSAhmad Fatoum 		panic();
145*95e4908eSAhmad Fatoum 	}
146*95e4908eSAhmad Fatoum 
147*95e4908eSAhmad Fatoum 	io_close(fip_hdr_handle);
148*95e4908eSAhmad Fatoum 
149*95e4908eSAhmad Fatoum 	VERBOSE("%s: eMMC boot magic at offset 256K: %08x\n",
150*95e4908eSAhmad Fatoum 		__func__, magic);
151*95e4908eSAhmad Fatoum 
152*95e4908eSAhmad Fatoum 	return magic;
153*95e4908eSAhmad Fatoum }
154*95e4908eSAhmad Fatoum #endif
155*95e4908eSAhmad Fatoum 
156c9d75b3cSYann Gautier static void print_boot_device(boot_api_context_t *boot_context)
157c9d75b3cSYann Gautier {
158c9d75b3cSYann Gautier 	switch (boot_context->boot_interface_selected) {
159c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
160c9d75b3cSYann Gautier 		INFO("Using SDMMC\n");
161c9d75b3cSYann Gautier 		break;
162c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
163c9d75b3cSYann Gautier 		INFO("Using EMMC\n");
164c9d75b3cSYann Gautier 		break;
165b1b218fbSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
166b1b218fbSLionel Debieve 		INFO("Using QSPI NOR\n");
167b1b218fbSLionel Debieve 		break;
16812e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
16912e21dfdSLionel Debieve 		INFO("Using FMC NAND\n");
17012e21dfdSLionel Debieve 		break;
17157044228SLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
17257044228SLionel Debieve 		INFO("Using SPI NAND\n");
17357044228SLionel Debieve 		break;
1749083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
1759083fa11SPatrick Delaunay 		INFO("Using UART\n");
1769083fa11SPatrick Delaunay 		break;
177fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
178fa92fef0SPatrick Delaunay 		INFO("Using USB\n");
179fa92fef0SPatrick Delaunay 		break;
180c9d75b3cSYann Gautier 	default:
1811d204ee4SYann Gautier 		ERROR("Boot interface %u not found\n",
1821d204ee4SYann Gautier 		      boot_context->boot_interface_selected);
183c9d75b3cSYann Gautier 		panic();
184c9d75b3cSYann Gautier 		break;
185c9d75b3cSYann Gautier 	}
186c9d75b3cSYann Gautier 
187c9d75b3cSYann Gautier 	if (boot_context->boot_interface_instance != 0U) {
188c9d75b3cSYann Gautier 		INFO("  Instance %d\n", boot_context->boot_interface_instance);
189c9d75b3cSYann Gautier 	}
190c9d75b3cSYann Gautier }
191c9d75b3cSYann Gautier 
19246554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC
1930b1aa772SYann Gautier static void boot_mmc(enum mmc_device_type mmc_dev_type,
1940b1aa772SYann Gautier 		     uint16_t boot_interface_instance)
195c9d75b3cSYann Gautier {
196c9d75b3cSYann Gautier 	int io_result __unused;
197c9d75b3cSYann Gautier 	struct stm32_sdmmc2_params params;
198c9d75b3cSYann Gautier 
19942beea8dSYann Gautier 	zeromem(&params, sizeof(struct stm32_sdmmc2_params));
200c9d75b3cSYann Gautier 
201cddf1bd7SYann Gautier 	mmc_info.mmc_dev_type = mmc_dev_type;
202c9d75b3cSYann Gautier 
2030b1aa772SYann Gautier 	switch (boot_interface_instance) {
204c9d75b3cSYann Gautier 	case 1:
2053f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC1_BASE;
206c9d75b3cSYann Gautier 		break;
207c9d75b3cSYann Gautier 	case 2:
2083f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC2_BASE;
209c9d75b3cSYann Gautier 		break;
210c9d75b3cSYann Gautier 	case 3:
2113f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC3_BASE;
212c9d75b3cSYann Gautier 		break;
213c9d75b3cSYann Gautier 	default:
214c9d75b3cSYann Gautier 		WARN("SDMMC instance not found, using default\n");
2150b1aa772SYann Gautier 		if (mmc_dev_type == MMC_IS_SD) {
2160b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC1_BASE;
2170b1aa772SYann Gautier 		} else {
2180b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC2_BASE;
2190b1aa772SYann Gautier 		}
220c9d75b3cSYann Gautier 		break;
221c9d75b3cSYann Gautier 	}
222c9d75b3cSYann Gautier 
223cddf1bd7SYann Gautier 	params.device_info = &mmc_info;
224c9d75b3cSYann Gautier 	if (stm32_sdmmc2_mmc_init(&params) != 0) {
2250b1aa772SYann Gautier 		ERROR("SDMMC%u init failed\n", boot_interface_instance);
226c9d75b3cSYann Gautier 		panic();
227c9d75b3cSYann Gautier 	}
228c9d75b3cSYann Gautier 
229*95e4908eSAhmad Fatoum 	/* Open MMC as a block device to read FIP */
230c9d75b3cSYann Gautier 	io_result = register_io_dev_block(&mmc_dev_con);
231c9d75b3cSYann Gautier 	if (io_result != 0) {
232c9d75b3cSYann Gautier 		panic();
233c9d75b3cSYann Gautier 	}
234c9d75b3cSYann Gautier 
2350b1aa772SYann Gautier 	io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
236c9d75b3cSYann Gautier 				&storage_dev_handle);
237c9d75b3cSYann Gautier 	assert(io_result == 0);
238*95e4908eSAhmad Fatoum 
239*95e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
240*95e4908eSAhmad Fatoum 	if (mmc_dev_type == MMC_IS_EMMC) {
241*95e4908eSAhmad Fatoum 		io_result = mmc_part_switch_current_boot();
242*95e4908eSAhmad Fatoum 		assert(io_result == 0);
243*95e4908eSAhmad Fatoum 
244*95e4908eSAhmad Fatoum 		if (get_boot_part_fip_header() != TOC_HEADER_NAME) {
245*95e4908eSAhmad Fatoum 			WARN("%s: Can't find FIP header on eMMC boot partition. Trying GPT\n",
246*95e4908eSAhmad Fatoum 			     __func__);
247*95e4908eSAhmad Fatoum 			io_result = mmc_part_switch_user();
248*95e4908eSAhmad Fatoum 			assert(io_result == 0);
249*95e4908eSAhmad Fatoum 			return;
250*95e4908eSAhmad Fatoum 		}
251*95e4908eSAhmad Fatoum 
252*95e4908eSAhmad Fatoum 		VERBOSE("%s: FIP header found on eMMC boot partition\n",
253*95e4908eSAhmad Fatoum 			__func__);
254*95e4908eSAhmad Fatoum 		image_block_spec.offset = STM32MP_EMMC_BOOT_FIP_OFFSET;
255*95e4908eSAhmad Fatoum 	}
256*95e4908eSAhmad Fatoum #endif
2570b1aa772SYann Gautier }
25846554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */
2590b1aa772SYann Gautier 
260b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
261b1b218fbSLionel Debieve static void boot_spi_nor(boot_api_context_t *boot_context)
262b1b218fbSLionel Debieve {
263b1b218fbSLionel Debieve 	int io_result __unused;
264b1b218fbSLionel Debieve 
265b1b218fbSLionel Debieve 	io_result = stm32_qspi_init();
266b1b218fbSLionel Debieve 	assert(io_result == 0);
267b1b218fbSLionel Debieve 
268b1b218fbSLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
269b1b218fbSLionel Debieve 	assert(io_result == 0);
270b1b218fbSLionel Debieve 
271b1b218fbSLionel Debieve 	/* Open connections to device */
272b1b218fbSLionel Debieve 	io_result = io_dev_open(spi_dev_con,
273b1b218fbSLionel Debieve 				(uintptr_t)&spi_nor_dev_spec,
274b1b218fbSLionel Debieve 				&storage_dev_handle);
275b1b218fbSLionel Debieve 	assert(io_result == 0);
276b1b218fbSLionel Debieve }
277b1b218fbSLionel Debieve #endif /* STM32MP_SPI_NOR */
278b1b218fbSLionel Debieve 
27912e21dfdSLionel Debieve #if STM32MP_RAW_NAND
28012e21dfdSLionel Debieve static void boot_fmc2_nand(boot_api_context_t *boot_context)
28112e21dfdSLionel Debieve {
28212e21dfdSLionel Debieve 	int io_result __unused;
28312e21dfdSLionel Debieve 
28412e21dfdSLionel Debieve 	io_result = stm32_fmc2_init();
28512e21dfdSLionel Debieve 	assert(io_result == 0);
28612e21dfdSLionel Debieve 
28712e21dfdSLionel Debieve 	/* Register the IO device on this platform */
28812e21dfdSLionel Debieve 	io_result = register_io_dev_mtd(&nand_dev_con);
28912e21dfdSLionel Debieve 	assert(io_result == 0);
29012e21dfdSLionel Debieve 
29112e21dfdSLionel Debieve 	/* Open connections to device */
29212e21dfdSLionel Debieve 	io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
29312e21dfdSLionel Debieve 				&storage_dev_handle);
29412e21dfdSLionel Debieve 	assert(io_result == 0);
29512e21dfdSLionel Debieve }
29612e21dfdSLionel Debieve #endif /* STM32MP_RAW_NAND */
29712e21dfdSLionel Debieve 
29857044228SLionel Debieve #if STM32MP_SPI_NAND
29957044228SLionel Debieve static void boot_spi_nand(boot_api_context_t *boot_context)
30057044228SLionel Debieve {
30157044228SLionel Debieve 	int io_result __unused;
30257044228SLionel Debieve 
30357044228SLionel Debieve 	io_result = stm32_qspi_init();
30457044228SLionel Debieve 	assert(io_result == 0);
30557044228SLionel Debieve 
30657044228SLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
30757044228SLionel Debieve 	assert(io_result == 0);
30857044228SLionel Debieve 
30957044228SLionel Debieve 	/* Open connections to device */
31057044228SLionel Debieve 	io_result = io_dev_open(spi_dev_con,
31157044228SLionel Debieve 				(uintptr_t)&spi_nand_dev_spec,
31257044228SLionel Debieve 				&storage_dev_handle);
31357044228SLionel Debieve 	assert(io_result == 0);
31457044228SLionel Debieve }
31557044228SLionel Debieve #endif /* STM32MP_SPI_NAND */
31657044228SLionel Debieve 
3179083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
318fa92fef0SPatrick Delaunay static void mmap_io_setup(void)
319fa92fef0SPatrick Delaunay {
320fa92fef0SPatrick Delaunay 	int io_result __unused;
321fa92fef0SPatrick Delaunay 
322fa92fef0SPatrick Delaunay 	io_result = register_io_dev_memmap(&memmap_dev_con);
323fa92fef0SPatrick Delaunay 	assert(io_result == 0);
324fa92fef0SPatrick Delaunay 
325fa92fef0SPatrick Delaunay 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
326fa92fef0SPatrick Delaunay 				&storage_dev_handle);
327fa92fef0SPatrick Delaunay 	assert(io_result == 0);
328fa92fef0SPatrick Delaunay }
329fa92fef0SPatrick Delaunay 
3309083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
3319083fa11SPatrick Delaunay static void stm32cubeprogrammer_uart(void)
3329083fa11SPatrick Delaunay {
3339083fa11SPatrick Delaunay 	int ret __unused;
3349083fa11SPatrick Delaunay 	boot_api_context_t *boot_context =
3359083fa11SPatrick Delaunay 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
3369083fa11SPatrick Delaunay 	uintptr_t uart_base;
3379083fa11SPatrick Delaunay 
3389083fa11SPatrick Delaunay 	uart_base = get_uart_address(boot_context->boot_interface_instance);
3399083fa11SPatrick Delaunay 	ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
3409083fa11SPatrick Delaunay 	assert(ret == 0);
3419083fa11SPatrick Delaunay }
3429083fa11SPatrick Delaunay #endif
3439083fa11SPatrick Delaunay 
3449083fa11SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
345fa92fef0SPatrick Delaunay static void stm32cubeprogrammer_usb(void)
346fa92fef0SPatrick Delaunay {
347fa92fef0SPatrick Delaunay 	int ret __unused;
348fa92fef0SPatrick Delaunay 	struct usb_handle *pdev;
349fa92fef0SPatrick Delaunay 
350fa92fef0SPatrick Delaunay 	/* Init USB on platform */
351fa92fef0SPatrick Delaunay 	pdev = usb_dfu_plat_init();
352fa92fef0SPatrick Delaunay 
353fa92fef0SPatrick Delaunay 	ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
354fa92fef0SPatrick Delaunay 	assert(ret == 0);
355fa92fef0SPatrick Delaunay }
356fa92fef0SPatrick Delaunay #endif
3579083fa11SPatrick Delaunay #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
3589083fa11SPatrick Delaunay 
359fa92fef0SPatrick Delaunay 
3600b1aa772SYann Gautier void stm32mp_io_setup(void)
3610b1aa772SYann Gautier {
3620b1aa772SYann Gautier 	int io_result __unused;
3630b1aa772SYann Gautier 	boot_api_context_t *boot_context =
3640b1aa772SYann Gautier 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
3650b1aa772SYann Gautier 
3660b1aa772SYann Gautier 	print_boot_device(boot_context);
3670b1aa772SYann Gautier 
3680b1aa772SYann Gautier 	if ((boot_context->boot_partition_used_toboot == 1U) ||
3690b1aa772SYann Gautier 	    (boot_context->boot_partition_used_toboot == 2U)) {
3701d204ee4SYann Gautier 		INFO("Boot used partition fsbl%u\n",
3710b1aa772SYann Gautier 		     boot_context->boot_partition_used_toboot);
3720b1aa772SYann Gautier 	}
3730b1aa772SYann Gautier 
3741d204ee4SYann Gautier 	io_result = register_io_dev_fip(&fip_dev_con);
3750b1aa772SYann Gautier 	assert(io_result == 0);
3760b1aa772SYann Gautier 
3771d204ee4SYann Gautier 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
3781d204ee4SYann Gautier 				&fip_dev_handle);
3790b1aa772SYann Gautier 
3800b1aa772SYann Gautier 	switch (boot_context->boot_interface_selected) {
38146554b64SNicolas Le Bayon #if STM32MP_SDMMC
3820b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
3830b1aa772SYann Gautier 		dmbsy();
3840b1aa772SYann Gautier 		boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
3850b1aa772SYann Gautier 		break;
38646554b64SNicolas Le Bayon #endif
38746554b64SNicolas Le Bayon #if STM32MP_EMMC
3880b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
3890b1aa772SYann Gautier 		dmbsy();
3900b1aa772SYann Gautier 		boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
391c9d75b3cSYann Gautier 		break;
39246554b64SNicolas Le Bayon #endif
393b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
394b1b218fbSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
395b1b218fbSLionel Debieve 		dmbsy();
396b1b218fbSLionel Debieve 		boot_spi_nor(boot_context);
397b1b218fbSLionel Debieve 		break;
398b1b218fbSLionel Debieve #endif
39912e21dfdSLionel Debieve #if STM32MP_RAW_NAND
40012e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
40112e21dfdSLionel Debieve 		dmbsy();
40212e21dfdSLionel Debieve 		boot_fmc2_nand(boot_context);
40312e21dfdSLionel Debieve 		break;
40412e21dfdSLionel Debieve #endif
40557044228SLionel Debieve #if STM32MP_SPI_NAND
40657044228SLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
40757044228SLionel Debieve 		dmbsy();
40857044228SLionel Debieve 		boot_spi_nand(boot_context);
40957044228SLionel Debieve 		break;
41057044228SLionel Debieve #endif
4119083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
4129083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
4139083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
4149083fa11SPatrick Delaunay #endif
415fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
416fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
4179083fa11SPatrick Delaunay #endif
418fa92fef0SPatrick Delaunay 		dmbsy();
419fa92fef0SPatrick Delaunay 		mmap_io_setup();
420fa92fef0SPatrick Delaunay 		break;
421fa92fef0SPatrick Delaunay #endif
422c9d75b3cSYann Gautier 
423c9d75b3cSYann Gautier 	default:
424c9d75b3cSYann Gautier 		ERROR("Boot interface %d not supported\n",
425c9d75b3cSYann Gautier 		      boot_context->boot_interface_selected);
42671693a66SYann Gautier 		panic();
427c9d75b3cSYann Gautier 		break;
428c9d75b3cSYann Gautier 	}
429c9d75b3cSYann Gautier }
430c9d75b3cSYann Gautier 
4311d204ee4SYann Gautier int bl2_plat_handle_pre_image_load(unsigned int image_id)
4321d204ee4SYann Gautier {
4331d204ee4SYann Gautier 	static bool gpt_init_done __unused;
4341d204ee4SYann Gautier 	uint16_t boot_itf = stm32mp_get_boot_itf_selected();
4351d204ee4SYann Gautier 
4361d204ee4SYann Gautier 	switch (boot_itf) {
4371d204ee4SYann Gautier #if STM32MP_SDMMC || STM32MP_EMMC
4381d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
439*95e4908eSAhmad Fatoum #if STM32MP_EMMC_BOOT
440*95e4908eSAhmad Fatoum 		if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) {
441*95e4908eSAhmad Fatoum 			break;
442*95e4908eSAhmad Fatoum 		}
443*95e4908eSAhmad Fatoum #endif
444*95e4908eSAhmad Fatoum 		/* fallthrough */
445*95e4908eSAhmad Fatoum 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
4461d204ee4SYann Gautier 		if (!gpt_init_done) {
4478dd75531SSughosh Ganu /*
4488dd75531SSughosh Ganu  * With FWU Multi Bank feature enabled, the selection of
4498dd75531SSughosh Ganu  * the image to boot will be done by fwu_init calling the
4508dd75531SSughosh Ganu  * platform hook, plat_fwu_set_images_source.
4518dd75531SSughosh Ganu  */
4528dd75531SSughosh Ganu #if !PSA_FWU_SUPPORT
4531d204ee4SYann Gautier 			const partition_entry_t *entry;
4541d204ee4SYann Gautier 
4551d204ee4SYann Gautier 			partition_init(GPT_IMAGE_ID);
4561d204ee4SYann Gautier 			entry = get_partition_entry(FIP_IMAGE_NAME);
4571d204ee4SYann Gautier 			if (entry == NULL) {
4581d204ee4SYann Gautier 				ERROR("Could NOT find the %s partition!\n",
4591d204ee4SYann Gautier 				      FIP_IMAGE_NAME);
4601d204ee4SYann Gautier 				return -ENOENT;
4611d204ee4SYann Gautier 			}
4621d204ee4SYann Gautier 
4631d204ee4SYann Gautier 			image_block_spec.offset = entry->start;
4641d204ee4SYann Gautier 			image_block_spec.length = entry->length;
4658dd75531SSughosh Ganu #endif
4661d204ee4SYann Gautier 			gpt_init_done = true;
46718b415beSYann Gautier 		} else {
46818b415beSYann Gautier 			bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
4692deff904SYann Gautier 			assert(bl_mem_params != NULL);
47018b415beSYann Gautier 
47118b415beSYann Gautier 			mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base;
47218b415beSYann Gautier 			mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size;
4731d204ee4SYann Gautier 		}
4741d204ee4SYann Gautier 
4751d204ee4SYann Gautier 		break;
4761d204ee4SYann Gautier #endif
4771d204ee4SYann Gautier 
4781d204ee4SYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
4791d204ee4SYann Gautier #if STM32MP_RAW_NAND
4801d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
4811d204ee4SYann Gautier #endif
4821d204ee4SYann Gautier #if STM32MP_SPI_NAND
4831d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
4841d204ee4SYann Gautier #endif
4851d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NAND_FIP_OFFSET;
4861d204ee4SYann Gautier 		break;
4871d204ee4SYann Gautier #endif
4881d204ee4SYann Gautier 
4891d204ee4SYann Gautier #if STM32MP_SPI_NOR
4901d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
4911d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NOR_FIP_OFFSET;
4921d204ee4SYann Gautier 		break;
4931d204ee4SYann Gautier #endif
4941d204ee4SYann Gautier 
4959083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
4969083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
4979083fa11SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
4989083fa11SPatrick Delaunay 			stm32cubeprogrammer_uart();
4999083fa11SPatrick Delaunay 			/* FIP loaded at DWL address */
5009083fa11SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
5019083fa11SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
5029083fa11SPatrick Delaunay 		}
5039083fa11SPatrick Delaunay 		break;
5049083fa11SPatrick Delaunay #endif
505fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
506fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
507fa92fef0SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
508fa92fef0SPatrick Delaunay 			stm32cubeprogrammer_usb();
509fa92fef0SPatrick Delaunay 			/* FIP loaded at DWL address */
510fa92fef0SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
511fa92fef0SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
512fa92fef0SPatrick Delaunay 		}
513fa92fef0SPatrick Delaunay 		break;
514fa92fef0SPatrick Delaunay #endif
515fa92fef0SPatrick Delaunay 
5161d204ee4SYann Gautier 	default:
5171d204ee4SYann Gautier 		ERROR("FIP Not found\n");
5181d204ee4SYann Gautier 		panic();
5191d204ee4SYann Gautier 	}
5201d204ee4SYann Gautier 
5211d204ee4SYann Gautier 	return 0;
5221d204ee4SYann Gautier }
5231d204ee4SYann Gautier 
524c9d75b3cSYann Gautier /*
525c9d75b3cSYann Gautier  * Return an IO device handle and specification which can be used to access
526c9d75b3cSYann Gautier  * an image. Use this to enforce platform load policy.
527c9d75b3cSYann Gautier  */
528c9d75b3cSYann Gautier int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
529c9d75b3cSYann Gautier 			  uintptr_t *image_spec)
530c9d75b3cSYann Gautier {
531c9d75b3cSYann Gautier 	int rc;
532c9d75b3cSYann Gautier 	const struct plat_io_policy *policy;
533c9d75b3cSYann Gautier 
534d5a84eeaSYann Gautier 	policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id);
535c9d75b3cSYann Gautier 	rc = policy->check(policy->image_spec);
536c9d75b3cSYann Gautier 	if (rc == 0) {
537c9d75b3cSYann Gautier 		*image_spec = policy->image_spec;
538c9d75b3cSYann Gautier 		*dev_handle = *(policy->dev_handle);
539c9d75b3cSYann Gautier 	}
540c9d75b3cSYann Gautier 
541c9d75b3cSYann Gautier 	return rc;
542c9d75b3cSYann Gautier }
5438dd75531SSughosh Ganu 
5448dd75531SSughosh Ganu #if (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT
5458dd75531SSughosh Ganu /*
546f87de907SNicolas Toromanoff  * In each boot in non-trial mode, we set the BKP register to
547f87de907SNicolas Toromanoff  * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata.
548f87de907SNicolas Toromanoff  *
549f87de907SNicolas Toromanoff  * As long as the update agent didn't update the "accepted" field in metadata
550f87de907SNicolas Toromanoff  * (i.e. we are in trial mode), we select the new active_index.
551f87de907SNicolas Toromanoff  * To avoid infinite boot loop at trial boot we decrement a BKP register.
552f87de907SNicolas Toromanoff  * If this counter is 0:
553f87de907SNicolas Toromanoff  *     - an unexpected TAMPER event raised (that resets the BKP registers to 0)
554f87de907SNicolas Toromanoff  *     - a power-off occurs before the update agent was able to update the
555f87de907SNicolas Toromanoff  *       "accepted' field
556f87de907SNicolas Toromanoff  *     - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode.
557f87de907SNicolas Toromanoff  * we select the previous_active_index.
5588dd75531SSughosh Ganu  */
559f87de907SNicolas Toromanoff #define INVALID_BOOT_IDX		0xFFFFFFFF
560f87de907SNicolas Toromanoff 
5618dd75531SSughosh Ganu uint32_t plat_fwu_get_boot_idx(void)
5628dd75531SSughosh Ganu {
563f87de907SNicolas Toromanoff 	/*
564f87de907SNicolas Toromanoff 	 * Select boot index and update boot counter only once per boot
565f87de907SNicolas Toromanoff 	 * even if this function is called several times.
566f87de907SNicolas Toromanoff 	 */
567f87de907SNicolas Toromanoff 	static uint32_t boot_idx = INVALID_BOOT_IDX;
568f87de907SNicolas Toromanoff 	const struct fwu_metadata *data;
5698dd75531SSughosh Ganu 
570f87de907SNicolas Toromanoff 	data = fwu_get_metadata();
5718dd75531SSughosh Ganu 
572f87de907SNicolas Toromanoff 	if (boot_idx == INVALID_BOOT_IDX) {
573f87de907SNicolas Toromanoff 		boot_idx = data->active_index;
574f87de907SNicolas Toromanoff 		if (fwu_is_trial_run_state()) {
575f87de907SNicolas Toromanoff 			if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) {
576f87de907SNicolas Toromanoff 				WARN("Trial FWU fails %u times\n",
577f87de907SNicolas Toromanoff 				     FWU_MAX_TRIAL_REBOOT);
578f87de907SNicolas Toromanoff 				boot_idx = data->previous_active_index;
579f87de907SNicolas Toromanoff 			}
580f87de907SNicolas Toromanoff 		} else {
581f87de907SNicolas Toromanoff 			stm32_set_max_fwu_trial_boot_cnt();
582f87de907SNicolas Toromanoff 		}
583f87de907SNicolas Toromanoff 	}
584f87de907SNicolas Toromanoff 
585f87de907SNicolas Toromanoff 	return boot_idx;
5868dd75531SSughosh Ganu }
5878dd75531SSughosh Ganu 
5888dd75531SSughosh Ganu static void *stm32_get_image_spec(const uuid_t *img_type_uuid)
5898dd75531SSughosh Ganu {
5908dd75531SSughosh Ganu 	unsigned int i;
5918dd75531SSughosh Ganu 
5928dd75531SSughosh Ganu 	for (i = 0U; i < MAX_NUMBER_IDS; i++) {
5938dd75531SSughosh Ganu 		if ((guidcmp(&policies[i].img_type_guid, img_type_uuid)) == 0) {
5948dd75531SSughosh Ganu 			return (void *)policies[i].image_spec;
5958dd75531SSughosh Ganu 		}
5968dd75531SSughosh Ganu 	}
5978dd75531SSughosh Ganu 
5988dd75531SSughosh Ganu 	return NULL;
5998dd75531SSughosh Ganu }
6008dd75531SSughosh Ganu 
6018dd75531SSughosh Ganu void plat_fwu_set_images_source(const struct fwu_metadata *metadata)
6028dd75531SSughosh Ganu {
6038dd75531SSughosh Ganu 	unsigned int i;
6048dd75531SSughosh Ganu 	uint32_t boot_idx;
6058dd75531SSughosh Ganu 	const partition_entry_t *entry;
6068dd75531SSughosh Ganu 	const uuid_t *img_type_uuid, *img_uuid;
6078dd75531SSughosh Ganu 	io_block_spec_t *image_spec;
6088dd75531SSughosh Ganu 
6098dd75531SSughosh Ganu 	boot_idx = plat_fwu_get_boot_idx();
6108dd75531SSughosh Ganu 	assert(boot_idx < NR_OF_FW_BANKS);
6118dd75531SSughosh Ganu 
6128dd75531SSughosh Ganu 	for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) {
6138dd75531SSughosh Ganu 		img_type_uuid = &metadata->img_entry[i].img_type_uuid;
6148dd75531SSughosh Ganu 		image_spec = stm32_get_image_spec(img_type_uuid);
6158dd75531SSughosh Ganu 		if (image_spec == NULL) {
6168dd75531SSughosh Ganu 			ERROR("Unable to get image spec for the image in the metadata\n");
6178dd75531SSughosh Ganu 			panic();
6188dd75531SSughosh Ganu 		}
6198dd75531SSughosh Ganu 
6208dd75531SSughosh Ganu 		img_uuid =
6218dd75531SSughosh Ganu 			&metadata->img_entry[i].img_props[boot_idx].img_uuid;
6228dd75531SSughosh Ganu 
6238dd75531SSughosh Ganu 		entry = get_partition_entry_by_uuid(img_uuid);
6248dd75531SSughosh Ganu 		if (entry == NULL) {
6258dd75531SSughosh Ganu 			ERROR("Unable to find the partition with the uuid mentioned in metadata\n");
6268dd75531SSughosh Ganu 			panic();
6278dd75531SSughosh Ganu 		}
6288dd75531SSughosh Ganu 
6298dd75531SSughosh Ganu 		image_spec->offset = entry->start;
6308dd75531SSughosh Ganu 		image_spec->length = entry->length;
6318dd75531SSughosh Ganu 	}
6328dd75531SSughosh Ganu }
6330ca180f6SSughosh Ganu 
6340ca180f6SSughosh Ganu static int plat_set_image_source(unsigned int image_id,
6350ca180f6SSughosh Ganu 				 uintptr_t *handle,
6360ca180f6SSughosh Ganu 				 uintptr_t *image_spec,
6370ca180f6SSughosh Ganu 				 const char *part_name)
6380ca180f6SSughosh Ganu {
6390ca180f6SSughosh Ganu 	struct plat_io_policy *policy;
6400ca180f6SSughosh Ganu 	io_block_spec_t *spec;
6410ca180f6SSughosh Ganu 	const partition_entry_t *entry = get_partition_entry(part_name);
6420ca180f6SSughosh Ganu 
6430ca180f6SSughosh Ganu 	if (entry == NULL) {
6440ca180f6SSughosh Ganu 		ERROR("Unable to find the %s partition\n", part_name);
6450ca180f6SSughosh Ganu 		return -ENOENT;
6460ca180f6SSughosh Ganu 	}
6470ca180f6SSughosh Ganu 
6480ca180f6SSughosh Ganu 	policy = &policies[image_id];
6490ca180f6SSughosh Ganu 
6500ca180f6SSughosh Ganu 	spec = (io_block_spec_t *)policy->image_spec;
6510ca180f6SSughosh Ganu 	spec->offset = entry->start;
6520ca180f6SSughosh Ganu 	spec->length = entry->length;
6530ca180f6SSughosh Ganu 
6540ca180f6SSughosh Ganu 	*image_spec = policy->image_spec;
6550ca180f6SSughosh Ganu 	*handle = *policy->dev_handle;
6560ca180f6SSughosh Ganu 
6570ca180f6SSughosh Ganu 	return 0;
6580ca180f6SSughosh Ganu }
6590ca180f6SSughosh Ganu 
6600ca180f6SSughosh Ganu int plat_fwu_set_metadata_image_source(unsigned int image_id,
6610ca180f6SSughosh Ganu 				       uintptr_t *handle,
6620ca180f6SSughosh Ganu 				       uintptr_t *image_spec)
6630ca180f6SSughosh Ganu {
6640ca180f6SSughosh Ganu 	char *part_name;
6650ca180f6SSughosh Ganu 
6660ca180f6SSughosh Ganu 	assert((image_id == FWU_METADATA_IMAGE_ID) ||
6670ca180f6SSughosh Ganu 	       (image_id == BKUP_FWU_METADATA_IMAGE_ID));
6680ca180f6SSughosh Ganu 
6690ca180f6SSughosh Ganu 	partition_init(GPT_IMAGE_ID);
6700ca180f6SSughosh Ganu 
6710ca180f6SSughosh Ganu 	if (image_id == FWU_METADATA_IMAGE_ID) {
6720ca180f6SSughosh Ganu 		part_name = METADATA_PART_1;
6730ca180f6SSughosh Ganu 	} else {
6740ca180f6SSughosh Ganu 		part_name = METADATA_PART_2;
6750ca180f6SSughosh Ganu 	}
6760ca180f6SSughosh Ganu 
6770ca180f6SSughosh Ganu 	return plat_set_image_source(image_id, handle, image_spec,
6780ca180f6SSughosh Ganu 				     part_name);
6790ca180f6SSughosh Ganu }
6808dd75531SSughosh Ganu #endif /* (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT */
681