xref: /openbmc/linux/arch/powerpc/boot/devtree.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * devtree.c - convenience functions for device tree manipulation
4  * Copyright 2007 David Gibson, IBM Corporation.
5  * Copyright (c) 2007 Freescale Semiconductor, Inc.
6  *
7  * Authors: David Gibson <david@gibson.dropbear.id.au>
8  *	    Scott Wood <scottwood@freescale.com>
9  */
10 #include <stdarg.h>
11 #include <stddef.h>
12 #include "types.h"
13 #include "string.h"
14 #include "stdio.h"
15 #include "ops.h"
16 
17 void dt_fixup_memory(u64 start, u64 size)
18 {
19 	void *root, *memory;
20 	int naddr, nsize, i;
21 	u32 memreg[4];
22 
23 	root = finddevice("/");
24 	if (getprop(root, "#address-cells", &naddr, sizeof(naddr)) < 0)
25 		naddr = 2;
26 	if (naddr < 1 || naddr > 2)
27 		fatal("Can't cope with #address-cells == %d in /\n\r", naddr);
28 
29 	if (getprop(root, "#size-cells", &nsize, sizeof(nsize)) < 0)
30 		nsize = 1;
31 	if (nsize < 1 || nsize > 2)
32 		fatal("Can't cope with #size-cells == %d in /\n\r", nsize);
33 
34 	i = 0;
35 	if (naddr == 2)
36 		memreg[i++] = start >> 32;
37 	memreg[i++] = start & 0xffffffff;
38 	if (nsize == 2)
39 		memreg[i++] = size >> 32;
40 	memreg[i++] = size & 0xffffffff;
41 
42 	memory = finddevice("/memory");
43 	if (! memory) {
44 		memory = create_node(NULL, "memory");
45 		setprop_str(memory, "device_type", "memory");
46 	}
47 
48 	printf("Memory <- <0x%x", memreg[0]);
49 	for (i = 1; i < (naddr + nsize); i++)
50 		printf(" 0x%x", memreg[i]);
51 	printf("> (%ldMB)\n\r", (unsigned long)(size >> 20));
52 
53 	setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32));
54 }
55 
56 #define MHZ(x)	((x + 500000) / 1000000)
57 
58 void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus)
59 {
60 	void *devp = NULL;
61 
62 	printf("CPU clock-frequency <- 0x%x (%dMHz)\n\r", cpu, MHZ(cpu));
63 	printf("CPU timebase-frequency <- 0x%x (%dMHz)\n\r", tb, MHZ(tb));
64 	if (bus > 0)
65 		printf("CPU bus-frequency <- 0x%x (%dMHz)\n\r", bus, MHZ(bus));
66 
67 	while ((devp = find_node_by_devtype(devp, "cpu"))) {
68 		setprop_val(devp, "clock-frequency", cpu);
69 		setprop_val(devp, "timebase-frequency", tb);
70 		if (bus > 0)
71 			setprop_val(devp, "bus-frequency", bus);
72 	}
73 
74 	timebase_period_ns = 1000000000 / tb;
75 }
76 
77 void dt_fixup_clock(const char *path, u32 freq)
78 {
79 	void *devp = finddevice(path);
80 
81 	if (devp) {
82 		printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq));
83 		setprop_val(devp, "clock-frequency", freq);
84 	}
85 }
86 
87 void dt_fixup_mac_address_by_alias(const char *alias, const u8 *addr)
88 {
89 	void *devp = find_node_by_alias(alias);
90 
91 	if (devp) {
92 		printf("%s: local-mac-address <-"
93 		       " %02x:%02x:%02x:%02x:%02x:%02x\n\r", alias,
94 		       addr[0], addr[1], addr[2],
95 		       addr[3], addr[4], addr[5]);
96 
97 		setprop(devp, "local-mac-address", addr, 6);
98 	}
99 }
100 
101 void dt_fixup_mac_address(u32 index, const u8 *addr)
102 {
103 	void *devp = find_node_by_prop_value(NULL, "linux,network-index",
104 	                                     (void*)&index, sizeof(index));
105 
106 	if (devp) {
107 		printf("ENET%d: local-mac-address <-"
108 		       " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index,
109 		       addr[0], addr[1], addr[2],
110 		       addr[3], addr[4], addr[5]);
111 
112 		setprop(devp, "local-mac-address", addr, 6);
113 	}
114 }
115 
116 void __dt_fixup_mac_addresses(u32 startindex, ...)
117 {
118 	va_list ap;
119 	u32 index = startindex;
120 	const u8 *addr;
121 
122 	va_start(ap, startindex);
123 
124 	while ((addr = va_arg(ap, const u8 *)))
125 		dt_fixup_mac_address(index++, addr);
126 
127 	va_end(ap);
128 }
129 
130 #define MAX_ADDR_CELLS 4
131 
132 void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize)
133 {
134 	if (getprop(node, "#address-cells", naddr, 4) != 4)
135 		*naddr = 2;
136 	if (getprop(node, "#size-cells", nsize, 4) != 4)
137 		*nsize = 1;
138 }
139 
140 static void copy_val(u32 *dest, u32 *src, int naddr)
141 {
142 	int pad = MAX_ADDR_CELLS - naddr;
143 
144 	memset(dest, 0, pad * 4);
145 	memcpy(dest + pad, src, naddr * 4);
146 }
147 
148 static int sub_reg(u32 *reg, u32 *sub)
149 {
150 	int i, borrow = 0;
151 
152 	for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) {
153 		int prev_borrow = borrow;
154 		borrow = reg[i] < sub[i] + prev_borrow;
155 		reg[i] -= sub[i] + prev_borrow;
156 	}
157 
158 	return !borrow;
159 }
160 
161 static int add_reg(u32 *reg, u32 *add, int naddr)
162 {
163 	int i, carry = 0;
164 
165 	for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) {
166 		u64 tmp = (u64)reg[i] + add[i] + carry;
167 		carry = tmp >> 32;
168 		reg[i] = (u32)tmp;
169 	}
170 
171 	return !carry;
172 }
173 
174 /* It is assumed that if the first byte of reg fits in a
175  * range, then the whole reg block fits.
176  */
177 static int compare_reg(u32 *reg, u32 *range, u32 *rangesize)
178 {
179 	int i;
180 	u32 end;
181 
182 	for (i = 0; i < MAX_ADDR_CELLS; i++) {
183 		if (reg[i] < range[i])
184 			return 0;
185 		if (reg[i] > range[i])
186 			break;
187 	}
188 
189 	for (i = 0; i < MAX_ADDR_CELLS; i++) {
190 		end = range[i] + rangesize[i];
191 
192 		if (reg[i] < end)
193 			break;
194 		if (reg[i] > end)
195 			return 0;
196 	}
197 
198 	return reg[i] != end;
199 }
200 
201 /* reg must be MAX_ADDR_CELLS */
202 static int find_range(u32 *reg, u32 *ranges, int nregaddr,
203                       int naddr, int nsize, int buflen)
204 {
205 	int nrange = nregaddr + naddr + nsize;
206 	int i;
207 
208 	for (i = 0; i + nrange <= buflen; i += nrange) {
209 		u32 range_addr[MAX_ADDR_CELLS];
210 		u32 range_size[MAX_ADDR_CELLS];
211 
212 		copy_val(range_addr, ranges + i, nregaddr);
213 		copy_val(range_size, ranges + i + nregaddr + naddr, nsize);
214 
215 		if (compare_reg(reg, range_addr, range_size))
216 			return i;
217 	}
218 
219 	return -1;
220 }
221 
222 /* Currently only generic buses without special encodings are supported.
223  * In particular, PCI is not supported.  Also, only the beginning of the
224  * reg block is tracked; size is ignored except in ranges.
225  */
226 static u32 prop_buf[MAX_PROP_LEN / 4];
227 
228 static int dt_xlate(void *node, int res, int reglen, unsigned long *addr,
229 		unsigned long *size)
230 {
231 	u32 last_addr[MAX_ADDR_CELLS];
232 	u32 this_addr[MAX_ADDR_CELLS];
233 	void *parent;
234 	u64 ret_addr, ret_size;
235 	u32 naddr, nsize, prev_naddr, prev_nsize;
236 	int buflen, offset;
237 
238 	parent = get_parent(node);
239 	if (!parent)
240 		return 0;
241 
242 	dt_get_reg_format(parent, &naddr, &nsize);
243 
244 	if (nsize > 2)
245 		return 0;
246 
247 	offset = (naddr + nsize) * res;
248 
249 	if (reglen < offset + naddr + nsize ||
250 	    MAX_PROP_LEN < (offset + naddr + nsize) * 4)
251 		return 0;
252 
253 	copy_val(last_addr, prop_buf + offset, naddr);
254 
255 	ret_size = prop_buf[offset + naddr];
256 	if (nsize == 2) {
257 		ret_size <<= 32;
258 		ret_size |= prop_buf[offset + naddr + 1];
259 	}
260 
261 	for (;;) {
262 		prev_naddr = naddr;
263 		prev_nsize = nsize;
264 		node = parent;
265 
266 		parent = get_parent(node);
267 		if (!parent)
268 			break;
269 
270 		dt_get_reg_format(parent, &naddr, &nsize);
271 
272 		buflen = getprop(node, "ranges", prop_buf,
273 				sizeof(prop_buf));
274 		if (buflen == 0)
275 			continue;
276 		if (buflen < 0 || buflen > sizeof(prop_buf))
277 			return 0;
278 
279 		offset = find_range(last_addr, prop_buf, prev_naddr,
280 		                    naddr, prev_nsize, buflen / 4);
281 
282 		if (offset < 0)
283 			return 0;
284 
285 		copy_val(this_addr, prop_buf + offset, prev_naddr);
286 
287 		if (!sub_reg(last_addr, this_addr))
288 			return 0;
289 
290 		copy_val(this_addr, prop_buf + offset + prev_naddr, naddr);
291 
292 		if (!add_reg(last_addr, this_addr, naddr))
293 			return 0;
294 	}
295 
296 	if (naddr > 2)
297 		return 0;
298 
299 	ret_addr = ((u64)last_addr[2] << 32) | last_addr[3];
300 
301 	if (sizeof(void *) == 4 &&
302 	    (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL ||
303 	     ret_addr + ret_size > 0x100000000ULL))
304 		return 0;
305 
306 	*addr = ret_addr;
307 	if (size)
308 		*size = ret_size;
309 
310 	return 1;
311 }
312 
313 int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size)
314 {
315 	int reglen;
316 
317 	reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4;
318 	return dt_xlate(node, res, reglen, addr, size);
319 }
320 
321 int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr)
322 {
323 
324 	if (buflen > sizeof(prop_buf))
325 		return 0;
326 
327 	memcpy(prop_buf, buf, buflen);
328 	return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL);
329 }
330 
331 int dt_is_compatible(void *node, const char *compat)
332 {
333 	char *buf = (char *)prop_buf;
334 	int len, pos;
335 
336 	len = getprop(node, "compatible", buf, MAX_PROP_LEN);
337 	if (len < 0)
338 		return 0;
339 
340 	for (pos = 0; pos < len; pos++) {
341 		if (!strcmp(buf + pos, compat))
342 			return 1;
343 
344 		pos += strnlen(&buf[pos], len - pos);
345 	}
346 
347 	return 0;
348 }
349 
350 int dt_get_virtual_reg(void *node, void **addr, int nres)
351 {
352 	unsigned long xaddr;
353 	int n;
354 
355 	n = getprop(node, "virtual-reg", addr, nres * 4);
356 	if (n > 0)
357 		return n / 4;
358 
359 	for (n = 0; n < nres; n++) {
360 		if (!dt_xlate_reg(node, n, &xaddr, NULL))
361 			break;
362 
363 		addr[n] = (void *)xaddr;
364 	}
365 
366 	return n;
367 }
368 
369