1 /*****************************************************************************
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
11 * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
12 * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
13 * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
14 * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
15 * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
16 * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
17 * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
18 * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
19 * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
20 * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
21 * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE.
24 * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group
25 * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group
26 * (c) Copyright 2007-2008 Xilinx Inc.
27 * All rights reserved.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 *****************************************************************************/
36 * This is the code behind /dev/icap* -- it allows a user-space
37 * application to use the Xilinx ICAP subsystem.
39 * The following operations are possible:
41 * open open the port and initialize for access.
42 * release release port
43 * write Write a bitstream to the configuration processor.
44 * read Read a data stream from the configuration processor.
46 * After being opened, the port is initialized and accessed to avoid a
47 * corrupted first read which may occur with some hardware. The port
48 * is left in a desynched state, requiring that a synch sequence be
49 * transmitted before any valid configuration data. A user will have
50 * exclusive access to the device while it remains open, and the state
51 * of the ICAP cannot be guaranteed after the device is closed. Note
52 * that a complete reset of the core and the state of the ICAP cannot
53 * be performed on many versions of the cores, hence users of this
54 * device should avoid making inconsistent accesses to the device. In
55 * particular, accessing the read interface, without first generating
56 * a write containing a readback packet can leave the ICAP in an
59 * Note that in order to use the read interface, it is first necessary
60 * to write a request packet to the write interface. i.e., it is not
61 * possible to simply readback the bitstream (or any configuration
62 * bits) from a device without specifically requesting them first.
63 * The code to craft such packets is intended to be part of the
64 * user-space application code that uses this device. The simplest
65 * way to use this interface is simply:
67 * cp foo.bit /dev/icap0
69 * Note that unless foo.bit is an appropriately constructed partial
70 * bitstream, this has a high likelihood of overwriting the design
71 * currently programmed in the FPGA.
74 #include <linux/module.h>
75 #include <linux/kernel.h>
76 #include <linux/types.h>
77 #include <linux/ioport.h>
78 #include <linux/interrupt.h>
79 #include <linux/fcntl.h>
80 #include <linux/init.h>
81 #include <linux/poll.h>
82 #include <linux/proc_fs.h>
83 #include <linux/mutex.h>
84 #include <linux/sysctl.h>
86 #include <linux/cdev.h>
87 #include <linux/platform_device.h>
88 #include <linux/slab.h>
90 #include <linux/uaccess.h>
93 /* For open firmware. */
94 #include <linux/of_address.h>
95 #include <linux/of_device.h>
96 #include <linux/of_platform.h>
99 #include "xilinx_hwicap.h"
100 #include "buffer_icap.h"
101 #include "fifo_icap.h"
103 #define DRIVER_NAME "icap"
105 #define HWICAP_REGS (0x10000)
107 #define XHWICAP_MAJOR 259
108 #define XHWICAP_MINOR 0
109 #define HWICAP_DEVICES 1
111 /* An array, which is set to true when the device is registered. */
112 static DEFINE_MUTEX(hwicap_mutex
);
113 static bool probed_devices
[HWICAP_DEVICES
];
114 static struct mutex icap_sem
;
116 static struct class *icap_class
;
118 #define UNIMPLEMENTED 0xFFFF
120 static const struct config_registers v2_config_registers
= {
136 .AXSS
= UNIMPLEMENTED
,
137 .C0R_1
= UNIMPLEMENTED
,
138 .CSOB
= UNIMPLEMENTED
,
139 .WBSTAR
= UNIMPLEMENTED
,
140 .TIMER
= UNIMPLEMENTED
,
141 .BOOTSTS
= UNIMPLEMENTED
,
142 .CTL_1
= UNIMPLEMENTED
,
145 static const struct config_registers v4_config_registers
= {
157 .FLR
= UNIMPLEMENTED
,
158 .KEY
= UNIMPLEMENTED
,
162 .C0R_1
= UNIMPLEMENTED
,
163 .CSOB
= UNIMPLEMENTED
,
164 .WBSTAR
= UNIMPLEMENTED
,
165 .TIMER
= UNIMPLEMENTED
,
166 .BOOTSTS
= UNIMPLEMENTED
,
167 .CTL_1
= UNIMPLEMENTED
,
170 static const struct config_registers v5_config_registers
= {
182 .FLR
= UNIMPLEMENTED
,
183 .KEY
= UNIMPLEMENTED
,
195 static const struct config_registers v6_config_registers
= {
207 .FLR
= UNIMPLEMENTED
,
208 .KEY
= UNIMPLEMENTED
,
221 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
222 * @drvdata: a pointer to the drvdata.
224 * Returns: '0' on success and failure value on error
226 * This command desynchronizes the ICAP After this command, a
227 * bitstream containing a NULL packet, followed by a SYNCH packet is
228 * required before the ICAP will recognize commands.
230 static int hwicap_command_desync(struct hwicap_drvdata
*drvdata
)
236 * Create the data to be written to the ICAP.
238 buffer
[index
++] = hwicap_type_1_write(drvdata
->config_regs
->CMD
) | 1;
239 buffer
[index
++] = XHI_CMD_DESYNCH
;
240 buffer
[index
++] = XHI_NOOP_PACKET
;
241 buffer
[index
++] = XHI_NOOP_PACKET
;
244 * Write the data to the FIFO and intiate the transfer of data present
245 * in the FIFO to the ICAP device.
247 return drvdata
->config
->set_configuration(drvdata
,
252 * hwicap_get_configuration_register - Query a configuration register.
253 * @drvdata: a pointer to the drvdata.
254 * @reg: a constant which represents the configuration
255 * register value to be returned.
256 * Examples: XHI_IDCODE, XHI_FLR.
257 * @reg_data: returns the value of the register.
259 * Returns: '0' on success and failure value on error
261 * Sends a query packet to the ICAP and then receives the response.
262 * The icap is left in Synched state.
264 static int hwicap_get_configuration_register(struct hwicap_drvdata
*drvdata
,
265 u32 reg
, u32
*reg_data
)
272 * Create the data to be written to the ICAP.
274 buffer
[index
++] = XHI_DUMMY_PACKET
;
275 buffer
[index
++] = XHI_NOOP_PACKET
;
276 buffer
[index
++] = XHI_SYNC_PACKET
;
277 buffer
[index
++] = XHI_NOOP_PACKET
;
278 buffer
[index
++] = XHI_NOOP_PACKET
;
281 * Write the data to the FIFO and initiate the transfer of data present
282 * in the FIFO to the ICAP device.
284 status
= drvdata
->config
->set_configuration(drvdata
,
289 /* If the syncword was not found, then we need to start over. */
290 status
= drvdata
->config
->get_status(drvdata
);
291 if ((status
& XHI_SR_DALIGN_MASK
) != XHI_SR_DALIGN_MASK
)
295 buffer
[index
++] = hwicap_type_1_read(reg
) | 1;
296 buffer
[index
++] = XHI_NOOP_PACKET
;
297 buffer
[index
++] = XHI_NOOP_PACKET
;
300 * Write the data to the FIFO and intiate the transfer of data present
301 * in the FIFO to the ICAP device.
303 status
= drvdata
->config
->set_configuration(drvdata
,
309 * Read the configuration register
311 status
= drvdata
->config
->get_configuration(drvdata
, reg_data
, 1);
318 static int hwicap_initialize_hwicap(struct hwicap_drvdata
*drvdata
)
323 dev_dbg(drvdata
->dev
, "initializing\n");
325 /* Abort any current transaction, to make sure we have the
326 * ICAP in a good state.
328 dev_dbg(drvdata
->dev
, "Reset...\n");
329 drvdata
->config
->reset(drvdata
);
331 dev_dbg(drvdata
->dev
, "Desync...\n");
332 status
= hwicap_command_desync(drvdata
);
336 /* Attempt to read the IDCODE from ICAP. This
337 * may not be returned correctly, due to the design of the
340 dev_dbg(drvdata
->dev
, "Reading IDCODE...\n");
341 status
= hwicap_get_configuration_register(
342 drvdata
, drvdata
->config_regs
->IDCODE
, &idcode
);
343 dev_dbg(drvdata
->dev
, "IDCODE = %x\n", idcode
);
347 dev_dbg(drvdata
->dev
, "Desync...\n");
348 status
= hwicap_command_desync(drvdata
);
356 hwicap_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
358 struct hwicap_drvdata
*drvdata
= file
->private_data
;
359 ssize_t bytes_to_read
= 0;
365 status
= mutex_lock_interruptible(&drvdata
->sem
);
369 if (drvdata
->read_buffer_in_use
) {
370 /* If there are leftover bytes in the buffer, just */
371 /* return them and don't try to read more from the */
374 (count
< drvdata
->read_buffer_in_use
) ? count
:
375 drvdata
->read_buffer_in_use
;
377 /* Return the data currently in the read buffer. */
378 if (copy_to_user(buf
, drvdata
->read_buffer
, bytes_to_read
)) {
382 drvdata
->read_buffer_in_use
-= bytes_to_read
;
383 memmove(drvdata
->read_buffer
,
384 drvdata
->read_buffer
+ bytes_to_read
,
387 /* Get new data from the ICAP, and return was was requested. */
388 kbuf
= (u32
*) get_zeroed_page(GFP_KERNEL
);
394 /* The ICAP device is only able to read complete */
395 /* words. If a number of bytes that do not correspond */
396 /* to complete words is requested, then we read enough */
397 /* words to get the required number of bytes, and then */
398 /* save the remaining bytes for the next read. */
400 /* Determine the number of words to read, rounding up */
402 words
= ((count
+ 3) >> 2);
403 bytes_to_read
= words
<< 2;
405 if (bytes_to_read
> PAGE_SIZE
)
406 bytes_to_read
= PAGE_SIZE
;
408 /* Ensure we only read a complete number of words. */
409 bytes_remaining
= bytes_to_read
& 3;
411 words
= bytes_to_read
>> 2;
413 status
= drvdata
->config
->get_configuration(drvdata
,
416 /* If we didn't read correctly, then bail out. */
418 free_page((unsigned long)kbuf
);
422 /* If we fail to return the data to the user, then bail out. */
423 if (copy_to_user(buf
, kbuf
, bytes_to_read
)) {
424 free_page((unsigned long)kbuf
);
428 memcpy(drvdata
->read_buffer
,
431 drvdata
->read_buffer_in_use
= bytes_remaining
;
432 free_page((unsigned long)kbuf
);
434 status
= bytes_to_read
;
436 mutex_unlock(&drvdata
->sem
);
441 hwicap_write(struct file
*file
, const char __user
*buf
,
442 size_t count
, loff_t
*ppos
)
444 struct hwicap_drvdata
*drvdata
= file
->private_data
;
446 ssize_t left
= count
;
451 status
= mutex_lock_interruptible(&drvdata
->sem
);
455 left
+= drvdata
->write_buffer_in_use
;
457 /* Only write multiples of 4 bytes. */
463 kbuf
= (u32
*) __get_free_page(GFP_KERNEL
);
470 /* only write multiples of 4 bytes, so there might */
471 /* be as many as 3 bytes left (at the end). */
478 if (drvdata
->write_buffer_in_use
) {
479 memcpy(kbuf
, drvdata
->write_buffer
,
480 drvdata
->write_buffer_in_use
);
482 (((char *)kbuf
) + drvdata
->write_buffer_in_use
),
484 len
- (drvdata
->write_buffer_in_use
))) {
485 free_page((unsigned long)kbuf
);
490 if (copy_from_user(kbuf
, buf
+ written
, len
)) {
491 free_page((unsigned long)kbuf
);
497 status
= drvdata
->config
->set_configuration(drvdata
,
501 free_page((unsigned long)kbuf
);
505 if (drvdata
->write_buffer_in_use
) {
506 len
-= drvdata
->write_buffer_in_use
;
507 left
-= drvdata
->write_buffer_in_use
;
508 drvdata
->write_buffer_in_use
= 0;
513 if ((left
> 0) && (left
< 4)) {
514 if (!copy_from_user(drvdata
->write_buffer
,
515 buf
+ written
, left
)) {
516 drvdata
->write_buffer_in_use
= left
;
522 free_page((unsigned long)kbuf
);
525 mutex_unlock(&drvdata
->sem
);
529 static int hwicap_open(struct inode
*inode
, struct file
*file
)
531 struct hwicap_drvdata
*drvdata
;
534 mutex_lock(&hwicap_mutex
);
535 drvdata
= container_of(inode
->i_cdev
, struct hwicap_drvdata
, cdev
);
537 status
= mutex_lock_interruptible(&drvdata
->sem
);
541 if (drvdata
->is_open
) {
546 status
= hwicap_initialize_hwicap(drvdata
);
548 dev_err(drvdata
->dev
, "Failed to open file");
552 file
->private_data
= drvdata
;
553 drvdata
->write_buffer_in_use
= 0;
554 drvdata
->read_buffer_in_use
= 0;
555 drvdata
->is_open
= 1;
558 mutex_unlock(&drvdata
->sem
);
560 mutex_unlock(&hwicap_mutex
);
564 static int hwicap_release(struct inode
*inode
, struct file
*file
)
566 struct hwicap_drvdata
*drvdata
= file
->private_data
;
570 mutex_lock(&drvdata
->sem
);
572 if (drvdata
->write_buffer_in_use
) {
573 /* Flush write buffer. */
574 for (i
= drvdata
->write_buffer_in_use
; i
< 4; i
++)
575 drvdata
->write_buffer
[i
] = 0;
577 status
= drvdata
->config
->set_configuration(drvdata
,
578 (u32
*) drvdata
->write_buffer
, 1);
583 status
= hwicap_command_desync(drvdata
);
588 drvdata
->is_open
= 0;
589 mutex_unlock(&drvdata
->sem
);
593 static const struct file_operations hwicap_fops
= {
594 .owner
= THIS_MODULE
,
595 .write
= hwicap_write
,
598 .release
= hwicap_release
,
599 .llseek
= noop_llseek
,
602 static int hwicap_setup(struct device
*dev
, int id
,
603 const struct resource
*regs_res
,
604 const struct hwicap_driver_config
*config
,
605 const struct config_registers
*config_regs
)
608 struct hwicap_drvdata
*drvdata
= NULL
;
611 dev_info(dev
, "Xilinx icap port driver\n");
613 mutex_lock(&icap_sem
);
616 for (id
= 0; id
< HWICAP_DEVICES
; id
++)
617 if (!probed_devices
[id
])
620 if (id
< 0 || id
>= HWICAP_DEVICES
) {
621 mutex_unlock(&icap_sem
);
622 dev_err(dev
, "%s%i too large\n", DRIVER_NAME
, id
);
625 if (probed_devices
[id
]) {
626 mutex_unlock(&icap_sem
);
627 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
632 probed_devices
[id
] = 1;
633 mutex_unlock(&icap_sem
);
635 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
+ id
);
637 drvdata
= kzalloc(sizeof(struct hwicap_drvdata
), GFP_KERNEL
);
642 dev_set_drvdata(dev
, (void *)drvdata
);
645 dev_err(dev
, "Couldn't get registers resource\n");
650 drvdata
->mem_start
= regs_res
->start
;
651 drvdata
->mem_end
= regs_res
->end
;
652 drvdata
->mem_size
= resource_size(regs_res
);
654 if (!request_mem_region(drvdata
->mem_start
,
655 drvdata
->mem_size
, DRIVER_NAME
)) {
656 dev_err(dev
, "Couldn't lock memory region at %Lx\n",
657 (unsigned long long) regs_res
->start
);
662 drvdata
->devt
= devt
;
664 drvdata
->base_address
= ioremap(drvdata
->mem_start
, drvdata
->mem_size
);
665 if (!drvdata
->base_address
) {
666 dev_err(dev
, "ioremap() failed\n");
671 drvdata
->config
= config
;
672 drvdata
->config_regs
= config_regs
;
674 mutex_init(&drvdata
->sem
);
675 drvdata
->is_open
= 0;
677 dev_info(dev
, "ioremap %llx to %p with size %llx\n",
678 (unsigned long long) drvdata
->mem_start
,
679 drvdata
->base_address
,
680 (unsigned long long) drvdata
->mem_size
);
682 cdev_init(&drvdata
->cdev
, &hwicap_fops
);
683 drvdata
->cdev
.owner
= THIS_MODULE
;
684 retval
= cdev_add(&drvdata
->cdev
, devt
, 1);
686 dev_err(dev
, "cdev_add() failed\n");
690 device_create(icap_class
, dev
, devt
, NULL
, "%s%d", DRIVER_NAME
, id
);
691 return 0; /* success */
694 iounmap(drvdata
->base_address
);
697 release_mem_region(regs_res
->start
, drvdata
->mem_size
);
703 mutex_lock(&icap_sem
);
704 probed_devices
[id
] = 0;
705 mutex_unlock(&icap_sem
);
710 static struct hwicap_driver_config buffer_icap_config
= {
711 .get_configuration
= buffer_icap_get_configuration
,
712 .set_configuration
= buffer_icap_set_configuration
,
713 .get_status
= buffer_icap_get_status
,
714 .reset
= buffer_icap_reset
,
717 static struct hwicap_driver_config fifo_icap_config
= {
718 .get_configuration
= fifo_icap_get_configuration
,
719 .set_configuration
= fifo_icap_set_configuration
,
720 .get_status
= fifo_icap_get_status
,
721 .reset
= fifo_icap_reset
,
724 static int hwicap_remove(struct device
*dev
)
726 struct hwicap_drvdata
*drvdata
;
728 drvdata
= dev_get_drvdata(dev
);
733 device_destroy(icap_class
, drvdata
->devt
);
734 cdev_del(&drvdata
->cdev
);
735 iounmap(drvdata
->base_address
);
736 release_mem_region(drvdata
->mem_start
, drvdata
->mem_size
);
739 mutex_lock(&icap_sem
);
740 probed_devices
[MINOR(dev
->devt
)-XHWICAP_MINOR
] = 0;
741 mutex_unlock(&icap_sem
);
742 return 0; /* success */
746 static int hwicap_of_probe(struct platform_device
*op
,
747 const struct hwicap_driver_config
*config
)
750 const unsigned int *id
;
753 const struct config_registers
*regs
;
756 rc
= of_address_to_resource(op
->dev
.of_node
, 0, &res
);
758 dev_err(&op
->dev
, "invalid address\n");
762 id
= of_get_property(op
->dev
.of_node
, "port-number", NULL
);
764 /* It's most likely that we're using V4, if the family is not
767 regs
= &v4_config_registers
;
768 family
= of_get_property(op
->dev
.of_node
, "xlnx,family", NULL
);
771 if (!strcmp(family
, "virtex2p"))
772 regs
= &v2_config_registers
;
773 else if (!strcmp(family
, "virtex4"))
774 regs
= &v4_config_registers
;
775 else if (!strcmp(family
, "virtex5"))
776 regs
= &v5_config_registers
;
777 else if (!strcmp(family
, "virtex6"))
778 regs
= &v6_config_registers
;
780 return hwicap_setup(&op
->dev
, id
? *id
: -1, &res
, config
,
784 static inline int hwicap_of_probe(struct platform_device
*op
,
785 const struct hwicap_driver_config
*config
)
789 #endif /* CONFIG_OF */
791 static const struct of_device_id hwicap_of_match
[];
792 static int hwicap_drv_probe(struct platform_device
*pdev
)
794 const struct of_device_id
*match
;
795 struct resource
*res
;
796 const struct config_registers
*regs
;
799 match
= of_match_device(hwicap_of_match
, &pdev
->dev
);
801 return hwicap_of_probe(pdev
, match
->data
);
803 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
807 /* It's most likely that we're using V4, if the family is not
810 regs
= &v4_config_registers
;
811 family
= pdev
->dev
.platform_data
;
814 if (!strcmp(family
, "virtex2p"))
815 regs
= &v2_config_registers
;
816 else if (!strcmp(family
, "virtex4"))
817 regs
= &v4_config_registers
;
818 else if (!strcmp(family
, "virtex5"))
819 regs
= &v5_config_registers
;
820 else if (!strcmp(family
, "virtex6"))
821 regs
= &v6_config_registers
;
824 return hwicap_setup(&pdev
->dev
, pdev
->id
, res
,
825 &buffer_icap_config
, regs
);
828 static int hwicap_drv_remove(struct platform_device
*pdev
)
830 return hwicap_remove(&pdev
->dev
);
834 /* Match table for device tree binding */
835 static const struct of_device_id hwicap_of_match
[] = {
836 { .compatible
= "xlnx,opb-hwicap-1.00.b", .data
= &buffer_icap_config
},
837 { .compatible
= "xlnx,xps-hwicap-1.00.a", .data
= &fifo_icap_config
},
840 MODULE_DEVICE_TABLE(of
, hwicap_of_match
);
842 #define hwicap_of_match NULL
845 static struct platform_driver hwicap_platform_driver
= {
846 .probe
= hwicap_drv_probe
,
847 .remove
= hwicap_drv_remove
,
850 .of_match_table
= hwicap_of_match
,
854 static int __init
hwicap_module_init(void)
859 icap_class
= class_create(THIS_MODULE
, "xilinx_config");
860 mutex_init(&icap_sem
);
862 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
863 retval
= register_chrdev_region(devt
,
869 retval
= platform_driver_register(&hwicap_platform_driver
);
876 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
881 static void __exit
hwicap_module_cleanup(void)
883 dev_t devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
885 class_destroy(icap_class
);
887 platform_driver_unregister(&hwicap_platform_driver
);
889 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
892 module_init(hwicap_module_init
);
893 module_exit(hwicap_module_cleanup
);
895 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
896 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
897 MODULE_LICENSE("GPL");