2 * (C) Copyright 2012-2016 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>
23 #ifdef CONFIG_VIDEO_IPUV3
27 #include <linux/list.h>
29 #include <jffs2/load_kernel.h>
34 #ifdef CONFIG_MAX_DTB_SIZE
35 #define MAX_DTB_SIZE CONFIG_MAX_DTB_SIZE
37 #define MAX_DTB_SIZE SZ_64K
40 DECLARE_GLOBAL_DATA_PTR;
42 static void karo_set_fdtsize(void *fdt)
44 size_t fdtsize = getenv_hex("fdtsize", 0);
46 if (fdtsize == fdt_totalsize(fdt)) {
49 debug("FDT size changed from %zu to %u\n",
50 fdtsize, fdt_totalsize(fdt));
51 setenv_hex("fdtsize", fdt_totalsize(fdt));
54 static void *karo_fdt_load_dtb(void)
59 if (getenv("fdtaddr") == NULL)
60 setenv_hex("fdtaddr", CONFIG_SYS_FDT_ADDR);
61 fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
64 printf("aborting DTB load\n");
68 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
70 printf("Failed to load dtb from flash: %d\n", ret);
71 memset(fdt, 0, MAX_DTB_SIZE);
75 if (fdt_check_header(fdt)) {
76 debug("No valid DTB in flash\n");
79 debug("Using DTB from flash\n");
80 karo_set_fdtsize(fdt);
84 static inline void karo_create_default_dtb(void *fdt)
88 fdt_create_empty_tree(fdt, 256);
89 offs = fdt_add_subnode(fdt, 0, "display");
90 offs = fdt_add_subnode(fdt, offs, "display-timings");
91 offs = fdt_add_subnode(fdt, 0, "aliases");
92 fdt_setprop(fdt, offs, "display", "/display", strlen("/display"));
95 void karo_fdt_move_fdt(void)
98 unsigned long fdt_addr = getenv_hex("fdtaddr", 0);
101 debug("DTB already loaded\n");
105 setenv("fdtsize", NULL);
108 fdt_addr = CONFIG_SYS_FDT_ADDR;
109 printf("fdtaddr is not set; using default: %08lx\n",
113 fdt = karo_fdt_load_dtb();
115 fdt = (void *)gd->fdt_blob;
117 #ifdef CONFIG_OF_EMBED
118 printf("Compiled in FDT not found");
120 printf("No FDT found");
122 printf("; creating empty DTB\n");
123 fdt = (void *)fdt_addr;
124 karo_create_default_dtb(fdt);
126 printf("No DTB in flash; using default DTB\n");
128 debug("Checking FDT header @ %p\n", fdt);
129 if (fdt_check_header(fdt)) {
130 printf("ERROR: No valid DTB found at %p\n", fdt);
133 debug("Copying FDT from %p..%p to %08lx..%08lx\n",
134 fdt, fdt + fdt_totalsize(fdt) - 1,
135 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
136 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
138 set_working_fdt_addr(fdt_addr);
139 karo_set_fdtsize(fdt);
142 void karo_fdt_remove_node(void *blob, const char *node)
144 int off = fdt_path_offset(blob, node);
147 debug("Removing node '%s' from DT\n", node);
150 printf("Could not find node '%s': %s\n", node,
153 ret = fdt_del_node(blob, off);
155 printf("Failed to remove node '%s': %s\n",
156 node, fdt_strerror(ret));
158 karo_set_fdtsize(blob);
161 void karo_fdt_enable_node(void *blob, const char *node, int enable)
163 int off = fdt_path_offset(blob, node);
165 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
167 printf("Could not find node '%s': %s\n", node,
171 fdt_set_node_status(blob, off,
172 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
174 karo_set_fdtsize(blob);
177 static void fdt_disable_tp_node(void *blob, const char *name)
179 int offs = fdt_node_offset_by_compatible(blob, -1, name);
182 debug("Disabling node '%s'\n", name);
183 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
184 offs = fdt_node_offset_by_compatible(blob, offs, name);
188 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
192 const char *model = getenv("touchpanel");
194 for (i = 0; i < num_panels; i++) {
195 const char *tp = panels[i];
198 if (strcmp(model, tp) == 0)
201 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
203 tp = strpbrk(tp + 1, ",-");
208 fdt_disable_tp_node(blob, panels[i]);
210 karo_set_fdtsize(blob);
213 static int karo_fdt_disable_node_phandle(void *blob, const char *parent,
219 off = fdt_path_offset(blob, parent);
221 printf("Failed to find node '%s'\n", parent);
225 ph = fdt_getprop(blob, off, name, NULL);
227 debug("Failed to find '%s' phandle in node '%s'\n", name,
228 fdt_get_name(blob, off, NULL));
229 return -FDT_ERR_NOTFOUND;
232 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
234 printf("Failed to find '%s' node via phandle %04x\n",
235 name, fdt32_to_cpu(*ph));
236 return -FDT_ERR_NOTFOUND;
238 return fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
241 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy,
242 const char *phy_supply)
244 const char *otg_mode = getenv("otg_mode");
248 int disable_phy_pins = 0;
250 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
252 off = fdt_path_offset(blob, node);
254 debug("Failed to find node %s\n", node);
258 if (otg_mode && (strcasecmp(otg_mode, "device") == 0 ||
259 strcasecmp(otg_mode, "gadget") == 0)) {
260 debug("Setting dr_mode to 'peripheral'\n");
261 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
262 disable_phy_pins = 1;
263 } else if (otg_mode && strcasecmp(otg_mode, "host") == 0) {
264 debug("Setting dr_mode to 'host'\n");
265 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
266 } else if (otg_mode && strcasecmp(otg_mode, "otg") == 0) {
267 debug("Setting dr_mode to 'otg'\n");
268 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
270 if (otg_mode && strcasecmp(otg_mode, "none") != 0)
271 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
277 if ((!disable_phy_pins && !disable_otg) || ret)
280 ret = karo_fdt_disable_node_phandle(blob, node, phy_supply);
281 if (ret && ret == -FDT_ERR_NOTFOUND) {
284 ph = fdt_getprop(blob, off, phy, NULL);
286 printf("Failed to find '%s' phandle in node '%s'\n",
288 ret = -FDT_ERR_NOTFOUND;
291 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
293 printf("Failed to find '%s' node via phandle %04x\n",
294 phy, fdt32_to_cpu(*ph));
298 ph = fdt_getprop(blob, off, phy_supply, NULL);
300 debug("Failed to find '%s' phandle in node '%s'\n",
301 phy_supply, fdt_get_name(blob, off, NULL));
302 ret = -FDT_ERR_NOTFOUND;
305 ret = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
307 debug("Disabling node %s via phandle %s:%s\n",
308 fdt_get_name(blob, ret, NULL),
309 fdt_get_name(blob, off, NULL), phy_supply);
310 ret = fdt_set_node_status(blob, ret,
311 FDT_STATUS_DISABLED, 0);
314 if (ret && ret != -FDT_ERR_NOTFOUND)
319 debug("Disabling '%s'\n", fdt_get_name(blob, off, NULL));
320 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
322 ret = karo_fdt_disable_node_phandle(blob, node, phy);
323 } else if (disable_phy_pins) {
324 debug("Removing '%s' from node '%s'\n", phy_supply,
325 fdt_get_name(blob, off, NULL));
326 ret = fdt_delprop(blob, off, phy_supply);
330 if (ret && ret != -FDT_ERR_NOTFOUND)
331 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
333 debug("node '%s' updated\n", node);
334 karo_set_fdtsize(blob);
337 static inline int karo_fdt_flexcan_enabled(void *blob)
339 static const char *can_ifs[] = {
345 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
347 int off = fdt_path_offset(blob, can_ifs[i]);
350 debug("node '%s' not found\n", can_ifs[i]);
353 status = fdt_getprop(blob, off, "status", NULL);
354 if (status && strcmp(status, "okay") == 0) {
355 debug("%s is enabled\n", can_ifs[i]);
359 debug("can driver is disabled\n");
363 static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
365 int off = fdt_path_offset(blob, name);
367 const struct fdt_property *pc;
373 ph = fdt_create_phandle(blob, off);
377 off = fdt_path_offset(blob, "display");
381 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
382 if (!pc || len < sizeof(ph))
385 memcpy((void *)pc->data, &ph, sizeof(ph));
386 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
389 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
392 const char *xcvr_status = "disabled";
393 const char *otg_mode = getenv("otg_mode");
396 if (karo_fdt_flexcan_enabled(blob)) {
398 debug("Changing LCD to use 23bits only\n");
399 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
402 } else if (!is_lvds()) {
403 debug("Changing LCD to use 24bits\n");
404 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
407 int off = fdt_path_offset(blob, "can0");
410 fdt_delprop(blob, off, "xceiver-supply");
411 off = fdt_path_offset(blob, "can1");
413 fdt_delprop(blob, off, "xceiver-supply");
415 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
418 if (otg_mode && strcasecmp(otg_mode, "host") == 0)
419 karo_fdt_enable_node(blob, "can1", 0);
422 debug("Disabling CAN XCVR\n");
423 ret = fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
424 xcvr_status, strlen(xcvr_status) + 1, 1);
426 printf("Failed to disable CAN transceiver switch: %s\n",
431 void karo_fdt_del_prop(void *blob, const char *compat, u32 offs,
432 const char *propname)
435 const fdt32_t *reg = NULL;
438 offset = fdt_node_offset_by_compatible(blob, offset, compat);
442 reg = fdt_getprop(blob, offset, "reg", NULL);
446 if (fdt32_to_cpu(*reg) == offs)
449 debug("Removing property '%s' from node %s@%x\n",
450 propname, compat, offs);
451 fdt_delprop(blob, offset, propname);
453 karo_set_fdtsize(blob);
457 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
459 const uint32_t *prop;
461 memset(fb_mode, 0, sizeof(*fb_mode));
463 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
465 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
467 prop = fdt_getprop(blob, off, "hactive", NULL);
469 fb_mode->xres = fdt32_to_cpu(*prop);
471 prop = fdt_getprop(blob, off, "vactive", NULL);
473 fb_mode->yres = fdt32_to_cpu(*prop);
475 prop = fdt_getprop(blob, off, "hback-porch", NULL);
477 fb_mode->left_margin = fdt32_to_cpu(*prop);
479 prop = fdt_getprop(blob, off, "hsync-len", NULL);
481 fb_mode->hsync_len = fdt32_to_cpu(*prop);
483 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
485 fb_mode->right_margin = fdt32_to_cpu(*prop);
487 prop = fdt_getprop(blob, off, "vback-porch", NULL);
489 fb_mode->upper_margin = fdt32_to_cpu(*prop);
491 prop = fdt_getprop(blob, off, "vsync-len", NULL);
493 fb_mode->vsync_len = fdt32_to_cpu(*prop);
495 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
497 fb_mode->lower_margin = fdt32_to_cpu(*prop);
499 prop = fdt_getprop(blob, off, "hsync-active", NULL);
501 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
503 prop = fdt_getprop(blob, off, "vsync-active", NULL);
505 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
507 prop = fdt_getprop(blob, off, "de-active", NULL);
509 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
511 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
513 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
518 static int fdt_update_native_fb_mode(void *blob, int off)
523 ret = fdt_increase_size(blob, 64);
525 printf("Warning: Failed to increase FDT size: %s\n",
528 debug("Creating phandle at offset %d\n", off);
529 ph = fdt_create_phandle(blob, off);
531 printf("Failed to create phandle for video timing\n");
535 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
537 off = fdt_parent_offset(blob, off);
540 debug("parent offset=%06x\n", off);
541 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
543 printf("Failed to set property 'native-mode': %s\n",
545 karo_set_fdtsize(blob);
549 static int karo_fdt_find_video_timings(const void *blob)
551 int off = fdt_path_offset(blob, "display");
552 const char *subnode = "display-timings";
555 debug("Could not find node 'display' in FDT: %s\n",
560 off = fdt_subnode_offset(blob, off, subnode);
562 debug("Could not find node '%s' in FDT: %s\n", subnode,
568 int karo_fdt_get_fb_mode(const void *blob, const char *name,
569 struct fb_videomode *fb_mode)
571 int off = karo_fdt_find_video_timings(blob);
576 const char *n, *endp;
579 off = fdt_next_node(blob, off, &d);
585 debug("Skipping node @ %04x %s depth %d\n", off,
586 fdt_get_name(blob, off, NULL), d);
590 n = fdt_getprop(blob, off, "panel-name", &len);
592 n = fdt_get_name(blob, off, NULL);
593 if (strcasecmp(n, name) == 0) {
599 for (endp = n + len; n < endp; n += strlen(n) + 1) {
600 debug("Checking panel-name '%s'\n", n);
601 if (strcasecmp(n, name) == 0) {
602 debug("Using node %s @ %04x\n",
603 fdt_get_name(blob, off, NULL), off);
613 return fdt_init_fb_mode(blob, off, fb_mode);
618 #define SET_FB_PROP(n, v) ({ \
620 ret = fdt_setprop_u32(blob, off, n, v); \
622 printf("Failed to set property %s: %s\n", name, \
623 fdt_strerror(ret)); \
628 int karo_fdt_create_fb_mode(void *blob, const char *name,
629 struct fb_videomode *fb_mode)
631 int off = fdt_path_offset(blob, "display");
633 const char *subnode = "display-timings";
636 printf("'display' node not found in FDT\n");
640 ret = fdt_increase_size(blob, 512);
642 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
646 ret = fdt_subnode_offset(blob, off, subnode);
648 debug("Could not find node '%s' in FDT: %s\n", subnode,
650 ret = fdt_add_subnode(blob, off, subnode);
652 printf("Failed to add %s subnode: %s\n", subnode,
658 ret = fdt_add_subnode(blob, ret, name);
660 printf("Failed to add %s subnode: %s\n", name,
666 ret = SET_FB_PROP("clock-frequency",
667 PICOS2KHZ(fb_mode->pixclock) * 1000);
670 ret = SET_FB_PROP("hactive", fb_mode->xres);
673 ret = SET_FB_PROP("vactive", fb_mode->yres);
676 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
679 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
682 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
685 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
688 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
691 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
694 ret = SET_FB_PROP("hsync-active",
695 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
698 ret = SET_FB_PROP("vsync-active",
699 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
702 ret = SET_FB_PROP("de-active",
703 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
706 ret = SET_FB_PROP("pixelclk-active",
707 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
709 karo_set_fdtsize(blob);
713 int karo_fdt_update_fb_mode(void *blob, const char *name)
715 int off = fdt_path_offset(blob, "display");
716 const char *subnode = "display-timings";
724 debug("Disabling node '%s' at %03x\n",
725 fdt_get_name(blob, off, NULL), off);
726 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
730 off = fdt_subnode_offset(blob, off, subnode);
732 debug("Could not find node '%s' in FDT: %s\n", subnode,
737 const char *n, *endp;
740 off = fdt_next_node(blob, off, &d);
746 debug("Skipping node @ %04x %s depth %d\n", off,
747 fdt_get_name(blob, off, NULL), d);
751 n = fdt_getprop(blob, off, "panel-name", &len);
753 n = fdt_get_name(blob, off, NULL);
754 if (strcasecmp(n, name) == 0) {
760 for (endp = n + len; n < endp; n += strlen(n) + 1) {
761 debug("Checking panel-name '%s'\n", n);
762 if (strcasecmp(n, name) == 0) {
763 debug("Using node %s @ %04x\n",
764 fdt_get_name(blob, off, NULL), off);
774 return fdt_update_native_fb_mode(blob, off);
778 #ifdef CONFIG_SYS_LVDS_IF
779 #ifdef CONFIG_SOC_MXC
780 #define LVDS_DATA_WIDTH_NAME "fsl,data-width"
781 #define LVDS_DATA_MAPPING_NAME "fsl,data-mapping"
783 #define LVDS_DATA_WIDTH_NAME "lvds-data-width"
784 #define LVDS_DATA_MAPPING_NAME "lvds-data-mapping"
787 int karo_fdt_get_lcd_bus_width(const void *blob, int default_width)
789 int off = fdt_path_offset(blob, "display");
792 const uint32_t *prop;
794 prop = fdt_getprop(blob, off, LVDS_DATA_WIDTH_NAME, NULL);
796 return fdt32_to_cpu(*prop);
798 return default_width;
801 int karo_fdt_get_lvds_mapping(const void *blob, int default_mapping)
803 int off = fdt_path_offset(blob, "display");
808 prop = fdt_getprop(blob, off, LVDS_DATA_MAPPING_NAME, NULL);
810 return strcmp(prop, "jeida") == 0;
812 return default_mapping;
815 u8 karo_fdt_get_lvds_channels(const void *blob)
817 static const char *lvds_chans[] = {
821 u8 lvds_chan_mask = 0;
824 for (i = 0; i < ARRAY_SIZE(lvds_chans); i++) {
826 int off = fdt_path_offset(blob, lvds_chans[i]);
831 status = fdt_getprop(blob, off, "status", NULL);
832 if (status && strcmp(status, "okay") == 0) {
833 debug("%s is enabled\n", lvds_chans[i]);
834 lvds_chan_mask |= 1 << i;
837 return lvds_chan_mask;
841 int karo_fdt_find_backlight_node(const void *blob)
843 int off = fdt_path_offset(blob, "backlight"); /* first try alias */
844 #ifdef CONFIG_SYS_LVDS_IF
845 const char *backlight_node = "/backlight0";
847 const char *backlight_node = "/backlight";
851 * if no 'backlight' alias exists try finding '/backlight0'
852 * or '/backlight' depending on LVDS or not
854 off = fdt_path_offset(blob, backlight_node);
856 printf("%s node not found in DT\n", backlight_node);
863 int karo_fdt_get_backlight_gpio(const void *blob, struct gpio_desc *desc)
865 int off = karo_fdt_find_backlight_node(blob);
866 const char *prop_name = "gpios";
867 const struct fdt_property *prop;
871 printf("Could not find 'backlight' node in FDT: %s\n",
876 prop = fdt_get_property(blob, off, prop_name, NULL);
879 prop = fdt_get_property(blob, off, prop_name, NULL);
881 debug("'gpios' or 'gpio' property not found in backlight node\n");
885 ret = gpio_request_by_name_nodev(blob, off, prop_name, 0, desc, 0);
887 printf("Failed to request backlight gpio %s/%s: %d\n",
888 fdt_get_name(blob, off, NULL), prop_name, ret);
893 int karo_fdt_get_backlight_polarity(const void *blob)
895 int off = karo_fdt_find_backlight_node(blob);
896 const struct fdt_property *prop;
898 const char *prop_name = "pwms";
901 printf("Could not find 'backlight' node in FDT: %s\n",
906 prop = fdt_get_property(blob, off, prop_name, &len);
909 prop = fdt_get_property(blob, off, prop_name, &len);
912 prop = fdt_get_property(blob, off, prop_name, &len);
915 printf("'pwms' or 'gpios' property not found in backlight node\n");
920 debug("'%s' property has len %d\n", prop_name, len);
923 if (prop && len > 3) {
924 const u32 *data = (const u32 *)prop->data;
925 return fdt32_to_cpu(data[3]) != 0;
929 #endif /* CONFIG_LCD */