socfpga_storage.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2019-2023, Intel Corporation. All rights reserved.
  4. * Copyright (c) 2024, Altera Corporation. All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include <arch_helpers.h>
  9. #include <assert.h>
  10. #include <common/debug.h>
  11. #include <common/tbbr/tbbr_img_def.h>
  12. #include <drivers/cadence/cdns_nand.h>
  13. #include <drivers/cadence/cdns_sdmmc.h>
  14. #include <drivers/io/io_block.h>
  15. #include <drivers/io/io_driver.h>
  16. #include <drivers/io/io_fip.h>
  17. #include <drivers/io/io_memmap.h>
  18. #include <drivers/io/io_mtd.h>
  19. #include <drivers/io/io_storage.h>
  20. #include <drivers/mmc.h>
  21. #include <drivers/partition/partition.h>
  22. #include <lib/mmio.h>
  23. #include <tools_share/firmware_image_package.h>
  24. #include "drivers/sdmmc/sdmmc.h"
  25. #include "socfpga_private.h"
  26. #include "socfpga_ros.h"
  27. static const io_dev_connector_t *fip_dev_con;
  28. static const io_dev_connector_t *boot_dev_con;
  29. static io_mtd_dev_spec_t nand_dev_spec;
  30. static uintptr_t fip_dev_handle;
  31. static uintptr_t boot_dev_handle;
  32. static const io_uuid_spec_t bl2_uuid_spec = {
  33. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  34. };
  35. static const io_uuid_spec_t bl31_uuid_spec = {
  36. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  37. };
  38. static const io_uuid_spec_t bl33_uuid_spec = {
  39. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  40. };
  41. # if ARM_LINUX_KERNEL_AS_BL33 != 0
  42. static const io_uuid_spec_t nt_fw_config_uuid_spec = {
  43. .uuid = UUID_NT_FW_CONFIG,
  44. };
  45. # endif
  46. uintptr_t a2_lba_offset;
  47. const char a2[] = {0xa2, 0x0};
  48. static const io_block_spec_t gpt_block_spec = {
  49. .offset = 0,
  50. .length = MMC_BLOCK_SIZE
  51. };
  52. static int check_fip(const uintptr_t spec);
  53. static int check_dev(const uintptr_t spec);
  54. static io_block_dev_spec_t boot_dev_spec;
  55. static int (*register_io_dev)(const io_dev_connector_t **);
  56. static io_block_spec_t fip_spec = {
  57. .offset = PLAT_FIP_BASE,
  58. .length = PLAT_FIP_MAX_SIZE,
  59. };
  60. struct plat_io_policy {
  61. uintptr_t *dev_handle;
  62. uintptr_t image_spec;
  63. int (*check)(const uintptr_t spec);
  64. };
  65. static const struct plat_io_policy policies[] = {
  66. [FIP_IMAGE_ID] = {
  67. &boot_dev_handle,
  68. (uintptr_t)&fip_spec,
  69. check_dev
  70. },
  71. [BL2_IMAGE_ID] = {
  72. &fip_dev_handle,
  73. (uintptr_t)&bl2_uuid_spec,
  74. check_fip
  75. },
  76. [BL31_IMAGE_ID] = {
  77. &fip_dev_handle,
  78. (uintptr_t)&bl31_uuid_spec,
  79. check_fip
  80. },
  81. [BL33_IMAGE_ID] = {
  82. &fip_dev_handle,
  83. (uintptr_t) &bl33_uuid_spec,
  84. check_fip
  85. },
  86. # if ARM_LINUX_KERNEL_AS_BL33 != 0
  87. [NT_FW_CONFIG_ID] = {
  88. &fip_dev_handle,
  89. (uintptr_t)&nt_fw_config_uuid_spec,
  90. check_fip
  91. },
  92. # endif
  93. [GPT_IMAGE_ID] = {
  94. &boot_dev_handle,
  95. (uintptr_t) &gpt_block_spec,
  96. check_dev
  97. },
  98. };
  99. static int check_dev(const uintptr_t spec)
  100. {
  101. int result;
  102. uintptr_t local_handle;
  103. result = io_dev_init(boot_dev_handle, (uintptr_t)NULL);
  104. if (result == 0) {
  105. result = io_open(boot_dev_handle, spec, &local_handle);
  106. if (result == 0)
  107. io_close(local_handle);
  108. }
  109. return result;
  110. }
  111. static int check_fip(const uintptr_t spec)
  112. {
  113. int result;
  114. uintptr_t local_image_handle;
  115. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  116. if (result == 0) {
  117. result = io_open(fip_dev_handle, spec, &local_image_handle);
  118. if (result == 0)
  119. io_close(local_image_handle);
  120. }
  121. return result;
  122. }
  123. void socfpga_io_setup(int boot_source, unsigned long offset)
  124. {
  125. int result;
  126. fip_spec.offset = offset;
  127. switch (boot_source) {
  128. case BOOT_SOURCE_SDMMC:
  129. register_io_dev = &register_io_dev_block;
  130. boot_dev_spec.buffer.offset = PLAT_MMC_DATA_BASE;
  131. boot_dev_spec.buffer.length = SOCFPGA_MMC_BLOCK_SIZE;
  132. boot_dev_spec.ops.read = SDMMC_READ_BLOCKS;
  133. boot_dev_spec.ops.write = SDMMC_WRITE_BLOCKS;
  134. boot_dev_spec.block_size = MMC_BLOCK_SIZE;
  135. break;
  136. case BOOT_SOURCE_QSPI:
  137. register_io_dev = &register_io_dev_memmap;
  138. break;
  139. #if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
  140. case BOOT_SOURCE_NAND:
  141. register_io_dev = &register_io_dev_mtd;
  142. nand_dev_spec.ops.init = cdns_nand_init_mtd;
  143. nand_dev_spec.ops.read = cdns_nand_read;
  144. nand_dev_spec.ops.write = NULL;
  145. break;
  146. #endif
  147. default:
  148. ERROR("Unsupported boot source\n");
  149. panic();
  150. break;
  151. }
  152. result = (*register_io_dev)(&boot_dev_con);
  153. assert(result == 0);
  154. result = register_io_dev_fip(&fip_dev_con);
  155. assert(result == 0);
  156. if (boot_source == BOOT_SOURCE_NAND) {
  157. result = io_dev_open(boot_dev_con, (uintptr_t)&nand_dev_spec,
  158. &boot_dev_handle);
  159. } else {
  160. result = io_dev_open(boot_dev_con, (uintptr_t)&boot_dev_spec,
  161. &boot_dev_handle);
  162. }
  163. assert(result == 0);
  164. result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
  165. assert(result == 0);
  166. if (boot_source == BOOT_SOURCE_SDMMC) {
  167. partition_init(GPT_IMAGE_ID);
  168. fip_spec.offset = get_partition_entry(a2)->start;
  169. }
  170. (void)result;
  171. }
  172. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  173. uintptr_t *image_spec)
  174. {
  175. int result;
  176. const struct plat_io_policy *policy;
  177. assert(image_id < ARRAY_SIZE(policies));
  178. policy = &policies[image_id];
  179. result = policy->check(policy->image_spec);
  180. assert(result == 0);
  181. *image_spec = policy->image_spec;
  182. *dev_handle = *(policy->dev_handle);
  183. return result;
  184. }