blob: 6f32e3f68df43cbc61c85a3505b0057579b2a142 [file] [log] [blame]
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04001/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
Kumar Galaa0342c02010-06-16 14:27:38 -05005 * Copyright 2010 Freescale Semiconductor, Inc.
6 *
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04007 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
Anton Vorontsov3e303f72009-10-15 17:47:04 +040027#include <stdio_dev.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040028#include <linux/ctype.h>
29#include <linux/types.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040030#include <asm/global_data.h>
31#include <fdt.h>
32#include <libfdt.h>
33#include <fdt_support.h>
Kumar Gala151c8b02007-11-26 17:06:15 -060034#include <exports.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040035
36/*
37 * Global data (for the gd->bd)
38 */
39DECLARE_GLOBAL_DATA_PTR;
40
Kumar Gala3bed2aa2008-10-23 00:05:47 -050041/**
42 * fdt_getprop_u32_default - Find a node and return it's property or a default
43 *
44 * @fdt: ptr to device tree
45 * @path: path of node
46 * @prop: property name
47 * @dflt: default value if the property isn't found
48 *
49 * Convenience function to find a node and return it's property or a
50 * default value if it doesn't exist.
51 */
52u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop,
53 const u32 dflt)
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)
64 return *val;
65 else
66 return dflt;
67}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040068
Kumar Galaa3c29332007-10-24 10:21:57 -050069/**
70 * fdt_find_and_setprop: Find a node and set it's property
71 *
72 * @fdt: ptr to device tree
73 * @node: path of node
74 * @prop: property name
75 * @val: ptr to new value
76 * @len: length of new property value
77 * @create: flag to create the property if it doesn't exist
78 *
79 * Convenience function to directly set a property given the path to the node.
80 */
81int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
82 const void *val, int len, int create)
83{
Kumar Gala8d04f022007-10-24 11:04:22 -050084 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -050085
86 if (nodeoff < 0)
87 return nodeoff;
88
89 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
90 return 0; /* create flag not set; so exit quietly */
91
92 return fdt_setprop(fdt, nodeoff, prop, val, len);
93}
94
Kumar Gala151c8b02007-11-26 17:06:15 -060095#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Anton Vorontsov3e303f72009-10-15 17:47:04 +040096
97#ifdef CONFIG_SERIAL_MULTI
98static 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}
109#else
110static inline void fdt_fill_multisername(char *sername, size_t maxlen) {}
111#endif /* CONFIG_SERIAL_MULTI */
112
Detlev Zundel40777812008-06-20 22:24:05 +0200113static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -0600114{
115 int err = 0;
116#ifdef CONFIG_CONS_INDEX
117 int node;
118 char sername[9] = { 0 };
119 const char *path;
120
Anton Vorontsov3e303f72009-10-15 17:47:04 +0400121 fdt_fill_multisername(sername, sizeof(sername) - 1);
122 if (!sername[0])
123 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600124
125 err = node = fdt_path_offset(fdt, "/aliases");
126 if (node >= 0) {
127 int len;
128 path = fdt_getprop(fdt, node, sername, &len);
129 if (path) {
130 char *p = malloc(len);
131 err = -FDT_ERR_NOSPACE;
132 if (p) {
133 memcpy(p, path, len);
Detlev Zundel40777812008-06-20 22:24:05 +0200134 err = fdt_setprop(fdt, chosenoff,
Kumar Gala151c8b02007-11-26 17:06:15 -0600135 "linux,stdout-path", p, len);
136 free(p);
137 }
138 } else {
139 err = len;
140 }
141 }
142#endif
143 if (err < 0)
144 printf("WARNING: could not set linux,stdout-path %s.\n",
145 fdt_strerror(err));
146
147 return err;
148}
149#endif
150
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500151int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
152{
153 int nodeoffset;
154 int err, j, total;
155 u32 tmp;
156 const char *path;
157 uint64_t addr, size;
158
159 /* Find the "chosen" node. */
160 nodeoffset = fdt_path_offset (fdt, "/chosen");
161
162 /* If there is no "chosen" node in the blob return */
163 if (nodeoffset < 0) {
164 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
165 return nodeoffset;
166 }
167
168 /* just return if initrd_start/end aren't valid */
169 if ((initrd_start == 0) || (initrd_end == 0))
170 return 0;
171
172 total = fdt_num_mem_rsv(fdt);
173
174 /*
175 * Look for an existing entry and update it. If we don't find
176 * the entry, we will j be the next available slot.
177 */
178 for (j = 0; j < total; j++) {
179 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
180 if (addr == initrd_start) {
181 fdt_del_mem_rsv(fdt, j);
182 break;
183 }
184 }
185
186 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
187 if (err < 0) {
188 printf("fdt_initrd: %s\n", fdt_strerror(err));
189 return err;
190 }
191
192 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
193 if ((path == NULL) || force) {
194 tmp = __cpu_to_be32(initrd_start);
195 err = fdt_setprop(fdt, nodeoffset,
196 "linux,initrd-start", &tmp, sizeof(tmp));
197 if (err < 0) {
198 printf("WARNING: "
199 "could not set linux,initrd-start %s.\n",
200 fdt_strerror(err));
201 return err;
202 }
203 tmp = __cpu_to_be32(initrd_end);
204 err = fdt_setprop(fdt, nodeoffset,
205 "linux,initrd-end", &tmp, sizeof(tmp));
206 if (err < 0) {
207 printf("WARNING: could not set linux,initrd-end %s.\n",
208 fdt_strerror(err));
209
210 return err;
211 }
212 }
213
214 return 0;
215}
216
Heiko Schocher56844a22008-09-11 08:11:23 +0200217int fdt_chosen(void *fdt, int force)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400218{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400219 int nodeoffset;
220 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400221 char *str; /* used to set string properties */
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500222 const char *path;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400223
224 err = fdt_check_header(fdt);
225 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400226 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400227 return err;
228 }
229
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400230 /*
231 * Find the "chosen" node.
232 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500233 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400234
235 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500236 * If there is no "chosen" node in the blob, create it.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400237 */
238 if (nodeoffset < 0) {
239 /*
240 * Create a new node "/chosen" (offset 0 is root level)
241 */
242 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
243 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400244 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400245 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400246 return nodeoffset;
247 }
248 }
249
250 /*
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500251 * Create /chosen properites that don't exist in the fdt.
252 * If the property exists, update it only if the "force" parameter
253 * is true.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400254 */
255 str = getenv("bootargs");
256 if (str != NULL) {
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500257 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
258 if ((path == NULL) || force) {
259 err = fdt_setprop(fdt, nodeoffset,
260 "bootargs", str, strlen(str)+1);
261 if (err < 0)
262 printf("WARNING: could not set bootargs %s.\n",
263 fdt_strerror(err));
264 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400265 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500266
Kumar Gala151c8b02007-11-26 17:06:15 -0600267#ifdef CONFIG_OF_STDOUT_VIA_ALIAS
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500268 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
269 if ((path == NULL) || force)
270 err = fdt_fixup_stdout(fdt, nodeoffset);
Kumar Gala151c8b02007-11-26 17:06:15 -0600271#endif
272
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400273#ifdef OF_STDOUT_PATH
Gerald Van Barenb60af3d2007-12-29 22:45:27 -0500274 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
275 if ((path == NULL) || force) {
276 err = fdt_setprop(fdt, nodeoffset,
277 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
278 if (err < 0)
279 printf("WARNING: could not set linux,stdout-path %s.\n",
280 fdt_strerror(err));
281 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400282#endif
283
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400284 return err;
285}
286
Kumar Galae93becf2007-11-03 19:46:28 -0500287void do_fixup_by_path(void *fdt, const char *path, const char *prop,
288 const void *val, int len, int create)
289{
290#if defined(DEBUG)
291 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600292 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500293 for (i = 0; i < len; i++)
294 debug(" %.2x", *(u8*)(val+i));
295 debug("\n");
296#endif
297 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
298 if (rc)
299 printf("Unable to update property %s:%s, err=%s\n",
300 path, prop, fdt_strerror(rc));
301}
302
303void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
304 u32 val, int create)
305{
306 val = cpu_to_fdt32(val);
307 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
308}
309
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600310void do_fixup_by_prop(void *fdt,
311 const char *pname, const void *pval, int plen,
312 const char *prop, const void *val, int len,
313 int create)
314{
315 int off;
316#if defined(DEBUG)
317 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600318 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600319 for (i = 0; i < len; i++)
320 debug(" %.2x", *(u8*)(val+i));
321 debug("\n");
322#endif
323 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
324 while (off != -FDT_ERR_NOTFOUND) {
325 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
326 fdt_setprop(fdt, off, prop, val, len);
327 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
328 }
329}
330
331void do_fixup_by_prop_u32(void *fdt,
332 const char *pname, const void *pval, int plen,
333 const char *prop, u32 val, int create)
334{
335 val = cpu_to_fdt32(val);
336 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
337}
338
339void do_fixup_by_compat(void *fdt, const char *compat,
340 const char *prop, const void *val, int len, int create)
341{
342 int off = -1;
343#if defined(DEBUG)
344 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600345 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600346 for (i = 0; i < len; i++)
347 debug(" %.2x", *(u8*)(val+i));
348 debug("\n");
349#endif
350 off = fdt_node_offset_by_compatible(fdt, -1, compat);
351 while (off != -FDT_ERR_NOTFOUND) {
352 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
353 fdt_setprop(fdt, off, prop, val, len);
354 off = fdt_node_offset_by_compatible(fdt, off, compat);
355 }
356}
357
358void do_fixup_by_compat_u32(void *fdt, const char *compat,
359 const char *prop, u32 val, int create)
360{
361 val = cpu_to_fdt32(val);
362 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
363}
364
Kumar Gala3c927282007-11-26 14:57:45 -0600365int fdt_fixup_memory(void *blob, u64 start, u64 size)
366{
367 int err, nodeoffset, len = 0;
368 u8 tmp[16];
369 const u32 *addrcell, *sizecell;
370
371 err = fdt_check_header(blob);
372 if (err < 0) {
373 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
374 return err;
375 }
376
377 /* update, or add and update /memory node */
378 nodeoffset = fdt_path_offset(blob, "/memory");
379 if (nodeoffset < 0) {
380 nodeoffset = fdt_add_subnode(blob, 0, "memory");
381 if (nodeoffset < 0)
382 printf("WARNING: could not create /memory: %s.\n",
383 fdt_strerror(nodeoffset));
384 return nodeoffset;
385 }
386 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
387 sizeof("memory"));
388 if (err < 0) {
389 printf("WARNING: could not set %s %s.\n", "device_type",
390 fdt_strerror(err));
391 return err;
392 }
393
394 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
395 /* use shifts and mask to ensure endianness */
396 if ((addrcell) && (*addrcell == 2)) {
397 tmp[0] = (start >> 56) & 0xff;
398 tmp[1] = (start >> 48) & 0xff;
399 tmp[2] = (start >> 40) & 0xff;
400 tmp[3] = (start >> 32) & 0xff;
401 tmp[4] = (start >> 24) & 0xff;
402 tmp[5] = (start >> 16) & 0xff;
403 tmp[6] = (start >> 8) & 0xff;
404 tmp[7] = (start ) & 0xff;
405 len = 8;
406 } else {
407 tmp[0] = (start >> 24) & 0xff;
408 tmp[1] = (start >> 16) & 0xff;
409 tmp[2] = (start >> 8) & 0xff;
410 tmp[3] = (start ) & 0xff;
411 len = 4;
412 }
413
414 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
415 /* use shifts and mask to ensure endianness */
416 if ((sizecell) && (*sizecell == 2)) {
417 tmp[0+len] = (size >> 56) & 0xff;
418 tmp[1+len] = (size >> 48) & 0xff;
419 tmp[2+len] = (size >> 40) & 0xff;
420 tmp[3+len] = (size >> 32) & 0xff;
421 tmp[4+len] = (size >> 24) & 0xff;
422 tmp[5+len] = (size >> 16) & 0xff;
423 tmp[6+len] = (size >> 8) & 0xff;
424 tmp[7+len] = (size ) & 0xff;
425 len += 8;
426 } else {
427 tmp[0+len] = (size >> 24) & 0xff;
428 tmp[1+len] = (size >> 16) & 0xff;
429 tmp[2+len] = (size >> 8) & 0xff;
430 tmp[3+len] = (size ) & 0xff;
431 len += 4;
432 }
433
434 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
435 if (err < 0) {
436 printf("WARNING: could not set %s %s.\n",
437 "reg", fdt_strerror(err));
438 return err;
439 }
440 return 0;
441}
442
Kumar Galaba37aa02008-08-19 15:41:18 -0500443void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600444{
Kumar Galaba37aa02008-08-19 15:41:18 -0500445 int node, i, j;
446 char enet[16], *tmp, *end;
447 char mac[16] = "ethaddr";
Kumar Galaab544632007-11-21 11:11:03 -0600448 const char *path;
Kumar Galaba37aa02008-08-19 15:41:18 -0500449 unsigned char mac_addr[6];
Kumar Galaab544632007-11-21 11:11:03 -0600450
451 node = fdt_path_offset(fdt, "/aliases");
Kumar Galaba37aa02008-08-19 15:41:18 -0500452 if (node < 0)
453 return;
454
455 i = 0;
456 while ((tmp = getenv(mac)) != NULL) {
457 sprintf(enet, "ethernet%d", i);
458 path = fdt_getprop(fdt, node, enet, NULL);
459 if (!path) {
460 debug("No alias for %s\n", enet);
461 sprintf(mac, "eth%daddr", ++i);
462 continue;
Kumar Galaab544632007-11-21 11:11:03 -0600463 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500464
465 for (j = 0; j < 6; j++) {
466 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
467 if (tmp)
468 tmp = (*end) ? end+1 : end;
Kumar Galaab544632007-11-21 11:11:03 -0600469 }
Kumar Galaba37aa02008-08-19 15:41:18 -0500470
471 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
472 do_fixup_by_path(fdt, path, "local-mac-address",
473 &mac_addr, 6, 1);
474
475 sprintf(mac, "eth%daddr", ++i);
Kumar Galaab544632007-11-21 11:11:03 -0600476 }
477}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300478
Kumar Gala3082d232008-08-15 08:24:42 -0500479/* Resize the fdt to its actual size + a bit of padding */
480int fdt_resize(void *blob)
481{
482 int i;
483 uint64_t addr, size;
484 int total, ret;
485 uint actualsize;
486
487 if (!blob)
488 return 0;
489
490 total = fdt_num_mem_rsv(blob);
491 for (i = 0; i < total; i++) {
492 fdt_get_mem_rsv(blob, i, &addr, &size);
493 if (addr == (uint64_t)(u32)blob) {
494 fdt_del_mem_rsv(blob, i);
495 break;
496 }
497 }
498
Peter Korsgaardf242a082008-10-28 08:26:52 +0100499 /*
500 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200501 * plus the size needed for 5 fdt_add_mem_rsv, one
502 * for the fdt itself and 4 for a possible initrd
503 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100504 */
Kumar Gala3082d232008-08-15 08:24:42 -0500505 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200506 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500507
508 /* Make it so the fdt ends on a page boundary */
Peter Korsgaardc088a102009-01-14 13:52:24 +0100509 actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
Kumar Gala3082d232008-08-15 08:24:42 -0500510 actualsize = actualsize - ((uint)blob & 0xfff);
511
512 /* Change the fdt header to reflect the correct size */
513 fdt_set_totalsize(blob, actualsize);
514
515 /* Add the new reservation */
516 ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
517 if (ret < 0)
518 return ret;
519
520 return actualsize;
521}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500522
523#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500524#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500525
526#define FDT_PCI_PREFETCH (0x40000000)
527#define FDT_PCI_MEM32 (0x02000000)
528#define FDT_PCI_IO (0x01000000)
529#define FDT_PCI_MEM64 (0x03000000)
530
531int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
532
533 int addrcell, sizecell, len, r;
534 u32 *dma_range;
535 /* sized based on pci addr cells, size-cells, & address-cells */
536 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
537
538 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
539 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
540
541 dma_range = &dma_ranges[0];
542 for (r = 0; r < hose->region_count; r++) {
543 u64 bus_start, phys_start, size;
544
Kumar Galaff4e66e2009-02-06 09:49:31 -0600545 /* skip if !PCI_REGION_SYS_MEMORY */
546 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500547 continue;
548
549 bus_start = (u64)hose->regions[r].bus_start;
550 phys_start = (u64)hose->regions[r].phys_start;
551 size = (u64)hose->regions[r].size;
552
553 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500554 if (size >= 0x100000000ull)
Kumar Gala8ab451c2008-10-22 23:33:56 -0500555 dma_range[0] |= FDT_PCI_MEM64;
556 else
557 dma_range[0] |= FDT_PCI_MEM32;
558 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
559 dma_range[0] |= FDT_PCI_PREFETCH;
560#ifdef CONFIG_SYS_PCI_64BIT
561 dma_range[1] = bus_start >> 32;
562#else
563 dma_range[1] = 0;
564#endif
565 dma_range[2] = bus_start & 0xffffffff;
566
567 if (addrcell == 2) {
568 dma_range[3] = phys_start >> 32;
569 dma_range[4] = phys_start & 0xffffffff;
570 } else {
571 dma_range[3] = phys_start & 0xffffffff;
572 }
573
574 if (sizecell == 2) {
575 dma_range[3 + addrcell + 0] = size >> 32;
576 dma_range[3 + addrcell + 1] = size & 0xffffffff;
577 } else {
578 dma_range[3 + addrcell + 0] = size & 0xffffffff;
579 }
580
581 dma_range += (3 + addrcell + sizecell);
582 }
583
584 len = dma_range - &dma_ranges[0];
585 if (len)
586 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
587
588 return 0;
589}
590#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200591
592#ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
593/*
Stefan Roese8a805df2010-09-16 14:01:53 +0200594 * Provide a weak default function to return the flash bank size.
595 * There might be multiple non-identical flash chips connected to one
596 * chip-select, so we need to pass an index as well.
597 */
598u32 __flash_get_bank_size(int cs, int idx)
599{
600 extern flash_info_t flash_info[];
601
602 /*
603 * As default, a simple 1:1 mapping is provided. Boards with
604 * a different mapping need to supply a board specific mapping
605 * routine.
606 */
607 return flash_info[cs].size;
608}
609u32 flash_get_bank_size(int cs, int idx)
610 __attribute__((weak, alias("__flash_get_bank_size")));
611
612/*
Stefan Roese30d45c02009-10-21 11:59:52 +0200613 * This function can be used to update the size in the "reg" property
Stefan Roese8a805df2010-09-16 14:01:53 +0200614 * of all NOR FLASH device nodes. This is necessary for boards with
Stefan Roese30d45c02009-10-21 11:59:52 +0200615 * non-fixed NOR FLASH sizes.
616 */
Stefan Roese8a805df2010-09-16 14:01:53 +0200617int fdt_fixup_nor_flash_size(void *blob)
Stefan Roese30d45c02009-10-21 11:59:52 +0200618{
619 char compat[][16] = { "cfi-flash", "jedec-flash" };
620 int off;
621 int len;
622 struct fdt_property *prop;
623 u32 *reg;
624 int i;
625
626 for (i = 0; i < 2; i++) {
627 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
628 while (off != -FDT_ERR_NOTFOUND) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200629 int idx;
630
Stefan Roese30d45c02009-10-21 11:59:52 +0200631 /*
Stefan Roese8a805df2010-09-16 14:01:53 +0200632 * Found one compatible node, so fixup the size
633 * int its reg properties
Stefan Roese30d45c02009-10-21 11:59:52 +0200634 */
635 prop = fdt_get_property_w(blob, off, "reg", &len);
636 if (prop) {
Stefan Roese8a805df2010-09-16 14:01:53 +0200637 int tuple_size = 3 * sizeof(reg);
Stefan Roese30d45c02009-10-21 11:59:52 +0200638
Stefan Roese8a805df2010-09-16 14:01:53 +0200639 /*
640 * There might be multiple reg-tuples,
641 * so loop through them all
642 */
643 len /= tuple_size;
644 reg = (u32 *)&prop->data[0];
645 for (idx = 0; idx < len; idx++) {
646 /*
647 * Update size in reg property
648 */
649 reg[2] = flash_get_bank_size(reg[0],
650 idx);
651 fdt_setprop(blob, off, "reg", reg,
652 tuple_size);
653 reg += tuple_size;
Stefan Roese30d45c02009-10-21 11:59:52 +0200654 }
655 }
656
657 /* Move to next compatible node */
658 off = fdt_node_offset_by_compatible(blob, off,
659 compat[i]);
660 }
661 }
662
Stefan Roese8a805df2010-09-16 14:01:53 +0200663 return 0;
Stefan Roese30d45c02009-10-21 11:59:52 +0200664}
665#endif
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100666
667#ifdef CONFIG_FDT_FIXUP_PARTITIONS
668#include <jffs2/load_kernel.h>
669#include <mtd_node.h>
670
671struct reg_cell {
672 unsigned int r0;
673 unsigned int r1;
674};
675
676int fdt_del_subnodes(const void *blob, int parent_offset)
677{
678 int off, ndepth;
679 int ret;
680
681 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
682 (off >= 0) && (ndepth > 0);
683 off = fdt_next_node(blob, off, &ndepth)) {
684 if (ndepth == 1) {
685 debug("delete %s: offset: %x\n",
686 fdt_get_name(blob, off, 0), off);
687 ret = fdt_del_node((void *)blob, off);
688 if (ret < 0) {
689 printf("Can't delete node: %s\n",
690 fdt_strerror(ret));
691 return ret;
692 } else {
693 ndepth = 0;
694 off = parent_offset;
695 }
696 }
697 }
698 return 0;
699}
700
701int fdt_increase_size(void *fdt, int add_len)
702{
703 int newlen;
704
705 newlen = fdt_totalsize(fdt) + add_len;
706
707 /* Open in place with a new len */
708 return fdt_open_into(fdt, fdt, newlen);
709}
710
711int fdt_del_partitions(void *blob, int parent_offset)
712{
713 const void *prop;
714 int ndepth = 0;
715 int off;
716 int ret;
717
718 off = fdt_next_node(blob, parent_offset, &ndepth);
719 if (off > 0 && ndepth == 1) {
720 prop = fdt_getprop(blob, off, "label", NULL);
721 if (prop == NULL) {
722 /*
723 * Could not find label property, nand {}; node?
724 * Check subnode, delete partitions there if any.
725 */
726 return fdt_del_partitions(blob, off);
727 } else {
728 ret = fdt_del_subnodes(blob, parent_offset);
729 if (ret < 0) {
730 printf("Can't remove subnodes: %s\n",
731 fdt_strerror(ret));
732 return ret;
733 }
734 }
735 }
736 return 0;
737}
738
739int fdt_node_set_part_info(void *blob, int parent_offset,
740 struct mtd_device *dev)
741{
742 struct list_head *pentry;
743 struct part_info *part;
744 struct reg_cell cell;
745 int off, ndepth = 0;
746 int part_num, ret;
747 char buf[64];
748
749 ret = fdt_del_partitions(blob, parent_offset);
750 if (ret < 0)
751 return ret;
752
753 /*
754 * Check if it is nand {}; subnode, adjust
755 * the offset in this case
756 */
757 off = fdt_next_node(blob, parent_offset, &ndepth);
758 if (off > 0 && ndepth == 1)
759 parent_offset = off;
760
761 part_num = 0;
762 list_for_each_prev(pentry, &dev->parts) {
763 int newoff;
764
765 part = list_entry(pentry, struct part_info, link);
766
767 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
768 part_num, part->name, part->size,
769 part->offset, part->mask_flags);
770
771 sprintf(buf, "partition@%x", part->offset);
772add_sub:
773 ret = fdt_add_subnode(blob, parent_offset, buf);
774 if (ret == -FDT_ERR_NOSPACE) {
775 ret = fdt_increase_size(blob, 512);
776 if (!ret)
777 goto add_sub;
778 else
779 goto err_size;
780 } else if (ret < 0) {
781 printf("Can't add partition node: %s\n",
782 fdt_strerror(ret));
783 return ret;
784 }
785 newoff = ret;
786
787 /* Check MTD_WRITEABLE_CMD flag */
788 if (part->mask_flags & 1) {
789add_ro:
790 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
791 if (ret == -FDT_ERR_NOSPACE) {
792 ret = fdt_increase_size(blob, 512);
793 if (!ret)
794 goto add_ro;
795 else
796 goto err_size;
797 } else if (ret < 0)
798 goto err_prop;
799 }
800
801 cell.r0 = cpu_to_fdt32(part->offset);
802 cell.r1 = cpu_to_fdt32(part->size);
803add_reg:
804 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
805 if (ret == -FDT_ERR_NOSPACE) {
806 ret = fdt_increase_size(blob, 512);
807 if (!ret)
808 goto add_reg;
809 else
810 goto err_size;
811 } else if (ret < 0)
812 goto err_prop;
813
814add_label:
815 ret = fdt_setprop_string(blob, newoff, "label", part->name);
816 if (ret == -FDT_ERR_NOSPACE) {
817 ret = fdt_increase_size(blob, 512);
818 if (!ret)
819 goto add_label;
820 else
821 goto err_size;
822 } else if (ret < 0)
823 goto err_prop;
824
825 part_num++;
826 }
827 return 0;
828err_size:
829 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
830 return ret;
831err_prop:
832 printf("Can't add property: %s\n", fdt_strerror(ret));
833 return ret;
834}
835
836/*
837 * Update partitions in nor/nand nodes using info from
838 * mtdparts environment variable. The nodes to update are
839 * specified by node_info structure which contains mtd device
840 * type and compatible string: E. g. the board code in
841 * ft_board_setup() could use:
842 *
843 * struct node_info nodes[] = {
844 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
845 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
846 * };
847 *
848 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
849 */
850void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
851{
852 struct node_info *ni = node_info;
853 struct mtd_device *dev;
854 char *parts;
855 int i, idx;
856 int noff;
857
858 parts = getenv("mtdparts");
859 if (!parts)
860 return;
861
862 if (mtdparts_init() != 0)
863 return;
864
865 for (i = 0; i < node_info_size; i++) {
866 idx = 0;
867 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
868 while (noff != -FDT_ERR_NOTFOUND) {
869 debug("%s: %s, mtd dev type %d\n",
870 fdt_get_name(blob, noff, 0),
871 ni[i].compat, ni[i].type);
872 dev = device_find(ni[i].type, idx++);
873 if (dev) {
874 if (fdt_node_set_part_info(blob, noff, dev))
875 return; /* return on error */
876 }
877
878 /* Jump to next flash node */
879 noff = fdt_node_offset_by_compatible(blob, noff,
880 ni[i].compat);
881 }
882 }
883}
884#endif
Kumar Gala49b97d92010-03-30 10:19:26 -0500885
886void fdt_del_node_and_alias(void *blob, const char *alias)
887{
888 int off = fdt_path_offset(blob, alias);
889
890 if (off < 0)
891 return;
892
893 fdt_del_node(blob, off);
894
895 off = fdt_path_offset(blob, "/aliases");
896 fdt_delprop(blob, off, alias);
897}
Kumar Galaa0342c02010-06-16 14:27:38 -0500898
899/* Helper to read a big number; size is in cells (not bytes) */
900static inline u64 of_read_number(const __be32 *cell, int size)
901{
902 u64 r = 0;
903 while (size--)
904 r = (r << 32) | be32_to_cpu(*(cell++));
905 return r;
906}
907
Kumar Galaa0342c02010-06-16 14:27:38 -0500908#define PRu64 "%llx"
909
910/* Max address size we deal with */
911#define OF_MAX_ADDR_CELLS 4
912#define OF_BAD_ADDR ((u64)-1)
913#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
914 (ns) > 0)
915
916/* Debug utility */
917#ifdef DEBUG
918static void of_dump_addr(const char *s, const u32 *addr, int na)
919{
920 printf("%s", s);
921 while(na--)
922 printf(" %08x", *(addr++));
923 printf("\n");
924}
925#else
926static void of_dump_addr(const char *s, const u32 *addr, int na) { }
927#endif
928
929/* Callbacks for bus specific translators */
930struct of_bus {
931 const char *name;
932 const char *addresses;
Scott Wood6395f312010-08-12 18:37:39 -0500933 void (*count_cells)(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500934 int *addrc, int *sizec);
935 u64 (*map)(u32 *addr, const u32 *range,
936 int na, int ns, int pna);
937 int (*translate)(u32 *addr, u64 offset, int na);
938};
939
940/* Default translator (generic bus) */
Scott Wood6395f312010-08-12 18:37:39 -0500941static void of_bus_default_count_cells(void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -0500942 int *addrc, int *sizec)
943{
Scott Wood6395f312010-08-12 18:37:39 -0500944 const u32 *prop;
945
946 if (addrc) {
947 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
948 if (prop)
949 *addrc = be32_to_cpup(prop);
950 else
951 *addrc = 2;
952 }
953
954 if (sizec) {
955 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
956 if (prop)
957 *sizec = be32_to_cpup(prop);
958 else
959 *sizec = 1;
960 }
Kumar Galaa0342c02010-06-16 14:27:38 -0500961}
962
963static u64 of_bus_default_map(u32 *addr, const u32 *range,
964 int na, int ns, int pna)
965{
966 u64 cp, s, da;
967
968 cp = of_read_number(range, na);
969 s = of_read_number(range + na + pna, ns);
970 da = of_read_number(addr, na);
971
972 debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
973 cp, s, da);
974
975 if (da < cp || da >= (cp + s))
976 return OF_BAD_ADDR;
977 return da - cp;
978}
979
980static int of_bus_default_translate(u32 *addr, u64 offset, int na)
981{
982 u64 a = of_read_number(addr, na);
983 memset(addr, 0, na * 4);
984 a += offset;
985 if (na > 1)
986 addr[na - 2] = a >> 32;
987 addr[na - 1] = a & 0xffffffffu;
988
989 return 0;
990}
991
992/* Array of bus specific translators */
993static struct of_bus of_busses[] = {
994 /* Default */
995 {
996 .name = "default",
997 .addresses = "reg",
998 .count_cells = of_bus_default_count_cells,
999 .map = of_bus_default_map,
1000 .translate = of_bus_default_translate,
1001 },
1002};
1003
1004static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1005 struct of_bus *pbus, u32 *addr,
1006 int na, int ns, int pna, const char *rprop)
1007{
1008 const u32 *ranges;
1009 int rlen;
1010 int rone;
1011 u64 offset = OF_BAD_ADDR;
1012
1013 /* Normally, an absence of a "ranges" property means we are
1014 * crossing a non-translatable boundary, and thus the addresses
1015 * below the current not cannot be converted to CPU physical ones.
1016 * Unfortunately, while this is very clear in the spec, it's not
1017 * what Apple understood, and they do have things like /uni-n or
1018 * /ht nodes with no "ranges" property and a lot of perfectly
1019 * useable mapped devices below them. Thus we treat the absence of
1020 * "ranges" as equivalent to an empty "ranges" property which means
1021 * a 1:1 translation at that level. It's up to the caller not to try
1022 * to translate addresses that aren't supposed to be translated in
1023 * the first place. --BenH.
1024 */
1025 ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen);
1026 if (ranges == NULL || rlen == 0) {
1027 offset = of_read_number(addr, na);
1028 memset(addr, 0, pna * 4);
1029 debug("OF: no ranges, 1:1 translation\n");
1030 goto finish;
1031 }
1032
1033 debug("OF: walking ranges...\n");
1034
1035 /* Now walk through the ranges */
1036 rlen /= 4;
1037 rone = na + pna + ns;
1038 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1039 offset = bus->map(addr, ranges, na, ns, pna);
1040 if (offset != OF_BAD_ADDR)
1041 break;
1042 }
1043 if (offset == OF_BAD_ADDR) {
1044 debug("OF: not found !\n");
1045 return 1;
1046 }
1047 memcpy(addr, ranges + na, 4 * pna);
1048
1049 finish:
1050 of_dump_addr("OF: parent translation for:", addr, pna);
1051 debug("OF: with offset: "PRu64"\n", offset);
1052
1053 /* Translate it into parent bus space */
1054 return pbus->translate(addr, offset, pna);
1055}
1056
1057/*
1058 * Translate an address from the device-tree into a CPU physical address,
1059 * this walks up the tree and applies the various bus mappings on the
1060 * way.
1061 *
1062 * Note: We consider that crossing any level with #size-cells == 0 to mean
1063 * that translation is impossible (that is we are not dealing with a value
1064 * that can be mapped to a cpu physical address). This is not really specified
1065 * that way, but this is traditionally the way IBM at least do things
1066 */
1067u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr,
1068 const char *rprop)
1069{
1070 int parent;
1071 struct of_bus *bus, *pbus;
1072 u32 addr[OF_MAX_ADDR_CELLS];
1073 int na, ns, pna, pns;
1074 u64 result = OF_BAD_ADDR;
1075
1076 debug("OF: ** translation for device %s **\n",
1077 fdt_get_name(blob, node_offset, NULL));
1078
1079 /* Get parent & match bus type */
1080 parent = fdt_parent_offset(blob, node_offset);
1081 if (parent < 0)
1082 goto bail;
1083 bus = &of_busses[0];
1084
1085 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001086 bus->count_cells(blob, parent, &na, &ns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001087 if (!OF_CHECK_COUNTS(na, ns)) {
1088 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1089 fdt_get_name(blob, node_offset, NULL));
1090 goto bail;
1091 }
1092 memcpy(addr, in_addr, na * 4);
1093
1094 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1095 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1096 of_dump_addr("OF: translating address:", addr, na);
1097
1098 /* Translate */
1099 for (;;) {
1100 /* Switch to parent bus */
1101 node_offset = parent;
1102 parent = fdt_parent_offset(blob, node_offset);
1103
1104 /* If root, we have finished */
1105 if (parent < 0) {
1106 debug("OF: reached root node\n");
1107 result = of_read_number(addr, na);
1108 break;
1109 }
1110
1111 /* Get new parent bus and counts */
1112 pbus = &of_busses[0];
Scott Wood6395f312010-08-12 18:37:39 -05001113 pbus->count_cells(blob, parent, &pna, &pns);
Kumar Galaa0342c02010-06-16 14:27:38 -05001114 if (!OF_CHECK_COUNTS(pna, pns)) {
1115 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1116 fdt_get_name(blob, node_offset, NULL));
1117 break;
1118 }
1119
1120 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1121 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1122
1123 /* Apply bus translation */
1124 if (of_translate_one(blob, node_offset, bus, pbus,
1125 addr, na, ns, pna, rprop))
1126 break;
1127
1128 /* Complete the move up one level */
1129 na = pna;
1130 ns = pns;
1131 bus = pbus;
1132
1133 of_dump_addr("OF: one level translation:", addr, na);
1134 }
1135 bail:
1136
1137 return result;
1138}
1139
1140u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr)
1141{
1142 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1143}
Kumar Gala75e73af2010-07-04 12:48:21 -05001144
1145/**
1146 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1147 * who's reg property matches a physical cpu address
1148 *
1149 * @blob: ptr to device tree
1150 * @compat: compatiable string to match
1151 * @compat_off: property name
1152 *
1153 */
1154int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1155 phys_addr_t compat_off)
1156{
1157 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1158 while (off != -FDT_ERR_NOTFOUND) {
1159 u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len);
1160 if (reg) {
1161 if (compat_off == fdt_translate_address(blob, off, reg))
1162 return off;
1163 }
1164 off = fdt_node_offset_by_compatible(blob, off, compat);
1165 }
1166
1167 return -FDT_ERR_NOTFOUND;
1168}
1169
Kumar Galab4b847e2010-07-09 16:18:58 -05001170/**
1171 * fdt_alloc_phandle: Return next free phandle value
1172 *
1173 * @blob: ptr to device tree
1174 */
1175int fdt_alloc_phandle(void *blob)
1176{
1177 int offset, len, phandle = 0;
1178 const u32 *val;
Kumar Gala75e73af2010-07-04 12:48:21 -05001179
Kumar Galab4b847e2010-07-09 16:18:58 -05001180 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1181 offset = fdt_next_node(blob, offset, NULL)) {
1182 val = fdt_getprop(blob, offset, "linux,phandle", &len);
1183 if (val)
1184 phandle = max(*val, phandle);
1185 }
1186
1187 return phandle + 1;
1188}