blob: 51966ccf4ea3f56c4449f88c93906ab81e136111 [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 *
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/*
Stephen Neuendorfferac646732008-03-18 04:36:32 +110039 * This is the code behind /dev/icap* -- it allows a user-space
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110040 * 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 *
Stephen Neuendorfferac646732008-03-18 04:36:32 +110070 * cp foo.bit /dev/icap0
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110071 *
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>
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +110087#include <linux/mutex.h>
Jonathan Corbetf4943db2008-05-16 13:50:20 -060088#include <linux/smp_lock.h>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110089#include <linux/sysctl.h>
90#include <linux/version.h>
91#include <linux/fs.h>
92#include <linux/cdev.h>
93#include <linux/platform_device.h>
94
95#include <asm/io.h>
96#include <asm/uaccess.h>
97#include <asm/system.h>
98
99#ifdef CONFIG_OF
100/* For open firmware. */
101#include <linux/of_device.h>
102#include <linux/of_platform.h>
103#endif
104
105#include "xilinx_hwicap.h"
106#include "buffer_icap.h"
107#include "fifo_icap.h"
108
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100109#define DRIVER_NAME "icap"
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100110
111#define HWICAP_REGS (0x10000)
112
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100113#define XHWICAP_MAJOR 259
114#define XHWICAP_MINOR 0
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100115#define HWICAP_DEVICES 1
116
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100117/* An array, which is set to true when the device is registered. */
118static bool probed_devices[HWICAP_DEVICES];
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100119static struct mutex icap_sem;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100120
121static struct class *icap_class;
122
123#define UNIMPLEMENTED 0xFFFF
124
125static const struct config_registers v2_config_registers = {
126 .CRC = 0,
127 .FAR = 1,
128 .FDRI = 2,
129 .FDRO = 3,
130 .CMD = 4,
131 .CTL = 5,
132 .MASK = 6,
133 .STAT = 7,
134 .LOUT = 8,
135 .COR = 9,
136 .MFWR = 10,
137 .FLR = 11,
138 .KEY = 12,
139 .CBC = 13,
140 .IDCODE = 14,
141 .AXSS = UNIMPLEMENTED,
142 .C0R_1 = UNIMPLEMENTED,
143 .CSOB = UNIMPLEMENTED,
144 .WBSTAR = UNIMPLEMENTED,
145 .TIMER = UNIMPLEMENTED,
146 .BOOTSTS = UNIMPLEMENTED,
147 .CTL_1 = UNIMPLEMENTED,
148};
149
150static const struct config_registers v4_config_registers = {
151 .CRC = 0,
152 .FAR = 1,
153 .FDRI = 2,
154 .FDRO = 3,
155 .CMD = 4,
156 .CTL = 5,
157 .MASK = 6,
158 .STAT = 7,
159 .LOUT = 8,
160 .COR = 9,
161 .MFWR = 10,
162 .FLR = UNIMPLEMENTED,
163 .KEY = UNIMPLEMENTED,
164 .CBC = 11,
165 .IDCODE = 12,
166 .AXSS = 13,
167 .C0R_1 = UNIMPLEMENTED,
168 .CSOB = UNIMPLEMENTED,
169 .WBSTAR = UNIMPLEMENTED,
170 .TIMER = UNIMPLEMENTED,
171 .BOOTSTS = UNIMPLEMENTED,
172 .CTL_1 = UNIMPLEMENTED,
173};
174static const struct config_registers v5_config_registers = {
175 .CRC = 0,
176 .FAR = 1,
177 .FDRI = 2,
178 .FDRO = 3,
179 .CMD = 4,
180 .CTL = 5,
181 .MASK = 6,
182 .STAT = 7,
183 .LOUT = 8,
184 .COR = 9,
185 .MFWR = 10,
186 .FLR = UNIMPLEMENTED,
187 .KEY = UNIMPLEMENTED,
188 .CBC = 11,
189 .IDCODE = 12,
190 .AXSS = 13,
191 .C0R_1 = 14,
192 .CSOB = 15,
193 .WBSTAR = 16,
194 .TIMER = 17,
195 .BOOTSTS = 18,
196 .CTL_1 = 19,
197};
198
199/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100200 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
201 * @drvdata: a pointer to the drvdata.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100202 *
203 * This command desynchronizes the ICAP After this command, a
204 * bitstream containing a NULL packet, followed by a SYNCH packet is
205 * required before the ICAP will recognize commands.
206 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100207static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100208{
209 u32 buffer[4];
210 u32 index = 0;
211
212 /*
213 * Create the data to be written to the ICAP.
214 */
215 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
216 buffer[index++] = XHI_CMD_DESYNCH;
217 buffer[index++] = XHI_NOOP_PACKET;
218 buffer[index++] = XHI_NOOP_PACKET;
219
220 /*
221 * Write the data to the FIFO and intiate the transfer of data present
222 * in the FIFO to the ICAP device.
223 */
224 return drvdata->config->set_configuration(drvdata,
225 &buffer[0], index);
226}
227
228/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100229 * hwicap_get_configuration_register - Query a configuration register.
230 * @drvdata: a pointer to the drvdata.
231 * @reg: a constant which represents the configuration
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100232 * register value to be returned.
233 * Examples: XHI_IDCODE, XHI_FLR.
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100234 * @reg_data: returns the value of the register.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100235 *
236 * Sends a query packet to the ICAP and then receives the response.
237 * The icap is left in Synched state.
238 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100239static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
240 u32 reg, u32 *reg_data)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100241{
242 int status;
243 u32 buffer[6];
244 u32 index = 0;
245
246 /*
247 * Create the data to be written to the ICAP.
248 */
249 buffer[index++] = XHI_DUMMY_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100250 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100251 buffer[index++] = XHI_SYNC_PACKET;
252 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100253 buffer[index++] = XHI_NOOP_PACKET;
254
255 /*
256 * Write the data to the FIFO and initiate the transfer of data present
257 * in the FIFO to the ICAP device.
258 */
259 status = drvdata->config->set_configuration(drvdata,
260 &buffer[0], index);
261 if (status)
262 return status;
263
264 /* If the syncword was not found, then we need to start over. */
265 status = drvdata->config->get_status(drvdata);
266 if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
267 return -EIO;
268
269 index = 0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100270 buffer[index++] = hwicap_type_1_read(reg) | 1;
271 buffer[index++] = XHI_NOOP_PACKET;
272 buffer[index++] = XHI_NOOP_PACKET;
273
274 /*
275 * Write the data to the FIFO and intiate the transfer of data present
276 * in the FIFO to the ICAP device.
277 */
278 status = drvdata->config->set_configuration(drvdata,
279 &buffer[0], index);
280 if (status)
281 return status;
282
283 /*
284 * Read the configuration register
285 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100286 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100287 if (status)
288 return status;
289
290 return 0;
291}
292
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100293static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100294{
295 int status;
296 u32 idcode;
297
298 dev_dbg(drvdata->dev, "initializing\n");
299
300 /* Abort any current transaction, to make sure we have the
301 * ICAP in a good state. */
302 dev_dbg(drvdata->dev, "Reset...\n");
303 drvdata->config->reset(drvdata);
304
305 dev_dbg(drvdata->dev, "Desync...\n");
306 status = hwicap_command_desync(drvdata);
307 if (status)
308 return status;
309
310 /* Attempt to read the IDCODE from ICAP. This
311 * may not be returned correctly, due to the design of the
312 * hardware.
313 */
314 dev_dbg(drvdata->dev, "Reading IDCODE...\n");
315 status = hwicap_get_configuration_register(
316 drvdata, drvdata->config_regs->IDCODE, &idcode);
317 dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
318 if (status)
319 return status;
320
321 dev_dbg(drvdata->dev, "Desync...\n");
322 status = hwicap_command_desync(drvdata);
323 if (status)
324 return status;
325
326 return 0;
327}
328
329static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100330hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100331{
332 struct hwicap_drvdata *drvdata = file->private_data;
333 ssize_t bytes_to_read = 0;
334 u32 *kbuf;
335 u32 words;
336 u32 bytes_remaining;
337 int status;
338
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100339 status = mutex_lock_interruptible(&drvdata->sem);
340 if (status)
341 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100342
343 if (drvdata->read_buffer_in_use) {
344 /* If there are leftover bytes in the buffer, just */
345 /* return them and don't try to read more from the */
346 /* ICAP device. */
347 bytes_to_read =
348 (count < drvdata->read_buffer_in_use) ? count :
349 drvdata->read_buffer_in_use;
350
351 /* Return the data currently in the read buffer. */
352 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
353 status = -EFAULT;
354 goto error;
355 }
356 drvdata->read_buffer_in_use -= bytes_to_read;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100357 memmove(drvdata->read_buffer,
358 drvdata->read_buffer + bytes_to_read,
359 4 - bytes_to_read);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100360 } else {
361 /* Get new data from the ICAP, and return was was requested. */
362 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
363 if (!kbuf) {
364 status = -ENOMEM;
365 goto error;
366 }
367
368 /* The ICAP device is only able to read complete */
369 /* words. If a number of bytes that do not correspond */
370 /* to complete words is requested, then we read enough */
371 /* words to get the required number of bytes, and then */
372 /* save the remaining bytes for the next read. */
373
374 /* Determine the number of words to read, rounding up */
375 /* if necessary. */
376 words = ((count + 3) >> 2);
377 bytes_to_read = words << 2;
378
379 if (bytes_to_read > PAGE_SIZE)
380 bytes_to_read = PAGE_SIZE;
381
382 /* Ensure we only read a complete number of words. */
383 bytes_remaining = bytes_to_read & 3;
384 bytes_to_read &= ~3;
385 words = bytes_to_read >> 2;
386
387 status = drvdata->config->get_configuration(drvdata,
388 kbuf, words);
389
390 /* If we didn't read correctly, then bail out. */
391 if (status) {
392 free_page((unsigned long)kbuf);
393 goto error;
394 }
395
396 /* If we fail to return the data to the user, then bail out. */
397 if (copy_to_user(buf, kbuf, bytes_to_read)) {
398 free_page((unsigned long)kbuf);
399 status = -EFAULT;
400 goto error;
401 }
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100402 memcpy(drvdata->read_buffer,
403 kbuf,
404 bytes_remaining);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100405 drvdata->read_buffer_in_use = bytes_remaining;
406 free_page((unsigned long)kbuf);
407 }
408 status = bytes_to_read;
409 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100410 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100411 return status;
412}
413
414static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100415hwicap_write(struct file *file, const char __user *buf,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100416 size_t count, loff_t *ppos)
417{
418 struct hwicap_drvdata *drvdata = file->private_data;
419 ssize_t written = 0;
420 ssize_t left = count;
421 u32 *kbuf;
422 ssize_t len;
423 ssize_t status;
424
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100425 status = mutex_lock_interruptible(&drvdata->sem);
426 if (status)
427 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100428
429 left += drvdata->write_buffer_in_use;
430
431 /* Only write multiples of 4 bytes. */
432 if (left < 4) {
433 status = 0;
434 goto error;
435 }
436
437 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
438 if (!kbuf) {
439 status = -ENOMEM;
440 goto error;
441 }
442
443 while (left > 3) {
444 /* only write multiples of 4 bytes, so there might */
445 /* be as many as 3 bytes left (at the end). */
446 len = left;
447
448 if (len > PAGE_SIZE)
449 len = PAGE_SIZE;
450 len &= ~3;
451
452 if (drvdata->write_buffer_in_use) {
453 memcpy(kbuf, drvdata->write_buffer,
454 drvdata->write_buffer_in_use);
455 if (copy_from_user(
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100456 (((char *)kbuf) + drvdata->write_buffer_in_use),
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100457 buf + written,
458 len - (drvdata->write_buffer_in_use))) {
459 free_page((unsigned long)kbuf);
460 status = -EFAULT;
461 goto error;
462 }
463 } else {
464 if (copy_from_user(kbuf, buf + written, len)) {
465 free_page((unsigned long)kbuf);
466 status = -EFAULT;
467 goto error;
468 }
469 }
470
471 status = drvdata->config->set_configuration(drvdata,
472 kbuf, len >> 2);
473
474 if (status) {
475 free_page((unsigned long)kbuf);
476 status = -EFAULT;
477 goto error;
478 }
479 if (drvdata->write_buffer_in_use) {
480 len -= drvdata->write_buffer_in_use;
481 left -= drvdata->write_buffer_in_use;
482 drvdata->write_buffer_in_use = 0;
483 }
484 written += len;
485 left -= len;
486 }
487 if ((left > 0) && (left < 4)) {
488 if (!copy_from_user(drvdata->write_buffer,
489 buf + written, left)) {
490 drvdata->write_buffer_in_use = left;
491 written += left;
492 left = 0;
493 }
494 }
495
496 free_page((unsigned long)kbuf);
497 status = written;
498 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100499 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100500 return status;
501}
502
503static int hwicap_open(struct inode *inode, struct file *file)
504{
505 struct hwicap_drvdata *drvdata;
506 int status;
507
Jonathan Corbetf4943db2008-05-16 13:50:20 -0600508 lock_kernel();
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100509 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
510
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100511 status = mutex_lock_interruptible(&drvdata->sem);
512 if (status)
Jonathan Corbetf4943db2008-05-16 13:50:20 -0600513 goto out;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100514
515 if (drvdata->is_open) {
516 status = -EBUSY;
517 goto error;
518 }
519
520 status = hwicap_initialize_hwicap(drvdata);
521 if (status) {
522 dev_err(drvdata->dev, "Failed to open file");
523 goto error;
524 }
525
526 file->private_data = drvdata;
527 drvdata->write_buffer_in_use = 0;
528 drvdata->read_buffer_in_use = 0;
529 drvdata->is_open = 1;
530
531 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100532 mutex_unlock(&drvdata->sem);
Jonathan Corbetf4943db2008-05-16 13:50:20 -0600533 out:
534 unlock_kernel();
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100535 return status;
536}
537
538static 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
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100544 mutex_lock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100545
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;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100563 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100564 return status;
565}
566
567static 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
575static 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
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100586 mutex_lock(&icap_sem);
587
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100588 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) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100594 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100595 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
596 return -EINVAL;
597 }
598 if (probed_devices[id]) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100599 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100600 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;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100606 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100607
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100608 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100609
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100610 drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100611 if (!drvdata) {
612 dev_err(dev, "Couldn't allocate device private record\n");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100613 retval = -ENOMEM;
614 goto failed0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100615 }
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100616 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)) {
Kumar Galab17b8182008-04-30 10:24:44 -0500630 dev_err(dev, "Couldn't lock memory region at %Lx\n",
631 regs_res->start);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100632 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
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100647 mutex_init(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100648 drvdata->is_open = 0;
649
Kumar Galab17b8182008-04-30 10:24:44 -0500650 dev_info(dev, "ioremap %lx to %p with size %Lx\n",
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100651 (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 }
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -0700661
662 device_create_drvdata(icap_class, dev, devt, NULL,
663 "%s%d", DRIVER_NAME, id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100664 return 0; /* success */
665
666 failed3:
667 iounmap(drvdata->base_address);
668
669 failed2:
670 release_mem_region(regs_res->start, drvdata->mem_size);
671
672 failed1:
673 kfree(drvdata);
674
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100675 failed0:
676 mutex_lock(&icap_sem);
677 probed_devices[id] = 0;
678 mutex_unlock(&icap_sem);
679
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100680 return retval;
681}
682
683static struct hwicap_driver_config buffer_icap_config = {
684 .get_configuration = buffer_icap_get_configuration,
685 .set_configuration = buffer_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100686 .get_status = buffer_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100687 .reset = buffer_icap_reset,
688};
689
690static struct hwicap_driver_config fifo_icap_config = {
691 .get_configuration = fifo_icap_get_configuration,
692 .set_configuration = fifo_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100693 .get_status = fifo_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100694 .reset = fifo_icap_reset,
695};
696
697static int __devexit hwicap_remove(struct device *dev)
698{
699 struct hwicap_drvdata *drvdata;
700
701 drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
702
703 if (!drvdata)
704 return 0;
705
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100706 device_destroy(icap_class, drvdata->devt);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100707 cdev_del(&drvdata->cdev);
708 iounmap(drvdata->base_address);
709 release_mem_region(drvdata->mem_start, drvdata->mem_size);
710 kfree(drvdata);
711 dev_set_drvdata(dev, NULL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100712
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100713 mutex_lock(&icap_sem);
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100714 probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100715 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100716 return 0; /* success */
717}
718
719static int __devinit hwicap_drv_probe(struct platform_device *pdev)
720{
721 struct resource *res;
722 const struct config_registers *regs;
723 const char *family;
724
725 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
726 if (!res)
727 return -ENODEV;
728
729 /* It's most likely that we're using V4, if the family is not
730 specified */
731 regs = &v4_config_registers;
732 family = pdev->dev.platform_data;
733
734 if (family) {
735 if (!strcmp(family, "virtex2p")) {
736 regs = &v2_config_registers;
737 } else if (!strcmp(family, "virtex4")) {
738 regs = &v4_config_registers;
739 } else if (!strcmp(family, "virtex5")) {
740 regs = &v5_config_registers;
741 }
742 }
743
744 return hwicap_setup(&pdev->dev, pdev->id, res,
745 &buffer_icap_config, regs);
746}
747
748static int __devexit hwicap_drv_remove(struct platform_device *pdev)
749{
750 return hwicap_remove(&pdev->dev);
751}
752
753static struct platform_driver hwicap_platform_driver = {
754 .probe = hwicap_drv_probe,
755 .remove = hwicap_drv_remove,
756 .driver = {
757 .owner = THIS_MODULE,
758 .name = DRIVER_NAME,
759 },
760};
761
762/* ---------------------------------------------------------------------
763 * OF bus binding
764 */
765
766#if defined(CONFIG_OF)
767static int __devinit
768hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
769{
770 struct resource res;
771 const unsigned int *id;
772 const char *family;
773 int rc;
774 const struct hwicap_driver_config *config = match->data;
775 const struct config_registers *regs;
776
777 dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
778
779 rc = of_address_to_resource(op->node, 0, &res);
780 if (rc) {
781 dev_err(&op->dev, "invalid address\n");
782 return rc;
783 }
784
785 id = of_get_property(op->node, "port-number", NULL);
786
787 /* It's most likely that we're using V4, if the family is not
788 specified */
789 regs = &v4_config_registers;
790 family = of_get_property(op->node, "xlnx,family", NULL);
791
792 if (family) {
793 if (!strcmp(family, "virtex2p")) {
794 regs = &v2_config_registers;
795 } else if (!strcmp(family, "virtex4")) {
796 regs = &v4_config_registers;
797 } else if (!strcmp(family, "virtex5")) {
798 regs = &v5_config_registers;
799 }
800 }
801 return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
802 regs);
803}
804
805static int __devexit hwicap_of_remove(struct of_device *op)
806{
807 return hwicap_remove(&op->dev);
808}
809
810/* Match table for of_platform binding */
Adrian Bunk2fd53e02008-04-23 19:51:38 +1000811static const struct of_device_id __devinitconst hwicap_of_match[] = {
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100812 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
813 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
814 {},
815};
816MODULE_DEVICE_TABLE(of, hwicap_of_match);
817
818static struct of_platform_driver hwicap_of_driver = {
819 .owner = THIS_MODULE,
820 .name = DRIVER_NAME,
821 .match_table = hwicap_of_match,
822 .probe = hwicap_of_probe,
823 .remove = __devexit_p(hwicap_of_remove),
824 .driver = {
825 .name = DRIVER_NAME,
826 },
827};
828
829/* Registration helpers to keep the number of #ifdefs to a minimum */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100830static inline int __init hwicap_of_register(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100831{
832 pr_debug("hwicap: calling of_register_platform_driver()\n");
833 return of_register_platform_driver(&hwicap_of_driver);
834}
835
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100836static inline void __exit hwicap_of_unregister(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100837{
838 of_unregister_platform_driver(&hwicap_of_driver);
839}
840#else /* CONFIG_OF */
841/* CONFIG_OF not enabled; do nothing helpers */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100842static inline int __init hwicap_of_register(void) { return 0; }
843static inline void __exit hwicap_of_unregister(void) { }
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100844#endif /* CONFIG_OF */
845
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100846static int __init hwicap_module_init(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100847{
848 dev_t devt;
849 int retval;
850
851 icap_class = class_create(THIS_MODULE, "xilinx_config");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100852 mutex_init(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100853
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100854 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
855 retval = register_chrdev_region(devt,
856 HWICAP_DEVICES,
857 DRIVER_NAME);
858 if (retval < 0)
859 return retval;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100860
861 retval = platform_driver_register(&hwicap_platform_driver);
862
863 if (retval)
864 goto failed1;
865
866 retval = hwicap_of_register();
867
868 if (retval)
869 goto failed2;
870
871 return retval;
872
873 failed2:
874 platform_driver_unregister(&hwicap_platform_driver);
875
876 failed1:
877 unregister_chrdev_region(devt, HWICAP_DEVICES);
878
879 return retval;
880}
881
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100882static void __exit hwicap_module_cleanup(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100883{
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100884 dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100885
886 class_destroy(icap_class);
887
888 platform_driver_unregister(&hwicap_platform_driver);
889
890 hwicap_of_unregister();
891
892 unregister_chrdev_region(devt, HWICAP_DEVICES);
893}
894
895module_init(hwicap_module_init);
896module_exit(hwicap_module_cleanup);
897
898MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
899MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
900MODULE_LICENSE("GPL");