add FPGA manager core
[firefly-linux-kernel-4.4.55.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  * @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  */
45 int 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 }
93 EXPORT_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  */
107 int 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 }
136 EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
137
138 static 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
164 static 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
172 static 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
180 static DEVICE_ATTR_RO(name);
181 static DEVICE_ATTR_RO(state);
182
183 static struct attribute *fpga_mgr_attrs[] = {
184         &dev_attr_name.attr,
185         &dev_attr_state.attr,
186         NULL,
187 };
188 ATTRIBUTE_GROUPS(fpga_mgr);
189
190 static 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  */
203 struct 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 }
232 EXPORT_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  */
238 void fpga_mgr_put(struct fpga_manager *mgr)
239 {
240         if (mgr) {
241                 module_put(THIS_MODULE);
242                 mutex_unlock(&mgr->ref_mutex);
243         }
244 }
245 EXPORT_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  */
256 int 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
319 error_device:
320         ida_simple_remove(&fpga_mgr_ida, id);
321 error_kfree:
322         kfree(mgr);
323
324         return ret;
325 }
326 EXPORT_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  */
332 void 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 }
347 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
348
349 static 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
357 static 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
371 static void __exit fpga_mgr_class_exit(void)
372 {
373         class_destroy(fpga_mgr_class);
374         ida_destroy(&fpga_mgr_ida);
375 }
376
377 MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
378 MODULE_DESCRIPTION("FPGA manager framework");
379 MODULE_LICENSE("GPL v2");
380
381 subsys_initcall(fpga_mgr_class_init);
382 module_exit(fpga_mgr_class_exit);