blob: 7e76083b3aef583494b636cca2188dbe65e735a2 [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautierac22dd52021-03-22 14:22:14 +01002 * Copyright (c) 2015-2021, 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
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <arch_helpers.h>
11#include <common/debug.h>
Yann Gautiera3bd8d12021-06-18 11:33:26 +020012#include <common/desc_image_load.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <drivers/io/io_block.h>
14#include <drivers/io/io_driver.h>
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020015#include <drivers/io/io_fip.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010016#include <drivers/io/io_mtd.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/io/io_storage.h>
18#include <drivers/mmc.h>
19#include <drivers/partition/partition.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010020#include <drivers/raw_nand.h>
Lionel Debieve186b0462019-09-24 18:30:12 +020021#include <drivers/spi_nand.h>
Lionel Debievecb0dbc42019-09-25 09:11:31 +020022#include <drivers/spi_nor.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/io_mmc.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010024#include <drivers/st/stm32_fmc2_nand.h>
Lionel Debieve186b0462019-09-24 18:30:12 +020025#include <drivers/st/stm32_qspi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <drivers/st/stm32_sdmmc2.h>
Yann Gautier29f1f942021-07-13 18:07:41 +020027#include <lib/fconf/fconf.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028#include <lib/mmio.h>
29#include <lib/utils.h>
30#include <plat/common/platform.h>
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020031#include <tools_share/firmware_image_package.h>
32
33#include <platform_def.h>
Yann Gautier29f1f942021-07-13 18:07:41 +020034#include <stm32mp_fconf_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000035
Yann Gautier4b0c72a2018-07-16 10:54:09 +020036/* IO devices */
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020037uintptr_t fip_dev_handle;
38uintptr_t storage_dev_handle;
Yann Gautier4b0c72a2018-07-16 10:54:09 +020039
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020040static const io_dev_connector_t *fip_dev_con;
Yann Gautier8244e1d2018-10-15 09:36:58 +020041
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020042#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautierac22dd52021-03-22 14:22:14 +010043static struct mmc_device_info mmc_info;
Yann Gautier8244e1d2018-10-15 09:36:58 +020044
Yann Gautierf9af3bc2018-11-09 15:57:18 +010045static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
Yann Gautier8244e1d2018-10-15 09:36:58 +020046
Yann Gautiera3bd8d12021-06-18 11:33:26 +020047static io_block_dev_spec_t mmc_block_dev_spec = {
Yann Gautier8244e1d2018-10-15 09:36:58 +020048 /* It's used as temp buffer in block driver */
49 .buffer = {
50 .offset = (size_t)&block_buffer,
51 .length = MMC_BLOCK_SIZE,
52 },
53 .ops = {
54 .read = mmc_read_blocks,
55 .write = NULL,
56 },
57 .block_size = MMC_BLOCK_SIZE,
58};
Vyacheslav Yurkove43a0802021-06-04 10:10:51 +020059
Yann Gautier8244e1d2018-10-15 09:36:58 +020060static const io_dev_connector_t *mmc_dev_con;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020061#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +020062
Lionel Debievecb0dbc42019-09-25 09:11:31 +020063#if STM32MP_SPI_NOR
64static io_mtd_dev_spec_t spi_nor_dev_spec = {
65 .ops = {
66 .init = spi_nor_init,
67 .read = spi_nor_read,
68 },
69};
70#endif
71
Lionel Debieve402a46b2019-11-04 12:28:15 +010072#if STM32MP_RAW_NAND
73static io_mtd_dev_spec_t nand_dev_spec = {
74 .ops = {
75 .init = nand_raw_init,
76 .read = nand_read,
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020077 .seek = nand_seek_bb
Lionel Debieve402a46b2019-11-04 12:28:15 +010078 },
79};
80
81static const io_dev_connector_t *nand_dev_con;
82#endif
83
Lionel Debieve186b0462019-09-24 18:30:12 +020084#if STM32MP_SPI_NAND
85static io_mtd_dev_spec_t spi_nand_dev_spec = {
86 .ops = {
87 .init = spi_nand_init,
88 .read = nand_read,
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020089 .seek = nand_seek_bb
Lionel Debieve186b0462019-09-24 18:30:12 +020090 },
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 Gautier29f1f942021-07-13 18:07:41 +020098io_block_spec_t image_block_spec = {
Yann Gautier0ed7b2a2021-05-19 18:48:16 +020099 .offset = 0U,
100 .length = 0U,
101};
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200102
Yann Gautier29f1f942021-07-13 18:07:41 +0200103int open_fip(const uintptr_t spec)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200104{
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200105 return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200106}
Yann Gautier8244e1d2018-10-15 09:36:58 +0200107
Yann Gautier29f1f942021-07-13 18:07:41 +0200108int open_storage(const uintptr_t spec)
Yann Gautier8244e1d2018-10-15 09:36:58 +0200109{
110 return io_dev_init(storage_dev_handle, 0);
111}
Vyacheslav Yurkove43a0802021-06-04 10:10:51 +0200112
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200113static void print_boot_device(boot_api_context_t *boot_context)
114{
115 switch (boot_context->boot_interface_selected) {
116 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
117 INFO("Using SDMMC\n");
118 break;
119 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
120 INFO("Using EMMC\n");
121 break;
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200122 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
123 INFO("Using QSPI NOR\n");
124 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100125 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
126 INFO("Using FMC NAND\n");
127 break;
Lionel Debieve186b0462019-09-24 18:30:12 +0200128 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
129 INFO("Using SPI NAND\n");
130 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200131 default:
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200132 ERROR("Boot interface %u not found\n",
133 boot_context->boot_interface_selected);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200134 panic();
135 break;
136 }
137
138 if (boot_context->boot_interface_instance != 0U) {
139 INFO(" Instance %d\n", boot_context->boot_interface_instance);
140 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200141}
142
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200143#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200144static void boot_mmc(enum mmc_device_type mmc_dev_type,
145 uint16_t boot_interface_instance)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200146{
147 int io_result __unused;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200148 struct stm32_sdmmc2_params params;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200149
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200150 zeromem(&params, sizeof(struct stm32_sdmmc2_params));
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200151
Yann Gautierac22dd52021-03-22 14:22:14 +0100152 mmc_info.mmc_dev_type = mmc_dev_type;
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200153
154 switch (boot_interface_instance) {
155 case 1:
156 params.reg_base = STM32MP_SDMMC1_BASE;
157 break;
158 case 2:
159 params.reg_base = STM32MP_SDMMC2_BASE;
160 break;
161 case 3:
162 params.reg_base = STM32MP_SDMMC3_BASE;
163 break;
164 default:
165 WARN("SDMMC instance not found, using default\n");
166 if (mmc_dev_type == MMC_IS_SD) {
167 params.reg_base = STM32MP_SDMMC1_BASE;
168 } else {
169 params.reg_base = STM32MP_SDMMC2_BASE;
170 }
171 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200172 }
173
Yann Gautierac22dd52021-03-22 14:22:14 +0100174 params.device_info = &mmc_info;
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200175 if (stm32_sdmmc2_mmc_init(&params) != 0) {
176 ERROR("SDMMC%u init failed\n", boot_interface_instance);
177 panic();
178 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200179
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200180 /* Open MMC as a block device to read GPT table */
181 io_result = register_io_dev_block(&mmc_dev_con);
182 if (io_result != 0) {
183 panic();
184 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200185
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200186 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
187 &storage_dev_handle);
188 assert(io_result == 0);
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200189}
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200190#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +0200191
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200192#if STM32MP_SPI_NOR
193static void boot_spi_nor(boot_api_context_t *boot_context)
194{
195 int io_result __unused;
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200196
197 io_result = stm32_qspi_init();
198 assert(io_result == 0);
199
200 io_result = register_io_dev_mtd(&spi_dev_con);
201 assert(io_result == 0);
202
203 /* Open connections to device */
204 io_result = io_dev_open(spi_dev_con,
205 (uintptr_t)&spi_nor_dev_spec,
206 &storage_dev_handle);
207 assert(io_result == 0);
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200208}
209#endif /* STM32MP_SPI_NOR */
210
Lionel Debieve402a46b2019-11-04 12:28:15 +0100211#if STM32MP_RAW_NAND
212static void boot_fmc2_nand(boot_api_context_t *boot_context)
213{
214 int io_result __unused;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100215
216 io_result = stm32_fmc2_init();
217 assert(io_result == 0);
218
219 /* Register the IO device on this platform */
220 io_result = register_io_dev_mtd(&nand_dev_con);
221 assert(io_result == 0);
222
223 /* Open connections to device */
224 io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
225 &storage_dev_handle);
226 assert(io_result == 0);
Lionel Debieve402a46b2019-11-04 12:28:15 +0100227}
228#endif /* STM32MP_RAW_NAND */
229
Lionel Debieve186b0462019-09-24 18:30:12 +0200230#if STM32MP_SPI_NAND
231static void boot_spi_nand(boot_api_context_t *boot_context)
232{
233 int io_result __unused;
Lionel Debieve186b0462019-09-24 18:30:12 +0200234
235 io_result = stm32_qspi_init();
236 assert(io_result == 0);
237
238 io_result = register_io_dev_mtd(&spi_dev_con);
239 assert(io_result == 0);
240
241 /* Open connections to device */
242 io_result = io_dev_open(spi_dev_con,
243 (uintptr_t)&spi_nand_dev_spec,
244 &storage_dev_handle);
245 assert(io_result == 0);
Lionel Debieve186b0462019-09-24 18:30:12 +0200246}
247#endif /* STM32MP_SPI_NAND */
248
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200249void stm32mp_io_setup(void)
250{
251 int io_result __unused;
252 boot_api_context_t *boot_context =
253 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautierf9d40d52019-01-17 14:41:46 +0100254
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200255 print_boot_device(boot_context);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200256
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200257 if ((boot_context->boot_partition_used_toboot == 1U) ||
258 (boot_context->boot_partition_used_toboot == 2U)) {
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200259 INFO("Boot used partition fsbl%u\n",
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200260 boot_context->boot_partition_used_toboot);
261 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200262
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200263 io_result = register_io_dev_fip(&fip_dev_con);
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200264 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200265
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200266 io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
267 &fip_dev_handle);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200268
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200269 switch (boot_context->boot_interface_selected) {
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200270#if STM32MP_SDMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200271 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
272 dmbsy();
273 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
274 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200275#endif
276#if STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200277 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
278 dmbsy();
279 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200280 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200281#endif
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200282#if STM32MP_SPI_NOR
283 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
284 dmbsy();
285 boot_spi_nor(boot_context);
286 break;
287#endif
Lionel Debieve402a46b2019-11-04 12:28:15 +0100288#if STM32MP_RAW_NAND
289 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
290 dmbsy();
291 boot_fmc2_nand(boot_context);
292 break;
293#endif
Lionel Debieve186b0462019-09-24 18:30:12 +0200294#if STM32MP_SPI_NAND
295 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
296 dmbsy();
297 boot_spi_nand(boot_context);
298 break;
299#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200300
301 default:
302 ERROR("Boot interface %d not supported\n",
303 boot_context->boot_interface_selected);
Yann Gautier4c2b73d2021-06-30 17:04:22 +0200304 panic();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200305 break;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200306 }
307}
308
309int bl2_plat_handle_pre_image_load(unsigned int image_id)
310{
311 static bool gpt_init_done __unused;
312 uint16_t boot_itf = stm32mp_get_boot_itf_selected();
313
314 switch (boot_itf) {
315#if STM32MP_SDMMC || STM32MP_EMMC
316 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
317 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
318 if (!gpt_init_done) {
319 const partition_entry_t *entry;
320
321 partition_init(GPT_IMAGE_ID);
322 entry = get_partition_entry(FIP_IMAGE_NAME);
323 if (entry == NULL) {
324 ERROR("Could NOT find the %s partition!\n",
325 FIP_IMAGE_NAME);
326 return -ENOENT;
327 }
328
329 image_block_spec.offset = entry->start;
330 image_block_spec.length = entry->length;
331
332 gpt_init_done = true;
Yann Gautiera3bd8d12021-06-18 11:33:26 +0200333 } else {
334 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
335
336 mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base;
337 mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200338 }
339
340 break;
341#endif
342
343#if STM32MP_RAW_NAND || STM32MP_SPI_NAND
344#if STM32MP_RAW_NAND
345 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
346#endif
347#if STM32MP_SPI_NAND
348 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_QSPI:
349#endif
350 image_block_spec.offset = STM32MP_NAND_FIP_OFFSET;
351 break;
352#endif
353
354#if STM32MP_SPI_NOR
355 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_QSPI:
356 image_block_spec.offset = STM32MP_NOR_FIP_OFFSET;
357 break;
358#endif
359
360 default:
361 ERROR("FIP Not found\n");
362 panic();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200363 }
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200364
365 return 0;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200366}
367
368/*
369 * Return an IO device handle and specification which can be used to access
370 * an image. Use this to enforce platform load policy.
371 */
372int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
373 uintptr_t *image_spec)
374{
375 int rc;
376 const struct plat_io_policy *policy;
377
Yann Gautier29f1f942021-07-13 18:07:41 +0200378 policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200379 rc = policy->check(policy->image_spec);
380 if (rc == 0) {
381 *image_spec = policy->image_spec;
382 *dev_handle = *(policy->dev_handle);
383 }
384
385 return rc;
386}