]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/common/fdt.c
karo: fdt: improve display fdt path handling
[karo-tx-uboot.git] / board / karo / common / fdt.c
1 /*
2  * (C) Copyright 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16 */
17
18 #include <common.h>
19 #include <errno.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <nand.h>
23 #include <mxcfb.h>
24 #include <linux/list.h>
25 #include <linux/fb.h>
26 #include <jffs2/load_kernel.h>
27 #include <malloc.h>
28
29 #include "karo.h"
30
31 #ifdef CONFIG_MAX_DTB_SIZE
32 #define MAX_DTB_SIZE    CONFIG_MAX_DTB_SIZE
33 #else
34 #define MAX_DTB_SIZE    SZ_64K
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 static void karo_set_fdtsize(void *fdt)
40 {
41         char fdt_size[9];
42         size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
43
44         if (fdtsize == fdt_totalsize(fdt)) {
45                 return;
46         }
47         debug("FDT size changed from %u to %u\n",
48                 fdtsize, fdt_totalsize(fdt));
49
50         snprintf(fdt_size, sizeof(fdt_size), "%08x", fdt_totalsize(fdt));
51         setenv("fdtsize", fdt_size);
52 }
53
54 void karo_fdt_move_fdt(void)
55 {
56         void *fdt;
57         unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
58
59         if (working_fdt) {
60                 debug("DTB already loaded\n");
61                 return;
62         }
63
64         if (!fdt_addr) {
65                 fdt_addr = CONFIG_SYS_FDT_ADDR;
66                 printf("fdtaddr is not set; using default: %08lx\n",
67                         fdt_addr);
68         }
69
70         fdt = karo_fdt_load_dtb();
71         if (fdt == NULL) {
72                 fdt = (void *)gd->fdt_blob;
73                 if (fdt == NULL) {
74 #ifdef CONFIG_OF_EMBED
75                         printf("Compiled in FDT not found\n");
76 #else
77                         printf("No FDT found\n");
78 #endif
79                         return;
80                 }
81                 debug("Checking FDT header @ %p\n", fdt);
82                 if (fdt_check_header(fdt)) {
83                         printf("ERROR: No valid DTB found at %p\n", fdt);
84                         return;
85                 }
86                 printf("No DTB in flash; using default DTB\n");
87                 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
88                         fdt, fdt + fdt_totalsize(fdt) - 1,
89                         fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
90                 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
91         }
92         set_working_fdt_addr((void *)fdt_addr);
93         gd->fdt_blob = fdt;
94         karo_set_fdtsize(fdt);
95 }
96
97 void karo_fdt_remove_node(void *blob, const char *node)
98 {
99         int off = fdt_path_offset(blob, node);
100         int ret;
101
102         debug("Removing node '%s' from DT\n", node);
103
104         if (off < 0) {
105                 printf("Could not find node '%s': %d\n", node, off);
106         } else {
107                 ret = fdt_del_node(blob, off);
108                 if (ret)
109                         printf("Failed to remove node '%s': %d\n",
110                                 node, ret);
111         }
112         karo_set_fdtsize(blob);
113 }
114
115 void karo_fdt_enable_node(void *blob, const char *node, int enable)
116 {
117         int off = fdt_path_offset(blob, node);
118
119         debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
120         if (off < 0) {
121                 printf("Could not find node '%s': %d\n", node, off);
122                 return;
123         }
124         fdt_set_node_status(blob, off,
125                         enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
126
127         karo_set_fdtsize(blob);
128 }
129
130 static const char *karo_touchpanels[] = {
131         "ti,tsc2007",
132         "edt,edt-ft5x06",
133 #ifdef CONFIG_MX28
134         "fsl,imx28-lradc",
135 #endif
136 };
137
138 static void fdt_disable_tp_node(void *blob, const char *name)
139 {
140         int offs = fdt_node_offset_by_compatible(blob, -1, name);
141
142         if (offs < 0) {
143                 debug("node '%s' not found: %d\n", name, offs);
144                 return;
145         }
146
147         debug("Disabling node '%s'\n", name);
148         fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
149 }
150
151 void karo_fdt_fixup_touchpanel(void *blob)
152 {
153         int i;
154         const char *model = getenv("touchpanel");
155
156         for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
157                 const char *tp = karo_touchpanels[i];
158
159                 if (model != NULL && strcmp(model, tp) == 0)
160                         continue;
161
162                 if (model != NULL) {
163                         if (strcmp(model, tp) == 0)
164                                 continue;
165                         tp = strchr(tp, ',');
166                         if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
167                                 continue;
168                 }
169                 fdt_disable_tp_node(blob, karo_touchpanels[i]);
170         }
171         karo_set_fdtsize(blob);
172 }
173
174 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
175 {
176         const char *otg_mode = getenv("otg_mode");
177         int off;
178         int ret;
179         const uint32_t *ph;
180         int disable_otg = 0;
181         int disable_phy_pins = 1;
182
183         debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
184
185         off = fdt_path_offset(blob, node);
186         if (off < 0) {
187                 debug("Failed to find node %s\n", node);
188                 return;
189         }
190
191         if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
192                                 strcmp(otg_mode, "gadget") == 0)) {
193                 debug("Setting dr_mode to 'peripheral'\n");
194                 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
195         } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
196                 debug("Setting dr_mode to 'host'\n");
197                 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
198                 disable_phy_pins = 0;
199         } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
200                 debug("Setting dr_mode to 'host'\n");
201                 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
202         } else {
203                 if (otg_mode && strcmp(otg_mode, "none") != 0)
204                         printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
205                                 otg_mode);
206                 disable_otg = 1;
207                 ret = 0;
208         }
209
210         if ((!disable_phy_pins && !disable_otg) || ret)
211                 goto out;
212
213         if (disable_otg) {
214                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
215                 if (ret)
216                         goto out;
217         }
218
219         ph = fdt_getprop(blob, off, phy, NULL);
220         if (ph == NULL) {
221                 printf("Failed to find '%s' phandle in node '%s'\n", phy,
222                         fdt_get_name(blob, off, NULL));
223                 goto out;
224         }
225
226         off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
227         if (off <= 0) {
228                 printf("Failed to find '%s' node via phandle %04x\n",
229                         phy, fdt32_to_cpu(*ph));
230                 goto out;
231         }
232
233         if (disable_otg) {
234                 debug("Disabling usbphy\n");
235                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
236         }
237 out:
238         if (ret)
239                 printf("Failed to update usbotg: %d\n", ret);
240         else
241                 debug("node '%s' updated\n", node);
242         karo_set_fdtsize(blob);
243 }
244
245 static int karo_fdt_flexcan_enabled(void *blob)
246 {
247         const char *can_ifs[] = {
248                 "can0",
249                 "can1",
250         };
251         size_t i;
252
253         for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
254                 const char *status;
255                 int off = fdt_path_offset(blob, can_ifs[i]);
256
257                 if (off < 0) {
258                         debug("node '%s' not found\n", can_ifs[i]);
259                         continue;
260                 }
261                 status = fdt_getprop(blob, off, "status", NULL);
262                 if (status && strcmp(status, "okay") == 0) {
263                         debug("%s is enabled\n", can_ifs[i]);
264                         return 1;
265                 }
266         }
267         debug("can driver is disabled\n");
268         return 0;
269 }
270
271 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
272 {
273         int off = fdt_path_offset(blob, name);
274         u32 ph;
275         const struct fdt_property *pc;
276         int len;
277
278         if (off < 0)
279                 return;
280
281         ph = fdt_create_phandle(blob, off);
282         if (!ph)
283                 return;
284
285         off = fdt_path_offset(blob, "display");
286         if (off < 0)
287                 return;
288
289         pc = fdt_get_property(blob, off, "pinctrl-0", &len);
290         if (!pc || len < sizeof(ph))
291                 return;
292
293         memcpy((void *)pc->data, &ph, sizeof(ph));
294         fdt_setprop_cell(blob, off, "pinctrl-0", ph);
295 }
296
297 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
298 {
299         const char *xcvr_status = "disabled";
300
301         if (xcvr_present) {
302                 if (karo_fdt_flexcan_enabled(blob)) {
303                         karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
304                         xcvr_status = "okay";
305                 } else {
306                         karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
307                 }
308         } else {
309                 const char *otg_mode = getenv("otg_mode");
310
311                 if (otg_mode && (strcmp(otg_mode, "host") == 0))
312                         karo_fdt_enable_node(blob, "can1", 0);
313
314                 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
315         }
316         fdt_find_and_setprop(blob, "/regulators/can-xcvr", "status",
317                         xcvr_status, strlen(xcvr_status) + 1, 1);
318 }
319
320 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
321                         const char *prop)
322 {
323         int ret;
324         int offset;
325         const uint32_t *phandle;
326         uint32_t ph = 0;
327
328         offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
329         if (offset <= 0)
330                 return;
331
332         phandle = fdt_getprop(blob, offset, prop, NULL);
333         if (phandle) {
334                 ph = fdt32_to_cpu(*phandle);
335         }
336
337         debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
338         ret = fdt_delprop(blob, offset, prop);
339         if (ret == 0)
340                 karo_set_fdtsize(blob);
341
342         if (!ph)
343                 return;
344
345         offset = fdt_node_offset_by_phandle(blob, ph);
346         if (offset <= 0)
347                 return;
348
349         debug("Removing node @ %08x\n", offset);
350         fdt_del_node(blob, offset);
351         karo_set_fdtsize(blob);
352 }
353
354 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
355 {
356         const uint32_t *prop;
357
358         memset(fb_mode, 0, sizeof(*fb_mode));
359
360         prop = fdt_getprop(blob, off, "clock-frequency", NULL);
361         if (prop)
362                 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
363
364         prop = fdt_getprop(blob, off, "hactive", NULL);
365         if (prop)
366                 fb_mode->xres = fdt32_to_cpu(*prop);
367
368         prop = fdt_getprop(blob, off, "vactive", NULL);
369         if (prop)
370                 fb_mode->yres = fdt32_to_cpu(*prop);
371
372         prop = fdt_getprop(blob, off, "hback-porch", NULL);
373         if (prop)
374                 fb_mode->left_margin = fdt32_to_cpu(*prop);
375
376         prop = fdt_getprop(blob, off, "hsync-len", NULL);
377         if (prop)
378                 fb_mode->hsync_len = fdt32_to_cpu(*prop);
379
380         prop = fdt_getprop(blob, off, "hfront-porch", NULL);
381         if (prop)
382                 fb_mode->right_margin = fdt32_to_cpu(*prop);
383
384         prop = fdt_getprop(blob, off, "vback-porch", NULL);
385         if (prop)
386                 fb_mode->upper_margin = fdt32_to_cpu(*prop);
387
388         prop = fdt_getprop(blob, off, "vsync-len", NULL);
389         if (prop)
390                 fb_mode->vsync_len = fdt32_to_cpu(*prop);
391
392         prop = fdt_getprop(blob, off, "vfront-porch", NULL);
393         if (prop)
394                 fb_mode->lower_margin = fdt32_to_cpu(*prop);
395
396         prop = fdt_getprop(blob, off, "hsync-active", NULL);
397         if (prop)
398                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
399
400         prop = fdt_getprop(blob, off, "vsync-active", NULL);
401         if (prop)
402                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
403
404         prop = fdt_getprop(blob, off, "de-active", NULL);
405         if (prop)
406                 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
407
408         prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
409         if (prop)
410                 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
411
412         return 0;
413 }
414
415 static int fdt_update_native_fb_mode(void *blob, int off)
416 {
417         int ret;
418         uint32_t ph;
419
420         ret = fdt_increase_size(blob, 64);
421         if (ret) {
422                 printf("Warning: Failed to increase FDT size: %d\n", ret);
423         }
424         debug("Creating phandle at offset %d\n", off);
425         ph = fdt_create_phandle(blob, off);
426         if (!ph) {
427                 printf("Failed to create phandle for video timing\n");
428                 return -ENOMEM;
429         }
430
431         debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
432                 off, ph);
433         off = fdt_parent_offset(blob, off);
434         if (off < 0)
435                 return off;
436         debug("parent offset=%06x\n", off);
437         ret = fdt_setprop_cell(blob, off, "native-mode", ph);
438         if (ret)
439                 printf("Failed to set property 'native-mode': %d\n", ret);
440         karo_set_fdtsize(blob);
441         return ret;
442 }
443
444 static int karo_fdt_find_video_timings(void *blob)
445 {
446         int off = fdt_path_offset(blob, "display");
447         const char *subnode = "display-timings";
448
449         if (off < 0) {
450                 debug("Could not find node 'display' in FDT: %d\n", off);
451                 return off;
452         }
453
454         off = fdt_subnode_offset(blob, off, subnode);
455         if (off < 0) {
456                 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
457         }
458         return off;
459 }
460
461 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
462 {
463         int off = karo_fdt_find_video_timings(blob);
464
465         if (off < 0)
466                 return off;
467         while (off > 0) {
468                 const char *n, *endp;
469                 int len, d = 1;
470
471                 off = fdt_next_node(blob, off, &d);
472                 if (off < 0)
473                         return off;
474                 if (d < 1)
475                         return -EINVAL;
476                 if (d > 2) {
477                         debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
478                         continue;
479                 }
480                 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
481
482                 n = fdt_getprop(blob, off, "panel-name", &len);
483                 if (!n) {
484                         n = fdt_get_name(blob, off, NULL);
485                         if (strcasecmp(n, name) == 0) {
486                                 break;
487                         }
488                 } else {
489                         int found = 0;
490
491                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
492                                 debug("Checking panel-name '%s'\n", n);
493                                 if (strcasecmp(n, name) == 0) {
494                                         debug("Using node %s @ %04x\n",
495                                                 fdt_get_name(blob, off, NULL), off);
496                                         found = 1;
497                                         break;
498                                 }
499                         }
500                         if (found)
501                                 break;
502                 }
503         }
504         if (off > 0) {
505                 return fdt_init_fb_mode(blob, off, fb_mode);
506         }
507         return -EINVAL;
508 }
509
510 #define SET_FB_PROP(n, v) ({                                            \
511         int ret;                                                        \
512         ret = fdt_setprop_u32(blob, off, n, v);                         \
513         if (ret) {                                                      \
514                 printf("Failed to set property %s: %d\n", name, ret);   \
515         }                                                               \
516         ret;                                                            \
517 })
518
519
520 int karo_fdt_create_fb_mode(void *blob, const char *name,
521                         struct fb_videomode *fb_mode)
522 {
523         int off = fdt_path_offset(blob, "display");
524         int ret;
525         const char *subnode = "display-timings";
526
527         if (off < 0) {
528                 printf("'display' node not found in FDT\n");
529                 return off;
530         }
531
532         ret = fdt_increase_size(blob, 512);
533         if (ret) {
534                 printf("Failed to increase FDT size: %d\n", ret);
535                 return ret;
536         }
537
538         ret = fdt_subnode_offset(blob, off, subnode);
539         if (ret < 0) {
540                 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
541                 ret = fdt_add_subnode(blob, off, subnode);
542                 if (ret < 0) {
543                         printf("Failed to add %s subnode: %d\n", subnode, ret);
544                         return ret;
545                 }
546         }
547
548         ret = fdt_add_subnode(blob, ret, name);
549         if (ret < 0) {
550                 printf("Failed to add %s subnode: %d\n", name, ret);
551                 return ret;
552         }
553         off = ret;
554
555         ret = SET_FB_PROP("clock-frequency",
556                         PICOS2KHZ(fb_mode->pixclock) * 1000);
557         if (ret)
558                 goto out;
559         ret = SET_FB_PROP("hactive", fb_mode->xres);
560         if (ret)
561                 goto out;
562         ret = SET_FB_PROP("vactive", fb_mode->yres);
563         if (ret)
564                 goto out;
565         ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
566         if (ret)
567                 goto out;
568         ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
569         if (ret)
570                 goto out;
571         ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
572         if (ret)
573                 goto out;
574         ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
575         if (ret)
576                 goto out;
577         ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
578         if (ret)
579                 goto out;
580         ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
581         if (ret)
582                 goto out;
583         ret = SET_FB_PROP("hsync-active",
584                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
585         if (ret)
586                 goto out;
587         ret = SET_FB_PROP("vsync-active",
588                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
589         if (ret)
590                 goto out;
591         ret = SET_FB_PROP("de-active",
592                         !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
593         if (ret)
594                 goto out;
595         ret = SET_FB_PROP("pixelclk-active",
596                         !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
597 out:
598         karo_set_fdtsize(blob);
599         return ret;
600 }
601
602 static int karo_fdt_set_display_alias(void *blob, const char *path,
603                                 const char *name)
604 {
605         int ret;
606         int off;
607         size_t size = strlen(path) + strlen(name) + 2;
608         char *display;
609
610         display = malloc(size);
611         if (display == NULL) {
612                 printf("%s: Failed to allocate buffer\n", __func__);
613                 return -ENOMEM;
614         }
615         sprintf(display, "%s/%s", path, name);
616         if (strlen(display) != size - 1)
617                 hang();
618         off = fdt_path_offset(blob, "/aliases");
619         if (off == FDT_ERR_BADMAGIC)
620                 return -EINVAL;
621         ret = fdt_resize(blob);
622         if (ret < 0) {
623                 printf("%s: Failed to resize FDT: %s\n",
624                         __func__, fdt_strerror(ret));
625         }
626         if (off < 0) {
627                 off = fdt_add_subnode(blob, 0, "aliases");
628                 if (off < 0) {
629                         printf("%s: Failed to create 'aliases' node: %s\n",
630                                 __func__, fdt_strerror(off));
631                         return off;
632                 }
633         }
634         ret = fdt_setprop_string(blob, off, "display", display);
635         debug("setprop_string(display='%s') returned %d\n", display, ret);
636         return ret;
637 }
638
639 const char *karo_fdt_set_display(const char *video_mode, const char *lcd_path,
640                                 const char *lvds_path)
641 {
642         const char *vmode = NULL;
643         int ret;
644         void *blob = working_fdt;
645
646         if (video_mode == NULL || strlen(video_mode) == 0)
647                 return NULL;
648
649         vmode = strchr(video_mode, ':');
650
651         if (lvds_path == NULL)
652                 return vmode ? vmode + 1 : video_mode;
653
654         if (lvds_path != NULL && vmode != NULL) {
655                 if (strncmp(video_mode, "LVDS:", 5) == 0 ||
656                         strncmp(video_mode, "LVDS0:", 6) == 0) {
657                         ret = karo_fdt_set_display_alias(blob, lvds_path,
658                                                         "lvds-channel@0");
659                 } else if (strncmp(video_mode, "LVDS1:", 6) == 0) {
660                         ret = karo_fdt_set_display_alias(blob, lvds_path,
661                                                         "lvds-channel@1");
662                 } else {
663                         debug("%s: Syntax error in video_mode\n", __func__);
664                         return vmode + 1;
665                 }
666                 video_mode = vmode + 1;
667         } else {
668                 int off;
669
670                 ret = karo_fdt_set_display_alias(blob, lcd_path,
671                                                 "display@di0");
672
673                 off = fdt_path_offset(blob, "lvds0");
674                 if (off >= 0) {
675                         ret = fdt_set_node_status(blob, off,
676                                                 FDT_STATUS_DISABLED, 0);
677                 }
678                 off = fdt_path_offset(blob, "lvds1");
679                 if (off >= 0) {
680                         ret = fdt_set_node_status(blob, off,
681                                                 FDT_STATUS_DISABLED, 0);
682                 }
683         }
684         if (ret) {
685                 printf("%s: failed to set 'display' alias: %s\n",
686                         __func__, fdt_strerror(ret));
687         }
688         return video_mode;
689 }
690
691 int karo_fdt_update_fb_mode(void *blob, const char *name)
692 {
693         int off = fdt_path_offset(blob, "display");
694         const char *subnode = "display-timings";
695
696         if (off < 0)
697                 return off;
698
699         if (name == NULL) {
700                 int ret;
701
702                 debug("Disabling node '%s' at %03x\n",
703                         fdt_get_name(blob, off, NULL), off);
704                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
705                 return ret;
706         }
707
708         off = fdt_subnode_offset(blob, off, subnode);
709         if (off < 0) {
710                 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
711                 return off;
712         }
713         while (off > 0) {
714                 const char *n, *endp;
715                 int len, d = 1;
716
717                 off = fdt_next_node(blob, off, &d);
718                 if (off < 0)
719                         return off;
720                 if (d < 1)
721                         return -EINVAL;
722                 if (d > 2) {
723                         debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
724                         continue;
725                 }
726                 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
727
728                 n = fdt_getprop(blob, off, "panel-name", &len);
729                 if (!n) {
730                         n = fdt_get_name(blob, off, NULL);
731                         if (strcasecmp(n, name) == 0) {
732                                 break;
733                         }
734                 } else {
735                         int found = 0;
736
737                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
738                                 debug("Checking panel-name '%s'\n", n);
739                                 if (strcasecmp(n, name) == 0) {
740                                         debug("Using node %s @ %04x\n",
741                                                 fdt_get_name(blob, off, NULL), off);
742                                         found = 1;
743                                         break;
744                                 }
745                         }
746                         if (found)
747                                 break;
748                 }
749         }
750         if (off > 0)
751                 return fdt_update_native_fb_mode(blob, off);
752         return off;
753 }
754
755 static int karo_load_part(const char *part, void *addr, size_t len)
756 {
757         int ret;
758         struct mtd_device *dev;
759         struct part_info *part_info;
760         u8 part_num;
761         size_t actual;
762
763         debug("Initializing mtd_parts\n");
764         ret = mtdparts_init();
765         if (ret)
766                 return ret;
767
768         debug("Trying to find NAND partition '%s'\n", part);
769         ret = find_dev_and_part(part, &dev, &part_num, &part_info);
770         if (ret) {
771                 printf("Failed to find flash partition '%s': %d\n",
772                         part, ret);
773
774                 return ret;
775         }
776         debug("Found partition '%s': offset=%08x size=%08x\n",
777                 part, part_info->offset, part_info->size);
778         if (part_info->size < len) {
779                 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
780                         part, len, part_info->size);
781                 len = part_info->size;
782         }
783         debug("Reading NAND partition '%s' to %p\n", part, addr);
784         ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
785                                 &actual, len, addr);
786         if (ret) {
787                 printf("Failed to load partition '%s' to %p\n", part, addr);
788                 return ret;
789         }
790         if (actual < len)
791                 printf("Read only %u of %u bytes due to bad blocks\n",
792                         actual, len);
793
794         debug("Read %u byte from partition '%s' @ offset %08x\n",
795                 len, part, part_info->offset);
796         return 0;
797 }
798
799 void *karo_fdt_load_dtb(void)
800 {
801         int ret;
802         void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
803
804         if (tstc()) {
805                 debug("aborting DTB load\n");
806                 return NULL;
807         }
808
809         /* clear FDT header in memory */
810         memset(fdt, 0, 4);
811
812         ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
813         if (ret) {
814                 printf("Failed to load dtb from flash: %d\n", ret);
815                 return NULL;
816         }
817
818         if (fdt_check_header(fdt)) {
819                 debug("No valid DTB in flash\n");
820                 return NULL;
821         }
822         debug("Using DTB from flash\n");
823         karo_set_fdtsize(fdt);
824         return fdt;
825 }