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>
28 #include <linux/scatterlist.h>
29 #include <linux/highmem.h>
31 static DEFINE_IDA(fpga_mgr_ida
);
32 static struct class *fpga_mgr_class
;
35 * Call the low level driver's write_init function. This will do the
36 * device-specific things to get the FPGA into the state where it is ready to
37 * receive an FPGA image. The low level driver only gets to see the first
38 * initial_header_size bytes in the buffer.
40 static int fpga_mgr_write_init_buf(struct fpga_manager
*mgr
,
41 struct fpga_image_info
*info
,
42 const char *buf
, size_t count
)
46 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT
;
47 if (!mgr
->mops
->initial_header_size
)
48 ret
= mgr
->mops
->write_init(mgr
, info
, NULL
, 0);
50 ret
= mgr
->mops
->write_init(
51 mgr
, info
, buf
, min(mgr
->mops
->initial_header_size
, count
));
54 dev_err(&mgr
->dev
, "Error preparing FPGA for writing\n");
55 mgr
->state
= FPGA_MGR_STATE_WRITE_INIT_ERR
;
62 static int fpga_mgr_write_init_sg(struct fpga_manager
*mgr
,
63 struct fpga_image_info
*info
,
66 struct sg_mapping_iter miter
;
71 if (!mgr
->mops
->initial_header_size
)
72 return fpga_mgr_write_init_buf(mgr
, info
, NULL
, 0);
75 * First try to use miter to map the first fragment to access the
76 * header, this is the typical path.
78 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
79 if (sg_miter_next(&miter
) &&
80 miter
.length
>= mgr
->mops
->initial_header_size
) {
81 ret
= fpga_mgr_write_init_buf(mgr
, info
, miter
.addr
,
83 sg_miter_stop(&miter
);
86 sg_miter_stop(&miter
);
88 /* Otherwise copy the fragments into temporary memory. */
89 buf
= kmalloc(mgr
->mops
->initial_header_size
, GFP_KERNEL
);
93 len
= sg_copy_to_buffer(sgt
->sgl
, sgt
->nents
, buf
,
94 mgr
->mops
->initial_header_size
);
95 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, len
);
103 * After all the FPGA image has been written, do the device specific steps to
104 * finish and set the FPGA into operating mode.
106 static int fpga_mgr_write_complete(struct fpga_manager
*mgr
,
107 struct fpga_image_info
*info
)
111 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE
;
112 ret
= mgr
->mops
->write_complete(mgr
, info
);
114 dev_err(&mgr
->dev
, "Error after writing image data to FPGA\n");
115 mgr
->state
= FPGA_MGR_STATE_WRITE_COMPLETE_ERR
;
118 mgr
->state
= FPGA_MGR_STATE_OPERATING
;
124 * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
126 * @info: fpga image specific information
127 * @sgt: scatterlist table
129 * Step the low level fpga manager through the device-specific steps of getting
130 * an FPGA ready to be configured, writing the image to it, then doing whatever
131 * post-configuration steps necessary. This code assumes the caller got the
132 * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
135 * This is the preferred entry point for FPGA programming, it does not require
136 * any contiguous kernel memory.
138 * Return: 0 on success, negative error code otherwise.
140 int fpga_mgr_buf_load_sg(struct fpga_manager
*mgr
, struct fpga_image_info
*info
,
141 struct sg_table
*sgt
)
145 ret
= fpga_mgr_write_init_sg(mgr
, info
, sgt
);
149 /* Write the FPGA image to the FPGA. */
150 mgr
->state
= FPGA_MGR_STATE_WRITE
;
151 if (mgr
->mops
->write_sg
) {
152 ret
= mgr
->mops
->write_sg(mgr
, sgt
);
154 struct sg_mapping_iter miter
;
156 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
157 while (sg_miter_next(&miter
)) {
158 ret
= mgr
->mops
->write(mgr
, miter
.addr
, miter
.length
);
162 sg_miter_stop(&miter
);
166 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
167 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
171 return fpga_mgr_write_complete(mgr
, info
);
173 EXPORT_SYMBOL_GPL(fpga_mgr_buf_load_sg
);
175 static int fpga_mgr_buf_load_mapped(struct fpga_manager
*mgr
,
176 struct fpga_image_info
*info
,
177 const char *buf
, size_t count
)
181 ret
= fpga_mgr_write_init_buf(mgr
, info
, buf
, count
);
186 * Write the FPGA image to the FPGA.
188 mgr
->state
= FPGA_MGR_STATE_WRITE
;
189 ret
= mgr
->mops
->write(mgr
, buf
, count
);
191 dev_err(&mgr
->dev
, "Error while writing image data to FPGA\n");
192 mgr
->state
= FPGA_MGR_STATE_WRITE_ERR
;
196 return fpga_mgr_write_complete(mgr
, info
);
200 * fpga_mgr_buf_load - load fpga from image in buffer
202 * @flags: flags setting fpga confuration modes
203 * @buf: buffer contain fpga image
204 * @count: byte count of buf
206 * Step the low level fpga manager through the device-specific steps of getting
207 * an FPGA ready to be configured, writing the image to it, then doing whatever
208 * post-configuration steps necessary. This code assumes the caller got the
209 * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
211 * Return: 0 on success, negative error code otherwise.
213 int fpga_mgr_buf_load(struct fpga_manager
*mgr
, struct fpga_image_info
*info
,
214 const char *buf
, size_t count
)
224 * This is just a fast path if the caller has already created a
225 * contiguous kernel buffer and the driver doesn't require SG, non-SG
226 * drivers will still work on the slow path.
228 if (mgr
->mops
->write
)
229 return fpga_mgr_buf_load_mapped(mgr
, info
, buf
, count
);
232 * Convert the linear kernel pointer into a sg_table of pages for use
235 nr_pages
= DIV_ROUND_UP((unsigned long)buf
+ count
, PAGE_SIZE
) -
236 (unsigned long)buf
/ PAGE_SIZE
;
237 pages
= kmalloc_array(nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
241 p
= buf
- offset_in_page(buf
);
242 for (index
= 0; index
< nr_pages
; index
++) {
243 if (is_vmalloc_addr(p
))
244 pages
[index
] = vmalloc_to_page(p
);
246 pages
[index
] = kmap_to_page((void *)p
);
255 * The temporary pages list is used to code share the merging algorithm
256 * in sg_alloc_table_from_pages
258 rc
= sg_alloc_table_from_pages(&sgt
, pages
, index
, offset_in_page(buf
),
264 rc
= fpga_mgr_buf_load_sg(mgr
, info
, &sgt
);
269 EXPORT_SYMBOL_GPL(fpga_mgr_buf_load
);
272 * fpga_mgr_firmware_load - request firmware and load to fpga
274 * @info: fpga image specific information
275 * @image_name: name of image file on the firmware search path
277 * Request an FPGA image using the firmware class, then write out to the FPGA.
278 * Update the state before each step to provide info on what step failed if
279 * there is a failure. This code assumes the caller got the mgr pointer
280 * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
283 * Return: 0 on success, negative error code otherwise.
285 int fpga_mgr_firmware_load(struct fpga_manager
*mgr
,
286 struct fpga_image_info
*info
,
287 const char *image_name
)
289 struct device
*dev
= &mgr
->dev
;
290 const struct firmware
*fw
;
293 dev_info(dev
, "writing %s to %s\n", image_name
, mgr
->name
);
295 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ
;
297 ret
= request_firmware(&fw
, image_name
, dev
);
299 mgr
->state
= FPGA_MGR_STATE_FIRMWARE_REQ_ERR
;
300 dev_err(dev
, "Error requesting firmware %s\n", image_name
);
304 ret
= fpga_mgr_buf_load(mgr
, info
, fw
->data
, fw
->size
);
306 release_firmware(fw
);
310 EXPORT_SYMBOL_GPL(fpga_mgr_firmware_load
);
312 static const char * const state_str
[] = {
313 [FPGA_MGR_STATE_UNKNOWN
] = "unknown",
314 [FPGA_MGR_STATE_POWER_OFF
] = "power off",
315 [FPGA_MGR_STATE_POWER_UP
] = "power up",
316 [FPGA_MGR_STATE_RESET
] = "reset",
318 /* requesting FPGA image from firmware */
319 [FPGA_MGR_STATE_FIRMWARE_REQ
] = "firmware request",
320 [FPGA_MGR_STATE_FIRMWARE_REQ_ERR
] = "firmware request error",
322 /* Preparing FPGA to receive image */
323 [FPGA_MGR_STATE_WRITE_INIT
] = "write init",
324 [FPGA_MGR_STATE_WRITE_INIT_ERR
] = "write init error",
326 /* Writing image to FPGA */
327 [FPGA_MGR_STATE_WRITE
] = "write",
328 [FPGA_MGR_STATE_WRITE_ERR
] = "write error",
330 /* Finishing configuration after image has been written */
331 [FPGA_MGR_STATE_WRITE_COMPLETE
] = "write complete",
332 [FPGA_MGR_STATE_WRITE_COMPLETE_ERR
] = "write complete error",
334 /* FPGA reports to be in normal operating mode */
335 [FPGA_MGR_STATE_OPERATING
] = "operating",
338 static ssize_t
name_show(struct device
*dev
,
339 struct device_attribute
*attr
, char *buf
)
341 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
343 return sprintf(buf
, "%s\n", mgr
->name
);
346 static ssize_t
state_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
351 return sprintf(buf
, "%s\n", state_str
[mgr
->state
]);
354 static DEVICE_ATTR_RO(name
);
355 static DEVICE_ATTR_RO(state
);
357 static struct attribute
*fpga_mgr_attrs
[] = {
359 &dev_attr_state
.attr
,
362 ATTRIBUTE_GROUPS(fpga_mgr
);
364 static struct fpga_manager
*__fpga_mgr_get(struct device
*dev
)
366 struct fpga_manager
*mgr
;
369 mgr
= to_fpga_manager(dev
);
373 /* Get exclusive use of fpga manager */
374 if (!mutex_trylock(&mgr
->ref_mutex
)) {
379 if (!try_module_get(dev
->parent
->driver
->owner
))
385 mutex_unlock(&mgr
->ref_mutex
);
391 static int fpga_mgr_dev_match(struct device
*dev
, const void *data
)
393 return dev
->parent
== data
;
397 * fpga_mgr_get - get an exclusive reference to a fpga mgr
398 * @dev: parent device that fpga mgr was registered with
400 * Given a device, get an exclusive reference to a fpga mgr.
402 * Return: fpga manager struct or IS_ERR() condition containing error code.
404 struct fpga_manager
*fpga_mgr_get(struct device
*dev
)
406 struct device
*mgr_dev
= class_find_device(fpga_mgr_class
, NULL
, dev
,
409 return ERR_PTR(-ENODEV
);
411 return __fpga_mgr_get(mgr_dev
);
413 EXPORT_SYMBOL_GPL(fpga_mgr_get
);
415 static int fpga_mgr_of_node_match(struct device
*dev
, const void *data
)
417 return dev
->of_node
== data
;
421 * of_fpga_mgr_get - get an exclusive reference to a fpga mgr
424 * Given a device node, get an exclusive reference to a fpga mgr.
426 * Return: fpga manager struct or IS_ERR() condition containing error code.
428 struct fpga_manager
*of_fpga_mgr_get(struct device_node
*node
)
432 dev
= class_find_device(fpga_mgr_class
, NULL
, node
,
433 fpga_mgr_of_node_match
);
435 return ERR_PTR(-ENODEV
);
437 return __fpga_mgr_get(dev
);
439 EXPORT_SYMBOL_GPL(of_fpga_mgr_get
);
442 * fpga_mgr_put - release a reference to a fpga manager
443 * @mgr: fpga manager structure
445 void fpga_mgr_put(struct fpga_manager
*mgr
)
447 module_put(mgr
->dev
.parent
->driver
->owner
);
448 mutex_unlock(&mgr
->ref_mutex
);
449 put_device(&mgr
->dev
);
451 EXPORT_SYMBOL_GPL(fpga_mgr_put
);
454 * fpga_mgr_register - register a low level fpga manager driver
455 * @dev: fpga manager device from pdev
456 * @name: fpga manager name
457 * @mops: pointer to structure of fpga manager ops
458 * @priv: fpga manager private data
460 * Return: 0 on success, negative error code otherwise.
462 int fpga_mgr_register(struct device
*dev
, const char *name
,
463 const struct fpga_manager_ops
*mops
,
466 struct fpga_manager
*mgr
;
469 if (!mops
|| !mops
->write_complete
|| !mops
->state
||
470 !mops
->write_init
|| (!mops
->write
&& !mops
->write_sg
) ||
471 (mops
->write
&& mops
->write_sg
)) {
472 dev_err(dev
, "Attempt to register without fpga_manager_ops\n");
476 if (!name
|| !strlen(name
)) {
477 dev_err(dev
, "Attempt to register with no name!\n");
481 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
485 id
= ida_simple_get(&fpga_mgr_ida
, 0, 0, GFP_KERNEL
);
491 mutex_init(&mgr
->ref_mutex
);
498 * Initialize framework state by requesting low level driver read state
499 * from device. FPGA may be in reset mode or may have been programmed
500 * by bootloader or EEPROM.
502 mgr
->state
= mgr
->mops
->state(mgr
);
504 device_initialize(&mgr
->dev
);
505 mgr
->dev
.class = fpga_mgr_class
;
506 mgr
->dev
.parent
= dev
;
507 mgr
->dev
.of_node
= dev
->of_node
;
509 dev_set_drvdata(dev
, mgr
);
511 ret
= dev_set_name(&mgr
->dev
, "fpga%d", id
);
515 ret
= device_add(&mgr
->dev
);
519 dev_info(&mgr
->dev
, "%s registered\n", mgr
->name
);
524 ida_simple_remove(&fpga_mgr_ida
, id
);
530 EXPORT_SYMBOL_GPL(fpga_mgr_register
);
533 * fpga_mgr_unregister - unregister a low level fpga manager driver
534 * @dev: fpga manager device from pdev
536 void fpga_mgr_unregister(struct device
*dev
)
538 struct fpga_manager
*mgr
= dev_get_drvdata(dev
);
540 dev_info(&mgr
->dev
, "%s %s\n", __func__
, mgr
->name
);
543 * If the low level driver provides a method for putting fpga into
544 * a desired state upon unregister, do it.
546 if (mgr
->mops
->fpga_remove
)
547 mgr
->mops
->fpga_remove(mgr
);
549 device_unregister(&mgr
->dev
);
551 EXPORT_SYMBOL_GPL(fpga_mgr_unregister
);
553 static void fpga_mgr_dev_release(struct device
*dev
)
555 struct fpga_manager
*mgr
= to_fpga_manager(dev
);
557 ida_simple_remove(&fpga_mgr_ida
, mgr
->dev
.id
);
561 static int __init
fpga_mgr_class_init(void)
563 pr_info("FPGA manager framework\n");
565 fpga_mgr_class
= class_create(THIS_MODULE
, "fpga_manager");
566 if (IS_ERR(fpga_mgr_class
))
567 return PTR_ERR(fpga_mgr_class
);
569 fpga_mgr_class
->dev_groups
= fpga_mgr_groups
;
570 fpga_mgr_class
->dev_release
= fpga_mgr_dev_release
;
575 static void __exit
fpga_mgr_class_exit(void)
577 class_destroy(fpga_mgr_class
);
578 ida_destroy(&fpga_mgr_ida
);
581 MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
582 MODULE_DESCRIPTION("FPGA manager framework");
583 MODULE_LICENSE("GPL v2");
585 subsys_initcall(fpga_mgr_class_init
);
586 module_exit(fpga_mgr_class_exit
);