Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 10 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 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 <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 Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 29 | #include <stm32mp1_private.h> |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 30 | |
| 31 | /* IO devices */ |
| 32 | static const io_dev_connector_t *dummy_dev_con; |
| 33 | static uintptr_t dummy_dev_handle; |
| 34 | static uintptr_t dummy_dev_spec; |
| 35 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 36 | static uintptr_t image_dev_handle; |
| 37 | |
| 38 | static io_block_spec_t gpt_block_spec = { |
| 39 | .offset = 0, |
| 40 | .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */ |
| 41 | }; |
| 42 | |
Yann Gautier | f9af3bc | 2018-11-09 15:57:18 +0100 | [diff] [blame] | 43 | static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 44 | |
| 45 | static 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 | |
| 58 | static uintptr_t storage_dev_handle; |
| 59 | static const io_dev_connector_t *mmc_dev_con; |
| 60 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 61 | static const io_block_spec_t bl32_block_spec = { |
| 62 | .offset = BL32_BASE, |
| 63 | .length = STM32MP1_BL32_SIZE |
| 64 | }; |
| 65 | |
| 66 | static const io_block_spec_t bl2_block_spec = { |
| 67 | .offset = BL2_BASE, |
| 68 | .length = STM32MP1_BL2_SIZE, |
| 69 | }; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 70 | |
| 71 | static const struct stm32image_part_info bl33_partition_spec = { |
| 72 | .name = BL33_IMAGE_NAME, |
| 73 | .binary_type = BL33_BINARY_TYPE, |
| 74 | }; |
| 75 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 76 | enum { |
| 77 | IMG_IDX_BL33, |
| 78 | IMG_IDX_NUM |
| 79 | }; |
| 80 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 81 | static 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 Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 89 | static io_block_spec_t stm32image_block_spec = { |
| 90 | .offset = 0, |
| 91 | .length = 0, |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 94 | static const io_dev_connector_t *stm32image_dev_con; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 95 | |
| 96 | static int open_dummy(const uintptr_t spec); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 97 | static int open_image(const uintptr_t spec); |
| 98 | static int open_storage(const uintptr_t spec); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 99 | |
| 100 | struct plat_io_policy { |
| 101 | uintptr_t *dev_handle; |
| 102 | uintptr_t image_spec; |
| 103 | int (*check)(const uintptr_t spec); |
| 104 | }; |
| 105 | |
| 106 | static 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 Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 117 | [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 Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static int open_dummy(const uintptr_t spec) |
| 135 | { |
| 136 | return io_dev_init(dummy_dev_handle, 0); |
| 137 | } |
| 138 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 139 | static int open_image(const uintptr_t spec) |
| 140 | { |
| 141 | return io_dev_init(image_dev_handle, 0); |
| 142 | } |
| 143 | |
| 144 | static int open_storage(const uintptr_t spec) |
| 145 | { |
| 146 | return io_dev_init(storage_dev_handle, 0); |
| 147 | } |
| 148 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 149 | static 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 Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void stm32mp1_io_setup(void) |
| 170 | { |
| 171 | int io_result __unused; |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 172 | uint8_t idx; |
| 173 | struct stm32image_part_info *part; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 174 | struct stm32_sdmmc2_params params; |
| 175 | struct mmc_device_info device_info; |
| 176 | uintptr_t mmc_default_instance; |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 177 | const partition_entry_t *entry; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 178 | boot_api_context_t *boot_context = |
| 179 | (boot_api_context_t *)stm32mp1_get_boot_ctx_address(); |
| 180 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 181 | 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 Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 195 | |
| 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 Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 199 | dmbsy(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 200 | |
| 201 | memset(¶ms, 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 Gautier | 03f0468 | 2018-11-29 15:44:04 +0100 | [diff] [blame] | 229 | if (stm32_sdmmc2_mmc_init(¶ms) != 0) { |
| 230 | ERROR("SDMMC%u init failed\n", |
| 231 | boot_context->boot_interface_instance); |
| 232 | panic(); |
| 233 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 234 | |
| 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 Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 253 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 254 | 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 Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 266 | |
| 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 Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 291 | } |
| 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 | */ |
| 297 | int 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 | } |