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 * Xilinx products are not intended for use in life support appliances,
25 * devices, or systems. Use in such applications is expressly prohibited.
27 * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group
28 * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group
29 * (c) Copyright 2007-2008 Xilinx Inc.
30 * All rights reserved.
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 *****************************************************************************/
39 * This is the code behind /dev/icap* -- it allows a user-space
40 * application to use the Xilinx ICAP subsystem.
42 * The following operations are possible:
44 * open open the port and initialize for access.
45 * release release port
46 * write Write a bitstream to the configuration processor.
47 * read Read a data stream from the configuration processor.
49 * After being opened, the port is initialized and accessed to avoid a
50 * corrupted first read which may occur with some hardware. The port
51 * is left in a desynched state, requiring that a synch sequence be
52 * transmitted before any valid configuration data. A user will have
53 * exclusive access to the device while it remains open, and the state
54 * of the ICAP cannot be guaranteed after the device is closed. Note
55 * that a complete reset of the core and the state of the ICAP cannot
56 * be performed on many versions of the cores, hence users of this
57 * device should avoid making inconsistent accesses to the device. In
58 * particular, accessing the read interface, without first generating
59 * a write containing a readback packet can leave the ICAP in an
62 * Note that in order to use the read interface, it is first necessary
63 * to write a request packet to the write interface. i.e., it is not
64 * possible to simply readback the bitstream (or any configuration
65 * bits) from a device without specifically requesting them first.
66 * The code to craft such packets is intended to be part of the
67 * user-space application code that uses this device. The simplest
68 * way to use this interface is simply:
70 * cp foo.bit /dev/icap0
72 * Note that unless foo.bit is an appropriately constructed partial
73 * bitstream, this has a high likelyhood of overwriting the design
74 * currently programmed in the FPGA.
77 #include <linux/version.h>
78 #include <linux/module.h>
79 #include <linux/kernel.h>
80 #include <linux/types.h>
81 #include <linux/ioport.h>
82 #include <linux/interrupt.h>
83 #include <linux/fcntl.h>
84 #include <linux/init.h>
85 #include <linux/poll.h>
86 #include <linux/proc_fs.h>
87 #include <linux/mutex.h>
88 #include <linux/smp_lock.h>
89 #include <linux/sysctl.h>
90 #include <linux/version.h>
92 #include <linux/cdev.h>
93 #include <linux/platform_device.h>
96 #include <asm/uaccess.h>
97 #include <asm/system.h>
100 /* For open firmware. */
101 #include <linux/of_device.h>
102 #include <linux/of_platform.h>
105 #include "xilinx_hwicap.h"
106 #include "buffer_icap.h"
107 #include "fifo_icap.h"
109 #define DRIVER_NAME "icap"
111 #define HWICAP_REGS (0x10000)
113 #define XHWICAP_MAJOR 259
114 #define XHWICAP_MINOR 0
115 #define HWICAP_DEVICES 1
117 /* An array, which is set to true when the device is registered. */
118 static bool probed_devices
[HWICAP_DEVICES
];
119 static struct mutex icap_sem
;
121 static struct class *icap_class
;
123 #define UNIMPLEMENTED 0xFFFF
125 static const struct config_registers v2_config_registers
= {
141 .AXSS
= UNIMPLEMENTED
,
142 .C0R_1
= UNIMPLEMENTED
,
143 .CSOB
= UNIMPLEMENTED
,
144 .WBSTAR
= UNIMPLEMENTED
,
145 .TIMER
= UNIMPLEMENTED
,
146 .BOOTSTS
= UNIMPLEMENTED
,
147 .CTL_1
= UNIMPLEMENTED
,
150 static const struct config_registers v4_config_registers
= {
162 .FLR
= UNIMPLEMENTED
,
163 .KEY
= UNIMPLEMENTED
,
167 .C0R_1
= UNIMPLEMENTED
,
168 .CSOB
= UNIMPLEMENTED
,
169 .WBSTAR
= UNIMPLEMENTED
,
170 .TIMER
= UNIMPLEMENTED
,
171 .BOOTSTS
= UNIMPLEMENTED
,
172 .CTL_1
= UNIMPLEMENTED
,
174 static const struct config_registers v5_config_registers
= {
186 .FLR
= UNIMPLEMENTED
,
187 .KEY
= UNIMPLEMENTED
,
200 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
201 * @drvdata: a pointer to the drvdata.
203 * This command desynchronizes the ICAP After this command, a
204 * bitstream containing a NULL packet, followed by a SYNCH packet is
205 * required before the ICAP will recognize commands.
207 static int hwicap_command_desync(struct hwicap_drvdata
*drvdata
)
213 * Create the data to be written to the ICAP.
215 buffer
[index
++] = hwicap_type_1_write(drvdata
->config_regs
->CMD
) | 1;
216 buffer
[index
++] = XHI_CMD_DESYNCH
;
217 buffer
[index
++] = XHI_NOOP_PACKET
;
218 buffer
[index
++] = XHI_NOOP_PACKET
;
221 * Write the data to the FIFO and intiate the transfer of data present
222 * in the FIFO to the ICAP device.
224 return drvdata
->config
->set_configuration(drvdata
,
229 * hwicap_get_configuration_register - Query a configuration register.
230 * @drvdata: a pointer to the drvdata.
231 * @reg: a constant which represents the configuration
232 * register value to be returned.
233 * Examples: XHI_IDCODE, XHI_FLR.
234 * @reg_data: returns the value of the register.
236 * Sends a query packet to the ICAP and then receives the response.
237 * The icap is left in Synched state.
239 static int hwicap_get_configuration_register(struct hwicap_drvdata
*drvdata
,
240 u32 reg
, u32
*reg_data
)
247 * Create the data to be written to the ICAP.
249 buffer
[index
++] = XHI_DUMMY_PACKET
;
250 buffer
[index
++] = XHI_NOOP_PACKET
;
251 buffer
[index
++] = XHI_SYNC_PACKET
;
252 buffer
[index
++] = XHI_NOOP_PACKET
;
253 buffer
[index
++] = XHI_NOOP_PACKET
;
256 * Write the data to the FIFO and initiate the transfer of data present
257 * in the FIFO to the ICAP device.
259 status
= drvdata
->config
->set_configuration(drvdata
,
264 /* If the syncword was not found, then we need to start over. */
265 status
= drvdata
->config
->get_status(drvdata
);
266 if ((status
& XHI_SR_DALIGN_MASK
) != XHI_SR_DALIGN_MASK
)
270 buffer
[index
++] = hwicap_type_1_read(reg
) | 1;
271 buffer
[index
++] = XHI_NOOP_PACKET
;
272 buffer
[index
++] = XHI_NOOP_PACKET
;
275 * Write the data to the FIFO and intiate the transfer of data present
276 * in the FIFO to the ICAP device.
278 status
= drvdata
->config
->set_configuration(drvdata
,
284 * Read the configuration register
286 status
= drvdata
->config
->get_configuration(drvdata
, reg_data
, 1);
293 static int hwicap_initialize_hwicap(struct hwicap_drvdata
*drvdata
)
298 dev_dbg(drvdata
->dev
, "initializing\n");
300 /* Abort any current transaction, to make sure we have the
301 * ICAP in a good state. */
302 dev_dbg(drvdata
->dev
, "Reset...\n");
303 drvdata
->config
->reset(drvdata
);
305 dev_dbg(drvdata
->dev
, "Desync...\n");
306 status
= hwicap_command_desync(drvdata
);
310 /* Attempt to read the IDCODE from ICAP. This
311 * may not be returned correctly, due to the design of the
314 dev_dbg(drvdata
->dev
, "Reading IDCODE...\n");
315 status
= hwicap_get_configuration_register(
316 drvdata
, drvdata
->config_regs
->IDCODE
, &idcode
);
317 dev_dbg(drvdata
->dev
, "IDCODE = %x\n", idcode
);
321 dev_dbg(drvdata
->dev
, "Desync...\n");
322 status
= hwicap_command_desync(drvdata
);
330 hwicap_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
332 struct hwicap_drvdata
*drvdata
= file
->private_data
;
333 ssize_t bytes_to_read
= 0;
339 status
= mutex_lock_interruptible(&drvdata
->sem
);
343 if (drvdata
->read_buffer_in_use
) {
344 /* If there are leftover bytes in the buffer, just */
345 /* return them and don't try to read more from the */
348 (count
< drvdata
->read_buffer_in_use
) ? count
:
349 drvdata
->read_buffer_in_use
;
351 /* Return the data currently in the read buffer. */
352 if (copy_to_user(buf
, drvdata
->read_buffer
, bytes_to_read
)) {
356 drvdata
->read_buffer_in_use
-= bytes_to_read
;
357 memmove(drvdata
->read_buffer
,
358 drvdata
->read_buffer
+ bytes_to_read
,
361 /* Get new data from the ICAP, and return was was requested. */
362 kbuf
= (u32
*) get_zeroed_page(GFP_KERNEL
);
368 /* The ICAP device is only able to read complete */
369 /* words. If a number of bytes that do not correspond */
370 /* to complete words is requested, then we read enough */
371 /* words to get the required number of bytes, and then */
372 /* save the remaining bytes for the next read. */
374 /* Determine the number of words to read, rounding up */
376 words
= ((count
+ 3) >> 2);
377 bytes_to_read
= words
<< 2;
379 if (bytes_to_read
> PAGE_SIZE
)
380 bytes_to_read
= PAGE_SIZE
;
382 /* Ensure we only read a complete number of words. */
383 bytes_remaining
= bytes_to_read
& 3;
385 words
= bytes_to_read
>> 2;
387 status
= drvdata
->config
->get_configuration(drvdata
,
390 /* If we didn't read correctly, then bail out. */
392 free_page((unsigned long)kbuf
);
396 /* If we fail to return the data to the user, then bail out. */
397 if (copy_to_user(buf
, kbuf
, bytes_to_read
)) {
398 free_page((unsigned long)kbuf
);
402 memcpy(drvdata
->read_buffer
,
405 drvdata
->read_buffer_in_use
= bytes_remaining
;
406 free_page((unsigned long)kbuf
);
408 status
= bytes_to_read
;
410 mutex_unlock(&drvdata
->sem
);
415 hwicap_write(struct file
*file
, const char __user
*buf
,
416 size_t count
, loff_t
*ppos
)
418 struct hwicap_drvdata
*drvdata
= file
->private_data
;
420 ssize_t left
= count
;
425 status
= mutex_lock_interruptible(&drvdata
->sem
);
429 left
+= drvdata
->write_buffer_in_use
;
431 /* Only write multiples of 4 bytes. */
437 kbuf
= (u32
*) __get_free_page(GFP_KERNEL
);
444 /* only write multiples of 4 bytes, so there might */
445 /* be as many as 3 bytes left (at the end). */
452 if (drvdata
->write_buffer_in_use
) {
453 memcpy(kbuf
, drvdata
->write_buffer
,
454 drvdata
->write_buffer_in_use
);
456 (((char *)kbuf
) + drvdata
->write_buffer_in_use
),
458 len
- (drvdata
->write_buffer_in_use
))) {
459 free_page((unsigned long)kbuf
);
464 if (copy_from_user(kbuf
, buf
+ written
, len
)) {
465 free_page((unsigned long)kbuf
);
471 status
= drvdata
->config
->set_configuration(drvdata
,
475 free_page((unsigned long)kbuf
);
479 if (drvdata
->write_buffer_in_use
) {
480 len
-= drvdata
->write_buffer_in_use
;
481 left
-= drvdata
->write_buffer_in_use
;
482 drvdata
->write_buffer_in_use
= 0;
487 if ((left
> 0) && (left
< 4)) {
488 if (!copy_from_user(drvdata
->write_buffer
,
489 buf
+ written
, left
)) {
490 drvdata
->write_buffer_in_use
= left
;
496 free_page((unsigned long)kbuf
);
499 mutex_unlock(&drvdata
->sem
);
503 static int hwicap_open(struct inode
*inode
, struct file
*file
)
505 struct hwicap_drvdata
*drvdata
;
509 drvdata
= container_of(inode
->i_cdev
, struct hwicap_drvdata
, cdev
);
511 status
= mutex_lock_interruptible(&drvdata
->sem
);
515 if (drvdata
->is_open
) {
520 status
= hwicap_initialize_hwicap(drvdata
);
522 dev_err(drvdata
->dev
, "Failed to open file");
526 file
->private_data
= drvdata
;
527 drvdata
->write_buffer_in_use
= 0;
528 drvdata
->read_buffer_in_use
= 0;
529 drvdata
->is_open
= 1;
532 mutex_unlock(&drvdata
->sem
);
538 static int hwicap_release(struct inode
*inode
, struct file
*file
)
540 struct hwicap_drvdata
*drvdata
= file
->private_data
;
544 mutex_lock(&drvdata
->sem
);
546 if (drvdata
->write_buffer_in_use
) {
547 /* Flush write buffer. */
548 for (i
= drvdata
->write_buffer_in_use
; i
< 4; i
++)
549 drvdata
->write_buffer
[i
] = 0;
551 status
= drvdata
->config
->set_configuration(drvdata
,
552 (u32
*) drvdata
->write_buffer
, 1);
557 status
= hwicap_command_desync(drvdata
);
562 drvdata
->is_open
= 0;
563 mutex_unlock(&drvdata
->sem
);
567 static struct file_operations hwicap_fops
= {
568 .owner
= THIS_MODULE
,
569 .write
= hwicap_write
,
572 .release
= hwicap_release
,
575 static int __devinit
hwicap_setup(struct device
*dev
, int id
,
576 const struct resource
*regs_res
,
577 const struct hwicap_driver_config
*config
,
578 const struct config_registers
*config_regs
)
581 struct hwicap_drvdata
*drvdata
= NULL
;
584 dev_info(dev
, "Xilinx icap port driver\n");
586 mutex_lock(&icap_sem
);
589 for (id
= 0; id
< HWICAP_DEVICES
; id
++)
590 if (!probed_devices
[id
])
593 if (id
< 0 || id
>= HWICAP_DEVICES
) {
594 mutex_unlock(&icap_sem
);
595 dev_err(dev
, "%s%i too large\n", DRIVER_NAME
, id
);
598 if (probed_devices
[id
]) {
599 mutex_unlock(&icap_sem
);
600 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
605 probed_devices
[id
] = 1;
606 mutex_unlock(&icap_sem
);
608 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
+ id
);
610 drvdata
= kzalloc(sizeof(struct hwicap_drvdata
), GFP_KERNEL
);
612 dev_err(dev
, "Couldn't allocate device private record\n");
616 dev_set_drvdata(dev
, (void *)drvdata
);
619 dev_err(dev
, "Couldn't get registers resource\n");
624 drvdata
->mem_start
= regs_res
->start
;
625 drvdata
->mem_end
= regs_res
->end
;
626 drvdata
->mem_size
= regs_res
->end
- regs_res
->start
+ 1;
628 if (!request_mem_region(drvdata
->mem_start
,
629 drvdata
->mem_size
, DRIVER_NAME
)) {
630 dev_err(dev
, "Couldn't lock memory region at %Lx\n",
636 drvdata
->devt
= devt
;
638 drvdata
->base_address
= ioremap(drvdata
->mem_start
, drvdata
->mem_size
);
639 if (!drvdata
->base_address
) {
640 dev_err(dev
, "ioremap() failed\n");
644 drvdata
->config
= config
;
645 drvdata
->config_regs
= config_regs
;
647 mutex_init(&drvdata
->sem
);
648 drvdata
->is_open
= 0;
650 dev_info(dev
, "ioremap %lx to %p with size %Lx\n",
651 (unsigned long int)drvdata
->mem_start
,
652 drvdata
->base_address
, drvdata
->mem_size
);
654 cdev_init(&drvdata
->cdev
, &hwicap_fops
);
655 drvdata
->cdev
.owner
= THIS_MODULE
;
656 retval
= cdev_add(&drvdata
->cdev
, devt
, 1);
658 dev_err(dev
, "cdev_add() failed\n");
662 device_create_drvdata(icap_class
, dev
, devt
, NULL
,
663 "%s%d", DRIVER_NAME
, id
);
664 return 0; /* success */
667 iounmap(drvdata
->base_address
);
670 release_mem_region(regs_res
->start
, drvdata
->mem_size
);
676 mutex_lock(&icap_sem
);
677 probed_devices
[id
] = 0;
678 mutex_unlock(&icap_sem
);
683 static struct hwicap_driver_config buffer_icap_config
= {
684 .get_configuration
= buffer_icap_get_configuration
,
685 .set_configuration
= buffer_icap_set_configuration
,
686 .get_status
= buffer_icap_get_status
,
687 .reset
= buffer_icap_reset
,
690 static struct hwicap_driver_config fifo_icap_config
= {
691 .get_configuration
= fifo_icap_get_configuration
,
692 .set_configuration
= fifo_icap_set_configuration
,
693 .get_status
= fifo_icap_get_status
,
694 .reset
= fifo_icap_reset
,
697 static int __devexit
hwicap_remove(struct device
*dev
)
699 struct hwicap_drvdata
*drvdata
;
701 drvdata
= (struct hwicap_drvdata
*)dev_get_drvdata(dev
);
706 device_destroy(icap_class
, drvdata
->devt
);
707 cdev_del(&drvdata
->cdev
);
708 iounmap(drvdata
->base_address
);
709 release_mem_region(drvdata
->mem_start
, drvdata
->mem_size
);
711 dev_set_drvdata(dev
, NULL
);
713 mutex_lock(&icap_sem
);
714 probed_devices
[MINOR(dev
->devt
)-XHWICAP_MINOR
] = 0;
715 mutex_unlock(&icap_sem
);
716 return 0; /* success */
719 static int __devinit
hwicap_drv_probe(struct platform_device
*pdev
)
721 struct resource
*res
;
722 const struct config_registers
*regs
;
725 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
729 /* It's most likely that we're using V4, if the family is not
731 regs
= &v4_config_registers
;
732 family
= pdev
->dev
.platform_data
;
735 if (!strcmp(family
, "virtex2p")) {
736 regs
= &v2_config_registers
;
737 } else if (!strcmp(family
, "virtex4")) {
738 regs
= &v4_config_registers
;
739 } else if (!strcmp(family
, "virtex5")) {
740 regs
= &v5_config_registers
;
744 return hwicap_setup(&pdev
->dev
, pdev
->id
, res
,
745 &buffer_icap_config
, regs
);
748 static int __devexit
hwicap_drv_remove(struct platform_device
*pdev
)
750 return hwicap_remove(&pdev
->dev
);
753 static struct platform_driver hwicap_platform_driver
= {
754 .probe
= hwicap_drv_probe
,
755 .remove
= hwicap_drv_remove
,
757 .owner
= THIS_MODULE
,
762 /* ---------------------------------------------------------------------
766 #if defined(CONFIG_OF)
768 hwicap_of_probe(struct of_device
*op
, const struct of_device_id
*match
)
771 const unsigned int *id
;
774 const struct hwicap_driver_config
*config
= match
->data
;
775 const struct config_registers
*regs
;
777 dev_dbg(&op
->dev
, "hwicap_of_probe(%p, %p)\n", op
, match
);
779 rc
= of_address_to_resource(op
->node
, 0, &res
);
781 dev_err(&op
->dev
, "invalid address\n");
785 id
= of_get_property(op
->node
, "port-number", NULL
);
787 /* It's most likely that we're using V4, if the family is not
789 regs
= &v4_config_registers
;
790 family
= of_get_property(op
->node
, "xlnx,family", NULL
);
793 if (!strcmp(family
, "virtex2p")) {
794 regs
= &v2_config_registers
;
795 } else if (!strcmp(family
, "virtex4")) {
796 regs
= &v4_config_registers
;
797 } else if (!strcmp(family
, "virtex5")) {
798 regs
= &v5_config_registers
;
801 return hwicap_setup(&op
->dev
, id
? *id
: -1, &res
, config
,
805 static int __devexit
hwicap_of_remove(struct of_device
*op
)
807 return hwicap_remove(&op
->dev
);
810 /* Match table for of_platform binding */
811 static const struct of_device_id __devinitconst hwicap_of_match
[] = {
812 { .compatible
= "xlnx,opb-hwicap-1.00.b", .data
= &buffer_icap_config
},
813 { .compatible
= "xlnx,xps-hwicap-1.00.a", .data
= &fifo_icap_config
},
816 MODULE_DEVICE_TABLE(of
, hwicap_of_match
);
818 static struct of_platform_driver hwicap_of_driver
= {
819 .owner
= THIS_MODULE
,
821 .match_table
= hwicap_of_match
,
822 .probe
= hwicap_of_probe
,
823 .remove
= __devexit_p(hwicap_of_remove
),
829 /* Registration helpers to keep the number of #ifdefs to a minimum */
830 static inline int __init
hwicap_of_register(void)
832 pr_debug("hwicap: calling of_register_platform_driver()\n");
833 return of_register_platform_driver(&hwicap_of_driver
);
836 static inline void __exit
hwicap_of_unregister(void)
838 of_unregister_platform_driver(&hwicap_of_driver
);
840 #else /* CONFIG_OF */
841 /* CONFIG_OF not enabled; do nothing helpers */
842 static inline int __init
hwicap_of_register(void) { return 0; }
843 static inline void __exit
hwicap_of_unregister(void) { }
844 #endif /* CONFIG_OF */
846 static int __init
hwicap_module_init(void)
851 icap_class
= class_create(THIS_MODULE
, "xilinx_config");
852 mutex_init(&icap_sem
);
854 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
855 retval
= register_chrdev_region(devt
,
861 retval
= platform_driver_register(&hwicap_platform_driver
);
866 retval
= hwicap_of_register();
874 platform_driver_unregister(&hwicap_platform_driver
);
877 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
882 static void __exit
hwicap_module_cleanup(void)
884 dev_t devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
886 class_destroy(icap_class
);
888 platform_driver_unregister(&hwicap_platform_driver
);
890 hwicap_of_unregister();
892 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
895 module_init(hwicap_module_init
);
896 module_exit(hwicap_module_cleanup
);
898 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
899 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
900 MODULE_LICENSE("GPL");