blob: f9f358e7e8308e17dd76a6268558f45f3fa0d570 [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/*
20 * Global data (for the gd->bd)
21 */
22DECLARE_GLOBAL_DATA_PTR;
23
David Fengd9850132013-12-14 11:47:29 +080024/*
25 * Get cells len in bytes
26 * if #NNNN-cells property is 2 then len is 8
27 * otherwise len is 4
28 */
29static int get_cells_len(void *blob, char *nr_cells_name)
30{
31 const fdt32_t *cell;
32
33 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
34 if (cell && fdt32_to_cpu(*cell) == 2)
35 return 8;
36
37 return 4;
38}
39
40/*
41 * Write a 4 or 8 byte big endian cell
42 */
43static void write_cell(u8 *addr, u64 val, int size)
44{
45 int shift = (size - 1) * 8;
46 while (size-- > 0) {
47 *addr++ = (val >> shift) & 0xff;
48 shift -= 8;
49 }
50}
51
Kumar Gala1ba00402008-10-23 00:05:47 -050052/**
53 * fdt_getprop_u32_default - Find a node and return it's property or a default
54 *
55 * @fdt: ptr to device tree
56 * @path: path of node
57 * @prop: property name
58 * @dflt: default value if the property isn't found
59 *
60 * Convenience function to find a node and return it's property or a
61 * default value if it doesn't exist.
62 */
Gabe Blackcd37de72011-11-08 01:09:44 -080063u32 fdt_getprop_u32_default(const void *fdt, const char *path,
64 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050065{
Kim Phillips6542c072013-01-16 14:00:11 +000066 const fdt32_t *val;
Kumar Gala1ba00402008-10-23 00:05:47 -050067 int off;
68
69 off = fdt_path_offset(fdt, path);
70 if (off < 0)
71 return dflt;
72
73 val = fdt_getprop(fdt, off, prop, NULL);
74 if (val)
Gabe Black9df8b4c2011-11-08 01:05:32 -080075 return fdt32_to_cpu(*val);
Kumar Gala1ba00402008-10-23 00:05:47 -050076 else
77 return dflt;
78}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040079
Kumar Galabf771dc2007-10-24 10:21:57 -050080/**
81 * fdt_find_and_setprop: Find a node and set it's property
82 *
83 * @fdt: ptr to device tree
84 * @node: path of node
85 * @prop: property name
86 * @val: ptr to new value
87 * @len: length of new property value
88 * @create: flag to create the property if it doesn't exist
89 *
90 * Convenience function to directly set a property given the path to the node.
91 */
92int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
93 const void *val, int len, int create)
94{
Kumar Galac8ab7052007-10-24 11:04:22 -050095 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050096
97 if (nodeoff < 0)
98 return nodeoff;
99
Kim Phillips6542c072013-01-16 14:00:11 +0000100 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500101 return 0; /* create flag not set; so exit quietly */
102
103 return fdt_setprop(fdt, nodeoff, prop, val, len);
104}
105
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600106#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400107
Marek Vasut52039da2012-09-14 23:45:51 +0200108#ifdef CONFIG_CONS_INDEX
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400109static void fdt_fill_multisername(char *sername, size_t maxlen)
110{
111 const char *outname = stdio_devices[stdout]->name;
112
113 if (strcmp(outname, "serial") > 0)
114 strncpy(sername, outname, maxlen);
115
116 /* eserial? */
117 if (strcmp(outname + 1, "serial") > 0)
118 strncpy(sername, outname + 1, maxlen);
119}
Marek Vasut52039da2012-09-14 23:45:51 +0200120#endif
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400121
Detlev Zundel46e192a2008-06-20 22:24:05 +0200122static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600123{
124 int err = 0;
125#ifdef CONFIG_CONS_INDEX
126 int node;
127 char sername[9] = { 0 };
128 const char *path;
129
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400130 fdt_fill_multisername(sername, sizeof(sername) - 1);
131 if (!sername[0])
132 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600133
134 err = node = fdt_path_offset(fdt, "/aliases");
135 if (node >= 0) {
136 int len;
137 path = fdt_getprop(fdt, node, sername, &len);
138 if (path) {
139 char *p = malloc(len);
140 err = -FDT_ERR_NOSPACE;
141 if (p) {
142 memcpy(p, path, len);
Detlev Zundel46e192a2008-06-20 22:24:05 +0200143 err = fdt_setprop(fdt, chosenoff,
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600144 "linux,stdout-path", p, len);
145 free(p);
146 }
147 } else {
148 err = len;
149 }
150 }
151#endif
152 if (err < 0)
153 printf("WARNING: could not set linux,stdout-path %s.\n",
154 fdt_strerror(err));
155
156 return err;
157}
158#endif
159
Kumar Gala99ebc052008-08-15 08:24:43 -0500160int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400161{
David Fengd9850132013-12-14 11:47:29 +0800162 int nodeoffset, addr_cell_len;
Kumar Gala99ebc052008-08-15 08:24:43 -0500163 int err, j, total;
David Fengd9850132013-12-14 11:47:29 +0800164 fdt64_t tmp;
Gerald Van Baren569c0352007-12-29 22:45:27 -0500165 const char *path;
Kumar Gala99ebc052008-08-15 08:24:43 -0500166 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400167
Kumar Gala99ebc052008-08-15 08:24:43 -0500168 /* Find the "chosen" node. */
169 nodeoffset = fdt_path_offset (fdt, "/chosen");
170
171 /* If there is no "chosen" node in the blob return */
172 if (nodeoffset < 0) {
173 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
174 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400175 }
176
Kumar Gala99ebc052008-08-15 08:24:43 -0500177 /* just return if initrd_start/end aren't valid */
178 if ((initrd_start == 0) || (initrd_end == 0))
179 return 0;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400180
Kumar Gala99ebc052008-08-15 08:24:43 -0500181 total = fdt_num_mem_rsv(fdt);
182
183 /*
184 * Look for an existing entry and update it. If we don't find
185 * the entry, we will j be the next available slot.
186 */
187 for (j = 0; j < total; j++) {
188 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
189 if (addr == initrd_start) {
190 fdt_del_mem_rsv(fdt, j);
191 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400192 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500193 }
194
Grant Likelyc379a562011-03-28 09:58:55 +0000195 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500196 if (err < 0) {
197 printf("fdt_initrd: %s\n", fdt_strerror(err));
198 return err;
199 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500200
David Fengd9850132013-12-14 11:47:29 +0800201 addr_cell_len = get_cells_len(fdt, "#address-cells");
202
Kumar Gala99ebc052008-08-15 08:24:43 -0500203 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
204 if ((path == NULL) || force) {
David Fengd9850132013-12-14 11:47:29 +0800205 write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
Kumar Gala99ebc052008-08-15 08:24:43 -0500206 err = fdt_setprop(fdt, nodeoffset,
Tom Rini2cc84cc2014-01-20 17:45:33 -0500207 "linux,initrd-start", &tmp, addr_cell_len);
Kumar Gala99ebc052008-08-15 08:24:43 -0500208 if (err < 0) {
209 printf("WARNING: "
210 "could not set linux,initrd-start %s.\n",
211 fdt_strerror(err));
212 return err;
213 }
David Fengd9850132013-12-14 11:47:29 +0800214 write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
Kumar Gala99ebc052008-08-15 08:24:43 -0500215 err = fdt_setprop(fdt, nodeoffset,
Tom Rini2cc84cc2014-01-20 17:45:33 -0500216 "linux,initrd-end", &tmp, addr_cell_len);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400217 if (err < 0) {
Kumar Gala99ebc052008-08-15 08:24:43 -0500218 printf("WARNING: could not set linux,initrd-end %s.\n",
219 fdt_strerror(err));
220
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400221 return err;
222 }
223 }
224
Kumar Gala99ebc052008-08-15 08:24:43 -0500225 return 0;
226}
227
Heiko Schocherd29abae2008-09-11 08:11:23 +0200228int fdt_chosen(void *fdt, int force)
Kumar Gala99ebc052008-08-15 08:24:43 -0500229{
230 int nodeoffset;
231 int err;
232 char *str; /* used to set string properties */
233 const char *path;
234
235 err = fdt_check_header(fdt);
236 if (err < 0) {
237 printf("fdt_chosen: %s\n", fdt_strerror(err));
238 return err;
239 }
240
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400241 /*
242 * Find the "chosen" node.
243 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500244 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400245
246 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500247 * If there is no "chosen" node in the blob, create it.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400248 */
249 if (nodeoffset < 0) {
250 /*
251 * Create a new node "/chosen" (offset 0 is root level)
252 */
253 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
254 if (nodeoffset < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400255 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400256 fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400257 return nodeoffset;
258 }
259 }
260
261 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500262 * Create /chosen properites that don't exist in the fdt.
263 * If the property exists, update it only if the "force" parameter
264 * is true.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400265 */
266 str = getenv("bootargs");
267 if (str != NULL) {
Gerald Van Baren569c0352007-12-29 22:45:27 -0500268 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
269 if ((path == NULL) || force) {
270 err = fdt_setprop(fdt, nodeoffset,
271 "bootargs", str, strlen(str)+1);
272 if (err < 0)
273 printf("WARNING: could not set bootargs %s.\n",
274 fdt_strerror(err));
275 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400276 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600277
278#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Baren569c0352007-12-29 22:45:27 -0500279 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
280 if ((path == NULL) || force)
281 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600282#endif
283
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400284#ifdef OF_STDOUT_PATH
Gerald Van Baren569c0352007-12-29 22:45:27 -0500285 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
286 if ((path == NULL) || force) {
287 err = fdt_setprop(fdt, nodeoffset,
288 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
289 if (err < 0)
290 printf("WARNING: could not set linux,stdout-path %s.\n",
291 fdt_strerror(err));
292 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400293#endif
294
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400295 return err;
296}
297
Kumar Gala7e64cf82007-11-03 19:46:28 -0500298void do_fixup_by_path(void *fdt, const char *path, const char *prop,
299 const void *val, int len, int create)
300{
301#if defined(DEBUG)
302 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600303 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500304 for (i = 0; i < len; i++)
305 debug(" %.2x", *(u8*)(val+i));
306 debug("\n");
307#endif
308 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
309 if (rc)
310 printf("Unable to update property %s:%s, err=%s\n",
311 path, prop, fdt_strerror(rc));
312}
313
314void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
315 u32 val, int create)
316{
Kim Phillips6542c072013-01-16 14:00:11 +0000317 fdt32_t tmp = cpu_to_fdt32(val);
318 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500319}
320
Kumar Gala7590a192007-11-21 13:30:15 -0600321void do_fixup_by_prop(void *fdt,
322 const char *pname, const void *pval, int plen,
323 const char *prop, const void *val, int len,
324 int create)
325{
326 int off;
327#if defined(DEBUG)
328 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600329 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600330 for (i = 0; i < len; i++)
331 debug(" %.2x", *(u8*)(val+i));
332 debug("\n");
333#endif
334 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
335 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000336 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600337 fdt_setprop(fdt, off, prop, val, len);
338 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
339 }
340}
341
342void do_fixup_by_prop_u32(void *fdt,
343 const char *pname, const void *pval, int plen,
344 const char *prop, u32 val, int create)
345{
Kim Phillips6542c072013-01-16 14:00:11 +0000346 fdt32_t tmp = cpu_to_fdt32(val);
347 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600348}
349
350void do_fixup_by_compat(void *fdt, const char *compat,
351 const char *prop, const void *val, int len, int create)
352{
353 int off = -1;
354#if defined(DEBUG)
355 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600356 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600357 for (i = 0; i < len; i++)
358 debug(" %.2x", *(u8*)(val+i));
359 debug("\n");
360#endif
361 off = fdt_node_offset_by_compatible(fdt, -1, compat);
362 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000363 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600364 fdt_setprop(fdt, off, prop, val, len);
365 off = fdt_node_offset_by_compatible(fdt, off, compat);
366 }
367}
368
369void do_fixup_by_compat_u32(void *fdt, const char *compat,
370 const char *prop, u32 val, int create)
371{
Kim Phillips6542c072013-01-16 14:00:11 +0000372 fdt32_t tmp = cpu_to_fdt32(val);
373 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600374}
375
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000376#ifdef CONFIG_NR_DRAM_BANKS
377#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
378#else
Kim Phillips6542c072013-01-16 14:00:11 +0000379#define MEMORY_BANKS_MAX 4
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000380#endif
John Rigbyb4ae9422010-10-13 13:57:33 -0600381int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
382{
383 int err, nodeoffset;
384 int addr_cell_len, size_cell_len, len;
Kim Phillips6542c072013-01-16 14:00:11 +0000385 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbyb4ae9422010-10-13 13:57:33 -0600386 int bank;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600387
Kim Phillips6542c072013-01-16 14:00:11 +0000388 if (banks > MEMORY_BANKS_MAX) {
389 printf("%s: num banks %d exceeds hardcoded limit %d."
390 " Recompile with higher MEMORY_BANKS_MAX?\n",
391 __FUNCTION__, banks, MEMORY_BANKS_MAX);
392 return -1;
393 }
394
Kumar Gala0db72ed2007-11-26 14:57:45 -0600395 err = fdt_check_header(blob);
396 if (err < 0) {
397 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
398 return err;
399 }
400
401 /* update, or add and update /memory node */
402 nodeoffset = fdt_path_offset(blob, "/memory");
403 if (nodeoffset < 0) {
404 nodeoffset = fdt_add_subnode(blob, 0, "memory");
Miao Yan0904eb22013-11-28 17:51:39 +0800405 if (nodeoffset < 0) {
Kumar Gala0db72ed2007-11-26 14:57:45 -0600406 printf("WARNING: could not create /memory: %s.\n",
407 fdt_strerror(nodeoffset));
Miao Yan0904eb22013-11-28 17:51:39 +0800408 return nodeoffset;
409 }
Kumar Gala0db72ed2007-11-26 14:57:45 -0600410 }
411 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
412 sizeof("memory"));
413 if (err < 0) {
414 printf("WARNING: could not set %s %s.\n", "device_type",
415 fdt_strerror(err));
416 return err;
417 }
418
John Rigbyb4ae9422010-10-13 13:57:33 -0600419 addr_cell_len = get_cells_len(blob, "#address-cells");
420 size_cell_len = get_cells_len(blob, "#size-cells");
421
422 for (bank = 0, len = 0; bank < banks; bank++) {
423 write_cell(tmp + len, start[bank], addr_cell_len);
424 len += addr_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600425
John Rigbyb4ae9422010-10-13 13:57:33 -0600426 write_cell(tmp + len, size[bank], size_cell_len);
427 len += size_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600428 }
429
430 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
431 if (err < 0) {
432 printf("WARNING: could not set %s %s.\n",
433 "reg", fdt_strerror(err));
434 return err;
435 }
436 return 0;
437}
438
John Rigbyb4ae9422010-10-13 13:57:33 -0600439int fdt_fixup_memory(void *blob, u64 start, u64 size)
440{
441 return fdt_fixup_memory_banks(blob, &start, &size, 1);
442}
443
Kumar Galafabda922008-08-19 15:41:18 -0500444void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600445{
Kumar Galafabda922008-08-19 15:41:18 -0500446 int node, i, j;
447 char enet[16], *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000448 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600449 const char *path;
Kumar Galafabda922008-08-19 15:41:18 -0500450 unsigned char mac_addr[6];
Kumar Gala22699102007-11-21 11:11:03 -0600451
452 node = fdt_path_offset(fdt, "/aliases");
Kumar Galafabda922008-08-19 15:41:18 -0500453 if (node < 0)
454 return;
455
456 i = 0;
Stephen Warren7c15c772013-05-27 18:01:19 +0000457 strcpy(mac, "ethaddr");
Kumar Galafabda922008-08-19 15:41:18 -0500458 while ((tmp = getenv(mac)) != NULL) {
459 sprintf(enet, "ethernet%d", i);
460 path = fdt_getprop(fdt, node, enet, NULL);
461 if (!path) {
462 debug("No alias for %s\n", enet);
463 sprintf(mac, "eth%daddr", ++i);
464 continue;
Kumar Gala22699102007-11-21 11:11:03 -0600465 }
Kumar Galafabda922008-08-19 15:41:18 -0500466
467 for (j = 0; j < 6; j++) {
468 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
469 if (tmp)
470 tmp = (*end) ? end+1 : end;
Kumar Gala22699102007-11-21 11:11:03 -0600471 }
Kumar Galafabda922008-08-19 15:41:18 -0500472
473 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
474 do_fixup_by_path(fdt, path, "local-mac-address",
475 &mac_addr, 6, 1);
476
477 sprintf(mac, "eth%daddr", ++i);
Kumar Gala22699102007-11-21 11:11:03 -0600478 }
479}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300480
Kumar Galaf2f58c52008-08-15 08:24:42 -0500481/* Resize the fdt to its actual size + a bit of padding */
482int fdt_resize(void *blob)
483{
484 int i;
485 uint64_t addr, size;
486 int total, ret;
487 uint actualsize;
488
489 if (!blob)
490 return 0;
491
492 total = fdt_num_mem_rsv(blob);
493 for (i = 0; i < total; i++) {
494 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000495 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500496 fdt_del_mem_rsv(blob, i);
497 break;
498 }
499 }
500
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100501 /*
502 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200503 * plus the size needed for 5 fdt_add_mem_rsv, one
504 * for the fdt itself and 4 for a possible initrd
505 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100506 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500507 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200508 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500509
510 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000511 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
512 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500513
514 /* Change the fdt header to reflect the correct size */
515 fdt_set_totalsize(blob, actualsize);
516
517 /* Add the new reservation */
Simon Glassfc953242011-09-17 06:48:58 +0000518 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500519 if (ret < 0)
520 return ret;
521
522 return actualsize;
523}
Kumar Galae0475cd2008-10-22 23:33:56 -0500524
525#ifdef CONFIG_PCI
Kumar Galabaf41892009-08-05 09:03:54 -0500526#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500527
528#define FDT_PCI_PREFETCH (0x40000000)
529#define FDT_PCI_MEM32 (0x02000000)
530#define FDT_PCI_IO (0x01000000)
531#define FDT_PCI_MEM64 (0x03000000)
532
533int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
534
535 int addrcell, sizecell, len, r;
536 u32 *dma_range;
537 /* sized based on pci addr cells, size-cells, & address-cells */
538 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
539
540 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
541 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
542
543 dma_range = &dma_ranges[0];
544 for (r = 0; r < hose->region_count; r++) {
545 u64 bus_start, phys_start, size;
546
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600547 /* skip if !PCI_REGION_SYS_MEMORY */
548 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500549 continue;
550
551 bus_start = (u64)hose->regions[r].bus_start;
552 phys_start = (u64)hose->regions[r].phys_start;
553 size = (u64)hose->regions[r].size;
554
555 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500556 if (size >= 0x100000000ull)
Kumar Galae0475cd2008-10-22 23:33:56 -0500557 dma_range[0] |= FDT_PCI_MEM64;
558 else
559 dma_range[0] |= FDT_PCI_MEM32;
560 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
561 dma_range[0] |= FDT_PCI_PREFETCH;
562#ifdef CONFIG_SYS_PCI_64BIT
563 dma_range[1] = bus_start >> 32;
564#else
565 dma_range[1] = 0;
566#endif
567 dma_range[2] = bus_start & 0xffffffff;
568
569 if (addrcell == 2) {
570 dma_range[3] = phys_start >> 32;
571 dma_range[4] = phys_start & 0xffffffff;
572 } else {
573 dma_range[3] = phys_start & 0xffffffff;
574 }
575
576 if (sizecell == 2) {
577 dma_range[3 + addrcell + 0] = size >> 32;
578 dma_range[3 + addrcell + 1] = size & 0xffffffff;
579 } else {
580 dma_range[3 + addrcell + 0] = size & 0xffffffff;
581 }
582
583 dma_range += (3 + addrcell + sizecell);
584 }
585
586 len = dma_range - &dma_ranges[0];
587 if (len)
588 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
589
590 return 0;
591}
592#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200593
594#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
595/*
Stefan Roese412a71a2010-09-16 14:01:53 +0200596 * Provide a weak default function to return the flash bank size.
597 * There might be multiple non-identical flash chips connected to one
598 * chip-select, so we need to pass an index as well.
599 */
600u32 __flash_get_bank_size(int cs, int idx)
601{
602 extern flash_info_t flash_info[];
603
604 /*
605 * As default, a simple 1:1 mapping is provided. Boards with
606 * a different mapping need to supply a board specific mapping
607 * routine.
608 */
609 return flash_info[cs].size;
610}
611u32 flash_get_bank_size(int cs, int idx)
612 __attribute__((weak, alias("__flash_get_bank_size")));
613
614/*
Stefan Roesea0f10c92009-10-21 11:59:52 +0200615 * This function can be used to update the size in the "reg" property
Stefan Roese412a71a2010-09-16 14:01:53 +0200616 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roesea0f10c92009-10-21 11:59:52 +0200617 * non-fixed NOR FLASH sizes.
618 */
Stefan Roese412a71a2010-09-16 14:01:53 +0200619int fdt_fixup_nor_flash_size(void *blob)
Stefan Roesea0f10c92009-10-21 11:59:52 +0200620{
621 char compat[][16] = { "cfi-flash", "jedec-flash" };
622 int off;
623 int len;
624 struct fdt_property *prop;
Stefan Roese442cd1a2010-09-24 13:51:50 +0200625 u32 *reg, *reg2;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200626 int i;
627
628 for (i = 0; i < 2; i++) {
629 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
630 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200631 int idx;
632
Stefan Roesea0f10c92009-10-21 11:59:52 +0200633 /*
Stefan Roese412a71a2010-09-16 14:01:53 +0200634 * Found one compatible node, so fixup the size
635 * int its reg properties
Stefan Roesea0f10c92009-10-21 11:59:52 +0200636 */
637 prop = fdt_get_property_w(blob, off, "reg", &len);
638 if (prop) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200639 int tuple_size = 3 * sizeof(reg);
640
641 /*
642 * There might be multiple reg-tuples,
643 * so loop through them all
644 */
Stefan Roese442cd1a2010-09-24 13:51:50 +0200645 reg = reg2 = (u32 *)&prop->data[0];
646 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200647 /*
648 * Update size in reg property
649 */
650 reg[2] = flash_get_bank_size(reg[0],
651 idx);
Stefan Roese442cd1a2010-09-24 13:51:50 +0200652
653 /*
654 * Point to next reg tuple
655 */
656 reg += 3;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200657 }
Stefan Roese442cd1a2010-09-24 13:51:50 +0200658
659 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roesea0f10c92009-10-21 11:59:52 +0200660 }
661
662 /* Move to next compatible node */
663 off = fdt_node_offset_by_compatible(blob, off,
664 compat[i]);
665 }
666 }
667
Stefan Roese412a71a2010-09-16 14:01:53 +0200668 return 0;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200669}
670#endif
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100671
Matthew McClintockacda3a42010-10-13 13:39:26 +0200672int fdt_increase_size(void *fdt, int add_len)
673{
674 int newlen;
675
676 newlen = fdt_totalsize(fdt) + add_len;
677
678 /* Open in place with a new len */
679 return fdt_open_into(fdt, fdt, newlen);
680}
681
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100682#ifdef CONFIG_FDT_FIXUP_PARTITIONS
683#include <jffs2/load_kernel.h>
684#include <mtd_node.h>
685
686struct reg_cell {
687 unsigned int r0;
688 unsigned int r1;
689};
690
691int fdt_del_subnodes(const void *blob, int parent_offset)
692{
693 int off, ndepth;
694 int ret;
695
696 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
697 (off >= 0) && (ndepth > 0);
698 off = fdt_next_node(blob, off, &ndepth)) {
699 if (ndepth == 1) {
700 debug("delete %s: offset: %x\n",
701 fdt_get_name(blob, off, 0), off);
702 ret = fdt_del_node((void *)blob, off);
703 if (ret < 0) {
704 printf("Can't delete node: %s\n",
705 fdt_strerror(ret));
706 return ret;
707 } else {
708 ndepth = 0;
709 off = parent_offset;
710 }
711 }
712 }
713 return 0;
714}
715
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100716int fdt_del_partitions(void *blob, int parent_offset)
717{
718 const void *prop;
719 int ndepth = 0;
720 int off;
721 int ret;
722
723 off = fdt_next_node(blob, parent_offset, &ndepth);
724 if (off > 0 && ndepth == 1) {
725 prop = fdt_getprop(blob, off, "label", NULL);
726 if (prop == NULL) {
727 /*
728 * Could not find label property, nand {}; node?
729 * Check subnode, delete partitions there if any.
730 */
731 return fdt_del_partitions(blob, off);
732 } else {
733 ret = fdt_del_subnodes(blob, parent_offset);
734 if (ret < 0) {
735 printf("Can't remove subnodes: %s\n",
736 fdt_strerror(ret));
737 return ret;
738 }
739 }
740 }
741 return 0;
742}
743
744int fdt_node_set_part_info(void *blob, int parent_offset,
745 struct mtd_device *dev)
746{
747 struct list_head *pentry;
748 struct part_info *part;
749 struct reg_cell cell;
750 int off, ndepth = 0;
751 int part_num, ret;
752 char buf[64];
753
754 ret = fdt_del_partitions(blob, parent_offset);
755 if (ret < 0)
756 return ret;
757
758 /*
759 * Check if it is nand {}; subnode, adjust
760 * the offset in this case
761 */
762 off = fdt_next_node(blob, parent_offset, &ndepth);
763 if (off > 0 && ndepth == 1)
764 parent_offset = off;
765
766 part_num = 0;
767 list_for_each_prev(pentry, &dev->parts) {
768 int newoff;
769
770 part = list_entry(pentry, struct part_info, link);
771
Scott Woodefe7f302013-10-15 17:41:27 -0500772 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100773 part_num, part->name, part->size,
774 part->offset, part->mask_flags);
775
Scott Woodefe7f302013-10-15 17:41:27 -0500776 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100777add_sub:
778 ret = fdt_add_subnode(blob, parent_offset, buf);
779 if (ret == -FDT_ERR_NOSPACE) {
780 ret = fdt_increase_size(blob, 512);
781 if (!ret)
782 goto add_sub;
783 else
784 goto err_size;
785 } else if (ret < 0) {
786 printf("Can't add partition node: %s\n",
787 fdt_strerror(ret));
788 return ret;
789 }
790 newoff = ret;
791
792 /* Check MTD_WRITEABLE_CMD flag */
793 if (part->mask_flags & 1) {
794add_ro:
795 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
796 if (ret == -FDT_ERR_NOSPACE) {
797 ret = fdt_increase_size(blob, 512);
798 if (!ret)
799 goto add_ro;
800 else
801 goto err_size;
802 } else if (ret < 0)
803 goto err_prop;
804 }
805
806 cell.r0 = cpu_to_fdt32(part->offset);
807 cell.r1 = cpu_to_fdt32(part->size);
808add_reg:
809 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
810 if (ret == -FDT_ERR_NOSPACE) {
811 ret = fdt_increase_size(blob, 512);
812 if (!ret)
813 goto add_reg;
814 else
815 goto err_size;
816 } else if (ret < 0)
817 goto err_prop;
818
819add_label:
820 ret = fdt_setprop_string(blob, newoff, "label", part->name);
821 if (ret == -FDT_ERR_NOSPACE) {
822 ret = fdt_increase_size(blob, 512);
823 if (!ret)
824 goto add_label;
825 else
826 goto err_size;
827 } else if (ret < 0)
828 goto err_prop;
829
830 part_num++;
831 }
832 return 0;
833err_size:
834 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
835 return ret;
836err_prop:
837 printf("Can't add property: %s\n", fdt_strerror(ret));
838 return ret;
839}
840
841/*
842 * Update partitions in nor/nand nodes using info from
843 * mtdparts environment variable. The nodes to update are
844 * specified by node_info structure which contains mtd device
845 * type and compatible string: E. g. the board code in
846 * ft_board_setup() could use:
847 *
848 * struct node_info nodes[] = {
849 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
850 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
851 * };
852 *
853 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
854 */
855void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
856{
857 struct node_info *ni = node_info;
858 struct mtd_device *dev;
859 char *parts;
860 int i, idx;
861 int noff;
862
863 parts = getenv("mtdparts");
864 if (!parts)
865 return;
866
867 if (mtdparts_init() != 0)
868 return;
869
870 for (i = 0; i < node_info_size; i++) {
871 idx = 0;
872 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
873 while (noff != -FDT_ERR_NOTFOUND) {
874 debug("%s: %s, mtd dev type %d\n",
875 fdt_get_name(blob, noff, 0),
876 ni[i].compat, ni[i].type);
877 dev = device_find(ni[i].type, idx++);
878 if (dev) {
879 if (fdt_node_set_part_info(blob, noff, dev))
880 return; /* return on error */
881 }
882
883 /* Jump to next flash node */
884 noff = fdt_node_offset_by_compatible(blob, noff,
885 ni[i].compat);
886 }
887 }
888}
889#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -0500890
891void fdt_del_node_and_alias(void *blob, const char *alias)
892{
893 int off = fdt_path_offset(blob, alias);
894
895 if (off < 0)
896 return;
897
898 fdt_del_node(blob, off);
899
900 off = fdt_path_offset(blob, "/aliases");
901 fdt_delprop(blob, off, alias);
902}
Kumar Galab87e7932010-06-16 14:27:38 -0500903
904/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips6542c072013-01-16 14:00:11 +0000905static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galab87e7932010-06-16 14:27:38 -0500906{
907 u64 r = 0;
908 while (size--)
Kim Phillips6542c072013-01-16 14:00:11 +0000909 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galab87e7932010-06-16 14:27:38 -0500910 return r;
911}
912
Kumar Galab87e7932010-06-16 14:27:38 -0500913#define PRu64 "%llx"
914
915/* Max address size we deal with */
916#define OF_MAX_ADDR_CELLS 4
917#define OF_BAD_ADDR ((u64)-1)
918#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
919 (ns) > 0)
920
921/* Debug utility */
922#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +0000923static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -0500924{
925 printf("%s", s);
926 while(na--)
927 printf(" %08x", *(addr++));
928 printf("\n");
929}
930#else
Kim Phillips6542c072013-01-16 14:00:11 +0000931static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -0500932#endif
933
934/* Callbacks for bus specific translators */
935struct of_bus {
936 const char *name;
937 const char *addresses;
Scott Wood99899712010-08-12 18:37:39 -0500938 void (*count_cells)(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500939 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +0000940 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500941 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +0000942 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -0500943};
944
945/* Default translator (generic bus) */
Scott Wood99899712010-08-12 18:37:39 -0500946static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500947 int *addrc, int *sizec)
948{
Kim Phillips6542c072013-01-16 14:00:11 +0000949 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -0500950
951 if (addrc) {
952 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
953 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000954 *addrc = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500955 else
956 *addrc = 2;
957 }
958
959 if (sizec) {
960 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
961 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000962 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500963 else
964 *sizec = 1;
965 }
Kumar Galab87e7932010-06-16 14:27:38 -0500966}
967
Kim Phillips6542c072013-01-16 14:00:11 +0000968static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500969 int na, int ns, int pna)
970{
971 u64 cp, s, da;
972
973 cp = of_read_number(range, na);
974 s = of_read_number(range + na + pna, ns);
975 da = of_read_number(addr, na);
976
977 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
978 cp, s, da);
979
980 if (da < cp || da >= (cp + s))
981 return OF_BAD_ADDR;
982 return da - cp;
983}
984
Kim Phillips6542c072013-01-16 14:00:11 +0000985static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -0500986{
987 u64 a = of_read_number(addr, na);
988 memset(addr, 0, na * 4);
989 a += offset;
990 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +0000991 addr[na - 2] = cpu_to_fdt32(a >> 32);
992 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -0500993
994 return 0;
995}
996
997/* Array of bus specific translators */
998static struct of_bus of_busses[] = {
999 /* Default */
1000 {
1001 .name = "default",
1002 .addresses = "reg",
1003 .count_cells = of_bus_default_count_cells,
1004 .map = of_bus_default_map,
1005 .translate = of_bus_default_translate,
1006 },
1007};
1008
1009static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001010 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001011 int na, int ns, int pna, const char *rprop)
1012{
Kim Phillips6542c072013-01-16 14:00:11 +00001013 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001014 int rlen;
1015 int rone;
1016 u64 offset = OF_BAD_ADDR;
1017
1018 /* Normally, an absence of a "ranges" property means we are
1019 * crossing a non-translatable boundary, and thus the addresses
1020 * below the current not cannot be converted to CPU physical ones.
1021 * Unfortunately, while this is very clear in the spec, it's not
1022 * what Apple understood, and they do have things like /uni-n or
1023 * /ht nodes with no "ranges" property and a lot of perfectly
1024 * useable mapped devices below them. Thus we treat the absence of
1025 * "ranges" as equivalent to an empty "ranges" property which means
1026 * a 1:1 translation at that level. It's up to the caller not to try
1027 * to translate addresses that aren't supposed to be translated in
1028 * the first place. --BenH.
1029 */
Kim Phillips6542c072013-01-16 14:00:11 +00001030 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001031 if (ranges == NULL || rlen == 0) {
1032 offset = of_read_number(addr, na);
1033 memset(addr, 0, pna * 4);
1034 debug("OF: no ranges, 1:1 translation\n");
1035 goto finish;
1036 }
1037
1038 debug("OF: walking ranges...\n");
1039
1040 /* Now walk through the ranges */
1041 rlen /= 4;
1042 rone = na + pna + ns;
1043 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1044 offset = bus->map(addr, ranges, na, ns, pna);
1045 if (offset != OF_BAD_ADDR)
1046 break;
1047 }
1048 if (offset == OF_BAD_ADDR) {
1049 debug("OF: not found !\n");
1050 return 1;
1051 }
1052 memcpy(addr, ranges + na, 4 * pna);
1053
1054 finish:
1055 of_dump_addr("OF: parent translation for:", addr, pna);
1056 debug("OF: with offset: "PRu64"\n", offset);
1057
1058 /* Translate it into parent bus space */
1059 return pbus->translate(addr, offset, pna);
1060}
1061
1062/*
1063 * Translate an address from the device-tree into a CPU physical address,
1064 * this walks up the tree and applies the various bus mappings on the
1065 * way.
1066 *
1067 * Note: We consider that crossing any level with #size-cells == 0 to mean
1068 * that translation is impossible (that is we are not dealing with a value
1069 * that can be mapped to a cpu physical address). This is not really specified
1070 * that way, but this is traditionally the way IBM at least do things
1071 */
Kim Phillips6542c072013-01-16 14:00:11 +00001072static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1073 const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001074{
1075 int parent;
1076 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001077 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001078 int na, ns, pna, pns;
1079 u64 result = OF_BAD_ADDR;
1080
1081 debug("OF: ** translation for device %s **\n",
1082 fdt_get_name(blob, node_offset, NULL));
1083
1084 /* Get parent & match bus type */
1085 parent = fdt_parent_offset(blob, node_offset);
1086 if (parent < 0)
1087 goto bail;
1088 bus = &of_busses[0];
1089
1090 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001091 bus->count_cells(blob, parent, &na, &ns);
Kumar Galab87e7932010-06-16 14:27:38 -05001092 if (!OF_CHECK_COUNTS(na, ns)) {
1093 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1094 fdt_get_name(blob, node_offset, NULL));
1095 goto bail;
1096 }
1097 memcpy(addr, in_addr, na * 4);
1098
1099 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1100 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1101 of_dump_addr("OF: translating address:", addr, na);
1102
1103 /* Translate */
1104 for (;;) {
1105 /* Switch to parent bus */
1106 node_offset = parent;
1107 parent = fdt_parent_offset(blob, node_offset);
1108
1109 /* If root, we have finished */
1110 if (parent < 0) {
1111 debug("OF: reached root node\n");
1112 result = of_read_number(addr, na);
1113 break;
1114 }
1115
1116 /* Get new parent bus and counts */
1117 pbus = &of_busses[0];
Scott Wood99899712010-08-12 18:37:39 -05001118 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galab87e7932010-06-16 14:27:38 -05001119 if (!OF_CHECK_COUNTS(pna, pns)) {
1120 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1121 fdt_get_name(blob, node_offset, NULL));
1122 break;
1123 }
1124
1125 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1126 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1127
1128 /* Apply bus translation */
1129 if (of_translate_one(blob, node_offset, bus, pbus,
1130 addr, na, ns, pna, rprop))
1131 break;
1132
1133 /* Complete the move up one level */
1134 na = pna;
1135 ns = pns;
1136 bus = pbus;
1137
1138 of_dump_addr("OF: one level translation:", addr, na);
1139 }
1140 bail:
1141
1142 return result;
1143}
1144
Kim Phillips6542c072013-01-16 14:00:11 +00001145u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001146{
1147 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1148}
Kumar Gala800d1d12010-07-04 12:48:21 -05001149
1150/**
1151 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1152 * who's reg property matches a physical cpu address
1153 *
1154 * @blob: ptr to device tree
1155 * @compat: compatiable string to match
1156 * @compat_off: property name
1157 *
1158 */
1159int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1160 phys_addr_t compat_off)
1161{
1162 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1163 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +00001164 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala800d1d12010-07-04 12:48:21 -05001165 if (reg) {
1166 if (compat_off == fdt_translate_address(blob, off, reg))
1167 return off;
1168 }
1169 off = fdt_node_offset_by_compatible(blob, off, compat);
1170 }
1171
1172 return -FDT_ERR_NOTFOUND;
1173}
1174
Kumar Galab7afb112010-07-09 16:18:58 -05001175/**
1176 * fdt_alloc_phandle: Return next free phandle value
1177 *
1178 * @blob: ptr to device tree
1179 */
1180int fdt_alloc_phandle(void *blob)
1181{
Timur Tabic6d83032011-09-20 18:24:35 -05001182 int offset, phandle = 0;
Kumar Galab7afb112010-07-09 16:18:58 -05001183
1184 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1185 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabic6d83032011-09-20 18:24:35 -05001186 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab7afb112010-07-09 16:18:58 -05001187 }
Kumar Gala800d1d12010-07-04 12:48:21 -05001188
Kumar Galab7afb112010-07-09 16:18:58 -05001189 return phandle + 1;
1190}
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001191
Gerald Van Barenaf737882011-07-14 21:40:10 -04001192/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001193 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001194 *
1195 * @fdt: ptr to device tree
1196 * @nodeoffset: node to update
1197 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001198 */
1199int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001200{
1201 int ret;
1202
1203#ifdef DEBUG
1204 int off = fdt_node_offset_by_phandle(fdt, phandle);
1205
1206 if ((off >= 0) && (off != nodeoffset)) {
1207 char buf[64];
1208
1209 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1210 printf("Trying to update node %s with phandle %u ",
1211 buf, phandle);
1212
1213 fdt_get_path(fdt, off, buf, sizeof(buf));
1214 printf("that already exists in node %s.\n", buf);
1215 return -FDT_ERR_BADPHANDLE;
1216 }
1217#endif
1218
1219 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1220 if (ret < 0)
1221 return ret;
1222
1223 /*
1224 * For now, also set the deprecated "linux,phandle" property, so that we
1225 * don't break older kernels.
1226 */
1227 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1228
1229 return ret;
1230}
1231
Kumar Galad5fc2582011-08-01 00:25:20 -05001232/*
1233 * fdt_create_phandle: Create a phandle property for the given node
1234 *
1235 * @fdt: ptr to device tree
1236 * @nodeoffset: node to update
1237 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001238unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001239{
1240 /* see if there is a phandle already */
1241 int phandle = fdt_get_phandle(fdt, nodeoffset);
1242
1243 /* if we got 0, means no phandle so create one */
1244 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001245 int ret;
1246
Kumar Galad5fc2582011-08-01 00:25:20 -05001247 phandle = fdt_alloc_phandle(fdt);
Timur Tabi0f46c802011-09-20 18:24:34 -05001248 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1249 if (ret < 0) {
1250 printf("Can't set phandle %u: %s\n", phandle,
1251 fdt_strerror(ret));
1252 return 0;
1253 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001254 }
1255
1256 return phandle;
1257}
1258
Shengzhou Liua7be8022011-10-14 16:26:05 +08001259/*
1260 * fdt_set_node_status: Set status for the given node
1261 *
1262 * @fdt: ptr to device tree
1263 * @nodeoffset: node to update
1264 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1265 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1266 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1267 */
1268int fdt_set_node_status(void *fdt, int nodeoffset,
1269 enum fdt_status status, unsigned int error_code)
1270{
1271 char buf[16];
1272 int ret = 0;
1273
1274 if (nodeoffset < 0)
1275 return nodeoffset;
1276
1277 switch (status) {
1278 case FDT_STATUS_OKAY:
1279 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1280 break;
1281 case FDT_STATUS_DISABLED:
1282 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1283 break;
1284 case FDT_STATUS_FAIL:
1285 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1286 break;
1287 case FDT_STATUS_FAIL_ERROR_CODE:
1288 sprintf(buf, "fail-%d", error_code);
1289 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1290 break;
1291 default:
1292 printf("Invalid fdt status: %x\n", status);
1293 ret = -1;
1294 break;
1295 }
1296
1297 return ret;
1298}
1299
1300/*
1301 * fdt_set_status_by_alias: Set status for the given node given an alias
1302 *
1303 * @fdt: ptr to device tree
1304 * @alias: alias of node to update
1305 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1306 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1307 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1308 */
1309int fdt_set_status_by_alias(void *fdt, const char* alias,
1310 enum fdt_status status, unsigned int error_code)
1311{
1312 int offset = fdt_path_offset(fdt, alias);
1313
1314 return fdt_set_node_status(fdt, offset, status, error_code);
1315}
1316
Tom Wai-Hong Tamdb4a88d2012-12-05 14:46:41 +00001317#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001318int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1319{
1320 int noff;
1321 int ret;
1322
1323 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1324 if (noff != -FDT_ERR_NOTFOUND) {
1325 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1326add_edid:
1327 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1328 if (ret == -FDT_ERR_NOSPACE) {
1329 ret = fdt_increase_size(blob, 512);
1330 if (!ret)
1331 goto add_edid;
1332 else
1333 goto err_size;
1334 } else if (ret < 0) {
1335 printf("Can't add property: %s\n", fdt_strerror(ret));
1336 return ret;
1337 }
1338 }
1339 return 0;
1340err_size:
1341 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1342 return ret;
1343}
1344#endif
Timur Tabi8ebaf202011-05-03 13:24:07 -05001345
1346/*
1347 * Verify the physical address of device tree node for a given alias
1348 *
1349 * This function locates the device tree node of a given alias, and then
1350 * verifies that the physical address of that device matches the given
1351 * parameter. It displays a message if there is a mismatch.
1352 *
1353 * Returns 1 on success, 0 on failure
1354 */
1355int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1356{
1357 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001358 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001359 int node, len;
1360 u64 dt_addr;
1361
1362 path = fdt_getprop(fdt, anode, alias, NULL);
1363 if (!path) {
1364 /* If there's no such alias, then it's not a failure */
1365 return 1;
1366 }
1367
1368 node = fdt_path_offset(fdt, path);
1369 if (node < 0) {
1370 printf("Warning: device tree alias '%s' points to invalid "
1371 "node %s.\n", alias, path);
1372 return 0;
1373 }
1374
1375 reg = fdt_getprop(fdt, node, "reg", &len);
1376 if (!reg) {
1377 printf("Warning: device tree node '%s' has no address.\n",
1378 path);
1379 return 0;
1380 }
1381
1382 dt_addr = fdt_translate_address(fdt, node, reg);
1383 if (addr != dt_addr) {
1384 printf("Warning: U-Boot configured device %s at address %llx,\n"
1385 " but the device tree has it address %llx.\n",
1386 alias, addr, dt_addr);
1387 return 0;
1388 }
1389
1390 return 1;
1391}
1392
1393/*
1394 * Returns the base address of an SOC or PCI node
1395 */
1396u64 fdt_get_base_address(void *fdt, int node)
1397{
1398 int size;
1399 u32 naddr;
Kim Phillips6542c072013-01-16 14:00:11 +00001400 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001401
1402 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1403 if (prop && size == 4)
Kim Phillips6542c072013-01-16 14:00:11 +00001404 naddr = be32_to_cpup(prop);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001405 else
1406 naddr = 2;
1407
1408 prop = fdt_getprop(fdt, node, "ranges", &size);
1409
1410 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1411}