device create: char: convert device_create to device_create_drvdata
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / xilinx_hwicap / xilinx_hwicap.c
blob51966ccf4ea3f56c4449f88c93906ab81e136111
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/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/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>
91 #include <linux/fs.h>
92 #include <linux/cdev.h>
93 #include <linux/platform_device.h>
95 #include <asm/io.h>
96 #include <asm/uaccess.h>
97 #include <asm/system.h>
99 #ifdef CONFIG_OF
100 /* For open firmware. */
101 #include <linux/of_device.h>
102 #include <linux/of_platform.h>
103 #endif
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 = {
126 .CRC = 0,
127 .FAR = 1,
128 .FDRI = 2,
129 .FDRO = 3,
130 .CMD = 4,
131 .CTL = 5,
132 .MASK = 6,
133 .STAT = 7,
134 .LOUT = 8,
135 .COR = 9,
136 .MFWR = 10,
137 .FLR = 11,
138 .KEY = 12,
139 .CBC = 13,
140 .IDCODE = 14,
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 = {
151 .CRC = 0,
152 .FAR = 1,
153 .FDRI = 2,
154 .FDRO = 3,
155 .CMD = 4,
156 .CTL = 5,
157 .MASK = 6,
158 .STAT = 7,
159 .LOUT = 8,
160 .COR = 9,
161 .MFWR = 10,
162 .FLR = UNIMPLEMENTED,
163 .KEY = UNIMPLEMENTED,
164 .CBC = 11,
165 .IDCODE = 12,
166 .AXSS = 13,
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 = {
175 .CRC = 0,
176 .FAR = 1,
177 .FDRI = 2,
178 .FDRO = 3,
179 .CMD = 4,
180 .CTL = 5,
181 .MASK = 6,
182 .STAT = 7,
183 .LOUT = 8,
184 .COR = 9,
185 .MFWR = 10,
186 .FLR = UNIMPLEMENTED,
187 .KEY = UNIMPLEMENTED,
188 .CBC = 11,
189 .IDCODE = 12,
190 .AXSS = 13,
191 .C0R_1 = 14,
192 .CSOB = 15,
193 .WBSTAR = 16,
194 .TIMER = 17,
195 .BOOTSTS = 18,
196 .CTL_1 = 19,
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)
209 u32 buffer[4];
210 u32 index = 0;
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,
225 &buffer[0], index);
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)
242 int status;
243 u32 buffer[6];
244 u32 index = 0;
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,
260 &buffer[0], index);
261 if (status)
262 return status;
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)
267 return -EIO;
269 index = 0;
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,
279 &buffer[0], index);
280 if (status)
281 return status;
284 * Read the configuration register
286 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
287 if (status)
288 return status;
290 return 0;
293 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
295 int status;
296 u32 idcode;
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);
307 if (status)
308 return status;
310 /* Attempt to read the IDCODE from ICAP. This
311 * may not be returned correctly, due to the design of the
312 * hardware.
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);
318 if (status)
319 return status;
321 dev_dbg(drvdata->dev, "Desync...\n");
322 status = hwicap_command_desync(drvdata);
323 if (status)
324 return status;
326 return 0;
329 static ssize_t
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;
334 u32 *kbuf;
335 u32 words;
336 u32 bytes_remaining;
337 int status;
339 status = mutex_lock_interruptible(&drvdata->sem);
340 if (status)
341 return status;
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 */
346 /* ICAP device. */
347 bytes_to_read =
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)) {
353 status = -EFAULT;
354 goto error;
356 drvdata->read_buffer_in_use -= bytes_to_read;
357 memmove(drvdata->read_buffer,
358 drvdata->read_buffer + bytes_to_read,
359 4 - bytes_to_read);
360 } else {
361 /* Get new data from the ICAP, and return was was requested. */
362 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
363 if (!kbuf) {
364 status = -ENOMEM;
365 goto error;
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 */
375 /* if necessary. */
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;
384 bytes_to_read &= ~3;
385 words = bytes_to_read >> 2;
387 status = drvdata->config->get_configuration(drvdata,
388 kbuf, words);
390 /* If we didn't read correctly, then bail out. */
391 if (status) {
392 free_page((unsigned long)kbuf);
393 goto error;
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);
399 status = -EFAULT;
400 goto error;
402 memcpy(drvdata->read_buffer,
403 kbuf,
404 bytes_remaining);
405 drvdata->read_buffer_in_use = bytes_remaining;
406 free_page((unsigned long)kbuf);
408 status = bytes_to_read;
409 error:
410 mutex_unlock(&drvdata->sem);
411 return status;
414 static ssize_t
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;
419 ssize_t written = 0;
420 ssize_t left = count;
421 u32 *kbuf;
422 ssize_t len;
423 ssize_t status;
425 status = mutex_lock_interruptible(&drvdata->sem);
426 if (status)
427 return status;
429 left += drvdata->write_buffer_in_use;
431 /* Only write multiples of 4 bytes. */
432 if (left < 4) {
433 status = 0;
434 goto error;
437 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
438 if (!kbuf) {
439 status = -ENOMEM;
440 goto error;
443 while (left > 3) {
444 /* only write multiples of 4 bytes, so there might */
445 /* be as many as 3 bytes left (at the end). */
446 len = left;
448 if (len > PAGE_SIZE)
449 len = PAGE_SIZE;
450 len &= ~3;
452 if (drvdata->write_buffer_in_use) {
453 memcpy(kbuf, drvdata->write_buffer,
454 drvdata->write_buffer_in_use);
455 if (copy_from_user(
456 (((char *)kbuf) + drvdata->write_buffer_in_use),
457 buf + written,
458 len - (drvdata->write_buffer_in_use))) {
459 free_page((unsigned long)kbuf);
460 status = -EFAULT;
461 goto error;
463 } else {
464 if (copy_from_user(kbuf, buf + written, len)) {
465 free_page((unsigned long)kbuf);
466 status = -EFAULT;
467 goto error;
471 status = drvdata->config->set_configuration(drvdata,
472 kbuf, len >> 2);
474 if (status) {
475 free_page((unsigned long)kbuf);
476 status = -EFAULT;
477 goto error;
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;
484 written += len;
485 left -= len;
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;
491 written += left;
492 left = 0;
496 free_page((unsigned long)kbuf);
497 status = written;
498 error:
499 mutex_unlock(&drvdata->sem);
500 return status;
503 static int hwicap_open(struct inode *inode, struct file *file)
505 struct hwicap_drvdata *drvdata;
506 int status;
508 lock_kernel();
509 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
511 status = mutex_lock_interruptible(&drvdata->sem);
512 if (status)
513 goto out;
515 if (drvdata->is_open) {
516 status = -EBUSY;
517 goto error;
520 status = hwicap_initialize_hwicap(drvdata);
521 if (status) {
522 dev_err(drvdata->dev, "Failed to open file");
523 goto error;
526 file->private_data = drvdata;
527 drvdata->write_buffer_in_use = 0;
528 drvdata->read_buffer_in_use = 0;
529 drvdata->is_open = 1;
531 error:
532 mutex_unlock(&drvdata->sem);
533 out:
534 unlock_kernel();
535 return status;
538 static int hwicap_release(struct inode *inode, struct file *file)
540 struct hwicap_drvdata *drvdata = file->private_data;
541 int i;
542 int status = 0;
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);
553 if (status)
554 goto error;
557 status = hwicap_command_desync(drvdata);
558 if (status)
559 goto error;
561 error:
562 drvdata->is_open = 0;
563 mutex_unlock(&drvdata->sem);
564 return status;
567 static struct file_operations hwicap_fops = {
568 .owner = THIS_MODULE,
569 .write = hwicap_write,
570 .read = hwicap_read,
571 .open = hwicap_open,
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)
580 dev_t devt;
581 struct hwicap_drvdata *drvdata = NULL;
582 int retval = 0;
584 dev_info(dev, "Xilinx icap port driver\n");
586 mutex_lock(&icap_sem);
588 if (id < 0) {
589 for (id = 0; id < HWICAP_DEVICES; id++)
590 if (!probed_devices[id])
591 break;
593 if (id < 0 || id >= HWICAP_DEVICES) {
594 mutex_unlock(&icap_sem);
595 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
596 return -EINVAL;
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",
601 DRIVER_NAME, id);
602 return -EBUSY;
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);
611 if (!drvdata) {
612 dev_err(dev, "Couldn't allocate device private record\n");
613 retval = -ENOMEM;
614 goto failed0;
616 dev_set_drvdata(dev, (void *)drvdata);
618 if (!regs_res) {
619 dev_err(dev, "Couldn't get registers resource\n");
620 retval = -EFAULT;
621 goto failed1;
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",
631 regs_res->start);
632 retval = -EBUSY;
633 goto failed1;
636 drvdata->devt = devt;
637 drvdata->dev = dev;
638 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
639 if (!drvdata->base_address) {
640 dev_err(dev, "ioremap() failed\n");
641 goto failed2;
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);
657 if (retval) {
658 dev_err(dev, "cdev_add() failed\n");
659 goto failed3;
662 device_create_drvdata(icap_class, dev, devt, NULL,
663 "%s%d", DRIVER_NAME, id);
664 return 0; /* success */
666 failed3:
667 iounmap(drvdata->base_address);
669 failed2:
670 release_mem_region(regs_res->start, drvdata->mem_size);
672 failed1:
673 kfree(drvdata);
675 failed0:
676 mutex_lock(&icap_sem);
677 probed_devices[id] = 0;
678 mutex_unlock(&icap_sem);
680 return retval;
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);
703 if (!drvdata)
704 return 0;
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);
710 kfree(drvdata);
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;
723 const char *family;
725 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
726 if (!res)
727 return -ENODEV;
729 /* It's most likely that we're using V4, if the family is not
730 specified */
731 regs = &v4_config_registers;
732 family = pdev->dev.platform_data;
734 if (family) {
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,
756 .driver = {
757 .owner = THIS_MODULE,
758 .name = DRIVER_NAME,
762 /* ---------------------------------------------------------------------
763 * OF bus binding
766 #if defined(CONFIG_OF)
767 static int __devinit
768 hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
770 struct resource res;
771 const unsigned int *id;
772 const char *family;
773 int rc;
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);
780 if (rc) {
781 dev_err(&op->dev, "invalid address\n");
782 return rc;
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
788 specified */
789 regs = &v4_config_registers;
790 family = of_get_property(op->node, "xlnx,family", NULL);
792 if (family) {
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,
802 regs);
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,
820 .name = DRIVER_NAME,
821 .match_table = hwicap_of_match,
822 .probe = hwicap_of_probe,
823 .remove = __devexit_p(hwicap_of_remove),
824 .driver = {
825 .name = DRIVER_NAME,
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)
848 dev_t devt;
849 int retval;
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,
856 HWICAP_DEVICES,
857 DRIVER_NAME);
858 if (retval < 0)
859 return retval;
861 retval = platform_driver_register(&hwicap_platform_driver);
863 if (retval)
864 goto failed1;
866 retval = hwicap_of_register();
868 if (retval)
869 goto failed2;
871 return retval;
873 failed2:
874 platform_driver_unregister(&hwicap_platform_driver);
876 failed1:
877 unregister_chrdev_region(devt, HWICAP_DEVICES);
879 return retval;
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");