2 * (C) Copyright 2012 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>
23 #include <linux/list.h>
25 #include <jffs2/load_kernel.h>
29 #ifdef CONFIG_MAX_DTB_SIZE
30 #define MAX_DTB_SIZE CONFIG_MAX_DTB_SIZE
32 #define MAX_DTB_SIZE SZ_64K
35 DECLARE_GLOBAL_DATA_PTR;
37 static void karo_set_fdtsize(void *fdt)
40 size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
42 if (fdtsize == fdt_totalsize(fdt)) {
45 debug("FDT size changed from %u to %u\n",
46 fdtsize, fdt_totalsize(fdt));
48 snprintf(fdt_size, sizeof(fdt_size), "%08x", fdt_totalsize(fdt));
49 setenv("fdtsize", fdt_size);
52 void karo_fdt_move_fdt(void)
55 unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
58 debug("DTB already loaded\n");
63 fdt_addr = CONFIG_SYS_FDT_ADDR;
64 printf("fdtaddr is not set; using default: %08lx\n",
68 fdt = karo_fdt_load_dtb();
70 fdt = (void *)gd->fdt_blob;
72 printf("Compiled in FDT not found\n");
75 debug("Checking FDT header @ %p\n", fdt);
76 if (fdt_check_header(fdt)) {
77 printf("ERROR: No valid DTB found at %p\n", fdt);
80 printf("No DTB in flash; using default DTB\n");
81 debug("Moving FDT from %p..%p to %08lx..%08lx\n",
82 fdt, fdt + fdt_totalsize(fdt) - 1,
83 fdt_addr, fdt_addr + fdt_totalsize(fdt) - 1);
84 memmove((void *)fdt_addr, fdt, fdt_totalsize(fdt));
86 set_working_fdt_addr((void *)fdt_addr);
88 karo_set_fdtsize(fdt);
91 void karo_fdt_remove_node(void *blob, const char *node)
93 int off = fdt_path_offset(blob, node);
96 debug("Removing node '%s' from DT\n", node);
99 printf("Could not find node '%s': %d\n", node, off);
101 ret = fdt_del_node(blob, off);
103 printf("Failed to remove node '%s': %d\n",
106 karo_set_fdtsize(blob);
109 void karo_fdt_enable_node(void *blob, const char *node, int enable)
111 int off = fdt_path_offset(blob, node);
113 debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
115 printf("Could not find node '%s': %d\n", node, off);
118 fdt_set_node_status(blob, off,
119 enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
121 karo_set_fdtsize(blob);
124 static const char *karo_touchpanels[] = {
132 static void fdt_del_tp_node(void *blob, const char *name)
134 int offs = fdt_node_offset_by_compatible(blob, -1, name);
137 debug("node '%s' not found: %d\n", name, offs);
141 debug("Removing node '%s' from DT\n", name);
142 fdt_del_node(blob, offs);
145 void karo_fdt_fixup_touchpanel(void *blob)
148 const char *model = getenv("touchpanel");
150 for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
151 const char *tp = karo_touchpanels[i];
153 if (model != NULL && strcmp(model, tp) == 0)
157 if (strcmp(model, tp) == 0)
159 tp = strchr(tp, ',');
160 if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
163 fdt_del_tp_node(blob, karo_touchpanels[i]);
164 karo_set_fdtsize(blob);
168 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
170 const char *otg_mode = getenv("otg_mode");
175 int disable_phy_pins = 1;
177 debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
179 off = fdt_path_offset(blob, node);
181 debug("Failed to find node %s\n", node);
185 if (otg_mode && (strcmp(otg_mode, "device") == 0 ||
186 strcmp(otg_mode, "gadget") == 0)) {
187 debug("Setting dr_mode to 'peripheral'\n");
188 ret = fdt_setprop_string(blob, off, "dr_mode", "peripheral");
189 } else if (otg_mode && strcmp(otg_mode, "host") == 0) {
190 debug("Setting dr_mode to 'host'\n");
191 ret = fdt_setprop_string(blob, off, "dr_mode", "host");
192 disable_phy_pins = 0;
193 } else if (otg_mode && strcmp(otg_mode, "otg") == 0) {
194 debug("Setting dr_mode to 'host'\n");
195 ret = fdt_setprop_string(blob, off, "dr_mode", "otg");
197 if (otg_mode && strcmp(otg_mode, "none") != 0)
198 printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
204 if ((!disable_phy_pins && !disable_otg) || ret)
208 ret = fdt_setprop_string(blob, off, "status", "disabled");
213 ph = fdt_getprop(blob, off, phy, NULL);
215 printf("Failed to find '%s' phandle in node '%s'\n", phy,
216 fdt_get_name(blob, off, NULL));
220 off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
222 printf("Failed to find '%s' node via phandle %04x\n",
223 phy, fdt32_to_cpu(*ph));
228 debug("Disabling usbphy\n");
229 ret = fdt_setprop_string(blob, off, "status", "disabled");
231 debug("Removing host pins from usbphy\n");
232 ret = fdt_delprop(blob, off, "pinctrl-0");
236 printf("Failed to update usbotg: %d\n", ret);
238 debug("node '%s' updated\n", node);
239 karo_set_fdtsize(blob);
242 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
247 const uint32_t *phandle;
250 offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
254 phandle = fdt_getprop(blob, offset, prop, NULL);
256 ph = fdt32_to_cpu(*phandle);
259 debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
260 ret = fdt_delprop(blob, offset, prop);
262 karo_set_fdtsize(blob);
267 offset = fdt_node_offset_by_phandle(blob, ph);
271 debug("Removing node @ %08x\n", offset);
272 fdt_del_node(blob, offset);
273 karo_set_fdtsize(blob);
276 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
278 const uint32_t *prop;
280 memset(fb_mode, 0, sizeof(*fb_mode));
282 prop = fdt_getprop(blob, off, "clock-frequency", NULL);
284 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
286 prop = fdt_getprop(blob, off, "hactive", NULL);
288 fb_mode->xres = fdt32_to_cpu(*prop);
290 prop = fdt_getprop(blob, off, "vactive", NULL);
292 fb_mode->yres = fdt32_to_cpu(*prop);
294 prop = fdt_getprop(blob, off, "hback-porch", NULL);
296 fb_mode->left_margin = fdt32_to_cpu(*prop);
298 prop = fdt_getprop(blob, off, "hsync-len", NULL);
300 fb_mode->hsync_len = fdt32_to_cpu(*prop);
302 prop = fdt_getprop(blob, off, "hfront-porch", NULL);
304 fb_mode->right_margin = fdt32_to_cpu(*prop);
306 prop = fdt_getprop(blob, off, "vback-porch", NULL);
308 fb_mode->upper_margin = fdt32_to_cpu(*prop);
310 prop = fdt_getprop(blob, off, "vsync-len", NULL);
312 fb_mode->vsync_len = fdt32_to_cpu(*prop);
314 prop = fdt_getprop(blob, off, "vfront-porch", NULL);
316 fb_mode->lower_margin = fdt32_to_cpu(*prop);
318 prop = fdt_getprop(blob, off, "hsync-active", NULL);
320 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
322 prop = fdt_getprop(blob, off, "vsync-active", NULL);
324 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
326 prop = fdt_getprop(blob, off, "de-active", NULL);
328 fb_mode->sync |= *prop ? FB_SYNC_DATA_ENABLE_HIGH_ACT : 0;
330 prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
332 fb_mode->sync |= *prop ? FB_SYNC_DOTCLK_FALLING_ACT : 0;
337 static int fdt_update_native_fb_mode(void *blob, int off)
343 for (i = 1; i < 16; i++) {
344 fdt_set_totalsize(blob, fdt_totalsize(blob) + 8 * 4);
345 karo_set_fdtsize(blob);
346 ph = fdt_create_phandle(blob, off);
351 printf("Failed to create phandle for video timing\n");
355 debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
357 off = fdt_parent_offset(blob, off);
360 debug("parent offset=%06x\n", off);
361 ret = fdt_setprop_cell(blob, off, "native-mode", ph);
365 static int karo_fdt_find_video_timings(void *blob)
367 int off = fdt_path_offset(blob, "display");
368 const char *subnode = "display-timings";
371 debug("Could not find node 'display' in FDT: %d\n", off);
375 off = fdt_subnode_offset(blob, off, subnode);
377 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
382 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
384 int off = karo_fdt_find_video_timings(blob);
389 const char *n, *endp;
392 off = fdt_next_node(blob, off, &d);
394 debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
397 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
398 if (off < 0 || d < 1)
401 n = fdt_getprop(blob, off, "panel-name", &len);
403 printf("Missing 'panel-name' property in node '%s'\n",
404 fdt_get_name(blob, off, NULL));
407 for (endp = n + len; n < endp; n += strlen(n) + 1) {
408 debug("Checking panel-name '%s'\n", n);
409 if (strcasecmp(n, name) == 0) {
410 fdt_init_fb_mode(blob, off, fb_mode);
411 return fdt_update_native_fb_mode(blob, off);
418 int karo_fdt_update_fb_mode(void *blob, const char *name)
420 int off = fdt_path_offset(blob, "display");
421 const char *subnode = "display-timings";
427 int parent = fdt_parent_offset(blob, off);
431 printf("Failed to find parent of node '%s'\n",
432 fdt_get_name(blob, off, NULL));
435 debug("parent offset=%06x\n", parent);
436 ret = fdt_setprop_string(blob, parent, "status", "disabled");
440 off = fdt_subnode_offset(blob, off, subnode);
442 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
445 const char *n, *endp;
447 int node = fdt_next_node(blob, off, &d);
451 debug("Skipping node @ %04x %s depth %d\n", node, fdt_get_name(blob, node, NULL), d);
454 debug("parsing subnode @ %04x %s depth %d\n", node, fdt_get_name(blob, node, NULL), d);
455 if (node < 0 || d < 1)
458 n = fdt_getprop(blob, node, "panel-name", &len);
460 printf("Missing 'panel-name' property in node '%s'\n",
461 fdt_get_name(blob, node, NULL));
465 for (endp = n + len; n < endp; n += strlen(n) + 1) {
466 debug("Checking panel-name '%s'\n", n);
467 if (strcasecmp(n, name) == 0) {
468 debug("Keeping node %s @ %04x\n",
469 fdt_get_name(blob, node, NULL), node);
476 debug("Deleting node %s @ %04x\n",
477 fdt_get_name(blob, node, NULL), node);
478 fdt_del_node(blob, node);
486 static int karo_load_part(const char *part, void *addr, size_t len)
489 struct mtd_device *dev;
490 struct part_info *part_info;
494 debug("Initializing mtd_parts\n");
495 ret = mtdparts_init();
499 debug("Trying to find NAND partition '%s'\n", part);
500 ret = find_dev_and_part(part, &dev, &part_num,
503 printf("Failed to find flash partition '%s': %d\n",
508 debug("Found partition '%s': offset=%08x size=%08x\n",
509 part, part_info->offset, part_info->size);
510 if (part_info->size < len) {
511 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
512 part, len, part_info->size);
513 len = part_info->size;
515 debug("Reading NAND partition '%s' to %p\n", part, addr);
516 ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
519 printf("Failed to load partition '%s' to %p\n", part, addr);
523 printf("Read only %u of %u bytes due to bad blocks\n",
526 debug("Read %u byte from partition '%s' @ offset %08x\n",
527 len, part, part_info->offset);
531 void *karo_fdt_load_dtb(void)
534 void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
537 debug("aborting DTB load\n");
541 /* clear FDT header in memory */
544 ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
546 printf("Failed to load dtb from flash: %d\n", ret);
550 if (fdt_check_header(fdt)) {
551 debug("No valid DTB in flash\n");
554 debug("Using DTB from flash\n");
555 karo_set_fdtsize(fdt);