uniphier_io_storage.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <platform_def.h>
  10. #include <drivers/io/io_block.h>
  11. #include <drivers/io/io_driver.h>
  12. #include <drivers/io/io_fip.h>
  13. #include <drivers/io/io_memmap.h>
  14. #include <lib/utils_def.h>
  15. #include <lib/xlat_tables/xlat_tables_v2.h>
  16. #include <tools_share/firmware_image_package.h>
  17. #include "uniphier.h"
  18. #define UNIPHIER_ROM_REGION_BASE 0x00000000ULL
  19. #define UNIPHIER_ROM_REGION_SIZE 0x04000000ULL
  20. #define UNIPHIER_OCM_REGION_SIZE 0x00040000ULL
  21. #define UNIPHIER_BLOCK_BUF_OFFSET 0x03000000UL
  22. #define UNIPHIER_BLOCK_BUF_SIZE 0x00800000UL
  23. static const io_dev_connector_t *uniphier_fip_dev_con;
  24. static uintptr_t uniphier_fip_dev_handle;
  25. static const io_dev_connector_t *uniphier_backend_dev_con;
  26. static uintptr_t uniphier_backend_dev_handle;
  27. static io_block_spec_t uniphier_fip_spec = {
  28. /* .offset will be set by the io_setup func */
  29. .length = 0x00200000,
  30. };
  31. static const io_uuid_spec_t uniphier_bl2_spec = {
  32. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  33. };
  34. static const io_uuid_spec_t uniphier_scp_spec = {
  35. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  36. };
  37. static const io_uuid_spec_t uniphier_bl31_spec = {
  38. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  39. };
  40. static const io_uuid_spec_t uniphier_bl32_spec = {
  41. .uuid = UUID_SECURE_PAYLOAD_BL32,
  42. };
  43. static const io_uuid_spec_t uniphier_bl33_spec = {
  44. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  45. };
  46. #if TRUSTED_BOARD_BOOT
  47. static const io_uuid_spec_t uniphier_tb_fw_cert_spec = {
  48. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  49. };
  50. static const io_uuid_spec_t uniphier_trusted_key_cert_spec = {
  51. .uuid = UUID_TRUSTED_KEY_CERT,
  52. };
  53. static const io_uuid_spec_t uniphier_scp_fw_key_cert_spec = {
  54. .uuid = UUID_SCP_FW_KEY_CERT,
  55. };
  56. static const io_uuid_spec_t uniphier_soc_fw_key_cert_spec = {
  57. .uuid = UUID_SOC_FW_KEY_CERT,
  58. };
  59. static const io_uuid_spec_t uniphier_tos_fw_key_cert_spec = {
  60. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  61. };
  62. static const io_uuid_spec_t uniphier_nt_fw_key_cert_spec = {
  63. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  64. };
  65. static const io_uuid_spec_t uniphier_scp_fw_cert_spec = {
  66. .uuid = UUID_SCP_FW_CONTENT_CERT,
  67. };
  68. static const io_uuid_spec_t uniphier_soc_fw_cert_spec = {
  69. .uuid = UUID_SOC_FW_CONTENT_CERT,
  70. };
  71. static const io_uuid_spec_t uniphier_tos_fw_cert_spec = {
  72. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  73. };
  74. static const io_uuid_spec_t uniphier_nt_fw_cert_spec = {
  75. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  76. };
  77. #endif /* TRUSTED_BOARD_BOOT */
  78. struct uniphier_io_policy {
  79. uintptr_t *dev_handle;
  80. uintptr_t image_spec;
  81. uintptr_t init_params;
  82. };
  83. static const struct uniphier_io_policy uniphier_io_policies[] = {
  84. [FIP_IMAGE_ID] = {
  85. .dev_handle = &uniphier_backend_dev_handle,
  86. .image_spec = (uintptr_t)&uniphier_fip_spec,
  87. },
  88. [BL2_IMAGE_ID] = {
  89. .dev_handle = &uniphier_fip_dev_handle,
  90. .image_spec = (uintptr_t)&uniphier_bl2_spec,
  91. .init_params = FIP_IMAGE_ID,
  92. },
  93. [SCP_BL2_IMAGE_ID] = {
  94. .dev_handle = &uniphier_fip_dev_handle,
  95. .image_spec = (uintptr_t)&uniphier_scp_spec,
  96. .init_params = FIP_IMAGE_ID,
  97. },
  98. [BL31_IMAGE_ID] = {
  99. .dev_handle = &uniphier_fip_dev_handle,
  100. .image_spec = (uintptr_t)&uniphier_bl31_spec,
  101. .init_params = FIP_IMAGE_ID,
  102. },
  103. [BL32_IMAGE_ID] = {
  104. .dev_handle = &uniphier_fip_dev_handle,
  105. .image_spec = (uintptr_t)&uniphier_bl32_spec,
  106. .init_params = FIP_IMAGE_ID,
  107. },
  108. [BL33_IMAGE_ID] = {
  109. .dev_handle = &uniphier_fip_dev_handle,
  110. .image_spec = (uintptr_t)&uniphier_bl33_spec,
  111. .init_params = FIP_IMAGE_ID,
  112. },
  113. #if TRUSTED_BOARD_BOOT
  114. [TRUSTED_BOOT_FW_CERT_ID] = {
  115. .dev_handle = &uniphier_fip_dev_handle,
  116. .image_spec = (uintptr_t)&uniphier_tb_fw_cert_spec,
  117. .init_params = FIP_IMAGE_ID,
  118. },
  119. [TRUSTED_KEY_CERT_ID] = {
  120. .dev_handle = &uniphier_fip_dev_handle,
  121. .image_spec = (uintptr_t)&uniphier_trusted_key_cert_spec,
  122. .init_params = FIP_IMAGE_ID,
  123. },
  124. [SCP_FW_KEY_CERT_ID] = {
  125. .dev_handle = &uniphier_fip_dev_handle,
  126. .image_spec = (uintptr_t)&uniphier_scp_fw_key_cert_spec,
  127. .init_params = FIP_IMAGE_ID,
  128. },
  129. [SOC_FW_KEY_CERT_ID] = {
  130. .dev_handle = &uniphier_fip_dev_handle,
  131. .image_spec = (uintptr_t)&uniphier_soc_fw_key_cert_spec,
  132. .init_params = FIP_IMAGE_ID,
  133. },
  134. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  135. .dev_handle = &uniphier_fip_dev_handle,
  136. .image_spec = (uintptr_t)&uniphier_tos_fw_key_cert_spec,
  137. .init_params = FIP_IMAGE_ID,
  138. },
  139. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  140. .dev_handle = &uniphier_fip_dev_handle,
  141. .image_spec = (uintptr_t)&uniphier_nt_fw_key_cert_spec,
  142. .init_params = FIP_IMAGE_ID,
  143. },
  144. [SCP_FW_CONTENT_CERT_ID] = {
  145. .dev_handle = &uniphier_fip_dev_handle,
  146. .image_spec = (uintptr_t)&uniphier_scp_fw_cert_spec,
  147. .init_params = FIP_IMAGE_ID,
  148. },
  149. [SOC_FW_CONTENT_CERT_ID] = {
  150. .dev_handle = &uniphier_fip_dev_handle,
  151. .image_spec = (uintptr_t)&uniphier_soc_fw_cert_spec,
  152. .init_params = FIP_IMAGE_ID,
  153. },
  154. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  155. .dev_handle = &uniphier_fip_dev_handle,
  156. .image_spec = (uintptr_t)&uniphier_tos_fw_cert_spec,
  157. .init_params = FIP_IMAGE_ID,
  158. },
  159. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  160. .dev_handle = &uniphier_fip_dev_handle,
  161. .image_spec = (uintptr_t)&uniphier_nt_fw_cert_spec,
  162. .init_params = FIP_IMAGE_ID,
  163. },
  164. #endif
  165. };
  166. static int uniphier_io_block_setup(size_t fip_offset,
  167. struct io_block_dev_spec *block_dev_spec,
  168. size_t buffer_offset)
  169. {
  170. int ret;
  171. uniphier_fip_spec.offset = fip_offset;
  172. block_dev_spec->buffer.offset = buffer_offset;
  173. block_dev_spec->buffer.length = UNIPHIER_BLOCK_BUF_SIZE;
  174. ret = mmap_add_dynamic_region(block_dev_spec->buffer.offset,
  175. block_dev_spec->buffer.offset,
  176. block_dev_spec->buffer.length,
  177. MT_MEMORY | MT_RW | MT_NS);
  178. if (ret)
  179. return ret;
  180. ret = register_io_dev_block(&uniphier_backend_dev_con);
  181. if (ret)
  182. return ret;
  183. return io_dev_open(uniphier_backend_dev_con, (uintptr_t)block_dev_spec,
  184. &uniphier_backend_dev_handle);
  185. }
  186. static int uniphier_io_memmap_setup(size_t fip_offset)
  187. {
  188. int ret;
  189. uniphier_fip_spec.offset = fip_offset;
  190. ret = mmap_add_dynamic_region(fip_offset, fip_offset,
  191. uniphier_fip_spec.length,
  192. MT_RO_DATA | MT_SECURE);
  193. if (ret)
  194. return ret;
  195. ret = register_io_dev_memmap(&uniphier_backend_dev_con);
  196. if (ret)
  197. return ret;
  198. return io_dev_open(uniphier_backend_dev_con, 0,
  199. &uniphier_backend_dev_handle);
  200. }
  201. static int uniphier_io_fip_setup(void)
  202. {
  203. int ret;
  204. ret = register_io_dev_fip(&uniphier_fip_dev_con);
  205. if (ret)
  206. return ret;
  207. return io_dev_open(uniphier_fip_dev_con, 0, &uniphier_fip_dev_handle);
  208. }
  209. static int uniphier_io_emmc_setup(unsigned int soc, size_t buffer_offset)
  210. {
  211. struct io_block_dev_spec *block_dev_spec;
  212. int ret;
  213. ret = uniphier_emmc_init(soc, &block_dev_spec);
  214. if (ret)
  215. return ret;
  216. return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
  217. }
  218. static int uniphier_io_nand_setup(unsigned int soc, size_t buffer_offset)
  219. {
  220. struct io_block_dev_spec *block_dev_spec;
  221. int ret;
  222. ret = uniphier_nand_init(soc, &block_dev_spec);
  223. if (ret)
  224. return ret;
  225. return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
  226. }
  227. static int uniphier_io_nor_setup(unsigned int soc_id, size_t buffer_offset)
  228. {
  229. return uniphier_io_memmap_setup(0x70000);
  230. }
  231. static const uintptr_t uniphier_ocm_base[] = {
  232. [UNIPHIER_SOC_LD11] = 0x30000000,
  233. [UNIPHIER_SOC_LD20] = 0x30000000,
  234. [UNIPHIER_SOC_PXS3] = 0x30000000,
  235. };
  236. static int uniphier_io_rom_api_setup(unsigned int soc)
  237. {
  238. uintptr_t ocm_base;
  239. int ret;
  240. assert(soc < ARRAY_SIZE(uniphier_ocm_base));
  241. ocm_base = uniphier_ocm_base[soc];
  242. ret = mmap_add_dynamic_region(UNIPHIER_ROM_REGION_BASE,
  243. UNIPHIER_ROM_REGION_BASE,
  244. UNIPHIER_ROM_REGION_SIZE,
  245. MT_CODE | MT_SECURE);
  246. if (ret)
  247. return ret;
  248. /*
  249. * on-chip SRAM region: should be DEVICE attribute because the USB
  250. * load functions provided by the ROM use this memory region as a work
  251. * area, but do not cater to cache coherency.
  252. */
  253. ret = mmap_add_dynamic_region(ocm_base, ocm_base,
  254. UNIPHIER_OCM_REGION_SIZE,
  255. MT_DEVICE | MT_RW | MT_SECURE);
  256. if (ret)
  257. return ret;
  258. return 0;
  259. }
  260. static int uniphier_io_usb_setup(unsigned int soc, size_t buffer_offset)
  261. {
  262. struct io_block_dev_spec *block_dev_spec;
  263. int ret;
  264. /* use ROM API for loading images from USB storage */
  265. ret = uniphier_io_rom_api_setup(soc);
  266. if (ret)
  267. return ret;
  268. ret = uniphier_usb_init(soc, &block_dev_spec);
  269. if (ret)
  270. return ret;
  271. return uniphier_io_block_setup(0x20000, block_dev_spec, buffer_offset);
  272. }
  273. static int (* const uniphier_io_setup_table[])(unsigned int, size_t) = {
  274. [UNIPHIER_BOOT_DEVICE_EMMC] = uniphier_io_emmc_setup,
  275. [UNIPHIER_BOOT_DEVICE_NAND] = uniphier_io_nand_setup,
  276. [UNIPHIER_BOOT_DEVICE_NOR] = uniphier_io_nor_setup,
  277. [UNIPHIER_BOOT_DEVICE_USB] = uniphier_io_usb_setup,
  278. };
  279. int uniphier_io_setup(unsigned int soc_id, uintptr_t mem_base)
  280. {
  281. int (*io_setup)(unsigned int soc_id, size_t buffer_offset);
  282. unsigned int boot_dev;
  283. int ret;
  284. boot_dev = uniphier_get_boot_device(soc_id);
  285. if (boot_dev == UNIPHIER_BOOT_DEVICE_RSV)
  286. return -EINVAL;
  287. io_setup = uniphier_io_setup_table[boot_dev];
  288. ret = io_setup(soc_id, mem_base + UNIPHIER_BLOCK_BUF_OFFSET);
  289. if (ret)
  290. return ret;
  291. ret = uniphier_io_fip_setup();
  292. if (ret)
  293. return ret;
  294. return 0;
  295. }
  296. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  297. uintptr_t *image_spec)
  298. {
  299. uintptr_t init_params;
  300. assert(image_id < ARRAY_SIZE(uniphier_io_policies));
  301. *dev_handle = *uniphier_io_policies[image_id].dev_handle;
  302. *image_spec = uniphier_io_policies[image_id].image_spec;
  303. init_params = uniphier_io_policies[image_id].init_params;
  304. return io_dev_init(*dev_handle, init_params);
  305. }