blob: eec7c2478b0dab4a28d8735042e3e01336a89582 [file] [log] [blame]
Alan Tull473f01f2018-05-16 18:49:58 -05001/* SPDX-License-Identifier: GPL-2.0 */
Alan Tull6a8c3be2015-10-07 16:36:28 +01002/*
3 * FPGA Framework
4 *
Alan Tull5cf0c7f2017-11-15 14:20:12 -06005 * Copyright (C) 2013-2016 Altera Corporation
6 * Copyright (C) 2017 Intel Corporation
Alan Tull6a8c3be2015-10-07 16:36:28 +01007 */
Alan Tull6a8c3be2015-10-07 16:36:28 +01008#ifndef _LINUX_FPGA_MGR_H
9#define _LINUX_FPGA_MGR_H
10
Alan Tull5cf0c7f2017-11-15 14:20:12 -060011#include <linux/mutex.h>
12#include <linux/platform_device.h>
13
Alan Tull6a8c3be2015-10-07 16:36:28 +010014struct fpga_manager;
Jason Gunthorpebaa6d392017-02-01 12:48:44 -070015struct sg_table;
Alan Tull6a8c3be2015-10-07 16:36:28 +010016
17/**
18 * enum fpga_mgr_states - fpga framework states
19 * @FPGA_MGR_STATE_UNKNOWN: can't determine state
20 * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
21 * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
22 * @FPGA_MGR_STATE_RESET: FPGA in reset state
23 * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
24 * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
25 * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
26 * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
27 * @FPGA_MGR_STATE_WRITE: writing image to FPGA
28 * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
29 * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
30 * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
31 * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
32 */
33enum fpga_mgr_states {
34 /* default FPGA states */
35 FPGA_MGR_STATE_UNKNOWN,
36 FPGA_MGR_STATE_POWER_OFF,
37 FPGA_MGR_STATE_POWER_UP,
38 FPGA_MGR_STATE_RESET,
39
40 /* getting an image for loading */
41 FPGA_MGR_STATE_FIRMWARE_REQ,
42 FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
43
44 /* write sequence: init, write, complete */
45 FPGA_MGR_STATE_WRITE_INIT,
46 FPGA_MGR_STATE_WRITE_INIT_ERR,
47 FPGA_MGR_STATE_WRITE,
48 FPGA_MGR_STATE_WRITE_ERR,
49 FPGA_MGR_STATE_WRITE_COMPLETE,
50 FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
51
52 /* fpga is programmed and operating */
53 FPGA_MGR_STATE_OPERATING,
54};
55
56/*
57 * FPGA Manager flags
58 * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
Alan Tull0fa20cd2016-11-01 14:14:29 -050059 * FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
Anatolij Gustschin68f6be62017-06-14 10:36:27 -050060 * FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
Anatolij Gustschinb37fa562017-06-14 10:36:34 -050061 * FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
Alan Tull6a8c3be2015-10-07 16:36:28 +010062 */
63#define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
Alan Tull0fa20cd2016-11-01 14:14:29 -050064#define FPGA_MGR_EXTERNAL_CONFIG BIT(1)
Moritz Fischer0f4f0c82017-02-27 09:19:00 -060065#define FPGA_MGR_ENCRYPTED_BITSTREAM BIT(2)
Anatolij Gustschin68f6be62017-06-14 10:36:27 -050066#define FPGA_MGR_BITSTREAM_LSB_FIRST BIT(3)
Anatolij Gustschinb37fa562017-06-14 10:36:34 -050067#define FPGA_MGR_COMPRESSED_BITSTREAM BIT(4)
Alan Tull6a8c3be2015-10-07 16:36:28 +010068
69/**
Alan Tull1df28652016-11-01 14:14:26 -050070 * struct fpga_image_info - information specific to a FPGA image
71 * @flags: boolean flags as defined above
72 * @enable_timeout_us: maximum time to enable traffic through bridge (uSec)
73 * @disable_timeout_us: maximum time to disable traffic through bridge (uSec)
Alan Tull42d5ec92017-03-23 19:34:27 -050074 * @config_complete_timeout_us: maximum time for FPGA to switch to operating
75 * status in the write_complete op.
Alan Tull5cf0c7f2017-11-15 14:20:12 -060076 * @firmware_name: name of FPGA image firmware file
77 * @sgt: scatter/gather table containing FPGA image
78 * @buf: contiguous buffer containing FPGA image
79 * @count: size of buf
80 * @dev: device that owns this
Alan Tull61c32102017-11-15 14:20:19 -060081 * @overlay: Device Tree overlay
Alan Tull1df28652016-11-01 14:14:26 -050082 */
83struct fpga_image_info {
84 u32 flags;
85 u32 enable_timeout_us;
86 u32 disable_timeout_us;
Alan Tull42d5ec92017-03-23 19:34:27 -050087 u32 config_complete_timeout_us;
Alan Tull5cf0c7f2017-11-15 14:20:12 -060088 char *firmware_name;
89 struct sg_table *sgt;
90 const char *buf;
91 size_t count;
92 struct device *dev;
Alan Tull61c32102017-11-15 14:20:19 -060093#ifdef CONFIG_OF
94 struct device_node *overlay;
95#endif
Alan Tull1df28652016-11-01 14:14:26 -050096};
97
98/**
Alan Tull6a8c3be2015-10-07 16:36:28 +010099 * struct fpga_manager_ops - ops for low level fpga manager drivers
Jason Gunthorpe1d7f1582016-11-22 18:22:09 +0000100 * @initial_header_size: Maximum number of bytes that should be passed into write_init
Alan Tull6a8c3be2015-10-07 16:36:28 +0100101 * @state: returns an enum value of the FPGA's state
102 * @write_init: prepare the FPGA to receive confuration data
103 * @write: write count bytes of configuration data to the FPGA
Jason Gunthorpebaa6d392017-02-01 12:48:44 -0700104 * @write_sg: write the scatter list of configuration data to the FPGA
Alan Tull6a8c3be2015-10-07 16:36:28 +0100105 * @write_complete: set FPGA to operating state after writing is done
106 * @fpga_remove: optional: Set FPGA into a specific state during driver remove
Alan Tull845089b2017-11-15 14:20:28 -0600107 * @groups: optional attribute groups.
Alan Tull6a8c3be2015-10-07 16:36:28 +0100108 *
109 * fpga_manager_ops are the low level functions implemented by a specific
110 * fpga manager driver. The optional ones are tested for NULL before being
111 * called, so leaving them out is fine.
112 */
113struct fpga_manager_ops {
Jason Gunthorpe1d7f1582016-11-22 18:22:09 +0000114 size_t initial_header_size;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100115 enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
Alan Tull1df28652016-11-01 14:14:26 -0500116 int (*write_init)(struct fpga_manager *mgr,
117 struct fpga_image_info *info,
Alan Tull6a8c3be2015-10-07 16:36:28 +0100118 const char *buf, size_t count);
119 int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
Jason Gunthorpebaa6d392017-02-01 12:48:44 -0700120 int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
Alan Tull1df28652016-11-01 14:14:26 -0500121 int (*write_complete)(struct fpga_manager *mgr,
122 struct fpga_image_info *info);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100123 void (*fpga_remove)(struct fpga_manager *mgr);
Alan Tull845089b2017-11-15 14:20:28 -0600124 const struct attribute_group **groups;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100125};
126
127/**
128 * struct fpga_manager - fpga manager structure
129 * @name: name of low level fpga manager
130 * @dev: fpga manager device
131 * @ref_mutex: only allows one reference to fpga manager
132 * @state: state of fpga manager
133 * @mops: pointer to struct of fpga manager ops
134 * @priv: low level driver private date
135 */
136struct fpga_manager {
137 const char *name;
138 struct device dev;
139 struct mutex ref_mutex;
140 enum fpga_mgr_states state;
141 const struct fpga_manager_ops *mops;
142 void *priv;
143};
144
145#define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
146
Alan Tull5cf0c7f2017-11-15 14:20:12 -0600147struct fpga_image_info *fpga_image_info_alloc(struct device *dev);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100148
Alan Tull5cf0c7f2017-11-15 14:20:12 -0600149void fpga_image_info_free(struct fpga_image_info *info);
150
151int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100152
Alan Tullebf877a52017-11-15 14:20:13 -0600153int fpga_mgr_lock(struct fpga_manager *mgr);
154void fpga_mgr_unlock(struct fpga_manager *mgr);
155
Alan Tull6a8c3be2015-10-07 16:36:28 +0100156struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
157
Alan Tull9dce0282016-11-01 14:14:23 -0500158struct fpga_manager *fpga_mgr_get(struct device *dev);
159
Alan Tull6a8c3be2015-10-07 16:36:28 +0100160void fpga_mgr_put(struct fpga_manager *mgr);
161
Alan Tull7085e2a2018-05-16 18:49:55 -0500162struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
163 const struct fpga_manager_ops *mops,
164 void *priv);
165void fpga_mgr_free(struct fpga_manager *mgr);
166int fpga_mgr_register(struct fpga_manager *mgr);
167void fpga_mgr_unregister(struct fpga_manager *mgr);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100168
169#endif /*_LINUX_FPGA_MGR_H */