xref: /linux/Documentation/driver-api/fpga/fpga-mgr.rst (revision a59f95c7a2c35031264eafc62e684df07f5f2a8f)
1547b822cSAlan TullFPGA Manager
2547b822cSAlan Tull============
3547b822cSAlan Tull
4547b822cSAlan TullOverview
5547b822cSAlan Tull--------
6547b822cSAlan Tull
7547b822cSAlan TullThe FPGA manager core exports a set of functions for programming an FPGA with
8547b822cSAlan Tullan image.  The API is manufacturer agnostic.  All manufacturer specifics are
9547b822cSAlan Tullhidden away in a low level driver which registers a set of ops with the core.
10547b822cSAlan TullThe FPGA image data itself is very manufacturer specific, but for our purposes
11547b822cSAlan Tullit's just binary data.  The FPGA manager core won't parse it.
12547b822cSAlan Tull
13547b822cSAlan TullThe FPGA image to be programmed can be in a scatter gather list, a single
14547b822cSAlan Tullcontiguous buffer, or a firmware file.  Because allocating contiguous kernel
15547b822cSAlan Tullmemory for the buffer should be avoided, users are encouraged to use a scatter
16547b822cSAlan Tullgather list instead if possible.
17547b822cSAlan Tull
18547b822cSAlan TullThe particulars for programming the image are presented in a structure (struct
19547b822cSAlan Tullfpga_image_info).  This struct contains parameters such as pointers to the
20547b822cSAlan TullFPGA image as well as image-specific particulars such as whether the image was
21547b822cSAlan Tullbuilt for full or partial reconfiguration.
22547b822cSAlan Tull
23547b822cSAlan TullHow to support a new FPGA device
24547b822cSAlan Tull--------------------------------
25547b822cSAlan Tull
26547b822cSAlan TullTo add another FPGA manager, write a driver that implements a set of ops.  The
27547b822cSAlan Tullprobe function calls fpga_mgr_register(), such as::
28547b822cSAlan Tull
29547b822cSAlan Tull	static const struct fpga_manager_ops socfpga_fpga_ops = {
30547b822cSAlan Tull		.write_init = socfpga_fpga_ops_configure_init,
31547b822cSAlan Tull		.write = socfpga_fpga_ops_configure_write,
32547b822cSAlan Tull		.write_complete = socfpga_fpga_ops_configure_complete,
33547b822cSAlan Tull		.state = socfpga_fpga_ops_state,
34547b822cSAlan Tull	};
35547b822cSAlan Tull
36547b822cSAlan Tull	static int socfpga_fpga_probe(struct platform_device *pdev)
37547b822cSAlan Tull	{
38547b822cSAlan Tull		struct device *dev = &pdev->dev;
39547b822cSAlan Tull		struct socfpga_fpga_priv *priv;
40547b822cSAlan Tull		struct fpga_manager *mgr;
41547b822cSAlan Tull		int ret;
42547b822cSAlan Tull
43547b822cSAlan Tull		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
44547b822cSAlan Tull		if (!priv)
45547b822cSAlan Tull			return -ENOMEM;
46547b822cSAlan Tull
47547b822cSAlan Tull		/*
48547b822cSAlan Tull		 * do ioremaps, get interrupts, etc. and save
49547b822cSAlan Tull		 * them in priv
50547b822cSAlan Tull		 */
51547b822cSAlan Tull
52547b822cSAlan Tull		mgr = fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
53547b822cSAlan Tull				      &socfpga_fpga_ops, priv);
54547b822cSAlan Tull		if (!mgr)
55547b822cSAlan Tull			return -ENOMEM;
56547b822cSAlan Tull
57547b822cSAlan Tull		platform_set_drvdata(pdev, mgr);
58547b822cSAlan Tull
59547b822cSAlan Tull		ret = fpga_mgr_register(mgr);
60547b822cSAlan Tull		if (ret)
61547b822cSAlan Tull			fpga_mgr_free(mgr);
62547b822cSAlan Tull
63547b822cSAlan Tull		return ret;
64547b822cSAlan Tull	}
65547b822cSAlan Tull
66547b822cSAlan Tull	static int socfpga_fpga_remove(struct platform_device *pdev)
67547b822cSAlan Tull	{
68547b822cSAlan Tull		struct fpga_manager *mgr = platform_get_drvdata(pdev);
69547b822cSAlan Tull
70547b822cSAlan Tull		fpga_mgr_unregister(mgr);
71547b822cSAlan Tull
72547b822cSAlan Tull		return 0;
73547b822cSAlan Tull	}
74547b822cSAlan Tull
75547b822cSAlan Tull
76547b822cSAlan TullThe ops will implement whatever device specific register writes are needed to
77547b822cSAlan Tulldo the programming sequence for this particular FPGA.  These ops return 0 for
78547b822cSAlan Tullsuccess or negative error codes otherwise.
79547b822cSAlan Tull
80547b822cSAlan TullThe programming sequence is::
81547b822cSAlan Tull 1. .write_init
82547b822cSAlan Tull 2. .write or .write_sg (may be called once or multiple times)
83547b822cSAlan Tull 3. .write_complete
84547b822cSAlan Tull
85547b822cSAlan TullThe .write_init function will prepare the FPGA to receive the image data.  The
86*a59f95c7SAlan Tullbuffer passed into .write_init will be at most .initial_header_size bytes long;
87547b822cSAlan Tullif the whole bitstream is not immediately available then the core code will
88547b822cSAlan Tullbuffer up at least this much before starting.
89547b822cSAlan Tull
90547b822cSAlan TullThe .write function writes a buffer to the FPGA. The buffer may be contain the
91547b822cSAlan Tullwhole FPGA image or may be a smaller chunk of an FPGA image.  In the latter
92547b822cSAlan Tullcase, this function is called multiple times for successive chunks. This interface
93547b822cSAlan Tullis suitable for drivers which use PIO.
94547b822cSAlan Tull
95547b822cSAlan TullThe .write_sg version behaves the same as .write except the input is a sg_table
96547b822cSAlan Tullscatter list. This interface is suitable for drivers which use DMA.
97547b822cSAlan Tull
98547b822cSAlan TullThe .write_complete function is called after all the image has been written
99547b822cSAlan Tullto put the FPGA into operating mode.
100547b822cSAlan Tull
101*a59f95c7SAlan TullThe ops include a .state function which will determine the state the FPGA is in
102*a59f95c7SAlan Tulland return a code of type enum fpga_mgr_states.  It doesn't result in a change
103*a59f95c7SAlan Tullin state.
104547b822cSAlan Tull
105547b822cSAlan TullHow to write an image buffer to a supported FPGA
106547b822cSAlan Tull------------------------------------------------
107547b822cSAlan Tull
108547b822cSAlan TullSome sample code::
109547b822cSAlan Tull
110547b822cSAlan Tull	#include <linux/fpga/fpga-mgr.h>
111547b822cSAlan Tull
112547b822cSAlan Tull	struct fpga_manager *mgr;
113547b822cSAlan Tull	struct fpga_image_info *info;
114547b822cSAlan Tull	int ret;
115547b822cSAlan Tull
116547b822cSAlan Tull	/*
117547b822cSAlan Tull	 * Get a reference to FPGA manager.  The manager is not locked, so you can
118547b822cSAlan Tull	 * hold onto this reference without it preventing programming.
119547b822cSAlan Tull	 *
120547b822cSAlan Tull	 * This example uses the device node of the manager.  Alternatively, use
121547b822cSAlan Tull	 * fpga_mgr_get(dev) instead if you have the device.
122547b822cSAlan Tull	 */
123547b822cSAlan Tull	mgr = of_fpga_mgr_get(mgr_node);
124547b822cSAlan Tull
125547b822cSAlan Tull	/* struct with information about the FPGA image to program. */
126547b822cSAlan Tull	info = fpga_image_info_alloc(dev);
127547b822cSAlan Tull
128547b822cSAlan Tull	/* flags indicates whether to do full or partial reconfiguration */
129547b822cSAlan Tull	info->flags = FPGA_MGR_PARTIAL_RECONFIG;
130547b822cSAlan Tull
131547b822cSAlan Tull	/*
132547b822cSAlan Tull	 * At this point, indicate where the image is. This is pseudo-code; you're
133547b822cSAlan Tull	 * going to use one of these three.
134547b822cSAlan Tull	 */
135547b822cSAlan Tull	if (image is in a scatter gather table) {
136547b822cSAlan Tull
137547b822cSAlan Tull		info->sgt = [your scatter gather table]
138547b822cSAlan Tull
139547b822cSAlan Tull	} else if (image is in a buffer) {
140547b822cSAlan Tull
141547b822cSAlan Tull		info->buf = [your image buffer]
142547b822cSAlan Tull		info->count = [image buffer size]
143547b822cSAlan Tull
144547b822cSAlan Tull	} else if (image is in a firmware file) {
145547b822cSAlan Tull
146547b822cSAlan Tull		info->firmware_name = devm_kstrdup(dev, firmware_name, GFP_KERNEL);
147547b822cSAlan Tull
148547b822cSAlan Tull	}
149547b822cSAlan Tull
150547b822cSAlan Tull	/* Get exclusive control of FPGA manager */
151547b822cSAlan Tull	ret = fpga_mgr_lock(mgr);
152547b822cSAlan Tull
153547b822cSAlan Tull	/* Load the buffer to the FPGA */
154547b822cSAlan Tull	ret = fpga_mgr_buf_load(mgr, &info, buf, count);
155547b822cSAlan Tull
156547b822cSAlan Tull	/* Release the FPGA manager */
157547b822cSAlan Tull	fpga_mgr_unlock(mgr);
158547b822cSAlan Tull	fpga_mgr_put(mgr);
159547b822cSAlan Tull
160547b822cSAlan Tull	/* Deallocate the image info if you're done with it */
161547b822cSAlan Tull	fpga_image_info_free(info);
162547b822cSAlan Tull
163547b822cSAlan TullAPI for implementing a new FPGA Manager driver
164547b822cSAlan Tull----------------------------------------------
165547b822cSAlan Tull
166547b822cSAlan Tull.. kernel-doc:: include/linux/fpga/fpga-mgr.h
167547b822cSAlan Tull   :functions: fpga_manager
168547b822cSAlan Tull
169547b822cSAlan Tull.. kernel-doc:: include/linux/fpga/fpga-mgr.h
170547b822cSAlan Tull   :functions: fpga_manager_ops
171547b822cSAlan Tull
172547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
173547b822cSAlan Tull   :functions: fpga_mgr_create
174547b822cSAlan Tull
175547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
176547b822cSAlan Tull   :functions: fpga_mgr_free
177547b822cSAlan Tull
178547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
179547b822cSAlan Tull   :functions: fpga_mgr_register
180547b822cSAlan Tull
181547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
182547b822cSAlan Tull   :functions: fpga_mgr_unregister
183547b822cSAlan Tull
184*a59f95c7SAlan TullAPI for programming an FPGA
185*a59f95c7SAlan Tull---------------------------
186547b822cSAlan Tull
187547b822cSAlan Tull.. kernel-doc:: include/linux/fpga/fpga-mgr.h
188547b822cSAlan Tull   :functions: fpga_image_info
189547b822cSAlan Tull
190547b822cSAlan Tull.. kernel-doc:: include/linux/fpga/fpga-mgr.h
191547b822cSAlan Tull   :functions: fpga_mgr_states
192547b822cSAlan Tull
193547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
194547b822cSAlan Tull   :functions: fpga_image_info_alloc
195547b822cSAlan Tull
196547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
197547b822cSAlan Tull   :functions: fpga_image_info_free
198547b822cSAlan Tull
199547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
200547b822cSAlan Tull   :functions: of_fpga_mgr_get
201547b822cSAlan Tull
202547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
203547b822cSAlan Tull   :functions: fpga_mgr_get
204547b822cSAlan Tull
205547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
206547b822cSAlan Tull   :functions: fpga_mgr_put
207547b822cSAlan Tull
208547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
209547b822cSAlan Tull   :functions: fpga_mgr_lock
210547b822cSAlan Tull
211547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
212547b822cSAlan Tull   :functions: fpga_mgr_unlock
213547b822cSAlan Tull
214547b822cSAlan Tull.. kernel-doc:: include/linux/fpga/fpga-mgr.h
215547b822cSAlan Tull   :functions: fpga_mgr_states
216547b822cSAlan Tull
217547b822cSAlan TullNote - use :c:func:`fpga_region_program_fpga()` instead of :c:func:`fpga_mgr_load()`
218547b822cSAlan Tull
219547b822cSAlan Tull.. kernel-doc:: drivers/fpga/fpga-mgr.c
220547b822cSAlan Tull   :functions: fpga_mgr_load
221