Skip to contentSkip to conversation

drivers: of: implement overlay support

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

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

PATCH 1/2 drivers: of: implement overlay support

Origin by Jan Luebbe:
http://lists.infradead.org/pipermail/barebox/2015-March/022674.html

I rebase the patches and fix the resolve for __local_fixups__, they was wrong copy/ported from linux kernel.

I merge the overlay code from oftree into own command: of_overlay

---
 Documentation/user/devicetree.rst     | 101 ++++++++-
 arch/sandbox/dts/Makefile             |   5 +-
 arch/sandbox/dts/sandbox-overlay.dtso |  26 +++
 arch/sandbox/dts/sandbox.dts          |   6 +
 commands/Kconfig                      |  10 +
 commands/Makefile                     |   1 +
 commands/of_overlay.c                 | 106 +++++++++
 drivers/of/Kconfig                    |   6 +
 drivers/of/Makefile                   |   1 +
 drivers/of/overlay.c                  | 234 +++++++++++++++++++
 drivers/of/resolver.c                 | 314 ++++++++++++++++++++++++++
 include/of.h                          |  71 ++++++
 scripts/Makefile.lib                  |   5 +-
 13 files changed, 882 insertions(+), 4 deletions(-)
 create mode 100644 arch/sandbox/dts/sandbox-overlay.dtso
 create mode 100644 commands/of_overlay.c
 create mode 100644 drivers/of/overlay.c
 create mode 100644 drivers/of/resolver.c
