blob: 6b9fa0550f18e5694cd9f1e83911c52c089920d8 [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>
31#include <fdt.h>
32#include <libfdt.h>
33#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060034#include <exports.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040035
36/*
37 * Global data (for the gd->bd)
38 */
39DECLARE_GLOBAL_DATA_PTR;
40
Kumar Gala1ba00402008-10-23 00:05:47 -050041/**
42 * fdt_getprop_u32_default - Find a node and return it's property or a default
43 *
44 * @fdt: ptr to device tree
45 * @path: path of node
46 * @prop: property name
47 * @dflt: default value if the property isn't found
48 *
49 * Convenience function to find a node and return it's property or a
50 * default value if it doesn't exist.
51 */
Gabe Blackcd37de72011-11-08 01:09:44 -080052u32 fdt_getprop_u32_default(const void *fdt, const char *path,
53 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050054{
55 const u32 *val;
56 int off;
57
58 off = fdt_path_offset(fdt, path);
59 if (off < 0)
60 return dflt;
61
62 val = fdt_getprop(fdt, off, prop, NULL);
63 if (val)
Gabe Black9df8b4c2011-11-08 01:05:32 -080064 return fdt32_to_cpu(*val);
Kumar Gala1ba00402008-10-23 00:05:47 -050065 else
66 return dflt;
67}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040068
Kumar Galabf771dc2007-10-24 10:21:57 -050069/**
70 * fdt_find_and_setprop: Find a node and set it's property
71 *
72 * @fdt: ptr to device tree
73 * @node: path of node
74 * @prop: property name
75 * @val: ptr to new value
76 * @len: length of new property value
77 * @create: flag to create the property if it doesn't exist
78 *
79 * Convenience function to directly set a property given the path to the node.
80 */
81int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
82 const void *val, int len, int create)
83{
Kumar Galac8ab7052007-10-24 11:04:22 -050084 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050085
86 if (nodeoff < 0)
87 return nodeoff;
88
89 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
90 return 0; /* create flag not set; so exit quietly */
91
92 return fdt_setprop(fdt, nodeoff, prop, val, len);
93}
94
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060095#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040096
Marek Vasut52039da2012-09-14 23:45:51 +020097#ifdef CONFIG_CONS_INDEX
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040098static void fdt_fill_multisername(char *sername, size_t maxlen)
99{
100 const char *outname = stdio_devices[stdout]->name;
101
102 if (strcmp(outname, "serial") > 0)
103 strncpy(sername, outname, maxlen);
104
105 /* eserial? */
106 if (strcmp(outname + 1, "serial") > 0)
107 strncpy(sername, outname + 1, maxlen);
108}
Marek Vasut52039da2012-09-14 23:45:51 +0200109#endif
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400110
Detlev Zundel46e192a2008-06-20 22:24:05 +0200111static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600112{
113 int err = 0;
114#ifdef CONFIG_CONS_INDEX
115 int node;
116 char sername[9] = { 0 };
117 const char *path;
118
Anton Vorontsov9e9cf602009-10-15 17:47:04 +0400119 fdt_fill_multisername(sername, sizeof(sername) - 1);
120 if (!sername[0])
121 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600122
123 err = node = fdt_path_offset(fdt, "/aliases");
124 if (node >= 0) {
125 int len;
126 path = fdt_getprop(fdt, node, sername, &len);
127 if (path) {
128 char *p = malloc(len);
129 err = -FDT_ERR_NOSPACE;
130 if (p) {
131 memcpy(p, path, len);
Detlev Zundel46e192a2008-06-20 22:24:05 +0200132 err = fdt_setprop(fdt, chosenoff,
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600133 "linux,stdout-path", p, len);
134 free(p);
135 }
136 } else {
137 err = len;
138 }
139 }
140#endif
141 if (err < 0)
142 printf("WARNING: could not set linux,stdout-path %s.\n",
143 fdt_strerror(err));
144
145 return err;
146}
147#endif
148
Kumar Gala99ebc052008-08-15 08:24:43 -0500149int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400150{
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400151 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500152 int err, j, total;
153 u32 tmp;
Gerald Van Baren569c0352007-12-29 22:45:27 -0500154 const char *path;
Kumar Gala99ebc052008-08-15 08:24:43 -0500155 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400156
Kumar Gala99ebc052008-08-15 08:24:43 -0500157 /* Find the "chosen" node. */
158 nodeoffset = fdt_path_offset (fdt, "/chosen");
159
160 /* If there is no "chosen" node in the blob return */
161 if (nodeoffset < 0) {
162 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
163 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400164 }
165
Kumar Gala99ebc052008-08-15 08:24:43 -0500166 /* just return if initrd_start/end aren't valid */
167 if ((initrd_start == 0) || (initrd_end == 0))
168 return 0;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400169
Kumar Gala99ebc052008-08-15 08:24:43 -0500170 total = fdt_num_mem_rsv(fdt);
171
172 /*
173 * Look for an existing entry and update it. If we don't find
174 * the entry, we will j be the next available slot.
175 */
176 for (j = 0; j < total; j++) {
177 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
178 if (addr == initrd_start) {
179 fdt_del_mem_rsv(fdt, j);
180 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400181 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500182 }
183
Grant Likelyc379a562011-03-28 09:58:55 +0000184 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500185 if (err < 0) {
186 printf("fdt_initrd: %s\n", fdt_strerror(err));
187 return err;
188 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500189
Kumar Gala99ebc052008-08-15 08:24:43 -0500190 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
191 if ((path == NULL) || force) {
192 tmp = __cpu_to_be32(initrd_start);
193 err = fdt_setprop(fdt, nodeoffset,
194 "linux,initrd-start", &tmp, sizeof(tmp));
195 if (err < 0) {
196 printf("WARNING: "
197 "could not set linux,initrd-start %s.\n",
198 fdt_strerror(err));
199 return err;
200 }
201 tmp = __cpu_to_be32(initrd_end);
202 err = fdt_setprop(fdt, nodeoffset,
203 "linux,initrd-end", &tmp, sizeof(tmp));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400204 if (err < 0) {
Kumar Gala99ebc052008-08-15 08:24:43 -0500205 printf("WARNING: could not set linux,initrd-end %s.\n",
206 fdt_strerror(err));
207
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400208 return err;
209 }
210 }
211
Kumar Gala99ebc052008-08-15 08:24:43 -0500212 return 0;
213}
214
Heiko Schocherd29abae2008-09-11 08:11:23 +0200215int fdt_chosen(void *fdt, int force)
Kumar Gala99ebc052008-08-15 08:24:43 -0500216{
217 int nodeoffset;
218 int err;
219 char *str; /* used to set string properties */
220 const char *path;
221
222 err = fdt_check_header(fdt);
223 if (err < 0) {
224 printf("fdt_chosen: %s\n", fdt_strerror(err));
225 return err;
226 }
227
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400228 /*
229 * Find the "chosen" node.
230 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500231 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400232
233 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500234 * If there is no "chosen" node in the blob, create it.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400235 */
236 if (nodeoffset < 0) {
237 /*
238 * Create a new node "/chosen" (offset 0 is root level)
239 */
240 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
241 if (nodeoffset < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400242 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400243 fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400244 return nodeoffset;
245 }
246 }
247
248 /*
Gerald Van Baren569c0352007-12-29 22:45:27 -0500249 * Create /chosen properites that don't exist in the fdt.
250 * If the property exists, update it only if the "force" parameter
251 * is true.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400252 */
253 str = getenv("bootargs");
254 if (str != NULL) {
Gerald Van Baren569c0352007-12-29 22:45:27 -0500255 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
256 if ((path == NULL) || force) {
257 err = fdt_setprop(fdt, nodeoffset,
258 "bootargs", str, strlen(str)+1);
259 if (err < 0)
260 printf("WARNING: could not set bootargs %s.\n",
261 fdt_strerror(err));
262 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400263 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600264
265#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Baren569c0352007-12-29 22:45:27 -0500266 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
267 if ((path == NULL) || force)
268 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600269#endif
270
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400271#ifdef OF_STDOUT_PATH
Gerald Van Baren569c0352007-12-29 22:45:27 -0500272 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
273 if ((path == NULL) || force) {
274 err = fdt_setprop(fdt, nodeoffset,
275 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
276 if (err < 0)
277 printf("WARNING: could not set linux,stdout-path %s.\n",
278 fdt_strerror(err));
279 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400280#endif
281
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400282 return err;
283}
284
Kumar Gala7e64cf82007-11-03 19:46:28 -0500285void do_fixup_by_path(void *fdt, const char *path, const char *prop,
286 const void *val, int len, int create)
287{
288#if defined(DEBUG)
289 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600290 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500291 for (i = 0; i < len; i++)
292 debug(" %.2x", *(u8*)(val+i));
293 debug("\n");
294#endif
295 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
296 if (rc)
297 printf("Unable to update property %s:%s, err=%s\n",
298 path, prop, fdt_strerror(rc));
299}
300
301void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
302 u32 val, int create)
303{
304 val = cpu_to_fdt32(val);
305 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
306}
307
Kumar Gala7590a192007-11-21 13:30:15 -0600308void do_fixup_by_prop(void *fdt,
309 const char *pname, const void *pval, int plen,
310 const char *prop, const void *val, int len,
311 int create)
312{
313 int off;
314#if defined(DEBUG)
315 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600316 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600317 for (i = 0; i < len; i++)
318 debug(" %.2x", *(u8*)(val+i));
319 debug("\n");
320#endif
321 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
322 while (off != -FDT_ERR_NOTFOUND) {
323 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
324 fdt_setprop(fdt, off, prop, val, len);
325 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
326 }
327}
328
329void do_fixup_by_prop_u32(void *fdt,
330 const char *pname, const void *pval, int plen,
331 const char *prop, u32 val, int create)
332{
333 val = cpu_to_fdt32(val);
334 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
335}
336
337void do_fixup_by_compat(void *fdt, const char *compat,
338 const char *prop, const void *val, int len, int create)
339{
340 int off = -1;
341#if defined(DEBUG)
342 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600343 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600344 for (i = 0; i < len; i++)
345 debug(" %.2x", *(u8*)(val+i));
346 debug("\n");
347#endif
348 off = fdt_node_offset_by_compatible(fdt, -1, compat);
349 while (off != -FDT_ERR_NOTFOUND) {
350 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
351 fdt_setprop(fdt, off, prop, val, len);
352 off = fdt_node_offset_by_compatible(fdt, off, compat);
353 }
354}
355
356void do_fixup_by_compat_u32(void *fdt, const char *compat,
357 const char *prop, u32 val, int create)
358{
359 val = cpu_to_fdt32(val);
360 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
361}
362
John Rigbyb4ae9422010-10-13 13:57:33 -0600363/*
364 * Get cells len in bytes
365 * if #NNNN-cells property is 2 then len is 8
366 * otherwise len is 4
367 */
368static int get_cells_len(void *blob, char *nr_cells_name)
Kumar Gala0db72ed2007-11-26 14:57:45 -0600369{
John Rigbyb4ae9422010-10-13 13:57:33 -0600370 const u32 *cell;
371
372 cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
Gabe Black9df8b4c2011-11-08 01:05:32 -0800373 if (cell && fdt32_to_cpu(*cell) == 2)
John Rigbyb4ae9422010-10-13 13:57:33 -0600374 return 8;
375
376 return 4;
377}
378
379/*
380 * Write a 4 or 8 byte big endian cell
381 */
382static void write_cell(u8 *addr, u64 val, int size)
383{
384 int shift = (size - 1) * 8;
385 while (size-- > 0) {
386 *addr++ = (val >> shift) & 0xff;
387 shift -= 8;
388 }
389}
390
391int 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;
Kyle Moffett215d8322011-02-23 06:18:24 +0000395 u8 tmp[banks * 16]; /* Up to 64-bit address + 64-bit size */
John Rigbyb4ae9422010-10-13 13:57:33 -0600396 int bank;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600397
398 err = fdt_check_header(blob);
399 if (err < 0) {
400 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
401 return err;
402 }
403
404 /* update, or add and update /memory node */
405 nodeoffset = fdt_path_offset(blob, "/memory");
406 if (nodeoffset < 0) {
407 nodeoffset = fdt_add_subnode(blob, 0, "memory");
408 if (nodeoffset < 0)
409 printf("WARNING: could not create /memory: %s.\n",
410 fdt_strerror(nodeoffset));
411 return nodeoffset;
412 }
413 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
414 sizeof("memory"));
415 if (err < 0) {
416 printf("WARNING: could not set %s %s.\n", "device_type",
417 fdt_strerror(err));
418 return err;
419 }
420
John Rigbyb4ae9422010-10-13 13:57:33 -0600421 addr_cell_len = get_cells_len(blob, "#address-cells");
422 size_cell_len = get_cells_len(blob, "#size-cells");
423
424 for (bank = 0, len = 0; bank < banks; bank++) {
425 write_cell(tmp + len, start[bank], addr_cell_len);
426 len += addr_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600427
John Rigbyb4ae9422010-10-13 13:57:33 -0600428 write_cell(tmp + len, size[bank], size_cell_len);
429 len += size_cell_len;
Kumar Gala0db72ed2007-11-26 14:57:45 -0600430 }
431
432 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
433 if (err < 0) {
434 printf("WARNING: could not set %s %s.\n",
435 "reg", fdt_strerror(err));
436 return err;
437 }
438 return 0;
439}
440
John Rigbyb4ae9422010-10-13 13:57:33 -0600441int fdt_fixup_memory(void *blob, u64 start, u64 size)
442{
443 return fdt_fixup_memory_banks(blob, &start, &size, 1);
444}
445
Kumar Galafabda922008-08-19 15:41:18 -0500446void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600447{
Kumar Galafabda922008-08-19 15:41:18 -0500448 int node, i, j;
449 char enet[16], *tmp, *end;
450 char mac[16] = "ethaddr";
Kumar Gala22699102007-11-21 11:11:03 -0600451 const char *path;
Kumar Galafabda922008-08-19 15:41:18 -0500452 unsigned char mac_addr[6];
Kumar Gala22699102007-11-21 11:11:03 -0600453
454 node = fdt_path_offset(fdt, "/aliases");
Kumar Galafabda922008-08-19 15:41:18 -0500455 if (node < 0)
456 return;
457
458 i = 0;
459 while ((tmp = getenv(mac)) != NULL) {
460 sprintf(enet, "ethernet%d", i);
461 path = fdt_getprop(fdt, node, enet, NULL);
462 if (!path) {
463 debug("No alias for %s\n", enet);
464 sprintf(mac, "eth%daddr", ++i);
465 continue;
Kumar Gala22699102007-11-21 11:11:03 -0600466 }
Kumar Galafabda922008-08-19 15:41:18 -0500467
468 for (j = 0; j < 6; j++) {
469 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
470 if (tmp)
471 tmp = (*end) ? end+1 : end;
Kumar Gala22699102007-11-21 11:11:03 -0600472 }
Kumar Galafabda922008-08-19 15:41:18 -0500473
474 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
475 do_fixup_by_path(fdt, path, "local-mac-address",
476 &mac_addr, 6, 1);
477
478 sprintf(mac, "eth%daddr", ++i);
Kumar Gala22699102007-11-21 11:11:03 -0600479 }
480}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300481
Kumar Galaf2f58c52008-08-15 08:24:42 -0500482/* Resize the fdt to its actual size + a bit of padding */
483int fdt_resize(void *blob)
484{
485 int i;
486 uint64_t addr, size;
487 int total, ret;
488 uint actualsize;
489
490 if (!blob)
491 return 0;
492
493 total = fdt_num_mem_rsv(blob);
494 for (i = 0; i < total; i++) {
495 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000496 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500497 fdt_del_mem_rsv(blob, i);
498 break;
499 }
500 }
501
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100502 /*
503 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200504 * plus the size needed for 5 fdt_add_mem_rsv, one
505 * for the fdt itself and 4 for a possible initrd
506 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100507 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500508 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200509 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500510
511 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000512 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
513 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500514
515 /* Change the fdt header to reflect the correct size */
516 fdt_set_totalsize(blob, actualsize);
517
518 /* Add the new reservation */
Simon Glassfc953242011-09-17 06:48:58 +0000519 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500520 if (ret < 0)
521 return ret;
522
523 return actualsize;
524}
Kumar Galae0475cd2008-10-22 23:33:56 -0500525
526#ifdef CONFIG_PCI
Kumar Galabaf41892009-08-05 09:03:54 -0500527#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500528
529#define FDT_PCI_PREFETCH (0x40000000)
530#define FDT_PCI_MEM32 (0x02000000)
531#define FDT_PCI_IO (0x01000000)
532#define FDT_PCI_MEM64 (0x03000000)
533
534int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
535
536 int addrcell, sizecell, len, r;
537 u32 *dma_range;
538 /* sized based on pci addr cells, size-cells, & address-cells */
539 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
540
541 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
542 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
543
544 dma_range = &dma_ranges[0];
545 for (r = 0; r < hose->region_count; r++) {
546 u64 bus_start, phys_start, size;
547
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600548 /* skip if !PCI_REGION_SYS_MEMORY */
549 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500550 continue;
551
552 bus_start = (u64)hose->regions[r].bus_start;
553 phys_start = (u64)hose->regions[r].phys_start;
554 size = (u64)hose->regions[r].size;
555
556 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500557 if (size >= 0x100000000ull)
Kumar Galae0475cd2008-10-22 23:33:56 -0500558 dma_range[0] |= FDT_PCI_MEM64;
559 else
560 dma_range[0] |= FDT_PCI_MEM32;
561 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
562 dma_range[0] |= FDT_PCI_PREFETCH;
563#ifdef CONFIG_SYS_PCI_64BIT
564 dma_range[1] = bus_start >> 32;
565#else
566 dma_range[1] = 0;
567#endif
568 dma_range[2] = bus_start & 0xffffffff;
569
570 if (addrcell == 2) {
571 dma_range[3] = phys_start >> 32;
572 dma_range[4] = phys_start & 0xffffffff;
573 } else {
574 dma_range[3] = phys_start & 0xffffffff;
575 }
576
577 if (sizecell == 2) {
578 dma_range[3 + addrcell + 0] = size >> 32;
579 dma_range[3 + addrcell + 1] = size & 0xffffffff;
580 } else {
581 dma_range[3 + addrcell + 0] = size & 0xffffffff;
582 }
583
584 dma_range += (3 + addrcell + sizecell);
585 }
586
587 len = dma_range - &dma_ranges[0];
588 if (len)
589 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
590
591 return 0;
592}
593#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200594
595#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
596/*
Stefan Roese412a71a2010-09-16 14:01:53 +0200597 * Provide a weak default function to return the flash bank size.
598 * There might be multiple non-identical flash chips connected to one
599 * chip-select, so we need to pass an index as well.
600 */
601u32 __flash_get_bank_size(int cs, int idx)
602{
603 extern flash_info_t flash_info[];
604
605 /*
606 * As default, a simple 1:1 mapping is provided. Boards with
607 * a different mapping need to supply a board specific mapping
608 * routine.
609 */
610 return flash_info[cs].size;
611}
612u32 flash_get_bank_size(int cs, int idx)
613 __attribute__((weak, alias("__flash_get_bank_size")));
614
615/*
Stefan Roesea0f10c92009-10-21 11:59:52 +0200616 * This function can be used to update the size in the "reg" property
Stefan Roese412a71a2010-09-16 14:01:53 +0200617 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roesea0f10c92009-10-21 11:59:52 +0200618 * non-fixed NOR FLASH sizes.
619 */
Stefan Roese412a71a2010-09-16 14:01:53 +0200620int fdt_fixup_nor_flash_size(void *blob)
Stefan Roesea0f10c92009-10-21 11:59:52 +0200621{
622 char compat[][16] = { "cfi-flash", "jedec-flash" };
623 int off;
624 int len;
625 struct fdt_property *prop;
Stefan Roese442cd1a2010-09-24 13:51:50 +0200626 u32 *reg, *reg2;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200627 int i;
628
629 for (i = 0; i < 2; i++) {
630 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
631 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200632 int idx;
633
Stefan Roesea0f10c92009-10-21 11:59:52 +0200634 /*
Stefan Roese412a71a2010-09-16 14:01:53 +0200635 * Found one compatible node, so fixup the size
636 * int its reg properties
Stefan Roesea0f10c92009-10-21 11:59:52 +0200637 */
638 prop = fdt_get_property_w(blob, off, "reg", &len);
639 if (prop) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200640 int tuple_size = 3 * sizeof(reg);
641
642 /*
643 * There might be multiple reg-tuples,
644 * so loop through them all
645 */
Stefan Roese442cd1a2010-09-24 13:51:50 +0200646 reg = reg2 = (u32 *)&prop->data[0];
647 for (idx = 0; idx < (len / tuple_size); idx++) {
Stefan Roese412a71a2010-09-16 14:01:53 +0200648 /*
649 * Update size in reg property
650 */
651 reg[2] = flash_get_bank_size(reg[0],
652 idx);
Stefan Roese442cd1a2010-09-24 13:51:50 +0200653
654 /*
655 * Point to next reg tuple
656 */
657 reg += 3;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200658 }
Stefan Roese442cd1a2010-09-24 13:51:50 +0200659
660 fdt_setprop(blob, off, "reg", reg2, len);
Stefan Roesea0f10c92009-10-21 11:59:52 +0200661 }
662
663 /* Move to next compatible node */
664 off = fdt_node_offset_by_compatible(blob, off,
665 compat[i]);
666 }
667 }
668
Stefan Roese412a71a2010-09-16 14:01:53 +0200669 return 0;
Stefan Roesea0f10c92009-10-21 11:59:52 +0200670}
671#endif
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100672
Matthew McClintockacda3a42010-10-13 13:39:26 +0200673int fdt_increase_size(void *fdt, int add_len)
674{
675 int newlen;
676
677 newlen = fdt_totalsize(fdt) + add_len;
678
679 /* Open in place with a new len */
680 return fdt_open_into(fdt, fdt, newlen);
681}
682
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100683#ifdef CONFIG_FDT_FIXUP_PARTITIONS
684#include <jffs2/load_kernel.h>
685#include <mtd_node.h>
686
687struct reg_cell {
688 unsigned int r0;
689 unsigned int r1;
690};
691
692int fdt_del_subnodes(const void *blob, int parent_offset)
693{
694 int off, ndepth;
695 int ret;
696
697 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
698 (off >= 0) && (ndepth > 0);
699 off = fdt_next_node(blob, off, &ndepth)) {
700 if (ndepth == 1) {
701 debug("delete %s: offset: %x\n",
702 fdt_get_name(blob, off, 0), off);
703 ret = fdt_del_node((void *)blob, off);
704 if (ret < 0) {
705 printf("Can't delete node: %s\n",
706 fdt_strerror(ret));
707 return ret;
708 } else {
709 ndepth = 0;
710 off = parent_offset;
711 }
712 }
713 }
714 return 0;
715}
716
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100717int fdt_del_partitions(void *blob, int parent_offset)
718{
719 const void *prop;
720 int ndepth = 0;
721 int off;
722 int ret;
723
724 off = fdt_next_node(blob, parent_offset, &ndepth);
725 if (off > 0 && ndepth == 1) {
726 prop = fdt_getprop(blob, off, "label", NULL);
727 if (prop == NULL) {
728 /*
729 * Could not find label property, nand {}; node?
730 * Check subnode, delete partitions there if any.
731 */
732 return fdt_del_partitions(blob, off);
733 } else {
734 ret = fdt_del_subnodes(blob, parent_offset);
735 if (ret < 0) {
736 printf("Can't remove subnodes: %s\n",
737 fdt_strerror(ret));
738 return ret;
739 }
740 }
741 }
742 return 0;
743}
744
745int fdt_node_set_part_info(void *blob, int parent_offset,
746 struct mtd_device *dev)
747{
748 struct list_head *pentry;
749 struct part_info *part;
750 struct reg_cell cell;
751 int off, ndepth = 0;
752 int part_num, ret;
753 char buf[64];
754
755 ret = fdt_del_partitions(blob, parent_offset);
756 if (ret < 0)
757 return ret;
758
759 /*
760 * Check if it is nand {}; subnode, adjust
761 * the offset in this case
762 */
763 off = fdt_next_node(blob, parent_offset, &ndepth);
764 if (off > 0 && ndepth == 1)
765 parent_offset = off;
766
767 part_num = 0;
768 list_for_each_prev(pentry, &dev->parts) {
769 int newoff;
770
771 part = list_entry(pentry, struct part_info, link);
772
773 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
774 part_num, part->name, part->size,
775 part->offset, part->mask_flags);
776
777 sprintf(buf, "partition@%x", part->offset);
778add_sub:
779 ret = fdt_add_subnode(blob, parent_offset, buf);
780 if (ret == -FDT_ERR_NOSPACE) {
781 ret = fdt_increase_size(blob, 512);
782 if (!ret)
783 goto add_sub;
784 else
785 goto err_size;
786 } else if (ret < 0) {
787 printf("Can't add partition node: %s\n",
788 fdt_strerror(ret));
789 return ret;
790 }
791 newoff = ret;
792
793 /* Check MTD_WRITEABLE_CMD flag */
794 if (part->mask_flags & 1) {
795add_ro:
796 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
797 if (ret == -FDT_ERR_NOSPACE) {
798 ret = fdt_increase_size(blob, 512);
799 if (!ret)
800 goto add_ro;
801 else
802 goto err_size;
803 } else if (ret < 0)
804 goto err_prop;
805 }
806
807 cell.r0 = cpu_to_fdt32(part->offset);
808 cell.r1 = cpu_to_fdt32(part->size);
809add_reg:
810 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
811 if (ret == -FDT_ERR_NOSPACE) {
812 ret = fdt_increase_size(blob, 512);
813 if (!ret)
814 goto add_reg;
815 else
816 goto err_size;
817 } else if (ret < 0)
818 goto err_prop;
819
820add_label:
821 ret = fdt_setprop_string(blob, newoff, "label", part->name);
822 if (ret == -FDT_ERR_NOSPACE) {
823 ret = fdt_increase_size(blob, 512);
824 if (!ret)
825 goto add_label;
826 else
827 goto err_size;
828 } else if (ret < 0)
829 goto err_prop;
830
831 part_num++;
832 }
833 return 0;
834err_size:
835 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
836 return ret;
837err_prop:
838 printf("Can't add property: %s\n", fdt_strerror(ret));
839 return ret;
840}
841
842/*
843 * Update partitions in nor/nand nodes using info from
844 * mtdparts environment variable. The nodes to update are
845 * specified by node_info structure which contains mtd device
846 * type and compatible string: E. g. the board code in
847 * ft_board_setup() could use:
848 *
849 * struct node_info nodes[] = {
850 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
851 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
852 * };
853 *
854 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
855 */
856void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
857{
858 struct node_info *ni = node_info;
859 struct mtd_device *dev;
860 char *parts;
861 int i, idx;
862 int noff;
863
864 parts = getenv("mtdparts");
865 if (!parts)
866 return;
867
868 if (mtdparts_init() != 0)
869 return;
870
871 for (i = 0; i < node_info_size; i++) {
872 idx = 0;
873 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
874 while (noff != -FDT_ERR_NOTFOUND) {
875 debug("%s: %s, mtd dev type %d\n",
876 fdt_get_name(blob, noff, 0),
877 ni[i].compat, ni[i].type);
878 dev = device_find(ni[i].type, idx++);
879 if (dev) {
880 if (fdt_node_set_part_info(blob, noff, dev))
881 return; /* return on error */
882 }
883
884 /* Jump to next flash node */
885 noff = fdt_node_offset_by_compatible(blob, noff,
886 ni[i].compat);
887 }
888 }
889}
890#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -0500891
892void fdt_del_node_and_alias(void *blob, const char *alias)
893{
894 int off = fdt_path_offset(blob, alias);
895
896 if (off < 0)
897 return;
898
899 fdt_del_node(blob, off);
900
901 off = fdt_path_offset(blob, "/aliases");
902 fdt_delprop(blob, off, alias);
903}
Kumar Galab87e7932010-06-16 14:27:38 -0500904
905/* Helper to read a big number; size is in cells (not bytes) */
906static inline u64 of_read_number(const __be32 *cell, int size)
907{
908 u64 r = 0;
909 while (size--)
910 r = (r << 32) | be32_to_cpu(*(cell++));
911 return r;
912}
913
Kumar Galab87e7932010-06-16 14:27:38 -0500914#define PRu64 "%llx"
915
916/* Max address size we deal with */
917#define OF_MAX_ADDR_CELLS 4
918#define OF_BAD_ADDR ((u64)-1)
919#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
920 (ns) > 0)
921
922/* Debug utility */
923#ifdef DEBUG
924static void of_dump_addr(const char *s, const u32 *addr, int na)
925{
926 printf("%s", s);
927 while(na--)
928 printf(" %08x", *(addr++));
929 printf("\n");
930}
931#else
932static void of_dump_addr(const char *s, const u32 *addr, int na) { }
933#endif
934
935/* Callbacks for bus specific translators */
936struct of_bus {
937 const char *name;
938 const char *addresses;
Scott Wood99899712010-08-12 18:37:39 -0500939 void (*count_cells)(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500940 int *addrc, int *sizec);
941 u64 (*map)(u32 *addr, const u32 *range,
942 int na, int ns, int pna);
943 int (*translate)(u32 *addr, u64 offset, int na);
944};
945
946/* Default translator (generic bus) */
Scott Wood99899712010-08-12 18:37:39 -0500947static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -0500948 int *addrc, int *sizec)
949{
Scott Wood99899712010-08-12 18:37:39 -0500950 const u32 *prop;
951
952 if (addrc) {
953 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
954 if (prop)
Dirk Behme4169ea02010-11-21 20:19:34 +0100955 *addrc = be32_to_cpup((u32 *)prop);
Scott Wood99899712010-08-12 18:37:39 -0500956 else
957 *addrc = 2;
958 }
959
960 if (sizec) {
961 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
962 if (prop)
Dirk Behme4169ea02010-11-21 20:19:34 +0100963 *sizec = be32_to_cpup((u32 *)prop);
Scott Wood99899712010-08-12 18:37:39 -0500964 else
965 *sizec = 1;
966 }
Kumar Galab87e7932010-06-16 14:27:38 -0500967}
968
969static u64 of_bus_default_map(u32 *addr, const u32 *range,
970 int na, int ns, int pna)
971{
972 u64 cp, s, da;
973
974 cp = of_read_number(range, na);
975 s = of_read_number(range + na + pna, ns);
976 da = of_read_number(addr, na);
977
978 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
979 cp, s, da);
980
981 if (da < cp || da >= (cp + s))
982 return OF_BAD_ADDR;
983 return da - cp;
984}
985
986static int of_bus_default_translate(u32 *addr, u64 offset, int na)
987{
988 u64 a = of_read_number(addr, na);
989 memset(addr, 0, na * 4);
990 a += offset;
991 if (na > 1)
992 addr[na - 2] = a >> 32;
993 addr[na - 1] = a & 0xffffffffu;
994
995 return 0;
996}
997
998/* Array of bus specific translators */
999static struct of_bus of_busses[] = {
1000 /* Default */
1001 {
1002 .name = "default",
1003 .addresses = "reg",
1004 .count_cells = of_bus_default_count_cells,
1005 .map = of_bus_default_map,
1006 .translate = of_bus_default_translate,
1007 },
1008};
1009
1010static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1011 struct of_bus *pbus, u32 *addr,
1012 int na, int ns, int pna, const char *rprop)
1013{
1014 const u32 *ranges;
1015 int rlen;
1016 int rone;
1017 u64 offset = OF_BAD_ADDR;
1018
1019 /* Normally, an absence of a "ranges" property means we are
1020 * crossing a non-translatable boundary, and thus the addresses
1021 * below the current not cannot be converted to CPU physical ones.
1022 * Unfortunately, while this is very clear in the spec, it's not
1023 * what Apple understood, and they do have things like /uni-n or
1024 * /ht nodes with no "ranges" property and a lot of perfectly
1025 * useable mapped devices below them. Thus we treat the absence of
1026 * "ranges" as equivalent to an empty "ranges" property which means
1027 * a 1:1 translation at that level. It's up to the caller not to try
1028 * to translate addresses that aren't supposed to be translated in
1029 * the first place. --BenH.
1030 */
1031 ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen);
1032 if (ranges == NULL || rlen == 0) {
1033 offset = of_read_number(addr, na);
1034 memset(addr, 0, pna * 4);
1035 debug("OF: no ranges, 1:1 translation\n");
1036 goto finish;
1037 }
1038
1039 debug("OF: walking ranges...\n");
1040
1041 /* Now walk through the ranges */
1042 rlen /= 4;
1043 rone = na + pna + ns;
1044 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1045 offset = bus->map(addr, ranges, na, ns, pna);
1046 if (offset != OF_BAD_ADDR)
1047 break;
1048 }
1049 if (offset == OF_BAD_ADDR) {
1050 debug("OF: not found !\n");
1051 return 1;
1052 }
1053 memcpy(addr, ranges + na, 4 * pna);
1054
1055 finish:
1056 of_dump_addr("OF: parent translation for:", addr, pna);
1057 debug("OF: with offset: "PRu64"\n", offset);
1058
1059 /* Translate it into parent bus space */
1060 return pbus->translate(addr, offset, pna);
1061}
1062
1063/*
1064 * Translate an address from the device-tree into a CPU physical address,
1065 * this walks up the tree and applies the various bus mappings on the
1066 * way.
1067 *
1068 * Note: We consider that crossing any level with #size-cells == 0 to mean
1069 * that translation is impossible (that is we are not dealing with a value
1070 * that can be mapped to a cpu physical address). This is not really specified
1071 * that way, but this is traditionally the way IBM at least do things
1072 */
1073u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr,
1074 const char *rprop)
1075{
1076 int parent;
1077 struct of_bus *bus, *pbus;
1078 u32 addr[OF_MAX_ADDR_CELLS];
1079 int na, ns, pna, pns;
1080 u64 result = OF_BAD_ADDR;
1081
1082 debug("OF: ** translation for device %s **\n",
1083 fdt_get_name(blob, node_offset, NULL));
1084
1085 /* Get parent & match bus type */
1086 parent = fdt_parent_offset(blob, node_offset);
1087 if (parent < 0)
1088 goto bail;
1089 bus = &of_busses[0];
1090
1091 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001092 bus->count_cells(blob, parent, &na, &ns);
Kumar Galab87e7932010-06-16 14:27:38 -05001093 if (!OF_CHECK_COUNTS(na, ns)) {
1094 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1095 fdt_get_name(blob, node_offset, NULL));
1096 goto bail;
1097 }
1098 memcpy(addr, in_addr, na * 4);
1099
1100 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1101 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1102 of_dump_addr("OF: translating address:", addr, na);
1103
1104 /* Translate */
1105 for (;;) {
1106 /* Switch to parent bus */
1107 node_offset = parent;
1108 parent = fdt_parent_offset(blob, node_offset);
1109
1110 /* If root, we have finished */
1111 if (parent < 0) {
1112 debug("OF: reached root node\n");
1113 result = of_read_number(addr, na);
1114 break;
1115 }
1116
1117 /* Get new parent bus and counts */
1118 pbus = &of_busses[0];
Scott Wood99899712010-08-12 18:37:39 -05001119 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galab87e7932010-06-16 14:27:38 -05001120 if (!OF_CHECK_COUNTS(pna, pns)) {
1121 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1122 fdt_get_name(blob, node_offset, NULL));
1123 break;
1124 }
1125
1126 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1127 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1128
1129 /* Apply bus translation */
1130 if (of_translate_one(blob, node_offset, bus, pbus,
1131 addr, na, ns, pna, rprop))
1132 break;
1133
1134 /* Complete the move up one level */
1135 na = pna;
1136 ns = pns;
1137 bus = pbus;
1138
1139 of_dump_addr("OF: one level translation:", addr, na);
1140 }
1141 bail:
1142
1143 return result;
1144}
1145
1146u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr)
1147{
1148 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1149}
Kumar Gala800d1d12010-07-04 12:48:21 -05001150
1151/**
1152 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1153 * who's reg property matches a physical cpu address
1154 *
1155 * @blob: ptr to device tree
1156 * @compat: compatiable string to match
1157 * @compat_off: property name
1158 *
1159 */
1160int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1161 phys_addr_t compat_off)
1162{
1163 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1164 while (off != -FDT_ERR_NOTFOUND) {
1165 u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len);
1166 if (reg) {
1167 if (compat_off == fdt_translate_address(blob, off, reg))
1168 return off;
1169 }
1170 off = fdt_node_offset_by_compatible(blob, off, compat);
1171 }
1172
1173 return -FDT_ERR_NOTFOUND;
1174}
1175
Kumar Galab7afb112010-07-09 16:18:58 -05001176/**
1177 * fdt_alloc_phandle: Return next free phandle value
1178 *
1179 * @blob: ptr to device tree
1180 */
1181int fdt_alloc_phandle(void *blob)
1182{
Timur Tabic6d83032011-09-20 18:24:35 -05001183 int offset, phandle = 0;
Kumar Galab7afb112010-07-09 16:18:58 -05001184
1185 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1186 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabic6d83032011-09-20 18:24:35 -05001187 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab7afb112010-07-09 16:18:58 -05001188 }
Kumar Gala800d1d12010-07-04 12:48:21 -05001189
Kumar Galab7afb112010-07-09 16:18:58 -05001190 return phandle + 1;
1191}
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001192
Gerald Van Barenaf737882011-07-14 21:40:10 -04001193/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001194 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001195 *
1196 * @fdt: ptr to device tree
1197 * @nodeoffset: node to update
1198 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001199 */
1200int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001201{
1202 int ret;
1203
1204#ifdef DEBUG
1205 int off = fdt_node_offset_by_phandle(fdt, phandle);
1206
1207 if ((off >= 0) && (off != nodeoffset)) {
1208 char buf[64];
1209
1210 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1211 printf("Trying to update node %s with phandle %u ",
1212 buf, phandle);
1213
1214 fdt_get_path(fdt, off, buf, sizeof(buf));
1215 printf("that already exists in node %s.\n", buf);
1216 return -FDT_ERR_BADPHANDLE;
1217 }
1218#endif
1219
1220 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1221 if (ret < 0)
1222 return ret;
1223
1224 /*
1225 * For now, also set the deprecated "linux,phandle" property, so that we
1226 * don't break older kernels.
1227 */
1228 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1229
1230 return ret;
1231}
1232
Kumar Galad5fc2582011-08-01 00:25:20 -05001233/*
1234 * fdt_create_phandle: Create a phandle property for the given node
1235 *
1236 * @fdt: ptr to device tree
1237 * @nodeoffset: node to update
1238 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001239unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001240{
1241 /* see if there is a phandle already */
1242 int phandle = fdt_get_phandle(fdt, nodeoffset);
1243
1244 /* if we got 0, means no phandle so create one */
1245 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001246 int ret;
1247
Kumar Galad5fc2582011-08-01 00:25:20 -05001248 phandle = fdt_alloc_phandle(fdt);
Timur Tabi0f46c802011-09-20 18:24:34 -05001249 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1250 if (ret < 0) {
1251 printf("Can't set phandle %u: %s\n", phandle,
1252 fdt_strerror(ret));
1253 return 0;
1254 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001255 }
1256
1257 return phandle;
1258}
1259
Shengzhou Liua7be8022011-10-14 16:26:05 +08001260/*
1261 * fdt_set_node_status: Set status for the given node
1262 *
1263 * @fdt: ptr to device tree
1264 * @nodeoffset: node to update
1265 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1266 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1267 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1268 */
1269int fdt_set_node_status(void *fdt, int nodeoffset,
1270 enum fdt_status status, unsigned int error_code)
1271{
1272 char buf[16];
1273 int ret = 0;
1274
1275 if (nodeoffset < 0)
1276 return nodeoffset;
1277
1278 switch (status) {
1279 case FDT_STATUS_OKAY:
1280 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1281 break;
1282 case FDT_STATUS_DISABLED:
1283 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1284 break;
1285 case FDT_STATUS_FAIL:
1286 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1287 break;
1288 case FDT_STATUS_FAIL_ERROR_CODE:
1289 sprintf(buf, "fail-%d", error_code);
1290 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1291 break;
1292 default:
1293 printf("Invalid fdt status: %x\n", status);
1294 ret = -1;
1295 break;
1296 }
1297
1298 return ret;
1299}
1300
1301/*
1302 * fdt_set_status_by_alias: Set status for the given node given an alias
1303 *
1304 * @fdt: ptr to device tree
1305 * @alias: alias of node to update
1306 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1307 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1308 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1309 */
1310int fdt_set_status_by_alias(void *fdt, const char* alias,
1311 enum fdt_status status, unsigned int error_code)
1312{
1313 int offset = fdt_path_offset(fdt, alias);
1314
1315 return fdt_set_node_status(fdt, offset, status, error_code);
1316}
1317
Tom Wai-Hong Tamdb4a88d2012-12-05 14:46:41 +00001318#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001319int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1320{
1321 int noff;
1322 int ret;
1323
1324 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1325 if (noff != -FDT_ERR_NOTFOUND) {
1326 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1327add_edid:
1328 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1329 if (ret == -FDT_ERR_NOSPACE) {
1330 ret = fdt_increase_size(blob, 512);
1331 if (!ret)
1332 goto add_edid;
1333 else
1334 goto err_size;
1335 } else if (ret < 0) {
1336 printf("Can't add property: %s\n", fdt_strerror(ret));
1337 return ret;
1338 }
1339 }
1340 return 0;
1341err_size:
1342 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1343 return ret;
1344}
1345#endif
Timur Tabi8ebaf202011-05-03 13:24:07 -05001346
1347/*
1348 * Verify the physical address of device tree node for a given alias
1349 *
1350 * This function locates the device tree node of a given alias, and then
1351 * verifies that the physical address of that device matches the given
1352 * parameter. It displays a message if there is a mismatch.
1353 *
1354 * Returns 1 on success, 0 on failure
1355 */
1356int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1357{
1358 const char *path;
1359 const u32 *reg;
1360 int node, len;
1361 u64 dt_addr;
1362
1363 path = fdt_getprop(fdt, anode, alias, NULL);
1364 if (!path) {
1365 /* If there's no such alias, then it's not a failure */
1366 return 1;
1367 }
1368
1369 node = fdt_path_offset(fdt, path);
1370 if (node < 0) {
1371 printf("Warning: device tree alias '%s' points to invalid "
1372 "node %s.\n", alias, path);
1373 return 0;
1374 }
1375
1376 reg = fdt_getprop(fdt, node, "reg", &len);
1377 if (!reg) {
1378 printf("Warning: device tree node '%s' has no address.\n",
1379 path);
1380 return 0;
1381 }
1382
1383 dt_addr = fdt_translate_address(fdt, node, reg);
1384 if (addr != dt_addr) {
1385 printf("Warning: U-Boot configured device %s at address %llx,\n"
1386 " but the device tree has it address %llx.\n",
1387 alias, addr, dt_addr);
1388 return 0;
1389 }
1390
1391 return 1;
1392}
1393
1394/*
1395 * Returns the base address of an SOC or PCI node
1396 */
1397u64 fdt_get_base_address(void *fdt, int node)
1398{
1399 int size;
1400 u32 naddr;
1401 const u32 *prop;
1402
1403 prop = fdt_getprop(fdt, node, "#address-cells", &size);
1404 if (prop && size == 4)
1405 naddr = *prop;
1406 else
1407 naddr = 2;
1408
1409 prop = fdt_getprop(fdt, node, "ranges", &size);
1410
1411 return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1412}