]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib/fdtdec.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / lib / fdtdec.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
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  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include <common.h>
23 #include <serial.h>
24 #include <libfdt.h>
25 #include <fdtdec.h>
26
27 #include <asm/gpio.h>
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 /*
32  * Here are the type we know about. One day we might allow drivers to
33  * register. For now we just put them here. The COMPAT macro allows us to
34  * turn this into a sparse list later, and keeps the ID with the name.
35  */
36 #define COMPAT(id, name) name
37 static const char * const compat_names[COMPAT_COUNT] = {
38         COMPAT(UNKNOWN, "<none>"),
39         COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
40         COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
41         COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
42         COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
43         COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
44         COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
45         COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
46         COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
47         COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
48         COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
49         COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
50         COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
51         COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
52         COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
53         COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
54         COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
55         COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
56         COMPAT(SMSC_LAN9215, "smsc,lan9215"),
57         COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
58         COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
59         COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
60         COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
61         COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
62         COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
63         COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
64         COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
65         COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
66         COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
67         COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
68         COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
69         COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
70         COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
71         COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
72         COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
73         COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
74         COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
75         COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
76 };
77
78 const char *fdtdec_get_compatible(enum fdt_compat_id id)
79 {
80         /* We allow reading of the 'unknown' ID for testing purposes */
81         assert(id >= 0 && id < COMPAT_COUNT);
82         return compat_names[id];
83 }
84
85 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
86                 const char *prop_name, fdt_size_t *sizep)
87 {
88         const fdt_addr_t *cell;
89         int len;
90
91         debug("%s: %s: ", __func__, prop_name);
92         cell = fdt_getprop(blob, node, prop_name, &len);
93         if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
94                      len == sizeof(fdt_addr_t) * 2)) {
95                 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
96                 if (sizep) {
97                         const fdt_size_t *size;
98
99                         size = (fdt_size_t *)((char *)cell +
100                                         sizeof(fdt_addr_t));
101                         *sizep = fdt_size_to_cpu(*size);
102                         debug("addr=%p, size=%p\n", (void *)addr,
103                               (void *)*sizep);
104                 } else {
105                         debug("%p\n", (void *)addr);
106                 }
107                 return addr;
108         }
109         debug("(not found)\n");
110         return FDT_ADDR_T_NONE;
111 }
112
113 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
114                 const char *prop_name)
115 {
116         return fdtdec_get_addr_size(blob, node, prop_name, NULL);
117 }
118
119 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
120                 s32 default_val)
121 {
122         const s32 *cell;
123         int len;
124
125         debug("%s: %s: ", __func__, prop_name);
126         cell = fdt_getprop(blob, node, prop_name, &len);
127         if (cell && len >= sizeof(s32)) {
128                 s32 val = fdt32_to_cpu(cell[0]);
129
130                 debug("%#x (%d)\n", val, val);
131                 return val;
132         }
133         debug("(not found)\n");
134         return default_val;
135 }
136
137 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
138                 uint64_t default_val)
139 {
140         const uint64_t *cell64;
141         int length;
142
143         cell64 = fdt_getprop(blob, node, prop_name, &length);
144         if (!cell64 || length < sizeof(*cell64))
145                 return default_val;
146
147         return fdt64_to_cpu(*cell64);
148 }
149
150 int fdtdec_get_is_enabled(const void *blob, int node)
151 {
152         const char *cell;
153
154         /*
155          * It should say "okay", so only allow that. Some fdts use "ok" but
156          * this is a bug. Please fix your device tree source file. See here
157          * for discussion:
158          *
159          * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
160          */
161         cell = fdt_getprop(blob, node, "status", NULL);
162         if (cell)
163                 return 0 == strcmp(cell, "okay");
164         return 1;
165 }
166
167 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
168 {
169         enum fdt_compat_id id;
170
171         /* Search our drivers */
172         for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
173                 if (0 == fdt_node_check_compatible(blob, node,
174                                 compat_names[id]))
175                         return id;
176         return COMPAT_UNKNOWN;
177 }
178
179 int fdtdec_next_compatible(const void *blob, int node,
180                 enum fdt_compat_id id)
181 {
182         return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
183 }
184
185 int fdtdec_next_compatible_subnode(const void *blob, int node,
186                 enum fdt_compat_id id, int *depthp)
187 {
188         do {
189                 node = fdt_next_node(blob, node, depthp);
190         } while (*depthp > 1);
191
192         /* If this is a direct subnode, and compatible, return it */
193         if (*depthp == 1 && 0 == fdt_node_check_compatible(
194                                                 blob, node, compat_names[id]))
195                 return node;
196
197         return -FDT_ERR_NOTFOUND;
198 }
199
200 int fdtdec_next_alias(const void *blob, const char *name,
201                 enum fdt_compat_id id, int *upto)
202 {
203 #define MAX_STR_LEN 20
204         char str[MAX_STR_LEN + 20];
205         int node, err;
206
207         /* snprintf() is not available */
208         assert(strlen(name) < MAX_STR_LEN);
209         sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
210         node = fdt_path_offset(blob, str);
211         if (node < 0)
212                 return node;
213         err = fdt_node_check_compatible(blob, node, compat_names[id]);
214         if (err < 0)
215                 return err;
216         if (err)
217                 return -FDT_ERR_NOTFOUND;
218         (*upto)++;
219         return node;
220 }
221
222 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
223                         enum fdt_compat_id id, int *node_list, int maxcount)
224 {
225         memset(node_list, '\0', sizeof(*node_list) * maxcount);
226
227         return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
228 }
229
230 /* TODO: Can we tighten this code up a little? */
231 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
232                         enum fdt_compat_id id, int *node_list, int maxcount)
233 {
234         int name_len = strlen(name);
235         int nodes[maxcount];
236         int num_found = 0;
237         int offset, node;
238         int alias_node;
239         int count;
240         int i, j;
241
242         /* find the alias node if present */
243         alias_node = fdt_path_offset(blob, "/aliases");
244
245         /*
246          * start with nothing, and we can assume that the root node can't
247          * match
248          */
249         memset(nodes, '\0', sizeof(nodes));
250
251         /* First find all the compatible nodes */
252         for (node = count = 0; node >= 0 && count < maxcount;) {
253                 node = fdtdec_next_compatible(blob, node, id);
254                 if (node >= 0)
255                         nodes[count++] = node;
256         }
257         if (node >= 0)
258                 debug("%s: warning: maxcount exceeded with alias '%s'\n",
259                        __func__, name);
260
261         /* Now find all the aliases */
262         for (offset = fdt_first_property_offset(blob, alias_node);
263                         offset > 0;
264                         offset = fdt_next_property_offset(blob, offset)) {
265                 const struct fdt_property *prop;
266                 const char *path;
267                 int number;
268                 int found;
269
270                 node = 0;
271                 prop = fdt_get_property_by_offset(blob, offset, NULL);
272                 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
273                 if (prop->len && 0 == strncmp(path, name, name_len))
274                         node = fdt_path_offset(blob, prop->data);
275                 if (node <= 0)
276                         continue;
277
278                 /* Get the alias number */
279                 number = simple_strtoul(path + name_len, NULL, 10);
280                 if (number < 0 || number >= maxcount) {
281                         debug("%s: warning: alias '%s' is out of range\n",
282                                __func__, path);
283                         continue;
284                 }
285
286                 /* Make sure the node we found is actually in our list! */
287                 found = -1;
288                 for (j = 0; j < count; j++)
289                         if (nodes[j] == node) {
290                                 found = j;
291                                 break;
292                         }
293
294                 if (found == -1) {
295                         debug("%s: warning: alias '%s' points to a node "
296                                 "'%s' that is missing or is not compatible "
297                                 " with '%s'\n", __func__, path,
298                                 fdt_get_name(blob, node, NULL),
299                                compat_names[id]);
300                         continue;
301                 }
302
303                 /*
304                  * Add this node to our list in the right place, and mark
305                  * it as done.
306                  */
307                 if (fdtdec_get_is_enabled(blob, node)) {
308                         if (node_list[number]) {
309                                 debug("%s: warning: alias '%s' requires that "
310                                       "a node be placed in the list in a "
311                                       "position which is already filled by "
312                                       "node '%s'\n", __func__, path,
313                                       fdt_get_name(blob, node, NULL));
314                                 continue;
315                         }
316                         node_list[number] = node;
317                         if (number >= num_found)
318                                 num_found = number + 1;
319                 }
320                 nodes[found] = 0;
321         }
322
323         /* Add any nodes not mentioned by an alias */
324         for (i = j = 0; i < maxcount; i++) {
325                 if (!node_list[i]) {
326                         for (; j < maxcount; j++)
327                                 if (nodes[j] &&
328                                         fdtdec_get_is_enabled(blob, nodes[j]))
329                                         break;
330
331                         /* Have we run out of nodes to add? */
332                         if (j == maxcount)
333                                 break;
334
335                         assert(!node_list[i]);
336                         node_list[i] = nodes[j++];
337                         if (i >= num_found)
338                                 num_found = i + 1;
339                 }
340         }
341
342         return num_found;
343 }
344
345 int fdtdec_check_fdt(void)
346 {
347         /*
348          * We must have an FDT, but we cannot panic() yet since the console
349          * is not ready. So for now, just assert(). Boards which need an early
350          * FDT (prior to console ready) will need to make their own
351          * arrangements and do their own checks.
352          */
353         assert(!fdtdec_prepare_fdt());
354         return 0;
355 }
356
357 /*
358  * This function is a little odd in that it accesses global data. At some
359  * point if the architecture board.c files merge this will make more sense.
360  * Even now, it is common code.
361  */
362 int fdtdec_prepare_fdt(void)
363 {
364         if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
365             fdt_check_header(gd->fdt_blob)) {
366                 printf("No valid FDT found - please append one to U-Boot "
367                         "binary, use u-boot-dtb.bin or define "
368                         "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
369                 return -1;
370         }
371         return 0;
372 }
373
374 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
375 {
376         const u32 *phandle;
377         int lookup;
378
379         debug("%s: %s\n", __func__, prop_name);
380         phandle = fdt_getprop(blob, node, prop_name, NULL);
381         if (!phandle)
382                 return -FDT_ERR_NOTFOUND;
383
384         lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
385         return lookup;
386 }
387
388 /**
389  * Look up a property in a node and check that it has a minimum length.
390  *
391  * @param blob          FDT blob
392  * @param node          node to examine
393  * @param prop_name     name of property to find
394  * @param min_len       minimum property length in bytes
395  * @param err           0 if ok, or -FDT_ERR_NOTFOUND if the property is not
396                         found, or -FDT_ERR_BADLAYOUT if not enough data
397  * @return pointer to cell, which is only valid if err == 0
398  */
399 static const void *get_prop_check_min_len(const void *blob, int node,
400                 const char *prop_name, int min_len, int *err)
401 {
402         const void *cell;
403         int len;
404
405         debug("%s: %s\n", __func__, prop_name);
406         cell = fdt_getprop(blob, node, prop_name, &len);
407         if (!cell)
408                 *err = -FDT_ERR_NOTFOUND;
409         else if (len < min_len)
410                 *err = -FDT_ERR_BADLAYOUT;
411         else
412                 *err = 0;
413         return cell;
414 }
415
416 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
417                 u32 *array, int count)
418 {
419         const u32 *cell;
420         int i, err = 0;
421
422         debug("%s: %s\n", __func__, prop_name);
423         cell = get_prop_check_min_len(blob, node, prop_name,
424                                       sizeof(u32) * count, &err);
425         if (!err) {
426                 for (i = 0; i < count; i++)
427                         array[i] = fdt32_to_cpu(cell[i]);
428         }
429         return err;
430 }
431
432 const u32 *fdtdec_locate_array(const void *blob, int node,
433                                const char *prop_name, int count)
434 {
435         const u32 *cell;
436         int err;
437
438         cell = get_prop_check_min_len(blob, node, prop_name,
439                                       sizeof(u32) * count, &err);
440         return err ? NULL : cell;
441 }
442
443 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
444 {
445         const s32 *cell;
446         int len;
447
448         debug("%s: %s\n", __func__, prop_name);
449         cell = fdt_getprop(blob, node, prop_name, &len);
450         return cell != NULL;
451 }
452
453 /**
454  * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
455  * terminating item.
456  *
457  * @param blob          FDT blob to use
458  * @param node          Node to look at
459  * @param prop_name     Node property name
460  * @param gpio          Array of gpio elements to fill from FDT. This will be
461  *                      untouched if either 0 or an error is returned
462  * @param max_count     Maximum number of elements allowed
463  * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
464  * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
465  */
466 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
467                 struct fdt_gpio_state *gpio, int max_count)
468 {
469         const struct fdt_property *prop;
470         const u32 *cell;
471         const char *name;
472         int len, i;
473
474         debug("%s: %s\n", __func__, prop_name);
475         assert(max_count > 0);
476         prop = fdt_get_property(blob, node, prop_name, &len);
477         if (!prop) {
478                 debug("%s: property '%s' missing\n", __func__, prop_name);
479                 return -FDT_ERR_NOTFOUND;
480         }
481
482         /* We will use the name to tag the GPIO */
483         name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
484         cell = (u32 *)prop->data;
485         len /= sizeof(u32) * 3;         /* 3 cells per GPIO record */
486         if (len > max_count) {
487                 debug(" %s: too many GPIOs / cells for "
488                         "property '%s'\n", __func__, prop_name);
489                 return -FDT_ERR_BADLAYOUT;
490         }
491
492         /* Read out the GPIO data from the cells */
493         for (i = 0; i < len; i++, cell += 3) {
494                 gpio[i].gpio = fdt32_to_cpu(cell[1]);
495                 gpio[i].flags = fdt32_to_cpu(cell[2]);
496                 gpio[i].name = name;
497         }
498
499         return len;
500 }
501
502 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
503                 struct fdt_gpio_state *gpio)
504 {
505         int err;
506
507         debug("%s: %s\n", __func__, prop_name);
508         gpio->gpio = FDT_GPIO_NONE;
509         gpio->name = NULL;
510         err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
511         return err == 1 ? 0 : err;
512 }
513
514 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
515 {
516         int val;
517
518         if (!fdt_gpio_isvalid(gpio))
519                 return -1;
520
521         val = gpio_get_value(gpio->gpio);
522         return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
523 }
524
525 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
526 {
527         if (!fdt_gpio_isvalid(gpio))
528                 return -1;
529
530         val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
531         return gpio_set_value(gpio->gpio, val);
532 }
533
534 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
535 {
536         /*
537          * Return success if there is no GPIO defined. This is used for
538          * optional GPIOs)
539          */
540         if (!fdt_gpio_isvalid(gpio))
541                 return 0;
542
543         if (gpio_request(gpio->gpio, gpio->name))
544                 return -1;
545         return 0;
546 }
547
548 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
549                 u8 *array, int count)
550 {
551         const u8 *cell;
552         int err;
553
554         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
555         if (!err)
556                 memcpy(array, cell, count);
557         return err;
558 }
559
560 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
561                              const char *prop_name, int count)
562 {
563         const u8 *cell;
564         int err;
565
566         cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
567         if (err)
568                 return NULL;
569         return cell;
570 }
571
572 int fdtdec_get_config_int(const void *blob, const char *prop_name,
573                 int default_val)
574 {
575         int config_node;
576
577         debug("%s: %s\n", __func__, prop_name);
578         config_node = fdt_path_offset(blob, "/config");
579         if (config_node < 0)
580                 return default_val;
581         return fdtdec_get_int(blob, config_node, prop_name, default_val);
582 }
583
584 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
585 {
586         int config_node;
587         const void *prop;
588
589         debug("%s: %s\n", __func__, prop_name);
590         config_node = fdt_path_offset(blob, "/config");
591         if (config_node < 0)
592                 return 0;
593         prop = fdt_get_property(blob, config_node, prop_name, NULL);
594
595         return prop != NULL;
596 }
597
598 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
599 {
600         const char *nodep;
601         int nodeoffset;
602         int len;
603
604         debug("%s: %s\n", __func__, prop_name);
605         nodeoffset = fdt_path_offset(blob, "/config");
606         if (nodeoffset < 0)
607                 return NULL;
608
609         nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
610         if (!nodep)
611                 return NULL;
612
613         return (char *)nodep;
614 }
615
616 int fdtdec_decode_region(const void *blob, int node,
617                 const char *prop_name, void **ptrp, size_t *size)
618 {
619         const fdt_addr_t *cell;
620         int len;
621
622         debug("%s: %s\n", __func__, prop_name);
623         cell = fdt_getprop(blob, node, prop_name, &len);
624         if (!cell || (len != sizeof(fdt_addr_t) * 2))
625                 return -1;
626
627         *ptrp = (void *)fdt_addr_to_cpu(*cell);
628         *size = fdt_size_to_cpu(cell[1]);
629         debug("%s: size=%zx\n", __func__, *size);
630         return 0;
631 }