2 * (C) Copyright 2012-2014 Lothar Waßmann <LW@KARO-electronics.de>
4 * See file CREDITS for list of people who contributed to this
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.
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.
21 #include <fdt_support.h>
24 #include <linux/list.h>
26 #include <jffs2/load_kernel.h>
31 #ifdef CONFIG_MAX_DTB_SIZE
32 #define MAX_DTB_SIZE CONFIG_MAX_DTB_SIZE
34 #define MAX_DTB_SIZE SZ_64K
37 DECLARE_GLOBAL_DATA_PTR;
39 static void karo_set_fdtsize(void *fdt)
41 size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
43 if (fdtsize == fdt_totalsize(fdt)) {
46 debug("FDT size changed from %u to %u\n",
47 fdtsize, fdt_totalsize(fdt));
48 setenv_hex("fdtsize", fdt_totalsize(fdt));
51 static void *karo_fdt_load_dtb(void)
56 if (getenv("fdtaddr") == NULL)
57 setenv_hex("fdtaddr", CONFIG_SYS_FDT_ADDR);
58 fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
61 printf("aborting DTB load\n");
65 /* clear FDT header in memory */
68 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
70 printf("Failed to load dtb from flash: %d\n", ret);
74 if (fdt_check_header(fdt)) {
75 debug("No valid DTB in flash\n");
78 debug("Using DTB from flash\n");
79 karo_set_fdtsize(fdt);
83 void karo_fdt_move_fdt(void)
86 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
89 debug("DTB already loaded\n");
96 fdt_addr = CONFIG_SYS_FDT_ADDR;
97 printf("fdtaddr is not set; using default: %08lx\n",
101 fdt = karo_fdt_load_dtb();
103 fdt = (void *)gd->fdt_blob;
105 #ifdef CONFIG_OF_EMBED
106 printf("Compiled in FDT not found");
108 printf("No FDT found");
110 printf("; creating empty DTB\n");
111 fdt = (void *)fdt_addr;
112 fdt_create_empty_tree(fdt, 256);
114 printf("No DTB in flash; using default DTB\n");
116 debug("Checking FDT header @ %p\n", fdt);
117 if (fdt_check_header(fdt)) {
118 printf("ERROR: No valid DTB found at %p\n", fdt);
121 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
122 fdt, fdt + fdt_totalsize(fdt) - 1,
123 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
124 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
126 set_working_fdt_addr(fdt_addr);
128 karo_set_fdtsize(fdt);
131 void karo_fdt_remove_node(void *blob, const char *node)
133 int off = fdt_path_offset(blob, node);
136 debug("Removing node '%s' from DT\n", node);
139 printf("Could not find node '%s': %s\n", node,
142 ret = fdt_del_node(blob, off);
144 printf("Failed to remove node '%s': %s\n",
145 node, fdt_strerror(ret));
147 karo_set_fdtsize(blob);
150 void karo_fdt_enable_node(void *blob, const char *node, int enable)
152 int off = fdt_path_offset(blob, node);
154 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
156 printf("Could not find node '%s': %s\n", node,
160 fdt_set_node_status(blob, off,
161 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
163 karo_set_fdtsize(blob);
166 static void fdt_disable_tp_node(void *blob, const char *name)
168 int offs = fdt_node_offset_by_compatible(blob, -1, name);
171 debug("Disabling node '%s'\n", name);
172 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
173 offs = fdt_node_offset_by_compatible(blob, offs, name);
177 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
181 const char *model = getenv("touchpanel");
183 for (i = 0; i < num_panels; i++) {
184 const char *tp = panels[i];
187 if (strcmp(model, tp) == 0)
190 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
192 tp = strpbrk(tp + 1, ",-");
197 fdt_disable_tp_node(blob, panels[i]);
199 karo_set_fdtsize(blob);
202 static int karo_fdt_disable_node_phandle(void *blob, const char *parent,
208 off = fdt_path_offset(blob, parent);
210 printf("Failed to find node '%s'\n", parent);
214 ph = fdt_getprop(blob, off, name, NULL);
216 debug("Failed to find '%s' phandle in node '%s'\n", name,
217 fdt_get_name(blob, off, NULL));
218 return -FDT_ERR_NOTFOUND;
221 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
223 printf("Failed to find '%s' node via phandle %04x\n",
224 name, fdt32_to_cpu(*ph));
225 return -FDT_ERR_NOTFOUND;
227 return fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
230 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy,
231 const char *phy_supply)
233 const char *otg_mode = getenv("otg_mode");
237 int disable_phy_pins = 0;
239 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
241 off = fdt_path_offset(blob, node);
243 debug("Failed to find node %s\n", node);
247 if (otg_mode && (strcasecmp(otg_mode, "device") == 0 ||
248 strcasecmp(otg_mode, "gadget") == 0)) {
249 debug("Setting dr_mode to 'peripheral'\n");
250 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
251 disable_phy_pins = 1;
252 } else if (otg_mode && strcasecmp(otg_mode, "host") == 0) {
253 debug("Setting dr_mode to 'host'\n");
254 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
255 } else if (otg_mode && strcasecmp(otg_mode, "otg") == 0) {
256 debug("Setting dr_mode to 'otg'\n");
257 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
259 if (otg_mode && strcasecmp(otg_mode, "none") != 0)
260 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
266 if ((!disable_phy_pins && !disable_otg) || ret)
269 ret = karo_fdt_disable_node_phandle(blob, node, phy_supply);
270 if (ret && ret == -FDT_ERR_NOTFOUND) {
273 ph = fdt_getprop(blob, off, phy, NULL);
275 printf("Failed to find '%s' phandle in node '%s'\n",
277 ret = -FDT_ERR_NOTFOUND;
280 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
282 printf("Failed to find '%s' node via phandle %04x\n",
283 phy, fdt32_to_cpu(*ph));
287 ph = fdt_getprop(blob, off, phy_supply, NULL);
289 debug("Failed to find '%s' phandle in node '%s'\n",
290 phy_supply, fdt_get_name(blob, off, NULL));
291 ret = -FDT_ERR_NOTFOUND;
294 ret = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
296 debug("Disabling node %s via phandle %s:%s\n",
297 fdt_get_name(blob, ret, NULL),
298 fdt_get_name(blob, off, NULL), phy_supply);
299 ret = fdt_set_node_status(blob, ret,
300 FDT_STATUS_DISABLED, 0);
303 if (ret && ret != -FDT_ERR_NOTFOUND)
308 debug("Disabling '%s'\n", fdt_get_name(blob, off, NULL));
309 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
311 ret = karo_fdt_disable_node_phandle(blob, node, phy);
312 } else if (disable_phy_pins) {
313 debug("Removing '%s' from node '%s'\n", phy_supply,
314 fdt_get_name(blob, off, NULL));
315 ret = fdt_delprop(blob, off, phy_supply);
319 if (ret && ret != -FDT_ERR_NOTFOUND)
320 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
322 debug("node '%s' updated\n", node);
323 karo_set_fdtsize(blob);
326 static inline int karo_fdt_flexcan_enabled(void *blob)
328 static const char *can_ifs[] = {
334 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
336 int off = fdt_path_offset(blob, can_ifs[i]);
339 debug("node '%s' not found\n", can_ifs[i]);
342 status = fdt_getprop(blob, off, "status", NULL);
343 if (status && strcmp(status, "okay") == 0) {
344 debug("%s is enabled\n", can_ifs[i]);
348 debug("can driver is disabled\n");
352 static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
354 int off = fdt_path_offset(blob, name);
356 const struct fdt_property *pc;
362 ph = fdt_create_phandle(blob, off);
366 off = fdt_path_offset(blob, "display");
370 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
371 if (!pc || len < sizeof(ph))
374 memcpy((void *)pc->data, &ph, sizeof(ph));
375 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
378 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
381 const char *xcvr_status = "disabled";
382 const char *otg_mode = getenv("otg_mode");
385 if (karo_fdt_flexcan_enabled(blob)) {
387 debug("Changing LCD to use 23bits only\n");
388 karo_fdt_set_lcd_pins(blob, "lcdif-23bit-pins-a");
389 /* handle legacy alias name */
390 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
393 } else if (!is_lvds()) {
394 debug("Changing LCD to use 24bits\n");
395 karo_fdt_set_lcd_pins(blob, "lcdif-24bit-pins-a");
396 /* handle legacy alias name */
397 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
400 int off = fdt_path_offset(blob, "can0");
403 fdt_delprop(blob, off, "xceiver-supply");
404 off = fdt_path_offset(blob, "can1");
406 fdt_delprop(blob, off, "xceiver-supply");
408 karo_fdt_set_lcd_pins(blob, "lcdif-24bit-pins-a");
409 /* handle legacy alias name */
410 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
414 if (otg_mode && strcasecmp(otg_mode, "host") == 0)
415 karo_fdt_enable_node(blob, "can1", 0);
418 debug("Disabling CAN XCVR\n");
419 ret = fdt_find_and_setprop(blob, "reg-can-xcvr", "status",
420 xcvr_status, strlen(xcvr_status) + 1, 1);
421 if (ret == -FDT_ERR_NOTFOUND || ret == -FDT_ERR_BADPATH)
422 ret = fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
423 xcvr_status, strlen(xcvr_status) + 1, 1);
424 if (ret && ret != -FDT_ERR_NOTFOUND && ret != -FDT_ERR_BADPATH)
425 printf("Failed to disable CAN transceiver switch: %s\n",
430 void karo_fdt_del_prop(void *blob, const char *compat, u32 offs,
431 const char *propname)
434 const fdt32_t *reg = NULL;
437 offset = fdt_node_offset_by_compatible(blob, offset, compat);
441 reg = fdt_getprop(blob, offset, "reg", NULL);
445 if (fdt32_to_cpu(*reg) == offs)
448 debug("Removing property '%s' from node %s@%x\n",
449 propname, compat, offs);
450 fdt_delprop(blob, offset, propname);
452 karo_set_fdtsize(blob);
455 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
457 const uint32_t *prop;
459 memset(fb_mode, 0, sizeof(*fb_mode));
461 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
463 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
465 prop = fdt_getprop(blob, off, "hactive", NULL);
467 fb_mode->xres = fdt32_to_cpu(*prop);
469 prop = fdt_getprop(blob, off, "vactive", NULL);
471 fb_mode->yres = fdt32_to_cpu(*prop);
473 prop = fdt_getprop(blob, off, "hback-porch", NULL);
475 fb_mode->left_margin = fdt32_to_cpu(*prop);
477 prop = fdt_getprop(blob, off, "hsync-len", NULL);
479 fb_mode->hsync_len = fdt32_to_cpu(*prop);
481 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
483 fb_mode->right_margin = fdt32_to_cpu(*prop);
485 prop = fdt_getprop(blob, off, "vback-porch", NULL);
487 fb_mode->upper_margin = fdt32_to_cpu(*prop);
489 prop = fdt_getprop(blob, off, "vsync-len", NULL);
491 fb_mode->vsync_len = fdt32_to_cpu(*prop);
493 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
495 fb_mode->lower_margin = fdt32_to_cpu(*prop);
497 prop = fdt_getprop(blob, off, "hsync-active", NULL);
499 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
501 prop = fdt_getprop(blob, off, "vsync-active", NULL);
503 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
505 prop = fdt_getprop(blob, off, "de-active", NULL);
507 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
509 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
511 fb_mode->sync |= *prop ? FB_SYNC_CLK_LAT_FALL : 0;
516 static int fdt_update_native_fb_mode(void *blob, int off)
521 ret = fdt_increase_size(blob, 64);
523 printf("Warning: Failed to increase FDT size: %s\n",
526 debug("Creating phandle at offset %d\n", off);
527 ph = fdt_create_phandle(blob, off);
529 printf("Failed to create phandle for video timing\n");
533 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
535 off = fdt_parent_offset(blob, off);
538 debug("parent offset=%06x\n", off);
539 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
541 printf("Failed to set property 'native-mode': %s\n",
543 karo_set_fdtsize(blob);
547 static int karo_fdt_find_video_timings(void *blob)
549 int off = fdt_path_offset(blob, "display");
550 const char *subnode = "display-timings";
553 debug("Could not find node 'display' in FDT: %s\n",
558 off = fdt_subnode_offset(blob, off, subnode);
560 debug("Could not find node '%s' in FDT: %s\n", subnode,
566 static int karo_fdt_check_panel_name(const char *pn, const char *name, int len)
568 const char *endp = pn + len;
573 if (strcasecmp(pn, name) == 0)
575 pn += strlen(pn) + 1;
580 static int karo_fdt_find_panel(const void *blob, int off, const char *name)
582 debug("Searching panel '%s'\n", name);
588 off = fdt_next_node(blob, off, &d);
594 debug("Skipping node @ %04x %s depth %d\n", off,
595 fdt_get_name(blob, off, NULL), d);
599 pn = fdt_get_name(blob, off, NULL);
600 debug("Checking node name '%s'\n", pn);
601 if (strcasecmp(pn, name) == 0)
603 pn = fdt_getprop(blob, off, "u-boot,panel-name", &len);
606 if (karo_fdt_check_panel_name(pn, name, len))
610 debug("Found LCD panel: '%s' @ off %03x\n",
611 fdt_get_name(blob, off, NULL), off);
615 int karo_fdt_get_fb_mode(void *blob, const char *name,
616 struct fb_videomode *fb_mode)
618 int off = karo_fdt_find_video_timings(blob);
622 off = karo_fdt_find_panel(blob, off, name);
624 return fdt_init_fb_mode(blob, off, fb_mode);
629 #define SET_FB_PROP(n, v) ({ \
631 ret = fdt_setprop_u32(blob, off, n, v); \
633 printf("Failed to set property %s: %s\n", name, \
634 fdt_strerror(ret)); \
640 int karo_fdt_create_fb_mode(void *blob, const char *name,
641 struct fb_videomode *fb_mode)
643 int off = fdt_path_offset(blob, "display");
645 const char *subnode = "display-timings";
648 printf("'display' node not found in FDT\n");
652 ret = fdt_increase_size(blob, 512);
654 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
658 ret = fdt_subnode_offset(blob, off, subnode);
660 debug("Could not find node '%s' in FDT: %s\n", subnode,
662 ret = fdt_add_subnode(blob, off, subnode);
664 printf("Failed to add %s subnode: %s\n", subnode,
670 ret = fdt_add_subnode(blob, ret, name);
672 printf("Failed to add %s subnode: %s\n", name,
678 ret = SET_FB_PROP("clock-frequency",
679 PICOS2KHZ(fb_mode->pixclock) * 1000);
682 ret = SET_FB_PROP("hactive", fb_mode->xres);
685 ret = SET_FB_PROP("vactive", fb_mode->yres);
688 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
691 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
694 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
697 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
700 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
703 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
706 ret = SET_FB_PROP("hsync-active",
707 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
710 ret = SET_FB_PROP("vsync-active",
711 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
714 ret = SET_FB_PROP("de-active",
715 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
718 ret = SET_FB_PROP("pixelclk-active",
719 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
721 karo_set_fdtsize(blob);
725 int karo_fdt_update_fb_mode(void *blob, const char *name,
726 const char *panel_name)
729 int off = fdt_path_offset(blob, "display");
731 const char *subnode = "display-timings";
734 panel_off = fdt_path_offset(blob, panel_name);
736 if (off < 0 && panel_off < 0)
740 debug("Disabling node '%s' at %03x\n",
741 fdt_get_name(blob, off, NULL), off);
742 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
744 printf("Failed to disable node '%s': %s\n",
745 fdt_get_name(blob, off, NULL),
750 panel_off = fdt_path_offset(blob, panel_name);
753 return fdt_set_node_status(blob, panel_off,
754 FDT_STATUS_DISABLED, 0);
757 off = fdt_subnode_offset(blob, off, subnode);
759 debug("Could not find node '%s' in FDT: %s\n", subnode,
763 off = karo_fdt_find_panel(blob, off, name);
765 ret = fdt_update_native_fb_mode(blob, off);
771 off = fdt_path_offset(blob, "display/display-timings");
772 off = karo_fdt_find_panel(blob, off, name);
773 panel_off = fdt_path_offset(blob, panel_name);
777 name = fdt_getprop(blob, off, "u-boot,panel-name",
784 debug("%s@%d: Updating 'compatible' property of '%s' from '%s' to '%s'\n",
785 __func__, __LINE__, fdt_get_name(blob, panel_off, NULL),
786 (char *)fdt_getprop(blob, panel_off, "compatible", NULL),
789 ret = fdt_setprop_string(blob, panel_off, "compatible",
792 printf("Failed to set 'compatible' property of node '%s': %s\n",
793 fdt_get_name(blob, panel_off, NULL),
800 #ifdef CONFIG_SYS_LVDS_IF
801 int karo_fdt_get_lcd_bus_width(const void *blob, int default_width)
803 int off = fdt_path_offset(blob, "display");
806 const uint32_t *prop;
808 prop = fdt_getprop(blob, off, "fsl,data-width", NULL);
810 return fdt32_to_cpu(*prop);
812 return default_width;
815 int karo_fdt_get_lvds_mapping(const void *blob, int default_mapping)
817 int off = fdt_path_offset(blob, "display");
822 prop = fdt_getprop(blob, off, "fsl,data-mapping", NULL);
824 return strcmp(prop, "jeida") == 0;
826 return default_mapping;
829 u8 karo_fdt_get_lvds_channels(const void *blob)
831 static const char *lvds_chans[] = {
835 u8 lvds_chan_mask = 0;
838 for (i = 0; i < ARRAY_SIZE(lvds_chans); i++) {
840 int off = fdt_path_offset(blob, lvds_chans[i]);
845 status = fdt_getprop(blob, off, "status", NULL);
846 if (status && strcmp(status, "okay") == 0) {
847 debug("%s is enabled\n", lvds_chans[i]);
848 lvds_chan_mask |= 1 << i;
851 return lvds_chan_mask;
855 int karo_fdt_get_backlight_polarity(const void *blob)
857 #ifdef CONFIG_SYS_LVDS_IF
858 const char *backlight_node = "/backlight0";
860 const char *backlight_node = "/backlight";
862 int off = fdt_path_offset(blob, "backlight"); /* first try alias */
863 const struct fdt_property *prop;
868 * if no 'backlight' alias exists try finding '/backlight0'
869 * or '/backlight' depending on LVDS or not
871 off = fdt_path_offset(blob, backlight_node);
873 printf("%s node not found in DT\n", backlight_node);
878 prop = fdt_get_property(blob, off, "pwms", &len);
880 printf("'pwms' property not found\n");
882 debug("'pwms' property has len %d\n", len);
885 if (prop && len > 3) {
886 const u32 *data = (const u32 *)prop->data;
887 return fdt32_to_cpu(data[3]) == 0;