blob: e90e1c74fd4c1f61a006dc82dbfe481d38c84ceb [file] [log] [blame]
Stephen Neuendorfferef141a02008-02-06 04:24:09 +11001/*****************************************************************************
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 *
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110024 * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group
25 * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group
26 * (c) Copyright 2007-2008 Xilinx Inc.
27 * All rights reserved.
28 *
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 *
33 *****************************************************************************/
34
35/*
Stephen Neuendorfferac646732008-03-18 04:36:32 +110036 * This is the code behind /dev/icap* -- it allows a user-space
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110037 * application to use the Xilinx ICAP subsystem.
38 *
39 * The following operations are possible:
40 *
41 * open open the port and initialize for access.
42 * release release port
43 * write Write a bitstream to the configuration processor.
44 * read Read a data stream from the configuration processor.
45 *
46 * After being opened, the port is initialized and accessed to avoid a
47 * corrupted first read which may occur with some hardware. The port
48 * is left in a desynched state, requiring that a synch sequence be
49 * transmitted before any valid configuration data. A user will have
50 * exclusive access to the device while it remains open, and the state
51 * of the ICAP cannot be guaranteed after the device is closed. Note
52 * that a complete reset of the core and the state of the ICAP cannot
53 * be performed on many versions of the cores, hence users of this
54 * device should avoid making inconsistent accesses to the device. In
55 * particular, accessing the read interface, without first generating
56 * a write containing a readback packet can leave the ICAP in an
57 * inaccessible state.
58 *
59 * Note that in order to use the read interface, it is first necessary
60 * to write a request packet to the write interface. i.e., it is not
61 * possible to simply readback the bitstream (or any configuration
62 * bits) from a device without specifically requesting them first.
63 * The code to craft such packets is intended to be part of the
64 * user-space application code that uses this device. The simplest
65 * way to use this interface is simply:
66 *
Stephen Neuendorfferac646732008-03-18 04:36:32 +110067 * cp foo.bit /dev/icap0
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110068 *
69 * Note that unless foo.bit is an appropriately constructed partial
Lucas De Marchi25985ed2011-03-30 22:57:33 -030070 * bitstream, this has a high likelihood of overwriting the design
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110071 * currently programmed in the FPGA.
72 */
73
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110074#include <linux/module.h>
75#include <linux/kernel.h>
76#include <linux/types.h>
77#include <linux/ioport.h>
78#include <linux/interrupt.h>
79#include <linux/fcntl.h>
80#include <linux/init.h>
81#include <linux/poll.h>
82#include <linux/proc_fs.h>
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +110083#include <linux/mutex.h>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110084#include <linux/sysctl.h>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110085#include <linux/fs.h>
86#include <linux/cdev.h>
87#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090088#include <linux/slab.h>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110089
90#include <asm/io.h>
91#include <asm/uaccess.h>
92#include <asm/system.h>
93
94#ifdef CONFIG_OF
95/* For open firmware. */
Grant Likelyf1ca09b2010-08-16 23:44:49 -060096#include <linux/of_address.h>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110097#include <linux/of_device.h>
98#include <linux/of_platform.h>
99#endif
100
101#include "xilinx_hwicap.h"
102#include "buffer_icap.h"
103#include "fifo_icap.h"
104
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100105#define DRIVER_NAME "icap"
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100106
107#define HWICAP_REGS (0x10000)
108
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100109#define XHWICAP_MAJOR 259
110#define XHWICAP_MINOR 0
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100111#define HWICAP_DEVICES 1
112
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100113/* An array, which is set to true when the device is registered. */
Arnd Bergmann613655f2010-06-02 14:28:52 +0200114static DEFINE_MUTEX(hwicap_mutex);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100115static bool probed_devices[HWICAP_DEVICES];
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100116static struct mutex icap_sem;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100117
118static struct class *icap_class;
119
120#define UNIMPLEMENTED 0xFFFF
121
122static const struct config_registers v2_config_registers = {
123 .CRC = 0,
124 .FAR = 1,
125 .FDRI = 2,
126 .FDRO = 3,
127 .CMD = 4,
128 .CTL = 5,
129 .MASK = 6,
130 .STAT = 7,
131 .LOUT = 8,
132 .COR = 9,
133 .MFWR = 10,
134 .FLR = 11,
135 .KEY = 12,
136 .CBC = 13,
137 .IDCODE = 14,
138 .AXSS = UNIMPLEMENTED,
139 .C0R_1 = UNIMPLEMENTED,
140 .CSOB = UNIMPLEMENTED,
141 .WBSTAR = UNIMPLEMENTED,
142 .TIMER = UNIMPLEMENTED,
143 .BOOTSTS = UNIMPLEMENTED,
144 .CTL_1 = UNIMPLEMENTED,
145};
146
147static const struct config_registers v4_config_registers = {
148 .CRC = 0,
149 .FAR = 1,
150 .FDRI = 2,
151 .FDRO = 3,
152 .CMD = 4,
153 .CTL = 5,
154 .MASK = 6,
155 .STAT = 7,
156 .LOUT = 8,
157 .COR = 9,
158 .MFWR = 10,
159 .FLR = UNIMPLEMENTED,
160 .KEY = UNIMPLEMENTED,
161 .CBC = 11,
162 .IDCODE = 12,
163 .AXSS = 13,
164 .C0R_1 = UNIMPLEMENTED,
165 .CSOB = UNIMPLEMENTED,
166 .WBSTAR = UNIMPLEMENTED,
167 .TIMER = UNIMPLEMENTED,
168 .BOOTSTS = UNIMPLEMENTED,
169 .CTL_1 = UNIMPLEMENTED,
170};
171static const struct config_registers v5_config_registers = {
172 .CRC = 0,
173 .FAR = 1,
174 .FDRI = 2,
175 .FDRO = 3,
176 .CMD = 4,
177 .CTL = 5,
178 .MASK = 6,
179 .STAT = 7,
180 .LOUT = 8,
181 .COR = 9,
182 .MFWR = 10,
183 .FLR = UNIMPLEMENTED,
184 .KEY = UNIMPLEMENTED,
185 .CBC = 11,
186 .IDCODE = 12,
187 .AXSS = 13,
188 .C0R_1 = 14,
189 .CSOB = 15,
190 .WBSTAR = 16,
191 .TIMER = 17,
192 .BOOTSTS = 18,
193 .CTL_1 = 19,
194};
195
196/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100197 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
198 * @drvdata: a pointer to the drvdata.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100199 *
200 * This command desynchronizes the ICAP After this command, a
201 * bitstream containing a NULL packet, followed by a SYNCH packet is
202 * required before the ICAP will recognize commands.
203 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100204static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100205{
206 u32 buffer[4];
207 u32 index = 0;
208
209 /*
210 * Create the data to be written to the ICAP.
211 */
212 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
213 buffer[index++] = XHI_CMD_DESYNCH;
214 buffer[index++] = XHI_NOOP_PACKET;
215 buffer[index++] = XHI_NOOP_PACKET;
216
217 /*
218 * Write the data to the FIFO and intiate the transfer of data present
219 * in the FIFO to the ICAP device.
220 */
221 return drvdata->config->set_configuration(drvdata,
222 &buffer[0], index);
223}
224
225/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100226 * hwicap_get_configuration_register - Query a configuration register.
227 * @drvdata: a pointer to the drvdata.
228 * @reg: a constant which represents the configuration
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100229 * register value to be returned.
230 * Examples: XHI_IDCODE, XHI_FLR.
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100231 * @reg_data: returns the value of the register.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100232 *
233 * Sends a query packet to the ICAP and then receives the response.
234 * The icap is left in Synched state.
235 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100236static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
237 u32 reg, u32 *reg_data)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100238{
239 int status;
240 u32 buffer[6];
241 u32 index = 0;
242
243 /*
244 * Create the data to be written to the ICAP.
245 */
246 buffer[index++] = XHI_DUMMY_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100247 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100248 buffer[index++] = XHI_SYNC_PACKET;
249 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100250 buffer[index++] = XHI_NOOP_PACKET;
251
252 /*
253 * Write the data to the FIFO and initiate the transfer of data present
254 * in the FIFO to the ICAP device.
255 */
256 status = drvdata->config->set_configuration(drvdata,
257 &buffer[0], index);
258 if (status)
259 return status;
260
261 /* If the syncword was not found, then we need to start over. */
262 status = drvdata->config->get_status(drvdata);
263 if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
264 return -EIO;
265
266 index = 0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100267 buffer[index++] = hwicap_type_1_read(reg) | 1;
268 buffer[index++] = XHI_NOOP_PACKET;
269 buffer[index++] = XHI_NOOP_PACKET;
270
271 /*
272 * Write the data to the FIFO and intiate the transfer of data present
273 * in the FIFO to the ICAP device.
274 */
275 status = drvdata->config->set_configuration(drvdata,
276 &buffer[0], index);
277 if (status)
278 return status;
279
280 /*
281 * Read the configuration register
282 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100283 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100284 if (status)
285 return status;
286
287 return 0;
288}
289
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100290static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100291{
292 int status;
293 u32 idcode;
294
295 dev_dbg(drvdata->dev, "initializing\n");
296
297 /* Abort any current transaction, to make sure we have the
298 * ICAP in a good state. */
299 dev_dbg(drvdata->dev, "Reset...\n");
300 drvdata->config->reset(drvdata);
301
302 dev_dbg(drvdata->dev, "Desync...\n");
303 status = hwicap_command_desync(drvdata);
304 if (status)
305 return status;
306
307 /* Attempt to read the IDCODE from ICAP. This
308 * may not be returned correctly, due to the design of the
309 * hardware.
310 */
311 dev_dbg(drvdata->dev, "Reading IDCODE...\n");
312 status = hwicap_get_configuration_register(
313 drvdata, drvdata->config_regs->IDCODE, &idcode);
314 dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
315 if (status)
316 return status;
317
318 dev_dbg(drvdata->dev, "Desync...\n");
319 status = hwicap_command_desync(drvdata);
320 if (status)
321 return status;
322
323 return 0;
324}
325
326static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100327hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100328{
329 struct hwicap_drvdata *drvdata = file->private_data;
330 ssize_t bytes_to_read = 0;
331 u32 *kbuf;
332 u32 words;
333 u32 bytes_remaining;
334 int status;
335
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100336 status = mutex_lock_interruptible(&drvdata->sem);
337 if (status)
338 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100339
340 if (drvdata->read_buffer_in_use) {
341 /* If there are leftover bytes in the buffer, just */
342 /* return them and don't try to read more from the */
343 /* ICAP device. */
344 bytes_to_read =
345 (count < drvdata->read_buffer_in_use) ? count :
346 drvdata->read_buffer_in_use;
347
348 /* Return the data currently in the read buffer. */
349 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
350 status = -EFAULT;
351 goto error;
352 }
353 drvdata->read_buffer_in_use -= bytes_to_read;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100354 memmove(drvdata->read_buffer,
355 drvdata->read_buffer + bytes_to_read,
356 4 - bytes_to_read);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100357 } else {
358 /* Get new data from the ICAP, and return was was requested. */
359 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
360 if (!kbuf) {
361 status = -ENOMEM;
362 goto error;
363 }
364
365 /* The ICAP device is only able to read complete */
366 /* words. If a number of bytes that do not correspond */
367 /* to complete words is requested, then we read enough */
368 /* words to get the required number of bytes, and then */
369 /* save the remaining bytes for the next read. */
370
371 /* Determine the number of words to read, rounding up */
372 /* if necessary. */
373 words = ((count + 3) >> 2);
374 bytes_to_read = words << 2;
375
376 if (bytes_to_read > PAGE_SIZE)
377 bytes_to_read = PAGE_SIZE;
378
379 /* Ensure we only read a complete number of words. */
380 bytes_remaining = bytes_to_read & 3;
381 bytes_to_read &= ~3;
382 words = bytes_to_read >> 2;
383
384 status = drvdata->config->get_configuration(drvdata,
385 kbuf, words);
386
387 /* If we didn't read correctly, then bail out. */
388 if (status) {
389 free_page((unsigned long)kbuf);
390 goto error;
391 }
392
393 /* If we fail to return the data to the user, then bail out. */
394 if (copy_to_user(buf, kbuf, bytes_to_read)) {
395 free_page((unsigned long)kbuf);
396 status = -EFAULT;
397 goto error;
398 }
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100399 memcpy(drvdata->read_buffer,
400 kbuf,
401 bytes_remaining);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100402 drvdata->read_buffer_in_use = bytes_remaining;
403 free_page((unsigned long)kbuf);
404 }
405 status = bytes_to_read;
406 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100407 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100408 return status;
409}
410
411static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100412hwicap_write(struct file *file, const char __user *buf,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100413 size_t count, loff_t *ppos)
414{
415 struct hwicap_drvdata *drvdata = file->private_data;
416 ssize_t written = 0;
417 ssize_t left = count;
418 u32 *kbuf;
419 ssize_t len;
420 ssize_t status;
421
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100422 status = mutex_lock_interruptible(&drvdata->sem);
423 if (status)
424 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100425
426 left += drvdata->write_buffer_in_use;
427
428 /* Only write multiples of 4 bytes. */
429 if (left < 4) {
430 status = 0;
431 goto error;
432 }
433
434 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
435 if (!kbuf) {
436 status = -ENOMEM;
437 goto error;
438 }
439
440 while (left > 3) {
441 /* only write multiples of 4 bytes, so there might */
442 /* be as many as 3 bytes left (at the end). */
443 len = left;
444
445 if (len > PAGE_SIZE)
446 len = PAGE_SIZE;
447 len &= ~3;
448
449 if (drvdata->write_buffer_in_use) {
450 memcpy(kbuf, drvdata->write_buffer,
451 drvdata->write_buffer_in_use);
452 if (copy_from_user(
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100453 (((char *)kbuf) + drvdata->write_buffer_in_use),
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100454 buf + written,
455 len - (drvdata->write_buffer_in_use))) {
456 free_page((unsigned long)kbuf);
457 status = -EFAULT;
458 goto error;
459 }
460 } else {
461 if (copy_from_user(kbuf, buf + written, len)) {
462 free_page((unsigned long)kbuf);
463 status = -EFAULT;
464 goto error;
465 }
466 }
467
468 status = drvdata->config->set_configuration(drvdata,
469 kbuf, len >> 2);
470
471 if (status) {
472 free_page((unsigned long)kbuf);
473 status = -EFAULT;
474 goto error;
475 }
476 if (drvdata->write_buffer_in_use) {
477 len -= drvdata->write_buffer_in_use;
478 left -= drvdata->write_buffer_in_use;
479 drvdata->write_buffer_in_use = 0;
480 }
481 written += len;
482 left -= len;
483 }
484 if ((left > 0) && (left < 4)) {
485 if (!copy_from_user(drvdata->write_buffer,
486 buf + written, left)) {
487 drvdata->write_buffer_in_use = left;
488 written += left;
489 left = 0;
490 }
491 }
492
493 free_page((unsigned long)kbuf);
494 status = written;
495 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100496 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100497 return status;
498}
499
500static int hwicap_open(struct inode *inode, struct file *file)
501{
502 struct hwicap_drvdata *drvdata;
503 int status;
504
Arnd Bergmann613655f2010-06-02 14:28:52 +0200505 mutex_lock(&hwicap_mutex);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100506 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
507
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100508 status = mutex_lock_interruptible(&drvdata->sem);
509 if (status)
Jonathan Corbetf4943db2008-05-16 13:50:20 -0600510 goto out;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100511
512 if (drvdata->is_open) {
513 status = -EBUSY;
514 goto error;
515 }
516
517 status = hwicap_initialize_hwicap(drvdata);
518 if (status) {
519 dev_err(drvdata->dev, "Failed to open file");
520 goto error;
521 }
522
523 file->private_data = drvdata;
524 drvdata->write_buffer_in_use = 0;
525 drvdata->read_buffer_in_use = 0;
526 drvdata->is_open = 1;
527
528 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100529 mutex_unlock(&drvdata->sem);
Jonathan Corbetf4943db2008-05-16 13:50:20 -0600530 out:
Arnd Bergmann613655f2010-06-02 14:28:52 +0200531 mutex_unlock(&hwicap_mutex);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100532 return status;
533}
534
535static int hwicap_release(struct inode *inode, struct file *file)
536{
537 struct hwicap_drvdata *drvdata = file->private_data;
538 int i;
539 int status = 0;
540
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100541 mutex_lock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100542
543 if (drvdata->write_buffer_in_use) {
544 /* Flush write buffer. */
545 for (i = drvdata->write_buffer_in_use; i < 4; i++)
546 drvdata->write_buffer[i] = 0;
547
548 status = drvdata->config->set_configuration(drvdata,
549 (u32 *) drvdata->write_buffer, 1);
550 if (status)
551 goto error;
552 }
553
554 status = hwicap_command_desync(drvdata);
555 if (status)
556 goto error;
557
558 error:
559 drvdata->is_open = 0;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100560 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100561 return status;
562}
563
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700564static const struct file_operations hwicap_fops = {
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100565 .owner = THIS_MODULE,
566 .write = hwicap_write,
567 .read = hwicap_read,
568 .open = hwicap_open,
569 .release = hwicap_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200570 .llseek = noop_llseek,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100571};
572
573static int __devinit hwicap_setup(struct device *dev, int id,
574 const struct resource *regs_res,
575 const struct hwicap_driver_config *config,
576 const struct config_registers *config_regs)
577{
578 dev_t devt;
579 struct hwicap_drvdata *drvdata = NULL;
580 int retval = 0;
581
582 dev_info(dev, "Xilinx icap port driver\n");
583
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100584 mutex_lock(&icap_sem);
585
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100586 if (id < 0) {
587 for (id = 0; id < HWICAP_DEVICES; id++)
588 if (!probed_devices[id])
589 break;
590 }
591 if (id < 0 || id >= HWICAP_DEVICES) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100592 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100593 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
594 return -EINVAL;
595 }
596 if (probed_devices[id]) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100597 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100598 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
599 DRIVER_NAME, id);
600 return -EBUSY;
601 }
602
603 probed_devices[id] = 1;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100604 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100605
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100606 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100607
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100608 drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100609 if (!drvdata) {
610 dev_err(dev, "Couldn't allocate device private record\n");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100611 retval = -ENOMEM;
612 goto failed0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100613 }
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100614 dev_set_drvdata(dev, (void *)drvdata);
615
616 if (!regs_res) {
617 dev_err(dev, "Couldn't get registers resource\n");
618 retval = -EFAULT;
619 goto failed1;
620 }
621
622 drvdata->mem_start = regs_res->start;
623 drvdata->mem_end = regs_res->end;
Joe Perches28f65c112011-06-09 09:13:32 -0700624 drvdata->mem_size = resource_size(regs_res);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100625
626 if (!request_mem_region(drvdata->mem_start,
627 drvdata->mem_size, DRIVER_NAME)) {
Kumar Galab17b8182008-04-30 10:24:44 -0500628 dev_err(dev, "Couldn't lock memory region at %Lx\n",
Grant Likelya1080962008-11-14 09:59:48 -0700629 (unsigned long long) regs_res->start);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100630 retval = -EBUSY;
631 goto failed1;
632 }
633
634 drvdata->devt = devt;
635 drvdata->dev = dev;
636 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
637 if (!drvdata->base_address) {
638 dev_err(dev, "ioremap() failed\n");
639 goto failed2;
640 }
641
642 drvdata->config = config;
643 drvdata->config_regs = config_regs;
644
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100645 mutex_init(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100646 drvdata->is_open = 0;
647
Grant Likelya1080962008-11-14 09:59:48 -0700648 dev_info(dev, "ioremap %llx to %p with size %llx\n",
649 (unsigned long long) drvdata->mem_start,
650 drvdata->base_address,
651 (unsigned long long) drvdata->mem_size);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100652
653 cdev_init(&drvdata->cdev, &hwicap_fops);
654 drvdata->cdev.owner = THIS_MODULE;
655 retval = cdev_add(&drvdata->cdev, devt, 1);
656 if (retval) {
657 dev_err(dev, "cdev_add() failed\n");
658 goto failed3;
659 }
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -0700660
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700661 device_create(icap_class, dev, devt, NULL, "%s%d", DRIVER_NAME, id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100662 return 0; /* success */
663
664 failed3:
665 iounmap(drvdata->base_address);
666
667 failed2:
668 release_mem_region(regs_res->start, drvdata->mem_size);
669
670 failed1:
671 kfree(drvdata);
672
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100673 failed0:
674 mutex_lock(&icap_sem);
675 probed_devices[id] = 0;
676 mutex_unlock(&icap_sem);
677
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100678 return retval;
679}
680
681static struct hwicap_driver_config buffer_icap_config = {
682 .get_configuration = buffer_icap_get_configuration,
683 .set_configuration = buffer_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100684 .get_status = buffer_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100685 .reset = buffer_icap_reset,
686};
687
688static struct hwicap_driver_config fifo_icap_config = {
689 .get_configuration = fifo_icap_get_configuration,
690 .set_configuration = fifo_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100691 .get_status = fifo_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100692 .reset = fifo_icap_reset,
693};
694
695static int __devexit hwicap_remove(struct device *dev)
696{
697 struct hwicap_drvdata *drvdata;
698
699 drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
700
701 if (!drvdata)
702 return 0;
703
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100704 device_destroy(icap_class, drvdata->devt);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100705 cdev_del(&drvdata->cdev);
706 iounmap(drvdata->base_address);
707 release_mem_region(drvdata->mem_start, drvdata->mem_size);
708 kfree(drvdata);
709 dev_set_drvdata(dev, NULL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100710
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100711 mutex_lock(&icap_sem);
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100712 probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100713 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100714 return 0; /* success */
715}
716
Grant Likely55f19d52011-02-22 20:13:26 -0700717#ifdef CONFIG_OF
Grant Likelyb1608d62011-05-18 11:19:24 -0600718static int __devinit hwicap_of_probe(struct platform_device *op,
719 const struct hwicap_driver_config *config)
Grant Likely55f19d52011-02-22 20:13:26 -0700720{
721 struct resource res;
722 const unsigned int *id;
723 const char *family;
724 int rc;
Grant Likely55f19d52011-02-22 20:13:26 -0700725 const struct config_registers *regs;
726
727
728 rc = of_address_to_resource(op->dev.of_node, 0, &res);
729 if (rc) {
730 dev_err(&op->dev, "invalid address\n");
731 return rc;
732 }
733
734 id = of_get_property(op->dev.of_node, "port-number", NULL);
735
736 /* It's most likely that we're using V4, if the family is not
737 specified */
738 regs = &v4_config_registers;
739 family = of_get_property(op->dev.of_node, "xlnx,family", NULL);
740
741 if (family) {
742 if (!strcmp(family, "virtex2p")) {
743 regs = &v2_config_registers;
744 } else if (!strcmp(family, "virtex4")) {
745 regs = &v4_config_registers;
746 } else if (!strcmp(family, "virtex5")) {
747 regs = &v5_config_registers;
748 }
749 }
750 return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
751 regs);
752}
753#else
Grant Likelyb1608d62011-05-18 11:19:24 -0600754static inline int hwicap_of_probe(struct platform_device *op,
755 const struct hwicap_driver_config *config)
Grant Likely55f19d52011-02-22 20:13:26 -0700756{
757 return -EINVAL;
758}
759#endif /* CONFIG_OF */
760
Grant Likelyb1608d62011-05-18 11:19:24 -0600761static const struct of_device_id __devinitconst hwicap_of_match[];
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100762static int __devinit hwicap_drv_probe(struct platform_device *pdev)
763{
Grant Likelyb1608d62011-05-18 11:19:24 -0600764 const struct of_device_id *match;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100765 struct resource *res;
766 const struct config_registers *regs;
767 const char *family;
768
Grant Likelyb1608d62011-05-18 11:19:24 -0600769 match = of_match_device(hwicap_of_match, &pdev->dev);
770 if (match)
771 return hwicap_of_probe(pdev, match->data);
Grant Likely55f19d52011-02-22 20:13:26 -0700772
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100773 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
774 if (!res)
775 return -ENODEV;
776
777 /* It's most likely that we're using V4, if the family is not
778 specified */
779 regs = &v4_config_registers;
780 family = pdev->dev.platform_data;
781
782 if (family) {
783 if (!strcmp(family, "virtex2p")) {
784 regs = &v2_config_registers;
785 } else if (!strcmp(family, "virtex4")) {
786 regs = &v4_config_registers;
787 } else if (!strcmp(family, "virtex5")) {
788 regs = &v5_config_registers;
789 }
790 }
791
792 return hwicap_setup(&pdev->dev, pdev->id, res,
793 &buffer_icap_config, regs);
794}
795
796static int __devexit hwicap_drv_remove(struct platform_device *pdev)
797{
798 return hwicap_remove(&pdev->dev);
799}
800
Grant Likely55f19d52011-02-22 20:13:26 -0700801#ifdef CONFIG_OF
802/* Match table for device tree binding */
Adrian Bunk2fd53e02008-04-23 19:51:38 +1000803static const struct of_device_id __devinitconst hwicap_of_match[] = {
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100804 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
805 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
806 {},
807};
808MODULE_DEVICE_TABLE(of, hwicap_of_match);
Grant Likely55f19d52011-02-22 20:13:26 -0700809#else
810#define hwicap_of_match NULL
811#endif
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100812
Grant Likely55f19d52011-02-22 20:13:26 -0700813static struct platform_driver hwicap_platform_driver = {
814 .probe = hwicap_drv_probe,
815 .remove = hwicap_drv_remove,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100816 .driver = {
Grant Likely40182942010-04-13 16:13:02 -0700817 .owner = THIS_MODULE,
Grant Likely55f19d52011-02-22 20:13:26 -0700818 .name = DRIVER_NAME,
Grant Likely40182942010-04-13 16:13:02 -0700819 .of_match_table = hwicap_of_match,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100820 },
821};
822
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100823static int __init hwicap_module_init(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100824{
825 dev_t devt;
826 int retval;
827
828 icap_class = class_create(THIS_MODULE, "xilinx_config");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100829 mutex_init(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100830
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100831 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
832 retval = register_chrdev_region(devt,
833 HWICAP_DEVICES,
834 DRIVER_NAME);
835 if (retval < 0)
836 return retval;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100837
838 retval = platform_driver_register(&hwicap_platform_driver);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100839 if (retval)
Grant Likely55f19d52011-02-22 20:13:26 -0700840 goto failed;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100841
842 return retval;
843
Grant Likely55f19d52011-02-22 20:13:26 -0700844 failed:
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100845 unregister_chrdev_region(devt, HWICAP_DEVICES);
846
847 return retval;
848}
849
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100850static void __exit hwicap_module_cleanup(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100851{
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100852 dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100853
854 class_destroy(icap_class);
855
856 platform_driver_unregister(&hwicap_platform_driver);
857
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100858 unregister_chrdev_region(devt, HWICAP_DEVICES);
859}
860
861module_init(hwicap_module_init);
862module_exit(hwicap_module_cleanup);
863
864MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
865MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
866MODULE_LICENSE("GPL");