Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 1 | /* |
Juan Pablo Conde | e7c0f42 | 2023-06-22 16:33:15 -0500 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, Arm Limited and Contributors. All rights reserved. |
Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* Helper functions to offer easier navigation of Device Tree Blob */ |
| 8 | |
Antonio Nino Diaz | c3cf06f | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 9 | #ifndef FDT_WRAPPERS_H |
| 10 | #define FDT_WRAPPERS_H |
Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 11 | |
Andre Przywara | 5a430c0 | 2020-07-09 12:33:17 +0100 | [diff] [blame] | 12 | #include <libfdt_env.h> |
Andre Przywara | 49b268c | 2023-02-03 11:11:18 +0000 | [diff] [blame] | 13 | #include <libfdt.h> |
Andre Przywara | 5a430c0 | 2020-07-09 12:33:17 +0100 | [diff] [blame] | 14 | |
Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 15 | /* Number of cells, given total length in bytes. Each cell is 4 bytes long */ |
Antonio Nino Diaz | 81542c0 | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 16 | #define NCELLS(len) ((len) / 4U) |
Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 17 | |
Andre Przywara | ff4e6c3 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 18 | int fdt_read_uint32(const void *dtb, int node, const char *prop_name, |
| 19 | uint32_t *value); |
Andre Przywara | be858cf | 2020-03-26 11:50:33 +0000 | [diff] [blame] | 20 | uint32_t fdt_read_uint32_default(const void *dtb, int node, |
| 21 | const char *prop_name, uint32_t dflt_value); |
Andre Przywara | ff4e6c3 | 2020-03-26 11:22:37 +0000 | [diff] [blame] | 22 | int fdt_read_uint64(const void *dtb, int node, const char *prop_name, |
| 23 | uint64_t *value); |
Andre Przywara | 6e3a89f | 2020-03-30 23:21:13 +0100 | [diff] [blame] | 24 | int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name, |
| 25 | unsigned int cells, uint32_t *value); |
Antonio Nino Diaz | 2747362 | 2018-06-26 10:34:07 +0100 | [diff] [blame] | 26 | int fdtw_read_string(const void *dtb, int node, const char *prop, |
| 27 | char *str, size_t size); |
David Horstmann | d13dbb6 | 2021-03-01 19:34:37 +0000 | [diff] [blame] | 28 | int fdtw_read_uuid(const void *dtb, int node, const char *prop, |
| 29 | unsigned int length, uint8_t *uuid); |
Soby Mathew | e5674e1 | 2017-11-06 13:56:40 +0000 | [diff] [blame] | 30 | int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, |
| 31 | unsigned int cells, void *value); |
Alexei Fedorov | 0a2ab6e | 2020-01-29 16:21:28 +0000 | [diff] [blame] | 32 | int fdtw_read_bytes(const void *dtb, int node, const char *prop, |
| 33 | unsigned int length, void *value); |
| 34 | int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop, |
| 35 | unsigned int length, const void *data); |
Andre Przywara | 364ad24 | 2020-03-26 11:57:43 +0000 | [diff] [blame] | 36 | int fdt_get_reg_props_by_index(const void *dtb, int node, int index, |
| 37 | uintptr_t *base, size_t *size); |
Andre Przywara | 7ad6d36 | 2020-03-26 12:11:34 +0000 | [diff] [blame] | 38 | int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name, |
| 39 | uintptr_t *base, size_t *size); |
Andre Przywara | 60e2e27 | 2020-03-26 12:52:06 +0000 | [diff] [blame] | 40 | int fdt_get_stdout_node_offset(const void *dtb); |
Antonio Nino Diaz | 2747362 | 2018-06-26 10:34:07 +0100 | [diff] [blame] | 41 | |
Madhukar Pappireddy | 447870b | 2020-03-24 10:03:34 -0500 | [diff] [blame] | 42 | uint64_t fdtw_translate_address(const void *dtb, int bus_node, |
| 43 | uint64_t base_address); |
| 44 | |
Chris Kay | 2d9ea36 | 2021-09-28 16:06:25 +0100 | [diff] [blame] | 45 | int fdtw_for_each_cpu(const void *fdt, |
| 46 | int (*callback)(const void *dtb, int node, uintptr_t mpidr)); |
| 47 | |
Ruchika Gupta | dea8ee0 | 2022-04-08 13:16:16 +0530 | [diff] [blame] | 48 | int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name); |
| 49 | |
Andre Przywara | 5a430c0 | 2020-07-09 12:33:17 +0100 | [diff] [blame] | 50 | static inline uint32_t fdt_blob_size(const void *dtb) |
| 51 | { |
Juan Pablo Conde | e7c0f42 | 2023-06-22 16:33:15 -0500 | [diff] [blame] | 52 | const uint32_t *dtb_header = (const uint32_t *)dtb; |
Andre Przywara | 5a430c0 | 2020-07-09 12:33:17 +0100 | [diff] [blame] | 53 | |
| 54 | return fdt32_to_cpu(dtb_header[1]); |
| 55 | } |
| 56 | |
Andre Przywara | 49b268c | 2023-02-03 11:11:18 +0000 | [diff] [blame] | 57 | static inline bool fdt_node_is_enabled(const void *fdt, int node) |
| 58 | { |
| 59 | int len; |
| 60 | const void *prop = fdt_getprop(fdt, node, "status", &len); |
| 61 | |
| 62 | /* A non-existing status property means the device is enabled. */ |
Juan Pablo Conde | e7c0f42 | 2023-06-22 16:33:15 -0500 | [diff] [blame] | 63 | return (prop == NULL) || (len == 5 && strcmp((const char *)prop, |
| 64 | "okay") == 0); |
Andre Przywara | 49b268c | 2023-02-03 11:11:18 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Laurent Carlier | ff76614 | 2021-09-30 15:57:09 +0100 | [diff] [blame] | 67 | #define fdt_for_each_compatible_node(dtb, node, compatible_str) \ |
| 68 | for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str); \ |
| 69 | node >= 0; \ |
| 70 | node = fdt_node_offset_by_compatible(dtb, node, compatible_str)) |
| 71 | |
Antonio Nino Diaz | c3cf06f | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 72 | #endif /* FDT_WRAPPERS_H */ |