1FPGA Manager
2============
3
4Overview
5--------
6
7The FPGA manager core exports a set of functions for programming an FPGA with
8an image.  The API is manufacturer agnostic.  All manufacturer specifics are
9hidden away in a low level driver which registers a set of ops with the core.
10The FPGA image data itself is very manufacturer specific, but for our purposes
11it's just binary data.  The FPGA manager core won't parse it.
12
13The FPGA image to be programmed can be in a scatter gather list, a single
14contiguous buffer, or a firmware file.  Because allocating contiguous kernel
15memory for the buffer should be avoided, users are encouraged to use a scatter
16gather list instead if possible.
17
18The particulars for programming the image are presented in a structure (struct
19fpga_image_info).  This struct contains parameters such as pointers to the
20FPGA image as well as image-specific particulars such as whether the image was
21built for full or partial reconfiguration.
22
23How to support a new FPGA device
24--------------------------------
25
26To add another FPGA manager, write a driver that implements a set of ops.  The
27probe function calls fpga_mgr_register() or fpga_mgr_register_full(), such as::
28
29	static const struct fpga_manager_ops socfpga_fpga_ops = {
30		.write_init = socfpga_fpga_ops_configure_init,
31		.write = socfpga_fpga_ops_configure_write,
32		.write_complete = socfpga_fpga_ops_configure_complete,
33		.state = socfpga_fpga_ops_state,
34	};
35
36	static int socfpga_fpga_probe(struct platform_device *pdev)
37	{
38		struct device *dev = &pdev->dev;
39		struct socfpga_fpga_priv *priv;
40		struct fpga_manager *mgr;
41		int ret;
42
43		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
44		if (!priv)
45			return -ENOMEM;
46
47		/*
48		 * do ioremaps, get interrupts, etc. and save
49		 * them in priv
50		 */
51
52		mgr = fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
53					&socfpga_fpga_ops, priv);
54		if (IS_ERR(mgr))
55			return PTR_ERR(mgr);
56
57		platform_set_drvdata(pdev, mgr);
58
59		return 0;
60	}
61
62	static int socfpga_fpga_remove(struct platform_device *pdev)
63	{
64		struct fpga_manager *mgr = platform_get_drvdata(pdev);
65
66		fpga_mgr_unregister(mgr);
67
68		return 0;
69	}
70
71Alternatively, the probe function could call one of the resource managed
72register functions, devm_fpga_mgr_register() or devm_fpga_mgr_register_full().
73When these functions are used, the parameter syntax is the same, but the call
74to fpga_mgr_unregister() should be removed. In the above example, the
75socfpga_fpga_remove() function would not be required.
76
77The ops will implement whatever device specific register writes are needed to
78do the programming sequence for this particular FPGA.  These ops return 0 for
79success or negative error codes otherwise.
80
81The programming sequence is::
82 1. .write_init
83 2. .write or .write_sg (may be called once or multiple times)
84 3. .write_complete
85
86The .write_init function will prepare the FPGA to receive the image data.  The
87buffer passed into .write_init will be at most .initial_header_size bytes long;
88if the whole bitstream is not immediately available then the core code will
89buffer up at least this much before starting.
90
91The .write function writes a buffer to the FPGA. The buffer may be contain the
92whole FPGA image or may be a smaller chunk of an FPGA image.  In the latter
93case, this function is called multiple times for successive chunks. This interface
94is suitable for drivers which use PIO.
95
96The .write_sg version behaves the same as .write except the input is a sg_table
97scatter list. This interface is suitable for drivers which use DMA.
98
99The .write_complete function is called after all the image has been written
100to put the FPGA into operating mode.
101
102The ops include a .state function which will determine the state the FPGA is in
103and return a code of type enum fpga_mgr_states.  It doesn't result in a change
104in state.
105
106API for implementing a new FPGA Manager driver
107----------------------------------------------
108
109* ``fpga_mgr_states`` -  Values for :c:expr:`fpga_manager->state`.
110* struct fpga_manager -  the FPGA manager struct
111* struct fpga_manager_ops -  Low level FPGA manager driver ops
112* struct fpga_manager_info -  Parameter structure for fpga_mgr_register_full()
113* fpga_mgr_register_full() -  Create and register an FPGA manager using the
114  fpga_mgr_info structure to provide the full flexibility of options
115* fpga_mgr_register() -  Create and register an FPGA manager using standard
116  arguments
117* devm_fpga_mgr_register_full() -  Resource managed version of
118  fpga_mgr_register_full()
119* devm_fpga_mgr_register() -  Resource managed version of fpga_mgr_register()
120* fpga_mgr_unregister() -  Unregister an FPGA manager
121
122.. kernel-doc:: include/linux/fpga/fpga-mgr.h
123   :functions: fpga_mgr_states
124
125.. kernel-doc:: include/linux/fpga/fpga-mgr.h
126   :functions: fpga_manager
127
128.. kernel-doc:: include/linux/fpga/fpga-mgr.h
129   :functions: fpga_manager_ops
130
131.. kernel-doc:: include/linux/fpga/fpga-mgr.h
132   :functions: fpga_manager_info
133
134.. kernel-doc:: drivers/fpga/fpga-mgr.c
135   :functions: fpga_mgr_register_full
136
137.. kernel-doc:: drivers/fpga/fpga-mgr.c
138   :functions: fpga_mgr_register
139
140.. kernel-doc:: drivers/fpga/fpga-mgr.c
141   :functions: devm_fpga_mgr_register_full
142
143.. kernel-doc:: drivers/fpga/fpga-mgr.c
144   :functions: devm_fpga_mgr_register
145
146.. kernel-doc:: drivers/fpga/fpga-mgr.c
147   :functions: fpga_mgr_unregister
148