blob: a3f7442c324a3b873bd630144c3afa883059b320 [file] [log] [blame]
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04001/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
Shengzhou Liu2a523f52011-10-14 16:26:05 +08005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Kumar Galaa0342c02010-06-16 14:27:38 -05006 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04008 */
9
10#include <common.h>
Anton Vorontsov3e303f72009-10-15 17:47:04 +040011#include <stdio_dev.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040012#include <linux/ctype.h>
13#include <linux/types.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040014#include <asm/global_data.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040015#include <libfdt.h>
16#include <fdt_support.h>
Kumar Gala151c8b02007-11-26 17:06:15 -060017#include <exports.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040018
19/*
20 * Global data (for the gd->bd)
21 */
22DECLARE_GLOBAL_DATA_PTR;
23
David Fengf77a6062013-12-14 11:47:29 +080024/*
25 * Get cells len in bytes
26 * if #NNNN-cells property is 2 then len is 8
27 * otherwise len is 4
28 */
29static int get_cells_len(void *blob, char *nr_cells_name)
30{
31 const fdt32_t *cell;
32
33 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
34 if (cell && fdt32_to_cpu(*cell) == 2)
35 return 8;
36
37 return 4;
38}
39
40/*
41 * Write a 4 or 8 byte big endian cell
42 */
43static void write_cell(u8 *addr, u64 val, int size)
44{
45 int shift = (size - 1) * 8;
46 while (size-- > 0) {
47 *addr++ = (val >> shift) & 0xff;
48 shift -= 8;
49 }
50}
51
Kumar Gala3bed2aa2008-10-23 00:05:47 -050052/**
53 * fdt_getprop_u32_default - Find a node and return it's property or a default
54 *
55 * @fdt: ptr to device tree
56 * @path: path of node
57 * @prop: property name
58 * @dflt: default value if the property isn't found
59 *
60 * Convenience function to find a node and return it's property or a
61 * default value if it doesn't exist.
62 */
Gabe Black07e12782011-11-08 01:09:44 -080063u32 fdt_getprop_u32_default(const void *fdt, const char *path,
64 const char *prop, const u32 dflt)
Kumar Gala3bed2aa2008-10-23 00:05:47 -050065{
Kim Phillips8aa5ec62013-01-16 14:00:11 +000066 const fdt32_t *val;
Kumar Gala3bed2aa2008-10-23 00:05:47 -050067 int off;
68
69 off = fdt_path_offset(fdt, path);
70 if (off < 0)
71 return dflt;
72
73 val = fdt_getprop(fdt, off, prop, NULL);
74 if (val)
Gabe Blackde166062011-11-08 01:05:32 -080075 return fdt32_to_cpu(*val);
Kumar Gala3bed2aa2008-10-23 00:05:47 -050076 else
77 return dflt;
78}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040079
Kumar Galaa3c29332007-10-24 10:21:57 -050080/**
81 * fdt_find_and_setprop: Find a node and set it's property
82 *
83 * @fdt: ptr to device tree
84 * @node: path of node
85 * @prop: property name
86 * @val: ptr to new value
87 * @len: length of new property value
88 * @create: flag to create the property if it doesn't exist
89 *
90 * Convenience function to directly set a property given the path to the node.
91 */
92int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
93 const void *val, int len, int create)
94{
Kumar Gala8d04f022007-10-24 11:04:22 -050095 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -050096
97 if (nodeoff < 0)
98 return nodeoff;
99
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000100 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galaa3c29332007-10-24 10:21:57 -0500101 return 0; /* create flag not set; so exit quietly */
102
103 return fdt_setprop(fdt, nodeoff, prop, val, len);
104}
105
Kumar Gala151c8b02007-11-26 17:06:15 -0600106#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400107
Marek Vasut036036d2012-09-14 23:45:51 +0200108#ifdef CONFIG_CONS_INDEX
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400109static void fdt_fill_multisername(char *sername, size_t maxlen)
110{
111 const char *outname = stdio_devices[stdout]->name;
112
113 if (strcmp(outname, "serial") > 0)
114 strncpy(sername, outname, maxlen);
115
116 /* eserial? */
117 if (strcmp(outname + 1, "serial") > 0)
118 strncpy(sername, outname + 1, maxlen);
119}
Marek Vasut036036d2012-09-14 23:45:51 +0200120#endif
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400121
Detlev Zundel40777812008-06-20 22:24:05 +0200122static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -0600123{
124 int err = 0;
125#ifdef CONFIG_CONS_INDEX
126 int node;
127 char sername[9] = { 0 };
128 const char *path;
129
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400130 fdt_fill_multisername(sername, sizeof(sername) - 1);
131 if (!sername[0])
132 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600133
134 err = node = fdt_path_offset(fdt, "/aliases");
135 if (node >= 0) {
136 int len;
137 path = fdt_getprop(fdt, node, sername, &len);
138 if (path) {
139 char *p = malloc(len);
140 err = -FDT_ERR_NOSPACE;
141 if (p) {
142 memcpy(p, path, len);
Detlev Zundel40777812008-06-20 22:24:05 +0200143 err = fdt_setprop(fdt, chosenoff,
Kumar Gala151c8b02007-11-26 17:06:15 -0600144 "linux,stdout-path", p, len);
145 free(p);
146 }
147 } else {
148 err = len;
149 }
150 }
151#endif
152 if (err < 0)
153 printf("WARNING: could not set linux,stdout-path %s.\n",
154 fdt_strerror(err));
155
156 return err;
157}
158#endif
159
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500160int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
161{
David Fengf77a6062013-12-14 11:47:29 +0800162 int nodeoffset, addr_cell_len;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500163 int err, j, total;
David Fengf77a6062013-12-14 11:47:29 +0800164 fdt64_t tmp;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500165 const char *path;
166 uint64_t addr, size;
167
168 /* Find the "chosen" node. */
169 nodeoffset = fdt_path_offset (fdt, "/chosen");
170
171 /* If there is no "chosen" node in the blob return */
172 if (nodeoffset < 0) {
173 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
174 return nodeoffset;
175 }
176
177 /* just return if initrd_start/end aren't valid */
178 if ((initrd_start == 0) || (initrd_end == 0))
179 return 0;
180
181 total = fdt_num_mem_rsv(fdt);
182
183 /*
184 * Look for an existing entry and update it. If we don't find
185 * the entry, we will j be the next available slot.
186 */
187 for (j = 0; j < total; j++) {
188 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
189 if (addr == initrd_start) {
190 fdt_del_mem_rsv(fdt, j);
191 break;
192 }
193 }
194
Grant Likelyce6b27a2011-03-28 09:58:55 +0000195 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500196 if (err < 0) {
197 printf("fdt_initrd: %s\n", fdt_strerror(err));
198 return err;
199 }
200
David Fengf77a6062013-12-14 11:47:29 +0800201 addr_cell_len = get_cells_len(fdt, "#address-cells");
202
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500203 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
204 if ((path == NULL) || force) {
David Fengf77a6062013-12-14 11:47:29 +0800205 write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500206 err = fdt_setprop(fdt, nodeoffset,
207 "linux,initrd-start", &tmp, sizeof(tmp));
208 if (err < 0) {
209 printf("WARNING: "
210 "could not set linux,initrd-start %s.\n",
211 fdt_strerror(err));
212 return err;
213 }
David Fengf77a6062013-12-14 11:47:29 +0800214 write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500215 err = fdt_setprop(fdt, nodeoffset,
216 "linux,initrd-end", &tmp, sizeof(tmp));
217 if (err < 0) {
218 printf("WARNING: could not set linux,initrd-end %s.\n",
219 fdt_strerror(err));
220
221 return err;
222 }
223 }
224
225 return 0;
226}
227
Heiko Schocher56844a22008-09-11 08:11:23 +0200228int fdt_chosen(void *fdt, int force)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400229{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400230 int nodeoffset;
231 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400232 char *str; /* used to set string properties */
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500233 const char *path;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400234
235 err = fdt_check_header(fdt);
236 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400237 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400238 return err;
239 }
240
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400241 /*
242 * Find the "chosen" node.
243 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500244 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400245
246 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500247 * If there is no "chosen" node in the blob, create it.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400248 */
249 if (nodeoffset < 0) {
250 /*
251 * Create a new node "/chosen" (offset 0 is root level)
252 */
253 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
254 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400255 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400256 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400257 return nodeoffset;
258 }
259 }
260
261 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500262 * Create /chosen properites that don't exist in the fdt.
263 * If the property exists, update it only if the "force" parameter
264 * is true.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400265 */
266 str = getenv("bootargs");
267 if (str != NULL) {
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500268 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
269 if ((path == NULL) || force) {
270 err = fdt_setprop(fdt, nodeoffset,
271 "bootargs", str, strlen(str)+1);
272 if (err < 0)
273 printf("WARNING: could not set bootargs %s.\n",
274 fdt_strerror(err));
275 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400276 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500277
Kumar Gala151c8b02007-11-26 17:06:15 -0600278#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500279 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
280 if ((path == NULL) || force)
281 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala151c8b02007-11-26 17:06:15 -0600282#endif
283
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400284#ifdef OF_STDOUT_PATH
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500285 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
286 if ((path == NULL) || force) {
287 err = fdt_setprop(fdt, nodeoffset,
288 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
289 if (err < 0)
290 printf("WARNING: could not set linux,stdout-path %s.\n",
291 fdt_strerror(err));
292 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400293#endif
294
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400295 return err;
296}
297
Kumar Galae93becf2007-11-03 19:46:28 -0500298void do_fixup_by_path(void *fdt, const char *path, const char *prop,
299 const void *val, int len, int create)
300{
301#if defined(DEBUG)
302 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600303 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500304 for (i = 0; i < len; i++)
305 debug(" %.2x", *(u8*)(val+i));
306 debug("\n");
307#endif
308 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
309 if (rc)
310 printf("Unable to update property %s:%s, err=%s\n",
311 path, prop, fdt_strerror(rc));
312}
313
314void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
315 u32 val, int create)
316{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000317 fdt32_t tmp = cpu_to_fdt32(val);
318 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Galae93becf2007-11-03 19:46:28 -0500319}
320
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600321void do_fixup_by_prop(void *fdt,
322 const char *pname, const void *pval, int plen,
323 const char *prop, const void *val, int len,
324 int create)
325{
326 int off;
327#if defined(DEBUG)
328 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600329 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600330 for (i = 0; i < len; i++)
331 debug(" %.2x", *(u8*)(val+i));
332 debug("\n");
333#endif
334 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
335 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000336 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600337 fdt_setprop(fdt, off, prop, val, len);
338 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
339 }
340}
341
342void do_fixup_by_prop_u32(void *fdt,
343 const char *pname, const void *pval, int plen,
344 const char *prop, u32 val, int create)
345{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000346 fdt32_t tmp = cpu_to_fdt32(val);
347 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600348}
349
350void do_fixup_by_compat(void *fdt, const char *compat,
351 const char *prop, const void *val, int len, int create)
352{
353 int off = -1;
354#if defined(DEBUG)
355 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600356 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600357 for (i = 0; i < len; i++)
358 debug(" %.2x", *(u8*)(val+i));
359 debug("\n");
360#endif
361 off = fdt_node_offset_by_compatible(fdt, -1, compat);
362 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000363 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600364 fdt_setprop(fdt, off, prop, val, len);
365 off = fdt_node_offset_by_compatible(fdt, off, compat);
366 }
367}
368
369void do_fixup_by_compat_u32(void *fdt, const char *compat,
370 const char *prop, u32 val, int create)
371{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000372 fdt32_t tmp = cpu_to_fdt32(val);
373 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600374}
375
Doug Anderson5e574542013-04-30 10:22:00 +0000376#ifdef CONFIG_NR_DRAM_BANKS
377#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
378#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000379#define MEMORY_BANKS_MAX 4
Doug Anderson5e574542013-04-30 10:22:00 +0000380#endif
John Rigbya6bd9e82010-10-13 13:57:33 -0600381int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
382{
383 int err, nodeoffset;
384 int addr_cell_len, size_cell_len, len;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000385 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbya6bd9e82010-10-13 13:57:33 -0600386 int bank;
Kumar Gala3c927282007-11-26 14:57:45 -0600387
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000388 if (banks > MEMORY_BANKS_MAX) {
389 printf("%s: num banks %d exceeds hardcoded limit %d."
390 " Recompile with higher MEMORY_BANKS_MAX?\n",
391 __FUNCTION__, banks, MEMORY_BANKS_MAX);
392 return -1;
393 }
394
Kumar Gala3c927282007-11-26 14:57:45 -0600395 err = fdt_check_header(blob);
396 if (err < 0) {
397 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
398 return err;
399 }
400
401 /* update, or add and update /memory node */
402 nodeoffset = fdt_path_offset(blob, "/memory");
403 if (nodeoffset < 0) {
404 nodeoffset = fdt_add_subnode(blob, 0, "memory");
405 if (nodeoffset < 0)
406 printf("WARNING: could not create /memory: %s.\n",
407 fdt_strerror(nodeoffset));
408 return nodeoffset;
409 }
410 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
411 sizeof("memory"));
412 if (err < 0) {
413 printf("WARNING: could not set %s %s.\n", "device_type",
414 fdt_strerror(err));
415 return err;
416 }
417
John Rigbya6bd9e82010-10-13 13:57:33 -0600418 addr_cell_len = get_cells_len(blob, "#address-cells");
419 size_cell_len = get_cells_len(blob, "#size-cells");
Kumar Gala3c927282007-11-26 14:57:45 -0600420
John Rigbya6bd9e82010-10-13 13:57:33 -0600421 for (bank = 0, len = 0; bank < banks; bank++) {
422 write_cell(tmp + len, start[bank], addr_cell_len);
423 len += addr_cell_len;
424
425 write_cell(tmp + len, size[bank], size_cell_len);
426 len += size_cell_len;
Kumar Gala3c927282007-11-26 14:57:45 -0600427 }
428
429 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
430 if (err < 0) {
431 printf("WARNING: could not set %s %s.\n",
432 "reg", fdt_strerror(err));
433 return err;
434 }
435 return 0;
436}
437
John Rigbya6bd9e82010-10-13 13:57:33 -0600438int fdt_fixup_memory(void *blob, u64 start, u64 size)
439{
440 return fdt_fixup_memory_banks(blob, &start, &size, 1);
441}
442
Kumar Galaba37aa02008-08-19 15:41:18 -0500443void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600444{
Kumar Galaba37aa02008-08-19 15:41:18 -0500445 int node, i, j;
446 char enet[16], *tmp, *end;
Stephen Warren064d55f2013-05-27 18:01:19 +0000447 char mac[16];
Kumar Galaab544632007-11-21 11:11:03 -0600448 const char *path;
Kumar Galaba37aa02008-08-19 15:41:18 -0500449 unsigned char mac_addr[6];
Kumar Galaab544632007-11-21 11:11:03 -0600450
451 node = fdt_path_offset(fdt, "/aliases");
Kumar Galaba37aa02008-08-19 15:41:18 -0500452 if (node < 0)
453 return;
454
455 i = 0;
Stephen Warren064d55f2013-05-27 18:01:19 +0000456 strcpy(mac, "ethaddr");
Kumar Galaba37aa02008-08-19 15:41:18 -0500457 while ((tmp = getenv(mac)) != NULL) {
458 sprintf(enet, "ethernet%d", i);
459 path = fdt_getprop(fdt, node, enet, NULL);
460 if (!path) {
461 debug("No alias for %s\n", enet);
462 sprintf(mac, "eth%daddr", ++i);
463 continue;
Kumar Galaab544632007-11-21 11:11:03 -0600464 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500465
466 for (j = 0; j < 6; j++) {
467 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
468 if (tmp)
469 tmp = (*end) ? end+1 : end;
Kumar Galaab544632007-11-21 11:11:03 -0600470 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500471
472 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
473 do_fixup_by_path(fdt, path, "local-mac-address",
474 &mac_addr, 6, 1);
475
476 sprintf(mac, "eth%daddr", ++i);
Kumar Galaab544632007-11-21 11:11:03 -0600477 }
478}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300479
Kumar Gala3082d232008-08-15 08:24:42 -0500480/* Resize the fdt to its actual size + a bit of padding */
481int fdt_resize(void *blob)
482{
483 int i;
484 uint64_t addr, size;
485 int total, ret;
486 uint actualsize;
487
488 if (!blob)
489 return 0;
490
491 total = fdt_num_mem_rsv(blob);
492 for (i = 0; i < total; i++) {
493 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000494 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500495 fdt_del_mem_rsv(blob, i);
496 break;
497 }
498 }
499
Peter Korsgaardf242a082008-10-28 08:26:52 +0100500 /*
501 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200502 * plus the size needed for 5 fdt_add_mem_rsv, one
503 * for the fdt itself and 4 for a possible initrd
504 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100505 */
Kumar Gala3082d232008-08-15 08:24:42 -0500506 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200507 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500508
509 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000510 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
511 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500512
513 /* Change the fdt header to reflect the correct size */
514 fdt_set_totalsize(blob, actualsize);
515
516 /* Add the new reservation */
Simon Glass92549352011-09-17 06:48:58 +0000517 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Gala3082d232008-08-15 08:24:42 -0500518 if (ret < 0)
519 return ret;
520
521 return actualsize;
522}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500523
524#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500525#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500526
527#define FDT_PCI_PREFETCH (0x40000000)
528#define FDT_PCI_MEM32 (0x02000000)
529#define FDT_PCI_IO (0x01000000)
530#define FDT_PCI_MEM64 (0x03000000)
531
532int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
533
534 int addrcell, sizecell, len, r;
535 u32 *dma_range;
536 /* sized based on pci addr cells, size-cells, & address-cells */
537 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
538
539 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
540 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
541
542 dma_range = &dma_ranges[0];
543 for (r = 0; r < hose->region_count; r++) {
544 u64 bus_start, phys_start, size;
545
Kumar Galaff4e66e2009-02-06 09:49:31 -0600546 /* skip if !PCI_REGION_SYS_MEMORY */
547 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500548 continue;
549
550 bus_start = (u64)hose->regions[r].bus_start;
551 phys_start = (u64)hose->regions[r].phys_start;
552 size = (u64)hose->regions[r].size;
553
554 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500555 if (size >= 0x100000000ull)
Kumar Gala8ab451c2008-10-22 23:33:56 -0500556 dma_range[0] |= FDT_PCI_MEM64;
557 else
558 dma_range[0] |= FDT_PCI_MEM32;
559 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
560 dma_range[0] |= FDT_PCI_PREFETCH;
561#ifdef CONFIG_SYS_PCI_64BIT
562 dma_range[1] = bus_start >> 32;
563#else
564 dma_range[1] = 0;
565#endif
566 dma_range[2] = bus_start & 0xffffffff;
567
568 if (addrcell == 2) {
569 dma_range[3] = phys_start >> 32;
570 dma_range[4] = phys_start & 0xffffffff;
571 } else {
572 dma_range[3] = phys_start & 0xffffffff;
573 }
574
575 if (sizecell == 2) {
576 dma_range[3 + addrcell + 0] = size >> 32;
577 dma_range[3 + addrcell + 1] = size & 0xffffffff;
578 } else {
579 dma_range[3 + addrcell + 0] = size & 0xffffffff;
580 }
581
582 dma_range += (3 + addrcell + sizecell);
583 }
584
585 len = dma_range - &dma_ranges[0];
586 if (len)
587 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
588
589 return 0;
590}
591#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200592
593#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
594/*
Stefan Roese8a805df2010-09-16 14:01:53 +0200595 * Provide a weak default function to return the flash bank size.
596 * There might be multiple non-identical flash chips connected to one
597 * chip-select, so we need to pass an index as well.
598 */
599u32 __flash_get_bank_size(int cs, int idx)
600{
601 extern flash_info_t flash_info[];
602
603 /*
604 * As default, a simple 1:1 mapping is provided. Boards with
605 * a different mapping need to supply a board specific mapping
606 * routine.
607 */
608 return flash_info[cs].size;
609}
610u32 flash_get_bank_size(int cs, int idx)
611 __attribute__((weak, alias("__flash_get_bank_size")));
612
613/*
Stefan Roese30d45c02009-10-21 11:59:52 +0200614 * This function can be used to update the size in the "reg" property
Stefan Roese8a805df2010-09-16 14:01:53 +0200615 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roese30d45c02009-10-21 11:59:52 +0200616 * non-fixed NOR FLASH sizes.
617 */
Stefan Roese8a805df2010-09-16 14:01:53 +0200618int fdt_fixup_nor_flash_size(void *blob)
Stefan Roese30d45c02009-10-21 11:59:52 +0200619{
620 char compat[][16] = { "cfi-flash", "jedec-flash" };
621 int off;
622 int len;
623 struct fdt_property *prop;
Stefan Roese2778a012010-09-24 13:51:50 +0200624 u32 *reg, *reg2;
Stefan Roese30d45c02009-10-21 11:59:52 +0200625 int i;
626
627 for (i = 0; i < 2; i++) {
628 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
629 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200630 int idx;
631
Stefan Roese30d45c02009-10-21 11:59:52 +0200632 /*
Stefan Roese8a805df2010-09-16 14:01:53 +0200633 * Found one compatible node, so fixup the size
634 * int its reg properties
Stefan Roese30d45c02009-10-21 11:59:52 +0200635 */
636 prop = fdt_get_property_w(blob, off, "reg", &len);
637 if (prop) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200638 int tuple_size = 3 * sizeof(reg);
Stefan Roese30d45c02009-10-21 11:59:52 +0200639
Stefan Roese8a805df2010-09-16 14:01:53 +0200640 /*
641 * There might be multiple reg-tuples,
642 * so loop through them all
643 */
Stefan Roese2778a012010-09-24 13:51:50 +0200644 reg = reg2 = (u32 *)&prop->data[0];
645 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200646 /*
647 * Update size in reg property
648 */
649 reg[2] = flash_get_bank_size(reg[0],
650 idx);
Stefan Roese2778a012010-09-24 13:51:50 +0200651
652 /*
653 * Point to next reg tuple
654 */
655 reg += 3;
Stefan Roese30d45c02009-10-21 11:59:52 +0200656 }
Stefan Roese2778a012010-09-24 13:51:50 +0200657
658 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roese30d45c02009-10-21 11:59:52 +0200659 }
660
661 /* Move to next compatible node */
662 off = fdt_node_offset_by_compatible(blob, off,
663 compat[i]);
664 }
665 }
666
Stefan Roese8a805df2010-09-16 14:01:53 +0200667 return 0;
Stefan Roese30d45c02009-10-21 11:59:52 +0200668}
669#endif
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100670
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200671int fdt_increase_size(void *fdt, int add_len)
672{
673 int newlen;
674
675 newlen = fdt_totalsize(fdt) + add_len;
676
677 /* Open in place with a new len */
678 return fdt_open_into(fdt, fdt, newlen);
679}
680
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100681#ifdef CONFIG_FDT_FIXUP_PARTITIONS
682#include <jffs2/load_kernel.h>
683#include <mtd_node.h>
684
685struct reg_cell {
686 unsigned int r0;
687 unsigned int r1;
688};
689
690int fdt_del_subnodes(const void *blob, int parent_offset)
691{
692 int off, ndepth;
693 int ret;
694
695 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
696 (off >= 0) && (ndepth > 0);
697 off = fdt_next_node(blob, off, &ndepth)) {
698 if (ndepth == 1) {
699 debug("delete %s: offset: %x\n",
700 fdt_get_name(blob, off, 0), off);
701 ret = fdt_del_node((void *)blob, off);
702 if (ret < 0) {
703 printf("Can't delete node: %s\n",
704 fdt_strerror(ret));
705 return ret;
706 } else {
707 ndepth = 0;
708 off = parent_offset;
709 }
710 }
711 }
712 return 0;
713}
714
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100715int fdt_del_partitions(void *blob, int parent_offset)
716{
717 const void *prop;
718 int ndepth = 0;
719 int off;
720 int ret;
721
722 off = fdt_next_node(blob, parent_offset, &ndepth);
723 if (off > 0 && ndepth == 1) {
724 prop = fdt_getprop(blob, off, "label", NULL);
725 if (prop == NULL) {
726 /*
727 * Could not find label property, nand {}; node?
728 * Check subnode, delete partitions there if any.
729 */
730 return fdt_del_partitions(blob, off);
731 } else {
732 ret = fdt_del_subnodes(blob, parent_offset);
733 if (ret < 0) {
734 printf("Can't remove subnodes: %s\n",
735 fdt_strerror(ret));
736 return ret;
737 }
738 }
739 }
740 return 0;
741}
742
743int fdt_node_set_part_info(void *blob, int parent_offset,
744 struct mtd_device *dev)
745{
746 struct list_head *pentry;
747 struct part_info *part;
748 struct reg_cell cell;
749 int off, ndepth = 0;
750 int part_num, ret;
751 char buf[64];
752
753 ret = fdt_del_partitions(blob, parent_offset);
754 if (ret < 0)
755 return ret;
756
757 /*
758 * Check if it is nand {}; subnode, adjust
759 * the offset in this case
760 */
761 off = fdt_next_node(blob, parent_offset, &ndepth);
762 if (off > 0 && ndepth == 1)
763 parent_offset = off;
764
765 part_num = 0;
766 list_for_each_prev(pentry, &dev->parts) {
767 int newoff;
768
769 part = list_entry(pentry, struct part_info, link);
770
Scott Wood06503f12013-10-15 17:41:27 -0500771 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100772 part_num, part->name, part->size,
773 part->offset, part->mask_flags);
774
Scott Wood06503f12013-10-15 17:41:27 -0500775 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100776add_sub:
777 ret = fdt_add_subnode(blob, parent_offset, buf);
778 if (ret == -FDT_ERR_NOSPACE) {
779 ret = fdt_increase_size(blob, 512);
780 if (!ret)
781 goto add_sub;
782 else
783 goto err_size;
784 } else if (ret < 0) {
785 printf("Can't add partition node: %s\n",
786 fdt_strerror(ret));
787 return ret;
788 }
789 newoff = ret;
790
791 /* Check MTD_WRITEABLE_CMD flag */
792 if (part->mask_flags & 1) {
793add_ro:
794 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
795 if (ret == -FDT_ERR_NOSPACE) {
796 ret = fdt_increase_size(blob, 512);
797 if (!ret)
798 goto add_ro;
799 else
800 goto err_size;
801 } else if (ret < 0)
802 goto err_prop;
803 }
804
805 cell.r0 = cpu_to_fdt32(part->offset);
806 cell.r1 = cpu_to_fdt32(part->size);
807add_reg:
808 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
809 if (ret == -FDT_ERR_NOSPACE) {
810 ret = fdt_increase_size(blob, 512);
811 if (!ret)
812 goto add_reg;
813 else
814 goto err_size;
815 } else if (ret < 0)
816 goto err_prop;
817
818add_label:
819 ret = fdt_setprop_string(blob, newoff, "label", part->name);
820 if (ret == -FDT_ERR_NOSPACE) {
821 ret = fdt_increase_size(blob, 512);
822 if (!ret)
823 goto add_label;
824 else
825 goto err_size;
826 } else if (ret < 0)
827 goto err_prop;
828
829 part_num++;
830 }
831 return 0;
832err_size:
833 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
834 return ret;
835err_prop:
836 printf("Can't add property: %s\n", fdt_strerror(ret));
837 return ret;
838}
839
840/*
841 * Update partitions in nor/nand nodes using info from
842 * mtdparts environment variable. The nodes to update are
843 * specified by node_info structure which contains mtd device
844 * type and compatible string: E. g. the board code in
845 * ft_board_setup() could use:
846 *
847 * struct node_info nodes[] = {
848 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
849 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
850 * };
851 *
852 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
853 */
854void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
855{
856 struct node_info *ni = node_info;
857 struct mtd_device *dev;
858 char *parts;
859 int i, idx;
860 int noff;
861
862 parts = getenv("mtdparts");
863 if (!parts)
864 return;
865
866 if (mtdparts_init() != 0)
867 return;
868
869 for (i = 0; i < node_info_size; i++) {
870 idx = 0;
871 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
872 while (noff != -FDT_ERR_NOTFOUND) {
873 debug("%s: %s, mtd dev type %d\n",
874 fdt_get_name(blob, noff, 0),
875 ni[i].compat, ni[i].type);
876 dev = device_find(ni[i].type, idx++);
877 if (dev) {
878 if (fdt_node_set_part_info(blob, noff, dev))
879 return; /* return on error */
880 }
881
882 /* Jump to next flash node */
883 noff = fdt_node_offset_by_compatible(blob, noff,
884 ni[i].compat);
885 }
886 }
887}
888#endif
Kumar Gala49b97d92010-03-30 10:19:26 -0500889
890void fdt_del_node_and_alias(void *blob, const char *alias)
891{
892 int off = fdt_path_offset(blob, alias);
893
894 if (off < 0)
895 return;
896
897 fdt_del_node(blob, off);
898
899 off = fdt_path_offset(blob, "/aliases");
900 fdt_delprop(blob, off, alias);
901}
Kumar Galaa0342c02010-06-16 14:27:38 -0500902
903/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000904static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galaa0342c02010-06-16 14:27:38 -0500905{
906 u64 r = 0;
907 while (size--)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000908 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galaa0342c02010-06-16 14:27:38 -0500909 return r;
910}
911
Kumar Galaa0342c02010-06-16 14:27:38 -0500912#define PRu64 "%llx"
913
914/* Max address size we deal with */
915#define OF_MAX_ADDR_CELLS 4
916#define OF_BAD_ADDR ((u64)-1)
917#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
918 (ns) > 0)
919
920/* Debug utility */
921#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000922static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500923{
924 printf("%s", s);
925 while(na--)
926 printf(" %08x", *(addr++));
927 printf("\n");
928}
929#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000930static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -0500931#endif
932
933/* Callbacks for bus specific translators */
934struct of_bus {
935 const char *name;
936 const char *addresses;
Scott Wood6395f312010-08-12 18:37:39 -0500937 void (*count_cells)(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500938 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000939 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500940 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000941 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -0500942};
943
944/* Default translator (generic bus) */
Scott Wood6395f312010-08-12 18:37:39 -0500945static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500946 int *addrc, int *sizec)
947{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000948 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -0500949
950 if (addrc) {
951 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
952 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000953 *addrc = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500954 else
955 *addrc = 2;
956 }
957
958 if (sizec) {
959 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
960 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000961 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500962 else
963 *sizec = 1;
964 }
Kumar Galaa0342c02010-06-16 14:27:38 -0500965}
966
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000967static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500968 int na, int ns, int pna)
969{
970 u64 cp, s, da;
971
972 cp = of_read_number(range, na);
973 s = of_read_number(range + na + pna, ns);
974 da = of_read_number(addr, na);
975
976 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
977 cp, s, da);
978
979 if (da < cp || da >= (cp + s))
980 return OF_BAD_ADDR;
981 return da - cp;
982}
983
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000984static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500985{
986 u64 a = of_read_number(addr, na);
987 memset(addr, 0, na * 4);
988 a += offset;
989 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000990 addr[na - 2] = cpu_to_fdt32(a >> 32);
991 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -0500992
993 return 0;
994}
995
996/* Array of bus specific translators */
997static struct of_bus of_busses[] = {
998 /* Default */
999 {
1000 .name = "default",
1001 .addresses = "reg",
1002 .count_cells = of_bus_default_count_cells,
1003 .map = of_bus_default_map,
1004 .translate = of_bus_default_translate,
1005 },
1006};
1007
1008static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001009 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001010 int na, int ns, int pna, const char *rprop)
1011{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001012 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001013 int rlen;
1014 int rone;
1015 u64 offset = OF_BAD_ADDR;
1016
1017 /* Normally, an absence of a "ranges" property means we are
1018 * crossing a non-translatable boundary, and thus the addresses
1019 * below the current not cannot be converted to CPU physical ones.
1020 * Unfortunately, while this is very clear in the spec, it's not
1021 * what Apple understood, and they do have things like /uni-n or
1022 * /ht nodes with no "ranges" property and a lot of perfectly
1023 * useable mapped devices below them. Thus we treat the absence of
1024 * "ranges" as equivalent to an empty "ranges" property which means
1025 * a 1:1 translation at that level. It's up to the caller not to try
1026 * to translate addresses that aren't supposed to be translated in
1027 * the first place. --BenH.
1028 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001029 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001030 if (ranges == NULL || rlen == 0) {
1031 offset = of_read_number(addr, na);
1032 memset(addr, 0, pna * 4);
1033 debug("OF: no ranges, 1:1 translation\n");
1034 goto finish;
1035 }
1036
1037 debug("OF: walking ranges...\n");
1038
1039 /* Now walk through the ranges */
1040 rlen /= 4;
1041 rone = na + pna + ns;
1042 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1043 offset = bus->map(addr, ranges, na, ns, pna);
1044 if (offset != OF_BAD_ADDR)
1045 break;
1046 }
1047 if (offset == OF_BAD_ADDR) {
1048 debug("OF: not found !\n");
1049 return 1;
1050 }
1051 memcpy(addr, ranges + na, 4 * pna);
1052
1053 finish:
1054 of_dump_addr("OF: parent translation for:", addr, pna);
1055 debug("OF: with offset: "PRu64"\n", offset);
1056
1057 /* Translate it into parent bus space */
1058 return pbus->translate(addr, offset, pna);
1059}
1060
1061/*
1062 * Translate an address from the device-tree into a CPU physical address,
1063 * this walks up the tree and applies the various bus mappings on the
1064 * way.
1065 *
1066 * Note: We consider that crossing any level with #size-cells == 0 to mean
1067 * that translation is impossible (that is we are not dealing with a value
1068 * that can be mapped to a cpu physical address). This is not really specified
1069 * that way, but this is traditionally the way IBM at least do things
1070 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001071static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1072 const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001073{
1074 int parent;
1075 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001076 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001077 int na, ns, pna, pns;
1078 u64 result = OF_BAD_ADDR;
1079
1080 debug("OF: ** translation for device %s **\n",
1081 fdt_get_name(blob, node_offset, NULL));
1082
1083 /* Get parent & match bus type */
1084 parent = fdt_parent_offset(blob, node_offset);
1085 if (parent < 0)
1086 goto bail;
1087 bus = &of_busses[0];
1088
1089 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001090 bus->count_cells(blob, parent, &na, &ns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001091 if (!OF_CHECK_COUNTS(na, ns)) {
1092 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1093 fdt_get_name(blob, node_offset, NULL));
1094 goto bail;
1095 }
1096 memcpy(addr, in_addr, na * 4);
1097
1098 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1099 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1100 of_dump_addr("OF: translating address:", addr, na);
1101
1102 /* Translate */
1103 for (;;) {
1104 /* Switch to parent bus */
1105 node_offset = parent;
1106 parent = fdt_parent_offset(blob, node_offset);
1107
1108 /* If root, we have finished */
1109 if (parent < 0) {
1110 debug("OF: reached root node\n");
1111 result = of_read_number(addr, na);
1112 break;
1113 }
1114
1115 /* Get new parent bus and counts */
1116 pbus = &of_busses[0];
Scott Wood6395f312010-08-12 18:37:39 -05001117 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001118 if (!OF_CHECK_COUNTS(pna, pns)) {
1119 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1120 fdt_get_name(blob, node_offset, NULL));
1121 break;
1122 }
1123
1124 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1125 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1126
1127 /* Apply bus translation */
1128 if (of_translate_one(blob, node_offset, bus, pbus,
1129 addr, na, ns, pna, rprop))
1130 break;
1131
1132 /* Complete the move up one level */
1133 na = pna;
1134 ns = pns;
1135 bus = pbus;
1136
1137 of_dump_addr("OF: one level translation:", addr, na);
1138 }
1139 bail:
1140
1141 return result;
1142}
1143
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001144u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001145{
1146 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1147}
Kumar Gala75e73af2010-07-04 12:48:21 -05001148
1149/**
1150 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1151 * who's reg property matches a physical cpu address
1152 *
1153 * @blob: ptr to device tree
1154 * @compat: compatiable string to match
1155 * @compat_off: property name
1156 *
1157 */
1158int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1159 phys_addr_t compat_off)
1160{
1161 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1162 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001163 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala75e73af2010-07-04 12:48:21 -05001164 if (reg) {
1165 if (compat_off == fdt_translate_address(blob, off, reg))
1166 return off;
1167 }
1168 off = fdt_node_offset_by_compatible(blob, off, compat);
1169 }
1170
1171 return -FDT_ERR_NOTFOUND;
1172}
1173
Kumar Galab4b847e2010-07-09 16:18:58 -05001174/**
1175 * fdt_alloc_phandle: Return next free phandle value
1176 *
1177 * @blob: ptr to device tree
1178 */
1179int fdt_alloc_phandle(void *blob)
1180{
Timur Tabi50bf17b2011-09-20 18:24:35 -05001181 int offset, phandle = 0;
Kumar Gala75e73af2010-07-04 12:48:21 -05001182
Kumar Galab4b847e2010-07-09 16:18:58 -05001183 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1184 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabi50bf17b2011-09-20 18:24:35 -05001185 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab4b847e2010-07-09 16:18:58 -05001186 }
1187
1188 return phandle + 1;
1189}
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001190
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001191/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001192 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001193 *
1194 * @fdt: ptr to device tree
1195 * @nodeoffset: node to update
1196 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001197 */
1198int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001199{
1200 int ret;
1201
1202#ifdef DEBUG
1203 int off = fdt_node_offset_by_phandle(fdt, phandle);
1204
1205 if ((off >= 0) && (off != nodeoffset)) {
1206 char buf[64];
1207
1208 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1209 printf("Trying to update node %s with phandle %u ",
1210 buf, phandle);
1211
1212 fdt_get_path(fdt, off, buf, sizeof(buf));
1213 printf("that already exists in node %s.\n", buf);
1214 return -FDT_ERR_BADPHANDLE;
1215 }
1216#endif
1217
1218 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1219 if (ret < 0)
1220 return ret;
1221
1222 /*
1223 * For now, also set the deprecated "linux,phandle" property, so that we
1224 * don't break older kernels.
1225 */
1226 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1227
1228 return ret;
1229}
1230
Kumar Gala10aeabd2011-08-01 00:25:20 -05001231/*
1232 * fdt_create_phandle: Create a phandle property for the given node
1233 *
1234 * @fdt: ptr to device tree
1235 * @nodeoffset: node to update
1236 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001237unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001238{
1239 /* see if there is a phandle already */
1240 int phandle = fdt_get_phandle(fdt, nodeoffset);
1241
1242 /* if we got 0, means no phandle so create one */
1243 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001244 int ret;
1245
Kumar Gala10aeabd2011-08-01 00:25:20 -05001246 phandle = fdt_alloc_phandle(fdt);
Timur Tabi3c927cc2011-09-20 18:24:34 -05001247 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1248 if (ret < 0) {
1249 printf("Can't set phandle %u: %s\n", phandle,
1250 fdt_strerror(ret));
1251 return 0;
1252 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001253 }
1254
1255 return phandle;
1256}
1257
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001258/*
1259 * fdt_set_node_status: Set status for the given node
1260 *
1261 * @fdt: ptr to device tree
1262 * @nodeoffset: node to update
1263 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1264 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1265 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1266 */
1267int fdt_set_node_status(void *fdt, int nodeoffset,
1268 enum fdt_status status, unsigned int error_code)
1269{
1270 char buf[16];
1271 int ret = 0;
1272
1273 if (nodeoffset < 0)
1274 return nodeoffset;
1275
1276 switch (status) {
1277 case FDT_STATUS_OKAY:
1278 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1279 break;
1280 case FDT_STATUS_DISABLED:
1281 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1282 break;
1283 case FDT_STATUS_FAIL:
1284 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1285 break;
1286 case FDT_STATUS_FAIL_ERROR_CODE:
1287 sprintf(buf, "fail-%d", error_code);
1288 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1289 break;
1290 default:
1291 printf("Invalid fdt status: %x\n", status);
1292 ret = -1;
1293 break;
1294 }
1295
1296 return ret;
1297}
1298
1299/*
1300 * fdt_set_status_by_alias: Set status for the given node given an alias
1301 *
1302 * @fdt: ptr to device tree
1303 * @alias: alias of node to update
1304 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1305 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1306 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1307 */
1308int fdt_set_status_by_alias(void *fdt, const char* alias,
1309 enum fdt_status status, unsigned int error_code)
1310{
1311 int offset = fdt_path_offset(fdt, alias);
1312
1313 return fdt_set_node_status(fdt, offset, status, error_code);
1314}
1315
Tom Wai-Hong Tam096eb3f2012-12-05 14:46:41 +00001316#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001317int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1318{
1319 int noff;
1320 int ret;
1321
1322 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1323 if (noff != -FDT_ERR_NOTFOUND) {
1324 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1325add_edid:
1326 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1327 if (ret == -FDT_ERR_NOSPACE) {
1328 ret = fdt_increase_size(blob, 512);
1329 if (!ret)
1330 goto add_edid;
1331 else
1332 goto err_size;
1333 } else if (ret < 0) {
1334 printf("Can't add property: %s\n", fdt_strerror(ret));
1335 return ret;
1336 }
1337 }
1338 return 0;
1339err_size:
1340 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1341 return ret;
1342}
1343#endif
Timur Tabibb682002011-05-03 13:24:07 -05001344
1345/*
1346 * Verify the physical address of device tree node for a given alias
1347 *
1348 * This function locates the device tree node of a given alias, and then
1349 * verifies that the physical address of that device matches the given
1350 * parameter. It displays a message if there is a mismatch.
1351 *
1352 * Returns 1 on success, 0 on failure
1353 */
1354int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1355{
1356 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001357 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001358 int node, len;
1359 u64 dt_addr;
1360
1361 path = fdt_getprop(fdt, anode, alias, NULL);
1362 if (!path) {
1363 /* If there's no such alias, then it's not a failure */
1364 return 1;
1365 }
1366
1367 node = fdt_path_offset(fdt, path);
1368 if (node < 0) {
1369 printf("Warning: device tree alias '%s' points to invalid "
1370 "node %s.\n", alias, path);
1371 return 0;
1372 }
1373
1374 reg = fdt_getprop(fdt, node, "reg", &len);
1375 if (!reg) {
1376 printf("Warning: device tree node '%s' has no address.\n",
1377 path);
1378 return 0;
1379 }
1380
1381 dt_addr = fdt_translate_address(fdt, node, reg);
1382 if (addr != dt_addr) {
1383 printf("Warning: U-Boot configured device %s at address %llx,\n"
1384 " but the device tree has it address %llx.\n",
1385 alias, addr, dt_addr);
1386 return 0;
1387 }
1388
1389 return 1;
1390}
1391
1392/*
1393 * Returns the base address of an SOC or PCI node
1394 */
1395u64 fdt_get_base_address(void *fdt, int node)
1396{
1397 int size;
1398 u32 naddr;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001399 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001400
1401 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1402 if (prop && size == 4)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001403 naddr = be32_to_cpup(prop);
Timur Tabibb682002011-05-03 13:24:07 -05001404 else
1405 naddr = 2;
1406
1407 prop = fdt_getprop(fdt, node, "ranges", &size);
1408
1409 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1410}