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