]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/fdt_support.c
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <linux/ctype.h>
26 #include <linux/types.h>
27 #include <asm/global_data.h>
30 #include <fdt_support.h>
34 * Global data (for the gd->bd)
36 DECLARE_GLOBAL_DATA_PTR
;
40 * fdt_find_and_setprop: Find a node and set it's property
42 * @fdt: ptr to device tree
44 * @prop: property name
45 * @val: ptr to new value
46 * @len: length of new property value
47 * @create: flag to create the property if it doesn't exist
49 * Convenience function to directly set a property given the path to the node.
51 int fdt_find_and_setprop(void *fdt
, const char *node
, const char *prop
,
52 const void *val
, int len
, int create
)
54 int nodeoff
= fdt_path_offset(fdt
, node
);
59 if ((!create
) && (fdt_get_property(fdt
, nodeoff
, prop
, 0) == NULL
))
60 return 0; /* create flag not set; so exit quietly */
62 return fdt_setprop(fdt
, nodeoff
, prop
, val
, len
);
65 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
66 static int fdt_fixup_stdout(void *fdt
, int chosenoff
)
69 #ifdef CONFIG_CONS_INDEX
71 char sername
[9] = { 0 };
74 sprintf(sername
, "serial%d", CONFIG_CONS_INDEX
- 1);
76 err
= node
= fdt_path_offset(fdt
, "/aliases");
79 path
= fdt_getprop(fdt
, node
, sername
, &len
);
81 char *p
= malloc(len
);
82 err
= -FDT_ERR_NOSPACE
;
85 err
= fdt_setprop(fdt
, chosenoff
,
86 "linux,stdout-path", p
, len
);
95 printf("WARNING: could not set linux,stdout-path %s.\n",
102 int fdt_chosen(void *fdt
, ulong initrd_start
, ulong initrd_end
, int force
)
106 u32 tmp
; /* used to set 32 bit integer properties */
107 char *str
; /* used to set string properties */
110 err
= fdt_check_header(fdt
);
112 printf("fdt_chosen: %s\n", fdt_strerror(err
));
116 if (initrd_start
&& initrd_end
) {
118 int total
= fdt_num_mem_rsv(fdt
);
122 * Look for an existing entry and update it. If we don't find
123 * the entry, we will j be the next available slot.
125 for (j
= 0; j
< total
; j
++) {
126 err
= fdt_get_mem_rsv(fdt
, j
, &addr
, &size
);
127 if (addr
== initrd_start
) {
128 fdt_del_mem_rsv(fdt
, j
);
133 err
= fdt_add_mem_rsv(fdt
, initrd_start
, initrd_end
- initrd_start
+ 1);
135 printf("fdt_chosen: %s\n", fdt_strerror(err
));
141 * Find the "chosen" node.
143 nodeoffset
= fdt_path_offset (fdt
, "/chosen");
146 * If there is no "chosen" node in the blob, create it.
148 if (nodeoffset
< 0) {
150 * Create a new node "/chosen" (offset 0 is root level)
152 nodeoffset
= fdt_add_subnode(fdt
, 0, "chosen");
153 if (nodeoffset
< 0) {
154 printf("WARNING: could not create /chosen %s.\n",
155 fdt_strerror(nodeoffset
));
161 * Create /chosen properites that don't exist in the fdt.
162 * If the property exists, update it only if the "force" parameter
165 str
= getenv("bootargs");
167 path
= fdt_getprop(fdt
, nodeoffset
, "bootargs", NULL
);
168 if ((path
== NULL
) || force
) {
169 err
= fdt_setprop(fdt
, nodeoffset
,
170 "bootargs", str
, strlen(str
)+1);
172 printf("WARNING: could not set bootargs %s.\n",
176 if (initrd_start
&& initrd_end
) {
177 path
= fdt_getprop(fdt
, nodeoffset
, "linux,initrd-start", NULL
);
178 if ((path
== NULL
) || force
) {
179 tmp
= __cpu_to_be32(initrd_start
);
180 err
= fdt_setprop(fdt
, nodeoffset
,
181 "linux,initrd-start", &tmp
, sizeof(tmp
));
184 "could not set linux,initrd-start %s.\n",
186 tmp
= __cpu_to_be32(initrd_end
);
187 err
= fdt_setprop(fdt
, nodeoffset
,
188 "linux,initrd-end", &tmp
, sizeof(tmp
));
190 printf("WARNING: could not set linux,initrd-end %s.\n",
195 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
196 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
197 if ((path
== NULL
) || force
)
198 err
= fdt_fixup_stdout(fdt
, nodeoffset
);
201 #ifdef OF_STDOUT_PATH
202 path
= fdt_getprop(fdt
, nodeoffset
, "linux,stdout-path", NULL
);
203 if ((path
== NULL
) || force
) {
204 err
= fdt_setprop(fdt
, nodeoffset
,
205 "linux,stdout-path", OF_STDOUT_PATH
, strlen(OF_STDOUT_PATH
)+1);
207 printf("WARNING: could not set linux,stdout-path %s.\n",
215 void do_fixup_by_path(void *fdt
, const char *path
, const char *prop
,
216 const void *val
, int len
, int create
)
220 debug("Updating property '%s/%s' = ", path
, prop
);
221 for (i
= 0; i
< len
; i
++)
222 debug(" %.2x", *(u8
*)(val
+i
));
225 int rc
= fdt_find_and_setprop(fdt
, path
, prop
, val
, len
, create
);
227 printf("Unable to update property %s:%s, err=%s\n",
228 path
, prop
, fdt_strerror(rc
));
231 void do_fixup_by_path_u32(void *fdt
, const char *path
, const char *prop
,
234 val
= cpu_to_fdt32(val
);
235 do_fixup_by_path(fdt
, path
, prop
, &val
, sizeof(val
), create
);
238 void do_fixup_by_prop(void *fdt
,
239 const char *pname
, const void *pval
, int plen
,
240 const char *prop
, const void *val
, int len
,
246 debug("Updating property '%s' = ", prop
);
247 for (i
= 0; i
< len
; i
++)
248 debug(" %.2x", *(u8
*)(val
+i
));
251 off
= fdt_node_offset_by_prop_value(fdt
, -1, pname
, pval
, plen
);
252 while (off
!= -FDT_ERR_NOTFOUND
) {
253 if (create
|| (fdt_get_property(fdt
, off
, prop
, 0) != NULL
))
254 fdt_setprop(fdt
, off
, prop
, val
, len
);
255 off
= fdt_node_offset_by_prop_value(fdt
, off
, pname
, pval
, plen
);
259 void do_fixup_by_prop_u32(void *fdt
,
260 const char *pname
, const void *pval
, int plen
,
261 const char *prop
, u32 val
, int create
)
263 val
= cpu_to_fdt32(val
);
264 do_fixup_by_prop(fdt
, pname
, pval
, plen
, prop
, &val
, 4, create
);
267 void do_fixup_by_compat(void *fdt
, const char *compat
,
268 const char *prop
, const void *val
, int len
, int create
)
273 debug("Updating property '%s' = ", prop
);
274 for (i
= 0; i
< len
; i
++)
275 debug(" %.2x", *(u8
*)(val
+i
));
278 off
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
279 while (off
!= -FDT_ERR_NOTFOUND
) {
280 if (create
|| (fdt_get_property(fdt
, off
, prop
, 0) != NULL
))
281 fdt_setprop(fdt
, off
, prop
, val
, len
);
282 off
= fdt_node_offset_by_compatible(fdt
, off
, compat
);
286 void do_fixup_by_compat_u32(void *fdt
, const char *compat
,
287 const char *prop
, u32 val
, int create
)
289 val
= cpu_to_fdt32(val
);
290 do_fixup_by_compat(fdt
, compat
, prop
, &val
, 4, create
);
293 int fdt_fixup_memory(void *blob
, u64 start
, u64 size
)
295 int err
, nodeoffset
, len
= 0;
297 const u32
*addrcell
, *sizecell
;
299 err
= fdt_check_header(blob
);
301 printf("%s: %s\n", __FUNCTION__
, fdt_strerror(err
));
305 /* update, or add and update /memory node */
306 nodeoffset
= fdt_path_offset(blob
, "/memory");
307 if (nodeoffset
< 0) {
308 nodeoffset
= fdt_add_subnode(blob
, 0, "memory");
310 printf("WARNING: could not create /memory: %s.\n",
311 fdt_strerror(nodeoffset
));
314 err
= fdt_setprop(blob
, nodeoffset
, "device_type", "memory",
317 printf("WARNING: could not set %s %s.\n", "device_type",
322 addrcell
= fdt_getprop(blob
, 0, "#address-cells", NULL
);
323 /* use shifts and mask to ensure endianness */
324 if ((addrcell
) && (*addrcell
== 2)) {
325 tmp
[0] = (start
>> 56) & 0xff;
326 tmp
[1] = (start
>> 48) & 0xff;
327 tmp
[2] = (start
>> 40) & 0xff;
328 tmp
[3] = (start
>> 32) & 0xff;
329 tmp
[4] = (start
>> 24) & 0xff;
330 tmp
[5] = (start
>> 16) & 0xff;
331 tmp
[6] = (start
>> 8) & 0xff;
332 tmp
[7] = (start
) & 0xff;
335 tmp
[0] = (start
>> 24) & 0xff;
336 tmp
[1] = (start
>> 16) & 0xff;
337 tmp
[2] = (start
>> 8) & 0xff;
338 tmp
[3] = (start
) & 0xff;
342 sizecell
= fdt_getprop(blob
, 0, "#size-cells", NULL
);
343 /* use shifts and mask to ensure endianness */
344 if ((sizecell
) && (*sizecell
== 2)) {
345 tmp
[0+len
] = (size
>> 56) & 0xff;
346 tmp
[1+len
] = (size
>> 48) & 0xff;
347 tmp
[2+len
] = (size
>> 40) & 0xff;
348 tmp
[3+len
] = (size
>> 32) & 0xff;
349 tmp
[4+len
] = (size
>> 24) & 0xff;
350 tmp
[5+len
] = (size
>> 16) & 0xff;
351 tmp
[6+len
] = (size
>> 8) & 0xff;
352 tmp
[7+len
] = (size
) & 0xff;
355 tmp
[0+len
] = (size
>> 24) & 0xff;
356 tmp
[1+len
] = (size
>> 16) & 0xff;
357 tmp
[2+len
] = (size
>> 8) & 0xff;
358 tmp
[3+len
] = (size
) & 0xff;
362 err
= fdt_setprop(blob
, nodeoffset
, "reg", tmp
, len
);
364 printf("WARNING: could not set %s %s.\n",
365 "reg", fdt_strerror(err
));
371 #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
372 defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
374 void fdt_fixup_ethernet(void *fdt
, bd_t
*bd
)
379 node
= fdt_path_offset(fdt
, "/aliases");
381 #if defined(CONFIG_HAS_ETH0)
382 path
= fdt_getprop(fdt
, node
, "ethernet0", NULL
);
384 do_fixup_by_path(fdt
, path
, "mac-address",
385 bd
->bi_enetaddr
, 6, 0);
386 do_fixup_by_path(fdt
, path
, "local-mac-address",
387 bd
->bi_enetaddr
, 6, 1);
390 #if defined(CONFIG_HAS_ETH1)
391 path
= fdt_getprop(fdt
, node
, "ethernet1", NULL
);
393 do_fixup_by_path(fdt
, path
, "mac-address",
394 bd
->bi_enet1addr
, 6, 0);
395 do_fixup_by_path(fdt
, path
, "local-mac-address",
396 bd
->bi_enet1addr
, 6, 1);
399 #if defined(CONFIG_HAS_ETH2)
400 path
= fdt_getprop(fdt
, node
, "ethernet2", NULL
);
402 do_fixup_by_path(fdt
, path
, "mac-address",
403 bd
->bi_enet2addr
, 6, 0);
404 do_fixup_by_path(fdt
, path
, "local-mac-address",
405 bd
->bi_enet2addr
, 6, 1);
408 #if defined(CONFIG_HAS_ETH3)
409 path
= fdt_getprop(fdt
, node
, "ethernet3", NULL
);
411 do_fixup_by_path(fdt
, path
, "mac-address",
412 bd
->bi_enet3addr
, 6, 0);
413 do_fixup_by_path(fdt
, path
, "local-mac-address",
414 bd
->bi_enet3addr
, 6, 1);
421 #ifdef CONFIG_HAS_FSL_DR_USB
422 void fdt_fixup_dr_usb(void *blob
, bd_t
*bd
)
425 const char *compat
= "fsl-usb2-dr";
426 const char *prop
= "dr_mode";
430 mode
= getenv("usb_dr_mode");
434 node_offset
= fdt_node_offset_by_compatible(blob
, 0, compat
);
436 printf("WARNING: could not find compatible node %s: %s.\n",
437 compat
, fdt_strerror(node_offset
));
439 err
= fdt_setprop(blob
, node_offset
, prop
, mode
, strlen(mode
) + 1);
441 printf("WARNING: could not set %s for %s: %s.\n",
442 prop
, compat
, fdt_strerror(err
));
444 #endif /* CONFIG_HAS_FSL_DR_USB */