1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2010-2016, NVIDIA CORPORATION.
4  */
5 
6 #include <env.h>
7 #include <fdtdec.h>
8 #include <stdlib.h>
9 #include <linux/if_ether.h>
10 #include <asm/arch-tegra/cboot.h>
11 #include <asm/arch-tegra/gpu.h>
12 
13 /*
14  * This function is called right before the kernel is booted. "blob" is the
15  * device tree that will be passed to the kernel.
16  */
ft_system_setup(void * blob,struct bd_info * bd)17 int ft_system_setup(void *blob, struct bd_info *bd)
18 {
19 	const char *gpu_compats[] = {
20 #if defined(CONFIG_TEGRA124)
21 		"nvidia,gk20a",
22 #endif
23 #if defined(CONFIG_TEGRA210)
24 		"nvidia,gm20b",
25 #endif
26 	};
27 	int i, ret;
28 
29 	/* Enable GPU node if GPU setup has been performed */
30 	for (i = 0; i < ARRAY_SIZE(gpu_compats); i++) {
31 		ret = tegra_gpu_enable_node(blob, gpu_compats[i]);
32 		if (ret)
33 			return ret;
34 	}
35 
36 	return 0;
37 }
38 
39 #if defined(CONFIG_ARM64)
ft_mac_address_setup(void * fdt)40 void ft_mac_address_setup(void *fdt)
41 {
42 	const void *cboot_fdt = (const void *)cboot_boot_x0;
43 	uint8_t mac[ETH_ALEN], local_mac[ETH_ALEN];
44 	const char *path;
45 	int offset, err;
46 
47 	err = cboot_get_ethaddr(cboot_fdt, local_mac);
48 	if (err < 0)
49 		memset(local_mac, 0, ETH_ALEN);
50 
51 	path = fdt_get_alias(fdt, "ethernet");
52 	if (!path)
53 		return;
54 
55 	debug("ethernet alias found: %s\n", path);
56 
57 	offset = fdt_path_offset(fdt, path);
58 	if (offset < 0) {
59 		printf("ethernet alias points to absent node %s\n", path);
60 		return;
61 	}
62 
63 	if (is_valid_ethaddr(local_mac)) {
64 		err = fdt_setprop(fdt, offset, "local-mac-address", local_mac,
65 				  ETH_ALEN);
66 		if (!err)
67 			debug("Local MAC address set: %pM\n", local_mac);
68 	}
69 
70 	if (eth_env_get_enetaddr("ethaddr", mac)) {
71 		if (memcmp(local_mac, mac, ETH_ALEN) != 0) {
72 			err = fdt_setprop(fdt, offset, "mac-address", mac,
73 					  ETH_ALEN);
74 			if (!err)
75 				debug("MAC address set: %pM\n", mac);
76 		}
77 	}
78 }
79 
ft_copy_carveout(void * dst,const void * src,const char * node)80 static int ft_copy_carveout(void *dst, const void *src, const char *node)
81 {
82 	const char *names = "memory-region-names";
83 	struct fdt_memory carveout;
84 	unsigned int index = 0;
85 	int err, offset, len;
86 	const void *prop;
87 
88 	while (true) {
89 		const char **compatibles = NULL;
90 		unsigned int num_compatibles;
91 		unsigned long flags;
92 		char *copy = NULL;
93 		const char *name;
94 
95 		err = fdtdec_get_carveout(src, node, "memory-region", index,
96 					  &carveout, &name, &compatibles,
97 					  &num_compatibles, &flags);
98 		if (err < 0) {
99 			if (err != -FDT_ERR_NOTFOUND)
100 				printf("failed to get carveout for %s: %d\n",
101 				       node, err);
102 			else
103 				break;
104 
105 			return err;
106 		}
107 
108 		if (name) {
109 			const char *ptr = strchr(name, '@');
110 
111 			if (ptr) {
112 				copy = strndup(name, ptr - name);
113 				name = copy;
114 			}
115 		} else {
116 			name = "carveout";
117 		}
118 
119 		err = fdtdec_set_carveout(dst, node, "memory-region", index,
120 					  &carveout, name, compatibles,
121 					  num_compatibles, flags);
122 		if (err < 0) {
123 			printf("failed to set carveout for %s: %d\n", node,
124 			       err);
125 			return err;
126 		}
127 
128 		if (copy)
129 			free(copy);
130 
131 		index++;
132 	}
133 
134 	offset = fdt_path_offset(src, node);
135 	if (offset < 0) {
136 		debug("failed to find source offset for %s: %s\n", node,
137 		      fdt_strerror(err));
138 		return err;
139 	}
140 
141 	prop = fdt_getprop(src, offset, names, &len);
142 	if (prop) {
143 		offset = fdt_path_offset(dst, node);
144 		if (offset < 0) {
145 			debug("failed to find destination offset for %s: %s\n",
146 			      node, fdt_strerror(err));
147 			return err;
148 		}
149 
150 		err = fdt_setprop(dst, offset, "memory-region-names", prop,
151 				  len);
152 		if (err < 0) {
153 			debug("failed to copy \"%s\" property: %s\n", names,
154 			      fdt_strerror(err));
155 			return err;
156 		}
157 	}
158 
159 	return 0;
160 }
161 
ft_carveout_setup(void * fdt,const char * const * nodes,unsigned int count)162 void ft_carveout_setup(void *fdt, const char * const *nodes, unsigned int count)
163 {
164 	const void *cboot_fdt = (const void *)cboot_boot_x0;
165 	unsigned int i;
166 	int err;
167 
168 	for (i = 0; i < count; i++) {
169 		printf("copying carveout for %s...\n", nodes[i]);
170 
171 		err = ft_copy_carveout(fdt, cboot_fdt, nodes[i]);
172 		if (err < 0) {
173 			if (err != -FDT_ERR_NOTFOUND)
174 				printf("failed to copy carveout for %s: %d\n",
175 				       nodes[i], err);
176 
177 			continue;
178 		}
179 	}
180 }
181 #endif
182