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