]>
Commit | Line | Data |
---|---|---|
64dbbd40 GVB |
1 | /* |
2 | * (C) Copyright 2007 | |
3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com | |
4 | * | |
2a523f52 | 5 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
a0342c08 | 6 | * |
64dbbd40 GVB |
7 | * 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> | |
3e303f74 | 27 | #include <stdio_dev.h> |
64dbbd40 GVB |
28 | #include <linux/ctype.h> |
29 | #include <linux/types.h> | |
64dbbd40 GVB |
30 | #include <asm/global_data.h> |
31 | #include <fdt.h> | |
32 | #include <libfdt.h> | |
33 | #include <fdt_support.h> | |
151c8b09 | 34 | #include <exports.h> |
64dbbd40 GVB |
35 | |
36 | /* | |
37 | * Global data (for the gd->bd) | |
38 | */ | |
39 | DECLARE_GLOBAL_DATA_PTR; | |
40 | ||
3bed2aaf KG |
41 | /** |
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 | */ | |
07e12784 GB |
52 | u32 fdt_getprop_u32_default(const void *fdt, const char *path, |
53 | const char *prop, const u32 dflt) | |
3bed2aaf KG |
54 | { |
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) | |
de16606a | 64 | return fdt32_to_cpu(*val); |
3bed2aaf KG |
65 | else |
66 | return dflt; | |
67 | } | |
64dbbd40 | 68 | |
a3c2933e KG |
69 | /** |
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 | */ | |
81 | int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, | |
82 | const void *val, int len, int create) | |
83 | { | |
8d04f02f | 84 | int nodeoff = fdt_path_offset(fdt, node); |
a3c2933e KG |
85 | |
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 | ||
151c8b09 | 95 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
3e303f74 | 96 | |
036036d7 | 97 | #ifdef CONFIG_CONS_INDEX |
3e303f74 AV |
98 | static 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 | } | |
036036d7 | 109 | #endif |
3e303f74 | 110 | |
40777812 | 111 | static int fdt_fixup_stdout(void *fdt, int chosenoff) |
151c8b09 KG |
112 | { |
113 | int err = 0; | |
114 | #ifdef CONFIG_CONS_INDEX | |
115 | int node; | |
116 | char sername[9] = { 0 }; | |
117 | const char *path; | |
118 | ||
3e303f74 AV |
119 | fdt_fill_multisername(sername, sizeof(sername) - 1); |
120 | if (!sername[0]) | |
121 | sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); | |
151c8b09 KG |
122 | |
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); | |
40777812 | 132 | err = fdt_setprop(fdt, chosenoff, |
151c8b09 KG |
133 | "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 | ||
2a1a2cb6 | 149 | int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) |
64dbbd40 | 150 | { |
64dbbd40 | 151 | int nodeoffset; |
2a1a2cb6 KG |
152 | int err, j, total; |
153 | u32 tmp; | |
b60af3d4 | 154 | const char *path; |
2a1a2cb6 | 155 | uint64_t addr, size; |
64dbbd40 | 156 | |
2a1a2cb6 KG |
157 | /* 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; | |
64dbbd40 GVB |
164 | } |
165 | ||
2a1a2cb6 KG |
166 | /* just return if initrd_start/end aren't valid */ |
167 | if ((initrd_start == 0) || (initrd_end == 0)) | |
168 | return 0; | |
c28abb9c | 169 | |
2a1a2cb6 KG |
170 | 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; | |
c28abb9c | 181 | } |
2a1a2cb6 | 182 | } |
8d04f02f | 183 | |
ce6b27a8 | 184 | err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); |
2a1a2cb6 KG |
185 | if (err < 0) { |
186 | printf("fdt_initrd: %s\n", fdt_strerror(err)); | |
187 | return err; | |
188 | } | |
189 | ||
190 | 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)); | |
64dbbd40 | 204 | if (err < 0) { |
2a1a2cb6 KG |
205 | printf("WARNING: could not set linux,initrd-end %s.\n", |
206 | fdt_strerror(err)); | |
207 | ||
64dbbd40 GVB |
208 | return err; |
209 | } | |
210 | } | |
211 | ||
2a1a2cb6 KG |
212 | return 0; |
213 | } | |
214 | ||
56844a22 | 215 | int fdt_chosen(void *fdt, int force) |
2a1a2cb6 KG |
216 | { |
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 | ||
64dbbd40 GVB |
228 | /* |
229 | * Find the "chosen" node. | |
230 | */ | |
8d04f02f | 231 | nodeoffset = fdt_path_offset (fdt, "/chosen"); |
64dbbd40 GVB |
232 | |
233 | /* | |
b60af3d4 | 234 | * If there is no "chosen" node in the blob, create it. |
64dbbd40 GVB |
235 | */ |
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) { | |
5fe6be62 | 242 | printf("WARNING: could not create /chosen %s.\n", |
35ec398f | 243 | fdt_strerror(nodeoffset)); |
64dbbd40 GVB |
244 | return nodeoffset; |
245 | } | |
246 | } | |
247 | ||
248 | /* | |
b60af3d4 GVB |
249 | * 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. | |
64dbbd40 GVB |
252 | */ |
253 | str = getenv("bootargs"); | |
254 | if (str != NULL) { | |
b60af3d4 GVB |
255 | 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 | } | |
64dbbd40 | 263 | } |
2a1a2cb6 | 264 | |
151c8b09 | 265 | #ifdef CONFIG_OF_STDOUT_VIA_ALIAS |
b60af3d4 GVB |
266 | path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); |
267 | if ((path == NULL) || force) | |
268 | err = fdt_fixup_stdout(fdt, nodeoffset); | |
151c8b09 KG |
269 | #endif |
270 | ||
64dbbd40 | 271 | #ifdef OF_STDOUT_PATH |
b60af3d4 GVB |
272 | 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 | } | |
64dbbd40 GVB |
280 | #endif |
281 | ||
64dbbd40 GVB |
282 | return err; |
283 | } | |
284 | ||
e93becf8 KG |
285 | void 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; | |
d9ad115b | 290 | debug("Updating property '%s/%s' = ", path, prop); |
e93becf8 KG |
291 | 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 | ||
301 | void 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 | ||
9eb77cea KG |
308 | void 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; | |
d9ad115b | 316 | debug("Updating property '%s' = ", prop); |
9eb77cea KG |
317 | 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 | ||
329 | void 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 | ||
337 | void 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; | |
d9ad115b | 343 | debug("Updating property '%s' = ", prop); |
9eb77cea KG |
344 | 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 | ||
356 | void 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 | ||
a6bd9e83 JR |
363 | /* |
364 | * Get cells len in bytes | |
365 | * if #NNNN-cells property is 2 then len is 8 | |
366 | * otherwise len is 4 | |
367 | */ | |
368 | static int get_cells_len(void *blob, char *nr_cells_name) | |
369 | { | |
370 | const u32 *cell; | |
371 | ||
372 | cell = fdt_getprop(blob, 0, nr_cells_name, NULL); | |
de16606a | 373 | if (cell && fdt32_to_cpu(*cell) == 2) |
a6bd9e83 JR |
374 | return 8; |
375 | ||
376 | return 4; | |
377 | } | |
378 | ||
379 | /* | |
380 | * Write a 4 or 8 byte big endian cell | |
381 | */ | |
382 | static void write_cell(u8 *addr, u64 val, int size) | |
3c927281 | 383 | { |
a6bd9e83 JR |
384 | int shift = (size - 1) * 8; |
385 | while (size-- > 0) { | |
386 | *addr++ = (val >> shift) & 0xff; | |
387 | shift -= 8; | |
388 | } | |
389 | } | |
390 | ||
391 | int 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; | |
d13ffa66 | 395 | u8 tmp[banks * 16]; /* Up to 64-bit address + 64-bit size */ |
a6bd9e83 | 396 | int bank; |
3c927281 KG |
397 | |
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 | ||
a6bd9e83 JR |
421 | addr_cell_len = get_cells_len(blob, "#address-cells"); |
422 | size_cell_len = get_cells_len(blob, "#size-cells"); | |
3c927281 | 423 | |
a6bd9e83 JR |
424 | for (bank = 0, len = 0; bank < banks; bank++) { |
425 | write_cell(tmp + len, start[bank], addr_cell_len); | |
426 | len += addr_cell_len; | |
427 | ||
428 | write_cell(tmp + len, size[bank], size_cell_len); | |
429 | len += size_cell_len; | |
3c927281 KG |
430 | } |
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 | ||
a6bd9e83 JR |
441 | int fdt_fixup_memory(void *blob, u64 start, u64 size) |
442 | { | |
443 | return fdt_fixup_memory_banks(blob, &start, &size, 1); | |
444 | } | |
445 | ||
ba37aa03 | 446 | void fdt_fixup_ethernet(void *fdt) |
ab544633 | 447 | { |
ba37aa03 KG |
448 | int node, i, j; |
449 | char enet[16], *tmp, *end; | |
450 | char mac[16] = "ethaddr"; | |
ab544633 | 451 | const char *path; |
ba37aa03 | 452 | unsigned char mac_addr[6]; |
ab544633 KG |
453 | |
454 | node = fdt_path_offset(fdt, "/aliases"); | |
ba37aa03 KG |
455 | 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; | |
ab544633 | 466 | } |
ba37aa03 KG |
467 | |
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; | |
ab544633 | 472 | } |
ba37aa03 KG |
473 | |
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); | |
ab544633 KG |
479 | } |
480 | } | |
18e69a35 | 481 | |
3082d234 KG |
482 | /* Resize the fdt to its actual size + a bit of padding */ |
483 | int 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); | |
92549358 | 496 | if (addr == (uintptr_t)blob) { |
3082d234 KG |
497 | fdt_del_mem_rsv(blob, i); |
498 | break; | |
499 | } | |
500 | } | |
501 | ||
f242a088 PK |
502 | /* |
503 | * Calculate the actual size of the fdt | |
3840ebfa FW |
504 | * 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)) | |
f242a088 | 507 | */ |
3082d234 | 508 | actualsize = fdt_off_dt_strings(blob) + |
3840ebfa | 509 | fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); |
3082d234 KG |
510 | |
511 | /* Make it so the fdt ends on a page boundary */ | |
92549358 SG |
512 | actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); |
513 | actualsize = actualsize - ((uintptr_t)blob & 0xfff); | |
3082d234 KG |
514 | |
515 | /* Change the fdt header to reflect the correct size */ | |
516 | fdt_set_totalsize(blob, actualsize); | |
517 | ||
518 | /* Add the new reservation */ | |
92549358 | 519 | ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize); |
3082d234 KG |
520 | if (ret < 0) |
521 | return ret; | |
522 | ||
523 | return actualsize; | |
524 | } | |
8ab451c4 KG |
525 | |
526 | #ifdef CONFIG_PCI | |
cfd700be | 527 | #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 |
8ab451c4 KG |
528 | |
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 | ||
534 | int 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 | ||
ff4e66e9 KG |
548 | /* skip if !PCI_REGION_SYS_MEMORY */ |
549 | if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) | |
8ab451c4 KG |
550 | 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; | |
cfd700be | 557 | if (size >= 0x100000000ull) |
8ab451c4 KG |
558 | 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 | |
30d45c0d SR |
594 | |
595 | #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE | |
8a805df1 SR |
596 | /* |
597 | * 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 | */ | |
601 | u32 __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 | } | |
612 | u32 flash_get_bank_size(int cs, int idx) | |
613 | __attribute__((weak, alias("__flash_get_bank_size"))); | |
614 | ||
30d45c0d SR |
615 | /* |
616 | * This function can be used to update the size in the "reg" property | |
8a805df1 | 617 | * of all NOR FLASH device nodes. This is necessary for boards with |
30d45c0d SR |
618 | * non-fixed NOR FLASH sizes. |
619 | */ | |
8a805df1 | 620 | int fdt_fixup_nor_flash_size(void *blob) |
30d45c0d SR |
621 | { |
622 | char compat[][16] = { "cfi-flash", "jedec-flash" }; | |
623 | int off; | |
624 | int len; | |
625 | struct fdt_property *prop; | |
2778a014 | 626 | u32 *reg, *reg2; |
30d45c0d SR |
627 | 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) { | |
8a805df1 SR |
632 | int idx; |
633 | ||
30d45c0d | 634 | /* |
8a805df1 SR |
635 | * Found one compatible node, so fixup the size |
636 | * int its reg properties | |
30d45c0d SR |
637 | */ |
638 | prop = fdt_get_property_w(blob, off, "reg", &len); | |
639 | if (prop) { | |
8a805df1 SR |
640 | int tuple_size = 3 * sizeof(reg); |
641 | ||
642 | /* | |
643 | * There might be multiple reg-tuples, | |
644 | * so loop through them all | |
645 | */ | |
2778a014 SR |
646 | reg = reg2 = (u32 *)&prop->data[0]; |
647 | for (idx = 0; idx < (len / tuple_size); idx++) { | |
8a805df1 SR |
648 | /* |
649 | * Update size in reg property | |
650 | */ | |
651 | reg[2] = flash_get_bank_size(reg[0], | |
652 | idx); | |
2778a014 SR |
653 | |
654 | /* | |
655 | * Point to next reg tuple | |
656 | */ | |
657 | reg += 3; | |
30d45c0d | 658 | } |
2778a014 SR |
659 | |
660 | fdt_setprop(blob, off, "reg", reg2, len); | |
30d45c0d SR |
661 | } |
662 | ||
663 | /* Move to next compatible node */ | |
664 | off = fdt_node_offset_by_compatible(blob, off, | |
665 | compat[i]); | |
666 | } | |
667 | } | |
668 | ||
8a805df1 | 669 | return 0; |
30d45c0d SR |
670 | } |
671 | #endif | |
3c950e2e | 672 | |
cb2707af MM |
673 | int 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 | ||
3c950e2e AG |
683 | #ifdef CONFIG_FDT_FIXUP_PARTITIONS |
684 | #include <jffs2/load_kernel.h> | |
685 | #include <mtd_node.h> | |
686 | ||
687 | struct reg_cell { | |
688 | unsigned int r0; | |
689 | unsigned int r1; | |
690 | }; | |
691 | ||
692 | int 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 | ||
3c950e2e AG |
717 | int 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 | ||
745 | int 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); | |
778 | add_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) { | |
795 | add_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); | |
809 | add_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 | ||
820 | add_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; | |
834 | err_size: | |
835 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); | |
836 | return ret; | |
837 | err_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 | */ | |
856 | void 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 | |
49b97d9c KG |
891 | |
892 | void 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 | } | |
a0342c08 KG |
904 | |
905 | /* Helper to read a big number; size is in cells (not bytes) */ | |
906 | static 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 | ||
a0342c08 KG |
914 | #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 | |
924 | static 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 | |
932 | static void of_dump_addr(const char *s, const u32 *addr, int na) { } | |
933 | #endif | |
934 | ||
935 | /* Callbacks for bus specific translators */ | |
936 | struct of_bus { | |
937 | const char *name; | |
938 | const char *addresses; | |
6395f318 | 939 | void (*count_cells)(void *blob, int parentoffset, |
a0342c08 KG |
940 | 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) */ | |
6395f318 | 947 | static void of_bus_default_count_cells(void *blob, int parentoffset, |
a0342c08 KG |
948 | int *addrc, int *sizec) |
949 | { | |
6395f318 SW |
950 | const u32 *prop; |
951 | ||
952 | if (addrc) { | |
953 | prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL); | |
954 | if (prop) | |
71ae5f7a | 955 | *addrc = be32_to_cpup((u32 *)prop); |
6395f318 SW |
956 | else |
957 | *addrc = 2; | |
958 | } | |
959 | ||
960 | if (sizec) { | |
961 | prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); | |
962 | if (prop) | |
71ae5f7a | 963 | *sizec = be32_to_cpup((u32 *)prop); |
6395f318 SW |
964 | else |
965 | *sizec = 1; | |
966 | } | |
a0342c08 KG |
967 | } |
968 | ||
969 | static 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 | ||
986 | static 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 */ | |
999 | static 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 | ||
1010 | static 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 | */ | |
1073 | u64 __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 */ | |
6395f318 | 1092 | bus->count_cells(blob, parent, &na, &ns); |
a0342c08 KG |
1093 | 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]; | |
6395f318 | 1119 | pbus->count_cells(blob, parent, &pna, &pns); |
a0342c08 KG |
1120 | 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 | ||
1146 | u64 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 | } | |
75e73afd KG |
1150 | |
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 | */ | |
1160 | int 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 | ||
b4b847e9 KG |
1176 | /** |
1177 | * fdt_alloc_phandle: Return next free phandle value | |
1178 | * | |
1179 | * @blob: ptr to device tree | |
1180 | */ | |
1181 | int fdt_alloc_phandle(void *blob) | |
1182 | { | |
50bf17bd | 1183 | int offset, phandle = 0; |
b4b847e9 KG |
1184 | |
1185 | for (offset = fdt_next_node(blob, -1, NULL); offset >= 0; | |
1186 | offset = fdt_next_node(blob, offset, NULL)) { | |
50bf17bd | 1187 | phandle = max(phandle, fdt_get_phandle(blob, offset)); |
b4b847e9 | 1188 | } |
75e73afd | 1189 | |
b4b847e9 KG |
1190 | return phandle + 1; |
1191 | } | |
beca5a5f | 1192 | |
a8d2a75d | 1193 | /* |
f117c0f0 | 1194 | * fdt_set_phandle: Create a phandle property for the given node |
a8d2a75d GVB |
1195 | * |
1196 | * @fdt: ptr to device tree | |
1197 | * @nodeoffset: node to update | |
1198 | * @phandle: phandle value to set (must be unique) | |
f117c0f0 KG |
1199 | */ |
1200 | int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) | |
a8d2a75d GVB |
1201 | { |
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 | ||
10aeabd1 KG |
1233 | /* |
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 | */ | |
3c927ccc | 1239 | unsigned int fdt_create_phandle(void *fdt, int nodeoffset) |
10aeabd1 KG |
1240 | { |
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) { | |
3c927ccc TT |
1246 | int ret; |
1247 | ||
10aeabd1 | 1248 | phandle = fdt_alloc_phandle(fdt); |
3c927ccc TT |
1249 | 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 | } | |
10aeabd1 KG |
1255 | } |
1256 | ||
1257 | return phandle; | |
1258 | } | |
1259 | ||
2a523f52 SL |
1260 | /* |
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 | */ | |
1269 | int 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 | */ | |
1310 | int 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 | ||
beca5a5f AG |
1318 | #if defined(CONFIG_VIDEO) |
1319 | int 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); | |
1327 | add_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; | |
1341 | err_size: | |
1342 | printf("Can't increase blob size: %s\n", fdt_strerror(ret)); | |
1343 | return ret; | |
1344 | } | |
1345 | #endif | |
bb682001 TT |
1346 | |
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 | */ | |
1356 | int 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 | */ | |
1397 | u64 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 | } |