Skip to contentSkip to conversation

FPGA Manager Framework

PATCH Superseded [email protected] 6 participants 28 messages Dec 9, 2014 – Dec 17, 2014

Subsystems (5)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover FPGA Manager Framework

From: Alan Tull <[email protected]>

Improvements in this v3 and v4:

I've moved the driver to drivers/staging.

I'm including the altera low level driver, defconfig changes, and DTS changes - everything to make it work on socfpga platform.

Fixed things in the framework that were left over from my original version (that Michal started with) that was a character driver. In particular, make the device be present in the fpga_manager struct, not as a pointer, so container_of will work.

Since not enough of the Device Tree Overlays code is in the main kernel yet, I'm leaving out the (small) changes to make this accept notifications when a device tree overlay is accepted. That can wait a little until enough of is in that it can build and work. That is a little disappointing to leave it out for now because it was very cool and, I think, more Linux-like than this is by itself.

Alan Tull (6):
doc: add bindings document for altera fpga manager
arm: dts: socfpga: add altera fpga manager
ARM: socfpga: defconfig: enable fpga manager
fpga manager: add sysfs interface document
staging: fpga manager: framework core
staging: fpga manager: add driver for altera socfpga manager

 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 +
 .../devicetree/bindings/fpga/altera-fpga-mgr.txt   |   17 +
 arch/arm/boot/dts/socfpga.dtsi                     |   10 +
 arch/arm/configs/socfpga_defconfig                 |    4 +
 drivers/staging/Kconfig                            |    2 +
 drivers/staging/Makefile                           |    1 +
 drivers/staging/fpga/Kconfig                       |   27 +
 drivers/staging/fpga/Makefile                      |   11 +
 drivers/staging/fpga/altera.c                      |  789 ++++++++++++++++++++
 drivers/staging/fpga/fpga-mgr.c                    |  485 ++++++++++++
 include/linux/fpga-mgr.h                           |  104 +++
 11 files changed, 1488 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
 create mode 100644 Documentation/devicetree/bindings/fpga/altera-fpga-mgr.txt
 create mode 100644 drivers/staging/fpga/Kconfig
 create mode 100644 drivers/staging/fpga/Makefile
 create mode 100644 drivers/staging/fpga/altera.c
 create mode 100644 drivers/staging/fpga/fpga-mgr.c
 create mode 100644 include/linux/fpga-mgr.h
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v4 1/6 doc: add bindings document for altera fpga manager

From: Alan Tull <[email protected]>

New bindings document for Altera fpga manager.

---
 .../devicetree/bindings/fpga/altera-fpga-mgr.txt   |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/fpga/altera-fpga-mgr.txt
Documentation/devicetree/bindings/fpga/altera-fpga-mgr.txt new file +17 -0
new file mode 100644
index 0000000..373af7b
@@ -0,0 +1,17 @@
+Altera FPGA Manager
+
+Required properties:
+- compatible : should contain "altr,fpga-mgr"
+- reg        : base address and size for memory mapped io.
+               - The first index is for FPGA manager register access.
+               - The second index is for writing FPGA configuration data.
+- interrupts : interrupt for the FPGA Manager device.
+
+Example:
+
+	hps_0_fpgamgr: fpgamgr@0xff706000 {
+		compatible = "altr,fpga-mgr";
+		reg = <0xFF706000 0x1000
+		       0xFFB90000 0x1000>;
+		interrupts = <0 175 4>;
+	};
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v4 2/6 arm: dts: socfpga: add altera fpga manager

In reply to [email protected]

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +10 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};
 	};
 };
Signature · 1 line
1.7.9.5
#8 lore raw

PATCH v4 3/6 ARM: socfpga: defconfig: enable fpga manager

In reply to [email protected]

From: Alan Tull <[email protected]>

Enable FPGA manager for Altera socfpga.

---
 arch/arm/configs/socfpga_defconfig |    4 ++++
 1 file changed, 4 insertions(+)
arch/arm/configs/socfpga_defconfig +4 -0
index a2956c3..2740057 100644
@@ -86,6 +86,10 @@ CONFIG_USB_DWC2=y
 CONFIG_USB_DWC2_HOST=y
 CONFIG_MMC=y
 CONFIG_MMC_DW=y
+CONFIG_STAGING=y
+CONFIG_FPGA=y
+CONFIG_FPGA_MGR_SYSFS=y
+CONFIG_FPGA_MGR_ALTERA=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT2_FS_XATTR=y
 CONFIG_EXT2_FS_POSIX_ACL=y
Signature · 1 line
1.7.9.5
#11 lore raw

PATCH v4 4/6 fpga manager: add sysfs interface document

In reply to [email protected]

From: Alan Tull <[email protected]>

Add documentation for new fpga manager sysfs interface.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
Documentation/ABI/testing/sysfs-class-fpga-manager new file +38 -0
new file mode 100644
index 0000000..eb600f2
@@ -0,0 +1,38 @@
+What:		/sys/class/fpga_manager/<fpga>/name
+Date:		October 2014
+KernelVersion:	3.18
+Contact:	Alan Tull <[email protected]>
+Description:	Name of low level driver.
+
+What:		/sys/class/fpga_manager/<fpga>/firmware
+Date:		October 2014
+KernelVersion:	3.18
+Contact:	Alan Tull <[email protected]>
+Description:	Name of the FPGA image file to load using firmware class.
+
+What:		/sys/class/fpga_manager/<fpga>/reset
+Date:		October 2014
+KernelVersion:	3.18
+Contact:	Alan Tull <[email protected]>
+Description:	Write 1 to reset the FPGA
+
+What:		/sys/class/fpga_manager/<fpga>/state
+Date:		October 2014
+KernelVersion:	3.18
+Contact:	Alan Tull <[email protected]>
+Description:	Read state of fpga framework state machine as a string.
+		Valid states may vary by manufacturer; superset is:
+		* unknown		= can't determine state
+		* power_off		= FPGA power is off
+		* power_up		= FPGA reports power is up
+		* reset			= FPGA held in reset state
+		* firmware_request	= firmware class request in progress
+		* firmware_request_err	= firmware request failed
+		* write_init		= FPGA being prepared for programming
+		* write_init_err	= Error while preparing FPGA for
+					  programming
+		* write			= FPGA ready to receive image data
+		* write_err		= Error while programming
+		* write_complete	= Doing post programming steps
+		* write_complete_err	= Error while doing post programming
+		* operating		= FPGA is programmed and operating
Signature · 1 line
1.7.9.5
#17 lore raw

PATCH v4 5/6 staging: fpga manager: framework core

In reply to [email protected]

From: Alan Tull <[email protected]>

Supports standard ops for low level FPGA drivers. Various manufacturors' FPGAs can be supported by adding low level drivers. Each driver needs to register its ops using fpga_mgr_register().

Exports methods of doing operations to program FPGAs. These should be sufficient for individual drivers to request FPGA programming directly if desired.

Adds a sysfs interface. The sysfs interface can be compiled out where desired in production builds.

Resume is supported by calling low level driver's resume function, then reloading the firmware image.

The following are exported as GPL:
* fpga_mgr_reset
Reset the FGPA.

* fpga_mgr_write
Write a image (in buffer) to the FPGA.

* fpga_mgr_firmware_write
Request firmware by file name and write it to the FPGA.

* fpga_mgr_name
Get name of FPGA manager.

* fpga_mgr_state
Get a state of framework as a string.

* fpga_mgr_register and fpga_mgr_remove Register/unregister low level fpga manager driver.

The following sysfs files are created:
* /sys/class/fpga_manager/<fpga>/name
Name of low level driver.

* /sys/class/fpga_manager/<fpga>/firmware
Name of FPGA image file to load using firmware class.

  $ echo image.rbf > /sys/class/fpga_manager/<fpga>/firmware

read: read back name of image file previous loaded

  $ cat /sys/class/fpga_manager/<fpga>/firmware

* /sys/class/fpga_manager/<fpga>/reset
reset the FPGA

  $ echo 1 > /sys/class/fpga_manager/<fpga>/reset

* /sys/class/fpga_manager/<fpga>/state
State of fpga framework state machine

---
v2: s/mangager/manager/
check for invalid request_nr
add fpga reset interface
rework the state code
remove FPGA_MGR_FAIL flag
add _ERR states to fpga manager states enum low level state op now returns a state enum value initialize framework state from driver state op remove unused fpga read stuff
merge sysfs.c into fpga-mgr.c
move suspend/resume from bus.c to fpga-mgr.c

v3: Add struct device to fpga_manager (not as a pointer)
Add to_fpga_manager
Don't get irq in fpga-mgr.c (let low level driver do it) remove from struct fpga_manager: nr, np, parent get rid of fpga_mgr_get_new_minor()
simplify fpga_manager_register:
reorder parameters
use dev instead of pdev
get rid of code that used to make more sense when this
was a char driver, don't alloc_chrdev_region
use a mutex instead of flags

v4: Move to drivers/staging

---
 drivers/staging/Kconfig         |    2 +
 drivers/staging/Makefile        |    1 +
 drivers/staging/fpga/Kconfig    |   21 ++
 drivers/staging/fpga/Makefile   |   10 +
 drivers/staging/fpga/fpga-mgr.c |  485 +++++++++++++++++++++++++++++++++++++++
 include/linux/fpga-mgr.h        |  104 +++++++++
 6 files changed, 623 insertions(+)
 create mode 100644 drivers/staging/fpga/Kconfig
 create mode 100644 drivers/staging/fpga/Makefile
 create mode 100644 drivers/staging/fpga/fpga-mgr.c
 create mode 100644 include/linux/fpga-mgr.h
drivers/staging/Kconfig +2 -0
index 4690ae9..4338a4c 100644
@@ -108,4 +108,6 @@ source "drivers/staging/skein/Kconfig"
 
 source "drivers/staging/unisys/Kconfig"
 
+source "drivers/staging/fpga/Kconfig"
+
 endif # STAGING
drivers/staging/Makefile +1 -0
index c780a0e..43654a2 100644
@@ -46,3 +46,4 @@ obj-$(CONFIG_MTD_SPINAND_MT29F)	+= mt29f_spinand/
 obj-$(CONFIG_GS_FPGABOOT)	+= gs_fpgaboot/
 obj-$(CONFIG_CRYPTO_SKEIN)	+= skein/
 obj-$(CONFIG_UNISYSSPAR)	+= unisys/
+obj-$(CONFIG_FPGA)		+= fpga/
drivers/staging/fpga/Kconfig new file +21 -0
new file mode 100644
index 0000000..e775b17
@@ -0,0 +1,21 @@
+#
+# FPGA framework configuration
+#
+
+menu "FPGA devices"
+
+config FPGA
+	tristate "FPGA Framework"
+	help
+	  Say Y here if you want support for configuring FPGAs from the
+	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
+	  manager drivers.
+
+config FPGA_MGR_SYSFS
+	bool "FPGA Manager SysFS Interface"
+	depends on FPGA
+	depends on SYSFS
+	help
+	  FPGA Manager SysFS interface.
+
+endmenu
drivers/staging/fpga/Makefile new file +10 -0
new file mode 100644
index 0000000..c8a676f
@@ -0,0 +1,10 @@
+#
+# Makefile for the fpga framework and fpga manager drivers.
+#
+
+fpga-mgr-core-y += fpga-mgr.o
+
+# Core FPGA Manager Framework
+obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o
+
+# FPGA Manager Drivers
drivers/staging/fpga/fpga-mgr.c new file +485 -0

485 lines not on this page · raw message

include/linux/fpga-mgr.h new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#21 lore raw

PATCH v4 6/6 staging: fpga manager: add driver for altera socfpga manager

In reply to [email protected]

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoC FPGA parts.

---
v2: fpga_manager struct now contains struct device
fpga_manager_register parameters now take device

v3: move to drivers/staging

---
 drivers/staging/fpga/Kconfig  |    6 +
 drivers/staging/fpga/Makefile |    1 +
 drivers/staging/fpga/altera.c |  789 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 796 insertions(+)
 create mode 100644 drivers/staging/fpga/altera.c
drivers/staging/fpga/Kconfig +6 -0
index e775b17..2944e3c 100644
@@ -18,4 +18,10 @@ config FPGA_MGR_SYSFS
 	help
 	  FPGA Manager SysFS interface.
 
+config FPGA_MGR_ALTERA
+	tristate "Altera"
+	depends on FPGA
+	help
+	  FPGA manager driver support for configuring Altera FPGAs.
+
 endmenu
drivers/staging/fpga/Makefile +1 -0
index c8a676f..6e0d2bf 100644
@@ -8,3 +8,4 @@ fpga-mgr-core-y += fpga-mgr.o
 obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_ALTERA)		+= altera.o
drivers/staging/fpga/altera.c new file +789 -0

789 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#9 lore raw

Re: [PATCH v4 3/6] ARM: socfpga: defconfig: enable fpga manager

In reply to [email protected]

Hi Alan,

Quoted 15 lines · On 12/09/2014 09:14 PM, [email protected]…

From: Alan Tull <[email protected]>

Enable FPGA manager for Altera socfpga.

---
 arch/arm/configs/socfpga_defconfig |    4 ++++
 1 file changed, 4 insertions(+)
arch/arm/configs/socfpga_defconfig +0 -0
index a2956c3..2740057 100644

arch/arm/configs/socfpga_defconfig CONFIG_USB_DWC2=y · line 95

Quoted 10 lines
arch/arm/configs/socfpga_defconfig +4 -0
@@ -86,6 +86,10 @@ CONFIG_USB_DWC2=y
 CONFIG_USB_DWC2_HOST=y
 CONFIG_MMC=y
 CONFIG_MMC_DW=y
+CONFIG_STAGING=y
+CONFIG_FPGA=y
+CONFIG_FPGA_MGR_SYSFS=y
+CONFIG_FPGA_MGR_ALTERA=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT2_FS_XATTR=y
 CONFIG_EXT2_FS_POSIX_ACL=y

This should the last patch in this series because you are changing Kconfig in 5/6 and 6/6.

Thanks,
Michal
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw

Re: [PATCH v4 4/6] fpga manager: add sysfs interface document

In reply to [email protected]

Hi Greg,

Quoted 9 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add documentation for new fpga manager sysfs interface.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager

What's the rule for adding sysfs doc for driver which is added to staging? Is this location fine?

Thanks,
Michal

#22 lore raw

Re: [PATCH v4 6/6] staging: fpga manager: add driver for altera socfpga manager

In reply to [email protected]
Quoted 11 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoC FPGA parts.

---
v2: fpga_manager struct now contains struct device
fpga_manager_register parameters now take device

v3: move to drivers/staging

here should be that v4 is completely new patch in this series as you are describing in cover letter.

Quoted 66 lines
---
 drivers/staging/fpga/Kconfig  |    6 +
 drivers/staging/fpga/Makefile |    1 +
 drivers/staging/fpga/altera.c |  789 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 796 insertions(+)
 create mode 100644 drivers/staging/fpga/altera.c
drivers/staging/fpga/Kconfig +6 -0
index e775b17..2944e3c 100644
@@ -18,4 +18,10 @@ config FPGA_MGR_SYSFS
 	help
 	  FPGA Manager SysFS interface.
 
+config FPGA_MGR_ALTERA
+	tristate "Altera"
+	depends on FPGA
+	help
+	  FPGA manager driver support for configuring Altera FPGAs.
+
 endmenu
drivers/staging/fpga/Makefile +1 -0
index c8a676f..6e0d2bf 100644
@@ -8,3 +8,4 @@ fpga-mgr-core-y += fpga-mgr.o
 obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_ALTERA)		+= altera.o
drivers/staging/fpga/altera.c new file +29 -0
new file mode 100644
index 0000000..14a16b4
@@ -0,0 +1,789 @@
+/*
+ * FPGA Manager Driver for Altera FPGAs
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <linux/cdev.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/fs.h>
+#include <linux/fpga-mgr.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>

drivers/staging/fpga/altera.c line 39

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/pm.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/regulator/consumer.h>

This is pretty long list I believe you will be able to shorten it a little bit.

drivers/staging/fpga/altera.c line 44

Quoted 5 lines
drivers/staging/fpga/altera.c new file +5 -0
+
+/* Controls whether to use the Configuration with DCLK steps */
+#ifndef _ALTTERA_FPGAMGR_USE_DCLK
+#define _ALTTERA_FPGAMGR_USE_DCLK 0
+#endif

This is more or less config option which you should reflect in binding.

Quoted 78 lines
drivers/staging/fpga/altera.c new file +78 -0
+
+/* Register offsets */
+#define ALTERA_FPGAMGR_STAT_OFST				0x0
+#define ALTERA_FPGAMGR_CTL_OFST					0x4
+#define ALTERA_FPGAMGR_DCLKCNT_OFST				0x8
+#define ALTERA_FPGAMGR_DCLKSTAT_OFST				0xc
+#define ALTERA_FPGAMGR_GPIO_INTEN_OFST				0x830
+#define ALTERA_FPGAMGR_GPIO_INTMSK_OFST				0x834
+#define ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST			0x838
+#define ALTERA_FPGAMGR_GPIO_INT_POL_OFST			0x83c
+#define ALTERA_FPGAMGR_GPIO_INTSTAT_OFST			0x840
+#define ALTERA_FPGAMGR_GPIO_RAW_INTSTAT_OFST			0x844
+#define ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST			0x84c
+#define ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST			0x850
+
+/* Register bit defines */
+/* ALTERA_FPGAMGR_STAT register mode field values */
+#define ALTERA_FPGAMGR_STAT_POWER_UP				0x0 /*ramping*/
+#define ALTERA_FPGAMGR_STAT_RESET				0x1
+#define ALTERA_FPGAMGR_STAT_CFG					0x2
+#define ALTERA_FPGAMGR_STAT_INIT				0x3
+#define ALTERA_FPGAMGR_STAT_USER_MODE				0x4
+#define ALTERA_FPGAMGR_STAT_UNKNOWN				0x5
+#define ALTERA_FPGAMGR_STAT_STATE_MASK				0x7
+/* This is a flag value that doesn't really happen in this register field */
+#define ALTERA_FPGAMGR_STAT_POWER_OFF				0x0
+
+#define MSEL_PP16_FAST_NOAES_NODC				0x0
+#define MSEL_PP16_FAST_AES_NODC					0x1
+#define MSEL_PP16_FAST_AESOPT_DC				0x2
+#define MSEL_PP16_SLOW_NOAES_NODC				0x4
+#define MSEL_PP16_SLOW_AES_NODC					0x5
+#define MSEL_PP16_SLOW_AESOPT_DC				0x6
+#define MSEL_PP32_FAST_NOAES_NODC				0x8
+#define MSEL_PP32_FAST_AES_NODC					0x9
+#define MSEL_PP32_FAST_AESOPT_DC				0xa
+#define MSEL_PP32_SLOW_NOAES_NODC				0xc
+#define MSEL_PP32_SLOW_AES_NODC					0xd
+#define MSEL_PP32_SLOW_AESOPT_DC				0xe
+#define ALTERA_FPGAMGR_STAT_MSEL_MASK				0x000000f8
+#define ALTERA_FPGAMGR_STAT_MSEL_SHIFT				3
+
+/* ALTERA_FPGAMGR_CTL register */
+#define ALTERA_FPGAMGR_CTL_EN					0x00000001
+#define ALTERA_FPGAMGR_CTL_NCE					0x00000002
+#define ALTERA_FPGAMGR_CTL_NCFGPULL				0x00000004
+
+#define CDRATIO_X1						0x00000000
+#define CDRATIO_X2						0x00000040
+#define CDRATIO_X4						0x00000080
+#define CDRATIO_X8						0x000000c0
+#define ALTERA_FPGAMGR_CTL_CDRATIO_MASK				0x000000c0
+
+#define ALTERA_FPGAMGR_CTL_AXICFGEN				0x00000100
+
+#define CFGWDTH_16						0x00000000
+#define CFGWDTH_32						0x00000200
+#define ALTERA_FPGAMGR_CTL_CFGWDTH_MASK				0x00000200
+
+/* ALTERA_FPGAMGR_DCLKSTAT register */
+#define ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE			0x1
+
+/* ALTERA_FPGAMGR_GPIO_* registers share the same bit positions */
+#define ALTERA_FPGAMGR_MON_NSTATUS				0x0001
+#define ALTERA_FPGAMGR_MON_CONF_DONE				0x0002
+#define ALTERA_FPGAMGR_MON_INIT_DONE				0x0004
+#define ALTERA_FPGAMGR_MON_CRC_ERROR				0x0008
+#define ALTERA_FPGAMGR_MON_CVP_CONF_DONE			0x0010
+#define ALTERA_FPGAMGR_MON_PR_READY				0x0020
+#define ALTERA_FPGAMGR_MON_PR_ERROR				0x0040
+#define ALTERA_FPGAMGR_MON_PR_DONE				0x0080
+#define ALTERA_FPGAMGR_MON_NCONFIG_PIN				0x0100
+#define ALTERA_FPGAMGR_MON_NSTATUS_PIN				0x0200
+#define ALTERA_FPGAMGR_MON_CONF_DONE_PIN			0x0400
+#define ALTERA_FPGAMGR_MON_FPGA_POWER_ON			0x0800
+#define ALTERA_FPGAMGR_MON_STATUS_MASK				0x0fff
+
+#define ALTERA_FPGAMGR_NUM_SUPPLIES 3

