]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/common/fdt.c
prevent error message 'Can't set phandle'
[karo-tx-uboot.git] / board / karo / common / fdt.c
1 /*
2  * (C) Copyright 2012,2013 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
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.
10  *
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.
15  *
16 */
17
18 #include <common.h>
19 #include <errno.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <nand.h>
23 #include <linux/list.h>
24 #include <linux/fb.h>
25 #include <jffs2/load_kernel.h>
26
27 #include "karo.h"
28
29 #ifdef CONFIG_MAX_DTB_SIZE
30 #define MAX_DTB_SIZE    CONFIG_MAX_DTB_SIZE
31 #else
32 #define MAX_DTB_SIZE    SZ_64K
33 #endif
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 static void karo_set_fdtsize(void *fdt)
38 {
39         char fdt_size[9];
40         size_t fdtsize = getenv_ulong("fdtsize", 16, 0);
41
42         if (fdtsize == fdt_totalsize(fdt)) {
43                 return;
44         }
45         debug("FDT size changed from %u to %u\n",
46                 fdtsize, fdt_totalsize(fdt));
47
48         snprintf(fdt_size, sizeof(fdt_size), "%08x", fdt_totalsize(fdt));
49         setenv("fdtsize", fdt_size);
50 }
51
52 void karo_fdt_move_fdt(void)
53 {
54         void *fdt;
55         unsigned long fdt_addr = getenv_ulong("fdtaddr", 16, 0);
56
57         if (working_fdt) {
58                 debug("DTB already loaded\n");
59                 return;
60         }
61
62         if (!fdt_addr) {
63                 fdt_addr = CONFIG_SYS_FDT_ADDR;
64                 printf("fdtaddr is not set; using default: %08lx\n",
65                         fdt_addr);
66         }
67
68         fdt = karo_fdt_load_dtb();
69         if (fdt == NULL) {
70                 fdt = (void *)gd->fdt_blob;
71                 if (fdt == NULL) {
72                         printf("Compiled in FDT not found\n");
73                         return;
74                 }
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);
78                         return;
79                 }
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));
85         }
86         set_working_fdt_addr((void *)fdt_addr);
87         gd->fdt_blob = fdt;
88         karo_set_fdtsize(fdt);
89 }
90
91 void karo_fdt_remove_node(void *blob, const char *node)
92 {
93         int off = fdt_path_offset(blob, node);
94         int ret;
95
96         debug("Removing node '%s' from DT\n", node);
97
98         if (off < 0) {
99                 printf("Could not find node '%s': %d\n", node, off);
100         } else {
101                 ret = fdt_del_node(blob, off);
102                 if (ret)
103                         printf("Failed to remove node '%s': %d\n",
104                                 node, ret);
105         }
106         karo_set_fdtsize(blob);
107 }
108
109 void karo_fdt_enable_node(void *blob, const char *node, int enable)
110 {
111         int off = fdt_path_offset(blob, node);
112
113         debug("%sabling node '%s'\n", enable ? "En" : "Dis", node);
114         if (off < 0) {
115                 printf("Could not find node '%s': %d\n", node, off);
116                 return;
117         }
118         fdt_set_node_status(blob, off,
119                         enable ? FDT_STATUS_OKAY : FDT_STATUS_DISABLED, 0);
120
121         karo_set_fdtsize(blob);
122 }
123
124 static const char *karo_touchpanels[] = {
125         "ti,tsc2007",
126         "edt,edt-ft5x06",
127 #ifdef CONFIG_MX28
128         "fsl,imx28-lradc",
129 #endif
130 };
131
132 static void fdt_disable_tp_node(void *blob, const char *name)
133 {
134         int offs = fdt_node_offset_by_compatible(blob, -1, name);
135
136         if (offs < 0) {
137                 debug("node '%s' not found: %d\n", name, offs);
138                 return;
139         }
140
141         debug("Disabling node '%s'\n", name);
142         fdt_set_node_status(blob, offs, FDT_STATUS_DISABLED, 0);
143 }
144
145 void karo_fdt_fixup_touchpanel(void *blob)
146 {
147         int i;
148         const char *model = getenv("touchpanel");
149
150         for (i = 0; i < ARRAY_SIZE(karo_touchpanels); i++) {
151                 const char *tp = karo_touchpanels[i];
152
153                 if (model != NULL && strcmp(model, tp) == 0)
154                         continue;
155
156                 if (model != NULL) {
157                         if (strcmp(model, tp) == 0)
158                                 continue;
159                         tp = strchr(tp, ',');
160                         if (tp != NULL && *tp != '\0' && strcmp(model, tp + 1) == 0)
161                                 continue;
162                 }
163                 fdt_disable_tp_node(blob, karo_touchpanels[i]);
164         }
165         karo_set_fdtsize(blob);
166 }
167
168 void karo_fdt_fixup_usb_otg(void *blob, const char *node, const char *phy)
169 {
170         const char *otg_mode = getenv("otg_mode");
171         int off;
172         int ret;
173         const uint32_t *ph;
174         int disable_otg = 0;
175         int disable_phy_pins = 1;
176
177         debug("OTG mode is '%s'\n", otg_mode ? otg_mode : "<UNSET>");
178
179         off = fdt_path_offset(blob, node);
180         if (off < 0) {
181                 debug("Failed to find node %s\n", node);
182                 return;
183         }
184
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");
196         } else {
197                 if (otg_mode && strcmp(otg_mode, "none") != 0)
198                         printf("Invalid 'otg_mode' setting '%s'; disabling usbotg port\n",
199                                 otg_mode);
200                 disable_otg = 1;
201                 ret = 0;
202         }
203
204         if ((!disable_phy_pins && !disable_otg) || ret)
205                 goto out;
206
207         if (disable_otg) {
208                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
209                 if (ret)
210                         goto out;
211         }
212
213         ph = fdt_getprop(blob, off, phy, NULL);
214         if (ph == NULL) {
215                 printf("Failed to find '%s' phandle in node '%s'\n", phy,
216                         fdt_get_name(blob, off, NULL));
217                 goto out;
218         }
219
220         off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*ph));
221         if (off <= 0) {
222                 printf("Failed to find '%s' node via phandle %04x\n",
223                         phy, fdt32_to_cpu(*ph));
224                 goto out;
225         }
226
227         if (disable_otg) {
228                 debug("Disabling usbphy\n");
229                 ret = fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
230         }
231 out:
232         if (ret)
233                 printf("Failed to update usbotg: %d\n", ret);
234         else
235                 debug("node '%s' updated\n", node);
236         karo_set_fdtsize(blob);
237 }
238
239 void karo_fdt_del_prop(void *blob, const char *compat, phys_addr_t offs,
240                         const char *prop)
241 {
242         int ret;
243         int offset;
244         const uint32_t *phandle;
245         uint32_t ph = 0;
246
247         offset = fdt_node_offset_by_compat_reg(blob, compat, offs);
248         if (offset <= 0)
249                 return;
250
251         phandle = fdt_getprop(blob, offset, prop, NULL);
252         if (phandle) {
253                 ph = fdt32_to_cpu(*phandle);
254         }
255
256         debug("Removing property '%s' from node %s@%08lx\n", prop, compat, offs);
257         ret = fdt_delprop(blob, offset, prop);
258         if (ret == 0)
259                 karo_set_fdtsize(blob);
260
261         if (!ph)
262                 return;
263
264         offset = fdt_node_offset_by_phandle(blob, ph);
265         if (offset <= 0)
266                 return;
267
268         debug("Removing node @ %08x\n", offset);
269         fdt_del_node(blob, offset);
270         karo_set_fdtsize(blob);
271 }
272
273 static int fdt_init_fb_mode(const void *blob, int off, struct fb_videomode *fb_mode)
274 {
275         const uint32_t *prop;
276
277         memset(fb_mode, 0, sizeof(*fb_mode));
278
279         prop = fdt_getprop(blob, off, "clock-frequency", NULL);
280         if (prop)
281                 fb_mode->pixclock = KHZ2PICOS(fdt32_to_cpu(*prop) / 1000);
282
283         prop = fdt_getprop(blob, off, "hactive", NULL);
284         if (prop)
285                 fb_mode->xres = fdt32_to_cpu(*prop);
286
287         prop = fdt_getprop(blob, off, "vactive", NULL);
288         if (prop)
289                 fb_mode->yres = fdt32_to_cpu(*prop);
290
291         prop = fdt_getprop(blob, off, "hback-porch", NULL);
292         if (prop)
293                 fb_mode->left_margin = fdt32_to_cpu(*prop);
294
295         prop = fdt_getprop(blob, off, "hsync-len", NULL);
296         if (prop)
297                 fb_mode->hsync_len = fdt32_to_cpu(*prop);
298
299         prop = fdt_getprop(blob, off, "hfront-porch", NULL);
300         if (prop)
301                 fb_mode->right_margin = fdt32_to_cpu(*prop);
302
303         prop = fdt_getprop(blob, off, "vback-porch", NULL);
304         if (prop)
305                 fb_mode->upper_margin = fdt32_to_cpu(*prop);
306
307         prop = fdt_getprop(blob, off, "vsync-len", NULL);
308         if (prop)
309                 fb_mode->vsync_len = fdt32_to_cpu(*prop);
310
311         prop = fdt_getprop(blob, off, "vfront-porch", NULL);
312         if (prop)
313                 fb_mode->lower_margin = fdt32_to_cpu(*prop);
314
315         prop = fdt_getprop(blob, off, "hsync-active", NULL);
316         if (prop)
317                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
318
319         prop = fdt_getprop(blob, off, "vsync-active", NULL);
320         if (prop)
321                 fb_mode->sync |= *prop ? FB_SYNC_VERT_HIGH_ACT : 0;
322 #if 0
323         prop = fdt_getprop(blob, off, "de-active", NULL);
324         if (prop)
325                 fb_mode->sync |= *prop ? FB_SYNC_DATA_ENABLE_HIGH_ACT : 0;
326
327         prop = fdt_getprop(blob, off, "pixelclk-active", NULL);
328         if (prop)
329                 fb_mode->sync |= *prop ? FB_SYNC_DOTCLK_FALLING_ACT : 0;
330 #endif
331         return 0;
332 }
333
334 static int fdt_update_native_fb_mode(void *blob, int off)
335 {
336         int ret;
337         uint32_t ph;
338
339         ret = fdt_increase_size(blob, 32);
340         if (ret) {
341                 printf("Warning: Failed to increase FDT size: %d\n", ret);
342         }
343         debug("Creating phandle at offset %d\n", off);
344         ph = fdt_create_phandle(blob, off);
345         if (!ph) {
346                 printf("Failed to create phandle for video timing\n");
347                 return -ENOMEM;
348         }
349
350         debug("phandle of %s @ %06x=%04x\n", fdt_get_name(blob, off, NULL),
351                 off, ph);
352         off = fdt_parent_offset(blob, off);
353         if (off < 0)
354                 return off;
355         debug("parent offset=%06x\n", off);
356         ret = fdt_setprop_cell(blob, off, "native-mode", ph);
357         if (ret)
358                 printf("Failed to set property 'native-mode': %d\n", ret);
359         karo_set_fdtsize(blob);
360         return ret;
361 }
362
363 static int karo_fdt_find_video_timings(void *blob)
364 {
365         int off = fdt_path_offset(blob, "display");
366         const char *subnode = "display-timings";
367
368         if (off < 0) {
369                 debug("Could not find node 'display' in FDT: %d\n", off);
370                 return off;
371         }
372
373         off = fdt_subnode_offset(blob, off, subnode);
374         if (off < 0) {
375                 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
376         }
377         return off;
378 }
379
380 int karo_fdt_get_fb_mode(void *blob, const char *name, struct fb_videomode *fb_mode)
381 {
382         int off = karo_fdt_find_video_timings(blob);
383
384         if (off < 0)
385                 return off;
386         while (off > 0) {
387                 const char *n, *endp;
388                 int len, d = 1;
389
390                 off = fdt_next_node(blob, off, &d);
391                 if (off < 0)
392                         return off;
393                 if (d < 1)
394                         return -EINVAL;
395                 if (d > 2) {
396                         debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
397                         continue;
398                 }
399                 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
400
401                 n = fdt_getprop(blob, off, "panel-name", &len);
402                 if (!n) {
403                         n = fdt_get_name(blob, off, NULL);
404                         if (strcasecmp(n, name) == 0) {
405                                 break;
406                         }
407                 } else {
408                         int found = 0;
409
410                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
411                                 debug("Checking panel-name '%s'\n", n);
412                                 if (strcasecmp(n, name) == 0) {
413                                         debug("Using node %s @ %04x\n",
414                                                 fdt_get_name(blob, off, NULL), off);
415                                         found = 1;
416                                         break;
417                                 }
418                         }
419                         if (found)
420                                 break;
421                 }
422         }
423         if (off > 0) {
424                 fdt_init_fb_mode(blob, off, fb_mode);
425                 return fdt_update_native_fb_mode(blob, off);
426         }
427         return -EINVAL;
428 }
429
430 #define SET_FB_PROP(n, v) ({                                            \
431         int ret;                                                        \
432         ret = fdt_setprop_u32(blob, off, n, v);                         \
433         if (ret) {                                                      \
434                 printf("Failed to set property %s: %d\n", name, ret);   \
435         }                                                               \
436         ret;                                                            \
437 })
438
439
440 int karo_fdt_create_fb_mode(void *blob, const char *name,
441                         struct fb_videomode *fb_mode)
442 {
443         int off = fdt_path_offset(blob, "display");
444         int ret;
445         const char *subnode = "display-timings";
446
447         if (off < 0) {
448                 printf("'display' node not found in FDT\n");
449                 return off;
450         }
451
452         ret = fdt_increase_size(blob, 512);
453         if (ret) {
454                 printf("Failed to increase FDT size: %d\n", ret);
455                 return ret;
456         }
457
458         printf("node '%s' offset=%d\n", "display", off);
459         ret = fdt_subnode_offset(blob, off, subnode);
460         if (ret < 0) {
461                 debug("Could not find node '%s' in FDT: %d\n", subnode, ret);
462                 ret = fdt_add_subnode(blob, off, subnode);
463                 if (ret < 0) {
464                         printf("Failed to add %s subnode: %d\n", subnode, ret);
465                         return ret;
466                 }
467         }
468
469         printf("node '%s' offset=%d\n", subnode, ret);
470         ret = fdt_add_subnode(blob, ret, name);
471         if (ret < 0) {
472                 printf("Failed to add %s subnode: %d\n", name, ret);
473                 return ret;
474         }
475         off = ret;
476
477         ret = SET_FB_PROP("clock-frequency",
478                         PICOS2KHZ(fb_mode->pixclock) * 1000);
479         if (ret)
480                 goto out;
481         ret = SET_FB_PROP("hactive", fb_mode->xres);
482         if (ret)
483                 goto out;
484         ret = SET_FB_PROP("vactive", fb_mode->yres);
485         if (ret)
486                 goto out;
487         ret = SET_FB_PROP("hback-porch", fb_mode->left_margin);
488         if (ret)
489                 goto out;
490         ret = SET_FB_PROP("hsync-len", fb_mode->hsync_len);
491         if (ret)
492                 goto out;
493         ret = SET_FB_PROP("hfront-porch", fb_mode->right_margin);
494         if (ret)
495                 goto out;
496         ret = SET_FB_PROP("vback-porch", fb_mode->upper_margin);
497         if (ret)
498                 goto out;
499         ret = SET_FB_PROP("vsync-len", fb_mode->vsync_len);
500         if (ret)
501                 goto out;
502         ret = SET_FB_PROP("vfront-porch", fb_mode->lower_margin);
503         if (ret)
504                 goto out;
505         ret = SET_FB_PROP("hsync-active",
506                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
507         if (ret)
508                 goto out;
509         ret = SET_FB_PROP("vsync-active",
510                         fb_mode->sync & FB_SYNC_VERT_HIGH_ACT ? 1 : 0);
511         if (ret)
512                 goto out;
513
514         /* TOTO: make these configurable */
515         ret = SET_FB_PROP("de-active", 1);
516         if (ret)
517                 goto out;
518         ret = SET_FB_PROP("pixelclk-active", 1);
519         if (ret)
520                 goto out;
521
522         return fdt_update_native_fb_mode(blob, off);
523
524 out:
525         karo_set_fdtsize(blob);
526         return ret;
527 }
528
529 int karo_fdt_update_fb_mode(void *blob, const char *name)
530 {
531         int off = fdt_path_offset(blob, "display");
532         const char *subnode = "display-timings";
533
534         if (off < 0)
535                 return off;
536
537         if (name == NULL) {
538                 int parent = fdt_parent_offset(blob, off);
539                 int ret;
540
541                 if (parent < 0) {
542                         printf("Failed to find parent of node '%s'\n",
543                                 fdt_get_name(blob, off, NULL));
544                         return parent;
545                 }
546                 debug("parent offset=%06x\n", parent);
547                 ret = fdt_set_node_status(blob, parent, FDT_STATUS_DISABLED, 0);
548                 return ret;
549         }
550
551         off = fdt_subnode_offset(blob, off, subnode);
552         if (off < 0) {
553                 debug("Could not find node '%s' in FDT: %d\n", subnode, off);
554                 return off;
555         }
556         while (off > 0) {
557                 const char *n, *endp;
558                 int len, d = 1;
559
560                 off = fdt_next_node(blob, off, &d);
561                 if (off < 0)
562                         return off;
563                 if (d < 1)
564                         return -EINVAL;
565                 if (d > 2) {
566                         debug("Skipping node @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
567                         continue;
568                 }
569                 debug("parsing subnode @ %04x %s depth %d\n", off, fdt_get_name(blob, off, NULL), d);
570
571                 n = fdt_getprop(blob, off, "panel-name", &len);
572                 if (!n) {
573                         n = fdt_get_name(blob, off, NULL);
574                         if (strcasecmp(n, name) == 0) {
575                                 break;
576                         }
577                 } else {
578                         int found = 0;
579
580                         for (endp = n + len; n < endp; n += strlen(n) + 1) {
581                                 debug("Checking panel-name '%s'\n", n);
582                                 if (strcasecmp(n, name) == 0) {
583                                         debug("Using node %s @ %04x\n",
584                                                 fdt_get_name(blob, off, NULL), off);
585                                         found = 1;
586                                         break;
587                                 }
588                         }
589                         if (found)
590                                 break;
591                 }
592         }
593         if (off > 0)
594                 return fdt_update_native_fb_mode(blob, off);
595         return off;
596 }
597
598 static int karo_load_part(const char *part, void *addr, size_t len)
599 {
600         int ret;
601         struct mtd_device *dev;
602         struct part_info *part_info;
603         u8 part_num;
604         size_t actual;
605
606         debug("Initializing mtd_parts\n");
607         ret = mtdparts_init();
608         if (ret)
609                 return ret;
610
611         debug("Trying to find NAND partition '%s'\n", part);
612         ret = find_dev_and_part(part, &dev, &part_num,
613                                 &part_info);
614         if (ret) {
615                 printf("Failed to find flash partition '%s': %d\n",
616                         part, ret);
617
618                 return ret;
619         }
620         debug("Found partition '%s': offset=%08x size=%08x\n",
621                 part, part_info->offset, part_info->size);
622         if (part_info->size < len) {
623                 printf("Warning: partition '%s' smaller than requested size: %u; truncating data to %u byte\n",
624                         part, len, part_info->size);
625                 len = part_info->size;
626         }
627         debug("Reading NAND partition '%s' to %p\n", part, addr);
628         ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
629                                 &actual, len, addr);
630         if (ret) {
631                 printf("Failed to load partition '%s' to %p\n", part, addr);
632                 return ret;
633         }
634         if (actual < len)
635                 printf("Read only %u of %u bytes due to bad blocks\n",
636                         actual, len);
637
638         debug("Read %u byte from partition '%s' @ offset %08x\n",
639                 len, part, part_info->offset);
640         return 0;
641 }
642
643 void *karo_fdt_load_dtb(void)
644 {
645         int ret;
646         void *fdt = (void *)getenv_ulong("fdtaddr", 16, CONFIG_SYS_FDT_ADDR);
647
648         if (tstc()) {
649                 debug("aborting DTB load\n");
650                 return NULL;
651         }
652
653         /* clear FDT header in memory */
654         memset(fdt, 0, 4);
655
656         ret = karo_load_part("dtb", fdt, MAX_DTB_SIZE);
657         if (ret) {
658                 printf("Failed to load dtb from flash: %d\n", ret);
659                 return NULL;
660         }
661
662         if (fdt_check_header(fdt)) {
663                 debug("No valid DTB in flash\n");
664                 return NULL;
665         }
666         debug("Using DTB from flash\n");
667         karo_set_fdtsize(fdt);
668         return fdt;
669 }