2015-10-07 23:36:26 +08:00
|
|
|
FPGA Manager Core
|
|
|
|
|
|
|
|
Alan Tull 2015
|
|
|
|
|
|
|
|
Overview
|
|
|
|
========
|
|
|
|
|
|
|
|
The FPGA manager core exports a set of functions for programming an FPGA with
|
|
|
|
an image. The API is manufacturer agnostic. All manufacturer specifics are
|
|
|
|
hidden away in a low level driver which registers a set of ops with the core.
|
|
|
|
The FPGA image data itself is very manufacturer specific, but for our purposes
|
|
|
|
it's just binary data. The FPGA manager core won't parse it.
|
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
The FPGA image to be programmed can be in a scatter gather list, a single
|
|
|
|
contiguous buffer, or a firmware file. Because allocating contiguous kernel
|
|
|
|
memory for the buffer should be avoided, users are encouraged to use a scatter
|
|
|
|
gather list instead if possible.
|
|
|
|
|
|
|
|
The particulars for programming the image are presented in a structure (struct
|
|
|
|
fpga_image_info). This struct contains parameters such as pointers to the
|
|
|
|
FPGA image as well as image-specific particulars such as whether the image was
|
|
|
|
built for full or partial reconfiguration.
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
API Functions:
|
|
|
|
==============
|
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
To program the FPGA:
|
|
|
|
--------------------
|
2017-02-02 03:48:44 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
int fpga_mgr_load(struct fpga_manager *mgr,
|
|
|
|
struct fpga_image_info *info);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
Load the FPGA from an image which is indicated in the info. If successful,
|
2016-11-02 03:14:24 +08:00
|
|
|
the FPGA ends up in operating mode. Return 0 on success or a negative error
|
|
|
|
code.
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
To allocate or free a struct fpga_image_info:
|
|
|
|
---------------------------------------------
|
|
|
|
|
|
|
|
struct fpga_image_info *fpga_image_info_alloc(struct device *dev);
|
|
|
|
|
|
|
|
void fpga_image_info_free(struct fpga_image_info *info);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
To get/put a reference to a FPGA manager:
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
|
2016-11-02 03:14:23 +08:00
|
|
|
struct fpga_manager *fpga_mgr_get(struct device *dev);
|
2015-10-07 23:36:26 +08:00
|
|
|
void fpga_mgr_put(struct fpga_manager *mgr);
|
|
|
|
|
2017-11-16 04:20:13 +08:00
|
|
|
Given a DT node or device, get a reference to a FPGA manager. This pointer
|
|
|
|
can be saved until you are ready to program the FPGA. fpga_mgr_put releases
|
|
|
|
the reference.
|
|
|
|
|
|
|
|
|
|
|
|
To get exclusive control of a FPGA manager:
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
int fpga_mgr_lock(struct fpga_manager *mgr);
|
|
|
|
void fpga_mgr_unlock(struct fpga_manager *mgr);
|
|
|
|
|
|
|
|
The user should call fpga_mgr_lock and verify that it returns 0 before
|
|
|
|
attempting to program the FPGA. Likewise, the user should call
|
|
|
|
fpga_mgr_unlock when done programming the FPGA.
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
To register or unregister the low level FPGA-specific driver:
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
|
|
int fpga_mgr_register(struct device *dev, const char *name,
|
2017-11-16 04:20:12 +08:00
|
|
|
const struct fpga_manager_ops *mops,
|
|
|
|
void *priv);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
void fpga_mgr_unregister(struct device *dev);
|
|
|
|
|
|
|
|
Use of these two functions is described below in "How To Support a new FPGA
|
|
|
|
device."
|
|
|
|
|
|
|
|
|
|
|
|
How to write an image buffer to a supported FPGA
|
|
|
|
================================================
|
|
|
|
#include <linux/fpga/fpga-mgr.h>
|
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
struct fpga_manager *mgr;
|
|
|
|
struct fpga_image_info *info;
|
|
|
|
int ret;
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:13 +08:00
|
|
|
/*
|
|
|
|
* Get a reference to FPGA manager. The manager is not locked, so you can
|
|
|
|
* hold onto this reference without it preventing programming.
|
|
|
|
*
|
|
|
|
* This example uses the device node of the manager. Alternatively, use
|
|
|
|
* fpga_mgr_get(dev) instead if you have the device.
|
|
|
|
*/
|
|
|
|
mgr = of_fpga_mgr_get(mgr_node);
|
|
|
|
|
2016-11-02 03:14:24 +08:00
|
|
|
/* struct with information about the FPGA image to program. */
|
2017-11-16 04:20:12 +08:00
|
|
|
info = fpga_image_info_alloc(dev);
|
2016-11-02 03:14:24 +08:00
|
|
|
|
2015-10-07 23:36:26 +08:00
|
|
|
/* flags indicates whether to do full or partial reconfiguration */
|
2017-11-16 04:20:12 +08:00
|
|
|
info->flags = FPGA_MGR_PARTIAL_RECONFIG;
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
/*
|
|
|
|
* At this point, indicate where the image is. This is pseudo-code; you're
|
|
|
|
* going to use one of these three.
|
|
|
|
*/
|
|
|
|
if (image is in a scatter gather table) {
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
info->sgt = [your scatter gather table]
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
} else if (image is in a buffer) {
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
info->buf = [your image buffer]
|
|
|
|
info->count = [image buffer size]
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
} else if (image is in a firmware file) {
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
info->firmware_name = devm_kstrdup(dev, firmware_name, GFP_KERNEL);
|
2016-11-02 03:14:24 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
}
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:13 +08:00
|
|
|
/* Get exclusive control of FPGA manager */
|
|
|
|
ret = fpga_mgr_lock(mgr);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
/* Load the buffer to the FPGA */
|
|
|
|
ret = fpga_mgr_buf_load(mgr, &info, buf, count);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
/* Release the FPGA manager */
|
2017-11-16 04:20:13 +08:00
|
|
|
fpga_mgr_unlock(mgr);
|
2015-10-07 23:36:26 +08:00
|
|
|
fpga_mgr_put(mgr);
|
|
|
|
|
2017-11-16 04:20:12 +08:00
|
|
|
/* Deallocate the image info if you're done with it */
|
|
|
|
fpga_image_info_free(info);
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
How to support a new FPGA device
|
|
|
|
================================
|
|
|
|
To add another FPGA manager, write a driver that implements a set of ops. The
|
|
|
|
probe function calls fpga_mgr_register(), such as:
|
|
|
|
|
|
|
|
static const struct fpga_manager_ops socfpga_fpga_ops = {
|
|
|
|
.write_init = socfpga_fpga_ops_configure_init,
|
|
|
|
.write = socfpga_fpga_ops_configure_write,
|
|
|
|
.write_complete = socfpga_fpga_ops_configure_complete,
|
|
|
|
.state = socfpga_fpga_ops_state,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int socfpga_fpga_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct socfpga_fpga_priv *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* ... do ioremaps, get interrupts, etc. and save
|
|
|
|
them in priv... */
|
|
|
|
|
|
|
|
return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
|
|
|
|
&socfpga_fpga_ops, priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int socfpga_fpga_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
fpga_mgr_unregister(&pdev->dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
The ops will implement whatever device specific register writes are needed to
|
|
|
|
do the programming sequence for this particular FPGA. These ops return 0 for
|
|
|
|
success or negative error codes otherwise.
|
|
|
|
|
|
|
|
The programming sequence is:
|
|
|
|
1. .write_init
|
2017-02-02 03:48:44 +08:00
|
|
|
2. .write or .write_sg (may be called once or multiple times)
|
2015-10-07 23:36:26 +08:00
|
|
|
3. .write_complete
|
|
|
|
|
2016-11-23 02:22:09 +08:00
|
|
|
The .write_init function will prepare the FPGA to receive the image data. The
|
|
|
|
buffer passed into .write_init will be atmost .initial_header_size bytes long,
|
|
|
|
if the whole bitstream is not immediately available then the core code will
|
|
|
|
buffer up at least this much before starting.
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
The .write function writes a buffer to the FPGA. The buffer may be contain the
|
|
|
|
whole FPGA image or may be a smaller chunk of an FPGA image. In the latter
|
2017-02-02 03:48:44 +08:00
|
|
|
case, this function is called multiple times for successive chunks. This interface
|
|
|
|
is suitable for drivers which use PIO.
|
|
|
|
|
|
|
|
The .write_sg version behaves the same as .write except the input is a sg_table
|
|
|
|
scatter list. This interface is suitable for drivers which use DMA.
|
2015-10-07 23:36:26 +08:00
|
|
|
|
|
|
|
The .write_complete function is called after all the image has been written
|
|
|
|
to put the FPGA into operating mode.
|
|
|
|
|
|
|
|
The ops include a .state function which will read the hardware FPGA manager and
|
|
|
|
return a code of type enum fpga_mgr_states. It doesn't result in a change in
|
|
|
|
hardware state.
|