blob: 45a352e0c5a12434cb97f24df0722463e8094f89 [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
2 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3 *
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
61#define IMG_IDX_BL33 0
62
63static const struct stm32image_part_info bl33_partition_spec = {
64 .name = BL33_IMAGE_NAME,
65 .binary_type = BL33_BINARY_TYPE,
66};
67
68static struct stm32image_device_info stm32image_dev_info_spec = {
69 .lba_size = MMC_BLOCK_SIZE,
70 .part_info[IMG_IDX_BL33] = {
71 .name = BL33_IMAGE_NAME,
72 .binary_type = BL33_BINARY_TYPE,
73 },
74};
75
76static io_block_spec_t stm32image_block_spec;
77
78static const io_dev_connector_t *stm32image_dev_con;
79
Yann Gautier4b0c72a2018-07-16 10:54:09 +020080static const io_block_spec_t bl32_block_spec = {
81 .offset = BL32_BASE,
82 .length = STM32MP1_BL32_SIZE
83};
84
85static const io_block_spec_t bl2_block_spec = {
86 .offset = BL2_BASE,
87 .length = STM32MP1_BL2_SIZE,
88};
89
90static int open_dummy(const uintptr_t spec);
Yann Gautier8244e1d2018-10-15 09:36:58 +020091static int open_image(const uintptr_t spec);
92static int open_storage(const uintptr_t spec);
Yann Gautier4b0c72a2018-07-16 10:54:09 +020093
94struct plat_io_policy {
95 uintptr_t *dev_handle;
96 uintptr_t image_spec;
97 int (*check)(const uintptr_t spec);
98};
99
100static const struct plat_io_policy policies[] = {
101 [BL2_IMAGE_ID] = {
102 .dev_handle = &dummy_dev_handle,
103 .image_spec = (uintptr_t)&bl2_block_spec,
104 .check = open_dummy
105 },
106 [BL32_IMAGE_ID] = {
107 .dev_handle = &dummy_dev_handle,
108 .image_spec = (uintptr_t)&bl32_block_spec,
109 .check = open_dummy
110 },
Yann Gautier8244e1d2018-10-15 09:36:58 +0200111 [BL33_IMAGE_ID] = {
112 .dev_handle = &image_dev_handle,
113 .image_spec = (uintptr_t)&bl33_partition_spec,
114 .check = open_image
115 },
116 [GPT_IMAGE_ID] = {
117 .dev_handle = &storage_dev_handle,
118 .image_spec = (uintptr_t)&gpt_block_spec,
119 .check = open_storage
120 },
121 [STM32_IMAGE_ID] = {
122 .dev_handle = &storage_dev_handle,
123 .image_spec = (uintptr_t)&stm32image_block_spec,
124 .check = open_storage
125 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200126};
127
128static int open_dummy(const uintptr_t spec)
129{
130 return io_dev_init(dummy_dev_handle, 0);
131}
132
Yann Gautier8244e1d2018-10-15 09:36:58 +0200133static int open_image(const uintptr_t spec)
134{
135 return io_dev_init(image_dev_handle, 0);
136}
137
138static int open_storage(const uintptr_t spec)
139{
140 return io_dev_init(storage_dev_handle, 0);
141}
142
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200143static void print_boot_device(boot_api_context_t *boot_context)
144{
145 switch (boot_context->boot_interface_selected) {
146 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
147 INFO("Using SDMMC\n");
148 break;
149 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
150 INFO("Using EMMC\n");
151 break;
152 default:
153 ERROR("Boot interface not found\n");
154 panic();
155 break;
156 }
157
158 if (boot_context->boot_interface_instance != 0U) {
159 INFO(" Instance %d\n", boot_context->boot_interface_instance);
160 }
161}
162
163static void print_reset_reason(void)
164{
165 uint32_t rstsr = mmio_read_32(RCC_BASE + RCC_MP_RSTSCLRR);
166
167 if (rstsr == 0U) {
168 WARN("Reset reason unknown\n");
169 return;
170 }
171
172 INFO("Reset reason (0x%x):\n", rstsr);
173
174 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
175 if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
176 INFO("System exits from STANDBY\n");
177 return;
178 }
179
180 if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
181 INFO("MPU exits from CSTANDBY\n");
182 return;
183 }
184 }
185
186 if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
187 INFO(" Power-on Reset (rst_por)\n");
188 return;
189 }
190
191 if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
192 INFO(" Brownout Reset (rst_bor)\n");
193 return;
194 }
195
196 if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
197 INFO(" System reset generated by MPU (MPSYSRST)\n");
198 return;
199 }
200
201 if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
202 INFO(" Reset due to a clock failure on HSE\n");
203 return;
204 }
205
206 if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
207 INFO(" IWDG1 Reset (rst_iwdg1)\n");
208 return;
209 }
210
211 if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
212 INFO(" IWDG2 Reset (rst_iwdg2)\n");
213 return;
214 }
215
216 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
217 INFO(" Pad Reset from NRST\n");
218 return;
219 }
220
221 if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
222 INFO(" Reset due to a failure of VDD_CORE\n");
223 return;
224 }
225
226 ERROR(" Unidentified reset reason\n");
227}
228
229void stm32mp1_io_setup(void)
230{
231 int io_result __unused;
Yann Gautier8244e1d2018-10-15 09:36:58 +0200232 struct stm32_sdmmc2_params params;
233 struct mmc_device_info device_info;
234 uintptr_t mmc_default_instance;
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200235 boot_api_context_t *boot_context =
236 (boot_api_context_t *)stm32mp1_get_boot_ctx_address();
237
238 print_reset_reason();
239
240 print_boot_device(boot_context);
241
242 if ((boot_context->boot_partition_used_toboot == 1U) ||
243 (boot_context->boot_partition_used_toboot == 2U)) {
244 INFO("Boot used partition fsbl%d\n",
245 boot_context->boot_partition_used_toboot);
246 }
247
248 io_result = register_io_dev_dummy(&dummy_dev_con);
249 assert(io_result == 0);
250
251 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec,
252 &dummy_dev_handle);
253 assert(io_result == 0);
Yann Gautier8244e1d2018-10-15 09:36:58 +0200254
255 switch (boot_context->boot_interface_selected) {
256 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
257 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
258 dmb();
259
260 memset(&params, 0, sizeof(struct stm32_sdmmc2_params));
261
262 if (boot_context->boot_interface_selected ==
263 BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC) {
264 device_info.mmc_dev_type = MMC_IS_EMMC;
265 mmc_default_instance = STM32MP1_SDMMC2_BASE;
266 } else {
267 device_info.mmc_dev_type = MMC_IS_SD;
268 mmc_default_instance = STM32MP1_SDMMC1_BASE;
269 }
270
271 switch (boot_context->boot_interface_instance) {
272 case 1:
273 params.reg_base = STM32MP1_SDMMC1_BASE;
274 break;
275 case 2:
276 params.reg_base = STM32MP1_SDMMC2_BASE;
277 break;
278 case 3:
279 params.reg_base = STM32MP1_SDMMC3_BASE;
280 break;
281 default:
282 WARN("SDMMC instance not found, using default\n");
283 params.reg_base = mmc_default_instance;
284 break;
285 }
286
287 params.device_info = &device_info;
Yann Gautier03f04682018-11-29 15:44:04 +0100288 if (stm32_sdmmc2_mmc_init(&params) != 0) {
289 ERROR("SDMMC%u init failed\n",
290 boot_context->boot_interface_instance);
291 panic();
292 }
Yann Gautier8244e1d2018-10-15 09:36:58 +0200293
294 /* Open MMC as a block device to read GPT table */
295 io_result = register_io_dev_block(&mmc_dev_con);
296 if (io_result != 0) {
297 panic();
298 }
299
300 io_result = io_dev_open(mmc_dev_con,
301 (uintptr_t)&mmc_block_dev_spec,
302 &storage_dev_handle);
303 assert(io_result == 0);
304
305 partition_init(GPT_IMAGE_ID);
306
307 io_result = io_dev_close(storage_dev_handle);
308 assert(io_result == 0);
309
310 stm32image_dev_info_spec.device_size =
311 stm32_sdmmc2_mmc_get_device_size();
312 stm32image_dev_info_spec.part_info[IMG_IDX_BL33].part_offset =
313 get_partition_entry(BL33_IMAGE_NAME)->start;
314 stm32image_dev_info_spec.part_info[IMG_IDX_BL33].bkp_offset =
315 get_partition_entry(BL33_IMAGE_NAME)->length;
316
317 stm32image_block_spec.offset = 0;
318 stm32image_block_spec.length =
319 get_partition_entry(BL33_IMAGE_NAME)->length;
320
321 /*
322 * Re-open MMC with io_mmc, for better perfs compared to
323 * io_block.
324 */
325 io_result = register_io_dev_mmc(&mmc_dev_con);
326 assert(io_result == 0);
327
328 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle);
329 assert(io_result == 0);
330
331 io_result = register_io_dev_stm32image(&stm32image_dev_con);
332 assert(io_result == 0);
333
334 io_result = io_dev_open(stm32image_dev_con,
335 (uintptr_t)&stm32image_dev_info_spec,
336 &image_dev_handle);
337 assert(io_result == 0);
338 break;
339
340 default:
341 ERROR("Boot interface %d not supported\n",
342 boot_context->boot_interface_selected);
343 break;
344 }
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200345}
346
347/*
348 * Return an IO device handle and specification which can be used to access
349 * an image. Use this to enforce platform load policy.
350 */
351int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
352 uintptr_t *image_spec)
353{
354 int rc;
355 const struct plat_io_policy *policy;
356
357 assert(image_id < ARRAY_SIZE(policies));
358
359 policy = &policies[image_id];
360 rc = policy->check(policy->image_spec);
361 if (rc == 0) {
362 *image_spec = policy->image_spec;
363 *dev_handle = *(policy->dev_handle);
364 }
365
366 return rc;
367}