blob: 1f0d8f5fe9a3348addd99255b4538dd3830fd424 [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
Kumar Gala3bed2aa2008-10-23 00:05:47 -050024/**
25 * fdt_getprop_u32_default - Find a node and return it's property or a default
26 *
27 * @fdt: ptr to device tree
28 * @path: path of node
29 * @prop: property name
30 * @dflt: default value if the property isn't found
31 *
32 * Convenience function to find a node and return it's property or a
33 * default value if it doesn't exist.
34 */
Gabe Black07e12782011-11-08 01:09:44 -080035u32 fdt_getprop_u32_default(const void *fdt, const char *path,
36 const char *prop, const u32 dflt)
Kumar Gala3bed2aa2008-10-23 00:05:47 -050037{
Kim Phillips8aa5ec62013-01-16 14:00:11 +000038 const fdt32_t *val;
Kumar Gala3bed2aa2008-10-23 00:05:47 -050039 int off;
40
41 off = fdt_path_offset(fdt, path);
42 if (off < 0)
43 return dflt;
44
45 val = fdt_getprop(fdt, off, prop, NULL);
46 if (val)
Gabe Blackde166062011-11-08 01:05:32 -080047 return fdt32_to_cpu(*val);
Kumar Gala3bed2aa2008-10-23 00:05:47 -050048 else
49 return dflt;
50}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040051
Kumar Galaa3c29332007-10-24 10:21:57 -050052/**
53 * fdt_find_and_setprop: Find a node and set it's property
54 *
55 * @fdt: ptr to device tree
56 * @node: path of node
57 * @prop: property name
58 * @val: ptr to new value
59 * @len: length of new property value
60 * @create: flag to create the property if it doesn't exist
61 *
62 * Convenience function to directly set a property given the path to the node.
63 */
64int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
65 const void *val, int len, int create)
66{
Kumar Gala8d04f022007-10-24 11:04:22 -050067 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -050068
69 if (nodeoff < 0)
70 return nodeoff;
71
Kim Phillips8aa5ec62013-01-16 14:00:11 +000072 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galaa3c29332007-10-24 10:21:57 -050073 return 0; /* create flag not set; so exit quietly */
74
75 return fdt_setprop(fdt, nodeoff, prop, val, len);
76}
77
Kumar Gala151c8b02007-11-26 17:06:15 -060078#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov3e303f72009-10-15 17:47:04 +040079
Marek Vasut036036d2012-09-14 23:45:51 +020080#ifdef CONFIG_CONS_INDEX
Anton Vorontsov3e303f72009-10-15 17:47:04 +040081static void fdt_fill_multisername(char *sername, size_t maxlen)
82{
83 const char *outname = stdio_devices[stdout]->name;
84
85 if (strcmp(outname, "serial") > 0)
86 strncpy(sername, outname, maxlen);
87
88 /* eserial? */
89 if (strcmp(outname + 1, "serial") > 0)
90 strncpy(sername, outname + 1, maxlen);
91}
Marek Vasut036036d2012-09-14 23:45:51 +020092#endif
Anton Vorontsov3e303f72009-10-15 17:47:04 +040093
Detlev Zundel40777812008-06-20 22:24:05 +020094static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -060095{
96 int err = 0;
97#ifdef CONFIG_CONS_INDEX
98 int node;
99 char sername[9] = { 0 };
100 const char *path;
101
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400102 fdt_fill_multisername(sername, sizeof(sername) - 1);
103 if (!sername[0])
104 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600105
106 err = node = fdt_path_offset(fdt, "/aliases");
107 if (node >= 0) {
108 int len;
109 path = fdt_getprop(fdt, node, sername, &len);
110 if (path) {
111 char *p = malloc(len);
112 err = -FDT_ERR_NOSPACE;
113 if (p) {
114 memcpy(p, path, len);
Detlev Zundel40777812008-06-20 22:24:05 +0200115 err = fdt_setprop(fdt, chosenoff,
Kumar Gala151c8b02007-11-26 17:06:15 -0600116 "linux,stdout-path", p, len);
117 free(p);
118 }
119 } else {
120 err = len;
121 }
122 }
123#endif
124 if (err < 0)
125 printf("WARNING: could not set linux,stdout-path %s.\n",
126 fdt_strerror(err));
127
128 return err;
129}
130#endif
131
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500132int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
133{
134 int nodeoffset;
135 int err, j, total;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000136 fdt32_t tmp;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500137 const char *path;
138 uint64_t addr, size;
139
140 /* Find the "chosen" node. */
141 nodeoffset = fdt_path_offset (fdt, "/chosen");
142
143 /* If there is no "chosen" node in the blob return */
144 if (nodeoffset < 0) {
145 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
146 return nodeoffset;
147 }
148
149 /* just return if initrd_start/end aren't valid */
150 if ((initrd_start == 0) || (initrd_end == 0))
151 return 0;
152
153 total = fdt_num_mem_rsv(fdt);
154
155 /*
156 * Look for an existing entry and update it. If we don't find
157 * the entry, we will j be the next available slot.
158 */
159 for (j = 0; j < total; j++) {
160 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
161 if (addr == initrd_start) {
162 fdt_del_mem_rsv(fdt, j);
163 break;
164 }
165 }
166
Grant Likelyce6b27a2011-03-28 09:58:55 +0000167 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500168 if (err < 0) {
169 printf("fdt_initrd: %s\n", fdt_strerror(err));
170 return err;
171 }
172
173 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
174 if ((path == NULL) || force) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000175 tmp = cpu_to_fdt32(initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500176 err = fdt_setprop(fdt, nodeoffset,
177 "linux,initrd-start", &tmp, sizeof(tmp));
178 if (err < 0) {
179 printf("WARNING: "
180 "could not set linux,initrd-start %s.\n",
181 fdt_strerror(err));
182 return err;
183 }
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000184 tmp = cpu_to_fdt32(initrd_end);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500185 err = fdt_setprop(fdt, nodeoffset,
186 "linux,initrd-end", &tmp, sizeof(tmp));
187 if (err < 0) {
188 printf("WARNING: could not set linux,initrd-end %s.\n",
189 fdt_strerror(err));
190
191 return err;
192 }
193 }
194
195 return 0;
196}
197
Heiko Schocher56844a22008-09-11 08:11:23 +0200198int fdt_chosen(void *fdt, int force)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400199{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400200 int nodeoffset;
201 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400202 char *str; /* used to set string properties */
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500203 const char *path;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400204
205 err = fdt_check_header(fdt);
206 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400207 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400208 return err;
209 }
210
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400211 /*
212 * Find the "chosen" node.
213 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500214 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400215
216 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500217 * If there is no "chosen" node in the blob, create it.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400218 */
219 if (nodeoffset < 0) {
220 /*
221 * Create a new node "/chosen" (offset 0 is root level)
222 */
223 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
224 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400225 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400226 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400227 return nodeoffset;
228 }
229 }
230
231 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500232 * Create /chosen properites that don't exist in the fdt.
233 * If the property exists, update it only if the "force" parameter
234 * is true.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400235 */
236 str = getenv("bootargs");
237 if (str != NULL) {
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500238 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
239 if ((path == NULL) || force) {
240 err = fdt_setprop(fdt, nodeoffset,
241 "bootargs", str, strlen(str)+1);
242 if (err < 0)
243 printf("WARNING: could not set bootargs %s.\n",
244 fdt_strerror(err));
245 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400246 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500247
Kumar Gala151c8b02007-11-26 17:06:15 -0600248#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500249 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
250 if ((path == NULL) || force)
251 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala151c8b02007-11-26 17:06:15 -0600252#endif
253
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400254#ifdef OF_STDOUT_PATH
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500255 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
256 if ((path == NULL) || force) {
257 err = fdt_setprop(fdt, nodeoffset,
258 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
259 if (err < 0)
260 printf("WARNING: could not set linux,stdout-path %s.\n",
261 fdt_strerror(err));
262 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400263#endif
264
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400265 return err;
266}
267
Kumar Galae93becf2007-11-03 19:46:28 -0500268void do_fixup_by_path(void *fdt, const char *path, const char *prop,
269 const void *val, int len, int create)
270{
271#if defined(DEBUG)
272 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600273 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500274 for (i = 0; i < len; i++)
275 debug(" %.2x", *(u8*)(val+i));
276 debug("\n");
277#endif
278 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
279 if (rc)
280 printf("Unable to update property %s:%s, err=%s\n",
281 path, prop, fdt_strerror(rc));
282}
283
284void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
285 u32 val, int create)
286{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000287 fdt32_t tmp = cpu_to_fdt32(val);
288 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Galae93becf2007-11-03 19:46:28 -0500289}
290
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600291void do_fixup_by_prop(void *fdt,
292 const char *pname, const void *pval, int plen,
293 const char *prop, const void *val, int len,
294 int create)
295{
296 int off;
297#if defined(DEBUG)
298 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600299 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600300 for (i = 0; i < len; i++)
301 debug(" %.2x", *(u8*)(val+i));
302 debug("\n");
303#endif
304 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
305 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000306 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600307 fdt_setprop(fdt, off, prop, val, len);
308 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
309 }
310}
311
312void do_fixup_by_prop_u32(void *fdt,
313 const char *pname, const void *pval, int plen,
314 const char *prop, u32 val, int create)
315{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000316 fdt32_t tmp = cpu_to_fdt32(val);
317 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600318}
319
320void do_fixup_by_compat(void *fdt, const char *compat,
321 const char *prop, const void *val, int len, int create)
322{
323 int off = -1;
324#if defined(DEBUG)
325 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600326 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600327 for (i = 0; i < len; i++)
328 debug(" %.2x", *(u8*)(val+i));
329 debug("\n");
330#endif
331 off = fdt_node_offset_by_compatible(fdt, -1, compat);
332 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000333 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600334 fdt_setprop(fdt, off, prop, val, len);
335 off = fdt_node_offset_by_compatible(fdt, off, compat);
336 }
337}
338
339void do_fixup_by_compat_u32(void *fdt, const char *compat,
340 const char *prop, u32 val, int create)
341{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000342 fdt32_t tmp = cpu_to_fdt32(val);
343 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600344}
345
John Rigbya6bd9e82010-10-13 13:57:33 -0600346/*
347 * Get cells len in bytes
348 * if #NNNN-cells property is 2 then len is 8
349 * otherwise len is 4
350 */
351static int get_cells_len(void *blob, char *nr_cells_name)
Kumar Gala3c927282007-11-26 14:57:45 -0600352{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000353 const fdt32_t *cell;
John Rigbya6bd9e82010-10-13 13:57:33 -0600354
355 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
Gabe Blackde166062011-11-08 01:05:32 -0800356 if (cell && fdt32_to_cpu(*cell) == 2)
John Rigbya6bd9e82010-10-13 13:57:33 -0600357 return 8;
358
359 return 4;
360}
361
362/*
363 * Write a 4 or 8 byte big endian cell
364 */
365static void write_cell(u8 *addr, u64 val, int size)
366{
367 int shift = (size - 1) * 8;
368 while (size-- > 0) {
369 *addr++ = (val >> shift) & 0xff;
370 shift -= 8;
371 }
372}
373
Doug Anderson5e574542013-04-30 10:22:00 +0000374#ifdef CONFIG_NR_DRAM_BANKS
375#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
376#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000377#define MEMORY_BANKS_MAX 4
Doug Anderson5e574542013-04-30 10:22:00 +0000378#endif
John Rigbya6bd9e82010-10-13 13:57:33 -0600379int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
380{
381 int err, nodeoffset;
382 int addr_cell_len, size_cell_len, len;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000383 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbya6bd9e82010-10-13 13:57:33 -0600384 int bank;
Kumar Gala3c927282007-11-26 14:57:45 -0600385
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000386 if (banks > MEMORY_BANKS_MAX) {
387 printf("%s: num banks %d exceeds hardcoded limit %d."
388 " Recompile with higher MEMORY_BANKS_MAX?\n",
389 __FUNCTION__, banks, MEMORY_BANKS_MAX);
390 return -1;
391 }
392
Kumar Gala3c927282007-11-26 14:57:45 -0600393 err = fdt_check_header(blob);
394 if (err < 0) {
395 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
396 return err;
397 }
398
399 /* update, or add and update /memory node */
400 nodeoffset = fdt_path_offset(blob, "/memory");
401 if (nodeoffset < 0) {
402 nodeoffset = fdt_add_subnode(blob, 0, "memory");
403 if (nodeoffset < 0)
404 printf("WARNING: could not create /memory: %s.\n",
405 fdt_strerror(nodeoffset));
406 return nodeoffset;
407 }
408 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
409 sizeof("memory"));
410 if (err < 0) {
411 printf("WARNING: could not set %s %s.\n", "device_type",
412 fdt_strerror(err));
413 return err;
414 }
415
John Rigbya6bd9e82010-10-13 13:57:33 -0600416 addr_cell_len = get_cells_len(blob, "#address-cells");
417 size_cell_len = get_cells_len(blob, "#size-cells");
Kumar Gala3c927282007-11-26 14:57:45 -0600418
John Rigbya6bd9e82010-10-13 13:57:33 -0600419 for (bank = 0, len = 0; bank < banks; bank++) {
420 write_cell(tmp + len, start[bank], addr_cell_len);
421 len += addr_cell_len;
422
423 write_cell(tmp + len, size[bank], size_cell_len);
424 len += size_cell_len;
Kumar Gala3c927282007-11-26 14:57:45 -0600425 }
426
427 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
428 if (err < 0) {
429 printf("WARNING: could not set %s %s.\n",
430 "reg", fdt_strerror(err));
431 return err;
432 }
433 return 0;
434}
435
John Rigbya6bd9e82010-10-13 13:57:33 -0600436int fdt_fixup_memory(void *blob, u64 start, u64 size)
437{
438 return fdt_fixup_memory_banks(blob, &start, &size, 1);
439}
440
Kumar Galaba37aa02008-08-19 15:41:18 -0500441void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600442{
Kumar Galaba37aa02008-08-19 15:41:18 -0500443 int node, i, j;
444 char enet[16], *tmp, *end;
Stephen Warren064d55f2013-05-27 18:01:19 +0000445 char mac[16];
Kumar Galaab544632007-11-21 11:11:03 -0600446 const char *path;
Kumar Galaba37aa02008-08-19 15:41:18 -0500447 unsigned char mac_addr[6];
Kumar Galaab544632007-11-21 11:11:03 -0600448
449 node = fdt_path_offset(fdt, "/aliases");
Kumar Galaba37aa02008-08-19 15:41:18 -0500450 if (node < 0)
451 return;
452
453 i = 0;
Stephen Warren064d55f2013-05-27 18:01:19 +0000454 strcpy(mac, "ethaddr");
Kumar Galaba37aa02008-08-19 15:41:18 -0500455 while ((tmp = getenv(mac)) != NULL) {
456 sprintf(enet, "ethernet%d", i);
457 path = fdt_getprop(fdt, node, enet, NULL);
458 if (!path) {
459 debug("No alias for %s\n", enet);
460 sprintf(mac, "eth%daddr", ++i);
461 continue;
Kumar Galaab544632007-11-21 11:11:03 -0600462 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500463
464 for (j = 0; j < 6; j++) {
465 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
466 if (tmp)
467 tmp = (*end) ? end+1 : end;
Kumar Galaab544632007-11-21 11:11:03 -0600468 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500469
470 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
471 do_fixup_by_path(fdt, path, "local-mac-address",
472 &mac_addr, 6, 1);
473
474 sprintf(mac, "eth%daddr", ++i);
Kumar Galaab544632007-11-21 11:11:03 -0600475 }
476}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300477
Kumar Gala3082d232008-08-15 08:24:42 -0500478/* Resize the fdt to its actual size + a bit of padding */
479int fdt_resize(void *blob)
480{
481 int i;
482 uint64_t addr, size;
483 int total, ret;
484 uint actualsize;
485
486 if (!blob)
487 return 0;
488
489 total = fdt_num_mem_rsv(blob);
490 for (i = 0; i < total; i++) {
491 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000492 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500493 fdt_del_mem_rsv(blob, i);
494 break;
495 }
496 }
497
Peter Korsgaardf242a082008-10-28 08:26:52 +0100498 /*
499 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200500 * plus the size needed for 5 fdt_add_mem_rsv, one
501 * for the fdt itself and 4 for a possible initrd
502 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100503 */
Kumar Gala3082d232008-08-15 08:24:42 -0500504 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200505 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500506
507 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000508 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
509 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500510
511 /* Change the fdt header to reflect the correct size */
512 fdt_set_totalsize(blob, actualsize);
513
514 /* Add the new reservation */
Simon Glass92549352011-09-17 06:48:58 +0000515 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Gala3082d232008-08-15 08:24:42 -0500516 if (ret < 0)
517 return ret;
518
519 return actualsize;
520}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500521
522#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500523#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500524
525#define FDT_PCI_PREFETCH (0x40000000)
526#define FDT_PCI_MEM32 (0x02000000)
527#define FDT_PCI_IO (0x01000000)
528#define FDT_PCI_MEM64 (0x03000000)
529
530int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
531
532 int addrcell, sizecell, len, r;
533 u32 *dma_range;
534 /* sized based on pci addr cells, size-cells, & address-cells */
535 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
536
537 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
538 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
539
540 dma_range = &dma_ranges[0];
541 for (r = 0; r < hose->region_count; r++) {
542 u64 bus_start, phys_start, size;
543
Kumar Galaff4e66e2009-02-06 09:49:31 -0600544 /* skip if !PCI_REGION_SYS_MEMORY */
545 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500546 continue;
547
548 bus_start = (u64)hose->regions[r].bus_start;
549 phys_start = (u64)hose->regions[r].phys_start;
550 size = (u64)hose->regions[r].size;
551
552 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500553 if (size >= 0x100000000ull)
Kumar Gala8ab451c2008-10-22 23:33:56 -0500554 dma_range[0] |= FDT_PCI_MEM64;
555 else
556 dma_range[0] |= FDT_PCI_MEM32;
557 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
558 dma_range[0] |= FDT_PCI_PREFETCH;
559#ifdef CONFIG_SYS_PCI_64BIT
560 dma_range[1] = bus_start >> 32;
561#else
562 dma_range[1] = 0;
563#endif
564 dma_range[2] = bus_start & 0xffffffff;
565
566 if (addrcell == 2) {
567 dma_range[3] = phys_start >> 32;
568 dma_range[4] = phys_start & 0xffffffff;
569 } else {
570 dma_range[3] = phys_start & 0xffffffff;
571 }
572
573 if (sizecell == 2) {
574 dma_range[3 + addrcell + 0] = size >> 32;
575 dma_range[3 + addrcell + 1] = size & 0xffffffff;
576 } else {
577 dma_range[3 + addrcell + 0] = size & 0xffffffff;
578 }
579
580 dma_range += (3 + addrcell + sizecell);
581 }
582
583 len = dma_range - &dma_ranges[0];
584 if (len)
585 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
586
587 return 0;
588}
589#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200590
591#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
592/*
Stefan Roese8a805df2010-09-16 14:01:53 +0200593 * Provide a weak default function to return the flash bank size.
594 * There might be multiple non-identical flash chips connected to one
595 * chip-select, so we need to pass an index as well.
596 */
597u32 __flash_get_bank_size(int cs, int idx)
598{
599 extern flash_info_t flash_info[];
600
601 /*
602 * As default, a simple 1:1 mapping is provided. Boards with
603 * a different mapping need to supply a board specific mapping
604 * routine.
605 */
606 return flash_info[cs].size;
607}
608u32 flash_get_bank_size(int cs, int idx)
609 __attribute__((weak, alias("__flash_get_bank_size")));
610
611/*
Stefan Roese30d45c02009-10-21 11:59:52 +0200612 * This function can be used to update the size in the "reg" property
Stefan Roese8a805df2010-09-16 14:01:53 +0200613 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roese30d45c02009-10-21 11:59:52 +0200614 * non-fixed NOR FLASH sizes.
615 */
Stefan Roese8a805df2010-09-16 14:01:53 +0200616int fdt_fixup_nor_flash_size(void *blob)
Stefan Roese30d45c02009-10-21 11:59:52 +0200617{
618 char compat[][16] = { "cfi-flash", "jedec-flash" };
619 int off;
620 int len;
621 struct fdt_property *prop;
Stefan Roese2778a012010-09-24 13:51:50 +0200622 u32 *reg, *reg2;
Stefan Roese30d45c02009-10-21 11:59:52 +0200623 int i;
624
625 for (i = 0; i < 2; i++) {
626 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
627 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200628 int idx;
629
Stefan Roese30d45c02009-10-21 11:59:52 +0200630 /*
Stefan Roese8a805df2010-09-16 14:01:53 +0200631 * Found one compatible node, so fixup the size
632 * int its reg properties
Stefan Roese30d45c02009-10-21 11:59:52 +0200633 */
634 prop = fdt_get_property_w(blob, off, "reg", &len);
635 if (prop) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200636 int tuple_size = 3 * sizeof(reg);
Stefan Roese30d45c02009-10-21 11:59:52 +0200637
Stefan Roese8a805df2010-09-16 14:01:53 +0200638 /*
639 * There might be multiple reg-tuples,
640 * so loop through them all
641 */
Stefan Roese2778a012010-09-24 13:51:50 +0200642 reg = reg2 = (u32 *)&prop->data[0];
643 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200644 /*
645 * Update size in reg property
646 */
647 reg[2] = flash_get_bank_size(reg[0],
648 idx);
Stefan Roese2778a012010-09-24 13:51:50 +0200649
650 /*
651 * Point to next reg tuple
652 */
653 reg += 3;
Stefan Roese30d45c02009-10-21 11:59:52 +0200654 }
Stefan Roese2778a012010-09-24 13:51:50 +0200655
656 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roese30d45c02009-10-21 11:59:52 +0200657 }
658
659 /* Move to next compatible node */
660 off = fdt_node_offset_by_compatible(blob, off,
661 compat[i]);
662 }
663 }
664
Stefan Roese8a805df2010-09-16 14:01:53 +0200665 return 0;
Stefan Roese30d45c02009-10-21 11:59:52 +0200666}
667#endif
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100668
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200669int fdt_increase_size(void *fdt, int add_len)
670{
671 int newlen;
672
673 newlen = fdt_totalsize(fdt) + add_len;
674
675 /* Open in place with a new len */
676 return fdt_open_into(fdt, fdt, newlen);
677}
678
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100679#ifdef CONFIG_FDT_FIXUP_PARTITIONS
680#include <jffs2/load_kernel.h>
681#include <mtd_node.h>
682
683struct reg_cell {
684 unsigned int r0;
685 unsigned int r1;
686};
687
688int fdt_del_subnodes(const void *blob, int parent_offset)
689{
690 int off, ndepth;
691 int ret;
692
693 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
694 (off >= 0) && (ndepth > 0);
695 off = fdt_next_node(blob, off, &ndepth)) {
696 if (ndepth == 1) {
697 debug("delete %s: offset: %x\n",
698 fdt_get_name(blob, off, 0), off);
699 ret = fdt_del_node((void *)blob, off);
700 if (ret < 0) {
701 printf("Can't delete node: %s\n",
702 fdt_strerror(ret));
703 return ret;
704 } else {
705 ndepth = 0;
706 off = parent_offset;
707 }
708 }
709 }
710 return 0;
711}
712
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100713int fdt_del_partitions(void *blob, int parent_offset)
714{
715 const void *prop;
716 int ndepth = 0;
717 int off;
718 int ret;
719
720 off = fdt_next_node(blob, parent_offset, &ndepth);
721 if (off > 0 && ndepth == 1) {
722 prop = fdt_getprop(blob, off, "label", NULL);
723 if (prop == NULL) {
724 /*
725 * Could not find label property, nand {}; node?
726 * Check subnode, delete partitions there if any.
727 */
728 return fdt_del_partitions(blob, off);
729 } else {
730 ret = fdt_del_subnodes(blob, parent_offset);
731 if (ret < 0) {
732 printf("Can't remove subnodes: %s\n",
733 fdt_strerror(ret));
734 return ret;
735 }
736 }
737 }
738 return 0;
739}
740
741int fdt_node_set_part_info(void *blob, int parent_offset,
742 struct mtd_device *dev)
743{
744 struct list_head *pentry;
745 struct part_info *part;
746 struct reg_cell cell;
747 int off, ndepth = 0;
748 int part_num, ret;
749 char buf[64];
750
751 ret = fdt_del_partitions(blob, parent_offset);
752 if (ret < 0)
753 return ret;
754
755 /*
756 * Check if it is nand {}; subnode, adjust
757 * the offset in this case
758 */
759 off = fdt_next_node(blob, parent_offset, &ndepth);
760 if (off > 0 && ndepth == 1)
761 parent_offset = off;
762
763 part_num = 0;
764 list_for_each_prev(pentry, &dev->parts) {
765 int newoff;
766
767 part = list_entry(pentry, struct part_info, link);
768
Scott Wood06503f12013-10-15 17:41:27 -0500769 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100770 part_num, part->name, part->size,
771 part->offset, part->mask_flags);
772
Scott Wood06503f12013-10-15 17:41:27 -0500773 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100774add_sub:
775 ret = fdt_add_subnode(blob, parent_offset, buf);
776 if (ret == -FDT_ERR_NOSPACE) {
777 ret = fdt_increase_size(blob, 512);
778 if (!ret)
779 goto add_sub;
780 else
781 goto err_size;
782 } else if (ret < 0) {
783 printf("Can't add partition node: %s\n",
784 fdt_strerror(ret));
785 return ret;
786 }
787 newoff = ret;
788
789 /* Check MTD_WRITEABLE_CMD flag */
790 if (part->mask_flags & 1) {
791add_ro:
792 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
793 if (ret == -FDT_ERR_NOSPACE) {
794 ret = fdt_increase_size(blob, 512);
795 if (!ret)
796 goto add_ro;
797 else
798 goto err_size;
799 } else if (ret < 0)
800 goto err_prop;
801 }
802
803 cell.r0 = cpu_to_fdt32(part->offset);
804 cell.r1 = cpu_to_fdt32(part->size);
805add_reg:
806 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
807 if (ret == -FDT_ERR_NOSPACE) {
808 ret = fdt_increase_size(blob, 512);
809 if (!ret)
810 goto add_reg;
811 else
812 goto err_size;
813 } else if (ret < 0)
814 goto err_prop;
815
816add_label:
817 ret = fdt_setprop_string(blob, newoff, "label", part->name);
818 if (ret == -FDT_ERR_NOSPACE) {
819 ret = fdt_increase_size(blob, 512);
820 if (!ret)
821 goto add_label;
822 else
823 goto err_size;
824 } else if (ret < 0)
825 goto err_prop;
826
827 part_num++;
828 }
829 return 0;
830err_size:
831 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
832 return ret;
833err_prop:
834 printf("Can't add property: %s\n", fdt_strerror(ret));
835 return ret;
836}
837
838/*
839 * Update partitions in nor/nand nodes using info from
840 * mtdparts environment variable. The nodes to update are
841 * specified by node_info structure which contains mtd device
842 * type and compatible string: E. g. the board code in
843 * ft_board_setup() could use:
844 *
845 * struct node_info nodes[] = {
846 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
847 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
848 * };
849 *
850 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
851 */
852void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
853{
854 struct node_info *ni = node_info;
855 struct mtd_device *dev;
856 char *parts;
857 int i, idx;
858 int noff;
859
860 parts = getenv("mtdparts");
861 if (!parts)
862 return;
863
864 if (mtdparts_init() != 0)
865 return;
866
867 for (i = 0; i < node_info_size; i++) {
868 idx = 0;
869 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
870 while (noff != -FDT_ERR_NOTFOUND) {
871 debug("%s: %s, mtd dev type %d\n",
872 fdt_get_name(blob, noff, 0),
873 ni[i].compat, ni[i].type);
874 dev = device_find(ni[i].type, idx++);
875 if (dev) {
876 if (fdt_node_set_part_info(blob, noff, dev))
877 return; /* return on error */
878 }
879
880 /* Jump to next flash node */
881 noff = fdt_node_offset_by_compatible(blob, noff,
882 ni[i].compat);
883 }
884 }
885}
886#endif
Kumar Gala49b97d92010-03-30 10:19:26 -0500887
888void fdt_del_node_and_alias(void *blob, const char *alias)
889{
890 int off = fdt_path_offset(blob, alias);
891
892 if (off < 0)
893 return;
894
895 fdt_del_node(blob, off);
896
897 off = fdt_path_offset(blob, "/aliases");
898 fdt_delprop(blob, off, alias);
899}
Kumar Galaa0342c02010-06-16 14:27:38 -0500900
901/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000902static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galaa0342c02010-06-16 14:27:38 -0500903{
904 u64 r = 0;
905 while (size--)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000906 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galaa0342c02010-06-16 14:27:38 -0500907 return r;
908}
909
Kumar Galaa0342c02010-06-16 14:27:38 -0500910#define PRu64 "%llx"
911
912/* Max address size we deal with */
913#define OF_MAX_ADDR_CELLS 4
914#define OF_BAD_ADDR ((u64)-1)
915#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
916 (ns) > 0)
917
918/* Debug utility */
919#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000920static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500921{
922 printf("%s", s);
923 while(na--)
924 printf(" %08x", *(addr++));
925 printf("\n");
926}
927#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000928static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -0500929#endif
930
931/* Callbacks for bus specific translators */
932struct of_bus {
933 const char *name;
934 const char *addresses;
Scott Wood6395f312010-08-12 18:37:39 -0500935 void (*count_cells)(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500936 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000937 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500938 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000939 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -0500940};
941
942/* Default translator (generic bus) */
Scott Wood6395f312010-08-12 18:37:39 -0500943static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500944 int *addrc, int *sizec)
945{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000946 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -0500947
948 if (addrc) {
949 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
950 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000951 *addrc = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500952 else
953 *addrc = 2;
954 }
955
956 if (sizec) {
957 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
958 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000959 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500960 else
961 *sizec = 1;
962 }
Kumar Galaa0342c02010-06-16 14:27:38 -0500963}
964
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000965static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500966 int na, int ns, int pna)
967{
968 u64 cp, s, da;
969
970 cp = of_read_number(range, na);
971 s = of_read_number(range + na + pna, ns);
972 da = of_read_number(addr, na);
973
974 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
975 cp, s, da);
976
977 if (da < cp || da >= (cp + s))
978 return OF_BAD_ADDR;
979 return da - cp;
980}
981
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000982static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500983{
984 u64 a = of_read_number(addr, na);
985 memset(addr, 0, na * 4);
986 a += offset;
987 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000988 addr[na - 2] = cpu_to_fdt32(a >> 32);
989 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -0500990
991 return 0;
992}
993
994/* Array of bus specific translators */
995static struct of_bus of_busses[] = {
996 /* Default */
997 {
998 .name = "default",
999 .addresses = "reg",
1000 .count_cells = of_bus_default_count_cells,
1001 .map = of_bus_default_map,
1002 .translate = of_bus_default_translate,
1003 },
1004};
1005
1006static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001007 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001008 int na, int ns, int pna, const char *rprop)
1009{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001010 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001011 int rlen;
1012 int rone;
1013 u64 offset = OF_BAD_ADDR;
1014
1015 /* Normally, an absence of a "ranges" property means we are
1016 * crossing a non-translatable boundary, and thus the addresses
1017 * below the current not cannot be converted to CPU physical ones.
1018 * Unfortunately, while this is very clear in the spec, it's not
1019 * what Apple understood, and they do have things like /uni-n or
1020 * /ht nodes with no "ranges" property and a lot of perfectly
1021 * useable mapped devices below them. Thus we treat the absence of
1022 * "ranges" as equivalent to an empty "ranges" property which means
1023 * a 1:1 translation at that level. It's up to the caller not to try
1024 * to translate addresses that aren't supposed to be translated in
1025 * the first place. --BenH.
1026 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001027 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001028 if (ranges == NULL || rlen == 0) {
1029 offset = of_read_number(addr, na);
1030 memset(addr, 0, pna * 4);
1031 debug("OF: no ranges, 1:1 translation\n");
1032 goto finish;
1033 }
1034
1035 debug("OF: walking ranges...\n");
1036
1037 /* Now walk through the ranges */
1038 rlen /= 4;
1039 rone = na + pna + ns;
1040 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1041 offset = bus->map(addr, ranges, na, ns, pna);
1042 if (offset != OF_BAD_ADDR)
1043 break;
1044 }
1045 if (offset == OF_BAD_ADDR) {
1046 debug("OF: not found !\n");
1047 return 1;
1048 }
1049 memcpy(addr, ranges + na, 4 * pna);
1050
1051 finish:
1052 of_dump_addr("OF: parent translation for:", addr, pna);
1053 debug("OF: with offset: "PRu64"\n", offset);
1054
1055 /* Translate it into parent bus space */
1056 return pbus->translate(addr, offset, pna);
1057}
1058
1059/*
1060 * Translate an address from the device-tree into a CPU physical address,
1061 * this walks up the tree and applies the various bus mappings on the
1062 * way.
1063 *
1064 * Note: We consider that crossing any level with #size-cells == 0 to mean
1065 * that translation is impossible (that is we are not dealing with a value
1066 * that can be mapped to a cpu physical address). This is not really specified
1067 * that way, but this is traditionally the way IBM at least do things
1068 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001069static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1070 const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001071{
1072 int parent;
1073 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001074 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001075 int na, ns, pna, pns;
1076 u64 result = OF_BAD_ADDR;
1077
1078 debug("OF: ** translation for device %s **\n",
1079 fdt_get_name(blob, node_offset, NULL));
1080
1081 /* Get parent & match bus type */
1082 parent = fdt_parent_offset(blob, node_offset);
1083 if (parent < 0)
1084 goto bail;
1085 bus = &of_busses[0];
1086
1087 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001088 bus->count_cells(blob, parent, &na, &ns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001089 if (!OF_CHECK_COUNTS(na, ns)) {
1090 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1091 fdt_get_name(blob, node_offset, NULL));
1092 goto bail;
1093 }
1094 memcpy(addr, in_addr, na * 4);
1095
1096 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1097 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1098 of_dump_addr("OF: translating address:", addr, na);
1099
1100 /* Translate */
1101 for (;;) {
1102 /* Switch to parent bus */
1103 node_offset = parent;
1104 parent = fdt_parent_offset(blob, node_offset);
1105
1106 /* If root, we have finished */
1107 if (parent < 0) {
1108 debug("OF: reached root node\n");
1109 result = of_read_number(addr, na);
1110 break;
1111 }
1112
1113 /* Get new parent bus and counts */
1114 pbus = &of_busses[0];
Scott Wood6395f312010-08-12 18:37:39 -05001115 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001116 if (!OF_CHECK_COUNTS(pna, pns)) {
1117 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1118 fdt_get_name(blob, node_offset, NULL));
1119 break;
1120 }
1121
1122 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1123 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1124
1125 /* Apply bus translation */
1126 if (of_translate_one(blob, node_offset, bus, pbus,
1127 addr, na, ns, pna, rprop))
1128 break;
1129
1130 /* Complete the move up one level */
1131 na = pna;
1132 ns = pns;
1133 bus = pbus;
1134
1135 of_dump_addr("OF: one level translation:", addr, na);
1136 }
1137 bail:
1138
1139 return result;
1140}
1141
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001142u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001143{
1144 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1145}
Kumar Gala75e73af2010-07-04 12:48:21 -05001146
1147/**
1148 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1149 * who's reg property matches a physical cpu address
1150 *
1151 * @blob: ptr to device tree
1152 * @compat: compatiable string to match
1153 * @compat_off: property name
1154 *
1155 */
1156int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1157 phys_addr_t compat_off)
1158{
1159 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1160 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001161 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala75e73af2010-07-04 12:48:21 -05001162 if (reg) {
1163 if (compat_off == fdt_translate_address(blob, off, reg))
1164 return off;
1165 }
1166 off = fdt_node_offset_by_compatible(blob, off, compat);
1167 }
1168
1169 return -FDT_ERR_NOTFOUND;
1170}
1171
Kumar Galab4b847e2010-07-09 16:18:58 -05001172/**
1173 * fdt_alloc_phandle: Return next free phandle value
1174 *
1175 * @blob: ptr to device tree
1176 */
1177int fdt_alloc_phandle(void *blob)
1178{
Timur Tabi50bf17b2011-09-20 18:24:35 -05001179 int offset, phandle = 0;
Kumar Gala75e73af2010-07-04 12:48:21 -05001180
Kumar Galab4b847e2010-07-09 16:18:58 -05001181 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1182 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabi50bf17b2011-09-20 18:24:35 -05001183 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab4b847e2010-07-09 16:18:58 -05001184 }
1185
1186 return phandle + 1;
1187}
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001188
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001189/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001190 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001191 *
1192 * @fdt: ptr to device tree
1193 * @nodeoffset: node to update
1194 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001195 */
1196int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001197{
1198 int ret;
1199
1200#ifdef DEBUG
1201 int off = fdt_node_offset_by_phandle(fdt, phandle);
1202
1203 if ((off >= 0) && (off != nodeoffset)) {
1204 char buf[64];
1205
1206 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1207 printf("Trying to update node %s with phandle %u ",
1208 buf, phandle);
1209
1210 fdt_get_path(fdt, off, buf, sizeof(buf));
1211 printf("that already exists in node %s.\n", buf);
1212 return -FDT_ERR_BADPHANDLE;
1213 }
1214#endif
1215
1216 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1217 if (ret < 0)
1218 return ret;
1219
1220 /*
1221 * For now, also set the deprecated "linux,phandle" property, so that we
1222 * don't break older kernels.
1223 */
1224 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1225
1226 return ret;
1227}
1228
Kumar Gala10aeabd2011-08-01 00:25:20 -05001229/*
1230 * fdt_create_phandle: Create a phandle property for the given node
1231 *
1232 * @fdt: ptr to device tree
1233 * @nodeoffset: node to update
1234 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001235unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001236{
1237 /* see if there is a phandle already */
1238 int phandle = fdt_get_phandle(fdt, nodeoffset);
1239
1240 /* if we got 0, means no phandle so create one */
1241 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001242 int ret;
1243
Kumar Gala10aeabd2011-08-01 00:25:20 -05001244 phandle = fdt_alloc_phandle(fdt);
Timur Tabi3c927cc2011-09-20 18:24:34 -05001245 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1246 if (ret < 0) {
1247 printf("Can't set phandle %u: %s\n", phandle,
1248 fdt_strerror(ret));
1249 return 0;
1250 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001251 }
1252
1253 return phandle;
1254}
1255
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001256/*
1257 * fdt_set_node_status: Set status for the given node
1258 *
1259 * @fdt: ptr to device tree
1260 * @nodeoffset: node to update
1261 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1262 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1263 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1264 */
1265int fdt_set_node_status(void *fdt, int nodeoffset,
1266 enum fdt_status status, unsigned int error_code)
1267{
1268 char buf[16];
1269 int ret = 0;
1270
1271 if (nodeoffset < 0)
1272 return nodeoffset;
1273
1274 switch (status) {
1275 case FDT_STATUS_OKAY:
1276 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1277 break;
1278 case FDT_STATUS_DISABLED:
1279 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1280 break;
1281 case FDT_STATUS_FAIL:
1282 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1283 break;
1284 case FDT_STATUS_FAIL_ERROR_CODE:
1285 sprintf(buf, "fail-%d", error_code);
1286 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1287 break;
1288 default:
1289 printf("Invalid fdt status: %x\n", status);
1290 ret = -1;
1291 break;
1292 }
1293
1294 return ret;
1295}
1296
1297/*
1298 * fdt_set_status_by_alias: Set status for the given node given an alias
1299 *
1300 * @fdt: ptr to device tree
1301 * @alias: alias of node to update
1302 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1303 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1304 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1305 */
1306int fdt_set_status_by_alias(void *fdt, const char* alias,
1307 enum fdt_status status, unsigned int error_code)
1308{
1309 int offset = fdt_path_offset(fdt, alias);
1310
1311 return fdt_set_node_status(fdt, offset, status, error_code);
1312}
1313
Tom Wai-Hong Tam096eb3f2012-12-05 14:46:41 +00001314#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001315int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1316{
1317 int noff;
1318 int ret;
1319
1320 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1321 if (noff != -FDT_ERR_NOTFOUND) {
1322 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1323add_edid:
1324 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1325 if (ret == -FDT_ERR_NOSPACE) {
1326 ret = fdt_increase_size(blob, 512);
1327 if (!ret)
1328 goto add_edid;
1329 else
1330 goto err_size;
1331 } else if (ret < 0) {
1332 printf("Can't add property: %s\n", fdt_strerror(ret));
1333 return ret;
1334 }
1335 }
1336 return 0;
1337err_size:
1338 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1339 return ret;
1340}
1341#endif
Timur Tabibb682002011-05-03 13:24:07 -05001342
1343/*
1344 * Verify the physical address of device tree node for a given alias
1345 *
1346 * This function locates the device tree node of a given alias, and then
1347 * verifies that the physical address of that device matches the given
1348 * parameter. It displays a message if there is a mismatch.
1349 *
1350 * Returns 1 on success, 0 on failure
1351 */
1352int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1353{
1354 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001355 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001356 int node, len;
1357 u64 dt_addr;
1358
1359 path = fdt_getprop(fdt, anode, alias, NULL);
1360 if (!path) {
1361 /* If there's no such alias, then it's not a failure */
1362 return 1;
1363 }
1364
1365 node = fdt_path_offset(fdt, path);
1366 if (node < 0) {
1367 printf("Warning: device tree alias '%s' points to invalid "
1368 "node %s.\n", alias, path);
1369 return 0;
1370 }
1371
1372 reg = fdt_getprop(fdt, node, "reg", &len);
1373 if (!reg) {
1374 printf("Warning: device tree node '%s' has no address.\n",
1375 path);
1376 return 0;
1377 }
1378
1379 dt_addr = fdt_translate_address(fdt, node, reg);
1380 if (addr != dt_addr) {
1381 printf("Warning: U-Boot configured device %s at address %llx,\n"
1382 " but the device tree has it address %llx.\n",
1383 alias, addr, dt_addr);
1384 return 0;
1385 }
1386
1387 return 1;
1388}
1389
1390/*
1391 * Returns the base address of an SOC or PCI node
1392 */
1393u64 fdt_get_base_address(void *fdt, int node)
1394{
1395 int size;
1396 u32 naddr;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001397 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001398
1399 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1400 if (prop && size == 4)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001401 naddr = be32_to_cpup(prop);
Timur Tabibb682002011-05-03 13:24:07 -05001402 else
1403 naddr = 2;
1404
1405 prop = fdt_getprop(fdt, node, "ranges", &size);
1406
1407 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1408}