4 * Copyright (C) 2013-2015 Altera Corporation
6 * With code from the mailing list:
7 * Copyright (C) 2013 Xilinx, Inc.
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.
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
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/>.
21 #include <linux/firmware.h>
22 #include <linux/fpga/fpga-mgr.h>
23 #include <linux/idr.h>
24 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/slab.h>
29 static DEFINE_IDA(fpga_mgr_ida);
30 static struct class *fpga_mgr_class;
33 * fpga_mgr_buf_load - load fpga from image in buffer
35 * @info: fpga image specific information
36 * @buf: buffer contain fpga image
37 * @count: byte count of buf
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
45 * Return: 0 on success, negative error code otherwise.
47 int fpga_mgr_buf_load(struct fpga_manager *mgr, struct fpga_image_info *info,
48 const char *buf, size_t count)
50 struct device *dev = &mgr->dev;
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.
58 mgr->state = FPGA_MGR_STATE_WRITE_INIT;
59 ret = mgr->mops->write_init(mgr, info, buf, count);
61 dev_err(dev, "Error preparing FPGA for writing\n");
62 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
67 * Write the FPGA image to the FPGA.
69 mgr->state = FPGA_MGR_STATE_WRITE;
70 ret = mgr->mops->write(mgr, buf, count);
72 dev_err(dev, "Error while writing image data to FPGA\n");
73 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
78 * After all the FPGA image has been written, do the device specific
79 * steps to finish and set the FPGA into operating mode.
81 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
82 ret = mgr->mops->write_complete(mgr, info);
84 dev_err(dev, "Error after writing image data to FPGA\n");
85 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
88 mgr->state = FPGA_MGR_STATE_OPERATING;
92 EXPORT_SYMBOL_GPL(fpga_mgr_buf_load);
95 * fpga_mgr_firmware_load - request firmware and load to fpga
97 * @info: fpga image specific information
98 * @image_name: name of image file on the firmware search path
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
106 * Return: 0 on success, negative error code otherwise.
108 int fpga_mgr_firmware_load(struct fpga_manager *mgr,
109 struct fpga_image_info *info,
110 const char *image_name)
112 struct device *dev = &mgr->dev;
113 const struct firmware *fw;
116 dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
118 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
120 ret = request_firmware(&fw, image_name, dev);
122 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
123 dev_err(dev, "Error requesting firmware %s\n", image_name);
127 ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
129 release_firmware(fw);
133 EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load);
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",
141 /* requesting FPGA image from firmware */
142 [FPGA_MGR_STATE_FIRMWARE_REQ] = "firmware request",
143 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
145 /* Preparing FPGA to receive image */
146 [FPGA_MGR_STATE_WRITE_INIT] = "write init",
147 [FPGA_MGR_STATE_WRITE_INIT_ERR] = "write init error",
149 /* Writing image to FPGA */
150 [FPGA_MGR_STATE_WRITE] = "write",
151 [FPGA_MGR_STATE_WRITE_ERR] = "write error",
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",
157 /* FPGA reports to be in normal operating mode */
158 [FPGA_MGR_STATE_OPERATING] = "operating",
161 static ssize_t name_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
164 struct fpga_manager *mgr = to_fpga_manager(dev);
166 return sprintf(buf, "%s\n", mgr->name);
169 static ssize_t state_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct fpga_manager *mgr = to_fpga_manager(dev);
174 return sprintf(buf, "%s\n", state_str[mgr->state]);
177 static DEVICE_ATTR_RO(name);
178 static DEVICE_ATTR_RO(state);
180 static struct attribute *fpga_mgr_attrs[] = {
182 &dev_attr_state.attr,
185 ATTRIBUTE_GROUPS(fpga_mgr);
187 struct fpga_manager *__fpga_mgr_get(struct device *dev)
189 struct fpga_manager *mgr;
192 mgr = to_fpga_manager(dev);
196 /* Get exclusive use of fpga manager */
197 if (!mutex_trylock(&mgr->ref_mutex)) {
202 if (!try_module_get(dev->parent->driver->owner))
208 mutex_unlock(&mgr->ref_mutex);
214 static int fpga_mgr_dev_match(struct device *dev, const void *data)
216 return dev->parent == data;
220 * fpga_mgr_get - get an exclusive reference to a fpga mgr
221 * @dev: parent device that fpga mgr was registered with
223 * Given a device, get an exclusive reference to a fpga mgr.
225 * Return: fpga manager struct or IS_ERR() condition containing error code.
227 struct fpga_manager *fpga_mgr_get(struct device *dev)
229 struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
232 return ERR_PTR(-ENODEV);
234 return __fpga_mgr_get(mgr_dev);
236 EXPORT_SYMBOL_GPL(fpga_mgr_get);
238 static int fpga_mgr_of_node_match(struct device *dev, const void *data)
240 return dev->of_node == data;
244 * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
247 * Given a device node, get an exclusive reference to a fpga mgr.
249 * Return: fpga manager struct or IS_ERR() condition containing error code.
251 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
255 dev = class_find_device(fpga_mgr_class, NULL, node,
256 fpga_mgr_of_node_match);
258 return ERR_PTR(-ENODEV);
260 return __fpga_mgr_get(dev);
262 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
265 * fpga_mgr_put - release a reference to a fpga manager
266 * @mgr: fpga manager structure
268 void fpga_mgr_put(struct fpga_manager *mgr)
270 module_put(mgr->dev.parent->driver->owner);
271 mutex_unlock(&mgr->ref_mutex);
272 put_device(&mgr->dev);
274 EXPORT_SYMBOL_GPL(fpga_mgr_put);
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
283 * Return: 0 on success, negative error code otherwise.
285 int fpga_mgr_register(struct device *dev, const char *name,
286 const struct fpga_manager_ops *mops,
289 struct fpga_manager *mgr;
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");
298 if (!name || !strlen(name)) {
299 dev_err(dev, "Attempt to register with no name!\n");
303 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
307 id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
313 mutex_init(&mgr->ref_mutex);
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.
324 mgr->state = mgr->mops->state(mgr);
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;
331 dev_set_drvdata(dev, mgr);
333 ret = dev_set_name(&mgr->dev, "fpga%d", id);
337 ret = device_add(&mgr->dev);
341 dev_info(&mgr->dev, "%s registered\n", mgr->name);
346 ida_simple_remove(&fpga_mgr_ida, id);
352 EXPORT_SYMBOL_GPL(fpga_mgr_register);
355 * fpga_mgr_unregister - unregister a low level fpga manager driver
356 * @dev: fpga manager device from pdev
358 void fpga_mgr_unregister(struct device *dev)
360 struct fpga_manager *mgr = dev_get_drvdata(dev);
362 dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
365 * If the low level driver provides a method for putting fpga into
366 * a desired state upon unregister, do it.
368 if (mgr->mops->fpga_remove)
369 mgr->mops->fpga_remove(mgr);
371 device_unregister(&mgr->dev);
373 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
375 static void fpga_mgr_dev_release(struct device *dev)
377 struct fpga_manager *mgr = to_fpga_manager(dev);
379 ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
383 static int __init fpga_mgr_class_init(void)
385 pr_info("FPGA manager framework\n");
387 fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
388 if (IS_ERR(fpga_mgr_class))
389 return PTR_ERR(fpga_mgr_class);
391 fpga_mgr_class->dev_groups = fpga_mgr_groups;
392 fpga_mgr_class->dev_release = fpga_mgr_dev_release;
397 static void __exit fpga_mgr_class_exit(void)
399 class_destroy(fpga_mgr_class);
400 ida_destroy(&fpga_mgr_ida);
404 MODULE_DESCRIPTION("FPGA manager framework");
405 MODULE_LICENSE("GPL v2");
407 subsys_initcall(fpga_mgr_class_init);
408 module_exit(fpga_mgr_class_exit);