blob: b690e65d55fe17254352a67501901e7fd2bcbe3e [file] [log] [blame]
Alan Tull6a8c3be2015-10-07 16:36:28 +01001/*
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
Alan Tull92d94a72015-10-22 12:38:38 -050041 * post-configuration steps necessary. This code assumes the caller got the
Alan Tull9dce0282016-11-01 14:14:23 -050042 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
43 * not an error code.
Alan Tull6a8c3be2015-10-07 16:36:28 +010044 *
45 * Return: 0 on success, negative error code otherwise.
46 */
47int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags, const char *buf,
48 size_t count)
49{
50 struct device *dev = &mgr->dev;
51 int ret;
52
Alan Tull6a8c3be2015-10-07 16:36:28 +010053 /*
54 * Call the low level driver's write_init function. This will do the
55 * device-specific things to get the FPGA into the state where it is
56 * ready to receive an FPGA image.
57 */
58 mgr->state = FPGA_MGR_STATE_WRITE_INIT;
59 ret = mgr->mops->write_init(mgr, flags, buf, count);
60 if (ret) {
61 dev_err(dev, "Error preparing FPGA for writing\n");
62 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
63 return ret;
64 }
65
66 /*
67 * Write the FPGA image to the FPGA.
68 */
69 mgr->state = FPGA_MGR_STATE_WRITE;
70 ret = mgr->mops->write(mgr, buf, count);
71 if (ret) {
72 dev_err(dev, "Error while writing image data to FPGA\n");
73 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
74 return ret;
75 }
76
77 /*
78 * After all the FPGA image has been written, do the device specific
79 * steps to finish and set the FPGA into operating mode.
80 */
81 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
82 ret = mgr->mops->write_complete(mgr, flags);
83 if (ret) {
84 dev_err(dev, "Error after writing image data to FPGA\n");
85 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
86 return ret;
87 }
88 mgr->state = FPGA_MGR_STATE_OPERATING;
89
90 return 0;
91}
92EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
93
94/**
95 * fpga_mgr_firmware_load - request firmware and load to fpga
96 * @mgr: fpga manager
97 * @flags: flags setting fpga confuration modes
98 * @image_name: name of image file on the firmware search path
99 *
100 * Request an FPGA image using the firmware class, then write out to the FPGA.
101 * Update the state before each step to provide info on what step failed if
Alan Tull92d94a72015-10-22 12:38:38 -0500102 * there is a failure. This code assumes the caller got the mgr pointer
Alan Tull9dce0282016-11-01 14:14:23 -0500103 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
104 * code.
Alan Tull6a8c3be2015-10-07 16:36:28 +0100105 *
106 * Return: 0 on success, negative error code otherwise.
107 */
108int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
109 const char *image_name)
110{
111 struct device *dev = &mgr->dev;
112 const struct firmware *fw;
113 int ret;
114
Alan Tull6a8c3be2015-10-07 16:36:28 +0100115 dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
116
117 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
118
119 ret = request_firmware(&fw, image_name, dev);
120 if (ret) {
121 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
122 dev_err(dev, "Error requesting firmware %s\n", image_name);
123 return ret;
124 }
125
126 ret = fpga_mgr_buf_load(mgr, flags, fw->data, fw->size);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100127
128 release_firmware(fw);
129
Tobias Klausere8c77bd2015-11-18 10:48:16 +0100130 return ret;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100131}
132EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
133
134static const char * const state_str[] = {
135 [FPGA_MGR_STATE_UNKNOWN] = "unknown",
136 [FPGA_MGR_STATE_POWER_OFF] = "power off",
137 [FPGA_MGR_STATE_POWER_UP] = "power up",
138 [FPGA_MGR_STATE_RESET] = "reset",
139
140 /* requesting FPGA image from firmware */
141 [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
142 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
143
144 /* Preparing FPGA to receive image */
145 [FPGA_MGR_STATE_WRITE_INIT] = "write init",
146 [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
147
148 /* Writing image to FPGA */
149 [FPGA_MGR_STATE_WRITE] = "write",
150 [FPGA_MGR_STATE_WRITE_ERR] = "write error",
151
152 /* Finishing configuration after image has been written */
153 [FPGA_MGR_STATE_WRITE_COMPLETE] = "write complete",
154 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] = "write complete error",
155
156 /* FPGA reports to be in normal operating mode */
157 [FPGA_MGR_STATE_OPERATING] = "operating",
158};
159
160static ssize_t name_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
163 struct fpga_manager *mgr = to_fpga_manager(dev);
164
165 return sprintf(buf, "%s\n", mgr->name);
166}
167
168static ssize_t state_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
171 struct fpga_manager *mgr = to_fpga_manager(dev);
172
173 return sprintf(buf, "%s\n", state_str[mgr->state]);
174}
175
176static DEVICE_ATTR_RO(name);
177static DEVICE_ATTR_RO(state);
178
179static struct attribute *fpga_mgr_attrs[] = {
180 &dev_attr_name.attr,
181 &dev_attr_state.attr,
182 NULL,
183};
184ATTRIBUTE_GROUPS(fpga_mgr);
185
Alan Tull9dce0282016-11-01 14:14:23 -0500186struct fpga_manager *__fpga_mgr_get(struct device *dev)
Alan Tull6a8c3be2015-10-07 16:36:28 +0100187{
188 struct fpga_manager *mgr;
Alan Tull654ba4c2015-10-22 12:38:37 -0500189 int ret = -ENODEV;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100190
Alan Tull6a8c3be2015-10-07 16:36:28 +0100191 mgr = to_fpga_manager(dev);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100192 if (!mgr)
Alan Tull654ba4c2015-10-22 12:38:37 -0500193 goto err_dev;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100194
195 /* Get exclusive use of fpga manager */
Alan Tull654ba4c2015-10-22 12:38:37 -0500196 if (!mutex_trylock(&mgr->ref_mutex)) {
197 ret = -EBUSY;
198 goto err_dev;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100199 }
200
Alan Tull654ba4c2015-10-22 12:38:37 -0500201 if (!try_module_get(dev->parent->driver->owner))
202 goto err_ll_mod;
203
Alan Tull6a8c3be2015-10-07 16:36:28 +0100204 return mgr;
Alan Tull654ba4c2015-10-22 12:38:37 -0500205
206err_ll_mod:
207 mutex_unlock(&mgr->ref_mutex);
208err_dev:
209 put_device(dev);
210 return ERR_PTR(ret);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100211}
Alan Tull9dce0282016-11-01 14:14:23 -0500212
213static int fpga_mgr_dev_match(struct device *dev, const void *data)
214{
215 return dev->parent == data;
216}
217
218/**
219 * fpga_mgr_get - get an exclusive reference to a fpga mgr
220 * @dev: parent device that fpga mgr was registered with
221 *
222 * Given a device, get an exclusive reference to a fpga mgr.
223 *
224 * Return: fpga manager struct or IS_ERR() condition containing error code.
225 */
226struct fpga_manager *fpga_mgr_get(struct device *dev)
227{
228 struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
229 fpga_mgr_dev_match);
230 if (!mgr_dev)
231 return ERR_PTR(-ENODEV);
232
233 return __fpga_mgr_get(mgr_dev);
234}
235EXPORT_SYMBOL_GPL(fpga_mgr_get);
236
237static int fpga_mgr_of_node_match(struct device *dev, const void *data)
238{
239 return dev->of_node == data;
240}
241
242/**
243 * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
244 * @node: device node
245 *
246 * Given a device node, get an exclusive reference to a fpga mgr.
247 *
248 * Return: fpga manager struct or IS_ERR() condition containing error code.
249 */
250struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
251{
252 struct device *dev;
253
254 dev = class_find_device(fpga_mgr_class, NULL, node,
255 fpga_mgr_of_node_match);
256 if (!dev)
257 return ERR_PTR(-ENODEV);
258
259 return __fpga_mgr_get(dev);
260}
Alan Tull6a8c3be2015-10-07 16:36:28 +0100261EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
262
263/**
264 * fpga_mgr_put - release a reference to a fpga manager
265 * @mgr: fpga manager structure
266 */
267void fpga_mgr_put(struct fpga_manager *mgr)
268{
Alan Tull654ba4c2015-10-22 12:38:37 -0500269 module_put(mgr->dev.parent->driver->owner);
270 mutex_unlock(&mgr->ref_mutex);
271 put_device(&mgr->dev);
Alan Tull6a8c3be2015-10-07 16:36:28 +0100272}
273EXPORT_SYMBOL_GPL(fpga_mgr_put);
274
275/**
276 * fpga_mgr_register - register a low level fpga manager driver
277 * @dev: fpga manager device from pdev
278 * @name: fpga manager name
279 * @mops: pointer to structure of fpga manager ops
280 * @priv: fpga manager private data
281 *
282 * Return: 0 on success, negative error code otherwise.
283 */
284int fpga_mgr_register(struct device *dev, const char *name,
285 const struct fpga_manager_ops *mops,
286 void *priv)
287{
288 struct fpga_manager *mgr;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100289 int id, ret;
290
291 if (!mops || !mops->write_init || !mops->write ||
292 !mops->write_complete || !mops->state) {
293 dev_err(dev, "Attempt to register without fpga_manager_ops\n");
294 return -EINVAL;
295 }
296
297 if (!name || !strlen(name)) {
298 dev_err(dev, "Attempt to register with no name!\n");
299 return -EINVAL;
300 }
301
302 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
303 if (!mgr)
304 return -ENOMEM;
305
306 id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
307 if (id < 0) {
308 ret = id;
309 goto error_kfree;
310 }
311
312 mutex_init(&mgr->ref_mutex);
313
314 mgr->name = name;
315 mgr->mops = mops;
316 mgr->priv = priv;
317
318 /*
319 * Initialize framework state by requesting low level driver read state
320 * from device. FPGA may be in reset mode or may have been programmed
321 * by bootloader or EEPROM.
322 */
323 mgr->state = mgr->mops->state(mgr);
324
325 device_initialize(&mgr->dev);
326 mgr->dev.class = fpga_mgr_class;
327 mgr->dev.parent = dev;
328 mgr->dev.of_node = dev->of_node;
329 mgr->dev.id = id;
330 dev_set_drvdata(dev, mgr);
331
Alan Tull07687c02015-10-29 14:39:56 -0500332 ret = dev_set_name(&mgr->dev, "fpga%d", id);
333 if (ret)
334 goto error_device;
Alan Tull6a8c3be2015-10-07 16:36:28 +0100335
336 ret = device_add(&mgr->dev);
337 if (ret)
338 goto error_device;
339
340 dev_info(&mgr->dev, "%s registered\n", mgr->name);
341
342 return 0;
343
344error_device:
345 ida_simple_remove(&fpga_mgr_ida, id);
346error_kfree:
347 kfree(mgr);
348
349 return ret;
350}
351EXPORT_SYMBOL_GPL(fpga_mgr_register);
352
353/**
354 * fpga_mgr_unregister - unregister a low level fpga manager driver
355 * @dev: fpga manager device from pdev
356 */
357void fpga_mgr_unregister(struct device *dev)
358{
359 struct fpga_manager *mgr = dev_get_drvdata(dev);
360
361 dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
362
363 /*
364 * If the low level driver provides a method for putting fpga into
365 * a desired state upon unregister, do it.
366 */
367 if (mgr->mops->fpga_remove)
368 mgr->mops->fpga_remove(mgr);
369
370 device_unregister(&mgr->dev);
371}
372EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
373
374static void fpga_mgr_dev_release(struct device *dev)
375{
376 struct fpga_manager *mgr = to_fpga_manager(dev);
377
378 ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
379 kfree(mgr);
380}
381
382static int __init fpga_mgr_class_init(void)
383{
384 pr_info("FPGA manager framework\n");
385
386 fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
387 if (IS_ERR(fpga_mgr_class))
388 return PTR_ERR(fpga_mgr_class);
389
390 fpga_mgr_class->dev_groups = fpga_mgr_groups;
391 fpga_mgr_class->dev_release = fpga_mgr_dev_release;
392
393 return 0;
394}
395
396static void __exit fpga_mgr_class_exit(void)
397{
398 class_destroy(fpga_mgr_class);
399 ida_destroy(&fpga_mgr_ida);
400}
401
402MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
403MODULE_DESCRIPTION("FPGA manager framework");
404MODULE_LICENSE("GPL v2");
405
406subsys_initcall(fpga_mgr_class_init);
407module_exit(fpga_mgr_class_exit);