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)
42 size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
44 if (fdtsize == fdt_totalsize(fdt)) {
47 debug("FDT size changed from %u to %u\n",
48 fdtsize, fdt_totalsize(fdt));
50 snprintf(fdt_size, sizeof(fdt_size), "%08x", fdt_totalsize(fdt));
51 setenv("fdtsize", fdt_size);
54 static int karo_load_part(const char *part, void *addr, size_t len)
57 struct mtd_device *dev;
58 struct part_info *part_info;
62 debug("Initializing mtd_parts\n");
63 ret = mtdparts_init();
67 debug("Trying to find NAND partition '%s'\n", part);
68 ret = find_dev_and_part(part, &dev, &part_num, &part_info);
70 printf("Failed to find flash partition '%s': %d\n",
75 debug("Found partition '%s': offset=%08x size=%08x\n",
76 part, part_info->offset, part_info->size);
77 if (part_info->size < len) {
78 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
79 part, len, part_info->size);
80 len = part_info->size;
82 debug("Reading NAND partition '%s' to %p\n", part, addr);
83 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
86 printf("Failed to load partition '%s' to %p\n", part, addr);
90 printf("Read only %u of %u bytes due to bad blocks\n",
93 debug("Read %u byte from partition '%s' @ offset %08x\n",
94 len, part, part_info->offset);
98 static void *karo_fdt_load_dtb(void)
101 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
104 debug("aborting DTB load\n");
108 /* clear FDT header in memory */
111 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
113 printf("Failed to load dtb from flash: %d\n", ret);
117 if (fdt_check_header(fdt)) {
118 debug("No valid DTB in flash\n");
121 debug("Using DTB from flash\n");
122 karo_set_fdtsize(fdt);
126 void karo_fdt_move_fdt(void)
129 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
132 debug("DTB already loaded\n");
137 fdt_addr = CONFIG_SYS_FDT_ADDR;
138 printf("fdtaddr is not set; using default: %08lx\n",
142 fdt = karo_fdt_load_dtb();
144 fdt = (void *)gd->fdt_blob;
146 #ifdef CONFIG_OF_EMBED
147 printf("Compiled in FDT not found");
149 printf("No FDT found");
151 printf("; creating empty DTB\n");
152 fdt = (void *)fdt_addr;
153 fdt_create_empty_tree(fdt, 256);
155 printf("No DTB in flash; using default DTB\n");
157 debug("Checking FDT header @ %p\n", fdt);
158 if (fdt_check_header(fdt)) {
159 printf("ERROR: No valid DTB found at %p\n", fdt);
162 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
163 fdt, fdt + fdt_totalsize(fdt) - 1,
164 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
165 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
167 set_working_fdt_addr((void *)fdt_addr);
169 karo_set_fdtsize(fdt);
172 void karo_fdt_remove_node(void *blob, const char *node)
174 int off = fdt_path_offset(blob, node);
177 debug("Removing node '%s' from DT\n", node);
180 printf("Could not find node '%s': %d\n", node, off);
182 ret = fdt_del_node(blob, off);
184 printf("Failed to remove node '%s': %d\n",
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': %d\n", node, off);
199 fdt_set_node_status(blob, off,
200 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
202 karo_set_fdtsize(blob);
205 static const char *karo_touchpanels[] = {
213 static void fdt_disable_tp_node(void *blob, const char *name)
215 int offs = fdt_node_offset_by_compatible(blob, -1, name);
218 debug("node '%s' not found: %d\n", name, offs);
222 debug("Disabling node '%s'\n", name);
223 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
226 void karo_fdt_fixup_touchpanel(void *blob)
229 const char *model = getenv("touchpanel");
231 for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
232 const char *tp = karo_touchpanels[i];
234 if (model != NULL && strcmp(model, tp) == 0)
238 if (strcmp(model, tp) == 0)
240 tp = strchr(tp, ',');
241 if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
244 fdt_disable_tp_node(blob, karo_touchpanels[i]);
246 karo_set_fdtsize(blob);
249 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
251 const char *otg_mode = getenv("otg_mode");
256 int disable_phy_pins = 1;
258 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
260 off = fdt_path_offset(blob, node);
262 debug("Failed to find node %s\n", node);
266 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
267 strcmp(otg_mode, "gadget") == 0)) {
268 debug("Setting dr_mode to 'peripheral'\n");
269 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
270 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
271 debug("Setting dr_mode to 'host'\n");
272 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
273 disable_phy_pins = 0;
274 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
275 debug("Setting dr_mode to 'host'\n");
276 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
278 if (otg_mode && strcmp(otg_mode, "none") != 0)
279 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
285 if ((!disable_phy_pins && !disable_otg) || ret)
289 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
294 ph = fdt_getprop(blob, off, phy, NULL);
296 printf("Failed to find '%s' phandle in node '%s'\n", phy,
297 fdt_get_name(blob, off, NULL));
301 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
303 printf("Failed to find '%s' node via phandle %04x\n",
304 phy, fdt32_to_cpu(*ph));
309 debug("Disabling usbphy\n");
310 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
314 printf("Failed to update usbotg: %d\n", ret);
316 debug("node '%s' updated\n", node);
317 karo_set_fdtsize(blob);
320 static int karo_fdt_flexcan_enabled(void *blob)
322 const char *can_ifs[] = {
328 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
330 int off = fdt_path_offset(blob, can_ifs[i]);
333 debug("node '%s' not found\n", can_ifs[i]);
336 status = fdt_getprop(blob, off, "status", NULL);
337 if (status && strcmp(status, "okay") == 0) {
338 debug("%s is enabled\n", can_ifs[i]);
342 debug("can driver is disabled\n");
346 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
348 int off = fdt_path_offset(blob, name);
350 const struct fdt_property *pc;
356 ph = fdt_create_phandle(blob, off);
360 off = fdt_path_offset(blob, "display");
364 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
365 if (!pc || len < sizeof(ph))
368 memcpy((void *)pc->data, &ph, sizeof(ph));
369 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
372 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
374 const char *xcvr_status = "disabled";
377 if (karo_fdt_flexcan_enabled(blob)) {
378 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
379 xcvr_status = "okay";
381 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
384 const char *otg_mode = getenv("otg_mode");
386 if (otg_mode && (strcmp(otg_mode, "host") == 0))
387 karo_fdt_enable_node(blob, "can1", 0);
389 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
391 fdt_find_and_setprop(blob, "/regulators/can-xcvr", "status",
392 xcvr_status, strlen(xcvr_status) + 1, 1);
395 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
400 const uint32_t *phandle;
403 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
407 phandle = fdt_getprop(blob, offset, prop, NULL);
409 ph = fdt32_to_cpu(*phandle);
412 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
413 ret = fdt_delprop(blob, offset, prop);
415 karo_set_fdtsize(blob);
420 offset = fdt_node_offset_by_phandle(blob, ph);
424 debug("Removing node @ %08x\n", offset);
425 fdt_del_node(blob, offset);
426 karo_set_fdtsize(blob);
429 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
431 const uint32_t *prop;
433 memset(fb_mode, 0, sizeof(*fb_mode));
435 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
437 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
439 prop = fdt_getprop(blob, off, "hactive", NULL);
441 fb_mode->xres = fdt32_to_cpu(*prop);
443 prop = fdt_getprop(blob, off, "vactive", NULL);
445 fb_mode->yres = fdt32_to_cpu(*prop);
447 prop = fdt_getprop(blob, off, "hback-porch", NULL);
449 fb_mode->left_margin = fdt32_to_cpu(*prop);
451 prop = fdt_getprop(blob, off, "hsync-len", NULL);
453 fb_mode->hsync_len = fdt32_to_cpu(*prop);
455 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
457 fb_mode->right_margin = fdt32_to_cpu(*prop);
459 prop = fdt_getprop(blob, off, "vback-porch", NULL);
461 fb_mode->upper_margin = fdt32_to_cpu(*prop);
463 prop = fdt_getprop(blob, off, "vsync-len", NULL);
465 fb_mode->vsync_len = fdt32_to_cpu(*prop);
467 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
469 fb_mode->lower_margin = fdt32_to_cpu(*prop);
471 prop = fdt_getprop(blob, off, "hsync-active", NULL);
473 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
475 prop = fdt_getprop(blob, off, "vsync-active", NULL);
477 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
479 prop = fdt_getprop(blob, off, "de-active", NULL);
481 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
483 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
485 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
490 static int fdt_update_native_fb_mode(void *blob, int off)
495 ret = fdt_increase_size(blob, 64);
497 printf("Warning: Failed to increase FDT size: %d\n", ret);
499 debug("Creating phandle at offset %d\n", off);
500 ph = fdt_create_phandle(blob, off);
502 printf("Failed to create phandle for video timing\n");
506 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
508 off = fdt_parent_offset(blob, off);
511 debug("parent offset=%06x\n", off);
512 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
514 printf("Failed to set property 'native-mode': %d\n", ret);
515 karo_set_fdtsize(blob);
519 static int karo_fdt_find_video_timings(void *blob)
521 int off = fdt_path_offset(blob, "display");
522 const char *subnode = "display-timings";
525 debug("Could not find node 'display' in FDT: %d\n", off);
529 off = fdt_subnode_offset(blob, off, subnode);
531 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
536 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
538 int off = karo_fdt_find_video_timings(blob);
543 const char *n, *endp;
546 off = fdt_next_node(blob, off, &d);
552 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
555 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
557 n = fdt_getprop(blob, off, "panel-name", &len);
559 n = fdt_get_name(blob, off, NULL);
560 if (strcasecmp(n, name) == 0) {
566 for (endp = n + len; n < endp; n += strlen(n) + 1) {
567 debug("Checking panel-name '%s'\n", n);
568 if (strcasecmp(n, name) == 0) {
569 debug("Using node %s @ %04x\n",
570 fdt_get_name(blob, off, NULL), off);
580 return fdt_init_fb_mode(blob, off, fb_mode);
585 #define SET_FB_PROP(n, v) ({ \
587 ret = fdt_setprop_u32(blob, off, n, v); \
589 printf("Failed to set property %s: %d\n", name, ret); \
595 int karo_fdt_create_fb_mode(void *blob, const char *name,
596 struct fb_videomode *fb_mode)
598 int off = fdt_path_offset(blob, "display");
600 const char *subnode = "display-timings";
603 printf("'display' node not found in FDT\n");
607 ret = fdt_increase_size(blob, 512);
609 printf("Failed to increase FDT size: %d\n", ret);
613 ret = fdt_subnode_offset(blob, off, subnode);
615 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
616 ret = fdt_add_subnode(blob, off, subnode);
618 printf("Failed to add %s subnode: %d\n", subnode, ret);
623 ret = fdt_add_subnode(blob, ret, name);
625 printf("Failed to add %s subnode: %d\n", name, ret);
630 ret = SET_FB_PROP("clock-frequency",
631 PICOS2KHZ(fb_mode->pixclock) * 1000);
634 ret = SET_FB_PROP("hactive", fb_mode->xres);
637 ret = SET_FB_PROP("vactive", fb_mode->yres);
640 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
643 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
646 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
649 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
652 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
655 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
658 ret = SET_FB_PROP("hsync-active",
659 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
662 ret = SET_FB_PROP("vsync-active",
663 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
666 ret = SET_FB_PROP("de-active",
667 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
670 ret = SET_FB_PROP("pixelclk-active",
671 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
673 karo_set_fdtsize(blob);
677 static int karo_fdt_set_display_alias(void *blob, const char *path,
682 size_t size = strlen(path) + strlen(name) + 2;
685 display = malloc(size);
686 if (display == NULL) {
687 printf("%s: Failed to allocate buffer\n", __func__);
690 sprintf(display, "%s/%s", path, name);
691 if (strlen(display) != size - 1)
693 off = fdt_path_offset(blob, "/aliases");
694 if (off == FDT_ERR_BADMAGIC)
696 ret = fdt_resize(blob);
698 printf("%s: Failed to resize FDT: %s\n",
699 __func__, fdt_strerror(ret));
702 off = fdt_add_subnode(blob, 0, "aliases");
704 printf("%s: Failed to create 'aliases' node: %s\n",
705 __func__, fdt_strerror(off));
709 ret = fdt_setprop_string(blob, off, "display", display);
710 debug("setprop_string(display='%s') returned %d\n", display, ret);
714 const char *karo_fdt_set_display(const char *video_mode, const char *lcd_path,
715 const char *lvds_path)
717 const char *vmode = NULL;
719 void *blob = working_fdt;
721 if (video_mode == NULL || strlen(video_mode) == 0)
724 vmode = strchr(video_mode, ':');
726 if (lvds_path == NULL)
727 return vmode ? vmode + 1 : video_mode;
729 if (lvds_path != NULL && vmode != NULL) {
730 if (strncmp(video_mode, "LVDS:", 5) == 0 ||
731 strncmp(video_mode, "LVDS0:", 6) == 0) {
732 ret = karo_fdt_set_display_alias(blob, lvds_path,
734 } else if (strncmp(video_mode, "LVDS1:", 6) == 0) {
735 ret = karo_fdt_set_display_alias(blob, lvds_path,
738 debug("%s: Syntax error in video_mode\n", __func__);
741 video_mode = vmode + 1;
745 ret = karo_fdt_set_display_alias(blob, lcd_path,
748 off = fdt_path_offset(blob, "lvds0");
750 ret = fdt_set_node_status(blob, off,
751 FDT_STATUS_DISABLED, 0);
753 off = fdt_path_offset(blob, "lvds1");
755 ret = fdt_set_node_status(blob, off,
756 FDT_STATUS_DISABLED, 0);
760 printf("%s: failed to set 'display' alias: %s\n",
761 __func__, fdt_strerror(ret));
766 int karo_fdt_update_fb_mode(void *blob, const char *name)
768 int off = fdt_path_offset(blob, "display");
769 const char *subnode = "display-timings";
777 debug("Disabling node '%s' at %03x\n",
778 fdt_get_name(blob, off, NULL), off);
779 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
783 off = fdt_subnode_offset(blob, off, subnode);
785 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
789 const char *n, *endp;
792 off = fdt_next_node(blob, off, &d);
798 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
801 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
803 n = fdt_getprop(blob, off, "panel-name", &len);
805 n = fdt_get_name(blob, off, NULL);
806 if (strcasecmp(n, name) == 0) {
812 for (endp = n + len; n < endp; n += strlen(n) + 1) {
813 debug("Checking panel-name '%s'\n", n);
814 if (strcasecmp(n, name) == 0) {
815 debug("Using node %s @ %04x\n",
816 fdt_get_name(blob, off, NULL), off);
826 return fdt_update_native_fb_mode(blob, off);