xref: /OK3568_Linux_fs/kernel/include/linux/fpga/fpga-mgr.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * FPGA Framework
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2013-2016 Altera Corporation
6*4882a593Smuzhiyun  *  Copyright (C) 2017 Intel Corporation
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #ifndef _LINUX_FPGA_MGR_H
9*4882a593Smuzhiyun #define _LINUX_FPGA_MGR_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/platform_device.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun struct fpga_manager;
15*4882a593Smuzhiyun struct sg_table;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun  * enum fpga_mgr_states - fpga framework states
19*4882a593Smuzhiyun  * @FPGA_MGR_STATE_UNKNOWN: can't determine state
20*4882a593Smuzhiyun  * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
21*4882a593Smuzhiyun  * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
22*4882a593Smuzhiyun  * @FPGA_MGR_STATE_RESET: FPGA in reset state
23*4882a593Smuzhiyun  * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
24*4882a593Smuzhiyun  * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
25*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
26*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
27*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE: writing image to FPGA
28*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
29*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
30*4882a593Smuzhiyun  * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
31*4882a593Smuzhiyun  * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun enum fpga_mgr_states {
34*4882a593Smuzhiyun 	/* default FPGA states */
35*4882a593Smuzhiyun 	FPGA_MGR_STATE_UNKNOWN,
36*4882a593Smuzhiyun 	FPGA_MGR_STATE_POWER_OFF,
37*4882a593Smuzhiyun 	FPGA_MGR_STATE_POWER_UP,
38*4882a593Smuzhiyun 	FPGA_MGR_STATE_RESET,
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/* getting an image for loading */
41*4882a593Smuzhiyun 	FPGA_MGR_STATE_FIRMWARE_REQ,
42*4882a593Smuzhiyun 	FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* write sequence: init, write, complete */
45*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE_INIT,
46*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE_INIT_ERR,
47*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE,
48*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE_ERR,
49*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE_COMPLETE,
50*4882a593Smuzhiyun 	FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	/* fpga is programmed and operating */
53*4882a593Smuzhiyun 	FPGA_MGR_STATE_OPERATING,
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun  * DOC: FPGA Manager flags
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Flags used in the &fpga_image_info->flags field
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * %FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * %FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * %FPGA_MGR_ENCRYPTED_BITSTREAM: indicates bitstream is encrypted
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * %FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * %FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
70*4882a593Smuzhiyun  */
71*4882a593Smuzhiyun #define FPGA_MGR_PARTIAL_RECONFIG	BIT(0)
72*4882a593Smuzhiyun #define FPGA_MGR_EXTERNAL_CONFIG	BIT(1)
73*4882a593Smuzhiyun #define FPGA_MGR_ENCRYPTED_BITSTREAM	BIT(2)
74*4882a593Smuzhiyun #define FPGA_MGR_BITSTREAM_LSB_FIRST	BIT(3)
75*4882a593Smuzhiyun #define FPGA_MGR_COMPRESSED_BITSTREAM	BIT(4)
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun  * struct fpga_image_info - information specific to a FPGA image
79*4882a593Smuzhiyun  * @flags: boolean flags as defined above
80*4882a593Smuzhiyun  * @enable_timeout_us: maximum time to enable traffic through bridge (uSec)
81*4882a593Smuzhiyun  * @disable_timeout_us: maximum time to disable traffic through bridge (uSec)
82*4882a593Smuzhiyun  * @config_complete_timeout_us: maximum time for FPGA to switch to operating
83*4882a593Smuzhiyun  *	   status in the write_complete op.
84*4882a593Smuzhiyun  * @firmware_name: name of FPGA image firmware file
85*4882a593Smuzhiyun  * @sgt: scatter/gather table containing FPGA image
86*4882a593Smuzhiyun  * @buf: contiguous buffer containing FPGA image
87*4882a593Smuzhiyun  * @count: size of buf
88*4882a593Smuzhiyun  * @region_id: id of target region
89*4882a593Smuzhiyun  * @dev: device that owns this
90*4882a593Smuzhiyun  * @overlay: Device Tree overlay
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun struct fpga_image_info {
93*4882a593Smuzhiyun 	u32 flags;
94*4882a593Smuzhiyun 	u32 enable_timeout_us;
95*4882a593Smuzhiyun 	u32 disable_timeout_us;
96*4882a593Smuzhiyun 	u32 config_complete_timeout_us;
97*4882a593Smuzhiyun 	char *firmware_name;
98*4882a593Smuzhiyun 	struct sg_table *sgt;
99*4882a593Smuzhiyun 	const char *buf;
100*4882a593Smuzhiyun 	size_t count;
101*4882a593Smuzhiyun 	int region_id;
102*4882a593Smuzhiyun 	struct device *dev;
103*4882a593Smuzhiyun #ifdef CONFIG_OF
104*4882a593Smuzhiyun 	struct device_node *overlay;
105*4882a593Smuzhiyun #endif
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun  * struct fpga_manager_ops - ops for low level fpga manager drivers
110*4882a593Smuzhiyun  * @initial_header_size: Maximum number of bytes that should be passed into write_init
111*4882a593Smuzhiyun  * @state: returns an enum value of the FPGA's state
112*4882a593Smuzhiyun  * @status: returns status of the FPGA, including reconfiguration error code
113*4882a593Smuzhiyun  * @write_init: prepare the FPGA to receive confuration data
114*4882a593Smuzhiyun  * @write: write count bytes of configuration data to the FPGA
115*4882a593Smuzhiyun  * @write_sg: write the scatter list of configuration data to the FPGA
116*4882a593Smuzhiyun  * @write_complete: set FPGA to operating state after writing is done
117*4882a593Smuzhiyun  * @fpga_remove: optional: Set FPGA into a specific state during driver remove
118*4882a593Smuzhiyun  * @groups: optional attribute groups.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * fpga_manager_ops are the low level functions implemented by a specific
121*4882a593Smuzhiyun  * fpga manager driver.  The optional ones are tested for NULL before being
122*4882a593Smuzhiyun  * called, so leaving them out is fine.
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun struct fpga_manager_ops {
125*4882a593Smuzhiyun 	size_t initial_header_size;
126*4882a593Smuzhiyun 	enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
127*4882a593Smuzhiyun 	u64 (*status)(struct fpga_manager *mgr);
128*4882a593Smuzhiyun 	int (*write_init)(struct fpga_manager *mgr,
129*4882a593Smuzhiyun 			  struct fpga_image_info *info,
130*4882a593Smuzhiyun 			  const char *buf, size_t count);
131*4882a593Smuzhiyun 	int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
132*4882a593Smuzhiyun 	int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
133*4882a593Smuzhiyun 	int (*write_complete)(struct fpga_manager *mgr,
134*4882a593Smuzhiyun 			      struct fpga_image_info *info);
135*4882a593Smuzhiyun 	void (*fpga_remove)(struct fpga_manager *mgr);
136*4882a593Smuzhiyun 	const struct attribute_group **groups;
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* FPGA manager status: Partial/Full Reconfiguration errors */
140*4882a593Smuzhiyun #define FPGA_MGR_STATUS_OPERATION_ERR		BIT(0)
141*4882a593Smuzhiyun #define FPGA_MGR_STATUS_CRC_ERR			BIT(1)
142*4882a593Smuzhiyun #define FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR	BIT(2)
143*4882a593Smuzhiyun #define FPGA_MGR_STATUS_IP_PROTOCOL_ERR		BIT(3)
144*4882a593Smuzhiyun #define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR	BIT(4)
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun  * struct fpga_compat_id - id for compatibility check
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * @id_h: high 64bit of the compat_id
150*4882a593Smuzhiyun  * @id_l: low 64bit of the compat_id
151*4882a593Smuzhiyun  */
152*4882a593Smuzhiyun struct fpga_compat_id {
153*4882a593Smuzhiyun 	u64 id_h;
154*4882a593Smuzhiyun 	u64 id_l;
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun  * struct fpga_manager - fpga manager structure
159*4882a593Smuzhiyun  * @name: name of low level fpga manager
160*4882a593Smuzhiyun  * @dev: fpga manager device
161*4882a593Smuzhiyun  * @ref_mutex: only allows one reference to fpga manager
162*4882a593Smuzhiyun  * @state: state of fpga manager
163*4882a593Smuzhiyun  * @compat_id: FPGA manager id for compatibility check.
164*4882a593Smuzhiyun  * @mops: pointer to struct of fpga manager ops
165*4882a593Smuzhiyun  * @priv: low level driver private date
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun struct fpga_manager {
168*4882a593Smuzhiyun 	const char *name;
169*4882a593Smuzhiyun 	struct device dev;
170*4882a593Smuzhiyun 	struct mutex ref_mutex;
171*4882a593Smuzhiyun 	enum fpga_mgr_states state;
172*4882a593Smuzhiyun 	struct fpga_compat_id *compat_id;
173*4882a593Smuzhiyun 	const struct fpga_manager_ops *mops;
174*4882a593Smuzhiyun 	void *priv;
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun #define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun struct fpga_image_info *fpga_image_info_alloc(struct device *dev);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun void fpga_image_info_free(struct fpga_image_info *info);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun int fpga_mgr_lock(struct fpga_manager *mgr);
186*4882a593Smuzhiyun void fpga_mgr_unlock(struct fpga_manager *mgr);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun struct fpga_manager *fpga_mgr_get(struct device *dev);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun void fpga_mgr_put(struct fpga_manager *mgr);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
195*4882a593Smuzhiyun 				     const struct fpga_manager_ops *mops,
196*4882a593Smuzhiyun 				     void *priv);
197*4882a593Smuzhiyun void fpga_mgr_free(struct fpga_manager *mgr);
198*4882a593Smuzhiyun int fpga_mgr_register(struct fpga_manager *mgr);
199*4882a593Smuzhiyun void fpga_mgr_unregister(struct fpga_manager *mgr);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
202*4882a593Smuzhiyun 					  const struct fpga_manager_ops *mops,
203*4882a593Smuzhiyun 					  void *priv);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun #endif /*_LINUX_FPGA_MGR_H */
206