xref: /rk3399_ARM-atf/plat/st/common/bl2_io_storage.c (revision b1391b294ca7803f46bc47048b4a02a15dda9a16)
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>
41*b1391b29SYann 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 
125c9d75b3cSYann Gautier static void print_boot_device(boot_api_context_t *boot_context)
126c9d75b3cSYann Gautier {
127c9d75b3cSYann Gautier 	switch (boot_context->boot_interface_selected) {
128c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
129c9d75b3cSYann Gautier 		INFO("Using SDMMC\n");
130c9d75b3cSYann Gautier 		break;
131c9d75b3cSYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
132c9d75b3cSYann Gautier 		INFO("Using EMMC\n");
133c9d75b3cSYann Gautier 		break;
134b1b218fbSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
135b1b218fbSLionel Debieve 		INFO("Using QSPI NOR\n");
136b1b218fbSLionel Debieve 		break;
13712e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
13812e21dfdSLionel Debieve 		INFO("Using FMC NAND\n");
13912e21dfdSLionel Debieve 		break;
14057044228SLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
14157044228SLionel Debieve 		INFO("Using SPI NAND\n");
14257044228SLionel Debieve 		break;
1439083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
1449083fa11SPatrick Delaunay 		INFO("Using UART\n");
1459083fa11SPatrick Delaunay 		break;
146fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
147fa92fef0SPatrick Delaunay 		INFO("Using USB\n");
148fa92fef0SPatrick Delaunay 		break;
149c9d75b3cSYann Gautier 	default:
1501d204ee4SYann Gautier 		ERROR("Boot interface %u not found\n",
1511d204ee4SYann Gautier 		      boot_context->boot_interface_selected);
152c9d75b3cSYann Gautier 		panic();
153c9d75b3cSYann Gautier 		break;
154c9d75b3cSYann Gautier 	}
155c9d75b3cSYann Gautier 
156c9d75b3cSYann Gautier 	if (boot_context->boot_interface_instance != 0U) {
157c9d75b3cSYann Gautier 		INFO("  Instance %d\n", boot_context->boot_interface_instance);
158c9d75b3cSYann Gautier 	}
159c9d75b3cSYann Gautier }
160c9d75b3cSYann Gautier 
16146554b64SNicolas Le Bayon #if STM32MP_SDMMC || STM32MP_EMMC
1620b1aa772SYann Gautier static void boot_mmc(enum mmc_device_type mmc_dev_type,
1630b1aa772SYann Gautier 		     uint16_t boot_interface_instance)
164c9d75b3cSYann Gautier {
165c9d75b3cSYann Gautier 	int io_result __unused;
166c9d75b3cSYann Gautier 	struct stm32_sdmmc2_params params;
167c9d75b3cSYann Gautier 
16842beea8dSYann Gautier 	zeromem(&params, sizeof(struct stm32_sdmmc2_params));
169c9d75b3cSYann Gautier 
170cddf1bd7SYann Gautier 	mmc_info.mmc_dev_type = mmc_dev_type;
171c9d75b3cSYann Gautier 
1720b1aa772SYann Gautier 	switch (boot_interface_instance) {
173c9d75b3cSYann Gautier 	case 1:
1743f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC1_BASE;
175c9d75b3cSYann Gautier 		break;
176c9d75b3cSYann Gautier 	case 2:
1773f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC2_BASE;
178c9d75b3cSYann Gautier 		break;
179c9d75b3cSYann Gautier 	case 3:
1803f9c9784SYann Gautier 		params.reg_base = STM32MP_SDMMC3_BASE;
181c9d75b3cSYann Gautier 		break;
182c9d75b3cSYann Gautier 	default:
183c9d75b3cSYann Gautier 		WARN("SDMMC instance not found, using default\n");
1840b1aa772SYann Gautier 		if (mmc_dev_type == MMC_IS_SD) {
1850b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC1_BASE;
1860b1aa772SYann Gautier 		} else {
1870b1aa772SYann Gautier 			params.reg_base = STM32MP_SDMMC2_BASE;
1880b1aa772SYann Gautier 		}
189c9d75b3cSYann Gautier 		break;
190c9d75b3cSYann Gautier 	}
191c9d75b3cSYann Gautier 
192cddf1bd7SYann Gautier 	params.device_info = &mmc_info;
193c9d75b3cSYann Gautier 	if (stm32_sdmmc2_mmc_init(&params) != 0) {
1940b1aa772SYann Gautier 		ERROR("SDMMC%u init failed\n", boot_interface_instance);
195c9d75b3cSYann Gautier 		panic();
196c9d75b3cSYann Gautier 	}
197c9d75b3cSYann Gautier 
198c9d75b3cSYann Gautier 	/* Open MMC as a block device to read GPT table */
199c9d75b3cSYann Gautier 	io_result = register_io_dev_block(&mmc_dev_con);
200c9d75b3cSYann Gautier 	if (io_result != 0) {
201c9d75b3cSYann Gautier 		panic();
202c9d75b3cSYann Gautier 	}
203c9d75b3cSYann Gautier 
2040b1aa772SYann Gautier 	io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
205c9d75b3cSYann Gautier 				&storage_dev_handle);
206c9d75b3cSYann Gautier 	assert(io_result == 0);
2070b1aa772SYann Gautier }
20846554b64SNicolas Le Bayon #endif /* STM32MP_SDMMC || STM32MP_EMMC */
2090b1aa772SYann Gautier 
210b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
211b1b218fbSLionel Debieve static void boot_spi_nor(boot_api_context_t *boot_context)
212b1b218fbSLionel Debieve {
213b1b218fbSLionel Debieve 	int io_result __unused;
214b1b218fbSLionel Debieve 
215b1b218fbSLionel Debieve 	io_result = stm32_qspi_init();
216b1b218fbSLionel Debieve 	assert(io_result == 0);
217b1b218fbSLionel Debieve 
218b1b218fbSLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
219b1b218fbSLionel Debieve 	assert(io_result == 0);
220b1b218fbSLionel Debieve 
221b1b218fbSLionel Debieve 	/* Open connections to device */
222b1b218fbSLionel Debieve 	io_result = io_dev_open(spi_dev_con,
223b1b218fbSLionel Debieve 				(uintptr_t)&spi_nor_dev_spec,
224b1b218fbSLionel Debieve 				&storage_dev_handle);
225b1b218fbSLionel Debieve 	assert(io_result == 0);
226b1b218fbSLionel Debieve }
227b1b218fbSLionel Debieve #endif /* STM32MP_SPI_NOR */
228b1b218fbSLionel Debieve 
22912e21dfdSLionel Debieve #if STM32MP_RAW_NAND
23012e21dfdSLionel Debieve static void boot_fmc2_nand(boot_api_context_t *boot_context)
23112e21dfdSLionel Debieve {
23212e21dfdSLionel Debieve 	int io_result __unused;
23312e21dfdSLionel Debieve 
23412e21dfdSLionel Debieve 	io_result = stm32_fmc2_init();
23512e21dfdSLionel Debieve 	assert(io_result == 0);
23612e21dfdSLionel Debieve 
23712e21dfdSLionel Debieve 	/* Register the IO device on this platform */
23812e21dfdSLionel Debieve 	io_result = register_io_dev_mtd(&nand_dev_con);
23912e21dfdSLionel Debieve 	assert(io_result == 0);
24012e21dfdSLionel Debieve 
24112e21dfdSLionel Debieve 	/* Open connections to device */
24212e21dfdSLionel Debieve 	io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
24312e21dfdSLionel Debieve 				&storage_dev_handle);
24412e21dfdSLionel Debieve 	assert(io_result == 0);
24512e21dfdSLionel Debieve }
24612e21dfdSLionel Debieve #endif /* STM32MP_RAW_NAND */
24712e21dfdSLionel Debieve 
24857044228SLionel Debieve #if STM32MP_SPI_NAND
24957044228SLionel Debieve static void boot_spi_nand(boot_api_context_t *boot_context)
25057044228SLionel Debieve {
25157044228SLionel Debieve 	int io_result __unused;
25257044228SLionel Debieve 
25357044228SLionel Debieve 	io_result = stm32_qspi_init();
25457044228SLionel Debieve 	assert(io_result == 0);
25557044228SLionel Debieve 
25657044228SLionel Debieve 	io_result = register_io_dev_mtd(&spi_dev_con);
25757044228SLionel Debieve 	assert(io_result == 0);
25857044228SLionel Debieve 
25957044228SLionel Debieve 	/* Open connections to device */
26057044228SLionel Debieve 	io_result = io_dev_open(spi_dev_con,
26157044228SLionel Debieve 				(uintptr_t)&spi_nand_dev_spec,
26257044228SLionel Debieve 				&storage_dev_handle);
26357044228SLionel Debieve 	assert(io_result == 0);
26457044228SLionel Debieve }
26557044228SLionel Debieve #endif /* STM32MP_SPI_NAND */
26657044228SLionel Debieve 
2679083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
268fa92fef0SPatrick Delaunay static void mmap_io_setup(void)
269fa92fef0SPatrick Delaunay {
270fa92fef0SPatrick Delaunay 	int io_result __unused;
271fa92fef0SPatrick Delaunay 
272fa92fef0SPatrick Delaunay 	io_result = register_io_dev_memmap(&memmap_dev_con);
273fa92fef0SPatrick Delaunay 	assert(io_result == 0);
274fa92fef0SPatrick Delaunay 
275fa92fef0SPatrick Delaunay 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
276fa92fef0SPatrick Delaunay 				&storage_dev_handle);
277fa92fef0SPatrick Delaunay 	assert(io_result == 0);
278fa92fef0SPatrick Delaunay }
279fa92fef0SPatrick Delaunay 
2809083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
2819083fa11SPatrick Delaunay static void stm32cubeprogrammer_uart(void)
2829083fa11SPatrick Delaunay {
2839083fa11SPatrick Delaunay 	int ret __unused;
2849083fa11SPatrick Delaunay 	boot_api_context_t *boot_context =
2859083fa11SPatrick Delaunay 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
2869083fa11SPatrick Delaunay 	uintptr_t uart_base;
2879083fa11SPatrick Delaunay 
2889083fa11SPatrick Delaunay 	uart_base = get_uart_address(boot_context->boot_interface_instance);
2899083fa11SPatrick Delaunay 	ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
2909083fa11SPatrick Delaunay 	assert(ret == 0);
2919083fa11SPatrick Delaunay }
2929083fa11SPatrick Delaunay #endif
2939083fa11SPatrick Delaunay 
2949083fa11SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
295fa92fef0SPatrick Delaunay static void stm32cubeprogrammer_usb(void)
296fa92fef0SPatrick Delaunay {
297fa92fef0SPatrick Delaunay 	int ret __unused;
298fa92fef0SPatrick Delaunay 	struct usb_handle *pdev;
299fa92fef0SPatrick Delaunay 
300fa92fef0SPatrick Delaunay 	/* Init USB on platform */
301fa92fef0SPatrick Delaunay 	pdev = usb_dfu_plat_init();
302fa92fef0SPatrick Delaunay 
303fa92fef0SPatrick Delaunay 	ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
304fa92fef0SPatrick Delaunay 	assert(ret == 0);
305fa92fef0SPatrick Delaunay }
306fa92fef0SPatrick Delaunay #endif
3079083fa11SPatrick Delaunay #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
3089083fa11SPatrick Delaunay 
309fa92fef0SPatrick Delaunay 
3100b1aa772SYann Gautier void stm32mp_io_setup(void)
3110b1aa772SYann Gautier {
3120b1aa772SYann Gautier 	int io_result __unused;
3130b1aa772SYann Gautier 	boot_api_context_t *boot_context =
3140b1aa772SYann Gautier 		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
3150b1aa772SYann Gautier 
3160b1aa772SYann Gautier 	print_boot_device(boot_context);
3170b1aa772SYann Gautier 
3180b1aa772SYann Gautier 	if ((boot_context->boot_partition_used_toboot == 1U) ||
3190b1aa772SYann Gautier 	    (boot_context->boot_partition_used_toboot == 2U)) {
3201d204ee4SYann Gautier 		INFO("Boot used partition fsbl%u\n",
3210b1aa772SYann Gautier 		     boot_context->boot_partition_used_toboot);
3220b1aa772SYann Gautier 	}
3230b1aa772SYann Gautier 
3241d204ee4SYann Gautier 	io_result = register_io_dev_fip(&fip_dev_con);
3250b1aa772SYann Gautier 	assert(io_result == 0);
3260b1aa772SYann Gautier 
3271d204ee4SYann Gautier 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
3281d204ee4SYann Gautier 				&fip_dev_handle);
3290b1aa772SYann Gautier 
3300b1aa772SYann Gautier 	switch (boot_context->boot_interface_selected) {
33146554b64SNicolas Le Bayon #if STM32MP_SDMMC
3320b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
3330b1aa772SYann Gautier 		dmbsy();
3340b1aa772SYann Gautier 		boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
3350b1aa772SYann Gautier 		break;
33646554b64SNicolas Le Bayon #endif
33746554b64SNicolas Le Bayon #if STM32MP_EMMC
3380b1aa772SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
3390b1aa772SYann Gautier 		dmbsy();
3400b1aa772SYann Gautier 		boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
341c9d75b3cSYann Gautier 		break;
34246554b64SNicolas Le Bayon #endif
343b1b218fbSLionel Debieve #if STM32MP_SPI_NOR
344b1b218fbSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
345b1b218fbSLionel Debieve 		dmbsy();
346b1b218fbSLionel Debieve 		boot_spi_nor(boot_context);
347b1b218fbSLionel Debieve 		break;
348b1b218fbSLionel Debieve #endif
34912e21dfdSLionel Debieve #if STM32MP_RAW_NAND
35012e21dfdSLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
35112e21dfdSLionel Debieve 		dmbsy();
35212e21dfdSLionel Debieve 		boot_fmc2_nand(boot_context);
35312e21dfdSLionel Debieve 		break;
35412e21dfdSLionel Debieve #endif
35557044228SLionel Debieve #if STM32MP_SPI_NAND
35657044228SLionel Debieve 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
35757044228SLionel Debieve 		dmbsy();
35857044228SLionel Debieve 		boot_spi_nand(boot_context);
35957044228SLionel Debieve 		break;
36057044228SLionel Debieve #endif
3619083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
3629083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
3639083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
3649083fa11SPatrick Delaunay #endif
365fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
366fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
3679083fa11SPatrick Delaunay #endif
368fa92fef0SPatrick Delaunay 		dmbsy();
369fa92fef0SPatrick Delaunay 		mmap_io_setup();
370fa92fef0SPatrick Delaunay 		break;
371fa92fef0SPatrick Delaunay #endif
372c9d75b3cSYann Gautier 
373c9d75b3cSYann Gautier 	default:
374c9d75b3cSYann Gautier 		ERROR("Boot interface %d not supported\n",
375c9d75b3cSYann Gautier 		      boot_context->boot_interface_selected);
37671693a66SYann Gautier 		panic();
377c9d75b3cSYann Gautier 		break;
378c9d75b3cSYann Gautier 	}
379c9d75b3cSYann Gautier }
380c9d75b3cSYann Gautier 
3811d204ee4SYann Gautier int bl2_plat_handle_pre_image_load(unsigned int image_id)
3821d204ee4SYann Gautier {
3831d204ee4SYann Gautier 	static bool gpt_init_done __unused;
3841d204ee4SYann Gautier 	uint16_t boot_itf = stm32mp_get_boot_itf_selected();
3851d204ee4SYann Gautier 
3861d204ee4SYann Gautier 	switch (boot_itf) {
3871d204ee4SYann Gautier #if STM32MP_SDMMC || STM32MP_EMMC
3881d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
3891d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
3901d204ee4SYann Gautier 		if (!gpt_init_done) {
3918dd75531SSughosh Ganu /*
3928dd75531SSughosh Ganu  * With FWU Multi Bank feature enabled, the selection of
3938dd75531SSughosh Ganu  * the image to boot will be done by fwu_init calling the
3948dd75531SSughosh Ganu  * platform hook, plat_fwu_set_images_source.
3958dd75531SSughosh Ganu  */
3968dd75531SSughosh Ganu #if !PSA_FWU_SUPPORT
3971d204ee4SYann Gautier 			const partition_entry_t *entry;
3981d204ee4SYann Gautier 
3991d204ee4SYann Gautier 			partition_init(GPT_IMAGE_ID);
4001d204ee4SYann Gautier 			entry = get_partition_entry(FIP_IMAGE_NAME);
4011d204ee4SYann Gautier 			if (entry == NULL) {
4021d204ee4SYann Gautier 				ERROR("Could NOT find the %s partition!\n",
4031d204ee4SYann Gautier 				      FIP_IMAGE_NAME);
4041d204ee4SYann Gautier 				return -ENOENT;
4051d204ee4SYann Gautier 			}
4061d204ee4SYann Gautier 
4071d204ee4SYann Gautier 			image_block_spec.offset = entry->start;
4081d204ee4SYann Gautier 			image_block_spec.length = entry->length;
4098dd75531SSughosh Ganu #endif
4101d204ee4SYann Gautier 			gpt_init_done = true;
41118b415beSYann Gautier 		} else {
41218b415beSYann Gautier 			bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
41318b415beSYann Gautier 
41418b415beSYann Gautier 			mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base;
41518b415beSYann Gautier 			mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size;
4161d204ee4SYann Gautier 		}
4171d204ee4SYann Gautier 
4181d204ee4SYann Gautier 		break;
4191d204ee4SYann Gautier #endif
4201d204ee4SYann Gautier 
4211d204ee4SYann Gautier #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
4221d204ee4SYann Gautier #if STM32MP_RAW_NAND
4231d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
4241d204ee4SYann Gautier #endif
4251d204ee4SYann Gautier #if STM32MP_SPI_NAND
4261d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
4271d204ee4SYann Gautier #endif
4281d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NAND_FIP_OFFSET;
4291d204ee4SYann Gautier 		break;
4301d204ee4SYann Gautier #endif
4311d204ee4SYann Gautier 
4321d204ee4SYann Gautier #if STM32MP_SPI_NOR
4331d204ee4SYann Gautier 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
4341d204ee4SYann Gautier 		image_block_spec.offset = STM32MP_NOR_FIP_OFFSET;
4351d204ee4SYann Gautier 		break;
4361d204ee4SYann Gautier #endif
4371d204ee4SYann Gautier 
4389083fa11SPatrick Delaunay #if STM32MP_UART_PROGRAMMER
4399083fa11SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
4409083fa11SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
4419083fa11SPatrick Delaunay 			stm32cubeprogrammer_uart();
4429083fa11SPatrick Delaunay 			/* FIP loaded at DWL address */
4439083fa11SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
4449083fa11SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
4459083fa11SPatrick Delaunay 		}
4469083fa11SPatrick Delaunay 		break;
4479083fa11SPatrick Delaunay #endif
448fa92fef0SPatrick Delaunay #if STM32MP_USB_PROGRAMMER
449fa92fef0SPatrick Delaunay 	case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
450fa92fef0SPatrick Delaunay 		if (image_id == FW_CONFIG_ID) {
451fa92fef0SPatrick Delaunay 			stm32cubeprogrammer_usb();
452fa92fef0SPatrick Delaunay 			/* FIP loaded at DWL address */
453fa92fef0SPatrick Delaunay 			image_block_spec.offset = DWL_BUFFER_BASE;
454fa92fef0SPatrick Delaunay 			image_block_spec.length = DWL_BUFFER_SIZE;
455fa92fef0SPatrick Delaunay 		}
456fa92fef0SPatrick Delaunay 		break;
457fa92fef0SPatrick Delaunay #endif
458fa92fef0SPatrick Delaunay 
4591d204ee4SYann Gautier 	default:
4601d204ee4SYann Gautier 		ERROR("FIP Not found\n");
4611d204ee4SYann Gautier 		panic();
4621d204ee4SYann Gautier 	}
4631d204ee4SYann Gautier 
4641d204ee4SYann Gautier 	return 0;
4651d204ee4SYann Gautier }
4661d204ee4SYann Gautier 
467c9d75b3cSYann Gautier /*
468c9d75b3cSYann Gautier  * Return an IO device handle and specification which can be used to access
469c9d75b3cSYann Gautier  * an image. Use this to enforce platform load policy.
470c9d75b3cSYann Gautier  */
471c9d75b3cSYann Gautier int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
472c9d75b3cSYann Gautier 			  uintptr_t *image_spec)
473c9d75b3cSYann Gautier {
474c9d75b3cSYann Gautier 	int rc;
475c9d75b3cSYann Gautier 	const struct plat_io_policy *policy;
476c9d75b3cSYann Gautier 
477d5a84eeaSYann Gautier 	policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id);
478c9d75b3cSYann Gautier 	rc = policy->check(policy->image_spec);
479c9d75b3cSYann Gautier 	if (rc == 0) {
480c9d75b3cSYann Gautier 		*image_spec = policy->image_spec;
481c9d75b3cSYann Gautier 		*dev_handle = *(policy->dev_handle);
482c9d75b3cSYann Gautier 	}
483c9d75b3cSYann Gautier 
484c9d75b3cSYann Gautier 	return rc;
485c9d75b3cSYann Gautier }
4868dd75531SSughosh Ganu 
4878dd75531SSughosh Ganu #if (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT
4888dd75531SSughosh Ganu /*
489f87de907SNicolas Toromanoff  * In each boot in non-trial mode, we set the BKP register to
490f87de907SNicolas Toromanoff  * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata.
491f87de907SNicolas Toromanoff  *
492f87de907SNicolas Toromanoff  * As long as the update agent didn't update the "accepted" field in metadata
493f87de907SNicolas Toromanoff  * (i.e. we are in trial mode), we select the new active_index.
494f87de907SNicolas Toromanoff  * To avoid infinite boot loop at trial boot we decrement a BKP register.
495f87de907SNicolas Toromanoff  * If this counter is 0:
496f87de907SNicolas Toromanoff  *     - an unexpected TAMPER event raised (that resets the BKP registers to 0)
497f87de907SNicolas Toromanoff  *     - a power-off occurs before the update agent was able to update the
498f87de907SNicolas Toromanoff  *       "accepted' field
499f87de907SNicolas Toromanoff  *     - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode.
500f87de907SNicolas Toromanoff  * we select the previous_active_index.
5018dd75531SSughosh Ganu  */
502f87de907SNicolas Toromanoff #define INVALID_BOOT_IDX		0xFFFFFFFF
503f87de907SNicolas Toromanoff 
5048dd75531SSughosh Ganu uint32_t plat_fwu_get_boot_idx(void)
5058dd75531SSughosh Ganu {
506f87de907SNicolas Toromanoff 	/*
507f87de907SNicolas Toromanoff 	 * Select boot index and update boot counter only once per boot
508f87de907SNicolas Toromanoff 	 * even if this function is called several times.
509f87de907SNicolas Toromanoff 	 */
510f87de907SNicolas Toromanoff 	static uint32_t boot_idx = INVALID_BOOT_IDX;
511f87de907SNicolas Toromanoff 	const struct fwu_metadata *data;
5128dd75531SSughosh Ganu 
513f87de907SNicolas Toromanoff 	data = fwu_get_metadata();
5148dd75531SSughosh Ganu 
515f87de907SNicolas Toromanoff 	if (boot_idx == INVALID_BOOT_IDX) {
516f87de907SNicolas Toromanoff 		boot_idx = data->active_index;
517f87de907SNicolas Toromanoff 		if (fwu_is_trial_run_state()) {
518f87de907SNicolas Toromanoff 			if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) {
519f87de907SNicolas Toromanoff 				WARN("Trial FWU fails %u times\n",
520f87de907SNicolas Toromanoff 				     FWU_MAX_TRIAL_REBOOT);
521f87de907SNicolas Toromanoff 				boot_idx = data->previous_active_index;
522f87de907SNicolas Toromanoff 			}
523f87de907SNicolas Toromanoff 		} else {
524f87de907SNicolas Toromanoff 			stm32_set_max_fwu_trial_boot_cnt();
525f87de907SNicolas Toromanoff 		}
526f87de907SNicolas Toromanoff 	}
527f87de907SNicolas Toromanoff 
528f87de907SNicolas Toromanoff 	return boot_idx;
5298dd75531SSughosh Ganu }
5308dd75531SSughosh Ganu 
5318dd75531SSughosh Ganu static void *stm32_get_image_spec(const uuid_t *img_type_uuid)
5328dd75531SSughosh Ganu {
5338dd75531SSughosh Ganu 	unsigned int i;
5348dd75531SSughosh Ganu 
5358dd75531SSughosh Ganu 	for (i = 0U; i < MAX_NUMBER_IDS; i++) {
5368dd75531SSughosh Ganu 		if ((guidcmp(&policies[i].img_type_guid, img_type_uuid)) == 0) {
5378dd75531SSughosh Ganu 			return (void *)policies[i].image_spec;
5388dd75531SSughosh Ganu 		}
5398dd75531SSughosh Ganu 	}
5408dd75531SSughosh Ganu 
5418dd75531SSughosh Ganu 	return NULL;
5428dd75531SSughosh Ganu }
5438dd75531SSughosh Ganu 
5448dd75531SSughosh Ganu void plat_fwu_set_images_source(const struct fwu_metadata *metadata)
5458dd75531SSughosh Ganu {
5468dd75531SSughosh Ganu 	unsigned int i;
5478dd75531SSughosh Ganu 	uint32_t boot_idx;
5488dd75531SSughosh Ganu 	const partition_entry_t *entry;
5498dd75531SSughosh Ganu 	const uuid_t *img_type_uuid, *img_uuid;
5508dd75531SSughosh Ganu 	io_block_spec_t *image_spec;
5518dd75531SSughosh Ganu 
5528dd75531SSughosh Ganu 	boot_idx = plat_fwu_get_boot_idx();
5538dd75531SSughosh Ganu 	assert(boot_idx < NR_OF_FW_BANKS);
5548dd75531SSughosh Ganu 
5558dd75531SSughosh Ganu 	for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) {
5568dd75531SSughosh Ganu 		img_type_uuid = &metadata->img_entry[i].img_type_uuid;
5578dd75531SSughosh Ganu 		image_spec = stm32_get_image_spec(img_type_uuid);
5588dd75531SSughosh Ganu 		if (image_spec == NULL) {
5598dd75531SSughosh Ganu 			ERROR("Unable to get image spec for the image in the metadata\n");
5608dd75531SSughosh Ganu 			panic();
5618dd75531SSughosh Ganu 		}
5628dd75531SSughosh Ganu 
5638dd75531SSughosh Ganu 		img_uuid =
5648dd75531SSughosh Ganu 			&metadata->img_entry[i].img_props[boot_idx].img_uuid;
5658dd75531SSughosh Ganu 
5668dd75531SSughosh Ganu 		entry = get_partition_entry_by_uuid(img_uuid);
5678dd75531SSughosh Ganu 		if (entry == NULL) {
5688dd75531SSughosh Ganu 			ERROR("Unable to find the partition with the uuid mentioned in metadata\n");
5698dd75531SSughosh Ganu 			panic();
5708dd75531SSughosh Ganu 		}
5718dd75531SSughosh Ganu 
5728dd75531SSughosh Ganu 		image_spec->offset = entry->start;
5738dd75531SSughosh Ganu 		image_spec->length = entry->length;
5748dd75531SSughosh Ganu 	}
5758dd75531SSughosh Ganu }
5760ca180f6SSughosh Ganu 
5770ca180f6SSughosh Ganu static int plat_set_image_source(unsigned int image_id,
5780ca180f6SSughosh Ganu 				 uintptr_t *handle,
5790ca180f6SSughosh Ganu 				 uintptr_t *image_spec,
5800ca180f6SSughosh Ganu 				 const char *part_name)
5810ca180f6SSughosh Ganu {
5820ca180f6SSughosh Ganu 	struct plat_io_policy *policy;
5830ca180f6SSughosh Ganu 	io_block_spec_t *spec;
5840ca180f6SSughosh Ganu 	const partition_entry_t *entry = get_partition_entry(part_name);
5850ca180f6SSughosh Ganu 
5860ca180f6SSughosh Ganu 	if (entry == NULL) {
5870ca180f6SSughosh Ganu 		ERROR("Unable to find the %s partition\n", part_name);
5880ca180f6SSughosh Ganu 		return -ENOENT;
5890ca180f6SSughosh Ganu 	}
5900ca180f6SSughosh Ganu 
5910ca180f6SSughosh Ganu 	policy = &policies[image_id];
5920ca180f6SSughosh Ganu 
5930ca180f6SSughosh Ganu 	spec = (io_block_spec_t *)policy->image_spec;
5940ca180f6SSughosh Ganu 	spec->offset = entry->start;
5950ca180f6SSughosh Ganu 	spec->length = entry->length;
5960ca180f6SSughosh Ganu 
5970ca180f6SSughosh Ganu 	*image_spec = policy->image_spec;
5980ca180f6SSughosh Ganu 	*handle = *policy->dev_handle;
5990ca180f6SSughosh Ganu 
6000ca180f6SSughosh Ganu 	return 0;
6010ca180f6SSughosh Ganu }
6020ca180f6SSughosh Ganu 
6030ca180f6SSughosh Ganu int plat_fwu_set_metadata_image_source(unsigned int image_id,
6040ca180f6SSughosh Ganu 				       uintptr_t *handle,
6050ca180f6SSughosh Ganu 				       uintptr_t *image_spec)
6060ca180f6SSughosh Ganu {
6070ca180f6SSughosh Ganu 	char *part_name;
6080ca180f6SSughosh Ganu 
6090ca180f6SSughosh Ganu 	assert((image_id == FWU_METADATA_IMAGE_ID) ||
6100ca180f6SSughosh Ganu 	       (image_id == BKUP_FWU_METADATA_IMAGE_ID));
6110ca180f6SSughosh Ganu 
6120ca180f6SSughosh Ganu 	partition_init(GPT_IMAGE_ID);
6130ca180f6SSughosh Ganu 
6140ca180f6SSughosh Ganu 	if (image_id == FWU_METADATA_IMAGE_ID) {
6150ca180f6SSughosh Ganu 		part_name = METADATA_PART_1;
6160ca180f6SSughosh Ganu 	} else {
6170ca180f6SSughosh Ganu 		part_name = METADATA_PART_2;
6180ca180f6SSughosh Ganu 	}
6190ca180f6SSughosh Ganu 
6200ca180f6SSughosh Ganu 	return plat_set_image_source(image_id, handle, image_spec,
6210ca180f6SSughosh Ganu 				     part_name);
6220ca180f6SSughosh Ganu }
6238dd75531SSughosh Ganu #endif /* (STM32MP_SDMMC || STM32MP_EMMC) && PSA_FWU_SUPPORT */
624