blob: d7f53ceec1ffd6983a8292bfc2e75f811c6b9b94 [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>
17#include <drivers/io/io_storage.h>
18#include <drivers/mmc.h>
19#include <drivers/partition/partition.h>
20#include <drivers/st/io_mmc.h>
21#include <drivers/st/io_stm32image.h>
22#include <drivers/st/stm32_sdmmc2.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <lib/mmio.h>
24#include <lib/utils.h>
25#include <plat/common/platform.h>
26
Yann Gautier4b0c72a2018-07-16 10:54:09 +020027/* IO devices */
28static const io_dev_connector_t *dummy_dev_con;
29static uintptr_t dummy_dev_handle;
30static uintptr_t dummy_dev_spec;
31
Yann Gautier8244e1d2018-10-15 09:36:58 +020032static uintptr_t image_dev_handle;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020033static uintptr_t storage_dev_handle;
Yann Gautier8244e1d2018-10-15 09:36:58 +020034
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020035#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautier8244e1d2018-10-15 09:36:58 +020036static io_block_spec_t gpt_block_spec = {
37 .offset = 0,
38 .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */
39};
40
Yann Gautierf9af3bc2018-11-09 15:57:18 +010041static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
Yann Gautier8244e1d2018-10-15 09:36:58 +020042
43static const io_block_dev_spec_t mmc_block_dev_spec = {
44 /* It's used as temp buffer in block driver */
45 .buffer = {
46 .offset = (size_t)&block_buffer,
47 .length = MMC_BLOCK_SIZE,
48 },
49 .ops = {
50 .read = mmc_read_blocks,
51 .write = NULL,
52 },
53 .block_size = MMC_BLOCK_SIZE,
54};
55
Yann Gautier8244e1d2018-10-15 09:36:58 +020056static const io_dev_connector_t *mmc_dev_con;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +020057#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +020058
Yann Gautierb3386f72019-04-19 09:41:01 +020059#ifdef AARCH32_SP_OPTEE
60static const struct stm32image_part_info optee_header_partition_spec = {
61 .name = OPTEE_HEADER_IMAGE_NAME,
62 .binary_type = OPTEE_HEADER_BINARY_TYPE,
63};
64
65static const struct stm32image_part_info optee_pager_partition_spec = {
66 .name = OPTEE_PAGER_IMAGE_NAME,
67 .binary_type = OPTEE_PAGER_BINARY_TYPE,
68};
69
70static const struct stm32image_part_info optee_paged_partition_spec = {
71 .name = OPTEE_PAGED_IMAGE_NAME,
72 .binary_type = OPTEE_PAGED_BINARY_TYPE,
73};
74#else
Yann Gautierf9d40d52019-01-17 14:41:46 +010075static const io_block_spec_t bl32_block_spec = {
76 .offset = BL32_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +010077 .length = STM32MP_BL32_SIZE
Yann Gautierf9d40d52019-01-17 14:41:46 +010078};
Yann Gautierb3386f72019-04-19 09:41:01 +020079#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +010080
81static const io_block_spec_t bl2_block_spec = {
82 .offset = BL2_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +010083 .length = STM32MP_BL2_SIZE,
Yann Gautierf9d40d52019-01-17 14:41:46 +010084};
Yann Gautier8244e1d2018-10-15 09:36:58 +020085
86static const struct stm32image_part_info bl33_partition_spec = {
87 .name = BL33_IMAGE_NAME,
88 .binary_type = BL33_BINARY_TYPE,
89};
90
Yann Gautierf9d40d52019-01-17 14:41:46 +010091enum {
92 IMG_IDX_BL33,
Yann Gautierb3386f72019-04-19 09:41:01 +020093#ifdef AARCH32_SP_OPTEE
94 IMG_IDX_OPTEE_HEADER,
95 IMG_IDX_OPTEE_PAGER,
96 IMG_IDX_OPTEE_PAGED,
97#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +010098 IMG_IDX_NUM
99};
100
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200101static struct stm32image_device_info stm32image_dev_info_spec __unused = {
Yann Gautier8244e1d2018-10-15 09:36:58 +0200102 .lba_size = MMC_BLOCK_SIZE,
103 .part_info[IMG_IDX_BL33] = {
104 .name = BL33_IMAGE_NAME,
105 .binary_type = BL33_BINARY_TYPE,
106 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200107#ifdef AARCH32_SP_OPTEE
108 .part_info[IMG_IDX_OPTEE_HEADER] = {
109 .name = OPTEE_HEADER_IMAGE_NAME,
110 .binary_type = OPTEE_HEADER_BINARY_TYPE,
111 },
112 .part_info[IMG_IDX_OPTEE_PAGER] = {
113 .name = OPTEE_PAGER_IMAGE_NAME,
114 .binary_type = OPTEE_PAGER_BINARY_TYPE,
115 },
116 .part_info[IMG_IDX_OPTEE_PAGED] = {
117 .name = OPTEE_PAGED_IMAGE_NAME,
118 .binary_type = OPTEE_PAGED_BINARY_TYPE,
119 },
120#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200121};
122
Yann Gautierf9d40d52019-01-17 14:41:46 +0100123static io_block_spec_t stm32image_block_spec = {
124 .offset = 0,
125 .length = 0,
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200126};
127
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200128static const io_dev_connector_t *stm32image_dev_con __unused;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200129
130static int open_dummy(const uintptr_t spec);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200131static int open_image(const uintptr_t spec);
132static int open_storage(const uintptr_t spec);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200133
134struct plat_io_policy {
135 uintptr_t *dev_handle;
136 uintptr_t image_spec;
137 int (*check)(const uintptr_t spec);
138};
139
140static const struct plat_io_policy policies[] = {
141 [BL2_IMAGE_ID] = {
142 .dev_handle = &dummy_dev_handle,
143 .image_spec = (uintptr_t)&bl2_block_spec,
144 .check = open_dummy
145 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200146#ifdef AARCH32_SP_OPTEE
147 [BL32_IMAGE_ID] = {
148 .dev_handle = &image_dev_handle,
149 .image_spec = (uintptr_t)&optee_header_partition_spec,
150 .check = open_image
151 },
152 [BL32_EXTRA1_IMAGE_ID] = {
153 .dev_handle = &image_dev_handle,
154 .image_spec = (uintptr_t)&optee_pager_partition_spec,
155 .check = open_image
156 },
157 [BL32_EXTRA2_IMAGE_ID] = {
158 .dev_handle = &image_dev_handle,
159 .image_spec = (uintptr_t)&optee_paged_partition_spec,
160 .check = open_image
161 },
162#else
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200163 [BL32_IMAGE_ID] = {
164 .dev_handle = &dummy_dev_handle,
165 .image_spec = (uintptr_t)&bl32_block_spec,
166 .check = open_dummy
167 },
Yann Gautierb3386f72019-04-19 09:41:01 +0200168#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200169 [BL33_IMAGE_ID] = {
170 .dev_handle = &image_dev_handle,
171 .image_spec = (uintptr_t)&bl33_partition_spec,
172 .check = open_image
173 },
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200174#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautier8244e1d2018-10-15 09:36:58 +0200175 [GPT_IMAGE_ID] = {
176 .dev_handle = &storage_dev_handle,
177 .image_spec = (uintptr_t)&gpt_block_spec,
178 .check = open_storage
179 },
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200180#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200181 [STM32_IMAGE_ID] = {
182 .dev_handle = &storage_dev_handle,
183 .image_spec = (uintptr_t)&stm32image_block_spec,
184 .check = open_storage
185 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200186};
187
188static int open_dummy(const uintptr_t spec)
189{
190 return io_dev_init(dummy_dev_handle, 0);
191}
192
Yann Gautier8244e1d2018-10-15 09:36:58 +0200193static int open_image(const uintptr_t spec)
194{
195 return io_dev_init(image_dev_handle, 0);
196}
197
198static int open_storage(const uintptr_t spec)
199{
200 return io_dev_init(storage_dev_handle, 0);
201}
202
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200203static void print_boot_device(boot_api_context_t *boot_context)
204{
205 switch (boot_context->boot_interface_selected) {
206 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
207 INFO("Using SDMMC\n");
208 break;
209 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
210 INFO("Using EMMC\n");
211 break;
212 default:
213 ERROR("Boot interface not found\n");
214 panic();
215 break;
216 }
217
218 if (boot_context->boot_interface_instance != 0U) {
219 INFO(" Instance %d\n", boot_context->boot_interface_instance);
220 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200221}
222
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200223#if STM32MP_SDMMC || STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200224static void boot_mmc(enum mmc_device_type mmc_dev_type,
225 uint16_t boot_interface_instance)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200226{
227 int io_result __unused;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100228 uint8_t idx;
229 struct stm32image_part_info *part;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200230 struct stm32_sdmmc2_params params;
231 struct mmc_device_info device_info;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100232 const partition_entry_t *entry;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200233
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200234 zeromem(&device_info, sizeof(struct mmc_device_info));
235 zeromem(&params, sizeof(struct stm32_sdmmc2_params));
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200236
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200237 device_info.mmc_dev_type = mmc_dev_type;
238
239 switch (boot_interface_instance) {
240 case 1:
241 params.reg_base = STM32MP_SDMMC1_BASE;
242 break;
243 case 2:
244 params.reg_base = STM32MP_SDMMC2_BASE;
245 break;
246 case 3:
247 params.reg_base = STM32MP_SDMMC3_BASE;
248 break;
249 default:
250 WARN("SDMMC instance not found, using default\n");
251 if (mmc_dev_type == MMC_IS_SD) {
252 params.reg_base = STM32MP_SDMMC1_BASE;
253 } else {
254 params.reg_base = STM32MP_SDMMC2_BASE;
255 }
256 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200257 }
258
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200259 params.device_info = &device_info;
260 if (stm32_sdmmc2_mmc_init(&params) != 0) {
261 ERROR("SDMMC%u init failed\n", boot_interface_instance);
262 panic();
263 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200264
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200265 /* Open MMC as a block device to read GPT table */
266 io_result = register_io_dev_block(&mmc_dev_con);
267 if (io_result != 0) {
268 panic();
269 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200270
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200271 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
272 &storage_dev_handle);
273 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200274
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200275 partition_init(GPT_IMAGE_ID);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200276
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200277 io_result = io_dev_close(storage_dev_handle);
278 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200279
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200280 stm32image_dev_info_spec.device_size =
281 stm32_sdmmc2_mmc_get_device_size();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200282
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200283 for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
284 part = &stm32image_dev_info_spec.part_info[idx];
285 entry = get_partition_entry(part->name);
286 if (entry == NULL) {
287 ERROR("Partition %s not found\n", part->name);
Yann Gautier03f04682018-11-29 15:44:04 +0100288 panic();
289 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200290
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200291 part->part_offset = entry->start;
292 part->bkp_offset = 0U;
293 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200294
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200295 /*
296 * Re-open MMC with io_mmc, for better perfs compared to
297 * io_block.
298 */
299 io_result = register_io_dev_mmc(&mmc_dev_con);
300 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200301
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200302 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
303 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200304
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200305 io_result = register_io_dev_stm32image(&stm32image_dev_con);
306 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200307
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200308 io_result = io_dev_open(stm32image_dev_con,
309 (uintptr_t)&stm32image_dev_info_spec,
310 &image_dev_handle);
311 assert(io_result == 0);
312}
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200313#endif /* STM32MP_SDMMC || STM32MP_EMMC */
Yann Gautier8244e1d2018-10-15 09:36:58 +0200314
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200315void stm32mp_io_setup(void)
316{
317 int io_result __unused;
318 boot_api_context_t *boot_context =
319 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautierf9d40d52019-01-17 14:41:46 +0100320
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200321 print_boot_device(boot_context);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200322
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200323 if ((boot_context->boot_partition_used_toboot == 1U) ||
324 (boot_context->boot_partition_used_toboot == 2U)) {
325 INFO("Boot used partition fsbl%d\n",
326 boot_context->boot_partition_used_toboot);
327 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200328
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200329 io_result = register_io_dev_dummy(&dummy_dev_con);
330 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200331
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200332 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
333 &dummy_dev_handle);
334 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200335
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200336 switch (boot_context->boot_interface_selected) {
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200337#if STM32MP_SDMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200338 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
339 dmbsy();
340 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
341 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200342#endif
343#if STM32MP_EMMC
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200344 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
345 dmbsy();
346 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200347 break;
Nicolas Le Bayon3f42cad2019-09-03 09:52:05 +0200348#endif
Yann Gautier8244e1d2018-10-15 09:36:58 +0200349
350 default:
351 ERROR("Boot interface %d not supported\n",
352 boot_context->boot_interface_selected);
353 break;
354 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200355}
356
357/*
358 * Return an IO device handle and specification which can be used to access
359 * an image. Use this to enforce platform load policy.
360 */
361int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
362 uintptr_t *image_spec)
363{
364 int rc;
365 const struct plat_io_policy *policy;
366
367 assert(image_id < ARRAY_SIZE(policies));
368
369 policy = &policies[image_id];
370 rc = policy->check(policy->image_spec);
371 if (rc == 0) {
372 *image_spec = policy->image_spec;
373 *dev_handle = *(policy->dev_handle);
374 }
375
376 return rc;
377}