1 /***************************************************************************** 2 * 3 * Author: Xilinx, Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License as published by the 7 * Free Software Foundation; either version 2 of the License, or (at your 8 * option) any later version. 9 * 10 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" 11 * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND 12 * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE, 13 * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, 14 * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION 15 * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT, 16 * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE 17 * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY 18 * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE 19 * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR 20 * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF 21 * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE. 23 * 24 * Xilinx products are not intended for use in life support appliances, 25 * devices, or systems. Use in such applications is expressly prohibited. 26 * 27 * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group 28 * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group 29 * (c) Copyright 2007-2008 Xilinx Inc. 30 * All rights reserved. 31 * 32 * You should have received a copy of the GNU General Public License along 33 * with this program; if not, write to the Free Software Foundation, Inc., 34 * 675 Mass Ave, Cambridge, MA 02139, USA. 35 * 36 *****************************************************************************/ 37 38 /* 39 * This is the code behind /dev/xilinx_icap -- it allows a user-space 40 * application to use the Xilinx ICAP subsystem. 41 * 42 * The following operations are possible: 43 * 44 * open open the port and initialize for access. 45 * release release port 46 * write Write a bitstream to the configuration processor. 47 * read Read a data stream from the configuration processor. 48 * 49 * After being opened, the port is initialized and accessed to avoid a 50 * corrupted first read which may occur with some hardware. The port 51 * is left in a desynched state, requiring that a synch sequence be 52 * transmitted before any valid configuration data. A user will have 53 * exclusive access to the device while it remains open, and the state 54 * of the ICAP cannot be guaranteed after the device is closed. Note 55 * that a complete reset of the core and the state of the ICAP cannot 56 * be performed on many versions of the cores, hence users of this 57 * device should avoid making inconsistent accesses to the device. In 58 * particular, accessing the read interface, without first generating 59 * a write containing a readback packet can leave the ICAP in an 60 * inaccessible state. 61 * 62 * Note that in order to use the read interface, it is first necessary 63 * to write a request packet to the write interface. i.e., it is not 64 * possible to simply readback the bitstream (or any configuration 65 * bits) from a device without specifically requesting them first. 66 * The code to craft such packets is intended to be part of the 67 * user-space application code that uses this device. The simplest 68 * way to use this interface is simply: 69 * 70 * cp foo.bit /dev/xilinx_icap 71 * 72 * Note that unless foo.bit is an appropriately constructed partial 73 * bitstream, this has a high likelyhood of overwriting the design 74 * currently programmed in the FPGA. 75 */ 76 77 #include <linux/version.h> 78 #include <linux/module.h> 79 #include <linux/kernel.h> 80 #include <linux/types.h> 81 #include <linux/ioport.h> 82 #include <linux/interrupt.h> 83 #include <linux/fcntl.h> 84 #include <linux/init.h> 85 #include <linux/poll.h> 86 #include <linux/proc_fs.h> 87 #include <linux/mutex.h> 88 #include <linux/sysctl.h> 89 #include <linux/version.h> 90 #include <linux/fs.h> 91 #include <linux/cdev.h> 92 #include <linux/platform_device.h> 93 94 #include <asm/io.h> 95 #include <asm/uaccess.h> 96 #include <asm/system.h> 97 98 #ifdef CONFIG_OF 99 /* For open firmware. */ 100 #include <linux/of_device.h> 101 #include <linux/of_platform.h> 102 #endif 103 104 #include "xilinx_hwicap.h" 105 #include "buffer_icap.h" 106 #include "fifo_icap.h" 107 108 #define DRIVER_NAME "xilinx_icap" 109 110 #define HWICAP_REGS (0x10000) 111 112 /* dynamically allocate device number */ 113 static int xhwicap_major; 114 static int xhwicap_minor; 115 #define HWICAP_DEVICES 1 116 117 module_param(xhwicap_major, int, S_IRUGO); 118 module_param(xhwicap_minor, int, S_IRUGO); 119 120 /* An array, which is set to true when the device is registered. */ 121 static bool probed_devices[HWICAP_DEVICES]; 122 static struct mutex icap_sem; 123 124 static struct class *icap_class; 125 126 #define UNIMPLEMENTED 0xFFFF 127 128 static const struct config_registers v2_config_registers = { 129 .CRC = 0, 130 .FAR = 1, 131 .FDRI = 2, 132 .FDRO = 3, 133 .CMD = 4, 134 .CTL = 5, 135 .MASK = 6, 136 .STAT = 7, 137 .LOUT = 8, 138 .COR = 9, 139 .MFWR = 10, 140 .FLR = 11, 141 .KEY = 12, 142 .CBC = 13, 143 .IDCODE = 14, 144 .AXSS = UNIMPLEMENTED, 145 .C0R_1 = UNIMPLEMENTED, 146 .CSOB = UNIMPLEMENTED, 147 .WBSTAR = UNIMPLEMENTED, 148 .TIMER = UNIMPLEMENTED, 149 .BOOTSTS = UNIMPLEMENTED, 150 .CTL_1 = UNIMPLEMENTED, 151 }; 152 153 static const struct config_registers v4_config_registers = { 154 .CRC = 0, 155 .FAR = 1, 156 .FDRI = 2, 157 .FDRO = 3, 158 .CMD = 4, 159 .CTL = 5, 160 .MASK = 6, 161 .STAT = 7, 162 .LOUT = 8, 163 .COR = 9, 164 .MFWR = 10, 165 .FLR = UNIMPLEMENTED, 166 .KEY = UNIMPLEMENTED, 167 .CBC = 11, 168 .IDCODE = 12, 169 .AXSS = 13, 170 .C0R_1 = UNIMPLEMENTED, 171 .CSOB = UNIMPLEMENTED, 172 .WBSTAR = UNIMPLEMENTED, 173 .TIMER = UNIMPLEMENTED, 174 .BOOTSTS = UNIMPLEMENTED, 175 .CTL_1 = UNIMPLEMENTED, 176 }; 177 static const struct config_registers v5_config_registers = { 178 .CRC = 0, 179 .FAR = 1, 180 .FDRI = 2, 181 .FDRO = 3, 182 .CMD = 4, 183 .CTL = 5, 184 .MASK = 6, 185 .STAT = 7, 186 .LOUT = 8, 187 .COR = 9, 188 .MFWR = 10, 189 .FLR = UNIMPLEMENTED, 190 .KEY = UNIMPLEMENTED, 191 .CBC = 11, 192 .IDCODE = 12, 193 .AXSS = 13, 194 .C0R_1 = 14, 195 .CSOB = 15, 196 .WBSTAR = 16, 197 .TIMER = 17, 198 .BOOTSTS = 18, 199 .CTL_1 = 19, 200 }; 201 202 /** 203 * hwicap_command_desync - Send a DESYNC command to the ICAP port. 204 * @drvdata: a pointer to the drvdata. 205 * 206 * This command desynchronizes the ICAP After this command, a 207 * bitstream containing a NULL packet, followed by a SYNCH packet is 208 * required before the ICAP will recognize commands. 209 */ 210 static int hwicap_command_desync(struct hwicap_drvdata *drvdata) 211 { 212 u32 buffer[4]; 213 u32 index = 0; 214 215 /* 216 * Create the data to be written to the ICAP. 217 */ 218 buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1; 219 buffer[index++] = XHI_CMD_DESYNCH; 220 buffer[index++] = XHI_NOOP_PACKET; 221 buffer[index++] = XHI_NOOP_PACKET; 222 223 /* 224 * Write the data to the FIFO and intiate the transfer of data present 225 * in the FIFO to the ICAP device. 226 */ 227 return drvdata->config->set_configuration(drvdata, 228 &buffer[0], index); 229 } 230 231 /** 232 * hwicap_get_configuration_register - Query a configuration register. 233 * @drvdata: a pointer to the drvdata. 234 * @reg: a constant which represents the configuration 235 * register value to be returned. 236 * Examples: XHI_IDCODE, XHI_FLR. 237 * @reg_data: returns the value of the register. 238 * 239 * Sends a query packet to the ICAP and then receives the response. 240 * The icap is left in Synched state. 241 */ 242 static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata, 243 u32 reg, u32 *reg_data) 244 { 245 int status; 246 u32 buffer[6]; 247 u32 index = 0; 248 249 /* 250 * Create the data to be written to the ICAP. 251 */ 252 buffer[index++] = XHI_DUMMY_PACKET; 253 buffer[index++] = XHI_SYNC_PACKET; 254 buffer[index++] = XHI_NOOP_PACKET; 255 buffer[index++] = hwicap_type_1_read(reg) | 1; 256 buffer[index++] = XHI_NOOP_PACKET; 257 buffer[index++] = XHI_NOOP_PACKET; 258 259 /* 260 * Write the data to the FIFO and intiate the transfer of data present 261 * in the FIFO to the ICAP device. 262 */ 263 status = drvdata->config->set_configuration(drvdata, 264 &buffer[0], index); 265 if (status) 266 return status; 267 268 /* 269 * Read the configuration register 270 */ 271 status = drvdata->config->get_configuration(drvdata, reg_data, 1); 272 if (status) 273 return status; 274 275 return 0; 276 } 277 278 static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata) 279 { 280 int status; 281 u32 idcode; 282 283 dev_dbg(drvdata->dev, "initializing\n"); 284 285 /* Abort any current transaction, to make sure we have the 286 * ICAP in a good state. */ 287 dev_dbg(drvdata->dev, "Reset...\n"); 288 drvdata->config->reset(drvdata); 289 290 dev_dbg(drvdata->dev, "Desync...\n"); 291 status = hwicap_command_desync(drvdata); 292 if (status) 293 return status; 294 295 /* Attempt to read the IDCODE from ICAP. This 296 * may not be returned correctly, due to the design of the 297 * hardware. 298 */ 299 dev_dbg(drvdata->dev, "Reading IDCODE...\n"); 300 status = hwicap_get_configuration_register( 301 drvdata, drvdata->config_regs->IDCODE, &idcode); 302 dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode); 303 if (status) 304 return status; 305 306 dev_dbg(drvdata->dev, "Desync...\n"); 307 status = hwicap_command_desync(drvdata); 308 if (status) 309 return status; 310 311 return 0; 312 } 313 314 static ssize_t 315 hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 316 { 317 struct hwicap_drvdata *drvdata = file->private_data; 318 ssize_t bytes_to_read = 0; 319 u32 *kbuf; 320 u32 words; 321 u32 bytes_remaining; 322 int status; 323 324 status = mutex_lock_interruptible(&drvdata->sem); 325 if (status) 326 return status; 327 328 if (drvdata->read_buffer_in_use) { 329 /* If there are leftover bytes in the buffer, just */ 330 /* return them and don't try to read more from the */ 331 /* ICAP device. */ 332 bytes_to_read = 333 (count < drvdata->read_buffer_in_use) ? count : 334 drvdata->read_buffer_in_use; 335 336 /* Return the data currently in the read buffer. */ 337 if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) { 338 status = -EFAULT; 339 goto error; 340 } 341 drvdata->read_buffer_in_use -= bytes_to_read; 342 memmove(drvdata->read_buffer, 343 drvdata->read_buffer + bytes_to_read, 344 4 - bytes_to_read); 345 } else { 346 /* Get new data from the ICAP, and return was was requested. */ 347 kbuf = (u32 *) get_zeroed_page(GFP_KERNEL); 348 if (!kbuf) { 349 status = -ENOMEM; 350 goto error; 351 } 352 353 /* The ICAP device is only able to read complete */ 354 /* words. If a number of bytes that do not correspond */ 355 /* to complete words is requested, then we read enough */ 356 /* words to get the required number of bytes, and then */ 357 /* save the remaining bytes for the next read. */ 358 359 /* Determine the number of words to read, rounding up */ 360 /* if necessary. */ 361 words = ((count + 3) >> 2); 362 bytes_to_read = words << 2; 363 364 if (bytes_to_read > PAGE_SIZE) 365 bytes_to_read = PAGE_SIZE; 366 367 /* Ensure we only read a complete number of words. */ 368 bytes_remaining = bytes_to_read & 3; 369 bytes_to_read &= ~3; 370 words = bytes_to_read >> 2; 371 372 status = drvdata->config->get_configuration(drvdata, 373 kbuf, words); 374 375 /* If we didn't read correctly, then bail out. */ 376 if (status) { 377 free_page((unsigned long)kbuf); 378 goto error; 379 } 380 381 /* If we fail to return the data to the user, then bail out. */ 382 if (copy_to_user(buf, kbuf, bytes_to_read)) { 383 free_page((unsigned long)kbuf); 384 status = -EFAULT; 385 goto error; 386 } 387 memcpy(drvdata->read_buffer, 388 kbuf, 389 bytes_remaining); 390 drvdata->read_buffer_in_use = bytes_remaining; 391 free_page((unsigned long)kbuf); 392 } 393 status = bytes_to_read; 394 error: 395 mutex_unlock(&drvdata->sem); 396 return status; 397 } 398 399 static ssize_t 400 hwicap_write(struct file *file, const char __user *buf, 401 size_t count, loff_t *ppos) 402 { 403 struct hwicap_drvdata *drvdata = file->private_data; 404 ssize_t written = 0; 405 ssize_t left = count; 406 u32 *kbuf; 407 ssize_t len; 408 ssize_t status; 409 410 status = mutex_lock_interruptible(&drvdata->sem); 411 if (status) 412 return status; 413 414 left += drvdata->write_buffer_in_use; 415 416 /* Only write multiples of 4 bytes. */ 417 if (left < 4) { 418 status = 0; 419 goto error; 420 } 421 422 kbuf = (u32 *) __get_free_page(GFP_KERNEL); 423 if (!kbuf) { 424 status = -ENOMEM; 425 goto error; 426 } 427 428 while (left > 3) { 429 /* only write multiples of 4 bytes, so there might */ 430 /* be as many as 3 bytes left (at the end). */ 431 len = left; 432 433 if (len > PAGE_SIZE) 434 len = PAGE_SIZE; 435 len &= ~3; 436 437 if (drvdata->write_buffer_in_use) { 438 memcpy(kbuf, drvdata->write_buffer, 439 drvdata->write_buffer_in_use); 440 if (copy_from_user( 441 (((char *)kbuf) + drvdata->write_buffer_in_use), 442 buf + written, 443 len - (drvdata->write_buffer_in_use))) { 444 free_page((unsigned long)kbuf); 445 status = -EFAULT; 446 goto error; 447 } 448 } else { 449 if (copy_from_user(kbuf, buf + written, len)) { 450 free_page((unsigned long)kbuf); 451 status = -EFAULT; 452 goto error; 453 } 454 } 455 456 status = drvdata->config->set_configuration(drvdata, 457 kbuf, len >> 2); 458 459 if (status) { 460 free_page((unsigned long)kbuf); 461 status = -EFAULT; 462 goto error; 463 } 464 if (drvdata->write_buffer_in_use) { 465 len -= drvdata->write_buffer_in_use; 466 left -= drvdata->write_buffer_in_use; 467 drvdata->write_buffer_in_use = 0; 468 } 469 written += len; 470 left -= len; 471 } 472 if ((left > 0) && (left < 4)) { 473 if (!copy_from_user(drvdata->write_buffer, 474 buf + written, left)) { 475 drvdata->write_buffer_in_use = left; 476 written += left; 477 left = 0; 478 } 479 } 480 481 free_page((unsigned long)kbuf); 482 status = written; 483 error: 484 mutex_unlock(&drvdata->sem); 485 return status; 486 } 487 488 static int hwicap_open(struct inode *inode, struct file *file) 489 { 490 struct hwicap_drvdata *drvdata; 491 int status; 492 493 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev); 494 495 status = mutex_lock_interruptible(&drvdata->sem); 496 if (status) 497 return status; 498 499 if (drvdata->is_open) { 500 status = -EBUSY; 501 goto error; 502 } 503 504 status = hwicap_initialize_hwicap(drvdata); 505 if (status) { 506 dev_err(drvdata->dev, "Failed to open file"); 507 goto error; 508 } 509 510 file->private_data = drvdata; 511 drvdata->write_buffer_in_use = 0; 512 drvdata->read_buffer_in_use = 0; 513 drvdata->is_open = 1; 514 515 error: 516 mutex_unlock(&drvdata->sem); 517 return status; 518 } 519 520 static int hwicap_release(struct inode *inode, struct file *file) 521 { 522 struct hwicap_drvdata *drvdata = file->private_data; 523 int i; 524 int status = 0; 525 526 mutex_lock(&drvdata->sem); 527 528 if (drvdata->write_buffer_in_use) { 529 /* Flush write buffer. */ 530 for (i = drvdata->write_buffer_in_use; i < 4; i++) 531 drvdata->write_buffer[i] = 0; 532 533 status = drvdata->config->set_configuration(drvdata, 534 (u32 *) drvdata->write_buffer, 1); 535 if (status) 536 goto error; 537 } 538 539 status = hwicap_command_desync(drvdata); 540 if (status) 541 goto error; 542 543 error: 544 drvdata->is_open = 0; 545 mutex_unlock(&drvdata->sem); 546 return status; 547 } 548 549 static struct file_operations hwicap_fops = { 550 .owner = THIS_MODULE, 551 .write = hwicap_write, 552 .read = hwicap_read, 553 .open = hwicap_open, 554 .release = hwicap_release, 555 }; 556 557 static int __devinit hwicap_setup(struct device *dev, int id, 558 const struct resource *regs_res, 559 const struct hwicap_driver_config *config, 560 const struct config_registers *config_regs) 561 { 562 dev_t devt; 563 struct hwicap_drvdata *drvdata = NULL; 564 int retval = 0; 565 566 dev_info(dev, "Xilinx icap port driver\n"); 567 568 mutex_lock(&icap_sem); 569 570 if (id < 0) { 571 for (id = 0; id < HWICAP_DEVICES; id++) 572 if (!probed_devices[id]) 573 break; 574 } 575 if (id < 0 || id >= HWICAP_DEVICES) { 576 mutex_unlock(&icap_sem); 577 dev_err(dev, "%s%i too large\n", DRIVER_NAME, id); 578 return -EINVAL; 579 } 580 if (probed_devices[id]) { 581 mutex_unlock(&icap_sem); 582 dev_err(dev, "cannot assign to %s%i; it is already in use\n", 583 DRIVER_NAME, id); 584 return -EBUSY; 585 } 586 587 probed_devices[id] = 1; 588 mutex_unlock(&icap_sem); 589 590 devt = MKDEV(xhwicap_major, xhwicap_minor + id); 591 592 drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL); 593 if (!drvdata) { 594 dev_err(dev, "Couldn't allocate device private record\n"); 595 retval = -ENOMEM; 596 goto failed0; 597 } 598 dev_set_drvdata(dev, (void *)drvdata); 599 600 if (!regs_res) { 601 dev_err(dev, "Couldn't get registers resource\n"); 602 retval = -EFAULT; 603 goto failed1; 604 } 605 606 drvdata->mem_start = regs_res->start; 607 drvdata->mem_end = regs_res->end; 608 drvdata->mem_size = regs_res->end - regs_res->start + 1; 609 610 if (!request_mem_region(drvdata->mem_start, 611 drvdata->mem_size, DRIVER_NAME)) { 612 dev_err(dev, "Couldn't lock memory region at %p\n", 613 (void *)regs_res->start); 614 retval = -EBUSY; 615 goto failed1; 616 } 617 618 drvdata->devt = devt; 619 drvdata->dev = dev; 620 drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size); 621 if (!drvdata->base_address) { 622 dev_err(dev, "ioremap() failed\n"); 623 goto failed2; 624 } 625 626 drvdata->config = config; 627 drvdata->config_regs = config_regs; 628 629 mutex_init(&drvdata->sem); 630 drvdata->is_open = 0; 631 632 dev_info(dev, "ioremap %lx to %p with size %x\n", 633 (unsigned long int)drvdata->mem_start, 634 drvdata->base_address, drvdata->mem_size); 635 636 cdev_init(&drvdata->cdev, &hwicap_fops); 637 drvdata->cdev.owner = THIS_MODULE; 638 retval = cdev_add(&drvdata->cdev, devt, 1); 639 if (retval) { 640 dev_err(dev, "cdev_add() failed\n"); 641 goto failed3; 642 } 643 /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */ 644 device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id); 645 return 0; /* success */ 646 647 failed3: 648 iounmap(drvdata->base_address); 649 650 failed2: 651 release_mem_region(regs_res->start, drvdata->mem_size); 652 653 failed1: 654 kfree(drvdata); 655 656 failed0: 657 mutex_lock(&icap_sem); 658 probed_devices[id] = 0; 659 mutex_unlock(&icap_sem); 660 661 return retval; 662 } 663 664 static struct hwicap_driver_config buffer_icap_config = { 665 .get_configuration = buffer_icap_get_configuration, 666 .set_configuration = buffer_icap_set_configuration, 667 .reset = buffer_icap_reset, 668 }; 669 670 static struct hwicap_driver_config fifo_icap_config = { 671 .get_configuration = fifo_icap_get_configuration, 672 .set_configuration = fifo_icap_set_configuration, 673 .reset = fifo_icap_reset, 674 }; 675 676 static int __devexit hwicap_remove(struct device *dev) 677 { 678 struct hwicap_drvdata *drvdata; 679 680 drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev); 681 682 if (!drvdata) 683 return 0; 684 685 device_destroy(icap_class, drvdata->devt); 686 cdev_del(&drvdata->cdev); 687 iounmap(drvdata->base_address); 688 release_mem_region(drvdata->mem_start, drvdata->mem_size); 689 kfree(drvdata); 690 dev_set_drvdata(dev, NULL); 691 692 mutex_lock(&icap_sem); 693 probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0; 694 mutex_unlock(&icap_sem); 695 return 0; /* success */ 696 } 697 698 static int __devinit hwicap_drv_probe(struct platform_device *pdev) 699 { 700 struct resource *res; 701 const struct config_registers *regs; 702 const char *family; 703 704 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 705 if (!res) 706 return -ENODEV; 707 708 /* It's most likely that we're using V4, if the family is not 709 specified */ 710 regs = &v4_config_registers; 711 family = pdev->dev.platform_data; 712 713 if (family) { 714 if (!strcmp(family, "virtex2p")) { 715 regs = &v2_config_registers; 716 } else if (!strcmp(family, "virtex4")) { 717 regs = &v4_config_registers; 718 } else if (!strcmp(family, "virtex5")) { 719 regs = &v5_config_registers; 720 } 721 } 722 723 return hwicap_setup(&pdev->dev, pdev->id, res, 724 &buffer_icap_config, regs); 725 } 726 727 static int __devexit hwicap_drv_remove(struct platform_device *pdev) 728 { 729 return hwicap_remove(&pdev->dev); 730 } 731 732 static struct platform_driver hwicap_platform_driver = { 733 .probe = hwicap_drv_probe, 734 .remove = hwicap_drv_remove, 735 .driver = { 736 .owner = THIS_MODULE, 737 .name = DRIVER_NAME, 738 }, 739 }; 740 741 /* --------------------------------------------------------------------- 742 * OF bus binding 743 */ 744 745 #if defined(CONFIG_OF) 746 static int __devinit 747 hwicap_of_probe(struct of_device *op, const struct of_device_id *match) 748 { 749 struct resource res; 750 const unsigned int *id; 751 const char *family; 752 int rc; 753 const struct hwicap_driver_config *config = match->data; 754 const struct config_registers *regs; 755 756 dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match); 757 758 rc = of_address_to_resource(op->node, 0, &res); 759 if (rc) { 760 dev_err(&op->dev, "invalid address\n"); 761 return rc; 762 } 763 764 id = of_get_property(op->node, "port-number", NULL); 765 766 /* It's most likely that we're using V4, if the family is not 767 specified */ 768 regs = &v4_config_registers; 769 family = of_get_property(op->node, "xlnx,family", NULL); 770 771 if (family) { 772 if (!strcmp(family, "virtex2p")) { 773 regs = &v2_config_registers; 774 } else if (!strcmp(family, "virtex4")) { 775 regs = &v4_config_registers; 776 } else if (!strcmp(family, "virtex5")) { 777 regs = &v5_config_registers; 778 } 779 } 780 return hwicap_setup(&op->dev, id ? *id : -1, &res, config, 781 regs); 782 } 783 784 static int __devexit hwicap_of_remove(struct of_device *op) 785 { 786 return hwicap_remove(&op->dev); 787 } 788 789 /* Match table for of_platform binding */ 790 static const struct of_device_id __devinit hwicap_of_match[] = { 791 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config}, 792 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config}, 793 {}, 794 }; 795 MODULE_DEVICE_TABLE(of, hwicap_of_match); 796 797 static struct of_platform_driver hwicap_of_driver = { 798 .owner = THIS_MODULE, 799 .name = DRIVER_NAME, 800 .match_table = hwicap_of_match, 801 .probe = hwicap_of_probe, 802 .remove = __devexit_p(hwicap_of_remove), 803 .driver = { 804 .name = DRIVER_NAME, 805 }, 806 }; 807 808 /* Registration helpers to keep the number of #ifdefs to a minimum */ 809 static inline int __init hwicap_of_register(void) 810 { 811 pr_debug("hwicap: calling of_register_platform_driver()\n"); 812 return of_register_platform_driver(&hwicap_of_driver); 813 } 814 815 static inline void __exit hwicap_of_unregister(void) 816 { 817 of_unregister_platform_driver(&hwicap_of_driver); 818 } 819 #else /* CONFIG_OF */ 820 /* CONFIG_OF not enabled; do nothing helpers */ 821 static inline int __init hwicap_of_register(void) { return 0; } 822 static inline void __exit hwicap_of_unregister(void) { } 823 #endif /* CONFIG_OF */ 824 825 static int __init hwicap_module_init(void) 826 { 827 dev_t devt; 828 int retval; 829 830 icap_class = class_create(THIS_MODULE, "xilinx_config"); 831 mutex_init(&icap_sem); 832 833 if (xhwicap_major) { 834 devt = MKDEV(xhwicap_major, xhwicap_minor); 835 retval = register_chrdev_region( 836 devt, 837 HWICAP_DEVICES, 838 DRIVER_NAME); 839 if (retval < 0) 840 return retval; 841 } else { 842 retval = alloc_chrdev_region(&devt, 843 xhwicap_minor, 844 HWICAP_DEVICES, 845 DRIVER_NAME); 846 if (retval < 0) 847 return retval; 848 xhwicap_major = MAJOR(devt); 849 } 850 851 retval = platform_driver_register(&hwicap_platform_driver); 852 853 if (retval) 854 goto failed1; 855 856 retval = hwicap_of_register(); 857 858 if (retval) 859 goto failed2; 860 861 return retval; 862 863 failed2: 864 platform_driver_unregister(&hwicap_platform_driver); 865 866 failed1: 867 unregister_chrdev_region(devt, HWICAP_DEVICES); 868 869 return retval; 870 } 871 872 static void __exit hwicap_module_cleanup(void) 873 { 874 dev_t devt = MKDEV(xhwicap_major, xhwicap_minor); 875 876 class_destroy(icap_class); 877 878 platform_driver_unregister(&hwicap_platform_driver); 879 880 hwicap_of_unregister(); 881 882 unregister_chrdev_region(devt, HWICAP_DEVICES); 883 } 884 885 module_init(hwicap_module_init); 886 module_exit(hwicap_module_cleanup); 887 888 MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group"); 889 MODULE_DESCRIPTION("Xilinx ICAP Port Driver"); 890 MODULE_LICENSE("GPL"); 891