1 /* 2 * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 /* Helper functions to offer easier navigation of Device Tree Blob */ 8 9 #ifndef FDT_WRAPPERS_H 10 #define FDT_WRAPPERS_H 11 12 #include <libfdt_env.h> 13 #include <libfdt.h> 14 15 /* Number of cells, given total length in bytes. Each cell is 4 bytes long */ 16 #define NCELLS(len) ((len) / 4U) 17 18 int fdt_read_uint32(const void *dtb, int node, const char *prop_name, 19 uint32_t *value); 20 uint32_t fdt_read_uint32_default(const void *dtb, int node, 21 const char *prop_name, uint32_t dflt_value); 22 int fdt_read_uint64(const void *dtb, int node, const char *prop_name, 23 uint64_t *value); 24 uint64_t fdt_read_uint64_default(const void *dtb, int node, 25 const char *prop_name, uint64_t dflt_value); 26 int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name, 27 unsigned int cells, uint32_t *value); 28 int fdtw_read_string(const void *dtb, int node, const char *prop, 29 char *str, size_t size); 30 int fdtw_read_uuid(const void *dtb, int node, const char *prop, 31 unsigned int length, uint8_t *uuid); 32 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, 33 unsigned int cells, void *value); 34 int fdtw_read_bytes(const void *dtb, int node, const char *prop, 35 unsigned int length, void *value); 36 int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop, 37 unsigned int length, const void *data); 38 int fdt_get_reg_props_by_index(const void *dtb, int node, int index, 39 uintptr_t *base, size_t *size); 40 int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name, 41 uintptr_t *base, size_t *size); 42 int fdt_get_stdout_node_offset(const void *dtb); 43 44 uint64_t fdtw_translate_address(const void *dtb, int bus_node, 45 uint64_t base_address); 46 47 int fdtw_for_each_cpu(const void *fdt, 48 int (*callback)(const void *dtb, int node, uintptr_t mpidr)); 49 50 int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name); 51 52 uint64_t fdt_read_prop_cells(const fdt32_t *prop, int nr_cells); 53 54 static inline uint32_t fdt_blob_size(const void *dtb) 55 { 56 const uint32_t *dtb_header = (const uint32_t *)dtb; 57 58 return fdt32_to_cpu(dtb_header[1]); 59 } 60 61 static 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. */ 67 return (prop == NULL) || (len == 5 && strcmp((const char *)prop, 68 "okay") == 0); 69 } 70 71 #define fdt_for_each_compatible_node(dtb, node, compatible_str) \ 72 for (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 76 #endif /* FDT_WRAPPERS_H */ 77