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 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 28 | /* IO devices */ |
| 29 | static const io_dev_connector_t *dummy_dev_con; |
| 30 | static uintptr_t dummy_dev_handle; |
| 31 | static uintptr_t dummy_dev_spec; |
| 32 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 33 | static uintptr_t image_dev_handle; |
| 34 | |
| 35 | static io_block_spec_t gpt_block_spec = { |
| 36 | .offset = 0, |
| 37 | .length = 34 * MMC_BLOCK_SIZE, /* Size of GPT table */ |
| 38 | }; |
| 39 | |
Yann Gautier | f9af3bc | 2018-11-09 15:57:18 +0100 | [diff] [blame] | 40 | static uint32_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 41 | |
| 42 | static const io_block_dev_spec_t mmc_block_dev_spec = { |
| 43 | /* It's used as temp buffer in block driver */ |
| 44 | .buffer = { |
| 45 | .offset = (size_t)&block_buffer, |
| 46 | .length = MMC_BLOCK_SIZE, |
| 47 | }, |
| 48 | .ops = { |
| 49 | .read = mmc_read_blocks, |
| 50 | .write = NULL, |
| 51 | }, |
| 52 | .block_size = MMC_BLOCK_SIZE, |
| 53 | }; |
| 54 | |
| 55 | static uintptr_t storage_dev_handle; |
| 56 | static const io_dev_connector_t *mmc_dev_con; |
| 57 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 58 | static const io_block_spec_t bl32_block_spec = { |
| 59 | .offset = BL32_BASE, |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 60 | .length = STM32MP_BL32_SIZE |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static const io_block_spec_t bl2_block_spec = { |
| 64 | .offset = BL2_BASE, |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 65 | .length = STM32MP_BL2_SIZE, |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 66 | }; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 67 | |
| 68 | static const struct stm32image_part_info bl33_partition_spec = { |
| 69 | .name = BL33_IMAGE_NAME, |
| 70 | .binary_type = BL33_BINARY_TYPE, |
| 71 | }; |
| 72 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 73 | enum { |
| 74 | IMG_IDX_BL33, |
| 75 | IMG_IDX_NUM |
| 76 | }; |
| 77 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 78 | static struct stm32image_device_info stm32image_dev_info_spec = { |
| 79 | .lba_size = MMC_BLOCK_SIZE, |
| 80 | .part_info[IMG_IDX_BL33] = { |
| 81 | .name = BL33_IMAGE_NAME, |
| 82 | .binary_type = BL33_BINARY_TYPE, |
| 83 | }, |
| 84 | }; |
| 85 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 86 | static io_block_spec_t stm32image_block_spec = { |
| 87 | .offset = 0, |
| 88 | .length = 0, |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 91 | static const io_dev_connector_t *stm32image_dev_con; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 92 | |
| 93 | static int open_dummy(const uintptr_t spec); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 94 | static int open_image(const uintptr_t spec); |
| 95 | static int open_storage(const uintptr_t spec); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 96 | |
| 97 | struct plat_io_policy { |
| 98 | uintptr_t *dev_handle; |
| 99 | uintptr_t image_spec; |
| 100 | int (*check)(const uintptr_t spec); |
| 101 | }; |
| 102 | |
| 103 | static const struct plat_io_policy policies[] = { |
| 104 | [BL2_IMAGE_ID] = { |
| 105 | .dev_handle = &dummy_dev_handle, |
| 106 | .image_spec = (uintptr_t)&bl2_block_spec, |
| 107 | .check = open_dummy |
| 108 | }, |
| 109 | [BL32_IMAGE_ID] = { |
| 110 | .dev_handle = &dummy_dev_handle, |
| 111 | .image_spec = (uintptr_t)&bl32_block_spec, |
| 112 | .check = open_dummy |
| 113 | }, |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 114 | [BL33_IMAGE_ID] = { |
| 115 | .dev_handle = &image_dev_handle, |
| 116 | .image_spec = (uintptr_t)&bl33_partition_spec, |
| 117 | .check = open_image |
| 118 | }, |
| 119 | [GPT_IMAGE_ID] = { |
| 120 | .dev_handle = &storage_dev_handle, |
| 121 | .image_spec = (uintptr_t)&gpt_block_spec, |
| 122 | .check = open_storage |
| 123 | }, |
| 124 | [STM32_IMAGE_ID] = { |
| 125 | .dev_handle = &storage_dev_handle, |
| 126 | .image_spec = (uintptr_t)&stm32image_block_spec, |
| 127 | .check = open_storage |
| 128 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static int open_dummy(const uintptr_t spec) |
| 132 | { |
| 133 | return io_dev_init(dummy_dev_handle, 0); |
| 134 | } |
| 135 | |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 136 | static int open_image(const uintptr_t spec) |
| 137 | { |
| 138 | return io_dev_init(image_dev_handle, 0); |
| 139 | } |
| 140 | |
| 141 | static int open_storage(const uintptr_t spec) |
| 142 | { |
| 143 | return io_dev_init(storage_dev_handle, 0); |
| 144 | } |
| 145 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 146 | static void print_boot_device(boot_api_context_t *boot_context) |
| 147 | { |
| 148 | switch (boot_context->boot_interface_selected) { |
| 149 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 150 | INFO("Using SDMMC\n"); |
| 151 | break; |
| 152 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
| 153 | INFO("Using EMMC\n"); |
| 154 | break; |
| 155 | default: |
| 156 | ERROR("Boot interface not found\n"); |
| 157 | panic(); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | if (boot_context->boot_interface_instance != 0U) { |
| 162 | INFO(" Instance %d\n", boot_context->boot_interface_instance); |
| 163 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 166 | void stm32mp_io_setup(void) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 167 | { |
| 168 | int io_result __unused; |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 169 | uint8_t idx; |
| 170 | struct stm32image_part_info *part; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 171 | struct stm32_sdmmc2_params params; |
| 172 | struct mmc_device_info device_info; |
| 173 | uintptr_t mmc_default_instance; |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 174 | const partition_entry_t *entry; |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 175 | boot_api_context_t *boot_context = |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 176 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 177 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 178 | print_boot_device(boot_context); |
| 179 | |
| 180 | if ((boot_context->boot_partition_used_toboot == 1U) || |
| 181 | (boot_context->boot_partition_used_toboot == 2U)) { |
| 182 | INFO("Boot used partition fsbl%d\n", |
| 183 | boot_context->boot_partition_used_toboot); |
| 184 | } |
| 185 | |
| 186 | io_result = register_io_dev_dummy(&dummy_dev_con); |
| 187 | assert(io_result == 0); |
| 188 | |
| 189 | io_result = io_dev_open(dummy_dev_con, dummy_dev_spec, |
| 190 | &dummy_dev_handle); |
| 191 | assert(io_result == 0); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 192 | |
| 193 | switch (boot_context->boot_interface_selected) { |
| 194 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD: |
| 195 | case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC: |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 196 | dmbsy(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 197 | |
| 198 | memset(¶ms, 0, sizeof(struct stm32_sdmmc2_params)); |
| 199 | |
| 200 | if (boot_context->boot_interface_selected == |
| 201 | BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC) { |
| 202 | device_info.mmc_dev_type = MMC_IS_EMMC; |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 203 | mmc_default_instance = STM32MP_SDMMC2_BASE; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 204 | } else { |
| 205 | device_info.mmc_dev_type = MMC_IS_SD; |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 206 | mmc_default_instance = STM32MP_SDMMC1_BASE; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | switch (boot_context->boot_interface_instance) { |
| 210 | case 1: |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 211 | params.reg_base = STM32MP_SDMMC1_BASE; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 212 | break; |
| 213 | case 2: |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 214 | params.reg_base = STM32MP_SDMMC2_BASE; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 215 | break; |
| 216 | case 3: |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame^] | 217 | params.reg_base = STM32MP_SDMMC3_BASE; |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 218 | break; |
| 219 | default: |
| 220 | WARN("SDMMC instance not found, using default\n"); |
| 221 | params.reg_base = mmc_default_instance; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | params.device_info = &device_info; |
Yann Gautier | 03f0468 | 2018-11-29 15:44:04 +0100 | [diff] [blame] | 226 | if (stm32_sdmmc2_mmc_init(¶ms) != 0) { |
| 227 | ERROR("SDMMC%u init failed\n", |
| 228 | boot_context->boot_interface_instance); |
| 229 | panic(); |
| 230 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 231 | |
| 232 | /* Open MMC as a block device to read GPT table */ |
| 233 | io_result = register_io_dev_block(&mmc_dev_con); |
| 234 | if (io_result != 0) { |
| 235 | panic(); |
| 236 | } |
| 237 | |
| 238 | io_result = io_dev_open(mmc_dev_con, |
| 239 | (uintptr_t)&mmc_block_dev_spec, |
| 240 | &storage_dev_handle); |
| 241 | assert(io_result == 0); |
| 242 | |
| 243 | partition_init(GPT_IMAGE_ID); |
| 244 | |
| 245 | io_result = io_dev_close(storage_dev_handle); |
| 246 | assert(io_result == 0); |
| 247 | |
| 248 | stm32image_dev_info_spec.device_size = |
| 249 | stm32_sdmmc2_mmc_get_device_size(); |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 250 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 251 | for (idx = 0U; idx < IMG_IDX_NUM; idx++) { |
| 252 | part = &stm32image_dev_info_spec.part_info[idx]; |
| 253 | entry = get_partition_entry(part->name); |
| 254 | if (entry == NULL) { |
| 255 | ERROR("Partition %s not found\n", |
| 256 | part->name); |
| 257 | panic(); |
| 258 | } |
| 259 | |
| 260 | part->part_offset = entry->start; |
| 261 | part->bkp_offset = 0U; |
| 262 | } |
Yann Gautier | 8244e1d | 2018-10-15 09:36:58 +0200 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * Re-open MMC with io_mmc, for better perfs compared to |
| 266 | * io_block. |
| 267 | */ |
| 268 | io_result = register_io_dev_mmc(&mmc_dev_con); |
| 269 | assert(io_result == 0); |
| 270 | |
| 271 | io_result = io_dev_open(mmc_dev_con, 0, &storage_dev_handle); |
| 272 | assert(io_result == 0); |
| 273 | |
| 274 | io_result = register_io_dev_stm32image(&stm32image_dev_con); |
| 275 | assert(io_result == 0); |
| 276 | |
| 277 | io_result = io_dev_open(stm32image_dev_con, |
| 278 | (uintptr_t)&stm32image_dev_info_spec, |
| 279 | &image_dev_handle); |
| 280 | assert(io_result == 0); |
| 281 | break; |
| 282 | |
| 283 | default: |
| 284 | ERROR("Boot interface %d not supported\n", |
| 285 | boot_context->boot_interface_selected); |
| 286 | break; |
| 287 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Return an IO device handle and specification which can be used to access |
| 292 | * an image. Use this to enforce platform load policy. |
| 293 | */ |
| 294 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 295 | uintptr_t *image_spec) |
| 296 | { |
| 297 | int rc; |
| 298 | const struct plat_io_policy *policy; |
| 299 | |
| 300 | assert(image_id < ARRAY_SIZE(policies)); |
| 301 | |
| 302 | policy = &policies[image_id]; |
| 303 | rc = policy->check(policy->image_spec); |
| 304 | if (rc == 0) { |
| 305 | *image_spec = policy->image_spec; |
| 306 | *dev_handle = *(policy->dev_handle); |
| 307 | } |
| 308 | |
| 309 | return rc; |
| 310 | } |