Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932607AbaJVT5H (ORCPT ); Wed, 22 Oct 2014 15:57:07 -0400 Received: from mail-by2on0061.outbound.protection.outlook.com ([207.46.100.61]:31031 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932553AbaJVT5B (ORCPT ); Wed, 22 Oct 2014 15:57:01 -0400 From: To: , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , Alan Tull Subject: [PATCH v2 3/3] fpga manager: bus driver Date: Wed, 22 Oct 2014 14:50:05 -0500 Message-ID: <1414007405-32186-4-git-send-email-atull@opensource.altera.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1414007405-32186-1-git-send-email-atull@opensource.altera.com> References: <1414007405-32186-1-git-send-email-atull@opensource.altera.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [64.129.157.38] X-ClientProxiedBy: DM2PR11CA0002.namprd11.prod.outlook.com (25.160.91.12) To BL2PR03MB307.namprd03.prod.outlook.com (10.141.68.21) X-MS-Exchange-Transport-FromEntityHeader: Hosted X-Microsoft-Antispam: UriScan:; X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:;SRVR:BL2PR03MB307; X-Exchange-Antispam-Report-Test: UriScan:; X-Forefront-PRVS: 037291602B X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(199003)(189002)(22564002)(62966002)(31966008)(50986999)(76176999)(106356001)(48376002)(15202345003)(77096002)(76482002)(99396003)(120916001)(50466002)(77156001)(97736003)(81156004)(107046002)(229853001)(105586002)(95666004)(19580395003)(122386002)(85306004)(19580405001)(15975445006)(46102003)(80022003)(102836001)(87286001)(69596002)(93916002)(575784001)(101416001)(40100003)(86362001)(104166001)(88136002)(89996001)(33646002)(66066001)(21056001)(47776003)(53416004)(20776003)(42186005)(50226001)(87976001)(4396001)(92566001)(85852003)(92726001)(64706001)(86152002)(2201001)(2004002)(2101003);DIR:OUT;SFP:1101;SCL:1;SRVR:BL2PR03MB307;H:atx-linux-37.altera.com;FPR:;MLV:sfv;PTR:InfoNoRecords;A:0;MX:1;LANG:en; X-OriginatorOrg: opensource.altera.com Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alan Tull Support programming the fpga from device tree overlays. This patch adds one exported function to the core (fpga-mgr.c): of_fpga_mgr_dev_lookup Get pointer to fpga manager struct given a phandle. This code is dependent on Pantelis Antoniou's current work on Device Tree overlays, a method of dynamically altering the kernel's live Device Tree. The rest of the patchset was tested with recent for-next, but this 'bus driver' patch was tested with Pantelis's and Grant Likely's stuff that is in git (his git repo https://github.com/pantoniou/linux-beagle-track-mainline v3.17-rc2-115-gbc778b8 == dt-ng/bbb) plus some dtc fixups for compiling overlays. Here's a simple example. Start with: * the altera-gpio driver built in to the kernel but not in the device tree. * raw fpga image at /lib/firmware/soc_system.rbf * Load appropriate device tree overlay in configfs by doing $ mkdir /config/device-tree/overlays/1 $ echo socfpga_overlay.dtbo > /config/device-tree/overlays/1/path * This results in the FPGA getting programmed and the altera gpio driver getting probed. * To remove/clear FPGA image by putting the FPGA in reset, remove device tree overlay by doing: $ rmdir /config/device-tree/overlays/1 Device tree overlay looks like this: /dts-v1/; /plugin/; / { fragment@0 { target-path="/soc"; __overlay__ { #address-cells = <1>; #size-cells = <1>; bridge@0xff200000 { compatible = "fpga-mgr-bus", "simple-bus"; reg = <0xff200000 0x200000>; clocks = <0x2 0x2>; clock-names = "h2f_lw_axi_clock", "f2h_sdram0_clock"; #address-cells = <0x2>; #size-cells = <0x1>; ranges = <0x1 0x10040 0xff210040 0x20>; fpgamgr = <&hps_0_fpgamgr>; fpga-firmware = "soc_system.rbf"; gpio@0x100010040 { compatible = "altr,pio-14.0", "altr,pio-1.0"; reg = <0x1 0x10040 0x20>; clocks = <0x2>; altr,gpio-bank-width = <0x4>; resetvalue = <0x0>; #gpio-cells = <0x2>; gpio-controller; linux,phandle = <0x2d>; }; }; }; }; }; Signed-off-by: Alan Tull v2: simplify of_fpga_mgr_dev_lookup to return NULL on failure move suspend/resume to fpga-mgr.c core support 'rmdir' to remove overlay --- drivers/fpga/Kconfig | 7 +++ drivers/fpga/Makefile | 1 + drivers/fpga/bus.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/fpga/fpga-mgr.c | 23 +++++++++ include/linux/fpga-mgr.h | 2 + 5 files changed, 157 insertions(+) create mode 100644 drivers/fpga/bus.c diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig index e775b17..2bd6c83 100644 --- a/drivers/fpga/Kconfig +++ b/drivers/fpga/Kconfig @@ -18,4 +18,11 @@ config FPGA_MGR_SYSFS help FPGA Manager SysFS interface. +config FPGA_MGR_BUS + bool "FPGA Manager Bus" + depends on FPGA + help + FPGA Manager Bus interface. Allows loading FPGA images + from Device Tree or from other drivers. + endmenu diff --git a/drivers/fpga/Makefile b/drivers/fpga/Makefile index c8a676f..e39911f 100644 --- a/drivers/fpga/Makefile +++ b/drivers/fpga/Makefile @@ -6,5 +6,6 @@ fpga-mgr-core-y += fpga-mgr.o # Core FPGA Manager Framework obj-$(CONFIG_FPGA) += fpga-mgr-core.o +obj-$(CONFIG_FPGA_MGR_BUS) += bus.o # FPGA Manager Drivers diff --git a/drivers/fpga/bus.c b/drivers/fpga/bus.c new file mode 100644 index 0000000..999346e --- /dev/null +++ b/drivers/fpga/bus.c @@ -0,0 +1,124 @@ +/* + * FPGA Manager Bus Driver + * + * Copyright (C) 2013-2014 Altera Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope 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. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct fpga_mgr_bus_priv { + struct device *dev; + struct device_node *np; + struct fpga_manager *mgr; + const char *path; +}; + +static int fpga_mgr_bus_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct device *dev = &pdev->dev; + struct fpga_mgr_bus_priv *priv; + const char *path; + struct fpga_manager *mgr; + int ret = 0; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + /* Find the FPGA Manager to associate with */ + mgr = of_fpga_mgr_dev_lookup(np, "fpgamgr"); + if (!mgr) { + dev_err(mgr->dev, "%s could not find fpga mgr\n", __func__); + return -ENODEV; + } + + /* Find the FPGA image on the firmware path */ + of_property_read_string(np, "fpga-firmware", &path); + + ret = fpga_mgr_firmware_write(mgr, path); + if (ret != 0) { + dev_err(mgr->dev, "%s fpga mgr write failure\n", __func__); + return -EIO; + } + + priv->dev = dev; + priv->np = np; + priv->mgr = mgr; + priv->path = path; + platform_set_drvdata(pdev, priv); + + return 0; +} + +/* Called when the Device Tree overlay is removed */ +static int fpga_mgr_bus_remove(struct platform_device *pdev) +{ + struct fpga_mgr_bus_priv *priv = platform_get_drvdata(pdev); + struct fpga_manager *mgr = priv->mgr; + + return fpga_mgr_reset(mgr); +} + +#ifdef CONFIG_OF +static const struct of_device_id fpga_mgr_bus_of_match[] = { + { .compatible = "fpga-mgr-bus", }, + {}, +}; + +MODULE_DEVICE_TABLE(of, fpga_mgr_bus_of_match); +#endif + +static struct platform_driver fpga_mgr_bus_driver = { + .probe = fpga_mgr_bus_probe, + .remove = fpga_mgr_bus_remove, + .driver = { + .name = "fpga_manager_bus", + .owner = THIS_MODULE, + .of_match_table = of_match_ptr(fpga_mgr_bus_of_match), + }, +}; + +static int __init fpga_mgr_bus_init(void) +{ + return platform_driver_register(&fpga_mgr_bus_driver); +} + +static void __exit fpga_mgr_bus_exit(void) +{ + platform_driver_unregister(&fpga_mgr_bus_driver); +} + +module_init(fpga_mgr_bus_init); +module_exit(fpga_mgr_bus_exit); + +MODULE_AUTHOR("Alan Tull "); +MODULE_DESCRIPTION("Altera FPGA Manager Bus"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c index 0c7236a..fed13a6 100644 --- a/drivers/fpga/fpga-mgr.c +++ b/drivers/fpga/fpga-mgr.c @@ -350,6 +350,29 @@ const struct attribute_group *fpga_mgr_groups[] = { #define fpga_mgr_groups NULL #endif /* CONFIG_FPGA_MGR_SYSFS */ +/* Find the fpga manager that is pointed to by a phandle */ +struct fpga_manager *of_fpga_mgr_dev_lookup(struct device_node *node, + const char *mgr_property) +{ + struct fpga_manager *mgr; + struct device_node *mgr_node; + + mgr_node = of_parse_phandle(node, mgr_property, 0); + if (!mgr_node) + return NULL; + + list_for_each_entry(mgr, &fpga_manager_list, list) + if (mgr_node == mgr->np) { + of_node_put(mgr_node); + return mgr; + } + + of_node_put(mgr_node); + + return NULL; +} +EXPORT_SYMBOL_GPL(of_fpga_mgr_dev_lookup); + static int fpga_mgr_suspend(struct device *dev) { struct fpga_manager *mgr = dev_get_drvdata(dev); diff --git a/include/linux/fpga-mgr.h b/include/linux/fpga-mgr.h index 97c7e0b..abf2699 100644 --- a/include/linux/fpga-mgr.h +++ b/include/linux/fpga-mgr.h @@ -110,6 +110,8 @@ int fpga_mgr_register(struct platform_device *pdev, struct fpga_manager_ops *mops, const char *name, void *priv); void fpga_mgr_remove(struct platform_device *pdev); +struct fpga_manager *of_fpga_mgr_dev_lookup(struct device_node *node, + const char *mgr_property); #endif /* CONFIG_FPGA */ #endif /*_LINUX_FPGA_MGR_H */ -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/