fdt_wrappers.h 2.6 KB

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