]>
Commit | Line | Data |
---|---|---|
64dbbd40 GVB |
1 | /* |
2 | * (C) Copyright 2007 | |
3 | * Gerald Van Baren, Custom IDEAS, [email protected] | |
4 | * | |
2a523f52 | 5 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
a0342c08 | 6 | * |
1a459660 | 7 | * SPDX-License-Identifier: GPL-2.0+ |
64dbbd40 GVB |
8 | */ |
9 | ||
10 | #include <common.h> | |
3e303f74 | 11 | #include <stdio_dev.h> |
64dbbd40 GVB |
12 | #include <linux/ctype.h> |
13 | #include <linux/types.h> | |
64dbbd40 | 14 | #include <asm/global_data.h> |
64dbbd40 GVB |
15 | #include <libfdt.h> |
16 | #include <fdt_support.h> | |
151c8b09 | 17 | #include <exports.h> |
64dbbd40 | 18 | |
f77a606a DF |
19 | /* |
20 | * Get cells len in bytes | |
21 | * if #NNNN-cells property is 2 then len is 8 | |
22 | * otherwise len is 4 | |
23 | */ | |
24 | static int get_cells_len(void *blob, char *nr_cells_name) | |
25 | { | |
26 | const fdt32_t *cell; | |
27 | ||
28 | cell = fdt_getprop(blob, 0, nr_cells_name, NULL); | |
29 | if (cell && fdt32_to_cpu(*cell) == 2) | |
30 | return 8; | |
31 | ||
32 | return 4; | |
33 | } | |
34 | ||
35 | /* | |
36 | * Write a 4 or 8 byte big endian cell | |
37 | */ | |
38 | static void write_cell(u8 *addr, u64 val, int size) | |
39 | { | |
40 | int shift = (size - 1) * 8; | |
41 | while (size-- > 0) { | |
42 | *addr++ = (val >> shift) & 0xff; | |
43 | shift -= 8; | |
44 | } | |
45 | } | |
46 | ||
94fb182c AG |
47 | /** |
48 | * fdt_getprop_u32_default_node - Return a node's property or a default | |
49 | * | |
50 | * @fdt: ptr to device tree | |
51 | * @off: offset of node | |
52 | * @cell: cell offset in property | |
53 | * @prop: property name | |
54 | * @dflt: default value if the property isn't found | |
55 | * | |
56 | * Convenience function to return a node's property or a default value if | |
57 | * the property doesn't exist. | |
58 | */ | |
59 | u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell, | |
60 | const char *prop, const u32 dflt) | |
61 | { | |
62 | const fdt32_t *val; | |
63 | int len; | |
64 | ||
65 | val = fdt_getprop(fdt, off, prop, &len); | |
66 | ||
67 | /* Check if property exists */ | |
68 | if (!val) | |
69 | return dflt; | |
70 | ||
71 | /* Check if property is long enough */ | |
72 | if (len < ((cell + 1) * sizeof(uint32_t))) | |
73 | return dflt; | |
74 | ||
75 | return fdt32_to_cpu(*val); | |
76 | } | |
77 | ||
3bed2aaf KG |
78 | /** |
79 | * fdt_getprop_u32_default - Find a node and return it's property or a default | |
80 | * | |
81 | * @fdt: ptr to device tree | |
82 | * @path: path of node | |
83 | * @prop: property name | |
84 | * @dflt: default value if the property isn't found | |
85 | * | |
86 | * Convenience function to find a node and return it's property or a | |
87 | * default value if it doesn't exist. | |
88 | */ | |
07e12784 GB |
89 | u32 fdt_getprop_u32_default(const void *fdt, const char *path, |
90 | const char *prop, const u32 dflt) | |
3bed2aaf | 91 | { |
3bed2aaf KG |
92 | int off; |
93 | ||
94 | off = fdt_path_offset(fdt, path); | |
95 | if (off < 0) | |
96 | return dflt; | |
97 | ||
94fb182c | 98 | return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt); |
3bed2aaf | 99 | } |
64dbbd40 | 100 | |
a3c2933e KG |
101 | /** |
102 | * fdt_find_and_setprop: Find a node and set it's property | |
103 | * | |
104 | * @fdt: ptr to device tree | |
105 | * @node: path of node | |
106 | * @prop: property name | |
107 | * @val: ptr to new value | |
108 | * @len: length of new property value | |
109 | * @create: flag to create the property if it doesn't exist | |
110 | * | |
111 | * Convenience function to directly set a property given the path to the node. | |
112 | */ | |
113 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, | |
114 | const void *val, int len, int create) | |
115 | { | |
8d04f02f | 116 | int nodeoff = fdt_path_offset(fdt, node); |
a3c2933e KG |
117 | |
118 | if (nodeoff < 0) | |
119 | return nodeoff; | |
120 | ||
8aa5ec6e | 121 | if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL)) |
a3c2933e KG |
122 | return 0; /* create flag not set; so exit quietly */ |
123 | ||
124 | return fdt_setprop(fdt, nodeoff, prop, val, len); | |
125 | } | |
126 | ||
8edb2192 MY |
127 | /** |
128 | * fdt_find_or_add_subnode - find or possibly add a subnode of a given node | |
129 | * @fdt: pointer to the device tree blob | |
130 | * @parentoffset: structure block offset of a node | |
131 | * @name: name of the subnode to locate | |
132 | * | |
133 | * fdt_subnode_offset() finds a subnode of the node with a given name. | |
134 | * If the subnode does not exist, it will be created. | |
135 | */ | |
136 | static int fdt_find_or_add_subnode(void *fdt, int parentoffset, | |
137 | const char *name) | |
138 | { | |
139 | int offset; | |
140 | ||
141 | offset = fdt_subnode_offset(fdt, parentoffset, name); | |
142 | ||
143 | if (offset == -FDT_ERR_NOTFOUND) | |
144 | offset = fdt_add_subnode(fdt, parentoffset, name); | |
145 | ||
146 | if (offset < 0) | |
147 | printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset)); | |
148 | ||
149 | return offset; | |
150 | } | |
151 | ||
151c8b09 | 152 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
3e303f74 | 153 | |
036036d7 | 154 | #ifdef CONFIG_CONS_INDEX |
3e303f74 AV |
155 | static void fdt_fill_multisername(char *sername, size_t maxlen) |
156 | { | |
157 | const char *outname = stdio_devices[stdout]->name; | |
158 | ||
159 | if (strcmp(outname, "serial") > 0) | |
160 | strncpy(sername, outname, maxlen); | |
161 | ||
162 | /* eserial? */ | |
163 | if (strcmp(outname + 1, "serial") > 0) | |
164 | strncpy(sername, outname + 1, maxlen); | |
165 | } | |
036036d7 | 166 | #endif |
3e303f74 | 167 | |
40777812 | 168 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
151c8b09 KG |
169 | { |
170 | int err = 0; | |
171 | #ifdef CONFIG_CONS_INDEX | |
172 | int node; | |
173 | char sername[9] = { 0 }; | |
174 | const char *path; | |
175 | ||
3e303f74 AV |
176 | fdt_fill_multisername(sername, sizeof(sername) - 1); |
177 | if (!sername[0]) | |
178 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); | |
151c8b09 KG |
179 | |
180 | err = node = fdt_path_offset(fdt, "/aliases"); | |
181 | if (node >= 0) { | |
182 | int len; | |
183 | path = fdt_getprop(fdt, node, sername, &len); | |
184 | if (path) { | |
185 | char *p = malloc(len); | |
186 | err = -FDT_ERR_NOSPACE; | |
187 | if (p) { | |
188 | memcpy(p, path, len); | |
40777812 | 189 | err = fdt_setprop(fdt, chosenoff, |
151c8b09 KG |
190 | "linux,stdout-path", p, len); |
191 | free(p); | |
192 | } | |
193 | } else { | |
194 | err = len; | |
195 | } | |
196 | } | |
197 | #endif | |
198 | if (err < 0) | |
199 | printf("WARNING: could not set linux,stdout-path %s.\n", | |
200 | fdt_strerror(err)); | |
201 | ||
202 | return err; | |
203 | } | |
204 | #endif | |
205 | ||
2a1a2cb6 | 206 | int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) |
64dbbd40 | 207 | { |
f77a606a | 208 | int nodeoffset, addr_cell_len; |
2a1a2cb6 | 209 | int err, j, total; |
f77a606a | 210 | fdt64_t tmp; |
b60af3d4 | 211 | const char *path; |
2a1a2cb6 | 212 | uint64_t addr, size; |
64dbbd40 | 213 | |
8edb2192 MY |
214 | /* find or create "/chosen" node. */ |
215 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); | |
216 | if (nodeoffset < 0) | |
2a1a2cb6 | 217 | return nodeoffset; |
64dbbd40 | 218 | |
2a1a2cb6 KG |
219 | /* just return if initrd_start/end aren't valid */ |
220 | if ((initrd_start == 0) || (initrd_end == 0)) | |
221 | return 0; | |
c28abb9c | 222 | |
2a1a2cb6 KG |
223 | total = fdt_num_mem_rsv(fdt); |
224 | ||
225 | /* | |
226 | * Look for an existing entry and update it. If we don't find | |
227 | * the entry, we will j be the next available slot. | |
228 | */ | |
229 | for (j = 0; j < total; j++) { | |
230 | err = fdt_get_mem_rsv(fdt, j, &addr, &size); | |
231 | if (addr == initrd_start) { | |
232 | fdt_del_mem_rsv(fdt, j); | |
233 | break; | |
c28abb9c | 234 | } |
2a1a2cb6 | 235 | } |
8d04f02f | 236 | |
ce6b27a8 | 237 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); |
2a1a2cb6 KG |
238 | if (err < 0) { |
239 | printf("fdt_initrd: %s\n", fdt_strerror(err)); | |
240 | return err; | |
241 | } | |
242 | ||
f77a606a DF |
243 | addr_cell_len = get_cells_len(fdt, "#address-cells"); |
244 | ||
2a1a2cb6 KG |
245 | path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL); |
246 | if ((path == NULL) || force) { | |
f77a606a | 247 | write_cell((u8 *)&tmp, initrd_start, addr_cell_len); |
2a1a2cb6 | 248 | err = fdt_setprop(fdt, nodeoffset, |
be6d4266 | 249 | "linux,initrd-start", &tmp, addr_cell_len); |
2a1a2cb6 KG |
250 | if (err < 0) { |
251 | printf("WARNING: " | |
252 | "could not set linux,initrd-start %s.\n", | |
253 | fdt_strerror(err)); | |
254 | return err; | |
255 | } | |
f77a606a | 256 | write_cell((u8 *)&tmp, initrd_end, addr_cell_len); |
2a1a2cb6 | 257 | err = fdt_setprop(fdt, nodeoffset, |
be6d4266 | 258 | "linux,initrd-end", &tmp, addr_cell_len); |
64dbbd40 | 259 | if (err < 0) { |
2a1a2cb6 KG |
260 | printf("WARNING: could not set linux,initrd-end %s.\n", |
261 | fdt_strerror(err)); | |
262 | ||
64dbbd40 GVB |
263 | return err; |
264 | } | |
265 | } | |
266 | ||
2a1a2cb6 KG |
267 | return 0; |
268 | } | |
269 | ||
56844a22 | 270 | int fdt_chosen(void *fdt, int force) |
2a1a2cb6 KG |
271 | { |
272 | int nodeoffset; | |
273 | int err; | |
274 | char *str; /* used to set string properties */ | |
275 | const char *path; | |
276 | ||
277 | err = fdt_check_header(fdt); | |
278 | if (err < 0) { | |
279 | printf("fdt_chosen: %s\n", fdt_strerror(err)); | |
280 | return err; | |
281 | } | |
282 | ||
8edb2192 MY |
283 | /* find or create "/chosen" node. */ |
284 | nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); | |
285 | if (nodeoffset < 0) | |
286 | return nodeoffset; | |
64dbbd40 GVB |
287 | |
288 | /* | |
b60af3d4 GVB |
289 | * Create /chosen properites that don't exist in the fdt. |
290 | * If the property exists, update it only if the "force" parameter | |
291 | * is true. | |
64dbbd40 GVB |
292 | */ |
293 | str = getenv("bootargs"); | |
294 | if (str != NULL) { | |
b60af3d4 GVB |
295 | path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL); |
296 | if ((path == NULL) || force) { | |
297 | err = fdt_setprop(fdt, nodeoffset, | |
298 | "bootargs", str, strlen(str)+1); | |
299 | if (err < 0) | |
300 | printf("WARNING: could not set bootargs %s.\n", | |
301 | fdt_strerror(err)); | |
302 | } | |
64dbbd40 | 303 | } |
2a1a2cb6 | 304 | |
151c8b09 | 305 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
b60af3d4 GVB |
306 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
307 | if ((path == NULL) || force) | |
308 | err = fdt_fixup_stdout(fdt, nodeoffset); | |
151c8b09 KG |
309 | #endif |
310 | ||
64dbbd40 | 311 | #ifdef OF_STDOUT_PATH |
b60af3d4 GVB |
312 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
313 | if ((path == NULL) || force) { | |
314 | err = fdt_setprop(fdt, nodeoffset, | |
315 | "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); | |
316 | if (err < 0) | |
317 | printf("WARNING: could not set linux,stdout-path %s.\n", | |
318 | fdt_strerror(err)); | |
319 | } | |
64dbbd40 GVB |
320 | #endif |
321 | ||
64dbbd40 GVB |
322 | return err; |
323 | } | |
324 | ||
e93becf8 KG |
325 | void do_fixup_by_path(void *fdt, const char *path, const char *prop, |
326 | const void *val, int len, int create) | |
327 | { | |
328 | #if defined(DEBUG) | |
329 | int i; | |
d9ad115b | 330 | debug("Updating property '%s/%s' = ", path, prop); |
e93becf8 KG |
331 | for (i = 0; i < len; i++) |
332 | debug(" %.2x", *(u8*)(val+i)); | |
333 | debug("\n"); | |
334 | #endif | |
335 | int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); | |
336 | if (rc) | |
337 | printf("Unable to update property %s:%s, err=%s\n", | |
338 | path, prop, fdt_strerror(rc)); | |
339 | } | |
340 | ||
341 | void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, | |
342 | u32 val, int create) | |
343 | { | |
8aa5ec6e KP |
344 | fdt32_t tmp = cpu_to_fdt32(val); |
345 | do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create); | |
e93becf8 KG |
346 | } |
347 | ||
9eb77cea KG |
348 | void do_fixup_by_prop(void *fdt, |
349 | const char *pname, const void *pval, int plen, | |
350 | const char *prop, const void *val, int len, | |
351 | int create) | |
352 | { | |
353 | int off; | |
354 | #if defined(DEBUG) | |
355 | int i; | |
d9ad115b | 356 | debug("Updating property '%s' = ", prop); |
9eb77cea KG |
357 | for (i = 0; i < len; i++) |
358 | debug(" %.2x", *(u8*)(val+i)); | |
359 | debug("\n"); | |
360 | #endif | |
361 | off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); | |
362 | while (off != -FDT_ERR_NOTFOUND) { | |
8aa5ec6e | 363 | if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) |
9eb77cea KG |
364 | fdt_setprop(fdt, off, prop, val, len); |
365 | off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); | |
366 | } | |
367 | } | |
368 | ||
369 | void do_fixup_by_prop_u32(void *fdt, | |
370 | const char *pname, const void *pval, int plen, | |
371 | const char *prop, u32 val, int create) | |
372 | { | |
8aa5ec6e KP |
373 | fdt32_t tmp = cpu_to_fdt32(val); |
374 | do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create); | |
9eb77cea KG |
375 | } |
376 | ||
377 | void do_fixup_by_compat(void *fdt, const char *compat, | |
378 | const char *prop, const void *val, int len, int create) | |
379 | { | |
380 | int off = -1; | |
381 | #if defined(DEBUG) | |
382 | int i; | |
d9ad115b | 383 | debug("Updating property '%s' = ", prop); |
9eb77cea KG |
384 | for (i = 0; i < len; i++) |
385 | debug(" %.2x", *(u8*)(val+i)); | |
386 | debug("\n"); | |
387 | #endif | |
388 | off = fdt_node_offset_by_compatible(fdt, -1, compat); | |
389 | while (off != -FDT_ERR_NOTFOUND) { | |
8aa5ec6e | 390 | if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) |
9eb77cea KG |
391 | fdt_setprop(fdt, off, prop, val, len); |
392 | off = fdt_node_offset_by_compatible(fdt, off, compat); | |
393 | } | |
394 | } | |
395 | ||
396 | void do_fixup_by_compat_u32(void *fdt, const char *compat, | |
397 | const char *prop, u32 val, int create) | |
398 | { | |
8aa5ec6e KP |
399 | fdt32_t tmp = cpu_to_fdt32(val); |
400 | do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create); | |
9eb77cea KG |
401 | } |
402 | ||
5e574546 DA |
403 | #ifdef CONFIG_NR_DRAM_BANKS |
404 | #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS | |
405 | #else | |
8aa5ec6e | 406 | #define MEMORY_BANKS_MAX 4 |
5e574546 | 407 | #endif |
a6bd9e83 JR |
408 | int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) |
409 | { | |
410 | int err, nodeoffset; | |
411 | int addr_cell_len, size_cell_len, len; | |
8aa5ec6e | 412 | u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */ |
a6bd9e83 | 413 | int bank; |
3c927281 | 414 | |
8aa5ec6e KP |
415 | if (banks > MEMORY_BANKS_MAX) { |
416 | printf("%s: num banks %d exceeds hardcoded limit %d." | |
417 | " Recompile with higher MEMORY_BANKS_MAX?\n", | |
418 | __FUNCTION__, banks, MEMORY_BANKS_MAX); | |
419 | return -1; | |
420 | } | |
421 | ||
3c927281 KG |
422 | err = fdt_check_header(blob); |
423 | if (err < 0) { | |
424 | printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); | |
425 | return err; | |
426 | } | |
427 | ||
8edb2192 MY |
428 | /* find or create "/memory" node. */ |
429 | nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); | |
430 | if (nodeoffset < 0) | |
35940de1 | 431 | return nodeoffset; |
8edb2192 | 432 | |
3c927281 KG |
433 | err = fdt_setprop(blob, nodeoffset, "device_type", "memory", |
434 | sizeof("memory")); | |
435 | if (err < 0) { | |
436 | printf("WARNING: could not set %s %s.\n", "device_type", | |
437 | fdt_strerror(err)); | |
438 | return err; | |
439 | } | |
440 | ||
a6bd9e83 JR |
441 | addr_cell_len = get_cells_len(blob, "#address-cells"); |
442 | size_cell_len = get_cells_len(blob, "#size-cells"); | |
3c927281 | 443 | |
a6bd9e83 JR |
444 | for (bank = 0, len = 0; bank < banks; bank++) { |
445 | write_cell(tmp + len, start[bank], addr_cell_len); | |
446 | len += addr_cell_len; | |
447 | ||
448 | write_cell(tmp + len, size[bank], size_cell_len); | |
449 | len += size_cell_len; | |
3c927281 KG |
450 | } |
451 | ||
452 | err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); | |
453 | if (err < 0) { | |
454 | printf("WARNING: could not set %s %s.\n", | |
455 | "reg", fdt_strerror(err)); | |
456 | return err; | |
457 | } | |
458 | return 0; | |
459 | } | |
460 | ||
a6bd9e83 JR |
461 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
462 | { | |
463 | return fdt_fixup_memory_banks(blob, &start, &size, 1); | |
464 | } | |
465 | ||
ba37aa03 | 466 | void fdt_fixup_ethernet(void *fdt) |
ab544633 | 467 | { |
ba37aa03 KG |
468 | int node, i, j; |
469 | char enet[16], *tmp, *end; | |
064d55f8 | 470 | char mac[16]; |
ab544633 | 471 | const char *path; |
ba37aa03 | 472 | unsigned char mac_addr[6]; |
ab544633 KG |
473 | |
474 | node = fdt_path_offset(fdt, "/aliases"); | |
ba37aa03 KG |
475 | if (node < 0) |
476 | return; | |
477 | ||
b1f49ab8 DM |
478 | if (!getenv("ethaddr")) { |
479 | if (getenv("usbethaddr")) { | |
480 | strcpy(mac, "usbethaddr"); | |
481 | } else { | |
482 | debug("No ethernet MAC Address defined\n"); | |
483 | return; | |
484 | } | |
485 | } else { | |
486 | strcpy(mac, "ethaddr"); | |
487 | } | |
488 | ||
ba37aa03 KG |
489 | i = 0; |
490 | while ((tmp = getenv(mac)) != NULL) { | |
491 | sprintf(enet, "ethernet%d", i); | |
492 | path = fdt_getprop(fdt, node, enet, NULL); | |
493 | if (!path) { | |
494 | debug("No alias for %s\n", enet); | |
495 | sprintf(mac, "eth%daddr", ++i); | |
496 | continue; | |
ab544633 | 497 | } |
ba37aa03 KG |
498 | |
499 | for (j = 0; j < 6; j++) { | |
500 | mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; | |
501 | if (tmp) | |
502 | tmp = (*end) ? end+1 : end; | |
ab544633 | 503 | } |
ba37aa03 KG |
504 | |
505 | do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); | |
506 | do_fixup_by_path(fdt, path, "local-mac-address", | |
507 | &mac_addr, 6, 1); | |
508 | ||
509 | sprintf(mac, "eth%daddr", ++i); | |
ab544633 KG |
510 | } |
511 | } | |
18e69a35 | 512 | |
3082d234 KG |
513 | /* Resize the fdt to its actual size + a bit of padding */ |
514 | int fdt_resize(void *blob) | |
515 | { | |
516 | int i; | |
517 | uint64_t addr, size; | |
518 | int total, ret; | |
519 | uint actualsize; | |
520 | ||
521 | if (!blob) | |
522 | return 0; | |
523 | ||
524 | total = fdt_num_mem_rsv(blob); | |
525 | for (i = 0; i < total; i++) { | |
526 | fdt_get_mem_rsv(blob, i, &addr, &size); | |
92549358 | 527 | if (addr == (uintptr_t)blob) { |
3082d234 KG |
528 | fdt_del_mem_rsv(blob, i); |
529 | break; | |
530 | } | |
531 | } | |
532 | ||
f242a088 PK |
533 | /* |
534 | * Calculate the actual size of the fdt | |
3840ebfa FW |
535 | * plus the size needed for 5 fdt_add_mem_rsv, one |
536 | * for the fdt itself and 4 for a possible initrd | |
537 | * ((initrd-start + initrd-end) * 2 (name & value)) | |
f242a088 | 538 | */ |
3082d234 | 539 | actualsize = fdt_off_dt_strings(blob) + |
3840ebfa | 540 | fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); |
3082d234 KG |
541 | |
542 | /* Make it so the fdt ends on a page boundary */ | |
92549358 SG |
543 | actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); |
544 | actualsize = actualsize - ((uintptr_t)blob & 0xfff); | |
3082d234 KG |
545 | |
546 | /* Change the fdt header to reflect the correct size */ | |
547 | fdt_set_totalsize(blob, actualsize); | |
548 | ||
549 | /* Add the new reservation */ | |
92549358 | 550 | ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize); |
3082d234 KG |
551 | if (ret < 0) |
552 | return ret; | |
553 | ||
554 | return actualsize; | |
555 | } | |
8ab451c4 KG |
556 | |
557 | #ifdef CONFIG_PCI | |
cfd700be | 558 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 |
8ab451c4 KG |
559 | |
560 | #define FDT_PCI_PREFETCH (0x40000000) | |
561 | #define FDT_PCI_MEM32 (0x02000000) | |
562 | #define FDT_PCI_IO (0x01000000) | |
563 | #define FDT_PCI_MEM64 (0x03000000) | |
564 | ||
565 | int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { | |
566 | ||
567 | int addrcell, sizecell, len, r; | |
568 | u32 *dma_range; | |
569 | /* sized based on pci addr cells, size-cells, & address-cells */ | |
570 | u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; | |
571 | ||
572 | addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); | |
573 | sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); | |
574 | ||
575 | dma_range = &dma_ranges[0]; | |
576 | for (r = 0; r < hose->region_count; r++) { | |
577 | u64 bus_start, phys_start, size; | |
578 | ||
ff4e66e9 KG |
579 | /* skip if !PCI_REGION_SYS_MEMORY */ |
580 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) | |
8ab451c4 KG |
581 | continue; |
582 | ||
583 | bus_start = (u64)hose->regions[r].bus_start; | |
584 | phys_start = (u64)hose->regions[r].phys_start; | |
585 | size = (u64)hose->regions[r].size; | |
586 | ||
587 | dma_range[0] = 0; | |
cfd700be | 588 | if (size >= 0x100000000ull) |
8ab451c4 KG |
589 | dma_range[0] |= FDT_PCI_MEM64; |
590 | else | |
591 | dma_range[0] |= FDT_PCI_MEM32; | |
592 | if (hose->regions[r].flags & PCI_REGION_PREFETCH) | |
593 | dma_range[0] |= FDT_PCI_PREFETCH; | |
594 | #ifdef CONFIG_SYS_PCI_64BIT | |
595 | dma_range[1] = bus_start >> 32; | |
596 | #else | |
597 | dma_range[1] = 0; | |
598 | #endif | |
599 | dma_range[2] = bus_start & 0xffffffff; | |
600 | ||
601 | if (addrcell == 2) { | |
602 | dma_range[3] = phys_start >> 32; | |
603 | dma_range[4] = phys_start & 0xffffffff; | |
604 | } else { | |
605 | dma_range[3] = phys_start & 0xffffffff; | |
606 | } | |
607 | ||
608 | if (sizecell == 2) { | |
609 | dma_range[3 + addrcell + 0] = size >> 32; | |
610 | dma_range[3 + addrcell + 1] = size & 0xffffffff; | |
611 | } else { | |
612 | dma_range[3 + addrcell + 0] = size & 0xffffffff; | |
613 | } | |
614 | ||
615 | dma_range += (3 + addrcell + sizecell); | |
616 | } | |
617 | ||
618 | len = dma_range - &dma_ranges[0]; | |
619 | if (len) | |
620 | fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); | |
621 | ||
622 | return 0; | |
623 | } | |
624 | #endif | |
30d45c0d SR |
625 | |
626 | #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE | |
8a805df1 SR |
627 | /* |
628 | * Provide a weak default function to return the flash bank size. | |
629 | * There might be multiple non-identical flash chips connected to one | |
630 | * chip-select, so we need to pass an index as well. | |
631 | */ | |
632 | u32 __flash_get_bank_size(int cs, int idx) | |
633 | { | |
634 | extern flash_info_t flash_info[]; | |
635 | ||
636 | /* | |
637 | * As default, a simple 1:1 mapping is provided. Boards with | |
638 | * a different mapping need to supply a board specific mapping | |
639 | * routine. | |
640 | */ | |
641 | return flash_info[cs].size; | |
642 | } | |
643 | u32 flash_get_bank_size(int cs, int idx) | |
644 | __attribute__((weak, alias("__flash_get_bank_size"))); | |
645 | ||
30d45c0d SR |
646 | /* |
647 | * This function can be used to update the size in the "reg" property | |
8a805df1 | 648 | * of all NOR FLASH device nodes. This is necessary for boards with |
30d45c0d SR |
649 | * non-fixed NOR FLASH sizes. |
650 | */ | |
8a805df1 | 651 | int fdt_fixup_nor_flash_size(void *blob) |
30d45c0d SR |
652 | { |
653 | char compat[][16] = { "cfi-flash", "jedec-flash" }; | |
654 | int off; | |
655 | int len; | |
656 | struct fdt_property *prop; | |
2778a014 | 657 | u32 *reg, *reg2; |
30d45c0d SR |
658 | int i; |
659 | ||
660 | for (i = 0; i < 2; i++) { | |
661 | off = fdt_node_offset_by_compatible(blob, -1, compat[i]); | |
662 | while (off != -FDT_ERR_NOTFOUND) { | |
8a805df1 SR |
663 | int idx; |
664 | ||
30d45c0d | 665 | /* |
8a805df1 SR |
666 | * Found one compatible node, so fixup the size |
667 | * int its reg properties | |
30d45c0d SR |
668 | */ |
669 | prop = fdt_get_property_w(blob, off, "reg", &len); | |
670 | if (prop) { | |
8a805df1 SR |
671 | int tuple_size = 3 * sizeof(reg); |
672 | ||
673 | /* | |
674 | * There might be multiple reg-tuples, | |
675 | * so loop through them all | |
676 | */ | |
2778a014 SR |
677 | reg = reg2 = (u32 *)&prop->data[0]; |
678 | for (idx = 0; idx < (len / tuple_size); idx++) { | |
8a805df1 SR |
679 | /* |
680 | * Update size in reg property | |
681 | */ | |
682 | reg[2] = flash_get_bank_size(reg[0], | |
683 | idx); | |
2778a014 SR |
684 | |
685 | /* | |
686 | * Point to next reg tuple | |
687 | */ | |
688 | reg += 3; | |
30d45c0d | 689 | } |
2778a014 SR |
690 | |
691 | fdt_setprop(blob, off, "reg", reg2, len); | |
30d45c0d SR |
692 | } |
693 | ||
694 | /* Move to next compatible node */ | |
695 | off = fdt_node_offset_by_compatible(blob, off, | |
696 | compat[i]); | |
697 | } | |
698 | } | |
699 | ||
8a805df1 | 700 | return 0; |
30d45c0d SR |
701 | } |
702 | #endif | |
3c950e2e | 703 | |
cb2707af MM |
704 | int fdt_increase_size(void *fdt, int add_len) |
705 | { | |
706 | int newlen; | |
707 | ||
708 | newlen = fdt_totalsize(fdt) + add_len; | |
709 | ||
710 | /* Open in place with a new len */ | |
711 | return fdt_open_into(fdt, fdt, newlen); | |
712 | } | |
713 | ||
3c950e2e AG |
714 | #ifdef CONFIG_FDT_FIXUP_PARTITIONS |
715 | #include <jffs2/load_kernel.h> | |
716 | #include <mtd_node.h> | |
717 | ||
718 | struct reg_cell { | |
719 | unsigned int r0; | |
720 | unsigned int r1; | |
721 | }; | |
722 | ||
723 | int fdt_del_subnodes(const void *blob, int parent_offset) | |
724 | { | |
725 | int off, ndepth; | |
726 | int ret; | |
727 | ||
728 | for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); | |
729 | (off >= 0) && (ndepth > 0); | |
730 | off = fdt_next_node(blob, off, &ndepth)) { | |
731 | if (ndepth == 1) { | |
732 | debug("delete %s: offset: %x\n", | |
733 | fdt_get_name(blob, off, 0), off); | |
734 | ret = fdt_del_node((void *)blob, off); | |
735 | if (ret < 0) { | |
736 | printf("Can't delete node: %s\n", | |
737 | fdt_strerror(ret)); | |
738 | return ret; | |
739 | } else { | |
740 | ndepth = 0; | |
741 | off = parent_offset; | |
742 | } | |
743 | } | |
744 | } | |
745 | return 0; | |
746 | } | |
747 | ||
3c950e2e AG |
748 | int fdt_del_partitions(void *blob, int parent_offset) |
749 | { | |
750 | const void *prop; | |
751 | int ndepth = 0; | |
752 | int off; | |
753 | int ret; | |
754 | ||
755 | off = fdt_next_node(blob, parent_offset, &ndepth); | |
756 | if (off > 0 && ndepth == 1) { | |
757 | prop = fdt_getprop(blob, off, "label", NULL); | |
758 | if (prop == NULL) { | |
759 | /* | |
760 | * Could not find label property, nand {}; node? | |
761 | * Check subnode, delete partitions there if any. | |
762 | */ | |
763 | return fdt_del_partitions(blob, off); | |
764 | } else { | |
765 | ret = fdt_del_subnodes(blob, parent_offset); | |
766 | if (ret < 0) { | |
767 | printf("Can't remove subnodes: %s\n", | |
768 | fdt_strerror(ret)); | |
769 | return ret; | |
770 | } | |
771 | } | |
772 | } | |
773 | return 0; | |
774 | } | |
775 | ||
776 | int fdt_node_set_part_info(void *blob, int parent_offset, | |
777 | struct mtd_device *dev) | |
778 | { | |
779 | struct list_head *pentry; | |
780 | struct part_info *part; | |
781 | struct reg_cell cell; | |
782 | int off, ndepth = 0; | |
783 | int part_num, ret; | |
784 | char buf[64]; | |
785 | ||
786 | ret = fdt_del_partitions(blob, parent_offset); | |
787 | if (ret < 0) | |
788 | return ret; | |
789 | ||
790 | /* | |
791 | * Check if it is nand {}; subnode, adjust | |
792 | * the offset in this case | |
793 | */ | |
794 | off = fdt_next_node(blob, parent_offset, &ndepth); | |
795 | if (off > 0 && ndepth == 1) | |
796 | parent_offset = off; | |
797 | ||
798 | part_num = 0; | |
799 | list_for_each_prev(pentry, &dev->parts) { | |
800 | int newoff; | |
801 | ||
802 | part = list_entry(pentry, struct part_info, link); | |
803 | ||
06503f16 | 804 | debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", |
3c950e2e AG |
805 | part_num, part->name, part->size, |
806 | part->offset, part->mask_flags); | |
807 | ||
06503f16 | 808 | sprintf(buf, "partition@%llx", part->offset); |
3c950e2e AG |
809 | add_sub: |
810 | ret = fdt_add_subnode(blob, parent_offset, buf); | |
811 | if (ret == -FDT_ERR_NOSPACE) { | |
812 | ret = fdt_increase_size(blob, 512); | |
813 | if (!ret) | |
814 | goto add_sub; | |
815 | else | |
816 | goto err_size; | |
817 | } else if (ret < 0) { | |
818 | printf("Can't add partition node: %s\n", | |
819 | fdt_strerror(ret)); | |
820 | return ret; | |
821 | } | |
822 | newoff = ret; | |
823 | ||
824 | /* Check MTD_WRITEABLE_CMD flag */ | |
825 | if (part->mask_flags & 1) { | |
826 | add_ro: | |
827 | ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); | |
828 | if (ret == -FDT_ERR_NOSPACE) { | |
829 | ret = fdt_increase_size(blob, 512); | |
830 | if (!ret) | |
831 | goto add_ro; | |
832 | else | |
833 | goto err_size; | |
834 | } else if (ret < 0) | |
835 | goto err_prop; | |
836 | } | |
837 | ||
838 | cell.r0 = cpu_to_fdt32(part->offset); | |
839 | cell.r1 = cpu_to_fdt32(part->size); | |
840 | add_reg: | |
841 | ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell)); | |
842 | if (ret == -FDT_ERR_NOSPACE) { | |
843 | ret = fdt_increase_size(blob, 512); | |
844 | if (!ret) | |
845 | goto add_reg; | |
846 | else | |
847 | goto err_size; | |
848 | } else if (ret < 0) | |
849 | goto err_prop; | |
850 | ||
851 | add_label: | |
852 | ret = fdt_setprop_string(blob, newoff, "label", part->name); | |
853 | if (ret == -FDT_ERR_NOSPACE) { | |
854 | ret = fdt_increase_size(blob, 512); | |
855 | if (!ret) | |
856 | goto add_label; | |
857 | else | |
858 | goto err_size; | |
859 | } else if (ret < 0) | |
860 | goto err_prop; | |
861 | ||
862 | part_num++; | |
863 | } | |
864 | return 0; | |
865 | err_size: | |
866 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); | |
867 | return ret; | |
868 | err_prop: | |
869 | printf("Can't add property: %s\n", fdt_strerror(ret)); | |
870 | return ret; | |
871 | } | |
872 | ||
873 | /* | |
874 | * Update partitions in nor/nand nodes using info from | |
875 | * mtdparts environment variable. The nodes to update are | |
876 | * specified by node_info structure which contains mtd device | |
877 | * type and compatible string: E. g. the board code in | |
878 | * ft_board_setup() could use: | |
879 | * | |
880 | * struct node_info nodes[] = { | |
881 | * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, | |
882 | * { "cfi-flash", MTD_DEV_TYPE_NOR, }, | |
883 | * }; | |
884 | * | |
885 | * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); | |
886 | */ | |
887 | void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size) | |
888 | { | |
889 | struct node_info *ni = node_info; | |
890 | struct mtd_device *dev; | |
891 | char *parts; | |
892 | int i, idx; | |
893 | int noff; | |
894 | ||
895 | parts = getenv("mtdparts"); | |
896 | if (!parts) | |
897 | return; | |
898 | ||
899 | if (mtdparts_init() != 0) | |
900 | return; | |
901 | ||
902 | for (i = 0; i < node_info_size; i++) { | |
903 | idx = 0; | |
904 | noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat); | |
905 | while (noff != -FDT_ERR_NOTFOUND) { | |
906 | debug("%s: %s, mtd dev type %d\n", | |
907 | fdt_get_name(blob, noff, 0), | |
908 | ni[i].compat, ni[i].type); | |
909 | dev = device_find(ni[i].type, idx++); | |
910 | if (dev) { | |
911 | if (fdt_node_set_part_info(blob, noff, dev)) | |
912 | return; /* return on error */ | |
913 | } | |
914 | ||
915 | /* Jump to next flash node */ | |
916 | noff = fdt_node_offset_by_compatible(blob, noff, | |
917 | ni[i].compat); | |
918 | } | |
919 | } | |
920 | } | |
921 | #endif | |
49b97d9c KG |
922 | |
923 | void fdt_del_node_and_alias(void *blob, const char *alias) | |
924 | { | |
925 | int off = fdt_path_offset(blob, alias); | |
926 | ||
927 | if (off < 0) | |
928 | return; | |
929 | ||
930 | fdt_del_node(blob, off); | |
931 | ||
932 | off = fdt_path_offset(blob, "/aliases"); | |
933 | fdt_delprop(blob, off, alias); | |
934 | } | |
a0342c08 KG |
935 | |
936 | /* Helper to read a big number; size is in cells (not bytes) */ | |
8aa5ec6e | 937 | static inline u64 of_read_number(const fdt32_t *cell, int size) |
a0342c08 KG |
938 | { |
939 | u64 r = 0; | |
940 | while (size--) | |
8aa5ec6e | 941 | r = (r << 32) | fdt32_to_cpu(*(cell++)); |
a0342c08 KG |
942 | return r; |
943 | } | |
944 | ||
a0342c08 KG |
945 | #define PRu64 "%llx" |
946 | ||
947 | /* Max address size we deal with */ | |
948 | #define OF_MAX_ADDR_CELLS 4 | |
949 | #define OF_BAD_ADDR ((u64)-1) | |
950 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ | |
951 | (ns) > 0) | |
952 | ||
953 | /* Debug utility */ | |
954 | #ifdef DEBUG | |
8aa5ec6e | 955 | static void of_dump_addr(const char *s, const fdt32_t *addr, int na) |
a0342c08 KG |
956 | { |
957 | printf("%s", s); | |
958 | while(na--) | |
959 | printf(" %08x", *(addr++)); | |
960 | printf("\n"); | |
961 | } | |
962 | #else | |
8aa5ec6e | 963 | static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { } |
a0342c08 KG |
964 | #endif |
965 | ||
966 | /* Callbacks for bus specific translators */ | |
967 | struct of_bus { | |
968 | const char *name; | |
969 | const char *addresses; | |
6395f318 | 970 | void (*count_cells)(void *blob, int parentoffset, |
a0342c08 | 971 | int *addrc, int *sizec); |
8aa5ec6e | 972 | u64 (*map)(fdt32_t *addr, const fdt32_t *range, |
a0342c08 | 973 | int na, int ns, int pna); |
8aa5ec6e | 974 | int (*translate)(fdt32_t *addr, u64 offset, int na); |
a0342c08 KG |
975 | }; |
976 | ||
977 | /* Default translator (generic bus) */ | |
6395f318 | 978 | static void of_bus_default_count_cells(void *blob, int parentoffset, |
a0342c08 KG |
979 | int *addrc, int *sizec) |
980 | { | |
8aa5ec6e | 981 | const fdt32_t *prop; |
6395f318 SW |
982 | |
983 | if (addrc) { | |
984 | prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL); | |
985 | if (prop) | |
8aa5ec6e | 986 | *addrc = be32_to_cpup(prop); |
6395f318 SW |
987 | else |
988 | *addrc = 2; | |
989 | } | |
990 | ||
991 | if (sizec) { | |
992 | prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); | |
993 | if (prop) | |
8aa5ec6e | 994 | *sizec = be32_to_cpup(prop); |
6395f318 SW |
995 | else |
996 | *sizec = 1; | |
997 | } | |
a0342c08 KG |
998 | } |
999 | ||
8aa5ec6e | 1000 | static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range, |
a0342c08 KG |
1001 | int na, int ns, int pna) |
1002 | { | |
1003 | u64 cp, s, da; | |
1004 | ||
1005 | cp = of_read_number(range, na); | |
1006 | s = of_read_number(range + na + pna, ns); | |
1007 | da = of_read_number(addr, na); | |
1008 | ||
1009 | debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", | |
1010 | cp, s, da); | |
1011 | ||
1012 | if (da < cp || da >= (cp + s)) | |
1013 | return OF_BAD_ADDR; | |
1014 | return da - cp; | |
1015 | } | |
1016 | ||
8aa5ec6e | 1017 | static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na) |
a0342c08 KG |
1018 | { |
1019 | u64 a = of_read_number(addr, na); | |
1020 | memset(addr, 0, na * 4); | |
1021 | a += offset; | |
1022 | if (na > 1) | |
8aa5ec6e KP |
1023 | addr[na - 2] = cpu_to_fdt32(a >> 32); |
1024 | addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu); | |
a0342c08 KG |
1025 | |
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | /* Array of bus specific translators */ | |
1030 | static struct of_bus of_busses[] = { | |
1031 | /* Default */ | |
1032 | { | |
1033 | .name = "default", | |
1034 | .addresses = "reg", | |
1035 | .count_cells = of_bus_default_count_cells, | |
1036 | .map = of_bus_default_map, | |
1037 | .translate = of_bus_default_translate, | |
1038 | }, | |
1039 | }; | |
1040 | ||
1041 | static int of_translate_one(void * blob, int parent, struct of_bus *bus, | |
8aa5ec6e | 1042 | struct of_bus *pbus, fdt32_t *addr, |
a0342c08 KG |
1043 | int na, int ns, int pna, const char *rprop) |
1044 | { | |
8aa5ec6e | 1045 | const fdt32_t *ranges; |
a0342c08 KG |
1046 | int rlen; |
1047 | int rone; | |
1048 | u64 offset = OF_BAD_ADDR; | |
1049 | ||
1050 | /* Normally, an absence of a "ranges" property means we are | |
1051 | * crossing a non-translatable boundary, and thus the addresses | |
1052 | * below the current not cannot be converted to CPU physical ones. | |
1053 | * Unfortunately, while this is very clear in the spec, it's not | |
1054 | * what Apple understood, and they do have things like /uni-n or | |
1055 | * /ht nodes with no "ranges" property and a lot of perfectly | |
1056 | * useable mapped devices below them. Thus we treat the absence of | |
1057 | * "ranges" as equivalent to an empty "ranges" property which means | |
1058 | * a 1:1 translation at that level. It's up to the caller not to try | |
1059 | * to translate addresses that aren't supposed to be translated in | |
1060 | * the first place. --BenH. | |
1061 | */ | |
8aa5ec6e | 1062 | ranges = fdt_getprop(blob, parent, rprop, &rlen); |
a0342c08 KG |
1063 | if (ranges == NULL || rlen == 0) { |
1064 | offset = of_read_number(addr, na); | |
1065 | memset(addr, 0, pna * 4); | |
1066 | debug("OF: no ranges, 1:1 translation\n"); | |
1067 | goto finish; | |
1068 | } | |
1069 | ||
1070 | debug("OF: walking ranges...\n"); | |
1071 | ||
1072 | /* Now walk through the ranges */ | |
1073 | rlen /= 4; | |
1074 | rone = na + pna + ns; | |
1075 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
1076 | offset = bus->map(addr, ranges, na, ns, pna); | |
1077 | if (offset != OF_BAD_ADDR) | |
1078 | break; | |
1079 | } | |
1080 | if (offset == OF_BAD_ADDR) { | |
1081 | debug("OF: not found !\n"); | |
1082 | return 1; | |
1083 | } | |
1084 | memcpy(addr, ranges + na, 4 * pna); | |
1085 | ||
1086 | finish: | |
1087 | of_dump_addr("OF: parent translation for:", addr, pna); | |
1088 | debug("OF: with offset: "PRu64"\n", offset); | |
1089 | ||
1090 | /* Translate it into parent bus space */ | |
1091 | return pbus->translate(addr, offset, pna); | |
1092 | } | |
1093 | ||
1094 | /* | |
1095 | * Translate an address from the device-tree into a CPU physical address, | |
1096 | * this walks up the tree and applies the various bus mappings on the | |
1097 | * way. | |
1098 | * | |
1099 | * Note: We consider that crossing any level with #size-cells == 0 to mean | |
1100 | * that translation is impossible (that is we are not dealing with a value | |
1101 | * that can be mapped to a cpu physical address). This is not really specified | |
1102 | * that way, but this is traditionally the way IBM at least do things | |
1103 | */ | |
8aa5ec6e KP |
1104 | static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr, |
1105 | const char *rprop) | |
a0342c08 KG |
1106 | { |
1107 | int parent; | |
1108 | struct of_bus *bus, *pbus; | |
8aa5ec6e | 1109 | fdt32_t addr[OF_MAX_ADDR_CELLS]; |
a0342c08 KG |
1110 | int na, ns, pna, pns; |
1111 | u64 result = OF_BAD_ADDR; | |
1112 | ||
1113 | debug("OF: ** translation for device %s **\n", | |
1114 | fdt_get_name(blob, node_offset, NULL)); | |
1115 | ||
1116 | /* Get parent & match bus type */ | |
1117 | parent = fdt_parent_offset(blob, node_offset); | |
1118 | if (parent < 0) | |
1119 | goto bail; | |
1120 | bus = &of_busses[0]; | |
1121 | ||
1122 | /* Cound address cells & copy address locally */ | |
6395f318 | 1123 | bus->count_cells(blob, parent, &na, &ns); |
a0342c08 KG |
1124 | if (!OF_CHECK_COUNTS(na, ns)) { |
1125 | printf("%s: Bad cell count for %s\n", __FUNCTION__, | |
1126 | fdt_get_name(blob, node_offset, NULL)); | |
1127 | goto bail; | |
1128 | } | |
1129 | memcpy(addr, in_addr, na * 4); | |
1130 | ||
1131 | debug("OF: bus is %s (na=%d, ns=%d) on %s\n", | |
1132 | bus->name, na, ns, fdt_get_name(blob, parent, NULL)); | |
1133 | of_dump_addr("OF: translating address:", addr, na); | |
1134 | ||
1135 | /* Translate */ | |
1136 | for (;;) { | |
1137 | /* Switch to parent bus */ | |
1138 | node_offset = parent; | |
1139 | parent = fdt_parent_offset(blob, node_offset); | |
1140 | ||
1141 | /* If root, we have finished */ | |
1142 | if (parent < 0) { | |
1143 | debug("OF: reached root node\n"); | |
1144 | result = of_read_number(addr, na); | |
1145 | break; | |
1146 | } | |
1147 | ||
1148 | /* Get new parent bus and counts */ | |
1149 | pbus = &of_busses[0]; | |
6395f318 | 1150 | pbus->count_cells(blob, parent, &pna, &pns); |
a0342c08 KG |
1151 | if (!OF_CHECK_COUNTS(pna, pns)) { |
1152 | printf("%s: Bad cell count for %s\n", __FUNCTION__, | |
1153 | fdt_get_name(blob, node_offset, NULL)); | |
1154 | break; | |
1155 | } | |
1156 | ||
1157 | debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", | |
1158 | pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); | |
1159 | ||
1160 | /* Apply bus translation */ | |
1161 | if (of_translate_one(blob, node_offset, bus, pbus, | |
1162 | addr, na, ns, pna, rprop)) | |
1163 | break; | |
1164 | ||
1165 | /* Complete the move up one level */ | |
1166 | na = pna; | |
1167 | ns = pns; | |
1168 | bus = pbus; | |
1169 | ||
1170 | of_dump_addr("OF: one level translation:", addr, na); | |
1171 | } | |
1172 | bail: | |
1173 | ||
1174 | return result; | |
1175 | } | |
1176 | ||
8aa5ec6e | 1177 | u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr) |
a0342c08 KG |
1178 | { |
1179 | return __of_translate_address(blob, node_offset, in_addr, "ranges"); | |
1180 | } | |
75e73afd KG |
1181 | |
1182 | /** | |
1183 | * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and | |
1184 | * who's reg property matches a physical cpu address | |
1185 | * | |
1186 | * @blob: ptr to device tree | |
1187 | * @compat: compatiable string to match | |
1188 | * @compat_off: property name | |
1189 | * | |
1190 | */ | |
1191 | int fdt_node_offset_by_compat_reg(void *blob, const char *compat, | |
1192 | phys_addr_t compat_off) | |
1193 | { | |
1194 | int len, off = fdt_node_offset_by_compatible(blob, -1, compat); | |
1195 | while (off != -FDT_ERR_NOTFOUND) { | |
8aa5ec6e | 1196 | const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len); |
75e73afd KG |
1197 | if (reg) { |
1198 | if (compat_off == fdt_translate_address(blob, off, reg)) | |
1199 | return off; | |
1200 | } | |
1201 | off = fdt_node_offset_by_compatible(blob, off, compat); | |
1202 | } | |
1203 | ||
1204 | return -FDT_ERR_NOTFOUND; | |
1205 | } | |
1206 | ||
b4b847e9 KG |
1207 | /** |
1208 | * fdt_alloc_phandle: Return next free phandle value | |
1209 | * | |
1210 | * @blob: ptr to device tree | |
1211 | */ | |
1212 | int fdt_alloc_phandle(void *blob) | |
1213 | { | |
50bf17bd | 1214 | int offset, phandle = 0; |
b4b847e9 KG |
1215 | |
1216 | for (offset = fdt_next_node(blob, -1, NULL); offset >= 0; | |
1217 | offset = fdt_next_node(blob, offset, NULL)) { | |
50bf17bd | 1218 | phandle = max(phandle, fdt_get_phandle(blob, offset)); |
b4b847e9 | 1219 | } |
75e73afd | 1220 | |
b4b847e9 KG |
1221 | return phandle + 1; |
1222 | } | |
beca5a5f | 1223 | |
a8d2a75d | 1224 | /* |
f117c0f0 | 1225 | * fdt_set_phandle: Create a phandle property for the given node |
a8d2a75d GVB |
1226 | * |
1227 | * @fdt: ptr to device tree | |
1228 | * @nodeoffset: node to update | |
1229 | * @phandle: phandle value to set (must be unique) | |
f117c0f0 KG |
1230 | */ |
1231 | int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) | |
a8d2a75d GVB |
1232 | { |
1233 | int ret; | |
1234 | ||
1235 | #ifdef DEBUG | |
1236 | int off = fdt_node_offset_by_phandle(fdt, phandle); | |
1237 | ||
1238 | if ((off >= 0) && (off != nodeoffset)) { | |
1239 | char buf[64]; | |
1240 | ||
1241 | fdt_get_path(fdt, nodeoffset, buf, sizeof(buf)); | |
1242 | printf("Trying to update node %s with phandle %u ", | |
1243 | buf, phandle); | |
1244 | ||
1245 | fdt_get_path(fdt, off, buf, sizeof(buf)); | |
1246 | printf("that already exists in node %s.\n", buf); | |
1247 | return -FDT_ERR_BADPHANDLE; | |
1248 | } | |
1249 | #endif | |
1250 | ||
1251 | ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle); | |
1252 | if (ret < 0) | |
1253 | return ret; | |
1254 | ||
1255 | /* | |
1256 | * For now, also set the deprecated "linux,phandle" property, so that we | |
1257 | * don't break older kernels. | |
1258 | */ | |
1259 | ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle); | |
1260 | ||
1261 | return ret; | |
1262 | } | |
1263 | ||
10aeabd1 KG |
1264 | /* |
1265 | * fdt_create_phandle: Create a phandle property for the given node | |
1266 | * | |
1267 | * @fdt: ptr to device tree | |
1268 | * @nodeoffset: node to update | |
1269 | */ | |
3c927ccc | 1270 | unsigned int fdt_create_phandle(void *fdt, int nodeoffset) |
10aeabd1 KG |
1271 | { |
1272 | /* see if there is a phandle already */ | |
1273 | int phandle = fdt_get_phandle(fdt, nodeoffset); | |
1274 | ||
1275 | /* if we got 0, means no phandle so create one */ | |
1276 | if (phandle == 0) { | |
3c927ccc TT |
1277 | int ret; |
1278 | ||
10aeabd1 | 1279 | phandle = fdt_alloc_phandle(fdt); |
3c927ccc TT |
1280 | ret = fdt_set_phandle(fdt, nodeoffset, phandle); |
1281 | if (ret < 0) { | |
1282 | printf("Can't set phandle %u: %s\n", phandle, | |
1283 | fdt_strerror(ret)); | |
1284 | return 0; | |
1285 | } | |
10aeabd1 KG |
1286 | } |
1287 | ||
1288 | return phandle; | |
1289 | } | |
1290 | ||
2a523f52 SL |
1291 | /* |
1292 | * fdt_set_node_status: Set status for the given node | |
1293 | * | |
1294 | * @fdt: ptr to device tree | |
1295 | * @nodeoffset: node to update | |
1296 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, | |
1297 | * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE | |
1298 | * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE | |
1299 | */ | |
1300 | int fdt_set_node_status(void *fdt, int nodeoffset, | |
1301 | enum fdt_status status, unsigned int error_code) | |
1302 | { | |
1303 | char buf[16]; | |
1304 | int ret = 0; | |
1305 | ||
1306 | if (nodeoffset < 0) | |
1307 | return nodeoffset; | |
1308 | ||
1309 | switch (status) { | |
1310 | case FDT_STATUS_OKAY: | |
1311 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay"); | |
1312 | break; | |
1313 | case FDT_STATUS_DISABLED: | |
1314 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled"); | |
1315 | break; | |
1316 | case FDT_STATUS_FAIL: | |
1317 | ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail"); | |
1318 | break; | |
1319 | case FDT_STATUS_FAIL_ERROR_CODE: | |
1320 | sprintf(buf, "fail-%d", error_code); | |
1321 | ret = fdt_setprop_string(fdt, nodeoffset, "status", buf); | |
1322 | break; | |
1323 | default: | |
1324 | printf("Invalid fdt status: %x\n", status); | |
1325 | ret = -1; | |
1326 | break; | |
1327 | } | |
1328 | ||
1329 | return ret; | |
1330 | } | |
1331 | ||
1332 | /* | |
1333 | * fdt_set_status_by_alias: Set status for the given node given an alias | |
1334 | * | |
1335 | * @fdt: ptr to device tree | |
1336 | * @alias: alias of node to update | |
1337 | * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, | |
1338 | * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE | |
1339 | * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE | |
1340 | */ | |
1341 | int fdt_set_status_by_alias(void *fdt, const char* alias, | |
1342 | enum fdt_status status, unsigned int error_code) | |
1343 | { | |
1344 | int offset = fdt_path_offset(fdt, alias); | |
1345 | ||
1346 | return fdt_set_node_status(fdt, offset, status, error_code); | |
1347 | } | |
1348 | ||
096eb3f5 | 1349 | #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD) |
beca5a5f AG |
1350 | int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf) |
1351 | { | |
1352 | int noff; | |
1353 | int ret; | |
1354 | ||
1355 | noff = fdt_node_offset_by_compatible(blob, -1, compat); | |
1356 | if (noff != -FDT_ERR_NOTFOUND) { | |
1357 | debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat); | |
1358 | add_edid: | |
1359 | ret = fdt_setprop(blob, noff, "edid", edid_buf, 128); | |
1360 | if (ret == -FDT_ERR_NOSPACE) { | |
1361 | ret = fdt_increase_size(blob, 512); | |
1362 | if (!ret) | |
1363 | goto add_edid; | |
1364 | else | |
1365 | goto err_size; | |
1366 | } else if (ret < 0) { | |
1367 | printf("Can't add property: %s\n", fdt_strerror(ret)); | |
1368 | return ret; | |
1369 | } | |
1370 | } | |
1371 | return 0; | |
1372 | err_size: | |
1373 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); | |
1374 | return ret; | |
1375 | } | |
1376 | #endif | |
bb682001 TT |
1377 | |
1378 | /* | |
1379 | * Verify the physical address of device tree node for a given alias | |
1380 | * | |
1381 | * This function locates the device tree node of a given alias, and then | |
1382 | * verifies that the physical address of that device matches the given | |
1383 | * parameter. It displays a message if there is a mismatch. | |
1384 | * | |
1385 | * Returns 1 on success, 0 on failure | |
1386 | */ | |
1387 | int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr) | |
1388 | { | |
1389 | const char *path; | |
8aa5ec6e | 1390 | const fdt32_t *reg; |
bb682001 TT |
1391 | int node, len; |
1392 | u64 dt_addr; | |
1393 | ||
1394 | path = fdt_getprop(fdt, anode, alias, NULL); | |
1395 | if (!path) { | |
1396 | /* If there's no such alias, then it's not a failure */ | |
1397 | return 1; | |
1398 | } | |
1399 | ||
1400 | node = fdt_path_offset(fdt, path); | |
1401 | if (node < 0) { | |
1402 | printf("Warning: device tree alias '%s' points to invalid " | |
1403 | "node %s.\n", alias, path); | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | reg = fdt_getprop(fdt, node, "reg", &len); | |
1408 | if (!reg) { | |
1409 | printf("Warning: device tree node '%s' has no address.\n", | |
1410 | path); | |
1411 | return 0; | |
1412 | } | |
1413 | ||
1414 | dt_addr = fdt_translate_address(fdt, node, reg); | |
1415 | if (addr != dt_addr) { | |
1416 | printf("Warning: U-Boot configured device %s at address %llx,\n" | |
1417 | " but the device tree has it address %llx.\n", | |
1418 | alias, addr, dt_addr); | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | return 1; | |
1423 | } | |
1424 | ||
1425 | /* | |
1426 | * Returns the base address of an SOC or PCI node | |
1427 | */ | |
1428 | u64 fdt_get_base_address(void *fdt, int node) | |
1429 | { | |
1430 | int size; | |
1431 | u32 naddr; | |
8aa5ec6e | 1432 | const fdt32_t *prop; |
bb682001 TT |
1433 | |
1434 | prop = fdt_getprop(fdt, node, "#address-cells", &size); | |
1435 | if (prop && size == 4) | |
8aa5ec6e | 1436 | naddr = be32_to_cpup(prop); |
bb682001 TT |
1437 | else |
1438 | naddr = 2; | |
1439 | ||
1440 | prop = fdt_getprop(fdt, node, "ranges", &size); | |
1441 | ||
1442 | return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0; | |
1443 | } | |
c48e6868 AG |
1444 | |
1445 | /* | |
1446 | * Read a property of size <prop_len>. Currently only supports 1 or 2 cells. | |
1447 | */ | |
1448 | static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off, | |
1449 | uint64_t *val, int cells) | |
1450 | { | |
1451 | const fdt32_t *prop32 = &prop[cell_off]; | |
1452 | const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off]; | |
1453 | ||
1454 | if ((cell_off + cells) > prop_len) | |
1455 | return -FDT_ERR_NOSPACE; | |
1456 | ||
1457 | switch (cells) { | |
1458 | case 1: | |
1459 | *val = fdt32_to_cpu(*prop32); | |
1460 | break; | |
1461 | case 2: | |
1462 | *val = fdt64_to_cpu(*prop64); | |
1463 | break; | |
1464 | default: | |
1465 | return -FDT_ERR_NOSPACE; | |
1466 | } | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | /** | |
1472 | * fdt_read_range - Read a node's n'th range property | |
1473 | * | |
1474 | * @fdt: ptr to device tree | |
1475 | * @node: offset of node | |
1476 | * @n: range index | |
1477 | * @child_addr: pointer to storage for the "child address" field | |
1478 | * @addr: pointer to storage for the CPU view translated physical start | |
1479 | * @len: pointer to storage for the range length | |
1480 | * | |
1481 | * Convenience function that reads and interprets a specific range out of | |
1482 | * a number of the "ranges" property array. | |
1483 | */ | |
1484 | int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr, | |
1485 | uint64_t *addr, uint64_t *len) | |
1486 | { | |
1487 | int pnode = fdt_parent_offset(fdt, node); | |
1488 | const fdt32_t *ranges; | |
1489 | int pacells; | |
1490 | int acells; | |
1491 | int scells; | |
1492 | int ranges_len; | |
1493 | int cell = 0; | |
1494 | int r = 0; | |
1495 | ||
1496 | /* | |
1497 | * The "ranges" property is an array of | |
1498 | * { <child address> <parent address> <size in child address space> } | |
1499 | * | |
1500 | * All 3 elements can span a diffent number of cells. Fetch their size. | |
1501 | */ | |
1502 | pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1); | |
1503 | acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1); | |
1504 | scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1); | |
1505 | ||
1506 | /* Now try to get the ranges property */ | |
1507 | ranges = fdt_getprop(fdt, node, "ranges", &ranges_len); | |
1508 | if (!ranges) | |
1509 | return -FDT_ERR_NOTFOUND; | |
1510 | ranges_len /= sizeof(uint32_t); | |
1511 | ||
1512 | /* Jump to the n'th entry */ | |
1513 | cell = n * (pacells + acells + scells); | |
1514 | ||
1515 | /* Read <child address> */ | |
1516 | if (child_addr) { | |
1517 | r = fdt_read_prop(ranges, ranges_len, cell, child_addr, | |
1518 | acells); | |
1519 | if (r) | |
1520 | return r; | |
1521 | } | |
1522 | cell += acells; | |
1523 | ||
1524 | /* Read <parent address> */ | |
1525 | if (addr) | |
1526 | *addr = fdt_translate_address(fdt, node, ranges + cell); | |
1527 | cell += pacells; | |
1528 | ||
1529 | /* Read <size in child address space> */ | |
1530 | if (len) { | |
1531 | r = fdt_read_prop(ranges, ranges_len, cell, len, scells); | |
1532 | if (r) | |
1533 | return r; | |
1534 | } | |
1535 | ||
1536 | return 0; | |
1537 | } |