blob: bfa14bc023fbc65014bc7e6010a7a10a181e0d2d [file] [log] [blame]
Alan Tull6a8c3be2015-10-07 16:36:28 +01001/*
2 * FPGA Framework
3 *
4 * Copyright (C) 2013-2015 Altera Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/mutex.h>
19#include <linux/platform_device.h>
20
21#ifndef _LINUX_FPGA_MGR_H
22#define _LINUX_FPGA_MGR_H
23
24struct fpga_manager;
Jason Gunthorpebaa6d392017-02-01 12:48:44 -070025struct sg_table;
Alan Tull6a8c3be2015-10-07 16:36:28 +010026
27/**
28 * enum fpga_mgr_states - fpga framework states
29 * @FPGA_MGR_STATE_UNKNOWN: can't determine state
30 * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
31 * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
32 * @FPGA_MGR_STATE_RESET: FPGA in reset state
33 * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
34 * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
35 * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
36 * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
37 * @FPGA_MGR_STATE_WRITE: writing image to FPGA
38 * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
39 * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
40 * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
41 * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
42 */
43enum fpga_mgr_states {
44 /* default FPGA states */
45 FPGA_MGR_STATE_UNKNOWN,
46 FPGA_MGR_STATE_POWER_OFF,
47 FPGA_MGR_STATE_POWER_UP,
48 FPGA_MGR_STATE_RESET,
49
50 /* getting an image for loading */
51 FPGA_MGR_STATE_FIRMWARE_REQ,
52 FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
53
54 /* write sequence: init, write, complete */
55 FPGA_MGR_STATE_WRITE_INIT,
56 FPGA_MGR_STATE_WRITE_INIT_ERR,
57 FPGA_MGR_STATE_WRITE,
58 FPGA_MGR_STATE_WRITE_ERR,
59 FPGA_MGR_STATE_WRITE_COMPLETE,
60 FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
61
62 /* fpga is programmed and operating */
63 FPGA_MGR_STATE_OPERATING,
64};
65
66/*
67 * FPGA Manager flags
68 * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
Alan Tull0fa20cd2016-11-01 14:14:29 -050069 * FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
Anatolij Gustschin68f6be62017-06-14 10:36:27 -050070 * FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
Anatolij Gustschinb37fa562017-06-14 10:36:34 -050071 * FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
Alan Tull6a8c3be2015-10-07 16:36:28 +010072 */
73#define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
Alan Tull0fa20cd2016-11-01 14:14:29 -050074#define FPGA_MGR_EXTERNAL_CONFIG BIT(1)
Moritz Fischer0f4f0c82017-02-27 09:19:00 -060075#define FPGA_MGR_ENCRYPTED_BITSTREAM BIT(2)
Anatolij Gustschin68f6be62017-06-14 10:36:27 -050076#define FPGA_MGR_BITSTREAM_LSB_FIRST BIT(3)
Anatolij Gustschinb37fa562017-06-14 10:36:34 -050077#define FPGA_MGR_COMPRESSED_BITSTREAM BIT(4)
Alan Tull6a8c3be2015-10-07 16:36:28 +010078
79/**
Alan Tull1df28652016-11-01 14:14:26 -050080 * struct fpga_image_info - information specific to a FPGA image
81 * @flags: boolean flags as defined above
82 * @enable_timeout_us: maximum time to enable traffic through bridge (uSec)
83 * @disable_timeout_us: maximum time to disable traffic through bridge (uSec)
Alan Tull42d5ec92017-03-23 19:34:27 -050084 * @config_complete_timeout_us: maximum time for FPGA to switch to operating
85 * status in the write_complete op.
Alan Tull1df28652016-11-01 14:14:26 -050086 */
87struct fpga_image_info {
88 u32 flags;
89 u32 enable_timeout_us;
90 u32 disable_timeout_us;
Alan Tull42d5ec92017-03-23 19:34:27 -050091 u32 config_complete_timeout_us;
Alan Tull1df28652016-11-01 14:14:26 -050092};
93
94/**
Alan Tull6a8c3be2015-10-07 16:36:28 +010095 * struct fpga_manager_ops - ops for low level fpga manager drivers
Jason Gunthorpe1d7f1582016-11-22 18:22:09 +000096 * @initial_header_size: Maximum number of bytes that should be passed into write_init
Alan Tull6a8c3be2015-10-07 16:36:28 +010097 * @state: returns an enum value of the FPGA's state
98 * @write_init: prepare the FPGA to receive confuration data
99 * @write: write count bytes of configuration data to the FPGA
Jason Gunthorpebaa6d392017-02-01 12:48:44 -0700100 * @write_sg: write the scatter list of configuration data to the FPGA
Alan Tull6a8c3be2015-10-07 16:36:28 +0100101 * @write_complete: set FPGA to operating state after writing is done
102 * @fpga_remove: optional: Set FPGA into a specific state during driver remove
103 *
104 * fpga_manager_ops are the low level functions implemented by a specific
105 * fpga manager driver. The optional ones are tested for NULL before being
106 * called, so leaving them out is fine.
107 */
108struct fpga_manager_ops {
Jason Gunthorpe1d7f1582016-11-22 18:22:09 +0000109 size_t initial_header_size;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100110 enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
Alan Tull1df28652016-11-01 14:14:26 -0500111 int (*write_init)(struct fpga_manager *mgr,
112 struct fpga_image_info *info,
Alan Tull6a8c3be2015-10-07 16:36:28 +0100113 const char *buf, size_t count);
114 int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
Jason Gunthorpebaa6d392017-02-01 12:48:44 -0700115 int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
Alan Tull1df28652016-11-01 14:14:26 -0500116 int (*write_complete)(struct fpga_manager *mgr,
117 struct fpga_image_info *info);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100118 void (*fpga_remove)(struct fpga_manager *mgr);
119};
120
121/**
122 * struct fpga_manager - fpga manager structure
123 * @name: name of low level fpga manager
124 * @dev: fpga manager device
125 * @ref_mutex: only allows one reference to fpga manager
126 * @state: state of fpga manager
127 * @mops: pointer to struct of fpga manager ops
128 * @priv: low level driver private date
129 */
130struct fpga_manager {
131 const char *name;
132 struct device dev;
133 struct mutex ref_mutex;
134 enum fpga_mgr_states state;
135 const struct fpga_manager_ops *mops;
136 void *priv;
137};
138
139#define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
140
Alan Tull1df28652016-11-01 14:14:26 -0500141int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
Alan Tull6a8c3be2015-10-07 16:36:28 +0100142 const char *buf, size_t count);
Jason Gunthorpebaa6d392017-02-01 12:48:44 -0700143int fpga_mgr_buf_load_sg(struct fpga_manager *mgr, struct fpga_image_info *info,
144 struct sg_table *sgt);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100145
Alan Tull1df28652016-11-01 14:14:26 -0500146int fpga_mgr_firmware_load(struct fpga_manager *mgr,
147 struct fpga_image_info *info,
Alan Tull6a8c3be2015-10-07 16:36:28 +0100148 const char *image_name);
149
150struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
151
Alan Tull9dce0282016-11-01 14:14:23 -0500152struct fpga_manager *fpga_mgr_get(struct device *dev);
153
Alan Tull6a8c3be2015-10-07 16:36:28 +0100154void fpga_mgr_put(struct fpga_manager *mgr);
155
156int fpga_mgr_register(struct device *dev, const char *name,
157 const struct fpga_manager_ops *mops, void *priv);
158
159void fpga_mgr_unregister(struct device *dev);
160
161#endif /*_LINUX_FPGA_MGR_H */