blob: 8ccbc246c9a13fcd77d91a8c270ec015fb48e050 [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>
23#include <drivers/st/stm32mp1_rcc.h>
24#include <lib/mmio.h>
25#include <lib/utils.h>
26#include <plat/common/platform.h>
27
28#include <boot_api.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020029#include <stm32mp1_private.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020030
31/* IO devices */
32static const io_dev_connector_t *dummy_dev_con;
33static uintptr_t dummy_dev_handle;
34static uintptr_t dummy_dev_spec;
35
Yann Gautier8244e1d2018-10-15 09:36:58 +020036static uintptr_t image_dev_handle;
37
38static io_block_spec_t gpt_block_spec = {
39 .offset = 0,
40 .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */
41};
42
Yann Gautierf9af3bc2018-11-09 15:57:18 +010043static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
Yann Gautier8244e1d2018-10-15 09:36:58 +020044
45static const io_block_dev_spec_t mmc_block_dev_spec = {
46 /* It's used as temp buffer in block driver */
47 .buffer = {
48 .offset = (size_t)&block_buffer,
49 .length = MMC_BLOCK_SIZE,
50 },
51 .ops = {
52 .read = mmc_read_blocks,
53 .write = NULL,
54 },
55 .block_size = MMC_BLOCK_SIZE,
56};
57
58static uintptr_t storage_dev_handle;
59static const io_dev_connector_t *mmc_dev_con;
60
Yann Gautierf9d40d52019-01-17 14:41:46 +010061static const io_block_spec_t bl32_block_spec = {
62 .offset = BL32_BASE,
63 .length = STM32MP1_BL32_SIZE
64};
65
66static const io_block_spec_t bl2_block_spec = {
67 .offset = BL2_BASE,
68 .length = STM32MP1_BL2_SIZE,
69};
Yann Gautier8244e1d2018-10-15 09:36:58 +020070
71static const struct stm32image_part_info bl33_partition_spec = {
72 .name = BL33_IMAGE_NAME,
73 .binary_type = BL33_BINARY_TYPE,
74};
75
Yann Gautierf9d40d52019-01-17 14:41:46 +010076enum {
77 IMG_IDX_BL33,
78 IMG_IDX_NUM
79};
80
Yann Gautier8244e1d2018-10-15 09:36:58 +020081static struct stm32image_device_info stm32image_dev_info_spec = {
82 .lba_size = MMC_BLOCK_SIZE,
83 .part_info[IMG_IDX_BL33] = {
84 .name = BL33_IMAGE_NAME,
85 .binary_type = BL33_BINARY_TYPE,
86 },
87};
88
Yann Gautierf9d40d52019-01-17 14:41:46 +010089static io_block_spec_t stm32image_block_spec = {
90 .offset = 0,
91 .length = 0,
Yann Gautier4b0c72a2018-07-16 10:54:09 +020092};
93
Yann Gautierf9d40d52019-01-17 14:41:46 +010094static const io_dev_connector_t *stm32image_dev_con;
Yann Gautier4b0c72a2018-07-16 10:54:09 +020095
96static int open_dummy(const uintptr_t spec);
Yann Gautier8244e1d2018-10-15 09:36:58 +020097static int open_image(const uintptr_t spec);
98static int open_storage(const uintptr_t spec);
Yann Gautier4b0c72a2018-07-16 10:54:09 +020099
100struct plat_io_policy {
101 uintptr_t *dev_handle;
102 uintptr_t image_spec;
103 int (*check)(const uintptr_t spec);
104};
105
106static const struct plat_io_policy policies[] = {
107 [BL2_IMAGE_ID] = {
108 .dev_handle = &dummy_dev_handle,
109 .image_spec = (uintptr_t)&bl2_block_spec,
110 .check = open_dummy
111 },
112 [BL32_IMAGE_ID] = {
113 .dev_handle = &dummy_dev_handle,
114 .image_spec = (uintptr_t)&bl32_block_spec,
115 .check = open_dummy
116 },
Yann Gautier8244e1d2018-10-15 09:36:58 +0200117 [BL33_IMAGE_ID] = {
118 .dev_handle = &image_dev_handle,
119 .image_spec = (uintptr_t)&bl33_partition_spec,
120 .check = open_image
121 },
122 [GPT_IMAGE_ID] = {
123 .dev_handle = &storage_dev_handle,
124 .image_spec = (uintptr_t)&gpt_block_spec,
125 .check = open_storage
126 },
127 [STM32_IMAGE_ID] = {
128 .dev_handle = &storage_dev_handle,
129 .image_spec = (uintptr_t)&stm32image_block_spec,
130 .check = open_storage
131 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200132};
133
134static int open_dummy(const uintptr_t spec)
135{
136 return io_dev_init(dummy_dev_handle, 0);
137}
138
Yann Gautier8244e1d2018-10-15 09:36:58 +0200139static int open_image(const uintptr_t spec)
140{
141 return io_dev_init(image_dev_handle, 0);
142}
143
144static int open_storage(const uintptr_t spec)
145{
146 return io_dev_init(storage_dev_handle, 0);
147}
148
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200149static void print_boot_device(boot_api_context_t *boot_context)
150{
151 switch (boot_context->boot_interface_selected) {
152 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
153 INFO("Using SDMMC\n");
154 break;
155 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
156 INFO("Using EMMC\n");
157 break;
158 default:
159 ERROR("Boot interface not found\n");
160 panic();
161 break;
162 }
163
164 if (boot_context->boot_interface_instance != 0U) {
165 INFO(" Instance %d\n", boot_context->boot_interface_instance);
166 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200167}
168
169void stm32mp1_io_setup(void)
170{
171 int io_result __unused;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100172 uint8_t idx;
173 struct stm32image_part_info *part;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200174 struct stm32_sdmmc2_params params;
175 struct mmc_device_info device_info;
176 uintptr_t mmc_default_instance;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100177 const partition_entry_t *entry;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200178 boot_api_context_t *boot_context =
179 (boot_api_context_t *)stm32mp1_get_boot_ctx_address();
180
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200181 print_boot_device(boot_context);
182
183 if ((boot_context->boot_partition_used_toboot == 1U) ||
184 (boot_context->boot_partition_used_toboot == 2U)) {
185 INFO("Boot used partition fsbl%d\n",
186 boot_context->boot_partition_used_toboot);
187 }
188
189 io_result = register_io_dev_dummy(&dummy_dev_con);
190 assert(io_result == 0);
191
192 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
193 &dummy_dev_handle);
194 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200195
196 switch (boot_context->boot_interface_selected) {
197 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
198 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
Yann Gautierf9d40d52019-01-17 14:41:46 +0100199 dmbsy();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200200
201 memset(&params, 0, sizeof(struct stm32_sdmmc2_params));
202
203 if (boot_context->boot_interface_selected ==
204 BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC) {
205 device_info.mmc_dev_type = MMC_IS_EMMC;
206 mmc_default_instance = STM32MP1_SDMMC2_BASE;
207 } else {
208 device_info.mmc_dev_type = MMC_IS_SD;
209 mmc_default_instance = STM32MP1_SDMMC1_BASE;
210 }
211
212 switch (boot_context->boot_interface_instance) {
213 case 1:
214 params.reg_base = STM32MP1_SDMMC1_BASE;
215 break;
216 case 2:
217 params.reg_base = STM32MP1_SDMMC2_BASE;
218 break;
219 case 3:
220 params.reg_base = STM32MP1_SDMMC3_BASE;
221 break;
222 default:
223 WARN("SDMMC instance not found, using default\n");
224 params.reg_base = mmc_default_instance;
225 break;
226 }
227
228 params.device_info = &device_info;
Yann Gautier03f04682018-11-29 15:44:04 +0100229 if (stm32_sdmmc2_mmc_init(&params) != 0) {
230 ERROR("SDMMC%u init failed\n",
231 boot_context->boot_interface_instance);
232 panic();
233 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200234
235 /* Open MMC as a block device to read GPT table */
236 io_result = register_io_dev_block(&mmc_dev_con);
237 if (io_result != 0) {
238 panic();
239 }
240
241 io_result = io_dev_open(mmc_dev_con,
242 (uintptr_t)&mmc_block_dev_spec,
243 &storage_dev_handle);
244 assert(io_result == 0);
245
246 partition_init(GPT_IMAGE_ID);
247
248 io_result = io_dev_close(storage_dev_handle);
249 assert(io_result == 0);
250
251 stm32image_dev_info_spec.device_size =
252 stm32_sdmmc2_mmc_get_device_size();
Yann Gautier8244e1d2018-10-15 09:36:58 +0200253
Yann Gautierf9d40d52019-01-17 14:41:46 +0100254 for (idx = 0U; idx < IMG_IDX_NUM; idx++) {
255 part = &stm32image_dev_info_spec.part_info[idx];
256 entry = get_partition_entry(part->name);
257 if (entry == NULL) {
258 ERROR("Partition %s not found\n",
259 part->name);
260 panic();
261 }
262
263 part->part_offset = entry->start;
264 part->bkp_offset = 0U;
265 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200266
267 /*
268 * Re-open MMC with io_mmc, for better perfs compared to
269 * io_block.
270 */
271 io_result = register_io_dev_mmc(&mmc_dev_con);
272 assert(io_result == 0);
273
274 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
275 assert(io_result == 0);
276
277 io_result = register_io_dev_stm32image(&stm32image_dev_con);
278 assert(io_result == 0);
279
280 io_result = io_dev_open(stm32image_dev_con,
281 (uintptr_t)&stm32image_dev_info_spec,
282 &image_dev_handle);
283 assert(io_result == 0);
284 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}