blob: 3edf1fc1296312e09670427b6500a442d50a7c5f [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>
Stephen Neuendorfferef141a02008-02-06 04:24:09 +110088#include <linux/sysctl.h>
89#include <linux/version.h>
90#include <linux/fs.h>
91#include <linux/cdev.h>
92#include <linux/platform_device.h>
93
94#include <asm/io.h>
95#include <asm/uaccess.h>
96#include <asm/system.h>
97
98#ifdef CONFIG_OF
99/* For open firmware. */
100#include <linux/of_device.h>
101#include <linux/of_platform.h>
102#endif
103
104#include "xilinx_hwicap.h"
105#include "buffer_icap.h"
106#include "fifo_icap.h"
107
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100108#define DRIVER_NAME "icap"
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100109
110#define HWICAP_REGS (0x10000)
111
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100112#define XHWICAP_MAJOR 259
113#define XHWICAP_MINOR 0
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100114#define HWICAP_DEVICES 1
115
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100116/* An array, which is set to true when the device is registered. */
117static bool probed_devices[HWICAP_DEVICES];
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100118static struct mutex icap_sem;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100119
120static struct class *icap_class;
121
122#define UNIMPLEMENTED 0xFFFF
123
124static const struct config_registers v2_config_registers = {
125 .CRC = 0,
126 .FAR = 1,
127 .FDRI = 2,
128 .FDRO = 3,
129 .CMD = 4,
130 .CTL = 5,
131 .MASK = 6,
132 .STAT = 7,
133 .LOUT = 8,
134 .COR = 9,
135 .MFWR = 10,
136 .FLR = 11,
137 .KEY = 12,
138 .CBC = 13,
139 .IDCODE = 14,
140 .AXSS = UNIMPLEMENTED,
141 .C0R_1 = UNIMPLEMENTED,
142 .CSOB = UNIMPLEMENTED,
143 .WBSTAR = UNIMPLEMENTED,
144 .TIMER = UNIMPLEMENTED,
145 .BOOTSTS = UNIMPLEMENTED,
146 .CTL_1 = UNIMPLEMENTED,
147};
148
149static const struct config_registers v4_config_registers = {
150 .CRC = 0,
151 .FAR = 1,
152 .FDRI = 2,
153 .FDRO = 3,
154 .CMD = 4,
155 .CTL = 5,
156 .MASK = 6,
157 .STAT = 7,
158 .LOUT = 8,
159 .COR = 9,
160 .MFWR = 10,
161 .FLR = UNIMPLEMENTED,
162 .KEY = UNIMPLEMENTED,
163 .CBC = 11,
164 .IDCODE = 12,
165 .AXSS = 13,
166 .C0R_1 = UNIMPLEMENTED,
167 .CSOB = UNIMPLEMENTED,
168 .WBSTAR = UNIMPLEMENTED,
169 .TIMER = UNIMPLEMENTED,
170 .BOOTSTS = UNIMPLEMENTED,
171 .CTL_1 = UNIMPLEMENTED,
172};
173static const struct config_registers v5_config_registers = {
174 .CRC = 0,
175 .FAR = 1,
176 .FDRI = 2,
177 .FDRO = 3,
178 .CMD = 4,
179 .CTL = 5,
180 .MASK = 6,
181 .STAT = 7,
182 .LOUT = 8,
183 .COR = 9,
184 .MFWR = 10,
185 .FLR = UNIMPLEMENTED,
186 .KEY = UNIMPLEMENTED,
187 .CBC = 11,
188 .IDCODE = 12,
189 .AXSS = 13,
190 .C0R_1 = 14,
191 .CSOB = 15,
192 .WBSTAR = 16,
193 .TIMER = 17,
194 .BOOTSTS = 18,
195 .CTL_1 = 19,
196};
197
198/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100199 * hwicap_command_desync - Send a DESYNC command to the ICAP port.
200 * @drvdata: a pointer to the drvdata.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100201 *
202 * This command desynchronizes the ICAP After this command, a
203 * bitstream containing a NULL packet, followed by a SYNCH packet is
204 * required before the ICAP will recognize commands.
205 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100206static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100207{
208 u32 buffer[4];
209 u32 index = 0;
210
211 /*
212 * Create the data to be written to the ICAP.
213 */
214 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
215 buffer[index++] = XHI_CMD_DESYNCH;
216 buffer[index++] = XHI_NOOP_PACKET;
217 buffer[index++] = XHI_NOOP_PACKET;
218
219 /*
220 * Write the data to the FIFO and intiate the transfer of data present
221 * in the FIFO to the ICAP device.
222 */
223 return drvdata->config->set_configuration(drvdata,
224 &buffer[0], index);
225}
226
227/**
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100228 * hwicap_get_configuration_register - Query a configuration register.
229 * @drvdata: a pointer to the drvdata.
230 * @reg: a constant which represents the configuration
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100231 * register value to be returned.
232 * Examples: XHI_IDCODE, XHI_FLR.
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100233 * @reg_data: returns the value of the register.
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100234 *
235 * Sends a query packet to the ICAP and then receives the response.
236 * The icap is left in Synched state.
237 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100238static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
239 u32 reg, u32 *reg_data)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100240{
241 int status;
242 u32 buffer[6];
243 u32 index = 0;
244
245 /*
246 * Create the data to be written to the ICAP.
247 */
248 buffer[index++] = XHI_DUMMY_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100249 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100250 buffer[index++] = XHI_SYNC_PACKET;
251 buffer[index++] = XHI_NOOP_PACKET;
Stephen Neuendorffer4c58f8f2008-03-18 04:36:31 +1100252 buffer[index++] = XHI_NOOP_PACKET;
253
254 /*
255 * Write the data to the FIFO and initiate the transfer of data present
256 * in the FIFO to the ICAP device.
257 */
258 status = drvdata->config->set_configuration(drvdata,
259 &buffer[0], index);
260 if (status)
261 return status;
262
263 /* If the syncword was not found, then we need to start over. */
264 status = drvdata->config->get_status(drvdata);
265 if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
266 return -EIO;
267
268 index = 0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100269 buffer[index++] = hwicap_type_1_read(reg) | 1;
270 buffer[index++] = XHI_NOOP_PACKET;
271 buffer[index++] = XHI_NOOP_PACKET;
272
273 /*
274 * Write the data to the FIFO and intiate the transfer of data present
275 * in the FIFO to the ICAP device.
276 */
277 status = drvdata->config->set_configuration(drvdata,
278 &buffer[0], index);
279 if (status)
280 return status;
281
282 /*
283 * Read the configuration register
284 */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100285 status = drvdata->config->get_configuration(drvdata, reg_data, 1);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100286 if (status)
287 return status;
288
289 return 0;
290}
291
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100292static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100293{
294 int status;
295 u32 idcode;
296
297 dev_dbg(drvdata->dev, "initializing\n");
298
299 /* Abort any current transaction, to make sure we have the
300 * ICAP in a good state. */
301 dev_dbg(drvdata->dev, "Reset...\n");
302 drvdata->config->reset(drvdata);
303
304 dev_dbg(drvdata->dev, "Desync...\n");
305 status = hwicap_command_desync(drvdata);
306 if (status)
307 return status;
308
309 /* Attempt to read the IDCODE from ICAP. This
310 * may not be returned correctly, due to the design of the
311 * hardware.
312 */
313 dev_dbg(drvdata->dev, "Reading IDCODE...\n");
314 status = hwicap_get_configuration_register(
315 drvdata, drvdata->config_regs->IDCODE, &idcode);
316 dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
317 if (status)
318 return status;
319
320 dev_dbg(drvdata->dev, "Desync...\n");
321 status = hwicap_command_desync(drvdata);
322 if (status)
323 return status;
324
325 return 0;
326}
327
328static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100329hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100330{
331 struct hwicap_drvdata *drvdata = file->private_data;
332 ssize_t bytes_to_read = 0;
333 u32 *kbuf;
334 u32 words;
335 u32 bytes_remaining;
336 int status;
337
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100338 status = mutex_lock_interruptible(&drvdata->sem);
339 if (status)
340 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100341
342 if (drvdata->read_buffer_in_use) {
343 /* If there are leftover bytes in the buffer, just */
344 /* return them and don't try to read more from the */
345 /* ICAP device. */
346 bytes_to_read =
347 (count < drvdata->read_buffer_in_use) ? count :
348 drvdata->read_buffer_in_use;
349
350 /* Return the data currently in the read buffer. */
351 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
352 status = -EFAULT;
353 goto error;
354 }
355 drvdata->read_buffer_in_use -= bytes_to_read;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100356 memmove(drvdata->read_buffer,
357 drvdata->read_buffer + bytes_to_read,
358 4 - bytes_to_read);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100359 } else {
360 /* Get new data from the ICAP, and return was was requested. */
361 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
362 if (!kbuf) {
363 status = -ENOMEM;
364 goto error;
365 }
366
367 /* The ICAP device is only able to read complete */
368 /* words. If a number of bytes that do not correspond */
369 /* to complete words is requested, then we read enough */
370 /* words to get the required number of bytes, and then */
371 /* save the remaining bytes for the next read. */
372
373 /* Determine the number of words to read, rounding up */
374 /* if necessary. */
375 words = ((count + 3) >> 2);
376 bytes_to_read = words << 2;
377
378 if (bytes_to_read > PAGE_SIZE)
379 bytes_to_read = PAGE_SIZE;
380
381 /* Ensure we only read a complete number of words. */
382 bytes_remaining = bytes_to_read & 3;
383 bytes_to_read &= ~3;
384 words = bytes_to_read >> 2;
385
386 status = drvdata->config->get_configuration(drvdata,
387 kbuf, words);
388
389 /* If we didn't read correctly, then bail out. */
390 if (status) {
391 free_page((unsigned long)kbuf);
392 goto error;
393 }
394
395 /* If we fail to return the data to the user, then bail out. */
396 if (copy_to_user(buf, kbuf, bytes_to_read)) {
397 free_page((unsigned long)kbuf);
398 status = -EFAULT;
399 goto error;
400 }
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100401 memcpy(drvdata->read_buffer,
402 kbuf,
403 bytes_remaining);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100404 drvdata->read_buffer_in_use = bytes_remaining;
405 free_page((unsigned long)kbuf);
406 }
407 status = bytes_to_read;
408 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100409 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100410 return status;
411}
412
413static ssize_t
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100414hwicap_write(struct file *file, const char __user *buf,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100415 size_t count, loff_t *ppos)
416{
417 struct hwicap_drvdata *drvdata = file->private_data;
418 ssize_t written = 0;
419 ssize_t left = count;
420 u32 *kbuf;
421 ssize_t len;
422 ssize_t status;
423
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100424 status = mutex_lock_interruptible(&drvdata->sem);
425 if (status)
426 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100427
428 left += drvdata->write_buffer_in_use;
429
430 /* Only write multiples of 4 bytes. */
431 if (left < 4) {
432 status = 0;
433 goto error;
434 }
435
436 kbuf = (u32 *) __get_free_page(GFP_KERNEL);
437 if (!kbuf) {
438 status = -ENOMEM;
439 goto error;
440 }
441
442 while (left > 3) {
443 /* only write multiples of 4 bytes, so there might */
444 /* be as many as 3 bytes left (at the end). */
445 len = left;
446
447 if (len > PAGE_SIZE)
448 len = PAGE_SIZE;
449 len &= ~3;
450
451 if (drvdata->write_buffer_in_use) {
452 memcpy(kbuf, drvdata->write_buffer,
453 drvdata->write_buffer_in_use);
454 if (copy_from_user(
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100455 (((char *)kbuf) + drvdata->write_buffer_in_use),
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100456 buf + written,
457 len - (drvdata->write_buffer_in_use))) {
458 free_page((unsigned long)kbuf);
459 status = -EFAULT;
460 goto error;
461 }
462 } else {
463 if (copy_from_user(kbuf, buf + written, len)) {
464 free_page((unsigned long)kbuf);
465 status = -EFAULT;
466 goto error;
467 }
468 }
469
470 status = drvdata->config->set_configuration(drvdata,
471 kbuf, len >> 2);
472
473 if (status) {
474 free_page((unsigned long)kbuf);
475 status = -EFAULT;
476 goto error;
477 }
478 if (drvdata->write_buffer_in_use) {
479 len -= drvdata->write_buffer_in_use;
480 left -= drvdata->write_buffer_in_use;
481 drvdata->write_buffer_in_use = 0;
482 }
483 written += len;
484 left -= len;
485 }
486 if ((left > 0) && (left < 4)) {
487 if (!copy_from_user(drvdata->write_buffer,
488 buf + written, left)) {
489 drvdata->write_buffer_in_use = left;
490 written += left;
491 left = 0;
492 }
493 }
494
495 free_page((unsigned long)kbuf);
496 status = written;
497 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100498 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100499 return status;
500}
501
502static int hwicap_open(struct inode *inode, struct file *file)
503{
504 struct hwicap_drvdata *drvdata;
505 int status;
506
507 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
508
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100509 status = mutex_lock_interruptible(&drvdata->sem);
510 if (status)
511 return status;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100512
513 if (drvdata->is_open) {
514 status = -EBUSY;
515 goto error;
516 }
517
518 status = hwicap_initialize_hwicap(drvdata);
519 if (status) {
520 dev_err(drvdata->dev, "Failed to open file");
521 goto error;
522 }
523
524 file->private_data = drvdata;
525 drvdata->write_buffer_in_use = 0;
526 drvdata->read_buffer_in_use = 0;
527 drvdata->is_open = 1;
528
529 error:
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100530 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100531 return status;
532}
533
534static int hwicap_release(struct inode *inode, struct file *file)
535{
536 struct hwicap_drvdata *drvdata = file->private_data;
537 int i;
538 int status = 0;
539
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100540 mutex_lock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100541
542 if (drvdata->write_buffer_in_use) {
543 /* Flush write buffer. */
544 for (i = drvdata->write_buffer_in_use; i < 4; i++)
545 drvdata->write_buffer[i] = 0;
546
547 status = drvdata->config->set_configuration(drvdata,
548 (u32 *) drvdata->write_buffer, 1);
549 if (status)
550 goto error;
551 }
552
553 status = hwicap_command_desync(drvdata);
554 if (status)
555 goto error;
556
557 error:
558 drvdata->is_open = 0;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100559 mutex_unlock(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100560 return status;
561}
562
563static struct file_operations hwicap_fops = {
564 .owner = THIS_MODULE,
565 .write = hwicap_write,
566 .read = hwicap_read,
567 .open = hwicap_open,
568 .release = hwicap_release,
569};
570
571static int __devinit hwicap_setup(struct device *dev, int id,
572 const struct resource *regs_res,
573 const struct hwicap_driver_config *config,
574 const struct config_registers *config_regs)
575{
576 dev_t devt;
577 struct hwicap_drvdata *drvdata = NULL;
578 int retval = 0;
579
580 dev_info(dev, "Xilinx icap port driver\n");
581
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100582 mutex_lock(&icap_sem);
583
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100584 if (id < 0) {
585 for (id = 0; id < HWICAP_DEVICES; id++)
586 if (!probed_devices[id])
587 break;
588 }
589 if (id < 0 || id >= HWICAP_DEVICES) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100590 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100591 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
592 return -EINVAL;
593 }
594 if (probed_devices[id]) {
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100595 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100596 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
597 DRIVER_NAME, id);
598 return -EBUSY;
599 }
600
601 probed_devices[id] = 1;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100602 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100603
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100604 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100605
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100606 drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100607 if (!drvdata) {
608 dev_err(dev, "Couldn't allocate device private record\n");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100609 retval = -ENOMEM;
610 goto failed0;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100611 }
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100612 dev_set_drvdata(dev, (void *)drvdata);
613
614 if (!regs_res) {
615 dev_err(dev, "Couldn't get registers resource\n");
616 retval = -EFAULT;
617 goto failed1;
618 }
619
620 drvdata->mem_start = regs_res->start;
621 drvdata->mem_end = regs_res->end;
622 drvdata->mem_size = regs_res->end - regs_res->start + 1;
623
624 if (!request_mem_region(drvdata->mem_start,
625 drvdata->mem_size, DRIVER_NAME)) {
Kumar Galab17b8182008-04-30 10:24:44 -0500626 dev_err(dev, "Couldn't lock memory region at %Lx\n",
627 regs_res->start);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100628 retval = -EBUSY;
629 goto failed1;
630 }
631
632 drvdata->devt = devt;
633 drvdata->dev = dev;
634 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
635 if (!drvdata->base_address) {
636 dev_err(dev, "ioremap() failed\n");
637 goto failed2;
638 }
639
640 drvdata->config = config;
641 drvdata->config_regs = config_regs;
642
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100643 mutex_init(&drvdata->sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100644 drvdata->is_open = 0;
645
Kumar Galab17b8182008-04-30 10:24:44 -0500646 dev_info(dev, "ioremap %lx to %p with size %Lx\n",
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100647 (unsigned long int)drvdata->mem_start,
648 drvdata->base_address, drvdata->mem_size);
649
650 cdev_init(&drvdata->cdev, &hwicap_fops);
651 drvdata->cdev.owner = THIS_MODULE;
652 retval = cdev_add(&drvdata->cdev, devt, 1);
653 if (retval) {
654 dev_err(dev, "cdev_add() failed\n");
655 goto failed3;
656 }
657 /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100658 device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100659 return 0; /* success */
660
661 failed3:
662 iounmap(drvdata->base_address);
663
664 failed2:
665 release_mem_region(regs_res->start, drvdata->mem_size);
666
667 failed1:
668 kfree(drvdata);
669
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100670 failed0:
671 mutex_lock(&icap_sem);
672 probed_devices[id] = 0;
673 mutex_unlock(&icap_sem);
674
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100675 return retval;
676}
677
678static struct hwicap_driver_config buffer_icap_config = {
679 .get_configuration = buffer_icap_get_configuration,
680 .set_configuration = buffer_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100681 .get_status = buffer_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100682 .reset = buffer_icap_reset,
683};
684
685static struct hwicap_driver_config fifo_icap_config = {
686 .get_configuration = fifo_icap_get_configuration,
687 .set_configuration = fifo_icap_set_configuration,
Stephen Neuendorffer6b06fdb2008-03-18 04:36:30 +1100688 .get_status = fifo_icap_get_status,
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100689 .reset = fifo_icap_reset,
690};
691
692static int __devexit hwicap_remove(struct device *dev)
693{
694 struct hwicap_drvdata *drvdata;
695
696 drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
697
698 if (!drvdata)
699 return 0;
700
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100701 device_destroy(icap_class, drvdata->devt);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100702 cdev_del(&drvdata->cdev);
703 iounmap(drvdata->base_address);
704 release_mem_region(drvdata->mem_start, drvdata->mem_size);
705 kfree(drvdata);
706 dev_set_drvdata(dev, NULL);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100707
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100708 mutex_lock(&icap_sem);
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100709 probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100710 mutex_unlock(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100711 return 0; /* success */
712}
713
714static int __devinit hwicap_drv_probe(struct platform_device *pdev)
715{
716 struct resource *res;
717 const struct config_registers *regs;
718 const char *family;
719
720 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
721 if (!res)
722 return -ENODEV;
723
724 /* It's most likely that we're using V4, if the family is not
725 specified */
726 regs = &v4_config_registers;
727 family = pdev->dev.platform_data;
728
729 if (family) {
730 if (!strcmp(family, "virtex2p")) {
731 regs = &v2_config_registers;
732 } else if (!strcmp(family, "virtex4")) {
733 regs = &v4_config_registers;
734 } else if (!strcmp(family, "virtex5")) {
735 regs = &v5_config_registers;
736 }
737 }
738
739 return hwicap_setup(&pdev->dev, pdev->id, res,
740 &buffer_icap_config, regs);
741}
742
743static int __devexit hwicap_drv_remove(struct platform_device *pdev)
744{
745 return hwicap_remove(&pdev->dev);
746}
747
748static struct platform_driver hwicap_platform_driver = {
749 .probe = hwicap_drv_probe,
750 .remove = hwicap_drv_remove,
751 .driver = {
752 .owner = THIS_MODULE,
753 .name = DRIVER_NAME,
754 },
755};
756
757/* ---------------------------------------------------------------------
758 * OF bus binding
759 */
760
761#if defined(CONFIG_OF)
762static int __devinit
763hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
764{
765 struct resource res;
766 const unsigned int *id;
767 const char *family;
768 int rc;
769 const struct hwicap_driver_config *config = match->data;
770 const struct config_registers *regs;
771
772 dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
773
774 rc = of_address_to_resource(op->node, 0, &res);
775 if (rc) {
776 dev_err(&op->dev, "invalid address\n");
777 return rc;
778 }
779
780 id = of_get_property(op->node, "port-number", NULL);
781
782 /* It's most likely that we're using V4, if the family is not
783 specified */
784 regs = &v4_config_registers;
785 family = of_get_property(op->node, "xlnx,family", NULL);
786
787 if (family) {
788 if (!strcmp(family, "virtex2p")) {
789 regs = &v2_config_registers;
790 } else if (!strcmp(family, "virtex4")) {
791 regs = &v4_config_registers;
792 } else if (!strcmp(family, "virtex5")) {
793 regs = &v5_config_registers;
794 }
795 }
796 return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
797 regs);
798}
799
800static int __devexit hwicap_of_remove(struct of_device *op)
801{
802 return hwicap_remove(&op->dev);
803}
804
805/* Match table for of_platform binding */
Adrian Bunk2fd53e02008-04-23 19:51:38 +1000806static const struct of_device_id __devinitconst hwicap_of_match[] = {
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100807 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
808 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
809 {},
810};
811MODULE_DEVICE_TABLE(of, hwicap_of_match);
812
813static struct of_platform_driver hwicap_of_driver = {
814 .owner = THIS_MODULE,
815 .name = DRIVER_NAME,
816 .match_table = hwicap_of_match,
817 .probe = hwicap_of_probe,
818 .remove = __devexit_p(hwicap_of_remove),
819 .driver = {
820 .name = DRIVER_NAME,
821 },
822};
823
824/* Registration helpers to keep the number of #ifdefs to a minimum */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100825static inline int __init hwicap_of_register(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100826{
827 pr_debug("hwicap: calling of_register_platform_driver()\n");
828 return of_register_platform_driver(&hwicap_of_driver);
829}
830
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100831static inline void __exit hwicap_of_unregister(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100832{
833 of_unregister_platform_driver(&hwicap_of_driver);
834}
835#else /* CONFIG_OF */
836/* CONFIG_OF not enabled; do nothing helpers */
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100837static inline int __init hwicap_of_register(void) { return 0; }
838static inline void __exit hwicap_of_unregister(void) { }
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100839#endif /* CONFIG_OF */
840
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100841static int __init hwicap_module_init(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100842{
843 dev_t devt;
844 int retval;
845
846 icap_class = class_create(THIS_MODULE, "xilinx_config");
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100847 mutex_init(&icap_sem);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100848
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100849 devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
850 retval = register_chrdev_region(devt,
851 HWICAP_DEVICES,
852 DRIVER_NAME);
853 if (retval < 0)
854 return retval;
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100855
856 retval = platform_driver_register(&hwicap_platform_driver);
857
858 if (retval)
859 goto failed1;
860
861 retval = hwicap_of_register();
862
863 if (retval)
864 goto failed2;
865
866 return retval;
867
868 failed2:
869 platform_driver_unregister(&hwicap_platform_driver);
870
871 failed1:
872 unregister_chrdev_region(devt, HWICAP_DEVICES);
873
874 return retval;
875}
876
Stephen Neuendorfferf62f2fd2008-02-25 10:34:47 +1100877static void __exit hwicap_module_cleanup(void)
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100878{
Stephen Neuendorfferac646732008-03-18 04:36:32 +1100879 dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
Stephen Neuendorfferef141a02008-02-06 04:24:09 +1100880
881 class_destroy(icap_class);
882
883 platform_driver_unregister(&hwicap_platform_driver);
884
885 hwicap_of_unregister();
886
887 unregister_chrdev_region(devt, HWICAP_DEVICES);
888}
889
890module_init(hwicap_module_init);
891module_exit(hwicap_module_cleanup);
892
893MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
894MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
895MODULE_LICENSE("GPL");