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