blob: 549463bf5eec2dc978fe56fe702965037b6b221e [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
91void __dt_fixup_mac_addresses(u32 startindex, ...)
92{
93 va_list ap;
94 u32 index = startindex;
95 void *devp;
96 const u8 *addr;
97
98 va_start(ap, startindex);
99 while ((addr = va_arg(ap, const u8 *))) {
100 devp = find_node_by_prop_value(NULL, "linux,network-index",
101 (void*)&index, sizeof(index));
102
Scott Wood96ebc3b2007-09-06 05:21:18 +1000103 if (devp) {
104 printf("ENET%d: local-mac-address <-"
105 " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index,
106 addr[0], addr[1], addr[2],
107 addr[3], addr[4], addr[5]);
David Gibson27fbaa92007-03-22 17:02:21 +1100108
David Gibson27fbaa92007-03-22 17:02:21 +1100109 setprop(devp, "local-mac-address", addr, 6);
Scott Wood96ebc3b2007-09-06 05:21:18 +1000110 }
David Gibson27fbaa92007-03-22 17:02:21 +1100111
112 index++;
113 }
114 va_end(ap);
115}
Scott Wood6e1af382007-03-26 15:52:24 -0500116
117#define MAX_ADDR_CELLS 4
Scott Wood6e1af382007-03-26 15:52:24 -0500118
Scott Woode5d8d542007-08-21 03:40:02 +1000119void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize)
Scott Wood6e1af382007-03-26 15:52:24 -0500120{
121 if (getprop(node, "#address-cells", naddr, 4) != 4)
122 *naddr = 2;
123 if (getprop(node, "#size-cells", nsize, 4) != 4)
124 *nsize = 1;
125}
126
127static void copy_val(u32 *dest, u32 *src, int naddr)
128{
Scott Woode4bb6882007-04-27 03:08:13 +1000129 int pad = MAX_ADDR_CELLS - naddr;
130
131 memset(dest, 0, pad * 4);
132 memcpy(dest + pad, src, naddr * 4);
Scott Wood6e1af382007-03-26 15:52:24 -0500133}
134
135static int sub_reg(u32 *reg, u32 *sub)
136{
137 int i, borrow = 0;
138
Scott Woode4bb6882007-04-27 03:08:13 +1000139 for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) {
Scott Wood6e1af382007-03-26 15:52:24 -0500140 int prev_borrow = borrow;
141 borrow = reg[i] < sub[i] + prev_borrow;
142 reg[i] -= sub[i] + prev_borrow;
143 }
144
145 return !borrow;
146}
147
Scott Woode4bb6882007-04-27 03:08:13 +1000148static int add_reg(u32 *reg, u32 *add, int naddr)
Scott Wood6e1af382007-03-26 15:52:24 -0500149{
150 int i, carry = 0;
151
Scott Woode4bb6882007-04-27 03:08:13 +1000152 for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) {
Scott Wood6e1af382007-03-26 15:52:24 -0500153 u64 tmp = (u64)reg[i] + add[i] + carry;
154 carry = tmp >> 32;
155 reg[i] = (u32)tmp;
156 }
157
158 return !carry;
159}
160
161/* It is assumed that if the first byte of reg fits in a
162 * range, then the whole reg block fits.
163 */
164static int compare_reg(u32 *reg, u32 *range, u32 *rangesize)
165{
166 int i;
167 u32 end;
168
169 for (i = 0; i < MAX_ADDR_CELLS; i++) {
170 if (reg[i] < range[i])
171 return 0;
172 if (reg[i] > range[i])
173 break;
174 }
175
176 for (i = 0; i < MAX_ADDR_CELLS; i++) {
177 end = range[i] + rangesize[i];
178
179 if (reg[i] < end)
180 break;
181 if (reg[i] > end)
182 return 0;
183 }
184
185 return reg[i] != end;
186}
187
188/* reg must be MAX_ADDR_CELLS */
189static int find_range(u32 *reg, u32 *ranges, int nregaddr,
190 int naddr, int nsize, int buflen)
191{
192 int nrange = nregaddr + naddr + nsize;
193 int i;
194
195 for (i = 0; i + nrange <= buflen; i += nrange) {
196 u32 range_addr[MAX_ADDR_CELLS];
197 u32 range_size[MAX_ADDR_CELLS];
198
199 copy_val(range_addr, ranges + i, naddr);
200 copy_val(range_size, ranges + i + nregaddr + naddr, nsize);
201
202 if (compare_reg(reg, range_addr, range_size))
203 return i;
204 }
205
206 return -1;
207}
208
209/* Currently only generic buses without special encodings are supported.
210 * In particular, PCI is not supported. Also, only the beginning of the
211 * reg block is tracked; size is ignored except in ranges.
212 */
Scott Wooda73ac502007-08-21 03:39:48 +1000213static u32 prop_buf[MAX_PROP_LEN / 4];
Mark A. Greer8895ea42007-04-28 06:48:24 +1000214
215static int dt_xlate(void *node, int res, int reglen, unsigned long *addr,
216 unsigned long *size)
Scott Wood6e1af382007-03-26 15:52:24 -0500217{
218 u32 last_addr[MAX_ADDR_CELLS];
219 u32 this_addr[MAX_ADDR_CELLS];
Scott Wood6e1af382007-03-26 15:52:24 -0500220 void *parent;
221 u64 ret_addr, ret_size;
Scott Wood06028012007-08-21 03:39:46 +1000222 u32 naddr, nsize, prev_naddr, prev_nsize;
Scott Wood6e1af382007-03-26 15:52:24 -0500223 int buflen, offset;
224
225 parent = get_parent(node);
226 if (!parent)
227 return 0;
228
Scott Woode5d8d542007-08-21 03:40:02 +1000229 dt_get_reg_format(parent, &naddr, &nsize);
Scott Wood6e1af382007-03-26 15:52:24 -0500230
231 if (nsize > 2)
232 return 0;
233
Scott Wood6e1af382007-03-26 15:52:24 -0500234 offset = (naddr + nsize) * res;
235
Mark A. Greer8895ea42007-04-28 06:48:24 +1000236 if (reglen < offset + naddr + nsize ||
Scott Wooda73ac502007-08-21 03:39:48 +1000237 MAX_PROP_LEN < (offset + naddr + nsize) * 4)
Scott Wood6e1af382007-03-26 15:52:24 -0500238 return 0;
239
Scott Wooda73ac502007-08-21 03:39:48 +1000240 copy_val(last_addr, prop_buf + offset, naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500241
Scott Wooda73ac502007-08-21 03:39:48 +1000242 ret_size = prop_buf[offset + naddr];
Scott Wood6e1af382007-03-26 15:52:24 -0500243 if (nsize == 2) {
244 ret_size <<= 32;
Scott Wooda73ac502007-08-21 03:39:48 +1000245 ret_size |= prop_buf[offset + naddr + 1];
Scott Wood6e1af382007-03-26 15:52:24 -0500246 }
247
Scott Wood06028012007-08-21 03:39:46 +1000248 for (;;) {
Scott Wood6e1af382007-03-26 15:52:24 -0500249 prev_naddr = naddr;
Scott Wood06028012007-08-21 03:39:46 +1000250 prev_nsize = nsize;
251 node = parent;
Scott Wood6e1af382007-03-26 15:52:24 -0500252
Scott Wood06028012007-08-21 03:39:46 +1000253 parent = get_parent(node);
254 if (!parent)
255 break;
256
Scott Woode5d8d542007-08-21 03:40:02 +1000257 dt_get_reg_format(parent, &naddr, &nsize);
Scott Wood6e1af382007-03-26 15:52:24 -0500258
Scott Wooda73ac502007-08-21 03:39:48 +1000259 buflen = getprop(node, "ranges", prop_buf,
260 sizeof(prop_buf));
Scott Wood06028012007-08-21 03:39:46 +1000261 if (buflen == 0)
Scott Wood6e1af382007-03-26 15:52:24 -0500262 continue;
Scott Wooda73ac502007-08-21 03:39:48 +1000263 if (buflen < 0 || buflen > sizeof(prop_buf))
Scott Wood6e1af382007-03-26 15:52:24 -0500264 return 0;
265
Scott Wooda73ac502007-08-21 03:39:48 +1000266 offset = find_range(last_addr, prop_buf, prev_naddr,
Scott Wood06028012007-08-21 03:39:46 +1000267 naddr, prev_nsize, buflen / 4);
Scott Wood6e1af382007-03-26 15:52:24 -0500268
269 if (offset < 0)
270 return 0;
271
Scott Wooda73ac502007-08-21 03:39:48 +1000272 copy_val(this_addr, prop_buf + offset, prev_naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500273
274 if (!sub_reg(last_addr, this_addr))
275 return 0;
276
Scott Wooda73ac502007-08-21 03:39:48 +1000277 copy_val(this_addr, prop_buf + offset + prev_naddr, naddr);
Scott Wood6e1af382007-03-26 15:52:24 -0500278
Scott Woode4bb6882007-04-27 03:08:13 +1000279 if (!add_reg(last_addr, this_addr, naddr))
Scott Wood6e1af382007-03-26 15:52:24 -0500280 return 0;
281 }
282
283 if (naddr > 2)
284 return 0;
285
Scott Woode4bb6882007-04-27 03:08:13 +1000286 ret_addr = ((u64)last_addr[2] << 32) | last_addr[3];
Scott Wood6e1af382007-03-26 15:52:24 -0500287
288 if (sizeof(void *) == 4 &&
289 (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL ||
290 ret_addr + ret_size > 0x100000000ULL))
291 return 0;
292
293 *addr = ret_addr;
294 if (size)
295 *size = ret_size;
296
297 return 1;
298}
Mark A. Greer8895ea42007-04-28 06:48:24 +1000299
300int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size)
301{
302 int reglen;
303
Scott Wooda73ac502007-08-21 03:39:48 +1000304 reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4;
Mark A. Greer8895ea42007-04-28 06:48:24 +1000305 return dt_xlate(node, res, reglen, addr, size);
306}
307
308int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr)
309{
310
Scott Wooda73ac502007-08-21 03:39:48 +1000311 if (buflen > sizeof(prop_buf))
Mark A. Greer8895ea42007-04-28 06:48:24 +1000312 return 0;
313
Scott Wooda73ac502007-08-21 03:39:48 +1000314 memcpy(prop_buf, buf, buflen);
Mark A. Greer8895ea42007-04-28 06:48:24 +1000315 return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL);
316}
Scott Wooda73ac502007-08-21 03:39:48 +1000317
318int dt_is_compatible(void *node, const char *compat)
319{
320 char *buf = (char *)prop_buf;
321 int len, pos;
322
323 len = getprop(node, "compatible", buf, MAX_PROP_LEN);
324 if (len < 0)
325 return 0;
326
327 for (pos = 0; pos < len; pos++) {
328 if (!strcmp(buf + pos, compat))
329 return 1;
330
331 pos += strnlen(&buf[pos], len - pos);
332 }
333
334 return 0;
335}