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 int karo_load_part(const char *part, void *addr, size_t len)
54 struct mtd_device *dev;
55 struct part_info *part_info;
59 debug("Initializing mtd_parts\n");
60 ret = mtdparts_init();
64 debug("Trying to find NAND partition '%s'\n", part);
65 ret = find_dev_and_part(part, &dev, &part_num, &part_info);
67 printf("Failed to find flash partition '%s': %d\n",
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;
79 debug("Reading NAND partition '%s' to %p\n", part, addr);
80 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
83 printf("Failed to load partition '%s' to %p\n", part, addr);
87 printf("Read only %u of %u bytes due to bad blocks\n",
90 debug("Read %u byte from partition '%s' @ offset %08x\n",
91 len, part, part_info->offset);
95 static void *karo_fdt_load_dtb(void)
98 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
101 printf("aborting DTB load\n");
105 /* clear FDT header in memory */
108 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
110 printf("Failed to load dtb from flash: %d\n", ret);
114 if (fdt_check_header(fdt)) {
115 debug("No valid DTB in flash\n");
118 debug("Using DTB from flash\n");
119 karo_set_fdtsize(fdt);
123 void karo_fdt_move_fdt(void)
126 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
129 debug("DTB already loaded\n");
133 setenv("fdtsize", 0);
136 fdt_addr = CONFIG_SYS_FDT_ADDR;
137 printf("fdtaddr is not set; using default: %08lx\n",
141 fdt = karo_fdt_load_dtb();
143 fdt = (void *)gd->fdt_blob;
145 #ifdef CONFIG_OF_EMBED
146 printf("Compiled in FDT not found");
148 printf("No FDT found");
150 printf("; creating empty DTB\n");
151 fdt = (void *)fdt_addr;
152 fdt_create_empty_tree(fdt, 256);
154 printf("No DTB in flash; using default DTB\n");
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);
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));
166 set_working_fdt_addr((void *)fdt_addr);
168 karo_set_fdtsize(fdt);
171 void karo_fdt_remove_node(void *blob, const char *node)
173 int off = fdt_path_offset(blob, node);
176 debug("Removing node '%s' from DT\n", node);
179 printf("Could not find node '%s': %s\n", node,
182 ret = fdt_del_node(blob, off);
184 printf("Failed to remove node '%s': %s\n",
185 node, fdt_strerror(ret));
187 karo_set_fdtsize(blob);
190 void karo_fdt_enable_node(void *blob, const char *node, int enable)
192 int off = fdt_path_offset(blob, node);
194 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
196 printf("Could not find node '%s': %s\n", node,
200 fdt_set_node_status(blob, off,
201 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
203 karo_set_fdtsize(blob);
206 static void fdt_disable_tp_node(void *blob, const char *name)
208 int offs = fdt_node_offset_by_compatible(blob, -1, name);
211 debug("node '%s' not found: %s\n", name, fdt_strerror(offs));
215 debug("Disabling node '%s'\n", name);
216 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
219 void karo_fdt_fixup_touchpanel(void *blob, const char *panels[],
223 const char *model = getenv("touchpanel");
225 for (i = 0; i < num_panels; i++) {
226 const char *tp = panels[i];
229 if (strcmp(model, tp) == 0)
232 if (*tp != '\0' && strcmp(model, tp + 1) == 0)
234 tp = strpbrk(tp + 1, ",-");
239 fdt_disable_tp_node(blob, panels[i]);
241 karo_set_fdtsize(blob);
244 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
246 const char *otg_mode = getenv("otg_mode");
251 int disable_phy_pins = 1;
253 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
255 off = fdt_path_offset(blob, node);
257 debug("Failed to find node %s\n", node);
261 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
262 strcmp(otg_mode, "gadget") == 0)) {
263 debug("Setting dr_mode to 'peripheral'\n");
264 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
265 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
266 debug("Setting dr_mode to 'host'\n");
267 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
268 disable_phy_pins = 0;
269 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
270 debug("Setting dr_mode to 'host'\n");
271 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
273 if (otg_mode && strcmp(otg_mode, "none") != 0)
274 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
280 if ((!disable_phy_pins && !disable_otg) || ret)
284 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
289 ph = fdt_getprop(blob, off, phy, NULL);
291 printf("Failed to find '%s' phandle in node '%s'\n", phy,
292 fdt_get_name(blob, off, NULL));
296 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
298 printf("Failed to find '%s' node via phandle %04x\n",
299 phy, fdt32_to_cpu(*ph));
304 debug("Disabling usbphy\n");
305 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
309 printf("Failed to update usbotg: %s\n", fdt_strerror(ret));
311 debug("node '%s' updated\n", node);
312 karo_set_fdtsize(blob);
315 static inline int karo_fdt_flexcan_enabled(void *blob)
317 static const char *can_ifs[] = {
323 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
325 int off = fdt_path_offset(blob, can_ifs[i]);
328 debug("node '%s' not found\n", can_ifs[i]);
331 status = fdt_getprop(blob, off, "status", NULL);
332 if (status && strcmp(status, "okay") == 0) {
333 debug("%s is enabled\n", can_ifs[i]);
337 debug("can driver is disabled\n");
341 static inline void karo_fdt_set_lcd_pins(void *blob, const char *name)
343 int off = fdt_path_offset(blob, name);
345 const struct fdt_property *pc;
351 ph = fdt_create_phandle(blob, off);
355 off = fdt_path_offset(blob, "display");
359 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
360 if (!pc || len < sizeof(ph))
363 memcpy((void *)pc->data, &ph, sizeof(ph));
364 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
367 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
369 const char *xcvr_status = "disabled";
371 #ifndef CONFIG_SYS_LVDS_IF
373 if (karo_fdt_flexcan_enabled(blob)) {
374 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
375 xcvr_status = "okay";
377 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
380 const char *otg_mode = getenv("otg_mode");
382 if (otg_mode && (strcmp(otg_mode, "host") == 0))
383 karo_fdt_enable_node(blob, "can1", 0);
385 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
388 fdt_find_and_setprop(blob, "reg_can_xcvr", "status",
389 xcvr_status, strlen(xcvr_status) + 1, 1);
392 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
397 const uint32_t *phandle;
400 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
404 phandle = fdt_getprop(blob, offset, prop, NULL);
406 ph = fdt32_to_cpu(*phandle);
409 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
410 ret = fdt_delprop(blob, offset, prop);
412 karo_set_fdtsize(blob);
417 offset = fdt_node_offset_by_phandle(blob, ph);
421 debug("Removing node @ %08x\n", offset);
422 fdt_del_node(blob, offset);
423 karo_set_fdtsize(blob);
426 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
428 const uint32_t *prop;
430 memset(fb_mode, 0, sizeof(*fb_mode));
432 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
434 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
436 prop = fdt_getprop(blob, off, "hactive", NULL);
438 fb_mode->xres = fdt32_to_cpu(*prop);
440 prop = fdt_getprop(blob, off, "vactive", NULL);
442 fb_mode->yres = fdt32_to_cpu(*prop);
444 prop = fdt_getprop(blob, off, "hback-porch", NULL);
446 fb_mode->left_margin = fdt32_to_cpu(*prop);
448 prop = fdt_getprop(blob, off, "hsync-len", NULL);
450 fb_mode->hsync_len = fdt32_to_cpu(*prop);
452 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
454 fb_mode->right_margin = fdt32_to_cpu(*prop);
456 prop = fdt_getprop(blob, off, "vback-porch", NULL);
458 fb_mode->upper_margin = fdt32_to_cpu(*prop);
460 prop = fdt_getprop(blob, off, "vsync-len", NULL);
462 fb_mode->vsync_len = fdt32_to_cpu(*prop);
464 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
466 fb_mode->lower_margin = fdt32_to_cpu(*prop);
468 prop = fdt_getprop(blob, off, "hsync-active", NULL);
470 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
472 prop = fdt_getprop(blob, off, "vsync-active", NULL);
474 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
476 prop = fdt_getprop(blob, off, "de-active", NULL);
478 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
480 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
482 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
487 static int fdt_update_native_fb_mode(void *blob, int off)
492 ret = fdt_increase_size(blob, 64);
494 printf("Warning: Failed to increase FDT size: %s\n",
497 debug("Creating phandle at offset %d\n", off);
498 ph = fdt_create_phandle(blob, off);
500 printf("Failed to create phandle for video timing\n");
504 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
506 off = fdt_parent_offset(blob, off);
509 debug("parent offset=%06x\n", off);
510 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
512 printf("Failed to set property 'native-mode': %s\n",
514 karo_set_fdtsize(blob);
518 static int karo_fdt_find_video_timings(void *blob)
520 int off = fdt_path_offset(blob, "display");
521 const char *subnode = "display-timings";
524 debug("Could not find node 'display' in FDT: %s\n",
529 off = fdt_subnode_offset(blob, off, subnode);
531 debug("Could not find node '%s' in FDT: %s\n", subnode,
537 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
539 int off = karo_fdt_find_video_timings(blob);
544 const char *n, *endp;
547 off = fdt_next_node(blob, off, &d);
553 debug("Skipping node @ %04x %s depth %d\n", off,
554 fdt_get_name(blob, off, NULL), d);
557 debug("parsing subnode @ %04x %s depth %d\n", off,
558 fdt_get_name(blob, off, NULL), d);
560 n = fdt_getprop(blob, off, "panel-name", &len);
562 n = fdt_get_name(blob, off, NULL);
563 if (strcasecmp(n, name) == 0) {
569 for (endp = n + len; n < endp; n += strlen(n) + 1) {
570 debug("Checking panel-name '%s'\n", n);
571 if (strcasecmp(n, name) == 0) {
572 debug("Using node %s @ %04x\n",
573 fdt_get_name(blob, off, NULL), off);
583 return fdt_init_fb_mode(blob, off, fb_mode);
588 #define SET_FB_PROP(n, v) ({ \
590 ret = fdt_setprop_u32(blob, off, n, v); \
592 printf("Failed to set property %s: %s\n", name, \
593 fdt_strerror(ret)); \
599 int karo_fdt_create_fb_mode(void *blob, const char *name,
600 struct fb_videomode *fb_mode)
602 int off = fdt_path_offset(blob, "display");
604 const char *subnode = "display-timings";
607 printf("'display' node not found in FDT\n");
611 ret = fdt_increase_size(blob, 512);
613 printf("Failed to increase FDT size: %s\n", fdt_strerror(ret));
617 ret = fdt_subnode_offset(blob, off, subnode);
619 debug("Could not find node '%s' in FDT: %s\n", subnode,
621 ret = fdt_add_subnode(blob, off, subnode);
623 printf("Failed to add %s subnode: %s\n", subnode,
629 ret = fdt_add_subnode(blob, ret, name);
631 printf("Failed to add %s subnode: %s\n", name,
637 ret = SET_FB_PROP("clock-frequency",
638 PICOS2KHZ(fb_mode->pixclock) * 1000);
641 ret = SET_FB_PROP("hactive", fb_mode->xres);
644 ret = SET_FB_PROP("vactive", fb_mode->yres);
647 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
650 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
653 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
656 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
659 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
662 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
665 ret = SET_FB_PROP("hsync-active",
666 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
669 ret = SET_FB_PROP("vsync-active",
670 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
673 ret = SET_FB_PROP("de-active",
674 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
677 ret = SET_FB_PROP("pixelclk-active",
678 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
680 karo_set_fdtsize(blob);
684 int karo_fdt_update_fb_mode(void *blob, const char *name)
686 int off = fdt_path_offset(blob, "display");
687 const char *subnode = "display-timings";
695 debug("Disabling node '%s' at %03x\n",
696 fdt_get_name(blob, off, NULL), off);
697 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
701 off = fdt_subnode_offset(blob, off, subnode);
703 debug("Could not find node '%s' in FDT: %s\n", subnode,
708 const char *n, *endp;
711 off = fdt_next_node(blob, off, &d);
717 debug("Skipping node @ %04x %s depth %d\n", off,
718 fdt_get_name(blob, off, NULL), d);
721 debug("parsing subnode @ %04x %s depth %d\n", off,
722 fdt_get_name(blob, off, NULL), d);
724 n = fdt_getprop(blob, off, "panel-name", &len);
726 n = fdt_get_name(blob, off, NULL);
727 if (strcasecmp(n, name) == 0) {
733 for (endp = n + len; n < endp; n += strlen(n) + 1) {
734 debug("Checking panel-name '%s'\n", n);
735 if (strcasecmp(n, name) == 0) {
736 debug("Using node %s @ %04x\n",
737 fdt_get_name(blob, off, NULL), off);
747 return fdt_update_native_fb_mode(blob, off);
751 int karo_fdt_get_lcd_bus_width(const void *blob, int default_width)
753 int off = fdt_path_offset(blob, "display");
756 const uint32_t *prop;
758 prop = fdt_getprop(blob, off, "fsl,data-width", NULL);
760 return fdt32_to_cpu(*prop);
762 return default_width;
765 int karo_fdt_get_lvds_mapping(const void *blob, int default_mapping)
767 int off = fdt_path_offset(blob, "display");
772 prop = fdt_getprop(blob, off, "fsl,data-mapping", NULL);
774 return strcmp(prop, "jeida") == 0;
776 return default_mapping;
779 u8 karo_fdt_get_lvds_channels(const void *blob)
781 static const char *lvds_chans[] = {
785 u8 lvds_chan_mask = 0;
788 for (i = 0; i < ARRAY_SIZE(lvds_chans); i++) {
790 int off = fdt_path_offset(blob, lvds_chans[i]);
795 status = fdt_getprop(blob, off, "status", NULL);
796 if (status && strcmp(status, "okay") == 0) {
797 debug("%s is enabled\n", lvds_chans[i]);
798 lvds_chan_mask |= 1 << i;
801 return lvds_chan_mask;