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