| .. | .. |
|---|
| 16 | 16 | |
|---|
| 17 | 17 | #include <linux/kernel.h> |
|---|
| 18 | 18 | #include <linux/wait.h> |
|---|
| 19 | | -#include <linux/spinlock_types.h> |
|---|
| 19 | +#include <linux/mutex.h> |
|---|
| 20 | 20 | #include <linux/device.h> |
|---|
| 21 | 21 | #include <linux/cdev.h> |
|---|
| 22 | 22 | #include <linux/init.h> |
|---|
| .. | .. |
|---|
| 27 | 27 | #include <linux/interrupt.h> |
|---|
| 28 | 28 | #include <linux/param.h> |
|---|
| 29 | 29 | #include <linux/fs.h> |
|---|
| 30 | | -#include <linux/device.h> |
|---|
| 31 | | -#include <linux/cdev.h> |
|---|
| 32 | 30 | #include <linux/types.h> |
|---|
| 33 | 31 | #include <linux/uaccess.h> |
|---|
| 34 | 32 | #include <linux/jiffies.h> |
|---|
| .. | .. |
|---|
| 127 | 125 | |
|---|
| 128 | 126 | struct axis_fifo { |
|---|
| 129 | 127 | int irq; /* interrupt */ |
|---|
| 130 | | - struct resource *mem; /* physical memory */ |
|---|
| 131 | 128 | void __iomem *base_addr; /* kernel space memory */ |
|---|
| 132 | 129 | |
|---|
| 133 | 130 | unsigned int rx_fifo_depth; /* max words in the receive fifo */ |
|---|
| .. | .. |
|---|
| 136 | 133 | int has_tx_fifo; /* whether the IP has the tx fifo enabled */ |
|---|
| 137 | 134 | |
|---|
| 138 | 135 | wait_queue_head_t read_queue; /* wait queue for asynchronos read */ |
|---|
| 139 | | - spinlock_t read_queue_lock; /* lock for reading waitqueue */ |
|---|
| 136 | + struct mutex read_lock; /* lock for reading */ |
|---|
| 140 | 137 | wait_queue_head_t write_queue; /* wait queue for asynchronos write */ |
|---|
| 141 | | - spinlock_t write_queue_lock; /* lock for writing waitqueue */ |
|---|
| 138 | + struct mutex write_lock; /* lock for writing */ |
|---|
| 142 | 139 | unsigned int write_flags; /* write file flags */ |
|---|
| 143 | 140 | unsigned int read_flags; /* read file flags */ |
|---|
| 144 | 141 | |
|---|
| .. | .. |
|---|
| 339 | 336 | iowrite32(XLLF_INT_ALL_MASK, fifo->base_addr + XLLF_ISR_OFFSET); |
|---|
| 340 | 337 | } |
|---|
| 341 | 338 | |
|---|
| 342 | | -/* reads a single packet from the fifo as dictated by the tlast signal */ |
|---|
| 339 | +/** |
|---|
| 340 | + * axis_fifo_write() - Read a packet from AXIS-FIFO character device. |
|---|
| 341 | + * @f Open file. |
|---|
| 342 | + * @buf User space buffer to read to. |
|---|
| 343 | + * @len User space buffer length. |
|---|
| 344 | + * @off Buffer offset. |
|---|
| 345 | + * |
|---|
| 346 | + * As defined by the device's documentation, we need to check the device's |
|---|
| 347 | + * occupancy before reading the length register and then the data. All these |
|---|
| 348 | + * operations must be executed atomically, in order and one after the other |
|---|
| 349 | + * without missing any. |
|---|
| 350 | + * |
|---|
| 351 | + * Returns the number of bytes read from the device or negative error code |
|---|
| 352 | + * on failure. |
|---|
| 353 | + */ |
|---|
| 343 | 354 | static ssize_t axis_fifo_read(struct file *f, char __user *buf, |
|---|
| 344 | 355 | size_t len, loff_t *off) |
|---|
| 345 | 356 | { |
|---|
| .. | .. |
|---|
| 353 | 364 | u32 tmp_buf[READ_BUF_SIZE]; |
|---|
| 354 | 365 | |
|---|
| 355 | 366 | if (fifo->read_flags & O_NONBLOCK) { |
|---|
| 356 | | - /* opened in non-blocking mode |
|---|
| 357 | | - * return if there are no packets available |
|---|
| 367 | + /* |
|---|
| 368 | + * Device opened in non-blocking mode. Try to lock it and then |
|---|
| 369 | + * check if any packet is available. |
|---|
| 358 | 370 | */ |
|---|
| 359 | | - if (!ioread32(fifo->base_addr + XLLF_RDFO_OFFSET)) |
|---|
| 371 | + if (!mutex_trylock(&fifo->read_lock)) |
|---|
| 360 | 372 | return -EAGAIN; |
|---|
| 373 | + |
|---|
| 374 | + if (!ioread32(fifo->base_addr + XLLF_RDFO_OFFSET)) { |
|---|
| 375 | + ret = -EAGAIN; |
|---|
| 376 | + goto end_unlock; |
|---|
| 377 | + } |
|---|
| 361 | 378 | } else { |
|---|
| 362 | 379 | /* opened in blocking mode |
|---|
| 363 | 380 | * wait for a packet available interrupt (or timeout) |
|---|
| 364 | 381 | * if nothing is currently available |
|---|
| 365 | 382 | */ |
|---|
| 366 | | - spin_lock_irq(&fifo->read_queue_lock); |
|---|
| 367 | | - ret = wait_event_interruptible_lock_irq_timeout( |
|---|
| 368 | | - fifo->read_queue, |
|---|
| 383 | + mutex_lock(&fifo->read_lock); |
|---|
| 384 | + ret = wait_event_interruptible_timeout(fifo->read_queue, |
|---|
| 369 | 385 | ioread32(fifo->base_addr + XLLF_RDFO_OFFSET), |
|---|
| 370 | | - fifo->read_queue_lock, |
|---|
| 371 | | - (read_timeout >= 0) ? msecs_to_jiffies(read_timeout) : |
|---|
| 372 | | - MAX_SCHEDULE_TIMEOUT); |
|---|
| 373 | | - spin_unlock_irq(&fifo->read_queue_lock); |
|---|
| 386 | + (read_timeout >= 0) ? |
|---|
| 387 | + msecs_to_jiffies(read_timeout) : |
|---|
| 388 | + MAX_SCHEDULE_TIMEOUT); |
|---|
| 374 | 389 | |
|---|
| 375 | | - if (ret == 0) { |
|---|
| 376 | | - /* timeout occurred */ |
|---|
| 377 | | - dev_dbg(fifo->dt_device, "read timeout"); |
|---|
| 378 | | - return -EAGAIN; |
|---|
| 379 | | - } else if (ret == -ERESTARTSYS) { |
|---|
| 380 | | - /* signal received */ |
|---|
| 381 | | - return -ERESTARTSYS; |
|---|
| 382 | | - } else if (ret < 0) { |
|---|
| 383 | | - dev_err(fifo->dt_device, "wait_event_interruptible_timeout() error in read (ret=%i)\n", |
|---|
| 384 | | - ret); |
|---|
| 385 | | - return ret; |
|---|
| 390 | + if (ret <= 0) { |
|---|
| 391 | + if (ret == 0) { |
|---|
| 392 | + ret = -EAGAIN; |
|---|
| 393 | + } else if (ret != -ERESTARTSYS) { |
|---|
| 394 | + dev_err(fifo->dt_device, "wait_event_interruptible_timeout() error in read (ret=%i)\n", |
|---|
| 395 | + ret); |
|---|
| 396 | + } |
|---|
| 397 | + |
|---|
| 398 | + goto end_unlock; |
|---|
| 386 | 399 | } |
|---|
| 387 | 400 | } |
|---|
| 388 | 401 | |
|---|
| .. | .. |
|---|
| 390 | 403 | if (!bytes_available) { |
|---|
| 391 | 404 | dev_err(fifo->dt_device, "received a packet of length 0 - fifo core will be reset\n"); |
|---|
| 392 | 405 | reset_ip_core(fifo); |
|---|
| 393 | | - return -EIO; |
|---|
| 406 | + ret = -EIO; |
|---|
| 407 | + goto end_unlock; |
|---|
| 394 | 408 | } |
|---|
| 395 | 409 | |
|---|
| 396 | 410 | if (bytes_available > len) { |
|---|
| 397 | 411 | dev_err(fifo->dt_device, "user read buffer too small (available bytes=%zu user buffer bytes=%zu) - fifo core will be reset\n", |
|---|
| 398 | 412 | bytes_available, len); |
|---|
| 399 | 413 | reset_ip_core(fifo); |
|---|
| 400 | | - return -EINVAL; |
|---|
| 414 | + ret = -EINVAL; |
|---|
| 415 | + goto end_unlock; |
|---|
| 401 | 416 | } |
|---|
| 402 | 417 | |
|---|
| 403 | 418 | if (bytes_available % sizeof(u32)) { |
|---|
| .. | .. |
|---|
| 406 | 421 | */ |
|---|
| 407 | 422 | dev_err(fifo->dt_device, "received a packet that isn't word-aligned - fifo core will be reset\n"); |
|---|
| 408 | 423 | reset_ip_core(fifo); |
|---|
| 409 | | - return -EIO; |
|---|
| 424 | + ret = -EIO; |
|---|
| 425 | + goto end_unlock; |
|---|
| 410 | 426 | } |
|---|
| 411 | 427 | |
|---|
| 412 | 428 | words_available = bytes_available / sizeof(u32); |
|---|
| .. | .. |
|---|
| 426 | 442 | if (copy_to_user(buf + copied * sizeof(u32), tmp_buf, |
|---|
| 427 | 443 | copy * sizeof(u32))) { |
|---|
| 428 | 444 | reset_ip_core(fifo); |
|---|
| 429 | | - return -EFAULT; |
|---|
| 445 | + ret = -EFAULT; |
|---|
| 446 | + goto end_unlock; |
|---|
| 430 | 447 | } |
|---|
| 431 | 448 | |
|---|
| 432 | 449 | copied += copy; |
|---|
| 433 | 450 | words_available -= copy; |
|---|
| 434 | 451 | } |
|---|
| 435 | 452 | |
|---|
| 436 | | - return bytes_available; |
|---|
| 453 | + ret = bytes_available; |
|---|
| 454 | + |
|---|
| 455 | +end_unlock: |
|---|
| 456 | + mutex_unlock(&fifo->read_lock); |
|---|
| 457 | + |
|---|
| 458 | + return ret; |
|---|
| 437 | 459 | } |
|---|
| 438 | 460 | |
|---|
| 461 | +/** |
|---|
| 462 | + * axis_fifo_write() - Write buffer to AXIS-FIFO character device. |
|---|
| 463 | + * @f Open file. |
|---|
| 464 | + * @buf User space buffer to write to the device. |
|---|
| 465 | + * @len User space buffer length. |
|---|
| 466 | + * @off Buffer offset. |
|---|
| 467 | + * |
|---|
| 468 | + * As defined by the device's documentation, we need to write to the device's |
|---|
| 469 | + * data buffer then to the device's packet length register atomically. Also, |
|---|
| 470 | + * we need to lock before checking if the device has available space to avoid |
|---|
| 471 | + * any concurrency issue. |
|---|
| 472 | + * |
|---|
| 473 | + * Returns the number of bytes written to the device or negative error code |
|---|
| 474 | + * on failure. |
|---|
| 475 | + */ |
|---|
| 439 | 476 | static ssize_t axis_fifo_write(struct file *f, const char __user *buf, |
|---|
| 440 | 477 | size_t len, loff_t *off) |
|---|
| 441 | 478 | { |
|---|
| .. | .. |
|---|
| 468 | 505 | } |
|---|
| 469 | 506 | |
|---|
| 470 | 507 | if (fifo->write_flags & O_NONBLOCK) { |
|---|
| 471 | | - /* opened in non-blocking mode |
|---|
| 472 | | - * return if there is not enough room available in the fifo |
|---|
| 508 | + /* |
|---|
| 509 | + * Device opened in non-blocking mode. Try to lock it and then |
|---|
| 510 | + * check if there is any room to write the given buffer. |
|---|
| 473 | 511 | */ |
|---|
| 512 | + if (!mutex_trylock(&fifo->write_lock)) |
|---|
| 513 | + return -EAGAIN; |
|---|
| 514 | + |
|---|
| 474 | 515 | if (words_to_write > ioread32(fifo->base_addr + |
|---|
| 475 | 516 | XLLF_TDFV_OFFSET)) { |
|---|
| 476 | | - return -EAGAIN; |
|---|
| 517 | + ret = -EAGAIN; |
|---|
| 518 | + goto end_unlock; |
|---|
| 477 | 519 | } |
|---|
| 478 | 520 | } else { |
|---|
| 479 | 521 | /* opened in blocking mode */ |
|---|
| .. | .. |
|---|
| 481 | 523 | /* wait for an interrupt (or timeout) if there isn't |
|---|
| 482 | 524 | * currently enough room in the fifo |
|---|
| 483 | 525 | */ |
|---|
| 484 | | - spin_lock_irq(&fifo->write_queue_lock); |
|---|
| 485 | | - ret = wait_event_interruptible_lock_irq_timeout( |
|---|
| 486 | | - fifo->write_queue, |
|---|
| 526 | + mutex_lock(&fifo->write_lock); |
|---|
| 527 | + ret = wait_event_interruptible_timeout(fifo->write_queue, |
|---|
| 487 | 528 | ioread32(fifo->base_addr + XLLF_TDFV_OFFSET) |
|---|
| 488 | | - >= words_to_write, |
|---|
| 489 | | - fifo->write_queue_lock, |
|---|
| 490 | | - (write_timeout >= 0) ? msecs_to_jiffies(write_timeout) : |
|---|
| 491 | | - MAX_SCHEDULE_TIMEOUT); |
|---|
| 492 | | - spin_unlock_irq(&fifo->write_queue_lock); |
|---|
| 529 | + >= words_to_write, |
|---|
| 530 | + (write_timeout >= 0) ? |
|---|
| 531 | + msecs_to_jiffies(write_timeout) : |
|---|
| 532 | + MAX_SCHEDULE_TIMEOUT); |
|---|
| 493 | 533 | |
|---|
| 494 | | - if (ret == 0) { |
|---|
| 495 | | - /* timeout occurred */ |
|---|
| 496 | | - dev_dbg(fifo->dt_device, "write timeout\n"); |
|---|
| 497 | | - return -EAGAIN; |
|---|
| 498 | | - } else if (ret == -ERESTARTSYS) { |
|---|
| 499 | | - /* signal received */ |
|---|
| 500 | | - return -ERESTARTSYS; |
|---|
| 501 | | - } else if (ret < 0) { |
|---|
| 502 | | - /* unknown error */ |
|---|
| 503 | | - dev_err(fifo->dt_device, |
|---|
| 504 | | - "wait_event_interruptible_timeout() error in write (ret=%i)\n", |
|---|
| 505 | | - ret); |
|---|
| 506 | | - return ret; |
|---|
| 534 | + if (ret <= 0) { |
|---|
| 535 | + if (ret == 0) { |
|---|
| 536 | + ret = -EAGAIN; |
|---|
| 537 | + } else if (ret != -ERESTARTSYS) { |
|---|
| 538 | + dev_err(fifo->dt_device, "wait_event_interruptible_timeout() error in write (ret=%i)\n", |
|---|
| 539 | + ret); |
|---|
| 540 | + } |
|---|
| 541 | + |
|---|
| 542 | + goto end_unlock; |
|---|
| 507 | 543 | } |
|---|
| 508 | 544 | } |
|---|
| 509 | 545 | |
|---|
| .. | .. |
|---|
| 517 | 553 | if (copy_from_user(tmp_buf, buf + copied * sizeof(u32), |
|---|
| 518 | 554 | copy * sizeof(u32))) { |
|---|
| 519 | 555 | reset_ip_core(fifo); |
|---|
| 520 | | - return -EFAULT; |
|---|
| 556 | + ret = -EFAULT; |
|---|
| 557 | + goto end_unlock; |
|---|
| 521 | 558 | } |
|---|
| 522 | 559 | |
|---|
| 523 | 560 | for (i = 0; i < copy; i++) |
|---|
| .. | .. |
|---|
| 528 | 565 | words_to_write -= copy; |
|---|
| 529 | 566 | } |
|---|
| 530 | 567 | |
|---|
| 531 | | - /* write packet size to fifo */ |
|---|
| 532 | | - iowrite32(copied * sizeof(u32), fifo->base_addr + XLLF_TLR_OFFSET); |
|---|
| 568 | + ret = copied * sizeof(u32); |
|---|
| 533 | 569 | |
|---|
| 534 | | - return (ssize_t)copied * sizeof(u32); |
|---|
| 570 | + /* write packet size to fifo */ |
|---|
| 571 | + iowrite32(ret, fifo->base_addr + XLLF_TLR_OFFSET); |
|---|
| 572 | + |
|---|
| 573 | +end_unlock: |
|---|
| 574 | + mutex_unlock(&fifo->write_lock); |
|---|
| 575 | + |
|---|
| 576 | + return ret; |
|---|
| 535 | 577 | } |
|---|
| 536 | 578 | |
|---|
| 537 | 579 | static irqreturn_t axis_fifo_irq(int irq, void *dw) |
|---|
| .. | .. |
|---|
| 702 | 744 | return 0; |
|---|
| 703 | 745 | } |
|---|
| 704 | 746 | |
|---|
| 747 | +static int axis_fifo_parse_dt(struct axis_fifo *fifo) |
|---|
| 748 | +{ |
|---|
| 749 | + int ret; |
|---|
| 750 | + unsigned int value; |
|---|
| 751 | + |
|---|
| 752 | + ret = get_dts_property(fifo, "xlnx,axi-str-rxd-tdata-width", &value); |
|---|
| 753 | + if (ret) { |
|---|
| 754 | + dev_err(fifo->dt_device, "missing xlnx,axi-str-rxd-tdata-width property\n"); |
|---|
| 755 | + goto end; |
|---|
| 756 | + } else if (value != 32) { |
|---|
| 757 | + dev_err(fifo->dt_device, "xlnx,axi-str-rxd-tdata-width only supports 32 bits\n"); |
|---|
| 758 | + ret = -EIO; |
|---|
| 759 | + goto end; |
|---|
| 760 | + } |
|---|
| 761 | + |
|---|
| 762 | + ret = get_dts_property(fifo, "xlnx,axi-str-txd-tdata-width", &value); |
|---|
| 763 | + if (ret) { |
|---|
| 764 | + dev_err(fifo->dt_device, "missing xlnx,axi-str-txd-tdata-width property\n"); |
|---|
| 765 | + goto end; |
|---|
| 766 | + } else if (value != 32) { |
|---|
| 767 | + dev_err(fifo->dt_device, "xlnx,axi-str-txd-tdata-width only supports 32 bits\n"); |
|---|
| 768 | + ret = -EIO; |
|---|
| 769 | + goto end; |
|---|
| 770 | + } |
|---|
| 771 | + |
|---|
| 772 | + ret = get_dts_property(fifo, "xlnx,rx-fifo-depth", |
|---|
| 773 | + &fifo->rx_fifo_depth); |
|---|
| 774 | + if (ret) { |
|---|
| 775 | + dev_err(fifo->dt_device, "missing xlnx,rx-fifo-depth property\n"); |
|---|
| 776 | + ret = -EIO; |
|---|
| 777 | + goto end; |
|---|
| 778 | + } |
|---|
| 779 | + |
|---|
| 780 | + ret = get_dts_property(fifo, "xlnx,tx-fifo-depth", |
|---|
| 781 | + &fifo->tx_fifo_depth); |
|---|
| 782 | + if (ret) { |
|---|
| 783 | + dev_err(fifo->dt_device, "missing xlnx,tx-fifo-depth property\n"); |
|---|
| 784 | + ret = -EIO; |
|---|
| 785 | + goto end; |
|---|
| 786 | + } |
|---|
| 787 | + |
|---|
| 788 | + /* IP sets TDFV to fifo depth - 4 so we will do the same */ |
|---|
| 789 | + fifo->tx_fifo_depth -= 4; |
|---|
| 790 | + |
|---|
| 791 | + ret = get_dts_property(fifo, "xlnx,use-rx-data", &fifo->has_rx_fifo); |
|---|
| 792 | + if (ret) { |
|---|
| 793 | + dev_err(fifo->dt_device, "missing xlnx,use-rx-data property\n"); |
|---|
| 794 | + ret = -EIO; |
|---|
| 795 | + goto end; |
|---|
| 796 | + } |
|---|
| 797 | + |
|---|
| 798 | + ret = get_dts_property(fifo, "xlnx,use-tx-data", &fifo->has_tx_fifo); |
|---|
| 799 | + if (ret) { |
|---|
| 800 | + dev_err(fifo->dt_device, "missing xlnx,use-tx-data property\n"); |
|---|
| 801 | + ret = -EIO; |
|---|
| 802 | + goto end; |
|---|
| 803 | + } |
|---|
| 804 | + |
|---|
| 805 | +end: |
|---|
| 806 | + return ret; |
|---|
| 807 | +} |
|---|
| 808 | + |
|---|
| 705 | 809 | static int axis_fifo_probe(struct platform_device *pdev) |
|---|
| 706 | 810 | { |
|---|
| 707 | 811 | struct resource *r_irq; /* interrupt resources */ |
|---|
| .. | .. |
|---|
| 712 | 816 | char device_name[32]; |
|---|
| 713 | 817 | |
|---|
| 714 | 818 | int rc = 0; /* error return value */ |
|---|
| 715 | | - |
|---|
| 716 | | - /* IP properties from device tree */ |
|---|
| 717 | | - unsigned int rxd_tdata_width; |
|---|
| 718 | | - unsigned int txc_tdata_width; |
|---|
| 719 | | - unsigned int txd_tdata_width; |
|---|
| 720 | | - unsigned int tdest_width; |
|---|
| 721 | | - unsigned int tid_width; |
|---|
| 722 | | - unsigned int tuser_width; |
|---|
| 723 | | - unsigned int data_interface_type; |
|---|
| 724 | | - unsigned int has_tdest; |
|---|
| 725 | | - unsigned int has_tid; |
|---|
| 726 | | - unsigned int has_tkeep; |
|---|
| 727 | | - unsigned int has_tstrb; |
|---|
| 728 | | - unsigned int has_tuser; |
|---|
| 729 | | - unsigned int rx_fifo_depth; |
|---|
| 730 | | - unsigned int rx_programmable_empty_threshold; |
|---|
| 731 | | - unsigned int rx_programmable_full_threshold; |
|---|
| 732 | | - unsigned int axi_id_width; |
|---|
| 733 | | - unsigned int axi4_data_width; |
|---|
| 734 | | - unsigned int select_xpm; |
|---|
| 735 | | - unsigned int tx_fifo_depth; |
|---|
| 736 | | - unsigned int tx_programmable_empty_threshold; |
|---|
| 737 | | - unsigned int tx_programmable_full_threshold; |
|---|
| 738 | | - unsigned int use_rx_cut_through; |
|---|
| 739 | | - unsigned int use_rx_data; |
|---|
| 740 | | - unsigned int use_tx_control; |
|---|
| 741 | | - unsigned int use_tx_cut_through; |
|---|
| 742 | | - unsigned int use_tx_data; |
|---|
| 743 | 819 | |
|---|
| 744 | 820 | /* ---------------------------- |
|---|
| 745 | 821 | * init wrapper device |
|---|
| .. | .. |
|---|
| 757 | 833 | init_waitqueue_head(&fifo->read_queue); |
|---|
| 758 | 834 | init_waitqueue_head(&fifo->write_queue); |
|---|
| 759 | 835 | |
|---|
| 760 | | - spin_lock_init(&fifo->read_queue_lock); |
|---|
| 761 | | - spin_lock_init(&fifo->write_queue_lock); |
|---|
| 836 | + mutex_init(&fifo->read_lock); |
|---|
| 837 | + mutex_init(&fifo->write_lock); |
|---|
| 762 | 838 | |
|---|
| 763 | 839 | /* ---------------------------- |
|---|
| 764 | 840 | * init device memory space |
|---|
| .. | .. |
|---|
| 773 | 849 | goto err_initial; |
|---|
| 774 | 850 | } |
|---|
| 775 | 851 | |
|---|
| 776 | | - fifo->mem = r_mem; |
|---|
| 777 | | - |
|---|
| 778 | 852 | /* request physical memory */ |
|---|
| 779 | | - if (!request_mem_region(fifo->mem->start, resource_size(fifo->mem), |
|---|
| 780 | | - DRIVER_NAME)) { |
|---|
| 781 | | - dev_err(fifo->dt_device, |
|---|
| 782 | | - "couldn't lock memory region at 0x%pa\n", |
|---|
| 783 | | - &fifo->mem->start); |
|---|
| 784 | | - rc = -EBUSY; |
|---|
| 853 | + fifo->base_addr = devm_ioremap_resource(fifo->dt_device, r_mem); |
|---|
| 854 | + if (IS_ERR(fifo->base_addr)) { |
|---|
| 855 | + rc = PTR_ERR(fifo->base_addr); |
|---|
| 856 | + dev_err(fifo->dt_device, "can't remap IO resource (%d)\n", rc); |
|---|
| 785 | 857 | goto err_initial; |
|---|
| 786 | 858 | } |
|---|
| 787 | | - dev_dbg(fifo->dt_device, "got memory location [0x%pa - 0x%pa]\n", |
|---|
| 788 | | - &fifo->mem->start, &fifo->mem->end); |
|---|
| 789 | 859 | |
|---|
| 790 | | - /* map physical memory to kernel virtual address space */ |
|---|
| 791 | | - fifo->base_addr = ioremap(fifo->mem->start, resource_size(fifo->mem)); |
|---|
| 792 | | - if (!fifo->base_addr) { |
|---|
| 793 | | - dev_err(fifo->dt_device, "couldn't map physical memory\n"); |
|---|
| 794 | | - rc = -ENOMEM; |
|---|
| 795 | | - goto err_mem; |
|---|
| 796 | | - } |
|---|
| 797 | 860 | dev_dbg(fifo->dt_device, "remapped memory to 0x%p\n", fifo->base_addr); |
|---|
| 798 | 861 | |
|---|
| 799 | 862 | /* create unique device name */ |
|---|
| 800 | 863 | snprintf(device_name, sizeof(device_name), "%s_%pa", |
|---|
| 801 | | - DRIVER_NAME, &fifo->mem->start); |
|---|
| 864 | + DRIVER_NAME, &r_mem->start); |
|---|
| 802 | 865 | |
|---|
| 803 | 866 | dev_dbg(fifo->dt_device, "device name [%s]\n", device_name); |
|---|
| 804 | 867 | |
|---|
| .. | .. |
|---|
| 807 | 870 | * ---------------------------- |
|---|
| 808 | 871 | */ |
|---|
| 809 | 872 | |
|---|
| 810 | | - /* retrieve device tree properties */ |
|---|
| 811 | | - rc = get_dts_property(fifo, "xlnx,axi-str-rxd-tdata-width", |
|---|
| 812 | | - &rxd_tdata_width); |
|---|
| 873 | + rc = axis_fifo_parse_dt(fifo); |
|---|
| 813 | 874 | if (rc) |
|---|
| 814 | | - goto err_unmap; |
|---|
| 815 | | - rc = get_dts_property(fifo, "xlnx,axi-str-txc-tdata-width", |
|---|
| 816 | | - &txc_tdata_width); |
|---|
| 817 | | - if (rc) |
|---|
| 818 | | - goto err_unmap; |
|---|
| 819 | | - rc = get_dts_property(fifo, "xlnx,axi-str-txd-tdata-width", |
|---|
| 820 | | - &txd_tdata_width); |
|---|
| 821 | | - if (rc) |
|---|
| 822 | | - goto err_unmap; |
|---|
| 823 | | - rc = get_dts_property(fifo, "xlnx,axis-tdest-width", &tdest_width); |
|---|
| 824 | | - if (rc) |
|---|
| 825 | | - goto err_unmap; |
|---|
| 826 | | - rc = get_dts_property(fifo, "xlnx,axis-tid-width", &tid_width); |
|---|
| 827 | | - if (rc) |
|---|
| 828 | | - goto err_unmap; |
|---|
| 829 | | - rc = get_dts_property(fifo, "xlnx,axis-tuser-width", &tuser_width); |
|---|
| 830 | | - if (rc) |
|---|
| 831 | | - goto err_unmap; |
|---|
| 832 | | - rc = get_dts_property(fifo, "xlnx,data-interface-type", |
|---|
| 833 | | - &data_interface_type); |
|---|
| 834 | | - if (rc) |
|---|
| 835 | | - goto err_unmap; |
|---|
| 836 | | - rc = get_dts_property(fifo, "xlnx,has-axis-tdest", &has_tdest); |
|---|
| 837 | | - if (rc) |
|---|
| 838 | | - goto err_unmap; |
|---|
| 839 | | - rc = get_dts_property(fifo, "xlnx,has-axis-tid", &has_tid); |
|---|
| 840 | | - if (rc) |
|---|
| 841 | | - goto err_unmap; |
|---|
| 842 | | - rc = get_dts_property(fifo, "xlnx,has-axis-tkeep", &has_tkeep); |
|---|
| 843 | | - if (rc) |
|---|
| 844 | | - goto err_unmap; |
|---|
| 845 | | - rc = get_dts_property(fifo, "xlnx,has-axis-tstrb", &has_tstrb); |
|---|
| 846 | | - if (rc) |
|---|
| 847 | | - goto err_unmap; |
|---|
| 848 | | - rc = get_dts_property(fifo, "xlnx,has-axis-tuser", &has_tuser); |
|---|
| 849 | | - if (rc) |
|---|
| 850 | | - goto err_unmap; |
|---|
| 851 | | - rc = get_dts_property(fifo, "xlnx,rx-fifo-depth", &rx_fifo_depth); |
|---|
| 852 | | - if (rc) |
|---|
| 853 | | - goto err_unmap; |
|---|
| 854 | | - rc = get_dts_property(fifo, "xlnx,rx-fifo-pe-threshold", |
|---|
| 855 | | - &rx_programmable_empty_threshold); |
|---|
| 856 | | - if (rc) |
|---|
| 857 | | - goto err_unmap; |
|---|
| 858 | | - rc = get_dts_property(fifo, "xlnx,rx-fifo-pf-threshold", |
|---|
| 859 | | - &rx_programmable_full_threshold); |
|---|
| 860 | | - if (rc) |
|---|
| 861 | | - goto err_unmap; |
|---|
| 862 | | - rc = get_dts_property(fifo, "xlnx,s-axi-id-width", &axi_id_width); |
|---|
| 863 | | - if (rc) |
|---|
| 864 | | - goto err_unmap; |
|---|
| 865 | | - rc = get_dts_property(fifo, "xlnx,s-axi4-data-width", &axi4_data_width); |
|---|
| 866 | | - if (rc) |
|---|
| 867 | | - goto err_unmap; |
|---|
| 868 | | - rc = get_dts_property(fifo, "xlnx,select-xpm", &select_xpm); |
|---|
| 869 | | - if (rc) |
|---|
| 870 | | - goto err_unmap; |
|---|
| 871 | | - rc = get_dts_property(fifo, "xlnx,tx-fifo-depth", &tx_fifo_depth); |
|---|
| 872 | | - if (rc) |
|---|
| 873 | | - goto err_unmap; |
|---|
| 874 | | - rc = get_dts_property(fifo, "xlnx,tx-fifo-pe-threshold", |
|---|
| 875 | | - &tx_programmable_empty_threshold); |
|---|
| 876 | | - if (rc) |
|---|
| 877 | | - goto err_unmap; |
|---|
| 878 | | - rc = get_dts_property(fifo, "xlnx,tx-fifo-pf-threshold", |
|---|
| 879 | | - &tx_programmable_full_threshold); |
|---|
| 880 | | - if (rc) |
|---|
| 881 | | - goto err_unmap; |
|---|
| 882 | | - rc = get_dts_property(fifo, "xlnx,use-rx-cut-through", |
|---|
| 883 | | - &use_rx_cut_through); |
|---|
| 884 | | - if (rc) |
|---|
| 885 | | - goto err_unmap; |
|---|
| 886 | | - rc = get_dts_property(fifo, "xlnx,use-rx-data", &use_rx_data); |
|---|
| 887 | | - if (rc) |
|---|
| 888 | | - goto err_unmap; |
|---|
| 889 | | - rc = get_dts_property(fifo, "xlnx,use-tx-ctrl", &use_tx_control); |
|---|
| 890 | | - if (rc) |
|---|
| 891 | | - goto err_unmap; |
|---|
| 892 | | - rc = get_dts_property(fifo, "xlnx,use-tx-cut-through", |
|---|
| 893 | | - &use_tx_cut_through); |
|---|
| 894 | | - if (rc) |
|---|
| 895 | | - goto err_unmap; |
|---|
| 896 | | - rc = get_dts_property(fifo, "xlnx,use-tx-data", &use_tx_data); |
|---|
| 897 | | - if (rc) |
|---|
| 898 | | - goto err_unmap; |
|---|
| 899 | | - |
|---|
| 900 | | - /* check validity of device tree properties */ |
|---|
| 901 | | - if (rxd_tdata_width != 32) { |
|---|
| 902 | | - dev_err(fifo->dt_device, |
|---|
| 903 | | - "rxd_tdata_width width [%u] unsupported\n", |
|---|
| 904 | | - rxd_tdata_width); |
|---|
| 905 | | - rc = -EIO; |
|---|
| 906 | | - goto err_unmap; |
|---|
| 907 | | - } |
|---|
| 908 | | - if (txd_tdata_width != 32) { |
|---|
| 909 | | - dev_err(fifo->dt_device, |
|---|
| 910 | | - "txd_tdata_width width [%u] unsupported\n", |
|---|
| 911 | | - txd_tdata_width); |
|---|
| 912 | | - rc = -EIO; |
|---|
| 913 | | - goto err_unmap; |
|---|
| 914 | | - } |
|---|
| 915 | | - if (has_tdest) { |
|---|
| 916 | | - dev_err(fifo->dt_device, "tdest not supported\n"); |
|---|
| 917 | | - rc = -EIO; |
|---|
| 918 | | - goto err_unmap; |
|---|
| 919 | | - } |
|---|
| 920 | | - if (has_tid) { |
|---|
| 921 | | - dev_err(fifo->dt_device, "tid not supported\n"); |
|---|
| 922 | | - rc = -EIO; |
|---|
| 923 | | - goto err_unmap; |
|---|
| 924 | | - } |
|---|
| 925 | | - if (has_tkeep) { |
|---|
| 926 | | - dev_err(fifo->dt_device, "tkeep not supported\n"); |
|---|
| 927 | | - rc = -EIO; |
|---|
| 928 | | - goto err_unmap; |
|---|
| 929 | | - } |
|---|
| 930 | | - if (has_tstrb) { |
|---|
| 931 | | - dev_err(fifo->dt_device, "tstrb not supported\n"); |
|---|
| 932 | | - rc = -EIO; |
|---|
| 933 | | - goto err_unmap; |
|---|
| 934 | | - } |
|---|
| 935 | | - if (has_tuser) { |
|---|
| 936 | | - dev_err(fifo->dt_device, "tuser not supported\n"); |
|---|
| 937 | | - rc = -EIO; |
|---|
| 938 | | - goto err_unmap; |
|---|
| 939 | | - } |
|---|
| 940 | | - if (use_rx_cut_through) { |
|---|
| 941 | | - dev_err(fifo->dt_device, "rx cut-through not supported\n"); |
|---|
| 942 | | - rc = -EIO; |
|---|
| 943 | | - goto err_unmap; |
|---|
| 944 | | - } |
|---|
| 945 | | - if (use_tx_cut_through) { |
|---|
| 946 | | - dev_err(fifo->dt_device, "tx cut-through not supported\n"); |
|---|
| 947 | | - rc = -EIO; |
|---|
| 948 | | - goto err_unmap; |
|---|
| 949 | | - } |
|---|
| 950 | | - if (use_tx_control) { |
|---|
| 951 | | - dev_err(fifo->dt_device, "tx control not supported\n"); |
|---|
| 952 | | - rc = -EIO; |
|---|
| 953 | | - goto err_unmap; |
|---|
| 954 | | - } |
|---|
| 955 | | - |
|---|
| 956 | | - /* TODO |
|---|
| 957 | | - * these exist in the device tree but it's unclear what they do |
|---|
| 958 | | - * - select-xpm |
|---|
| 959 | | - * - data-interface-type |
|---|
| 960 | | - */ |
|---|
| 961 | | - |
|---|
| 962 | | - /* set device wrapper properties based on IP config */ |
|---|
| 963 | | - fifo->rx_fifo_depth = rx_fifo_depth; |
|---|
| 964 | | - /* IP sets TDFV to fifo depth - 4 so we will do the same */ |
|---|
| 965 | | - fifo->tx_fifo_depth = tx_fifo_depth - 4; |
|---|
| 966 | | - fifo->has_rx_fifo = use_rx_data; |
|---|
| 967 | | - fifo->has_tx_fifo = use_tx_data; |
|---|
| 875 | + goto err_initial; |
|---|
| 968 | 876 | |
|---|
| 969 | 877 | reset_ip_core(fifo); |
|---|
| 970 | 878 | |
|---|
| .. | .. |
|---|
| 977 | 885 | r_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
|---|
| 978 | 886 | if (!r_irq) { |
|---|
| 979 | 887 | dev_err(fifo->dt_device, "no IRQ found for 0x%pa\n", |
|---|
| 980 | | - &fifo->mem->start); |
|---|
| 888 | + &r_mem->start); |
|---|
| 981 | 889 | rc = -EIO; |
|---|
| 982 | | - goto err_unmap; |
|---|
| 890 | + goto err_initial; |
|---|
| 983 | 891 | } |
|---|
| 984 | 892 | |
|---|
| 985 | 893 | /* request IRQ */ |
|---|
| 986 | 894 | fifo->irq = r_irq->start; |
|---|
| 987 | | - rc = request_irq(fifo->irq, &axis_fifo_irq, 0, DRIVER_NAME, fifo); |
|---|
| 895 | + rc = devm_request_irq(fifo->dt_device, fifo->irq, &axis_fifo_irq, 0, |
|---|
| 896 | + DRIVER_NAME, fifo); |
|---|
| 988 | 897 | if (rc) { |
|---|
| 989 | 898 | dev_err(fifo->dt_device, "couldn't allocate interrupt %i\n", |
|---|
| 990 | 899 | fifo->irq); |
|---|
| 991 | | - goto err_unmap; |
|---|
| 900 | + goto err_initial; |
|---|
| 992 | 901 | } |
|---|
| 993 | 902 | |
|---|
| 994 | 903 | /* ---------------------------- |
|---|
| .. | .. |
|---|
| 999 | 908 | /* allocate device number */ |
|---|
| 1000 | 909 | rc = alloc_chrdev_region(&fifo->devt, 0, 1, DRIVER_NAME); |
|---|
| 1001 | 910 | if (rc < 0) |
|---|
| 1002 | | - goto err_irq; |
|---|
| 911 | + goto err_initial; |
|---|
| 1003 | 912 | dev_dbg(fifo->dt_device, "allocated device number major %i minor %i\n", |
|---|
| 1004 | 913 | MAJOR(fifo->devt), MINOR(fifo->devt)); |
|---|
| 1005 | 914 | |
|---|
| .. | .. |
|---|
| 1023 | 932 | } |
|---|
| 1024 | 933 | |
|---|
| 1025 | 934 | /* create sysfs entries */ |
|---|
| 1026 | | - rc = sysfs_create_group(&fifo->device->kobj, &axis_fifo_attrs_group); |
|---|
| 935 | + rc = devm_device_add_group(fifo->device, &axis_fifo_attrs_group); |
|---|
| 1027 | 936 | if (rc < 0) { |
|---|
| 1028 | 937 | dev_err(fifo->dt_device, "couldn't register sysfs group\n"); |
|---|
| 1029 | 938 | goto err_cdev; |
|---|
| 1030 | 939 | } |
|---|
| 1031 | 940 | |
|---|
| 1032 | 941 | dev_info(fifo->dt_device, "axis-fifo created at %pa mapped to 0x%pa, irq=%i, major=%i, minor=%i\n", |
|---|
| 1033 | | - &fifo->mem->start, &fifo->base_addr, fifo->irq, |
|---|
| 942 | + &r_mem->start, &fifo->base_addr, fifo->irq, |
|---|
| 1034 | 943 | MAJOR(fifo->devt), MINOR(fifo->devt)); |
|---|
| 1035 | 944 | |
|---|
| 1036 | 945 | return 0; |
|---|
| .. | .. |
|---|
| 1041 | 950 | device_destroy(axis_fifo_driver_class, fifo->devt); |
|---|
| 1042 | 951 | err_chrdev_region: |
|---|
| 1043 | 952 | unregister_chrdev_region(fifo->devt, 1); |
|---|
| 1044 | | -err_irq: |
|---|
| 1045 | | - free_irq(fifo->irq, fifo); |
|---|
| 1046 | | -err_unmap: |
|---|
| 1047 | | - iounmap(fifo->base_addr); |
|---|
| 1048 | | -err_mem: |
|---|
| 1049 | | - release_mem_region(fifo->mem->start, resource_size(fifo->mem)); |
|---|
| 1050 | 953 | err_initial: |
|---|
| 1051 | 954 | dev_set_drvdata(dev, NULL); |
|---|
| 1052 | 955 | return rc; |
|---|
| .. | .. |
|---|
| 1057 | 960 | struct device *dev = &pdev->dev; |
|---|
| 1058 | 961 | struct axis_fifo *fifo = dev_get_drvdata(dev); |
|---|
| 1059 | 962 | |
|---|
| 1060 | | - sysfs_remove_group(&fifo->device->kobj, &axis_fifo_attrs_group); |
|---|
| 1061 | 963 | cdev_del(&fifo->char_device); |
|---|
| 1062 | 964 | dev_set_drvdata(fifo->device, NULL); |
|---|
| 1063 | 965 | device_destroy(axis_fifo_driver_class, fifo->devt); |
|---|
| 1064 | 966 | unregister_chrdev_region(fifo->devt, 1); |
|---|
| 1065 | | - free_irq(fifo->irq, fifo); |
|---|
| 1066 | | - iounmap(fifo->base_addr); |
|---|
| 1067 | | - release_mem_region(fifo->mem->start, resource_size(fifo->mem)); |
|---|
| 1068 | 967 | dev_set_drvdata(dev, NULL); |
|---|
| 968 | + |
|---|
| 1069 | 969 | return 0; |
|---|
| 1070 | 970 | } |
|---|
| 1071 | 971 | |
|---|
| .. | .. |
|---|
| 1089 | 989 | pr_info("axis-fifo driver loaded with parameters read_timeout = %i, write_timeout = %i\n", |
|---|
| 1090 | 990 | read_timeout, write_timeout); |
|---|
| 1091 | 991 | axis_fifo_driver_class = class_create(THIS_MODULE, DRIVER_NAME); |
|---|
| 992 | + if (IS_ERR(axis_fifo_driver_class)) |
|---|
| 993 | + return PTR_ERR(axis_fifo_driver_class); |
|---|
| 1092 | 994 | return platform_driver_register(&axis_fifo_driver); |
|---|
| 1093 | 995 | } |
|---|
| 1094 | 996 | |
|---|