blob: e5dfe449731393cb7aec718b4fd169964309def0 [file] [log] [blame]
David Gibson27fbaa92007-03-22 17:02:21 +11001/*
2 * devtree.c - convenience functions for device tree manipulation
3 * Copyright 2007 David Gibson, IBM Corporation.
4 * Copyright (c) 2007 Freescale Semiconductor, Inc.
5 *
6 * Authors: David Gibson <david@gibson.dropbear.id.au>
7 * Scott Wood <scottwood@freescale.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14#include <stdarg.h>
15#include <stddef.h>
16#include "types.h"
17#include "string.h"
18#include "stdio.h"
19#include "ops.h"
20
21void dt_fixup_memory(u64 start, u64 size)
22{
23 void *root, *memory;
24 int naddr, nsize, i;
25 u32 memreg[4];
26
27 root = finddevice("/");
28 if (getprop(root, "#address-cells", &naddr, sizeof(naddr)) < 0)
29 naddr = 2;
30 if (naddr < 1 || naddr > 2)
31 fatal("Can't cope with #address-cells == %d in /\n\r", naddr);
32
33 if (getprop(root, "#size-cells", &nsize, sizeof(nsize)) < 0)
34 nsize = 1;
35 if (nsize < 1 || nsize > 2)
36 fatal("Can't cope with #size-cells == %d in /\n\r", nsize);
37
38 i = 0;
39 if (naddr == 2)
40 memreg[i++] = start >> 32;
41 memreg[i++] = start & 0xffffffff;
42 if (nsize == 2)
43 memreg[i++] = size >> 32;
44 memreg[i++] = size & 0xffffffff;
45
46 memory = finddevice("/memory");
47 if (! memory) {
48 memory = create_node(NULL, "memory");
49 setprop_str(memory, "device_type", "memory");
50 }
51
52 printf("Memory <- <0x%x", memreg[0]);
53 for (i = 1; i < (naddr + nsize); i++)
54 printf(" 0x%x", memreg[i]);
55 printf("> (%ldMB)\n\r", (unsigned long)(size >> 20));
56
57 setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32));
58}
59
60#define MHZ(x) ((x + 500000) / 1000000)
61
62void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus)
63{
64 void *devp = NULL;
65
66 printf("CPU clock-frequency <- 0x%x (%dMHz)\n\r", cpu, MHZ(cpu));
67 printf("CPU timebase-frequency <- 0x%x (%dMHz)\n\r", tb, MHZ(tb));
68 if (bus > 0)
69 printf("CPU bus-frequency <- 0x%x (%dMHz)\n\r", bus, MHZ(bus));
70
71 while ((devp = find_node_by_devtype(devp, "cpu"))) {
72 setprop_val(devp, "clock-frequency", cpu);
73 setprop_val(devp, "timebase-frequency", tb);
74 if (bus > 0)
75 setprop_val(devp, "bus-frequency", bus);
76 }
Scott Wood643d3c12007-08-21 03:39:45 +100077
78 timebase_period_ns = 1000000000 / tb;
David Gibson27fbaa92007-03-22 17:02:21 +110079}
80
81void dt_fixup_clock(const char *path, u32 freq)
82{
83 void *devp = finddevice(path);
84
85 if (devp) {
86 printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq));
87 setprop_val(devp, "clock-frequency", freq);
88 }
89}
90
Scott Wood27ff35d2007-09-25 06:09:11 +100091void dt_fixup_mac_address(u32 index, const u8 *addr)
92{
93 void *devp = find_node_by_prop_value(NULL, "linux,network-index",
94 (void*)&index, sizeof(index));
95
96 if (devp) {
97 printf("ENET%d: local-mac-address <-"
98 " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index,
99 addr[0], addr[1], addr[2],
100 addr[3], addr[4], addr[5]);
101
102 setprop(devp, "local-mac-address", addr, 6);
103 }
104}
105
David Gibson27fbaa92007-03-22 17:02:21 +1100106void __dt_fixup_mac_addresses(u32 startindex, ...)
107{
108 va_list ap;
109 u32 index = startindex;
David Gibson27fbaa92007-03-22 17:02:21 +1100110 const u8 *addr;
111
112 va_start(ap, startindex);
David Gibson27fbaa92007-03-22 17:02:21 +1100113
Scott Wood27ff35d2007-09-25 06:09:11 +1000114 while ((addr = va_arg(ap, const u8 *)))
115 dt_fixup_mac_address(index++, addr);
David Gibson27fbaa92007-03-22 17:02:21 +1100116
David Gibson27fbaa92007-03-22 17:02:21 +1100117 va_end(ap);
118}
Scott Wood6e1af382007-03-26 15:52:24 -0500119
120#define MAX_ADDR_CELLS 4
Scott Wood6e1af382007-03-26 15:52:24 -0500121
Scott Woode5d8d542007-08-21 03:40:02 +1000122void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize)
Scott Wood6e1af382007-03-26 15:52:24 -0500123{
124 if (getprop(node, "#address-cells", naddr, 4) != 4)
125 *naddr = 2;
126 if (getprop(node, "#size-cells", nsize, 4) != 4)
127 *nsize = 1;
128}
129
130static void copy_val(u32 *dest, u32 *src, int naddr)
131{
Scott Woode4bb6882007-04-27 03:08:13 +1000132 int pad = MAX_ADDR_CELLS - naddr;
133
134 memset(dest, 0, pad * 4);
135 memcpy(dest + pad, src, naddr * 4);
Scott Wood6e1af382007-03-26 15:52:24 -0500136}
137
138static int sub_reg(u32 *reg, u32 *sub)
139{
140 int i, borrow = 0;
141
Scott Woode4bb6882007-04-27 03:08:13 +1000142 for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) {
Scott Wood6e1af382007-03-26 15:52:24 -0500143 int prev_borrow = borrow;
144 borrow = reg[i] < sub[i] + prev_borrow;
145 reg[i] -= sub[i] + prev_borrow;
146 }
147
148 return !borrow;
149}
150
Scott Woode4bb6882007-04-27 03:08:13 +1000151static int add_reg(u32 *reg, u32 *add, int naddr)
Scott Wood6e1af382007-03-26 15:52:24 -0500152{
153 int i, carry = 0;
154
Scott Woode4bb6882007-04-27 03:08:13 +1000155 for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) {
Scott Wood6e1af382007-03-26 15:52:24 -0500156 u64 tmp = (u64)reg[i] + add[i] + carry;
157 carry = tmp >> 32;
158 reg[i] = (u32)tmp;
159 }
160
161 return !carry;
162}
163
164/* It is assumed that if the first byte of reg fits in a
165 * range, then the whole reg block fits.
166 */
167static int compare_reg(u32 *reg, u32 *range, u32 *rangesize)
168{
169 int i;
170 u32 end;
171
172 for (i = 0; i < MAX_ADDR_CELLS; i++) {
173 if (reg[i] < range[i])
174 return 0;
175 if (reg[i] > range[i])
176 break;
177 }
178
179 for (i = 0; i < MAX_ADDR_CELLS; i++) {
180 end = range[i] + rangesize[i];
181
182 if (reg[i] < end)
183 break;
184 if (reg[i] > end)
185 return 0;
186 }
187
188 return reg[i] != end;
189}
190
191/* reg must be MAX_ADDR_CELLS */
192static int find_range(u32 *reg, u32 *ranges, int nregaddr,
193 int naddr, int nsize, int buflen)
194{
195 int nrange = nregaddr + naddr + nsize;
196 int i;
197
198 for (i = 0; i + nrange <= buflen; i += nrange) {
199 u32 range_addr[MAX_ADDR_CELLS];
200 u32 range_size[MAX_ADDR_CELLS];
201
202 copy_val(range_addr, ranges + i, naddr);
203 copy_val(range_size, ranges + i + nregaddr + naddr, nsize);
204
205 if (compare_reg(reg, range_addr, range_size))
206 return i;
207 }
208
209 return -1;
210}
211
212/* Currently only generic buses without special encodings are supported.
213 * In particular, PCI is not supported. Also, only the beginning of the
214 * reg block is tracked; size is ignored except in ranges.
215 */
Scott Wooda73ac502007-08-21 03:39:48 +1000216static u32 prop_buf[MAX_PROP_LEN / 4];
Mark A. Greer8895ea42007-04-28 06:48:24 +1000217
218static int dt_xlate(void *node, int res, int reglen, unsigned long *addr,
219 unsigned long *size)
Scott Wood6e1af382007-03-26 15:52:24 -0500220{
221 u32 last_addr[MAX_ADDR_CELLS];
222 u32 this_addr[MAX_ADDR_CELLS];
Scott Wood6e1af382007-03-26 15:52:24 -0500223 void *parent;
224 u64 ret_addr, ret_size;
Scott Wood06028012007-08-21 03:39:46 +1000225 u32 naddr, nsize, prev_naddr, prev_nsize;
Scott Wood6e1af382007-03-26 15:52:24 -0500226 int buflen, offset;
227
228 parent = get_parent(node);
229 if (!parent)
230 return 0;
231
Scott Woode5d8d542007-08-21 03:40:02 +1000232 dt_get_reg_format(parent, &naddr, &nsize);
Scott Wood6e1af382007-03-26 15:52:24 -0500233
234 if (nsize > 2)
235 return 0;
236
Scott Wood6e1af382007-03-26 15:52:24 -0500237 offset = (naddr + nsize) * res;
238
Mark A. Greer8895ea42007-04-28 06:48:24 +1000239 if (reglen < offset + naddr + nsize ||
Scott Wooda73ac502007-08-21 03:39:48 +1000240 MAX_PROP_LEN < (offset + naddr + nsize) * 4)
Scott Wood6e1af382007-03-26 15:52:24 -0500241 return 0;
242
Scott Wooda73ac502007-08-21 03:39:48 +1000243 copy_val(last_addr, prop_buf + offset, naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500244
Scott Wooda73ac502007-08-21 03:39:48 +1000245 ret_size = prop_buf[offset + naddr];
Scott Wood6e1af382007-03-26 15:52:24 -0500246 if (nsize == 2) {
247 ret_size <<= 32;
Scott Wooda73ac502007-08-21 03:39:48 +1000248 ret_size |= prop_buf[offset + naddr + 1];
Scott Wood6e1af382007-03-26 15:52:24 -0500249 }
250
Scott Wood06028012007-08-21 03:39:46 +1000251 for (;;) {
Scott Wood6e1af382007-03-26 15:52:24 -0500252 prev_naddr = naddr;
Scott Wood06028012007-08-21 03:39:46 +1000253 prev_nsize = nsize;
254 node = parent;
Scott Wood6e1af382007-03-26 15:52:24 -0500255
Scott Wood06028012007-08-21 03:39:46 +1000256 parent = get_parent(node);
257 if (!parent)
258 break;
259
Scott Woode5d8d542007-08-21 03:40:02 +1000260 dt_get_reg_format(parent, &naddr, &nsize);
Scott Wood6e1af382007-03-26 15:52:24 -0500261
Scott Wooda73ac502007-08-21 03:39:48 +1000262 buflen = getprop(node, "ranges", prop_buf,
263 sizeof(prop_buf));
Scott Wood06028012007-08-21 03:39:46 +1000264 if (buflen == 0)
Scott Wood6e1af382007-03-26 15:52:24 -0500265 continue;
Scott Wooda73ac502007-08-21 03:39:48 +1000266 if (buflen < 0 || buflen > sizeof(prop_buf))
Scott Wood6e1af382007-03-26 15:52:24 -0500267 return 0;
268
Scott Wooda73ac502007-08-21 03:39:48 +1000269 offset = find_range(last_addr, prop_buf, prev_naddr,
Scott Wood06028012007-08-21 03:39:46 +1000270 naddr, prev_nsize, buflen / 4);
Scott Wood6e1af382007-03-26 15:52:24 -0500271
272 if (offset < 0)
273 return 0;
274
Scott Wooda73ac502007-08-21 03:39:48 +1000275 copy_val(this_addr, prop_buf + offset, prev_naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500276
277 if (!sub_reg(last_addr, this_addr))
278 return 0;
279
Scott Wooda73ac502007-08-21 03:39:48 +1000280 copy_val(this_addr, prop_buf + offset + prev_naddr, naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500281
Scott Woode4bb6882007-04-27 03:08:13 +1000282 if (!add_reg(last_addr, this_addr, naddr))
Scott Wood6e1af382007-03-26 15:52:24 -0500283 return 0;
284 }
285
286 if (naddr > 2)
287 return 0;
288
Scott Woode4bb6882007-04-27 03:08:13 +1000289 ret_addr = ((u64)last_addr[2] << 32) | last_addr[3];
Scott Wood6e1af382007-03-26 15:52:24 -0500290
291 if (sizeof(void *) == 4 &&
292 (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL ||
293 ret_addr + ret_size > 0x100000000ULL))
294 return 0;
295
296 *addr = ret_addr;
297 if (size)
298 *size = ret_size;
299
300 return 1;
301}
Mark A. Greer8895ea42007-04-28 06:48:24 +1000302
303int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size)
304{
305 int reglen;
306
Scott Wooda73ac502007-08-21 03:39:48 +1000307 reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4;
Mark A. Greer8895ea42007-04-28 06:48:24 +1000308 return dt_xlate(node, res, reglen, addr, size);
309}
310
311int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr)
312{
313
Scott Wooda73ac502007-08-21 03:39:48 +1000314 if (buflen > sizeof(prop_buf))
Mark A. Greer8895ea42007-04-28 06:48:24 +1000315 return 0;
316
Scott Wooda73ac502007-08-21 03:39:48 +1000317 memcpy(prop_buf, buf, buflen);
Mark A. Greer8895ea42007-04-28 06:48:24 +1000318 return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL);
319}
Scott Wooda73ac502007-08-21 03:39:48 +1000320
321int dt_is_compatible(void *node, const char *compat)
322{
323 char *buf = (char *)prop_buf;
324 int len, pos;
325
326 len = getprop(node, "compatible", buf, MAX_PROP_LEN);
327 if (len < 0)
328 return 0;
329
330 for (pos = 0; pos < len; pos++) {
331 if (!strcmp(buf + pos, compat))
332 return 1;
333
334 pos += strnlen(&buf[pos], len - pos);
335 }
336
337 return 0;
338}