Skip to contentSkip to conversation

drivers: of: implement overlay support

PATCH In review Pascal Vizeli 3 participants 5 messages Jun 2, 2018 – Jun 5, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drivers: of: implement overlay support

---
 Documentation/user/devicetree.rst | 101 ++++++++-
 arch/sandbox/dts/Makefile         |   5 +-
 arch/sandbox/dts/sandbox.dts      |   6 +
 commands/oftree.c                 |  65 +++++-
 drivers/of/Kconfig                |   6 +
 drivers/of/Makefile               |   1 +
 drivers/of/overlay.c              | 234 ++++++++++++++++++++
 drivers/of/resolver.c             | 346 ++++++++++++++++++++++++++++++
 include/of.h                      |  71 ++++++
 scripts/Makefile.lib              |   5 +-
 10 files changed, 831 insertions(+), 9 deletions(-)
 create mode 100644 drivers/of/overlay.c
 create mode 100644 drivers/of/resolver.c
Documentation/user/devicetree.rst +100 -1
index 17934d86e..b2220997d 100644
@@ -21,7 +21,7 @@ The internal devicetree
 -----------------------
 
 The devicetree consulted by barebox plays a special role. It is referred to
-as the "internal devicetree." The barebox devicetree commands work on this
+as the "internal devicetree". The barebox devicetree commands work on this
 devicetree. The devicetree source (DTS) files are kept in sync with the kernel DTS
 files. As the FDT files are meant to be backward compatible, it should always be possible
 to start a kernel with the barebox internal devicetree. However, since the barebox
@@ -83,3 +83,102 @@ you can exchange the internal devicetree during runtime using the
 
    oftree -f
    oftree -l /new/dtb
