blob: 9a6f6b7d8b16c822496c53a62df26d3850081b29 [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 *
Gerald Van Barenc4a57ea2007-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 Vorontsov9e9cf602009-10-15 17:47:04 +040027#include <stdio_dev.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040028#include <linux/ctype.h>
29#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040030#include <asm/global_data.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040031#include <libfdt.h>
32#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060033#include <exports.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040034
35/*
36 * Global data (for the gd->bd)
37 */
38DECLARE_GLOBAL_DATA_PTR;
39
Kumar Gala1ba00402008-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 Blackcd37de72011-11-08 01:09:44 -080051u32 fdt_getprop_u32_default(const void *fdt, const char *path,
52 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050053{
Kim Phillips6542c072013-01-16 14:00:11 +000054 const fdt32_t *val;
Kumar Gala1ba00402008-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 Black9df8b4c2011-11-08 01:05:32 -080063 return fdt32_to_cpu(*val);
Kumar Gala1ba00402008-10-23 00:05:47 -050064 else
65 return dflt;
66}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040067
Kumar Galabf771dc2007-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 Galac8ab7052007-10-24 11:04:22 -050083 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050084
85 if (nodeoff < 0)
86 return nodeoff;
87
Kim Phillips6542c072013-01-16 14:00:11 +000088 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-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 Gala2dc9b4a2007-11-26 17:06:15 -060094#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040095
Marek Vasut52039da2012-09-14 23:45:51 +020096#ifdef CONFIG_CONS_INDEX
Anton Vorontsov9e9cf602009-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 Vasut52039da2012-09-14 23:45:51 +0200108#endif
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400109
Detlev Zundel46e192a2008-06-20 22:24:05 +0200110static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-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 Vorontsov9e9cf602009-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 Gala2dc9b4a2007-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 Zundel46e192a2008-06-20 22:24:05 +0200131 err = fdt_setprop(fdt, chosenoff,
Kumar Gala2dc9b4a2007-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 Gala99ebc052008-08-15 08:24:43 -0500148int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400149{
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400150 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500151 int err, j, total;
Kim Phillips6542c072013-01-16 14:00:11 +0000152 fdt32_t tmp;
Gerald Van Baren569c0352007-12-29 22:45:27 -0500153 const char *path;
Kumar Gala99ebc052008-08-15 08:24:43 -0500154 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400155
Kumar Gala99ebc052008-08-15 08:24:43 -0500156 /* 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;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400163 }
164
Kumar Gala99ebc052008-08-15 08:24:43 -0500165 /* just return if initrd_start/end aren't valid */
166 if ((initrd_start == 0) || (initrd_end == 0))
167 return 0;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400168
Kumar Gala99ebc052008-08-15 08:24:43 -0500169 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;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400180 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500181 }
182
Grant Likelyc379a562011-03-28 09:58:55 +0000183 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500184 if (err < 0) {
185 printf("fdt_initrd: %s\n", fdt_strerror(err));
186 return err;
187 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500188
Kumar Gala99ebc052008-08-15 08:24:43 -0500189 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
190 if ((path == NULL) || force) {
Kim Phillips6542c072013-01-16 14:00:11 +0000191 tmp = cpu_to_fdt32(initrd_start);
Kumar Gala99ebc052008-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 Phillips6542c072013-01-16 14:00:11 +0000200 tmp = cpu_to_fdt32(initrd_end);
Kumar Gala99ebc052008-08-15 08:24:43 -0500201 err = fdt_setprop(fdt, nodeoffset,
202 "linux,initrd-end", &tmp, sizeof(tmp));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400203 if (err < 0) {
Kumar Gala99ebc052008-08-15 08:24:43 -0500204 printf("WARNING: could not set linux,initrd-end %s.\n",
205 fdt_strerror(err));
206
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400207 return err;
208 }
209 }
210
Kumar Gala99ebc052008-08-15 08:24:43 -0500211 return 0;
212}
213
Heiko Schocherd29abae2008-09-11 08:11:23 +0200214int fdt_chosen(void *fdt, int force)
Kumar Gala99ebc052008-08-15 08:24:43 -0500215{
216 int nodeoffset;
217 int err;
218 char *str; /* used to set string properties */
219 const char *path;
220
221 err = fdt_check_header(fdt);
222 if (err < 0) {
223 printf("fdt_chosen: %s\n", fdt_strerror(err));
224 return err;
225 }
226
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400227 /*
228 * Find the "chosen" node.
229 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500230 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400231
232 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500233 * If there is no "chosen" node in the blob, create it.
Gerald Van Barenc4a57ea2007-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 Baren0dc25122007-08-07 21:14:22 -0400241 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400242 fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400243 return nodeoffset;
244 }
245 }
246
247 /*
Gerald Van Baren569c0352007-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 Barenc4a57ea2007-04-06 14:19:43 -0400251 */
252 str = getenv("bootargs");
253 if (str != NULL) {
Gerald Van Baren569c0352007-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 Barenc4a57ea2007-04-06 14:19:43 -0400262 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600263
264#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Baren569c0352007-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 Gala2dc9b4a2007-11-26 17:06:15 -0600268#endif
269
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400270#ifdef OF_STDOUT_PATH
Gerald Van Baren569c0352007-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 Barenc4a57ea2007-04-06 14:19:43 -0400279#endif
280
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400281 return err;
282}
283
Kumar Gala7e64cf82007-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 Gala88f0ca22008-02-13 15:09:58 -0600289 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-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 Phillips6542c072013-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 Gala7e64cf82007-11-03 19:46:28 -0500305}
306
Kumar Gala7590a192007-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 Gala88f0ca22008-02-13 15:09:58 -0600315 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-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 Phillips6542c072013-01-16 14:00:11 +0000322 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-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 Phillips6542c072013-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 Gala7590a192007-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 Gala88f0ca22008-02-13 15:09:58 -0600342 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-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 Phillips6542c072013-01-16 14:00:11 +0000349 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-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 Phillips6542c072013-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 Gala7590a192007-11-21 13:30:15 -0600360}
361
John Rigbyb4ae9422010-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 Gala0db72ed2007-11-26 14:57:45 -0600368{
Kim Phillips6542c072013-01-16 14:00:11 +0000369 const fdt32_t *cell;
John Rigbyb4ae9422010-10-13 13:57:33 -0600370
371 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
Gabe Black9df8b4c2011-11-08 01:05:32 -0800372 if (cell && fdt32_to_cpu(*cell) == 2)
John Rigbyb4ae9422010-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
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000390#ifdef CONFIG_NR_DRAM_BANKS
391#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
392#else
Kim Phillips6542c072013-01-16 14:00:11 +0000393#define MEMORY_BANKS_MAX 4
Doug Anderson8e4ed9a2013-04-30 10:22:00 +0000394#endif
John Rigbyb4ae9422010-10-13 13:57:33 -0600395int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
396{
397 int err, nodeoffset;
398 int addr_cell_len, size_cell_len, len;
Kim Phillips6542c072013-01-16 14:00:11 +0000399 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbyb4ae9422010-10-13 13:57:33 -0600400 int bank;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600401
Kim Phillips6542c072013-01-16 14:00:11 +0000402 if (banks > MEMORY_BANKS_MAX) {
403 printf("%s: num banks %d exceeds hardcoded limit %d."
404 " Recompile with higher MEMORY_BANKS_MAX?\n",
405 __FUNCTION__, banks, MEMORY_BANKS_MAX);
406 return -1;
407 }
408
Kumar Gala0db72ed2007-11-26 14:57:45 -0600409 err = fdt_check_header(blob);
410 if (err < 0) {
411 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
412 return err;
413 }
414
415 /* update, or add and update /memory node */
416 nodeoffset = fdt_path_offset(blob, "/memory");
417 if (nodeoffset < 0) {
418 nodeoffset = fdt_add_subnode(blob, 0, "memory");
419 if (nodeoffset < 0)
420 printf("WARNING: could not create /memory: %s.\n",
421 fdt_strerror(nodeoffset));
422 return nodeoffset;
423 }
424 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
425 sizeof("memory"));
426 if (err < 0) {
427 printf("WARNING: could not set %s %s.\n", "device_type",
428 fdt_strerror(err));
429 return err;
430 }
431
John Rigbyb4ae9422010-10-13 13:57:33 -0600432 addr_cell_len = get_cells_len(blob, "#address-cells");
433 size_cell_len = get_cells_len(blob, "#size-cells");
434
435 for (bank = 0, len = 0; bank < banks; bank++) {
436 write_cell(tmp + len, start[bank], addr_cell_len);
437 len += addr_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600438
John Rigbyb4ae9422010-10-13 13:57:33 -0600439 write_cell(tmp + len, size[bank], size_cell_len);
440 len += size_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600441 }
442
443 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
444 if (err < 0) {
445 printf("WARNING: could not set %s %s.\n",
446 "reg", fdt_strerror(err));
447 return err;
448 }
449 return 0;
450}
451
John Rigbyb4ae9422010-10-13 13:57:33 -0600452int fdt_fixup_memory(void *blob, u64 start, u64 size)
453{
454 return fdt_fixup_memory_banks(blob, &start, &size, 1);
455}
456
Kumar Galafabda922008-08-19 15:41:18 -0500457void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600458{
Kumar Galafabda922008-08-19 15:41:18 -0500459 int node, i, j;
460 char enet[16], *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000461 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600462 const char *path;
Kumar Galafabda922008-08-19 15:41:18 -0500463 unsigned char mac_addr[6];
Kumar Gala22699102007-11-21 11:11:03 -0600464
465 node = fdt_path_offset(fdt, "/aliases");
Kumar Galafabda922008-08-19 15:41:18 -0500466 if (node < 0)
467 return;
468
469 i = 0;
Stephen Warren7c15c772013-05-27 18:01:19 +0000470 strcpy(mac, "ethaddr");
Kumar Galafabda922008-08-19 15:41:18 -0500471 while ((tmp = getenv(mac)) != NULL) {
472 sprintf(enet, "ethernet%d", i);
473 path = fdt_getprop(fdt, node, enet, NULL);
474 if (!path) {
475 debug("No alias for %s\n", enet);
476 sprintf(mac, "eth%daddr", ++i);
477 continue;
Kumar Gala22699102007-11-21 11:11:03 -0600478 }
Kumar Galafabda922008-08-19 15:41:18 -0500479
480 for (j = 0; j < 6; j++) {
481 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
482 if (tmp)
483 tmp = (*end) ? end+1 : end;
Kumar Gala22699102007-11-21 11:11:03 -0600484 }
Kumar Galafabda922008-08-19 15:41:18 -0500485
486 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
487 do_fixup_by_path(fdt, path, "local-mac-address",
488 &mac_addr, 6, 1);
489
490 sprintf(mac, "eth%daddr", ++i);
Kumar Gala22699102007-11-21 11:11:03 -0600491 }
492}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300493
Kumar Galaf2f58c52008-08-15 08:24:42 -0500494/* Resize the fdt to its actual size + a bit of padding */
495int fdt_resize(void *blob)
496{
497 int i;
498 uint64_t addr, size;
499 int total, ret;
500 uint actualsize;
501
502 if (!blob)
503 return 0;
504
505 total = fdt_num_mem_rsv(blob);
506 for (i = 0; i < total; i++) {
507 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000508 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500509 fdt_del_mem_rsv(blob, i);
510 break;
511 }
512 }
513
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100514 /*
515 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200516 * plus the size needed for 5 fdt_add_mem_rsv, one
517 * for the fdt itself and 4 for a possible initrd
518 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100519 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500520 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200521 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500522
523 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000524 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
525 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500526
527 /* Change the fdt header to reflect the correct size */
528 fdt_set_totalsize(blob, actualsize);
529
530 /* Add the new reservation */
Simon Glassfc953242011-09-17 06:48:58 +0000531 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500532 if (ret < 0)
533 return ret;
534
535 return actualsize;
536}
Kumar Galae0475cd2008-10-22 23:33:56 -0500537
538#ifdef CONFIG_PCI
Kumar Galabaf41892009-08-05 09:03:54 -0500539#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500540
541#define FDT_PCI_PREFETCH (0x40000000)
542#define FDT_PCI_MEM32 (0x02000000)
543#define FDT_PCI_IO (0x01000000)
544#define FDT_PCI_MEM64 (0x03000000)
545
546int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
547
548 int addrcell, sizecell, len, r;
549 u32 *dma_range;
550 /* sized based on pci addr cells, size-cells, & address-cells */
551 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
552
553 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
554 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
555
556 dma_range = &dma_ranges[0];
557 for (r = 0; r < hose->region_count; r++) {
558 u64 bus_start, phys_start, size;
559
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600560 /* skip if !PCI_REGION_SYS_MEMORY */
561 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500562 continue;
563
564 bus_start = (u64)hose->regions[r].bus_start;
565 phys_start = (u64)hose->regions[r].phys_start;
566 size = (u64)hose->regions[r].size;
567
568 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500569 if (size >= 0x100000000ull)
Kumar Galae0475cd2008-10-22 23:33:56 -0500570 dma_range[0] |= FDT_PCI_MEM64;
571 else
572 dma_range[0] |= FDT_PCI_MEM32;
573 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
574 dma_range[0] |= FDT_PCI_PREFETCH;
575#ifdef CONFIG_SYS_PCI_64BIT
576 dma_range[1] = bus_start >> 32;
577#else
578 dma_range[1] = 0;
579#endif
580 dma_range[2] = bus_start & 0xffffffff;
581
582 if (addrcell == 2) {
583 dma_range[3] = phys_start >> 32;
584 dma_range[4] = phys_start & 0xffffffff;
585 } else {
586 dma_range[3] = phys_start & 0xffffffff;
587 }
588
589 if (sizecell == 2) {
590 dma_range[3 + addrcell + 0] = size >> 32;
591 dma_range[3 + addrcell + 1] = size & 0xffffffff;
592 } else {
593 dma_range[3 + addrcell + 0] = size & 0xffffffff;
594 }
595
596 dma_range += (3 + addrcell + sizecell);
597 }
598
599 len = dma_range - &dma_ranges[0];
600 if (len)
601 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
602
603 return 0;
604}
605#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200606
607#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
608/*
Stefan Roese412a71a2010-09-16 14:01:53 +0200609 * Provide a weak default function to return the flash bank size.
610 * There might be multiple non-identical flash chips connected to one
611 * chip-select, so we need to pass an index as well.
612 */
613u32 __flash_get_bank_size(int cs, int idx)
614{
615 extern flash_info_t flash_info[];
616
617 /*
618 * As default, a simple 1:1 mapping is provided. Boards with
619 * a different mapping need to supply a board specific mapping
620 * routine.
621 */
622 return flash_info[cs].size;
623}
624u32 flash_get_bank_size(int cs, int idx)
625 __attribute__((weak, alias("__flash_get_bank_size")));
626
627/*
Stefan Roesea0f10c92009-10-21 11:59:52 +0200628 * This function can be used to update the size in the "reg" property
Stefan Roese412a71a2010-09-16 14:01:53 +0200629 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roesea0f10c92009-10-21 11:59:52 +0200630 * non-fixed NOR FLASH sizes.
631 */
Stefan Roese412a71a2010-09-16 14:01:53 +0200632int fdt_fixup_nor_flash_size(void *blob)
Stefan Roesea0f10c92009-10-21 11:59:52 +0200633{
634 char compat[][16] = { "cfi-flash", "jedec-flash" };
635 int off;
636 int len;
637 struct fdt_property *prop;
Stefan Roese442cd1a2010-09-24 13:51:50 +0200638 u32 *reg, *reg2;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200639 int i;
640
641 for (i = 0; i < 2; i++) {
642 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
643 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200644 int idx;
645
Stefan Roesea0f10c92009-10-21 11:59:52 +0200646 /*
Stefan Roese412a71a2010-09-16 14:01:53 +0200647 * Found one compatible node, so fixup the size
648 * int its reg properties
Stefan Roesea0f10c92009-10-21 11:59:52 +0200649 */
650 prop = fdt_get_property_w(blob, off, "reg", &len);
651 if (prop) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200652 int tuple_size = 3 * sizeof(reg);
653
654 /*
655 * There might be multiple reg-tuples,
656 * so loop through them all
657 */
Stefan Roese442cd1a2010-09-24 13:51:50 +0200658 reg = reg2 = (u32 *)&prop->data[0];
659 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200660 /*
661 * Update size in reg property
662 */
663 reg[2] = flash_get_bank_size(reg[0],
664 idx);
Stefan Roese442cd1a2010-09-24 13:51:50 +0200665
666 /*
667 * Point to next reg tuple
668 */
669 reg += 3;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200670 }
Stefan Roese442cd1a2010-09-24 13:51:50 +0200671
672 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roesea0f10c92009-10-21 11:59:52 +0200673 }
674
675 /* Move to next compatible node */
676 off = fdt_node_offset_by_compatible(blob, off,
677 compat[i]);
678 }
679 }
680
Stefan Roese412a71a2010-09-16 14:01:53 +0200681 return 0;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200682}
683#endif
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100684
Matthew McClintockacda3a42010-10-13 13:39:26 +0200685int fdt_increase_size(void *fdt, int add_len)
686{
687 int newlen;
688
689 newlen = fdt_totalsize(fdt) + add_len;
690
691 /* Open in place with a new len */
692 return fdt_open_into(fdt, fdt, newlen);
693}
694
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100695#ifdef CONFIG_FDT_FIXUP_PARTITIONS
696#include <jffs2/load_kernel.h>
697#include <mtd_node.h>
698
699struct reg_cell {
700 unsigned int r0;
701 unsigned int r1;
702};
703
704int fdt_del_subnodes(const void *blob, int parent_offset)
705{
706 int off, ndepth;
707 int ret;
708
709 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
710 (off >= 0) && (ndepth > 0);
711 off = fdt_next_node(blob, off, &ndepth)) {
712 if (ndepth == 1) {
713 debug("delete %s: offset: %x\n",
714 fdt_get_name(blob, off, 0), off);
715 ret = fdt_del_node((void *)blob, off);
716 if (ret < 0) {
717 printf("Can't delete node: %s\n",
718 fdt_strerror(ret));
719 return ret;
720 } else {
721 ndepth = 0;
722 off = parent_offset;
723 }
724 }
725 }
726 return 0;
727}
728
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100729int fdt_del_partitions(void *blob, int parent_offset)
730{
731 const void *prop;
732 int ndepth = 0;
733 int off;
734 int ret;
735
736 off = fdt_next_node(blob, parent_offset, &ndepth);
737 if (off > 0 && ndepth == 1) {
738 prop = fdt_getprop(blob, off, "label", NULL);
739 if (prop == NULL) {
740 /*
741 * Could not find label property, nand {}; node?
742 * Check subnode, delete partitions there if any.
743 */
744 return fdt_del_partitions(blob, off);
745 } else {
746 ret = fdt_del_subnodes(blob, parent_offset);
747 if (ret < 0) {
748 printf("Can't remove subnodes: %s\n",
749 fdt_strerror(ret));
750 return ret;
751 }
752 }
753 }
754 return 0;
755}
756
757int fdt_node_set_part_info(void *blob, int parent_offset,
758 struct mtd_device *dev)
759{
760 struct list_head *pentry;
761 struct part_info *part;
762 struct reg_cell cell;
763 int off, ndepth = 0;
764 int part_num, ret;
765 char buf[64];
766
767 ret = fdt_del_partitions(blob, parent_offset);
768 if (ret < 0)
769 return ret;
770
771 /*
772 * Check if it is nand {}; subnode, adjust
773 * the offset in this case
774 */
775 off = fdt_next_node(blob, parent_offset, &ndepth);
776 if (off > 0 && ndepth == 1)
777 parent_offset = off;
778
779 part_num = 0;
780 list_for_each_prev(pentry, &dev->parts) {
781 int newoff;
782
783 part = list_entry(pentry, struct part_info, link);
784
785 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
786 part_num, part->name, part->size,
787 part->offset, part->mask_flags);
788
789 sprintf(buf, "partition@%x", part->offset);
790add_sub:
791 ret = fdt_add_subnode(blob, parent_offset, buf);
792 if (ret == -FDT_ERR_NOSPACE) {
793 ret = fdt_increase_size(blob, 512);
794 if (!ret)
795 goto add_sub;
796 else
797 goto err_size;
798 } else if (ret < 0) {
799 printf("Can't add partition node: %s\n",
800 fdt_strerror(ret));
801 return ret;
802 }
803 newoff = ret;
804
805 /* Check MTD_WRITEABLE_CMD flag */
806 if (part->mask_flags & 1) {
807add_ro:
808 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
809 if (ret == -FDT_ERR_NOSPACE) {
810 ret = fdt_increase_size(blob, 512);
811 if (!ret)
812 goto add_ro;
813 else
814 goto err_size;
815 } else if (ret < 0)
816 goto err_prop;
817 }
818
819 cell.r0 = cpu_to_fdt32(part->offset);
820 cell.r1 = cpu_to_fdt32(part->size);
821add_reg:
822 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
823 if (ret == -FDT_ERR_NOSPACE) {
824 ret = fdt_increase_size(blob, 512);
825 if (!ret)
826 goto add_reg;
827 else
828 goto err_size;
829 } else if (ret < 0)
830 goto err_prop;
831
832add_label:
833 ret = fdt_setprop_string(blob, newoff, "label", part->name);
834 if (ret == -FDT_ERR_NOSPACE) {
835 ret = fdt_increase_size(blob, 512);
836 if (!ret)
837 goto add_label;
838 else
839 goto err_size;
840 } else if (ret < 0)
841 goto err_prop;
842
843 part_num++;
844 }
845 return 0;
846err_size:
847 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
848 return ret;
849err_prop:
850 printf("Can't add property: %s\n", fdt_strerror(ret));
851 return ret;
852}
853
854/*
855 * Update partitions in nor/nand nodes using info from
856 * mtdparts environment variable. The nodes to update are
857 * specified by node_info structure which contains mtd device
858 * type and compatible string: E. g. the board code in
859 * ft_board_setup() could use:
860 *
861 * struct node_info nodes[] = {
862 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
863 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
864 * };
865 *
866 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
867 */
868void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
869{
870 struct node_info *ni = node_info;
871 struct mtd_device *dev;
872 char *parts;
873 int i, idx;
874 int noff;
875
876 parts = getenv("mtdparts");
877 if (!parts)
878 return;
879
880 if (mtdparts_init() != 0)
881 return;
882
883 for (i = 0; i < node_info_size; i++) {
884 idx = 0;
885 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
886 while (noff != -FDT_ERR_NOTFOUND) {
887 debug("%s: %s, mtd dev type %d\n",
888 fdt_get_name(blob, noff, 0),
889 ni[i].compat, ni[i].type);
890 dev = device_find(ni[i].type, idx++);
891 if (dev) {
892 if (fdt_node_set_part_info(blob, noff, dev))
893 return; /* return on error */
894 }
895
896 /* Jump to next flash node */
897 noff = fdt_node_offset_by_compatible(blob, noff,
898 ni[i].compat);
899 }
900 }
901}
902#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -0500903
904void fdt_del_node_and_alias(void *blob, const char *alias)
905{
906 int off = fdt_path_offset(blob, alias);
907
908 if (off < 0)
909 return;
910
911 fdt_del_node(blob, off);
912
913 off = fdt_path_offset(blob, "/aliases");
914 fdt_delprop(blob, off, alias);
915}
Kumar Galab87e7932010-06-16 14:27:38 -0500916
917/* Helper to read a big number; size is in cells (not bytes) */
Kim Phillips6542c072013-01-16 14:00:11 +0000918static inline u64 of_read_number(const fdt32_t *cell, int size)
Kumar Galab87e7932010-06-16 14:27:38 -0500919{
920 u64 r = 0;
921 while (size--)
Kim Phillips6542c072013-01-16 14:00:11 +0000922 r = (r << 32) | fdt32_to_cpu(*(cell++));
Kumar Galab87e7932010-06-16 14:27:38 -0500923 return r;
924}
925
Kumar Galab87e7932010-06-16 14:27:38 -0500926#define PRu64 "%llx"
927
928/* Max address size we deal with */
929#define OF_MAX_ADDR_CELLS 4
930#define OF_BAD_ADDR ((u64)-1)
931#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
932 (ns) > 0)
933
934/* Debug utility */
935#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +0000936static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -0500937{
938 printf("%s", s);
939 while(na--)
940 printf(" %08x", *(addr++));
941 printf("\n");
942}
943#else
Kim Phillips6542c072013-01-16 14:00:11 +0000944static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -0500945#endif
946
947/* Callbacks for bus specific translators */
948struct of_bus {
949 const char *name;
950 const char *addresses;
Scott Wood99899712010-08-12 18:37:39 -0500951 void (*count_cells)(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500952 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +0000953 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500954 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +0000955 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -0500956};
957
958/* Default translator (generic bus) */
Scott Wood99899712010-08-12 18:37:39 -0500959static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500960 int *addrc, int *sizec)
961{
Kim Phillips6542c072013-01-16 14:00:11 +0000962 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -0500963
964 if (addrc) {
965 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
966 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000967 *addrc = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500968 else
969 *addrc = 2;
970 }
971
972 if (sizec) {
973 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
974 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +0000975 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -0500976 else
977 *sizec = 1;
978 }
Kumar Galab87e7932010-06-16 14:27:38 -0500979}
980
Kim Phillips6542c072013-01-16 14:00:11 +0000981static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -0500982 int na, int ns, int pna)
983{
984 u64 cp, s, da;
985
986 cp = of_read_number(range, na);
987 s = of_read_number(range + na + pna, ns);
988 da = of_read_number(addr, na);
989
990 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
991 cp, s, da);
992
993 if (da < cp || da >= (cp + s))
994 return OF_BAD_ADDR;
995 return da - cp;
996}
997
Kim Phillips6542c072013-01-16 14:00:11 +0000998static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -0500999{
1000 u64 a = of_read_number(addr, na);
1001 memset(addr, 0, na * 4);
1002 a += offset;
1003 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001004 addr[na - 2] = cpu_to_fdt32(a >> 32);
1005 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001006
1007 return 0;
1008}
1009
1010/* Array of bus specific translators */
1011static struct of_bus of_busses[] = {
1012 /* Default */
1013 {
1014 .name = "default",
1015 .addresses = "reg",
1016 .count_cells = of_bus_default_count_cells,
1017 .map = of_bus_default_map,
1018 .translate = of_bus_default_translate,
1019 },
1020};
1021
1022static int of_translate_one(void * blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001023 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001024 int na, int ns, int pna, const char *rprop)
1025{
Kim Phillips6542c072013-01-16 14:00:11 +00001026 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001027 int rlen;
1028 int rone;
1029 u64 offset = OF_BAD_ADDR;
1030
1031 /* Normally, an absence of a "ranges" property means we are
1032 * crossing a non-translatable boundary, and thus the addresses
1033 * below the current not cannot be converted to CPU physical ones.
1034 * Unfortunately, while this is very clear in the spec, it's not
1035 * what Apple understood, and they do have things like /uni-n or
1036 * /ht nodes with no "ranges" property and a lot of perfectly
1037 * useable mapped devices below them. Thus we treat the absence of
1038 * "ranges" as equivalent to an empty "ranges" property which means
1039 * a 1:1 translation at that level. It's up to the caller not to try
1040 * to translate addresses that aren't supposed to be translated in
1041 * the first place. --BenH.
1042 */
Kim Phillips6542c072013-01-16 14:00:11 +00001043 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001044 if (ranges == NULL || rlen == 0) {
1045 offset = of_read_number(addr, na);
1046 memset(addr, 0, pna * 4);
1047 debug("OF: no ranges, 1:1 translation\n");
1048 goto finish;
1049 }
1050
1051 debug("OF: walking ranges...\n");
1052
1053 /* Now walk through the ranges */
1054 rlen /= 4;
1055 rone = na + pna + ns;
1056 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1057 offset = bus->map(addr, ranges, na, ns, pna);
1058 if (offset != OF_BAD_ADDR)
1059 break;
1060 }
1061 if (offset == OF_BAD_ADDR) {
1062 debug("OF: not found !\n");
1063 return 1;
1064 }
1065 memcpy(addr, ranges + na, 4 * pna);
1066
1067 finish:
1068 of_dump_addr("OF: parent translation for:", addr, pna);
1069 debug("OF: with offset: "PRu64"\n", offset);
1070
1071 /* Translate it into parent bus space */
1072 return pbus->translate(addr, offset, pna);
1073}
1074
1075/*
1076 * Translate an address from the device-tree into a CPU physical address,
1077 * this walks up the tree and applies the various bus mappings on the
1078 * way.
1079 *
1080 * Note: We consider that crossing any level with #size-cells == 0 to mean
1081 * that translation is impossible (that is we are not dealing with a value
1082 * that can be mapped to a cpu physical address). This is not really specified
1083 * that way, but this is traditionally the way IBM at least do things
1084 */
Kim Phillips6542c072013-01-16 14:00:11 +00001085static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1086 const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001087{
1088 int parent;
1089 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001090 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001091 int na, ns, pna, pns;
1092 u64 result = OF_BAD_ADDR;
1093
1094 debug("OF: ** translation for device %s **\n",
1095 fdt_get_name(blob, node_offset, NULL));
1096
1097 /* Get parent & match bus type */
1098 parent = fdt_parent_offset(blob, node_offset);
1099 if (parent < 0)
1100 goto bail;
1101 bus = &of_busses[0];
1102
1103 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001104 bus->count_cells(blob, parent, &na, &ns);
Kumar Galab87e7932010-06-16 14:27:38 -05001105 if (!OF_CHECK_COUNTS(na, ns)) {
1106 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1107 fdt_get_name(blob, node_offset, NULL));
1108 goto bail;
1109 }
1110 memcpy(addr, in_addr, na * 4);
1111
1112 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1113 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1114 of_dump_addr("OF: translating address:", addr, na);
1115
1116 /* Translate */
1117 for (;;) {
1118 /* Switch to parent bus */
1119 node_offset = parent;
1120 parent = fdt_parent_offset(blob, node_offset);
1121
1122 /* If root, we have finished */
1123 if (parent < 0) {
1124 debug("OF: reached root node\n");
1125 result = of_read_number(addr, na);
1126 break;
1127 }
1128
1129 /* Get new parent bus and counts */
1130 pbus = &of_busses[0];
Scott Wood99899712010-08-12 18:37:39 -05001131 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galab87e7932010-06-16 14:27:38 -05001132 if (!OF_CHECK_COUNTS(pna, pns)) {
1133 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1134 fdt_get_name(blob, node_offset, NULL));
1135 break;
1136 }
1137
1138 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1139 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1140
1141 /* Apply bus translation */
1142 if (of_translate_one(blob, node_offset, bus, pbus,
1143 addr, na, ns, pna, rprop))
1144 break;
1145
1146 /* Complete the move up one level */
1147 na = pna;
1148 ns = pns;
1149 bus = pbus;
1150
1151 of_dump_addr("OF: one level translation:", addr, na);
1152 }
1153 bail:
1154
1155 return result;
1156}
1157
Kim Phillips6542c072013-01-16 14:00:11 +00001158u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001159{
1160 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1161}
Kumar Gala800d1d12010-07-04 12:48:21 -05001162
1163/**
1164 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1165 * who's reg property matches a physical cpu address
1166 *
1167 * @blob: ptr to device tree
1168 * @compat: compatiable string to match
1169 * @compat_off: property name
1170 *
1171 */
1172int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1173 phys_addr_t compat_off)
1174{
1175 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1176 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +00001177 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala800d1d12010-07-04 12:48:21 -05001178 if (reg) {
1179 if (compat_off == fdt_translate_address(blob, off, reg))
1180 return off;
1181 }
1182 off = fdt_node_offset_by_compatible(blob, off, compat);
1183 }
1184
1185 return -FDT_ERR_NOTFOUND;
1186}
1187
Kumar Galab7afb112010-07-09 16:18:58 -05001188/**
1189 * fdt_alloc_phandle: Return next free phandle value
1190 *
1191 * @blob: ptr to device tree
1192 */
1193int fdt_alloc_phandle(void *blob)
1194{
Timur Tabic6d83032011-09-20 18:24:35 -05001195 int offset, phandle = 0;
Kumar Galab7afb112010-07-09 16:18:58 -05001196
1197 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1198 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabic6d83032011-09-20 18:24:35 -05001199 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab7afb112010-07-09 16:18:58 -05001200 }
Kumar Gala800d1d12010-07-04 12:48:21 -05001201
Kumar Galab7afb112010-07-09 16:18:58 -05001202 return phandle + 1;
1203}
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001204
Gerald Van Barenaf737882011-07-14 21:40:10 -04001205/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001206 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001207 *
1208 * @fdt: ptr to device tree
1209 * @nodeoffset: node to update
1210 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001211 */
1212int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001213{
1214 int ret;
1215
1216#ifdef DEBUG
1217 int off = fdt_node_offset_by_phandle(fdt, phandle);
1218
1219 if ((off >= 0) && (off != nodeoffset)) {
1220 char buf[64];
1221
1222 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1223 printf("Trying to update node %s with phandle %u ",
1224 buf, phandle);
1225
1226 fdt_get_path(fdt, off, buf, sizeof(buf));
1227 printf("that already exists in node %s.\n", buf);
1228 return -FDT_ERR_BADPHANDLE;
1229 }
1230#endif
1231
1232 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1233 if (ret < 0)
1234 return ret;
1235
1236 /*
1237 * For now, also set the deprecated "linux,phandle" property, so that we
1238 * don't break older kernels.
1239 */
1240 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1241
1242 return ret;
1243}
1244
Kumar Galad5fc2582011-08-01 00:25:20 -05001245/*
1246 * fdt_create_phandle: Create a phandle property for the given node
1247 *
1248 * @fdt: ptr to device tree
1249 * @nodeoffset: node to update
1250 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001251unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001252{
1253 /* see if there is a phandle already */
1254 int phandle = fdt_get_phandle(fdt, nodeoffset);
1255
1256 /* if we got 0, means no phandle so create one */
1257 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001258 int ret;
1259
Kumar Galad5fc2582011-08-01 00:25:20 -05001260 phandle = fdt_alloc_phandle(fdt);
Timur Tabi0f46c802011-09-20 18:24:34 -05001261 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1262 if (ret < 0) {
1263 printf("Can't set phandle %u: %s\n", phandle,
1264 fdt_strerror(ret));
1265 return 0;
1266 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001267 }
1268
1269 return phandle;
1270}
1271
Shengzhou Liua7be8022011-10-14 16:26:05 +08001272/*
1273 * fdt_set_node_status: Set status for the given node
1274 *
1275 * @fdt: ptr to device tree
1276 * @nodeoffset: node to update
1277 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1278 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1279 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1280 */
1281int fdt_set_node_status(void *fdt, int nodeoffset,
1282 enum fdt_status status, unsigned int error_code)
1283{
1284 char buf[16];
1285 int ret = 0;
1286
1287 if (nodeoffset < 0)
1288 return nodeoffset;
1289
1290 switch (status) {
1291 case FDT_STATUS_OKAY:
1292 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1293 break;
1294 case FDT_STATUS_DISABLED:
1295 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1296 break;
1297 case FDT_STATUS_FAIL:
1298 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1299 break;
1300 case FDT_STATUS_FAIL_ERROR_CODE:
1301 sprintf(buf, "fail-%d", error_code);
1302 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1303 break;
1304 default:
1305 printf("Invalid fdt status: %x\n", status);
1306 ret = -1;
1307 break;
1308 }
1309
1310 return ret;
1311}
1312
1313/*
1314 * fdt_set_status_by_alias: Set status for the given node given an alias
1315 *
1316 * @fdt: ptr to device tree
1317 * @alias: alias of node to update
1318 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1319 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1320 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1321 */
1322int fdt_set_status_by_alias(void *fdt, const char* alias,
1323 enum fdt_status status, unsigned int error_code)
1324{
1325 int offset = fdt_path_offset(fdt, alias);
1326
1327 return fdt_set_node_status(fdt, offset, status, error_code);
1328}
1329
Tom Wai-Hong Tamdb4a88d2012-12-05 14:46:41 +00001330#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001331int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1332{
1333 int noff;
1334 int ret;
1335
1336 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1337 if (noff != -FDT_ERR_NOTFOUND) {
1338 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1339add_edid:
1340 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1341 if (ret == -FDT_ERR_NOSPACE) {
1342 ret = fdt_increase_size(blob, 512);
1343 if (!ret)
1344 goto add_edid;
1345 else
1346 goto err_size;
1347 } else if (ret < 0) {
1348 printf("Can't add property: %s\n", fdt_strerror(ret));
1349 return ret;
1350 }
1351 }
1352 return 0;
1353err_size:
1354 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1355 return ret;
1356}
1357#endif
Timur Tabi8ebaf202011-05-03 13:24:07 -05001358
1359/*
1360 * Verify the physical address of device tree node for a given alias
1361 *
1362 * This function locates the device tree node of a given alias, and then
1363 * verifies that the physical address of that device matches the given
1364 * parameter. It displays a message if there is a mismatch.
1365 *
1366 * Returns 1 on success, 0 on failure
1367 */
1368int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1369{
1370 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001371 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001372 int node, len;
1373 u64 dt_addr;
1374
1375 path = fdt_getprop(fdt, anode, alias, NULL);
1376 if (!path) {
1377 /* If there's no such alias, then it's not a failure */
1378 return 1;
1379 }
1380
1381 node = fdt_path_offset(fdt, path);
1382 if (node < 0) {
1383 printf("Warning: device tree alias '%s' points to invalid "
1384 "node %s.\n", alias, path);
1385 return 0;
1386 }
1387
1388 reg = fdt_getprop(fdt, node, "reg", &len);
1389 if (!reg) {
1390 printf("Warning: device tree node '%s' has no address.\n",
1391 path);
1392 return 0;
1393 }
1394
1395 dt_addr = fdt_translate_address(fdt, node, reg);
1396 if (addr != dt_addr) {
1397 printf("Warning: U-Boot configured device %s at address %llx,\n"
1398 " but the device tree has it address %llx.\n",
1399 alias, addr, dt_addr);
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
1406/*
1407 * Returns the base address of an SOC or PCI node
1408 */
1409u64 fdt_get_base_address(void *fdt, int node)
1410{
1411 int size;
1412 u32 naddr;
Kim Phillips6542c072013-01-16 14:00:11 +00001413 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001414
1415 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1416 if (prop && size == 4)
Kim Phillips6542c072013-01-16 14:00:11 +00001417 naddr = be32_to_cpup(prop);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001418 else
1419 naddr = 2;
1420
1421 prop = fdt_getprop(fdt, node, "ranges", &size);
1422
1423 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1424}