drivers/staging/fpga/altera.c line 132

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+#define ALTERA_RESUME_TIMEOUT 3
+
+#if IS_ENABLED(CONFIG_REGULATOR)
+/* In power-up order. Reverse for power-down. */
+static const char *supply_names[ALTERA_FPGAMGR_NUM_SUPPLIES] = {
+	"FPGA-1.5V",
+	"FPGA-1.1V",
+	"FPGA-2.5V",
+};
+#endif

__maybe_unused should work here.

Quoted 99 lines
drivers/staging/fpga/altera.c new file +99 -0
+
+struct altera_fpga_priv {
+	void __iomem *fpga_base_addr;
+	void __iomem *fpga_data_addr;
+	struct completion status_complete;
+	int irq;
+	struct regulator *fpga_supplies[ALTERA_FPGAMGR_NUM_SUPPLIES];
+};
+
+struct cfgmgr_mode {
+	/* Values to set in the CTRL register */
+	u32 ctrl;
+
+	/* flag that this table entry is a valid mode */
+	bool valid;
+};
+
+/* For ALTERA_FPGAMGR_STAT_MSEL field */
+static struct cfgmgr_mode cfgmgr_modes[] = {
+	[MSEL_PP16_FAST_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_FAST_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_FAST_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP16_SLOW_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_SLOW_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_SLOW_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_FAST_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+	[MSEL_PP32_SLOW_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_SLOW_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_SLOW_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+};
+
+static u32 altera_fpga_reg_readl(struct altera_fpga_priv *priv, u32 reg_offset)
+{
+	return readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_writel(struct altera_fpga_priv *priv,
+				   u32 reg_offset, u32 value)
+{
+	writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static u32 altera_fpga_reg_raw_readl(struct altera_fpga_priv *priv,
+				     u32 reg_offset)
+{
+	return __raw_readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_raw_writel(struct altera_fpga_priv *priv,
+				       u32 reg_offset, u32 value)
+{
+	__raw_writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_data_writel(struct altera_fpga_priv *priv, u32 value)
+{
+	writel(value, priv->fpga_data_addr);
+}
+
+static inline void altera_fpga_reg_set_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val |= bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+
+static inline void altera_fpga_reg_clr_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val &= ~bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+
+static int altera_fpga_mon_status_get(struct altera_fpga_priv *priv)
+{
+	return altera_fpga_reg_readl(priv,
+				     ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST) &
+		ALTERA_FPGAMGR_MON_STATUS_MASK;
+}
+
+static int altera_fpga_state_get(struct altera_fpga_priv *priv)
+{
+	int status = altera_fpga_mon_status_get(priv);
+
+	if ((status & ALTERA_FPGAMGR_MON_FPGA_POWER_ON) == 0)
+		return ALTERA_FPGAMGR_STAT_POWER_OFF;
+
+	return altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST) &
+		ALTERA_FPGAMGR_STAT_STATE_MASK;
+}
+

drivers/staging/fpga/altera.c line 241

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+static void altera_fpga_clear_done_status(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST,
+			       ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE);
+}
+
+/*
+ * Set the DCLKCNT, wait for DCLKSTAT to report the count completed, and clear
+ * the complete status.
+ */

kernel-doc format please.

Quoted 15 lines
drivers/staging/fpga/altera.c new file +15 -0
+static int altera_fpga_dclk_set_and_wait_clear(struct altera_fpga_priv *priv,
+					       u32 count)
+{
+	int timeout = 2;
+	u32 done;
+
+	/* Clear any existing DONE status. */
+	if (altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST))
+		altera_fpga_clear_done_status(priv);
+
+	/* Issue the DCLK count. */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKCNT_OFST, count);
+
+	/* Poll DCLKSTAT to see if it completed in the timeout period. */
+	do {

drivers/staging/fpga/altera.c line 266

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+		done = altera_fpga_reg_readl(priv,
+					     ALTERA_FPGAMGR_DCLKSTAT_OFST);
+		if (done == ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE) {
+			altera_fpga_clear_done_status(priv);
+			return 0;
+		}
+		if (count <= 4)
+			udelay(1);
+		else
+			msleep(20);

This looks weird to me.

Quoted 160 lines
drivers/staging/fpga/altera.c new file +160 -0
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static int altera_fpga_wait_for_state(struct altera_fpga_priv *priv, u32 state)
+{
+	int timeout = 2;
+
+	/*
+	 * HW doesn't support an interrupt for changes in state, so poll to see
+	 * if it matches the requested state within the timeout period.
+	 */
+	do {
+		if ((altera_fpga_state_get(priv) & state) != 0)
+			return 0;
+		msleep(20);
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static void altera_fpga_enable_irqs(struct altera_fpga_priv *priv, u32 irqs)
+{
+	/* set irqs to level sensitive */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST, 0);
+
+	/* set interrupt polarity */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INT_POL_OFST, irqs);
+
+	/* clear irqs */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST, irqs);
+
+	/* unmask interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTMSK_OFST, 0);
+
+	/* enable interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, irqs);
+}
+
+static void altera_fpga_disable_irqs(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+}
+
+static irqreturn_t altera_fpga_isr(int irq, void *dev_id)
+{
+	struct altera_fpga_priv *priv = dev_id;
+	u32 irqs, st;
+	bool conf_done, nstatus;
+
+	/* clear irqs */
+	irqs = altera_fpga_reg_raw_readl(priv,
+					 ALTERA_FPGAMGR_GPIO_INTSTAT_OFST);
+
+	altera_fpga_reg_raw_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+				   irqs);
+
+	st = altera_fpga_reg_raw_readl(priv,
+				       ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST);
+	conf_done = (st & ALTERA_FPGAMGR_MON_CONF_DONE) != 0;
+	nstatus = (st & ALTERA_FPGAMGR_MON_NSTATUS) != 0;
+
+	/* success */
+	if (conf_done && nstatus) {
+		/* disable irqs */
+		altera_fpga_reg_raw_writel(priv,
+					   ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+		complete(&priv->status_complete);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int altera_fpga_wait_for_config_done(struct altera_fpga_priv *priv)
+{
+	int timeout, ret = 0;
+
+	altera_fpga_disable_irqs(priv);
+	init_completion(&priv->status_complete);
+	altera_fpga_enable_irqs(priv, ALTERA_FPGAMGR_MON_CONF_DONE);
+
+	timeout = wait_for_completion_interruptible_timeout(
+						&priv->status_complete,
+						msecs_to_jiffies(10));
+	if (timeout == 0)
+		ret = -ETIMEDOUT;
+
+	altera_fpga_disable_irqs(priv);
+	return ret;
+}
+
+static int altera_fpga_cfg_mode_get(struct altera_fpga_priv *priv)
+{
+	u32 msel;
+
+	msel = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST);
+	msel &= ALTERA_FPGAMGR_STAT_MSEL_MASK;
+	msel >>= ALTERA_FPGAMGR_STAT_MSEL_SHIFT;
+
+	/* Check that this MSEL setting is supported */
+	if ((msel >= sizeof(cfgmgr_modes)/sizeof(struct cfgmgr_mode)) ||
+	    !cfgmgr_modes[msel].valid)
+		return -EINVAL;
+
+	return msel;
+}
+
+static int altera_fpga_cfg_mode_set(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, mode;
+
+	/* get value from MSEL pins */
+	mode = altera_fpga_cfg_mode_get(priv);
+	if (mode < 0)
+		return mode;
+
+	/* Adjust CTRL for the CDRATIO */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CDRATIO_MASK;
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CFGWDTH_MASK;
+	ctrl_reg |= cfgmgr_modes[mode].ctrl;
+
+	/* Set NCE to 0. */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCE;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	return 0;
+}
+
+static int altera_fpga_reset(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, status;
+	int ret;
+
+	/*
+	 * Step 1:
+	 *  - Set CTRL.CFGWDTH, CTRL.CDRATIO to match cfg mode
+	 *  - Set CTRL.NCE to 0
+	 */
+	ret = altera_fpga_cfg_mode_set(priv);
+	if (ret)
+		return ret;
+
+	/* Step 2: Set CTRL.EN to 1 */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	/* Step 3: Set CTRL.NCONFIGPULL to 1 to put FPGA in reset */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg |= ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	/* Step 4: Wait for STATUS.MODE to report FPGA is in reset phase */
+	status = altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_RESET);
+
+	/* Step 5: Set CONTROL.NCONFIGPULL to 0 to release FPGA from reset */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+

drivers/staging/fpga/altera.c line 436

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	/* Timeout waiting for reset */
+	if (status)
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+/*
+ * Prepare the FPGA to receive the configuration data.
+ */

kernel-doc

Quoted 18 lines
drivers/staging/fpga/altera.c new file +18 -0
+static int altera_fpga_ops_configure_init(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	int ret;
+
+	/* Steps 1 - 5: Reset the FPGA */
+	ret = altera_fpga_reset(priv);
+	if (ret)
+		return ret;
+
+	/* Step 6: Wait for FPGA to enter configuration phase */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_CFG))
+		return -ETIMEDOUT;
+
+	/* Step 7: Clear nSTATUS interrupt */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+			       ALTERA_FPGAMGR_MON_NSTATUS);
+

drivers/staging/fpga/altera.c line 464

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	/* Step 8: Set CTRL.AXICFGEN to 1 to enable transfer of config data */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	return 0;
+}
+
+/*
+ * Step 9: write data to the FPGA data register
+ */

step 9 here?

Quoted 85 lines
drivers/staging/fpga/altera.c new file +85 -0
+static int altera_fpga_ops_configure_write(struct fpga_manager *mgr,
+					   const char *buf, size_t count)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 *buffer_32 = (u32 *)buf;
+	size_t i = 0;
+
+	if (count <= 0)
+		return -EINVAL;
+
+	/* Write out the complete 32-bit chunks. */
+	while (count >= sizeof(u32)) {
+		altera_fpga_data_writel(priv, buffer_32[i++]);
+		count -= sizeof(u32);
+	}
+
+	/* Write out remaining non 32-bit chunks. */
+	switch (count) {
+	case 3:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x00ffffff);
+		break;
+	case 2:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x0000ffff);
+		break;
+	case 1:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x000000ff);
+		break;
+	default:
+		/* This will never happen. */
+		break;
+	}
+
+	return 0;
+}
+
+static int altera_fpga_ops_configure_complete(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 status;
+
+	/*
+	 * Step 10:
+	 *  - Observe CONF_DONE and nSTATUS (active low)
+	 *  - if CONF_DONE = 1 and nSTATUS = 1, configuration was successful
+	 *  - if CONF_DONE = 0 and nSTATUS = 0, configuration failed
+	 */
+	status = altera_fpga_wait_for_config_done(priv);
+	if (status)
+		return status;
+
+	/* Step 11: Clear CTRL.AXICFGEN to disable transfer of config data */
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	/*
+	 * Step 12:
+	 *  - Write 4 to DCLKCNT
+	 *  - Wait for STATUS.DCNTDONE = 1
+	 *  - Clear W1C bit in STATUS.DCNTDONE
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 4))
+		return -ETIMEDOUT;
+
+#if _ALTTERA_FPGAMGR_USE_DCLK
+	/* Step 13: Wait for STATUS.MODE to report INIT or USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_INIT |
+				       ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/*
+	 * Extra steps for Configuration with DCLK for Initialization Phase
+	 * Step 14 (using 4.2.1.2 steps), 15 (using 4.2.1.2 steps)
+	 *  - Write 0x5000 to DCLKCNT == the number of clocks needed to exit
+	 *    the Initialization Phase.
+	 *  - Poll until STATUS.DCNTDONE = 1, write 1 to clear
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 0x5000))
+		return -ETIMEDOUT;
+#endif
+
+	/* Step 13: Wait for STATUS.MODE to report USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/* Step 14: Set CTRL.EN to 0 */

drivers/staging/fpga/altera.c line 559

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	return 0;
+}
+
+static int altera_fpga_ops_reset(struct fpga_manager *mgr)
+{
+	return altera_fpga_reset(mgr->priv);
+}

looks like not useful code - just use altera_fpga_reset instead of ops.

btw: just a generic question - isn't it better to use any better name than altera_fpga. You can have different loader in future and this is very generic.

drivers/staging/fpga/altera.c line 571

Quoted 12 lines
drivers/staging/fpga/altera.c new file +12 -0
+
+/* Translate state register values to FPGA framework state */
+static const int altera_state_to_framework_state[] = {
+	[ALTERA_FPGAMGR_STAT_POWER_OFF] = FPGA_MGR_STATE_POWER_OFF,
+	[ALTERA_FPGAMGR_STAT_RESET] = FPGA_MGR_STATE_RESET,
+	[ALTERA_FPGAMGR_STAT_CFG] = FPGA_MGR_STATE_WRITE_INIT,
+	[ALTERA_FPGAMGR_STAT_INIT] = FPGA_MGR_STATE_WRITE_INIT,
+	[ALTERA_FPGAMGR_STAT_USER_MODE] = FPGA_MGR_STATE_OPERATING,
+	[ALTERA_FPGAMGR_STAT_UNKNOWN] = FPGA_MGR_STATE_UNKNOWN,
+};
+
+static int altera_fpga_ops_state(struct fpga_manager *mgr)

here should return enum - look at 5/6 comment.

drivers/staging/fpga/altera.c line 574

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;

this is also int not unsigned.

drivers/staging/fpga/altera.c line 587

Quoted 13 lines
drivers/staging/fpga/altera.c new file +13 -0
+	int ret;
+
+	state = altera_fpga_state_get(priv);
+
+	if (state < ARRAY_SIZE(altera_state_to_framework_state))
+		ret = altera_state_to_framework_state[state];
+	else
+		ret = FPGA_MGR_STATE_UNKNOWN;
+
+	return ret;
+}
+
+#if IS_ENABLED(CONFIG_REGULATOR)

Instead of this look below

drivers/staging/fpga/altera.c line 591

Quoted 4 lines
drivers/staging/fpga/altera.c new file +4 -0
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	int i, ret;
+

use this

if (!IS_ENABLED(CONFIG_REGULATOR))

	return 0;

Then you can simple compile code just once.

The same change for all these functions.

Quoted 75 lines
drivers/staging/fpga/altera.c new file +75 -0
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		ret = regulator_enable(priv->fpga_supplies[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+	int i;
+
+	for (i = ALTERA_FPGAMGR_NUM_SUPPLIES - 1; i >= 0; i--)
+		regulator_disable(priv->fpga_supplies[i]);
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	struct regulator *supply;
+	unsigned int i;
+
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		supply = devm_regulator_get_optional(&pdev->dev,
+						     supply_names[i]);
+		if (IS_ERR(supply)) {
+			dev_err(&pdev->dev, "could not get regulators");
+			return -EPROBE_DEFER;
+		}
+		priv->fpga_supplies[i] = supply;
+	}
+
+	return altera_fpga_regulators_on(priv);
+}
+#else
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+#endif
+
+static int altera_fpga_suspend(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	altera_fpga_regulators_power_off(priv);
+
+	return 0;
+}
+
+static int altera_fpga_resume(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;
+	unsigned int timeout;
+	int ret;
+
+	ret = altera_fpga_regulators_on(priv);
+	if (ret)
+		return ret;
+
+	for (timeout = 0; timeout < ALTERA_RESUME_TIMEOUT; timeout++) {
+		state = altera_fpga_state_get(priv);
+		if (state != ALTERA_FPGAMGR_STAT_POWER_OFF)

drivers/staging/fpga/altera.c line 676

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+			break;
+		msleep(20);
+	}
+	if (state == ALTERA_FPGAMGR_STAT_POWER_OFF)
+		return -ETIMEDOUT;
+
+	return ret;
+}
+
+struct fpga_manager_ops altera_altera_fpga_ops = {

static here.

Quoted 26 lines
drivers/staging/fpga/altera.c new file +26 -0
+	.reset = altera_fpga_ops_reset,
+	.state = altera_fpga_ops_state,
+	.write_init = altera_fpga_ops_configure_init,
+	.write = altera_fpga_ops_configure_write,
+	.write_complete = altera_fpga_ops_configure_complete,
+	.suspend = altera_fpga_suspend,
+	.resume = altera_fpga_resume,
+};
+
+static int altera_fpga_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = pdev->dev.of_node;
+	struct altera_fpga_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = altera_fpga_regulator_probe(pdev, priv);
+	if (ret)
+		return ret;
+
+	priv->fpga_base_addr = of_iomap(np, 0);
+	if (!priv->fpga_base_addr)

drivers/staging/fpga/altera.c line 712

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+		return -EINVAL;
+
+	priv->fpga_data_addr = of_iomap(np, 1);
+	if (!priv->fpga_data_addr) {
+		ret = -EINVAL;
+		goto err_unmap_base_addr;
+	}
+
+	priv->irq = irq_of_parse_and_map(np, 0);
+	if (priv->irq == NO_IRQ) {

NO_IRQ is not defined for all archs that's why you will get compilation error.

<= 0 should be fine here.

drivers/staging/fpga/altera.c line 715

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+		ret = -EINVAL;
+		goto err_unmap_data_addr;
+	}

Anyway for all of these you should be able to use
platform_get_resource
platform_get_irq functions

then you have simplified error path here.

Quoted 43 lines
drivers/staging/fpga/altera.c new file +43 -0
+
+	ret = request_irq(priv->irq, altera_fpga_isr, 0, "altera-fpga-mgr",
+			  priv);
+	if (ret < 0)
+		goto err_dispose_irq;
+
+	ret = fpga_mgr_register(dev, "Altera FPGA Manager",
+				&altera_altera_fpga_ops, priv);
+	if (ret)
+		goto err_free_irq;
+
+	return 0;
+
+err_free_irq:
+	free_irq(priv->irq, priv);
+err_dispose_irq:
+	irq_dispose_mapping(priv->irq);
+err_unmap_data_addr:
+	iounmap(priv->fpga_data_addr);
+err_unmap_base_addr:
+	iounmap(priv->fpga_base_addr);
+	return ret;
+}
+
+static int altera_fpga_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	fpga_mgr_remove(dev);
+	free_irq(priv->irq, priv);
+	irq_dispose_mapping(priv->irq);
+	iounmap(priv->fpga_data_addr);
+	iounmap(priv->fpga_base_addr);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id altera_fpga_of_match[] = {
+	{ .compatible = "altr,fpga-mgr", },
+	{},

drivers/staging/fpga/altera.c line 768

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+};
+
+MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
+#endif
+
+static struct platform_driver altera_fpga_driver = {
+	.remove = altera_fpga_remove,
+	.driver = {
+		.name	= "altera_fpga_manager",
+		.owner	= THIS_MODULE,

This line should go away

drivers/staging/fpga/altera.c line 771

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+		.of_match_table = of_match_ptr(altera_fpga_of_match),
+	},
+	.probe = altera_fpga_probe,

I tend to have probe and remove close to each other.

Quoted 4 lines
drivers/staging/fpga/altera.c new file +4 -0
+};
+
+static int __init altera_fpga_init(void)
+{

drivers/staging/fpga/altera.c line 785

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	return platform_driver_register(&altera_fpga_driver);
+}
+
+static void __exit altera_fpga_exit(void)
+{
+	platform_driver_unregister(&altera_fpga_driver);
+}
+
+module_init(altera_fpga_init);
+module_exit(altera_fpga_exit);

module_platform_driver here

drivers/staging/fpga/altera.c line 789

Quoted 5 lines
drivers/staging/fpga/altera.c new file +4 -0
+
+MODULE_AUTHOR("Alan Tull <[email protected]>");
+MODULE_DESCRIPTION("Altera FPGA Manager");
+MODULE_LICENSE("GPL v2");

Thanks,
Michal

#18 lore raw

Re: [PATCH v4 5/6] staging: fpga manager: framework core

In reply to [email protected]
Quoted 123 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Supports standard ops for low level FPGA drivers. Various manufacturors' FPGAs can be supported by adding low level drivers. Each driver needs to register its ops using fpga_mgr_register().

Exports methods of doing operations to program FPGAs. These should be sufficient for individual drivers to request FPGA programming directly if desired.

Adds a sysfs interface. The sysfs interface can be compiled out where desired in production builds.

Resume is supported by calling low level driver's resume function, then reloading the firmware image.

The following are exported as GPL:
* fpga_mgr_reset
Reset the FGPA.

* fpga_mgr_write
Write a image (in buffer) to the FPGA.

* fpga_mgr_firmware_write
Request firmware by file name and write it to the FPGA.

* fpga_mgr_name
Get name of FPGA manager.

* fpga_mgr_state
Get a state of framework as a string.

* fpga_mgr_register and fpga_mgr_remove Register/unregister low level fpga manager driver.

The following sysfs files are created:
* /sys/class/fpga_manager/<fpga>/name
Name of low level driver.

* /sys/class/fpga_manager/<fpga>/firmware
Name of FPGA image file to load using firmware class.

  $ echo image.rbf > /sys/class/fpga_manager/<fpga>/firmware

read: read back name of image file previous loaded

  $ cat /sys/class/fpga_manager/<fpga>/firmware

* /sys/class/fpga_manager/<fpga>/reset
reset the FPGA

  $ echo 1 > /sys/class/fpga_manager/<fpga>/reset

* /sys/class/fpga_manager/<fpga>/state
State of fpga framework state machine

---
v2: s/mangager/manager/
check for invalid request_nr
add fpga reset interface
rework the state code
remove FPGA_MGR_FAIL flag
add _ERR states to fpga manager states enum low level state op now returns a state enum value initialize framework state from driver state op remove unused fpga read stuff
merge sysfs.c into fpga-mgr.c
move suspend/resume from bus.c to fpga-mgr.c

v3: Add struct device to fpga_manager (not as a pointer)
Add to_fpga_manager
Don't get irq in fpga-mgr.c (let low level driver do it) remove from struct fpga_manager: nr, np, parent get rid of fpga_mgr_get_new_minor()
simplify fpga_manager_register:
reorder parameters
use dev instead of pdev
get rid of code that used to make more sense when this
was a char driver, don't alloc_chrdev_region
use a mutex instead of flags

v4: Move to drivers/staging

---
 drivers/staging/Kconfig         |    2 +
 drivers/staging/Makefile        |    1 +
 drivers/staging/fpga/Kconfig    |   21 ++
 drivers/staging/fpga/Makefile   |   10 +
 drivers/staging/fpga/fpga-mgr.c |  485 +++++++++++++++++++++++++++++++++++++++
 include/linux/fpga-mgr.h        |  104 +++++++++
 6 files changed, 623 insertions(+)
 create mode 100644 drivers/staging/fpga/Kconfig
 create mode 100644 drivers/staging/fpga/Makefile
 create mode 100644 drivers/staging/fpga/fpga-mgr.c
 create mode 100644 include/linux/fpga-mgr.h
drivers/staging/Kconfig +2 -0
index 4690ae9..4338a4c 100644
@@ -108,4 +108,6 @@ source "drivers/staging/skein/Kconfig"
 
 source "drivers/staging/unisys/Kconfig"
 
+source "drivers/staging/fpga/Kconfig"
+
 endif # STAGING
drivers/staging/Makefile +1 -0
index c780a0e..43654a2 100644
@@ -46,3 +46,4 @@ obj-$(CONFIG_MTD_SPINAND_MT29F)	+= mt29f_spinand/
 obj-$(CONFIG_GS_FPGABOOT)	+= gs_fpgaboot/
 obj-$(CONFIG_CRYPTO_SKEIN)	+= skein/
 obj-$(CONFIG_UNISYSSPAR)	+= unisys/
+obj-$(CONFIG_FPGA)		+= fpga/
drivers/staging/fpga/Kconfig new file +3 -0
new file mode 100644
index 0000000..e775b17
@@ -0,0 +1,21 @@
+#
+# FPGA framework configuration
+#

drivers/staging/fpga/Kconfig line 13

Quoted 10 lines
drivers/staging/fpga/Kconfig new file +10 -0
+
+menu "FPGA devices"
+
+config FPGA
+	tristate "FPGA Framework"
+	help
+	  Say Y here if you want support for configuring FPGAs from the
+	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
+	  manager drivers.
+

I would add here
if FPGA

drivers/staging/fpga/Kconfig line 16

Quoted 3 lines
drivers/staging/fpga/Kconfig new file +3 -0
+config FPGA_MGR_SYSFS
+	bool "FPGA Manager SysFS Interface"
+	depends on FPGA

remove this line.

drivers/staging/fpga/Kconfig line 20

Quoted 4 lines
drivers/staging/fpga/Kconfig new file +4 -0
+	depends on SYSFS
+	help
+	  FPGA Manager SysFS interface.
+

maybe this is not needed at, i2c has no this dependency too and compilation looks good too.

endif here

The reason is that all "low level" drivers will depend on FPGA that's why with this we don't need to add depends on FPGA to every entry.

Quoted 7 lines
drivers/staging/fpga/Kconfig new file +1 -0
+endmenu
drivers/staging/fpga/Makefile new file +0 -0
new file mode 100644
index 0000000..c8a676f

drivers/staging/fpga/Makefile line 8

Quoted 8 lines
drivers/staging/fpga/Makefile new file +8 -0
@@ -0,0 +1,10 @@
+#
+# Makefile for the fpga framework and fpga manager drivers.
+#
+
+fpga-mgr-core-y += fpga-mgr.o
+
+# Core FPGA Manager Framework
+obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o

Just
obj-$(CONFIG_FPGA) += fpga-mgr.o
should be enough here.

Quoted 18 lines
drivers/staging/fpga/Makefile new file +2 -0
+
+# FPGA Manager Drivers
drivers/staging/fpga/fpga-mgr.c new file +10 -0
new file mode 100644
index 0000000..d08cb82
@@ -0,0 +1,485 @@
+/*
+ * FPGA Manager Core
+ *
+ *  Copyright (C) 2013-2014 Altera Corporation
+ *
+ * With code from the mailing list:
+ * Copyright (C) 2013 Xilinx, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,

drivers/staging/fpga/fpga-mgr.c line 20

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+ * 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 <http://www.gnu.org/licenses/>.
+ */

Greg, Grant: What about to use just SPDX?

SPDX-License-Identifier: GPL-2.0

drivers/staging/fpga/fpga-mgr.c line 23

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/fpga-mgr.h>

I think it will be worth to add this to linux/fpga/ folder.

drivers/staging/fpga/fpga-mgr.c line 26

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+#include <linux/fs.h>
+#include <linux/idr.h>
+#include <linux/interrupt.h>

Already added by fpga-mgr.h

drivers/staging/fpga/fpga-mgr.c line 27

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+#include <linux/kernel.h>

linux/delay.h is adding this header already.

Quoted 5 lines
drivers/staging/fpga/fpga-mgr.c new file +5 -0
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pm.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>

drivers/staging/fpga/fpga-mgr.c line 42

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+
+static DEFINE_MUTEX(fpga_mgr_mutex);
+static DEFINE_IDA(fpga_mgr_ida);
+static struct class *fpga_mgr_class;
+
+static LIST_HEAD(fpga_manager_list);
+
+/*
+ * Get FPGA state from low level driver
+ * return value is same enum as fpga_mgr_framework_state

This is enum fpga_mgr_states based on fpga-mgr.h

drivers/staging/fpga/fpga-mgr.c line 45

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+ *
+ * This will be used to initialize framework state
+ */

We have an opportunity to have this in kernel-doc format. Description is pretty similar to kernel doc anyway.

drivers/staging/fpga/fpga-mgr.c line 46

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+int fpga_mgr_low_level_state(struct fpga_manager *mgr)

static enum fpga_mgr_states here

Quoted 158 lines
drivers/staging/fpga/fpga-mgr.c new file +158 -0
+{
+	if (!mgr || !mgr->mops || !mgr->mops->state)
+		return FPGA_MGR_STATE_UNKNOWN;
+
+	return mgr->mops->state(mgr);
+}
+
+/*
+ * Unlocked version
+ */
+static int __fpga_mgr_reset(struct fpga_manager *mgr)
+{
+	int ret;
+
+	if (!mgr->mops->reset)
+		return -EINVAL;
+
+	ret = mgr->mops->reset(mgr);
+
+	mgr->state = fpga_mgr_low_level_state(mgr);
+
+	return ret;
+}
+
+int fpga_mgr_reset(struct fpga_manager *mgr)
+{
+	int ret;
+
+	if (!mutex_trylock(&mgr->lock))
+		return -EBUSY;
+
+	ret = __fpga_mgr_reset(mgr);
+
+	mutex_unlock(&mgr->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_reset);
+
+/*
+ * Unlocked
+ */
+static int __fpga_mgr_stage_write_init(struct fpga_manager *mgr)
+{
+	int ret;
+
+	if (mgr->mops->write_init) {
+		mgr->state = FPGA_MGR_STATE_WRITE_INIT;
+		ret = mgr->mops->write_init(mgr);
+		if (ret) {
+			mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int __fpga_mgr_stage_write(struct fpga_manager *mgr, const char *buf,
+				  size_t count)
+{
+	int ret;
+
+	mgr->state = FPGA_MGR_STATE_WRITE;
+	ret = mgr->mops->write(mgr, buf, count);
+	if (ret) {
+		mgr->state = FPGA_MGR_STATE_WRITE_ERR;
+		return ret;
+	}
+
+	return 0;
+}
+
+static int __fpga_mgr_stage_write_complete(struct fpga_manager *mgr)
+{
+	int ret;
+
+	if (mgr->mops->write_complete) {
+		mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
+		ret = mgr->mops->write_complete(mgr);
+		if (ret) {
+			mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
+			return ret;
+		}
+	}
+
+	mgr->state = fpga_mgr_low_level_state(mgr);
+
+	return 0;
+}
+
+static int __fpga_mgr_write(struct fpga_manager *mgr, const char *buf,
+			    size_t count)
+{
+	int ret;
+
+	ret = __fpga_mgr_stage_write_init(mgr);
+	if (ret)
+		return ret;
+
+	ret = __fpga_mgr_stage_write(mgr, buf, count);
+	if (ret)
+		return ret;
+
+	return __fpga_mgr_stage_write_complete(mgr);
+}
+
+int fpga_mgr_write(struct fpga_manager *mgr, const char *buf, size_t count)
+{
+	int ret;
+
+	if (!mutex_trylock(&mgr->lock))
+		return -EBUSY;
+
+	dev_info(&mgr->dev, "writing buffer to %s\n", mgr->name);
+
+	ret = __fpga_mgr_write(mgr, buf, count);
+	mutex_unlock(&mgr->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_write);
+
+/*
+ * Grab lock, request firmware, and write out to the FPGA.
+ * Update the state before each step to provide info on what step
+ * failed if there is a failure.
+ */
+int fpga_mgr_firmware_write(struct fpga_manager *mgr, const char *image_name)
+{
+	const struct firmware *fw;
+	int ret;
+
+	if (!mutex_trylock(&mgr->lock))
+		return -EBUSY;
+
+	dev_info(&mgr->dev, "writing %s to %s\n", image_name, mgr->name);
+
+	mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
+	ret = request_firmware(&fw, image_name, &mgr->dev);
+	if (ret) {
+		mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
+		goto fw_wr_exit;
+	}
+
+	ret = __fpga_mgr_write(mgr, fw->data, fw->size);
+	if (ret)
+		goto fw_wr_exit;
+
+	strcpy(mgr->image_name, image_name);
+
+fw_wr_exit:
+	mutex_unlock(&mgr->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_firmware_write);
+

drivers/staging/fpga/fpga-mgr.c line 214

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+int fpga_mgr_name(struct fpga_manager *mgr, char *buf)
+{
+	if (!mgr)
+		return -ENODEV;
+
+	return sprintf(buf, "%s\n", mgr->name);
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_name);
+
+#if IS_ENABLED(CONFIG_FPGA_MGR_SYSFS)

This shouldn't be needed.

drivers/staging/fpga/fpga-mgr.c line 215

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+const char *state_str[] = {

static const ...

Quoted 81 lines
drivers/staging/fpga/fpga-mgr.c new file +81 -0
+	[FPGA_MGR_STATE_UNKNOWN] =		"unknown",
+	[FPGA_MGR_STATE_POWER_OFF] =		"power_off",
+	[FPGA_MGR_STATE_POWER_UP] =		"power_up",
+	[FPGA_MGR_STATE_RESET] =		"reset",
+
+	/* write sequence */
+	[FPGA_MGR_STATE_FIRMWARE_REQ] =		"firmware_request",
+	[FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =	"firmware_request_err",
+	[FPGA_MGR_STATE_WRITE_INIT] =		"write_init",
+	[FPGA_MGR_STATE_WRITE_INIT_ERR] =	"write_init_err",
+	[FPGA_MGR_STATE_WRITE] =		"write",
+	[FPGA_MGR_STATE_WRITE_ERR] =		"write_err",
+	[FPGA_MGR_STATE_WRITE_COMPLETE] =	"write_complete",
+	[FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =	"write_complete_err",
+
+	[FPGA_MGR_STATE_OPERATING] =		"operating",
+};
+
+/*
+ * class attributes
+ */
+static ssize_t fpga_mgr_name_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return fpga_mgr_name(mgr, buf);
+}
+
+static ssize_t fpga_mgr_state_show(struct device *dev,
+				   struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return sprintf(buf, "%s\n", state_str[mgr->state]);
+}
+
+static ssize_t fpga_mgr_firmware_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return sprintf(buf, "%s\n", mgr->image_name);
+}
+
+static ssize_t fpga_mgr_firmware_store(struct device *dev,
+				       struct device_attribute *attr,
+				       const char *buf, size_t count)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	unsigned int len;
+	char image_name[NAME_MAX];
+	int ret;
+
+	/* lose terminating \n */
+	strcpy(image_name, buf);
+	len = strlen(image_name);
+	if (image_name[len - 1] == '\n')
+		image_name[len - 1] = 0;
+
+	ret = fpga_mgr_firmware_write(mgr, image_name);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t fpga_mgr_reset_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t count)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	unsigned long val;
+	int ret;
+
+	ret = kstrtoul(buf, 0, &val);
+	if (ret)
+		return ret;
+
+	if (val == 1) {
+		ret = fpga_mgr_reset(mgr);

drivers/staging/fpga/fpga-mgr.c line 306

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+		if (ret)
+			return ret;
+	} else {
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static DEVICE_ATTR(name, S_IRUGO, fpga_mgr_name_show, NULL);

DEVICE_ATTR_RO - name change.

drivers/staging/fpga/fpga-mgr.c line 307

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+static DEVICE_ATTR(state, S_IRUGO, fpga_mgr_state_show, NULL);

DEVICE_ATTR_RO

drivers/staging/fpga/fpga-mgr.c line 309

Quoted 2 lines
drivers/staging/fpga/fpga-mgr.c new file +2 -0
+static DEVICE_ATTR(firmware, S_IRUGO | S_IWUSR,
+		   fpga_mgr_firmware_show, fpga_mgr_firmware_store);

DEVICE_ATTR_RW

drivers/staging/fpga/fpga-mgr.c line 310

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+static DEVICE_ATTR(reset, S_IWUSR, NULL, fpga_mgr_reset_store);

DEVICE_ATTR_WO

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+
+static struct attribute *fpga_mgr_attrs[] = {
+	&dev_attr_name.attr,
+	&dev_attr_state.attr,
+	&dev_attr_firmware.attr,
+	&dev_attr_reset.attr,
+	NULL,
+};
+
+static const struct attribute_group fpga_mgr_group = {

drivers/staging/fpga/fpga-mgr.c line 330

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+	.attrs = fpga_mgr_attrs,
+};
+
+const struct attribute_group *fpga_mgr_groups[] = {
+	&fpga_mgr_group,
+	NULL,
+};
+#else
+#define fpga_mgr_groups NULL
+#endif /* CONFIG_FPGA_MGR_SYSFS */

ATTRIBUTE_GROUPS(fpga_mgr)

Quoted 25 lines
drivers/staging/fpga/fpga-mgr.c new file +25 -0
+
+static int fpga_mgr_suspend(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	if (!mgr)
+		return -ENODEV;
+
+	if (mgr->mops->suspend)
+		return mgr->mops->suspend(mgr);
+
+	return 0;
+}
+
+static int fpga_mgr_resume(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	int ret = 0;
+
+	if (!mgr)
+		return -ENODEV;
+
+	if (mgr->mops->resume) {
+		ret = mgr->mops->resume(mgr);
+		if (ret)

drivers/staging/fpga/fpga-mgr.c line 365

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+			return ret;
+	}
+
+	if (strlen(mgr->image_name) != 0)
+		fpga_mgr_firmware_write(mgr, mgr->image_name);
+
+	return 0;
+}
+
+const struct dev_pm_ops fpga_mgr_dev_pm_ops = {

static

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+	.suspend	= fpga_mgr_suspend,
+	.resume		= fpga_mgr_resume,
+};
+
+static void fpga_mgr_dev_release(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
+	kfree(mgr);

drivers/staging/fpga/fpga-mgr.c line 385

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+}
+
+int fpga_mgr_register(struct device *dev, const char *name,
+		      struct fpga_manager_ops *mops,
+		      void *priv)
+{
+	struct fpga_manager *mgr;
+	int id, ret;
+
+	BUG_ON(!mops || !name || !strlen(name));

Isn't this pretty strong? Any reasonable reaction will be better.

drivers/staging/fpga/fpga-mgr.c line 393

Quoted 8 lines
drivers/staging/fpga/fpga-mgr.c new file +8 -0
+
+	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
+	if (id < 0)
+		return id;
+
+	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	if (!mgr)
+		return -ENOMEM;

here is missing error path handling because you got id. It should be moved before ida_simple_get call

Quoted 40 lines
drivers/staging/fpga/fpga-mgr.c new file +40 -0
+
+	mutex_init(&mgr->lock);
+
+	mgr->name = name;
+	mgr->mops = mops;
+	mgr->priv = priv;
+
+	/*
+	 * Initialize framework state by requesting low level driver read state
+	 * from device.  FPGA may be in reset mode or may have been programmed
+	 * by bootloader or EEPROM.
+	 */
+	mgr->state = fpga_mgr_low_level_state(mgr);
+
+	INIT_LIST_HEAD(&mgr->list);
+	mutex_lock(&fpga_mgr_mutex);
+	list_add(&mgr->list, &fpga_manager_list);
+	mutex_unlock(&fpga_mgr_mutex);
+
+	device_initialize(&mgr->dev);
+	mgr->dev.class = fpga_mgr_class;
+	mgr->dev.parent = dev;
+	mgr->dev.of_node = dev->of_node;
+	mgr->dev.release = fpga_mgr_dev_release;
+	mgr->dev.id = id;
+	dev_set_name(&mgr->dev, "%d", id);
+	ret = device_add(&mgr->dev);
+	if (ret)
+		goto error_device;
+
+	dev_info(&mgr->dev, "fpga manager [%s] registered as id %d\n",
+		 dev_name(&mgr->dev), id);
+
+	return 0;
+
+error_device:
+	ida_simple_remove(&fpga_mgr_ida, id);
+	kfree(mgr);
+
+	return ret;

drivers/staging/fpga/fpga-mgr.c line 443

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_register);
+
+void fpga_mgr_remove(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	int id;
+
+	if (mgr)
+		id = mgr->dev.id;

Why is here the checking? What cases this can cover if mrg is not there?

Quoted 75 lines
drivers/staging/fpga/fpga-mgr.c new file +42 -0
+
+	device_unregister(dev);
+
+	if (mgr) {
+		if (mgr->mops->fpga_remove)
+			mgr->mops->fpga_remove(mgr);
+
+		mutex_lock(&fpga_mgr_mutex);
+		list_del(&mgr->list);
+		mutex_unlock(&fpga_mgr_mutex);
+		kfree(mgr);
+		ida_simple_remove(&fpga_mgr_ida, id);
+	}
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_remove);
+
+static int __init fpga_mgr_dev_init(void)
+{
+	pr_info("FPGA Manager framework driver\n");
+
+	fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
+	if (IS_ERR(fpga_mgr_class))
+		return PTR_ERR(fpga_mgr_class);
+
+	fpga_mgr_class->dev_groups = fpga_mgr_groups;
+	fpga_mgr_class->pm = &fpga_mgr_dev_pm_ops;
+
+	return 0;
+}
+
+static void __exit fpga_mgr_dev_exit(void)
+{
+	class_destroy(fpga_mgr_class);
+	ida_destroy(&fpga_mgr_ida);
+}
+
+MODULE_AUTHOR("Alan Tull <[email protected]>");
+MODULE_DESCRIPTION("FPGA Manager framework driver");
+MODULE_LICENSE("GPL v2");
+
+subsys_initcall(fpga_mgr_dev_init);
+module_exit(fpga_mgr_dev_exit);
include/linux/fpga-mgr.h new file +27 -0
new file mode 100644
index 0000000..7ac762c
@@ -0,0 +1,104 @@
+/*
+ * FPGA Framework
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/limits.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+#ifndef _LINUX_FPGA_MGR_H
+#define _LINUX_FPGA_MGR_H
+

include/linux/fpga-mgr.h line 37

Quoted 10 lines
include/linux/fpga-mgr.h new file +10 -0
+struct fpga_manager;
+
+/*
+ * fpga_manager_ops are the low level functions implemented by a specific
+ * fpga manager driver.  Leaving any of these out that aren't needed is fine
+ * as they are all tested for NULL before being called.
+ */
+struct fpga_manager_ops {
+	/* Returns an enum value of the FPGA's state */
+	int (*state)(struct fpga_manager *mgr);

here return should be enum not int.

include/linux/fpga-mgr.h line 50

Quoted 13 lines
include/linux/fpga-mgr.h new file +13 -0
+
+	/* Put FPGA into reset state */
+	int (*reset)(struct fpga_manager *mgr);
+
+	/* Prepare the FPGA to receive confuration data */
+	int (*write_init)(struct fpga_manager *mgr);
+
+	/* Write count bytes of configuration data to the FPGA */
+	int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
+
+	/* Return FPGA to default state after writing is done */
+	int (*write_complete)(struct fpga_manager *mgr);
+

Here are missing read options which were in any previous series. But they can be added in the following patch.

Quoted 18 lines
include/linux/fpga-mgr.h new file +18 -0
+	/* Optional: Set FPGA into a specific state during driver remove */
+	void (*fpga_remove)(struct fpga_manager *mgr);
+
+	int (*suspend)(struct fpga_manager *mgr);
+
+	int (*resume)(struct fpga_manager *mgr);
+};
+
+/* Valid states may vary by manufacturer; superset is: */
+enum fpga_mgr_states {
+	FPGA_MGR_STATE_UNKNOWN,		/* can't determine state */
+	FPGA_MGR_STATE_POWER_OFF,	/* FPGA power is off */
+	FPGA_MGR_STATE_POWER_UP,	/* FPGA reports power is up */
+	FPGA_MGR_STATE_RESET,		/* FPGA held in reset state */
+
+	/* write sequence */
+	FPGA_MGR_STATE_FIRMWARE_REQ,	/* firmware request in progress */
+	FPGA_MGR_STATE_FIRMWARE_REQ_ERR, /* firmware request failed */

include/linux/fpga-mgr.h line 78

Quoted 10 lines
include/linux/fpga-mgr.h new file +10 -0
+	FPGA_MGR_STATE_WRITE_INIT,	/* preparing FPGA for programming */
+	FPGA_MGR_STATE_WRITE_INIT_ERR,	/* Error during write_init stage */
+	FPGA_MGR_STATE_WRITE,		/* FPGA ready to receive image data */
+	FPGA_MGR_STATE_WRITE_ERR,	/* Error while programming FPGA */
+	FPGA_MGR_STATE_WRITE_COMPLETE,	/* Doing post programming steps */
+	FPGA_MGR_STATE_WRITE_COMPLETE_ERR, /* Error during write_complete */
+
+	FPGA_MGR_STATE_OPERATING,	/* FPGA is programmed and operating */
+};
+

kernel-doc here will be good.

Quoted 5 lines
include/linux/fpga-mgr.h new file +5 -0
+struct fpga_manager {
+	const char *name;
+	struct device dev;
+	struct list_head list;
+	struct mutex lock;

include/linux/fpga-mgr.h line 93

Quoted 10 lines
include/linux/fpga-mgr.h new file +10 -0
+	enum fpga_mgr_states state;
+	char image_name[NAME_MAX];
+
+	struct fpga_manager_ops *mops;
+	void *priv;
+};
+
+#define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
+
+#if IS_ENABLED(CONFIG_FPGA)

you can simple remove this if.

include/linux/fpga-mgr.h line 104

Quoted 12 lines
include/linux/fpga-mgr.h new file +11 -0
+
+int fpga_mgr_firmware_write(struct fpga_manager *mgr, const char *image_name);
+int fpga_mgr_write(struct fpga_manager *mgr, const char *buf, size_t count);
+int fpga_mgr_name(struct fpga_manager *mgr, char *buf);
+int fpga_mgr_reset(struct fpga_manager *mgr);
+int fpga_mgr_register(struct device *pdev, const char *name,
+		      struct fpga_manager_ops *mops, void *priv);
+void fpga_mgr_remove(struct device *dev);
+
+#endif /* CONFIG_FPGA */
+#endif /*_LINUX_FPGA_MGR_H */

Thanks,
Michal

maintainer
#13 lore raw

Re: [PATCH v4 4/6] fpga manager: add sysfs interface document

In reply to Michal Simek
Quoted 7 lines · Michal Simek

Hi Greg,

Quoted 4 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add documentation for new fpga manager sysfs interface.

Quoted 8 lines
Quoted 5 lines
---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager

What's the rule for adding sysfs doc for driver which is added to staging? Is this location fine?

No, staging drivers should be "self-contained" so when^Wif we delete them, it is easy to do so :)

Put it in the same directory as your driver.

thanks,

greg k-h

#16 lore raw

Re: [PATCH v4 4/6] fpga manager: add sysfs interface document

In reply to [email protected]

On Tue, 9 Dec 2014 14:14:48 -0600

Quoted 7 lines
Documentation/ABI/testing/sysfs-class-fpga-manager new file +7 -0
+Contact:	Alan Tull <[email protected]>
+Description:	Read state of fpga framework state machine as a string.
+		Valid states may vary by manufacturer; superset is:
+		* unknown		= can't determine state
+		* power_off		= FPGA power is off
+		* power_up		= FPGA reports power is up
+		* reset			= FPGA held in reset state

Documentation/ABI/testing/sysfs-class-fpga-manager line 38

Quoted 10 lines
Documentation/ABI/testing/sysfs-class-fpga-manager new file +10 -0
+		* firmware_request	= firmware class request in progress
+		* firmware_request_err	= firmware request failed
+		* write_init		= FPGA being prepared for programming
+		* write_init_err	= Error while preparing FPGA for
+					  programming
+		* write			= FPGA ready to receive image data
+		* write_err		= Error while programming
+		* write_complete	= Doing post programming steps
+		* write_complete_err	= Error while doing post programming
+		* operating		= FPGA is programmed and operating

How does this work as a security model?

I want to assign an FPGA resource to a specific end user. How do I do that with a pile of sysfs nodes ?

maintainer
#26 lore raw
In reply to [email protected]
Quoted 3 lines · On Tue, Dec 09, 2014 at 02:14:44PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Improvements in this v3 and v4:

Due to the review comments here, and all of the kbuild issues found, please just work on a v5 series that I can apply after 3.19-rc1 is out. I'm dropping this series from my queue now because of those issues.

thanks,

greg k-h

#3 lore raw

Re: [PATCH v4 2/6] arm: dts: socfpga: add altera fpga manager

In reply to [email protected]

Hi!

Quoted 17 lines · On Tue, Dec 09, 2014 at 02:14:46PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +0 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};

arch/arm/boot/dts/socfpga.dtsi line 763

Quoted 10 lines
arch/arm/boot/dts/socfpga.dtsi +10 -0
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};

The fpgamgr is NOT a bus. You don't need ranges and/or simple-bus.

Regards,
Steffen

Signature · 4 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 |
#24 lore raw

Re: [PATCH v4 6/6] staging: fpga manager: add driver for altera socfpga manager

In reply to [email protected]
Quoted 251 lines · On Tue, Dec 09, 2014 at 02:14:50PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoC FPGA parts.

---
v2: fpga_manager struct now contains struct device
fpga_manager_register parameters now take device

v3: move to drivers/staging

---
 drivers/staging/fpga/Kconfig  |    6 +
 drivers/staging/fpga/Makefile |    1 +
 drivers/staging/fpga/altera.c |  789 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 796 insertions(+)
 create mode 100644 drivers/staging/fpga/altera.c
drivers/staging/fpga/Kconfig +6 -0
index e775b17..2944e3c 100644
@@ -18,4 +18,10 @@ config FPGA_MGR_SYSFS
 	help
 	  FPGA Manager SysFS interface.
 
+config FPGA_MGR_ALTERA
+	tristate "Altera"
+	depends on FPGA
+	help
+	  FPGA manager driver support for configuring Altera FPGAs.
+
 endmenu
drivers/staging/fpga/Makefile +1 -0
index c8a676f..6e0d2bf 100644
@@ -8,3 +8,4 @@ fpga-mgr-core-y += fpga-mgr.o
 obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_ALTERA)		+= altera.o
drivers/staging/fpga/altera.c new file +203 -0
new file mode 100644
index 0000000..14a16b4
@@ -0,0 +1,789 @@
+/*
+ * FPGA Manager Driver for Altera FPGAs
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <linux/cdev.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/fs.h>
+#include <linux/fpga-mgr.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/pm.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/regulator/consumer.h>
+
+/* Controls whether to use the Configuration with DCLK steps */
+#ifndef _ALTTERA_FPGAMGR_USE_DCLK
+#define _ALTTERA_FPGAMGR_USE_DCLK 0
+#endif
+
+/* Register offsets */
+#define ALTERA_FPGAMGR_STAT_OFST				0x0
+#define ALTERA_FPGAMGR_CTL_OFST					0x4
+#define ALTERA_FPGAMGR_DCLKCNT_OFST				0x8
+#define ALTERA_FPGAMGR_DCLKSTAT_OFST				0xc
+#define ALTERA_FPGAMGR_GPIO_INTEN_OFST				0x830
+#define ALTERA_FPGAMGR_GPIO_INTMSK_OFST				0x834
+#define ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST			0x838
+#define ALTERA_FPGAMGR_GPIO_INT_POL_OFST			0x83c
+#define ALTERA_FPGAMGR_GPIO_INTSTAT_OFST			0x840
+#define ALTERA_FPGAMGR_GPIO_RAW_INTSTAT_OFST			0x844
+#define ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST			0x84c
+#define ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST			0x850
+
+/* Register bit defines */
+/* ALTERA_FPGAMGR_STAT register mode field values */
+#define ALTERA_FPGAMGR_STAT_POWER_UP				0x0 /*ramping*/
+#define ALTERA_FPGAMGR_STAT_RESET				0x1
+#define ALTERA_FPGAMGR_STAT_CFG					0x2
+#define ALTERA_FPGAMGR_STAT_INIT				0x3
+#define ALTERA_FPGAMGR_STAT_USER_MODE				0x4
+#define ALTERA_FPGAMGR_STAT_UNKNOWN				0x5
+#define ALTERA_FPGAMGR_STAT_STATE_MASK				0x7
+/* This is a flag value that doesn't really happen in this register field */
+#define ALTERA_FPGAMGR_STAT_POWER_OFF				0x0
+
+#define MSEL_PP16_FAST_NOAES_NODC				0x0
+#define MSEL_PP16_FAST_AES_NODC					0x1
+#define MSEL_PP16_FAST_AESOPT_DC				0x2
+#define MSEL_PP16_SLOW_NOAES_NODC				0x4
+#define MSEL_PP16_SLOW_AES_NODC					0x5
+#define MSEL_PP16_SLOW_AESOPT_DC				0x6
+#define MSEL_PP32_FAST_NOAES_NODC				0x8
+#define MSEL_PP32_FAST_AES_NODC					0x9
+#define MSEL_PP32_FAST_AESOPT_DC				0xa
+#define MSEL_PP32_SLOW_NOAES_NODC				0xc
+#define MSEL_PP32_SLOW_AES_NODC					0xd
+#define MSEL_PP32_SLOW_AESOPT_DC				0xe
+#define ALTERA_FPGAMGR_STAT_MSEL_MASK				0x000000f8
+#define ALTERA_FPGAMGR_STAT_MSEL_SHIFT				3
+
+/* ALTERA_FPGAMGR_CTL register */
+#define ALTERA_FPGAMGR_CTL_EN					0x00000001
+#define ALTERA_FPGAMGR_CTL_NCE					0x00000002
+#define ALTERA_FPGAMGR_CTL_NCFGPULL				0x00000004
+
+#define CDRATIO_X1						0x00000000
+#define CDRATIO_X2						0x00000040
+#define CDRATIO_X4						0x00000080
+#define CDRATIO_X8						0x000000c0
+#define ALTERA_FPGAMGR_CTL_CDRATIO_MASK				0x000000c0
+
+#define ALTERA_FPGAMGR_CTL_AXICFGEN				0x00000100
+
+#define CFGWDTH_16						0x00000000
+#define CFGWDTH_32						0x00000200
+#define ALTERA_FPGAMGR_CTL_CFGWDTH_MASK				0x00000200
+
+/* ALTERA_FPGAMGR_DCLKSTAT register */
+#define ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE			0x1
+
+/* ALTERA_FPGAMGR_GPIO_* registers share the same bit positions */
+#define ALTERA_FPGAMGR_MON_NSTATUS				0x0001
+#define ALTERA_FPGAMGR_MON_CONF_DONE				0x0002
+#define ALTERA_FPGAMGR_MON_INIT_DONE				0x0004
+#define ALTERA_FPGAMGR_MON_CRC_ERROR				0x0008
+#define ALTERA_FPGAMGR_MON_CVP_CONF_DONE			0x0010
+#define ALTERA_FPGAMGR_MON_PR_READY				0x0020
+#define ALTERA_FPGAMGR_MON_PR_ERROR				0x0040
+#define ALTERA_FPGAMGR_MON_PR_DONE				0x0080
+#define ALTERA_FPGAMGR_MON_NCONFIG_PIN				0x0100
+#define ALTERA_FPGAMGR_MON_NSTATUS_PIN				0x0200
+#define ALTERA_FPGAMGR_MON_CONF_DONE_PIN			0x0400
+#define ALTERA_FPGAMGR_MON_FPGA_POWER_ON			0x0800
+#define ALTERA_FPGAMGR_MON_STATUS_MASK				0x0fff
+
+#define ALTERA_FPGAMGR_NUM_SUPPLIES 3
+#define ALTERA_RESUME_TIMEOUT 3
+
+#if IS_ENABLED(CONFIG_REGULATOR)
+/* In power-up order. Reverse for power-down. */
+static const char *supply_names[ALTERA_FPGAMGR_NUM_SUPPLIES] = {
+	"FPGA-1.5V",
+	"FPGA-1.1V",
+	"FPGA-2.5V",
+};
+#endif
+
+struct altera_fpga_priv {
+	void __iomem *fpga_base_addr;
+	void __iomem *fpga_data_addr;
+	struct completion status_complete;
+	int irq;
+	struct regulator *fpga_supplies[ALTERA_FPGAMGR_NUM_SUPPLIES];
+};
+
+struct cfgmgr_mode {
+	/* Values to set in the CTRL register */
+	u32 ctrl;
+
+	/* flag that this table entry is a valid mode */
+	bool valid;
+};
+
+/* For ALTERA_FPGAMGR_STAT_MSEL field */
+static struct cfgmgr_mode cfgmgr_modes[] = {
+	[MSEL_PP16_FAST_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_FAST_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_FAST_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP16_SLOW_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_SLOW_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_SLOW_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_FAST_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+	[MSEL_PP32_SLOW_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_SLOW_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_SLOW_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+};
+
+static u32 altera_fpga_reg_readl(struct altera_fpga_priv *priv, u32 reg_offset)
+{
+	return readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_writel(struct altera_fpga_priv *priv,
+				   u32 reg_offset, u32 value)
+{
+	writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static u32 altera_fpga_reg_raw_readl(struct altera_fpga_priv *priv,
+				     u32 reg_offset)
+{
+	return __raw_readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_raw_writel(struct altera_fpga_priv *priv,
+				       u32 reg_offset, u32 value)
+{
+	__raw_writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_data_writel(struct altera_fpga_priv *priv, u32 value)
+{
+	writel(value, priv->fpga_data_addr);
+}
+
+static inline void altera_fpga_reg_set_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val |= bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+

drivers/staging/fpga/altera.c line 213

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+static inline void altera_fpga_reg_clr_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val &= ~bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+

Why do you need these? Can't you just use regmap_mmio?

Quoted 497 lines
drivers/staging/fpga/altera.c new file +497 -0
+static int altera_fpga_mon_status_get(struct altera_fpga_priv *priv)
+{
+	return altera_fpga_reg_readl(priv,
+				     ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST) &
+		ALTERA_FPGAMGR_MON_STATUS_MASK;
+}
+
+static int altera_fpga_state_get(struct altera_fpga_priv *priv)
+{
+	int status = altera_fpga_mon_status_get(priv);
+
+	if ((status & ALTERA_FPGAMGR_MON_FPGA_POWER_ON) == 0)
+		return ALTERA_FPGAMGR_STAT_POWER_OFF;
+
+	return altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST) &
+		ALTERA_FPGAMGR_STAT_STATE_MASK;
+}
+
+static void altera_fpga_clear_done_status(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST,
+			       ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE);
+}
+
+/*
+ * Set the DCLKCNT, wait for DCLKSTAT to report the count completed, and clear
+ * the complete status.
+ */
+static int altera_fpga_dclk_set_and_wait_clear(struct altera_fpga_priv *priv,
+					       u32 count)
+{
+	int timeout = 2;
+	u32 done;
+
+	/* Clear any existing DONE status. */
+	if (altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST))
+		altera_fpga_clear_done_status(priv);
+
+	/* Issue the DCLK count. */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKCNT_OFST, count);
+
+	/* Poll DCLKSTAT to see if it completed in the timeout period. */
+	do {
+		done = altera_fpga_reg_readl(priv,
+					     ALTERA_FPGAMGR_DCLKSTAT_OFST);
+		if (done == ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE) {
+			altera_fpga_clear_done_status(priv);
+			return 0;
+		}
+		if (count <= 4)
+			udelay(1);
+		else
+			msleep(20);
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static int altera_fpga_wait_for_state(struct altera_fpga_priv *priv, u32 state)
+{
+	int timeout = 2;
+
+	/*
+	 * HW doesn't support an interrupt for changes in state, so poll to see
+	 * if it matches the requested state within the timeout period.
+	 */
+	do {
+		if ((altera_fpga_state_get(priv) & state) != 0)
+			return 0;
+		msleep(20);
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static void altera_fpga_enable_irqs(struct altera_fpga_priv *priv, u32 irqs)
+{
+	/* set irqs to level sensitive */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST, 0);
+
+	/* set interrupt polarity */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INT_POL_OFST, irqs);
+
+	/* clear irqs */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST, irqs);
+
+	/* unmask interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTMSK_OFST, 0);
+
+	/* enable interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, irqs);
+}
+
+static void altera_fpga_disable_irqs(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+}
+
+static irqreturn_t altera_fpga_isr(int irq, void *dev_id)
+{
+	struct altera_fpga_priv *priv = dev_id;
+	u32 irqs, st;
+	bool conf_done, nstatus;
+
+	/* clear irqs */
+	irqs = altera_fpga_reg_raw_readl(priv,
+					 ALTERA_FPGAMGR_GPIO_INTSTAT_OFST);
+
+	altera_fpga_reg_raw_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+				   irqs);
+
+	st = altera_fpga_reg_raw_readl(priv,
+				       ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST);
+	conf_done = (st & ALTERA_FPGAMGR_MON_CONF_DONE) != 0;
+	nstatus = (st & ALTERA_FPGAMGR_MON_NSTATUS) != 0;
+
+	/* success */
+	if (conf_done && nstatus) {
+		/* disable irqs */
+		altera_fpga_reg_raw_writel(priv,
+					   ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+		complete(&priv->status_complete);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int altera_fpga_wait_for_config_done(struct altera_fpga_priv *priv)
+{
+	int timeout, ret = 0;
+
+	altera_fpga_disable_irqs(priv);
+	init_completion(&priv->status_complete);
+	altera_fpga_enable_irqs(priv, ALTERA_FPGAMGR_MON_CONF_DONE);
+
+	timeout = wait_for_completion_interruptible_timeout(
+						&priv->status_complete,
+						msecs_to_jiffies(10));
+	if (timeout == 0)
+		ret = -ETIMEDOUT;
+
+	altera_fpga_disable_irqs(priv);
+	return ret;
+}
+
+static int altera_fpga_cfg_mode_get(struct altera_fpga_priv *priv)
+{
+	u32 msel;
+
+	msel = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST);
+	msel &= ALTERA_FPGAMGR_STAT_MSEL_MASK;
+	msel >>= ALTERA_FPGAMGR_STAT_MSEL_SHIFT;
+
+	/* Check that this MSEL setting is supported */
+	if ((msel >= sizeof(cfgmgr_modes)/sizeof(struct cfgmgr_mode)) ||
+	    !cfgmgr_modes[msel].valid)
+		return -EINVAL;
+
+	return msel;
+}
+
+static int altera_fpga_cfg_mode_set(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, mode;
+
+	/* get value from MSEL pins */
+	mode = altera_fpga_cfg_mode_get(priv);
+	if (mode < 0)
+		return mode;
+
+	/* Adjust CTRL for the CDRATIO */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CDRATIO_MASK;
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CFGWDTH_MASK;
+	ctrl_reg |= cfgmgr_modes[mode].ctrl;
+
+	/* Set NCE to 0. */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCE;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	return 0;
+}
+
+static int altera_fpga_reset(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, status;
+	int ret;
+
+	/*
+	 * Step 1:
+	 *  - Set CTRL.CFGWDTH, CTRL.CDRATIO to match cfg mode
+	 *  - Set CTRL.NCE to 0
+	 */
+	ret = altera_fpga_cfg_mode_set(priv);
+	if (ret)
+		return ret;
+
+	/* Step 2: Set CTRL.EN to 1 */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	/* Step 3: Set CTRL.NCONFIGPULL to 1 to put FPGA in reset */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg |= ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	/* Step 4: Wait for STATUS.MODE to report FPGA is in reset phase */
+	status = altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_RESET);
+
+	/* Step 5: Set CONTROL.NCONFIGPULL to 0 to release FPGA from reset */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	/* Timeout waiting for reset */
+	if (status)
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+/*
+ * Prepare the FPGA to receive the configuration data.
+ */
+static int altera_fpga_ops_configure_init(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	int ret;
+
+	/* Steps 1 - 5: Reset the FPGA */
+	ret = altera_fpga_reset(priv);
+	if (ret)
+		return ret;
+
+	/* Step 6: Wait for FPGA to enter configuration phase */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_CFG))
+		return -ETIMEDOUT;
+
+	/* Step 7: Clear nSTATUS interrupt */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+			       ALTERA_FPGAMGR_MON_NSTATUS);
+
+	/* Step 8: Set CTRL.AXICFGEN to 1 to enable transfer of config data */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	return 0;
+}
+
+/*
+ * Step 9: write data to the FPGA data register
+ */
+static int altera_fpga_ops_configure_write(struct fpga_manager *mgr,
+					   const char *buf, size_t count)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 *buffer_32 = (u32 *)buf;
+	size_t i = 0;
+
+	if (count <= 0)
+		return -EINVAL;
+
+	/* Write out the complete 32-bit chunks. */
+	while (count >= sizeof(u32)) {
+		altera_fpga_data_writel(priv, buffer_32[i++]);
+		count -= sizeof(u32);
+	}
+
+	/* Write out remaining non 32-bit chunks. */
+	switch (count) {
+	case 3:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x00ffffff);
+		break;
+	case 2:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x0000ffff);
+		break;
+	case 1:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x000000ff);
+		break;
+	default:
+		/* This will never happen. */
+		break;
+	}
+
+	return 0;
+}
+
+static int altera_fpga_ops_configure_complete(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 status;
+
+	/*
+	 * Step 10:
+	 *  - Observe CONF_DONE and nSTATUS (active low)
+	 *  - if CONF_DONE = 1 and nSTATUS = 1, configuration was successful
+	 *  - if CONF_DONE = 0 and nSTATUS = 0, configuration failed
+	 */
+	status = altera_fpga_wait_for_config_done(priv);
+	if (status)
+		return status;
+
+	/* Step 11: Clear CTRL.AXICFGEN to disable transfer of config data */
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	/*
+	 * Step 12:
+	 *  - Write 4 to DCLKCNT
+	 *  - Wait for STATUS.DCNTDONE = 1
+	 *  - Clear W1C bit in STATUS.DCNTDONE
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 4))
+		return -ETIMEDOUT;
+
+#if _ALTTERA_FPGAMGR_USE_DCLK
+	/* Step 13: Wait for STATUS.MODE to report INIT or USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_INIT |
+				       ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/*
+	 * Extra steps for Configuration with DCLK for Initialization Phase
+	 * Step 14 (using 4.2.1.2 steps), 15 (using 4.2.1.2 steps)
+	 *  - Write 0x5000 to DCLKCNT == the number of clocks needed to exit
+	 *    the Initialization Phase.
+	 *  - Poll until STATUS.DCNTDONE = 1, write 1 to clear
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 0x5000))
+		return -ETIMEDOUT;
+#endif
+
+	/* Step 13: Wait for STATUS.MODE to report USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/* Step 14: Set CTRL.EN to 0 */
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	return 0;
+}
+
+static int altera_fpga_ops_reset(struct fpga_manager *mgr)
+{
+	return altera_fpga_reset(mgr->priv);
+}
+
+/* Translate state register values to FPGA framework state */
+static const int altera_state_to_framework_state[] = {
+	[ALTERA_FPGAMGR_STAT_POWER_OFF] = FPGA_MGR_STATE_POWER_OFF,
+	[ALTERA_FPGAMGR_STAT_RESET] = FPGA_MGR_STATE_RESET,
+	[ALTERA_FPGAMGR_STAT_CFG] = FPGA_MGR_STATE_WRITE_INIT,
+	[ALTERA_FPGAMGR_STAT_INIT] = FPGA_MGR_STATE_WRITE_INIT,
+	[ALTERA_FPGAMGR_STAT_USER_MODE] = FPGA_MGR_STATE_OPERATING,
+	[ALTERA_FPGAMGR_STAT_UNKNOWN] = FPGA_MGR_STATE_UNKNOWN,
+};
+
+static int altera_fpga_ops_state(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;
+	int ret;
+
+	state = altera_fpga_state_get(priv);
+
+	if (state < ARRAY_SIZE(altera_state_to_framework_state))
+		ret = altera_state_to_framework_state[state];
+	else
+		ret = FPGA_MGR_STATE_UNKNOWN;
+
+	return ret;
+}
+
+#if IS_ENABLED(CONFIG_REGULATOR)
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	int i, ret;
+
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		ret = regulator_enable(priv->fpga_supplies[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+	int i;
+
+	for (i = ALTERA_FPGAMGR_NUM_SUPPLIES - 1; i >= 0; i--)
+		regulator_disable(priv->fpga_supplies[i]);
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	struct regulator *supply;
+	unsigned int i;
+
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		supply = devm_regulator_get_optional(&pdev->dev,
+						     supply_names[i]);
+		if (IS_ERR(supply)) {
+			dev_err(&pdev->dev, "could not get regulators");
+			return -EPROBE_DEFER;
+		}
+		priv->fpga_supplies[i] = supply;
+	}
+
+	return altera_fpga_regulators_on(priv);
+}
+#else
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+#endif
+
+static int altera_fpga_suspend(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	altera_fpga_regulators_power_off(priv);
+
+	return 0;
+}
+
+static int altera_fpga_resume(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;
+	unsigned int timeout;
+	int ret;
+
+	ret = altera_fpga_regulators_on(priv);
+	if (ret)
+		return ret;
+
+	for (timeout = 0; timeout < ALTERA_RESUME_TIMEOUT; timeout++) {
+		state = altera_fpga_state_get(priv);
+		if (state != ALTERA_FPGAMGR_STAT_POWER_OFF)
+			break;
+		msleep(20);
+	}
+	if (state == ALTERA_FPGAMGR_STAT_POWER_OFF)
+		return -ETIMEDOUT;
+
+	return ret;
+}
+
+struct fpga_manager_ops altera_altera_fpga_ops = {
+	.reset = altera_fpga_ops_reset,
+	.state = altera_fpga_ops_state,
+	.write_init = altera_fpga_ops_configure_init,
+	.write = altera_fpga_ops_configure_write,
+	.write_complete = altera_fpga_ops_configure_complete,
+	.suspend = altera_fpga_suspend,
+	.resume = altera_fpga_resume,
+};
+
+static int altera_fpga_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = pdev->dev.of_node;
+	struct altera_fpga_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = altera_fpga_regulator_probe(pdev, priv);
+	if (ret)
+		return ret;
+
+	priv->fpga_base_addr = of_iomap(np, 0);
+	if (!priv->fpga_base_addr)
+		return -EINVAL;
+
+	priv->fpga_data_addr = of_iomap(np, 1);
+	if (!priv->fpga_data_addr) {
+		ret = -EINVAL;
+		goto err_unmap_base_addr;
+	}
+

drivers/staging/fpga/altera.c line 720

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	priv->irq = irq_of_parse_and_map(np, 0);
+	if (priv->irq == NO_IRQ) {
+		ret = -EINVAL;
+		goto err_unmap_data_addr;
+	}
+
+	ret = request_irq(priv->irq, altera_fpga_isr, 0, "altera-fpga-mgr",
+			  priv);
+	if (ret < 0)
+		goto err_dispose_irq;

devm_request_irq

Quoted 73 lines
drivers/staging/fpga/altera.c new file +69 -0
+
+	ret = fpga_mgr_register(dev, "Altera FPGA Manager",
+				&altera_altera_fpga_ops, priv);
+	if (ret)
+		goto err_free_irq;
+
+	return 0;
+
+err_free_irq:
+	free_irq(priv->irq, priv);
+err_dispose_irq:
+	irq_dispose_mapping(priv->irq);
+err_unmap_data_addr:
+	iounmap(priv->fpga_data_addr);
+err_unmap_base_addr:
+	iounmap(priv->fpga_base_addr);
+	return ret;
+}
+
+static int altera_fpga_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	fpga_mgr_remove(dev);
+	free_irq(priv->irq, priv);
+	irq_dispose_mapping(priv->irq);
+	iounmap(priv->fpga_data_addr);
+	iounmap(priv->fpga_base_addr);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id altera_fpga_of_match[] = {
+	{ .compatible = "altr,fpga-mgr", },
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
+#endif
+
+static struct platform_driver altera_fpga_driver = {
+	.remove = altera_fpga_remove,
+	.driver = {
+		.name	= "altera_fpga_manager",
+		.owner	= THIS_MODULE,
+		.of_match_table = of_match_ptr(altera_fpga_of_match),
+	},
+	.probe = altera_fpga_probe,
+};
+
+static int __init altera_fpga_init(void)
+{
+	return platform_driver_register(&altera_fpga_driver);
+}
+
+static void __exit altera_fpga_exit(void)
+{
+	platform_driver_unregister(&altera_fpga_driver);
+}
+
+module_init(altera_fpga_init);
+module_exit(altera_fpga_exit);
+
+MODULE_AUTHOR("Alan Tull <[email protected]>");
+MODULE_DESCRIPTION("Altera FPGA Manager");
+MODULE_LICENSE("GPL v2");
Signature · 1 line
1.7.9.5

Regards,
Steffen

Signature · 4 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 |
#14 lore raw

Re: [PATCH v4 4/6] fpga manager: add sysfs interface document

In reply to Greg KH
Quoted 14 lines · Greg KH
Quoted 13 lines · Michal Simek

Hi Greg,

Quoted 9 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add documentation for new fpga manager sysfs interface.

---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager
Quoted 7 lines
Quoted 2 lines

What's the rule for adding sysfs doc for driver which is added to staging? Is this location fine?

No, staging drivers should be "self-contained" so when^Wif we delete them, it is easy to do so :)

Put it in the same directory as your driver.

That's what I thought.
Thanks for confirmation.

Thanks,
Michal

author
#10 lore raw

Re: [PATCH v4 3/6] ARM: socfpga: defconfig: enable fpga manager

In reply to Michal Simek
Quoted 32 lines · Michal Simek

Hi Alan,

Quoted 15 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Enable FPGA manager for Altera socfpga.

---
 arch/arm/configs/socfpga_defconfig |    4 ++++
 1 file changed, 4 insertions(+)
arch/arm/configs/socfpga_defconfig +0 -0
index a2956c3..2740057 100644

arch/arm/configs/socfpga_defconfig CONFIG_USB_DWC2=y · line 95

Quoted 10 lines
arch/arm/configs/socfpga_defconfig +4 -0
@@ -86,6 +86,10 @@ CONFIG_USB_DWC2=y
 CONFIG_USB_DWC2_HOST=y
 CONFIG_MMC=y
 CONFIG_MMC_DW=y
+CONFIG_STAGING=y
+CONFIG_FPGA=y
+CONFIG_FPGA_MGR_SYSFS=y
+CONFIG_FPGA_MGR_ALTERA=y
 CONFIG_EXT2_FS=y
 CONFIG_EXT2_FS_XATTR=y
 CONFIG_EXT2_FS_POSIX_ACL=y

This should the last patch in this series because you are changing Kconfig in 5/6 and 6/6.

Quoted 3 lines

Thanks,
Michal

OK, I'm fixing this in v5, also taking your other comments. Thanks for the review.

Alan

author
#15 lore raw

Re: [PATCH v4 4/6] fpga manager: add sysfs interface document

In reply to Michal Simek
Quoted 21 lines · Michal Simek
Quoted 20 lines · Greg KH
Quoted 7 lines · Michal Simek

Hi Greg,

Quoted 4 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add documentation for new fpga manager sysfs interface.

Quoted 8 lines
Quoted 5 lines
---
 Documentation/ABI/testing/sysfs-class-fpga-manager |   38 ++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-fpga-manager

What's the rule for adding sysfs doc for driver which is added to staging? Is this location fine?

No, staging drivers should be "self-contained" so when^Wif we delete them, it is easy to do so :)

Quoted 9 lines
Quoted 1 line

Put it in the same directory as your driver.

That's what I thought.
Thanks for confirmation.

Thanks,
Michal

OK I've got that in v5 also.

Alan

author
#4 lore raw

Re: [PATCH v4 2/6] arm: dts: socfpga: add altera fpga manager

In reply to Steffen Trumtrar
Quoted 33 lines · Steffen Trumtrar

Hi!

Quoted 17 lines · On Tue, Dec 09, 2014 at 02:14:46PM -0600…

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +0 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};

arch/arm/boot/dts/socfpga.dtsi line 763

Quoted 10 lines
arch/arm/boot/dts/socfpga.dtsi +10 -0
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};

The fpgamgr is NOT a bus. You don't need ranges and/or simple-bus.

Quoted 2 lines

Regards,
Steffen

Actually, I am going to be using this when I add the DT notification stuff. That stuff is waiting for DT overlays to show up in the main tree.

So the choice it to either change this file now, once, or do the minimal changes and have to change it again later.

Alan

Quoted 12 lines
Signature · 10 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 |
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [PATCH v4 2/6] arm: dts: socfpga: add altera fpga manager

Quoted 38 lines · atull
Quoted 33 lines · Steffen Trumtrar

Hi!

Quoted 17 lines · On Tue, Dec 09, 2014 at 02:14:46PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +0 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};

arch/arm/boot/dts/socfpga.dtsi line 763

Quoted 10 lines
arch/arm/boot/dts/socfpga.dtsi +10 -0
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};

The fpgamgr is NOT a bus. You don't need ranges and/or simple-bus.

Quoted 2 lines

Regards,
Steffen

Quoted 7 lines

Actually, I am going to be using this when I add the DT notification stuff. That stuff is waiting for DT overlays to show up in the main tree.

So the choice it to either change this file now, once, or do the minimal changes and have to change it again later.

Yes, that is what I thought. And still, devices can NOT show up at the fpgamgr. They show up on the various bridges.

And as far as I understand the DT overlay stuff, you specify in the overlay what the parent node will be.
For i2c this is one of the i2c-hosts, for spi one of the spi-hosts and for IP cores in a bitstream one of the bridges.

The fpgamgr is, from my understanding, only responsible for writing the bitstream to the FPGA; nothing more.

Regards,
Steffen

Signature · 4 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 |
author
#27 lore raw
In reply to Greg KH
Quoted 8 lines · Greg KH
Quoted 3 lines · On Tue, Dec 09, 2014 at 02:14:44PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Improvements in this v3 and v4:

Due to the review comments here, and all of the kbuild issues found, please just work on a v5 series that I can apply after 3.19-rc1 is out. I'm dropping this series from my queue now because of those issues.

That sounds great.

Thank you,

Alan

Quoted 9 lines

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [PATCH v4 2/6] arm: dts: socfpga: add altera fpga manager

In reply to Steffen Trumtrar
Quoted 50 lines · Steffen Trumtrar
Quoted 38 lines · atull
Quoted 33 lines · Steffen Trumtrar

Hi!

Quoted 17 lines · On Tue, Dec 09, 2014 at 02:14:46PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +0 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};

arch/arm/boot/dts/socfpga.dtsi line 763

Quoted 10 lines
arch/arm/boot/dts/socfpga.dtsi +10 -0
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};

The fpgamgr is NOT a bus. You don't need ranges and/or simple-bus.

Quoted 2 lines

Regards,
Steffen

Quoted 7 lines

Actually, I am going to be using this when I add the DT notification stuff. That stuff is waiting for DT overlays to show up in the main tree.

So the choice it to either change this file now, once, or do the minimal changes and have to change it again later.

Yes, that is what I thought. And still, devices can NOT show up at the fpgamgr. They show up on the various bridges.

Quoted 7 lines

And as far as I understand the DT overlay stuff, you specify in the overlay what the parent node will be.
For i2c this is one of the i2c-hosts, for spi one of the spi-hosts and for IP cores in a bitstream one of the bridges.

The fpgamgr is, from my understanding, only responsible for writing the bitstream to the FPGA; nothing more.

+1 on this.
I think you can easily decide later where DT overlays will be added.

Thanks,
Michal

author
#7 lore raw

Re: [PATCH v4 2/6] arm: dts: socfpga: add altera fpga manager

Quoted 56 lines · Michal Simek
Quoted 55 lines · Steffen Trumtrar
Quoted 38 lines · atull
Quoted 33 lines · Steffen Trumtrar

Hi!

Quoted 17 lines · On Tue, Dec 09, 2014 at 02:14:46PM -0600, [email protected] wrote:

From: Alan Tull <[email protected]>

Add Altera FGPA manager to device tree.

---
 arch/arm/boot/dts/socfpga.dtsi |   10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm/boot/dts/socfpga.dtsi +0 -0
index 4472fd9..bab98b6 100644
@@ -751,5 +751,15 @@
 			compatible = "altr,sys-mgr", "syscon";
 			reg = <0xffd08000 0x4000>;
 		};

arch/arm/boot/dts/socfpga.dtsi line 763

Quoted 10 lines
arch/arm/boot/dts/socfpga.dtsi +10 -0
+
+		hps_0_fpgamgr: fpgamgr@0xff706000 {
+			compatible = "altr,fpga-mgr", "simple-bus";
+			reg = <0xFF706000 0x1000
+			       0xFFB90000 0x1000>;
+			interrupts = <0 175 4>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			ranges;
+		};

The fpgamgr is NOT a bus. You don't need ranges and/or simple-bus.

Quoted 2 lines

Regards,
Steffen

Quoted 7 lines

Actually, I am going to be using this when I add the DT notification stuff. That stuff is waiting for DT overlays to show up in the main tree.

So the choice it to either change this file now, once, or do the minimal changes and have to change it again later.

Yes, that is what I thought. And still, devices can NOT show up at the fpgamgr. They show up on the various bridges.

And as far as I understand the DT overlay stuff, you specify in the overlay what the parent node will be.
For i2c this is one of the i2c-hosts, for spi one of the spi-hosts and for IP cores in a bitstream one of the bridges.

Quoted 10 lines
Quoted 2 lines

The fpgamgr is, from my understanding, only responsible for writing the bitstream to the FPGA; nothing more.

+1 on this.
I think you can easily decide later where DT overlays will be added.

Thanks,
Michal

OK guys, I'll simplify it down to the following in v5:

+ hps_0_fpgamgr: fpgamgr@0xff706000 {
+ compatible = "altr,fpga-mgr";
+ reg = <0xFF706000 0x1000
+ 0xFFB90000 0x1000>;
+ interrupts = <0 175 4>;
+ };

Alan

author
#25 lore raw

Re: [PATCH v4 6/6] staging: fpga manager: add driver for altera socfpga manager

In reply to Steffen Trumtrar
Quoted 14 lines · Steffen Trumtrar
Quoted 14 lines
drivers/staging/fpga/altera.c new file +14 -0
+static inline void altera_fpga_reg_set_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val |= bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+
+static inline void altera_fpga_reg_clr_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
Quoted 8 lines

drivers/staging/fpga/altera.c line 213

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val &= ~bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+

Why do you need these? Can't you just use regmap_mmio?

I would like to do that as an improvement as time allows, not now.

Quoted 6 lines

drivers/staging/fpga/altera.c line 720

Quoted 4 lines
drivers/staging/fpga/altera.c new file +4 -0
+	ret = request_irq(priv->irq, altera_fpga_isr, 0, "altera-fpga-mgr",
+			  priv);
+	if (ret < 0)
+		goto err_dispose_irq;

devm_request_irq

Will do, thanks.

Alan

author
#19 lore raw

Re: [PATCH v4 5/6] staging: fpga manager: framework core

In reply to Michal Simek

On Wed, 10 Dec 2014, Michal Simek wrote:

Hi Michal,

Thanks for the review comments. I have incorporated most of them. A few questions below.

Quoted 11 lines
Quoted 11 lines
drivers/staging/fpga/Kconfig new file +8 -0
@@ -0,0 +1,21 @@
+#
+# FPGA framework configuration
+#
+
+menu "FPGA devices"
+
+config FPGA
+	tristate "FPGA Framework"
Quoted 9 lines

drivers/staging/fpga/Kconfig line 13

Quoted 5 lines
drivers/staging/fpga/Kconfig new file +5 -0
+	help
+	  Say Y here if you want support for configuring FPGAs from the
+	  kernel.  The FPGA framework adds a FPGA manager class and FPGA
+	  manager drivers.
+

I would add here
if FPGA

OK

Quoted 6 lines

drivers/staging/fpga/Kconfig line 16

Quoted 4 lines
drivers/staging/fpga/Kconfig new file +3 -0
+config FPGA_MGR_SYSFS
+	bool "FPGA Manager SysFS Interface"
+	depends on FPGA

remove this line.

Good

Quoted 9 lines

drivers/staging/fpga/Kconfig line 20

Quoted 5 lines
drivers/staging/fpga/Kconfig new file +4 -0
+	depends on SYSFS
+	help
+	  FPGA Manager SysFS interface.
+

maybe this is not needed at, i2c has no this dependency too and compilation looks good too.

Seems like we are dependent on sysfs, so I need 'depends on SYSFS', right? Also, I want to be able to disable the sysfs interface (more below on that).

Quoted 2 lines

endif here

OK

Quoted 15 lines

The reason is that all "low level" drivers will depend on FPGA that's why with this we don't need to add depends on FPGA to every entry.

Quoted 11 lines
drivers/staging/fpga/Kconfig new file +1 -0
+endmenu
drivers/staging/fpga/Makefile new file +4 -0
new file mode 100644
index 0000000..c8a676f
@@ -0,0 +1,10 @@
+#
+# Makefile for the fpga framework and fpga manager drivers.
+#
+
Quoted 8 lines

drivers/staging/fpga/Makefile line 8

Quoted 4 lines
drivers/staging/fpga/Makefile new file +4 -0
+fpga-mgr-core-y += fpga-mgr.o
+
+# Core FPGA Manager Framework
+obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o

Just
obj-$(CONFIG_FPGA) += fpga-mgr.o
should be enough here.

Yes

Quoted 32 lines
Quoted 19 lines
drivers/staging/fpga/Makefile new file +2 -0
+
+# FPGA Manager Drivers
drivers/staging/fpga/fpga-mgr.c new file +10 -0
new file mode 100644
index 0000000..d08cb82
@@ -0,0 +1,485 @@
+/*
+ * FPGA Manager Core
+ *
+ *  Copyright (C) 2013-2014 Altera Corporation
+ *
+ * With code from the mailing list:
+ * Copyright (C) 2013 Xilinx, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,

drivers/staging/fpga/fpga-mgr.c line 20

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+ * 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 <http://www.gnu.org/licenses/>.
+ */

Greg, Grant: What about to use just SPDX?

Quoted 2 lines

SPDX-License-Identifier: GPL-2.0

This is a standared GPLv2 header, we're keeping it. Our legal dept wants it.

Quoted 6 lines

drivers/staging/fpga/fpga-mgr.c line 23

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/fpga-mgr.h>

I think it will be worth to add this to linux/fpga/ folder.

OK

Quoted 9 lines

drivers/staging/fpga/fpga-mgr.c line 26

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+#include <linux/fs.h>
+#include <linux/idr.h>
+#include <linux/interrupt.h>

Already added by fpga-mgr.h

drivers/staging/fpga/fpga-mgr.c line 27

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+#include <linux/kernel.h>

linux/delay.h is adding this header already.

I'm cleaning up the includes in v5.

Quoted 4 lines

drivers/staging/fpga/fpga-mgr.c line 42

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+/*
+ * Get FPGA state from low level driver
+ * return value is same enum as fpga_mgr_framework_state
Quoted 8 lines

This is enum fpga_mgr_states based on fpga-mgr.h

drivers/staging/fpga/fpga-mgr.c line 45

Quoted 3 lines
drivers/staging/fpga/fpga-mgr.c new file +3 -0
+ *
+ * This will be used to initialize framework state
+ */

We have an opportunity to have this in kernel-doc format. Description is pretty similar to kernel doc anyway.

I'll change the core (fpga-mgr.c and fpga-mgr.h) to use kernel-doc. I'll leave it out of the altera.c part though.

Quoted 4 lines

drivers/staging/fpga/fpga-mgr.c line 46

Quoted 2 lines
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+int fpga_mgr_low_level_state(struct fpga_manager *mgr)

static enum fpga_mgr_states here

OK, there are several places where I've changed to use the enum.

Quoted 5 lines

drivers/staging/fpga/fpga-mgr.c line 214

Quoted 1 line
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+#if IS_ENABLED(CONFIG_FPGA_MGR_SYSFS)

This shouldn't be needed.

From the way the discussion has gone about this, there is a desire for

different interfaces for this. I want it to be possible to disable this sysfs interface if I'm just using the exported functions or some other future interface. Part of my main point here is to export enough functionality in the core functions to make it possible to build other interfaces that people
want to support different use cases or use models. Since we are going to be
in staging, that is a good place to be doing this.

Quoted 4 lines

drivers/staging/fpga/fpga-mgr.c line 215

Quoted 2 lines
drivers/staging/fpga/fpga-mgr.c new file +1 -0
+const char *state_str[] = {

static const ...

Yes, well actually 'static const *const state_str[]...'

Quoted 39 lines
Quoted 22 lines
drivers/staging/fpga/fpga-mgr.c new file +21 -0
+	[FPGA_MGR_STATE_UNKNOWN] =		"unknown",
+	[FPGA_MGR_STATE_POWER_OFF] =		"power_off",
+	[FPGA_MGR_STATE_POWER_UP] =		"power_up",
+	[FPGA_MGR_STATE_RESET] =		"reset",
+
+	/* write sequence */
+	[FPGA_MGR_STATE_FIRMWARE_REQ] =		"firmware_request",
+	[FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =	"firmware_request_err",
+	[FPGA_MGR_STATE_WRITE_INIT] =		"write_init",
+	[FPGA_MGR_STATE_WRITE_INIT_ERR] =	"write_init_err",
+	[FPGA_MGR_STATE_WRITE] =		"write",
+	[FPGA_MGR_STATE_WRITE_ERR] =		"write_err",
+	[FPGA_MGR_STATE_WRITE_COMPLETE] =	"write_complete",
+	[FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =	"write_complete_err",
+
+	[FPGA_MGR_STATE_OPERATING] =		"operating",
+};
+
+/*
+ * class attributes
+ */

drivers/staging/fpga/fpga-mgr.c line 306

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +9 -0
+static ssize_t fpga_mgr_name_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	return fpga_mgr_name(mgr, buf);
+}
+
 
+static DEVICE_ATTR(name, S_IRUGO, fpga_mgr_name_show, NULL);

DEVICE_ATTR_RO - name change.

Quoted 1 line

+static DEVICE_ATTR(state, S_IRUGO, fpga_mgr_state_show, NULL);

DEVICE_ATTR_RO

Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 309

Quoted 2 lines
drivers/staging/fpga/fpga-mgr.c new file +2 -0
+static DEVICE_ATTR(firmware, S_IRUGO | S_IWUSR,
+		   fpga_mgr_firmware_show, fpga_mgr_firmware_store);

DEVICE_ATTR_RW

Quoted 1 line

+static DEVICE_ATTR(reset, S_IWUSR, NULL, fpga_mgr_reset_store);

DEVICE_ATTR_WO

Good, thanks.

Quoted 15 lines
Quoted 15 lines
drivers/staging/fpga/fpga-mgr.c new file +14 -0
+
+static struct attribute *fpga_mgr_attrs[] = {
+	&dev_attr_name.attr,
+	&dev_attr_state.attr,
+	&dev_attr_firmware.attr,
+	&dev_attr_reset.attr,
+	NULL,
+};
+
+static const struct attribute_group fpga_mgr_group = {
+	.attrs = fpga_mgr_attrs,
+};
+
+const struct attribute_group *fpga_mgr_groups[] = {
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 330

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+	&fpga_mgr_group,
+	NULL,
+};
+#else
+#define fpga_mgr_groups NULL
+#endif /* CONFIG_FPGA_MGR_SYSFS */

ATTRIBUTE_GROUPS(fpga_mgr)

Yes

Quoted 30 lines
Quoted 30 lines
drivers/staging/fpga/fpga-mgr.c new file +29 -0
+
+static int fpga_mgr_suspend(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	if (!mgr)
+		return -ENODEV;
+
+	if (mgr->mops->suspend)
+		return mgr->mops->suspend(mgr);
+
+	return 0;
+}
+
+static int fpga_mgr_resume(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	int ret = 0;
+
+	if (!mgr)
+		return -ENODEV;
+
+	if (mgr->mops->resume) {
+		ret = mgr->mops->resume(mgr);
+		if (ret)
+			return ret;
+	}
+
+	if (strlen(mgr->image_name) != 0)
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 365

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+		fpga_mgr_firmware_write(mgr, mgr->image_name);
+
+	return 0;
+}
+
+const struct dev_pm_ops fpga_mgr_dev_pm_ops = {

static

Yes

Quoted 15 lines
Quoted 15 lines
drivers/staging/fpga/fpga-mgr.c new file +14 -0
+	.suspend	= fpga_mgr_suspend,
+	.resume		= fpga_mgr_resume,
+};
+
+static void fpga_mgr_dev_release(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
+	kfree(mgr);
+}
+
+int fpga_mgr_register(struct device *dev, const char *name,
+		      struct fpga_manager_ops *mops,
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 385

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+		      void *priv)
+{
+	struct fpga_manager *mgr;
+	int id, ret;
+
+	BUG_ON(!mops || !name || !strlen(name));

Isn't this pretty strong? Any reasonable reaction will be better.

You are right. Changing it to return an error.

Quoted 13 lines

drivers/staging/fpga/fpga-mgr.c line 393

Quoted 9 lines
drivers/staging/fpga/fpga-mgr.c new file +8 -0
+
+	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
+	if (id < 0)
+		return id;
+
+	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	if (!mgr)
+		return -ENOMEM;

here is missing error path handling because you got id. It should be moved before ida_simple_get call

OK

Quoted 12 lines

drivers/staging/fpga/fpga-mgr.c line 443

Quoted 8 lines
drivers/staging/fpga/fpga-mgr.c new file +8 -0
+
+void fpga_mgr_remove(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	int id;
+
+	if (mgr)
+		id = mgr->dev.id;

Why is here the checking? What cases this can cover if mrg is not there?

Not needed. Removing it.

Quoted 50 lines
Quoted 50 lines
drivers/staging/fpga/fpga-mgr.c new file +48 -0
+
+	device_unregister(dev);
+
+	if (mgr) {
+		if (mgr->mops->fpga_remove)
+			mgr->mops->fpga_remove(mgr);
+
+		mutex_lock(&fpga_mgr_mutex);
+		list_del(&mgr->list);
+		mutex_unlock(&fpga_mgr_mutex);
+		kfree(mgr);
+		ida_simple_remove(&fpga_mgr_ida, id);
+	}
+}
+EXPORT_SYMBOL_GPL(fpga_mgr_remove);
+
 
+++ b/include/linux/fpga-mgr.h
@@ -0,0 +1,104 @@
+/*
+ * FPGA Framework
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/limits.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+#ifndef _LINUX_FPGA_MGR_H
+#define _LINUX_FPGA_MGR_H
+
+struct fpga_manager;
+
+/*
+ * fpga_manager_ops are the low level functions implemented by a specific
Quoted 9 lines

drivers/staging/fpga/fpga-mgr.c line 37

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+ * fpga manager driver.  Leaving any of these out that aren't needed is fine
+ * as they are all tested for NULL before being called.
+ */
+struct fpga_manager_ops {
+	/* Returns an enum value of the FPGA's state */
+	int (*state)(struct fpga_manager *mgr);

here return should be enum not int.

Yes

Quoted 8 lines
Quoted 8 lines
drivers/staging/fpga/fpga-mgr.c new file +8 -0
+
+	/* Put FPGA into reset state */
+	int (*reset)(struct fpga_manager *mgr);
+
+	/* Prepare the FPGA to receive confuration data */
+	int (*write_init)(struct fpga_manager *mgr);
+
+	/* Write count bytes of configuration data to the FPGA */
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 50

Quoted 5 lines
drivers/staging/fpga/fpga-mgr.c new file +5 -0
+	int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
+
+	/* Return FPGA to default state after writing is done */
+	int (*write_complete)(struct fpga_manager *mgr);
+

Here are missing read options which were in any previous series. But they can be added in the following patch.

Yes, they can be added later.

Quoted 23 lines
Quoted 23 lines
drivers/staging/fpga/fpga-mgr.c new file +22 -0
+	/* Optional: Set FPGA into a specific state during driver remove */
+	void (*fpga_remove)(struct fpga_manager *mgr);
+
+	int (*suspend)(struct fpga_manager *mgr);
+
+	int (*resume)(struct fpga_manager *mgr);
+};
+
+/* Valid states may vary by manufacturer; superset is: */
+enum fpga_mgr_states {
+	FPGA_MGR_STATE_UNKNOWN,		/* can't determine state */
+	FPGA_MGR_STATE_POWER_OFF,	/* FPGA power is off */
+	FPGA_MGR_STATE_POWER_UP,	/* FPGA reports power is up */
+	FPGA_MGR_STATE_RESET,		/* FPGA held in reset state */
+
+	/* write sequence */
+	FPGA_MGR_STATE_FIRMWARE_REQ,	/* firmware request in progress */
+	FPGA_MGR_STATE_FIRMWARE_REQ_ERR, /* firmware request failed */
+	FPGA_MGR_STATE_WRITE_INIT,	/* preparing FPGA for programming */
+	FPGA_MGR_STATE_WRITE_INIT_ERR,	/* Error during write_init stage */
+	FPGA_MGR_STATE_WRITE,		/* FPGA ready to receive image data */
+	FPGA_MGR_STATE_WRITE_ERR,	/* Error while programming FPGA */
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 78

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+	FPGA_MGR_STATE_WRITE_COMPLETE,	/* Doing post programming steps */
+	FPGA_MGR_STATE_WRITE_COMPLETE_ERR, /* Error during write_complete */
+
+	FPGA_MGR_STATE_OPERATING,	/* FPGA is programmed and operating */
+};
+

kernel-doc here will be good.

OK

Quoted 10 lines
Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +9 -0
+struct fpga_manager {
+	const char *name;
+	struct device dev;
+	struct list_head list;
+	struct mutex lock;
+	enum fpga_mgr_states state;
+	char image_name[NAME_MAX];
+
+	struct fpga_manager_ops *mops;
Quoted 8 lines

drivers/staging/fpga/fpga-mgr.c line 93

Quoted 6 lines
drivers/staging/fpga/fpga-mgr.c new file +6 -0
+	void *priv;
+};
+
+#define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
+
+#if IS_ENABLED(CONFIG_FPGA)

you can simple remove this if.

Cool.

Alan

#20 lore raw

SPDX for kernel (was Re: [PATCH v4 5/6] staging: fpga manager: framework core)

Hi!

Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
@@ -0,0 +1,485 @@
+/*
+ * FPGA Manager Core
+ *
+ *  Copyright (C) 2013-2014 Altera Corporation
+ *
+ * With code from the mailing list:
+ * Copyright (C) 2013 Xilinx, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,

drivers/staging/fpga/fpga-mgr.c line 20

Quoted 10 lines
drivers/staging/fpga/fpga-mgr.c new file +10 -0
+ * 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 <http://www.gnu.org/licenses/>.
+ */

Greg, Grant: What about to use just SPDX?

Quoted 4 lines
Quoted 1 line

SPDX-License-Identifier: GPL-2.0

This is a standared GPLv2 header, we're keeping it. Our legal dept wants it.

This has nothing to do with Altera here, but... SPDX works great for u-boot and yes, I believe it would be good idea to kernel as well.

Pavel

Signature · 6 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#23 lore raw

Re: [PATCH v4 6/6] staging: fpga manager: add driver for altera socfpga manager

In reply to Michal Simek
Quoted 86 lines · Michal Simek
Quoted 11 lines · On 12/09/2014 09:14 PM, [email protected] wrote:

From: Alan Tull <[email protected]>

Add driver to fpga manager framework to allow configuration of FPGA in Altera SoC FPGA parts.

---
v2: fpga_manager struct now contains struct device
fpga_manager_register parameters now take device

v3: move to drivers/staging

here should be that v4 is completely new patch in this series as you are describing in cover letter.

Quoted 70 lines
---
 drivers/staging/fpga/Kconfig  |    6 +
 drivers/staging/fpga/Makefile |    1 +
 drivers/staging/fpga/altera.c |  789 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 796 insertions(+)
 create mode 100644 drivers/staging/fpga/altera.c
drivers/staging/fpga/Kconfig +6 -0
index e775b17..2944e3c 100644
@@ -18,4 +18,10 @@ config FPGA_MGR_SYSFS
 	help
 	  FPGA Manager SysFS interface.
 
+config FPGA_MGR_ALTERA
+	tristate "Altera"
+	depends on FPGA
+	help
+	  FPGA manager driver support for configuring Altera FPGAs.
+
 endmenu
drivers/staging/fpga/Makefile +1 -0
index c8a676f..6e0d2bf 100644
@@ -8,3 +8,4 @@ fpga-mgr-core-y += fpga-mgr.o
 obj-$(CONFIG_FPGA)			+= fpga-mgr-core.o
 
 # FPGA Manager Drivers
+obj-$(CONFIG_FPGA_MGR_ALTERA)		+= altera.o
drivers/staging/fpga/altera.c new file +33 -0
new file mode 100644
index 0000000..14a16b4
@@ -0,0 +1,789 @@
+/*
+ * FPGA Manager Driver for Altera FPGAs
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+#include <linux/cdev.h>
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/fs.h>
+#include <linux/fpga-mgr.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_irq.h>
Quoted 9 lines

drivers/staging/fpga/altera.c line 39

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+#include <linux/of_platform.h>
+#include <linux/pm.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/regulator/consumer.h>

This is pretty long list I believe you will be able to shorten it a little bit.

I've done this now in v5.

Quoted 8 lines

drivers/staging/fpga/altera.c line 44

Quoted 5 lines
drivers/staging/fpga/altera.c new file +5 -0
+
+/* Controls whether to use the Configuration with DCLK steps */
+#ifndef _ALTTERA_FPGAMGR_USE_DCLK
+#define _ALTTERA_FPGAMGR_USE_DCLK 0
+#endif

This is more or less config option which you should reflect in binding.

I just removed the unused code.

Quoted 82 lines
Quoted 82 lines
drivers/staging/fpga/altera.c new file +82 -0
+
+/* Register offsets */
+#define ALTERA_FPGAMGR_STAT_OFST				0x0
+#define ALTERA_FPGAMGR_CTL_OFST					0x4
+#define ALTERA_FPGAMGR_DCLKCNT_OFST				0x8
+#define ALTERA_FPGAMGR_DCLKSTAT_OFST				0xc
+#define ALTERA_FPGAMGR_GPIO_INTEN_OFST				0x830
+#define ALTERA_FPGAMGR_GPIO_INTMSK_OFST				0x834
+#define ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST			0x838
+#define ALTERA_FPGAMGR_GPIO_INT_POL_OFST			0x83c
+#define ALTERA_FPGAMGR_GPIO_INTSTAT_OFST			0x840
+#define ALTERA_FPGAMGR_GPIO_RAW_INTSTAT_OFST			0x844
+#define ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST			0x84c
+#define ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST			0x850
+
+/* Register bit defines */
+/* ALTERA_FPGAMGR_STAT register mode field values */
+#define ALTERA_FPGAMGR_STAT_POWER_UP				0x0 /*ramping*/
+#define ALTERA_FPGAMGR_STAT_RESET				0x1
+#define ALTERA_FPGAMGR_STAT_CFG					0x2
+#define ALTERA_FPGAMGR_STAT_INIT				0x3
+#define ALTERA_FPGAMGR_STAT_USER_MODE				0x4
+#define ALTERA_FPGAMGR_STAT_UNKNOWN				0x5
+#define ALTERA_FPGAMGR_STAT_STATE_MASK				0x7
+/* This is a flag value that doesn't really happen in this register field */
+#define ALTERA_FPGAMGR_STAT_POWER_OFF				0x0
+
+#define MSEL_PP16_FAST_NOAES_NODC				0x0
+#define MSEL_PP16_FAST_AES_NODC					0x1
+#define MSEL_PP16_FAST_AESOPT_DC				0x2
+#define MSEL_PP16_SLOW_NOAES_NODC				0x4
+#define MSEL_PP16_SLOW_AES_NODC					0x5
+#define MSEL_PP16_SLOW_AESOPT_DC				0x6
+#define MSEL_PP32_FAST_NOAES_NODC				0x8
+#define MSEL_PP32_FAST_AES_NODC					0x9
+#define MSEL_PP32_FAST_AESOPT_DC				0xa
+#define MSEL_PP32_SLOW_NOAES_NODC				0xc
+#define MSEL_PP32_SLOW_AES_NODC					0xd
+#define MSEL_PP32_SLOW_AESOPT_DC				0xe
+#define ALTERA_FPGAMGR_STAT_MSEL_MASK				0x000000f8
+#define ALTERA_FPGAMGR_STAT_MSEL_SHIFT				3
+
+/* ALTERA_FPGAMGR_CTL register */
+#define ALTERA_FPGAMGR_CTL_EN					0x00000001
+#define ALTERA_FPGAMGR_CTL_NCE					0x00000002
+#define ALTERA_FPGAMGR_CTL_NCFGPULL				0x00000004
+
+#define CDRATIO_X1						0x00000000
+#define CDRATIO_X2						0x00000040
+#define CDRATIO_X4						0x00000080
+#define CDRATIO_X8						0x000000c0
+#define ALTERA_FPGAMGR_CTL_CDRATIO_MASK				0x000000c0
+
+#define ALTERA_FPGAMGR_CTL_AXICFGEN				0x00000100
+
+#define CFGWDTH_16						0x00000000
+#define CFGWDTH_32						0x00000200
+#define ALTERA_FPGAMGR_CTL_CFGWDTH_MASK				0x00000200
+
+/* ALTERA_FPGAMGR_DCLKSTAT register */
+#define ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE			0x1
+
+/* ALTERA_FPGAMGR_GPIO_* registers share the same bit positions */
+#define ALTERA_FPGAMGR_MON_NSTATUS				0x0001
+#define ALTERA_FPGAMGR_MON_CONF_DONE				0x0002
+#define ALTERA_FPGAMGR_MON_INIT_DONE				0x0004
+#define ALTERA_FPGAMGR_MON_CRC_ERROR				0x0008
+#define ALTERA_FPGAMGR_MON_CVP_CONF_DONE			0x0010
+#define ALTERA_FPGAMGR_MON_PR_READY				0x0020
+#define ALTERA_FPGAMGR_MON_PR_ERROR				0x0040
+#define ALTERA_FPGAMGR_MON_PR_DONE				0x0080
+#define ALTERA_FPGAMGR_MON_NCONFIG_PIN				0x0100
+#define ALTERA_FPGAMGR_MON_NSTATUS_PIN				0x0200
+#define ALTERA_FPGAMGR_MON_CONF_DONE_PIN			0x0400
+#define ALTERA_FPGAMGR_MON_FPGA_POWER_ON			0x0800
+#define ALTERA_FPGAMGR_MON_STATUS_MASK				0x0fff
+
+#define ALTERA_FPGAMGR_NUM_SUPPLIES 3
+#define ALTERA_RESUME_TIMEOUT 3
+
+#if IS_ENABLED(CONFIG_REGULATOR)
+/* In power-up order. Reverse for power-down. */
Quoted 9 lines

drivers/staging/fpga/altera.c line 132

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+static const char *supply_names[ALTERA_FPGAMGR_NUM_SUPPLIES] = {
+	"FPGA-1.5V",
+	"FPGA-1.1V",
+	"FPGA-2.5V",
+};
+#endif

__maybe_unused should work here.

OK

Quoted 103 lines
Quoted 103 lines
drivers/staging/fpga/altera.c new file +103 -0
+
+struct altera_fpga_priv {
+	void __iomem *fpga_base_addr;
+	void __iomem *fpga_data_addr;
+	struct completion status_complete;
+	int irq;
+	struct regulator *fpga_supplies[ALTERA_FPGAMGR_NUM_SUPPLIES];
+};
+
+struct cfgmgr_mode {
+	/* Values to set in the CTRL register */
+	u32 ctrl;
+
+	/* flag that this table entry is a valid mode */
+	bool valid;
+};
+
+/* For ALTERA_FPGAMGR_STAT_MSEL field */
+static struct cfgmgr_mode cfgmgr_modes[] = {
+	[MSEL_PP16_FAST_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_FAST_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_FAST_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP16_SLOW_NOAES_NODC] = { CFGWDTH_16 | CDRATIO_X1, 1 },
+	[MSEL_PP16_SLOW_AES_NODC] =   { CFGWDTH_16 | CDRATIO_X2, 1 },
+	[MSEL_PP16_SLOW_AESOPT_DC] =  { CFGWDTH_16 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_FAST_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_FAST_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+	[MSEL_PP32_SLOW_NOAES_NODC] = { CFGWDTH_32 | CDRATIO_X1, 1 },
+	[MSEL_PP32_SLOW_AES_NODC] =   { CFGWDTH_32 | CDRATIO_X4, 1 },
+	[MSEL_PP32_SLOW_AESOPT_DC] =  { CFGWDTH_32 | CDRATIO_X8, 1 },
+};
+
+static u32 altera_fpga_reg_readl(struct altera_fpga_priv *priv, u32 reg_offset)
+{
+	return readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_writel(struct altera_fpga_priv *priv,
+				   u32 reg_offset, u32 value)
+{
+	writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static u32 altera_fpga_reg_raw_readl(struct altera_fpga_priv *priv,
+				     u32 reg_offset)
+{
+	return __raw_readl(priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_reg_raw_writel(struct altera_fpga_priv *priv,
+				       u32 reg_offset, u32 value)
+{
+	__raw_writel(value, priv->fpga_base_addr + reg_offset);
+}
+
+static void altera_fpga_data_writel(struct altera_fpga_priv *priv, u32 value)
+{
+	writel(value, priv->fpga_data_addr);
+}
+
+static inline void altera_fpga_reg_set_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val |= bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+
+static inline void altera_fpga_reg_clr_bitsl(struct altera_fpga_priv *priv,
+					     u32 offset, u32 bits)
+{
+	u32 val;
+
+	val = altera_fpga_reg_readl(priv, offset);
+	val &= ~bits;
+	altera_fpga_reg_writel(priv, offset, val);
+}
+
+static int altera_fpga_mon_status_get(struct altera_fpga_priv *priv)
+{
+	return altera_fpga_reg_readl(priv,
+				     ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST) &
+		ALTERA_FPGAMGR_MON_STATUS_MASK;
+}
+
+static int altera_fpga_state_get(struct altera_fpga_priv *priv)
+{
+	int status = altera_fpga_mon_status_get(priv);
+
+	if ((status & ALTERA_FPGAMGR_MON_FPGA_POWER_ON) == 0)
+		return ALTERA_FPGAMGR_STAT_POWER_OFF;
+
+	return altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST) &
+		ALTERA_FPGAMGR_STAT_STATE_MASK;
+}
+
+static void altera_fpga_clear_done_status(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST,
+			       ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE);
Quoted 9 lines

drivers/staging/fpga/altera.c line 241

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+}
+
+/*
+ * Set the DCLKCNT, wait for DCLKSTAT to report the count completed, and clear
+ * the complete status.
+ */

kernel-doc format please.

Not really necessary. But I have done it for the core fpga-mgr.c.

Quoted 19 lines
Quoted 19 lines
drivers/staging/fpga/altera.c new file +19 -0
+static int altera_fpga_dclk_set_and_wait_clear(struct altera_fpga_priv *priv,
+					       u32 count)
+{
+	int timeout = 2;
+	u32 done;
+
+	/* Clear any existing DONE status. */
+	if (altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_DCLKSTAT_OFST))
+		altera_fpga_clear_done_status(priv);
+
+	/* Issue the DCLK count. */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_DCLKCNT_OFST, count);
+
+	/* Poll DCLKSTAT to see if it completed in the timeout period. */
+	do {
+		done = altera_fpga_reg_readl(priv,
+					     ALTERA_FPGAMGR_DCLKSTAT_OFST);
+		if (done == ALTERA_FPGAMGR_DCLKSTAT_DCNTDONE_E_DONE) {
+			altera_fpga_clear_done_status(priv);
Quoted 8 lines

drivers/staging/fpga/altera.c line 266

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+			return 0;
+		}
+		if (count <= 4)
+			udelay(1);
+		else
+			msleep(20);

This looks weird to me.

I simplified it, due to other code going away.

Quoted 294 lines
Quoted 161 lines
drivers/staging/fpga/altera.c new file +160 -0
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static int altera_fpga_wait_for_state(struct altera_fpga_priv *priv, u32 state)
+{
+	int timeout = 2;
+
+	/*
+	 * HW doesn't support an interrupt for changes in state, so poll to see
+	 * if it matches the requested state within the timeout period.
+	 */
+	do {
+		if ((altera_fpga_state_get(priv) & state) != 0)
+			return 0;
+		msleep(20);
+	} while (timeout--);
+
+	return -ETIMEDOUT;
+}
+
+static void altera_fpga_enable_irqs(struct altera_fpga_priv *priv, u32 irqs)
+{
+	/* set irqs to level sensitive */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTTYPE_LEVEL_OFST, 0);
+
+	/* set interrupt polarity */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INT_POL_OFST, irqs);
+
+	/* clear irqs */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST, irqs);
+
+	/* unmask interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTMSK_OFST, 0);
+
+	/* enable interrupts */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, irqs);
+}
+
+static void altera_fpga_disable_irqs(struct altera_fpga_priv *priv)
+{
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+}
+
+static irqreturn_t altera_fpga_isr(int irq, void *dev_id)
+{
+	struct altera_fpga_priv *priv = dev_id;
+	u32 irqs, st;
+	bool conf_done, nstatus;
+
+	/* clear irqs */
+	irqs = altera_fpga_reg_raw_readl(priv,
+					 ALTERA_FPGAMGR_GPIO_INTSTAT_OFST);
+
+	altera_fpga_reg_raw_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+				   irqs);
+
+	st = altera_fpga_reg_raw_readl(priv,
+				       ALTERA_FPGAMGR_GPIO_EXT_PORTA_OFST);
+	conf_done = (st & ALTERA_FPGAMGR_MON_CONF_DONE) != 0;
+	nstatus = (st & ALTERA_FPGAMGR_MON_NSTATUS) != 0;
+
+	/* success */
+	if (conf_done && nstatus) {
+		/* disable irqs */
+		altera_fpga_reg_raw_writel(priv,
+					   ALTERA_FPGAMGR_GPIO_INTEN_OFST, 0);
+		complete(&priv->status_complete);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int altera_fpga_wait_for_config_done(struct altera_fpga_priv *priv)
+{
+	int timeout, ret = 0;
+
+	altera_fpga_disable_irqs(priv);
+	init_completion(&priv->status_complete);
+	altera_fpga_enable_irqs(priv, ALTERA_FPGAMGR_MON_CONF_DONE);
+
+	timeout = wait_for_completion_interruptible_timeout(
+						&priv->status_complete,
+						msecs_to_jiffies(10));
+	if (timeout == 0)
+		ret = -ETIMEDOUT;
+
+	altera_fpga_disable_irqs(priv);
+	return ret;
+}
+
+static int altera_fpga_cfg_mode_get(struct altera_fpga_priv *priv)
+{
+	u32 msel;
+
+	msel = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_STAT_OFST);
+	msel &= ALTERA_FPGAMGR_STAT_MSEL_MASK;
+	msel >>= ALTERA_FPGAMGR_STAT_MSEL_SHIFT;
+
+	/* Check that this MSEL setting is supported */
+	if ((msel >= sizeof(cfgmgr_modes)/sizeof(struct cfgmgr_mode)) ||
+	    !cfgmgr_modes[msel].valid)
+		return -EINVAL;
+
+	return msel;
+}
+
+static int altera_fpga_cfg_mode_set(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, mode;
+
+	/* get value from MSEL pins */
+	mode = altera_fpga_cfg_mode_get(priv);
+	if (mode < 0)
+		return mode;
+
+	/* Adjust CTRL for the CDRATIO */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CDRATIO_MASK;
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_CFGWDTH_MASK;
+	ctrl_reg |= cfgmgr_modes[mode].ctrl;
+
+	/* Set NCE to 0. */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCE;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	return 0;
+}
+
+static int altera_fpga_reset(struct altera_fpga_priv *priv)
+{
+	u32 ctrl_reg, status;
+	int ret;
+
+	/*
+	 * Step 1:
+	 *  - Set CTRL.CFGWDTH, CTRL.CDRATIO to match cfg mode
+	 *  - Set CTRL.NCE to 0
+	 */
+	ret = altera_fpga_cfg_mode_set(priv);
+	if (ret)
+		return ret;
+
+	/* Step 2: Set CTRL.EN to 1 */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	/* Step 3: Set CTRL.NCONFIGPULL to 1 to put FPGA in reset */
+	ctrl_reg = altera_fpga_reg_readl(priv, ALTERA_FPGAMGR_CTL_OFST);
+	ctrl_reg |= ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+
+	/* Step 4: Wait for STATUS.MODE to report FPGA is in reset phase */
+	status = altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_RESET);
+
+	/* Step 5: Set CONTROL.NCONFIGPULL to 0 to release FPGA from reset */
+	ctrl_reg &= ~ALTERA_FPGAMGR_CTL_NCFGPULL;
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_CTL_OFST, ctrl_reg);
+

drivers/staging/fpga/altera.c line 436

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	/* Timeout waiting for reset */
+	if (status)
+		return -ETIMEDOUT;
+
+	return 0;
+}
+
+/*
+ * Prepare the FPGA to receive the configuration data.
+ */

kernel-doc

Quoted 18 lines
drivers/staging/fpga/altera.c new file +18 -0
+static int altera_fpga_ops_configure_init(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	int ret;
+
+	/* Steps 1 - 5: Reset the FPGA */
+	ret = altera_fpga_reset(priv);
+	if (ret)
+		return ret;
+
+	/* Step 6: Wait for FPGA to enter configuration phase */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_CFG))
+		return -ETIMEDOUT;
+
+	/* Step 7: Clear nSTATUS interrupt */
+	altera_fpga_reg_writel(priv, ALTERA_FPGAMGR_GPIO_PORTA_EOI_OFST,
+			       ALTERA_FPGAMGR_MON_NSTATUS);
+

drivers/staging/fpga/altera.c line 464

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+	/* Step 8: Set CTRL.AXICFGEN to 1 to enable transfer of config data */
+	altera_fpga_reg_set_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	return 0;
+}
+
+/*
+ * Step 9: write data to the FPGA data register
+ */

step 9 here?

Quoted 89 lines
drivers/staging/fpga/altera.c new file +89 -0
+static int altera_fpga_ops_configure_write(struct fpga_manager *mgr,
+					   const char *buf, size_t count)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 *buffer_32 = (u32 *)buf;
+	size_t i = 0;
+
+	if (count <= 0)
+		return -EINVAL;
+
+	/* Write out the complete 32-bit chunks. */
+	while (count >= sizeof(u32)) {
+		altera_fpga_data_writel(priv, buffer_32[i++]);
+		count -= sizeof(u32);
+	}
+
+	/* Write out remaining non 32-bit chunks. */
+	switch (count) {
+	case 3:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x00ffffff);
+		break;
+	case 2:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x0000ffff);
+		break;
+	case 1:
+		altera_fpga_data_writel(priv, buffer_32[i++] & 0x000000ff);
+		break;
+	default:
+		/* This will never happen. */
+		break;
+	}
+
+	return 0;
+}
+
+static int altera_fpga_ops_configure_complete(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 status;
+
+	/*
+	 * Step 10:
+	 *  - Observe CONF_DONE and nSTATUS (active low)
+	 *  - if CONF_DONE = 1 and nSTATUS = 1, configuration was successful
+	 *  - if CONF_DONE = 0 and nSTATUS = 0, configuration failed
+	 */
+	status = altera_fpga_wait_for_config_done(priv);
+	if (status)
+		return status;
+
+	/* Step 11: Clear CTRL.AXICFGEN to disable transfer of config data */
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_AXICFGEN);
+
+	/*
+	 * Step 12:
+	 *  - Write 4 to DCLKCNT
+	 *  - Wait for STATUS.DCNTDONE = 1
+	 *  - Clear W1C bit in STATUS.DCNTDONE
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 4))
+		return -ETIMEDOUT;
+
+#if _ALTTERA_FPGAMGR_USE_DCLK
+	/* Step 13: Wait for STATUS.MODE to report INIT or USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_INIT |
+				       ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/*
+	 * Extra steps for Configuration with DCLK for Initialization Phase
+	 * Step 14 (using 4.2.1.2 steps), 15 (using 4.2.1.2 steps)
+	 *  - Write 0x5000 to DCLKCNT == the number of clocks needed to exit
+	 *    the Initialization Phase.
+	 *  - Poll until STATUS.DCNTDONE = 1, write 1 to clear
+	 */
+	if (altera_fpga_dclk_set_and_wait_clear(priv, 0x5000))
+		return -ETIMEDOUT;
+#endif
+
+	/* Step 13: Wait for STATUS.MODE to report USER MODE */
+	if (altera_fpga_wait_for_state(priv, ALTERA_FPGAMGR_STAT_USER_MODE))
+		return -ETIMEDOUT;
+
+	/* Step 14: Set CTRL.EN to 0 */
+	altera_fpga_reg_clr_bitsl(priv, ALTERA_FPGAMGR_CTL_OFST,
+				  ALTERA_FPGAMGR_CTL_EN);
+
+	return 0;
Quoted 9 lines

drivers/staging/fpga/altera.c line 559

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+}
+
+static int altera_fpga_ops_reset(struct fpga_manager *mgr)
+{
+	return altera_fpga_reset(mgr->priv);
+}

looks like not useful code - just use altera_fpga_reset instead of ops.

Done.

Quoted 4 lines

btw: just a generic question - isn't it better to use any better name than altera_fpga. You can have different loader in future and this is very generic.

Good feedback. Changed from altera.c to socfpga.c

Quoted 6 lines
Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+
+/* Translate state register values to FPGA framework state */
+static const int altera_state_to_framework_state[] = {
+	[ALTERA_FPGAMGR_STAT_POWER_OFF] = FPGA_MGR_STATE_POWER_OFF,
+	[ALTERA_FPGAMGR_STAT_RESET] = FPGA_MGR_STATE_RESET,
+	[ALTERA_FPGAMGR_STAT_CFG] = FPGA_MGR_STATE_WRITE_INIT,
Quoted 9 lines

drivers/staging/fpga/altera.c line 571

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+	[ALTERA_FPGAMGR_STAT_INIT] = FPGA_MGR_STATE_WRITE_INIT,
+	[ALTERA_FPGAMGR_STAT_USER_MODE] = FPGA_MGR_STATE_OPERATING,
+	[ALTERA_FPGAMGR_STAT_UNKNOWN] = FPGA_MGR_STATE_UNKNOWN,
+};
+
+static int altera_fpga_ops_state(struct fpga_manager *mgr)

here should return enum - look at 5/6 comment.

Yes

Quoted 6 lines

drivers/staging/fpga/altera.c line 574

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;

this is also int not unsigned.

Yes

Quoted 23 lines

drivers/staging/fpga/altera.c line 587

Quoted 13 lines
drivers/staging/fpga/altera.c new file +13 -0
+	int ret;
+
+	state = altera_fpga_state_get(priv);
+
+	if (state < ARRAY_SIZE(altera_state_to_framework_state))
+		ret = altera_state_to_framework_state[state];
+	else
+		ret = FPGA_MGR_STATE_UNKNOWN;
+
+	return ret;
+}
+
+#if IS_ENABLED(CONFIG_REGULATOR)

Instead of this look below

drivers/staging/fpga/altera.c line 591

Quoted 4 lines
drivers/staging/fpga/altera.c new file +4 -0
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	int i, ret;
+

use this

Quoted 7 lines

if (!IS_ENABLED(CONFIG_REGULATOR))

	return 0;

Then you can simple compile code just once.

The same change for all these functions.

Thanks. I only had to do it in one of the functions. If CONFIG_REGULATOR is not enabled, regulator_enable() will return 0.

Quoted 79 lines
Quoted 79 lines
drivers/staging/fpga/altera.c new file +79 -0
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		ret = regulator_enable(priv->fpga_supplies[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+	int i;
+
+	for (i = ALTERA_FPGAMGR_NUM_SUPPLIES - 1; i >= 0; i--)
+		regulator_disable(priv->fpga_supplies[i]);
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	struct regulator *supply;
+	unsigned int i;
+
+	for (i = 0; i < ALTERA_FPGAMGR_NUM_SUPPLIES; i++) {
+		supply = devm_regulator_get_optional(&pdev->dev,
+						     supply_names[i]);
+		if (IS_ERR(supply)) {
+			dev_err(&pdev->dev, "could not get regulators");
+			return -EPROBE_DEFER;
+		}
+		priv->fpga_supplies[i] = supply;
+	}
+
+	return altera_fpga_regulators_on(priv);
+}
+#else
+static int altera_fpga_regulators_on(struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+
+static void altera_fpga_regulators_power_off(struct altera_fpga_priv *priv)
+{
+}
+
+static int altera_fpga_regulator_probe(struct platform_device *pdev,
+				       struct altera_fpga_priv *priv)
+{
+	return 0;
+}
+#endif
+
+static int altera_fpga_suspend(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	altera_fpga_regulators_power_off(priv);
+
+	return 0;
+}
+
+static int altera_fpga_resume(struct fpga_manager *mgr)
+{
+	struct altera_fpga_priv *priv = mgr->priv;
+	u32 state;
+	unsigned int timeout;
+	int ret;
+
+	ret = altera_fpga_regulators_on(priv);
+	if (ret)
+		return ret;
+
+	for (timeout = 0; timeout < ALTERA_RESUME_TIMEOUT; timeout++) {
+		state = altera_fpga_state_get(priv);
+		if (state != ALTERA_FPGAMGR_STAT_POWER_OFF)
+			break;
+		msleep(20);
+	}
+	if (state == ALTERA_FPGAMGR_STAT_POWER_OFF)
Quoted 9 lines

drivers/staging/fpga/altera.c line 676

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+		return -ETIMEDOUT;
+
+	return ret;
+}
+
+struct fpga_manager_ops altera_altera_fpga_ops = {

static here.

Yes

Quoted 49 lines
Quoted 26 lines
drivers/staging/fpga/altera.c new file +26 -0
+	.reset = altera_fpga_ops_reset,
+	.state = altera_fpga_ops_state,
+	.write_init = altera_fpga_ops_configure_init,
+	.write = altera_fpga_ops_configure_write,
+	.write_complete = altera_fpga_ops_configure_complete,
+	.suspend = altera_fpga_suspend,
+	.resume = altera_fpga_resume,
+};
+
+static int altera_fpga_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = pdev->dev.of_node;
+	struct altera_fpga_priv *priv;
+	int ret;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	ret = altera_fpga_regulator_probe(pdev, priv);
+	if (ret)
+		return ret;
+
+	priv->fpga_base_addr = of_iomap(np, 0);
+	if (!priv->fpga_base_addr)

drivers/staging/fpga/altera.c line 712

Quoted 10 lines
drivers/staging/fpga/altera.c new file +10 -0
+		return -EINVAL;
+
+	priv->fpga_data_addr = of_iomap(np, 1);
+	if (!priv->fpga_data_addr) {
+		ret = -EINVAL;
+		goto err_unmap_base_addr;
+	}
+
+	priv->irq = irq_of_parse_and_map(np, 0);
+	if (priv->irq == NO_IRQ) {

NO_IRQ is not defined for all archs that's why you will get compilation error.

<= 0 should be fine here.

drivers/staging/fpga/altera.c line 715

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+		ret = -EINVAL;
+		goto err_unmap_data_addr;
+	}

Anyway for all of these you should be able to use
platform_get_resource
platform_get_irq functions

Quoted 2 lines

then you have simplified error path here.

OK

Quoted 48 lines
Quoted 48 lines
drivers/staging/fpga/altera.c new file +47 -0
+
+	ret = request_irq(priv->irq, altera_fpga_isr, 0, "altera-fpga-mgr",
+			  priv);
+	if (ret < 0)
+		goto err_dispose_irq;
+
+	ret = fpga_mgr_register(dev, "Altera FPGA Manager",
+				&altera_altera_fpga_ops, priv);
+	if (ret)
+		goto err_free_irq;
+
+	return 0;
+
+err_free_irq:
+	free_irq(priv->irq, priv);
+err_dispose_irq:
+	irq_dispose_mapping(priv->irq);
+err_unmap_data_addr:
+	iounmap(priv->fpga_data_addr);
+err_unmap_base_addr:
+	iounmap(priv->fpga_base_addr);
+	return ret;
+}
+
+static int altera_fpga_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+	struct altera_fpga_priv *priv = mgr->priv;
+
+	fpga_mgr_remove(dev);
+	free_irq(priv->irq, priv);
+	irq_dispose_mapping(priv->irq);
+	iounmap(priv->fpga_data_addr);
+	iounmap(priv->fpga_base_addr);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id altera_fpga_of_match[] = {
+	{ .compatible = "altr,fpga-mgr", },
+	{},
+};
+
+MODULE_DEVICE_TABLE(of, altera_fpga_of_match);
+#endif
Quoted 9 lines

drivers/staging/fpga/altera.c line 768

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+
+static struct platform_driver altera_fpga_driver = {
+	.remove = altera_fpga_remove,
+	.driver = {
+		.name	= "altera_fpga_manager",
+		.owner	= THIS_MODULE,

This line should go away

Yes

Quoted 6 lines

drivers/staging/fpga/altera.c line 771

Quoted 3 lines
drivers/staging/fpga/altera.c new file +3 -0
+		.of_match_table = of_match_ptr(altera_fpga_of_match),
+	},
+	.probe = altera_fpga_probe,

I tend to have probe and remove close to each other.

OK

Quoted 8 lines
Quoted 8 lines
drivers/staging/fpga/altera.c new file +8 -0
+};
+
+static int __init altera_fpga_init(void)
+{
+	return platform_driver_register(&altera_fpga_driver);
+}
+
+static void __exit altera_fpga_exit(void)
Quoted 9 lines

drivers/staging/fpga/altera.c line 785

Quoted 6 lines
drivers/staging/fpga/altera.c new file +6 -0
+{
+	platform_driver_unregister(&altera_fpga_driver);
+}
+
+module_init(altera_fpga_init);
+module_exit(altera_fpga_exit);

module_platform_driver here

Yes

Quoted 9 lines

drivers/staging/fpga/altera.c line 789

Quoted 5 lines
drivers/staging/fpga/altera.c new file +4 -0
+
+MODULE_AUTHOR("Alan Tull <[email protected]>");
+MODULE_DESCRIPTION("Altera FPGA Manager");
+MODULE_LICENSE("GPL v2");

Thanks,
Michal

Thanks for the review.

Alan