blob: 1aa3df76e709b259e053094ca08bae398d76f478 [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;
33
34static io_block_spec_t gpt_block_spec = {
35 .offset = 0,
36 .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */
37};
38
Yann Gautierf9af3bc2018-11-09 15:57:18 +010039static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
Yann Gautier8244e1d2018-10-15 09:36:58 +020040
41static const io_block_dev_spec_t mmc_block_dev_spec = {
42 /* It's used as temp buffer in block driver */
43 .buffer = {
44 .offset = (size_t)&block_buffer,
45 .length = MMC_BLOCK_SIZE,
46 },
47 .ops = {
48 .read = mmc_read_blocks,
49 .write = NULL,
50 },
51 .block_size = MMC_BLOCK_SIZE,
52};
53
54static uintptr_t storage_dev_handle;
55static const io_dev_connector_t *mmc_dev_con;
56
Yann Gautierf9d40d52019-01-17 14:41:46 +010057static const io_block_spec_t bl32_block_spec = {
58 .offset = BL32_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +010059 .length = STM32MP_BL32_SIZE
Yann Gautierf9d40d52019-01-17 14:41:46 +010060};
61
62static const io_block_spec_t bl2_block_spec = {
63 .offset = BL2_BASE,
Yann Gautiera2e2a302019-02-14 11:13:39 +010064 .length = STM32MP_BL2_SIZE,
Yann Gautierf9d40d52019-01-17 14:41:46 +010065};
Yann Gautier8244e1d2018-10-15 09:36:58 +020066
67static const struct stm32image_part_info bl33_partition_spec = {
68 .name = BL33_IMAGE_NAME,
69 .binary_type = BL33_BINARY_TYPE,
70};
71
Yann Gautierf9d40d52019-01-17 14:41:46 +010072enum {
73 IMG_IDX_BL33,
74 IMG_IDX_NUM
75};
76
Yann Gautier8244e1d2018-10-15 09:36:58 +020077static struct stm32image_device_info stm32image_dev_info_spec = {
78 .lba_size = MMC_BLOCK_SIZE,
79 .part_info[IMG_IDX_BL33] = {
80 .name = BL33_IMAGE_NAME,
81 .binary_type = BL33_BINARY_TYPE,
82 },
83};
84
Yann Gautierf9d40d52019-01-17 14:41:46 +010085static io_block_spec_t stm32image_block_spec = {
86 .offset = 0,
87 .length = 0,
Yann Gautier4b0c72a2018-07-16 10:54:09 +020088};
89
Yann Gautierf9d40d52019-01-17 14:41:46 +010090static const io_dev_connector_t *stm32image_dev_con;
Yann Gautier4b0c72a2018-07-16 10:54:09 +020091
92static int open_dummy(const uintptr_t spec);
Yann Gautier8244e1d2018-10-15 09:36:58 +020093static int open_image(const uintptr_t spec);
94static int open_storage(const uintptr_t spec);
Yann Gautier4b0c72a2018-07-16 10:54:09 +020095
96struct plat_io_policy {
97 uintptr_t *dev_handle;
98 uintptr_t image_spec;
99 int (*check)(const uintptr_t spec);
100};
101
102static const struct plat_io_policy policies[] = {
103 [BL2_IMAGE_ID] = {
104 .dev_handle = &dummy_dev_handle,
105 .image_spec = (uintptr_t)&bl2_block_spec,
106 .check = open_dummy
107 },
108 [BL32_IMAGE_ID] = {
109 .dev_handle = &dummy_dev_handle,
110 .image_spec = (uintptr_t)&bl32_block_spec,
111 .check = open_dummy
112 },
Yann Gautier8244e1d2018-10-15 09:36:58 +0200113 [BL33_IMAGE_ID] = {
114 .dev_handle = &image_dev_handle,
115 .image_spec = (uintptr_t)&bl33_partition_spec,
116 .check = open_image
117 },
118 [GPT_IMAGE_ID] = {
119 .dev_handle = &storage_dev_handle,
120 .image_spec = (uintptr_t)&gpt_block_spec,
121 .check = open_storage
122 },
123 [STM32_IMAGE_ID] = {
124 .dev_handle = &storage_dev_handle,
125 .image_spec = (uintptr_t)&stm32image_block_spec,
126 .check = open_storage
127 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200128};
129
130static int open_dummy(const uintptr_t spec)
131{
132 return io_dev_init(dummy_dev_handle, 0);
133}
134
Yann Gautier8244e1d2018-10-15 09:36:58 +0200135static int open_image(const uintptr_t spec)
136{
137 return io_dev_init(image_dev_handle, 0);
138}
139
140static int open_storage(const uintptr_t spec)
141{
142 return io_dev_init(storage_dev_handle, 0);
143}
144
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200145static void print_boot_device(boot_api_context_t *boot_context)
146{
147 switch (boot_context->boot_interface_selected) {
148 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
149 INFO("Using SDMMC\n");
150 break;
151 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
152 INFO("Using EMMC\n");
153 break;
154 default:
155 ERROR("Boot interface not found\n");
156 panic();
157 break;
158 }
159
160 if (boot_context->boot_interface_instance != 0U) {
161 INFO(" Instance %d\n", boot_context->boot_interface_instance);
162 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200163}
164
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200165static void boot_mmc(enum mmc_device_type mmc_dev_type,
166 uint16_t boot_interface_instance)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200167{
168 int io_result __unused;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100169 uint8_t idx;
170 struct stm32image_part_info *part;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200171 struct stm32_sdmmc2_params params;
172 struct mmc_device_info device_info;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100173 const partition_entry_t *entry;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200174
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200175 zeromem(&device_info, sizeof(struct mmc_device_info));
176 zeromem(&params, sizeof(struct stm32_sdmmc2_params));
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200177
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200178 device_info.mmc_dev_type = mmc_dev_type;
179
180 switch (boot_interface_instance) {
181 case 1:
182 params.reg_base = STM32MP_SDMMC1_BASE;
183 break;
184 case 2:
185 params.reg_base = STM32MP_SDMMC2_BASE;
186 break;
187 case 3:
188 params.reg_base = STM32MP_SDMMC3_BASE;
189 break;
190 default:
191 WARN("SDMMC instance not found, using default\n");
192 if (mmc_dev_type == MMC_IS_SD) {
193 params.reg_base = STM32MP_SDMMC1_BASE;
194 } else {
195 params.reg_base = STM32MP_SDMMC2_BASE;
196 }
197 break;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200198 }
199
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200200 params.device_info = &device_info;
201 if (stm32_sdmmc2_mmc_init(&params) != 0) {
202 ERROR("SDMMC%u init failed\n", boot_interface_instance);
203 panic();
204 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200205
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200206 /* Open MMC as a block device to read GPT table */
207 io_result = register_io_dev_block(&mmc_dev_con);
208 if (io_result != 0) {
209 panic();
210 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200211
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200212 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
213 &storage_dev_handle);
214 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200215
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200216 partition_init(GPT_IMAGE_ID);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200217
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200218 io_result = io_dev_close(storage_dev_handle);
219 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200220
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200221 stm32image_dev_info_spec.device_size =
222 stm32_sdmmc2_mmc_get_device_size();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200223
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200224 for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
225 part = &stm32image_dev_info_spec.part_info[idx];
226 entry = get_partition_entry(part->name);
227 if (entry == NULL) {
228 ERROR("Partition %s not found\n", part->name);
Yann Gautier03f04682018-11-29 15:44:04 +0100229 panic();
230 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200231
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200232 part->part_offset = entry->start;
233 part->bkp_offset = 0U;
234 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200235
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200236 /*
237 * Re-open MMC with io_mmc, for better perfs compared to
238 * io_block.
239 */
240 io_result = register_io_dev_mmc(&mmc_dev_con);
241 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200242
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200243 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
244 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200245
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200246 io_result = register_io_dev_stm32image(&stm32image_dev_con);
247 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200248
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200249 io_result = io_dev_open(stm32image_dev_con,
250 (uintptr_t)&stm32image_dev_info_spec,
251 &image_dev_handle);
252 assert(io_result == 0);
253}
Yann Gautier8244e1d2018-10-15 09:36:58 +0200254
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200255void stm32mp_io_setup(void)
256{
257 int io_result __unused;
258 boot_api_context_t *boot_context =
259 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautierf9d40d52019-01-17 14:41:46 +0100260
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200261 print_boot_device(boot_context);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200262
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200263 if ((boot_context->boot_partition_used_toboot == 1U) ||
264 (boot_context->boot_partition_used_toboot == 2U)) {
265 INFO("Boot used partition fsbl%d\n",
266 boot_context->boot_partition_used_toboot);
267 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200268
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200269 io_result = register_io_dev_dummy(&dummy_dev_con);
270 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200271
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200272 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
273 &dummy_dev_handle);
274 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200275
Yann Gautiereae3fbf2019-04-23 13:34:03 +0200276 switch (boot_context->boot_interface_selected) {
277 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
278 dmbsy();
279 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
280 break;
281 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
282 dmbsy();
283 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200284 break;
285
286 default:
287 ERROR("Boot interface %d not supported\n",
288 boot_context->boot_interface_selected);
289 break;
290 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200291}
292
293/*
294 * Return an IO device handle and specification which can be used to access
295 * an image. Use this to enforce platform load policy.
296 */
297int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
298 uintptr_t *image_spec)
299{
300 int rc;
301 const struct plat_io_policy *policy;
302
303 assert(image_id < ARRAY_SIZE(policies));
304
305 policy = &policies[image_id];
306 rc = policy->check(policy->image_spec);
307 if (rc == 0) {
308 *image_spec = policy->image_spec;
309 *dev_handle = *(policy->dev_handle);
310 }
311
312 return rc;
313}