tizen: bootmode: enable uart console with home key
[profile/mobile/platform/kernel/u-boot-tm1.git] / common / fdt_support.c
1 /*
2  * (C) Copyright 2007
3  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4  *
5  * Copyright 2010 Freescale Semiconductor, Inc.
6  *
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>
27 #include <stdio_dev.h>
28 #include <linux/ctype.h>
29 #include <linux/types.h>
30 #include <asm/global_data.h>
31 #include <fdt.h>
32 #include <libfdt.h>
33 #include <fdt_support.h>
34 #include <exports.h>
35 #include <normal_mode.h>
36
37 extern PUBLIC phys_size_t get_dram_size_from_gd(void);
38 extern char* get_calibration_parameter(void);
39 extern bool is_calibration_by_uart(void);
40 extern int poweron_by_calibration(void);
41
42 /**
43  * fdt_getprop_u32_default - Find a node and return it's property or a default
44  *
45  * @fdt: ptr to device tree
46  * @path: path of node
47  * @prop: property name
48  * @dflt: default value if the property isn't found
49  *
50  * Convenience function to find a node and return it's property or a
51  * default value if it doesn't exist.
52  */
53 u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop,
54                                 const u32 dflt)
55 {
56         const u32 *val;
57         int off;
58
59         off = fdt_path_offset(fdt, path);
60         if (off < 0)
61                 return dflt;
62
63         val = fdt_getprop(fdt, off, prop, NULL);
64         if (val)
65                 return *val;
66         else
67                 return dflt;
68 }
69
70 /**
71  * fdt_find_and_setprop: Find a node and set it's property
72  *
73  * @fdt: ptr to device tree
74  * @node: path of node
75  * @prop: property name
76  * @val: ptr to new value
77  * @len: length of new property value
78  * @create: flag to create the property if it doesn't exist
79  *
80  * Convenience function to directly set a property given the path to the node.
81  */
82 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
83                          const void *val, int len, int create)
84 {
85         int nodeoff = fdt_path_offset(fdt, node);
86
87         if (nodeoff < 0)
88                 return nodeoff;
89
90         if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
91                 return 0; /* create flag not set; so exit quietly */
92
93         return fdt_setprop(fdt, nodeoff, prop, val, len);
94 }
95
96 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
97
98 #ifdef CONFIG_SERIAL_MULTI
99 static void fdt_fill_multisername(char *sername, size_t maxlen)
100 {
101         const char *outname = stdio_devices[stdout]->name;
102
103         if (strcmp(outname, "serial") > 0)
104                 strncpy(sername, outname, maxlen);
105
106         /* eserial? */
107         if (strcmp(outname + 1, "serial") > 0)
108                 strncpy(sername, outname + 1, maxlen);
109 }
110 #else
111 static inline void fdt_fill_multisername(char *sername, size_t maxlen) {}
112 #endif /* CONFIG_SERIAL_MULTI */
113
114 static int fdt_fixup_stdout(void *fdt, int chosenoff)
115 {
116         int err = 0;
117 #ifdef CONFIG_CONS_INDEX
118         int node;
119         char sername[9] = { 0 };
120         const char *path;
121
122         fdt_fill_multisername(sername, sizeof(sername) - 1);
123         if (!sername[0])
124                 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
125
126         err = node = fdt_path_offset(fdt, "/aliases");
127         if (node >= 0) {
128                 int len;
129                 path = fdt_getprop(fdt, node, sername, &len);
130                 if (path) {
131                         char *p = malloc(len);
132                         err = -FDT_ERR_NOSPACE;
133                         if (p) {
134                                 memcpy(p, path, len);
135                                 err = fdt_setprop(fdt, chosenoff,
136                                         "linux,stdout-path", p, len);
137                                 free(p);
138                         }
139                 } else {
140                         err = len;
141                 }
142         }
143 #endif
144         if (err < 0)
145                 printf("WARNING: could not set linux,stdout-path %s.\n",
146                                 fdt_strerror(err));
147
148         return err;
149 }
150 #endif
151
152 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
153 {
154         int   nodeoffset;
155         int   err, j, total;
156         u32   tmp;
157         const char *path;
158         uint64_t addr, size;
159
160         /* Find the "chosen" node.  */
161         nodeoffset = fdt_path_offset (fdt, "/chosen");
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
217 int fdt_initrd_norsvmem(void *fdt, ulong initrd_start, ulong initrd_end, int force)
218 {
219         int   nodeoffset;
220         int   err;
221         u32   tmp;
222         const char *path;
223
224         /* Find the "chosen" node.  */
225         nodeoffset = fdt_path_offset (fdt, "/chosen");
226
227         /* If there is no "chosen" node in the blob return */
228         if (nodeoffset < 0) {
229                 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
230                 return nodeoffset;
231         }
232
233         /* just return if initrd_start/end aren't valid */
234         if ((initrd_start == 0) || (initrd_end == 0))
235                 return 0;
236
237         path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
238         if ((path == NULL) || force) {
239                 tmp = __cpu_to_be32(initrd_start);
240                 err = fdt_setprop(fdt, nodeoffset,
241                                 "linux,initrd-start", &tmp, sizeof(tmp));
242                 if (err < 0) {
243                         printf("WARNING: "
244                                         "could not set linux,initrd-start %s.\n",
245                                         fdt_strerror(err));
246                         return err;
247                 }
248                 tmp = __cpu_to_be32(initrd_end);
249                 err = fdt_setprop(fdt, nodeoffset,
250                                 "linux,initrd-end", &tmp, sizeof(tmp));
251                 if (err < 0) {
252                         printf("WARNING: could not set linux,initrd-end %s.\n",
253                                         fdt_strerror(err));
254
255                         return err;
256                 }
257         }
258
259         return 0;
260 }
261
262 int fdt_chosen(void *fdt, int force)
263 {
264         int   nodeoffset;
265         int   err;
266         char  *str;             /* used to set string properties */
267         const char *path;
268
269         err = fdt_check_header(fdt);
270         if (err < 0) {
271                 printf("fdt_chosen: %s\n", fdt_strerror(err));
272                 return err;
273         }
274
275         /*
276          * Find the "chosen" node.
277          */
278         nodeoffset = fdt_path_offset (fdt, "/chosen");
279         /*
280          * If there is no "chosen" node in the blob, create it.
281          */
282         if (nodeoffset < 0) {
283                 /*
284                  * Create a new node "/chosen" (offset 0 is root level)
285                  */
286                 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
287                 if (nodeoffset < 0) {
288                         printf("WARNING: could not create /chosen %s.\n",
289                                 fdt_strerror(nodeoffset));
290                         return nodeoffset;
291                 }
292         }
293
294         /*
295          * Create /chosen properites that don't exist in the fdt.
296          * If the property exists, update it only if the "force" parameter
297          * is true.
298          */
299         str = getenv("bootargs");
300         if (str != NULL) {
301                 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
302                 if ((path == NULL) || force) {
303                         err = fdt_setprop(fdt, nodeoffset,
304                                 "bootargs", str, strlen(str)+1);
305                         if (err < 0)
306                                 printf("WARNING: could not set bootargs %s.\n",
307                                         fdt_strerror(err));
308                 }
309         }
310
311 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
312         path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
313         if ((path == NULL) || force)
314                 err = fdt_fixup_stdout(fdt, nodeoffset);
315 #endif
316
317 #ifdef OF_STDOUT_PATH
318         path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
319         if ((path == NULL) || force) {
320                 err = fdt_setprop(fdt, nodeoffset,
321                         "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
322                 if (err < 0)
323                         printf("WARNING: could not set linux,stdout-path %s.\n",
324                                 fdt_strerror(err));
325         }
326 #endif
327
328         return err;
329 }
330
331 int fdt_chosen_bootargs_replace(void *fdt, char *old_args, char *new_args)
332 {
333         int nodeoffset;
334         int err, i;
335         char *str, *dst;
336         const char *src, *path;
337         char *strargs;
338
339         if (!old_args || !new_args)
340                 return -1;
341
342         err = fdt_check_header(fdt);
343         if (err < 0) {
344                 printf("fdt_chosen_bootargs_replace: %s\n", fdt_strerror(err));
345                 return err;
346         }
347
348         /*
349          * Find the "chosen" node.
350          */
351         nodeoffset = fdt_path_offset(fdt, "/chosen");
352
353         /*
354          * If there is no "chosen" node in the blob, leave.
355          */
356         if (nodeoffset < 0) {
357                 printf("fdt_chosen_bootargs_replace: cann't find chosen");
358                 return -1;
359         }
360
361         /*
362          * If the property exists, update it only if the "force" parameter
363          * is true.
364          */
365         path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
366         if (path != NULL ) {
367         str = strstr(path, old_args);
368         if(!str)
369         {
370             printf("fdt_chosen_bootargs_replace: cann't find str %s!", old_args);
371             return -1;
372         }
373                 strargs = malloc(1024);
374                 if (!strargs)
375                         return -1;
376         src = path;
377         dst = strargs;
378         i = 0;
379         /* copy the front str */
380         while(src != str && i < 1023){
381             *dst++ = *src++;
382             i++;
383         }
384
385         /* copy the new str */
386         src = new_args;
387         while(*src && i < 1023){
388             *dst++ = *src++;
389             i++;
390         }
391
392         /* copy the back str */
393         src = str + strlen(old_args);
394         while(*src && i < 1023){
395             *dst++ = *src++;
396             i++;
397         }
398         *dst = 0;
399
400         printf("fdt_chosen_bootargs_replace: new bootargs %s!", strargs);
401                 err = fdt_setprop(fdt, nodeoffset, "bootargs", strargs, strlen(strargs) + 1);
402                 if (err < 0)
403                         printf("WARNING: could not set bootargs %s.\n", fdt_strerror(err));
404                 free(strargs);
405         }
406
407         return err;
408 }
409
410 int fdt_chosen_bootargs_append(void *fdt, char* append_args,int force)
411 {
412         int   nodeoffset;
413         int   err;
414         const char *path;
415         char *strargs;
416
417         if (!append_args)
418                 return -1;
419
420         err = fdt_check_header(fdt);
421         if (err < 0) {
422                 printf("fdt_chosen_bootargs_append: %s\n", fdt_strerror(err));
423                 return err;
424         }
425
426         /*
427          * Find the "chosen" node.
428          */
429         nodeoffset = fdt_path_offset (fdt, "/chosen");
430
431         /*
432          * If there is no "chosen" node in the blob, leave.
433          */
434         if (nodeoffset < 0) {
435                 printf("fdt_chosen_bootargs_append: cann't find chosen");
436                 return -1;
437         }
438
439         /*
440          * If the property exists, update it only if the "force" parameter
441          * is true.
442          */
443         path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
444         if ((path == NULL) || force) {
445                 strargs = malloc(1024);
446                 if (!strargs)
447                         return -1;
448                 memset(strargs, 0, 1024);
449                 if(path != NULL)
450                 {
451                     sprintf(strargs, "%s %s", path, append_args);
452                 }
453                 else
454                 {
455                     sprintf(strargs, "%s", append_args);
456                 }
457                 err = fdt_setprop(fdt, nodeoffset,
458                                 "bootargs", strargs, strlen(strargs)+1);
459                 if (err < 0)
460                         printf("WARNING: could not set bootargs %s.\n",
461                                 fdt_strerror(err));
462                 free(strargs);
463         }
464
465         return err;
466 }
467
468 int fdt_fixup_lcdid(void *fdt)
469 {
470         char buf[16];
471         extern uint32_t load_lcd_id_to_kernel(void);
472         uint32_t lcd_id = 0;
473         int str_len;
474         int ret;
475
476         lcd_id = load_lcd_id_to_kernel();
477         memset(buf, 0, 16);
478
479         sprintf(buf, "lcd_id=ID");
480         str_len = strlen(buf);
481         sprintf(&buf[str_len], "%x",lcd_id);
482         str_len = strlen(buf);
483         buf[str_len] = '\0';
484
485         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
486         return ret;
487 }
488
489 int fdt_fixup_lcdbase(void *fdt)
490 {
491         char buf[32];
492         int str_len;
493         int ret;
494         extern void *lcd_base;
495
496         memset(buf, 0, 32);
497
498         //add lcd frame buffer base, length should be lcd w*h*2(RGB565)
499         sprintf(buf, "lcd_base=");
500         str_len = strlen(buf);
501         sprintf(&buf[str_len], "%lx",(unsigned long)lcd_base);
502         str_len = strlen(buf);
503         buf[str_len] = '\0';
504
505         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
506         return ret;
507 }
508
509 int fdt_fixup_calibration_parameter(void *fdt)
510 {
511         char *buf;
512         int ret;
513
514         buf = get_calibration_parameter();
515
516         if(!buf)
517                 {
518                 printf("warn:nothing transfer to kernel about calibration\n");
519                 return -1;
520         }
521         else{
522                 ret = fdt_chosen_bootargs_append(fdt, buf, 1);
523                 if(0 == ret)
524                 {
525                         /* if is uart calibraton, remove ttys1 console */
526                         if(is_calibration_by_uart())
527                         {
528                                 //__raw_writel(0x285480, CTL_PIN_BASE + REG_PIN_CTRL2);
529                                 //ret = fdt_chosen_bootargs_replace(fdt,"console=ttyS1", "console=ttyS3");
530                                 ret = fdt_chosen_bootargs_replace(fdt,"console=ttyS1", "console=null");
531                         }
532                 }
533                 return ret;
534         }
535 }
536
537 int fdt_fixup_serialno(void *fdt)
538 {
539     char buf[64];
540     int str_len;
541     int ret;
542     memset(buf, 0, 64);
543
544     sprintf(buf, " androidboot.serialno=%s", get_product_sn());
545     str_len = strlen(buf);
546     buf[str_len] = '\0';
547     ret = fdt_chosen_bootargs_append(fdt, buf, 1);
548     return ret;
549 }
550 int fdt_fixup_dram_training(void *fdt)
551 {
552     char buf[64];
553     int str_len;
554     int ret;
555     memset(buf, 0, 64);
556     extern PUBLIC int get_dram_cs_number(void);
557     extern PUBLIC int get_dram_cs0_size(void);
558     sprintf(buf, " mem_cs=%d, mem_cs0_sz=%08x",get_dram_cs_number(), get_dram_cs0_size());
559     str_len = strlen(buf);
560     buf[str_len] = '\0';
561     ret = fdt_chosen_bootargs_append(fdt, buf, 1);
562     return ret;
563 }
564
565 static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
566                                   uint32_t val)
567 {
568         val = cpu_to_fdt32(val);
569         return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
570 }
571
572 static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
573                                      const char *name, uint32_t val)
574 {
575         val = cpu_to_fdt32(val);
576         return fdt_appendprop(fdt, nodeoffset, name, &val, sizeof(val));
577 }
578
579
580 /**
581  * Fix me:
582  * fix_memory_size() interface which can be fixup memory size, but because of our
583  * device tree is not suitable for the context, for simple, we add a new function. In arm64,
584  * we should discard it. It should be ok now.
585  */
586 #ifdef CONFIG_DDR_AUTO_DETECT
587 int fdt_fixup_ddr_size(void *fdt)
588 {
589     int nodeoffset;
590     int err;
591     const char *path;
592     u32 val = CONFIG_SYS_SDRAM_BASE;
593     u32 dram_size = get_dram_size_from_gd();
594
595     nodeoffset = fdt_path_offset(fdt, "/memory");
596     printf("nodeoffset = %d\n", nodeoffset);
597     if (nodeoffset < 0) {
598         printf("ERROR: device tree must have /memory node %s.\n", fdt_strerror(nodeoffset));
599         return nodeoffset;
600     }
601
602     fdt_delprop(fdt, nodeoffset, "reg");
603
604     err = fdt_setprop_u32(fdt, nodeoffset, "reg", val);
605     if (err < 0) {
606         printf("ERROR: cannot set /memory node's reg property(addr)!\n");
607         return err;
608     }
609
610     err = fdt_appendprop_u32(fdt, nodeoffset, "reg", (u32)get_dram_size_from_gd());
611     if (err < 0) {
612         printf("ERROR: cannot set /memory node's reg property(size)!\n");
613     }
614
615     return err;
616 }
617 #else
618 int fdt_fixup_ddr_size(void *fdt)
619 {
620     return 0;
621 }
622 #endif
623
624 int fdt_fixup_adc_calibration_data(void *fdt)
625 {
626     //extern unsigned int *adc_data_to_transfer;
627     unsigned int *adc_data = malloc(64);
628     int ret = read_adc_calibration_data((void *)adc_data,48);
629
630     if (ret)
631     {
632         char buf[64];
633         int str_len;
634
635         memset(buf, 0, 64);
636         if(((adc_data[2]&0xffff) < 4500 )&&((adc_data[2]&0xffff) > 3000)&&
637         ((adc_data[3]&0xffff) < 4500 )&&((adc_data[3]&0xffff) > 3000))
638         {
639             str_len = strlen(buf);
640             sprintf(&buf[str_len], " adc_cal=%d,%d",adc_data[2],adc_data[3]);
641         }
642          /*just after fgu adc calibration,and no aux adc calibration,need save fgu adc parameters */
643         if((0x00000002 == adc_data[10])&&(0x00000002 & adc_data[11]))
644         {
645             str_len = strlen(buf);
646             sprintf(&buf[str_len], " fgu_cal=%d,%d,%d",adc_data[4],adc_data[5],adc_data[6]);
647         }
648
649         str_len = strlen(buf);
650         buf[str_len] = '\0';
651         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
652     }else{
653         printf("read_adc_calibration_data failed\n");
654     }
655     free(adc_data);
656     return ret;
657 }
658
659 int fdt_fixup_mtd(void *fdt)
660 {
661         char buf[128];
662         int str_len;
663         int ret;
664
665         memset(buf, 0, 128);
666
667         sprintf(buf, MTDPARTS_DEFAULT);
668         str_len = strlen(buf);
669         buf[str_len] = '\0';
670
671         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
672         return ret;
673 }
674
675 int fdt_fixup_boot_mode(void *fdt,char * boot_mode)
676 {
677         char buf[128];
678         int str_len;
679         int ret;
680
681         memset(buf, 0, 128);
682
683         sprintf(buf, boot_mode);
684         str_len = strlen(buf);
685         buf[str_len] = '\0';
686
687         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
688         return ret;
689 }
690
691 int fdt_fixup_boot_ram_log(void *fdt)
692 {
693 #ifdef CONFIG_RAM_CONSOLE
694         char buf[128];
695         int str_len;
696         int ret;
697
698         memset(buf, 0, 128);
699
700         sprintf(buf, "boot_ram_log=%#010x,%#x", CONFIG_RAM_CONSOLE_START, CONFIG_RAM_CONSOLE_SIZE);
701         str_len = strlen(buf);
702         buf[str_len] = '\0';
703
704         ret = fdt_chosen_bootargs_append(fdt, buf, 1);
705
706         /* gerenally, reserved memory should be configured in dts file. Add fixup here to avoid the wide
707            range of change for various boards.
708          */
709         if (ret < 0)
710                 printf("failed to append ram log option in bootargs\n");
711         else
712                 ret = fdt_add_mem_rsv(fdt, CONFIG_RAM_CONSOLE_START, CONFIG_RAM_CONSOLE_SIZE);
713
714         return ret;
715 #endif
716 }
717
718
719 /**
720  * You can re-define function void fdt_fixup_chosen_bootargs_board(char *buf, const char *boot_mode, int calibration_mode)
721  * in your u-boot/board/spreadtrum/xxx/openphone.c to override this default function
722  */
723 void __attribute__((weak)) fdt_fixup_chosen_bootargs_board(char *buf, const char *boot_mode, int calibration_mode)
724 { }
725
726 int fdt_fixup_chosen_bootargs_board_private(void *fdt, const char *boot_mode)
727 {
728         int ret = 0;
729         char buf[512];
730         memset(buf, 0, sizeof buf);
731         fdt_fixup_chosen_bootargs_board(buf, boot_mode, poweron_by_calibration());
732         if (buf[0])
733                 ret = fdt_chosen_bootargs_append(fdt, buf, 1);
734         return ret;
735 }
736
737 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
738                       const void *val, int len, int create)
739 {
740 #if defined(DEBUG)
741         int i;
742         debug("Updating property '%s/%s' = ", path, prop);
743         for (i = 0; i < len; i++)
744                 debug(" %.2x", *(u8*)(val+i));
745         debug("\n");
746 #endif
747         int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
748         if (rc)
749                 printf("Unable to update property %s:%s, err=%s\n",
750                         path, prop, fdt_strerror(rc));
751 }
752
753 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
754                           u32 val, int create)
755 {
756         val = cpu_to_fdt32(val);
757         do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
758 }
759
760 void do_fixup_by_prop(void *fdt,
761                       const char *pname, const void *pval, int plen,
762                       const char *prop, const void *val, int len,
763                       int create)
764 {
765         int off;
766 #if defined(DEBUG)
767         int i;
768         debug("Updating property '%s' = ", prop);
769         for (i = 0; i < len; i++)
770                 debug(" %.2x", *(u8*)(val+i));
771         debug("\n");
772 #endif
773         off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
774         while (off != -FDT_ERR_NOTFOUND) {
775                 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
776                         fdt_setprop(fdt, off, prop, val, len);
777                 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
778         }
779 }
780
781 void do_fixup_by_prop_u32(void *fdt,
782                           const char *pname, const void *pval, int plen,
783                           const char *prop, u32 val, int create)
784 {
785         val = cpu_to_fdt32(val);
786         do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
787 }
788
789 void do_fixup_by_compat(void *fdt, const char *compat,
790                         const char *prop, const void *val, int len, int create)
791 {
792         int off = -1;
793 #if defined(DEBUG)
794         int i;
795         debug("Updating property '%s' = ", prop);
796         for (i = 0; i < len; i++)
797                 debug(" %.2x", *(u8*)(val+i));
798         debug("\n");
799 #endif
800         off = fdt_node_offset_by_compatible(fdt, -1, compat);
801         while (off != -FDT_ERR_NOTFOUND) {
802                 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
803                         fdt_setprop(fdt, off, prop, val, len);
804                 off = fdt_node_offset_by_compatible(fdt, off, compat);
805         }
806 }
807
808 void do_fixup_by_compat_u32(void *fdt, const char *compat,
809                             const char *prop, u32 val, int create)
810 {
811         val = cpu_to_fdt32(val);
812         do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
813 }
814
815 /*
816  * Get cells len in bytes
817  *     if #NNNN-cells property is 2 then len is 8
818  *     otherwise len is 4
819  */
820 static int get_cells_len(void *blob, char *nr_cells_name)
821 {
822         const u32 *cell;
823
824         cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
825         if (cell && *cell == 2)
826                 return 8;
827
828         return 4;
829 }
830
831 /*
832  * Write a 4 or 8 byte big endian cell
833  */
834 static void write_cell(u8 *addr, u64 val, int size)
835 {
836         int shift = (size - 1) * 8;
837         while (size-- > 0) {
838                 *addr++ = (val >> shift) & 0xff;
839                 shift -= 8;
840         }
841 }
842
843 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
844 {
845         int err, nodeoffset;
846         int addr_cell_len, size_cell_len, len;
847         u8 tmp[banks * 8];
848         int bank;
849
850         err = fdt_check_header(blob);
851         if (err < 0) {
852                 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
853                 return err;
854         }
855
856         /* update, or add and update /memory node */
857         nodeoffset = fdt_path_offset(blob, "/memory");
858         if (nodeoffset < 0) {
859                 nodeoffset = fdt_add_subnode(blob, 0, "memory");
860                 if (nodeoffset < 0)
861                         printf("WARNING: could not create /memory: %s.\n",
862                                         fdt_strerror(nodeoffset));
863                 return nodeoffset;
864         }
865         err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
866                         sizeof("memory"));
867         if (err < 0) {
868                 printf("WARNING: could not set %s %s.\n", "device_type",
869                                 fdt_strerror(err));
870                 return err;
871         }
872
873         addr_cell_len = get_cells_len(blob, "#address-cells");
874         size_cell_len = get_cells_len(blob, "#size-cells");
875
876         for (bank = 0, len = 0; bank < banks; bank++) {
877                 write_cell(tmp + len, start[bank], addr_cell_len);
878                 len += addr_cell_len;
879
880                 write_cell(tmp + len, size[bank], size_cell_len);
881                 len += size_cell_len;
882         }
883
884         err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
885         if (err < 0) {
886                 printf("WARNING: could not set %s %s.\n",
887                                 "reg", fdt_strerror(err));
888                 return err;
889         }
890         return 0;
891 }
892
893 int fdt_fixup_memory(void *blob, u64 start, u64 size)
894 {
895         return fdt_fixup_memory_banks(blob, &start, &size, 1);
896 }
897
898 void fdt_fixup_ethernet(void *fdt)
899 {
900         int node, i, j;
901         char enet[16], *tmp, *end;
902         char mac[16] = "ethaddr";
903         const char *path;
904         unsigned char mac_addr[6];
905
906         node = fdt_path_offset(fdt, "/aliases");
907         if (node < 0)
908                 return;
909
910         i = 0;
911         while ((tmp = getenv(mac)) != NULL) {
912                 sprintf(enet, "ethernet%d", i);
913                 path = fdt_getprop(fdt, node, enet, NULL);
914                 if (!path) {
915                         debug("No alias for %s\n", enet);
916                         sprintf(mac, "eth%daddr", ++i);
917                         continue;
918                 }
919
920                 for (j = 0; j < 6; j++) {
921                         mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
922                         if (tmp)
923                                 tmp = (*end) ? end+1 : end;
924                 }
925
926                 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
927                 do_fixup_by_path(fdt, path, "local-mac-address",
928                                 &mac_addr, 6, 1);
929
930                 sprintf(mac, "eth%daddr", ++i);
931         }
932 }
933
934 /* Resize the fdt to its actual size + a bit of padding */
935 int fdt_resize(void *blob)
936 {
937         int i;
938         uint64_t addr, size;
939         int total, ret;
940         uint actualsize;
941
942         if (!blob)
943                 return 0;
944
945         total = fdt_num_mem_rsv(blob);
946         for (i = 0; i < total; i++) {
947                 fdt_get_mem_rsv(blob, i, &addr, &size);
948                 if (addr == (uint64_t)(u32)blob) {
949                         fdt_del_mem_rsv(blob, i);
950                         break;
951                 }
952         }
953
954         /*
955          * Calculate the actual size of the fdt
956          * plus the size needed for 5 fdt_add_mem_rsv, one
957          * for the fdt itself and 4 for a possible initrd
958          * ((initrd-start + initrd-end) * 2 (name & value))
959          */
960         actualsize = fdt_off_dt_strings(blob) +
961                 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
962
963         /* Make it so the fdt ends on a page boundary */
964         actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
965         actualsize = actualsize - ((uint)blob & 0xfff);
966
967         /* Change the fdt header to reflect the correct size */
968         fdt_set_totalsize(blob, actualsize);
969
970         /* Add the new reservation */
971         ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
972         if (ret < 0)
973                 return ret;
974
975         return actualsize;
976 }
977
978 #ifdef CONFIG_PCI
979 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
980
981 #define FDT_PCI_PREFETCH        (0x40000000)
982 #define FDT_PCI_MEM32           (0x02000000)
983 #define FDT_PCI_IO              (0x01000000)
984 #define FDT_PCI_MEM64           (0x03000000)
985
986 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
987
988         int addrcell, sizecell, len, r;
989         u32 *dma_range;
990         /* sized based on pci addr cells, size-cells, & address-cells */
991         u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
992
993         addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
994         sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
995
996         dma_range = &dma_ranges[0];
997         for (r = 0; r < hose->region_count; r++) {
998                 u64 bus_start, phys_start, size;
999
1000                 /* skip if !PCI_REGION_SYS_MEMORY */
1001                 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
1002                         continue;
1003
1004                 bus_start = (u64)hose->regions[r].bus_start;
1005                 phys_start = (u64)hose->regions[r].phys_start;
1006                 size = (u64)hose->regions[r].size;
1007
1008                 dma_range[0] = 0;
1009                 if (size >= 0x100000000ull)
1010                         dma_range[0] |= FDT_PCI_MEM64;
1011                 else
1012                         dma_range[0] |= FDT_PCI_MEM32;
1013                 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
1014                         dma_range[0] |= FDT_PCI_PREFETCH;
1015 #ifdef CONFIG_SYS_PCI_64BIT
1016                 dma_range[1] = bus_start >> 32;
1017 #else
1018                 dma_range[1] = 0;
1019 #endif
1020                 dma_range[2] = bus_start & 0xffffffff;
1021
1022                 if (addrcell == 2) {
1023                         dma_range[3] = phys_start >> 32;
1024                         dma_range[4] = phys_start & 0xffffffff;
1025                 } else {
1026                         dma_range[3] = phys_start & 0xffffffff;
1027                 }
1028
1029                 if (sizecell == 2) {
1030                         dma_range[3 + addrcell + 0] = size >> 32;
1031                         dma_range[3 + addrcell + 1] = size & 0xffffffff;
1032                 } else {
1033                         dma_range[3 + addrcell + 0] = size & 0xffffffff;
1034                 }
1035
1036                 dma_range += (3 + addrcell + sizecell);
1037         }
1038
1039         len = dma_range - &dma_ranges[0];
1040         if (len)
1041                 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
1042
1043         return 0;
1044 }
1045 #endif
1046
1047 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
1048 /*
1049  * Provide a weak default function to return the flash bank size.
1050  * There might be multiple non-identical flash chips connected to one
1051  * chip-select, so we need to pass an index as well.
1052  */
1053 u32 __flash_get_bank_size(int cs, int idx)
1054 {
1055         extern flash_info_t flash_info[];
1056
1057         /*
1058          * As default, a simple 1:1 mapping is provided. Boards with
1059          * a different mapping need to supply a board specific mapping
1060          * routine.
1061          */
1062         return flash_info[cs].size;
1063 }
1064 u32 flash_get_bank_size(int cs, int idx)
1065         __attribute__((weak, alias("__flash_get_bank_size")));
1066
1067 /*
1068  * This function can be used to update the size in the "reg" property
1069  * of all NOR FLASH device nodes. This is necessary for boards with
1070  * non-fixed NOR FLASH sizes.
1071  */
1072 int fdt_fixup_nor_flash_size(void *blob)
1073 {
1074         char compat[][16] = { "cfi-flash", "jedec-flash" };
1075         int off;
1076         int len;
1077         struct fdt_property *prop;
1078         u32 *reg, *reg2;
1079         int i;
1080
1081         for (i = 0; i < 2; i++) {
1082                 off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
1083                 while (off != -FDT_ERR_NOTFOUND) {
1084                         int idx;
1085
1086                         /*
1087                          * Found one compatible node, so fixup the size
1088                          * int its reg properties
1089                          */
1090                         prop = fdt_get_property_w(blob, off, "reg", &len);
1091                         if (prop) {
1092                                 int tuple_size = 3 * sizeof(reg);
1093
1094                                 /*
1095                                  * There might be multiple reg-tuples,
1096                                  * so loop through them all
1097                                  */
1098                                 reg = reg2 = (u32 *)&prop->data[0];
1099                                 for (idx = 0; idx < (len / tuple_size); idx++) {
1100                                         /*
1101                                          * Update size in reg property
1102                                          */
1103                                         reg[2] = flash_get_bank_size(reg[0],
1104                                                                      idx);
1105
1106                                         /*
1107                                          * Point to next reg tuple
1108                                          */
1109                                         reg += 3;
1110                                 }
1111
1112                                 fdt_setprop(blob, off, "reg", reg2, len);
1113                         }
1114
1115                         /* Move to next compatible node */
1116                         off = fdt_node_offset_by_compatible(blob, off,
1117                                                             compat[i]);
1118                 }
1119         }
1120
1121         return 0;
1122 }
1123 #endif
1124
1125 int fdt_increase_size(void *fdt, int add_len)
1126 {
1127         int newlen;
1128
1129         newlen = fdt_totalsize(fdt) + add_len;
1130
1131         /* Open in place with a new len */
1132         return fdt_open_into(fdt, fdt, newlen);
1133 }
1134
1135 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
1136 #include <jffs2/load_kernel.h>
1137 #include <mtd_node.h>
1138
1139 struct reg_cell {
1140         unsigned int r0;
1141         unsigned int r1;
1142 };
1143
1144 int fdt_del_subnodes(const void *blob, int parent_offset)
1145 {
1146         int off, ndepth;
1147         int ret;
1148
1149         for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
1150              (off >= 0) && (ndepth > 0);
1151              off = fdt_next_node(blob, off, &ndepth)) {
1152                 if (ndepth == 1) {
1153                         debug("delete %s: offset: %x\n",
1154                                 fdt_get_name(blob, off, 0), off);
1155                         ret = fdt_del_node((void *)blob, off);
1156                         if (ret < 0) {
1157                                 printf("Can't delete node: %s\n",
1158                                         fdt_strerror(ret));
1159                                 return ret;
1160                         } else {
1161                                 ndepth = 0;
1162                                 off = parent_offset;
1163                         }
1164                 }
1165         }
1166         return 0;
1167 }
1168
1169 int fdt_del_partitions(void *blob, int parent_offset)
1170 {
1171         const void *prop;
1172         int ndepth = 0;
1173         int off;
1174         int ret;
1175
1176         off = fdt_next_node(blob, parent_offset, &ndepth);
1177         if (off > 0 && ndepth == 1) {
1178                 prop = fdt_getprop(blob, off, "label", NULL);
1179                 if (prop == NULL) {
1180                         /*
1181                          * Could not find label property, nand {}; node?
1182                          * Check subnode, delete partitions there if any.
1183                          */
1184                         return fdt_del_partitions(blob, off);
1185                 } else {
1186                         ret = fdt_del_subnodes(blob, parent_offset);
1187                         if (ret < 0) {
1188                                 printf("Can't remove subnodes: %s\n",
1189                                         fdt_strerror(ret));
1190                                 return ret;
1191                         }
1192                 }
1193         }
1194         return 0;
1195 }
1196
1197 int fdt_node_set_part_info(void *blob, int parent_offset,
1198                            struct mtd_device *dev)
1199 {
1200         struct list_head *pentry;
1201         struct part_info *part;
1202         struct reg_cell cell;
1203         int off, ndepth = 0;
1204         int part_num, ret;
1205         char buf[64];
1206
1207         ret = fdt_del_partitions(blob, parent_offset);
1208         if (ret < 0)
1209                 return ret;
1210
1211         /*
1212          * Check if it is nand {}; subnode, adjust
1213          * the offset in this case
1214          */
1215         off = fdt_next_node(blob, parent_offset, &ndepth);
1216         if (off > 0 && ndepth == 1)
1217                 parent_offset = off;
1218
1219         part_num = 0;
1220         list_for_each_prev(pentry, &dev->parts) {
1221                 int newoff;
1222
1223                 part = list_entry(pentry, struct part_info, link);
1224
1225                 debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1226                         part_num, part->name, part->size,
1227                         part->offset, part->mask_flags);
1228
1229                 sprintf(buf, "partition@%x", part->offset);
1230 add_sub:
1231                 ret = fdt_add_subnode(blob, parent_offset, buf);
1232                 if (ret == -FDT_ERR_NOSPACE) {
1233                         ret = fdt_increase_size(blob, 512);
1234                         if (!ret)
1235                                 goto add_sub;
1236                         else
1237                                 goto err_size;
1238                 } else if (ret < 0) {
1239                         printf("Can't add partition node: %s\n",
1240                                 fdt_strerror(ret));
1241                         return ret;
1242                 }
1243                 newoff = ret;
1244
1245                 /* Check MTD_WRITEABLE_CMD flag */
1246                 if (part->mask_flags & 1) {
1247 add_ro:
1248                         ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
1249                         if (ret == -FDT_ERR_NOSPACE) {
1250                                 ret = fdt_increase_size(blob, 512);
1251                                 if (!ret)
1252                                         goto add_ro;
1253                                 else
1254                                         goto err_size;
1255                         } else if (ret < 0)
1256                                 goto err_prop;
1257                 }
1258
1259                 cell.r0 = cpu_to_fdt32(part->offset);
1260                 cell.r1 = cpu_to_fdt32(part->size);
1261 add_reg:
1262                 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
1263                 if (ret == -FDT_ERR_NOSPACE) {
1264                         ret = fdt_increase_size(blob, 512);
1265                         if (!ret)
1266                                 goto add_reg;
1267                         else
1268                                 goto err_size;
1269                 } else if (ret < 0)
1270                         goto err_prop;
1271
1272 add_label:
1273                 ret = fdt_setprop_string(blob, newoff, "label", part->name);
1274                 if (ret == -FDT_ERR_NOSPACE) {
1275                         ret = fdt_increase_size(blob, 512);
1276                         if (!ret)
1277                                 goto add_label;
1278                         else
1279                                 goto err_size;
1280                 } else if (ret < 0)
1281                         goto err_prop;
1282
1283                 part_num++;
1284         }
1285         return 0;
1286 err_size:
1287         printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1288         return ret;
1289 err_prop:
1290         printf("Can't add property: %s\n", fdt_strerror(ret));
1291         return ret;
1292 }
1293
1294 /*
1295  * Update partitions in nor/nand nodes using info from
1296  * mtdparts environment variable. The nodes to update are
1297  * specified by node_info structure which contains mtd device
1298  * type and compatible string: E. g. the board code in
1299  * ft_board_setup() could use:
1300  *
1301  *      struct node_info nodes[] = {
1302  *              { "fsl,mpc5121-nfc",    MTD_DEV_TYPE_NAND, },
1303  *              { "cfi-flash",          MTD_DEV_TYPE_NOR,  },
1304  *      };
1305  *
1306  *      fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1307  */
1308 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
1309 {
1310         struct node_info *ni = node_info;
1311         struct mtd_device *dev;
1312         char *parts;
1313         int i, idx;
1314         int noff;
1315
1316         parts = getenv("mtdparts");
1317         if (!parts)
1318                 return;
1319
1320         if (mtdparts_init() != 0)
1321                 return;
1322
1323         for (i = 0; i < node_info_size; i++) {
1324                 idx = 0;
1325                 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
1326                 while (noff != -FDT_ERR_NOTFOUND) {
1327                         debug("%s: %s, mtd dev type %d\n",
1328                                 fdt_get_name(blob, noff, 0),
1329                                 ni[i].compat, ni[i].type);
1330                         dev = device_find(ni[i].type, idx++);
1331                         if (dev) {
1332                                 if (fdt_node_set_part_info(blob, noff, dev))
1333                                         return; /* return on error */
1334                         }
1335
1336                         /* Jump to next flash node */
1337                         noff = fdt_node_offset_by_compatible(blob, noff,
1338                                                              ni[i].compat);
1339                 }
1340         }
1341 }
1342 #endif
1343
1344 void fdt_del_node_and_alias(void *blob, const char *alias)
1345 {
1346         int off = fdt_path_offset(blob, alias);
1347
1348         if (off < 0)
1349                 return;
1350
1351         fdt_del_node(blob, off);
1352
1353         off = fdt_path_offset(blob, "/aliases");
1354         fdt_delprop(blob, off, alias);
1355 }
1356
1357 /* Helper to read a big number; size is in cells (not bytes) */
1358 static inline u64 of_read_number(const __be32 *cell, int size)
1359 {
1360         u64 r = 0;
1361         while (size--)
1362                 r = (r << 32) | be32_to_cpu(*(cell++));
1363         return r;
1364 }
1365
1366 #define PRu64   "%llx"
1367
1368 /* Max address size we deal with */
1369 #define OF_MAX_ADDR_CELLS       4
1370 #define OF_BAD_ADDR     ((u64)-1)
1371 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1372                         (ns) > 0)
1373
1374 /* Debug utility */
1375 #ifdef DEBUG
1376 static void of_dump_addr(const char *s, const u32 *addr, int na)
1377 {
1378         printf("%s", s);
1379         while(na--)
1380                 printf(" %08x", *(addr++));
1381         printf("\n");
1382 }
1383 #else
1384 static void of_dump_addr(const char *s, const u32 *addr, int na) { }
1385 #endif
1386
1387 /* Callbacks for bus specific translators */
1388 struct of_bus {
1389         const char      *name;
1390         const char      *addresses;
1391         void            (*count_cells)(void *blob, int parentoffset,
1392                                 int *addrc, int *sizec);
1393         u64             (*map)(u32 *addr, const u32 *range,
1394                                 int na, int ns, int pna);
1395         int             (*translate)(u32 *addr, u64 offset, int na);
1396 };
1397
1398 /* Default translator (generic bus) */
1399 static void of_bus_default_count_cells(void *blob, int parentoffset,
1400                                         int *addrc, int *sizec)
1401 {
1402         const u32 *prop;
1403
1404         if (addrc) {
1405                 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
1406                 if (prop)
1407                         *addrc = be32_to_cpup((u32 *)prop);
1408                 else
1409                         *addrc = 2;
1410         }
1411
1412         if (sizec) {
1413                 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1414                 if (prop)
1415                         *sizec = be32_to_cpup((u32 *)prop);
1416                 else
1417                         *sizec = 1;
1418         }
1419 }
1420
1421 static u64 of_bus_default_map(u32 *addr, const u32 *range,
1422                 int na, int ns, int pna)
1423 {
1424         u64 cp, s, da;
1425
1426         cp = of_read_number(range, na);
1427         s  = of_read_number(range + na + pna, ns);
1428         da = of_read_number(addr, na);
1429
1430         debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
1431             cp, s, da);
1432
1433         if (da < cp || da >= (cp + s))
1434                 return OF_BAD_ADDR;
1435         return da - cp;
1436 }
1437
1438 static int of_bus_default_translate(u32 *addr, u64 offset, int na)
1439 {
1440         u64 a = of_read_number(addr, na);
1441         memset(addr, 0, na * 4);
1442         a += offset;
1443         if (na > 1)
1444                 addr[na - 2] = a >> 32;
1445         addr[na - 1] = a & 0xffffffffu;
1446
1447         return 0;
1448 }
1449
1450 /* Array of bus specific translators */
1451 static struct of_bus of_busses[] = {
1452         /* Default */
1453         {
1454                 .name = "default",
1455                 .addresses = "reg",
1456                 .count_cells = of_bus_default_count_cells,
1457                 .map = of_bus_default_map,
1458                 .translate = of_bus_default_translate,
1459         },
1460 };
1461
1462 static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1463                             struct of_bus *pbus, u32 *addr,
1464                             int na, int ns, int pna, const char *rprop)
1465 {
1466         const u32 *ranges;
1467         int rlen;
1468         int rone;
1469         u64 offset = OF_BAD_ADDR;
1470
1471         /* Normally, an absence of a "ranges" property means we are
1472          * crossing a non-translatable boundary, and thus the addresses
1473          * below the current not cannot be converted to CPU physical ones.
1474          * Unfortunately, while this is very clear in the spec, it's not
1475          * what Apple understood, and they do have things like /uni-n or
1476          * /ht nodes with no "ranges" property and a lot of perfectly
1477          * useable mapped devices below them. Thus we treat the absence of
1478          * "ranges" as equivalent to an empty "ranges" property which means
1479          * a 1:1 translation at that level. It's up to the caller not to try
1480          * to translate addresses that aren't supposed to be translated in
1481          * the first place. --BenH.
1482          */
1483         ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen);
1484         if (ranges == NULL || rlen == 0) {
1485                 offset = of_read_number(addr, na);
1486                 memset(addr, 0, pna * 4);
1487                 debug("OF: no ranges, 1:1 translation\n");
1488                 goto finish;
1489         }
1490
1491         debug("OF: walking ranges...\n");
1492
1493         /* Now walk through the ranges */
1494         rlen /= 4;
1495         rone = na + pna + ns;
1496         for (; rlen >= rone; rlen -= rone, ranges += rone) {
1497                 offset = bus->map(addr, ranges, na, ns, pna);
1498                 if (offset != OF_BAD_ADDR)
1499                         break;
1500         }
1501         if (offset == OF_BAD_ADDR) {
1502                 debug("OF: not found !\n");
1503                 return 1;
1504         }
1505         memcpy(addr, ranges + na, 4 * pna);
1506
1507  finish:
1508         of_dump_addr("OF: parent translation for:", addr, pna);
1509         debug("OF: with offset: "PRu64"\n", offset);
1510
1511         /* Translate it into parent bus space */
1512         return pbus->translate(addr, offset, pna);
1513 }
1514
1515 /*
1516  * Translate an address from the device-tree into a CPU physical address,
1517  * this walks up the tree and applies the various bus mappings on the
1518  * way.
1519  *
1520  * Note: We consider that crossing any level with #size-cells == 0 to mean
1521  * that translation is impossible (that is we are not dealing with a value
1522  * that can be mapped to a cpu physical address). This is not really specified
1523  * that way, but this is traditionally the way IBM at least do things
1524  */
1525 u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr,
1526                            const char *rprop)
1527 {
1528         int parent;
1529         struct of_bus *bus, *pbus;
1530         u32 addr[OF_MAX_ADDR_CELLS];
1531         int na, ns, pna, pns;
1532         u64 result = OF_BAD_ADDR;
1533
1534         debug("OF: ** translation for device %s **\n",
1535                 fdt_get_name(blob, node_offset, NULL));
1536
1537         /* Get parent & match bus type */
1538         parent = fdt_parent_offset(blob, node_offset);
1539         if (parent < 0)
1540                 goto bail;
1541         bus = &of_busses[0];
1542
1543         /* Cound address cells & copy address locally */
1544         bus->count_cells(blob, parent, &na, &ns);
1545         if (!OF_CHECK_COUNTS(na, ns)) {
1546                 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1547                        fdt_get_name(blob, node_offset, NULL));
1548                 goto bail;
1549         }
1550         memcpy(addr, in_addr, na * 4);
1551
1552         debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1553             bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1554         of_dump_addr("OF: translating address:", addr, na);
1555
1556         /* Translate */
1557         for (;;) {
1558                 /* Switch to parent bus */
1559                 node_offset = parent;
1560                 parent = fdt_parent_offset(blob, node_offset);
1561
1562                 /* If root, we have finished */
1563                 if (parent < 0) {
1564                         debug("OF: reached root node\n");
1565                         result = of_read_number(addr, na);
1566                         break;
1567                 }
1568
1569                 /* Get new parent bus and counts */
1570                 pbus = &of_busses[0];
1571                 pbus->count_cells(blob, parent, &pna, &pns);
1572                 if (!OF_CHECK_COUNTS(pna, pns)) {
1573                         printf("%s: Bad cell count for %s\n", __FUNCTION__,
1574                                 fdt_get_name(blob, node_offset, NULL));
1575                         break;
1576                 }
1577
1578                 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1579                     pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1580
1581                 /* Apply bus translation */
1582                 if (of_translate_one(blob, node_offset, bus, pbus,
1583                                         addr, na, ns, pna, rprop))
1584                         break;
1585
1586                 /* Complete the move up one level */
1587                 na = pna;
1588                 ns = pns;
1589                 bus = pbus;
1590
1591                 of_dump_addr("OF: one level translation:", addr, na);
1592         }
1593  bail:
1594
1595         return result;
1596 }
1597
1598 u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr)
1599 {
1600         return __of_translate_address(blob, node_offset, in_addr, "ranges");
1601 }
1602
1603 /**
1604  * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1605  * who's reg property matches a physical cpu address
1606  *
1607  * @blob: ptr to device tree
1608  * @compat: compatiable string to match
1609  * @compat_off: property name
1610  *
1611  */
1612 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1613                                         phys_addr_t compat_off)
1614 {
1615         int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1616         while (off != -FDT_ERR_NOTFOUND) {
1617                 u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len);
1618                 if (reg) {
1619                         if (compat_off == fdt_translate_address(blob, off, reg))
1620                                 return off;
1621                 }
1622                 off = fdt_node_offset_by_compatible(blob, off, compat);
1623         }
1624
1625         return -FDT_ERR_NOTFOUND;
1626 }
1627
1628 /**
1629  * fdt_alloc_phandle: Return next free phandle value
1630  *
1631  * @blob: ptr to device tree
1632  */
1633 int fdt_alloc_phandle(void *blob)
1634 {
1635         int offset, len, phandle = 0;
1636         const u32 *val;
1637
1638         for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1639              offset = fdt_next_node(blob, offset, NULL)) {
1640                 val = fdt_getprop(blob, offset, "linux,phandle", &len);
1641                 if (val)
1642                         phandle = max(*val, phandle);
1643         }
1644
1645         return phandle + 1;
1646 }
1647
1648 #if defined(CONFIG_VIDEO)
1649 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1650 {
1651         int noff;
1652         int ret;
1653
1654         noff = fdt_node_offset_by_compatible(blob, -1, compat);
1655         if (noff != -FDT_ERR_NOTFOUND) {
1656                 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1657 add_edid:
1658                 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1659                 if (ret == -FDT_ERR_NOSPACE) {
1660                         ret = fdt_increase_size(blob, 512);
1661                         if (!ret)
1662                                 goto add_edid;
1663                         else
1664                                 goto err_size;
1665                 } else if (ret < 0) {
1666                         printf("Can't add property: %s\n", fdt_strerror(ret));
1667                         return ret;
1668                 }
1669         }
1670         return 0;
1671 err_size:
1672         printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1673         return ret;
1674 }
1675 #endif
1676
1677 void fdt_debug_print_prop(void *fdt, const char *node_path, const char *name)
1678 {
1679         int   nodeoffset;
1680         int   err;
1681         const char *path;
1682         int     len;
1683         int i = 0;
1684
1685         printf("fdt addr: 0x%lx\n", (unsigned long)fdt);
1686
1687         err = fdt_check_header(fdt);
1688         if (err < 0) {
1689                 printf("not found fdt header: %s\n", fdt_strerror(err));
1690                 return;
1691         }
1692
1693         nodeoffset = fdt_path_offset (fdt, node_path);
1694
1695         if (nodeoffset < 0) {
1696                 printf("not found %s path\n", node_path);
1697                 return;
1698         }
1699
1700         path = fdt_getprop(fdt, nodeoffset, name, &len);
1701
1702         if (len < 0) {
1703                 printf("not found %s prop\n", name);
1704                 return;
1705         }
1706
1707         printf("================\n");
1708         printf("%s/%s: ", node_path, name);
1709         for (i = 0; i < len; i++) {
1710                 printf("%c", *(path)++);
1711         }
1712         printf("\n");
1713         printf("================\n");
1714
1715         return;
1716 }