2 * (C) Copyright 2012,2013 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)
54 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
57 printf("aborting DTB load\n");
61 /* clear FDT header in memory */
64 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
66 printf("Failed to load dtb from flash: %d\n", ret);
70 if (fdt_check_header(fdt)) {
71 debug("No valid DTB in flash\n");
74 debug("Using DTB from flash\n");
75 karo_set_fdtsize(fdt);
79 void karo_fdt_move_fdt(void)
82 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
85 debug("DTB already loaded\n");
92 fdt_addr = CONFIG_SYS_FDT_ADDR;
93 printf("fdtaddr is not set; using default: %08lx\n",
97 fdt = karo_fdt_load_dtb();
99 fdt = (void *)gd->fdt_blob;
101 #ifdef CONFIG_OF_EMBED
102 printf("Compiled in FDT not found");
104 printf("No FDT found");
106 printf("; creating empty DTB\n");
107 fdt = (void *)fdt_addr;
108 fdt_create_empty_tree(fdt, 256);
110 printf("No DTB in flash; using default DTB\n");
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);
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));
122 set_working_fdt_addr((void *)fdt_addr);
124 karo_set_fdtsize(fdt);
127 void karo_fdt_remove_node(void *blob, const char *node)
129 int off = fdt_path_offset(blob, node);
132 debug("Removing node '%s' from DT\n", node);
135 printf("Could not find node '%s': %s\n", node,
138 ret = fdt_del_node(blob, off);
140 printf("Failed to remove node '%s': %s\n",
141 node, fdt_strerror(ret));
143 karo_set_fdtsize(blob);
146 void karo_fdt_enable_node(void *blob, const char *node, int enable)
148 int off = fdt_path_offset(blob, node);
150 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
152 printf("Could not find node '%s': %s\n", node,
156 fdt_set_node_status(blob, off,
157 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
159 karo_set_fdtsize(blob);
162 static void fdt_disable_tp_node(void *blob, const char *name)
164 int offs = fdt_node_offset_by_compatible(blob, -1, name);
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);
173 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
177 const char *model = getenv("touchpanel");
179 for (i = 0; i < num_panels; i++) {
180 const char *tp = panels[i];
183 if (strcmp(model, tp) == 0)
186 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
188 tp = strpbrk(tp + 1, ",-");
193 fdt_disable_tp_node(blob, panels[i]);
195 karo_set_fdtsize(blob);
198 static int karo_fdt_disable_node_phandle(void *blob, const char *parent,
204 off = fdt_path_offset(blob, parent);
206 printf("Failed to find node '%s'\n", parent);
210 ph = fdt_getprop(blob, off, name, 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;
217 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
219 printf("Failed to find '%s' node via phandle %04x\n",
220 name, fdt32_to_cpu(*ph));
221 return -FDT_ERR_NOTFOUND;
223 return fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
226 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy,
227 const char *phy_supply)
229 const char *otg_mode = getenv("otg_mode");
233 int disable_phy_pins = 0;
235 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
237 off = fdt_path_offset(blob, node);
239 debug("Failed to find node %s\n", node);
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");
255 if (otg_mode && strcasecmp(otg_mode, "none") != 0)
256 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
262 if ((!disable_phy_pins && !disable_otg) || ret)
265 ret = karo_fdt_disable_node_phandle(blob, node, phy_supply);
266 if (ret && ret == -FDT_ERR_NOTFOUND) {
269 ph = fdt_getprop(blob, off, phy, NULL);
271 printf("Failed to find '%s' phandle in node '%s'\n",
273 ret = -FDT_ERR_NOTFOUND;
276 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
278 printf("Failed to find '%s' node via phandle %04x\n",
279 phy, fdt32_to_cpu(*ph));
283 ph = fdt_getprop(blob, off, phy_supply, 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;
290 ret = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
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);
299 if (ret && ret != -FDT_ERR_NOTFOUND)
304 debug("Disabling '%s'\n", fdt_get_name(blob, off, NULL));
305 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 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);
315 if (ret && ret != -FDT_ERR_NOTFOUND)
316 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
318 debug("node '%s' updated\n", node);
319 karo_set_fdtsize(blob);
322 static inline int karo_fdt_flexcan_enabled(void *blob)
324 static const char *can_ifs[] = {
330 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
332 int off = fdt_path_offset(blob, can_ifs[i]);
335 debug("node '%s' not found\n", can_ifs[i]);
338 status = fdt_getprop(blob, off, "status", NULL);
339 if (status && strcmp(status, "okay") == 0) {
340 debug("%s is enabled\n", can_ifs[i]);
344 debug("can driver is disabled\n");
348 static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
350 int off = fdt_path_offset(blob, name);
352 const struct fdt_property *pc;
358 ph = fdt_create_phandle(blob, off);
362 off = fdt_path_offset(blob, "display");
366 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
367 if (!pc || len < sizeof(ph))
370 memcpy((void *)pc->data, &ph, sizeof(ph));
371 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
374 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
377 const char *xcvr_status = "disabled";
378 const char *otg_mode = getenv("otg_mode");
381 if (karo_fdt_flexcan_enabled(blob)) {
383 debug("Changing LCD to use 23bits only\n");
384 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
387 } else if (!is_lvds()) {
388 debug("Changing LCD to use 24bits\n");
389 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
392 int off = fdt_path_offset(blob, "can0");
395 fdt_delprop(blob, off, "xceiver-supply");
396 off = fdt_path_offset(blob, "can1");
398 fdt_delprop(blob, off, "xceiver-supply");
400 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
403 if (otg_mode && strcasecmp(otg_mode, "host") == 0)
404 karo_fdt_enable_node(blob, "can1", 0);
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);
411 printf("Failed to disable CAN transceiver switch: %s\n",
416 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
421 const uint32_t *phandle;
424 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
428 phandle = fdt_getprop(blob, offset, prop, NULL);
430 ph = fdt32_to_cpu(*phandle);
433 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
434 ret = fdt_delprop(blob, offset, prop);
436 karo_set_fdtsize(blob);
441 offset = fdt_node_offset_by_phandle(blob, ph);
445 debug("Removing node @ %08x\n", offset);
446 fdt_del_node(blob, offset);
447 karo_set_fdtsize(blob);
450 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
452 const uint32_t *prop;
454 memset(fb_mode, 0, sizeof(*fb_mode));
456 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
458 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
460 prop = fdt_getprop(blob, off, "hactive", NULL);
462 fb_mode->xres = fdt32_to_cpu(*prop);
464 prop = fdt_getprop(blob, off, "vactive", NULL);
466 fb_mode->yres = fdt32_to_cpu(*prop);
468 prop = fdt_getprop(blob, off, "hback-porch", NULL);
470 fb_mode->left_margin = fdt32_to_cpu(*prop);
472 prop = fdt_getprop(blob, off, "hsync-len", NULL);
474 fb_mode->hsync_len = fdt32_to_cpu(*prop);
476 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
478 fb_mode->right_margin = fdt32_to_cpu(*prop);
480 prop = fdt_getprop(blob, off, "vback-porch", NULL);
482 fb_mode->upper_margin = fdt32_to_cpu(*prop);
484 prop = fdt_getprop(blob, off, "vsync-len", NULL);
486 fb_mode->vsync_len = fdt32_to_cpu(*prop);
488 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
490 fb_mode->lower_margin = fdt32_to_cpu(*prop);
492 prop = fdt_getprop(blob, off, "hsync-active", NULL);
494 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
496 prop = fdt_getprop(blob, off, "vsync-active", NULL);
498 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
500 prop = fdt_getprop(blob, off, "de-active", NULL);
502 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
504 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
506 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
511 static int fdt_update_native_fb_mode(void *blob, int off)
516 ret = fdt_increase_size(blob, 64);
518 printf("Warning: Failed to increase FDT size: %s\n",
521 debug("Creating phandle at offset %d\n", off);
522 ph = fdt_create_phandle(blob, off);
524 printf("Failed to create phandle for video timing\n");
528 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
530 off = fdt_parent_offset(blob, off);
533 debug("parent offset=%06x\n", off);
534 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
536 printf("Failed to set property 'native-mode': %s\n",
538 karo_set_fdtsize(blob);
542 static int karo_fdt_find_video_timings(void *blob)
544 int off = fdt_path_offset(blob, "display");
545 const char *subnode = "display-timings";
548 debug("Could not find node 'display' in FDT: %s\n",
553 off = fdt_subnode_offset(blob, off, subnode);
555 debug("Could not find node '%s' in FDT: %s\n", subnode,
561 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
563 int off = karo_fdt_find_video_timings(blob);
568 const char *n, *endp;
571 off = fdt_next_node(blob, off, &d);
577 debug("Skipping node @ %04x %s depth %d\n", off,
578 fdt_get_name(blob, off, NULL), d);
582 n = fdt_getprop(blob, off, "panel-name", &len);
584 n = fdt_get_name(blob, off, NULL);
585 if (strcasecmp(n, name) == 0) {
591 for (endp = n + len; n < endp; n += strlen(n) + 1) {
592 debug("Checking panel-name '%s'\n", n);
593 if (strcasecmp(n, name) == 0) {
594 debug("Using node %s @ %04x\n",
595 fdt_get_name(blob, off, NULL), off);
605 return fdt_init_fb_mode(blob, off, fb_mode);
610 #define SET_FB_PROP(n, v) ({ \
612 ret = fdt_setprop_u32(blob, off, n, v); \
614 printf("Failed to set property %s: %s\n", name, \
615 fdt_strerror(ret)); \
621 int karo_fdt_create_fb_mode(void *blob, const char *name,
622 struct fb_videomode *fb_mode)
624 int off = fdt_path_offset(blob, "display");
626 const char *subnode = "display-timings";
629 printf("'display' node not found in FDT\n");
633 ret = fdt_increase_size(blob, 512);
635 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
639 ret = fdt_subnode_offset(blob, off, subnode);
641 debug("Could not find node '%s' in FDT: %s\n", subnode,
643 ret = fdt_add_subnode(blob, off, subnode);
645 printf("Failed to add %s subnode: %s\n", subnode,
651 ret = fdt_add_subnode(blob, ret, name);
653 printf("Failed to add %s subnode: %s\n", name,
659 ret = SET_FB_PROP("clock-frequency",
660 PICOS2KHZ(fb_mode->pixclock) * 1000);
663 ret = SET_FB_PROP("hactive", fb_mode->xres);
666 ret = SET_FB_PROP("vactive", fb_mode->yres);
669 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
672 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
675 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
678 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
681 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
684 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
687 ret = SET_FB_PROP("hsync-active",
688 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
691 ret = SET_FB_PROP("vsync-active",
692 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
695 ret = SET_FB_PROP("de-active",
696 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
699 ret = SET_FB_PROP("pixelclk-active",
700 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
702 karo_set_fdtsize(blob);
706 int karo_fdt_update_fb_mode(void *blob, const char *name)
708 int off = fdt_path_offset(blob, "display");
709 const char *subnode = "display-timings";
717 debug("Disabling node '%s' at %03x\n",
718 fdt_get_name(blob, off, NULL), off);
719 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
723 off = fdt_subnode_offset(blob, off, subnode);
725 debug("Could not find node '%s' in FDT: %s\n", subnode,
730 const char *n, *endp;
733 off = fdt_next_node(blob, off, &d);
739 debug("Skipping node @ %04x %s depth %d\n", off,
740 fdt_get_name(blob, off, NULL), d);
744 n = fdt_getprop(blob, off, "panel-name", &len);
746 n = fdt_get_name(blob, off, NULL);
747 if (strcasecmp(n, name) == 0) {
753 for (endp = n + len; n < endp; n += strlen(n) + 1) {
754 debug("Checking panel-name '%s'\n", n);
755 if (strcasecmp(n, name) == 0) {
756 debug("Using node %s @ %04x\n",
757 fdt_get_name(blob, off, NULL), off);
767 return fdt_update_native_fb_mode(blob, off);
771 #ifdef CONFIG_SYS_LVDS_IF
772 int karo_fdt_get_lcd_bus_width(const void *blob, int default_width)
774 int off = fdt_path_offset(blob, "display");
777 const uint32_t *prop;
779 prop = fdt_getprop(blob, off, "fsl,data-width", NULL);
781 return fdt32_to_cpu(*prop);
783 return default_width;
786 int karo_fdt_get_lvds_mapping(const void *blob, int default_mapping)
788 int off = fdt_path_offset(blob, "display");
793 prop = fdt_getprop(blob, off, "fsl,data-mapping", NULL);
795 return strcmp(prop, "jeida") == 0;
797 return default_mapping;
800 u8 karo_fdt_get_lvds_channels(const void *blob)
802 static const char *lvds_chans[] = {
806 u8 lvds_chan_mask = 0;
809 for (i = 0; i < ARRAY_SIZE(lvds_chans); i++) {
811 int off = fdt_path_offset(blob, lvds_chans[i]);
816 status = fdt_getprop(blob, off, "status", NULL);
817 if (status && strcmp(status, "okay") == 0) {
818 debug("%s is enabled\n", lvds_chans[i]);
819 lvds_chan_mask |= 1 << i;
822 return lvds_chan_mask;
826 int karo_fdt_get_backlight_polarity(const void *blob)
828 #ifdef CONFIG_SYS_LVDS_IF
829 const char *backlight_node = "/backlight0";
831 const char *backlight_node = "/backlight";
833 int off = fdt_path_offset(blob, "backlight"); /* first try alias */
834 const struct fdt_property *prop;
839 * if no 'backlight' alias exists try finding '/backlight0'
840 * or '/backlight' depending on LVDS or not
842 off = fdt_path_offset(blob, backlight_node);
844 printf("%s node not found in DT\n", backlight_node);
849 prop = fdt_get_property(blob, off, "pwms", &len);
851 printf("'pwms' property not found\n");
853 debug("'pwms' property has len %d\n", len);
856 if (prop && len > 3) {
857 const u32 *data = (const u32 *)prop->data;
858 return fdt32_to_cpu(data[3]) == 0;