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