iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / char / xilinx_hwicap / xilinx_hwicap.c
blob2284fa2a5a5726c52c873e4f13caff36cfab5f3b
1 /*****************************************************************************
3 * Author: Xilinx, Inc.
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
60 * inaccessible state.
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>
90 #include <linux/fs.h>
91 #include <linux/cdev.h>
92 #include <linux/platform_device.h>
94 #include <asm/io.h>
95 #include <asm/uaccess.h>
96 #include <asm/system.h>
98 #ifdef CONFIG_OF
99 /* For open firmware. */
100 #include <linux/of_device.h>
101 #include <linux/of_platform.h>
102 #endif
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 = {
129 .CRC = 0,
130 .FAR = 1,
131 .FDRI = 2,
132 .FDRO = 3,
133 .CMD = 4,
134 .CTL = 5,
135 .MASK = 6,
136 .STAT = 7,
137 .LOUT = 8,
138 .COR = 9,
139 .MFWR = 10,
140 .FLR = 11,
141 .KEY = 12,
142 .CBC = 13,
143 .IDCODE = 14,
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 = {
154 .CRC = 0,
155 .FAR = 1,
156 .FDRI = 2,
157 .FDRO = 3,
158 .CMD = 4,
159 .CTL = 5,
160 .MASK = 6,
161 .STAT = 7,
162 .LOUT = 8,
163 .COR = 9,
164 .MFWR = 10,
165 .FLR = UNIMPLEMENTED,
166 .KEY = UNIMPLEMENTED,
167 .CBC = 11,
168 .IDCODE = 12,
169 .AXSS = 13,
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 = {
178 .CRC = 0,
179 .FAR = 1,
180 .FDRI = 2,
181 .FDRO = 3,
182 .CMD = 4,
183 .CTL = 5,
184 .MASK = 6,
185 .STAT = 7,
186 .LOUT = 8,
187 .COR = 9,
188 .MFWR = 10,
189 .FLR = UNIMPLEMENTED,
190 .KEY = UNIMPLEMENTED,
191 .CBC = 11,
192 .IDCODE = 12,
193 .AXSS = 13,
194 .C0R_1 = 14,
195 .CSOB = 15,
196 .WBSTAR = 16,
197 .TIMER = 17,
198 .BOOTSTS = 18,
199 .CTL_1 = 19,
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)
212 u32 buffer[4];
213 u32 index = 0;
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,
228 &buffer[0], index);
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)
245 int status;
246 u32 buffer[6];
247 u32 index = 0;
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,
264 &buffer[0], index);
265 if (status)
266 return status;
269 * Read the configuration register
271 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
272 if (status)
273 return status;
275 return 0;
278 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
280 int status;
281 u32 idcode;
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);
292 if (status)
293 return status;
295 /* Attempt to read the IDCODE from ICAP. This
296 * may not be returned correctly, due to the design of the
297 * hardware.
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);
303 if (status)
304 return status;
306 dev_dbg(drvdata->dev, "Desync...\n");
307 status = hwicap_command_desync(drvdata);
308 if (status)
309 return status;
311 return 0;
314 static ssize_t
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;
319 u32 *kbuf;
320 u32 words;
321 u32 bytes_remaining;
322 int status;
324 status = mutex_lock_interruptible(&drvdata->sem);
325 if (status)
326 return status;
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 */
331 /* ICAP device. */
332 bytes_to_read =
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)) {
338 status = -EFAULT;
339 goto error;
341 drvdata->read_buffer_in_use -= bytes_to_read;
342 memmove(drvdata->read_buffer,
343 drvdata->read_buffer + bytes_to_read,
344 4 - bytes_to_read);
345 } else {
346 /* Get new data from the ICAP, and return was was requested. */
347 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
348 if (!kbuf) {
349 status = -ENOMEM;
350 goto error;
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 */
360 /* if necessary. */
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;
369 bytes_to_read &= ~3;
370 words = bytes_to_read >> 2;
372 status = drvdata->config->get_configuration(drvdata,
373 kbuf, words);
375 /* If we didn't read correctly, then bail out. */
376 if (status) {
377 free_page((unsigned long)kbuf);
378 goto error;
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);
384 status = -EFAULT;
385 goto error;
387 memcpy(drvdata->read_buffer,
388 kbuf,
389 bytes_remaining);
390 drvdata->read_buffer_in_use = bytes_remaining;
391 free_page((unsigned long)kbuf);
393 status = bytes_to_read;
394 error:
395 mutex_unlock(&drvdata->sem);
396 return status;
399 static ssize_t
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;
404 ssize_t written = 0;
405 ssize_t left = count;
406 u32 *kbuf;
407 ssize_t len;
408 ssize_t status;
410 status = mutex_lock_interruptible(&drvdata->sem);
411 if (status)
412 return status;
414 left += drvdata->write_buffer_in_use;
416 /* Only write multiples of 4 bytes. */
417 if (left < 4) {
418 status = 0;
419 goto error;
422 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
423 if (!kbuf) {
424 status = -ENOMEM;
425 goto error;
428 while (left > 3) {
429 /* only write multiples of 4 bytes, so there might */
430 /* be as many as 3 bytes left (at the end). */
431 len = left;
433 if (len > PAGE_SIZE)
434 len = PAGE_SIZE;
435 len &= ~3;
437 if (drvdata->write_buffer_in_use) {
438 memcpy(kbuf, drvdata->write_buffer,
439 drvdata->write_buffer_in_use);
440 if (copy_from_user(
441 (((char *)kbuf) + drvdata->write_buffer_in_use),
442 buf + written,
443 len - (drvdata->write_buffer_in_use))) {
444 free_page((unsigned long)kbuf);
445 status = -EFAULT;
446 goto error;
448 } else {
449 if (copy_from_user(kbuf, buf + written, len)) {
450 free_page((unsigned long)kbuf);
451 status = -EFAULT;
452 goto error;
456 status = drvdata->config->set_configuration(drvdata,
457 kbuf, len >> 2);
459 if (status) {
460 free_page((unsigned long)kbuf);
461 status = -EFAULT;
462 goto error;
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;
469 written += len;
470 left -= len;
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;
476 written += left;
477 left = 0;
481 free_page((unsigned long)kbuf);
482 status = written;
483 error:
484 mutex_unlock(&drvdata->sem);
485 return status;
488 static int hwicap_open(struct inode *inode, struct file *file)
490 struct hwicap_drvdata *drvdata;
491 int status;
493 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
495 status = mutex_lock_interruptible(&drvdata->sem);
496 if (status)
497 return status;
499 if (drvdata->is_open) {
500 status = -EBUSY;
501 goto error;
504 status = hwicap_initialize_hwicap(drvdata);
505 if (status) {
506 dev_err(drvdata->dev, "Failed to open file");
507 goto error;
510 file->private_data = drvdata;
511 drvdata->write_buffer_in_use = 0;
512 drvdata->read_buffer_in_use = 0;
513 drvdata->is_open = 1;
515 error:
516 mutex_unlock(&drvdata->sem);
517 return status;
520 static int hwicap_release(struct inode *inode, struct file *file)
522 struct hwicap_drvdata *drvdata = file->private_data;
523 int i;
524 int status = 0;
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);
535 if (status)
536 goto error;
539 status = hwicap_command_desync(drvdata);
540 if (status)
541 goto error;
543 error:
544 drvdata->is_open = 0;
545 mutex_unlock(&drvdata->sem);
546 return status;
549 static struct file_operations hwicap_fops = {
550 .owner = THIS_MODULE,
551 .write = hwicap_write,
552 .read = hwicap_read,
553 .open = hwicap_open,
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)
562 dev_t devt;
563 struct hwicap_drvdata *drvdata = NULL;
564 int retval = 0;
566 dev_info(dev, "Xilinx icap port driver\n");
568 mutex_lock(&icap_sem);
570 if (id < 0) {
571 for (id = 0; id < HWICAP_DEVICES; id++)
572 if (!probed_devices[id])
573 break;
575 if (id < 0 || id >= HWICAP_DEVICES) {
576 mutex_unlock(&icap_sem);
577 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
578 return -EINVAL;
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",
583 DRIVER_NAME, id);
584 return -EBUSY;
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);
593 if (!drvdata) {
594 dev_err(dev, "Couldn't allocate device private record\n");
595 retval = -ENOMEM;
596 goto failed0;
598 dev_set_drvdata(dev, (void *)drvdata);
600 if (!regs_res) {
601 dev_err(dev, "Couldn't get registers resource\n");
602 retval = -EFAULT;
603 goto failed1;
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);
614 retval = -EBUSY;
615 goto failed1;
618 drvdata->devt = devt;
619 drvdata->dev = dev;
620 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
621 if (!drvdata->base_address) {
622 dev_err(dev, "ioremap() failed\n");
623 goto failed2;
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);
639 if (retval) {
640 dev_err(dev, "cdev_add() failed\n");
641 goto failed3;
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 */
647 failed3:
648 iounmap(drvdata->base_address);
650 failed2:
651 release_mem_region(regs_res->start, drvdata->mem_size);
653 failed1:
654 kfree(drvdata);
656 failed0:
657 mutex_lock(&icap_sem);
658 probed_devices[id] = 0;
659 mutex_unlock(&icap_sem);
661 return retval;
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);
682 if (!drvdata)
683 return 0;
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);
689 kfree(drvdata);
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;
702 const char *family;
704 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
705 if (!res)
706 return -ENODEV;
708 /* It's most likely that we're using V4, if the family is not
709 specified */
710 regs = &v4_config_registers;
711 family = pdev->dev.platform_data;
713 if (family) {
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,
735 .driver = {
736 .owner = THIS_MODULE,
737 .name = DRIVER_NAME,
741 /* ---------------------------------------------------------------------
742 * OF bus binding
745 #if defined(CONFIG_OF)
746 static int __devinit
747 hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
749 struct resource res;
750 const unsigned int *id;
751 const char *family;
752 int rc;
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);
759 if (rc) {
760 dev_err(&op->dev, "invalid address\n");
761 return rc;
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
767 specified */
768 regs = &v4_config_registers;
769 family = of_get_property(op->node, "xlnx,family", NULL);
771 if (family) {
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,
781 regs);
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,
799 .name = DRIVER_NAME,
800 .match_table = hwicap_of_match,
801 .probe = hwicap_of_probe,
802 .remove = __devexit_p(hwicap_of_remove),
803 .driver = {
804 .name = DRIVER_NAME,
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)
827 dev_t devt;
828 int retval;
830 icap_class = class_create(THIS_MODULE, "xilinx_config");
831 mutex_init(&icap_sem);
833 if (xhwicap_major) {
834 devt = MKDEV(xhwicap_major, xhwicap_minor);
835 retval = register_chrdev_region(
836 devt,
837 HWICAP_DEVICES,
838 DRIVER_NAME);
839 if (retval < 0)
840 return retval;
841 } else {
842 retval = alloc_chrdev_region(&devt,
843 xhwicap_minor,
844 HWICAP_DEVICES,
845 DRIVER_NAME);
846 if (retval < 0)
847 return retval;
848 xhwicap_major = MAJOR(devt);
851 retval = platform_driver_register(&hwicap_platform_driver);
853 if (retval)
854 goto failed1;
856 retval = hwicap_of_register();
858 if (retval)
859 goto failed2;
861 return retval;
863 failed2:
864 platform_driver_unregister(&hwicap_platform_driver);
866 failed1:
867 unregister_chrdev_region(devt, HWICAP_DEVICES);
869 return retval;
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");