blob: 43911955ad5886639cca8ea25a8db3e905f067a7 [file] [log] [blame]
Yann Gautier0ed7b2a2021-05-19 18:48:16 +02001/*
2 * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <string.h>
9
10#include <arch_helpers.h>
11#include <common/debug.h>
12#include <drivers/io/io_block.h>
13#include <drivers/io/io_driver.h>
14#include <drivers/io/io_dummy.h>
15#include <drivers/io/io_mtd.h>
16#include <drivers/io/io_storage.h>
17#include <drivers/mmc.h>
18#include <drivers/partition/partition.h>
19#include <drivers/raw_nand.h>
20#include <drivers/spi_nand.h>
21#include <drivers/spi_nor.h>
22#include <drivers/st/io_mmc.h>
23#include <drivers/st/io_stm32image.h>
24#include <drivers/st/stm32_fmc2_nand.h>
25#include <drivers/st/stm32_qspi.h>
26#include <drivers/st/stm32_sdmmc2.h>
27#include <lib/mmio.h>
28#include <lib/utils.h>
29#include <plat/common/platform.h>
30
31#include <platform_def.h>
32
33/* IO devices */
34#ifndef AARCH32_SP_OPTEE
35static const io_dev_connector_t *dummy_dev_con;
36static uintptr_t dummy_dev_handle;
37static uintptr_t dummy_dev_spec;
38#endif
39
40static uintptr_t image_dev_handle;
41static uintptr_t storage_dev_handle;
42
43#if STM32MP_SDMMC || STM32MP_EMMC
44static struct mmc_device_info mmc_info;
45static io_block_spec_t gpt_block_spec = {
46 .offset = 0U,
47 .length = 34U * MMC_BLOCK_SIZE, /* Size of GPT table */
48};
49
50static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
51
52static const io_block_dev_spec_t mmc_block_dev_spec = {
53 /* It's used as temp buffer in block driver */
54 .buffer = {
55 .offset = (size_t)&block_buffer,
56 .length = MMC_BLOCK_SIZE,
57 },
58 .ops = {
59 .read = mmc_read_blocks,
60 .write = NULL,
61 },
62 .block_size = MMC_BLOCK_SIZE,
63};
64
65#if STM32MP_EMMC_BOOT
66static io_block_spec_t emmc_boot_ssbl_block_spec = {
67 .offset = PLAT_EMMC_BOOT_SSBL_OFFSET,
68 .length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */
69};
70
71static const io_block_dev_spec_t mmc_block_dev_boot_part_spec = {
72 /* It's used as temp buffer in block driver */
73 .buffer = {
74 .offset = (size_t)&block_buffer,
75 .length = MMC_BLOCK_SIZE,
76 },
77 .ops = {
78 .read = mmc_boot_part_read_blocks,
79 .write = NULL,
80 },
81 .block_size = MMC_BLOCK_SIZE,
82};
83#endif
84
85static struct io_mmc_dev_spec mmc_device_spec = {
86 .use_boot_part = false,
87};
88
89static const io_dev_connector_t *mmc_dev_con;
90#endif /* STM32MP_SDMMC || STM32MP_EMMC */
91
92#if STM32MP_SPI_NOR
93static io_mtd_dev_spec_t spi_nor_dev_spec = {
94 .ops = {
95 .init = spi_nor_init,
96 .read = spi_nor_read,
97 },
98};
99#endif
100
101#if STM32MP_RAW_NAND
102static io_mtd_dev_spec_t nand_dev_spec = {
103 .ops = {
104 .init = nand_raw_init,
105 .read = nand_read,
106 },
107};
108
109static const io_dev_connector_t *nand_dev_con;
110#endif
111
112#if STM32MP_SPI_NAND
113static io_mtd_dev_spec_t spi_nand_dev_spec = {
114 .ops = {
115 .init = spi_nand_init,
116 .read = nand_read,
117 },
118};
119#endif
120
121#if STM32MP_SPI_NAND || STM32MP_SPI_NOR
122static const io_dev_connector_t *spi_dev_con;
123#endif
124
125#ifdef AARCH32_SP_OPTEE
126static const struct stm32image_part_info optee_header_partition_spec = {
127 .name = OPTEE_HEADER_IMAGE_NAME,
128 .binary_type = OPTEE_HEADER_BINARY_TYPE,
129};
130
131static const struct stm32image_part_info optee_core_partition_spec = {
132 .name = OPTEE_CORE_IMAGE_NAME,
133 .binary_type = OPTEE_CORE_BINARY_TYPE,
134};
135
136static const struct stm32image_part_info optee_paged_partition_spec = {
137 .name = OPTEE_PAGED_IMAGE_NAME,
138 .binary_type = OPTEE_PAGED_BINARY_TYPE,
139};
140#else
141static const io_block_spec_t bl32_block_spec = {
142 .offset = BL32_BASE,
143 .length = STM32MP_BL32_SIZE
144};
145#endif
146
147static const struct stm32image_part_info bl33_partition_spec = {
148 .name = BL33_IMAGE_NAME,
149 .binary_type = BL33_BINARY_TYPE,
150};
151
152enum {
153 IMG_IDX_BL33,
154#ifdef AARCH32_SP_OPTEE
155 IMG_IDX_OPTEE_HEADER,
156 IMG_IDX_OPTEE_CORE,
157 IMG_IDX_OPTEE_PAGED,
158#endif
159 IMG_IDX_NUM
160};
161
162static struct stm32image_device_info stm32image_dev_info_spec __unused = {
163 .lba_size = MMC_BLOCK_SIZE,
164 .part_info[IMG_IDX_BL33] = {
165 .name = BL33_IMAGE_NAME,
166 .binary_type = BL33_BINARY_TYPE,
167 },
168#ifdef AARCH32_SP_OPTEE
169 .part_info[IMG_IDX_OPTEE_HEADER] = {
170 .name = OPTEE_HEADER_IMAGE_NAME,
171 .binary_type = OPTEE_HEADER_BINARY_TYPE,
172 },
173 .part_info[IMG_IDX_OPTEE_CORE] = {
174 .name = OPTEE_CORE_IMAGE_NAME,
175 .binary_type = OPTEE_CORE_BINARY_TYPE,
176 },
177 .part_info[IMG_IDX_OPTEE_PAGED] = {
178 .name = OPTEE_PAGED_IMAGE_NAME,
179 .binary_type = OPTEE_PAGED_BINARY_TYPE,
180 },
181#endif
182};
183
184static io_block_spec_t stm32image_block_spec = {
185 .offset = 0U,
186 .length = 0U,
187};
188
189static const io_dev_connector_t *stm32image_dev_con __unused;
190
191#ifndef AARCH32_SP_OPTEE
192static int open_dummy(const uintptr_t spec);
193#endif
194static int open_image(const uintptr_t spec);
195static int open_storage(const uintptr_t spec);
196
197struct plat_io_policy {
198 uintptr_t *dev_handle;
199 uintptr_t image_spec;
200 int (*check)(const uintptr_t spec);
201};
202
203static const struct plat_io_policy policies[] = {
204#ifdef AARCH32_SP_OPTEE
205 [BL32_IMAGE_ID] = {
206 .dev_handle = &image_dev_handle,
207 .image_spec = (uintptr_t)&optee_header_partition_spec,
208 .check = open_image
209 },
210 [BL32_EXTRA1_IMAGE_ID] = {
211 .dev_handle = &image_dev_handle,
212 .image_spec = (uintptr_t)&optee_core_partition_spec,
213 .check = open_image
214 },
215 [BL32_EXTRA2_IMAGE_ID] = {
216 .dev_handle = &image_dev_handle,
217 .image_spec = (uintptr_t)&optee_paged_partition_spec,
218 .check = open_image
219 },
220#else
221 [BL32_IMAGE_ID] = {
222 .dev_handle = &dummy_dev_handle,
223 .image_spec = (uintptr_t)&bl32_block_spec,
224 .check = open_dummy
225 },
226#endif
227 [BL33_IMAGE_ID] = {
228 .dev_handle = &image_dev_handle,
229 .image_spec = (uintptr_t)&bl33_partition_spec,
230 .check = open_image
231 },
232#if STM32MP_SDMMC || STM32MP_EMMC
233 [GPT_IMAGE_ID] = {
234 .dev_handle = &storage_dev_handle,
235 .image_spec = (uintptr_t)&gpt_block_spec,
236 .check = open_storage
237 },
238#endif
239 [STM32_IMAGE_ID] = {
240 .dev_handle = &storage_dev_handle,
241 .image_spec = (uintptr_t)&stm32image_block_spec,
242 .check = open_storage
243 }
244};
245
246#ifndef AARCH32_SP_OPTEE
247static int open_dummy(const uintptr_t spec)
248{
249 return io_dev_init(dummy_dev_handle, 0);
250}
251#endif
252
253static int open_image(const uintptr_t spec)
254{
255 return io_dev_init(image_dev_handle, 0);
256}
257
258static int open_storage(const uintptr_t spec)
259{
260 return io_dev_init(storage_dev_handle, 0);
261}
262
263#if STM32MP_EMMC_BOOT
264static uint32_t get_boot_part_ssbl_header(void)
265{
266 uint32_t magic = 0U;
267 int io_result;
268 size_t bytes_read;
269
270 io_result = register_io_dev_block(&mmc_dev_con);
271 if (io_result != 0) {
272 panic();
273 }
274
275 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_boot_part_spec,
276 &storage_dev_handle);
277 assert(io_result == 0);
278
279 io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_ssbl_block_spec,
280 &image_dev_handle);
281 assert(io_result == 0);
282
283 io_result = io_read(image_dev_handle, (uintptr_t)&magic, sizeof(magic),
284 &bytes_read);
285 assert(io_result == 0);
286 assert(bytes_read == sizeof(magic));
287
288 io_result = io_dev_close(storage_dev_handle);
289 assert(io_result == 0);
290
291 return magic;
292}
293#endif
294
295static void print_boot_device(boot_api_context_t *boot_context)
296{
297 switch (boot_context->boot_interface_selected) {
298 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
299 INFO("Using SDMMC\n");
300 break;
301 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
302 INFO("Using EMMC\n");
303 break;
304 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
305 INFO("Using QSPI NOR\n");
306 break;
307 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
308 INFO("Using FMC NAND\n");
309 break;
310 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
311 INFO("Using SPI NAND\n");
312 break;
313 default:
314 ERROR("Boot interface not found\n");
315 panic();
316 break;
317 }
318
319 if (boot_context->boot_interface_instance != 0U) {
320 INFO(" Instance %d\n", boot_context->boot_interface_instance);
321 }
322}
323
324static void stm32image_io_setup(void)
325{
326 int io_result __unused;
327
328 io_result = register_io_dev_stm32image(&stm32image_dev_con);
329 assert(io_result == 0);
330
331 io_result = io_dev_open(stm32image_dev_con,
332 (uintptr_t)&stm32image_dev_info_spec,
333 &image_dev_handle);
334 assert(io_result == 0);
335}
336
337#if STM32MP_SDMMC || STM32MP_EMMC
338static void boot_mmc(enum mmc_device_type mmc_dev_type,
339 uint16_t boot_interface_instance)
340{
341 int io_result __unused;
342 uint8_t idx;
343 struct stm32image_part_info *part;
344 struct stm32_sdmmc2_params params;
345 const partition_entry_t *entry __unused;
346 uint32_t magic __unused;
347
348 zeromem(&params, sizeof(struct stm32_sdmmc2_params));
349
350 mmc_info.mmc_dev_type = mmc_dev_type;
351
352 switch (boot_interface_instance) {
353 case 1:
354 params.reg_base = STM32MP_SDMMC1_BASE;
355 break;
356 case 2:
357 params.reg_base = STM32MP_SDMMC2_BASE;
358 break;
359 case 3:
360 params.reg_base = STM32MP_SDMMC3_BASE;
361 break;
362 default:
363 WARN("SDMMC instance not found, using default\n");
364 if (mmc_dev_type == MMC_IS_SD) {
365 params.reg_base = STM32MP_SDMMC1_BASE;
366 } else {
367 params.reg_base = STM32MP_SDMMC2_BASE;
368 }
369 break;
370 }
371
372 params.device_info = &mmc_info;
373 if (stm32_sdmmc2_mmc_init(&params) != 0) {
374 ERROR("SDMMC%u init failed\n", boot_interface_instance);
375 panic();
376 }
377
378 stm32image_dev_info_spec.device_size =
379 stm32_sdmmc2_mmc_get_device_size();
380
381#if STM32MP_EMMC_BOOT
Uwe Kleine-Könige8b31fa2022-03-10 22:21:55 +0100382 if (mmc_dev_type == MMC_IS_EMMC) {
383 magic = get_boot_part_ssbl_header();
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200384
Uwe Kleine-Könige8b31fa2022-03-10 22:21:55 +0100385 if (magic == BOOT_API_IMAGE_HEADER_MAGIC_NB) {
386 VERBOSE("%s, header found, jump to emmc load\n", __func__);
387 idx = IMG_IDX_BL33;
388 part = &stm32image_dev_info_spec.part_info[idx];
389 part->part_offset = PLAT_EMMC_BOOT_SSBL_OFFSET;
390 part->bkp_offset = 0U;
391 mmc_device_spec.use_boot_part = true;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200392
Uwe Kleine-Könige8b31fa2022-03-10 22:21:55 +0100393 goto emmc_boot;
394 } else {
395 WARN("%s: Can't find STM32 header on a boot partition\n", __func__);
396 }
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200397 }
398#endif
399
400 /* Open MMC as a block device to read GPT table */
401 io_result = register_io_dev_block(&mmc_dev_con);
402 if (io_result != 0) {
403 panic();
404 }
405
406 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
407 &storage_dev_handle);
408 assert(io_result == 0);
409
410 partition_init(GPT_IMAGE_ID);
411
412 io_result = io_dev_close(storage_dev_handle);
413 assert(io_result == 0);
414
415 for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
416 part = &stm32image_dev_info_spec.part_info[idx];
417 entry = get_partition_entry(part->name);
418 if (entry == NULL) {
419 ERROR("Partition %s not found\n", part->name);
420 panic();
421 }
422
423 part->part_offset = entry->start;
424 part->bkp_offset = 0U;
425 }
426
427#if STM32MP_EMMC_BOOT
428emmc_boot:
429#endif
430 /*
431 * Re-open MMC with io_mmc, for better perfs compared to
432 * io_block.
433 */
434 io_result = register_io_dev_mmc(&mmc_dev_con);
435 assert(io_result == 0);
436
437 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_device_spec,
438 &storage_dev_handle);
439 assert(io_result == 0);
440}
441#endif /* STM32MP_SDMMC || STM32MP_EMMC */
442
443#if STM32MP_SPI_NOR
444static void boot_spi_nor(boot_api_context_t *boot_context)
445{
446 int io_result __unused;
447 uint8_t idx;
448 struct stm32image_part_info *part;
449
450 io_result = stm32_qspi_init();
451 assert(io_result == 0);
452
453 io_result = register_io_dev_mtd(&spi_dev_con);
454 assert(io_result == 0);
455
456 /* Open connections to device */
457 io_result = io_dev_open(spi_dev_con,
458 (uintptr_t)&spi_nor_dev_spec,
459 &storage_dev_handle);
460 assert(io_result == 0);
461
462 stm32image_dev_info_spec.device_size = spi_nor_dev_spec.device_size;
463
464 idx = IMG_IDX_BL33;
465 part = &stm32image_dev_info_spec.part_info[idx];
466 part->part_offset = STM32MP_NOR_BL33_OFFSET;
467 part->bkp_offset = 0U;
468
469#ifdef AARCH32_SP_OPTEE
470 idx = IMG_IDX_OPTEE_HEADER;
471 part = &stm32image_dev_info_spec.part_info[idx];
472 part->part_offset = STM32MP_NOR_TEEH_OFFSET;
473 part->bkp_offset = 0U;
474
475 idx = IMG_IDX_OPTEE_PAGED;
476 part = &stm32image_dev_info_spec.part_info[idx];
477 part->part_offset = STM32MP_NOR_TEED_OFFSET;
478 part->bkp_offset = 0U;
479
480 idx = IMG_IDX_OPTEE_CORE;
481 part = &stm32image_dev_info_spec.part_info[idx];
482 part->part_offset = STM32MP_NOR_TEEX_OFFSET;
483 part->bkp_offset = 0U;
484#endif
485}
486#endif /* STM32MP_SPI_NOR */
487
488#if STM32MP_RAW_NAND
489static void boot_fmc2_nand(boot_api_context_t *boot_context)
490{
491 int io_result __unused;
492 uint8_t idx;
493 struct stm32image_part_info *part;
494
495 io_result = stm32_fmc2_init();
496 assert(io_result == 0);
497
498 /* Register the IO device on this platform */
499 io_result = register_io_dev_mtd(&nand_dev_con);
500 assert(io_result == 0);
501
502 /* Open connections to device */
503 io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
504 &storage_dev_handle);
505 assert(io_result == 0);
506
507 stm32image_dev_info_spec.device_size = nand_dev_spec.device_size;
508
509 idx = IMG_IDX_BL33;
510 part = &stm32image_dev_info_spec.part_info[idx];
511 part->part_offset = STM32MP_NAND_BL33_OFFSET;
512 part->bkp_offset = nand_dev_spec.erase_size;
513
514#ifdef AARCH32_SP_OPTEE
515 idx = IMG_IDX_OPTEE_HEADER;
516 part = &stm32image_dev_info_spec.part_info[idx];
517 part->part_offset = STM32MP_NAND_TEEH_OFFSET;
518 part->bkp_offset = nand_dev_spec.erase_size;
519
520 idx = IMG_IDX_OPTEE_PAGED;
521 part = &stm32image_dev_info_spec.part_info[idx];
522 part->part_offset = STM32MP_NAND_TEED_OFFSET;
523 part->bkp_offset = nand_dev_spec.erase_size;
524
525 idx = IMG_IDX_OPTEE_CORE;
526 part = &stm32image_dev_info_spec.part_info[idx];
527 part->part_offset = STM32MP_NAND_TEEX_OFFSET;
528 part->bkp_offset = nand_dev_spec.erase_size;
529#endif
530}
531#endif /* STM32MP_RAW_NAND */
532
533#if STM32MP_SPI_NAND
534static void boot_spi_nand(boot_api_context_t *boot_context)
535{
536 int io_result __unused;
537 uint8_t idx;
538 struct stm32image_part_info *part;
539
540 io_result = stm32_qspi_init();
541 assert(io_result == 0);
542
543 io_result = register_io_dev_mtd(&spi_dev_con);
544 assert(io_result == 0);
545
546 /* Open connections to device */
547 io_result = io_dev_open(spi_dev_con,
548 (uintptr_t)&spi_nand_dev_spec,
549 &storage_dev_handle);
550 assert(io_result == 0);
551
552 stm32image_dev_info_spec.device_size =
553 spi_nand_dev_spec.device_size;
554
555 idx = IMG_IDX_BL33;
556 part = &stm32image_dev_info_spec.part_info[idx];
557 part->part_offset = STM32MP_NAND_BL33_OFFSET;
558 part->bkp_offset = spi_nand_dev_spec.erase_size;
559
560#ifdef AARCH32_SP_OPTEE
561 idx = IMG_IDX_OPTEE_HEADER;
562 part = &stm32image_dev_info_spec.part_info[idx];
563 part->part_offset = STM32MP_NAND_TEEH_OFFSET;
564 part->bkp_offset = spi_nand_dev_spec.erase_size;
565
566 idx = IMG_IDX_OPTEE_PAGED;
567 part = &stm32image_dev_info_spec.part_info[idx];
568 part->part_offset = STM32MP_NAND_TEED_OFFSET;
569 part->bkp_offset = spi_nand_dev_spec.erase_size;
570
571 idx = IMG_IDX_OPTEE_CORE;
572 part = &stm32image_dev_info_spec.part_info[idx];
573 part->part_offset = STM32MP_NAND_TEEX_OFFSET;
574 part->bkp_offset = spi_nand_dev_spec.erase_size;
575#endif
576}
577#endif /* STM32MP_SPI_NAND */
578
579void stm32mp_io_setup(void)
580{
581 int io_result __unused;
582 boot_api_context_t *boot_context =
583 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
584
585 print_boot_device(boot_context);
586
587 if ((boot_context->boot_partition_used_toboot == 1U) ||
588 (boot_context->boot_partition_used_toboot == 2U)) {
589 INFO("Boot used partition fsbl%u\n",
590 boot_context->boot_partition_used_toboot);
591 }
592
593#ifndef AARCH32_SP_OPTEE
594 io_result = register_io_dev_dummy(&dummy_dev_con);
595 assert(io_result == 0);
596
597 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
598 &dummy_dev_handle);
599 assert(io_result == 0);
600#endif
601
602 switch (boot_context->boot_interface_selected) {
603#if STM32MP_SDMMC
604 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
605 dmbsy();
606 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
607 stm32image_io_setup();
608 break;
609#endif
610#if STM32MP_EMMC
611 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
612 dmbsy();
613 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
614 stm32image_io_setup();
615 break;
616#endif
617#if STM32MP_SPI_NOR
618 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
619 dmbsy();
620 boot_spi_nor(boot_context);
621 stm32image_io_setup();
622 break;
623#endif
624#if STM32MP_RAW_NAND
625 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
626 dmbsy();
627 boot_fmc2_nand(boot_context);
628 stm32image_io_setup();
629 break;
630#endif
631#if STM32MP_SPI_NAND
632 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
633 dmbsy();
634 boot_spi_nand(boot_context);
635 stm32image_io_setup();
636 break;
637#endif
638
639 default:
640 ERROR("Boot interface %d not supported\n",
641 boot_context->boot_interface_selected);
642 panic();
643 break;
644 }
645}
646
647/*
648 * Return an IO device handle and specification which can be used to access
649 * an image. Use this to enforce platform load policy.
650 */
651int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
652 uintptr_t *image_spec)
653{
654 int rc;
655 const struct plat_io_policy *policy;
656
657 assert(image_id < ARRAY_SIZE(policies));
658
659 policy = &policies[image_id];
660 rc = policy->check(policy->image_spec);
661 if (rc == 0) {
662 *image_spec = policy->image_spec;
663 *dev_handle = *(policy->dev_handle);
664 }
665
666 return rc;
667}