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 likelyhood 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/smp_lock.h>
85 #include <linux/sysctl.h>
87 #include <linux/cdev.h>
88 #include <linux/platform_device.h>
89 #include <linux/slab.h>
92 #include <asm/uaccess.h>
93 #include <asm/system.h>
96 /* For open firmware. */
97 #include <linux/of_address.h>
98 #include <linux/of_device.h>
99 #include <linux/of_platform.h>
102 #include "xilinx_hwicap.h"
103 #include "buffer_icap.h"
104 #include "fifo_icap.h"
106 #define DRIVER_NAME "icap"
108 #define HWICAP_REGS (0x10000)
110 #define XHWICAP_MAJOR 259
111 #define XHWICAP_MINOR 0
112 #define HWICAP_DEVICES 1
114 /* An array, which is set to true when the device is registered. */
115 static bool probed_devices
[HWICAP_DEVICES
];
116 static struct mutex icap_sem
;
118 static struct class *icap_class
;
120 #define UNIMPLEMENTED 0xFFFF
122 static const struct config_registers v2_config_registers
= {
138 .AXSS
= UNIMPLEMENTED
,
139 .C0R_1
= UNIMPLEMENTED
,
140 .CSOB
= UNIMPLEMENTED
,
141 .WBSTAR
= UNIMPLEMENTED
,
142 .TIMER
= UNIMPLEMENTED
,
143 .BOOTSTS
= UNIMPLEMENTED
,
144 .CTL_1
= UNIMPLEMENTED
,
147 static const struct config_registers v4_config_registers
= {
159 .FLR
= UNIMPLEMENTED
,
160 .KEY
= UNIMPLEMENTED
,
164 .C0R_1
= UNIMPLEMENTED
,
165 .CSOB
= UNIMPLEMENTED
,
166 .WBSTAR
= UNIMPLEMENTED
,
167 .TIMER
= UNIMPLEMENTED
,
168 .BOOTSTS
= UNIMPLEMENTED
,
169 .CTL_1
= UNIMPLEMENTED
,
171 static const struct config_registers v5_config_registers
= {
183 .FLR
= UNIMPLEMENTED
,
184 .KEY
= UNIMPLEMENTED
,
197 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
198 * @drvdata: a pointer to the drvdata.
200 * This command desynchronizes the ICAP After this command, a
201 * bitstream containing a NULL packet, followed by a SYNCH packet is
202 * required before the ICAP will recognize commands.
204 static int hwicap_command_desync(struct hwicap_drvdata
*drvdata
)
210 * Create the data to be written to the ICAP.
212 buffer
[index
++] = hwicap_type_1_write(drvdata
->config_regs
->CMD
) | 1;
213 buffer
[index
++] = XHI_CMD_DESYNCH
;
214 buffer
[index
++] = XHI_NOOP_PACKET
;
215 buffer
[index
++] = XHI_NOOP_PACKET
;
218 * Write the data to the FIFO and intiate the transfer of data present
219 * in the FIFO to the ICAP device.
221 return drvdata
->config
->set_configuration(drvdata
,
226 * hwicap_get_configuration_register - Query a configuration register.
227 * @drvdata: a pointer to the drvdata.
228 * @reg: a constant which represents the configuration
229 * register value to be returned.
230 * Examples: XHI_IDCODE, XHI_FLR.
231 * @reg_data: returns the value of the register.
233 * Sends a query packet to the ICAP and then receives the response.
234 * The icap is left in Synched state.
236 static int hwicap_get_configuration_register(struct hwicap_drvdata
*drvdata
,
237 u32 reg
, u32
*reg_data
)
244 * Create the data to be written to the ICAP.
246 buffer
[index
++] = XHI_DUMMY_PACKET
;
247 buffer
[index
++] = XHI_NOOP_PACKET
;
248 buffer
[index
++] = XHI_SYNC_PACKET
;
249 buffer
[index
++] = XHI_NOOP_PACKET
;
250 buffer
[index
++] = XHI_NOOP_PACKET
;
253 * Write the data to the FIFO and initiate the transfer of data present
254 * in the FIFO to the ICAP device.
256 status
= drvdata
->config
->set_configuration(drvdata
,
261 /* If the syncword was not found, then we need to start over. */
262 status
= drvdata
->config
->get_status(drvdata
);
263 if ((status
& XHI_SR_DALIGN_MASK
) != XHI_SR_DALIGN_MASK
)
267 buffer
[index
++] = hwicap_type_1_read(reg
) | 1;
268 buffer
[index
++] = XHI_NOOP_PACKET
;
269 buffer
[index
++] = XHI_NOOP_PACKET
;
272 * Write the data to the FIFO and intiate the transfer of data present
273 * in the FIFO to the ICAP device.
275 status
= drvdata
->config
->set_configuration(drvdata
,
281 * Read the configuration register
283 status
= drvdata
->config
->get_configuration(drvdata
, reg_data
, 1);
290 static int hwicap_initialize_hwicap(struct hwicap_drvdata
*drvdata
)
295 dev_dbg(drvdata
->dev
, "initializing\n");
297 /* Abort any current transaction, to make sure we have the
298 * ICAP in a good state. */
299 dev_dbg(drvdata
->dev
, "Reset...\n");
300 drvdata
->config
->reset(drvdata
);
302 dev_dbg(drvdata
->dev
, "Desync...\n");
303 status
= hwicap_command_desync(drvdata
);
307 /* Attempt to read the IDCODE from ICAP. This
308 * may not be returned correctly, due to the design of the
311 dev_dbg(drvdata
->dev
, "Reading IDCODE...\n");
312 status
= hwicap_get_configuration_register(
313 drvdata
, drvdata
->config_regs
->IDCODE
, &idcode
);
314 dev_dbg(drvdata
->dev
, "IDCODE = %x\n", idcode
);
318 dev_dbg(drvdata
->dev
, "Desync...\n");
319 status
= hwicap_command_desync(drvdata
);
327 hwicap_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
329 struct hwicap_drvdata
*drvdata
= file
->private_data
;
330 ssize_t bytes_to_read
= 0;
336 status
= mutex_lock_interruptible(&drvdata
->sem
);
340 if (drvdata
->read_buffer_in_use
) {
341 /* If there are leftover bytes in the buffer, just */
342 /* return them and don't try to read more from the */
345 (count
< drvdata
->read_buffer_in_use
) ? count
:
346 drvdata
->read_buffer_in_use
;
348 /* Return the data currently in the read buffer. */
349 if (copy_to_user(buf
, drvdata
->read_buffer
, bytes_to_read
)) {
353 drvdata
->read_buffer_in_use
-= bytes_to_read
;
354 memmove(drvdata
->read_buffer
,
355 drvdata
->read_buffer
+ bytes_to_read
,
358 /* Get new data from the ICAP, and return was was requested. */
359 kbuf
= (u32
*) get_zeroed_page(GFP_KERNEL
);
365 /* The ICAP device is only able to read complete */
366 /* words. If a number of bytes that do not correspond */
367 /* to complete words is requested, then we read enough */
368 /* words to get the required number of bytes, and then */
369 /* save the remaining bytes for the next read. */
371 /* Determine the number of words to read, rounding up */
373 words
= ((count
+ 3) >> 2);
374 bytes_to_read
= words
<< 2;
376 if (bytes_to_read
> PAGE_SIZE
)
377 bytes_to_read
= PAGE_SIZE
;
379 /* Ensure we only read a complete number of words. */
380 bytes_remaining
= bytes_to_read
& 3;
382 words
= bytes_to_read
>> 2;
384 status
= drvdata
->config
->get_configuration(drvdata
,
387 /* If we didn't read correctly, then bail out. */
389 free_page((unsigned long)kbuf
);
393 /* If we fail to return the data to the user, then bail out. */
394 if (copy_to_user(buf
, kbuf
, bytes_to_read
)) {
395 free_page((unsigned long)kbuf
);
399 memcpy(drvdata
->read_buffer
,
402 drvdata
->read_buffer_in_use
= bytes_remaining
;
403 free_page((unsigned long)kbuf
);
405 status
= bytes_to_read
;
407 mutex_unlock(&drvdata
->sem
);
412 hwicap_write(struct file
*file
, const char __user
*buf
,
413 size_t count
, loff_t
*ppos
)
415 struct hwicap_drvdata
*drvdata
= file
->private_data
;
417 ssize_t left
= count
;
422 status
= mutex_lock_interruptible(&drvdata
->sem
);
426 left
+= drvdata
->write_buffer_in_use
;
428 /* Only write multiples of 4 bytes. */
434 kbuf
= (u32
*) __get_free_page(GFP_KERNEL
);
441 /* only write multiples of 4 bytes, so there might */
442 /* be as many as 3 bytes left (at the end). */
449 if (drvdata
->write_buffer_in_use
) {
450 memcpy(kbuf
, drvdata
->write_buffer
,
451 drvdata
->write_buffer_in_use
);
453 (((char *)kbuf
) + drvdata
->write_buffer_in_use
),
455 len
- (drvdata
->write_buffer_in_use
))) {
456 free_page((unsigned long)kbuf
);
461 if (copy_from_user(kbuf
, buf
+ written
, len
)) {
462 free_page((unsigned long)kbuf
);
468 status
= drvdata
->config
->set_configuration(drvdata
,
472 free_page((unsigned long)kbuf
);
476 if (drvdata
->write_buffer_in_use
) {
477 len
-= drvdata
->write_buffer_in_use
;
478 left
-= drvdata
->write_buffer_in_use
;
479 drvdata
->write_buffer_in_use
= 0;
484 if ((left
> 0) && (left
< 4)) {
485 if (!copy_from_user(drvdata
->write_buffer
,
486 buf
+ written
, left
)) {
487 drvdata
->write_buffer_in_use
= left
;
493 free_page((unsigned long)kbuf
);
496 mutex_unlock(&drvdata
->sem
);
500 static int hwicap_open(struct inode
*inode
, struct file
*file
)
502 struct hwicap_drvdata
*drvdata
;
506 drvdata
= container_of(inode
->i_cdev
, struct hwicap_drvdata
, cdev
);
508 status
= mutex_lock_interruptible(&drvdata
->sem
);
512 if (drvdata
->is_open
) {
517 status
= hwicap_initialize_hwicap(drvdata
);
519 dev_err(drvdata
->dev
, "Failed to open file");
523 file
->private_data
= drvdata
;
524 drvdata
->write_buffer_in_use
= 0;
525 drvdata
->read_buffer_in_use
= 0;
526 drvdata
->is_open
= 1;
529 mutex_unlock(&drvdata
->sem
);
535 static int hwicap_release(struct inode
*inode
, struct file
*file
)
537 struct hwicap_drvdata
*drvdata
= file
->private_data
;
541 mutex_lock(&drvdata
->sem
);
543 if (drvdata
->write_buffer_in_use
) {
544 /* Flush write buffer. */
545 for (i
= drvdata
->write_buffer_in_use
; i
< 4; i
++)
546 drvdata
->write_buffer
[i
] = 0;
548 status
= drvdata
->config
->set_configuration(drvdata
,
549 (u32
*) drvdata
->write_buffer
, 1);
554 status
= hwicap_command_desync(drvdata
);
559 drvdata
->is_open
= 0;
560 mutex_unlock(&drvdata
->sem
);
564 static const struct file_operations hwicap_fops
= {
565 .owner
= THIS_MODULE
,
566 .write
= hwicap_write
,
569 .release
= hwicap_release
,
572 static int __devinit
hwicap_setup(struct device
*dev
, int id
,
573 const struct resource
*regs_res
,
574 const struct hwicap_driver_config
*config
,
575 const struct config_registers
*config_regs
)
578 struct hwicap_drvdata
*drvdata
= NULL
;
581 dev_info(dev
, "Xilinx icap port driver\n");
583 mutex_lock(&icap_sem
);
586 for (id
= 0; id
< HWICAP_DEVICES
; id
++)
587 if (!probed_devices
[id
])
590 if (id
< 0 || id
>= HWICAP_DEVICES
) {
591 mutex_unlock(&icap_sem
);
592 dev_err(dev
, "%s%i too large\n", DRIVER_NAME
, id
);
595 if (probed_devices
[id
]) {
596 mutex_unlock(&icap_sem
);
597 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
602 probed_devices
[id
] = 1;
603 mutex_unlock(&icap_sem
);
605 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
+ id
);
607 drvdata
= kzalloc(sizeof(struct hwicap_drvdata
), GFP_KERNEL
);
609 dev_err(dev
, "Couldn't allocate device private record\n");
613 dev_set_drvdata(dev
, (void *)drvdata
);
616 dev_err(dev
, "Couldn't get registers resource\n");
621 drvdata
->mem_start
= regs_res
->start
;
622 drvdata
->mem_end
= regs_res
->end
;
623 drvdata
->mem_size
= regs_res
->end
- regs_res
->start
+ 1;
625 if (!request_mem_region(drvdata
->mem_start
,
626 drvdata
->mem_size
, DRIVER_NAME
)) {
627 dev_err(dev
, "Couldn't lock memory region at %Lx\n",
628 (unsigned long long) regs_res
->start
);
633 drvdata
->devt
= devt
;
635 drvdata
->base_address
= ioremap(drvdata
->mem_start
, drvdata
->mem_size
);
636 if (!drvdata
->base_address
) {
637 dev_err(dev
, "ioremap() failed\n");
641 drvdata
->config
= config
;
642 drvdata
->config_regs
= config_regs
;
644 mutex_init(&drvdata
->sem
);
645 drvdata
->is_open
= 0;
647 dev_info(dev
, "ioremap %llx to %p with size %llx\n",
648 (unsigned long long) drvdata
->mem_start
,
649 drvdata
->base_address
,
650 (unsigned long long) drvdata
->mem_size
);
652 cdev_init(&drvdata
->cdev
, &hwicap_fops
);
653 drvdata
->cdev
.owner
= THIS_MODULE
;
654 retval
= cdev_add(&drvdata
->cdev
, devt
, 1);
656 dev_err(dev
, "cdev_add() failed\n");
660 device_create(icap_class
, dev
, devt
, NULL
, "%s%d", DRIVER_NAME
, id
);
661 return 0; /* success */
664 iounmap(drvdata
->base_address
);
667 release_mem_region(regs_res
->start
, drvdata
->mem_size
);
673 mutex_lock(&icap_sem
);
674 probed_devices
[id
] = 0;
675 mutex_unlock(&icap_sem
);
680 static struct hwicap_driver_config buffer_icap_config
= {
681 .get_configuration
= buffer_icap_get_configuration
,
682 .set_configuration
= buffer_icap_set_configuration
,
683 .get_status
= buffer_icap_get_status
,
684 .reset
= buffer_icap_reset
,
687 static struct hwicap_driver_config fifo_icap_config
= {
688 .get_configuration
= fifo_icap_get_configuration
,
689 .set_configuration
= fifo_icap_set_configuration
,
690 .get_status
= fifo_icap_get_status
,
691 .reset
= fifo_icap_reset
,
694 static int __devexit
hwicap_remove(struct device
*dev
)
696 struct hwicap_drvdata
*drvdata
;
698 drvdata
= (struct hwicap_drvdata
*)dev_get_drvdata(dev
);
703 device_destroy(icap_class
, drvdata
->devt
);
704 cdev_del(&drvdata
->cdev
);
705 iounmap(drvdata
->base_address
);
706 release_mem_region(drvdata
->mem_start
, drvdata
->mem_size
);
708 dev_set_drvdata(dev
, NULL
);
710 mutex_lock(&icap_sem
);
711 probed_devices
[MINOR(dev
->devt
)-XHWICAP_MINOR
] = 0;
712 mutex_unlock(&icap_sem
);
713 return 0; /* success */
716 static int __devinit
hwicap_drv_probe(struct platform_device
*pdev
)
718 struct resource
*res
;
719 const struct config_registers
*regs
;
722 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
726 /* It's most likely that we're using V4, if the family is not
728 regs
= &v4_config_registers
;
729 family
= pdev
->dev
.platform_data
;
732 if (!strcmp(family
, "virtex2p")) {
733 regs
= &v2_config_registers
;
734 } else if (!strcmp(family
, "virtex4")) {
735 regs
= &v4_config_registers
;
736 } else if (!strcmp(family
, "virtex5")) {
737 regs
= &v5_config_registers
;
741 return hwicap_setup(&pdev
->dev
, pdev
->id
, res
,
742 &buffer_icap_config
, regs
);
745 static int __devexit
hwicap_drv_remove(struct platform_device
*pdev
)
747 return hwicap_remove(&pdev
->dev
);
750 static struct platform_driver hwicap_platform_driver
= {
751 .probe
= hwicap_drv_probe
,
752 .remove
= hwicap_drv_remove
,
754 .owner
= THIS_MODULE
,
759 /* ---------------------------------------------------------------------
763 #if defined(CONFIG_OF)
765 hwicap_of_probe(struct platform_device
*op
, const struct of_device_id
*match
)
768 const unsigned int *id
;
771 const struct hwicap_driver_config
*config
= match
->data
;
772 const struct config_registers
*regs
;
774 dev_dbg(&op
->dev
, "hwicap_of_probe(%p, %p)\n", op
, match
);
776 rc
= of_address_to_resource(op
->dev
.of_node
, 0, &res
);
778 dev_err(&op
->dev
, "invalid address\n");
782 id
= of_get_property(op
->dev
.of_node
, "port-number", NULL
);
784 /* It's most likely that we're using V4, if the family is not
786 regs
= &v4_config_registers
;
787 family
= of_get_property(op
->dev
.of_node
, "xlnx,family", NULL
);
790 if (!strcmp(family
, "virtex2p")) {
791 regs
= &v2_config_registers
;
792 } else if (!strcmp(family
, "virtex4")) {
793 regs
= &v4_config_registers
;
794 } else if (!strcmp(family
, "virtex5")) {
795 regs
= &v5_config_registers
;
798 return hwicap_setup(&op
->dev
, id
? *id
: -1, &res
, config
,
802 static int __devexit
hwicap_of_remove(struct platform_device
*op
)
804 return hwicap_remove(&op
->dev
);
807 /* Match table for of_platform binding */
808 static const struct of_device_id __devinitconst hwicap_of_match
[] = {
809 { .compatible
= "xlnx,opb-hwicap-1.00.b", .data
= &buffer_icap_config
},
810 { .compatible
= "xlnx,xps-hwicap-1.00.a", .data
= &fifo_icap_config
},
813 MODULE_DEVICE_TABLE(of
, hwicap_of_match
);
815 static struct of_platform_driver hwicap_of_driver
= {
816 .probe
= hwicap_of_probe
,
817 .remove
= __devexit_p(hwicap_of_remove
),
820 .owner
= THIS_MODULE
,
821 .of_match_table
= hwicap_of_match
,
825 /* Registration helpers to keep the number of #ifdefs to a minimum */
826 static inline int __init
hwicap_of_register(void)
828 pr_debug("hwicap: calling of_register_platform_driver()\n");
829 return of_register_platform_driver(&hwicap_of_driver
);
832 static inline void __exit
hwicap_of_unregister(void)
834 of_unregister_platform_driver(&hwicap_of_driver
);
836 #else /* CONFIG_OF */
837 /* CONFIG_OF not enabled; do nothing helpers */
838 static inline int __init
hwicap_of_register(void) { return 0; }
839 static inline void __exit
hwicap_of_unregister(void) { }
840 #endif /* CONFIG_OF */
842 static int __init
hwicap_module_init(void)
847 icap_class
= class_create(THIS_MODULE
, "xilinx_config");
848 mutex_init(&icap_sem
);
850 devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
851 retval
= register_chrdev_region(devt
,
857 retval
= platform_driver_register(&hwicap_platform_driver
);
862 retval
= hwicap_of_register();
870 platform_driver_unregister(&hwicap_platform_driver
);
873 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
878 static void __exit
hwicap_module_cleanup(void)
880 dev_t devt
= MKDEV(XHWICAP_MAJOR
, XHWICAP_MINOR
);
882 class_destroy(icap_class
);
884 platform_driver_unregister(&hwicap_platform_driver
);
886 hwicap_of_unregister();
888 unregister_chrdev_region(devt
, HWICAP_DEVICES
);
891 module_init(hwicap_module_init
);
892 module_exit(hwicap_module_cleanup
);
894 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
895 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
896 MODULE_LICENSE("GPL");