1 /* 2 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <string.h> 9 10 #include <platform_def.h> 11 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 <lib/mmio.h> 24 #include <lib/utils.h> 25 #include <plat/common/platform.h> 26 27 /* IO devices */ 28 static const io_dev_connector_t *dummy_dev_con; 29 static uintptr_t dummy_dev_handle; 30 static uintptr_t dummy_dev_spec; 31 32 static uintptr_t image_dev_handle; 33 static uintptr_t storage_dev_handle; 34 35 #if STM32MP_SDMMC || STM32MP_EMMC 36 static io_block_spec_t gpt_block_spec = { 37 .offset = 0, 38 .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */ 39 }; 40 41 static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); 42 43 static 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 56 static const io_dev_connector_t *mmc_dev_con; 57 #endif /* STM32MP_SDMMC || STM32MP_EMMC */ 58 59 #ifdef AARCH32_SP_OPTEE 60 static const struct stm32image_part_info optee_header_partition_spec = { 61 .name = OPTEE_HEADER_IMAGE_NAME, 62 .binary_type = OPTEE_HEADER_BINARY_TYPE, 63 }; 64 65 static const struct stm32image_part_info optee_pager_partition_spec = { 66 .name = OPTEE_PAGER_IMAGE_NAME, 67 .binary_type = OPTEE_PAGER_BINARY_TYPE, 68 }; 69 70 static 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 75 static const io_block_spec_t bl32_block_spec = { 76 .offset = BL32_BASE, 77 .length = STM32MP_BL32_SIZE 78 }; 79 #endif 80 81 static const io_block_spec_t bl2_block_spec = { 82 .offset = BL2_BASE, 83 .length = STM32MP_BL2_SIZE, 84 }; 85 86 static const struct stm32image_part_info bl33_partition_spec = { 87 .name = BL33_IMAGE_NAME, 88 .binary_type = BL33_BINARY_TYPE, 89 }; 90 91 enum { 92 IMG_IDX_BL33, 93 #ifdef AARCH32_SP_OPTEE 94 IMG_IDX_OPTEE_HEADER, 95 IMG_IDX_OPTEE_PAGER, 96 IMG_IDX_OPTEE_PAGED, 97 #endif 98 IMG_IDX_NUM 99 }; 100 101 static struct stm32image_device_info stm32image_dev_info_spec __unused = { 102 .lba_size = MMC_BLOCK_SIZE, 103 .part_info[IMG_IDX_BL33] = { 104 .name = BL33_IMAGE_NAME, 105 .binary_type = BL33_BINARY_TYPE, 106 }, 107 #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 121 }; 122 123 static io_block_spec_t stm32image_block_spec = { 124 .offset = 0, 125 .length = 0, 126 }; 127 128 static const io_dev_connector_t *stm32image_dev_con __unused; 129 130 static int open_dummy(const uintptr_t spec); 131 static int open_image(const uintptr_t spec); 132 static int open_storage(const uintptr_t spec); 133 134 struct plat_io_policy { 135 uintptr_t *dev_handle; 136 uintptr_t image_spec; 137 int (*check)(const uintptr_t spec); 138 }; 139 140 static 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 }, 146 #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 163 [BL32_IMAGE_ID] = { 164 .dev_handle = &dummy_dev_handle, 165 .image_spec = (uintptr_t)&bl32_block_spec, 166 .check = open_dummy 167 }, 168 #endif 169 [BL33_IMAGE_ID] = { 170 .dev_handle = &image_dev_handle, 171 .image_spec = (uintptr_t)&bl33_partition_spec, 172 .check = open_image 173 }, 174 #if STM32MP_SDMMC || STM32MP_EMMC 175 [GPT_IMAGE_ID] = { 176 .dev_handle = &storage_dev_handle, 177 .image_spec = (uintptr_t)&gpt_block_spec, 178 .check = open_storage 179 }, 180 #endif 181 [STM32_IMAGE_ID] = { 182 .dev_handle = &storage_dev_handle, 183 .image_spec = (uintptr_t)&stm32image_block_spec, 184 .check = open_storage 185 } 186 }; 187 188 static int open_dummy(const uintptr_t spec) 189 { 190 return io_dev_init(dummy_dev_handle, 0); 191 } 192 193 static int open_image(const uintptr_t spec) 194 { 195 return io_dev_init(image_dev_handle, 0); 196 } 197 198 static int open_storage(const uintptr_t spec) 199 { 200 return io_dev_init(storage_dev_handle, 0); 201 } 202 203 static 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 } 221 } 222 223 #if STM32MP_SDMMC || STM32MP_EMMC 224 static void boot_mmc(enum mmc_device_type mmc_dev_type, 225 uint16_t boot_interface_instance) 226 { 227 int io_result __unused; 228 uint8_t idx; 229 struct stm32image_part_info *part; 230 struct stm32_sdmmc2_params params; 231 struct mmc_device_info device_info; 232 const partition_entry_t *entry; 233 234 zeromem(&device_info, sizeof(struct mmc_device_info)); 235 zeromem(¶ms, sizeof(struct stm32_sdmmc2_params)); 236 237 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; 257 } 258 259 params.device_info = &device_info; 260 if (stm32_sdmmc2_mmc_init(¶ms) != 0) { 261 ERROR("SDMMC%u init failed\n", boot_interface_instance); 262 panic(); 263 } 264 265 /* 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 } 270 271 io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec, 272 &storage_dev_handle); 273 assert(io_result == 0); 274 275 partition_init(GPT_IMAGE_ID); 276 277 io_result = io_dev_close(storage_dev_handle); 278 assert(io_result == 0); 279 280 stm32image_dev_info_spec.device_size = 281 stm32_sdmmc2_mmc_get_device_size(); 282 283 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); 288 panic(); 289 } 290 291 part->part_offset = entry->start; 292 part->bkp_offset = 0U; 293 } 294 295 /* 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); 301 302 io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle); 303 assert(io_result == 0); 304 305 io_result = register_io_dev_stm32image(&stm32image_dev_con); 306 assert(io_result == 0); 307 308 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 } 313 #endif /* STM32MP_SDMMC || STM32MP_EMMC */ 314 315 void 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(); 320 321 print_boot_device(boot_context); 322 323 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 } 328 329 io_result = register_io_dev_dummy(&dummy_dev_con); 330 assert(io_result == 0); 331 332 io_result = io_dev_open(dummy_dev_con, dummy_dev_spec, 333 &dummy_dev_handle); 334 assert(io_result == 0); 335 336 switch (boot_context->boot_interface_selected) { 337 #if STM32MP_SDMMC 338 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: 339 dmbsy(); 340 boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance); 341 break; 342 #endif 343 #if STM32MP_EMMC 344 case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: 345 dmbsy(); 346 boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance); 347 break; 348 #endif 349 350 default: 351 ERROR("Boot interface %d not supported\n", 352 boot_context->boot_interface_selected); 353 break; 354 } 355 } 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 */ 361 int 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 } 378