From 7ae2291e8e0f34ac520044c8af0437a701e1c967 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Thu, 17 May 2012 12:47:57 +0200 Subject: dt: add helpers for multi-cell adds We have device tree helpers that allow us to create single cell (u32) wide properties. However, when creating properties that contain an array of cells, we need to jump through hoops, manually passing in an array with converted endianness. To ease the pain of this, create a generic macro helper that allows us to pass the cells as arguments. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'device_tree.h') diff --git a/device_tree.h b/device_tree.h index 4378685b7a..1e671e2506 100644 --- a/device_tree.h +++ b/device_tree.h @@ -25,4 +25,16 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, int qemu_devtree_nop_node(void *fdt, const char *node_path); int qemu_devtree_add_subnode(void *fdt, const char *name); +#define qemu_devtree_setprop_cells(fdt, node_path, property, ...) \ + do { \ + uint32_t qdt_tmp[] = { __VA_ARGS__ }; \ + int i; \ + \ + for (i = 0; i < ARRAY_SIZE(qdt_tmp); i++) { \ + qdt_tmp[i] = cpu_to_be32(qdt_tmp[i]); \ + } \ + qemu_devtree_setprop(fdt, node_path, property, qdt_tmp, \ + sizeof(qdt_tmp)); \ + } while (0) + #endif /* __DEVICE_TREE_H__ */ -- cgit 1.4.1 From 8535ab125853d1ba9067ba408c532c2ad79146e6 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Thu, 17 May 2012 14:11:52 +0200 Subject: dt: add helper for phandle references Phandles are the fancy device tree name for "pointer to another node". To create a phandle property, we most likely want to reference to the node we're pointing to by its path. So create a helper that allows us to do so. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.c | 8 ++++++++ device_tree.h | 3 +++ 2 files changed, 11 insertions(+) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index 94a239e987..2905f9afe4 100644 --- a/device_tree.c +++ b/device_tree.c @@ -132,6 +132,14 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, return r; } +int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, + const char *property, + const char *target_node_path) +{ + uint32_t phandle = fdt_get_phandle(fdt, findnode_nofail(fdt, target_node_path)); + return qemu_devtree_setprop_cell(fdt, node_path, property, phandle); +} + int qemu_devtree_nop_node(void *fdt, const char *node_path) { int r; diff --git a/device_tree.h b/device_tree.h index 1e671e2506..754bd2b2ec 100644 --- a/device_tree.h +++ b/device_tree.h @@ -22,6 +22,9 @@ int qemu_devtree_setprop_cell(void *fdt, const char *node_path, const char *property, uint32_t val); int qemu_devtree_setprop_string(void *fdt, const char *node_path, const char *property, const char *string); +int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, + const char *property, + const char *target_node_path); int qemu_devtree_nop_node(void *fdt, const char *node_path); int qemu_devtree_add_subnode(void *fdt, const char *name); -- cgit 1.4.1 From 7d5fd1089c36391ffc53a694101f441ab8cbbc05 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Thu, 17 May 2012 15:23:39 +0200 Subject: dt: add helper for phandle enumeration This patch adds a helper to search for a node's phandle by its path. This is especially useful when the phandle is part of an array, not just a single cell in which case qemu_devtree_setprop_phandle would be the easy choice. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.c | 16 +++++++++++++++- device_tree.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index 967c97ac04..2f127b71b0 100644 --- a/device_tree.c +++ b/device_tree.c @@ -132,11 +132,25 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, return r; } +uint32_t qemu_devtree_get_phandle(void *fdt, const char *path) +{ + uint32_t r; + + r = fdt_get_phandle(fdt, findnode_nofail(fdt, path)); + if (r <= 0) { + fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__, + path, fdt_strerror(r)); + exit(1); + } + + return r; +} + int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, const char *property, const char *target_node_path) { - uint32_t phandle = fdt_get_phandle(fdt, findnode_nofail(fdt, target_node_path)); + uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path); return qemu_devtree_setprop_cell(fdt, node_path, property, phandle); } diff --git a/device_tree.h b/device_tree.h index 754bd2b2ec..36fc9dbede 100644 --- a/device_tree.h +++ b/device_tree.h @@ -25,6 +25,7 @@ int qemu_devtree_setprop_string(void *fdt, const char *node_path, int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, const char *property, const char *target_node_path); +uint32_t qemu_devtree_get_phandle(void *fdt, const char *path); int qemu_devtree_nop_node(void *fdt, const char *node_path); int qemu_devtree_add_subnode(void *fdt, const char *name); -- cgit 1.4.1 From ce36252cc1711a5e222ed68e72235088aec2529d Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Thu, 17 May 2012 15:33:54 +0200 Subject: dt: add helper for empty dt creation We want to get rid of the concept of loading an external device tree and instead generate our own. However, to do this we need to also create a device tree template programatically. This patch adds a helper to create an empty device tree in memory. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.c | 37 +++++++++++++++++++++++++++++++++++++ device_tree.h | 1 + 2 files changed, 38 insertions(+) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index 2f127b71b0..d0378961f0 100644 --- a/device_tree.c +++ b/device_tree.c @@ -25,6 +25,43 @@ #include +#define FDT_MAX_SIZE 0x10000 + +void *create_device_tree(int *sizep) +{ + void *fdt; + int ret; + + *sizep = FDT_MAX_SIZE; + fdt = g_malloc0(FDT_MAX_SIZE); + ret = fdt_create(fdt, FDT_MAX_SIZE); + if (ret < 0) { + goto fail; + } + ret = fdt_begin_node(fdt, ""); + if (ret < 0) { + goto fail; + } + ret = fdt_end_node(fdt); + if (ret < 0) { + goto fail; + } + ret = fdt_finish(fdt); + if (ret < 0) { + goto fail; + } + ret = fdt_open_into(fdt, fdt, *sizep); + if (ret) { + fprintf(stderr, "Unable to copy device tree in memory\n"); + exit(1); + } + + return fdt; +fail: + fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret)); + exit(1); +} + void *load_device_tree(const char *filename_path, int *sizep) { int dt_size; diff --git a/device_tree.h b/device_tree.h index 36fc9dbede..5f76f40eeb 100644 --- a/device_tree.h +++ b/device_tree.h @@ -14,6 +14,7 @@ #ifndef __DEVICE_TREE_H__ #define __DEVICE_TREE_H__ +void *create_device_tree(int *sizep); void *load_device_tree(const char *filename_path, int *sizep); int qemu_devtree_setprop(void *fdt, const char *node_path, -- cgit 1.4.1 From 3601b572820ee5f3676ab24c068a4e011b72236d Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Thu, 17 May 2012 16:58:55 +0200 Subject: dt: add helper for phandle allocation Phandle references work by having 2 pieces: - a "phandle" 1-cell property in the device tree node - a reference to the same value in a property we want to point to the other node To generate the 1-cell property, we need an allocation mechanism that gives us a unique number space. This patch adds an allocator for these properties. Signed-off-by: Alexander Graf --- device_tree.c | 7 +++++++ device_tree.h | 1 + 2 files changed, 8 insertions(+) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index d0378961f0..75412747b7 100644 --- a/device_tree.c +++ b/device_tree.c @@ -191,6 +191,13 @@ int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, return qemu_devtree_setprop_cell(fdt, node_path, property, phandle); } +uint32_t qemu_devtree_alloc_phandle(void *fdt) +{ + static int phandle = 0x8000; + + return phandle++; +} + int qemu_devtree_nop_node(void *fdt, const char *node_path) { int r; diff --git a/device_tree.h b/device_tree.h index 5f76f40eeb..97af3454f7 100644 --- a/device_tree.h +++ b/device_tree.h @@ -27,6 +27,7 @@ int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, const char *property, const char *target_node_path); uint32_t qemu_devtree_get_phandle(void *fdt, const char *path); +uint32_t qemu_devtree_alloc_phandle(void *fdt); int qemu_devtree_nop_node(void *fdt, const char *node_path); int qemu_devtree_add_subnode(void *fdt, const char *name); -- cgit 1.4.1 From bb28eb3797b0fbb4baa6d87edf69b3a0ff499e94 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 18 May 2012 01:53:01 +0200 Subject: dt: add helper for 64bit cell adds Some times in the device tree, we find an array of 2 u32 cells that really are a single u64 value. This patch adds a helper to make the creation of these easy. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.c | 7 +++++++ device_tree.h | 2 ++ 2 files changed, 9 insertions(+) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index 75412747b7..c8d68c203e 100644 --- a/device_tree.c +++ b/device_tree.c @@ -154,6 +154,13 @@ int qemu_devtree_setprop_cell(void *fdt, const char *node_path, return r; } +int qemu_devtree_setprop_u64(void *fdt, const char *node_path, + const char *property, uint64_t val) +{ + val = cpu_to_be64(val); + return qemu_devtree_setprop(fdt, node_path, property, &val, sizeof(val)); +} + int qemu_devtree_setprop_string(void *fdt, const char *node_path, const char *property, const char *string) { diff --git a/device_tree.h b/device_tree.h index 97af3454f7..4898d957af 100644 --- a/device_tree.h +++ b/device_tree.h @@ -21,6 +21,8 @@ int qemu_devtree_setprop(void *fdt, const char *node_path, const char *property, void *val_array, int size); int qemu_devtree_setprop_cell(void *fdt, const char *node_path, const char *property, uint32_t val); +int qemu_devtree_setprop_u64(void *fdt, const char *node_path, + const char *property, uint64_t val); int qemu_devtree_setprop_string(void *fdt, const char *node_path, const char *property, const char *string); int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, -- cgit 1.4.1 From 45e9dfb2fdd6ab7b60f823a1ee5ed2f2722beaf6 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Wed, 20 Jun 2012 20:39:59 +0200 Subject: dt: make setprop argument static Whatever we pass in to qemu_devtree_setprop to put into the device tree will not get modified by that function, so it can easily be declared const. Signed-off-by: Alexander Graf Reviewed-by: Peter Crosthwaite --- device_tree.c | 2 +- device_tree.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'device_tree.h') diff --git a/device_tree.c b/device_tree.c index acae53e6b6..b366fddeaf 100644 --- a/device_tree.c +++ b/device_tree.c @@ -127,7 +127,7 @@ static int findnode_nofail(void *fdt, const char *node_path) } int qemu_devtree_setprop(void *fdt, const char *node_path, - const char *property, void *val_array, int size) + const char *property, const void *val_array, int size) { int r; diff --git a/device_tree.h b/device_tree.h index 4898d957af..2244270b2d 100644 --- a/device_tree.h +++ b/device_tree.h @@ -18,7 +18,7 @@ void *create_device_tree(int *sizep); void *load_device_tree(const char *filename_path, int *sizep); int qemu_devtree_setprop(void *fdt, const char *node_path, - const char *property, void *val_array, int size); + const char *property, const void *val_array, int size); int qemu_devtree_setprop_cell(void *fdt, const char *node_path, const char *property, uint32_t val); int qemu_devtree_setprop_u64(void *fdt, const char *node_path, -- cgit 1.4.1