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. The low level driver only gets to
57 * see the first initial_header_size bytes in the buffer.
59 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT
;
60 ret
= mgr
->mops
->write_init(mgr
, info
, buf
,
61 min(mgr
->mops
->initial_header_size
, count
));
63 dev_err(dev
, "Error preparing FPGA for writing\n");
64 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT_ERR
;
69 * Write the FPGA image to the FPGA.
71 mgr
->state
= FPGA_MGR_STATE_WRITE
;
72 ret
= mgr
->mops
->write(mgr
, buf
, count
);
74 dev_err(dev
, "Error while writing image data to FPGA\n");
75 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
80 * After all the FPGA image has been written, do the device specific
81 * steps to finish and set the FPGA into operating mode.
83 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE
;
84 ret
= mgr
->mops
->write_complete(mgr
, info
);
86 dev_err(dev
, "Error after writing image data to FPGA\n");
87 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE_ERR
;
90 mgr
->state
= FPGA_MGR_STATE_OPERATING
;
94 EXPORT_SYMBOL_GPL(fpga_mgr_buf_load
);
97 * fpga_mgr_firmware_load - request firmware and load to fpga
99 * @info: fpga image specific information
100 * @image_name: name of image file on the firmware search path
102 * Request an FPGA image using the firmware class, then write out to the FPGA.
103 * Update the state before each step to provide info on what step failed if
104 * there is a failure. This code assumes the caller got the mgr pointer
105 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
108 * Return: 0 on success, negative error code otherwise.
110 int fpga_mgr_firmware_load(struct fpga_manager
*mgr
,
111 struct fpga_image_info
*info
,
112 const char *image_name
)
114 struct device
*dev
= &mgr
->dev
;
115 const struct firmware
*fw
;
118 dev_info(dev
, "writing %s to %s\n", image_name
, mgr
->name
);
120 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ
;
122 ret
= request_firmware(&fw
, image_name
, dev
);
124 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ_ERR
;
125 dev_err(dev
, "Error requesting firmware %s\n", image_name
);
129 ret
= fpga_mgr_buf_load(mgr
, info
, fw
->data
, fw
->size
);
131 release_firmware(fw
);
135 EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load
);
137 static const char * const state_str
[] = {
138 [FPGA_MGR_STATE_UNKNOWN
] = "unknown",
139 [FPGA_MGR_STATE_POWER_OFF
] = "power off",
140 [FPGA_MGR_STATE_POWER_UP
] = "power up",
141 [FPGA_MGR_STATE_RESET
] = "reset",
143 /* requesting FPGA image from firmware */
144 [FPGA_MGR_STATE_FIRMWARE_REQ
] = "firmware request",
145 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR
] = "firmware request error",
147 /* Preparing FPGA to receive image */
148 [FPGA_MGR_STATE_WRITE_INIT
] = "write init",
149 [FPGA_MGR_STATE_WRITE_INIT_ERR
] = "write init error",
151 /* Writing image to FPGA */
152 [FPGA_MGR_STATE_WRITE
] = "write",
153 [FPGA_MGR_STATE_WRITE_ERR
] = "write error",
155 /* Finishing configuration after image has been written */
156 [FPGA_MGR_STATE_WRITE_COMPLETE
] = "write complete",
157 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR
] = "write complete error",
159 /* FPGA reports to be in normal operating mode */
160 [FPGA_MGR_STATE_OPERATING
] = "operating",
163 static ssize_t
name_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
168 return sprintf(buf
, "%s\n", mgr
->name
);
171 static ssize_t
state_show(struct device
*dev
,
172 struct device_attribute
*attr
, char *buf
)
174 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
176 return sprintf(buf
, "%s\n", state_str
[mgr
->state
]);
179 static DEVICE_ATTR_RO(name
);
180 static DEVICE_ATTR_RO(state
);
182 static struct attribute
*fpga_mgr_attrs
[] = {
184 &dev_attr_state
.attr
,
187 ATTRIBUTE_GROUPS(fpga_mgr
);
189 struct fpga_manager
*__fpga_mgr_get(struct device
*dev
)
191 struct fpga_manager
*mgr
;
194 mgr
= to_fpga_manager(dev
);
198 /* Get exclusive use of fpga manager */
199 if (!mutex_trylock(&mgr
->ref_mutex
)) {
204 if (!try_module_get(dev
->parent
->driver
->owner
))
210 mutex_unlock(&mgr
->ref_mutex
);
216 static int fpga_mgr_dev_match(struct device
*dev
, const void *data
)
218 return dev
->parent
== data
;
222 * fpga_mgr_get - get an exclusive reference to a fpga mgr
223 * @dev: parent device that fpga mgr was registered with
225 * Given a device, get an exclusive reference to a fpga mgr.
227 * Return: fpga manager struct or IS_ERR() condition containing error code.
229 struct fpga_manager
*fpga_mgr_get(struct device
*dev
)
231 struct device
*mgr_dev
= class_find_device(fpga_mgr_class
, NULL
, dev
,
234 return ERR_PTR(-ENODEV
);
236 return __fpga_mgr_get(mgr_dev
);
238 EXPORT_SYMBOL_GPL(fpga_mgr_get
);
240 static int fpga_mgr_of_node_match(struct device
*dev
, const void *data
)
242 return dev
->of_node
== data
;
246 * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
249 * Given a device node, get an exclusive reference to a fpga mgr.
251 * Return: fpga manager struct or IS_ERR() condition containing error code.
253 struct fpga_manager
*of_fpga_mgr_get(struct device_node
*node
)
257 dev
= class_find_device(fpga_mgr_class
, NULL
, node
,
258 fpga_mgr_of_node_match
);
260 return ERR_PTR(-ENODEV
);
262 return __fpga_mgr_get(dev
);
264 EXPORT_SYMBOL_GPL(of_fpga_mgr_get
);
267 * fpga_mgr_put - release a reference to a fpga manager
268 * @mgr: fpga manager structure
270 void fpga_mgr_put(struct fpga_manager
*mgr
)
272 module_put(mgr
->dev
.parent
->driver
->owner
);
273 mutex_unlock(&mgr
->ref_mutex
);
274 put_device(&mgr
->dev
);
276 EXPORT_SYMBOL_GPL(fpga_mgr_put
);
279 * fpga_mgr_register - register a low level fpga manager driver
280 * @dev: fpga manager device from pdev
281 * @name: fpga manager name
282 * @mops: pointer to structure of fpga manager ops
283 * @priv: fpga manager private data
285 * Return: 0 on success, negative error code otherwise.
287 int fpga_mgr_register(struct device
*dev
, const char *name
,
288 const struct fpga_manager_ops
*mops
,
291 struct fpga_manager
*mgr
;
294 if (!mops
|| !mops
->write_init
|| !mops
->write
||
295 !mops
->write_complete
|| !mops
->state
) {
296 dev_err(dev
, "Attempt to register without fpga_manager_ops\n");
300 if (!name
|| !strlen(name
)) {
301 dev_err(dev
, "Attempt to register with no name!\n");
305 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
309 id
= ida_simple_get(&fpga_mgr_ida
, 0, 0, GFP_KERNEL
);
315 mutex_init(&mgr
->ref_mutex
);
322 * Initialize framework state by requesting low level driver read state
323 * from device. FPGA may be in reset mode or may have been programmed
324 * by bootloader or EEPROM.
326 mgr
->state
= mgr
->mops
->state(mgr
);
328 device_initialize(&mgr
->dev
);
329 mgr
->dev
.class = fpga_mgr_class
;
330 mgr
->dev
.parent
= dev
;
331 mgr
->dev
.of_node
= dev
->of_node
;
333 dev_set_drvdata(dev
, mgr
);
335 ret
= dev_set_name(&mgr
->dev
, "fpga%d", id
);
339 ret
= device_add(&mgr
->dev
);
343 dev_info(&mgr
->dev
, "%s registered\n", mgr
->name
);
348 ida_simple_remove(&fpga_mgr_ida
, id
);
354 EXPORT_SYMBOL_GPL(fpga_mgr_register
);
357 * fpga_mgr_unregister - unregister a low level fpga manager driver
358 * @dev: fpga manager device from pdev
360 void fpga_mgr_unregister(struct device
*dev
)
362 struct fpga_manager
*mgr
= dev_get_drvdata(dev
);
364 dev_info(&mgr
->dev
, "%s %s\n", __func__
, mgr
->name
);
367 * If the low level driver provides a method for putting fpga into
368 * a desired state upon unregister, do it.
370 if (mgr
->mops
->fpga_remove
)
371 mgr
->mops
->fpga_remove(mgr
);
373 device_unregister(&mgr
->dev
);
375 EXPORT_SYMBOL_GPL(fpga_mgr_unregister
);
377 static void fpga_mgr_dev_release(struct device
*dev
)
379 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
381 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
385 static int __init
fpga_mgr_class_init(void)
387 pr_info("FPGA manager framework\n");
389 fpga_mgr_class
= class_create(THIS_MODULE
, "fpga_manager");
390 if (IS_ERR(fpga_mgr_class
))
391 return PTR_ERR(fpga_mgr_class
);
393 fpga_mgr_class
->dev_groups
= fpga_mgr_groups
;
394 fpga_mgr_class
->dev_release
= fpga_mgr_dev_release
;
399 static void __exit
fpga_mgr_class_exit(void)
401 class_destroy(fpga_mgr_class
);
402 ida_destroy(&fpga_mgr_ida
);
405 MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
406 MODULE_DESCRIPTION("FPGA manager framework");
407 MODULE_LICENSE("GPL v2");
409 subsys_initcall(fpga_mgr_class_init
);
410 module_exit(fpga_mgr_class_exit
);