+
+Devicetree overlays
+-------------------
+
+Since version 3.19, the Linux kernel supports applying "devicetree overlays" to
+its loaded device tree. This can be used to inform the kernel about additional
+non-discoverable devices after the system has booted, which is useful for modular
+boards and FPGAs. The details of the overlay format are specified in the Linux
+`kernel documentation <https://www.kernel.org/doc/Documentation/devicetree/overlay-notes.txt>`_
+and an updated DTC is required to compile the overlays.
+
+The use cases for overlays in barebox are a bit different:
+
+* some of the modular devices are needed to boot Linux to userspace, but barebox
+  can detect which module variant is connected
+* one of several parallel or LVDS displays (which use timing data from devicetree)
+  can be connected to the SoC and should be used for boot messages
+* a generic Linux (distribution) kernel should be booted on a modular
+  system and support additional hardware on modules
+
+barebox supports applying overlays in the internal devicetree was well using the
+:ref:`command_oftree` command with option ``-o``:
+
+.. code-block:: sh
+
+   $ ./barebox -d arch/sandbox/dts/sandbox.dtb -i arch/sandbox/dts/sandbox-overlay.dtbo
+   add fd0 backed by file arch/sandbox/dts/sandbox-overlay.dtbo
+
+   barebox 2015.02.0 #26 Wed Mar 4 09:41:19 CET 2015
+   ...
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       status = "disabled";
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+     };
+     dummy@1 {
+       status = "disabled";
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+   barebox@barebox sandbox:/ of_dump -f /dev/fd0
+    {
+     fragment@0 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     fragment@1 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     __fixups__ {
+       dummy0 = "/fragment@0:target:0";
+       dummy1 = "/fragment@1:target:0";
+     };
+   };
+   barebox@barebox sandbox:/ oftree -o /dev/fd0
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     dummy@1 {
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+
+If you need to use a different base devicetree instead of the one compiled into
+barebox, it needs to be replaced as described in the previous section.
arch/sandbox/dts/Makefile +3 -2
index 6f6838857..ede219e6f 100644
@@ -1,6 +1,7 @@
 ifeq ($(CONFIG_OFTREE),y)
 dtb-y += \
-	sandbox.dtb
+	sandbox.dtb \
+	sandbox-overlay.dtbo
 endif
 
 # just to build a built-in.o. Otherwise compilation fails when no devicetree is
@@ -8,4 +9,4 @@ endif
 obj- += dummy.o
 
 always := $(dtb-y)
-clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts
+clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtbo
arch/sandbox/dts/sandbox.dts +6 -0
index 2595aa13f..1b99a4960 100644
@@ -3,5 +3,11 @@
 #include "skeleton.dtsi"
 
 / {
+	dummy0: dummy@0 {
+		status = "disabled";
+	};
 
+	dummy1: dummy@1 {
+		status = "disabled";
+	};
 };
commands/oftree.c +60 -5

111 lines not on this page · raw message

drivers/of/Kconfig +6 -0
index a1fac0e61..d90bd9c6b 100644
@@ -15,6 +15,12 @@ config OFDEVICE
 	select DTC
 	bool "Enable probing of devices from the devicetree"
 
+config OFTREE_OVERLAY
+	bool "Enable support for devicetree overlays"
+	depends on OFTREE
+	help
+	  Allows you to modify the live tree using overlays.
+
 config OF_ADDRESS_PCI
 	bool
 
drivers/of/Makefile +1 -0
index ec4387006..323543461 100644
@@ -6,3 +6,4 @@ obj-y += partition.o
 obj-y += of_net.o
 obj-$(CONFIG_MTD) += of_mtd.o
 obj-$(CONFIG_OF_BAREBOX_DRIVERS) += barebox.o
+obj-$(CONFIG_OFTREE_OVERLAY) += resolver.o overlay.o
drivers/of/overlay.c new file +234 -0

234 lines not on this page · raw message

drivers/of/resolver.c new file +346 -0

346 lines not on this page · raw message

include/of.h +71 -0

81 lines not on this page · raw message

scripts/Makefile.lib +4 -1
index 272b5981e..e7cea55c4 100644
@@ -261,7 +261,7 @@ $(obj)/%.dtb.S: $(obj)/%.dtb $(srctree)/scripts/gen-dtb-s FORCE
 
 quiet_cmd_dtc = DTC     $@
 cmd_dtc = $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
-	$(objtree)/scripts/dtc/dtc -O dtb -o $@ -b 0 \
+	$(objtree)/scripts/dtc/dtc -@ -O dtb -o $@ -b 0 \
 		-i $(srctree)/arch/$(SRCARCH)/dts $(DTC_FLAGS) \
 		-i $(srctree)/dts/src/$(SRCARCH) \
 		-d $(depfile).dtc $(dtc-tmp) ; \
@@ -270,6 +270,9 @@ cmd_dtc = $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
 $(obj)/%.dtb: $(src)/%.dts FORCE
 	$(call if_changed_dep,dtc)
 
+$(obj)/%.dtbo: $(src)/%.dtso FORCE
+	$(call if_changed_dep,dtc)
+
 dtc-tmp = $(subst $(comma),_,$(dot-target).dts)
 
 obj-y += $(patsubst %,%.bbenv$(DEFAULT_COMPRESSION_SUFFIX).o,$(bbenv-y))
Signature · 7 lines
2.17.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 3/3 drivers: of: bugfix local fixups resolving

---
 drivers/of/resolver.c | 144 +++++++++++++++++-------------------------
 1 file changed, 58 insertions(+), 86 deletions(-)
drivers/of/resolver.c +58 -86
index 62476093c..03c61e959 100644
@@ -66,108 +66,72 @@ static void of_adjust_tree_phandles(struct device_node *node,
  * of the tree. Does not take any devtree locks so make sure you
  * call this on a tree which is at the detached state.
  */
-static int of_adjust_tree_phandle_references(struct device_node *node,
-		int phandle_delta)
+static int of_adjust_tree_phandle_references(struct device_node *local_fixups,
+		struct device_node *overlay, int phandle_delta)
 {
 	phandle phandle;
-	struct device_node *refnode, *child;
-	struct property *rprop, *sprop;
-	char *propval, *propcur, *propend, *nodestr, *propstr, *s;
-	int offset, propcurlen;
-	int err;
-	bool found = false;
+	struct device_node *child, *overlay_child;
+	struct property *fixprop, *prop;
+	int i, count, err;
+	unsigned int off;
 
 	/* locate the symbols & fixups nodes on resolve */
-	for_each_child_of_node(node, child)
-		if (of_node_cmp(child->name, "__local_fixups__") == 0) {
-			found = true;
-			break;
-		}
-
-	/* no local fixups */
-	if (!found)
+	if (!local_fixups)
 		return 0;
 
-	/* find the local fixups property */
-	for_each_property_of_node(child, rprop) {
+	for_each_property_of_node(local_fixups, fixprop) {
 		/* skip properties added automatically */
-		if (of_prop_cmp(rprop->name, "name") == 0)
+		if (of_prop_cmp(fixprop->name, "name") == 0 ||
+		    of_prop_cmp(fixprop->name, "phandle") == 0 ||
+		    of_prop_cmp(fixprop->name, "linux,phandle") == 0)
 			continue;
 
-		/* make a copy */
-		propval = kmalloc(rprop->length, GFP_KERNEL);
-		if (propval == NULL) {
-			pr_err("%s: Could not copy value of '%s'\n",
-					__func__, rprop->name);
-			return -ENOMEM;
-		}
-		memcpy(propval, rprop->value, rprop->length);
-
-		propend = propval + rprop->length;
-		for (propcur = propval; propcur < propend;
-				propcur += propcurlen + 1) {
-
-			propcurlen = strlen(propcur);
-
-			nodestr = propcur;
-			s = strchr(propcur, ':');
-			if (s == NULL) {
-				pr_err("%s: Illegal symbol entry '%s' (1)\n",
-					__func__, propcur);
-				err = -EINVAL;
-				goto err_fail;
-			}
-			*s++ = '\0';
+		if ((fixprop->length % 4) != 0 || fixprop->length == 0)
+			return -EINVAL;
+		count = fixprop->length / sizeof(uint32_t);
 
-			propstr = s;
-			s = strchr(s, ':');
-			if (s == NULL) {
-				pr_err("%s: Illegal symbol entry '%s' (2)\n",
-					__func__, (char *)rprop->value);
-				err = -EINVAL;
-				goto err_fail;
-			}
-
-			*s++ = '\0';
-			offset = simple_strtoul(s, NULL, 10);
+		for_each_property_of_node(overlay, prop) {
+			if (!of_prop_cmp(fixprop->name, prop->name))
+				break;
+		}
 
-			/* look into the resolve node for the full path */
-			refnode = of_find_node_by_path_from(node, nodestr);
-			if (refnode == NULL) {
-				pr_warn("%s: Could not find refnode '%s'\n",
-					__func__, (char *)rprop->value);
-				continue;
-			}
+		if (!prop)
+			return -EINVAL;
 
-			/* now find the property */
-			found = false;
-			for_each_property_of_node(refnode, sprop)
-				if (of_prop_cmp(sprop->name, propstr) == 0) {
-					found = true;
-					break;
-				}
+		for (i=0; i < count; i++) {
+			off = be32_to_cpu(((uint32_t *)fixprop->value)[i]);
+			if ((off + 4) > prop->length)
+				return -EINVAL;
 
-			if (!found) {
-				pr_err("%s: Could not find property '%s'\n",
-					__func__, (char *)rprop->value);
-				err = -ENOENT;
-				goto err_fail;
-			}
-
-			phandle = be32_to_cpu(*(uint32_t *)
-					(sprop->value + offset));
-			*(uint32_t *)(sprop->value + offset) =
-				cpu_to_be32(phandle + phandle_delta);
+			phandle = be32_to_cpu(*(uint32_t *)(prop->value + off));
+			phandle += phandle_delta;
+			*(uint32_t *)(prop->value + off) = cpu_to_be32(phandle);
 		}
+	}
 
-		kfree(propval);
+	/*
+	 * These nested loops recurse down two subtrees in parallel, where the
+	 * node names in the two subtrees match.
+	 *
+	 * The roots of the subtrees are the overlay's __local_fixups__ node
+	 * and the overlay's root node.
+	 */
+	for_each_child_of_node(local_fixups, child) {
+
+		for_each_child_of_node(overlay, overlay_child)
+			if (!of_node_cmp(child->name, overlay_child->name))
+				break;
+
+		if (!overlay_child)
+			return -EINVAL;
+
+		err = of_adjust_tree_phandle_references(child, overlay_child,
+				phandle_delta);
+		if (err)
+			return err;
 	}
 
 	return 0;
-
-err_fail:
-	kfree(propval);
-	return err;
 }
 
 /**
@@ -185,7 +149,7 @@ err_fail:
  */
 int of_resolve(struct device_node *resolve)
 {
-	struct device_node *child, *refnode;
+	struct device_node *child, *refnode, *local_fixups;
 	struct device_node *root_sym, *resolve_sym, *resolve_fix;
 	struct property *rprop, *sprop;
 	const char *refpath;
@@ -203,7 +167,15 @@ int of_resolve(struct device_node *resolve)
 	/* first we need to adjust the phandles */
 	phandle_delta = of_get_tree_max_phandle(NULL) + 1;
 	of_adjust_tree_phandles(resolve, phandle_delta);
-	err = of_adjust_tree_phandle_references(resolve, phandle_delta);
+
+	/* second we need lookup local fixups of phandles */
+	for_each_child_of_node(resolve, local_fixups) {
+		if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
+			break;
+	}
+
+	err = of_adjust_tree_phandle_references(local_fixups, resolve,
+			phandle_delta);
 	if (err != 0)
 		return err;
 
Signature · 7 lines
2.17.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 2/3 scripts/dtc: Update to upstream version 1.4.6

In reply to Pascal Vizeli
---
 scripts/dtc/.gitignore               |    1 -
 scripts/dtc/Makefile                 |    8 +-
 scripts/dtc/checks.c                 | 1194 +++++++++++++++++++++-----
 scripts/dtc/data.c                   |   16 +-
 scripts/dtc/dtc-lexer.l              |   63 +-
 scripts/dtc/dtc-lexer.lex.c_shipped  |  894 +++++++++----------
 scripts/dtc/dtc-parser.tab.c_shipped |  813 ++++++++++--------
 scripts/dtc/dtc-parser.tab.h_shipped |   54 +-
 scripts/dtc/dtc-parser.y             |   91 +-
 scripts/dtc/dtc.c                    |  139 ++-
 scripts/dtc/dtc.h                    |   62 +-
 scripts/dtc/fdt.c                    |   24 +-
 scripts/dtc/fdt.h                    |    6 +-
 scripts/dtc/fdt_empty_tree.c         |    1 -
 scripts/dtc/fdt_overlay.c            |  912 ++++++++++++++++++++
 scripts/dtc/fdt_ro.c                 |  288 ++++++-
 scripts/dtc/fdt_rw.c                 |  121 +--
 scripts/dtc/fdt_strerror.c           |    6 +
 scripts/dtc/fdt_sw.c                 |   40 +-
 scripts/dtc/fdt_wip.c                |   43 +-
 scripts/dtc/fdtdump.c                |  136 +--
 scripts/dtc/fdtget.c                 |   60 +-
 scripts/dtc/fdtput.c                 |  142 ++-
 scripts/dtc/flattree.c               |  105 +--
 scripts/dtc/fstree.c                 |    5 +-
 scripts/dtc/libfdt.h                 |  429 +++++++--
 scripts/dtc/libfdt_env.h             |   60 +-
 scripts/dtc/libfdt_internal.h        |   32 +-
 scripts/dtc/livetree.c               |  356 +++++++-
 scripts/dtc/srcpos.c                 |   42 +-
 scripts/dtc/srcpos.h                 |   18 +-
 scripts/dtc/treesource.c             |   20 +-
 scripts/dtc/update-dtc-source.sh     |   46 +-
 scripts/dtc/util.c                   |   44 +-
 scripts/dtc/util.h                   |   33 +-
 scripts/dtc/version_gen.h            |    2 +-
 36 files changed, 4578 insertions(+), 1728 deletions(-)
 create mode 100644 scripts/dtc/fdt_overlay.c
scripts/dtc/.gitignore +0 -1
index 80f6b50fd..cdabdc95a 100644
@@ -2,4 +2,3 @@ dtc
 dtc-lexer.lex.c
 dtc-parser.tab.c
 dtc-parser.tab.h
-fdtget
scripts/dtc/Makefile +3 -5
index 05973b12a..06aaa8c55 100644
@@ -1,17 +1,15 @@
 # scripts/dtc makefile
 
-hostprogs-y	:= dtc fdtget
+hostprogs-y	:= dtc
 always		:= $(hostprogs-y)
 
 dtc-objs	:= dtc.o flattree.o fstree.o data.o livetree.o treesource.o \
 		   srcpos.o checks.o util.o
 dtc-objs	+= dtc-lexer.lex.o dtc-parser.tab.o
 
-libfdt-objs	= fdt.o fdt_ro.o fdt_strerror.o fdt_wip.o
+libfdt-objs	= fdt.o fdt_ro.o fdt_strerror.o fdt_wip.o fdt_overlay.o
 libfdt-objs	+= fdt_empty_tree.o fdt_rw.o fdt_sw.o
 
-fdtget-objs	+= fdtget.o $(libfdt-objs) util.o
-
 # Source files need to get at the userspace version of libfdt_env.h to compile
 
 HOSTCFLAGS_DTC := -I$(src)
@@ -33,7 +31,7 @@ HOSTCFLAGS_fdt_wip.o := $(HOSTCFLAGS_DTC)
 HOSTCFLAGS_fdt_empty_tree.o := $(HOSTCFLAGS_DTC)
 HOSTCFLAGS_fdt_rw.o := $(HOSTCFLAGS_DTC)
 HOSTCFLAGS_fdt_sw.o := $(HOSTCFLAGS_DTC)
-HOSTCFLAGS_fdtget.o := $(HOSTCFLAGS_DTC)
+HOSTCFLAGS_fdt_overlay.o := $(HOSTCFLAGS_DTC)
 
 HOSTCFLAGS_dtc-lexer.lex.o := $(HOSTCFLAGS_DTC)
 HOSTCFLAGS_dtc-parser.tab.o := $(HOSTCFLAGS_DTC)
scripts/dtc/checks.c +970 -224

1,515 lines not on this page · raw message

scripts/dtc/data.c +8 -8
index 8cae23746..aa37a16c8 100644
@@ -171,9 +171,9 @@ struct data data_merge(struct data d1, struct data d2)
 struct data data_append_integer(struct data d, uint64_t value, int bits)
 {
 	uint8_t value_8;
-	uint16_t value_16;
-	uint32_t value_32;
-	uint64_t value_64;
+	fdt16_t value_16;
+	fdt32_t value_32;
+	fdt64_t value_64;
 
 	switch (bits) {
 	case 8:
@@ -197,14 +197,14 @@ struct data data_append_integer(struct data d, uint64_t value, int bits)
 	}
 }
 
-struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
+struct data data_append_re(struct data d, uint64_t address, uint64_t size)
 {
-	struct fdt_reserve_entry bere;
+	struct fdt_reserve_entry re;
 
-	bere.address = cpu_to_fdt64(re->address);
-	bere.size = cpu_to_fdt64(re->size);
+	re.address = cpu_to_fdt64(address);
+	re.size = cpu_to_fdt64(size);
 
-	return data_append_data(d, &bere, sizeof(bere));
+	return data_append_data(d, &re, sizeof(re));
 }
 
 struct data data_append_cell(struct data d, cell_t word)
scripts/dtc/dtc-lexer.l +40 -23

100 lines not on this page · raw message

scripts/dtc/dtc-lexer.lex.c_shipped +456 -438

1,323 lines not on this page · raw message

scripts/dtc/dtc-parser.tab.c_shipped +453 -360

813 lines not on this page · raw message

scripts/dtc/dtc-parser.tab.h_shipped +29 -25

54 lines not on this page · raw message

scripts/dtc/dtc-parser.y +82 -9

91 lines not on this page · raw message

scripts/dtc/dtc.c +121 -18

139 lines not on this page · raw message

scripts/dtc/dtc.h +43 -19

62 lines not on this page · raw message

scripts/dtc/fdt.c +14 -10
index 2ce6a4417..7855a1787 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/fdt.h +3 -3
index 526aedb51..74961f902 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/fdt_empty_tree.c +0 -1
index f72d13b1d..f2ae9b77c 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/fdt_overlay.c new file +912 -0

912 lines not on this page · raw message

scripts/dtc/fdt_ro.c +244 -44

288 lines not on this page · raw message

scripts/dtc/fdt_rw.c +67 -54

121 lines not on this page · raw message

scripts/dtc/fdt_strerror.c +6 -0
index e6c3ceee8..9677a1887 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/fdt_sw.c +26 -14
index 6a804859f..6d33cc29d 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/fdt_wip.c +32 -11

43 lines not on this page · raw message

scripts/dtc/fdtdump.c +36 -100

136 lines not on this page · raw message

scripts/dtc/fdtget.c +33 -27

60 lines not on this page · raw message

scripts/dtc/fdtput.c +54 -88

142 lines not on this page · raw message

scripts/dtc/flattree.c +56 -49

105 lines not on this page · raw message

scripts/dtc/fstree.c +2 -3
index 6d1beec95..ae7d06c3c 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/libfdt.h +378 -51

429 lines not on this page · raw message

scripts/dtc/libfdt_env.h +44 -16

60 lines not on this page · raw message

scripts/dtc/libfdt_internal.h +16 -16
index 02cfa6fb6..7681e1922 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/livetree.c +329 -27

356 lines not on this page · raw message

scripts/dtc/srcpos.c +9 -33

42 lines not on this page · raw message

scripts/dtc/srcpos.h +8 -10
index f81827bd6..9ded12a38 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/treesource.c +10 -10
index a55d1d128..2461a3d06 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/update-dtc-source.sh +32 -14

46 lines not on this page · raw message

scripts/dtc/util.c +37 -7

44 lines not on this page · raw message

scripts/dtc/util.h +24 -9
index ccfdf4b12..66fba8ea7 100644

Lines of this file are not shown. View the raw message.

scripts/dtc/version_gen.h +1 -1
index 607afac1c..89d4e0ad1 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 7 lines
2.17.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Part of the diff is not shown here. View the raw message.

#3 lore raw
In reply to Pascal Vizeli
Quoted 31 lines · Pascal Vizeli
Documentation/user/devicetree.rst +15 -1
index 17934d86e..b2220997d 100644
@@ -21,7 +21,7 @@ The internal devicetree
 -----------------------
 
 The devicetree consulted by barebox plays a special role. It is referred to
-as the "internal devicetree." The barebox devicetree commands work on this
+as the "internal devicetree". The barebox devicetree commands work on this
 devicetree. The devicetree source (DTS) files are kept in sync with the kernel DTS
 files. As the FDT files are meant to be backward compatible, it should always be possible
 to start a kernel with the barebox internal devicetree. However, since the barebox
@@ -83,3 +83,102 @@ you can exchange the internal devicetree during runtime using the
 
    oftree -f
    oftree -l /new/dtb
+
+Devicetree overlays
+-------------------
+
+Since version 3.19, the Linux kernel supports applying "devicetree overlays" to
+its loaded device tree. This can be used to inform the kernel about additional
+non-discoverable devices after the system has booted, which is useful for modular
+boards and FPGAs. The details of the overlay format are specified in the Linux
+`kernel documentation <https://www.kernel.org/doc/Documentation/devicetree/overlay-notes.txt>`_
+and an updated DTC is required to compile the overlays.
+
+The use cases for overlays in barebox are a bit different:
+
+* some of the modular devices are needed to boot Linux to userspace, but barebox

Documentation/user/devicetree.rst you can exchange the internal devicetree during runtime using the · line 109

Quoted 10 lines
Documentation/user/devicetree.rst +10 -0
+  can detect which module variant is connected
+* one of several parallel or LVDS displays (which use timing data from devicetree)
+  can be connected to the SoC and should be used for boot messages
+* a generic Linux (distribution) kernel should be booted on a modular
+  system and support additional hardware on modules
+
+barebox supports applying overlays in the internal devicetree was well using the
+:ref:`command_oftree` command with option ``-o``:
+
+.. code-block:: sh

That highlighting looks weird with mixed languages. I suggest

.. code-block:: none

instead. (Ideally, this should also be used for all the other code blocks on that page, IMO ☺)

- Roland

Quoted 75 lines
Documentation/user/devicetree.rst +75 -0
+
+   $ ./barebox -d arch/sandbox/dts/sandbox.dtb -i arch/sandbox/dts/sandbox-overlay.dtbo
+   add fd0 backed by file arch/sandbox/dts/sandbox-overlay.dtbo
+
+   barebox 2015.02.0 #26 Wed Mar 4 09:41:19 CET 2015
+   ...
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       status = "disabled";
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+     };
+     dummy@1 {
+       status = "disabled";
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+   barebox@barebox sandbox:/ of_dump -f /dev/fd0
+    {
+     fragment@0 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     fragment@1 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     __fixups__ {
+       dummy0 = "/fragment@0:target:0";
+       dummy1 = "/fragment@1:target:0";
+     };
+   };
+   barebox@barebox sandbox:/ oftree -o /dev/fd0
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     dummy@1 {
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+
+If you need to use a different base devicetree instead of the one compiled into
+barebox, it needs to be replaced as described in the previous section.

[...]

Signature · 9 lines
Roland Hieber                     | [email protected]     |
Pengutronix e.K.                  | https://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim | Phone: +49-5121-206917-5086 |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
In reply to Pascal Vizeli
Quoted 1 line · Pascal Vizeli

This lacks the original Signed-off-by tag from Jan. In case Jan didn't provide a Signed-off-by tag please ignore.

What are your changes compared to Jans version? Just rebased or more?

Quoted 214 lines
---
 Documentation/user/devicetree.rst | 101 ++++++++-
 arch/sandbox/dts/Makefile         |   5 +-
 arch/sandbox/dts/sandbox.dts      |   6 +
 commands/oftree.c                 |  65 +++++-
 drivers/of/Kconfig                |   6 +
 drivers/of/Makefile               |   1 +
 drivers/of/overlay.c              | 234 ++++++++++++++++++++
 drivers/of/resolver.c             | 346 ++++++++++++++++++++++++++++++
 include/of.h                      |  71 ++++++
 scripts/Makefile.lib              |   5 +-
 10 files changed, 831 insertions(+), 9 deletions(-)
 create mode 100644 drivers/of/overlay.c
 create mode 100644 drivers/of/resolver.c
Documentation/user/devicetree.rst +100 -1
index 17934d86e..b2220997d 100644
@@ -21,7 +21,7 @@ The internal devicetree
 -----------------------
 
 The devicetree consulted by barebox plays a special role. It is referred to
-as the "internal devicetree." The barebox devicetree commands work on this
+as the "internal devicetree". The barebox devicetree commands work on this
 devicetree. The devicetree source (DTS) files are kept in sync with the kernel DTS
 files. As the FDT files are meant to be backward compatible, it should always be possible
 to start a kernel with the barebox internal devicetree. However, since the barebox
@@ -83,3 +83,102 @@ you can exchange the internal devicetree during runtime using the
 
    oftree -f
    oftree -l /new/dtb
+
+Devicetree overlays
+-------------------
+
+Since version 3.19, the Linux kernel supports applying "devicetree overlays" to
+its loaded device tree. This can be used to inform the kernel about additional
+non-discoverable devices after the system has booted, which is useful for modular
+boards and FPGAs. The details of the overlay format are specified in the Linux
+`kernel documentation <https://www.kernel.org/doc/Documentation/devicetree/overlay-notes.txt>`_
+and an updated DTC is required to compile the overlays.
+
+The use cases for overlays in barebox are a bit different:
+
+* some of the modular devices are needed to boot Linux to userspace, but barebox
+  can detect which module variant is connected
+* one of several parallel or LVDS displays (which use timing data from devicetree)
+  can be connected to the SoC and should be used for boot messages
+* a generic Linux (distribution) kernel should be booted on a modular
+  system and support additional hardware on modules
+
+barebox supports applying overlays in the internal devicetree was well using the
+:ref:`command_oftree` command with option ``-o``:
+
+.. code-block:: sh
+
+   $ ./barebox -d arch/sandbox/dts/sandbox.dtb -i arch/sandbox/dts/sandbox-overlay.dtbo
+   add fd0 backed by file arch/sandbox/dts/sandbox-overlay.dtbo
+
+   barebox 2015.02.0 #26 Wed Mar 4 09:41:19 CET 2015
+   ...
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       status = "disabled";
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+     };
+     dummy@1 {
+       status = "disabled";
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+   barebox@barebox sandbox:/ of_dump -f /dev/fd0
+    {
+     fragment@0 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     fragment@1 {
+       target = <0xdeadbeef>;
+       __overlay__ {
+         status = "okay";
+         child {
+           compatible = "barebox,dummy";
+         };
+       };
+     };
+     __fixups__ {
+       dummy0 = "/fragment@0:target:0";
+       dummy1 = "/fragment@1:target:0";
+     };
+   };
+   barebox@barebox sandbox:/ oftree -o /dev/fd0
+   barebox@barebox sandbox:/ of_dump
+   ...
+     dummy@0 {
+       linux,phandle = <0x1>;
+       phandle = <0x1>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     dummy@1 {
+       linux,phandle = <0x2>;
+       phandle = <0x2>;
+       status = "okay";
+       child {
+         compatible = "barebox,dummy";
+       };
+     };
+     __symbols__ {
+       dummy0 = "/dummy@0";
+       dummy1 = "/dummy@1";
+     };
+   ...
+
+If you need to use a different base devicetree instead of the one compiled into
+barebox, it needs to be replaced as described in the previous section.
arch/sandbox/dts/Makefile +3 -2
index 6f6838857..ede219e6f 100644
@@ -1,6 +1,7 @@
 ifeq ($(CONFIG_OFTREE),y)
 dtb-y += \
-	sandbox.dtb
+	sandbox.dtb \
+	sandbox-overlay.dtbo
 endif
 
 # just to build a built-in.o. Otherwise compilation fails when no devicetree is
@@ -8,4 +9,4 @@ endif
 obj- += dummy.o
 
 always := $(dtb-y)
-clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts
+clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtbo
arch/sandbox/dts/sandbox.dts +6 -0
index 2595aa13f..1b99a4960 100644
@@ -3,5 +3,11 @@
 #include "skeleton.dtsi"
 
 / {
+	dummy0: dummy@0 {
+		status = "disabled";
+	};
 
+	dummy1: dummy@1 {
+		status = "disabled";
+	};
 };
commands/oftree.c +9 -2
index 8a47c0be5..e0624ad34 100644
@@ -2,6 +2,7 @@
  * oftree.c - device tree command support
  *
  * Copyright (c) 2011 Sascha Hauer <[email protected]>, Pengutronix
+ * Copyright (C) 2015 Pengutronix, Jan Luebbe <[email protected]>
  *
  * based on U-Boot code by:
  *
@@ -48,11 +49,14 @@ static int do_oftree(int argc, char *argv[])
 	int probe = 0;
 	char *load = NULL;
 	char *save = NULL;
+	char *overlay = NULL;
 	int free_of = 0;
 	int ret;
 	struct device_node *root;
+	int ovinfo_cnt;
+	struct of_overlay_info *ovinfo;
 
-	while ((opt = getopt(argc, argv, "pfl:s:")) > 0) {
+	while ((opt = getopt(argc, argv, "pfl:o:s:")) > 0) {
 		switch (opt) {
 		case 'l':
 			load = optarg;
@@ -68,6 +72,9 @@ static int do_oftree(int argc, char *argv[])
 		case 'f':
 			free_of = 1;
 			break;
+		case 'o':
+			overlay = optarg;
+			break;
 		case 's':
 			save = optarg;
 			break;
@@ -84,7 +91,7 @@ static int do_oftree(int argc, char *argv[])
 			return 0;
 	}
 
-	if (!probe && !load && !save)
+	if (!probe && !load && !save && !overlay)
 		return COMMAND_ERROR_USAGE;
 
 	if (save) {

commands/oftree.c static int do_oftree(int argc, char *argv[]) · line 133

Quoted 4 lines
commands/oftree.c +1 -0
@@ -123,6 +130,53 @@ static int do_oftree(int argc, char *argv[])
 		}
 	}
 
+	if (IS_ENABLED(CONFIG_OFTREE_OVERLAY) && overlay) {

Better make the overlay handling code a separate function, the result is probably a bit better readable.

Quoted 35 lines
commands/oftree.c +35 -0
+		struct device_node *ov;
+
+		root = of_get_root_node();
+		if (!root) {
+			printf("no oftree loaded\n");
+			goto out;
+		}
+
+		fdt = read_file(overlay, &size);
+		if (!fdt) {
+			printf("unable to read %s\n", overlay);
+			return 1;
+		}
+
+		ov = of_unflatten_dtb(fdt);
+		free(fdt);
+
+		if (IS_ERR(ov)) {
+			printf("parse oftree: %s\n", strerror(-ret));
+			return PTR_ERR(ov);
+		}
+
+		ret = of_resolve(ov);
+		if (ret) {
+			printf("resolve oftree overlay: %s\n", strerror(-ret));
+			of_delete_node(ov);
+			goto out;
+		}
+
+		ret = of_build_overlay_info(ov, &ovinfo_cnt, &ovinfo);
+		if (ret) {
+			printf("prepare oftree overlay: %s\n", strerror(-ret));
+			of_delete_node(ov);
+			goto out;
+		}

commands/oftree.c static int do_oftree(int argc, char *argv[]) · line 178

Quoted 10 lines
commands/oftree.c +10 -0
+
+		ret = of_overlay(ovinfo_cnt, ovinfo);
+		if (ret) {
+			printf("apply oftree overlay: %s\n", strerror(-ret));
+			of_delete_node(ov);
+			goto out;
+		}
+
+		of_delete_node(ov);
+	}

It's probably an interesting option to be able to apply the overlay to the devicetree the kernel is started with rather than the live tree.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox