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>
30 #ifdef CONFIG_MAX_DTB_SIZE
31 #define MAX_DTB_SIZE CONFIG_MAX_DTB_SIZE
33 #define MAX_DTB_SIZE SZ_64K
36 DECLARE_GLOBAL_DATA_PTR;
38 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));
49 snprintf(fdt_size, sizeof(fdt_size), "%08x", fdt_totalsize(fdt));
50 setenv("fdtsize", fdt_size);
53 void karo_fdt_move_fdt(void)
56 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
59 debug("DTB already loaded\n");
64 fdt_addr = CONFIG_SYS_FDT_ADDR;
65 printf("fdtaddr is not set; using default: %08lx\n",
69 fdt = karo_fdt_load_dtb();
71 fdt = (void *)gd->fdt_blob;
73 #ifdef CONFIG_OF_EMBED
74 printf("Compiled in FDT not found\n");
76 printf("No FDT found\n");
80 debug("Checking FDT header @ %p\n", fdt);
81 if (fdt_check_header(fdt)) {
82 printf("ERROR: No valid DTB found at %p\n", fdt);
85 printf("No DTB in flash; using default DTB\n");
86 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
87 fdt, fdt + fdt_totalsize(fdt) - 1,
88 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
89 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
91 set_working_fdt_addr((void *)fdt_addr);
93 karo_set_fdtsize(fdt);
96 void karo_fdt_remove_node(void *blob, const char *node)
98 int off = fdt_path_offset(blob, node);
101 debug("Removing node '%s' from DT\n", node);
104 printf("Could not find node '%s': %d\n", node, off);
106 ret = fdt_del_node(blob, off);
108 printf("Failed to remove node '%s': %d\n",
111 karo_set_fdtsize(blob);
114 void karo_fdt_enable_node(void *blob, const char *node, int enable)
116 int off = fdt_path_offset(blob, node);
118 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
120 printf("Could not find node '%s': %d\n", node, off);
123 fdt_set_node_status(blob, off,
124 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
126 karo_set_fdtsize(blob);
129 static const char *karo_touchpanels[] = {
137 static void fdt_disable_tp_node(void *blob, const char *name)
139 int offs = fdt_node_offset_by_compatible(blob, -1, name);
142 debug("node '%s' not found: %d\n", name, offs);
146 debug("Disabling node '%s'\n", name);
147 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
150 void karo_fdt_fixup_touchpanel(void *blob)
153 const char *model = getenv("touchpanel");
155 for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
156 const char *tp = karo_touchpanels[i];
158 if (model != NULL && strcmp(model, tp) == 0)
162 if (strcmp(model, tp) == 0)
164 tp = strchr(tp, ',');
165 if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
168 fdt_disable_tp_node(blob, karo_touchpanels[i]);
170 karo_set_fdtsize(blob);
173 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
175 const char *otg_mode = getenv("otg_mode");
180 int disable_phy_pins = 1;
182 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
184 off = fdt_path_offset(blob, node);
186 debug("Failed to find node %s\n", node);
190 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
191 strcmp(otg_mode, "gadget") == 0)) {
192 debug("Setting dr_mode to 'peripheral'\n");
193 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
194 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
195 debug("Setting dr_mode to 'host'\n");
196 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
197 disable_phy_pins = 0;
198 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
199 debug("Setting dr_mode to 'host'\n");
200 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
202 if (otg_mode && strcmp(otg_mode, "none") != 0)
203 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
209 if ((!disable_phy_pins && !disable_otg) || ret)
213 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
218 ph = fdt_getprop(blob, off, phy, NULL);
220 printf("Failed to find '%s' phandle in node '%s'\n", phy,
221 fdt_get_name(blob, off, NULL));
225 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
227 printf("Failed to find '%s' node via phandle %04x\n",
228 phy, fdt32_to_cpu(*ph));
233 debug("Disabling usbphy\n");
234 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
238 printf("Failed to update usbotg: %d\n", ret);
240 debug("node '%s' updated\n", node);
241 karo_set_fdtsize(blob);
244 static int karo_fdt_flexcan_enabled(void *blob)
246 const char *can_ifs[] = {
252 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
254 int off = fdt_path_offset(blob, can_ifs[i]);
257 debug("node '%s' not found\n", can_ifs[i]);
260 status = fdt_getprop(blob, off, "status", NULL);
261 if (status && strcmp(status, "okay") == 0) {
262 debug("%s is enabled\n", can_ifs[i]);
266 debug("can driver is disabled\n");
270 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
272 int off = fdt_path_offset(blob, name);
274 const struct fdt_property *pc;
280 ph = fdt_create_phandle(blob, off);
284 off = fdt_path_offset(blob, "display");
288 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
289 if (!pc || len < sizeof(ph))
292 memcpy((void *)pc->data, &ph, sizeof(ph));
293 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
296 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
298 const char *xcvr_status = "disabled";
301 if (karo_fdt_flexcan_enabled(blob)) {
302 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
303 xcvr_status = "okay";
305 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
308 const char *otg_mode = getenv("otg_mode");
310 if (otg_mode && (strcmp(otg_mode, "host") == 0))
311 karo_fdt_enable_node(blob, "can1", 0);
313 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
315 fdt_find_and_setprop(blob, "/regulators/can-xcvr", "status",
316 xcvr_status, strlen(xcvr_status) + 1, 1);
319 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
324 const uint32_t *phandle;
327 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
331 phandle = fdt_getprop(blob, offset, prop, NULL);
333 ph = fdt32_to_cpu(*phandle);
336 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
337 ret = fdt_delprop(blob, offset, prop);
339 karo_set_fdtsize(blob);
344 offset = fdt_node_offset_by_phandle(blob, ph);
348 debug("Removing node @ %08x\n", offset);
349 fdt_del_node(blob, offset);
350 karo_set_fdtsize(blob);
353 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
355 const uint32_t *prop;
357 memset(fb_mode, 0, sizeof(*fb_mode));
359 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
361 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
363 prop = fdt_getprop(blob, off, "hactive", NULL);
365 fb_mode->xres = fdt32_to_cpu(*prop);
367 prop = fdt_getprop(blob, off, "vactive", NULL);
369 fb_mode->yres = fdt32_to_cpu(*prop);
371 prop = fdt_getprop(blob, off, "hback-porch", NULL);
373 fb_mode->left_margin = fdt32_to_cpu(*prop);
375 prop = fdt_getprop(blob, off, "hsync-len", NULL);
377 fb_mode->hsync_len = fdt32_to_cpu(*prop);
379 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
381 fb_mode->right_margin = fdt32_to_cpu(*prop);
383 prop = fdt_getprop(blob, off, "vback-porch", NULL);
385 fb_mode->upper_margin = fdt32_to_cpu(*prop);
387 prop = fdt_getprop(blob, off, "vsync-len", NULL);
389 fb_mode->vsync_len = fdt32_to_cpu(*prop);
391 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
393 fb_mode->lower_margin = fdt32_to_cpu(*prop);
395 prop = fdt_getprop(blob, off, "hsync-active", NULL);
397 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
399 prop = fdt_getprop(blob, off, "vsync-active", NULL);
401 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
403 prop = fdt_getprop(blob, off, "de-active", NULL);
405 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
407 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
409 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
414 static int fdt_update_native_fb_mode(void *blob, int off)
419 ret = fdt_increase_size(blob, 64);
421 printf("Warning: Failed to increase FDT size: %d\n", ret);
423 debug("Creating phandle at offset %d\n", off);
424 ph = fdt_create_phandle(blob, off);
426 printf("Failed to create phandle for video timing\n");
430 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
432 off = fdt_parent_offset(blob, off);
435 debug("parent offset=%06x\n", off);
436 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
438 printf("Failed to set property 'native-mode': %d\n", ret);
439 karo_set_fdtsize(blob);
443 static int karo_fdt_find_video_timings(void *blob)
445 int off = fdt_path_offset(blob, "display");
446 const char *subnode = "display-timings";
449 debug("Could not find node 'display' in FDT: %d\n", off);
453 off = fdt_subnode_offset(blob, off, subnode);
455 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
460 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
462 int off = karo_fdt_find_video_timings(blob);
467 const char *n, *endp;
470 off = fdt_next_node(blob, off, &d);
476 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
479 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
481 n = fdt_getprop(blob, off, "panel-name", &len);
483 n = fdt_get_name(blob, off, NULL);
484 if (strcasecmp(n, name) == 0) {
490 for (endp = n + len; n < endp; n += strlen(n) + 1) {
491 debug("Checking panel-name '%s'\n", n);
492 if (strcasecmp(n, name) == 0) {
493 debug("Using node %s @ %04x\n",
494 fdt_get_name(blob, off, NULL), off);
504 return fdt_init_fb_mode(blob, off, fb_mode);
509 #define SET_FB_PROP(n, v) ({ \
511 ret = fdt_setprop_u32(blob, off, n, v); \
513 printf("Failed to set property %s: %d\n", name, ret); \
519 int karo_fdt_create_fb_mode(void *blob, const char *name,
520 struct fb_videomode *fb_mode)
522 int off = fdt_path_offset(blob, "display");
524 const char *subnode = "display-timings";
527 printf("'display' node not found in FDT\n");
531 ret = fdt_increase_size(blob, 512);
533 printf("Failed to increase FDT size: %d\n", ret);
537 ret = fdt_subnode_offset(blob, off, subnode);
539 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
540 ret = fdt_add_subnode(blob, off, subnode);
542 printf("Failed to add %s subnode: %d\n", subnode, ret);
547 ret = fdt_add_subnode(blob, ret, name);
549 printf("Failed to add %s subnode: %d\n", name, ret);
554 ret = SET_FB_PROP("clock-frequency",
555 PICOS2KHZ(fb_mode->pixclock) * 1000);
558 ret = SET_FB_PROP("hactive", fb_mode->xres);
561 ret = SET_FB_PROP("vactive", fb_mode->yres);
564 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
567 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
570 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
573 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
576 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
579 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
582 ret = SET_FB_PROP("hsync-active",
583 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
586 ret = SET_FB_PROP("vsync-active",
587 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
590 ret = SET_FB_PROP("de-active",
591 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
594 ret = SET_FB_PROP("pixelclk-active",
595 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
597 karo_set_fdtsize(blob);
601 const char *karo_fdt_set_display(const char *video_mode, const char *lcd_path,
602 const char *lvds_path)
604 static char buffer[128];
605 char *vm, *vmode = buffer;
608 void *blob = (void *)gd->fdt_blob;
610 if (video_mode == NULL || strlen(video_mode) == 0)
613 if (strlen(video_mode) >= sizeof(buffer)) {
614 printf("video_mode string too long\n");
617 strcpy(buffer, video_mode);
619 off = fdt_path_offset(blob, "/aliases");
620 ret = fdt_resize(blob);
622 printf("%s: Failed to resize FDT: %s\n",
623 __func__, fdt_strerror(ret));
625 vm = strsep(&vmode, ":");
627 off = fdt_add_subnode(blob, off, "aliases");
629 printf("%s: Failed to create 'aliases' node: %s\n",
630 __func__, fdt_strerror(off));
635 char display[strlen(lvds_path) + sizeof("/lvds-channel@x")];
637 strncpy(display, lvds_path, sizeof(display));
638 if (strcmp(vm, "LVDS") == 0 ||
639 strcmp(vm, "LVDS0") == 0) {
640 strncat(display, "/lvds-channel@0", sizeof(display));
641 } else if (strcmp(vm, "LVDS1") == 0) {
642 strncat(display, "/lvds-channel@1", sizeof(display));
644 debug("%s: Syntax error in video_mode\n", __func__);
647 ret = fdt_setprop_string(blob, off, "display", display);
648 debug("setprop_string(display='%s') returned %d\n", display, ret);
650 char display[strlen(lcd_path) + sizeof("/display@di0")];
652 snprintf(display, sizeof(display), "%s/display@di0", lcd_path);
654 ret = fdt_setprop_string(blob, off, "display",
657 off = fdt_path_offset(blob, "lvds0");
659 ret = fdt_set_node_status(blob, off,
660 FDT_STATUS_DISABLED, 0);
662 off = fdt_path_offset(blob, "lvds1");
664 ret = fdt_set_node_status(blob, off,
665 FDT_STATUS_DISABLED, 0);
670 printf("%s: failed to set 'display' alias: %s\n",
671 __func__, fdt_strerror(ret));
676 int karo_fdt_update_fb_mode(void *blob, const char *name)
678 int off = fdt_path_offset(blob, "display");
679 const char *subnode = "display-timings";
687 debug("Disabling node '%s' at %03x\n",
688 fdt_get_name(blob, off, NULL), off);
689 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
693 off = fdt_subnode_offset(blob, off, subnode);
695 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
699 const char *n, *endp;
702 off = fdt_next_node(blob, off, &d);
708 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
711 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
713 n = fdt_getprop(blob, off, "panel-name", &len);
715 n = fdt_get_name(blob, off, NULL);
716 if (strcasecmp(n, name) == 0) {
722 for (endp = n + len; n < endp; n += strlen(n) + 1) {
723 debug("Checking panel-name '%s'\n", n);
724 if (strcasecmp(n, name) == 0) {
725 debug("Using node %s @ %04x\n",
726 fdt_get_name(blob, off, NULL), off);
736 return fdt_update_native_fb_mode(blob, off);
740 static int karo_load_part(const char *part, void *addr, size_t len)
743 struct mtd_device *dev;
744 struct part_info *part_info;
748 debug("Initializing mtd_parts\n");
749 ret = mtdparts_init();
753 debug("Trying to find NAND partition '%s'\n", part);
754 ret = find_dev_and_part(part, &dev, &part_num, &part_info);
756 printf("Failed to find flash partition '%s': %d\n",
761 debug("Found partition '%s': offset=%08x size=%08x\n",
762 part, part_info->offset, part_info->size);
763 if (part_info->size < len) {
764 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
765 part, len, part_info->size);
766 len = part_info->size;
768 debug("Reading NAND partition '%s' to %p\n", part, addr);
769 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
772 printf("Failed to load partition '%s' to %p\n", part, addr);
776 printf("Read only %u of %u bytes due to bad blocks\n",
779 debug("Read %u byte from partition '%s' @ offset %08x\n",
780 len, part, part_info->offset);
784 void *karo_fdt_load_dtb(void)
787 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
790 debug("aborting DTB load\n");
794 /* clear FDT header in memory */
797 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
799 printf("Failed to load dtb from flash: %d\n", ret);
803 if (fdt_check_header(fdt)) {
804 debug("No valid DTB in flash\n");
807 debug("Using DTB from flash\n");
808 karo_set_fdtsize(fdt);