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 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
245 const uint32_t *phandle;
248 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
252 phandle = fdt_getprop(blob, offset, prop, NULL);
254 ph = fdt32_to_cpu(*phandle);
257 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
258 ret = fdt_delprop(blob, offset, prop);
260 karo_set_fdtsize(blob);
265 offset = fdt_node_offset_by_phandle(blob, ph);
269 debug("Removing node @ %08x\n", offset);
270 fdt_del_node(blob, offset);
271 karo_set_fdtsize(blob);
274 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
276 const uint32_t *prop;
278 memset(fb_mode, 0, sizeof(*fb_mode));
280 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
282 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
284 prop = fdt_getprop(blob, off, "hactive", NULL);
286 fb_mode->xres = fdt32_to_cpu(*prop);
288 prop = fdt_getprop(blob, off, "vactive", NULL);
290 fb_mode->yres = fdt32_to_cpu(*prop);
292 prop = fdt_getprop(blob, off, "hback-porch", NULL);
294 fb_mode->left_margin = fdt32_to_cpu(*prop);
296 prop = fdt_getprop(blob, off, "hsync-len", NULL);
298 fb_mode->hsync_len = fdt32_to_cpu(*prop);
300 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
302 fb_mode->right_margin = fdt32_to_cpu(*prop);
304 prop = fdt_getprop(blob, off, "vback-porch", NULL);
306 fb_mode->upper_margin = fdt32_to_cpu(*prop);
308 prop = fdt_getprop(blob, off, "vsync-len", NULL);
310 fb_mode->vsync_len = fdt32_to_cpu(*prop);
312 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
314 fb_mode->lower_margin = fdt32_to_cpu(*prop);
316 prop = fdt_getprop(blob, off, "hsync-active", NULL);
318 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
320 prop = fdt_getprop(blob, off, "vsync-active", NULL);
322 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
323 #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
324 prop = fdt_getprop(blob, off, "de-active", NULL);
326 fb_mode->sync |= *prop ? 0 : FB_SYNC_OE_LOW_ACT;
328 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
330 fb_mode->sync |= *prop ? 0 : FB_SYNC_CLK_LAT_FALL;
335 static int fdt_update_native_fb_mode(void *blob, int off)
340 ret = fdt_increase_size(blob, 32);
342 printf("Warning: Failed to increase FDT size: %d\n", ret);
344 debug("Creating phandle at offset %d\n", off);
345 ph = fdt_create_phandle(blob, off);
347 printf("Failed to create phandle for video timing\n");
351 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
353 off = fdt_parent_offset(blob, off);
356 debug("parent offset=%06x\n", off);
357 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
359 printf("Failed to set property 'native-mode': %d\n", ret);
360 karo_set_fdtsize(blob);
364 static int karo_fdt_find_video_timings(void *blob)
366 int off = fdt_path_offset(blob, "display");
367 const char *subnode = "display-timings";
370 debug("Could not find node 'display' in FDT: %d\n", off);
374 off = fdt_subnode_offset(blob, off, subnode);
376 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
381 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
383 int off = karo_fdt_find_video_timings(blob);
388 const char *n, *endp;
391 off = fdt_next_node(blob, off, &d);
397 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
400 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
402 n = fdt_getprop(blob, off, "panel-name", &len);
404 n = fdt_get_name(blob, off, NULL);
405 if (strcasecmp(n, name) == 0) {
411 for (endp = n + len; n < endp; n += strlen(n) + 1) {
412 debug("Checking panel-name '%s'\n", n);
413 if (strcasecmp(n, name) == 0) {
414 debug("Using node %s @ %04x\n",
415 fdt_get_name(blob, off, NULL), off);
425 return fdt_init_fb_mode(blob, off, fb_mode);
430 #define SET_FB_PROP(n, v) ({ \
432 ret = fdt_setprop_u32(blob, off, n, v); \
434 printf("Failed to set property %s: %d\n", name, ret); \
440 int karo_fdt_create_fb_mode(void *blob, const char *name,
441 struct fb_videomode *fb_mode)
443 int off = fdt_path_offset(blob, "display");
445 const char *subnode = "display-timings";
448 printf("'display' node not found in FDT\n");
452 ret = fdt_increase_size(blob, 512);
454 printf("Failed to increase FDT size: %d\n", ret);
458 printf("node '%s' offset=%d\n", "display", off);
459 ret = fdt_subnode_offset(blob, off, subnode);
461 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
462 ret = fdt_add_subnode(blob, off, subnode);
464 printf("Failed to add %s subnode: %d\n", subnode, ret);
469 printf("node '%s' offset=%d\n", subnode, ret);
470 ret = fdt_add_subnode(blob, ret, name);
472 printf("Failed to add %s subnode: %d\n", name, ret);
477 ret = SET_FB_PROP("clock-frequency",
478 PICOS2KHZ(fb_mode->pixclock) * 1000);
481 ret = SET_FB_PROP("hactive", fb_mode->xres);
484 ret = SET_FB_PROP("vactive", fb_mode->yres);
487 ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
490 ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
493 ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
496 ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
499 ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
502 ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
505 ret = SET_FB_PROP("hsync-active",
506 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
509 ret = SET_FB_PROP("vsync-active",
510 fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
514 #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
515 ret = SET_FB_PROP("de-active",
516 !(fb_mode->sync & FB_SYNC_OE_LOW_ACT));
519 ret = SET_FB_PROP("pixelclk-active",
520 !(fb_mode->sync & FB_SYNC_CLK_LAT_FALL));
524 /* TODO: make these configurable */
525 ret = SET_FB_PROP("de-active", 1);
528 ret = SET_FB_PROP("pixelclk-active", 1);
533 karo_set_fdtsize(blob);
537 int karo_fdt_update_fb_mode(void *blob, const char *name)
539 int off = fdt_path_offset(blob, "display");
540 const char *subnode = "display-timings";
546 int parent = fdt_parent_offset(blob, off);
550 printf("Failed to find parent of node '%s'\n",
551 fdt_get_name(blob, off, NULL));
554 debug("parent offset=%06x\n", parent);
555 ret = fdt_set_node_status(blob, parent, FDT_STATUS_DISABLED, 0);
559 off = fdt_subnode_offset(blob, off, subnode);
561 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
565 const char *n, *endp;
568 off = fdt_next_node(blob, off, &d);
574 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
577 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
579 n = fdt_getprop(blob, off, "panel-name", &len);
581 n = fdt_get_name(blob, off, NULL);
582 if (strcasecmp(n, name) == 0) {
588 for (endp = n + len; n < endp; n += strlen(n) + 1) {
589 debug("Checking panel-name '%s'\n", n);
590 if (strcasecmp(n, name) == 0) {
591 debug("Using node %s @ %04x\n",
592 fdt_get_name(blob, off, NULL), off);
602 return fdt_update_native_fb_mode(blob, off);
606 static int karo_load_part(const char *part, void *addr, size_t len)
609 struct mtd_device *dev;
610 struct part_info *part_info;
614 debug("Initializing mtd_parts\n");
615 ret = mtdparts_init();
619 debug("Trying to find NAND partition '%s'\n", part);
620 ret = find_dev_and_part(part, &dev, &part_num,
623 printf("Failed to find flash partition '%s': %d\n",
628 debug("Found partition '%s': offset=%08x size=%08x\n",
629 part, part_info->offset, part_info->size);
630 if (part_info->size < len) {
631 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
632 part, len, part_info->size);
633 len = part_info->size;
635 debug("Reading NAND partition '%s' to %p\n", part, addr);
636 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
639 printf("Failed to load partition '%s' to %p\n", part, addr);
643 printf("Read only %u of %u bytes due to bad blocks\n",
646 debug("Read %u byte from partition '%s' @ offset %08x\n",
647 len, part, part_info->offset);
651 void *karo_fdt_load_dtb(void)
654 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
657 debug("aborting DTB load\n");
661 /* clear FDT header in memory */
664 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
666 printf("Failed to load dtb from flash: %d\n", ret);
670 if (fdt_check_header(fdt)) {
671 debug("No valid DTB in flash\n");
674 debug("Using DTB from flash\n");
675 karo_set_fdtsize(fdt);