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>
91 #include <linux/cdev.h>
92 #include <linux/platform_device.h>
95 #include <asm/uaccess.h>
96 #include <asm/system.h>
99 /* For open firmware. */
100 #include <linux/of_device.h>
101 #include <linux/of_platform.h>
104 #include "xilinx_hwicap.h"
105 #include "buffer_icap.h"
106 #include "fifo_icap.h"
108 #define DRIVER_NAME "icap"
110 #define HWICAP_REGS (0x10000)
112 #define XHWICAP_MAJOR 259
113 #define XHWICAP_MINOR 0
114 #define HWICAP_DEVICES 1
116 /* An array, which is set to true when the device is registered. */
117 static bool probed_devices
[HWICAP_DEVICES
];
118 static struct mutex icap_sem
;
120 static struct class *icap_class
;
122 #define UNIMPLEMENTED 0xFFFF
124 static const struct config_registers v2_config_registers
= {
140 .AXSS
= UNIMPLEMENTED
,
141 .C0R_1
= UNIMPLEMENTED
,
142 .CSOB
= UNIMPLEMENTED
,
143 .WBSTAR
= UNIMPLEMENTED
,
144 .TIMER
= UNIMPLEMENTED
,
145 .BOOTSTS
= UNIMPLEMENTED
,
146 .CTL_1
= UNIMPLEMENTED
,
149 static const struct config_registers v4_config_registers
= {
161 .FLR
= UNIMPLEMENTED
,
162 .KEY
= UNIMPLEMENTED
,
166 .C0R_1
= UNIMPLEMENTED
,
167 .CSOB
= UNIMPLEMENTED
,
168 .WBSTAR
= UNIMPLEMENTED
,
169 .TIMER
= UNIMPLEMENTED
,
170 .BOOTSTS
= UNIMPLEMENTED
,
171 .CTL_1
= UNIMPLEMENTED
,
173 static const struct config_registers v5_config_registers
= {
185 .FLR
= UNIMPLEMENTED
,
186 .KEY
= UNIMPLEMENTED
,
199 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
200 * @drvdata: a pointer to the drvdata.
202 * This command desynchronizes the ICAP After this command, a
203 * bitstream containing a NULL packet, followed by a SYNCH packet is
204 * required before the ICAP will recognize commands.
206 static int hwicap_command_desync(struct hwicap_drvdata
*drvdata
)
212 * Create the data to be written to the ICAP.
214 buffer
[index
++] = hwicap_type_1_write(drvdata
->config_regs
->CMD
) | 1;
215 buffer
[index
++] = XHI_CMD_DESYNCH
;
216 buffer
[index
++] = XHI_NOOP_PACKET
;
217 buffer
[index
++] = XHI_NOOP_PACKET
;
220 * Write the data to the FIFO and intiate the transfer of data present
221 * in the FIFO to the ICAP device.
223 return drvdata
->config
->set_configuration(drvdata
,
228 * hwicap_get_configuration_register - Query a configuration register.
229 * @drvdata: a pointer to the drvdata.
230 * @reg: a constant which represents the configuration
231 * register value to be returned.
232 * Examples: XHI_IDCODE, XHI_FLR.
233 * @reg_data: returns the value of the register.
235 * Sends a query packet to the ICAP and then receives the response.
236 * The icap is left in Synched state.
238 static int hwicap_get_configuration_register(struct hwicap_drvdata
*drvdata
,
239 u32 reg
, u32
*reg_data
)
246 * Create the data to be written to the ICAP.
248 buffer
[index
++] = XHI_DUMMY_PACKET
;
249 buffer
[index
++] = XHI_NOOP_PACKET
;
250 buffer
[index
++] = XHI_SYNC_PACKET
;
251 buffer
[index
++] = XHI_NOOP_PACKET
;
252 buffer
[index
++] = XHI_NOOP_PACKET
;
255 * Write the data to the FIFO and initiate the transfer of data present
256 * in the FIFO to the ICAP device.
258 status
= drvdata
->config
->set_configuration(drvdata
,
263 /* If the syncword was not found, then we need to start over. */
264 status
= drvdata
->config
->get_status(drvdata
);
265 if ((status
& XHI_SR_DALIGN_MASK
) != XHI_SR_DALIGN_MASK
)
269 buffer
[index
++] = hwicap_type_1_read(reg
) | 1;
270 buffer
[index
++] = XHI_NOOP_PACKET
;
271 buffer
[index
++] = XHI_NOOP_PACKET
;
274 * Write the data to the FIFO and intiate the transfer of data present
275 * in the FIFO to the ICAP device.
277 status
= drvdata
->config
->set_configuration(drvdata
,
283 * Read the configuration register
285 status
= drvdata
->config
->get_configuration(drvdata
, reg_data
, 1);
292 static int hwicap_initialize_hwicap(struct hwicap_drvdata
*drvdata
)
297 dev_dbg(drvdata
->dev
, "initializing\n");
299 /* Abort any current transaction, to make sure we have the
300 * ICAP in a good state. */
301 dev_dbg(drvdata
->dev
, "Reset...\n");
302 drvdata
->config
->reset(drvdata
);
304 dev_dbg(drvdata
->dev
, "Desync...\n");
305 status
= hwicap_command_desync(drvdata
);
309 /* Attempt to read the IDCODE from ICAP. This
310 * may not be returned correctly, due to the design of the
313 dev_dbg(drvdata
->dev
, "Reading IDCODE...\n");
314 status
= hwicap_get_configuration_register(
315 drvdata
, drvdata
->config_regs
->IDCODE
, &idcode
);
316 dev_dbg(drvdata
->dev
, "IDCODE = %x\n", idcode
);
320 dev_dbg(drvdata
->dev
, "Desync...\n");
321 status
= hwicap_command_desync(drvdata
);
329 hwicap_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
331 struct hwicap_drvdata
*drvdata
= file
->private_data
;
332 ssize_t bytes_to_read
= 0;
338 status
= mutex_lock_interruptible(&drvdata
->sem
);
342 if (drvdata
->read_buffer_in_use
) {
343 /* If there are leftover bytes in the buffer, just */
344 /* return them and don't try to read more from the */
347 (count
< drvdata
->read_buffer_in_use
) ? count
:
348 drvdata
->read_buffer_in_use
;
350 /* Return the data currently in the read buffer. */
351 if (copy_to_user(buf
, drvdata
->read_buffer
, bytes_to_read
)) {
355 drvdata
->read_buffer_in_use
-= bytes_to_read
;
356 memmove(drvdata
->read_buffer
,
357 drvdata
->read_buffer
+ bytes_to_read
,
360 /* Get new data from the ICAP, and return was was requested. */
361 kbuf
= (u32
*) get_zeroed_page(GFP_KERNEL
);
367 /* The ICAP device is only able to read complete */
368 /* words. If a number of bytes that do not correspond */
369 /* to complete words is requested, then we read enough */
370 /* words to get the required number of bytes, and then */
371 /* save the remaining bytes for the next read. */
373 /* Determine the number of words to read, rounding up */
375 words
= ((count
+ 3) >> 2);
376 bytes_to_read
= words
<< 2;
378 if (bytes_to_read
> PAGE_SIZE
)
379 bytes_to_read
= PAGE_SIZE
;
381 /* Ensure we only read a complete number of words. */
382 bytes_remaining
= bytes_to_read
& 3;
384 words
= bytes_to_read
>> 2;
386 status
= drvdata
->config
->get_configuration(drvdata
,
389 /* If we didn't read correctly, then bail out. */
391 free_page((unsigned long)kbuf
);
395 /* If we fail to return the data to the user, then bail out. */
396 if (copy_to_user(buf
, kbuf
, bytes_to_read
)) {
397 free_page((unsigned long)kbuf
);
401 memcpy(drvdata
->read_buffer
,
404 drvdata
->read_buffer_in_use
= bytes_remaining
;
405 free_page((unsigned long)kbuf
);
407 status
= bytes_to_read
;
409 mutex_unlock(&drvdata
->sem
);
414 hwicap_write(struct file
*file
, const char __user
*buf
,
415 size_t count
, loff_t
*ppos
)
417 struct hwicap_drvdata
*drvdata
= file
->private_data
;
419 ssize_t left
= count
;
424 status
= mutex_lock_interruptible(&drvdata
->sem
);
428 left
+= drvdata
->write_buffer_in_use
;
430 /* Only write multiples of 4 bytes. */
436 kbuf
= (u32
*) __get_free_page(GFP_KERNEL
);
443 /* only write multiples of 4 bytes, so there might */
444 /* be as many as 3 bytes left (at the end). */
451 if (drvdata
->write_buffer_in_use
) {
452 memcpy(kbuf
, drvdata
->write_buffer
,
453 drvdata
->write_buffer_in_use
);
455 (((char *)kbuf
) + drvdata
->write_buffer_in_use
),
457 len
- (drvdata
->write_buffer_in_use
))) {
458 free_page((unsigned long)kbuf
);
463 if (copy_from_user(kbuf
, buf
+ written
, len
)) {
464 free_page((unsigned long)kbuf
);
470 status
= drvdata
->config
->set_configuration(drvdata
,
474 free_page((unsigned long)kbuf
);
478 if (drvdata
->write_buffer_in_use
) {
479 len
-= drvdata
->write_buffer_in_use
;
480 left
-= drvdata
->write_buffer_in_use
;
481 drvdata
->write_buffer_in_use
= 0;
486 if ((left
> 0) && (left
< 4)) {
487 if (!copy_from_user(drvdata
->write_buffer
,
488 buf
+ written
, left
)) {
489 drvdata
->write_buffer_in_use
= left
;
495 free_page((unsigned long)kbuf
);
498 mutex_unlock(&drvdata
->sem
);
502 static int hwicap_open(struct inode
*inode
, struct file
*file
)
504 struct hwicap_drvdata
*drvdata
;
508 drvdata
= container_of(inode
->i_cdev
, struct hwicap_drvdata
, cdev
);
510 status
= mutex_lock_interruptible(&drvdata
->sem
);
514 if (drvdata
->is_open
) {
519 status
= hwicap_initialize_hwicap(drvdata
);
521 dev_err(drvdata
->dev
, "Failed to open file");
525 file
->private_data
= drvdata
;
526 drvdata
->write_buffer_in_use
= 0;
527 drvdata
->read_buffer_in_use
= 0;
528 drvdata
->is_open
= 1;
531 mutex_unlock(&drvdata
->sem
);
537 static int hwicap_release(struct inode
*inode
, struct file
*file
)
539 struct hwicap_drvdata
*drvdata
= file
->private_data
;
543 mutex_lock(&drvdata
->sem
);
545 if (drvdata
->write_buffer_in_use
) {
546 /* Flush write buffer. */
547 for (i
= drvdata
->write_buffer_in_use
; i
< 4; i
++)
548 drvdata
->write_buffer
[i
] = 0;
550 status
= drvdata
->config
->set_configuration(drvdata
,
551 (u32
*) drvdata
->write_buffer
, 1);
556 status
= hwicap_command_desync(drvdata
);
561 drvdata
->is_open
= 0;
562 mutex_unlock(&drvdata
->sem
);
566 static struct file_operations hwicap_fops
= {
567 .owner
= THIS_MODULE
,
568 .write
= hwicap_write
,
571 .release
= hwicap_release
,
574 static int __devinit
hwicap_setup(struct device
*dev
, int id
,
575 const struct resource
*regs_res
,
576 const struct hwicap_driver_config
*config
,
577 const struct config_registers
*config_regs
)
580 struct hwicap_drvdata
*drvdata
= NULL
;
583 dev_info(dev
, "Xilinx icap port driver\n");
585 mutex_lock(&icap_sem
);
588 for (id
= 0; id
< HWICAP_DEVICES
; id
++)
589 if (!probed_devices
[id
])
592 if (id
< 0 || id
>= HWICAP_DEVICES
) {
593 mutex_unlock(&icap_sem
);
594 dev_err(dev
, "%s%i too large\n", DRIVER_NAME
, id
);
597 if (probed_devices
[id
]) {
598 mutex_unlock(&icap_sem
);
599 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
604 probed_devices
[id
] = 1;
605 mutex_unlock(&icap_sem
);
607 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
+ id
);
609 drvdata
= kzalloc(sizeof(struct hwicap_drvdata
), GFP_KERNEL
);
611 dev_err(dev
, "Couldn't allocate device private record\n");
615 dev_set_drvdata(dev
, (void *)drvdata
);
618 dev_err(dev
, "Couldn't get registers resource\n");
623 drvdata
->mem_start
= regs_res
->start
;
624 drvdata
->mem_end
= regs_res
->end
;
625 drvdata
->mem_size
= regs_res
->end
- regs_res
->start
+ 1;
627 if (!request_mem_region(drvdata
->mem_start
,
628 drvdata
->mem_size
, DRIVER_NAME
)) {
629 dev_err(dev
, "Couldn't lock memory region at %Lx\n",
635 drvdata
->devt
= devt
;
637 drvdata
->base_address
= ioremap(drvdata
->mem_start
, drvdata
->mem_size
);
638 if (!drvdata
->base_address
) {
639 dev_err(dev
, "ioremap() failed\n");
643 drvdata
->config
= config
;
644 drvdata
->config_regs
= config_regs
;
646 mutex_init(&drvdata
->sem
);
647 drvdata
->is_open
= 0;
649 dev_info(dev
, "ioremap %lx to %p with size %Lx\n",
650 (unsigned long int)drvdata
->mem_start
,
651 drvdata
->base_address
, drvdata
->mem_size
);
653 cdev_init(&drvdata
->cdev
, &hwicap_fops
);
654 drvdata
->cdev
.owner
= THIS_MODULE
;
655 retval
= cdev_add(&drvdata
->cdev
, devt
, 1);
657 dev_err(dev
, "cdev_add() failed\n");
661 device_create_drvdata(icap_class
, dev
, devt
, NULL
,
662 "%s%d", DRIVER_NAME
, id
);
663 return 0; /* success */
666 iounmap(drvdata
->base_address
);
669 release_mem_region(regs_res
->start
, drvdata
->mem_size
);
675 mutex_lock(&icap_sem
);
676 probed_devices
[id
] = 0;
677 mutex_unlock(&icap_sem
);
682 static struct hwicap_driver_config buffer_icap_config
= {
683 .get_configuration
= buffer_icap_get_configuration
,
684 .set_configuration
= buffer_icap_set_configuration
,
685 .get_status
= buffer_icap_get_status
,
686 .reset
= buffer_icap_reset
,
689 static struct hwicap_driver_config fifo_icap_config
= {
690 .get_configuration
= fifo_icap_get_configuration
,
691 .set_configuration
= fifo_icap_set_configuration
,
692 .get_status
= fifo_icap_get_status
,
693 .reset
= fifo_icap_reset
,
696 static int __devexit
hwicap_remove(struct device
*dev
)
698 struct hwicap_drvdata
*drvdata
;
700 drvdata
= (struct hwicap_drvdata
*)dev_get_drvdata(dev
);
705 device_destroy(icap_class
, drvdata
->devt
);
706 cdev_del(&drvdata
->cdev
);
707 iounmap(drvdata
->base_address
);
708 release_mem_region(drvdata
->mem_start
, drvdata
->mem_size
);
710 dev_set_drvdata(dev
, NULL
);
712 mutex_lock(&icap_sem
);
713 probed_devices
[MINOR(dev
->devt
)-XHWICAP_MINOR
] = 0;
714 mutex_unlock(&icap_sem
);
715 return 0; /* success */
718 static int __devinit
hwicap_drv_probe(struct platform_device
*pdev
)
720 struct resource
*res
;
721 const struct config_registers
*regs
;
724 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
728 /* It's most likely that we're using V4, if the family is not
730 regs
= &v4_config_registers
;
731 family
= pdev
->dev
.platform_data
;
734 if (!strcmp(family
, "virtex2p")) {
735 regs
= &v2_config_registers
;
736 } else if (!strcmp(family
, "virtex4")) {
737 regs
= &v4_config_registers
;
738 } else if (!strcmp(family
, "virtex5")) {
739 regs
= &v5_config_registers
;
743 return hwicap_setup(&pdev
->dev
, pdev
->id
, res
,
744 &buffer_icap_config
, regs
);
747 static int __devexit
hwicap_drv_remove(struct platform_device
*pdev
)
749 return hwicap_remove(&pdev
->dev
);
752 static struct platform_driver hwicap_platform_driver
= {
753 .probe
= hwicap_drv_probe
,
754 .remove
= hwicap_drv_remove
,
756 .owner
= THIS_MODULE
,
761 /* ---------------------------------------------------------------------
765 #if defined(CONFIG_OF)
767 hwicap_of_probe(struct of_device
*op
, const struct of_device_id
*match
)
770 const unsigned int *id
;
773 const struct hwicap_driver_config
*config
= match
->data
;
774 const struct config_registers
*regs
;
776 dev_dbg(&op
->dev
, "hwicap_of_probe(%p, %p)\n", op
, match
);
778 rc
= of_address_to_resource(op
->node
, 0, &res
);
780 dev_err(&op
->dev
, "invalid address\n");
784 id
= of_get_property(op
->node
, "port-number", NULL
);
786 /* It's most likely that we're using V4, if the family is not
788 regs
= &v4_config_registers
;
789 family
= of_get_property(op
->node
, "xlnx,family", NULL
);
792 if (!strcmp(family
, "virtex2p")) {
793 regs
= &v2_config_registers
;
794 } else if (!strcmp(family
, "virtex4")) {
795 regs
= &v4_config_registers
;
796 } else if (!strcmp(family
, "virtex5")) {
797 regs
= &v5_config_registers
;
800 return hwicap_setup(&op
->dev
, id
? *id
: -1, &res
, config
,
804 static int __devexit
hwicap_of_remove(struct of_device
*op
)
806 return hwicap_remove(&op
->dev
);
809 /* Match table for of_platform binding */
810 static const struct of_device_id __devinitconst hwicap_of_match
[] = {
811 { .compatible
= "xlnx,opb-hwicap-1.00.b", .data
= &buffer_icap_config
},
812 { .compatible
= "xlnx,xps-hwicap-1.00.a", .data
= &fifo_icap_config
},
815 MODULE_DEVICE_TABLE(of
, hwicap_of_match
);
817 static struct of_platform_driver hwicap_of_driver
= {
818 .owner
= THIS_MODULE
,
820 .match_table
= hwicap_of_match
,
821 .probe
= hwicap_of_probe
,
822 .remove
= __devexit_p(hwicap_of_remove
),
828 /* Registration helpers to keep the number of #ifdefs to a minimum */
829 static inline int __init
hwicap_of_register(void)
831 pr_debug("hwicap: calling of_register_platform_driver()\n");
832 return of_register_platform_driver(&hwicap_of_driver
);
835 static inline void __exit
hwicap_of_unregister(void)
837 of_unregister_platform_driver(&hwicap_of_driver
);
839 #else /* CONFIG_OF */
840 /* CONFIG_OF not enabled; do nothing helpers */
841 static inline int __init
hwicap_of_register(void) { return 0; }
842 static inline void __exit
hwicap_of_unregister(void) { }
843 #endif /* CONFIG_OF */
845 static int __init
hwicap_module_init(void)
850 icap_class
= class_create(THIS_MODULE
, "xilinx_config");
851 mutex_init(&icap_sem
);
853 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
854 retval
= register_chrdev_region(devt
,
860 retval
= platform_driver_register(&hwicap_platform_driver
);
865 retval
= hwicap_of_register();
873 platform_driver_unregister(&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 hwicap_of_unregister();
891 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
894 module_init(hwicap_module_init
);
895 module_exit(hwicap_module_cleanup
);
897 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
898 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
899 MODULE_LICENSE("GPL");