fdt-common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016-2017 Texas Instruments, Inc.
  4. */
  5. #include <common.h>
  6. #include <linux/libfdt.h>
  7. #include <fdt_support.h>
  8. #include <asm/omap_common.h>
  9. #include <asm/omap_sec_common.h>
  10. #ifdef CONFIG_TI_SECURE_DEVICE
  11. /* Give zero values if not already defined */
  12. #ifndef TI_OMAP5_SECURE_BOOT_RESV_SRAM_SZ
  13. #define TI_OMAP5_SECURE_BOOT_RESV_SRAM_SZ (0)
  14. #endif
  15. #ifndef CONFIG_SECURE_RUNTIME_RESV_SRAM_SZ
  16. #define CONFIG_SECURE_RUNTIME_RESV_SRAM_SZ (0)
  17. #endif
  18. int ft_hs_disable_rng(void *fdt, bd_t *bd)
  19. {
  20. const char *path;
  21. int offs;
  22. int ret;
  23. /* Make HW RNG reserved for secure world use */
  24. path = "/ocp/rng";
  25. offs = fdt_path_offset(fdt, path);
  26. if (offs < 0) {
  27. debug("Node %s not found.\n", path);
  28. return 0;
  29. }
  30. ret = fdt_setprop_string(fdt, offs,
  31. "status", "disabled");
  32. if (ret < 0) {
  33. printf("Could not add status property to node %s: %s\n",
  34. path, fdt_strerror(ret));
  35. return ret;
  36. }
  37. return 0;
  38. }
  39. #if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE != 0)
  40. /*
  41. * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
  42. */
  43. static int fdt_pack_reg(const void *fdt, void *buf, u64 address, u64 size)
  44. {
  45. int address_cells = fdt_address_cells(fdt, 0);
  46. int size_cells = fdt_size_cells(fdt, 0);
  47. char *p = buf;
  48. if (address_cells == 2)
  49. *(fdt64_t *)p = cpu_to_fdt64(address);
  50. else
  51. *(fdt32_t *)p = cpu_to_fdt32(address);
  52. p += 4 * address_cells;
  53. if (size_cells == 2)
  54. *(fdt64_t *)p = cpu_to_fdt64(size);
  55. else
  56. *(fdt32_t *)p = cpu_to_fdt32(size);
  57. p += 4 * size_cells;
  58. return p - (char *)buf;
  59. }
  60. int ft_hs_fixup_dram(void *fdt, bd_t *bd)
  61. {
  62. const char *path, *subpath;
  63. int offs, len;
  64. u32 sec_mem_start = get_sec_mem_start();
  65. u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
  66. fdt32_t address_cells = cpu_to_fdt32(fdt_address_cells(fdt, 0));
  67. fdt32_t size_cells = cpu_to_fdt32(fdt_size_cells(fdt, 0));
  68. u8 temp[16]; /* Up to 64-bit address + 64-bit size */
  69. /* Delete any original secure_reserved node */
  70. path = "/reserved-memory/secure_reserved";
  71. offs = fdt_path_offset(fdt, path);
  72. if (offs >= 0)
  73. fdt_del_node(fdt, offs);
  74. /* Add new secure_reserved node */
  75. path = "/reserved-memory";
  76. offs = fdt_path_offset(fdt, path);
  77. if (offs < 0) {
  78. debug("Node %s not found\n", path);
  79. path = "/";
  80. subpath = "reserved-memory";
  81. offs = fdt_path_offset(fdt, path);
  82. offs = fdt_add_subnode(fdt, offs, subpath);
  83. if (offs < 0) {
  84. printf("Could not create %s%s node.\n", path, subpath);
  85. return 1;
  86. }
  87. path = "/reserved-memory";
  88. offs = fdt_path_offset(fdt, path);
  89. fdt_setprop(fdt, offs, "#address-cells", &address_cells, sizeof(address_cells));
  90. fdt_setprop(fdt, offs, "#size-cells", &size_cells, sizeof(size_cells));
  91. fdt_setprop(fdt, offs, "ranges", NULL, 0);
  92. }
  93. subpath = "secure_reserved";
  94. offs = fdt_add_subnode(fdt, offs, subpath);
  95. if (offs < 0) {
  96. printf("Could not create %s%s node.\n", path, subpath);
  97. return 1;
  98. }
  99. fdt_setprop_string(fdt, offs, "compatible", "ti,secure-memory");
  100. fdt_setprop_string(fdt, offs, "status", "okay");
  101. fdt_setprop(fdt, offs, "no-map", NULL, 0);
  102. len = fdt_pack_reg(fdt, temp, sec_mem_start, sec_mem_size);
  103. fdt_setprop(fdt, offs, "reg", temp, len);
  104. return 0;
  105. }
  106. #else
  107. int ft_hs_fixup_dram(void *fdt, bd_t *bd) { return 0; }
  108. #endif
  109. int ft_hs_add_tee(void *fdt, bd_t *bd)
  110. {
  111. const char *path, *subpath;
  112. int offs;
  113. extern int tee_loaded;
  114. if (!tee_loaded)
  115. return 0;
  116. path = "/firmware";
  117. offs = fdt_path_offset(fdt, path);
  118. if (offs < 0) {
  119. path = "/";
  120. offs = fdt_path_offset(fdt, path);
  121. if (offs < 0) {
  122. printf("Could not find root node.\n");
  123. return 1;
  124. }
  125. subpath = "firmware";
  126. offs = fdt_add_subnode(fdt, offs, subpath);
  127. if (offs < 0) {
  128. printf("Could not create %s node.\n", subpath);
  129. return 1;
  130. }
  131. }
  132. subpath = "optee";
  133. offs = fdt_add_subnode(fdt, offs, subpath);
  134. if (offs < 0) {
  135. printf("Could not create %s node.\n", subpath);
  136. return 1;
  137. }
  138. fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
  139. fdt_setprop_string(fdt, offs, "method", "smc");
  140. return 0;
  141. }
  142. #endif