aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Tull <atull@opensource.altera.com>2015-10-07 11:36:28 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2015-10-07 13:08:15 -0400
commit6a8c3be7ec8eb3c1197766f9245e0d65a4e5aff8 (patch)
tree311c3845b068f737086813b1231b1c3b695f0a1c
parentafb79e993a949d02895b912eacc86ab0e416b6fd (diff)
add FPGA manager core
API to support programming FPGA's. The following functions are exported as GPL: * fpga_mgr_buf_load Load fpga from image in buffer * fpga_mgr_firmware_load Request firmware and load it to the FPGA. * fpga_mgr_register * fpga_mgr_unregister FPGA device drivers can be added by calling fpga_mgr_register() to register a set of fpga_manager_ops to do device specific stuff. * of_fpga_mgr_get * fpga_mgr_put Get/put a reference to a fpga manager. The following sysfs files are created: * /sys/class/fpga_manager/<fpga>/name Name of low level driver. * /sys/class/fpga_manager/<fpga>/state State of fpga manager Signed-off-by: Alan Tull <atull@opensource.altera.com> Acked-by: Michal Simek <michal.simek@xilinx.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/fpga/Kconfig14
-rw-r--r--drivers/fpga/Makefile8
-rw-r--r--drivers/fpga/fpga-mgr.c382
-rw-r--r--include/linux/fpga/fpga-mgr.h127
6 files changed, 534 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 709488ae882e..5a89e409ad18 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -192,4 +192,6 @@ source "drivers/hwtracing/stm/Kconfig"
192 192
193source "drivers/hwtracing/intel_th/Kconfig" 193source "drivers/hwtracing/intel_th/Kconfig"
194 194
195source "drivers/fpga/Kconfig"
196
195endmenu 197endmenu
diff --git a/drivers/Makefile b/drivers/Makefile
index e63542dd7010..7064bf476c2a 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -169,3 +169,4 @@ obj-y += hwtracing/intel_th/
169obj-$(CONFIG_STM) += hwtracing/stm/ 169obj-$(CONFIG_STM) += hwtracing/stm/
170obj-$(CONFIG_ANDROID) += android/ 170obj-$(CONFIG_ANDROID) += android/
171obj-$(CONFIG_NVMEM) += nvmem/ 171obj-$(CONFIG_NVMEM) += nvmem/
172obj-$(CONFIG_FPGA) += fpga/
diff --git a/drivers/fpga/Kconfig b/drivers/fpga/Kconfig
new file mode 100644
index 000000000000..f1f1f6df54d3
--- /dev/null
+++ b/drivers/fpga/Kconfig
@@ -0,0 +1,14 @@
1#
2# FPGA framework configuration
3#
4
5menu "FPGA Configuration Support"
6
7config FPGA
8 tristate "FPGA Configuration Framework"
9 help
10 Say Y here if you want support for configuring FPGAs from the
11 kernel. The FPGA framework adds a FPGA manager class and FPGA
12 manager drivers.
13
14endmenu
diff --git a/drivers/fpga/Makefile b/drivers/fpga/Makefile
new file mode 100644
index 000000000000..3313c5266795
--- /dev/null
+++ b/drivers/fpga/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for the fpga framework and fpga manager drivers.
3#
4
5# Core FPGA Manager Framework
6obj-$(CONFIG_FPGA) += fpga-mgr.o
7
8# FPGA Manager Drivers
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
new file mode 100644
index 000000000000..25261636687c
--- /dev/null
+++ b/drivers/fpga/fpga-mgr.c
@@ -0,0 +1,382 @@
1/*
2 * FPGA Manager Core
3 *
4 * Copyright (C) 2013-2015 Altera Corporation
5 *
6 * With code from the mailing list:
7 * Copyright (C) 2013 Xilinx, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21#include <linux/firmware.h>
22#include <linux/fpga/fpga-mgr.h>
23#include <linux/idr.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/mutex.h>
27#include <linux/slab.h>
28
29static DEFINE_IDA(fpga_mgr_ida);
30static struct class *fpga_mgr_class;
31
32/**
33 * fpga_mgr_buf_load - load fpga from image in buffer
34 * @mgr: fpga manager
35 * @flags: flags setting fpga confuration modes
36 * @buf: buffer contain fpga image
37 * @count: byte count of buf
38 *
39 * Step the low level fpga manager through the device-specific steps of getting
40 * an FPGA ready to be configured, writing the image to it, then doing whatever
41 * post-configuration steps necessary.
42 *
43 * Return: 0 on success, negative error code otherwise.
44 */
45int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags, const char *buf,
46 size_t count)
47{
48 struct device *dev = &mgr->dev;
49 int ret;
50
51 if (!mgr)
52 return -ENODEV;
53
54 /*
55 * Call the low level driver's write_init function. This will do the
56 * device-specific things to get the FPGA into the state where it is
57 * ready to receive an FPGA image.
58 */
59 mgr->state = FPGA_MGR_STATE_WRITE_INIT;
60 ret = mgr->mops->write_init(mgr, flags, buf, count);
61 if (ret) {
62 dev_err(dev, "Error preparing FPGA for writing\n");
63 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
64 return ret;
65 }
66
67 /*
68 * Write the FPGA image to the FPGA.
69 */
70 mgr->state = FPGA_MGR_STATE_WRITE;
71 ret = mgr->mops->write(mgr, buf, count);
72 if (ret) {
73 dev_err(dev, "Error while writing image data to FPGA\n");
74 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
75 return ret;
76 }
77
78 /*
79 * After all the FPGA image has been written, do the device specific
80 * steps to finish and set the FPGA into operating mode.
81 */
82 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
83 ret = mgr->mops->write_complete(mgr, flags);
84 if (ret) {
85 dev_err(dev, "Error after writing image data to FPGA\n");
86 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
87 return ret;
88 }
89 mgr->state = FPGA_MGR_STATE_OPERATING;
90
91 return 0;
92}
93EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
94
95/**
96 * fpga_mgr_firmware_load - request firmware and load to fpga
97 * @mgr: fpga manager
98 * @flags: flags setting fpga confuration modes
99 * @image_name: name of image file on the firmware search path
100 *
101 * Request an FPGA image using the firmware class, then write out to the FPGA.
102 * Update the state before each step to provide info on what step failed if
103 * there is a failure.
104 *
105 * Return: 0 on success, negative error code otherwise.
106 */
107int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
108 const char *image_name)
109{
110 struct device *dev = &mgr->dev;
111 const struct firmware *fw;
112 int ret;
113
114 if (!mgr)
115 return -ENODEV;
116
117 dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
118
119 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
120
121 ret = request_firmware(&fw, image_name, dev);
122 if (ret) {
123 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
124 dev_err(dev, "Error requesting firmware %s\n", image_name);
125 return ret;
126 }
127
128 ret = fpga_mgr_buf_load(mgr, flags, fw->data, fw->size);
129 if (ret)
130 return ret;
131
132 release_firmware(fw);
133
134 return 0;
135}
136EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
137
138static const char * const state_str[] = {
139 [FPGA_MGR_STATE_UNKNOWN] = "unknown",
140 [FPGA_MGR_STATE_POWER_OFF] = "power off",
141 [FPGA_MGR_STATE_POWER_UP] = "power up",
142 [FPGA_MGR_STATE_RESET] = "reset",
143
144 /* requesting FPGA image from firmware */
145 [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
146 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
147
148 /* Preparing FPGA to receive image */
149 [FPGA_MGR_STATE_WRITE_INIT] = "write init",
150 [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
151
152 /* Writing image to FPGA */
153 [FPGA_MGR_STATE_WRITE] = "write",
154 [FPGA_MGR_STATE_WRITE_ERR] = "write error",
155
156 /* Finishing configuration after image has been written */
157 [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
158 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
159
160 /* FPGA reports to be in normal operating mode */
161 [FPGA_MGR_STATE_OPERATING] = "operating",
162};
163
164static ssize_t name_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 struct fpga_manager *mgr = to_fpga_manager(dev);
168
169 return sprintf(buf, "%s\n", mgr->name);
170}
171
172static ssize_t state_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct fpga_manager *mgr = to_fpga_manager(dev);
176
177 return sprintf(buf, "%s\n", state_str[mgr->state]);
178}
179
180static DEVICE_ATTR_RO(name);
181static DEVICE_ATTR_RO(state);
182
183static struct attribute *fpga_mgr_attrs[] = {
184 &dev_attr_name.attr,
185 &dev_attr_state.attr,
186 NULL,
187};
188ATTRIBUTE_GROUPS(fpga_mgr);
189
190static int fpga_mgr_of_node_match(struct device *dev, const void *data)
191{
192 return dev->of_node == data;
193}
194
195/**
196 * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
197 * @node: device node
198 *
199 * Given a device node, get an exclusive reference to a fpga mgr.
200 *
201 * Return: fpga manager struct or IS_ERR() condition containing error code.
202 */
203struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
204{
205 struct fpga_manager *mgr;
206 struct device *dev;
207
208 if (!node)
209 return ERR_PTR(-EINVAL);
210
211 dev = class_find_device(fpga_mgr_class, NULL, node,
212 fpga_mgr_of_node_match);
213 if (!dev)
214 return ERR_PTR(-ENODEV);
215
216 mgr = to_fpga_manager(dev);
217 put_device(dev);
218 if (!mgr)
219 return ERR_PTR(-ENODEV);
220
221 /* Get exclusive use of fpga manager */
222 if (!mutex_trylock(&mgr->ref_mutex))
223 return ERR_PTR(-EBUSY);
224
225 if (!try_module_get(THIS_MODULE)) {
226 mutex_unlock(&mgr->ref_mutex);
227 return ERR_PTR(-ENODEV);
228 }
229
230 return mgr;
231}
232EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
233
234/**
235 * fpga_mgr_put - release a reference to a fpga manager
236 * @mgr: fpga manager structure
237 */
238void fpga_mgr_put(struct fpga_manager *mgr)
239{
240 if (mgr) {
241 module_put(THIS_MODULE);
242 mutex_unlock(&mgr->ref_mutex);
243 }
244}
245EXPORT_SYMBOL_GPL(fpga_mgr_put);
246
247/**
248 * fpga_mgr_register - register a low level fpga manager driver
249 * @dev: fpga manager device from pdev
250 * @name: fpga manager name
251 * @mops: pointer to structure of fpga manager ops
252 * @priv: fpga manager private data
253 *
254 * Return: 0 on success, negative error code otherwise.
255 */
256int fpga_mgr_register(struct device *dev, const char *name,
257 const struct fpga_manager_ops *mops,
258 void *priv)
259{
260 struct fpga_manager *mgr;
261 const char *dt_label;
262 int id, ret;
263
264 if (!mops || !mops->write_init || !mops->write ||
265 !mops->write_complete || !mops->state) {
266 dev_err(dev, "Attempt to register without fpga_manager_ops\n");
267 return -EINVAL;
268 }
269
270 if (!name || !strlen(name)) {
271 dev_err(dev, "Attempt to register with no name!\n");
272 return -EINVAL;
273 }
274
275 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
276 if (!mgr)
277 return -ENOMEM;
278
279 id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
280 if (id < 0) {
281 ret = id;
282 goto error_kfree;
283 }
284
285 mutex_init(&mgr->ref_mutex);
286
287 mgr->name = name;
288 mgr->mops = mops;
289 mgr->priv = priv;
290
291 /*
292 * Initialize framework state by requesting low level driver read state
293 * from device. FPGA may be in reset mode or may have been programmed
294 * by bootloader or EEPROM.
295 */
296 mgr->state = mgr->mops->state(mgr);
297
298 device_initialize(&mgr->dev);
299 mgr->dev.class = fpga_mgr_class;
300 mgr->dev.parent = dev;
301 mgr->dev.of_node = dev->of_node;
302 mgr->dev.id = id;
303 dev_set_drvdata(dev, mgr);
304
305 dt_label = of_get_property(mgr->dev.of_node, "label", NULL);
306 if (dt_label)
307 ret = dev_set_name(&mgr->dev, "%s", dt_label);
308 else
309 ret = dev_set_name(&mgr->dev, "fpga%d", id);
310
311 ret = device_add(&mgr->dev);
312 if (ret)
313 goto error_device;
314
315 dev_info(&mgr->dev, "%s registered\n", mgr->name);
316
317 return 0;
318
319error_device:
320 ida_simple_remove(&fpga_mgr_ida, id);
321error_kfree:
322 kfree(mgr);
323
324 return ret;
325}
326EXPORT_SYMBOL_GPL(fpga_mgr_register);
327
328/**
329 * fpga_mgr_unregister - unregister a low level fpga manager driver
330 * @dev: fpga manager device from pdev
331 */
332void fpga_mgr_unregister(struct device *dev)
333{
334 struct fpga_manager *mgr = dev_get_drvdata(dev);
335
336 dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
337
338 /*
339 * If the low level driver provides a method for putting fpga into
340 * a desired state upon unregister, do it.
341 */
342 if (mgr->mops->fpga_remove)
343 mgr->mops->fpga_remove(mgr);
344
345 device_unregister(&mgr->dev);
346}
347EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
348
349static void fpga_mgr_dev_release(struct device *dev)
350{
351 struct fpga_manager *mgr = to_fpga_manager(dev);
352
353 ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
354 kfree(mgr);
355}
356
357static int __init fpga_mgr_class_init(void)
358{
359 pr_info("FPGA manager framework\n");
360
361 fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
362 if (IS_ERR(fpga_mgr_class))
363 return PTR_ERR(fpga_mgr_class);
364
365 fpga_mgr_class->dev_groups = fpga_mgr_groups;
366 fpga_mgr_class->dev_release = fpga_mgr_dev_release;
367
368 return 0;
369}
370
371static void __exit fpga_mgr_class_exit(void)
372{
373 class_destroy(fpga_mgr_class);
374 ida_destroy(&fpga_mgr_ida);
375}
376
377MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
378MODULE_DESCRIPTION("FPGA manager framework");
379MODULE_LICENSE("GPL v2");
380
381subsys_initcall(fpga_mgr_class_init);
382module_exit(fpga_mgr_class_exit);
diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
new file mode 100644
index 000000000000..0940bf45e2f2
--- /dev/null
+++ b/include/linux/fpga/fpga-mgr.h
@@ -0,0 +1,127 @@
1/*
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;
25
26/**
27 * enum fpga_mgr_states - fpga framework states
28 * @FPGA_MGR_STATE_UNKNOWN: can't determine state
29 * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
30 * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
31 * @FPGA_MGR_STATE_RESET: FPGA in reset state
32 * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
33 * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
34 * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
35 * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
36 * @FPGA_MGR_STATE_WRITE: writing image to FPGA
37 * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
38 * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
39 * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
40 * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
41 */
42enum fpga_mgr_states {
43 /* default FPGA states */
44 FPGA_MGR_STATE_UNKNOWN,
45 FPGA_MGR_STATE_POWER_OFF,
46 FPGA_MGR_STATE_POWER_UP,
47 FPGA_MGR_STATE_RESET,
48
49 /* getting an image for loading */
50 FPGA_MGR_STATE_FIRMWARE_REQ,
51 FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
52
53 /* write sequence: init, write, complete */
54 FPGA_MGR_STATE_WRITE_INIT,
55 FPGA_MGR_STATE_WRITE_INIT_ERR,
56 FPGA_MGR_STATE_WRITE,
57 FPGA_MGR_STATE_WRITE_ERR,
58 FPGA_MGR_STATE_WRITE_COMPLETE,
59 FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
60
61 /* fpga is programmed and operating */
62 FPGA_MGR_STATE_OPERATING,
63};
64
65/*
66 * FPGA Manager flags
67 * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
68 */
69#define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
70
71/**
72 * struct fpga_manager_ops - ops for low level fpga manager drivers
73 * @state: returns an enum value of the FPGA's state
74 * @write_init: prepare the FPGA to receive confuration data
75 * @write: write count bytes of configuration data to the FPGA
76 * @write_complete: set FPGA to operating state after writing is done
77 * @fpga_remove: optional: Set FPGA into a specific state during driver remove
78 *
79 * fpga_manager_ops are the low level functions implemented by a specific
80 * fpga manager driver. The optional ones are tested for NULL before being
81 * called, so leaving them out is fine.
82 */
83struct fpga_manager_ops {
84 enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
85 int (*write_init)(struct fpga_manager *mgr, u32 flags,
86 const char *buf, size_t count);
87 int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
88 int (*write_complete)(struct fpga_manager *mgr, u32 flags);
89 void (*fpga_remove)(struct fpga_manager *mgr);
90};
91
92/**
93 * struct fpga_manager - fpga manager structure
94 * @name: name of low level fpga manager
95 * @dev: fpga manager device
96 * @ref_mutex: only allows one reference to fpga manager
97 * @state: state of fpga manager
98 * @mops: pointer to struct of fpga manager ops
99 * @priv: low level driver private date
100 */
101struct fpga_manager {
102 const char *name;
103 struct device dev;
104 struct mutex ref_mutex;
105 enum fpga_mgr_states state;
106 const struct fpga_manager_ops *mops;
107 void *priv;
108};
109
110#define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
111
112int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags,
113 const char *buf, size_t count);
114
115int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
116 const char *image_name);
117
118struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
119
120void fpga_mgr_put(struct fpga_manager *mgr);
121
122int fpga_mgr_register(struct device *dev, const char *name,
123 const struct fpga_manager_ops *mops, void *priv);
124
125void fpga_mgr_unregister(struct device *dev);
126
127#endif /*_LINUX_FPGA_MGR_H */