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