1e5674e1fSSoby Mathew /* 2*bc8dfca6SNicolas Le Bayon * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved. 3e5674e1fSSoby Mathew * 4e5674e1fSSoby Mathew * SPDX-License-Identifier: BSD-3-Clause 5e5674e1fSSoby Mathew */ 6e5674e1fSSoby Mathew 7e5674e1fSSoby Mathew /* Helper functions to offer easier navigation of Device Tree Blob */ 8e5674e1fSSoby Mathew 9c3cf06f1SAntonio Nino Diaz #ifndef FDT_WRAPPERS_H 10c3cf06f1SAntonio Nino Diaz #define FDT_WRAPPERS_H 11e5674e1fSSoby Mathew 125a430c02SAndre Przywara #include <libfdt_env.h> 1349b268ceSAndre Przywara #include <libfdt.h> 145a430c02SAndre Przywara 15e5674e1fSSoby Mathew /* Number of cells, given total length in bytes. Each cell is 4 bytes long */ 1681542c00SAntonio Nino Diaz #define NCELLS(len) ((len) / 4U) 17e5674e1fSSoby Mathew 18ff4e6c35SAndre Przywara int fdt_read_uint32(const void *dtb, int node, const char *prop_name, 19ff4e6c35SAndre Przywara uint32_t *value); 20be858cffSAndre Przywara uint32_t fdt_read_uint32_default(const void *dtb, int node, 21be858cffSAndre Przywara const char *prop_name, uint32_t dflt_value); 22ff4e6c35SAndre Przywara int fdt_read_uint64(const void *dtb, int node, const char *prop_name, 23ff4e6c35SAndre Przywara uint64_t *value); 24*bc8dfca6SNicolas Le Bayon uint64_t fdt_read_uint64_default(const void *dtb, int node, 25*bc8dfca6SNicolas Le Bayon const char *prop_name, uint64_t dflt_value); 266e3a89f4SAndre Przywara int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name, 276e3a89f4SAndre Przywara unsigned int cells, uint32_t *value); 2827473620SAntonio Nino Diaz int fdtw_read_string(const void *dtb, int node, const char *prop, 2927473620SAntonio Nino Diaz char *str, size_t size); 30d13dbb6fSDavid Horstmann int fdtw_read_uuid(const void *dtb, int node, const char *prop, 31d13dbb6fSDavid Horstmann unsigned int length, uint8_t *uuid); 32e5674e1fSSoby Mathew int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, 33e5674e1fSSoby Mathew unsigned int cells, void *value); 340a2ab6e6SAlexei Fedorov int fdtw_read_bytes(const void *dtb, int node, const char *prop, 350a2ab6e6SAlexei Fedorov unsigned int length, void *value); 360a2ab6e6SAlexei Fedorov int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop, 370a2ab6e6SAlexei Fedorov unsigned int length, const void *data); 38364ad245SAndre Przywara int fdt_get_reg_props_by_index(const void *dtb, int node, int index, 39364ad245SAndre Przywara uintptr_t *base, size_t *size); 407ad6d362SAndre Przywara int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name, 417ad6d362SAndre Przywara uintptr_t *base, size_t *size); 4260e2e27dSAndre Przywara int fdt_get_stdout_node_offset(const void *dtb); 4327473620SAntonio Nino Diaz 44447870bfSMadhukar Pappireddy uint64_t fdtw_translate_address(const void *dtb, int bus_node, 45447870bfSMadhukar Pappireddy uint64_t base_address); 46447870bfSMadhukar Pappireddy 472d9ea360SChris Kay int fdtw_for_each_cpu(const void *fdt, 482d9ea360SChris Kay int (*callback)(const void *dtb, int node, uintptr_t mpidr)); 492d9ea360SChris Kay 50dea8ee0dSRuchika Gupta int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name); 51dea8ee0dSRuchika Gupta 525a430c02SAndre Przywara static inline uint32_t fdt_blob_size(const void *dtb) 535a430c02SAndre Przywara { 54e7c0f42aSJuan Pablo Conde const uint32_t *dtb_header = (const uint32_t *)dtb; 555a430c02SAndre Przywara 565a430c02SAndre Przywara return fdt32_to_cpu(dtb_header[1]); 575a430c02SAndre Przywara } 585a430c02SAndre Przywara 5949b268ceSAndre Przywara static inline bool fdt_node_is_enabled(const void *fdt, int node) 6049b268ceSAndre Przywara { 6149b268ceSAndre Przywara int len; 6249b268ceSAndre Przywara const void *prop = fdt_getprop(fdt, node, "status", &len); 6349b268ceSAndre Przywara 6449b268ceSAndre Przywara /* A non-existing status property means the device is enabled. */ 65e7c0f42aSJuan Pablo Conde return (prop == NULL) || (len == 5 && strcmp((const char *)prop, 66e7c0f42aSJuan Pablo Conde "okay") == 0); 6749b268ceSAndre Przywara } 6849b268ceSAndre Przywara 69ff766148SLaurent Carlier #define fdt_for_each_compatible_node(dtb, node, compatible_str) \ 70ff766148SLaurent Carlier for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str); \ 71ff766148SLaurent Carlier node >= 0; \ 72ff766148SLaurent Carlier node = fdt_node_offset_by_compatible(dtb, node, compatible_str)) 73ff766148SLaurent Carlier 74c3cf06f1SAntonio Nino Diaz #endif /* FDT_WRAPPERS_H */ 75