Documentation/user/devicetree.rst +100 -1
index 17934d86e..8b7be4f5b 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:: none
+
+   $ ./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:/ of_overlay /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-overlay.dtso new file +26 -0
new file mode 100644
index 000000000..f9ced04ca
@@ -0,0 +1,26 @@
+/dts-v1/;
+
+/plugin/;
+
+/ {
+        fragment@0 {
+                target = <&dummy0>;
+                __overlay__ {
+			status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+
+        fragment@1 {
+                target = <&dummy1>;
+                __overlay__ {
+			status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+};
+
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/Kconfig +10 -0
index 951a86963..22d131da3 100644
@@ -2033,6 +2033,16 @@ config CMD_OF_NODE
 		  -c	create a new node
 		  -d	delete a node
 
+config CMD_OF_OVERLAY
+	tristate
+	select OFTREE
+	select OFTREE_OVERLAY
+	prompt "of_overlay"
+	help
+	  Apply a dtb overlay to internal device tree
+
+	  Usage: of_overlay DTBO
+
 config CMD_OF_PROPERTY
 	tristate
 	select OFTREE
commands/Makefile +1 -0
index eb4796389..f404338fa 100644
@@ -75,6 +75,7 @@ obj-$(CONFIG_CMD_USB)		+= usb.o
 obj-$(CONFIG_CMD_TIME)		+= time.o
 obj-$(CONFIG_CMD_OFTREE)	+= oftree.o
 obj-$(CONFIG_CMD_OF_PROPERTY)	+= of_property.o
+obj-$(CONFIG_CMD_OF_OVERLAY)	+= of_overlay.o
 obj-$(CONFIG_CMD_OF_NODE)	+= of_node.o
 obj-$(CONFIG_CMD_OF_DUMP)	+= of_dump.o
 obj-$(CONFIG_CMD_OF_DISPLAY_TIMINGS)	+= of_display_timings.o
commands/of_overlay.c new file +106 -0

106 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 +314 -0

314 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
#1 lore raw

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

---
 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

Hi Pascal.

Quoted 9 lines · Pascal Vizeli

Origin by Jan Luebbe:
http://lists.infradead.org/pipermail/barebox/2015-March/022674.html

I rebase the patches and fix the resolve for __local_fixups__, they was wrong copy/ported from linux kernel.

I merge the overlay code from oftree into own command: of_overlay

You left out the "Signed-off-by: Jan ..."
Is this because you fundamentally changed the patches, then please say so. Otherwise add it.

This comment from Jans changelog is missing:
This is based on Pantelis Antoniou's overlay support for the kernel. It has been simplified by leaving out transaction support, locking and other details required by the Linux DT support.

Is this because the above info is no longer valid? Otherwise include it.

Quoted 242 lines
---
 Documentation/user/devicetree.rst     | 101 ++++++++-
 arch/sandbox/dts/Makefile             |   5 +-
 arch/sandbox/dts/sandbox-overlay.dtso |  26 +++
 arch/sandbox/dts/sandbox.dts          |   6 +
 commands/Kconfig                      |  10 +
 commands/Makefile                     |   1 +
 commands/of_overlay.c                 | 106 +++++++++
 drivers/of/Kconfig                    |   6 +
 drivers/of/Makefile                   |   1 +
 drivers/of/overlay.c                  | 234 +++++++++++++++++++
 drivers/of/resolver.c                 | 314 ++++++++++++++++++++++++++
 include/of.h                          |  71 ++++++
 scripts/Makefile.lib                  |   5 +-
 13 files changed, 882 insertions(+), 4 deletions(-)
 create mode 100644 arch/sandbox/dts/sandbox-overlay.dtso
 create mode 100644 commands/of_overlay.c
 create mode 100644 drivers/of/overlay.c
 create mode 100644 drivers/of/resolver.c
Documentation/user/devicetree.rst +100 -1
index 17934d86e..8b7be4f5b 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:: none
+
+   $ ./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:/ of_overlay /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-overlay.dtso new file +26 -0
new file mode 100644
index 000000000..f9ced04ca
@@ -0,0 +1,26 @@
+/dts-v1/;
+
+/plugin/;
+
+/ {
+        fragment@0 {
+                target = <&dummy0>;
+                __overlay__ {
+			status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+
+        fragment@1 {
+                target = <&dummy1>;
+                __overlay__ {
+			status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+};
+
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/Kconfig +10 -0
index 951a86963..22d131da3 100644
@@ -2033,6 +2033,16 @@ config CMD_OF_NODE
 		  -c	create a new node
 		  -d	delete a node
 
+config CMD_OF_OVERLAY
+	tristate
+	select OFTREE
+	select OFTREE_OVERLAY
+	prompt "of_overlay"
+	help
+	  Apply a dtb overlay to internal device tree
+
+	  Usage: of_overlay DTBO
+
 config CMD_OF_PROPERTY
 	tristate
 	select OFTREE
commands/Makefile +1 -0
index eb4796389..f404338fa 100644
@@ -75,6 +75,7 @@ obj-$(CONFIG_CMD_USB)		+= usb.o
 obj-$(CONFIG_CMD_TIME)		+= time.o
 obj-$(CONFIG_CMD_OFTREE)	+= oftree.o
 obj-$(CONFIG_CMD_OF_PROPERTY)	+= of_property.o
+obj-$(CONFIG_CMD_OF_OVERLAY)	+= of_overlay.o
 obj-$(CONFIG_CMD_OF_NODE)	+= of_node.o
 obj-$(CONFIG_CMD_OF_DUMP)	+= of_dump.o
 obj-$(CONFIG_CMD_OF_DISPLAY_TIMINGS)	+= of_display_timings.o
commands/of_overlay.c new file +0 -0
new file mode 100644
index 000000000..6c74c72bc

commands/of_overlay.c line 4

Quoted 4 lines
commands/of_overlay.c new file +4 -0
@@ -0,0 +1,106 @@
+/*
+ * of_overlay.c - device tree overlay command support
+ *
+ * Copyright (c) 2018 Pascal Vizeli <[email protected]>, Hass.io

Most of the code was made by Jan,
so you should keep his Copyright.

Quoted 33 lines
commands/of_overlay.c new file +33 -0
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <environment.h>
+#include <fdt.h>
+#include <libfile.h>
+#include <of.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <asm/byteorder.h>
+#include <errno.h>
+#include <getopt.h>
+#include <init.h>
+#include <fcntl.h>
+#include <complete.h>
+
+static int do_of_overlay(int argc, char *argv[])

commands/of_overlay.c line 47

Quoted 10 lines
commands/of_overlay.c new file +10 -0
+{
+	struct fdt_header *fdt = NULL;
+	size_t size;
+	char *filename = NULL;
+	int ret = 0, ovinfo_cnt;
+	struct device_node *overlay = NULL, *root;
+	struct of_overlay_info *ovinfo;
+
+	if (argc != 2 || !IS_ENABLED(CONFIG_OFTREE_OVERLAY))
+		return COMMAND_ERROR_USAGE;

CONFIG_OFTREE_OVERLAY is "select" when adding this command, so this check should not be needed.

commands/of_overlay.c line 55

Quoted 8 lines
commands/of_overlay.c new file +8 -0
+
+	filename = argv[1];
+
+	root = of_get_root_node();
+	if (!root) {
+		printf("no oftree loaded\n");
+		goto out;
+	}

This will result in return 0 - which indicate that there was no error. Should be return 1 or something

commands/of_overlay.c line 61

Quoted 6 lines
commands/of_overlay.c new file +6 -0
+
+	fdt = read_file(filename, &size);
+	if (!fdt) {
+		printf("unable to read %s\n", filename);
+		return 1;
+	}

Like here.

commands/of_overlay.c line 71

Quoted 10 lines
commands/of_overlay.c new file +10 -0
+
+	overlay = of_unflatten_dtb(fdt);
+	free(fdt);
+
+	if (IS_ERR(overlay)) {
+		printf("parse oftree: %s\n", strerror(-ret));
+		return PTR_ERR(overlay);
+	}
+
+	ret = of_resolve(overlay);

of_resolve return a negative error in case of errors.

commands/of_overlay.c line 74

Quoted 3 lines
commands/of_overlay.c new file +3 -0
+	if (ret) {
+		printf("resolve oftree overlay: %s\n", strerror(-ret));
+		goto out;

So here we will return a negative number from this command.

commands/of_overlay.c line 81

Quoted 8 lines
commands/of_overlay.c new file +7 -0
+	}
 
+
+	ret = of_build_overlay_info(overlay, &ovinfo_cnt, &ovinfo);
+	if (ret) {
+		printf("prepare oftree overlay: %s\n", strerror(-ret));
+		goto out;
+	}

Likewise

commands/of_overlay.c

Quoted 2 lines
commands/of_overlay.c new file +2 -0
+
+	ret = of_overlay(ovinfo_cnt, ovinfo);

of_overlay may return a positive or negative number

commands/of_overlay.c

Quoted 2 lines
commands/of_overlay.c new file +2 -0
+	if (ret) {
+		printf("apply oftree overlay: %s\n", strerror(-ret));

So this may not print what you want.

Quoted 11 lines
commands/of_overlay.c new file +11 -0
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	of_delete_node(overlay);
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(of_overlay)

commands/of_overlay.c line 106

Quoted 10 lines
commands/of_overlay.c new file +10 -0
+BAREBOX_CMD_HELP_TEXT("Apply a dtb overlay to internal device tree")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(of_overlay)
+	.cmd		= do_of_overlay,
+	BAREBOX_CMD_DESC("Apply a dtb overlay")
+	BAREBOX_CMD_OPTS("DTBO")
+	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_HELP(cmd_of_overlay_help)
+BAREBOX_CMD_END

Sam

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

#4 lore raw
In reply to Sam Ravnborg

I send a new patch with suggested change.
Feel you free to edit the commit message. I have no copy left or other copyrights on my code.

best regards
Pascal

2018-06-05 21:54 GMT+02:00 Sam Ravnborg <[email protected]>:

Quoted 391 lines

Hi Pascal.

Quoted 9 lines · Pascal Vizeli

Origin by Jan Luebbe:
http://lists.infradead.org/pipermail/barebox/2015-March/022674.html

I rebase the patches and fix the resolve for __local_fixups__, they was wrong copy/ported from linux kernel.

I merge the overlay code from oftree into own command: of_overlay

You left out the "Signed-off-by: Jan ..."
Is this because you fundamentally changed the patches, then please say so. Otherwise add it.

This comment from Jans changelog is missing:
This is based on Pantelis Antoniou's overlay support for the kernel. It has been simplified by leaving out transaction support, locking and other details required by the Linux DT support.

Is this because the above info is no longer valid? Otherwise include it.

Quoted 242 lines
---
 Documentation/user/devicetree.rst     | 101 ++++++++-
 arch/sandbox/dts/Makefile             |   5 +-
 arch/sandbox/dts/sandbox-overlay.dtso |  26 +++
 arch/sandbox/dts/sandbox.dts          |   6 +
 commands/Kconfig                      |  10 +
 commands/Makefile                     |   1 +
 commands/of_overlay.c                 | 106 +++++++++
 drivers/of/Kconfig                    |   6 +
 drivers/of/Makefile                   |   1 +
 drivers/of/overlay.c                  | 234 +++++++++++++++++++
 drivers/of/resolver.c                 | 314 ++++++++++++++++++++++++++
 include/of.h                          |  71 ++++++
 scripts/Makefile.lib                  |   5 +-
 13 files changed, 882 insertions(+), 4 deletions(-)
 create mode 100644 arch/sandbox/dts/sandbox-overlay.dtso
 create mode 100644 commands/of_overlay.c
 create mode 100644 drivers/of/overlay.c
 create mode 100644 drivers/of/resolver.c
Documentation/user/devicetree.rst +100 -1
index 17934d86e..8b7be4f5b 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:: none
+
+   $ ./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:/ of_overlay /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-overlay.dtso new file +26 -0
new file mode 100644
index 000000000..f9ced04ca
@@ -0,0 +1,26 @@
+/dts-v1/;
+
+/plugin/;
+
+/ {
+        fragment@0 {
+                target = <&dummy0>;
+                __overlay__ {
+                     status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+
+        fragment@1 {
+                target = <&dummy1>;
+                __overlay__ {
+                     status = "okay";
+                        child {
+                                compatible = "barebox,dummy";
+                        };
+                };
+        };
+};
+
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/Kconfig +10 -0
index 951a86963..22d131da3 100644
@@ -2033,6 +2033,16 @@ config CMD_OF_NODE
                -c    create a new node
                -d    delete a node
 
+config CMD_OF_OVERLAY
+     tristate
+     select OFTREE
+     select OFTREE_OVERLAY
+     prompt "of_overlay"
+     help
+       Apply a dtb overlay to internal device tree
+
+       Usage: of_overlay DTBO
+
 config CMD_OF_PROPERTY
      tristate
      select OFTREE
commands/Makefile +1 -0
index eb4796389..f404338fa 100644
@@ -75,6 +75,7 @@ obj-$(CONFIG_CMD_USB)               += usb.o
 obj-$(CONFIG_CMD_TIME)               += time.o
 obj-$(CONFIG_CMD_OFTREE)     += oftree.o
 obj-$(CONFIG_CMD_OF_PROPERTY)        += of_property.o
+obj-$(CONFIG_CMD_OF_OVERLAY) += of_overlay.o
 obj-$(CONFIG_CMD_OF_NODE)    += of_node.o
 obj-$(CONFIG_CMD_OF_DUMP)    += of_dump.o
 obj-$(CONFIG_CMD_OF_DISPLAY_TIMINGS) += of_display_timings.o
commands/of_overlay.c new file +0 -0
new file mode 100644
index 000000000..6c74c72bc

commands/of_overlay.c line 4

Quoted 4 lines
commands/of_overlay.c new file +4 -0
@@ -0,0 +1,106 @@
+/*
+ * of_overlay.c - device tree overlay command support
+ *
+ * Copyright (c) 2018 Pascal Vizeli <[email protected]>, Hass.io

Most of the code was made by Jan,
so you should keep his Copyright.

Quoted 33 lines
commands/of_overlay.c new file +33 -0
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <environment.h>
+#include <fdt.h>
+#include <libfile.h>
+#include <of.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <asm/byteorder.h>
+#include <errno.h>
+#include <getopt.h>
+#include <init.h>
+#include <fcntl.h>
+#include <complete.h>
+
+static int do_of_overlay(int argc, char *argv[])

commands/of_overlay.c line 47

Quoted 10 lines
commands/of_overlay.c new file +10 -0
+{
+     struct fdt_header *fdt = NULL;
+     size_t size;
+     char *filename = NULL;
+     int ret = 0, ovinfo_cnt;
+     struct device_node *overlay = NULL, *root;
+     struct of_overlay_info *ovinfo;
+
+     if (argc != 2 || !IS_ENABLED(CONFIG_OFTREE_OVERLAY))
+             return COMMAND_ERROR_USAGE;

CONFIG_OFTREE_OVERLAY is "select" when adding this command, so this check should not be needed.

commands/of_overlay.c line 55

Quoted 8 lines
commands/of_overlay.c new file +8 -0
+
+     filename = argv[1];
+
+     root = of_get_root_node();
+     if (!root) {
+             printf("no oftree loaded\n");
+             goto out;
+     }

This will result in return 0 - which indicate that there was no error. Should be return 1 or something

commands/of_overlay.c line 61

Quoted 6 lines
commands/of_overlay.c new file +6 -0
+
+     fdt = read_file(filename, &size);
+     if (!fdt) {
+             printf("unable to read %s\n", filename);
+             return 1;
+     }

Like here.

commands/of_overlay.c line 71

Quoted 10 lines
commands/of_overlay.c new file +10 -0
+
+     overlay = of_unflatten_dtb(fdt);
+     free(fdt);
+
+     if (IS_ERR(overlay)) {
+             printf("parse oftree: %s\n", strerror(-ret));
+             return PTR_ERR(overlay);
+     }
+
+     ret = of_resolve(overlay);

of_resolve return a negative error in case of errors.

commands/of_overlay.c line 74

Quoted 3 lines
commands/of_overlay.c new file +3 -0
+     if (ret) {
+             printf("resolve oftree overlay: %s\n", strerror(-ret));
+             goto out;

So here we will return a negative number from this command.

commands/of_overlay.c line 81

Quoted 8 lines
commands/of_overlay.c new file +7 -0
+     }
 
+
+     ret = of_build_overlay_info(overlay, &ovinfo_cnt, &ovinfo);
+     if (ret) {
+             printf("prepare oftree overlay: %s\n", strerror(-ret));
+             goto out;
+     }

Likewise

commands/of_overlay.c

Quoted 2 lines
commands/of_overlay.c new file +2 -0
+
+     ret = of_overlay(ovinfo_cnt, ovinfo);

of_overlay may return a positive or negative number

commands/of_overlay.c

Quoted 2 lines
commands/of_overlay.c new file +2 -0
+     if (ret) {
+             printf("apply oftree overlay: %s\n", strerror(-ret));

So this may not print what you want.

Quoted 16 lines
commands/of_overlay.c new file +16 -0
+             goto out;
+     }
+
+     ret = 0;
+
+out:
+     of_delete_node(overlay);
+     return ret;
+}
+
+BAREBOX_CMD_HELP_START(of_overlay)
+BAREBOX_CMD_HELP_TEXT("Apply a dtb overlay to internal device tree")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(of_overlay)
+     .cmd            = do_of_overlay,
Quoted 8 lines

commands/of_overlay.c line 106

Quoted 5 lines
commands/of_overlay.c new file +5 -0
+     BAREBOX_CMD_DESC("Apply a dtb overlay")
+     BAREBOX_CMD_OPTS("DTBO")
+     BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+     BAREBOX_CMD_HELP(cmd_of_overlay_help)
+BAREBOX_CMD_END

Sam

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