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 void karo_fdt_move_fdt(void)
57 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
60 debug("DTB already loaded\n");
65 fdt_addr = CONFIG_SYS_FDT_ADDR;
66 printf("fdtaddr is not set; using default: %08lx\n",
70 fdt = karo_fdt_load_dtb();
72 fdt = (void *)gd->fdt_blob;
74 #ifdef CONFIG_OF_EMBED
75 printf("Compiled in FDT not found\n");
77 printf("No FDT found\n");
81 debug("Checking FDT header @ %p\n", fdt);
82 if (fdt_check_header(fdt)) {
83 printf("ERROR: No valid DTB found at %p\n", fdt);
86 printf("No DTB in flash; using default DTB\n");
87 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
88 fdt, fdt + fdt_totalsize(fdt) - 1,
89 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
90 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
92 set_working_fdt_addr((void *)fdt_addr);
94 karo_set_fdtsize(fdt);
97 void karo_fdt_remove_node(void *blob, const char *node)
99 int off = fdt_path_offset(blob, node);
102 debug("Removing node '%s' from DT\n", node);
105 printf("Could not find node '%s': %d\n", node, off);
107 ret = fdt_del_node(blob, off);
109 printf("Failed to remove node '%s': %d\n",
112 karo_set_fdtsize(blob);
115 void karo_fdt_enable_node(void *blob, const char *node, int enable)
117 int off = fdt_path_offset(blob, node);
119 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
121 printf("Could not find node '%s': %d\n", node, off);
124 fdt_set_node_status(blob, off,
125 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
127 karo_set_fdtsize(blob);
130 static const char *karo_touchpanels[] = {
138 static void fdt_disable_tp_node(void *blob, const char *name)
140 int offs = fdt_node_offset_by_compatible(blob, -1, name);
143 debug("node '%s' not found: %d\n", name, offs);
147 debug("Disabling node '%s'\n", name);
148 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
151 void karo_fdt_fixup_touchpanel(void *blob)
154 const char *model = getenv("touchpanel");
156 for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
157 const char *tp = karo_touchpanels[i];
159 if (model != NULL && strcmp(model, tp) == 0)
163 if (strcmp(model, tp) == 0)
165 tp = strchr(tp, ',');
166 if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
169 fdt_disable_tp_node(blob, karo_touchpanels[i]);
171 karo_set_fdtsize(blob);
174 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
176 const char *otg_mode = getenv("otg_mode");
181 int disable_phy_pins = 1;
183 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
185 off = fdt_path_offset(blob, node);
187 debug("Failed to find node %s\n", node);
191 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
192 strcmp(otg_mode, "gadget") == 0)) {
193 debug("Setting dr_mode to 'peripheral'\n");
194 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
195 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
196 debug("Setting dr_mode to 'host'\n");
197 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
198 disable_phy_pins = 0;
199 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
200 debug("Setting dr_mode to 'host'\n");
201 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
203 if (otg_mode && strcmp(otg_mode, "none") != 0)
204 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
210 if ((!disable_phy_pins && !disable_otg) || ret)
214 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
219 ph = fdt_getprop(blob, off, phy, NULL);
221 printf("Failed to find '%s' phandle in node '%s'\n", phy,
222 fdt_get_name(blob, off, NULL));
226 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
228 printf("Failed to find '%s' node via phandle %04x\n",
229 phy, fdt32_to_cpu(*ph));
234 debug("Disabling usbphy\n");
235 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
239 printf("Failed to update usbotg: %d\n", ret);
241 debug("node '%s' updated\n", node);
242 karo_set_fdtsize(blob);
245 static int karo_fdt_flexcan_enabled(void *blob)
247 const char *can_ifs[] = {
253 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
255 int off = fdt_path_offset(blob, can_ifs[i]);
258 debug("node '%s' not found\n", can_ifs[i]);
261 status = fdt_getprop(blob, off, "status", NULL);
262 if (status && strcmp(status, "okay") == 0) {
263 debug("%s is enabled\n", can_ifs[i]);
267 debug("can driver is disabled\n");
271 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
273 int off = fdt_path_offset(blob, name);
275 const struct fdt_property *pc;
281 ph = fdt_create_phandle(blob, off);
285 off = fdt_path_offset(blob, "display");
289 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
290 if (!pc || len < sizeof(ph))
293 memcpy((void *)pc->data, &ph, sizeof(ph));
294 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
297 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
299 const char *xcvr_status = "disabled";
302 if (karo_fdt_flexcan_enabled(blob)) {
303 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
304 xcvr_status = "okay";
306 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
309 const char *otg_mode = getenv("otg_mode");
311 if (otg_mode && (strcmp(otg_mode, "host") == 0))
312 karo_fdt_enable_node(blob, "can1", 0);
314 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
316 fdt_find_and_setprop(blob, "/regulators/can-xcvr", "status",
317 xcvr_status, strlen(xcvr_status) + 1, 1);
320 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
325 const uint32_t *phandle;
328 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
332 phandle = fdt_getprop(blob, offset, prop, NULL);
334 ph = fdt32_to_cpu(*phandle);
337 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
338 ret = fdt_delprop(blob, offset, prop);
340 karo_set_fdtsize(blob);
345 offset = fdt_node_offset_by_phandle(blob, ph);
349 debug("Removing node @ %08x\n", offset);
350 fdt_del_node(blob, offset);
351 karo_set_fdtsize(blob);
354 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
356 const uint32_t *prop;
358 memset(fb_mode, 0, sizeof(*fb_mode));
360 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
362 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
364 prop = fdt_getprop(blob, off, "hactive", NULL);
366 fb_mode->xres = fdt32_to_cpu(*prop);
368 prop = fdt_getprop(blob, off, "vactive", NULL);
370 fb_mode->yres = fdt32_to_cpu(*prop);
372 prop = fdt_getprop(blob, off, "hback-porch", NULL);
374 fb_mode->left_margin = fdt32_to_cpu(*prop);
376 prop = fdt_getprop(blob, off, "hsync-len", NULL);
378 fb_mode->hsync_len = fdt32_to_cpu(*prop);
380 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
382 fb_mode->right_margin = fdt32_to_cpu(*prop);
384 prop = fdt_getprop(blob, off, "vback-porch", NULL);
386 fb_mode->upper_margin = fdt32_to_cpu(*prop);
388 prop = fdt_getprop(blob, off, "vsync-len", NULL);
390 fb_mode->vsync_len = fdt32_to_cpu(*prop);
392 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
394 fb_mode->lower_margin = fdt32_to_cpu(*prop);
396 prop = fdt_getprop(blob, off, "hsync-active", NULL);
398 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
400 prop = fdt_getprop(blob, off, "vsync-active", NULL);
402 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
404 prop = fdt_getprop(blob, off, "de-active", NULL);
406 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
408 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
410 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
415 static int fdt_update_native_fb_mode(void *blob, int off)
420 ret = fdt_increase_size(blob, 64);
422 printf("Warning: Failed to increase FDT size: %d\n", ret);
424 debug("Creating phandle at offset %d\n", off);
425 ph = fdt_create_phandle(blob, off);
427 printf("Failed to create phandle for video timing\n");
431 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
433 off = fdt_parent_offset(blob, off);
436 debug("parent offset=%06x\n", off);
437 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
439 printf("Failed to set property 'native-mode': %d\n", ret);
440 karo_set_fdtsize(blob);
444 static int karo_fdt_find_video_timings(void *blob)
446 int off = fdt_path_offset(blob, "display");
447 const char *subnode = "display-timings";
450 debug("Could not find node 'display' in FDT: %d\n", off);
454 off = fdt_subnode_offset(blob, off, subnode);
456 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
461 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
463 int off = karo_fdt_find_video_timings(blob);
468 const char *n, *endp;
471 off = fdt_next_node(blob, off, &d);
477 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
480 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
482 n = fdt_getprop(blob, off, "panel-name", &len);
484 n = fdt_get_name(blob, off, NULL);
485 if (strcasecmp(n, name) == 0) {
491 for (endp = n + len; n < endp; n += strlen(n) + 1) {
492 debug("Checking panel-name '%s'\n", n);
493 if (strcasecmp(n, name) == 0) {
494 debug("Using node %s @ %04x\n",
495 fdt_get_name(blob, off, NULL), off);
505 return fdt_init_fb_mode(blob, off, fb_mode);
510 #define SET_FB_PROP(n, v) ({ \
512 ret = fdt_setprop_u32(blob, off, n, v); \
514 printf("Failed to set property %s: %d\n", name, ret); \
520 int karo_fdt_create_fb_mode(void *blob, const char *name,
521 struct fb_videomode *fb_mode)
523 int off = fdt_path_offset(blob, "display");
525 const char *subnode = "display-timings";
528 printf("'display' node not found in FDT\n");
532 ret = fdt_increase_size(blob, 512);
534 printf("Failed to increase FDT size: %d\n", ret);
538 ret = fdt_subnode_offset(blob, off, subnode);
540 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
541 ret = fdt_add_subnode(blob, off, subnode);
543 printf("Failed to add %s subnode: %d\n", subnode, ret);
548 ret = fdt_add_subnode(blob, ret, name);
550 printf("Failed to add %s subnode: %d\n", name, ret);
555 ret = SET_FB_PROP("clock-frequency",
556 PICOS2KHZ(fb_mode->pixclock) * 1000);
559 ret = SET_FB_PROP("hactive", fb_mode->xres);
562 ret = SET_FB_PROP("vactive", fb_mode->yres);
565 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
568 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
571 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
574 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
577 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
580 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
583 ret = SET_FB_PROP("hsync-active",
584 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
587 ret = SET_FB_PROP("vsync-active",
588 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
591 ret = SET_FB_PROP("de-active",
592 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
595 ret = SET_FB_PROP("pixelclk-active",
596 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
598 karo_set_fdtsize(blob);
602 static int karo_fdt_set_display_alias(void *blob, const char *path,
607 size_t size = strlen(path) + strlen(name) + 2;
610 display = malloc(size);
611 if (display == NULL) {
612 printf("%s: Failed to allocate buffer\n", __func__);
615 sprintf(display, "%s/%s", path, name);
616 if (strlen(display) != size - 1)
618 off = fdt_path_offset(blob, "/aliases");
619 if (off == FDT_ERR_BADMAGIC)
621 ret = fdt_resize(blob);
623 printf("%s: Failed to resize FDT: %s\n",
624 __func__, fdt_strerror(ret));
627 off = fdt_add_subnode(blob, 0, "aliases");
629 printf("%s: Failed to create 'aliases' node: %s\n",
630 __func__, fdt_strerror(off));
634 ret = fdt_setprop_string(blob, off, "display", display);
635 debug("setprop_string(display='%s') returned %d\n", display, ret);
639 const char *karo_fdt_set_display(const char *video_mode, const char *lcd_path,
640 const char *lvds_path)
642 const char *vmode = NULL;
644 void *blob = working_fdt;
646 if (video_mode == NULL || strlen(video_mode) == 0)
649 vmode = strchr(video_mode, ':');
651 if (lvds_path == NULL)
652 return vmode ? vmode + 1 : video_mode;
654 if (lvds_path != NULL && vmode != NULL) {
655 if (strncmp(video_mode, "LVDS:", 5) == 0 ||
656 strncmp(video_mode, "LVDS0:", 6) == 0) {
657 ret = karo_fdt_set_display_alias(blob, lvds_path,
659 } else if (strncmp(video_mode, "LVDS1:", 6) == 0) {
660 ret = karo_fdt_set_display_alias(blob, lvds_path,
663 debug("%s: Syntax error in video_mode\n", __func__);
666 video_mode = vmode + 1;
670 ret = karo_fdt_set_display_alias(blob, lcd_path,
673 off = fdt_path_offset(blob, "lvds0");
675 ret = fdt_set_node_status(blob, off,
676 FDT_STATUS_DISABLED, 0);
678 off = fdt_path_offset(blob, "lvds1");
680 ret = fdt_set_node_status(blob, off,
681 FDT_STATUS_DISABLED, 0);
685 printf("%s: failed to set 'display' alias: %s\n",
686 __func__, fdt_strerror(ret));
691 int karo_fdt_update_fb_mode(void *blob, const char *name)
693 int off = fdt_path_offset(blob, "display");
694 const char *subnode = "display-timings";
702 debug("Disabling node '%s' at %03x\n",
703 fdt_get_name(blob, off, NULL), off);
704 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
708 off = fdt_subnode_offset(blob, off, subnode);
710 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
714 const char *n, *endp;
717 off = fdt_next_node(blob, off, &d);
723 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
726 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
728 n = fdt_getprop(blob, off, "panel-name", &len);
730 n = fdt_get_name(blob, off, NULL);
731 if (strcasecmp(n, name) == 0) {
737 for (endp = n + len; n < endp; n += strlen(n) + 1) {
738 debug("Checking panel-name '%s'\n", n);
739 if (strcasecmp(n, name) == 0) {
740 debug("Using node %s @ %04x\n",
741 fdt_get_name(blob, off, NULL), off);
751 return fdt_update_native_fb_mode(blob, off);
755 static int karo_load_part(const char *part, void *addr, size_t len)
758 struct mtd_device *dev;
759 struct part_info *part_info;
763 debug("Initializing mtd_parts\n");
764 ret = mtdparts_init();
768 debug("Trying to find NAND partition '%s'\n", part);
769 ret = find_dev_and_part(part, &dev, &part_num, &part_info);
771 printf("Failed to find flash partition '%s': %d\n",
776 debug("Found partition '%s': offset=%08x size=%08x\n",
777 part, part_info->offset, part_info->size);
778 if (part_info->size < len) {
779 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
780 part, len, part_info->size);
781 len = part_info->size;
783 debug("Reading NAND partition '%s' to %p\n", part, addr);
784 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
787 printf("Failed to load partition '%s' to %p\n", part, addr);
791 printf("Read only %u of %u bytes due to bad blocks\n",
794 debug("Read %u byte from partition '%s' @ offset %08x\n",
795 len, part, part_info->offset);
799 void *karo_fdt_load_dtb(void)
802 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
805 debug("aborting DTB load\n");
809 /* clear FDT header in memory */
812 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
814 printf("Failed to load dtb from flash: %d\n", ret);
818 if (fdt_check_header(fdt)) {
819 debug("No valid DTB in flash\n");
822 debug("Using DTB from flash\n");
823 karo_set_fdtsize(fdt);