Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / char / xilinx_hwicap / xilinx_hwicap.c
blob56fea1b9571fcb4eb20fc230a6e7d84b9dd538e0
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 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
88 #include <asm/semaphore.h>
89 =======
90 #include <linux/mutex.h>
91 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
92 #include <linux/sysctl.h>
93 #include <linux/version.h>
94 #include <linux/fs.h>
95 #include <linux/cdev.h>
96 #include <linux/platform_device.h>
98 #include <asm/io.h>
99 #include <asm/uaccess.h>
100 #include <asm/system.h>
102 #ifdef CONFIG_OF
103 /* For open firmware. */
104 #include <linux/of_device.h>
105 #include <linux/of_platform.h>
106 #endif
108 #include "xilinx_hwicap.h"
109 #include "buffer_icap.h"
110 #include "fifo_icap.h"
112 #define DRIVER_NAME "xilinx_icap"
114 #define HWICAP_REGS (0x10000)
116 /* dynamically allocate device number */
117 static int xhwicap_major;
118 static int xhwicap_minor;
119 #define HWICAP_DEVICES 1
121 module_param(xhwicap_major, int, S_IRUGO);
122 module_param(xhwicap_minor, int, S_IRUGO);
124 /* An array, which is set to true when the device is registered. */
125 static bool probed_devices[HWICAP_DEVICES];
126 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
127 =======
128 static struct mutex icap_sem;
129 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
131 static struct class *icap_class;
133 #define UNIMPLEMENTED 0xFFFF
135 static const struct config_registers v2_config_registers = {
136 .CRC = 0,
137 .FAR = 1,
138 .FDRI = 2,
139 .FDRO = 3,
140 .CMD = 4,
141 .CTL = 5,
142 .MASK = 6,
143 .STAT = 7,
144 .LOUT = 8,
145 .COR = 9,
146 .MFWR = 10,
147 .FLR = 11,
148 .KEY = 12,
149 .CBC = 13,
150 .IDCODE = 14,
151 .AXSS = UNIMPLEMENTED,
152 .C0R_1 = UNIMPLEMENTED,
153 .CSOB = UNIMPLEMENTED,
154 .WBSTAR = UNIMPLEMENTED,
155 .TIMER = UNIMPLEMENTED,
156 .BOOTSTS = UNIMPLEMENTED,
157 .CTL_1 = UNIMPLEMENTED,
160 static const struct config_registers v4_config_registers = {
161 .CRC = 0,
162 .FAR = 1,
163 .FDRI = 2,
164 .FDRO = 3,
165 .CMD = 4,
166 .CTL = 5,
167 .MASK = 6,
168 .STAT = 7,
169 .LOUT = 8,
170 .COR = 9,
171 .MFWR = 10,
172 .FLR = UNIMPLEMENTED,
173 .KEY = UNIMPLEMENTED,
174 .CBC = 11,
175 .IDCODE = 12,
176 .AXSS = 13,
177 .C0R_1 = UNIMPLEMENTED,
178 .CSOB = UNIMPLEMENTED,
179 .WBSTAR = UNIMPLEMENTED,
180 .TIMER = UNIMPLEMENTED,
181 .BOOTSTS = UNIMPLEMENTED,
182 .CTL_1 = UNIMPLEMENTED,
184 static const struct config_registers v5_config_registers = {
185 .CRC = 0,
186 .FAR = 1,
187 .FDRI = 2,
188 .FDRO = 3,
189 .CMD = 4,
190 .CTL = 5,
191 .MASK = 6,
192 .STAT = 7,
193 .LOUT = 8,
194 .COR = 9,
195 .MFWR = 10,
196 .FLR = UNIMPLEMENTED,
197 .KEY = UNIMPLEMENTED,
198 .CBC = 11,
199 .IDCODE = 12,
200 .AXSS = 13,
201 .C0R_1 = 14,
202 .CSOB = 15,
203 .WBSTAR = 16,
204 .TIMER = 17,
205 .BOOTSTS = 18,
206 .CTL_1 = 19,
210 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
211 * hwicap_command_desync: Send a DESYNC command to the ICAP port.
212 * @parameter drvdata: a pointer to the drvdata.
213 =======
214 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
215 * @drvdata: a pointer to the drvdata.
216 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
218 * This command desynchronizes the ICAP After this command, a
219 * bitstream containing a NULL packet, followed by a SYNCH packet is
220 * required before the ICAP will recognize commands.
222 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
223 int hwicap_command_desync(struct hwicap_drvdata *drvdata)
224 =======
225 static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
226 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
228 u32 buffer[4];
229 u32 index = 0;
232 * Create the data to be written to the ICAP.
234 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
235 buffer[index++] = XHI_CMD_DESYNCH;
236 buffer[index++] = XHI_NOOP_PACKET;
237 buffer[index++] = XHI_NOOP_PACKET;
240 * Write the data to the FIFO and intiate the transfer of data present
241 * in the FIFO to the ICAP device.
243 return drvdata->config->set_configuration(drvdata,
244 &buffer[0], index);
248 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
249 * hwicap_command_capture: Send a CAPTURE command to the ICAP port.
250 * @parameter drvdata: a pointer to the drvdata.
252 * This command captures all of the flip flop states so they will be
253 * available during readback. One can use this command instead of
254 * enabling the CAPTURE block in the design.
256 int hwicap_command_capture(struct hwicap_drvdata *drvdata)
258 u32 buffer[7];
259 u32 index = 0;
262 * Create the data to be written to the ICAP.
264 buffer[index++] = XHI_DUMMY_PACKET;
265 buffer[index++] = XHI_SYNC_PACKET;
266 buffer[index++] = XHI_NOOP_PACKET;
267 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
268 buffer[index++] = XHI_CMD_GCAPTURE;
269 buffer[index++] = XHI_DUMMY_PACKET;
270 buffer[index++] = XHI_DUMMY_PACKET;
273 * Write the data to the FIFO and intiate the transfer of data
274 * present in the FIFO to the ICAP device.
276 return drvdata->config->set_configuration(drvdata,
277 &buffer[0], index);
282 * hwicap_get_configuration_register: Query a configuration register.
283 * @parameter drvdata: a pointer to the drvdata.
284 * @parameter reg: a constant which represents the configuration
285 =======
286 * hwicap_get_configuration_register - Query a configuration register.
287 * @drvdata: a pointer to the drvdata.
288 * @reg: a constant which represents the configuration
289 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
290 * register value to be returned.
291 * Examples: XHI_IDCODE, XHI_FLR.
292 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
293 * @parameter RegData: returns the value of the register.
294 =======
295 * @reg_data: returns the value of the register.
296 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
298 * Sends a query packet to the ICAP and then receives the response.
299 * The icap is left in Synched state.
301 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
302 int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
303 u32 reg, u32 *RegData)
304 =======
305 static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
306 u32 reg, u32 *reg_data)
307 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
309 int status;
310 u32 buffer[6];
311 u32 index = 0;
314 * Create the data to be written to the ICAP.
316 buffer[index++] = XHI_DUMMY_PACKET;
317 buffer[index++] = XHI_SYNC_PACKET;
318 buffer[index++] = XHI_NOOP_PACKET;
319 buffer[index++] = hwicap_type_1_read(reg) | 1;
320 buffer[index++] = XHI_NOOP_PACKET;
321 buffer[index++] = XHI_NOOP_PACKET;
324 * Write the data to the FIFO and intiate the transfer of data present
325 * in the FIFO to the ICAP device.
327 status = drvdata->config->set_configuration(drvdata,
328 &buffer[0], index);
329 if (status)
330 return status;
333 * Read the configuration register
335 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
336 status = drvdata->config->get_configuration(drvdata, RegData, 1);
337 =======
338 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
339 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
340 if (status)
341 return status;
343 return 0;
346 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
347 int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
348 =======
349 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
350 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
352 int status;
353 u32 idcode;
355 dev_dbg(drvdata->dev, "initializing\n");
357 /* Abort any current transaction, to make sure we have the
358 * ICAP in a good state. */
359 dev_dbg(drvdata->dev, "Reset...\n");
360 drvdata->config->reset(drvdata);
362 dev_dbg(drvdata->dev, "Desync...\n");
363 status = hwicap_command_desync(drvdata);
364 if (status)
365 return status;
367 /* Attempt to read the IDCODE from ICAP. This
368 * may not be returned correctly, due to the design of the
369 * hardware.
371 dev_dbg(drvdata->dev, "Reading IDCODE...\n");
372 status = hwicap_get_configuration_register(
373 drvdata, drvdata->config_regs->IDCODE, &idcode);
374 dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
375 if (status)
376 return status;
378 dev_dbg(drvdata->dev, "Desync...\n");
379 status = hwicap_command_desync(drvdata);
380 if (status)
381 return status;
383 return 0;
386 static ssize_t
387 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
388 hwicap_read(struct file *file, char *buf, size_t count, loff_t *ppos)
389 =======
390 hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
391 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
393 struct hwicap_drvdata *drvdata = file->private_data;
394 ssize_t bytes_to_read = 0;
395 u32 *kbuf;
396 u32 words;
397 u32 bytes_remaining;
398 int status;
400 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
401 if (down_interruptible(&drvdata->sem))
402 return -ERESTARTSYS;
403 =======
404 status = mutex_lock_interruptible(&drvdata->sem);
405 if (status)
406 return status;
407 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
409 if (drvdata->read_buffer_in_use) {
410 /* If there are leftover bytes in the buffer, just */
411 /* return them and don't try to read more from the */
412 /* ICAP device. */
413 bytes_to_read =
414 (count < drvdata->read_buffer_in_use) ? count :
415 drvdata->read_buffer_in_use;
417 /* Return the data currently in the read buffer. */
418 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
419 status = -EFAULT;
420 goto error;
422 drvdata->read_buffer_in_use -= bytes_to_read;
423 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
424 memcpy(drvdata->read_buffer + bytes_to_read,
425 drvdata->read_buffer, 4 - bytes_to_read);
426 =======
427 memmove(drvdata->read_buffer,
428 drvdata->read_buffer + bytes_to_read,
429 4 - bytes_to_read);
430 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
431 } else {
432 /* Get new data from the ICAP, and return was was requested. */
433 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
434 if (!kbuf) {
435 status = -ENOMEM;
436 goto error;
439 /* The ICAP device is only able to read complete */
440 /* words. If a number of bytes that do not correspond */
441 /* to complete words is requested, then we read enough */
442 /* words to get the required number of bytes, and then */
443 /* save the remaining bytes for the next read. */
445 /* Determine the number of words to read, rounding up */
446 /* if necessary. */
447 words = ((count + 3) >> 2);
448 bytes_to_read = words << 2;
450 if (bytes_to_read > PAGE_SIZE)
451 bytes_to_read = PAGE_SIZE;
453 /* Ensure we only read a complete number of words. */
454 bytes_remaining = bytes_to_read & 3;
455 bytes_to_read &= ~3;
456 words = bytes_to_read >> 2;
458 status = drvdata->config->get_configuration(drvdata,
459 kbuf, words);
461 /* If we didn't read correctly, then bail out. */
462 if (status) {
463 free_page((unsigned long)kbuf);
464 goto error;
467 /* If we fail to return the data to the user, then bail out. */
468 if (copy_to_user(buf, kbuf, bytes_to_read)) {
469 free_page((unsigned long)kbuf);
470 status = -EFAULT;
471 goto error;
473 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
474 memcpy(kbuf, drvdata->read_buffer, bytes_remaining);
475 =======
476 memcpy(drvdata->read_buffer,
477 kbuf,
478 bytes_remaining);
479 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
480 drvdata->read_buffer_in_use = bytes_remaining;
481 free_page((unsigned long)kbuf);
483 status = bytes_to_read;
484 error:
485 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
486 up(&drvdata->sem);
487 =======
488 mutex_unlock(&drvdata->sem);
489 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
490 return status;
493 static ssize_t
494 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
495 hwicap_write(struct file *file, const char *buf,
496 =======
497 hwicap_write(struct file *file, const char __user *buf,
498 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
499 size_t count, loff_t *ppos)
501 struct hwicap_drvdata *drvdata = file->private_data;
502 ssize_t written = 0;
503 ssize_t left = count;
504 u32 *kbuf;
505 ssize_t len;
506 ssize_t status;
508 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
509 if (down_interruptible(&drvdata->sem))
510 return -ERESTARTSYS;
511 =======
512 status = mutex_lock_interruptible(&drvdata->sem);
513 if (status)
514 return status;
515 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
517 left += drvdata->write_buffer_in_use;
519 /* Only write multiples of 4 bytes. */
520 if (left < 4) {
521 status = 0;
522 goto error;
525 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
526 if (!kbuf) {
527 status = -ENOMEM;
528 goto error;
531 while (left > 3) {
532 /* only write multiples of 4 bytes, so there might */
533 /* be as many as 3 bytes left (at the end). */
534 len = left;
536 if (len > PAGE_SIZE)
537 len = PAGE_SIZE;
538 len &= ~3;
540 if (drvdata->write_buffer_in_use) {
541 memcpy(kbuf, drvdata->write_buffer,
542 drvdata->write_buffer_in_use);
543 if (copy_from_user(
544 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
545 (((char *)kbuf) + (drvdata->write_buffer_in_use)),
546 =======
547 (((char *)kbuf) + drvdata->write_buffer_in_use),
548 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
549 buf + written,
550 len - (drvdata->write_buffer_in_use))) {
551 free_page((unsigned long)kbuf);
552 status = -EFAULT;
553 goto error;
555 } else {
556 if (copy_from_user(kbuf, buf + written, len)) {
557 free_page((unsigned long)kbuf);
558 status = -EFAULT;
559 goto error;
563 status = drvdata->config->set_configuration(drvdata,
564 kbuf, len >> 2);
566 if (status) {
567 free_page((unsigned long)kbuf);
568 status = -EFAULT;
569 goto error;
571 if (drvdata->write_buffer_in_use) {
572 len -= drvdata->write_buffer_in_use;
573 left -= drvdata->write_buffer_in_use;
574 drvdata->write_buffer_in_use = 0;
576 written += len;
577 left -= len;
579 if ((left > 0) && (left < 4)) {
580 if (!copy_from_user(drvdata->write_buffer,
581 buf + written, left)) {
582 drvdata->write_buffer_in_use = left;
583 written += left;
584 left = 0;
588 free_page((unsigned long)kbuf);
589 status = written;
590 error:
591 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
592 up(&drvdata->sem);
593 =======
594 mutex_unlock(&drvdata->sem);
595 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
596 return status;
599 static int hwicap_open(struct inode *inode, struct file *file)
601 struct hwicap_drvdata *drvdata;
602 int status;
604 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
606 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
607 if (down_interruptible(&drvdata->sem))
608 return -ERESTARTSYS;
609 =======
610 status = mutex_lock_interruptible(&drvdata->sem);
611 if (status)
612 return status;
613 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
615 if (drvdata->is_open) {
616 status = -EBUSY;
617 goto error;
620 status = hwicap_initialize_hwicap(drvdata);
621 if (status) {
622 dev_err(drvdata->dev, "Failed to open file");
623 goto error;
626 file->private_data = drvdata;
627 drvdata->write_buffer_in_use = 0;
628 drvdata->read_buffer_in_use = 0;
629 drvdata->is_open = 1;
631 error:
632 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
633 up(&drvdata->sem);
634 =======
635 mutex_unlock(&drvdata->sem);
636 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
637 return status;
640 static int hwicap_release(struct inode *inode, struct file *file)
642 struct hwicap_drvdata *drvdata = file->private_data;
643 int i;
644 int status = 0;
646 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
647 if (down_interruptible(&drvdata->sem))
648 return -ERESTARTSYS;
649 =======
650 mutex_lock(&drvdata->sem);
651 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
653 if (drvdata->write_buffer_in_use) {
654 /* Flush write buffer. */
655 for (i = drvdata->write_buffer_in_use; i < 4; i++)
656 drvdata->write_buffer[i] = 0;
658 status = drvdata->config->set_configuration(drvdata,
659 (u32 *) drvdata->write_buffer, 1);
660 if (status)
661 goto error;
664 status = hwicap_command_desync(drvdata);
665 if (status)
666 goto error;
668 error:
669 drvdata->is_open = 0;
670 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
671 up(&drvdata->sem);
672 =======
673 mutex_unlock(&drvdata->sem);
674 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
675 return status;
678 static struct file_operations hwicap_fops = {
679 .owner = THIS_MODULE,
680 .write = hwicap_write,
681 .read = hwicap_read,
682 .open = hwicap_open,
683 .release = hwicap_release,
686 static int __devinit hwicap_setup(struct device *dev, int id,
687 const struct resource *regs_res,
688 const struct hwicap_driver_config *config,
689 const struct config_registers *config_regs)
691 dev_t devt;
692 struct hwicap_drvdata *drvdata = NULL;
693 int retval = 0;
695 dev_info(dev, "Xilinx icap port driver\n");
697 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
698 =======
699 mutex_lock(&icap_sem);
701 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
702 if (id < 0) {
703 for (id = 0; id < HWICAP_DEVICES; id++)
704 if (!probed_devices[id])
705 break;
707 if (id < 0 || id >= HWICAP_DEVICES) {
708 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
709 =======
710 mutex_unlock(&icap_sem);
711 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
712 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
713 return -EINVAL;
715 if (probed_devices[id]) {
716 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
717 =======
718 mutex_unlock(&icap_sem);
719 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
720 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
721 DRIVER_NAME, id);
722 return -EBUSY;
725 probed_devices[id] = 1;
726 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
727 =======
728 mutex_unlock(&icap_sem);
729 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
731 devt = MKDEV(xhwicap_major, xhwicap_minor + id);
733 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
734 drvdata = kmalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
735 =======
736 drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
737 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
738 if (!drvdata) {
739 dev_err(dev, "Couldn't allocate device private record\n");
740 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
741 return -ENOMEM;
742 =======
743 retval = -ENOMEM;
744 goto failed0;
745 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
747 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
748 memset((void *)drvdata, 0, sizeof(struct hwicap_drvdata));
749 =======
750 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
751 dev_set_drvdata(dev, (void *)drvdata);
753 if (!regs_res) {
754 dev_err(dev, "Couldn't get registers resource\n");
755 retval = -EFAULT;
756 goto failed1;
759 drvdata->mem_start = regs_res->start;
760 drvdata->mem_end = regs_res->end;
761 drvdata->mem_size = regs_res->end - regs_res->start + 1;
763 if (!request_mem_region(drvdata->mem_start,
764 drvdata->mem_size, DRIVER_NAME)) {
765 dev_err(dev, "Couldn't lock memory region at %p\n",
766 (void *)regs_res->start);
767 retval = -EBUSY;
768 goto failed1;
771 drvdata->devt = devt;
772 drvdata->dev = dev;
773 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
774 if (!drvdata->base_address) {
775 dev_err(dev, "ioremap() failed\n");
776 goto failed2;
779 drvdata->config = config;
780 drvdata->config_regs = config_regs;
782 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
783 init_MUTEX(&drvdata->sem);
784 =======
785 mutex_init(&drvdata->sem);
786 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
787 drvdata->is_open = 0;
789 dev_info(dev, "ioremap %lx to %p with size %x\n",
790 (unsigned long int)drvdata->mem_start,
791 drvdata->base_address, drvdata->mem_size);
793 cdev_init(&drvdata->cdev, &hwicap_fops);
794 drvdata->cdev.owner = THIS_MODULE;
795 retval = cdev_add(&drvdata->cdev, devt, 1);
796 if (retval) {
797 dev_err(dev, "cdev_add() failed\n");
798 goto failed3;
800 /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
801 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
802 class_device_create(icap_class, NULL, devt, NULL, DRIVER_NAME);
803 =======
804 device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
805 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
806 return 0; /* success */
808 failed3:
809 iounmap(drvdata->base_address);
811 failed2:
812 release_mem_region(regs_res->start, drvdata->mem_size);
814 failed1:
815 kfree(drvdata);
817 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
818 =======
819 failed0:
820 mutex_lock(&icap_sem);
821 probed_devices[id] = 0;
822 mutex_unlock(&icap_sem);
824 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
825 return retval;
828 static struct hwicap_driver_config buffer_icap_config = {
829 .get_configuration = buffer_icap_get_configuration,
830 .set_configuration = buffer_icap_set_configuration,
831 .reset = buffer_icap_reset,
834 static struct hwicap_driver_config fifo_icap_config = {
835 .get_configuration = fifo_icap_get_configuration,
836 .set_configuration = fifo_icap_set_configuration,
837 .reset = fifo_icap_reset,
840 static int __devexit hwicap_remove(struct device *dev)
842 struct hwicap_drvdata *drvdata;
844 drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
846 if (!drvdata)
847 return 0;
849 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
850 class_device_destroy(icap_class, drvdata->devt);
851 =======
852 device_destroy(icap_class, drvdata->devt);
853 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
854 cdev_del(&drvdata->cdev);
855 iounmap(drvdata->base_address);
856 release_mem_region(drvdata->mem_start, drvdata->mem_size);
857 kfree(drvdata);
858 dev_set_drvdata(dev, NULL);
859 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
860 probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0;
861 =======
862 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
864 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
865 =======
866 mutex_lock(&icap_sem);
867 probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0;
868 mutex_unlock(&icap_sem);
869 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
870 return 0; /* success */
873 static int __devinit hwicap_drv_probe(struct platform_device *pdev)
875 struct resource *res;
876 const struct config_registers *regs;
877 const char *family;
879 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
880 if (!res)
881 return -ENODEV;
883 /* It's most likely that we're using V4, if the family is not
884 specified */
885 regs = &v4_config_registers;
886 family = pdev->dev.platform_data;
888 if (family) {
889 if (!strcmp(family, "virtex2p")) {
890 regs = &v2_config_registers;
891 } else if (!strcmp(family, "virtex4")) {
892 regs = &v4_config_registers;
893 } else if (!strcmp(family, "virtex5")) {
894 regs = &v5_config_registers;
898 return hwicap_setup(&pdev->dev, pdev->id, res,
899 &buffer_icap_config, regs);
902 static int __devexit hwicap_drv_remove(struct platform_device *pdev)
904 return hwicap_remove(&pdev->dev);
907 static struct platform_driver hwicap_platform_driver = {
908 .probe = hwicap_drv_probe,
909 .remove = hwicap_drv_remove,
910 .driver = {
911 .owner = THIS_MODULE,
912 .name = DRIVER_NAME,
916 /* ---------------------------------------------------------------------
917 * OF bus binding
920 #if defined(CONFIG_OF)
921 static int __devinit
922 hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
924 struct resource res;
925 const unsigned int *id;
926 const char *family;
927 int rc;
928 const struct hwicap_driver_config *config = match->data;
929 const struct config_registers *regs;
931 dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
933 rc = of_address_to_resource(op->node, 0, &res);
934 if (rc) {
935 dev_err(&op->dev, "invalid address\n");
936 return rc;
939 id = of_get_property(op->node, "port-number", NULL);
941 /* It's most likely that we're using V4, if the family is not
942 specified */
943 regs = &v4_config_registers;
944 family = of_get_property(op->node, "xlnx,family", NULL);
946 if (family) {
947 if (!strcmp(family, "virtex2p")) {
948 regs = &v2_config_registers;
949 } else if (!strcmp(family, "virtex4")) {
950 regs = &v4_config_registers;
951 } else if (!strcmp(family, "virtex5")) {
952 regs = &v5_config_registers;
955 return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
956 regs);
959 static int __devexit hwicap_of_remove(struct of_device *op)
961 return hwicap_remove(&op->dev);
964 /* Match table for of_platform binding */
965 static const struct of_device_id __devinit hwicap_of_match[] = {
966 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
967 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
970 MODULE_DEVICE_TABLE(of, hwicap_of_match);
972 static struct of_platform_driver hwicap_of_driver = {
973 .owner = THIS_MODULE,
974 .name = DRIVER_NAME,
975 .match_table = hwicap_of_match,
976 .probe = hwicap_of_probe,
977 .remove = __devexit_p(hwicap_of_remove),
978 .driver = {
979 .name = DRIVER_NAME,
983 /* Registration helpers to keep the number of #ifdefs to a minimum */
984 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
985 static inline int __devinit hwicap_of_register(void)
986 =======
987 static inline int __init hwicap_of_register(void)
988 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
990 pr_debug("hwicap: calling of_register_platform_driver()\n");
991 return of_register_platform_driver(&hwicap_of_driver);
994 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
995 static inline void __devexit hwicap_of_unregister(void)
996 =======
997 static inline void __exit hwicap_of_unregister(void)
998 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1000 of_unregister_platform_driver(&hwicap_of_driver);
1002 #else /* CONFIG_OF */
1003 /* CONFIG_OF not enabled; do nothing helpers */
1004 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1005 static inline int __devinit hwicap_of_register(void) { return 0; }
1006 static inline void __devexit hwicap_of_unregister(void) { }
1007 =======
1008 static inline int __init hwicap_of_register(void) { return 0; }
1009 static inline void __exit hwicap_of_unregister(void) { }
1010 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1011 #endif /* CONFIG_OF */
1013 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1014 static int __devinit hwicap_module_init(void)
1015 =======
1016 static int __init hwicap_module_init(void)
1017 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1019 dev_t devt;
1020 int retval;
1022 icap_class = class_create(THIS_MODULE, "xilinx_config");
1023 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1024 =======
1025 mutex_init(&icap_sem);
1026 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1028 if (xhwicap_major) {
1029 devt = MKDEV(xhwicap_major, xhwicap_minor);
1030 retval = register_chrdev_region(
1031 devt,
1032 HWICAP_DEVICES,
1033 DRIVER_NAME);
1034 if (retval < 0)
1035 return retval;
1036 } else {
1037 retval = alloc_chrdev_region(&devt,
1038 xhwicap_minor,
1039 HWICAP_DEVICES,
1040 DRIVER_NAME);
1041 if (retval < 0)
1042 return retval;
1043 xhwicap_major = MAJOR(devt);
1046 retval = platform_driver_register(&hwicap_platform_driver);
1048 if (retval)
1049 goto failed1;
1051 retval = hwicap_of_register();
1053 if (retval)
1054 goto failed2;
1056 return retval;
1058 failed2:
1059 platform_driver_unregister(&hwicap_platform_driver);
1061 failed1:
1062 unregister_chrdev_region(devt, HWICAP_DEVICES);
1064 return retval;
1067 <<<<<<< HEAD:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1068 static void __devexit hwicap_module_cleanup(void)
1069 =======
1070 static void __exit hwicap_module_cleanup(void)
1071 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/xilinx_hwicap/xilinx_hwicap.c
1073 dev_t devt = MKDEV(xhwicap_major, xhwicap_minor);
1075 class_destroy(icap_class);
1077 platform_driver_unregister(&hwicap_platform_driver);
1079 hwicap_of_unregister();
1081 unregister_chrdev_region(devt, HWICAP_DEVICES);
1084 module_init(hwicap_module_init);
1085 module_exit(hwicap_module_cleanup);
1087 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
1088 MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
1089 MODULE_LICENSE("GPL");