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 printf("Compiled in FDT not found\n");
76 debug("Checking FDT header @ %p\n", fdt);
77 if (fdt_check_header(fdt)) {
78 printf("ERROR: No valid DTB found at %p\n", fdt);
81 printf("No DTB in flash; using default DTB\n");
82 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
83 fdt, fdt + fdt_totalsize(fdt) - 1,
84 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
85 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
87 set_working_fdt_addr((void *)fdt_addr);
89 karo_set_fdtsize(fdt);
92 void karo_fdt_remove_node(void *blob, const char *node)
94 int off = fdt_path_offset(blob, node);
97 debug("Removing node '%s' from DT\n", node);
100 printf("Could not find node '%s': %d\n", node, off);
102 ret = fdt_del_node(blob, off);
104 printf("Failed to remove node '%s': %d\n",
107 karo_set_fdtsize(blob);
110 void karo_fdt_enable_node(void *blob, const char *node, int enable)
112 int off = fdt_path_offset(blob, node);
114 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
116 printf("Could not find node '%s': %d\n", node, off);
119 fdt_set_node_status(blob, off,
120 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
122 karo_set_fdtsize(blob);
125 static const char *karo_touchpanels[] = {
133 static void fdt_disable_tp_node(void *blob, const char *name)
135 int offs = fdt_node_offset_by_compatible(blob, -1, name);
138 debug("node '%s' not found: %d\n", name, offs);
142 debug("Disabling node '%s'\n", name);
143 fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
146 void karo_fdt_fixup_touchpanel(void *blob)
149 const char *model = getenv("touchpanel");
151 for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
152 const char *tp = karo_touchpanels[i];
154 if (model != NULL && strcmp(model, tp) == 0)
158 if (strcmp(model, tp) == 0)
160 tp = strchr(tp, ',');
161 if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
164 fdt_disable_tp_node(blob, karo_touchpanels[i]);
166 karo_set_fdtsize(blob);
169 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
171 const char *otg_mode = getenv("otg_mode");
176 int disable_phy_pins = 1;
178 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
180 off = fdt_path_offset(blob, node);
182 debug("Failed to find node %s\n", node);
186 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
187 strcmp(otg_mode, "gadget") == 0)) {
188 debug("Setting dr_mode to 'peripheral'\n");
189 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
190 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
191 debug("Setting dr_mode to 'host'\n");
192 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
193 disable_phy_pins = 0;
194 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
195 debug("Setting dr_mode to 'host'\n");
196 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
198 if (otg_mode && strcmp(otg_mode, "none") != 0)
199 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
205 if ((!disable_phy_pins && !disable_otg) || ret)
209 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
214 ph = fdt_getprop(blob, off, phy, NULL);
216 printf("Failed to find '%s' phandle in node '%s'\n", phy,
217 fdt_get_name(blob, off, NULL));
221 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
223 printf("Failed to find '%s' node via phandle %04x\n",
224 phy, fdt32_to_cpu(*ph));
229 debug("Disabling usbphy\n");
230 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
234 printf("Failed to update usbotg: %d\n", ret);
236 debug("node '%s' updated\n", node);
237 karo_set_fdtsize(blob);
240 static int karo_fdt_flexcan_enabled(void *blob)
242 const char *can_ifs[] = {
248 for (i = 0; i < ARRAY_SIZE(can_ifs); i++) {
250 int off = fdt_path_offset(blob, can_ifs[i]);
253 debug("node '%s' not found\n", can_ifs[i]);
256 status = fdt_getprop(blob, off, "status", NULL);
257 if (strcmp(status, "okay") == 0) {
258 debug("%s is enabled\n", can_ifs[i]);
262 debug("can driver is disabled\n");
266 static void karo_fdt_set_lcd_pins(void *blob, const char *name)
268 int off = fdt_path_offset(blob, name);
270 const struct fdt_property *pc;
276 ph = fdt_create_phandle(blob, off);
280 off = fdt_path_offset(blob, "display");
284 pc = fdt_get_property(blob, off, "pinctrl-0", &len);
285 if (!pc || len < sizeof(ph))
288 memcpy((void *)pc->data, &ph, sizeof(ph));
289 fdt_setprop_cell(blob, off, "pinctrl-0", ph);
292 void karo_fdt_fixup_flexcan(void *blob, int xcvr_present)
294 const char *xcvr_status = "disabled";
297 if (karo_fdt_flexcan_enabled(blob)) {
298 karo_fdt_set_lcd_pins(blob, "lcdif_23bit_pins_a");
299 xcvr_status = "okay";
301 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
304 const char *otg_mode = getenv("otg_mode");
306 if (otg_mode && (strcmp(otg_mode, "host") == 0))
307 karo_fdt_enable_node(blob, "can1", 0);
309 karo_fdt_set_lcd_pins(blob, "lcdif_24bit_pins_a");
311 fdt_find_and_setprop(blob, "/regulators/can-xcvr", "status",
312 xcvr_status, strlen(xcvr_status) + 1, 1);
315 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
320 const uint32_t *phandle;
323 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
327 phandle = fdt_getprop(blob, offset, prop, NULL);
329 ph = fdt32_to_cpu(*phandle);
332 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
333 ret = fdt_delprop(blob, offset, prop);
335 karo_set_fdtsize(blob);
340 offset = fdt_node_offset_by_phandle(blob, ph);
344 debug("Removing node @ %08x\n", offset);
345 fdt_del_node(blob, offset);
346 karo_set_fdtsize(blob);
349 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
351 const uint32_t *prop;
353 memset(fb_mode, 0, sizeof(*fb_mode));
355 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
357 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
359 prop = fdt_getprop(blob, off, "hactive", NULL);
361 fb_mode->xres = fdt32_to_cpu(*prop);
363 prop = fdt_getprop(blob, off, "vactive", NULL);
365 fb_mode->yres = fdt32_to_cpu(*prop);
367 prop = fdt_getprop(blob, off, "hback-porch", NULL);
369 fb_mode->left_margin = fdt32_to_cpu(*prop);
371 prop = fdt_getprop(blob, off, "hsync-len", NULL);
373 fb_mode->hsync_len = fdt32_to_cpu(*prop);
375 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
377 fb_mode->right_margin = fdt32_to_cpu(*prop);
379 prop = fdt_getprop(blob, off, "vback-porch", NULL);
381 fb_mode->upper_margin = fdt32_to_cpu(*prop);
383 prop = fdt_getprop(blob, off, "vsync-len", NULL);
385 fb_mode->vsync_len = fdt32_to_cpu(*prop);
387 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
389 fb_mode->lower_margin = fdt32_to_cpu(*prop);
391 prop = fdt_getprop(blob, off, "hsync-active", NULL);
393 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
395 prop = fdt_getprop(blob, off, "vsync-active", NULL);
397 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
398 #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
399 prop = fdt_getprop(blob, off, "de-active", NULL);
401 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
403 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
405 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
410 static int fdt_update_native_fb_mode(void *blob, int off)
415 ret = fdt_increase_size(blob, 64);
417 printf("Warning: Failed to increase FDT size: %d\n", ret);
419 debug("Creating phandle at offset %d\n", off);
420 ph = fdt_create_phandle(blob, off);
422 printf("Failed to create phandle for video timing\n");
426 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
428 off = fdt_parent_offset(blob, off);
431 debug("parent offset=%06x\n", off);
432 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
434 printf("Failed to set property 'native-mode': %d\n", ret);
435 karo_set_fdtsize(blob);
439 static int karo_fdt_find_video_timings(void *blob)
441 int off = fdt_path_offset(blob, "display");
442 const char *subnode = "display-timings";
445 debug("Could not find node 'display' in FDT: %d\n", off);
449 off = fdt_subnode_offset(blob, off, subnode);
451 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
456 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
458 int off = karo_fdt_find_video_timings(blob);
463 const char *n, *endp;
466 off = fdt_next_node(blob, off, &d);
472 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
475 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
477 n = fdt_getprop(blob, off, "panel-name", &len);
479 n = fdt_get_name(blob, off, NULL);
480 if (strcasecmp(n, name) == 0) {
486 for (endp = n + len; n < endp; n += strlen(n) + 1) {
487 debug("Checking panel-name '%s'\n", n);
488 if (strcasecmp(n, name) == 0) {
489 debug("Using node %s @ %04x\n",
490 fdt_get_name(blob, off, NULL), off);
500 return fdt_init_fb_mode(blob, off, fb_mode);
505 #define SET_FB_PROP(n, v) ({ \
507 ret = fdt_setprop_u32(blob, off, n, v); \
509 printf("Failed to set property %s: %d\n", name, ret); \
515 int karo_fdt_create_fb_mode(void *blob, const char *name,
516 struct fb_videomode *fb_mode)
518 int off = fdt_path_offset(blob, "display");
520 const char *subnode = "display-timings";
523 printf("'display' node not found in FDT\n");
527 ret = fdt_increase_size(blob, 512);
529 printf("Failed to increase FDT size: %d\n", ret);
533 printf("node '%s' offset=%d\n", "display", off);
534 ret = fdt_subnode_offset(blob, off, subnode);
536 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
537 ret = fdt_add_subnode(blob, off, subnode);
539 printf("Failed to add %s subnode: %d\n", subnode, ret);
544 printf("node '%s' offset=%d\n", subnode, ret);
545 ret = fdt_add_subnode(blob, ret, name);
547 printf("Failed to add %s subnode: %d\n", name, ret);
552 ret = SET_FB_PROP("clock-frequency",
553 PICOS2KHZ(fb_mode->pixclock) * 1000);
556 ret = SET_FB_PROP("hactive", fb_mode->xres);
559 ret = SET_FB_PROP("vactive", fb_mode->yres);
562 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
565 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
568 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
571 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
574 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
577 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
580 ret = SET_FB_PROP("hsync-active",
581 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
584 ret = SET_FB_PROP("vsync-active",
585 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
589 #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
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));
599 /* TODO: make these configurable */
600 ret = SET_FB_PROP("de-active", 1);
603 ret = SET_FB_PROP("pixelclk-active", 1);
608 karo_set_fdtsize(blob);
612 int karo_fdt_update_fb_mode(void *blob, const char *name)
614 int off = fdt_path_offset(blob, "display");
615 const char *subnode = "display-timings";
623 debug("Disabling node '%s' at %03x\n",
624 fdt_get_name(blob, off, NULL), off);
625 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
629 off = fdt_subnode_offset(blob, off, subnode);
631 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
635 const char *n, *endp;
638 off = fdt_next_node(blob, off, &d);
644 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
647 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
649 n = fdt_getprop(blob, off, "panel-name", &len);
651 n = fdt_get_name(blob, off, NULL);
652 if (strcasecmp(n, name) == 0) {
658 for (endp = n + len; n < endp; n += strlen(n) + 1) {
659 debug("Checking panel-name '%s'\n", n);
660 if (strcasecmp(n, name) == 0) {
661 debug("Using node %s @ %04x\n",
662 fdt_get_name(blob, off, NULL), off);
672 return fdt_update_native_fb_mode(blob, off);
676 static int karo_load_part(const char *part, void *addr, size_t len)
679 struct mtd_device *dev;
680 struct part_info *part_info;
684 debug("Initializing mtd_parts\n");
685 ret = mtdparts_init();
689 debug("Trying to find NAND partition '%s'\n", part);
690 ret = find_dev_and_part(part, &dev, &part_num,
693 printf("Failed to find flash partition '%s': %d\n",
698 debug("Found partition '%s': offset=%08x size=%08x\n",
699 part, part_info->offset, part_info->size);
700 if (part_info->size < len) {
701 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
702 part, len, part_info->size);
703 len = part_info->size;
705 debug("Reading NAND partition '%s' to %p\n", part, addr);
706 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
709 printf("Failed to load partition '%s' to %p\n", part, addr);
713 printf("Read only %u of %u bytes due to bad blocks\n",
716 debug("Read %u byte from partition '%s' @ offset %08x\n",
717 len, part, part_info->offset);
721 void *karo_fdt_load_dtb(void)
724 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
727 debug("aborting DTB load\n");
731 /* clear FDT header in memory */
734 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
736 printf("Failed to load dtb from flash: %d\n", ret);
740 if (fdt_check_header(fdt)) {
741 debug("No valid DTB in flash\n");
744 debug("Using DTB from flash\n");
745 karo_set_fdtsize(fdt);