]> Git Repo - linux.git/blob - drivers/fpga/fpga-mgr.c
Merge 4.9-rc5 into char-misc-next
[linux.git] / drivers / fpga / fpga-mgr.c
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
29 static DEFINE_IDA(fpga_mgr_ida);
30 static struct class *fpga_mgr_class;
31
32 /**
33  * fpga_mgr_buf_load - load fpga from image in buffer
34  * @mgr:        fpga manager
35  * @info:       fpga image specific information
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.  This code assumes the caller got the
42  * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
43  * not an error code.
44  *
45  * Return: 0 on success, negative error code otherwise.
46  */
47 int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
48                       const char *buf, size_t count)
49 {
50         struct device *dev = &mgr->dev;
51         int ret;
52
53         /*
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, info, 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, info);
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 }
92 EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
93
94 /**
95  * fpga_mgr_firmware_load - request firmware and load to fpga
96  * @mgr:        fpga manager
97  * @info:       fpga image specific information
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
102  * there is a failure.  This code assumes the caller got the mgr pointer
103  * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
104  * code.
105  *
106  * Return: 0 on success, negative error code otherwise.
107  */
108 int fpga_mgr_firmware_load(struct fpga_manager *mgr,
109                            struct fpga_image_info *info,
110                            const char *image_name)
111 {
112         struct device *dev = &mgr->dev;
113         const struct firmware *fw;
114         int ret;
115
116         dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
117
118         mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
119
120         ret = request_firmware(&fw, image_name, dev);
121         if (ret) {
122                 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
123                 dev_err(dev, "Error requesting firmware %s\n", image_name);
124                 return ret;
125         }
126
127         ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
128
129         release_firmware(fw);
130
131         return ret;
132 }
133 EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
134
135 static const char * const state_str[] = {
136         [FPGA_MGR_STATE_UNKNOWN] =              "unknown",
137         [FPGA_MGR_STATE_POWER_OFF] =            "power off",
138         [FPGA_MGR_STATE_POWER_UP] =             "power up",
139         [FPGA_MGR_STATE_RESET] =                "reset",
140
141         /* requesting FPGA image from firmware */
142         [FPGA_MGR_STATE_FIRMWARE_REQ] =         "firmware request",
143         [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =     "firmware request error",
144
145         /* Preparing FPGA to receive image */
146         [FPGA_MGR_STATE_WRITE_INIT] =           "write init",
147         [FPGA_MGR_STATE_WRITE_INIT_ERR] =       "write init error",
148
149         /* Writing image to FPGA */
150         [FPGA_MGR_STATE_WRITE] =                "write",
151         [FPGA_MGR_STATE_WRITE_ERR] =            "write error",
152
153         /* Finishing configuration after image has been written */
154         [FPGA_MGR_STATE_WRITE_COMPLETE] =       "write complete",
155         [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =   "write complete error",
156
157         /* FPGA reports to be in normal operating mode */
158         [FPGA_MGR_STATE_OPERATING] =            "operating",
159 };
160
161 static ssize_t name_show(struct device *dev,
162                          struct device_attribute *attr, char *buf)
163 {
164         struct fpga_manager *mgr = to_fpga_manager(dev);
165
166         return sprintf(buf, "%s\n", mgr->name);
167 }
168
169 static ssize_t state_show(struct device *dev,
170                           struct device_attribute *attr, char *buf)
171 {
172         struct fpga_manager *mgr = to_fpga_manager(dev);
173
174         return sprintf(buf, "%s\n", state_str[mgr->state]);
175 }
176
177 static DEVICE_ATTR_RO(name);
178 static DEVICE_ATTR_RO(state);
179
180 static struct attribute *fpga_mgr_attrs[] = {
181         &dev_attr_name.attr,
182         &dev_attr_state.attr,
183         NULL,
184 };
185 ATTRIBUTE_GROUPS(fpga_mgr);
186
187 struct fpga_manager *__fpga_mgr_get(struct device *dev)
188 {
189         struct fpga_manager *mgr;
190         int ret = -ENODEV;
191
192         mgr = to_fpga_manager(dev);
193         if (!mgr)
194                 goto err_dev;
195
196         /* Get exclusive use of fpga manager */
197         if (!mutex_trylock(&mgr->ref_mutex)) {
198                 ret = -EBUSY;
199                 goto err_dev;
200         }
201
202         if (!try_module_get(dev->parent->driver->owner))
203                 goto err_ll_mod;
204
205         return mgr;
206
207 err_ll_mod:
208         mutex_unlock(&mgr->ref_mutex);
209 err_dev:
210         put_device(dev);
211         return ERR_PTR(ret);
212 }
213
214 static int fpga_mgr_dev_match(struct device *dev, const void *data)
215 {
216         return dev->parent == data;
217 }
218
219 /**
220  * fpga_mgr_get - get an exclusive reference to a fpga mgr
221  * @dev:        parent device that fpga mgr was registered with
222  *
223  * Given a device, get an exclusive reference to a fpga mgr.
224  *
225  * Return: fpga manager struct or IS_ERR() condition containing error code.
226  */
227 struct fpga_manager *fpga_mgr_get(struct device *dev)
228 {
229         struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
230                                                    fpga_mgr_dev_match);
231         if (!mgr_dev)
232                 return ERR_PTR(-ENODEV);
233
234         return __fpga_mgr_get(mgr_dev);
235 }
236 EXPORT_SYMBOL_GPL(fpga_mgr_get);
237
238 static int fpga_mgr_of_node_match(struct device *dev, const void *data)
239 {
240         return dev->of_node == data;
241 }
242
243 /**
244  * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
245  * @node:       device node
246  *
247  * Given a device node, get an exclusive reference to a fpga mgr.
248  *
249  * Return: fpga manager struct or IS_ERR() condition containing error code.
250  */
251 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
252 {
253         struct device *dev;
254
255         dev = class_find_device(fpga_mgr_class, NULL, node,
256                                 fpga_mgr_of_node_match);
257         if (!dev)
258                 return ERR_PTR(-ENODEV);
259
260         return __fpga_mgr_get(dev);
261 }
262 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
263
264 /**
265  * fpga_mgr_put - release a reference to a fpga manager
266  * @mgr:        fpga manager structure
267  */
268 void fpga_mgr_put(struct fpga_manager *mgr)
269 {
270         module_put(mgr->dev.parent->driver->owner);
271         mutex_unlock(&mgr->ref_mutex);
272         put_device(&mgr->dev);
273 }
274 EXPORT_SYMBOL_GPL(fpga_mgr_put);
275
276 /**
277  * fpga_mgr_register - register a low level fpga manager driver
278  * @dev:        fpga manager device from pdev
279  * @name:       fpga manager name
280  * @mops:       pointer to structure of fpga manager ops
281  * @priv:       fpga manager private data
282  *
283  * Return: 0 on success, negative error code otherwise.
284  */
285 int fpga_mgr_register(struct device *dev, const char *name,
286                       const struct fpga_manager_ops *mops,
287                       void *priv)
288 {
289         struct fpga_manager *mgr;
290         int id, ret;
291
292         if (!mops || !mops->write_init || !mops->write ||
293             !mops->write_complete || !mops->state) {
294                 dev_err(dev, "Attempt to register without fpga_manager_ops\n");
295                 return -EINVAL;
296         }
297
298         if (!name || !strlen(name)) {
299                 dev_err(dev, "Attempt to register with no name!\n");
300                 return -EINVAL;
301         }
302
303         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
304         if (!mgr)
305                 return -ENOMEM;
306
307         id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
308         if (id < 0) {
309                 ret = id;
310                 goto error_kfree;
311         }
312
313         mutex_init(&mgr->ref_mutex);
314
315         mgr->name = name;
316         mgr->mops = mops;
317         mgr->priv = priv;
318
319         /*
320          * Initialize framework state by requesting low level driver read state
321          * from device.  FPGA may be in reset mode or may have been programmed
322          * by bootloader or EEPROM.
323          */
324         mgr->state = mgr->mops->state(mgr);
325
326         device_initialize(&mgr->dev);
327         mgr->dev.class = fpga_mgr_class;
328         mgr->dev.parent = dev;
329         mgr->dev.of_node = dev->of_node;
330         mgr->dev.id = id;
331         dev_set_drvdata(dev, mgr);
332
333         ret = dev_set_name(&mgr->dev, "fpga%d", id);
334         if (ret)
335                 goto error_device;
336
337         ret = device_add(&mgr->dev);
338         if (ret)
339                 goto error_device;
340
341         dev_info(&mgr->dev, "%s registered\n", mgr->name);
342
343         return 0;
344
345 error_device:
346         ida_simple_remove(&fpga_mgr_ida, id);
347 error_kfree:
348         kfree(mgr);
349
350         return ret;
351 }
352 EXPORT_SYMBOL_GPL(fpga_mgr_register);
353
354 /**
355  * fpga_mgr_unregister - unregister a low level fpga manager driver
356  * @dev:        fpga manager device from pdev
357  */
358 void fpga_mgr_unregister(struct device *dev)
359 {
360         struct fpga_manager *mgr = dev_get_drvdata(dev);
361
362         dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
363
364         /*
365          * If the low level driver provides a method for putting fpga into
366          * a desired state upon unregister, do it.
367          */
368         if (mgr->mops->fpga_remove)
369                 mgr->mops->fpga_remove(mgr);
370
371         device_unregister(&mgr->dev);
372 }
373 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
374
375 static void fpga_mgr_dev_release(struct device *dev)
376 {
377         struct fpga_manager *mgr = to_fpga_manager(dev);
378
379         ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
380         kfree(mgr);
381 }
382
383 static int __init fpga_mgr_class_init(void)
384 {
385         pr_info("FPGA manager framework\n");
386
387         fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
388         if (IS_ERR(fpga_mgr_class))
389                 return PTR_ERR(fpga_mgr_class);
390
391         fpga_mgr_class->dev_groups = fpga_mgr_groups;
392         fpga_mgr_class->dev_release = fpga_mgr_dev_release;
393
394         return 0;
395 }
396
397 static void __exit fpga_mgr_class_exit(void)
398 {
399         class_destroy(fpga_mgr_class);
400         ida_destroy(&fpga_mgr_ida);
401 }
402
403 MODULE_AUTHOR("Alan Tull <[email protected]>");
404 MODULE_DESCRIPTION("FPGA manager framework");
405 MODULE_LICENSE("GPL v2");
406
407 subsys_initcall(fpga_mgr_class_init);
408 module_exit(fpga_mgr_class_exit);
This page took 0.055194 seconds and 4 git commands to generate.