fdt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2019-2020, STMicroelectronics - All Rights Reserved
  4. */
  5. #define LOG_CATEGORY LOGC_ARCH
  6. #include <common.h>
  7. #include <fdtdec.h>
  8. #include <fdt_support.h>
  9. #include <log.h>
  10. #include <tee.h>
  11. #include <asm/arch/sys_proto.h>
  12. #include <dt-bindings/pinctrl/stm32-pinfunc.h>
  13. #include <linux/io.h>
  14. #define ETZPC_DECPROT(n) (STM32_ETZPC_BASE + 0x10 + 4 * (n))
  15. #define ETZPC_DECPROT_NB 6
  16. #define DECPROT_MASK 0x03
  17. #define NB_PROT_PER_REG 0x10
  18. #define DECPROT_NB_BITS 2
  19. #define DECPROT_SECURED 0x00
  20. #define DECPROT_WRITE_SECURE 0x01
  21. #define DECPROT_MCU_ISOLATION 0x02
  22. #define DECPROT_NON_SECURED 0x03
  23. #define ETZPC_RESERVED 0xffffffff
  24. #define STM32_FDCAN_BASE 0x4400e000
  25. #define STM32_CRYP2_BASE 0x4c005000
  26. #define STM32_CRYP1_BASE 0x54001000
  27. #define STM32_GPU_BASE 0x59000000
  28. #define STM32_DSI_BASE 0x5a000000
  29. static const u32 stm32mp1_ip_addr[] = {
  30. 0x5c008000, /* 00 stgenc */
  31. 0x54000000, /* 01 bkpsram */
  32. 0x5c003000, /* 02 iwdg1 */
  33. 0x5c000000, /* 03 usart1 */
  34. 0x5c001000, /* 04 spi6 */
  35. 0x5c002000, /* 05 i2c4 */
  36. ETZPC_RESERVED, /* 06 reserved */
  37. 0x54003000, /* 07 rng1 */
  38. 0x54002000, /* 08 hash1 */
  39. STM32_CRYP1_BASE, /* 09 cryp1 */
  40. 0x5a003000, /* 0A ddrctrl */
  41. 0x5a004000, /* 0B ddrphyc */
  42. 0x5c009000, /* 0C i2c6 */
  43. ETZPC_RESERVED, /* 0D reserved */
  44. ETZPC_RESERVED, /* 0E reserved */
  45. ETZPC_RESERVED, /* 0F reserved */
  46. 0x40000000, /* 10 tim2 */
  47. 0x40001000, /* 11 tim3 */
  48. 0x40002000, /* 12 tim4 */
  49. 0x40003000, /* 13 tim5 */
  50. 0x40004000, /* 14 tim6 */
  51. 0x40005000, /* 15 tim7 */
  52. 0x40006000, /* 16 tim12 */
  53. 0x40007000, /* 17 tim13 */
  54. 0x40008000, /* 18 tim14 */
  55. 0x40009000, /* 19 lptim1 */
  56. 0x4000a000, /* 1A wwdg1 */
  57. 0x4000b000, /* 1B spi2 */
  58. 0x4000c000, /* 1C spi3 */
  59. 0x4000d000, /* 1D spdifrx */
  60. 0x4000e000, /* 1E usart2 */
  61. 0x4000f000, /* 1F usart3 */
  62. 0x40010000, /* 20 uart4 */
  63. 0x40011000, /* 21 uart5 */
  64. 0x40012000, /* 22 i2c1 */
  65. 0x40013000, /* 23 i2c2 */
  66. 0x40014000, /* 24 i2c3 */
  67. 0x40015000, /* 25 i2c5 */
  68. 0x40016000, /* 26 cec */
  69. 0x40017000, /* 27 dac */
  70. 0x40018000, /* 28 uart7 */
  71. 0x40019000, /* 29 uart8 */
  72. ETZPC_RESERVED, /* 2A reserved */
  73. ETZPC_RESERVED, /* 2B reserved */
  74. 0x4001c000, /* 2C mdios */
  75. ETZPC_RESERVED, /* 2D reserved */
  76. ETZPC_RESERVED, /* 2E reserved */
  77. ETZPC_RESERVED, /* 2F reserved */
  78. 0x44000000, /* 30 tim1 */
  79. 0x44001000, /* 31 tim8 */
  80. ETZPC_RESERVED, /* 32 reserved */
  81. 0x44003000, /* 33 usart6 */
  82. 0x44004000, /* 34 spi1 */
  83. 0x44005000, /* 35 spi4 */
  84. 0x44006000, /* 36 tim15 */
  85. 0x44007000, /* 37 tim16 */
  86. 0x44008000, /* 38 tim17 */
  87. 0x44009000, /* 39 spi5 */
  88. 0x4400a000, /* 3A sai1 */
  89. 0x4400b000, /* 3B sai2 */
  90. 0x4400c000, /* 3C sai3 */
  91. 0x4400d000, /* 3D dfsdm */
  92. STM32_FDCAN_BASE, /* 3E tt_fdcan */
  93. ETZPC_RESERVED, /* 3F reserved */
  94. 0x50021000, /* 40 lptim2 */
  95. 0x50022000, /* 41 lptim3 */
  96. 0x50023000, /* 42 lptim4 */
  97. 0x50024000, /* 43 lptim5 */
  98. 0x50027000, /* 44 sai4 */
  99. 0x50025000, /* 45 vrefbuf */
  100. 0x4c006000, /* 46 dcmi */
  101. 0x4c004000, /* 47 crc2 */
  102. 0x48003000, /* 48 adc */
  103. 0x4c002000, /* 49 hash2 */
  104. 0x4c003000, /* 4A rng2 */
  105. STM32_CRYP2_BASE, /* 4B cryp2 */
  106. ETZPC_RESERVED, /* 4C reserved */
  107. ETZPC_RESERVED, /* 4D reserved */
  108. ETZPC_RESERVED, /* 4E reserved */
  109. ETZPC_RESERVED, /* 4F reserved */
  110. ETZPC_RESERVED, /* 50 sram1 */
  111. ETZPC_RESERVED, /* 51 sram2 */
  112. ETZPC_RESERVED, /* 52 sram3 */
  113. ETZPC_RESERVED, /* 53 sram4 */
  114. ETZPC_RESERVED, /* 54 retram */
  115. 0x49000000, /* 55 otg */
  116. 0x48004000, /* 56 sdmmc3 */
  117. 0x48005000, /* 57 dlybsd3 */
  118. 0x48000000, /* 58 dma1 */
  119. 0x48001000, /* 59 dma2 */
  120. 0x48002000, /* 5A dmamux */
  121. 0x58002000, /* 5B fmc */
  122. 0x58003000, /* 5C qspi */
  123. 0x58004000, /* 5D dlybq */
  124. 0x5800a000, /* 5E eth */
  125. ETZPC_RESERVED, /* 5F reserved */
  126. };
  127. /* fdt helper */
  128. static bool fdt_disable_subnode_by_address(void *fdt, int offset, u32 addr)
  129. {
  130. int node;
  131. fdt_addr_t regs;
  132. for (node = fdt_first_subnode(fdt, offset);
  133. node >= 0;
  134. node = fdt_next_subnode(fdt, node)) {
  135. regs = fdtdec_get_addr(fdt, node, "reg");
  136. if (addr == regs) {
  137. if (fdtdec_get_is_enabled(fdt, node)) {
  138. fdt_status_disabled(fdt, node);
  139. return true;
  140. }
  141. return false;
  142. }
  143. }
  144. return false;
  145. }
  146. static int stm32_fdt_fixup_etzpc(void *fdt, int soc_node)
  147. {
  148. const u32 *array;
  149. int array_size, i;
  150. int offset, shift;
  151. u32 addr, status, decprot[ETZPC_DECPROT_NB];
  152. array = stm32mp1_ip_addr;
  153. array_size = ARRAY_SIZE(stm32mp1_ip_addr);
  154. for (i = 0; i < ETZPC_DECPROT_NB; i++)
  155. decprot[i] = readl(ETZPC_DECPROT(i));
  156. for (i = 0; i < array_size; i++) {
  157. offset = i / NB_PROT_PER_REG;
  158. shift = (i % NB_PROT_PER_REG) * DECPROT_NB_BITS;
  159. status = (decprot[offset] >> shift) & DECPROT_MASK;
  160. addr = array[i];
  161. log_debug("ETZPC: 0x%08x decprot %d=%d\n", addr, i, status);
  162. if (addr == ETZPC_RESERVED ||
  163. status == DECPROT_NON_SECURED)
  164. continue;
  165. if (fdt_disable_subnode_by_address(fdt, soc_node, addr))
  166. log_notice("ETZPC: 0x%08x node disabled, decprot %d=%d\n",
  167. addr, i, status);
  168. }
  169. return 0;
  170. }
  171. /* deactivate all the cpu except core 0 */
  172. static void stm32_fdt_fixup_cpu(void *blob, char *name)
  173. {
  174. int off;
  175. u32 reg;
  176. off = fdt_path_offset(blob, "/cpus");
  177. if (off < 0) {
  178. log_warning("%s: couldn't find /cpus node\n", __func__);
  179. return;
  180. }
  181. off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
  182. while (off != -FDT_ERR_NOTFOUND) {
  183. reg = fdtdec_get_addr(blob, off, "reg");
  184. if (reg != 0) {
  185. fdt_del_node(blob, off);
  186. log_notice("FDT: cpu %d node remove for %s\n",
  187. reg, name);
  188. /* after delete we can't trust the offsets anymore */
  189. off = -1;
  190. }
  191. off = fdt_node_offset_by_prop_value(blob, off,
  192. "device_type", "cpu", 4);
  193. }
  194. }
  195. static void stm32_fdt_disable(void *fdt, int offset, u32 addr,
  196. const char *string, const char *name)
  197. {
  198. if (fdt_disable_subnode_by_address(fdt, offset, addr))
  199. log_notice("FDT: %s@%08x node disabled for %s\n",
  200. string, addr, name);
  201. }
  202. static void stm32_fdt_disable_optee(void *blob)
  203. {
  204. int off, node;
  205. /* Delete "optee" firmware node */
  206. off = fdt_node_offset_by_compatible(blob, -1, "linaro,optee-tz");
  207. if (off >= 0 && fdtdec_get_is_enabled(blob, off))
  208. fdt_del_node(blob, off);
  209. /* Delete "optee@..." reserved-memory node */
  210. off = fdt_path_offset(blob, "/reserved-memory/");
  211. if (off < 0)
  212. return;
  213. for (node = fdt_first_subnode(blob, off);
  214. node >= 0;
  215. node = fdt_next_subnode(blob, node)) {
  216. if (strncmp(fdt_get_name(blob, node, NULL), "optee@", 6))
  217. continue;
  218. if (fdt_del_node(blob, node))
  219. printf("Failed to remove optee reserved-memory node\n");
  220. }
  221. }
  222. /*
  223. * This function is called right before the kernel is booted. "blob" is the
  224. * device tree that will be passed to the kernel.
  225. */
  226. int ft_system_setup(void *blob, struct bd_info *bd)
  227. {
  228. int ret = 0;
  229. int soc;
  230. u32 pkg, cpu;
  231. char name[SOC_NAME_SIZE];
  232. soc = fdt_path_offset(blob, "/soc");
  233. if (soc < 0)
  234. return soc;
  235. if (CONFIG_IS_ENABLED(STM32_ETZPC)) {
  236. ret = stm32_fdt_fixup_etzpc(blob, soc);
  237. if (ret)
  238. return ret;
  239. }
  240. /* MPUs Part Numbers and name*/
  241. cpu = get_cpu_type();
  242. get_soc_name(name);
  243. switch (cpu) {
  244. case CPU_STM32MP151Fxx:
  245. case CPU_STM32MP151Dxx:
  246. case CPU_STM32MP151Cxx:
  247. case CPU_STM32MP151Axx:
  248. stm32_fdt_fixup_cpu(blob, name);
  249. /* after cpu delete we can't trust the soc offsets anymore */
  250. soc = fdt_path_offset(blob, "/soc");
  251. stm32_fdt_disable(blob, soc, STM32_FDCAN_BASE, "can", name);
  252. /* fall through */
  253. case CPU_STM32MP153Fxx:
  254. case CPU_STM32MP153Dxx:
  255. case CPU_STM32MP153Cxx:
  256. case CPU_STM32MP153Axx:
  257. stm32_fdt_disable(blob, soc, STM32_GPU_BASE, "gpu", name);
  258. stm32_fdt_disable(blob, soc, STM32_DSI_BASE, "dsi", name);
  259. break;
  260. default:
  261. break;
  262. }
  263. switch (cpu) {
  264. case CPU_STM32MP157Dxx:
  265. case CPU_STM32MP157Axx:
  266. case CPU_STM32MP153Dxx:
  267. case CPU_STM32MP153Axx:
  268. case CPU_STM32MP151Dxx:
  269. case CPU_STM32MP151Axx:
  270. stm32_fdt_disable(blob, soc, STM32_CRYP1_BASE, "cryp", name);
  271. stm32_fdt_disable(blob, soc, STM32_CRYP2_BASE, "cryp", name);
  272. break;
  273. default:
  274. break;
  275. }
  276. switch (get_cpu_package()) {
  277. case PKG_AA_LBGA448:
  278. pkg = STM32MP_PKG_AA;
  279. break;
  280. case PKG_AB_LBGA354:
  281. pkg = STM32MP_PKG_AB;
  282. break;
  283. case PKG_AC_TFBGA361:
  284. pkg = STM32MP_PKG_AC;
  285. break;
  286. case PKG_AD_TFBGA257:
  287. pkg = STM32MP_PKG_AD;
  288. break;
  289. default:
  290. pkg = 0;
  291. break;
  292. }
  293. if (pkg) {
  294. do_fixup_by_compat_u32(blob, "st,stm32mp157-pinctrl",
  295. "st,package", pkg, false);
  296. do_fixup_by_compat_u32(blob, "st,stm32mp157-z-pinctrl",
  297. "st,package", pkg, false);
  298. }
  299. /*
  300. * TEMP: remove OP-TEE nodes in kernel device tree
  301. * copied from U-Boot device tree by optee_copy_fdt_nodes
  302. * when OP-TEE is not detected (probe failed)
  303. * these OP-TEE nodes are present in <board>-u-boot.dtsi
  304. * under CONFIG_STM32MP15x_STM32IMAGE only for compatibility
  305. * when FIP is not used by TF-A
  306. */
  307. if (CONFIG_IS_ENABLED(STM32MP15x_STM32IMAGE) &&
  308. CONFIG_IS_ENABLED(OPTEE) &&
  309. !tee_find_device(NULL, NULL, NULL, NULL))
  310. stm32_fdt_disable_optee(blob);
  311. return ret;
  312. }