fdt-common.c 4.0 KB

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