Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / powerpc / boot / devtree.c
blob5d91036ad626d5a330a83ded285afba9c4d04c52
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.
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"
17 void dt_fixup_memory(u64 start, u64 size)
19 void *root, *memory;
20 int naddr, nsize, i;
21 u32 memreg[4];
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);
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);
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;
42 memory = finddevice("/memory");
43 if (! memory) {
44 memory = create_node(NULL, "memory");
45 setprop_str(memory, "device_type", "memory");
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));
53 setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32));
56 #define MHZ(x) ((x + 500000) / 1000000)
58 void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus)
60 void *devp = NULL;
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));
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);
74 timebase_period_ns = 1000000000 / tb;
77 void dt_fixup_clock(const char *path, u32 freq)
79 void *devp = finddevice(path);
81 if (devp) {
82 printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq));
83 setprop_val(devp, "clock-frequency", freq);
87 void dt_fixup_mac_address_by_alias(const char *alias, const u8 *addr)
89 void *devp = find_node_by_alias(alias);
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]);
97 setprop(devp, "local-mac-address", addr, 6);
101 void dt_fixup_mac_address(u32 index, const u8 *addr)
103 void *devp = find_node_by_prop_value(NULL, "linux,network-index",
104 (void*)&index, sizeof(index));
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]);
112 setprop(devp, "local-mac-address", addr, 6);
116 void __dt_fixup_mac_addresses(u32 startindex, ...)
118 va_list ap;
119 u32 index = startindex;
120 const u8 *addr;
122 va_start(ap, startindex);
124 while ((addr = va_arg(ap, const u8 *)))
125 dt_fixup_mac_address(index++, addr);
127 va_end(ap);
130 #define MAX_ADDR_CELLS 4
132 void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize)
134 if (getprop(node, "#address-cells", naddr, 4) != 4)
135 *naddr = 2;
136 if (getprop(node, "#size-cells", nsize, 4) != 4)
137 *nsize = 1;
140 static void copy_val(u32 *dest, u32 *src, int naddr)
142 int pad = MAX_ADDR_CELLS - naddr;
144 memset(dest, 0, pad * 4);
145 memcpy(dest + pad, src, naddr * 4);
148 static int sub_reg(u32 *reg, u32 *sub)
150 int i, borrow = 0;
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;
158 return !borrow;
161 static int add_reg(u32 *reg, u32 *add, int naddr)
163 int i, carry = 0;
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;
171 return !carry;
174 /* It is assumed that if the first byte of reg fits in a
175 * range, then the whole reg block fits.
177 static int compare_reg(u32 *reg, u32 *range, u32 *rangesize)
179 int i;
180 u32 end;
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;
189 for (i = 0; i < MAX_ADDR_CELLS; i++) {
190 end = range[i] + rangesize[i];
192 if (reg[i] < end)
193 break;
194 if (reg[i] > end)
195 return 0;
198 return reg[i] != end;
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)
205 int nrange = nregaddr + naddr + nsize;
206 int i;
208 for (i = 0; i + nrange <= buflen; i += nrange) {
209 u32 range_addr[MAX_ADDR_CELLS];
210 u32 range_size[MAX_ADDR_CELLS];
212 copy_val(range_addr, ranges + i, nregaddr);
213 copy_val(range_size, ranges + i + nregaddr + naddr, nsize);
215 if (compare_reg(reg, range_addr, range_size))
216 return i;
219 return -1;
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.
226 static u32 prop_buf[MAX_PROP_LEN / 4];
228 static int dt_xlate(void *node, int res, int reglen, unsigned long *addr,
229 unsigned long *size)
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;
238 parent = get_parent(node);
239 if (!parent)
240 return 0;
242 dt_get_reg_format(parent, &naddr, &nsize);
244 if (nsize > 2)
245 return 0;
247 offset = (naddr + nsize) * res;
249 if (reglen < offset + naddr + nsize ||
250 MAX_PROP_LEN < (offset + naddr + nsize) * 4)
251 return 0;
253 copy_val(last_addr, prop_buf + offset, naddr);
255 ret_size = prop_buf[offset + naddr];
256 if (nsize == 2) {
257 ret_size <<= 32;
258 ret_size |= prop_buf[offset + naddr + 1];
261 for (;;) {
262 prev_naddr = naddr;
263 prev_nsize = nsize;
264 node = parent;
266 parent = get_parent(node);
267 if (!parent)
268 break;
270 dt_get_reg_format(parent, &naddr, &nsize);
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;
279 offset = find_range(last_addr, prop_buf, prev_naddr,
280 naddr, prev_nsize, buflen / 4);
282 if (offset < 0)
283 return 0;
285 copy_val(this_addr, prop_buf + offset, prev_naddr);
287 if (!sub_reg(last_addr, this_addr))
288 return 0;
290 copy_val(this_addr, prop_buf + offset + prev_naddr, naddr);
292 if (!add_reg(last_addr, this_addr, naddr))
293 return 0;
296 if (naddr > 2)
297 return 0;
299 ret_addr = ((u64)last_addr[2] << 32) | last_addr[3];
301 if (sizeof(void *) == 4 &&
302 (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL ||
303 ret_addr + ret_size > 0x100000000ULL))
304 return 0;
306 *addr = ret_addr;
307 if (size)
308 *size = ret_size;
310 return 1;
313 int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size)
315 int reglen;
317 reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4;
318 return dt_xlate(node, res, reglen, addr, size);
321 int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr)
324 if (buflen > sizeof(prop_buf))
325 return 0;
327 memcpy(prop_buf, buf, buflen);
328 return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL);
331 int dt_is_compatible(void *node, const char *compat)
333 char *buf = (char *)prop_buf;
334 int len, pos;
336 len = getprop(node, "compatible", buf, MAX_PROP_LEN);
337 if (len < 0)
338 return 0;
340 for (pos = 0; pos < len; pos++) {
341 if (!strcmp(buf + pos, compat))
342 return 1;
344 pos += strnlen(&buf[pos], len - pos);
347 return 0;
350 int dt_get_virtual_reg(void *node, void **addr, int nres)
352 unsigned long xaddr;
353 int n;
355 n = getprop(node, "virtual-reg", addr, nres * 4);
356 if (n > 0)
357 return n / 4;
359 for (n = 0; n < nres; n++) {
360 if (!dt_xlate_reg(node, n, &xaddr, NULL))
361 break;
363 addr[n] = (void *)xaddr;
366 return n;