From: Michael Tretter <m.tretter@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Michael Tretter <m.tretter@pengutronix.de>
Subject: [PATCH v3 5/8] firmware: add support to load firmware from dt overlay
Date: Fri, 13 Sep 2019 15:14:43 +0200 [thread overview]
Message-ID: <20190913131446.8202-6-m.tretter@pengutronix.de> (raw)
In-Reply-To: <20190913131446.8202-1-m.tretter@pengutronix.de>
fpga-region device tree nodes have the firmware-name property that
contains the file name of firmware in the firmware search path (but not
the path) that shall be loaded before the overlay is applied.
Add the of_firmware_load_overlay() function that accepts an overlay and a
firmware search path, finds the responsible firmware_mgr and loads the
firmware.
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changelog:
v2->v3:
- move functions from firmware to new module of_firmware
- rename function accordingly
- return success whenever there is no firmware in the overlay
v1->v2: none
---
drivers/of/Makefile | 2 +-
drivers/of/of_firmware.c | 86 ++++++++++++++++++++++++++++++++++++++++
include/of.h | 7 ++++
3 files changed, 94 insertions(+), 1 deletion(-)
create mode 100644 drivers/of/of_firmware.c
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 9c6f8de814..b6847752d2 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -6,4 +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_OF_OVERLAY) += overlay.o resolver.o
+obj-$(CONFIG_OF_OVERLAY) += overlay.o resolver.o of_firmware.o
diff --git a/drivers/of/of_firmware.c b/drivers/of/of_firmware.c
new file mode 100644
index 0000000000..0135631fb8
--- /dev/null
+++ b/drivers/of/of_firmware.c
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de>
+ */
+#include <common.h>
+#include <firmware.h>
+#include <of.h>
+
+struct overlay_info {
+ const char *firmware_path;
+};
+
+static struct firmware_mgr *of_node_get_mgr(struct device_node *np)
+{
+ struct device_node *mgr_node;
+
+ do {
+ if (of_device_is_compatible(np, "fpga-region")) {
+ mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
+ if (mgr_node)
+ return firmwaremgr_find_by_node(mgr_node);
+ }
+ } while ((np = of_get_parent(np)) != NULL);
+
+ return NULL;
+}
+
+static int load_firmware(struct device_node *target,
+ struct device_node *fragment, void *data)
+{
+ struct overlay_info *info = data;
+ const char *firmware_name;
+ const char *firmware_path = info->firmware_path;
+ char *firmware;
+ int err;
+ struct firmware_mgr *mgr;
+
+ err = of_property_read_string(fragment,
+ "firmware-name", &firmware_name);
+ /* Nothing to do if property does not exist. */
+ if (err == -EINVAL)
+ return 0;
+ else if (err)
+ return -EINVAL;
+
+ mgr = of_node_get_mgr(target);
+ if (!mgr)
+ return -EINVAL;
+
+ firmware = basprintf("%s/%s", firmware_path, firmware_name);
+ if (!firmware)
+ return -ENOMEM;
+
+ err = firmwaremgr_load_file(mgr, firmware);
+
+ free(firmware);
+
+ return err;
+}
+
+int of_firmware_load_overlay(struct device_node *overlay, const char *path)
+{
+ struct overlay_info info = {
+ .firmware_path = path,
+ };
+ int err;
+ struct device_node *root;
+ struct device_node *resolved;
+ struct device_node *ovl;
+
+ root = of_get_root_node();
+ /*
+ * If we cannot resolve the symbols in the overlay, ensure that the
+ * overlay does depend on firmware to be loaded.
+ */
+ resolved = of_resolve_phandles(root, overlay);
+ ovl = resolved ? resolved : overlay;
+
+ err = of_process_overlay(root, ovl,
+ load_firmware, &info);
+
+ if (resolved)
+ of_delete_node(resolved);
+
+ return err;
+}
diff --git a/include/of.h b/include/of.h
index 06dc9f61a5..5466517a6d 100644
--- a/include/of.h
+++ b/include/of.h
@@ -882,6 +882,8 @@ int of_process_overlay(struct device_node *root,
int (*process)(struct device_node *target,
struct device_node *overlay, void *data),
void *data);
+
+int of_firmware_load_overlay(struct device_node *overlay, const char *path);
#else
static inline struct device_node *of_resolve_phandles(struct device_node *root,
const struct device_node *overlay)
@@ -908,6 +910,11 @@ static inline int of_process_overlay(struct device_node *root,
{
return -ENOSYS;
}
+
+static inline int of_firmware_load_overlay(struct device_node *overlay, const char *path)
+{
+ return -ENOSYS;
+}
#endif
#endif /* __OF_H */
--
2.20.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2019-09-13 13:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-13 13:14 [PATCH v3 0/8] Device Tree Overlay Support Michael Tretter
2019-09-13 13:14 ` [PATCH v3 1/8] of: add support for devicetree overlays Michael Tretter
2019-09-13 13:14 ` [PATCH v3 2/8] blspec: " Michael Tretter
2019-09-13 13:14 ` [PATCH v3 3/8] of: add iterator for overlays Michael Tretter
2019-09-13 13:14 ` [PATCH v3 4/8] firmware: add function to find firmware by devicetree node Michael Tretter
2019-09-13 13:14 ` Michael Tretter [this message]
2019-09-13 13:14 ` [PATCH v3 6/8] blspec: load firmware if specified in dt overlay Michael Tretter
2019-09-13 13:14 ` [PATCH v3 7/8] commands: add of_overlay command for device tree overlays Michael Tretter
2019-09-13 13:14 ` [PATCH v3 8/8] dtc: optionally add add __symbols__ to build-in devicetree Michael Tretter
2019-09-16 7:07 ` [PATCH v3 0/8] Device Tree Overlay Support Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190913131446.8202-6-m.tretter@pengutronix.de \
--to=m.tretter@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox