···11+FPGA Manager Core22+33+Alan Tull 201544+55+Overview66+========77+88+The FPGA manager core exports a set of functions for programming an FPGA with99+an image. The API is manufacturer agnostic. All manufacturer specifics are1010+hidden away in a low level driver which registers a set of ops with the core.1111+The FPGA image data itself is very manufacturer specific, but for our purposes1212+it's just binary data. The FPGA manager core won't parse it.1313+1414+1515+API Functions:1616+==============1717+1818+To program the FPGA from a file or from a buffer:1919+-------------------------------------------------2020+2121+ int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags,2222+ const char *buf, size_t count);2323+2424+Load the FPGA from an image which exists as a buffer in memory.2525+2626+ int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,2727+ const char *image_name);2828+2929+Load the FPGA from an image which exists as a file. The image file must be on3030+the firmware search path (see the firmware class documentation).3131+3232+For both these functions, flags == 0 for normal full reconfiguration or3333+FPGA_MGR_PARTIAL_RECONFIG for partial reconfiguration. If successful, the FPGA3434+ends up in operating mode. Return 0 on success or a negative error code.3535+3636+3737+To get/put a reference to a FPGA manager:3838+-----------------------------------------3939+4040+ struct fpga_manager *of_fpga_mgr_get(struct device_node *node);4141+4242+ void fpga_mgr_put(struct fpga_manager *mgr);4343+4444+Given a DT node, get an exclusive reference to a FPGA manager or release4545+the reference.4646+4747+4848+To register or unregister the low level FPGA-specific driver:4949+-------------------------------------------------------------5050+5151+ int fpga_mgr_register(struct device *dev, const char *name,5252+ const struct fpga_manager_ops *mops,5353+ void *priv);5454+5555+ void fpga_mgr_unregister(struct device *dev);5656+5757+Use of these two functions is described below in "How To Support a new FPGA5858+device."5959+6060+6161+How to write an image buffer to a supported FPGA6262+================================================6363+/* Include to get the API */6464+#include <linux/fpga/fpga-mgr.h>6565+6666+/* device node that specifies the FPGA manager to use */6767+struct device_node *mgr_node = ...6868+6969+/* FPGA image is in this buffer. count is size of the buffer. */7070+char *buf = ...7171+int count = ...7272+7373+/* flags indicates whether to do full or partial reconfiguration */7474+int flags = 0;7575+7676+int ret;7777+7878+/* Get exclusive control of FPGA manager */7979+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);8080+8181+/* Load the buffer to the FPGA */8282+ret = fpga_mgr_buf_load(mgr, flags, buf, count);8383+8484+/* Release the FPGA manager */8585+fpga_mgr_put(mgr);8686+8787+8888+How to write an image file to a supported FPGA8989+==============================================9090+/* Include to get the API */9191+#include <linux/fpga/fpga-mgr.h>9292+9393+/* device node that specifies the FPGA manager to use */9494+struct device_node *mgr_node = ...9595+9696+/* FPGA image is in this file which is in the firmware search path */9797+const char *path = "fpga-image-9.rbf"9898+9999+/* flags indicates whether to do full or partial reconfiguration */100100+int flags = 0;101101+102102+int ret;103103+104104+/* Get exclusive control of FPGA manager */105105+struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);106106+107107+/* Get the firmware image (path) and load it to the FPGA */108108+ret = fpga_mgr_firmware_load(mgr, flags, path);109109+110110+/* Release the FPGA manager */111111+fpga_mgr_put(mgr);112112+113113+114114+How to support a new FPGA device115115+================================116116+To add another FPGA manager, write a driver that implements a set of ops. The117117+probe function calls fpga_mgr_register(), such as:118118+119119+static const struct fpga_manager_ops socfpga_fpga_ops = {120120+ .write_init = socfpga_fpga_ops_configure_init,121121+ .write = socfpga_fpga_ops_configure_write,122122+ .write_complete = socfpga_fpga_ops_configure_complete,123123+ .state = socfpga_fpga_ops_state,124124+};125125+126126+static int socfpga_fpga_probe(struct platform_device *pdev)127127+{128128+ struct device *dev = &pdev->dev;129129+ struct socfpga_fpga_priv *priv;130130+ int ret;131131+132132+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);133133+ if (!priv)134134+ return -ENOMEM;135135+136136+ /* ... do ioremaps, get interrupts, etc. and save137137+ them in priv... */138138+139139+ return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",140140+ &socfpga_fpga_ops, priv);141141+}142142+143143+static int socfpga_fpga_remove(struct platform_device *pdev)144144+{145145+ fpga_mgr_unregister(&pdev->dev);146146+147147+ return 0;148148+}149149+150150+151151+The ops will implement whatever device specific register writes are needed to152152+do the programming sequence for this particular FPGA. These ops return 0 for153153+success or negative error codes otherwise.154154+155155+The programming sequence is:156156+ 1. .write_init157157+ 2. .write (may be called once or multiple times)158158+ 3. .write_complete159159+160160+The .write_init function will prepare the FPGA to receive the image data.161161+162162+The .write function writes a buffer to the FPGA. The buffer may be contain the163163+whole FPGA image or may be a smaller chunk of an FPGA image. In the latter164164+case, this function is called multiple times for successive chunks.165165+166166+The .write_complete function is called after all the image has been written167167+to put the FPGA into operating mode.168168+169169+The ops include a .state function which will read the hardware FPGA manager and170170+return a code of type enum fpga_mgr_states. It doesn't result in a change in171171+hardware state.