blob: 321dd2a655f89f5166e87334ddb915667e986174 [file] [log] [blame]
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04001/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
Shengzhou Liua7be8022011-10-14 16:26:05 +08005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Kumar Galab87e7932010-06-16 14:27:38 -05006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04008 */
9
10#include <common.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040011#include <stdio_dev.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040012#include <linux/ctype.h>
13#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040014#include <asm/global_data.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040015#include <libfdt.h>
16#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060017#include <exports.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040018
19/*
David Fengd9850132013-12-14 11:47:29 +080020 * Get cells len in bytes
21 * if #NNNN-cells property is 2 then len is 8
22 * otherwise len is 4
23 */
24static int get_cells_len(void *blob, char *nr_cells_name)
25{
26 const fdt32_t *cell;
27
28 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
29 if (cell && fdt32_to_cpu(*cell) == 2)
30 return 8;
31
32 return 4;
33}
34
35/*
36 * Write a 4 or 8 byte big endian cell
37 */
38static void write_cell(u8 *addr, u64 val, int size)
39{
40 int shift = (size - 1) * 8;
41 while (size-- > 0) {
42 *addr++ = (val >> shift) & 0xff;
43 shift -= 8;
44 }
45}
46
Kumar Gala1ba00402008-10-23 00:05:47 -050047/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020048 * fdt_getprop_u32_default_node - Return a node's property or a default
49 *
50 * @fdt: ptr to device tree
51 * @off: offset of node
52 * @cell: cell offset in property
53 * @prop: property name
54 * @dflt: default value if the property isn't found
55 *
56 * Convenience function to return a node's property or a default value if
57 * the property doesn't exist.
58 */
59u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
60 const char *prop, const u32 dflt)
61{
62 const fdt32_t *val;
63 int len;
64
65 val = fdt_getprop(fdt, off, prop, &len);
66
67 /* Check if property exists */
68 if (!val)
69 return dflt;
70
71 /* Check if property is long enough */
72 if (len < ((cell + 1) * sizeof(uint32_t)))
73 return dflt;
74
75 return fdt32_to_cpu(*val);
76}
77
78/**
Kumar Gala1ba00402008-10-23 00:05:47 -050079 * fdt_getprop_u32_default - Find a node and return it's property or a default
80 *
81 * @fdt: ptr to device tree
82 * @path: path of node
83 * @prop: property name
84 * @dflt: default value if the property isn't found
85 *
86 * Convenience function to find a node and return it's property or a
87 * default value if it doesn't exist.
88 */
Gabe Blackcd37de72011-11-08 01:09:44 -080089u32 fdt_getprop_u32_default(const void *fdt, const char *path,
90 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050091{
Kumar Gala1ba00402008-10-23 00:05:47 -050092 int off;
93
94 off = fdt_path_offset(fdt, path);
95 if (off < 0)
96 return dflt;
97
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020098 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050099}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400100
Kumar Galabf771dc2007-10-24 10:21:57 -0500101/**
102 * fdt_find_and_setprop: Find a node and set it's property
103 *
104 * @fdt: ptr to device tree
105 * @node: path of node
106 * @prop: property name
107 * @val: ptr to new value
108 * @len: length of new property value
109 * @create: flag to create the property if it doesn't exist
110 *
111 * Convenience function to directly set a property given the path to the node.
112 */
113int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
114 const void *val, int len, int create)
115{
Kumar Galac8ab7052007-10-24 11:04:22 -0500116 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -0500117
118 if (nodeoff < 0)
119 return nodeoff;
120
Kim Phillips6542c072013-01-16 14:00:11 +0000121 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500122 return 0; /* create flag not set; so exit quietly */
123
124 return fdt_setprop(fdt, nodeoff, prop, val, len);
125}
126
Masahiro Yamadac0207282014-04-18 17:40:58 +0900127/**
128 * fdt_find_or_add_subnode - find or possibly add a subnode of a given node
129 * @fdt: pointer to the device tree blob
130 * @parentoffset: structure block offset of a node
131 * @name: name of the subnode to locate
132 *
133 * fdt_subnode_offset() finds a subnode of the node with a given name.
134 * If the subnode does not exist, it will be created.
135 */
136static int fdt_find_or_add_subnode(void *fdt, int parentoffset,
137 const char *name)
138{
139 int offset;
140
141 offset = fdt_subnode_offset(fdt, parentoffset, name);
142
143 if (offset == -FDT_ERR_NOTFOUND)
144 offset = fdt_add_subnode(fdt, parentoffset, name);
145
146 if (offset < 0)
147 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
148
149 return offset;
150}
151
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600152#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400153
Marek Vasut52039da2012-09-14 23:45:51 +0200154#ifdef CONFIG_CONS_INDEX
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400155static void fdt_fill_multisername(char *sername, size_t maxlen)
156{
157 const char *outname = stdio_devices[stdout]->name;
158
159 if (strcmp(outname, "serial") > 0)
160 strncpy(sername, outname, maxlen);
161
162 /* eserial? */
163 if (strcmp(outname + 1, "serial") > 0)
164 strncpy(sername, outname + 1, maxlen);
165}
Marek Vasut52039da2012-09-14 23:45:51 +0200166#endif
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400167
Detlev Zundel46e192a2008-06-20 22:24:05 +0200168static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600169{
170 int err = 0;
171#ifdef CONFIG_CONS_INDEX
172 int node;
173 char sername[9] = { 0 };
174 const char *path;
175
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400176 fdt_fill_multisername(sername, sizeof(sername) - 1);
177 if (!sername[0])
178 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600179
180 err = node = fdt_path_offset(fdt, "/aliases");
181 if (node >= 0) {
182 int len;
183 path = fdt_getprop(fdt, node, sername, &len);
184 if (path) {
185 char *p = malloc(len);
186 err = -FDT_ERR_NOSPACE;
187 if (p) {
188 memcpy(p, path, len);
Detlev Zundel46e192a2008-06-20 22:24:05 +0200189 err = fdt_setprop(fdt, chosenoff,
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600190 "linux,stdout-path", p, len);
191 free(p);
192 }
193 } else {
194 err = len;
195 }
196 }
197#endif
198 if (err < 0)
199 printf("WARNING: could not set linux,stdout-path %s.\n",
200 fdt_strerror(err));
201
202 return err;
203}
204#endif
205
Masahiro Yamada5b114892014-04-18 17:40:59 +0900206int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400207{
David Fengd9850132013-12-14 11:47:29 +0800208 int nodeoffset, addr_cell_len;
Kumar Gala99ebc052008-08-15 08:24:43 -0500209 int err, j, total;
David Fengd9850132013-12-14 11:47:29 +0800210 fdt64_t tmp;
Kumar Gala99ebc052008-08-15 08:24:43 -0500211 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400212
Masahiro Yamadac0207282014-04-18 17:40:58 +0900213 /* find or create "/chosen" node. */
214 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
215 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500216 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400217
Kumar Gala99ebc052008-08-15 08:24:43 -0500218 /* just return if initrd_start/end aren't valid */
219 if ((initrd_start == 0) || (initrd_end == 0))
220 return 0;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400221
Kumar Gala99ebc052008-08-15 08:24:43 -0500222 total = fdt_num_mem_rsv(fdt);
223
224 /*
225 * Look for an existing entry and update it. If we don't find
226 * the entry, we will j be the next available slot.
227 */
228 for (j = 0; j < total; j++) {
229 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
230 if (addr == initrd_start) {
231 fdt_del_mem_rsv(fdt, j);
232 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400233 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500234 }
235
Grant Likelyc379a562011-03-28 09:58:55 +0000236 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500237 if (err < 0) {
238 printf("fdt_initrd: %s\n", fdt_strerror(err));
239 return err;
240 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500241
David Fengd9850132013-12-14 11:47:29 +0800242 addr_cell_len = get_cells_len(fdt, "#address-cells");
243
Masahiro Yamada5b114892014-04-18 17:40:59 +0900244 write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
245 err = fdt_setprop(fdt, nodeoffset,
246 "linux,initrd-start", &tmp, addr_cell_len);
247 if (err < 0) {
248 printf("WARNING: could not set linux,initrd-start %s.\n",
249 fdt_strerror(err));
250 return err;
251 }
252 write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
253 err = fdt_setprop(fdt, nodeoffset,
Tom Rini2cc84cc2014-01-20 17:45:33 -0500254 "linux,initrd-end", &tmp, addr_cell_len);
Masahiro Yamada5b114892014-04-18 17:40:59 +0900255 if (err < 0) {
256 printf("WARNING: could not set linux,initrd-end %s.\n",
257 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500258
Masahiro Yamada5b114892014-04-18 17:40:59 +0900259 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400260 }
261
Kumar Gala99ebc052008-08-15 08:24:43 -0500262 return 0;
263}
264
Heiko Schocherd29abae2008-09-11 08:11:23 +0200265int fdt_chosen(void *fdt, int force)
Kumar Gala99ebc052008-08-15 08:24:43 -0500266{
267 int nodeoffset;
268 int err;
269 char *str; /* used to set string properties */
270 const char *path;
271
272 err = fdt_check_header(fdt);
273 if (err < 0) {
274 printf("fdt_chosen: %s\n", fdt_strerror(err));
275 return err;
276 }
277
Masahiro Yamadac0207282014-04-18 17:40:58 +0900278 /* find or create "/chosen" node. */
279 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
280 if (nodeoffset < 0)
281 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400282
283 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500284 * Create /chosen properites that don't exist in the fdt.
285 * If the property exists, update it only if the "force" parameter
286 * is true.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400287 */
288 str = getenv("bootargs");
289 if (str != NULL) {
Gerald Van Baren569c0352007-12-29 22:45:27 -0500290 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
291 if ((path == NULL) || force) {
292 err = fdt_setprop(fdt, nodeoffset,
293 "bootargs", str, strlen(str)+1);
294 if (err < 0)
295 printf("WARNING: could not set bootargs %s.\n",
296 fdt_strerror(err));
297 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400298 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600299
300#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Baren569c0352007-12-29 22:45:27 -0500301 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
302 if ((path == NULL) || force)
303 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600304#endif
305
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400306#ifdef OF_STDOUT_PATH
Gerald Van Baren569c0352007-12-29 22:45:27 -0500307 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
308 if ((path == NULL) || force) {
309 err = fdt_setprop(fdt, nodeoffset,
310 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
311 if (err < 0)
312 printf("WARNING: could not set linux,stdout-path %s.\n",
313 fdt_strerror(err));
314 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400315#endif
316
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400317 return err;
318}
319
Kumar Gala7e64cf82007-11-03 19:46:28 -0500320void do_fixup_by_path(void *fdt, const char *path, const char *prop,
321 const void *val, int len, int create)
322{
323#if defined(DEBUG)
324 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600325 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500326 for (i = 0; i < len; i++)
327 debug(" %.2x", *(u8*)(val+i));
328 debug("\n");
329#endif
330 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
331 if (rc)
332 printf("Unable to update property %s:%s, err=%s\n",
333 path, prop, fdt_strerror(rc));
334}
335
336void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
337 u32 val, int create)
338{
Kim Phillips6542c072013-01-16 14:00:11 +0000339 fdt32_t tmp = cpu_to_fdt32(val);
340 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500341}
342
Kumar Gala7590a192007-11-21 13:30:15 -0600343void do_fixup_by_prop(void *fdt,
344 const char *pname, const void *pval, int plen,
345 const char *prop, const void *val, int len,
346 int create)
347{
348 int off;
349#if defined(DEBUG)
350 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600351 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600352 for (i = 0; i < len; i++)
353 debug(" %.2x", *(u8*)(val+i));
354 debug("\n");
355#endif
356 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
357 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000358 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600359 fdt_setprop(fdt, off, prop, val, len);
360 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
361 }
362}
363
364void do_fixup_by_prop_u32(void *fdt,
365 const char *pname, const void *pval, int plen,
366 const char *prop, u32 val, int create)
367{
Kim Phillips6542c072013-01-16 14:00:11 +0000368 fdt32_t tmp = cpu_to_fdt32(val);
369 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600370}
371
372void do_fixup_by_compat(void *fdt, const char *compat,
373 const char *prop, const void *val, int len, int create)
374{
375 int off = -1;
376#if defined(DEBUG)
377 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600378 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600379 for (i = 0; i < len; i++)
380 debug(" %.2x", *(u8*)(val+i));
381 debug("\n");
382#endif
383 off = fdt_node_offset_by_compatible(fdt, -1, compat);
384 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000385 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600386 fdt_setprop(fdt, off, prop, val, len);
387 off = fdt_node_offset_by_compatible(fdt, off, compat);
388 }
389}
390
391void do_fixup_by_compat_u32(void *fdt, const char *compat,
392 const char *prop, u32 val, int create)
393{
Kim Phillips6542c072013-01-16 14:00:11 +0000394 fdt32_t tmp = cpu_to_fdt32(val);
395 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600396}
397
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000398#ifdef CONFIG_NR_DRAM_BANKS
399#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
400#else
Kim Phillips6542c072013-01-16 14:00:11 +0000401#define MEMORY_BANKS_MAX 4
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000402#endif
John Rigbyb4ae9422010-10-13 13:57:33 -0600403int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
404{
405 int err, nodeoffset;
406 int addr_cell_len, size_cell_len, len;
Kim Phillips6542c072013-01-16 14:00:11 +0000407 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbyb4ae9422010-10-13 13:57:33 -0600408 int bank;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600409
Kim Phillips6542c072013-01-16 14:00:11 +0000410 if (banks > MEMORY_BANKS_MAX) {
411 printf("%s: num banks %d exceeds hardcoded limit %d."
412 " Recompile with higher MEMORY_BANKS_MAX?\n",
413 __FUNCTION__, banks, MEMORY_BANKS_MAX);
414 return -1;
415 }
416
Kumar Gala0db72ed2007-11-26 14:57:45 -0600417 err = fdt_check_header(blob);
418 if (err < 0) {
419 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
420 return err;
421 }
422
Masahiro Yamadac0207282014-04-18 17:40:58 +0900423 /* find or create "/memory" node. */
424 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
425 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800426 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900427
Kumar Gala0db72ed2007-11-26 14:57:45 -0600428 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
429 sizeof("memory"));
430 if (err < 0) {
431 printf("WARNING: could not set %s %s.\n", "device_type",
432 fdt_strerror(err));
433 return err;
434 }
435
John Rigbyb4ae9422010-10-13 13:57:33 -0600436 addr_cell_len = get_cells_len(blob, "#address-cells");
437 size_cell_len = get_cells_len(blob, "#size-cells");
438
439 for (bank = 0, len = 0; bank < banks; bank++) {
440 write_cell(tmp + len, start[bank], addr_cell_len);
441 len += addr_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600442
John Rigbyb4ae9422010-10-13 13:57:33 -0600443 write_cell(tmp + len, size[bank], size_cell_len);
444 len += size_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600445 }
446
447 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
448 if (err < 0) {
449 printf("WARNING: could not set %s %s.\n",
450 "reg", fdt_strerror(err));
451 return err;
452 }
453 return 0;
454}
455
John Rigbyb4ae9422010-10-13 13:57:33 -0600456int fdt_fixup_memory(void *blob, u64 start, u64 size)
457{
458 return fdt_fixup_memory_banks(blob, &start, &size, 1);
459}
460
Kumar Galafabda922008-08-19 15:41:18 -0500461void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600462{
Kumar Galafabda922008-08-19 15:41:18 -0500463 int node, i, j;
464 char enet[16], *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000465 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600466 const char *path;
Kumar Galafabda922008-08-19 15:41:18 -0500467 unsigned char mac_addr[6];
Kumar Gala22699102007-11-21 11:11:03 -0600468
469 node = fdt_path_offset(fdt, "/aliases");
Kumar Galafabda922008-08-19 15:41:18 -0500470 if (node < 0)
471 return;
472
Dan Murphy0b9bf272013-10-02 14:00:15 -0500473 if (!getenv("ethaddr")) {
474 if (getenv("usbethaddr")) {
475 strcpy(mac, "usbethaddr");
476 } else {
477 debug("No ethernet MAC Address defined\n");
478 return;
479 }
480 } else {
481 strcpy(mac, "ethaddr");
482 }
483
Kumar Galafabda922008-08-19 15:41:18 -0500484 i = 0;
485 while ((tmp = getenv(mac)) != NULL) {
486 sprintf(enet, "ethernet%d", i);
487 path = fdt_getprop(fdt, node, enet, NULL);
488 if (!path) {
489 debug("No alias for %s\n", enet);
490 sprintf(mac, "eth%daddr", ++i);
491 continue;
Kumar Gala22699102007-11-21 11:11:03 -0600492 }
Kumar Galafabda922008-08-19 15:41:18 -0500493
494 for (j = 0; j < 6; j++) {
495 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
496 if (tmp)
497 tmp = (*end) ? end+1 : end;
Kumar Gala22699102007-11-21 11:11:03 -0600498 }
Kumar Galafabda922008-08-19 15:41:18 -0500499
500 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
501 do_fixup_by_path(fdt, path, "local-mac-address",
502 &mac_addr, 6, 1);
503
504 sprintf(mac, "eth%daddr", ++i);
Kumar Gala22699102007-11-21 11:11:03 -0600505 }
506}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300507
Kumar Galaf2f58c52008-08-15 08:24:42 -0500508/* Resize the fdt to its actual size + a bit of padding */
509int fdt_resize(void *blob)
510{
511 int i;
512 uint64_t addr, size;
513 int total, ret;
514 uint actualsize;
515
516 if (!blob)
517 return 0;
518
519 total = fdt_num_mem_rsv(blob);
520 for (i = 0; i < total; i++) {
521 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000522 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500523 fdt_del_mem_rsv(blob, i);
524 break;
525 }
526 }
527
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100528 /*
529 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200530 * plus the size needed for 5 fdt_add_mem_rsv, one
531 * for the fdt itself and 4 for a possible initrd
532 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100533 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500534 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200535 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500536
537 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000538 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
539 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500540
541 /* Change the fdt header to reflect the correct size */
542 fdt_set_totalsize(blob, actualsize);
543
544 /* Add the new reservation */
Simon Glassfc953242011-09-17 06:48:58 +0000545 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500546 if (ret < 0)
547 return ret;
548
549 return actualsize;
550}
Kumar Galae0475cd2008-10-22 23:33:56 -0500551
552#ifdef CONFIG_PCI
Kumar Galabaf41892009-08-05 09:03:54 -0500553#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500554
555#define FDT_PCI_PREFETCH (0x40000000)
556#define FDT_PCI_MEM32 (0x02000000)
557#define FDT_PCI_IO (0x01000000)
558#define FDT_PCI_MEM64 (0x03000000)
559
560int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
561
562 int addrcell, sizecell, len, r;
563 u32 *dma_range;
564 /* sized based on pci addr cells, size-cells, & address-cells */
565 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
566
567 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
568 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
569
570 dma_range = &dma_ranges[0];
571 for (r = 0; r < hose->region_count; r++) {
572 u64 bus_start, phys_start, size;
573
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600574 /* skip if !PCI_REGION_SYS_MEMORY */
575 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500576 continue;
577
578 bus_start = (u64)hose->regions[r].bus_start;
579 phys_start = (u64)hose->regions[r].phys_start;
580 size = (u64)hose->regions[r].size;
581
582 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500583 if (size >= 0x100000000ull)
Kumar Galae0475cd2008-10-22 23:33:56 -0500584 dma_range[0] |= FDT_PCI_MEM64;
585 else
586 dma_range[0] |= FDT_PCI_MEM32;
587 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
588 dma_range[0] |= FDT_PCI_PREFETCH;
589#ifdef CONFIG_SYS_PCI_64BIT
590 dma_range[1] = bus_start >> 32;
591#else
592 dma_range[1] = 0;
593#endif
594 dma_range[2] = bus_start & 0xffffffff;
595
596 if (addrcell == 2) {
597 dma_range[3] = phys_start >> 32;
598 dma_range[4] = phys_start & 0xffffffff;
599 } else {
600 dma_range[3] = phys_start & 0xffffffff;
601 }
602
603 if (sizecell == 2) {
604 dma_range[3 + addrcell + 0] = size >> 32;
605 dma_range[3 + addrcell + 1] = size & 0xffffffff;
606 } else {
607 dma_range[3 + addrcell + 0] = size & 0xffffffff;
608 }
609
610 dma_range += (3 + addrcell + sizecell);
611 }
612
613 len = dma_range - &dma_ranges[0];
614 if (len)
615 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
616
617 return 0;
618}
619#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200620
621#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
622/*
Stefan Roese412a71a2010-09-16 14:01:53 +0200623 * Provide a weak default function to return the flash bank size.
624 * There might be multiple non-identical flash chips connected to one
625 * chip-select, so we need to pass an index as well.
626 */
627u32 __flash_get_bank_size(int cs, int idx)
628{
629 extern flash_info_t flash_info[];
630
631 /*
632 * As default, a simple 1:1 mapping is provided. Boards with
633 * a different mapping need to supply a board specific mapping
634 * routine.
635 */
636 return flash_info[cs].size;
637}
638u32 flash_get_bank_size(int cs, int idx)
639 __attribute__((weak, alias("__flash_get_bank_size")));
640
641/*
Stefan Roesea0f10c92009-10-21 11:59:52 +0200642 * This function can be used to update the size in the "reg" property
Stefan Roese412a71a2010-09-16 14:01:53 +0200643 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roesea0f10c92009-10-21 11:59:52 +0200644 * non-fixed NOR FLASH sizes.
645 */
Stefan Roese412a71a2010-09-16 14:01:53 +0200646int fdt_fixup_nor_flash_size(void *blob)
Stefan Roesea0f10c92009-10-21 11:59:52 +0200647{
648 char compat[][16] = { "cfi-flash", "jedec-flash" };
649 int off;
650 int len;
651 struct fdt_property *prop;
Stefan Roese442cd1a2010-09-24 13:51:50 +0200652 u32 *reg, *reg2;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200653 int i;
654
655 for (i = 0; i < 2; i++) {
656 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
657 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200658 int idx;
659
Stefan Roesea0f10c92009-10-21 11:59:52 +0200660 /*
Stefan Roese412a71a2010-09-16 14:01:53 +0200661 * Found one compatible node, so fixup the size
662 * int its reg properties
Stefan Roesea0f10c92009-10-21 11:59:52 +0200663 */
664 prop = fdt_get_property_w(blob, off, "reg", &len);
665 if (prop) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200666 int tuple_size = 3 * sizeof(reg);
667
668 /*
669 * There might be multiple reg-tuples,
670 * so loop through them all
671 */
Stefan Roese442cd1a2010-09-24 13:51:50 +0200672 reg = reg2 = (u32 *)&prop->data[0];
673 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200674 /*
675 * Update size in reg property
676 */
677 reg[2] = flash_get_bank_size(reg[0],
678 idx);
Stefan Roese442cd1a2010-09-24 13:51:50 +0200679
680 /*
681 * Point to next reg tuple
682 */
683 reg += 3;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200684 }
Stefan Roese442cd1a2010-09-24 13:51:50 +0200685
686 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roesea0f10c92009-10-21 11:59:52 +0200687 }
688
689 /* Move to next compatible node */
690 off = fdt_node_offset_by_compatible(blob, off,
691 compat[i]);
692 }
693 }
694
Stefan Roese412a71a2010-09-16 14:01:53 +0200695 return 0;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200696}
697#endif
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100698
Matthew McClintockacda3a42010-10-13 13:39:26 +0200699int fdt_increase_size(void *fdt, int add_len)
700{
701 int newlen;
702
703 newlen = fdt_totalsize(fdt) + add_len;
704
705 /* Open in place with a new len */
706 return fdt_open_into(fdt, fdt, newlen);
707}
708
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100709#ifdef CONFIG_FDT_FIXUP_PARTITIONS
710#include <jffs2/load_kernel.h>
711#include <mtd_node.h>
712
713struct reg_cell {
714 unsigned int r0;
715 unsigned int r1;
716};
717
718int fdt_del_subnodes(const void *blob, int parent_offset)
719{
720 int off, ndepth;
721 int ret;
722
723 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
724 (off >= 0) && (ndepth > 0);
725 off = fdt_next_node(blob, off, &ndepth)) {
726 if (ndepth == 1) {
727 debug("delete %s: offset: %x\n",
728 fdt_get_name(blob, off, 0), off);
729 ret = fdt_del_node((void *)blob, off);
730 if (ret < 0) {
731 printf("Can't delete node: %s\n",
732 fdt_strerror(ret));
733 return ret;
734 } else {
735 ndepth = 0;
736 off = parent_offset;
737 }
738 }
739 }
740 return 0;
741}
742
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100743int fdt_del_partitions(void *blob, int parent_offset)
744{
745 const void *prop;
746 int ndepth = 0;
747 int off;
748 int ret;
749
750 off = fdt_next_node(blob, parent_offset, &ndepth);
751 if (off > 0 && ndepth == 1) {
752 prop = fdt_getprop(blob, off, "label", NULL);
753 if (prop == NULL) {
754 /*
755 * Could not find label property, nand {}; node?
756 * Check subnode, delete partitions there if any.
757 */
758 return fdt_del_partitions(blob, off);
759 } else {
760 ret = fdt_del_subnodes(blob, parent_offset);
761 if (ret < 0) {
762 printf("Can't remove subnodes: %s\n",
763 fdt_strerror(ret));
764 return ret;
765 }
766 }
767 }
768 return 0;
769}
770
771int fdt_node_set_part_info(void *blob, int parent_offset,
772 struct mtd_device *dev)
773{
774 struct list_head *pentry;
775 struct part_info *part;
776 struct reg_cell cell;
777 int off, ndepth = 0;
778 int part_num, ret;
779 char buf[64];
780
781 ret = fdt_del_partitions(blob, parent_offset);
782 if (ret < 0)
783 return ret;
784
785 /*
786 * Check if it is nand {}; subnode, adjust
787 * the offset in this case
788 */
789 off = fdt_next_node(blob, parent_offset, &ndepth);
790 if (off > 0 && ndepth == 1)
791 parent_offset = off;
792
793 part_num = 0;
794 list_for_each_prev(pentry, &dev->parts) {
795 int newoff;
796
797 part = list_entry(pentry, struct part_info, link);
798
Scott Woodefe7f302013-10-15 17:41:27 -0500799 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100800 part_num, part->name, part->size,
801 part->offset, part->mask_flags);
802
Scott Woodefe7f302013-10-15 17:41:27 -0500803 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100804add_sub:
805 ret = fdt_add_subnode(blob, parent_offset, buf);
806 if (ret == -FDT_ERR_NOSPACE) {
807 ret = fdt_increase_size(blob, 512);
808 if (!ret)
809 goto add_sub;
810 else
811 goto err_size;
812 } else if (ret < 0) {
813 printf("Can't add partition node: %s\n",
814 fdt_strerror(ret));
815 return ret;
816 }
817 newoff = ret;
818
819 /* Check MTD_WRITEABLE_CMD flag */
820 if (part->mask_flags & 1) {
821add_ro:
822 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
823 if (ret == -FDT_ERR_NOSPACE) {
824 ret = fdt_increase_size(blob, 512);
825 if (!ret)
826 goto add_ro;
827 else
828 goto err_size;
829 } else if (ret < 0)
830 goto err_prop;
831 }
832
833 cell.r0 = cpu_to_fdt32(part->offset);
834 cell.r1 = cpu_to_fdt32(part->size);
835add_reg:
836 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
837 if (ret == -FDT_ERR_NOSPACE) {
838 ret = fdt_increase_size(blob, 512);
839 if (!ret)
840 goto add_reg;
841 else
842 goto err_size;
843 } else if (ret < 0)
844 goto err_prop;
845
846add_label:
847 ret = fdt_setprop_string(blob, newoff, "label", part->name);
848 if (ret == -FDT_ERR_NOSPACE) {
849 ret = fdt_increase_size(blob, 512);
850 if (!ret)
851 goto add_label;
852 else
853 goto err_size;
854 } else if (ret < 0)
855 goto err_prop;
856
857 part_num++;
858 }
859 return 0;
860err_size:
861 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
862 return ret;
863err_prop:
864 printf("Can't add property: %s\n", fdt_strerror(ret));
865 return ret;
866}
867
868/*
869 * Update partitions in nor/nand nodes using info from
870 * mtdparts environment variable. The nodes to update are
871 * specified by node_info structure which contains mtd device
872 * type and compatible string: E. g. the board code in
873 * ft_board_setup() could use:
874 *
875 * struct node_info nodes[] = {
876 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
877 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
878 * };
879 *
880 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
881 */
882void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
883{
884 struct node_info *ni = node_info;
885 struct mtd_device *dev;
886 char *parts;
887 int i, idx;
888 int noff;
889
890 parts = getenv("mtdparts");
891 if (!parts)
892 return;
893
894 if (mtdparts_init() != 0)
895 return;
896
897 for (i = 0; i < node_info_size; i++) {
898 idx = 0;
899 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
900 while (noff != -FDT_ERR_NOTFOUND) {
901 debug("%s: %s, mtd dev type %d\n",
902 fdt_get_name(blob, noff, 0),
903 ni[i].compat, ni[i].type);
904 dev = device_find(ni[i].type, idx++);
905 if (dev) {
906 if (fdt_node_set_part_info(blob, noff, dev))
907 return; /* return on error */
908 }
909
910 /* Jump to next flash node */
911 noff = fdt_node_offset_by_compatible(blob, noff,
912 ni[i].compat);
913 }
914 }
915}
916#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -0500917
918void fdt_del_node_and_alias(void *blob, const char *alias)
919{
920 int off = fdt_path_offset(blob, alias);
921
922 if (off < 0)
923 return;
924
925 fdt_del_node(blob, off);
926
927 off = fdt_path_offset(blob, "/aliases");
928 fdt_delprop(blob, off, alias);
929}
Kumar Galab87e7932010-06-16 14:27:38 -0500930
931/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips6542c072013-01-16 14:00:11 +0000932static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galab87e7932010-06-16 14:27:38 -0500933{
934 u64 r = 0;
935 while (size--)
Kim Phillips6542c072013-01-16 14:00:11 +0000936 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galab87e7932010-06-16 14:27:38 -0500937 return r;
938}
939
Kumar Galab87e7932010-06-16 14:27:38 -0500940#define PRu64 "%llx"
941
942/* Max address size we deal with */
943#define OF_MAX_ADDR_CELLS 4
944#define OF_BAD_ADDR ((u64)-1)
945#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
946 (ns) > 0)
947
948/* Debug utility */
949#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +0000950static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -0500951{
952 printf("%s", s);
953 while(na--)
954 printf(" %08x", *(addr++));
955 printf("\n");
956}
957#else
Kim Phillips6542c072013-01-16 14:00:11 +0000958static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -0500959#endif
960
961/* Callbacks for bus specific translators */
962struct of_bus {
963 const char *name;
964 const char *addresses;
Scott Wood99899712010-08-12 18:37:39 -0500965 void (*count_cells)(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500966 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +0000967 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500968 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +0000969 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -0500970};
971
972/* Default translator (generic bus) */
Scott Wood99899712010-08-12 18:37:39 -0500973static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500974 int *addrc, int *sizec)
975{
Kim Phillips6542c072013-01-16 14:00:11 +0000976 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -0500977
978 if (addrc) {
979 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
980 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000981 *addrc = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500982 else
983 *addrc = 2;
984 }
985
986 if (sizec) {
987 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
988 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000989 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500990 else
991 *sizec = 1;
992 }
Kumar Galab87e7932010-06-16 14:27:38 -0500993}
994
Kim Phillips6542c072013-01-16 14:00:11 +0000995static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500996 int na, int ns, int pna)
997{
998 u64 cp, s, da;
999
1000 cp = of_read_number(range, na);
1001 s = of_read_number(range + na + pna, ns);
1002 da = of_read_number(addr, na);
1003
1004 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
1005 cp, s, da);
1006
1007 if (da < cp || da >= (cp + s))
1008 return OF_BAD_ADDR;
1009 return da - cp;
1010}
1011
Kim Phillips6542c072013-01-16 14:00:11 +00001012static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001013{
1014 u64 a = of_read_number(addr, na);
1015 memset(addr, 0, na * 4);
1016 a += offset;
1017 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001018 addr[na - 2] = cpu_to_fdt32(a >> 32);
1019 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001020
1021 return 0;
1022}
1023
1024/* Array of bus specific translators */
1025static struct of_bus of_busses[] = {
1026 /* Default */
1027 {
1028 .name = "default",
1029 .addresses = "reg",
1030 .count_cells = of_bus_default_count_cells,
1031 .map = of_bus_default_map,
1032 .translate = of_bus_default_translate,
1033 },
1034};
1035
1036static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001037 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001038 int na, int ns, int pna, const char *rprop)
1039{
Kim Phillips6542c072013-01-16 14:00:11 +00001040 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001041 int rlen;
1042 int rone;
1043 u64 offset = OF_BAD_ADDR;
1044
1045 /* Normally, an absence of a "ranges" property means we are
1046 * crossing a non-translatable boundary, and thus the addresses
1047 * below the current not cannot be converted to CPU physical ones.
1048 * Unfortunately, while this is very clear in the spec, it's not
1049 * what Apple understood, and they do have things like /uni-n or
1050 * /ht nodes with no "ranges" property and a lot of perfectly
1051 * useable mapped devices below them. Thus we treat the absence of
1052 * "ranges" as equivalent to an empty "ranges" property which means
1053 * a 1:1 translation at that level. It's up to the caller not to try
1054 * to translate addresses that aren't supposed to be translated in
1055 * the first place. --BenH.
1056 */
Kim Phillips6542c072013-01-16 14:00:11 +00001057 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001058 if (ranges == NULL || rlen == 0) {
1059 offset = of_read_number(addr, na);
1060 memset(addr, 0, pna * 4);
1061 debug("OF: no ranges, 1:1 translation\n");
1062 goto finish;
1063 }
1064
1065 debug("OF: walking ranges...\n");
1066
1067 /* Now walk through the ranges */
1068 rlen /= 4;
1069 rone = na + pna + ns;
1070 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1071 offset = bus->map(addr, ranges, na, ns, pna);
1072 if (offset != OF_BAD_ADDR)
1073 break;
1074 }
1075 if (offset == OF_BAD_ADDR) {
1076 debug("OF: not found !\n");
1077 return 1;
1078 }
1079 memcpy(addr, ranges + na, 4 * pna);
1080
1081 finish:
1082 of_dump_addr("OF: parent translation for:", addr, pna);
1083 debug("OF: with offset: "PRu64"\n", offset);
1084
1085 /* Translate it into parent bus space */
1086 return pbus->translate(addr, offset, pna);
1087}
1088
1089/*
1090 * Translate an address from the device-tree into a CPU physical address,
1091 * this walks up the tree and applies the various bus mappings on the
1092 * way.
1093 *
1094 * Note: We consider that crossing any level with #size-cells == 0 to mean
1095 * that translation is impossible (that is we are not dealing with a value
1096 * that can be mapped to a cpu physical address). This is not really specified
1097 * that way, but this is traditionally the way IBM at least do things
1098 */
Kim Phillips6542c072013-01-16 14:00:11 +00001099static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1100 const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001101{
1102 int parent;
1103 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001104 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001105 int na, ns, pna, pns;
1106 u64 result = OF_BAD_ADDR;
1107
1108 debug("OF: ** translation for device %s **\n",
1109 fdt_get_name(blob, node_offset, NULL));
1110
1111 /* Get parent & match bus type */
1112 parent = fdt_parent_offset(blob, node_offset);
1113 if (parent < 0)
1114 goto bail;
1115 bus = &of_busses[0];
1116
1117 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001118 bus->count_cells(blob, parent, &na, &ns);
Kumar Galab87e7932010-06-16 14:27:38 -05001119 if (!OF_CHECK_COUNTS(na, ns)) {
1120 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1121 fdt_get_name(blob, node_offset, NULL));
1122 goto bail;
1123 }
1124 memcpy(addr, in_addr, na * 4);
1125
1126 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1127 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1128 of_dump_addr("OF: translating address:", addr, na);
1129
1130 /* Translate */
1131 for (;;) {
1132 /* Switch to parent bus */
1133 node_offset = parent;
1134 parent = fdt_parent_offset(blob, node_offset);
1135
1136 /* If root, we have finished */
1137 if (parent < 0) {
1138 debug("OF: reached root node\n");
1139 result = of_read_number(addr, na);
1140 break;
1141 }
1142
1143 /* Get new parent bus and counts */
1144 pbus = &of_busses[0];
Scott Wood99899712010-08-12 18:37:39 -05001145 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galab87e7932010-06-16 14:27:38 -05001146 if (!OF_CHECK_COUNTS(pna, pns)) {
1147 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1148 fdt_get_name(blob, node_offset, NULL));
1149 break;
1150 }
1151
1152 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1153 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1154
1155 /* Apply bus translation */
1156 if (of_translate_one(blob, node_offset, bus, pbus,
1157 addr, na, ns, pna, rprop))
1158 break;
1159
1160 /* Complete the move up one level */
1161 na = pna;
1162 ns = pns;
1163 bus = pbus;
1164
1165 of_dump_addr("OF: one level translation:", addr, na);
1166 }
1167 bail:
1168
1169 return result;
1170}
1171
Kim Phillips6542c072013-01-16 14:00:11 +00001172u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001173{
1174 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1175}
Kumar Gala800d1d12010-07-04 12:48:21 -05001176
1177/**
1178 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1179 * who's reg property matches a physical cpu address
1180 *
1181 * @blob: ptr to device tree
1182 * @compat: compatiable string to match
1183 * @compat_off: property name
1184 *
1185 */
1186int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1187 phys_addr_t compat_off)
1188{
1189 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1190 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +00001191 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala800d1d12010-07-04 12:48:21 -05001192 if (reg) {
1193 if (compat_off == fdt_translate_address(blob, off, reg))
1194 return off;
1195 }
1196 off = fdt_node_offset_by_compatible(blob, off, compat);
1197 }
1198
1199 return -FDT_ERR_NOTFOUND;
1200}
1201
Kumar Galab7afb112010-07-09 16:18:58 -05001202/**
1203 * fdt_alloc_phandle: Return next free phandle value
1204 *
1205 * @blob: ptr to device tree
1206 */
1207int fdt_alloc_phandle(void *blob)
1208{
Timur Tabic6d83032011-09-20 18:24:35 -05001209 int offset, phandle = 0;
Kumar Galab7afb112010-07-09 16:18:58 -05001210
1211 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1212 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabic6d83032011-09-20 18:24:35 -05001213 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab7afb112010-07-09 16:18:58 -05001214 }
Kumar Gala800d1d12010-07-04 12:48:21 -05001215
Kumar Galab7afb112010-07-09 16:18:58 -05001216 return phandle + 1;
1217}
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001218
Gerald Van Barenaf737882011-07-14 21:40:10 -04001219/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001220 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001221 *
1222 * @fdt: ptr to device tree
1223 * @nodeoffset: node to update
1224 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001225 */
1226int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001227{
1228 int ret;
1229
1230#ifdef DEBUG
1231 int off = fdt_node_offset_by_phandle(fdt, phandle);
1232
1233 if ((off >= 0) && (off != nodeoffset)) {
1234 char buf[64];
1235
1236 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1237 printf("Trying to update node %s with phandle %u ",
1238 buf, phandle);
1239
1240 fdt_get_path(fdt, off, buf, sizeof(buf));
1241 printf("that already exists in node %s.\n", buf);
1242 return -FDT_ERR_BADPHANDLE;
1243 }
1244#endif
1245
1246 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1247 if (ret < 0)
1248 return ret;
1249
1250 /*
1251 * For now, also set the deprecated "linux,phandle" property, so that we
1252 * don't break older kernels.
1253 */
1254 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1255
1256 return ret;
1257}
1258
Kumar Galad5fc2582011-08-01 00:25:20 -05001259/*
1260 * fdt_create_phandle: Create a phandle property for the given node
1261 *
1262 * @fdt: ptr to device tree
1263 * @nodeoffset: node to update
1264 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001265unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001266{
1267 /* see if there is a phandle already */
1268 int phandle = fdt_get_phandle(fdt, nodeoffset);
1269
1270 /* if we got 0, means no phandle so create one */
1271 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001272 int ret;
1273
Kumar Galad5fc2582011-08-01 00:25:20 -05001274 phandle = fdt_alloc_phandle(fdt);
Timur Tabi0f46c802011-09-20 18:24:34 -05001275 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1276 if (ret < 0) {
1277 printf("Can't set phandle %u: %s\n", phandle,
1278 fdt_strerror(ret));
1279 return 0;
1280 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001281 }
1282
1283 return phandle;
1284}
1285
Shengzhou Liua7be8022011-10-14 16:26:05 +08001286/*
1287 * fdt_set_node_status: Set status for the given node
1288 *
1289 * @fdt: ptr to device tree
1290 * @nodeoffset: node to update
1291 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1292 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1293 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1294 */
1295int fdt_set_node_status(void *fdt, int nodeoffset,
1296 enum fdt_status status, unsigned int error_code)
1297{
1298 char buf[16];
1299 int ret = 0;
1300
1301 if (nodeoffset < 0)
1302 return nodeoffset;
1303
1304 switch (status) {
1305 case FDT_STATUS_OKAY:
1306 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1307 break;
1308 case FDT_STATUS_DISABLED:
1309 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1310 break;
1311 case FDT_STATUS_FAIL:
1312 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1313 break;
1314 case FDT_STATUS_FAIL_ERROR_CODE:
1315 sprintf(buf, "fail-%d", error_code);
1316 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1317 break;
1318 default:
1319 printf("Invalid fdt status: %x\n", status);
1320 ret = -1;
1321 break;
1322 }
1323
1324 return ret;
1325}
1326
1327/*
1328 * fdt_set_status_by_alias: Set status for the given node given an alias
1329 *
1330 * @fdt: ptr to device tree
1331 * @alias: alias of node to update
1332 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1333 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1334 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1335 */
1336int fdt_set_status_by_alias(void *fdt, const char* alias,
1337 enum fdt_status status, unsigned int error_code)
1338{
1339 int offset = fdt_path_offset(fdt, alias);
1340
1341 return fdt_set_node_status(fdt, offset, status, error_code);
1342}
1343
Tom Wai-Hong Tamdb4a88d2012-12-05 14:46:41 +00001344#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001345int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1346{
1347 int noff;
1348 int ret;
1349
1350 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1351 if (noff != -FDT_ERR_NOTFOUND) {
1352 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1353add_edid:
1354 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1355 if (ret == -FDT_ERR_NOSPACE) {
1356 ret = fdt_increase_size(blob, 512);
1357 if (!ret)
1358 goto add_edid;
1359 else
1360 goto err_size;
1361 } else if (ret < 0) {
1362 printf("Can't add property: %s\n", fdt_strerror(ret));
1363 return ret;
1364 }
1365 }
1366 return 0;
1367err_size:
1368 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1369 return ret;
1370}
1371#endif
Timur Tabi8ebaf202011-05-03 13:24:07 -05001372
1373/*
1374 * Verify the physical address of device tree node for a given alias
1375 *
1376 * This function locates the device tree node of a given alias, and then
1377 * verifies that the physical address of that device matches the given
1378 * parameter. It displays a message if there is a mismatch.
1379 *
1380 * Returns 1 on success, 0 on failure
1381 */
1382int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1383{
1384 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001385 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001386 int node, len;
1387 u64 dt_addr;
1388
1389 path = fdt_getprop(fdt, anode, alias, NULL);
1390 if (!path) {
1391 /* If there's no such alias, then it's not a failure */
1392 return 1;
1393 }
1394
1395 node = fdt_path_offset(fdt, path);
1396 if (node < 0) {
1397 printf("Warning: device tree alias '%s' points to invalid "
1398 "node %s.\n", alias, path);
1399 return 0;
1400 }
1401
1402 reg = fdt_getprop(fdt, node, "reg", &len);
1403 if (!reg) {
1404 printf("Warning: device tree node '%s' has no address.\n",
1405 path);
1406 return 0;
1407 }
1408
1409 dt_addr = fdt_translate_address(fdt, node, reg);
1410 if (addr != dt_addr) {
1411 printf("Warning: U-Boot configured device %s at address %llx,\n"
1412 " but the device tree has it address %llx.\n",
1413 alias, addr, dt_addr);
1414 return 0;
1415 }
1416
1417 return 1;
1418}
1419
1420/*
1421 * Returns the base address of an SOC or PCI node
1422 */
1423u64 fdt_get_base_address(void *fdt, int node)
1424{
1425 int size;
1426 u32 naddr;
Kim Phillips6542c072013-01-16 14:00:11 +00001427 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001428
1429 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1430 if (prop && size == 4)
Kim Phillips6542c072013-01-16 14:00:11 +00001431 naddr = be32_to_cpup(prop);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001432 else
1433 naddr = 2;
1434
1435 prop = fdt_getprop(fdt, node, "ranges", &size);
1436
1437 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1438}
Alexander Graf19555be2014-04-11 17:09:41 +02001439
1440/*
1441 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
1442 */
1443static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1444 uint64_t *val, int cells)
1445{
1446 const fdt32_t *prop32 = &prop[cell_off];
1447 const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
1448
1449 if ((cell_off + cells) > prop_len)
1450 return -FDT_ERR_NOSPACE;
1451
1452 switch (cells) {
1453 case 1:
1454 *val = fdt32_to_cpu(*prop32);
1455 break;
1456 case 2:
1457 *val = fdt64_to_cpu(*prop64);
1458 break;
1459 default:
1460 return -FDT_ERR_NOSPACE;
1461 }
1462
1463 return 0;
1464}
1465
1466/**
1467 * fdt_read_range - Read a node's n'th range property
1468 *
1469 * @fdt: ptr to device tree
1470 * @node: offset of node
1471 * @n: range index
1472 * @child_addr: pointer to storage for the "child address" field
1473 * @addr: pointer to storage for the CPU view translated physical start
1474 * @len: pointer to storage for the range length
1475 *
1476 * Convenience function that reads and interprets a specific range out of
1477 * a number of the "ranges" property array.
1478 */
1479int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1480 uint64_t *addr, uint64_t *len)
1481{
1482 int pnode = fdt_parent_offset(fdt, node);
1483 const fdt32_t *ranges;
1484 int pacells;
1485 int acells;
1486 int scells;
1487 int ranges_len;
1488 int cell = 0;
1489 int r = 0;
1490
1491 /*
1492 * The "ranges" property is an array of
1493 * { <child address> <parent address> <size in child address space> }
1494 *
1495 * All 3 elements can span a diffent number of cells. Fetch their size.
1496 */
1497 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1498 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1499 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1500
1501 /* Now try to get the ranges property */
1502 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1503 if (!ranges)
1504 return -FDT_ERR_NOTFOUND;
1505 ranges_len /= sizeof(uint32_t);
1506
1507 /* Jump to the n'th entry */
1508 cell = n * (pacells + acells + scells);
1509
1510 /* Read <child address> */
1511 if (child_addr) {
1512 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1513 acells);
1514 if (r)
1515 return r;
1516 }
1517 cell += acells;
1518
1519 /* Read <parent address> */
1520 if (addr)
1521 *addr = fdt_translate_address(fdt, node, ranges + cell);
1522 cell += pacells;
1523
1524 /* Read <size in child address space> */
1525 if (len) {
1526 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1527 if (r)
1528 return r;
1529 }
1530
1531 return 0;
1532}