]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/common/fdt.c
karo: fdt: move list of supported touchpanels out of common file
[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 (had_ctrlc()) {
101                 printf("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': %s\n", node,
180                         fdt_strerror(off));
181         } else {
182                 ret = fdt_del_node(blob, off);
183                 if (ret)
184                         printf("Failed to remove node '%s': %s\n",
185                                 node, fdt_strerror(ret));
186         }
187         karo_set_fdtsize(blob);
188 }
189
190 void karo_fdt_enable_node(void *blob, const char *node, int enable)
191 {
192         int off = fdt_path_offset(blob, node);
193
194         debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
195         if (off < 0) {
196                 printf("Could not find node '%s': %s\n", node,
197                         fdt_strerror(off));
198                 return;
199         }
200         fdt_set_node_status(blob, off,
201                         enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
202
203         karo_set_fdtsize(blob);
204 }
205
206 static void fdt_disable_tp_node(void *blob, const char *name)
207 {
208         int offs = fdt_node_offset_by_compatible(blob, -1, name);
209
210         if (offs < 0) {
211                 debug("node '%s' not found: %s\n", name, fdt_strerror(offs));
212                 return;
213         }
214
215         debug("Disabling node '%s'\n", name);
216         fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
217 }
218
219 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
220                         size_t num_panels)
221 {
222         int i;
223         const char *model = getenv("touchpanel");
224
225         for (i = 0; i < num_panels; i++) {
226                 const char *tp = panels[i];
227
228                 if (model != NULL) {
229                         if (strcmp(model, tp) == 0)
230                                 continue;
231                         while (tp != NULL) {
232                                 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
233                                         break;
234                                 tp = strpbrk(tp + 1, ",-");
235                         }
236                         if (tp != NULL)
237                                 continue;
238                 }
239                 fdt_disable_tp_node(blob, panels[i]);
240         }
241         karo_set_fdtsize(blob);
242 }
243
244 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
245 {
246         const char *otg_mode = getenv("otg_mode");
247         int off;
248         int ret;
249         const uint32_t *ph;
250         int disable_otg = 0;
251         int disable_phy_pins = 1;
252
253         debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
254
255         off = fdt_path_offset(blob, node);
256         if (off < 0) {
257                 debug("Failed to find node %s\n", node);
258                 return;
259         }
260
261         if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
262                                 strcmp(otg_mode, "gadget") == 0)) {
263                 debug("Setting dr_mode to 'peripheral'\n");
264                 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
265         } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
266                 debug("Setting dr_mode to 'host'\n");
267                 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
268                 disable_phy_pins = 0;
269         } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
270                 debug("Setting dr_mode to 'host'\n");
271                 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
272         } else {
273                 if (otg_mode && strcmp(otg_mode, "none") != 0)
274                         printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
275                                 otg_mode);
276                 disable_otg = 1;
277                 ret = 0;
278         }
279
280         if ((!disable_phy_pins && !disable_otg) || ret)
281                 goto out;
282
283         if (disable_otg) {
284                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
285                 if (ret)
286                         goto out;
287         }
288
289         ph = fdt_getprop(blob, off, phy, NULL);
290         if (ph == NULL) {
291                 printf("Failed to find '%s' phandle in node '%s'\n", phy,
292                         fdt_get_name(blob, off, NULL));
293                 goto out;
294         }
295
296         off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
297         if (off <= 0) {
298                 printf("Failed to find '%s' node via phandle %04x\n",
299                         phy, fdt32_to_cpu(*ph));
300                 goto out;
301         }
302
303         if (disable_otg) {
304                 debug("Disabling usbphy\n");
305                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
306         }
307 out:
308         if (ret)
309                 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
310         else
311                 debug("node '%s' updated\n", node);
312         karo_set_fdtsize(blob);
313 }
314
315 static int karo_fdt_flexcan_enabled(void *blob)
316 {
317         const char *can_ifs[] = {
318                 "can0",
319                 "can1",
320         };
321         size_t i;
322
323         for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
324                 const char *status;
325                 int off = fdt_path_offset(blob, can_ifs[i]);
326
327                 if (off < 0) {
328                         debug("node '%s' not found\n", can_ifs[i]);
329                         continue;
330                 }
331                 status = fdt_getprop(blob, off, "status", NULL);
332                 if (status && strcmp(status, "okay") == 0) {
333                         debug("%s is enabled\n", can_ifs[i]);
334                         return 1;
335                 }
336         }
337         debug("can driver is disabled\n");
338         return 0;
339 }
340
341 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
342 {
343         int off = fdt_path_offset(blob, name);
344         u32 ph;
345         const struct fdt_property *pc;
346         int len;
347
348         if (off < 0)
349                 return;
350
351         ph = fdt_create_phandle(blob, off);
352         if (!ph)
353                 return;
354
355         off = fdt_path_offset(blob, "display");
356         if (off < 0)
357                 return;
358
359         pc = fdt_get_property(blob, off, "pinctrl-0", &len);
360         if (!pc || len < sizeof(ph))
361                 return;
362
363         memcpy((void *)pc->data, &ph, sizeof(ph));
364         fdt_setprop_cell(blob, off, "pinctrl-0", ph);
365 }
366
367 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
368 {
369         const char *xcvr_status = "disabled";
370
371         if (xcvr_present) {
372                 if (karo_fdt_flexcan_enabled(blob)) {
373                         karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
374                         xcvr_status = "okay";
375                 } else {
376                         karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
377                 }
378         } else {
379                 const char *otg_mode = getenv("otg_mode");
380
381                 if (otg_mode && (strcmp(otg_mode, "host") == 0))
382                         karo_fdt_enable_node(blob, "can1", 0);
383
384                 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
385         }
386         fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
387                         xcvr_status, strlen(xcvr_status) + 1, 1);
388 }
389
390 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
391                         const char *prop)
392 {
393         int ret;
394         int offset;
395         const uint32_t *phandle;
396         uint32_t ph = 0;
397
398         offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
399         if (offset <= 0)
400                 return;
401
402         phandle = fdt_getprop(blob, offset, prop, NULL);
403         if (phandle) {
404                 ph = fdt32_to_cpu(*phandle);
405         }
406
407         debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
408         ret = fdt_delprop(blob, offset, prop);
409         if (ret == 0)
410                 karo_set_fdtsize(blob);
411
412         if (!ph)
413                 return;
414
415         offset = fdt_node_offset_by_phandle(blob, ph);
416         if (offset <= 0)
417                 return;
418
419         debug("Removing node @ %08x\n", offset);
420         fdt_del_node(blob, offset);
421         karo_set_fdtsize(blob);
422 }
423
424 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
425 {
426         const uint32_t *prop;
427
428         memset(fb_mode, 0, sizeof(*fb_mode));
429
430         prop = fdt_getprop(blob, off, "clock-frequency", NULL);
431         if (prop)
432                 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
433
434         prop = fdt_getprop(blob, off, "hactive", NULL);
435         if (prop)
436                 fb_mode->xres = fdt32_to_cpu(*prop);
437
438         prop = fdt_getprop(blob, off, "vactive", NULL);
439         if (prop)
440                 fb_mode->yres = fdt32_to_cpu(*prop);
441
442         prop = fdt_getprop(blob, off, "hback-porch", NULL);
443         if (prop)
444                 fb_mode->left_margin = fdt32_to_cpu(*prop);
445
446         prop = fdt_getprop(blob, off, "hsync-len", NULL);
447         if (prop)
448                 fb_mode->hsync_len = fdt32_to_cpu(*prop);
449
450         prop = fdt_getprop(blob, off, "hfront-porch", NULL);
451         if (prop)
452                 fb_mode->right_margin = fdt32_to_cpu(*prop);
453
454         prop = fdt_getprop(blob, off, "vback-porch", NULL);
455         if (prop)
456                 fb_mode->upper_margin = fdt32_to_cpu(*prop);
457
458         prop = fdt_getprop(blob, off, "vsync-len", NULL);
459         if (prop)
460                 fb_mode->vsync_len = fdt32_to_cpu(*prop);
461
462         prop = fdt_getprop(blob, off, "vfront-porch", NULL);
463         if (prop)
464                 fb_mode->lower_margin = fdt32_to_cpu(*prop);
465
466         prop = fdt_getprop(blob, off, "hsync-active", NULL);
467         if (prop)
468                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
469
470         prop = fdt_getprop(blob, off, "vsync-active", NULL);
471         if (prop)
472                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
473
474         prop = fdt_getprop(blob, off, "de-active", NULL);
475         if (prop)
476                 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
477
478         prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
479         if (prop)
480                 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
481
482         return 0;
483 }
484
485 static int fdt_update_native_fb_mode(void *blob, int off)
486 {
487         int ret;
488         uint32_t ph;
489
490         ret = fdt_increase_size(blob, 64);
491         if (ret) {
492                 printf("Warning: Failed to increase FDT size: %s\n",
493                         fdt_strerror(ret));
494         }
495         debug("Creating phandle at offset %d\n", off);
496         ph = fdt_create_phandle(blob, off);
497         if (!ph) {
498                 printf("Failed to create phandle for video timing\n");
499                 return -ENOMEM;
500         }
501
502         debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
503                 off, ph);
504         off = fdt_parent_offset(blob, off);
505         if (off < 0)
506                 return off;
507         debug("parent offset=%06x\n", off);
508         ret = fdt_setprop_cell(blob, off, "native-mode", ph);
509         if (ret)
510                 printf("Failed to set property 'native-mode': %s\n",
511                         fdt_strerror(ret));
512         karo_set_fdtsize(blob);
513         return ret;
514 }
515
516 static int karo_fdt_find_video_timings(void *blob)
517 {
518         int off = fdt_path_offset(blob, "display");
519         const char *subnode = "display-timings";
520
521         if (off < 0) {
522                 debug("Could not find node 'display' in FDT: %s\n",
523                         fdt_strerror(off));
524                 return off;
525         }
526
527         off = fdt_subnode_offset(blob, off, subnode);
528         if (off < 0) {
529                 debug("Could not find node '%s' in FDT: %s\n", subnode,
530                         fdt_strerror(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,
552                                 fdt_get_name(blob, off, NULL), d);
553                         continue;
554                 }
555                 debug("parsing subnode @ %04x %s depth %d\n", off,
556                         fdt_get_name(blob, off, NULL), d);
557
558                 n = fdt_getprop(blob, off, "panel-name", &len);
559                 if (!n) {
560                         n = fdt_get_name(blob, off, NULL);
561                         if (strcasecmp(n, name) == 0) {
562                                 break;
563                         }
564                 } else {
565                         int found = 0;
566
567                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
568                                 debug("Checking panel-name '%s'\n", n);
569                                 if (strcasecmp(n, name) == 0) {
570                                         debug("Using node %s @ %04x\n",
571                                                 fdt_get_name(blob, off, NULL), off);
572                                         found = 1;
573                                         break;
574                                 }
575                         }
576                         if (found)
577                                 break;
578                 }
579         }
580         if (off > 0) {
581                 return fdt_init_fb_mode(blob, off, fb_mode);
582         }
583         return -EINVAL;
584 }
585
586 #define SET_FB_PROP(n, v) ({                                            \
587         int ret;                                                        \
588         ret = fdt_setprop_u32(blob, off, n, v);                         \
589         if (ret) {                                                      \
590                 printf("Failed to set property %s: %s\n", name,         \
591                         fdt_strerror(ret));                             \
592         }                                                               \
593         ret;                                                            \
594 })
595
596
597 int karo_fdt_create_fb_mode(void *blob, const char *name,
598                         struct fb_videomode *fb_mode)
599 {
600         int off = fdt_path_offset(blob, "display");
601         int ret;
602         const char *subnode = "display-timings";
603
604         if (off < 0) {
605                 printf("'display' node not found in FDT\n");
606                 return off;
607         }
608
609         ret = fdt_increase_size(blob, 512);
610         if (ret) {
611                 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
612                 return ret;
613         }
614
615         ret = fdt_subnode_offset(blob, off, subnode);
616         if (ret < 0) {
617                 debug("Could not find node '%s' in FDT: %s\n", subnode,
618                         fdt_strerror(ret));
619                 ret = fdt_add_subnode(blob, off, subnode);
620                 if (ret < 0) {
621                         printf("Failed to add %s subnode: %s\n", subnode,
622                                 fdt_strerror(ret));
623                         return ret;
624                 }
625         }
626
627         ret = fdt_add_subnode(blob, ret, name);
628         if (ret < 0) {
629                 printf("Failed to add %s subnode: %s\n", name,
630                         fdt_strerror(ret));
631                 return ret;
632         }
633         off = ret;
634
635         ret = SET_FB_PROP("clock-frequency",
636                         PICOS2KHZ(fb_mode->pixclock) * 1000);
637         if (ret)
638                 goto out;
639         ret = SET_FB_PROP("hactive", fb_mode->xres);
640         if (ret)
641                 goto out;
642         ret = SET_FB_PROP("vactive", fb_mode->yres);
643         if (ret)
644                 goto out;
645         ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
646         if (ret)
647                 goto out;
648         ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
649         if (ret)
650                 goto out;
651         ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
652         if (ret)
653                 goto out;
654         ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
655         if (ret)
656                 goto out;
657         ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
658         if (ret)
659                 goto out;
660         ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
661         if (ret)
662                 goto out;
663         ret = SET_FB_PROP("hsync-active",
664                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
665         if (ret)
666                 goto out;
667         ret = SET_FB_PROP("vsync-active",
668                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
669         if (ret)
670                 goto out;
671         ret = SET_FB_PROP("de-active",
672                         !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
673         if (ret)
674                 goto out;
675         ret = SET_FB_PROP("pixelclk-active",
676                         !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
677 out:
678         karo_set_fdtsize(blob);
679         return ret;
680 }
681
682 static int karo_fdt_set_display_alias(void *blob, const char *path,
683                                 const char *name)
684 {
685         int ret;
686         int off;
687         size_t size = strlen(path) + strlen(name) + 2;
688         char *display;
689
690         display = malloc(size);
691         if (display == NULL) {
692                 printf("%s: Failed to allocate buffer\n", __func__);
693                 return -ENOMEM;
694         }
695         sprintf(display, "%s/%s", path, name);
696         if (strlen(display) != size - 1)
697                 hang();
698         off = fdt_path_offset(blob, "/aliases");
699         if (off == FDT_ERR_BADMAGIC)
700                 return -EINVAL;
701         ret = fdt_resize(blob);
702         if (ret < 0) {
703                 printf("%s: Failed to resize FDT: %s\n",
704                         __func__, fdt_strerror(ret));
705         }
706         if (off < 0) {
707                 off = fdt_add_subnode(blob, 0, "aliases");
708                 if (off < 0) {
709                         printf("%s: Failed to create 'aliases' node: %s\n",
710                                 __func__, fdt_strerror(off));
711                         return off;
712                 }
713         }
714         ret = fdt_setprop_string(blob, off, "display", display);
715         debug("setprop_string(display='%s') returned %d (%s)\n", display, ret,
716                 ret < 0 ? fdt_strerror(ret) : "OK");
717         return ret;
718 }
719
720 const char *karo_fdt_set_display(const char *video_mode, const char *lcd_path,
721                                 const char *lvds_path)
722 {
723         const char *vmode = NULL;
724         int ret;
725         void *blob = working_fdt;
726
727         if (video_mode == NULL || strlen(video_mode) == 0)
728                 return NULL;
729
730         vmode = strchr(video_mode, ':');
731
732         if (lvds_path == NULL)
733                 return vmode ? vmode + 1 : video_mode;
734
735         if (lvds_path != NULL && vmode != NULL) {
736                 if (strncmp(video_mode, "LVDS:", 5) == 0 ||
737                         strncmp(video_mode, "LVDS0:", 6) == 0) {
738                         ret = karo_fdt_set_display_alias(blob, lvds_path,
739                                                         "lvds-channel@0");
740                 } else if (strncmp(video_mode, "LVDS1:", 6) == 0) {
741                         ret = karo_fdt_set_display_alias(blob, lvds_path,
742                                                         "lvds-channel@1");
743                 } else {
744                         debug("%s: Syntax error in video_mode\n", __func__);
745                         return vmode + 1;
746                 }
747                 video_mode = vmode + 1;
748         } else {
749                 int off;
750
751                 ret = karo_fdt_set_display_alias(blob, lcd_path,
752                                                 "display@di0");
753
754                 off = fdt_path_offset(blob, "lvds0");
755                 if (off >= 0) {
756                         ret = fdt_set_node_status(blob, off,
757                                                 FDT_STATUS_DISABLED, 0);
758                 }
759                 off = fdt_path_offset(blob, "lvds1");
760                 if (off >= 0) {
761                         ret = fdt_set_node_status(blob, off,
762                                                 FDT_STATUS_DISABLED, 0);
763                 }
764         }
765         if (ret) {
766                 printf("%s: failed to set 'display' alias: %s\n",
767                         __func__, fdt_strerror(ret));
768         }
769         return video_mode;
770 }
771
772 int karo_fdt_update_fb_mode(void *blob, const char *name)
773 {
774         int off = fdt_path_offset(blob, "display");
775         const char *subnode = "display-timings";
776
777         if (off < 0)
778                 return off;
779
780         if (name == NULL) {
781                 int ret;
782
783                 debug("Disabling node '%s' at %03x\n",
784                         fdt_get_name(blob, off, NULL), off);
785                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
786                 return ret;
787         }
788
789         off = fdt_subnode_offset(blob, off, subnode);
790         if (off < 0) {
791                 debug("Could not find node '%s' in FDT: %s\n", subnode,
792                         fdt_strerror(off));
793                 return off;
794         }
795         while (off > 0) {
796                 const char *n, *endp;
797                 int len, d = 1;
798
799                 off = fdt_next_node(blob, off, &d);
800                 if (off < 0)
801                         return off;
802                 if (d < 1)
803                         return -EINVAL;
804                 if (d > 2) {
805                         debug("Skipping node @ %04x %s depth %d\n", off,
806                                 fdt_get_name(blob, off, NULL), d);
807                         continue;
808                 }
809                 debug("parsing subnode @ %04x %s depth %d\n", off,
810                         fdt_get_name(blob, off, NULL), d);
811
812                 n = fdt_getprop(blob, off, "panel-name", &len);
813                 if (!n) {
814                         n = fdt_get_name(blob, off, NULL);
815                         if (strcasecmp(n, name) == 0) {
816                                 break;
817                         }
818                 } else {
819                         int found = 0;
820
821                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
822                                 debug("Checking panel-name '%s'\n", n);
823                                 if (strcasecmp(n, name) == 0) {
824                                         debug("Using node %s @ %04x\n",
825                                                 fdt_get_name(blob, off, NULL), off);
826                                         found = 1;
827                                         break;
828                                 }
829                         }
830                         if (found)
831                                 break;
832                 }
833         }
834         if (off > 0)
835                 return fdt_update_native_fb_mode(blob, off);
836         return off;
837 }