[POWERPC] Xilinx: hwicap: Verify sync before reading idcode.
[safe/jmp/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/xilinx_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/xilinx_icap
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/sysctl.h>
89 #include <linux/version.h>
90 #include <linux/fs.h>
91 #include <linux/cdev.h>
92 #include <linux/platform_device.h>
93
94 #include <asm/io.h>
95 #include <asm/uaccess.h>
96 #include <asm/system.h>
97
98 #ifdef CONFIG_OF
99 /* For open firmware. */
100 #include <linux/of_device.h>
101 #include <linux/of_platform.h>
102 #endif
103
104 #include "xilinx_hwicap.h"
105 #include "buffer_icap.h"
106 #include "fifo_icap.h"
107
108 #define DRIVER_NAME "xilinx_icap"
109
110 #define HWICAP_REGS   (0x10000)
111
112 /* dynamically allocate device number */
113 static int xhwicap_major;
114 static int xhwicap_minor;
115 #define HWICAP_DEVICES 1
116
117 module_param(xhwicap_major, int, S_IRUGO);
118 module_param(xhwicap_minor, int, S_IRUGO);
119
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;
123
124 static struct class *icap_class;
125
126 #define UNIMPLEMENTED 0xFFFF
127
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,
151 };
152
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,
176 };
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,
200 };
201
202 /**
203  * hwicap_command_desync - Send a DESYNC command to the ICAP port.
204  * @drvdata: a pointer to the drvdata.
205  *
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.
209  */
210 static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
211 {
212         u32 buffer[4];
213         u32 index = 0;
214
215         /*
216          * Create the data to be written to the ICAP.
217          */
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;
222
223         /*
224          * Write the data to the FIFO and intiate the transfer of data present
225          * in the FIFO to the ICAP device.
226          */
227         return drvdata->config->set_configuration(drvdata,
228                         &buffer[0], index);
229 }
230
231 /**
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.
238  *
239  * Sends a query packet to the ICAP and then receives the response.
240  * The icap is left in Synched state.
241  */
242 static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
243                 u32 reg, u32 *reg_data)
244 {
245         int status;
246         u32 buffer[6];
247         u32 index = 0;
248
249         /*
250          * Create the data to be written to the ICAP.
251          */
252         buffer[index++] = XHI_DUMMY_PACKET;
253         buffer[index++] = XHI_NOOP_PACKET;
254         buffer[index++] = XHI_SYNC_PACKET;
255         buffer[index++] = XHI_NOOP_PACKET;
256         buffer[index++] = XHI_NOOP_PACKET;
257
258         /*
259          * Write the data to the FIFO and initiate the transfer of data present
260          * in the FIFO to the ICAP device.
261          */
262         status = drvdata->config->set_configuration(drvdata,
263                                                     &buffer[0], index);
264         if (status)
265                 return status;
266
267         /* If the syncword was not found, then we need to start over. */
268         status = drvdata->config->get_status(drvdata);
269         if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
270                 return -EIO;
271
272         index = 0;
273         buffer[index++] = hwicap_type_1_read(reg) | 1;
274         buffer[index++] = XHI_NOOP_PACKET;
275         buffer[index++] = XHI_NOOP_PACKET;
276
277         /*
278          * Write the data to the FIFO and intiate the transfer of data present
279          * in the FIFO to the ICAP device.
280          */
281         status = drvdata->config->set_configuration(drvdata,
282                         &buffer[0], index);
283         if (status)
284                 return status;
285
286         /*
287          * Read the configuration register
288          */
289         status = drvdata->config->get_configuration(drvdata, reg_data, 1);
290         if (status)
291                 return status;
292
293         return 0;
294 }
295
296 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
297 {
298         int status;
299         u32 idcode;
300
301         dev_dbg(drvdata->dev, "initializing\n");
302
303         /* Abort any current transaction, to make sure we have the
304          * ICAP in a good state. */
305         dev_dbg(drvdata->dev, "Reset...\n");
306         drvdata->config->reset(drvdata);
307
308         dev_dbg(drvdata->dev, "Desync...\n");
309         status = hwicap_command_desync(drvdata);
310         if (status)
311                 return status;
312
313         /* Attempt to read the IDCODE from ICAP.  This
314          * may not be returned correctly, due to the design of the
315          * hardware.
316          */
317         dev_dbg(drvdata->dev, "Reading IDCODE...\n");
318         status = hwicap_get_configuration_register(
319                         drvdata, drvdata->config_regs->IDCODE, &idcode);
320         dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
321         if (status)
322                 return status;
323
324         dev_dbg(drvdata->dev, "Desync...\n");
325         status = hwicap_command_desync(drvdata);
326         if (status)
327                 return status;
328
329         return 0;
330 }
331
332 static ssize_t
333 hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
334 {
335         struct hwicap_drvdata *drvdata = file->private_data;
336         ssize_t bytes_to_read = 0;
337         u32 *kbuf;
338         u32 words;
339         u32 bytes_remaining;
340         int status;
341
342         status = mutex_lock_interruptible(&drvdata->sem);
343         if (status)
344                 return status;
345
346         if (drvdata->read_buffer_in_use) {
347                 /* If there are leftover bytes in the buffer, just */
348                 /* return them and don't try to read more from the */
349                 /* ICAP device. */
350                 bytes_to_read =
351                         (count < drvdata->read_buffer_in_use) ? count :
352                         drvdata->read_buffer_in_use;
353
354                 /* Return the data currently in the read buffer. */
355                 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
356                         status = -EFAULT;
357                         goto error;
358                 }
359                 drvdata->read_buffer_in_use -= bytes_to_read;
360                 memmove(drvdata->read_buffer,
361                        drvdata->read_buffer + bytes_to_read,
362                        4 - bytes_to_read);
363         } else {
364                 /* Get new data from the ICAP, and return was was requested. */
365                 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
366                 if (!kbuf) {
367                         status = -ENOMEM;
368                         goto error;
369                 }
370
371                 /* The ICAP device is only able to read complete */
372                 /* words.  If a number of bytes that do not correspond */
373                 /* to complete words is requested, then we read enough */
374                 /* words to get the required number of bytes, and then */
375                 /* save the remaining bytes for the next read. */
376
377                 /* Determine the number of words to read, rounding up */
378                 /* if necessary. */
379                 words = ((count + 3) >> 2);
380                 bytes_to_read = words << 2;
381
382                 if (bytes_to_read > PAGE_SIZE)
383                         bytes_to_read = PAGE_SIZE;
384
385                 /* Ensure we only read a complete number of words. */
386                 bytes_remaining = bytes_to_read & 3;
387                 bytes_to_read &= ~3;
388                 words = bytes_to_read >> 2;
389
390                 status = drvdata->config->get_configuration(drvdata,
391                                 kbuf, words);
392
393                 /* If we didn't read correctly, then bail out. */
394                 if (status) {
395                         free_page((unsigned long)kbuf);
396                         goto error;
397                 }
398
399                 /* If we fail to return the data to the user, then bail out. */
400                 if (copy_to_user(buf, kbuf, bytes_to_read)) {
401                         free_page((unsigned long)kbuf);
402                         status = -EFAULT;
403                         goto error;
404                 }
405                 memcpy(drvdata->read_buffer,
406                        kbuf,
407                        bytes_remaining);
408                 drvdata->read_buffer_in_use = bytes_remaining;
409                 free_page((unsigned long)kbuf);
410         }
411         status = bytes_to_read;
412  error:
413         mutex_unlock(&drvdata->sem);
414         return status;
415 }
416
417 static ssize_t
418 hwicap_write(struct file *file, const char __user *buf,
419                 size_t count, loff_t *ppos)
420 {
421         struct hwicap_drvdata *drvdata = file->private_data;
422         ssize_t written = 0;
423         ssize_t left = count;
424         u32 *kbuf;
425         ssize_t len;
426         ssize_t status;
427
428         status = mutex_lock_interruptible(&drvdata->sem);
429         if (status)
430                 return status;
431
432         left += drvdata->write_buffer_in_use;
433
434         /* Only write multiples of 4 bytes. */
435         if (left < 4) {
436                 status = 0;
437                 goto error;
438         }
439
440         kbuf = (u32 *) __get_free_page(GFP_KERNEL);
441         if (!kbuf) {
442                 status = -ENOMEM;
443                 goto error;
444         }
445
446         while (left > 3) {
447                 /* only write multiples of 4 bytes, so there might */
448                 /* be as many as 3 bytes left (at the end). */
449                 len = left;
450
451                 if (len > PAGE_SIZE)
452                         len = PAGE_SIZE;
453                 len &= ~3;
454
455                 if (drvdata->write_buffer_in_use) {
456                         memcpy(kbuf, drvdata->write_buffer,
457                                         drvdata->write_buffer_in_use);
458                         if (copy_from_user(
459                             (((char *)kbuf) + drvdata->write_buffer_in_use),
460                             buf + written,
461                             len - (drvdata->write_buffer_in_use))) {
462                                 free_page((unsigned long)kbuf);
463                                 status = -EFAULT;
464                                 goto error;
465                         }
466                 } else {
467                         if (copy_from_user(kbuf, buf + written, len)) {
468                                 free_page((unsigned long)kbuf);
469                                 status = -EFAULT;
470                                 goto error;
471                         }
472                 }
473
474                 status = drvdata->config->set_configuration(drvdata,
475                                 kbuf, len >> 2);
476
477                 if (status) {
478                         free_page((unsigned long)kbuf);
479                         status = -EFAULT;
480                         goto error;
481                 }
482                 if (drvdata->write_buffer_in_use) {
483                         len -= drvdata->write_buffer_in_use;
484                         left -= drvdata->write_buffer_in_use;
485                         drvdata->write_buffer_in_use = 0;
486                 }
487                 written += len;
488                 left -= len;
489         }
490         if ((left > 0) && (left < 4)) {
491                 if (!copy_from_user(drvdata->write_buffer,
492                                                 buf + written, left)) {
493                         drvdata->write_buffer_in_use = left;
494                         written += left;
495                         left = 0;
496                 }
497         }
498
499         free_page((unsigned long)kbuf);
500         status = written;
501  error:
502         mutex_unlock(&drvdata->sem);
503         return status;
504 }
505
506 static int hwicap_open(struct inode *inode, struct file *file)
507 {
508         struct hwicap_drvdata *drvdata;
509         int status;
510
511         drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
512
513         status = mutex_lock_interruptible(&drvdata->sem);
514         if (status)
515                 return status;
516
517         if (drvdata->is_open) {
518                 status = -EBUSY;
519                 goto error;
520         }
521
522         status = hwicap_initialize_hwicap(drvdata);
523         if (status) {
524                 dev_err(drvdata->dev, "Failed to open file");
525                 goto error;
526         }
527
528         file->private_data = drvdata;
529         drvdata->write_buffer_in_use = 0;
530         drvdata->read_buffer_in_use = 0;
531         drvdata->is_open = 1;
532
533  error:
534         mutex_unlock(&drvdata->sem);
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 %p\n",
631                         (void *)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 %x\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 __devinit 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         if (xhwicap_major) {
854                 devt = MKDEV(xhwicap_major, xhwicap_minor);
855                 retval = register_chrdev_region(
856                                 devt,
857                                 HWICAP_DEVICES,
858                                 DRIVER_NAME);
859                 if (retval < 0)
860                         return retval;
861         } else {
862                 retval = alloc_chrdev_region(&devt,
863                                 xhwicap_minor,
864                                 HWICAP_DEVICES,
865                                 DRIVER_NAME);
866                 if (retval < 0)
867                         return retval;
868                 xhwicap_major = MAJOR(devt);
869         }
870
871         retval = platform_driver_register(&hwicap_platform_driver);
872
873         if (retval)
874                 goto failed1;
875
876         retval = hwicap_of_register();
877
878         if (retval)
879                 goto failed2;
880
881         return retval;
882
883  failed2:
884         platform_driver_unregister(&hwicap_platform_driver);
885
886  failed1:
887         unregister_chrdev_region(devt, HWICAP_DEVICES);
888
889         return retval;
890 }
891
892 static void __exit hwicap_module_cleanup(void)
893 {
894         dev_t devt = MKDEV(xhwicap_major, xhwicap_minor);
895
896         class_destroy(icap_class);
897
898         platform_driver_unregister(&hwicap_platform_driver);
899
900         hwicap_of_unregister();
901
902         unregister_chrdev_region(devt, HWICAP_DEVICES);
903 }
904
905 module_init(hwicap_module_init);
906 module_exit(hwicap_module_cleanup);
907
908 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
909 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
910 MODULE_LICENSE("GPL");