blob: 3ec7d4048a3b7aacb87c7b1a8255f191cab3660b [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautierf9d40d52019-01-17 14:41:46 +01002 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
Yann Gautier4b0c72a2018-07-16 10:54:09 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier4b0c72a2018-07-16 10:54:09 +02007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
Yann Gautier4b0c72a2018-07-16 10:54:09 +020010#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <arch_helpers.h>
13#include <common/debug.h>
14#include <drivers/io/io_block.h>
15#include <drivers/io/io_driver.h>
16#include <drivers/io/io_dummy.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010017#include <drivers/io/io_mtd.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <drivers/io/io_storage.h>
19#include <drivers/mmc.h>
20#include <drivers/partition/partition.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010021#include <drivers/raw_nand.h>
Lionel Debieve186b0462019-09-24 18:30:12 +020022#include <drivers/spi_nand.h>
Lionel Debievecb0dbc42019-09-25 09:11:31 +020023#include <drivers/spi_nor.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <drivers/st/io_mmc.h>
25#include <drivers/st/io_stm32image.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010026#include <drivers/st/stm32_fmc2_nand.h>
Lionel Debieve186b0462019-09-24 18:30:12 +020027#include <drivers/st/stm32_qspi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028#include <drivers/st/stm32_sdmmc2.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000029#include <lib/mmio.h>
30#include <lib/utils.h>
31#include <plat/common/platform.h>
32
Yann Gautier4b0c72a2018-07-16 10:54:09 +020033/* IO devices */
34static const io_dev_connector_t *dummy_dev_con;
35static uintptr_t dummy_dev_handle;
36static uintptr_t dummy_dev_spec;
37
Yann Gautier8244e1d2018-10-15 09:36:58 +020038static uintptr_t image_dev_handle;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020039static uintptr_t storage_dev_handle;
Yann Gautier8244e1d2018-10-15 09:36:58 +020040
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020041#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautier8244e1d2018-10-15 09:36:58 +020042static io_block_spec_t gpt_block_spec = {
43 .offset = 0,
44 .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */
45};
46
Yann Gautierf9af3bc2018-11-09 15:57:18 +010047static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
Yann Gautier8244e1d2018-10-15 09:36:58 +020048
49static const io_block_dev_spec_t mmc_block_dev_spec = {
50 /* It's used as temp buffer in block driver */
51 .buffer = {
52 .offset = (size_t)&block_buffer,
53 .length = MMC_BLOCK_SIZE,
54 },
55 .ops = {
56 .read = mmc_read_blocks,
57 .write = NULL,
58 },
59 .block_size = MMC_BLOCK_SIZE,
60};
61
Yann Gautier8244e1d2018-10-15 09:36:58 +020062static const io_dev_connector_t *mmc_dev_con;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020063#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +020064
Lionel Debievecb0dbc42019-09-25 09:11:31 +020065#if STM32MP_SPI_NOR
66static io_mtd_dev_spec_t spi_nor_dev_spec = {
67 .ops = {
68 .init = spi_nor_init,
69 .read = spi_nor_read,
70 },
71};
72#endif
73
Lionel Debieve402a46b2019-11-04 12:28:15 +010074#if STM32MP_RAW_NAND
75static io_mtd_dev_spec_t nand_dev_spec = {
76 .ops = {
77 .init = nand_raw_init,
78 .read = nand_read,
79 },
80};
81
82static const io_dev_connector_t *nand_dev_con;
83#endif
84
Lionel Debieve186b0462019-09-24 18:30:12 +020085#if STM32MP_SPI_NAND
86static io_mtd_dev_spec_t spi_nand_dev_spec = {
87 .ops = {
88 .init = spi_nand_init,
89 .read = nand_read,
90 },
91};
Lionel Debievecb0dbc42019-09-25 09:11:31 +020092#endif
Lionel Debieve186b0462019-09-24 18:30:12 +020093
Lionel Debievecb0dbc42019-09-25 09:11:31 +020094#if STM32MP_SPI_NAND || STM32MP_SPI_NOR
Lionel Debieve186b0462019-09-24 18:30:12 +020095static const io_dev_connector_t *spi_dev_con;
96#endif
97
Yann Gautierb3386f72019-04-19 09:41:01 +020098#ifdef AARCH32_SP_OPTEE
99static const struct stm32image_part_info optee_header_partition_spec = {
100 .name = OPTEE_HEADER_IMAGE_NAME,
101 .binary_type = OPTEE_HEADER_BINARY_TYPE,
102};
103
104static const struct stm32image_part_info optee_pager_partition_spec = {
105 .name = OPTEE_PAGER_IMAGE_NAME,
106 .binary_type = OPTEE_PAGER_BINARY_TYPE,
107};
108
109static const struct stm32image_part_info optee_paged_partition_spec = {
110 .name = OPTEE_PAGED_IMAGE_NAME,
111 .binary_type = OPTEE_PAGED_BINARY_TYPE,
112};
113#else
Yann Gautierf9d40d52019-01-17 14:41:46 +0100114static const io_block_spec_t bl32_block_spec = {
115 .offset = BL32_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +0100116 .length = STM32MP_BL32_SIZE
Yann Gautierf9d40d52019-01-17 14:41:46 +0100117};
Yann Gautierb3386f72019-04-19 09:41:01 +0200118#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +0100119
120static const io_block_spec_t bl2_block_spec = {
121 .offset = BL2_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +0100122 .length = STM32MP_BL2_SIZE,
Yann Gautierf9d40d52019-01-17 14:41:46 +0100123};
Yann Gautier8244e1d2018-10-15 09:36:58 +0200124
125static const struct stm32image_part_info bl33_partition_spec = {
126 .name = BL33_IMAGE_NAME,
127 .binary_type = BL33_BINARY_TYPE,
128};
129
Yann Gautierf9d40d52019-01-17 14:41:46 +0100130enum {
131 IMG_IDX_BL33,
Yann Gautierb3386f72019-04-19 09:41:01 +0200132#ifdef AARCH32_SP_OPTEE
133 IMG_IDX_OPTEE_HEADER,
134 IMG_IDX_OPTEE_PAGER,
135 IMG_IDX_OPTEE_PAGED,
136#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +0100137 IMG_IDX_NUM
138};
139
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200140static struct stm32image_device_info stm32image_dev_info_spec __unused = {
Yann Gautier8244e1d2018-10-15 09:36:58 +0200141 .lba_size = MMC_BLOCK_SIZE,
142 .part_info[IMG_IDX_BL33] = {
143 .name = BL33_IMAGE_NAME,
144 .binary_type = BL33_BINARY_TYPE,
145 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200146#ifdef AARCH32_SP_OPTEE
147 .part_info[IMG_IDX_OPTEE_HEADER] = {
148 .name = OPTEE_HEADER_IMAGE_NAME,
149 .binary_type = OPTEE_HEADER_BINARY_TYPE,
150 },
151 .part_info[IMG_IDX_OPTEE_PAGER] = {
152 .name = OPTEE_PAGER_IMAGE_NAME,
153 .binary_type = OPTEE_PAGER_BINARY_TYPE,
154 },
155 .part_info[IMG_IDX_OPTEE_PAGED] = {
156 .name = OPTEE_PAGED_IMAGE_NAME,
157 .binary_type = OPTEE_PAGED_BINARY_TYPE,
158 },
159#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200160};
161
Yann Gautierf9d40d52019-01-17 14:41:46 +0100162static io_block_spec_t stm32image_block_spec = {
163 .offset = 0,
164 .length = 0,
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200165};
166
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200167static const io_dev_connector_t *stm32image_dev_con __unused;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200168
169static int open_dummy(const uintptr_t spec);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200170static int open_image(const uintptr_t spec);
171static int open_storage(const uintptr_t spec);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200172
173struct plat_io_policy {
174 uintptr_t *dev_handle;
175 uintptr_t image_spec;
176 int (*check)(const uintptr_t spec);
177};
178
179static const struct plat_io_policy policies[] = {
180 [BL2_IMAGE_ID] = {
181 .dev_handle = &dummy_dev_handle,
182 .image_spec = (uintptr_t)&bl2_block_spec,
183 .check = open_dummy
184 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200185#ifdef AARCH32_SP_OPTEE
186 [BL32_IMAGE_ID] = {
187 .dev_handle = &image_dev_handle,
188 .image_spec = (uintptr_t)&optee_header_partition_spec,
189 .check = open_image
190 },
191 [BL32_EXTRA1_IMAGE_ID] = {
192 .dev_handle = &image_dev_handle,
193 .image_spec = (uintptr_t)&optee_pager_partition_spec,
194 .check = open_image
195 },
196 [BL32_EXTRA2_IMAGE_ID] = {
197 .dev_handle = &image_dev_handle,
198 .image_spec = (uintptr_t)&optee_paged_partition_spec,
199 .check = open_image
200 },
201#else
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200202 [BL32_IMAGE_ID] = {
203 .dev_handle = &dummy_dev_handle,
204 .image_spec = (uintptr_t)&bl32_block_spec,
205 .check = open_dummy
206 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200207#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200208 [BL33_IMAGE_ID] = {
209 .dev_handle = &image_dev_handle,
210 .image_spec = (uintptr_t)&bl33_partition_spec,
211 .check = open_image
212 },
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200213#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautier8244e1d2018-10-15 09:36:58 +0200214 [GPT_IMAGE_ID] = {
215 .dev_handle = &storage_dev_handle,
216 .image_spec = (uintptr_t)&gpt_block_spec,
217 .check = open_storage
218 },
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200219#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200220 [STM32_IMAGE_ID] = {
221 .dev_handle = &storage_dev_handle,
222 .image_spec = (uintptr_t)&stm32image_block_spec,
223 .check = open_storage
224 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200225};
226
227static int open_dummy(const uintptr_t spec)
228{
229 return io_dev_init(dummy_dev_handle, 0);
230}
231
Yann Gautier8244e1d2018-10-15 09:36:58 +0200232static int open_image(const uintptr_t spec)
233{
234 return io_dev_init(image_dev_handle, 0);
235}
236
237static int open_storage(const uintptr_t spec)
238{
239 return io_dev_init(storage_dev_handle, 0);
240}
241
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200242static void print_boot_device(boot_api_context_t *boot_context)
243{
244 switch (boot_context->boot_interface_selected) {
245 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
246 INFO("Using SDMMC\n");
247 break;
248 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
249 INFO("Using EMMC\n");
250 break;
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200251 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
252 INFO("Using QSPI NOR\n");
253 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100254 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
255 INFO("Using FMC NAND\n");
256 break;
Lionel Debieve186b0462019-09-24 18:30:12 +0200257 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
258 INFO("Using SPI NAND\n");
259 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200260 default:
261 ERROR("Boot interface not found\n");
262 panic();
263 break;
264 }
265
266 if (boot_context->boot_interface_instance != 0U) {
267 INFO(" Instance %d\n", boot_context->boot_interface_instance);
268 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200269}
270
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200271#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200272static void boot_mmc(enum mmc_device_type mmc_dev_type,
273 uint16_t boot_interface_instance)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200274{
275 int io_result __unused;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100276 uint8_t idx;
277 struct stm32image_part_info *part;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200278 struct stm32_sdmmc2_params params;
279 struct mmc_device_info device_info;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100280 const partition_entry_t *entry;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200281
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200282 zeromem(&device_info, sizeof(struct mmc_device_info));
283 zeromem(&params, sizeof(struct stm32_sdmmc2_params));
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200284
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200285 device_info.mmc_dev_type = mmc_dev_type;
286
287 switch (boot_interface_instance) {
288 case 1:
289 params.reg_base = STM32MP_SDMMC1_BASE;
290 break;
291 case 2:
292 params.reg_base = STM32MP_SDMMC2_BASE;
293 break;
294 case 3:
295 params.reg_base = STM32MP_SDMMC3_BASE;
296 break;
297 default:
298 WARN("SDMMC instance not found, using default\n");
299 if (mmc_dev_type == MMC_IS_SD) {
300 params.reg_base = STM32MP_SDMMC1_BASE;
301 } else {
302 params.reg_base = STM32MP_SDMMC2_BASE;
303 }
304 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200305 }
306
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200307 params.device_info = &device_info;
308 if (stm32_sdmmc2_mmc_init(&params) != 0) {
309 ERROR("SDMMC%u init failed\n", boot_interface_instance);
310 panic();
311 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200312
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200313 /* Open MMC as a block device to read GPT table */
314 io_result = register_io_dev_block(&mmc_dev_con);
315 if (io_result != 0) {
316 panic();
317 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200318
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200319 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
320 &storage_dev_handle);
321 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200322
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200323 partition_init(GPT_IMAGE_ID);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200324
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200325 io_result = io_dev_close(storage_dev_handle);
326 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200327
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200328 stm32image_dev_info_spec.device_size =
329 stm32_sdmmc2_mmc_get_device_size();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200330
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200331 for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
332 part = &stm32image_dev_info_spec.part_info[idx];
333 entry = get_partition_entry(part->name);
334 if (entry == NULL) {
335 ERROR("Partition %s not found\n", part->name);
Yann Gautier03f04682018-11-29 15:44:04 +0100336 panic();
337 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200338
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200339 part->part_offset = entry->start;
340 part->bkp_offset = 0U;
341 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200342
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200343 /*
344 * Re-open MMC with io_mmc, for better perfs compared to
345 * io_block.
346 */
347 io_result = register_io_dev_mmc(&mmc_dev_con);
348 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200349
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200350 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
351 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200352
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200353 io_result = register_io_dev_stm32image(&stm32image_dev_con);
354 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200355
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200356 io_result = io_dev_open(stm32image_dev_con,
357 (uintptr_t)&stm32image_dev_info_spec,
358 &image_dev_handle);
359 assert(io_result == 0);
360}
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200361#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +0200362
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200363#if STM32MP_SPI_NOR
364static void boot_spi_nor(boot_api_context_t *boot_context)
365{
366 int io_result __unused;
367 uint8_t idx;
368 struct stm32image_part_info *part;
369
370 io_result = stm32_qspi_init();
371 assert(io_result == 0);
372
373 io_result = register_io_dev_mtd(&spi_dev_con);
374 assert(io_result == 0);
375
376 /* Open connections to device */
377 io_result = io_dev_open(spi_dev_con,
378 (uintptr_t)&spi_nor_dev_spec,
379 &storage_dev_handle);
380 assert(io_result == 0);
381
382 stm32image_dev_info_spec.device_size = spi_nor_dev_spec.device_size;
383
384 idx = IMG_IDX_BL33;
385 part = &stm32image_dev_info_spec.part_info[idx];
386 part->part_offset = STM32MP_NOR_BL33_OFFSET;
387 part->bkp_offset = 0U;
388
389#ifdef AARCH32_SP_OPTEE
390 idx = IMG_IDX_OPTEE_HEADER;
391 part = &stm32image_dev_info_spec.part_info[idx];
392 part->part_offset = STM32MP_NOR_TEEH_OFFSET;
393 part->bkp_offset = 0U;
394
395 idx = IMG_IDX_OPTEE_PAGED;
396 part = &stm32image_dev_info_spec.part_info[idx];
397 part->part_offset = STM32MP_NOR_TEED_OFFSET;
398 part->bkp_offset = 0U;
399
400 idx = IMG_IDX_OPTEE_PAGER;
401 part = &stm32image_dev_info_spec.part_info[idx];
402 part->part_offset = STM32MP_NOR_TEEX_OFFSET;
403 part->bkp_offset = 0U;
404#endif
405
406 io_result = register_io_dev_stm32image(&stm32image_dev_con);
407 assert(io_result == 0);
408
409 io_result = io_dev_open(stm32image_dev_con,
410 (uintptr_t)&stm32image_dev_info_spec,
411 &image_dev_handle);
412 assert(io_result == 0);
413}
414#endif /* STM32MP_SPI_NOR */
415
Lionel Debieve402a46b2019-11-04 12:28:15 +0100416#if STM32MP_RAW_NAND
417static void boot_fmc2_nand(boot_api_context_t *boot_context)
418{
419 int io_result __unused;
420 uint8_t idx;
421 struct stm32image_part_info *part;
422
423 io_result = stm32_fmc2_init();
424 assert(io_result == 0);
425
426 /* Register the IO device on this platform */
427 io_result = register_io_dev_mtd(&nand_dev_con);
428 assert(io_result == 0);
429
430 /* Open connections to device */
431 io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
432 &storage_dev_handle);
433 assert(io_result == 0);
434
435 stm32image_dev_info_spec.device_size = nand_dev_spec.device_size;
436
437 idx = IMG_IDX_BL33;
438 part = &stm32image_dev_info_spec.part_info[idx];
439 part->part_offset = STM32MP_NAND_BL33_OFFSET;
440 part->bkp_offset = nand_dev_spec.erase_size;
441
442#ifdef AARCH32_SP_OPTEE
443 idx = IMG_IDX_OPTEE_HEADER;
444 part = &stm32image_dev_info_spec.part_info[idx];
445 part->part_offset = STM32MP_NAND_TEEH_OFFSET;
446 part->bkp_offset = nand_dev_spec.erase_size;
447
448 idx = IMG_IDX_OPTEE_PAGED;
449 part = &stm32image_dev_info_spec.part_info[idx];
450 part->part_offset = STM32MP_NAND_TEED_OFFSET;
451 part->bkp_offset = nand_dev_spec.erase_size;
452
453 idx = IMG_IDX_OPTEE_PAGER;
454 part = &stm32image_dev_info_spec.part_info[idx];
455 part->part_offset = STM32MP_NAND_TEEX_OFFSET;
456 part->bkp_offset = nand_dev_spec.erase_size;
457#endif
458
459 io_result = register_io_dev_stm32image(&stm32image_dev_con);
460 assert(io_result == 0);
461
462 io_result = io_dev_open(stm32image_dev_con,
463 (uintptr_t)&stm32image_dev_info_spec,
464 &image_dev_handle);
465 assert(io_result == 0);
466}
467#endif /* STM32MP_RAW_NAND */
468
Lionel Debieve186b0462019-09-24 18:30:12 +0200469#if STM32MP_SPI_NAND
470static void boot_spi_nand(boot_api_context_t *boot_context)
471{
472 int io_result __unused;
473 uint8_t idx;
474 struct stm32image_part_info *part;
475
476 io_result = stm32_qspi_init();
477 assert(io_result == 0);
478
479 io_result = register_io_dev_mtd(&spi_dev_con);
480 assert(io_result == 0);
481
482 /* Open connections to device */
483 io_result = io_dev_open(spi_dev_con,
484 (uintptr_t)&spi_nand_dev_spec,
485 &storage_dev_handle);
486 assert(io_result == 0);
487
488 stm32image_dev_info_spec.device_size =
489 spi_nand_dev_spec.device_size;
490
491 idx = IMG_IDX_BL33;
492 part = &stm32image_dev_info_spec.part_info[idx];
493 part->part_offset = STM32MP_NAND_BL33_OFFSET;
494 part->bkp_offset = spi_nand_dev_spec.erase_size;
495
496#ifdef AARCH32_SP_OPTEE
497 idx = IMG_IDX_OPTEE_HEADER;
498 part = &stm32image_dev_info_spec.part_info[idx];
499 part->part_offset = STM32MP_NAND_TEEH_OFFSET;
500 part->bkp_offset = spi_nand_dev_spec.erase_size;
501
502 idx = IMG_IDX_OPTEE_PAGED;
503 part = &stm32image_dev_info_spec.part_info[idx];
504 part->part_offset = STM32MP_NAND_TEED_OFFSET;
505 part->bkp_offset = spi_nand_dev_spec.erase_size;
506
507 idx = IMG_IDX_OPTEE_PAGER;
508 part = &stm32image_dev_info_spec.part_info[idx];
509 part->part_offset = STM32MP_NAND_TEEX_OFFSET;
510 part->bkp_offset = spi_nand_dev_spec.erase_size;
511#endif
512
513 io_result = register_io_dev_stm32image(&stm32image_dev_con);
514 assert(io_result == 0);
515
516 io_result = io_dev_open(stm32image_dev_con,
517 (uintptr_t)&stm32image_dev_info_spec,
518 &image_dev_handle);
519 assert(io_result == 0);
520}
521#endif /* STM32MP_SPI_NAND */
522
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200523void stm32mp_io_setup(void)
524{
525 int io_result __unused;
526 boot_api_context_t *boot_context =
527 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautierf9d40d52019-01-17 14:41:46 +0100528
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200529 print_boot_device(boot_context);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200530
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200531 if ((boot_context->boot_partition_used_toboot == 1U) ||
532 (boot_context->boot_partition_used_toboot == 2U)) {
533 INFO("Boot used partition fsbl%d\n",
534 boot_context->boot_partition_used_toboot);
535 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200536
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200537 io_result = register_io_dev_dummy(&dummy_dev_con);
538 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200539
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200540 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
541 &dummy_dev_handle);
542 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200543
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200544 switch (boot_context->boot_interface_selected) {
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200545#if STM32MP_SDMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200546 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
547 dmbsy();
548 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
549 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200550#endif
551#if STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200552 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
553 dmbsy();
554 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200555 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200556#endif
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200557#if STM32MP_SPI_NOR
558 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
559 dmbsy();
560 boot_spi_nor(boot_context);
561 break;
562#endif
Lionel Debieve402a46b2019-11-04 12:28:15 +0100563#if STM32MP_RAW_NAND
564 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
565 dmbsy();
566 boot_fmc2_nand(boot_context);
567 break;
568#endif
Lionel Debieve186b0462019-09-24 18:30:12 +0200569#if STM32MP_SPI_NAND
570 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
571 dmbsy();
572 boot_spi_nand(boot_context);
573 break;
574#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200575
576 default:
577 ERROR("Boot interface %d not supported\n",
578 boot_context->boot_interface_selected);
579 break;
580 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200581}
582
583/*
584 * Return an IO device handle and specification which can be used to access
585 * an image. Use this to enforce platform load policy.
586 */
587int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
588 uintptr_t *image_spec)
589{
590 int rc;
591 const struct plat_io_policy *policy;
592
593 assert(image_id < ARRAY_SIZE(policies));
594
595 policy = &policies[image_id];
596 rc = policy->check(policy->image_spec);
597 if (rc == 0) {
598 *image_spec = policy->image_spec;
599 *dev_handle = *(policy->dev_handle);
600 }
601
602 return rc;
603}