blob: 7f6af59c8699a27d877d1599e42b261a66ec7db5 [file] [log] [blame]
Soby Mathewb9fccca2017-11-06 13:56:40 +00001/*
Nicolas Le Bayon51f94402023-05-17 12:36:01 +02002 * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved.
Soby Mathewb9fccca2017-11-06 13:56:40 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/* Helper functions to offer easier navigation of Device Tree Blob */
8
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00009#ifndef FDT_WRAPPERS_H
10#define FDT_WRAPPERS_H
Soby Mathewb9fccca2017-11-06 13:56:40 +000011
Andre Przywara97c6e902020-07-09 12:33:17 +010012#include <libfdt_env.h>
Andre Przywara8def1962023-02-03 11:11:18 +000013#include <libfdt.h>
Andre Przywara97c6e902020-07-09 12:33:17 +010014
Soby Mathewb9fccca2017-11-06 13:56:40 +000015/* Number of cells, given total length in bytes. Each cell is 4 bytes long */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010016#define NCELLS(len) ((len) / 4U)
Soby Mathewb9fccca2017-11-06 13:56:40 +000017
Andre Przywarafe5bdf52020-03-26 11:22:37 +000018int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
19 uint32_t *value);
Andre Przywara2d5690c2020-03-26 11:50:33 +000020uint32_t fdt_read_uint32_default(const void *dtb, int node,
21 const char *prop_name, uint32_t dflt_value);
Andre Przywarafe5bdf52020-03-26 11:22:37 +000022int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
23 uint64_t *value);
Nicolas Le Bayon51f94402023-05-17 12:36:01 +020024uint64_t fdt_read_uint64_default(const void *dtb, int node,
25 const char *prop_name, uint64_t dflt_value);
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010026int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
27 unsigned int cells, uint32_t *value);
Antonio Nino Diazd85c8782018-06-26 10:34:07 +010028int fdtw_read_string(const void *dtb, int node, const char *prop,
29 char *str, size_t size);
David Horstmanna65a8ff2021-03-01 19:34:37 +000030int fdtw_read_uuid(const void *dtb, int node, const char *prop,
31 unsigned int length, uint8_t *uuid);
Soby Mathewb9fccca2017-11-06 13:56:40 +000032int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
33 unsigned int cells, void *value);
Alexei Fedorov2de82fc2020-01-29 16:21:28 +000034int fdtw_read_bytes(const void *dtb, int node, const char *prop,
35 unsigned int length, void *value);
36int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
37 unsigned int length, const void *data);
Andre Przywarae183e152020-03-26 11:57:43 +000038int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
39 uintptr_t *base, size_t *size);
Andre Przywara4a1c8742020-03-26 12:11:34 +000040int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
41 uintptr_t *base, size_t *size);
Andre Przywaraa0eaca52020-03-26 12:52:06 +000042int fdt_get_stdout_node_offset(const void *dtb);
Antonio Nino Diazd85c8782018-06-26 10:34:07 +010043
Madhukar Pappireddy361c1b32020-03-24 10:03:34 -050044uint64_t fdtw_translate_address(const void *dtb, int bus_node,
45 uint64_t base_address);
46
Chris Kay93c9db02021-09-28 16:06:25 +010047int fdtw_for_each_cpu(const void *fdt,
48 int (*callback)(const void *dtb, int node, uintptr_t mpidr));
49
Ruchika Guptad4802022022-04-08 13:16:16 +053050int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name);
51
AlexeiFedorov4ed1b2b2024-10-14 15:23:34 +010052uint64_t fdt_read_prop_cells(const fdt32_t *prop, int nr_cells);
53
Andre Przywara97c6e902020-07-09 12:33:17 +010054static inline uint32_t fdt_blob_size(const void *dtb)
55{
Juan Pablo Condec3229992023-06-22 16:33:15 -050056 const uint32_t *dtb_header = (const uint32_t *)dtb;
Andre Przywara97c6e902020-07-09 12:33:17 +010057
58 return fdt32_to_cpu(dtb_header[1]);
59}
60
Andre Przywara8def1962023-02-03 11:11:18 +000061static inline bool fdt_node_is_enabled(const void *fdt, int node)
62{
63 int len;
64 const void *prop = fdt_getprop(fdt, node, "status", &len);
65
66 /* A non-existing status property means the device is enabled. */
Juan Pablo Condec3229992023-06-22 16:33:15 -050067 return (prop == NULL) || (len == 5 && strcmp((const char *)prop,
68 "okay") == 0);
Andre Przywara8def1962023-02-03 11:11:18 +000069}
70
Laurent Carlier246b1ad2021-09-30 15:57:09 +010071#define fdt_for_each_compatible_node(dtb, node, compatible_str) \
72for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str); \
73 node >= 0; \
74 node = fdt_node_offset_by_compatible(dtb, node, compatible_str))
75
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000076#endif /* FDT_WRAPPERS_H */