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