mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Michael Tretter <m.tretter@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Michael Tretter <m.tretter@pengutronix.de>
Subject: [RFC PATCH 6/7] firmware: add support for fpga-regions
Date: Thu,  4 Apr 2019 16:53:19 +0200	[thread overview]
Message-ID: <20190404145320.11465-7-m.tretter@pengutronix.de> (raw)
In-Reply-To: <20190404145320.11465-1-m.tretter@pengutronix.de>

The Barebox fpga-region driver is merely glue code between the
devicetree overlay support and the various firmware handlers.

The driver registers notifiers for each fpga-region in the devicetree.
If an overlay is registered for a fpga-region, it uses the referenced
firmware handler to load the specified firmware image.

Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
 drivers/firmware/Kconfig          |   7 ++
 drivers/firmware/Makefile         |   1 +
 drivers/firmware/of-fpga-region.c | 153 ++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 drivers/firmware/of-fpga-region.c

diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index 710b500ab0..95167efae8 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -14,4 +14,11 @@ config FIRMWARE_ALTERA_SOCFPGA
 	bool "Altera SoCFPGA fpga loader"
 	depends on ARCH_SOCFPGA
 	select FIRMWARE
+
+config OF_FPGA_REGION
+        tristate "FPGA Region Device Tree Overlay Support"
+        help
+          Support for loading FPGA images by applying a Device Tree
+          overlay.
+
 endmenu
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index c3a3c34004..f648ad6259 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -1,2 +1,3 @@
 obj-$(CONFIG_FIRMWARE_ALTERA_SERIAL) += altera_serial.o
 obj-$(CONFIG_FIRMWARE_ALTERA_SOCFPGA) += socfpga.o
+obj-$(CONFIG_OF_FPGA_REGION) += of-fpga-region.o
diff --git a/drivers/firmware/of-fpga-region.c b/drivers/firmware/of-fpga-region.c
new file mode 100644
index 0000000000..dcc6b66d81
--- /dev/null
+++ b/drivers/firmware/of-fpga-region.c
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FPGA Region - Device Tree support for FPGA programming under Linux
+ *
+ *  Copyright (C) 2013-2016 Altera Corporation
+ *  Copyright (C) 2017 Intel Corporation
+ *  Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de>
+ */
+#include <common.h>
+#include <globalvar.h>
+#include <firmware.h>
+#include <init.h>
+#include <magicvar.h>
+#include <of.h>
+
+char *firmware_path;
+
+struct fpga_region {
+	struct device_d *dev;
+	struct notifier_block nb;
+	struct firmware_mgr *mgr;
+};
+
+static struct firmware_mgr *of_fpga_region_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 of_fpga_region_notify(struct notifier_block *nb,
+				 unsigned long action, void *arg)
+{
+	struct of_overlay_notify_data *nd = arg;
+	struct fpga_region *region = container_of(nb, struct fpga_region, nb);
+	const char *firmware_name;
+	char *firmware;
+	int err;
+
+	if (action != OF_OVERLAY_PRE_APPLY) {
+		dev_dbg(region->dev, "only interested in pre-apply hook\n");
+		return 0;
+	}
+
+	if (nd->target != region->dev->device_node) {
+		dev_dbg(region->dev, "%s is not overlay target %s\n",
+			nd->target->name, region->dev->device_node->name);
+		return 0;
+	}
+
+	err = of_property_read_string(nd->target,
+				      "firmware-name", &firmware_name);
+	if (err != -EINVAL) {
+		dev_dbg(region->dev,
+			"%s has already been programmed with firmware %s\n",
+			nd->target->name, firmware_name);
+		return 0;
+	}
+
+	err = of_property_read_string(nd->overlay,
+				      "firmware-name", &firmware_name);
+	if (err)
+		return err;
+
+	firmware = basprintf("%s/%s", firmware_path, firmware_name);
+	if (!firmware) {
+		err = -ENOMEM;
+		goto out;
+	}
+
+	dev_dbg(region->dev, "programming %s with firmware %s\n",
+		nd->target->name, firmware);
+
+	err = firmwaremgr_load_file(region->mgr, firmware);
+	if (err) {
+		dev_err("programming %s failed: %s\n", strerror(-err));
+		goto out;
+	}
+
+	/*
+	 * Add the firmware-name to the live device tree to indicate that the
+	 * FPGA has been programmed with this image.
+	 */
+	of_property_write_string(nd->target, "firmware-name", firmware_name);
+
+out:
+	free(firmware);
+
+	return err;
+}
+
+static int of_fpga_region_probe(struct device_d *dev)
+{
+	struct device_node *np = dev->device_node;
+	struct fpga_region *region;
+	struct firmware_mgr *mgr;
+	int err;
+
+	mgr = of_fpga_region_get_mgr(np);
+	if (!mgr) {
+		pr_err("cannot find firmware loader for %s\n", np->name);
+		return -EINVAL;
+	}
+
+	region = kzalloc(sizeof(*region), GFP_KERNEL);
+	if (!region)
+		return -ENOMEM;
+
+	region->dev = dev;
+	region->mgr = mgr;
+	region->nb.notifier_call = of_fpga_region_notify;
+
+	err = of_overlay_notifier_register(&region->nb);
+	if (err)
+		goto out;
+
+	return 0;
+
+out:
+	kfree(region);
+	return err;
+}
+
+static const struct of_device_id fpga_region_of_match[] = {
+	{ .compatible = "fpga-region", },
+	{},
+};
+
+static struct driver_d of_fpga_region_driver = {
+	.name = "of-region",
+	.probe = of_fpga_region_probe,
+	.of_compatible = DRV_OF_COMPAT(fpga_region_of_match),
+};
+device_platform_driver(of_fpga_region_driver);
+
+static int fpga_region_init(void)
+{
+	globalvar_add_simple_string("firmware.path", &firmware_path);
+
+	return 0;
+}
+postcore_initcall(fpga_region_init);
+
+BAREBOX_MAGICVAR_NAMED(global_firmware_path, global.firmware.path,
+		       "Firmware search path");
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-04-04 14:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-04 14:53 [RFC PATCH 0/7] Device Tree Overlay Support Michael Tretter
2019-04-04 14:53 ` [RFC PATCH 1/7] commands: unify newlines for options Michael Tretter
2019-04-05 12:47   ` Sascha Hauer
2019-04-04 14:53 ` [RFC PATCH 2/7] dtc: add -@ option to enable __symbols__ Michael Tretter
2019-04-04 14:53 ` [RFC PATCH 3/7] of: add support for devicetree overlays Michael Tretter
2019-04-04 14:53 ` [RFC PATCH 4/7] commands: add oftree -o option for overlays Michael Tretter
2019-04-04 14:53 ` [RFC PATCH 5/7] firmware: allow to find manager by device node Michael Tretter
2019-04-04 14:53 ` Michael Tretter [this message]
2019-04-05 10:39   ` [RFC PATCH 6/7] firmware: add support for fpga-regions Sascha Hauer
2019-04-04 14:53 ` [RFC PATCH 7/7] blspec: add support for devicetree overlays Michael Tretter
2019-04-05 11:04   ` 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=20190404145320.11465-7-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