blob: 812acb401c176f81976fceb05e637fec024dc91c [file] [log] [blame]
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04001/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
Shengzhou Liu2a523f52011-10-14 16:26:05 +08005 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Kumar Galaa0342c02010-06-16 14:27:38 -05006 *
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04007 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
Anton Vorontsov3e303f72009-10-15 17:47:04 +040027#include <stdio_dev.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040028#include <linux/ctype.h>
29#include <linux/types.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040030#include <asm/global_data.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040031#include <libfdt.h>
32#include <fdt_support.h>
Kumar Gala151c8b02007-11-26 17:06:15 -060033#include <exports.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040034
35/*
36 * Global data (for the gd->bd)
37 */
38DECLARE_GLOBAL_DATA_PTR;
39
Kumar Gala3bed2aa2008-10-23 00:05:47 -050040/**
41 * fdt_getprop_u32_default - Find a node and return it's property or a default
42 *
43 * @fdt: ptr to device tree
44 * @path: path of node
45 * @prop: property name
46 * @dflt: default value if the property isn't found
47 *
48 * Convenience function to find a node and return it's property or a
49 * default value if it doesn't exist.
50 */
Gabe Black07e12782011-11-08 01:09:44 -080051u32 fdt_getprop_u32_default(const void *fdt, const char *path,
52 const char *prop, const u32 dflt)
Kumar Gala3bed2aa2008-10-23 00:05:47 -050053{
Kim Phillips8aa5ec62013-01-16 14:00:11 +000054 const fdt32_t *val;
Kumar Gala3bed2aa2008-10-23 00:05:47 -050055 int off;
56
57 off = fdt_path_offset(fdt, path);
58 if (off < 0)
59 return dflt;
60
61 val = fdt_getprop(fdt, off, prop, NULL);
62 if (val)
Gabe Blackde166062011-11-08 01:05:32 -080063 return fdt32_to_cpu(*val);
Kumar Gala3bed2aa2008-10-23 00:05:47 -050064 else
65 return dflt;
66}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040067
Kumar Galaa3c29332007-10-24 10:21:57 -050068/**
69 * fdt_find_and_setprop: Find a node and set it's property
70 *
71 * @fdt: ptr to device tree
72 * @node: path of node
73 * @prop: property name
74 * @val: ptr to new value
75 * @len: length of new property value
76 * @create: flag to create the property if it doesn't exist
77 *
78 * Convenience function to directly set a property given the path to the node.
79 */
80int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
81 const void *val, int len, int create)
82{
Kumar Gala8d04f022007-10-24 11:04:22 -050083 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -050084
85 if (nodeoff < 0)
86 return nodeoff;
87
Kim Phillips8aa5ec62013-01-16 14:00:11 +000088 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galaa3c29332007-10-24 10:21:57 -050089 return 0; /* create flag not set; so exit quietly */
90
91 return fdt_setprop(fdt, nodeoff, prop, val, len);
92}
93
Kumar Gala151c8b02007-11-26 17:06:15 -060094#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov3e303f72009-10-15 17:47:04 +040095
Marek Vasut036036d2012-09-14 23:45:51 +020096#ifdef CONFIG_CONS_INDEX
Anton Vorontsov3e303f72009-10-15 17:47:04 +040097static void fdt_fill_multisername(char *sername, size_t maxlen)
98{
99 const char *outname = stdio_devices[stdout]->name;
100
101 if (strcmp(outname, "serial") > 0)
102 strncpy(sername, outname, maxlen);
103
104 /* eserial? */
105 if (strcmp(outname + 1, "serial") > 0)
106 strncpy(sername, outname + 1, maxlen);
107}
Marek Vasut036036d2012-09-14 23:45:51 +0200108#endif
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400109
Detlev Zundel40777812008-06-20 22:24:05 +0200110static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -0600111{
112 int err = 0;
113#ifdef CONFIG_CONS_INDEX
114 int node;
115 char sername[9] = { 0 };
116 const char *path;
117
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400118 fdt_fill_multisername(sername, sizeof(sername) - 1);
119 if (!sername[0])
120 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600121
122 err = node = fdt_path_offset(fdt, "/aliases");
123 if (node >= 0) {
124 int len;
125 path = fdt_getprop(fdt, node, sername, &len);
126 if (path) {
127 char *p = malloc(len);
128 err = -FDT_ERR_NOSPACE;
129 if (p) {
130 memcpy(p, path, len);
Detlev Zundel40777812008-06-20 22:24:05 +0200131 err = fdt_setprop(fdt, chosenoff,
Kumar Gala151c8b02007-11-26 17:06:15 -0600132 "linux,stdout-path", p, len);
133 free(p);
134 }
135 } else {
136 err = len;
137 }
138 }
139#endif
140 if (err < 0)
141 printf("WARNING: could not set linux,stdout-path %s.\n",
142 fdt_strerror(err));
143
144 return err;
145}
146#endif
147
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500148int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
149{
150 int nodeoffset;
151 int err, j, total;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000152 fdt32_t tmp;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500153 const char *path;
154 uint64_t addr, size;
155
156 /* Find the "chosen" node. */
157 nodeoffset = fdt_path_offset (fdt, "/chosen");
158
159 /* If there is no "chosen" node in the blob return */
160 if (nodeoffset < 0) {
161 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
162 return nodeoffset;
163 }
164
165 /* just return if initrd_start/end aren't valid */
166 if ((initrd_start == 0) || (initrd_end == 0))
167 return 0;
168
169 total = fdt_num_mem_rsv(fdt);
170
171 /*
172 * Look for an existing entry and update it. If we don't find
173 * the entry, we will j be the next available slot.
174 */
175 for (j = 0; j < total; j++) {
176 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
177 if (addr == initrd_start) {
178 fdt_del_mem_rsv(fdt, j);
179 break;
180 }
181 }
182
Grant Likelyce6b27a2011-03-28 09:58:55 +0000183 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500184 if (err < 0) {
185 printf("fdt_initrd: %s\n", fdt_strerror(err));
186 return err;
187 }
188
189 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
190 if ((path == NULL) || force) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000191 tmp = cpu_to_fdt32(initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500192 err = fdt_setprop(fdt, nodeoffset,
193 "linux,initrd-start", &tmp, sizeof(tmp));
194 if (err < 0) {
195 printf("WARNING: "
196 "could not set linux,initrd-start %s.\n",
197 fdt_strerror(err));
198 return err;
199 }
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000200 tmp = cpu_to_fdt32(initrd_end);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500201 err = fdt_setprop(fdt, nodeoffset,
202 "linux,initrd-end", &tmp, sizeof(tmp));
203 if (err < 0) {
204 printf("WARNING: could not set linux,initrd-end %s.\n",
205 fdt_strerror(err));
206
207 return err;
208 }
209 }
210
211 return 0;
212}
213
Heiko Schocher56844a22008-09-11 08:11:23 +0200214int fdt_chosen(void *fdt, int force)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400215{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400216 int nodeoffset;
217 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400218 char *str; /* used to set string properties */
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500219 const char *path;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400220
221 err = fdt_check_header(fdt);
222 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400223 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400224 return err;
225 }
226
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400227 /*
228 * Find the "chosen" node.
229 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500230 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400231
232 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500233 * If there is no "chosen" node in the blob, create it.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400234 */
235 if (nodeoffset < 0) {
236 /*
237 * Create a new node "/chosen" (offset 0 is root level)
238 */
239 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
240 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400241 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400242 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400243 return nodeoffset;
244 }
245 }
246
247 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500248 * Create /chosen properites that don't exist in the fdt.
249 * If the property exists, update it only if the "force" parameter
250 * is true.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400251 */
252 str = getenv("bootargs");
253 if (str != NULL) {
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500254 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
255 if ((path == NULL) || force) {
256 err = fdt_setprop(fdt, nodeoffset,
257 "bootargs", str, strlen(str)+1);
258 if (err < 0)
259 printf("WARNING: could not set bootargs %s.\n",
260 fdt_strerror(err));
261 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400262 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500263
Kumar Gala151c8b02007-11-26 17:06:15 -0600264#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500265 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
266 if ((path == NULL) || force)
267 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala151c8b02007-11-26 17:06:15 -0600268#endif
269
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400270#ifdef OF_STDOUT_PATH
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500271 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
272 if ((path == NULL) || force) {
273 err = fdt_setprop(fdt, nodeoffset,
274 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
275 if (err < 0)
276 printf("WARNING: could not set linux,stdout-path %s.\n",
277 fdt_strerror(err));
278 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400279#endif
280
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400281 return err;
282}
283
Kumar Galae93becf2007-11-03 19:46:28 -0500284void do_fixup_by_path(void *fdt, const char *path, const char *prop,
285 const void *val, int len, int create)
286{
287#if defined(DEBUG)
288 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600289 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500290 for (i = 0; i < len; i++)
291 debug(" %.2x", *(u8*)(val+i));
292 debug("\n");
293#endif
294 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
295 if (rc)
296 printf("Unable to update property %s:%s, err=%s\n",
297 path, prop, fdt_strerror(rc));
298}
299
300void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
301 u32 val, int create)
302{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000303 fdt32_t tmp = cpu_to_fdt32(val);
304 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Galae93becf2007-11-03 19:46:28 -0500305}
306
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600307void do_fixup_by_prop(void *fdt,
308 const char *pname, const void *pval, int plen,
309 const char *prop, const void *val, int len,
310 int create)
311{
312 int off;
313#if defined(DEBUG)
314 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600315 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600316 for (i = 0; i < len; i++)
317 debug(" %.2x", *(u8*)(val+i));
318 debug("\n");
319#endif
320 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
321 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000322 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600323 fdt_setprop(fdt, off, prop, val, len);
324 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
325 }
326}
327
328void do_fixup_by_prop_u32(void *fdt,
329 const char *pname, const void *pval, int plen,
330 const char *prop, u32 val, int create)
331{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000332 fdt32_t tmp = cpu_to_fdt32(val);
333 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600334}
335
336void do_fixup_by_compat(void *fdt, const char *compat,
337 const char *prop, const void *val, int len, int create)
338{
339 int off = -1;
340#if defined(DEBUG)
341 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600342 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600343 for (i = 0; i < len; i++)
344 debug(" %.2x", *(u8*)(val+i));
345 debug("\n");
346#endif
347 off = fdt_node_offset_by_compatible(fdt, -1, compat);
348 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000349 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600350 fdt_setprop(fdt, off, prop, val, len);
351 off = fdt_node_offset_by_compatible(fdt, off, compat);
352 }
353}
354
355void do_fixup_by_compat_u32(void *fdt, const char *compat,
356 const char *prop, u32 val, int create)
357{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000358 fdt32_t tmp = cpu_to_fdt32(val);
359 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600360}
361
John Rigbya6bd9e82010-10-13 13:57:33 -0600362/*
363 * Get cells len in bytes
364 * if #NNNN-cells property is 2 then len is 8
365 * otherwise len is 4
366 */
367static int get_cells_len(void *blob, char *nr_cells_name)
Kumar Gala3c927282007-11-26 14:57:45 -0600368{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000369 const fdt32_t *cell;
John Rigbya6bd9e82010-10-13 13:57:33 -0600370
371 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
Gabe Blackde166062011-11-08 01:05:32 -0800372 if (cell && fdt32_to_cpu(*cell) == 2)
John Rigbya6bd9e82010-10-13 13:57:33 -0600373 return 8;
374
375 return 4;
376}
377
378/*
379 * Write a 4 or 8 byte big endian cell
380 */
381static void write_cell(u8 *addr, u64 val, int size)
382{
383 int shift = (size - 1) * 8;
384 while (size-- > 0) {
385 *addr++ = (val >> shift) & 0xff;
386 shift -= 8;
387 }
388}
389
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000390#define MEMORY_BANKS_MAX 4
John Rigbya6bd9e82010-10-13 13:57:33 -0600391int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
392{
393 int err, nodeoffset;
394 int addr_cell_len, size_cell_len, len;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000395 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbya6bd9e82010-10-13 13:57:33 -0600396 int bank;
Kumar Gala3c927282007-11-26 14:57:45 -0600397
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000398 if (banks > MEMORY_BANKS_MAX) {
399 printf("%s: num banks %d exceeds hardcoded limit %d."
400 " Recompile with higher MEMORY_BANKS_MAX?\n",
401 __FUNCTION__, banks, MEMORY_BANKS_MAX);
402 return -1;
403 }
404
Kumar Gala3c927282007-11-26 14:57:45 -0600405 err = fdt_check_header(blob);
406 if (err < 0) {
407 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
408 return err;
409 }
410
411 /* update, or add and update /memory node */
412 nodeoffset = fdt_path_offset(blob, "/memory");
413 if (nodeoffset < 0) {
414 nodeoffset = fdt_add_subnode(blob, 0, "memory");
415 if (nodeoffset < 0)
416 printf("WARNING: could not create /memory: %s.\n",
417 fdt_strerror(nodeoffset));
418 return nodeoffset;
419 }
420 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
421 sizeof("memory"));
422 if (err < 0) {
423 printf("WARNING: could not set %s %s.\n", "device_type",
424 fdt_strerror(err));
425 return err;
426 }
427
John Rigbya6bd9e82010-10-13 13:57:33 -0600428 addr_cell_len = get_cells_len(blob, "#address-cells");
429 size_cell_len = get_cells_len(blob, "#size-cells");
Kumar Gala3c927282007-11-26 14:57:45 -0600430
John Rigbya6bd9e82010-10-13 13:57:33 -0600431 for (bank = 0, len = 0; bank < banks; bank++) {
432 write_cell(tmp + len, start[bank], addr_cell_len);
433 len += addr_cell_len;
434
435 write_cell(tmp + len, size[bank], size_cell_len);
436 len += size_cell_len;
Kumar Gala3c927282007-11-26 14:57:45 -0600437 }
438
439 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
440 if (err < 0) {
441 printf("WARNING: could not set %s %s.\n",
442 "reg", fdt_strerror(err));
443 return err;
444 }
445 return 0;
446}
447
John Rigbya6bd9e82010-10-13 13:57:33 -0600448int fdt_fixup_memory(void *blob, u64 start, u64 size)
449{
450 return fdt_fixup_memory_banks(blob, &start, &size, 1);
451}
452
Kumar Galaba37aa02008-08-19 15:41:18 -0500453void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600454{
Kumar Galaba37aa02008-08-19 15:41:18 -0500455 int node, i, j;
456 char enet[16], *tmp, *end;
457 char mac[16] = "ethaddr";
Kumar Galaab544632007-11-21 11:11:03 -0600458 const char *path;
Kumar Galaba37aa02008-08-19 15:41:18 -0500459 unsigned char mac_addr[6];
Kumar Galaab544632007-11-21 11:11:03 -0600460
461 node = fdt_path_offset(fdt, "/aliases");
Kumar Galaba37aa02008-08-19 15:41:18 -0500462 if (node < 0)
463 return;
464
465 i = 0;
466 while ((tmp = getenv(mac)) != NULL) {
467 sprintf(enet, "ethernet%d", i);
468 path = fdt_getprop(fdt, node, enet, NULL);
469 if (!path) {
470 debug("No alias for %s\n", enet);
471 sprintf(mac, "eth%daddr", ++i);
472 continue;
Kumar Galaab544632007-11-21 11:11:03 -0600473 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500474
475 for (j = 0; j < 6; j++) {
476 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
477 if (tmp)
478 tmp = (*end) ? end+1 : end;
Kumar Galaab544632007-11-21 11:11:03 -0600479 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500480
481 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
482 do_fixup_by_path(fdt, path, "local-mac-address",
483 &mac_addr, 6, 1);
484
485 sprintf(mac, "eth%daddr", ++i);
Kumar Galaab544632007-11-21 11:11:03 -0600486 }
487}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300488
Kumar Gala3082d232008-08-15 08:24:42 -0500489/* Resize the fdt to its actual size + a bit of padding */
490int fdt_resize(void *blob)
491{
492 int i;
493 uint64_t addr, size;
494 int total, ret;
495 uint actualsize;
496
497 if (!blob)
498 return 0;
499
500 total = fdt_num_mem_rsv(blob);
501 for (i = 0; i < total; i++) {
502 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000503 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500504 fdt_del_mem_rsv(blob, i);
505 break;
506 }
507 }
508
Peter Korsgaardf242a082008-10-28 08:26:52 +0100509 /*
510 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200511 * plus the size needed for 5 fdt_add_mem_rsv, one
512 * for the fdt itself and 4 for a possible initrd
513 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100514 */
Kumar Gala3082d232008-08-15 08:24:42 -0500515 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200516 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500517
518 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000519 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
520 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500521
522 /* Change the fdt header to reflect the correct size */
523 fdt_set_totalsize(blob, actualsize);
524
525 /* Add the new reservation */
Simon Glass92549352011-09-17 06:48:58 +0000526 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Gala3082d232008-08-15 08:24:42 -0500527 if (ret < 0)
528 return ret;
529
530 return actualsize;
531}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500532
533#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500534#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500535
536#define FDT_PCI_PREFETCH (0x40000000)
537#define FDT_PCI_MEM32 (0x02000000)
538#define FDT_PCI_IO (0x01000000)
539#define FDT_PCI_MEM64 (0x03000000)
540
541int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
542
543 int addrcell, sizecell, len, r;
544 u32 *dma_range;
545 /* sized based on pci addr cells, size-cells, & address-cells */
546 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
547
548 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
549 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
550
551 dma_range = &dma_ranges[0];
552 for (r = 0; r < hose->region_count; r++) {
553 u64 bus_start, phys_start, size;
554
Kumar Galaff4e66e2009-02-06 09:49:31 -0600555 /* skip if !PCI_REGION_SYS_MEMORY */
556 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500557 continue;
558
559 bus_start = (u64)hose->regions[r].bus_start;
560 phys_start = (u64)hose->regions[r].phys_start;
561 size = (u64)hose->regions[r].size;
562
563 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500564 if (size >= 0x100000000ull)
Kumar Gala8ab451c2008-10-22 23:33:56 -0500565 dma_range[0] |= FDT_PCI_MEM64;
566 else
567 dma_range[0] |= FDT_PCI_MEM32;
568 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
569 dma_range[0] |= FDT_PCI_PREFETCH;
570#ifdef CONFIG_SYS_PCI_64BIT
571 dma_range[1] = bus_start >> 32;
572#else
573 dma_range[1] = 0;
574#endif
575 dma_range[2] = bus_start & 0xffffffff;
576
577 if (addrcell == 2) {
578 dma_range[3] = phys_start >> 32;
579 dma_range[4] = phys_start & 0xffffffff;
580 } else {
581 dma_range[3] = phys_start & 0xffffffff;
582 }
583
584 if (sizecell == 2) {
585 dma_range[3 + addrcell + 0] = size >> 32;
586 dma_range[3 + addrcell + 1] = size & 0xffffffff;
587 } else {
588 dma_range[3 + addrcell + 0] = size & 0xffffffff;
589 }
590
591 dma_range += (3 + addrcell + sizecell);
592 }
593
594 len = dma_range - &dma_ranges[0];
595 if (len)
596 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
597
598 return 0;
599}
600#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200601
602#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
603/*
Stefan Roese8a805df2010-09-16 14:01:53 +0200604 * Provide a weak default function to return the flash bank size.
605 * There might be multiple non-identical flash chips connected to one
606 * chip-select, so we need to pass an index as well.
607 */
608u32 __flash_get_bank_size(int cs, int idx)
609{
610 extern flash_info_t flash_info[];
611
612 /*
613 * As default, a simple 1:1 mapping is provided. Boards with
614 * a different mapping need to supply a board specific mapping
615 * routine.
616 */
617 return flash_info[cs].size;
618}
619u32 flash_get_bank_size(int cs, int idx)
620 __attribute__((weak, alias("__flash_get_bank_size")));
621
622/*
Stefan Roese30d45c02009-10-21 11:59:52 +0200623 * This function can be used to update the size in the "reg" property
Stefan Roese8a805df2010-09-16 14:01:53 +0200624 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roese30d45c02009-10-21 11:59:52 +0200625 * non-fixed NOR FLASH sizes.
626 */
Stefan Roese8a805df2010-09-16 14:01:53 +0200627int fdt_fixup_nor_flash_size(void *blob)
Stefan Roese30d45c02009-10-21 11:59:52 +0200628{
629 char compat[][16] = { "cfi-flash", "jedec-flash" };
630 int off;
631 int len;
632 struct fdt_property *prop;
Stefan Roese2778a012010-09-24 13:51:50 +0200633 u32 *reg, *reg2;
Stefan Roese30d45c02009-10-21 11:59:52 +0200634 int i;
635
636 for (i = 0; i < 2; i++) {
637 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
638 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200639 int idx;
640
Stefan Roese30d45c02009-10-21 11:59:52 +0200641 /*
Stefan Roese8a805df2010-09-16 14:01:53 +0200642 * Found one compatible node, so fixup the size
643 * int its reg properties
Stefan Roese30d45c02009-10-21 11:59:52 +0200644 */
645 prop = fdt_get_property_w(blob, off, "reg", &len);
646 if (prop) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200647 int tuple_size = 3 * sizeof(reg);
Stefan Roese30d45c02009-10-21 11:59:52 +0200648
Stefan Roese8a805df2010-09-16 14:01:53 +0200649 /*
650 * There might be multiple reg-tuples,
651 * so loop through them all
652 */
Stefan Roese2778a012010-09-24 13:51:50 +0200653 reg = reg2 = (u32 *)&prop->data[0];
654 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200655 /*
656 * Update size in reg property
657 */
658 reg[2] = flash_get_bank_size(reg[0],
659 idx);
Stefan Roese2778a012010-09-24 13:51:50 +0200660
661 /*
662 * Point to next reg tuple
663 */
664 reg += 3;
Stefan Roese30d45c02009-10-21 11:59:52 +0200665 }
Stefan Roese2778a012010-09-24 13:51:50 +0200666
667 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roese30d45c02009-10-21 11:59:52 +0200668 }
669
670 /* Move to next compatible node */
671 off = fdt_node_offset_by_compatible(blob, off,
672 compat[i]);
673 }
674 }
675
Stefan Roese8a805df2010-09-16 14:01:53 +0200676 return 0;
Stefan Roese30d45c02009-10-21 11:59:52 +0200677}
678#endif
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100679
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200680int fdt_increase_size(void *fdt, int add_len)
681{
682 int newlen;
683
684 newlen = fdt_totalsize(fdt) + add_len;
685
686 /* Open in place with a new len */
687 return fdt_open_into(fdt, fdt, newlen);
688}
689
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100690#ifdef CONFIG_FDT_FIXUP_PARTITIONS
691#include <jffs2/load_kernel.h>
692#include <mtd_node.h>
693
694struct reg_cell {
695 unsigned int r0;
696 unsigned int r1;
697};
698
699int fdt_del_subnodes(const void *blob, int parent_offset)
700{
701 int off, ndepth;
702 int ret;
703
704 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
705 (off >= 0) && (ndepth > 0);
706 off = fdt_next_node(blob, off, &ndepth)) {
707 if (ndepth == 1) {
708 debug("delete %s: offset: %x\n",
709 fdt_get_name(blob, off, 0), off);
710 ret = fdt_del_node((void *)blob, off);
711 if (ret < 0) {
712 printf("Can't delete node: %s\n",
713 fdt_strerror(ret));
714 return ret;
715 } else {
716 ndepth = 0;
717 off = parent_offset;
718 }
719 }
720 }
721 return 0;
722}
723
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100724int fdt_del_partitions(void *blob, int parent_offset)
725{
726 const void *prop;
727 int ndepth = 0;
728 int off;
729 int ret;
730
731 off = fdt_next_node(blob, parent_offset, &ndepth);
732 if (off > 0 && ndepth == 1) {
733 prop = fdt_getprop(blob, off, "label", NULL);
734 if (prop == NULL) {
735 /*
736 * Could not find label property, nand {}; node?
737 * Check subnode, delete partitions there if any.
738 */
739 return fdt_del_partitions(blob, off);
740 } else {
741 ret = fdt_del_subnodes(blob, parent_offset);
742 if (ret < 0) {
743 printf("Can't remove subnodes: %s\n",
744 fdt_strerror(ret));
745 return ret;
746 }
747 }
748 }
749 return 0;
750}
751
752int fdt_node_set_part_info(void *blob, int parent_offset,
753 struct mtd_device *dev)
754{
755 struct list_head *pentry;
756 struct part_info *part;
757 struct reg_cell cell;
758 int off, ndepth = 0;
759 int part_num, ret;
760 char buf[64];
761
762 ret = fdt_del_partitions(blob, parent_offset);
763 if (ret < 0)
764 return ret;
765
766 /*
767 * Check if it is nand {}; subnode, adjust
768 * the offset in this case
769 */
770 off = fdt_next_node(blob, parent_offset, &ndepth);
771 if (off > 0 && ndepth == 1)
772 parent_offset = off;
773
774 part_num = 0;
775 list_for_each_prev(pentry, &dev->parts) {
776 int newoff;
777
778 part = list_entry(pentry, struct part_info, link);
779
780 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
781 part_num, part->name, part->size,
782 part->offset, part->mask_flags);
783
784 sprintf(buf, "partition@%x", part->offset);
785add_sub:
786 ret = fdt_add_subnode(blob, parent_offset, buf);
787 if (ret == -FDT_ERR_NOSPACE) {
788 ret = fdt_increase_size(blob, 512);
789 if (!ret)
790 goto add_sub;
791 else
792 goto err_size;
793 } else if (ret < 0) {
794 printf("Can't add partition node: %s\n",
795 fdt_strerror(ret));
796 return ret;
797 }
798 newoff = ret;
799
800 /* Check MTD_WRITEABLE_CMD flag */
801 if (part->mask_flags & 1) {
802add_ro:
803 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
804 if (ret == -FDT_ERR_NOSPACE) {
805 ret = fdt_increase_size(blob, 512);
806 if (!ret)
807 goto add_ro;
808 else
809 goto err_size;
810 } else if (ret < 0)
811 goto err_prop;
812 }
813
814 cell.r0 = cpu_to_fdt32(part->offset);
815 cell.r1 = cpu_to_fdt32(part->size);
816add_reg:
817 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
818 if (ret == -FDT_ERR_NOSPACE) {
819 ret = fdt_increase_size(blob, 512);
820 if (!ret)
821 goto add_reg;
822 else
823 goto err_size;
824 } else if (ret < 0)
825 goto err_prop;
826
827add_label:
828 ret = fdt_setprop_string(blob, newoff, "label", part->name);
829 if (ret == -FDT_ERR_NOSPACE) {
830 ret = fdt_increase_size(blob, 512);
831 if (!ret)
832 goto add_label;
833 else
834 goto err_size;
835 } else if (ret < 0)
836 goto err_prop;
837
838 part_num++;
839 }
840 return 0;
841err_size:
842 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
843 return ret;
844err_prop:
845 printf("Can't add property: %s\n", fdt_strerror(ret));
846 return ret;
847}
848
849/*
850 * Update partitions in nor/nand nodes using info from
851 * mtdparts environment variable. The nodes to update are
852 * specified by node_info structure which contains mtd device
853 * type and compatible string: E. g. the board code in
854 * ft_board_setup() could use:
855 *
856 * struct node_info nodes[] = {
857 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
858 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
859 * };
860 *
861 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
862 */
863void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
864{
865 struct node_info *ni = node_info;
866 struct mtd_device *dev;
867 char *parts;
868 int i, idx;
869 int noff;
870
871 parts = getenv("mtdparts");
872 if (!parts)
873 return;
874
875 if (mtdparts_init() != 0)
876 return;
877
878 for (i = 0; i < node_info_size; i++) {
879 idx = 0;
880 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
881 while (noff != -FDT_ERR_NOTFOUND) {
882 debug("%s: %s, mtd dev type %d\n",
883 fdt_get_name(blob, noff, 0),
884 ni[i].compat, ni[i].type);
885 dev = device_find(ni[i].type, idx++);
886 if (dev) {
887 if (fdt_node_set_part_info(blob, noff, dev))
888 return; /* return on error */
889 }
890
891 /* Jump to next flash node */
892 noff = fdt_node_offset_by_compatible(blob, noff,
893 ni[i].compat);
894 }
895 }
896}
897#endif
Kumar Gala49b97d92010-03-30 10:19:26 -0500898
899void fdt_del_node_and_alias(void *blob, const char *alias)
900{
901 int off = fdt_path_offset(blob, alias);
902
903 if (off < 0)
904 return;
905
906 fdt_del_node(blob, off);
907
908 off = fdt_path_offset(blob, "/aliases");
909 fdt_delprop(blob, off, alias);
910}
Kumar Galaa0342c02010-06-16 14:27:38 -0500911
912/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000913static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galaa0342c02010-06-16 14:27:38 -0500914{
915 u64 r = 0;
916 while (size--)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000917 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galaa0342c02010-06-16 14:27:38 -0500918 return r;
919}
920
Kumar Galaa0342c02010-06-16 14:27:38 -0500921#define PRu64 "%llx"
922
923/* Max address size we deal with */
924#define OF_MAX_ADDR_CELLS 4
925#define OF_BAD_ADDR ((u64)-1)
926#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
927 (ns) > 0)
928
929/* Debug utility */
930#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000931static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500932{
933 printf("%s", s);
934 while(na--)
935 printf(" %08x", *(addr++));
936 printf("\n");
937}
938#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000939static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -0500940#endif
941
942/* Callbacks for bus specific translators */
943struct of_bus {
944 const char *name;
945 const char *addresses;
Scott Wood6395f312010-08-12 18:37:39 -0500946 void (*count_cells)(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500947 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000948 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500949 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000950 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -0500951};
952
953/* Default translator (generic bus) */
Scott Wood6395f312010-08-12 18:37:39 -0500954static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500955 int *addrc, int *sizec)
956{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000957 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -0500958
959 if (addrc) {
960 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
961 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000962 *addrc = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500963 else
964 *addrc = 2;
965 }
966
967 if (sizec) {
968 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
969 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000970 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -0500971 else
972 *sizec = 1;
973 }
Kumar Galaa0342c02010-06-16 14:27:38 -0500974}
975
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000976static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -0500977 int na, int ns, int pna)
978{
979 u64 cp, s, da;
980
981 cp = of_read_number(range, na);
982 s = of_read_number(range + na + pna, ns);
983 da = of_read_number(addr, na);
984
985 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
986 cp, s, da);
987
988 if (da < cp || da >= (cp + s))
989 return OF_BAD_ADDR;
990 return da - cp;
991}
992
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000993static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -0500994{
995 u64 a = of_read_number(addr, na);
996 memset(addr, 0, na * 4);
997 a += offset;
998 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000999 addr[na - 2] = cpu_to_fdt32(a >> 32);
1000 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -05001001
1002 return 0;
1003}
1004
1005/* Array of bus specific translators */
1006static struct of_bus of_busses[] = {
1007 /* Default */
1008 {
1009 .name = "default",
1010 .addresses = "reg",
1011 .count_cells = of_bus_default_count_cells,
1012 .map = of_bus_default_map,
1013 .translate = of_bus_default_translate,
1014 },
1015};
1016
1017static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001018 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001019 int na, int ns, int pna, const char *rprop)
1020{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001021 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001022 int rlen;
1023 int rone;
1024 u64 offset = OF_BAD_ADDR;
1025
1026 /* Normally, an absence of a "ranges" property means we are
1027 * crossing a non-translatable boundary, and thus the addresses
1028 * below the current not cannot be converted to CPU physical ones.
1029 * Unfortunately, while this is very clear in the spec, it's not
1030 * what Apple understood, and they do have things like /uni-n or
1031 * /ht nodes with no "ranges" property and a lot of perfectly
1032 * useable mapped devices below them. Thus we treat the absence of
1033 * "ranges" as equivalent to an empty "ranges" property which means
1034 * a 1:1 translation at that level. It's up to the caller not to try
1035 * to translate addresses that aren't supposed to be translated in
1036 * the first place. --BenH.
1037 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001038 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001039 if (ranges == NULL || rlen == 0) {
1040 offset = of_read_number(addr, na);
1041 memset(addr, 0, pna * 4);
1042 debug("OF: no ranges, 1:1 translation\n");
1043 goto finish;
1044 }
1045
1046 debug("OF: walking ranges...\n");
1047
1048 /* Now walk through the ranges */
1049 rlen /= 4;
1050 rone = na + pna + ns;
1051 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1052 offset = bus->map(addr, ranges, na, ns, pna);
1053 if (offset != OF_BAD_ADDR)
1054 break;
1055 }
1056 if (offset == OF_BAD_ADDR) {
1057 debug("OF: not found !\n");
1058 return 1;
1059 }
1060 memcpy(addr, ranges + na, 4 * pna);
1061
1062 finish:
1063 of_dump_addr("OF: parent translation for:", addr, pna);
1064 debug("OF: with offset: "PRu64"\n", offset);
1065
1066 /* Translate it into parent bus space */
1067 return pbus->translate(addr, offset, pna);
1068}
1069
1070/*
1071 * Translate an address from the device-tree into a CPU physical address,
1072 * this walks up the tree and applies the various bus mappings on the
1073 * way.
1074 *
1075 * Note: We consider that crossing any level with #size-cells == 0 to mean
1076 * that translation is impossible (that is we are not dealing with a value
1077 * that can be mapped to a cpu physical address). This is not really specified
1078 * that way, but this is traditionally the way IBM at least do things
1079 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001080static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1081 const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001082{
1083 int parent;
1084 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001085 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001086 int na, ns, pna, pns;
1087 u64 result = OF_BAD_ADDR;
1088
1089 debug("OF: ** translation for device %s **\n",
1090 fdt_get_name(blob, node_offset, NULL));
1091
1092 /* Get parent & match bus type */
1093 parent = fdt_parent_offset(blob, node_offset);
1094 if (parent < 0)
1095 goto bail;
1096 bus = &of_busses[0];
1097
1098 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001099 bus->count_cells(blob, parent, &na, &ns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001100 if (!OF_CHECK_COUNTS(na, ns)) {
1101 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1102 fdt_get_name(blob, node_offset, NULL));
1103 goto bail;
1104 }
1105 memcpy(addr, in_addr, na * 4);
1106
1107 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1108 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1109 of_dump_addr("OF: translating address:", addr, na);
1110
1111 /* Translate */
1112 for (;;) {
1113 /* Switch to parent bus */
1114 node_offset = parent;
1115 parent = fdt_parent_offset(blob, node_offset);
1116
1117 /* If root, we have finished */
1118 if (parent < 0) {
1119 debug("OF: reached root node\n");
1120 result = of_read_number(addr, na);
1121 break;
1122 }
1123
1124 /* Get new parent bus and counts */
1125 pbus = &of_busses[0];
Scott Wood6395f312010-08-12 18:37:39 -05001126 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001127 if (!OF_CHECK_COUNTS(pna, pns)) {
1128 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1129 fdt_get_name(blob, node_offset, NULL));
1130 break;
1131 }
1132
1133 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1134 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1135
1136 /* Apply bus translation */
1137 if (of_translate_one(blob, node_offset, bus, pbus,
1138 addr, na, ns, pna, rprop))
1139 break;
1140
1141 /* Complete the move up one level */
1142 na = pna;
1143 ns = pns;
1144 bus = pbus;
1145
1146 of_dump_addr("OF: one level translation:", addr, na);
1147 }
1148 bail:
1149
1150 return result;
1151}
1152
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001153u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001154{
1155 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1156}
Kumar Gala75e73af2010-07-04 12:48:21 -05001157
1158/**
1159 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1160 * who's reg property matches a physical cpu address
1161 *
1162 * @blob: ptr to device tree
1163 * @compat: compatiable string to match
1164 * @compat_off: property name
1165 *
1166 */
1167int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1168 phys_addr_t compat_off)
1169{
1170 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1171 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001172 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala75e73af2010-07-04 12:48:21 -05001173 if (reg) {
1174 if (compat_off == fdt_translate_address(blob, off, reg))
1175 return off;
1176 }
1177 off = fdt_node_offset_by_compatible(blob, off, compat);
1178 }
1179
1180 return -FDT_ERR_NOTFOUND;
1181}
1182
Kumar Galab4b847e2010-07-09 16:18:58 -05001183/**
1184 * fdt_alloc_phandle: Return next free phandle value
1185 *
1186 * @blob: ptr to device tree
1187 */
1188int fdt_alloc_phandle(void *blob)
1189{
Timur Tabi50bf17b2011-09-20 18:24:35 -05001190 int offset, phandle = 0;
Kumar Gala75e73af2010-07-04 12:48:21 -05001191
Kumar Galab4b847e2010-07-09 16:18:58 -05001192 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1193 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabi50bf17b2011-09-20 18:24:35 -05001194 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab4b847e2010-07-09 16:18:58 -05001195 }
1196
1197 return phandle + 1;
1198}
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001199
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001200/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001201 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001202 *
1203 * @fdt: ptr to device tree
1204 * @nodeoffset: node to update
1205 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001206 */
1207int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001208{
1209 int ret;
1210
1211#ifdef DEBUG
1212 int off = fdt_node_offset_by_phandle(fdt, phandle);
1213
1214 if ((off >= 0) && (off != nodeoffset)) {
1215 char buf[64];
1216
1217 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1218 printf("Trying to update node %s with phandle %u ",
1219 buf, phandle);
1220
1221 fdt_get_path(fdt, off, buf, sizeof(buf));
1222 printf("that already exists in node %s.\n", buf);
1223 return -FDT_ERR_BADPHANDLE;
1224 }
1225#endif
1226
1227 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1228 if (ret < 0)
1229 return ret;
1230
1231 /*
1232 * For now, also set the deprecated "linux,phandle" property, so that we
1233 * don't break older kernels.
1234 */
1235 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1236
1237 return ret;
1238}
1239
Kumar Gala10aeabd2011-08-01 00:25:20 -05001240/*
1241 * fdt_create_phandle: Create a phandle property for the given node
1242 *
1243 * @fdt: ptr to device tree
1244 * @nodeoffset: node to update
1245 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001246unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001247{
1248 /* see if there is a phandle already */
1249 int phandle = fdt_get_phandle(fdt, nodeoffset);
1250
1251 /* if we got 0, means no phandle so create one */
1252 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001253 int ret;
1254
Kumar Gala10aeabd2011-08-01 00:25:20 -05001255 phandle = fdt_alloc_phandle(fdt);
Timur Tabi3c927cc2011-09-20 18:24:34 -05001256 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1257 if (ret < 0) {
1258 printf("Can't set phandle %u: %s\n", phandle,
1259 fdt_strerror(ret));
1260 return 0;
1261 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001262 }
1263
1264 return phandle;
1265}
1266
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001267/*
1268 * fdt_set_node_status: Set status for the given node
1269 *
1270 * @fdt: ptr to device tree
1271 * @nodeoffset: node to update
1272 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1273 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1274 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1275 */
1276int fdt_set_node_status(void *fdt, int nodeoffset,
1277 enum fdt_status status, unsigned int error_code)
1278{
1279 char buf[16];
1280 int ret = 0;
1281
1282 if (nodeoffset < 0)
1283 return nodeoffset;
1284
1285 switch (status) {
1286 case FDT_STATUS_OKAY:
1287 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1288 break;
1289 case FDT_STATUS_DISABLED:
1290 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1291 break;
1292 case FDT_STATUS_FAIL:
1293 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1294 break;
1295 case FDT_STATUS_FAIL_ERROR_CODE:
1296 sprintf(buf, "fail-%d", error_code);
1297 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1298 break;
1299 default:
1300 printf("Invalid fdt status: %x\n", status);
1301 ret = -1;
1302 break;
1303 }
1304
1305 return ret;
1306}
1307
1308/*
1309 * fdt_set_status_by_alias: Set status for the given node given an alias
1310 *
1311 * @fdt: ptr to device tree
1312 * @alias: alias of node to update
1313 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1314 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1315 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1316 */
1317int fdt_set_status_by_alias(void *fdt, const char* alias,
1318 enum fdt_status status, unsigned int error_code)
1319{
1320 int offset = fdt_path_offset(fdt, alias);
1321
1322 return fdt_set_node_status(fdt, offset, status, error_code);
1323}
1324
Tom Wai-Hong Tam096eb3f2012-12-05 14:46:41 +00001325#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschinbeca5a52010-08-18 11:25:20 +02001326int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1327{
1328 int noff;
1329 int ret;
1330
1331 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1332 if (noff != -FDT_ERR_NOTFOUND) {
1333 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1334add_edid:
1335 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1336 if (ret == -FDT_ERR_NOSPACE) {
1337 ret = fdt_increase_size(blob, 512);
1338 if (!ret)
1339 goto add_edid;
1340 else
1341 goto err_size;
1342 } else if (ret < 0) {
1343 printf("Can't add property: %s\n", fdt_strerror(ret));
1344 return ret;
1345 }
1346 }
1347 return 0;
1348err_size:
1349 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1350 return ret;
1351}
1352#endif
Timur Tabibb682002011-05-03 13:24:07 -05001353
1354/*
1355 * Verify the physical address of device tree node for a given alias
1356 *
1357 * This function locates the device tree node of a given alias, and then
1358 * verifies that the physical address of that device matches the given
1359 * parameter. It displays a message if there is a mismatch.
1360 *
1361 * Returns 1 on success, 0 on failure
1362 */
1363int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1364{
1365 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001366 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001367 int node, len;
1368 u64 dt_addr;
1369
1370 path = fdt_getprop(fdt, anode, alias, NULL);
1371 if (!path) {
1372 /* If there's no such alias, then it's not a failure */
1373 return 1;
1374 }
1375
1376 node = fdt_path_offset(fdt, path);
1377 if (node < 0) {
1378 printf("Warning: device tree alias '%s' points to invalid "
1379 "node %s.\n", alias, path);
1380 return 0;
1381 }
1382
1383 reg = fdt_getprop(fdt, node, "reg", &len);
1384 if (!reg) {
1385 printf("Warning: device tree node '%s' has no address.\n",
1386 path);
1387 return 0;
1388 }
1389
1390 dt_addr = fdt_translate_address(fdt, node, reg);
1391 if (addr != dt_addr) {
1392 printf("Warning: U-Boot configured device %s at address %llx,\n"
1393 " but the device tree has it address %llx.\n",
1394 alias, addr, dt_addr);
1395 return 0;
1396 }
1397
1398 return 1;
1399}
1400
1401/*
1402 * Returns the base address of an SOC or PCI node
1403 */
1404u64 fdt_get_base_address(void *fdt, int node)
1405{
1406 int size;
1407 u32 naddr;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001408 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001409
1410 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1411 if (prop && size == 4)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001412 naddr = be32_to_cpup(prop);
Timur Tabibb682002011-05-03 13:24:07 -05001413 else
1414 naddr = 2;
1415
1416 prop = fdt_getprop(fdt, node, "ranges", &size);
1417
1418 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1419}