Merge branch 'for-2.6.27' of git://linux-nfs.org/~bfields/linux
[linux-2.6] / drivers / char / xilinx_hwicap / xilinx_hwicap.c
1 /*****************************************************************************
2  *
3  *     Author: Xilinx, Inc.
4  *
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.
9  *
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.
23  *
24  *     Xilinx products are not intended for use in life support appliances,
25  *     devices, or systems. Use in such applications is expressly prohibited.
26  *
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.
31  *
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.
35  *
36  *****************************************************************************/
37
38 /*
39  * This is the code behind /dev/icap* -- it allows a user-space
40  * application to use the Xilinx ICAP subsystem.
41  *
42  * The following operations are possible:
43  *
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.
48  *
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.
61  *
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:
69  *
70  * cp foo.bit /dev/icap0
71  *
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.
75  */
76
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>
94
95 #include <asm/io.h>
96 #include <asm/uaccess.h>
97 #include <asm/system.h>
98
99 #ifdef CONFIG_OF
100 /* For open firmware. */
101 #include <linux/of_device.h>
102 #include <linux/of_platform.h>
103 #endif
104
105 #include "xilinx_hwicap.h"
106 #include "buffer_icap.h"
107 #include "fifo_icap.h"
108
109 #define DRIVER_NAME "icap"
110
111 #define HWICAP_REGS   (0x10000)
112
113 #define XHWICAP_MAJOR 259
114 #define XHWICAP_MINOR 0
115 #define HWICAP_DEVICES 1
116
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;
120
121 static struct class *icap_class;
122
123 #define UNIMPLEMENTED 0xFFFF
124
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,
148 };
149
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,
173 };
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,
197 };
198
199 /**
200  * hwicap_command_desync - Send a DESYNC command to the ICAP port.
201  * @drvdata: a pointer to the drvdata.
202  *
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.
206  */
207 static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
208 {
209         u32 buffer[4];
210         u32 index = 0;
211
212         /*
213          * Create the data to be written to the ICAP.
214          */
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;
219
220         /*
221          * Write the data to the FIFO and intiate the transfer of data present
222          * in the FIFO to the ICAP device.
223          */
224         return drvdata->config->set_configuration(drvdata,
225                         &buffer[0], index);
226 }
227
228 /**
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.
235  *
236  * Sends a query packet to the ICAP and then receives the response.
237  * The icap is left in Synched state.
238  */
239 static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
240                 u32 reg, u32 *reg_data)
241 {
242         int status;
243         u32 buffer[6];
244         u32 index = 0;
245
246         /*
247          * Create the data to be written to the ICAP.
248          */
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;
254
255         /*
256          * Write the data to the FIFO and initiate the transfer of data present
257          * in the FIFO to the ICAP device.
258          */
259         status = drvdata->config->set_configuration(drvdata,
260                                                     &buffer[0], index);
261         if (status)
262                 return status;
263
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;
268
269         index = 0;
270         buffer[index++] = hwicap_type_1_read(reg) | 1;
271         buffer[index++] = XHI_NOOP_PACKET;
272         buffer[index++] = XHI_NOOP_PACKET;
273
274         /*
275          * Write the data to the FIFO and intiate the transfer of data present
276          * in the FIFO to the ICAP device.
277          */
278         status = drvdata->config->set_configuration(drvdata,
279                         &buffer[0], index);
280         if (status)
281                 return status;
282
283         /*
284          * Read the configuration register
285          */
286         status = drvdata->config->get_configuration(drvdata, reg_data, 1);
287         if (status)
288                 return status;
289
290         return 0;
291 }
292
293 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
294 {
295         int status;
296         u32 idcode;
297
298         dev_dbg(drvdata->dev, "initializing\n");
299
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);
304
305         dev_dbg(drvdata->dev, "Desync...\n");
306         status = hwicap_command_desync(drvdata);
307         if (status)
308                 return status;
309
310         /* Attempt to read the IDCODE from ICAP.  This
311          * may not be returned correctly, due to the design of the
312          * hardware.
313          */
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;
320
321         dev_dbg(drvdata->dev, "Desync...\n");
322         status = hwicap_command_desync(drvdata);
323         if (status)
324                 return status;
325
326         return 0;
327 }
328
329 static ssize_t
330 hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
331 {
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;
338
339         status = mutex_lock_interruptible(&drvdata->sem);
340         if (status)
341                 return status;
342
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;
350
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;
355                 }
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;
366                 }
367
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. */
373
374                 /* Determine the number of words to read, rounding up */
375                 /* if necessary. */
376                 words = ((count + 3) >> 2);
377                 bytes_to_read = words << 2;
378
379                 if (bytes_to_read > PAGE_SIZE)
380                         bytes_to_read = PAGE_SIZE;
381
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;
386
387                 status = drvdata->config->get_configuration(drvdata,
388                                 kbuf, words);
389
390                 /* If we didn't read correctly, then bail out. */
391                 if (status) {
392                         free_page((unsigned long)kbuf);
393                         goto error;
394                 }
395
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;
401                 }
402                 memcpy(drvdata->read_buffer,
403                        kbuf,
404                        bytes_remaining);
405                 drvdata->read_buffer_in_use = bytes_remaining;
406                 free_page((unsigned long)kbuf);
407         }
408         status = bytes_to_read;
409  error:
410         mutex_unlock(&drvdata->sem);
411         return status;
412 }
413
414 static ssize_t
415 hwicap_write(struct file *file, const char __user *buf,
416                 size_t count, loff_t *ppos)
417 {
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;
424
425         status = mutex_lock_interruptible(&drvdata->sem);
426         if (status)
427                 return status;
428
429         left += drvdata->write_buffer_in_use;
430
431         /* Only write multiples of 4 bytes. */
432         if (left < 4) {
433                 status = 0;
434                 goto error;
435         }
436
437         kbuf = (u32 *) __get_free_page(GFP_KERNEL);
438         if (!kbuf) {
439                 status = -ENOMEM;
440                 goto error;
441         }
442
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;
447
448                 if (len > PAGE_SIZE)
449                         len = PAGE_SIZE;
450                 len &= ~3;
451
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;
462                         }
463                 } else {
464                         if (copy_from_user(kbuf, buf + written, len)) {
465                                 free_page((unsigned long)kbuf);
466                                 status = -EFAULT;
467                                 goto error;
468                         }
469                 }
470
471                 status = drvdata->config->set_configuration(drvdata,
472                                 kbuf, len >> 2);
473
474                 if (status) {
475                         free_page((unsigned long)kbuf);
476                         status = -EFAULT;
477                         goto error;
478                 }
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;
483                 }
484                 written += len;
485                 left -= len;
486         }
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;
493                 }
494         }
495
496         free_page((unsigned long)kbuf);
497         status = written;
498  error:
499         mutex_unlock(&drvdata->sem);
500         return status;
501 }
502
503 static int hwicap_open(struct inode *inode, struct file *file)
504 {
505         struct hwicap_drvdata *drvdata;
506         int status;
507
508         lock_kernel();
509         drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
510
511         status = mutex_lock_interruptible(&drvdata->sem);
512         if (status)
513                 goto out;
514
515         if (drvdata->is_open) {
516                 status = -EBUSY;
517                 goto error;
518         }
519
520         status = hwicap_initialize_hwicap(drvdata);
521         if (status) {
522                 dev_err(drvdata->dev, "Failed to open file");
523                 goto error;
524         }
525
526         file->private_data = drvdata;
527         drvdata->write_buffer_in_use = 0;
528         drvdata->read_buffer_in_use = 0;
529         drvdata->is_open = 1;
530
531  error:
532         mutex_unlock(&drvdata->sem);
533  out:
534         unlock_kernel();
535         return status;
536 }
537
538 static int hwicap_release(struct inode *inode, struct file *file)
539 {
540         struct hwicap_drvdata *drvdata = file->private_data;
541         int i;
542         int status = 0;
543
544         mutex_lock(&drvdata->sem);
545
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;
550
551                 status = drvdata->config->set_configuration(drvdata,
552                                 (u32 *) drvdata->write_buffer, 1);
553                 if (status)
554                         goto error;
555         }
556
557         status = hwicap_command_desync(drvdata);
558         if (status)
559                 goto error;
560
561  error:
562         drvdata->is_open = 0;
563         mutex_unlock(&drvdata->sem);
564         return status;
565 }
566
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,
573 };
574
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)
579 {
580         dev_t devt;
581         struct hwicap_drvdata *drvdata = NULL;
582         int retval = 0;
583
584         dev_info(dev, "Xilinx icap port driver\n");
585
586         mutex_lock(&icap_sem);
587
588         if (id < 0) {
589                 for (id = 0; id < HWICAP_DEVICES; id++)
590                         if (!probed_devices[id])
591                                 break;
592         }
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;
597         }
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;
603         }
604
605         probed_devices[id] = 1;
606         mutex_unlock(&icap_sem);
607
608         devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
609
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;
615         }
616         dev_set_drvdata(dev, (void *)drvdata);
617
618         if (!regs_res) {
619                 dev_err(dev, "Couldn't get registers resource\n");
620                 retval = -EFAULT;
621                 goto failed1;
622         }
623
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;
627
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;
634         }
635
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;
642         }
643
644         drvdata->config = config;
645         drvdata->config_regs = config_regs;
646
647         mutex_init(&drvdata->sem);
648         drvdata->is_open = 0;
649
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);
653
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;
660         }
661         /*  devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
662         device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
663         return 0;               /* success */
664
665  failed3:
666         iounmap(drvdata->base_address);
667
668  failed2:
669         release_mem_region(regs_res->start, drvdata->mem_size);
670
671  failed1:
672         kfree(drvdata);
673
674  failed0:
675         mutex_lock(&icap_sem);
676         probed_devices[id] = 0;
677         mutex_unlock(&icap_sem);
678
679         return retval;
680 }
681
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,
687 };
688
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,
694 };
695
696 static int __devexit hwicap_remove(struct device *dev)
697 {
698         struct hwicap_drvdata *drvdata;
699
700         drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
701
702         if (!drvdata)
703                 return 0;
704
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);
709         kfree(drvdata);
710         dev_set_drvdata(dev, NULL);
711
712         mutex_lock(&icap_sem);
713         probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
714         mutex_unlock(&icap_sem);
715         return 0;               /* success */
716 }
717
718 static int __devinit hwicap_drv_probe(struct platform_device *pdev)
719 {
720         struct resource *res;
721         const struct config_registers *regs;
722         const char *family;
723
724         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
725         if (!res)
726                 return -ENODEV;
727
728         /* It's most likely that we're using V4, if the family is not
729            specified */
730         regs = &v4_config_registers;
731         family = pdev->dev.platform_data;
732
733         if (family) {
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;
740                 }
741         }
742
743         return hwicap_setup(&pdev->dev, pdev->id, res,
744                         &buffer_icap_config, regs);
745 }
746
747 static int __devexit hwicap_drv_remove(struct platform_device *pdev)
748 {
749         return hwicap_remove(&pdev->dev);
750 }
751
752 static struct platform_driver hwicap_platform_driver = {
753         .probe = hwicap_drv_probe,
754         .remove = hwicap_drv_remove,
755         .driver = {
756                 .owner = THIS_MODULE,
757                 .name = DRIVER_NAME,
758         },
759 };
760
761 /* ---------------------------------------------------------------------
762  * OF bus binding
763  */
764
765 #if defined(CONFIG_OF)
766 static int __devinit
767 hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
768 {
769         struct resource res;
770         const unsigned int *id;
771         const char *family;
772         int rc;
773         const struct hwicap_driver_config *config = match->data;
774         const struct config_registers *regs;
775
776         dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
777
778         rc = of_address_to_resource(op->node, 0, &res);
779         if (rc) {
780                 dev_err(&op->dev, "invalid address\n");
781                 return rc;
782         }
783
784         id = of_get_property(op->node, "port-number", NULL);
785
786         /* It's most likely that we're using V4, if the family is not
787            specified */
788         regs = &v4_config_registers;
789         family = of_get_property(op->node, "xlnx,family", NULL);
790
791         if (family) {
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;
798                 }
799         }
800         return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
801                         regs);
802 }
803
804 static int __devexit hwicap_of_remove(struct of_device *op)
805 {
806         return hwicap_remove(&op->dev);
807 }
808
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},
813         {},
814 };
815 MODULE_DEVICE_TABLE(of, hwicap_of_match);
816
817 static struct of_platform_driver hwicap_of_driver = {
818         .owner = THIS_MODULE,
819         .name = DRIVER_NAME,
820         .match_table = hwicap_of_match,
821         .probe = hwicap_of_probe,
822         .remove = __devexit_p(hwicap_of_remove),
823         .driver = {
824                 .name = DRIVER_NAME,
825         },
826 };
827
828 /* Registration helpers to keep the number of #ifdefs to a minimum */
829 static inline int __init hwicap_of_register(void)
830 {
831         pr_debug("hwicap: calling of_register_platform_driver()\n");
832         return of_register_platform_driver(&hwicap_of_driver);
833 }
834
835 static inline void __exit hwicap_of_unregister(void)
836 {
837         of_unregister_platform_driver(&hwicap_of_driver);
838 }
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 */
844
845 static int __init hwicap_module_init(void)
846 {
847         dev_t devt;
848         int retval;
849
850         icap_class = class_create(THIS_MODULE, "xilinx_config");
851         mutex_init(&icap_sem);
852
853         devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
854         retval = register_chrdev_region(devt,
855                                         HWICAP_DEVICES,
856                                         DRIVER_NAME);
857         if (retval < 0)
858                 return retval;
859
860         retval = platform_driver_register(&hwicap_platform_driver);
861
862         if (retval)
863                 goto failed1;
864
865         retval = hwicap_of_register();
866
867         if (retval)
868                 goto failed2;
869
870         return retval;
871
872  failed2:
873         platform_driver_unregister(&hwicap_platform_driver);
874
875  failed1:
876         unregister_chrdev_region(devt, HWICAP_DEVICES);
877
878         return retval;
879 }
880
881 static void __exit hwicap_module_cleanup(void)
882 {
883         dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
884
885         class_destroy(icap_class);
886
887         platform_driver_unregister(&hwicap_platform_driver);
888
889         hwicap_of_unregister();
890
891         unregister_chrdev_region(devt, HWICAP_DEVICES);
892 }
893
894 module_init(hwicap_module_init);
895 module_exit(hwicap_module_cleanup);
896
897 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
898 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
899 MODULE_LICENSE("GPL");