Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <common/debug.h> |
| 10 | #include <drivers/mmc.h> |
| 11 | #include <tools_share/firmware_image_package.h> |
| 12 | #include <drivers/io/io_block.h> |
| 13 | #include <drivers/io/io_driver.h> |
| 14 | #include <drivers/io/io_fip.h> |
| 15 | #include <drivers/io/io_memmap.h> |
| 16 | #include <drivers/io/io_storage.h> |
| 17 | #include <lib/mmio.h> |
| 18 | #include <drivers/partition/partition.h> |
| 19 | #include <lib/semihosting.h> |
| 20 | #include <string.h> |
| 21 | #include <lib/utils.h> |
| 22 | #include <common/tbbr/tbbr_img_def.h> |
| 23 | #include "platform_def.h" |
Hadi Asyrafi | 309ac01 | 2019-08-01 14:48:39 +0800 | [diff] [blame^] | 24 | #include "stratix10_private.h" |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 25 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 26 | #define STRATIX10_FIP_BASE (0) |
| 27 | #define STRATIX10_FIP_MAX_SIZE (0x1000000) |
| 28 | #define STRATIX10_MMC_DATA_BASE (0xffe3c000) |
| 29 | #define STRATIX10_MMC_DATA_SIZE (0x2000) |
| 30 | #define STRATIX10_QSPI_DATA_BASE (0x3C00000) |
| 31 | #define STRATIX10_QSPI_DATA_SIZE (0x1000000) |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 32 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 33 | |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 34 | static const io_dev_connector_t *fip_dev_con; |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 35 | static const io_dev_connector_t *boot_dev_con; |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 36 | |
| 37 | static uintptr_t fip_dev_handle; |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 38 | static uintptr_t boot_dev_handle; |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 39 | |
| 40 | static const io_uuid_spec_t bl2_uuid_spec = { |
| 41 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 42 | }; |
| 43 | |
| 44 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 45 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 46 | }; |
| 47 | |
| 48 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 49 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 50 | }; |
| 51 | |
| 52 | uintptr_t a2_lba_offset; |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 53 | const char a2[] = {0xa2, 0x0}; |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 54 | |
| 55 | static const io_block_spec_t gpt_block_spec = { |
| 56 | .offset = 0, |
| 57 | .length = MMC_BLOCK_SIZE |
| 58 | }; |
| 59 | |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 60 | static int check_fip(const uintptr_t spec); |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 61 | static int check_dev(const uintptr_t spec); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 62 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 63 | static io_block_dev_spec_t boot_dev_spec; |
| 64 | static int (*register_io_dev)(const io_dev_connector_t **); |
| 65 | |
| 66 | static io_block_spec_t fip_spec = { |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 67 | .offset = STRATIX10_FIP_BASE, |
| 68 | .length = STRATIX10_FIP_MAX_SIZE, |
| 69 | }; |
| 70 | |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 71 | struct plat_io_policy { |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 72 | uintptr_t *dev_handle; |
| 73 | uintptr_t image_spec; |
| 74 | int (*check)(const uintptr_t spec); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static const struct plat_io_policy policies[] = { |
| 78 | [FIP_IMAGE_ID] = { |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 79 | &boot_dev_handle, |
| 80 | (uintptr_t)&fip_spec, |
| 81 | check_dev |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 82 | }, |
| 83 | [BL2_IMAGE_ID] = { |
| 84 | &fip_dev_handle, |
| 85 | (uintptr_t)&bl2_uuid_spec, |
| 86 | check_fip |
| 87 | }, |
| 88 | [BL31_IMAGE_ID] = { |
| 89 | &fip_dev_handle, |
| 90 | (uintptr_t)&bl31_uuid_spec, |
| 91 | check_fip |
| 92 | }, |
| 93 | [BL33_IMAGE_ID] = { |
| 94 | &fip_dev_handle, |
| 95 | (uintptr_t) &bl33_uuid_spec, |
| 96 | check_fip |
| 97 | }, |
| 98 | [GPT_IMAGE_ID] = { |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 99 | &boot_dev_handle, |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 100 | (uintptr_t) &gpt_block_spec, |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 101 | check_dev |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 102 | }, |
| 103 | }; |
| 104 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 105 | static int check_dev(const uintptr_t spec) |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 106 | { |
| 107 | int result; |
| 108 | uintptr_t local_handle; |
| 109 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 110 | result = io_dev_init(boot_dev_handle, (uintptr_t)NULL); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 111 | if (result == 0) { |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 112 | result = io_open(boot_dev_handle, spec, &local_handle); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 113 | if (result == 0) |
| 114 | io_close(local_handle); |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | static int check_fip(const uintptr_t spec) |
| 120 | { |
| 121 | int result; |
| 122 | uintptr_t local_image_handle; |
| 123 | |
| 124 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
| 125 | if (result == 0) { |
| 126 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 127 | if (result == 0) |
| 128 | io_close(local_image_handle); |
| 129 | } |
| 130 | return result; |
| 131 | } |
| 132 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 133 | void stratix10_io_setup(int boot_source) |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 134 | { |
| 135 | int result; |
| 136 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 137 | switch (boot_source) { |
| 138 | case BOOT_SOURCE_SDMMC: |
| 139 | register_io_dev = ®ister_io_dev_block; |
| 140 | boot_dev_spec.buffer.offset = STRATIX10_MMC_DATA_BASE; |
| 141 | boot_dev_spec.buffer.length = MMC_BLOCK_SIZE; |
| 142 | boot_dev_spec.ops.read = mmc_read_blocks; |
| 143 | boot_dev_spec.ops.write = mmc_write_blocks; |
| 144 | boot_dev_spec.block_size = MMC_BLOCK_SIZE; |
| 145 | break; |
| 146 | |
| 147 | case BOOT_SOURCE_QSPI: |
| 148 | register_io_dev = ®ister_io_dev_memmap; |
| 149 | fip_spec.offset = fip_spec.offset + STRATIX10_QSPI_DATA_BASE; |
| 150 | break; |
| 151 | |
| 152 | default: |
| 153 | ERROR("Unsupported boot source\n"); |
| 154 | panic(); |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | result = (*register_io_dev)(&boot_dev_con); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 159 | assert(result == 0); |
| 160 | |
| 161 | result = register_io_dev_fip(&fip_dev_con); |
| 162 | assert(result == 0); |
| 163 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 164 | result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec, |
| 165 | &boot_dev_handle); |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 166 | assert(result == 0); |
| 167 | |
| 168 | result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle); |
| 169 | assert(result == 0); |
| 170 | |
Muhammad Hadi Asyrafi Abdul Halim | 2444bfa | 2019-03-08 19:02:33 +0800 | [diff] [blame] | 171 | if (boot_source == BOOT_SOURCE_SDMMC) { |
| 172 | partition_init(GPT_IMAGE_ID); |
| 173 | fip_spec.offset = get_partition_entry(a2)->start; |
| 174 | } |
Loh Tien Hock | 59400a4 | 2019-02-04 16:17:24 +0800 | [diff] [blame] | 175 | |
| 176 | (void)result; |
| 177 | } |
| 178 | |
| 179 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 180 | uintptr_t *image_spec) |
| 181 | { |
| 182 | int result; |
| 183 | const struct plat_io_policy *policy; |
| 184 | |
| 185 | assert(image_id < ARRAY_SIZE(policies)); |
| 186 | |
| 187 | policy = &policies[image_id]; |
| 188 | result = policy->check(policy->image_spec); |
| 189 | assert(result == 0); |
| 190 | |
| 191 | *image_spec = policy->image_spec; |
| 192 | *dev_handle = *(policy->dev_handle); |
| 193 | |
| 194 | return result; |
| 195 | } |