blob: 4e32b02aa233118a677a4d0b3d405373b04bf34f [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");
Miao Yan35940de2013-11-28 17:51:39 +0800403 if (nodeoffset < 0) {
Kumar Gala3c927282007-11-26 14:57:45 -0600404 printf("WARNING: could not create /memory: %s.\n",
405 fdt_strerror(nodeoffset));
Miao Yan35940de2013-11-28 17:51:39 +0800406 return nodeoffset;
407 }
Kumar Gala3c927282007-11-26 14:57:45 -0600408 }
409 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
410 sizeof("memory"));
411 if (err < 0) {
412 printf("WARNING: could not set %s %s.\n", "device_type",
413 fdt_strerror(err));
414 return err;
415 }
416
John Rigbya6bd9e82010-10-13 13:57:33 -0600417 addr_cell_len = get_cells_len(blob, "#address-cells");
418 size_cell_len = get_cells_len(blob, "#size-cells");
Kumar Gala3c927282007-11-26 14:57:45 -0600419
John Rigbya6bd9e82010-10-13 13:57:33 -0600420 for (bank = 0, len = 0; bank < banks; bank++) {
421 write_cell(tmp + len, start[bank], addr_cell_len);
422 len += addr_cell_len;
423
424 write_cell(tmp + len, size[bank], size_cell_len);
425 len += size_cell_len;
Kumar Gala3c927282007-11-26 14:57:45 -0600426 }
427
428 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
429 if (err < 0) {
430 printf("WARNING: could not set %s %s.\n",
431 "reg", fdt_strerror(err));
432 return err;
433 }
434 return 0;
435}
436
John Rigbya6bd9e82010-10-13 13:57:33 -0600437int fdt_fixup_memory(void *blob, u64 start, u64 size)
438{
439 return fdt_fixup_memory_banks(blob, &start, &size, 1);
440}
441
Kumar Galaba37aa02008-08-19 15:41:18 -0500442void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600443{
Kumar Galaba37aa02008-08-19 15:41:18 -0500444 int node, i, j;
445 char enet[16], *tmp, *end;
Stephen Warren064d55f2013-05-27 18:01:19 +0000446 char mac[16];
Kumar Galaab544632007-11-21 11:11:03 -0600447 const char *path;
Kumar Galaba37aa02008-08-19 15:41:18 -0500448 unsigned char mac_addr[6];
Kumar Galaab544632007-11-21 11:11:03 -0600449
450 node = fdt_path_offset(fdt, "/aliases");
Kumar Galaba37aa02008-08-19 15:41:18 -0500451 if (node < 0)
452 return;
453
454 i = 0;
Stephen Warren064d55f2013-05-27 18:01:19 +0000455 strcpy(mac, "ethaddr");
Kumar Galaba37aa02008-08-19 15:41:18 -0500456 while ((tmp = getenv(mac)) != NULL) {
457 sprintf(enet, "ethernet%d", i);
458 path = fdt_getprop(fdt, node, enet, NULL);
459 if (!path) {
460 debug("No alias for %s\n", enet);
461 sprintf(mac, "eth%daddr", ++i);
462 continue;
Kumar Galaab544632007-11-21 11:11:03 -0600463 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500464
465 for (j = 0; j < 6; j++) {
466 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
467 if (tmp)
468 tmp = (*end) ? end+1 : end;
Kumar Galaab544632007-11-21 11:11:03 -0600469 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500470
471 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
472 do_fixup_by_path(fdt, path, "local-mac-address",
473 &mac_addr, 6, 1);
474
475 sprintf(mac, "eth%daddr", ++i);
Kumar Galaab544632007-11-21 11:11:03 -0600476 }
477}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300478
Kumar Gala3082d232008-08-15 08:24:42 -0500479/* Resize the fdt to its actual size + a bit of padding */
480int fdt_resize(void *blob)
481{
482 int i;
483 uint64_t addr, size;
484 int total, ret;
485 uint actualsize;
486
487 if (!blob)
488 return 0;
489
490 total = fdt_num_mem_rsv(blob);
491 for (i = 0; i < total; i++) {
492 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000493 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500494 fdt_del_mem_rsv(blob, i);
495 break;
496 }
497 }
498
Peter Korsgaardf242a082008-10-28 08:26:52 +0100499 /*
500 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200501 * plus the size needed for 5 fdt_add_mem_rsv, one
502 * for the fdt itself and 4 for a possible initrd
503 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100504 */
Kumar Gala3082d232008-08-15 08:24:42 -0500505 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200506 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500507
508 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000509 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
510 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500511
512 /* Change the fdt header to reflect the correct size */
513 fdt_set_totalsize(blob, actualsize);
514
515 /* Add the new reservation */
Simon Glass92549352011-09-17 06:48:58 +0000516 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Gala3082d232008-08-15 08:24:42 -0500517 if (ret < 0)
518 return ret;
519
520 return actualsize;
521}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500522
523#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500524#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500525
526#define FDT_PCI_PREFETCH (0x40000000)
527#define FDT_PCI_MEM32 (0x02000000)
528#define FDT_PCI_IO (0x01000000)
529#define FDT_PCI_MEM64 (0x03000000)
530
531int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
532
533 int addrcell, sizecell, len, r;
534 u32 *dma_range;
535 /* sized based on pci addr cells, size-cells, & address-cells */
536 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
537
538 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
539 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
540
541 dma_range = &dma_ranges[0];
542 for (r = 0; r < hose->region_count; r++) {
543 u64 bus_start, phys_start, size;
544
Kumar Galaff4e66e2009-02-06 09:49:31 -0600545 /* skip if !PCI_REGION_SYS_MEMORY */
546 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500547 continue;
548
549 bus_start = (u64)hose->regions[r].bus_start;
550 phys_start = (u64)hose->regions[r].phys_start;
551 size = (u64)hose->regions[r].size;
552
553 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500554 if (size >= 0x100000000ull)
Kumar Gala8ab451c2008-10-22 23:33:56 -0500555 dma_range[0] |= FDT_PCI_MEM64;
556 else
557 dma_range[0] |= FDT_PCI_MEM32;
558 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
559 dma_range[0] |= FDT_PCI_PREFETCH;
560#ifdef CONFIG_SYS_PCI_64BIT
561 dma_range[1] = bus_start >> 32;
562#else
563 dma_range[1] = 0;
564#endif
565 dma_range[2] = bus_start & 0xffffffff;
566
567 if (addrcell == 2) {
568 dma_range[3] = phys_start >> 32;
569 dma_range[4] = phys_start & 0xffffffff;
570 } else {
571 dma_range[3] = phys_start & 0xffffffff;
572 }
573
574 if (sizecell == 2) {
575 dma_range[3 + addrcell + 0] = size >> 32;
576 dma_range[3 + addrcell + 1] = size & 0xffffffff;
577 } else {
578 dma_range[3 + addrcell + 0] = size & 0xffffffff;
579 }
580
581 dma_range += (3 + addrcell + sizecell);
582 }
583
584 len = dma_range - &dma_ranges[0];
585 if (len)
586 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
587
588 return 0;
589}
590#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200591
592#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
593/*
Stefan Roese8a805df2010-09-16 14:01:53 +0200594 * Provide a weak default function to return the flash bank size.
595 * There might be multiple non-identical flash chips connected to one
596 * chip-select, so we need to pass an index as well.
597 */
598u32 __flash_get_bank_size(int cs, int idx)
599{
600 extern flash_info_t flash_info[];
601
602 /*
603 * As default, a simple 1:1 mapping is provided. Boards with
604 * a different mapping need to supply a board specific mapping
605 * routine.
606 */
607 return flash_info[cs].size;
608}
609u32 flash_get_bank_size(int cs, int idx)
610 __attribute__((weak, alias("__flash_get_bank_size")));
611
612/*
Stefan Roese30d45c02009-10-21 11:59:52 +0200613 * This function can be used to update the size in the "reg" property
Stefan Roese8a805df2010-09-16 14:01:53 +0200614 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roese30d45c02009-10-21 11:59:52 +0200615 * non-fixed NOR FLASH sizes.
616 */
Stefan Roese8a805df2010-09-16 14:01:53 +0200617int fdt_fixup_nor_flash_size(void *blob)
Stefan Roese30d45c02009-10-21 11:59:52 +0200618{
619 char compat[][16] = { "cfi-flash", "jedec-flash" };
620 int off;
621 int len;
622 struct fdt_property *prop;
Stefan Roese2778a012010-09-24 13:51:50 +0200623 u32 *reg, *reg2;
Stefan Roese30d45c02009-10-21 11:59:52 +0200624 int i;
625
626 for (i = 0; i < 2; i++) {
627 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
628 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200629 int idx;
630
Stefan Roese30d45c02009-10-21 11:59:52 +0200631 /*
Stefan Roese8a805df2010-09-16 14:01:53 +0200632 * Found one compatible node, so fixup the size
633 * int its reg properties
Stefan Roese30d45c02009-10-21 11:59:52 +0200634 */
635 prop = fdt_get_property_w(blob, off, "reg", &len);
636 if (prop) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200637 int tuple_size = 3 * sizeof(reg);
Stefan Roese30d45c02009-10-21 11:59:52 +0200638
Stefan Roese8a805df2010-09-16 14:01:53 +0200639 /*
640 * There might be multiple reg-tuples,
641 * so loop through them all
642 */
Stefan Roese2778a012010-09-24 13:51:50 +0200643 reg = reg2 = (u32 *)&prop->data[0];
644 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200645 /*
646 * Update size in reg property
647 */
648 reg[2] = flash_get_bank_size(reg[0],
649 idx);
Stefan Roese2778a012010-09-24 13:51:50 +0200650
651 /*
652 * Point to next reg tuple
653 */
654 reg += 3;
Stefan Roese30d45c02009-10-21 11:59:52 +0200655 }
Stefan Roese2778a012010-09-24 13:51:50 +0200656
657 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roese30d45c02009-10-21 11:59:52 +0200658 }
659
660 /* Move to next compatible node */
661 off = fdt_node_offset_by_compatible(blob, off,
662 compat[i]);
663 }
664 }
665
Stefan Roese8a805df2010-09-16 14:01:53 +0200666 return 0;
Stefan Roese30d45c02009-10-21 11:59:52 +0200667}
668#endif
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100669
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200670int fdt_increase_size(void *fdt, int add_len)
671{
672 int newlen;
673
674 newlen = fdt_totalsize(fdt) + add_len;
675
676 /* Open in place with a new len */
677 return fdt_open_into(fdt, fdt, newlen);
678}
679
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100680#ifdef CONFIG_FDT_FIXUP_PARTITIONS
681#include <jffs2/load_kernel.h>
682#include <mtd_node.h>
683
684struct reg_cell {
685 unsigned int r0;
686 unsigned int r1;
687};
688
689int fdt_del_subnodes(const void *blob, int parent_offset)
690{
691 int off, ndepth;
692 int ret;
693
694 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
695 (off >= 0) && (ndepth > 0);
696 off = fdt_next_node(blob, off, &ndepth)) {
697 if (ndepth == 1) {
698 debug("delete %s: offset: %x\n",
699 fdt_get_name(blob, off, 0), off);
700 ret = fdt_del_node((void *)blob, off);
701 if (ret < 0) {
702 printf("Can't delete node: %s\n",
703 fdt_strerror(ret));
704 return ret;
705 } else {
706 ndepth = 0;
707 off = parent_offset;
708 }
709 }
710 }
711 return 0;
712}
713
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100714int fdt_del_partitions(void *blob, int parent_offset)
715{
716 const void *prop;
717 int ndepth = 0;
718 int off;
719 int ret;
720
721 off = fdt_next_node(blob, parent_offset, &ndepth);
722 if (off > 0 && ndepth == 1) {
723 prop = fdt_getprop(blob, off, "label", NULL);
724 if (prop == NULL) {
725 /*
726 * Could not find label property, nand {}; node?
727 * Check subnode, delete partitions there if any.
728 */
729 return fdt_del_partitions(blob, off);
730 } else {
731 ret = fdt_del_subnodes(blob, parent_offset);
732 if (ret < 0) {
733 printf("Can't remove subnodes: %s\n",
734 fdt_strerror(ret));
735 return ret;
736 }
737 }
738 }
739 return 0;
740}
741
742int fdt_node_set_part_info(void *blob, int parent_offset,
743 struct mtd_device *dev)
744{
745 struct list_head *pentry;
746 struct part_info *part;
747 struct reg_cell cell;
748 int off, ndepth = 0;
749 int part_num, ret;
750 char buf[64];
751
752 ret = fdt_del_partitions(blob, parent_offset);
753 if (ret < 0)
754 return ret;
755
756 /*
757 * Check if it is nand {}; subnode, adjust
758 * the offset in this case
759 */
760 off = fdt_next_node(blob, parent_offset, &ndepth);
761 if (off > 0 && ndepth == 1)
762 parent_offset = off;
763
764 part_num = 0;
765 list_for_each_prev(pentry, &dev->parts) {
766 int newoff;
767
768 part = list_entry(pentry, struct part_info, link);
769
Scott Wood06503f12013-10-15 17:41:27 -0500770 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100771 part_num, part->name, part->size,
772 part->offset, part->mask_flags);
773
Scott Wood06503f12013-10-15 17:41:27 -0500774 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100775add_sub:
776 ret = fdt_add_subnode(blob, parent_offset, buf);
777 if (ret == -FDT_ERR_NOSPACE) {
778 ret = fdt_increase_size(blob, 512);
779 if (!ret)
780 goto add_sub;
781 else
782 goto err_size;
783 } else if (ret < 0) {
784 printf("Can't add partition node: %s\n",
785 fdt_strerror(ret));
786 return ret;
787 }
788 newoff = ret;
789
790 /* Check MTD_WRITEABLE_CMD flag */
791 if (part->mask_flags & 1) {
792add_ro:
793 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
794 if (ret == -FDT_ERR_NOSPACE) {
795 ret = fdt_increase_size(blob, 512);
796 if (!ret)
797 goto add_ro;
798 else
799 goto err_size;
800 } else if (ret < 0)
801 goto err_prop;
802 }
803
804 cell.r0 = cpu_to_fdt32(part->offset);
805 cell.r1 = cpu_to_fdt32(part->size);
806add_reg:
807 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
808 if (ret == -FDT_ERR_NOSPACE) {
809 ret = fdt_increase_size(blob, 512);
810 if (!ret)
811 goto add_reg;
812 else
813 goto err_size;
814 } else if (ret < 0)
815 goto err_prop;
816
817add_label:
818 ret = fdt_setprop_string(blob, newoff, "label", part->name);
819 if (ret == -FDT_ERR_NOSPACE) {
820 ret = fdt_increase_size(blob, 512);
821 if (!ret)
822 goto add_label;
823 else
824 goto err_size;
825 } else if (ret < 0)
826 goto err_prop;
827
828 part_num++;
829 }
830 return 0;
831err_size:
832 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
833 return ret;
834err_prop:
835 printf("Can't add property: %s\n", fdt_strerror(ret));
836 return ret;
837}
838
839/*
840 * Update partitions in nor/nand nodes using info from
841 * mtdparts environment variable. The nodes to update are
842 * specified by node_info structure which contains mtd device
843 * type and compatible string: E. g. the board code in
844 * ft_board_setup() could use:
845 *
846 * struct node_info nodes[] = {
847 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
848 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
849 * };
850 *
851 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
852 */
853void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
854{
855 struct node_info *ni = node_info;
856 struct mtd_device *dev;
857 char *parts;
858 int i, idx;
859 int noff;
860
861 parts = getenv("mtdparts");
862 if (!parts)
863 return;
864
865 if (mtdparts_init() != 0)
866 return;
867
868 for (i = 0; i < node_info_size; i++) {
869 idx = 0;
870 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
871 while (noff != -FDT_ERR_NOTFOUND) {
872 debug("%s: %s, mtd dev type %d\n",
873 fdt_get_name(blob, noff, 0),
874 ni[i].compat, ni[i].type);
875 dev = device_find(ni[i].type, idx++);
876 if (dev) {
877 if (fdt_node_set_part_info(blob, noff, dev))
878 return; /* return on error */
879 }
880
881 /* Jump to next flash node */
882 noff = fdt_node_offset_by_compatible(blob, noff,
883 ni[i].compat);
884 }
885 }
886}
887#endif
Kumar Gala49b97d92010-03-30 10:19:26 -0500888
889void fdt_del_node_and_alias(void *blob, const char *alias)
890{
891 int off = fdt_path_offset(blob, alias);
892
893 if (off < 0)
894 return;
895
896 fdt_del_node(blob, off);
897
898 off = fdt_path_offset(blob, "/aliases");
899 fdt_delprop(blob, off, alias);
900}
Kumar Galaa0342c02010-06-16 14:27:38 -0500901
902/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000903static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galaa0342c02010-06-16 14:27:38 -0500904{
905 u64 r = 0;
906 while (size--)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000907 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galaa0342c02010-06-16 14:27:38 -0500908 return r;
909}
910
Kumar Galaa0342c02010-06-16 14:27:38 -0500911#define PRu64 "%llx"
912
913/* Max address size we deal with */
914#define OF_MAX_ADDR_CELLS 4
915#define OF_BAD_ADDR ((u64)-1)
916#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
917 (ns) > 0)
918
919/* Debug utility */
920#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000921static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500922{
923 printf("%s", s);
924 while(na--)
925 printf(" %08x", *(addr++));
926 printf("\n");
927}
928#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000929static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -0500930#endif
931
932/* Callbacks for bus specific translators */
933struct of_bus {
934 const char *name;
935 const char *addresses;
Scott Wood6395f312010-08-12 18:37:39 -0500936 void (*count_cells)(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500937 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000938 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500939 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000940 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -0500941};
942
943/* Default translator (generic bus) */
Scott Wood6395f312010-08-12 18:37:39 -0500944static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500945 int *addrc, int *sizec)
946{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000947 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -0500948
949 if (addrc) {
950 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
951 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000952 *addrc = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500953 else
954 *addrc = 2;
955 }
956
957 if (sizec) {
958 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
959 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000960 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500961 else
962 *sizec = 1;
963 }
Kumar Galaa0342c02010-06-16 14:27:38 -0500964}
965
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000966static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500967 int na, int ns, int pna)
968{
969 u64 cp, s, da;
970
971 cp = of_read_number(range, na);
972 s = of_read_number(range + na + pna, ns);
973 da = of_read_number(addr, na);
974
975 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
976 cp, s, da);
977
978 if (da < cp || da >= (cp + s))
979 return OF_BAD_ADDR;
980 return da - cp;
981}
982
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000983static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500984{
985 u64 a = of_read_number(addr, na);
986 memset(addr, 0, na * 4);
987 a += offset;
988 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000989 addr[na - 2] = cpu_to_fdt32(a >> 32);
990 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -0500991
992 return 0;
993}
994
995/* Array of bus specific translators */
996static struct of_bus of_busses[] = {
997 /* Default */
998 {
999 .name = "default",
1000 .addresses = "reg",
1001 .count_cells = of_bus_default_count_cells,
1002 .map = of_bus_default_map,
1003 .translate = of_bus_default_translate,
1004 },
1005};
1006
1007static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001008 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001009 int na, int ns, int pna, const char *rprop)
1010{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001011 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001012 int rlen;
1013 int rone;
1014 u64 offset = OF_BAD_ADDR;
1015
1016 /* Normally, an absence of a "ranges" property means we are
1017 * crossing a non-translatable boundary, and thus the addresses
1018 * below the current not cannot be converted to CPU physical ones.
1019 * Unfortunately, while this is very clear in the spec, it's not
1020 * what Apple understood, and they do have things like /uni-n or
1021 * /ht nodes with no "ranges" property and a lot of perfectly
1022 * useable mapped devices below them. Thus we treat the absence of
1023 * "ranges" as equivalent to an empty "ranges" property which means
1024 * a 1:1 translation at that level. It's up to the caller not to try
1025 * to translate addresses that aren't supposed to be translated in
1026 * the first place. --BenH.
1027 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001028 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001029 if (ranges == NULL || rlen == 0) {
1030 offset = of_read_number(addr, na);
1031 memset(addr, 0, pna * 4);
1032 debug("OF: no ranges, 1:1 translation\n");
1033 goto finish;
1034 }
1035
1036 debug("OF: walking ranges...\n");
1037
1038 /* Now walk through the ranges */
1039 rlen /= 4;
1040 rone = na + pna + ns;
1041 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1042 offset = bus->map(addr, ranges, na, ns, pna);
1043 if (offset != OF_BAD_ADDR)
1044 break;
1045 }
1046 if (offset == OF_BAD_ADDR) {
1047 debug("OF: not found !\n");
1048 return 1;
1049 }
1050 memcpy(addr, ranges + na, 4 * pna);
1051
1052 finish:
1053 of_dump_addr("OF: parent translation for:", addr, pna);
1054 debug("OF: with offset: "PRu64"\n", offset);
1055
1056 /* Translate it into parent bus space */
1057 return pbus->translate(addr, offset, pna);
1058}
1059
1060/*
1061 * Translate an address from the device-tree into a CPU physical address,
1062 * this walks up the tree and applies the various bus mappings on the
1063 * way.
1064 *
1065 * Note: We consider that crossing any level with #size-cells == 0 to mean
1066 * that translation is impossible (that is we are not dealing with a value
1067 * that can be mapped to a cpu physical address). This is not really specified
1068 * that way, but this is traditionally the way IBM at least do things
1069 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001070static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1071 const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001072{
1073 int parent;
1074 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001075 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001076 int na, ns, pna, pns;
1077 u64 result = OF_BAD_ADDR;
1078
1079 debug("OF: ** translation for device %s **\n",
1080 fdt_get_name(blob, node_offset, NULL));
1081
1082 /* Get parent & match bus type */
1083 parent = fdt_parent_offset(blob, node_offset);
1084 if (parent < 0)
1085 goto bail;
1086 bus = &of_busses[0];
1087
1088 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001089 bus->count_cells(blob, parent, &na, &ns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001090 if (!OF_CHECK_COUNTS(na, ns)) {
1091 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1092 fdt_get_name(blob, node_offset, NULL));
1093 goto bail;
1094 }
1095 memcpy(addr, in_addr, na * 4);
1096
1097 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1098 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1099 of_dump_addr("OF: translating address:", addr, na);
1100
1101 /* Translate */
1102 for (;;) {
1103 /* Switch to parent bus */
1104 node_offset = parent;
1105 parent = fdt_parent_offset(blob, node_offset);
1106
1107 /* If root, we have finished */
1108 if (parent < 0) {
1109 debug("OF: reached root node\n");
1110 result = of_read_number(addr, na);
1111 break;
1112 }
1113
1114 /* Get new parent bus and counts */
1115 pbus = &of_busses[0];
Scott Wood6395f312010-08-12 18:37:39 -05001116 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001117 if (!OF_CHECK_COUNTS(pna, pns)) {
1118 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1119 fdt_get_name(blob, node_offset, NULL));
1120 break;
1121 }
1122
1123 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1124 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1125
1126 /* Apply bus translation */
1127 if (of_translate_one(blob, node_offset, bus, pbus,
1128 addr, na, ns, pna, rprop))
1129 break;
1130
1131 /* Complete the move up one level */
1132 na = pna;
1133 ns = pns;
1134 bus = pbus;
1135
1136 of_dump_addr("OF: one level translation:", addr, na);
1137 }
1138 bail:
1139
1140 return result;
1141}
1142
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001143u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001144{
1145 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1146}
Kumar Gala75e73af2010-07-04 12:48:21 -05001147
1148/**
1149 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1150 * who's reg property matches a physical cpu address
1151 *
1152 * @blob: ptr to device tree
1153 * @compat: compatiable string to match
1154 * @compat_off: property name
1155 *
1156 */
1157int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1158 phys_addr_t compat_off)
1159{
1160 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1161 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001162 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala75e73af2010-07-04 12:48:21 -05001163 if (reg) {
1164 if (compat_off == fdt_translate_address(blob, off, reg))
1165 return off;
1166 }
1167 off = fdt_node_offset_by_compatible(blob, off, compat);
1168 }
1169
1170 return -FDT_ERR_NOTFOUND;
1171}
1172
Kumar Galab4b847e2010-07-09 16:18:58 -05001173/**
1174 * fdt_alloc_phandle: Return next free phandle value
1175 *
1176 * @blob: ptr to device tree
1177 */
1178int fdt_alloc_phandle(void *blob)
1179{
Timur Tabi50bf17b2011-09-20 18:24:35 -05001180 int offset, phandle = 0;
Kumar Gala75e73af2010-07-04 12:48:21 -05001181
Kumar Galab4b847e2010-07-09 16:18:58 -05001182 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1183 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabi50bf17b2011-09-20 18:24:35 -05001184 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab4b847e2010-07-09 16:18:58 -05001185 }
1186
1187 return phandle + 1;
1188}
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001189
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001190/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001191 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001192 *
1193 * @fdt: ptr to device tree
1194 * @nodeoffset: node to update
1195 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001196 */
1197int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001198{
1199 int ret;
1200
1201#ifdef DEBUG
1202 int off = fdt_node_offset_by_phandle(fdt, phandle);
1203
1204 if ((off >= 0) && (off != nodeoffset)) {
1205 char buf[64];
1206
1207 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1208 printf("Trying to update node %s with phandle %u ",
1209 buf, phandle);
1210
1211 fdt_get_path(fdt, off, buf, sizeof(buf));
1212 printf("that already exists in node %s.\n", buf);
1213 return -FDT_ERR_BADPHANDLE;
1214 }
1215#endif
1216
1217 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1218 if (ret < 0)
1219 return ret;
1220
1221 /*
1222 * For now, also set the deprecated "linux,phandle" property, so that we
1223 * don't break older kernels.
1224 */
1225 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1226
1227 return ret;
1228}
1229
Kumar Gala10aeabd2011-08-01 00:25:20 -05001230/*
1231 * fdt_create_phandle: Create a phandle property for the given node
1232 *
1233 * @fdt: ptr to device tree
1234 * @nodeoffset: node to update
1235 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001236unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001237{
1238 /* see if there is a phandle already */
1239 int phandle = fdt_get_phandle(fdt, nodeoffset);
1240
1241 /* if we got 0, means no phandle so create one */
1242 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001243 int ret;
1244
Kumar Gala10aeabd2011-08-01 00:25:20 -05001245 phandle = fdt_alloc_phandle(fdt);
Timur Tabi3c927cc2011-09-20 18:24:34 -05001246 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1247 if (ret < 0) {
1248 printf("Can't set phandle %u: %s\n", phandle,
1249 fdt_strerror(ret));
1250 return 0;
1251 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001252 }
1253
1254 return phandle;
1255}
1256
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001257/*
1258 * fdt_set_node_status: Set status for the given node
1259 *
1260 * @fdt: ptr to device tree
1261 * @nodeoffset: node to update
1262 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1263 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1264 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1265 */
1266int fdt_set_node_status(void *fdt, int nodeoffset,
1267 enum fdt_status status, unsigned int error_code)
1268{
1269 char buf[16];
1270 int ret = 0;
1271
1272 if (nodeoffset < 0)
1273 return nodeoffset;
1274
1275 switch (status) {
1276 case FDT_STATUS_OKAY:
1277 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1278 break;
1279 case FDT_STATUS_DISABLED:
1280 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1281 break;
1282 case FDT_STATUS_FAIL:
1283 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1284 break;
1285 case FDT_STATUS_FAIL_ERROR_CODE:
1286 sprintf(buf, "fail-%d", error_code);
1287 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1288 break;
1289 default:
1290 printf("Invalid fdt status: %x\n", status);
1291 ret = -1;
1292 break;
1293 }
1294
1295 return ret;
1296}
1297
1298/*
1299 * fdt_set_status_by_alias: Set status for the given node given an alias
1300 *
1301 * @fdt: ptr to device tree
1302 * @alias: alias of node to update
1303 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1304 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1305 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1306 */
1307int fdt_set_status_by_alias(void *fdt, const char* alias,
1308 enum fdt_status status, unsigned int error_code)
1309{
1310 int offset = fdt_path_offset(fdt, alias);
1311
1312 return fdt_set_node_status(fdt, offset, status, error_code);
1313}
1314
Tom Wai-Hong Tam096eb3f2012-12-05 14:46:41 +00001315#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001316int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1317{
1318 int noff;
1319 int ret;
1320
1321 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1322 if (noff != -FDT_ERR_NOTFOUND) {
1323 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1324add_edid:
1325 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1326 if (ret == -FDT_ERR_NOSPACE) {
1327 ret = fdt_increase_size(blob, 512);
1328 if (!ret)
1329 goto add_edid;
1330 else
1331 goto err_size;
1332 } else if (ret < 0) {
1333 printf("Can't add property: %s\n", fdt_strerror(ret));
1334 return ret;
1335 }
1336 }
1337 return 0;
1338err_size:
1339 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1340 return ret;
1341}
1342#endif
Timur Tabibb682002011-05-03 13:24:07 -05001343
1344/*
1345 * Verify the physical address of device tree node for a given alias
1346 *
1347 * This function locates the device tree node of a given alias, and then
1348 * verifies that the physical address of that device matches the given
1349 * parameter. It displays a message if there is a mismatch.
1350 *
1351 * Returns 1 on success, 0 on failure
1352 */
1353int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1354{
1355 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001356 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001357 int node, len;
1358 u64 dt_addr;
1359
1360 path = fdt_getprop(fdt, anode, alias, NULL);
1361 if (!path) {
1362 /* If there's no such alias, then it's not a failure */
1363 return 1;
1364 }
1365
1366 node = fdt_path_offset(fdt, path);
1367 if (node < 0) {
1368 printf("Warning: device tree alias '%s' points to invalid "
1369 "node %s.\n", alias, path);
1370 return 0;
1371 }
1372
1373 reg = fdt_getprop(fdt, node, "reg", &len);
1374 if (!reg) {
1375 printf("Warning: device tree node '%s' has no address.\n",
1376 path);
1377 return 0;
1378 }
1379
1380 dt_addr = fdt_translate_address(fdt, node, reg);
1381 if (addr != dt_addr) {
1382 printf("Warning: U-Boot configured device %s at address %llx,\n"
1383 " but the device tree has it address %llx.\n",
1384 alias, addr, dt_addr);
1385 return 0;
1386 }
1387
1388 return 1;
1389}
1390
1391/*
1392 * Returns the base address of an SOC or PCI node
1393 */
1394u64 fdt_get_base_address(void *fdt, int node)
1395{
1396 int size;
1397 u32 naddr;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001398 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001399
1400 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1401 if (prop && size == 4)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001402 naddr = be32_to_cpup(prop);
Timur Tabibb682002011-05-03 13:24:07 -05001403 else
1404 naddr = 2;
1405
1406 prop = fdt_getprop(fdt, node, "ranges", &size);
1407
1408 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1409}