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/xilinx_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/xilinx_icap
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/sysctl.h>
89 #include <linux/version.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 "xilinx_icap"
110 #define HWICAP_REGS (0x10000)
112 /* dynamically allocate device number */
113 static int xhwicap_major
;
114 static int xhwicap_minor
;
115 #define HWICAP_DEVICES 1
117 module_param(xhwicap_major
, int, S_IRUGO
);
118 module_param(xhwicap_minor
, int, S_IRUGO
);
120 /* An array, which is set to true when the device is registered. */
121 static bool probed_devices
[HWICAP_DEVICES
];
122 static struct mutex icap_sem
;
124 static struct class *icap_class
;
126 #define UNIMPLEMENTED 0xFFFF
128 static const struct config_registers v2_config_registers
= {
144 .AXSS
= UNIMPLEMENTED
,
145 .C0R_1
= UNIMPLEMENTED
,
146 .CSOB
= UNIMPLEMENTED
,
147 .WBSTAR
= UNIMPLEMENTED
,
148 .TIMER
= UNIMPLEMENTED
,
149 .BOOTSTS
= UNIMPLEMENTED
,
150 .CTL_1
= UNIMPLEMENTED
,
153 static const struct config_registers v4_config_registers
= {
165 .FLR
= UNIMPLEMENTED
,
166 .KEY
= UNIMPLEMENTED
,
170 .C0R_1
= UNIMPLEMENTED
,
171 .CSOB
= UNIMPLEMENTED
,
172 .WBSTAR
= UNIMPLEMENTED
,
173 .TIMER
= UNIMPLEMENTED
,
174 .BOOTSTS
= UNIMPLEMENTED
,
175 .CTL_1
= UNIMPLEMENTED
,
177 static const struct config_registers v5_config_registers
= {
189 .FLR
= UNIMPLEMENTED
,
190 .KEY
= UNIMPLEMENTED
,
203 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
204 * @drvdata: a pointer to the drvdata.
206 * This command desynchronizes the ICAP After this command, a
207 * bitstream containing a NULL packet, followed by a SYNCH packet is
208 * required before the ICAP will recognize commands.
210 static int hwicap_command_desync(struct hwicap_drvdata
*drvdata
)
216 * Create the data to be written to the ICAP.
218 buffer
[index
++] = hwicap_type_1_write(drvdata
->config_regs
->CMD
) | 1;
219 buffer
[index
++] = XHI_CMD_DESYNCH
;
220 buffer
[index
++] = XHI_NOOP_PACKET
;
221 buffer
[index
++] = XHI_NOOP_PACKET
;
224 * Write the data to the FIFO and intiate the transfer of data present
225 * in the FIFO to the ICAP device.
227 return drvdata
->config
->set_configuration(drvdata
,
232 * hwicap_get_configuration_register - Query a configuration register.
233 * @drvdata: a pointer to the drvdata.
234 * @reg: a constant which represents the configuration
235 * register value to be returned.
236 * Examples: XHI_IDCODE, XHI_FLR.
237 * @reg_data: returns the value of the register.
239 * Sends a query packet to the ICAP and then receives the response.
240 * The icap is left in Synched state.
242 static int hwicap_get_configuration_register(struct hwicap_drvdata
*drvdata
,
243 u32 reg
, u32
*reg_data
)
250 * Create the data to be written to the ICAP.
252 buffer
[index
++] = XHI_DUMMY_PACKET
;
253 buffer
[index
++] = XHI_SYNC_PACKET
;
254 buffer
[index
++] = XHI_NOOP_PACKET
;
255 buffer
[index
++] = hwicap_type_1_read(reg
) | 1;
256 buffer
[index
++] = XHI_NOOP_PACKET
;
257 buffer
[index
++] = XHI_NOOP_PACKET
;
260 * Write the data to the FIFO and intiate the transfer of data present
261 * in the FIFO to the ICAP device.
263 status
= drvdata
->config
->set_configuration(drvdata
,
269 * Read the configuration register
271 status
= drvdata
->config
->get_configuration(drvdata
, reg_data
, 1);
278 static int hwicap_initialize_hwicap(struct hwicap_drvdata
*drvdata
)
283 dev_dbg(drvdata
->dev
, "initializing\n");
285 /* Abort any current transaction, to make sure we have the
286 * ICAP in a good state. */
287 dev_dbg(drvdata
->dev
, "Reset...\n");
288 drvdata
->config
->reset(drvdata
);
290 dev_dbg(drvdata
->dev
, "Desync...\n");
291 status
= hwicap_command_desync(drvdata
);
295 /* Attempt to read the IDCODE from ICAP. This
296 * may not be returned correctly, due to the design of the
299 dev_dbg(drvdata
->dev
, "Reading IDCODE...\n");
300 status
= hwicap_get_configuration_register(
301 drvdata
, drvdata
->config_regs
->IDCODE
, &idcode
);
302 dev_dbg(drvdata
->dev
, "IDCODE = %x\n", idcode
);
306 dev_dbg(drvdata
->dev
, "Desync...\n");
307 status
= hwicap_command_desync(drvdata
);
315 hwicap_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
317 struct hwicap_drvdata
*drvdata
= file
->private_data
;
318 ssize_t bytes_to_read
= 0;
324 status
= mutex_lock_interruptible(&drvdata
->sem
);
328 if (drvdata
->read_buffer_in_use
) {
329 /* If there are leftover bytes in the buffer, just */
330 /* return them and don't try to read more from the */
333 (count
< drvdata
->read_buffer_in_use
) ? count
:
334 drvdata
->read_buffer_in_use
;
336 /* Return the data currently in the read buffer. */
337 if (copy_to_user(buf
, drvdata
->read_buffer
, bytes_to_read
)) {
341 drvdata
->read_buffer_in_use
-= bytes_to_read
;
342 memmove(drvdata
->read_buffer
,
343 drvdata
->read_buffer
+ bytes_to_read
,
346 /* Get new data from the ICAP, and return was was requested. */
347 kbuf
= (u32
*) get_zeroed_page(GFP_KERNEL
);
353 /* The ICAP device is only able to read complete */
354 /* words. If a number of bytes that do not correspond */
355 /* to complete words is requested, then we read enough */
356 /* words to get the required number of bytes, and then */
357 /* save the remaining bytes for the next read. */
359 /* Determine the number of words to read, rounding up */
361 words
= ((count
+ 3) >> 2);
362 bytes_to_read
= words
<< 2;
364 if (bytes_to_read
> PAGE_SIZE
)
365 bytes_to_read
= PAGE_SIZE
;
367 /* Ensure we only read a complete number of words. */
368 bytes_remaining
= bytes_to_read
& 3;
370 words
= bytes_to_read
>> 2;
372 status
= drvdata
->config
->get_configuration(drvdata
,
375 /* If we didn't read correctly, then bail out. */
377 free_page((unsigned long)kbuf
);
381 /* If we fail to return the data to the user, then bail out. */
382 if (copy_to_user(buf
, kbuf
, bytes_to_read
)) {
383 free_page((unsigned long)kbuf
);
387 memcpy(drvdata
->read_buffer
,
390 drvdata
->read_buffer_in_use
= bytes_remaining
;
391 free_page((unsigned long)kbuf
);
393 status
= bytes_to_read
;
395 mutex_unlock(&drvdata
->sem
);
400 hwicap_write(struct file
*file
, const char __user
*buf
,
401 size_t count
, loff_t
*ppos
)
403 struct hwicap_drvdata
*drvdata
= file
->private_data
;
405 ssize_t left
= count
;
410 status
= mutex_lock_interruptible(&drvdata
->sem
);
414 left
+= drvdata
->write_buffer_in_use
;
416 /* Only write multiples of 4 bytes. */
422 kbuf
= (u32
*) __get_free_page(GFP_KERNEL
);
429 /* only write multiples of 4 bytes, so there might */
430 /* be as many as 3 bytes left (at the end). */
437 if (drvdata
->write_buffer_in_use
) {
438 memcpy(kbuf
, drvdata
->write_buffer
,
439 drvdata
->write_buffer_in_use
);
441 (((char *)kbuf
) + drvdata
->write_buffer_in_use
),
443 len
- (drvdata
->write_buffer_in_use
))) {
444 free_page((unsigned long)kbuf
);
449 if (copy_from_user(kbuf
, buf
+ written
, len
)) {
450 free_page((unsigned long)kbuf
);
456 status
= drvdata
->config
->set_configuration(drvdata
,
460 free_page((unsigned long)kbuf
);
464 if (drvdata
->write_buffer_in_use
) {
465 len
-= drvdata
->write_buffer_in_use
;
466 left
-= drvdata
->write_buffer_in_use
;
467 drvdata
->write_buffer_in_use
= 0;
472 if ((left
> 0) && (left
< 4)) {
473 if (!copy_from_user(drvdata
->write_buffer
,
474 buf
+ written
, left
)) {
475 drvdata
->write_buffer_in_use
= left
;
481 free_page((unsigned long)kbuf
);
484 mutex_unlock(&drvdata
->sem
);
488 static int hwicap_open(struct inode
*inode
, struct file
*file
)
490 struct hwicap_drvdata
*drvdata
;
493 drvdata
= container_of(inode
->i_cdev
, struct hwicap_drvdata
, cdev
);
495 status
= mutex_lock_interruptible(&drvdata
->sem
);
499 if (drvdata
->is_open
) {
504 status
= hwicap_initialize_hwicap(drvdata
);
506 dev_err(drvdata
->dev
, "Failed to open file");
510 file
->private_data
= drvdata
;
511 drvdata
->write_buffer_in_use
= 0;
512 drvdata
->read_buffer_in_use
= 0;
513 drvdata
->is_open
= 1;
516 mutex_unlock(&drvdata
->sem
);
520 static int hwicap_release(struct inode
*inode
, struct file
*file
)
522 struct hwicap_drvdata
*drvdata
= file
->private_data
;
526 mutex_lock(&drvdata
->sem
);
528 if (drvdata
->write_buffer_in_use
) {
529 /* Flush write buffer. */
530 for (i
= drvdata
->write_buffer_in_use
; i
< 4; i
++)
531 drvdata
->write_buffer
[i
] = 0;
533 status
= drvdata
->config
->set_configuration(drvdata
,
534 (u32
*) drvdata
->write_buffer
, 1);
539 status
= hwicap_command_desync(drvdata
);
544 drvdata
->is_open
= 0;
545 mutex_unlock(&drvdata
->sem
);
549 static struct file_operations hwicap_fops
= {
550 .owner
= THIS_MODULE
,
551 .write
= hwicap_write
,
554 .release
= hwicap_release
,
557 static int __devinit
hwicap_setup(struct device
*dev
, int id
,
558 const struct resource
*regs_res
,
559 const struct hwicap_driver_config
*config
,
560 const struct config_registers
*config_regs
)
563 struct hwicap_drvdata
*drvdata
= NULL
;
566 dev_info(dev
, "Xilinx icap port driver\n");
568 mutex_lock(&icap_sem
);
571 for (id
= 0; id
< HWICAP_DEVICES
; id
++)
572 if (!probed_devices
[id
])
575 if (id
< 0 || id
>= HWICAP_DEVICES
) {
576 mutex_unlock(&icap_sem
);
577 dev_err(dev
, "%s%i too large\n", DRIVER_NAME
, id
);
580 if (probed_devices
[id
]) {
581 mutex_unlock(&icap_sem
);
582 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
587 probed_devices
[id
] = 1;
588 mutex_unlock(&icap_sem
);
590 devt
= MKDEV(xhwicap_major
, xhwicap_minor
+ id
);
592 drvdata
= kzalloc(sizeof(struct hwicap_drvdata
), GFP_KERNEL
);
594 dev_err(dev
, "Couldn't allocate device private record\n");
598 dev_set_drvdata(dev
, (void *)drvdata
);
601 dev_err(dev
, "Couldn't get registers resource\n");
606 drvdata
->mem_start
= regs_res
->start
;
607 drvdata
->mem_end
= regs_res
->end
;
608 drvdata
->mem_size
= regs_res
->end
- regs_res
->start
+ 1;
610 if (!request_mem_region(drvdata
->mem_start
,
611 drvdata
->mem_size
, DRIVER_NAME
)) {
612 dev_err(dev
, "Couldn't lock memory region at %p\n",
613 (void *)regs_res
->start
);
618 drvdata
->devt
= devt
;
620 drvdata
->base_address
= ioremap(drvdata
->mem_start
, drvdata
->mem_size
);
621 if (!drvdata
->base_address
) {
622 dev_err(dev
, "ioremap() failed\n");
626 drvdata
->config
= config
;
627 drvdata
->config_regs
= config_regs
;
629 mutex_init(&drvdata
->sem
);
630 drvdata
->is_open
= 0;
632 dev_info(dev
, "ioremap %lx to %p with size %x\n",
633 (unsigned long int)drvdata
->mem_start
,
634 drvdata
->base_address
, drvdata
->mem_size
);
636 cdev_init(&drvdata
->cdev
, &hwicap_fops
);
637 drvdata
->cdev
.owner
= THIS_MODULE
;
638 retval
= cdev_add(&drvdata
->cdev
, devt
, 1);
640 dev_err(dev
, "cdev_add() failed\n");
643 /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
644 device_create(icap_class
, dev
, devt
, "%s%d", DRIVER_NAME
, id
);
645 return 0; /* success */
648 iounmap(drvdata
->base_address
);
651 release_mem_region(regs_res
->start
, drvdata
->mem_size
);
657 mutex_lock(&icap_sem
);
658 probed_devices
[id
] = 0;
659 mutex_unlock(&icap_sem
);
664 static struct hwicap_driver_config buffer_icap_config
= {
665 .get_configuration
= buffer_icap_get_configuration
,
666 .set_configuration
= buffer_icap_set_configuration
,
667 .reset
= buffer_icap_reset
,
670 static struct hwicap_driver_config fifo_icap_config
= {
671 .get_configuration
= fifo_icap_get_configuration
,
672 .set_configuration
= fifo_icap_set_configuration
,
673 .reset
= fifo_icap_reset
,
676 static int __devexit
hwicap_remove(struct device
*dev
)
678 struct hwicap_drvdata
*drvdata
;
680 drvdata
= (struct hwicap_drvdata
*)dev_get_drvdata(dev
);
685 device_destroy(icap_class
, drvdata
->devt
);
686 cdev_del(&drvdata
->cdev
);
687 iounmap(drvdata
->base_address
);
688 release_mem_region(drvdata
->mem_start
, drvdata
->mem_size
);
690 dev_set_drvdata(dev
, NULL
);
692 mutex_lock(&icap_sem
);
693 probed_devices
[MINOR(dev
->devt
)-xhwicap_minor
] = 0;
694 mutex_unlock(&icap_sem
);
695 return 0; /* success */
698 static int __devinit
hwicap_drv_probe(struct platform_device
*pdev
)
700 struct resource
*res
;
701 const struct config_registers
*regs
;
704 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
708 /* It's most likely that we're using V4, if the family is not
710 regs
= &v4_config_registers
;
711 family
= pdev
->dev
.platform_data
;
714 if (!strcmp(family
, "virtex2p")) {
715 regs
= &v2_config_registers
;
716 } else if (!strcmp(family
, "virtex4")) {
717 regs
= &v4_config_registers
;
718 } else if (!strcmp(family
, "virtex5")) {
719 regs
= &v5_config_registers
;
723 return hwicap_setup(&pdev
->dev
, pdev
->id
, res
,
724 &buffer_icap_config
, regs
);
727 static int __devexit
hwicap_drv_remove(struct platform_device
*pdev
)
729 return hwicap_remove(&pdev
->dev
);
732 static struct platform_driver hwicap_platform_driver
= {
733 .probe
= hwicap_drv_probe
,
734 .remove
= hwicap_drv_remove
,
736 .owner
= THIS_MODULE
,
741 /* ---------------------------------------------------------------------
745 #if defined(CONFIG_OF)
747 hwicap_of_probe(struct of_device
*op
, const struct of_device_id
*match
)
750 const unsigned int *id
;
753 const struct hwicap_driver_config
*config
= match
->data
;
754 const struct config_registers
*regs
;
756 dev_dbg(&op
->dev
, "hwicap_of_probe(%p, %p)\n", op
, match
);
758 rc
= of_address_to_resource(op
->node
, 0, &res
);
760 dev_err(&op
->dev
, "invalid address\n");
764 id
= of_get_property(op
->node
, "port-number", NULL
);
766 /* It's most likely that we're using V4, if the family is not
768 regs
= &v4_config_registers
;
769 family
= of_get_property(op
->node
, "xlnx,family", NULL
);
772 if (!strcmp(family
, "virtex2p")) {
773 regs
= &v2_config_registers
;
774 } else if (!strcmp(family
, "virtex4")) {
775 regs
= &v4_config_registers
;
776 } else if (!strcmp(family
, "virtex5")) {
777 regs
= &v5_config_registers
;
780 return hwicap_setup(&op
->dev
, id
? *id
: -1, &res
, config
,
784 static int __devexit
hwicap_of_remove(struct of_device
*op
)
786 return hwicap_remove(&op
->dev
);
789 /* Match table for of_platform binding */
790 static const struct of_device_id __devinit hwicap_of_match
[] = {
791 { .compatible
= "xlnx,opb-hwicap-1.00.b", .data
= &buffer_icap_config
},
792 { .compatible
= "xlnx,xps-hwicap-1.00.a", .data
= &fifo_icap_config
},
795 MODULE_DEVICE_TABLE(of
, hwicap_of_match
);
797 static struct of_platform_driver hwicap_of_driver
= {
798 .owner
= THIS_MODULE
,
800 .match_table
= hwicap_of_match
,
801 .probe
= hwicap_of_probe
,
802 .remove
= __devexit_p(hwicap_of_remove
),
808 /* Registration helpers to keep the number of #ifdefs to a minimum */
809 static inline int __init
hwicap_of_register(void)
811 pr_debug("hwicap: calling of_register_platform_driver()\n");
812 return of_register_platform_driver(&hwicap_of_driver
);
815 static inline void __exit
hwicap_of_unregister(void)
817 of_unregister_platform_driver(&hwicap_of_driver
);
819 #else /* CONFIG_OF */
820 /* CONFIG_OF not enabled; do nothing helpers */
821 static inline int __init
hwicap_of_register(void) { return 0; }
822 static inline void __exit
hwicap_of_unregister(void) { }
823 #endif /* CONFIG_OF */
825 static int __init
hwicap_module_init(void)
830 icap_class
= class_create(THIS_MODULE
, "xilinx_config");
831 mutex_init(&icap_sem
);
834 devt
= MKDEV(xhwicap_major
, xhwicap_minor
);
835 retval
= register_chrdev_region(
842 retval
= alloc_chrdev_region(&devt
,
848 xhwicap_major
= MAJOR(devt
);
851 retval
= platform_driver_register(&hwicap_platform_driver
);
856 retval
= hwicap_of_register();
864 platform_driver_unregister(&hwicap_platform_driver
);
867 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
872 static void __exit
hwicap_module_cleanup(void)
874 dev_t devt
= MKDEV(xhwicap_major
, xhwicap_minor
);
876 class_destroy(icap_class
);
878 platform_driver_unregister(&hwicap_platform_driver
);
880 hwicap_of_unregister();
882 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
885 module_init(hwicap_module_init
);
886 module_exit(hwicap_module_cleanup
);
888 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
889 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
890 MODULE_LICENSE("GPL");