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