1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx AXIS FIFO: interface to the Xilinx AXI-Stream FIFO IP core
5 * Copyright (C) 2018 Jacob Feder
7 * Authors: Jacob Feder <jacobsfeder@gmail.com>
9 * See Xilinx PG080 document for IP details
12 /* ----------------------------
14 * ----------------------------
17 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/wait.h>
21 #include <linux/mutex.h>
22 #include <linux/device.h>
23 #include <linux/cdev.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h>
30 #include <linux/param.h>
32 #include <linux/types.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <linux/miscdevice.h>
37 /* ----------------------------
39 * ----------------------------
42 #define DRIVER_NAME "axis_fifo"
44 #define READ_BUF_SIZE 128U /* read buffer length in words */
45 #define WRITE_BUF_SIZE 128U /* write buffer length in words */
47 /* ----------------------------
49 * ----------------------------
52 #define XLLF_ISR_OFFSET 0x00000000 /* Interrupt Status */
53 #define XLLF_IER_OFFSET 0x00000004 /* Interrupt Enable */
55 #define XLLF_TDFR_OFFSET 0x00000008 /* Transmit Reset */
56 #define XLLF_TDFV_OFFSET 0x0000000c /* Transmit Vacancy */
57 #define XLLF_TDFD_OFFSET 0x00000010 /* Transmit Data */
58 #define XLLF_TLR_OFFSET 0x00000014 /* Transmit Length */
60 #define XLLF_RDFR_OFFSET 0x00000018 /* Receive Reset */
61 #define XLLF_RDFO_OFFSET 0x0000001c /* Receive Occupancy */
62 #define XLLF_RDFD_OFFSET 0x00000020 /* Receive Data */
63 #define XLLF_RLR_OFFSET 0x00000024 /* Receive Length */
64 #define XLLF_SRR_OFFSET 0x00000028 /* Local Link Reset */
65 #define XLLF_TDR_OFFSET 0x0000002C /* Transmit Destination */
66 #define XLLF_RDR_OFFSET 0x00000030 /* Receive Destination */
68 /* ----------------------------
69 * reset register masks
70 * ----------------------------
73 #define XLLF_RDFR_RESET_MASK 0x000000a5 /* receive reset value */
74 #define XLLF_TDFR_RESET_MASK 0x000000a5 /* Transmit reset value */
75 #define XLLF_SRR_RESET_MASK 0x000000a5 /* Local Link reset value */
77 /* ----------------------------
79 * ----------------------------
82 #define XLLF_INT_RPURE_MASK 0x80000000 /* Receive under-read */
83 #define XLLF_INT_RPORE_MASK 0x40000000 /* Receive over-read */
84 #define XLLF_INT_RPUE_MASK 0x20000000 /* Receive underrun (empty) */
85 #define XLLF_INT_TPOE_MASK 0x10000000 /* Transmit overrun */
86 #define XLLF_INT_TC_MASK 0x08000000 /* Transmit complete */
87 #define XLLF_INT_RC_MASK 0x04000000 /* Receive complete */
88 #define XLLF_INT_TSE_MASK 0x02000000 /* Transmit length mismatch */
89 #define XLLF_INT_TRC_MASK 0x01000000 /* Transmit reset complete */
90 #define XLLF_INT_RRC_MASK 0x00800000 /* Receive reset complete */
91 #define XLLF_INT_TFPF_MASK 0x00400000 /* Tx FIFO Programmable Full */
92 #define XLLF_INT_TFPE_MASK 0x00200000 /* Tx FIFO Programmable Empty */
93 #define XLLF_INT_RFPF_MASK 0x00100000 /* Rx FIFO Programmable Full */
94 #define XLLF_INT_RFPE_MASK 0x00080000 /* Rx FIFO Programmable Empty */
95 #define XLLF_INT_ALL_MASK 0xfff80000 /* All the ints */
96 #define XLLF_INT_ERROR_MASK 0xf2000000 /* Error status ints */
97 #define XLLF_INT_RXERROR_MASK 0xe0000000 /* Receive Error status ints */
98 #define XLLF_INT_TXERROR_MASK 0x12000000 /* Transmit Error status ints */
100 /* ----------------------------
102 * ----------------------------
104 static long read_timeout
= 1000; /* ms to wait before read() times out */
105 static long write_timeout
= 1000; /* ms to wait before write() times out */
107 /* ----------------------------
108 * module command-line arguments
109 * ----------------------------
112 module_param(read_timeout
, long, 0444);
113 MODULE_PARM_DESC(read_timeout
, "ms to wait before blocking read() timing out; set to -1 for no timeout");
114 module_param(write_timeout
, long, 0444);
115 MODULE_PARM_DESC(write_timeout
, "ms to wait before blocking write() timing out; set to -1 for no timeout");
117 /* ----------------------------
119 * ----------------------------
123 int irq
; /* interrupt */
124 void __iomem
*base_addr
; /* kernel space memory */
126 unsigned int rx_fifo_depth
; /* max words in the receive fifo */
127 unsigned int tx_fifo_depth
; /* max words in the transmit fifo */
128 int has_rx_fifo
; /* whether the IP has the rx fifo enabled */
129 int has_tx_fifo
; /* whether the IP has the tx fifo enabled */
131 wait_queue_head_t read_queue
; /* wait queue for asynchronos read */
132 struct mutex read_lock
; /* lock for reading */
133 wait_queue_head_t write_queue
; /* wait queue for asynchronos write */
134 struct mutex write_lock
; /* lock for writing */
135 unsigned int write_flags
; /* write file flags */
136 unsigned int read_flags
; /* read file flags */
138 struct device
*dt_device
; /* device created from the device tree */
139 struct miscdevice miscdev
;
142 /* ----------------------------
144 * ----------------------------
147 static ssize_t
sysfs_write(struct device
*dev
, const char *buf
,
148 size_t count
, unsigned int addr_offset
)
150 struct axis_fifo
*fifo
= dev_get_drvdata(dev
);
154 rc
= kstrtoul(buf
, 0, &tmp
);
158 iowrite32(tmp
, fifo
->base_addr
+ addr_offset
);
163 static ssize_t
sysfs_read(struct device
*dev
, char *buf
,
164 unsigned int addr_offset
)
166 struct axis_fifo
*fifo
= dev_get_drvdata(dev
);
167 unsigned int read_val
;
169 read_val
= ioread32(fifo
->base_addr
+ addr_offset
);
170 return sysfs_emit(buf
, "0x%x\n", read_val
);
173 static ssize_t
isr_store(struct device
*dev
, struct device_attribute
*attr
,
174 const char *buf
, size_t count
)
176 return sysfs_write(dev
, buf
, count
, XLLF_ISR_OFFSET
);
179 static ssize_t
isr_show(struct device
*dev
,
180 struct device_attribute
*attr
, char *buf
)
182 return sysfs_read(dev
, buf
, XLLF_ISR_OFFSET
);
185 static DEVICE_ATTR_RW(isr
);
187 static ssize_t
ier_store(struct device
*dev
, struct device_attribute
*attr
,
188 const char *buf
, size_t count
)
190 return sysfs_write(dev
, buf
, count
, XLLF_IER_OFFSET
);
193 static ssize_t
ier_show(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
196 return sysfs_read(dev
, buf
, XLLF_IER_OFFSET
);
199 static DEVICE_ATTR_RW(ier
);
201 static ssize_t
tdfr_store(struct device
*dev
, struct device_attribute
*attr
,
202 const char *buf
, size_t count
)
204 return sysfs_write(dev
, buf
, count
, XLLF_TDFR_OFFSET
);
207 static DEVICE_ATTR_WO(tdfr
);
209 static ssize_t
tdfv_show(struct device
*dev
,
210 struct device_attribute
*attr
, char *buf
)
212 return sysfs_read(dev
, buf
, XLLF_TDFV_OFFSET
);
215 static DEVICE_ATTR_RO(tdfv
);
217 static ssize_t
tdfd_store(struct device
*dev
, struct device_attribute
*attr
,
218 const char *buf
, size_t count
)
220 return sysfs_write(dev
, buf
, count
, XLLF_TDFD_OFFSET
);
223 static DEVICE_ATTR_WO(tdfd
);
225 static ssize_t
tlr_store(struct device
*dev
, struct device_attribute
*attr
,
226 const char *buf
, size_t count
)
228 return sysfs_write(dev
, buf
, count
, XLLF_TLR_OFFSET
);
231 static DEVICE_ATTR_WO(tlr
);
233 static ssize_t
rdfr_store(struct device
*dev
, struct device_attribute
*attr
,
234 const char *buf
, size_t count
)
236 return sysfs_write(dev
, buf
, count
, XLLF_RDFR_OFFSET
);
239 static DEVICE_ATTR_WO(rdfr
);
241 static ssize_t
rdfo_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
244 return sysfs_read(dev
, buf
, XLLF_RDFO_OFFSET
);
247 static DEVICE_ATTR_RO(rdfo
);
249 static ssize_t
rdfd_show(struct device
*dev
,
250 struct device_attribute
*attr
, char *buf
)
252 return sysfs_read(dev
, buf
, XLLF_RDFD_OFFSET
);
255 static DEVICE_ATTR_RO(rdfd
);
257 static ssize_t
rlr_show(struct device
*dev
,
258 struct device_attribute
*attr
, char *buf
)
260 return sysfs_read(dev
, buf
, XLLF_RLR_OFFSET
);
263 static DEVICE_ATTR_RO(rlr
);
265 static ssize_t
srr_store(struct device
*dev
, struct device_attribute
*attr
,
266 const char *buf
, size_t count
)
268 return sysfs_write(dev
, buf
, count
, XLLF_SRR_OFFSET
);
271 static DEVICE_ATTR_WO(srr
);
273 static ssize_t
tdr_store(struct device
*dev
, struct device_attribute
*attr
,
274 const char *buf
, size_t count
)
276 return sysfs_write(dev
, buf
, count
, XLLF_TDR_OFFSET
);
279 static DEVICE_ATTR_WO(tdr
);
281 static ssize_t
rdr_show(struct device
*dev
,
282 struct device_attribute
*attr
, char *buf
)
284 return sysfs_read(dev
, buf
, XLLF_RDR_OFFSET
);
287 static DEVICE_ATTR_RO(rdr
);
289 static struct attribute
*axis_fifo_attrs
[] = {
306 static const struct attribute_group axis_fifo_attrs_group
= {
307 .name
= "ip_registers",
308 .attrs
= axis_fifo_attrs
,
311 static const struct attribute_group
*axis_fifo_attrs_groups
[] = {
312 &axis_fifo_attrs_group
,
316 /* ----------------------------
318 * ----------------------------
321 static void reset_ip_core(struct axis_fifo
*fifo
)
323 iowrite32(XLLF_SRR_RESET_MASK
, fifo
->base_addr
+ XLLF_SRR_OFFSET
);
324 iowrite32(XLLF_TDFR_RESET_MASK
, fifo
->base_addr
+ XLLF_TDFR_OFFSET
);
325 iowrite32(XLLF_RDFR_RESET_MASK
, fifo
->base_addr
+ XLLF_RDFR_OFFSET
);
326 iowrite32(XLLF_INT_TC_MASK
| XLLF_INT_RC_MASK
| XLLF_INT_RPURE_MASK
|
327 XLLF_INT_RPORE_MASK
| XLLF_INT_RPUE_MASK
|
328 XLLF_INT_TPOE_MASK
| XLLF_INT_TSE_MASK
,
329 fifo
->base_addr
+ XLLF_IER_OFFSET
);
330 iowrite32(XLLF_INT_ALL_MASK
, fifo
->base_addr
+ XLLF_ISR_OFFSET
);
334 * axis_fifo_read() - Read a packet from AXIS-FIFO character device.
336 * @buf: User space buffer to read to.
337 * @len: User space buffer length.
338 * @off: Buffer offset.
340 * As defined by the device's documentation, we need to check the device's
341 * occupancy before reading the length register and then the data. All these
342 * operations must be executed atomically, in order and one after the other
343 * without missing any.
345 * Returns the number of bytes read from the device or negative error code
348 static ssize_t
axis_fifo_read(struct file
*f
, char __user
*buf
,
349 size_t len
, loff_t
*off
)
351 struct axis_fifo
*fifo
= (struct axis_fifo
*)f
->private_data
;
352 size_t bytes_available
;
353 unsigned int words_available
;
358 u32 tmp_buf
[READ_BUF_SIZE
];
360 if (fifo
->read_flags
& O_NONBLOCK
) {
362 * Device opened in non-blocking mode. Try to lock it and then
363 * check if any packet is available.
365 if (!mutex_trylock(&fifo
->read_lock
))
368 if (!ioread32(fifo
->base_addr
+ XLLF_RDFO_OFFSET
)) {
373 /* opened in blocking mode
374 * wait for a packet available interrupt (or timeout)
375 * if nothing is currently available
377 mutex_lock(&fifo
->read_lock
);
378 ret
= wait_event_interruptible_timeout(fifo
->read_queue
,
379 ioread32(fifo
->base_addr
+ XLLF_RDFO_OFFSET
),
385 } else if (ret
!= -ERESTARTSYS
) {
386 dev_err(fifo
->dt_device
, "wait_event_interruptible_timeout() error in read (ret=%i)\n",
394 bytes_available
= ioread32(fifo
->base_addr
+ XLLF_RLR_OFFSET
);
395 if (!bytes_available
) {
396 dev_err(fifo
->dt_device
, "received a packet of length 0 - fifo core will be reset\n");
402 if (bytes_available
> len
) {
403 dev_err(fifo
->dt_device
, "user read buffer too small (available bytes=%zu user buffer bytes=%zu) - fifo core will be reset\n",
404 bytes_available
, len
);
410 if (bytes_available
% sizeof(u32
)) {
411 /* this probably can't happen unless IP
412 * registers were previously mishandled
414 dev_err(fifo
->dt_device
, "received a packet that isn't word-aligned - fifo core will be reset\n");
420 words_available
= bytes_available
/ sizeof(u32
);
422 /* read data into an intermediate buffer, copying the contents
423 * to userspace when the buffer is full
426 while (words_available
> 0) {
427 copy
= min(words_available
, READ_BUF_SIZE
);
429 for (i
= 0; i
< copy
; i
++) {
430 tmp_buf
[i
] = ioread32(fifo
->base_addr
+
434 if (copy_to_user(buf
+ copied
* sizeof(u32
), tmp_buf
,
435 copy
* sizeof(u32
))) {
442 words_available
-= copy
;
445 ret
= bytes_available
;
448 mutex_unlock(&fifo
->read_lock
);
454 * axis_fifo_write() - Write buffer to AXIS-FIFO character device.
456 * @buf: User space buffer to write to the device.
457 * @len: User space buffer length.
458 * @off: Buffer offset.
460 * As defined by the device's documentation, we need to write to the device's
461 * data buffer then to the device's packet length register atomically. Also,
462 * we need to lock before checking if the device has available space to avoid
463 * any concurrency issue.
465 * Returns the number of bytes written to the device or negative error code
468 static ssize_t
axis_fifo_write(struct file
*f
, const char __user
*buf
,
469 size_t len
, loff_t
*off
)
471 struct axis_fifo
*fifo
= (struct axis_fifo
*)f
->private_data
;
472 unsigned int words_to_write
;
477 u32 tmp_buf
[WRITE_BUF_SIZE
];
479 if (len
% sizeof(u32
)) {
480 dev_err(fifo
->dt_device
,
481 "tried to send a packet that isn't word-aligned\n");
485 words_to_write
= len
/ sizeof(u32
);
487 if (!words_to_write
) {
488 dev_err(fifo
->dt_device
,
489 "tried to send a packet of length 0\n");
493 if (words_to_write
> fifo
->tx_fifo_depth
) {
494 dev_err(fifo
->dt_device
, "tried to write more words [%u] than slots in the fifo buffer [%u]\n",
495 words_to_write
, fifo
->tx_fifo_depth
);
499 if (fifo
->write_flags
& O_NONBLOCK
) {
501 * Device opened in non-blocking mode. Try to lock it and then
502 * check if there is any room to write the given buffer.
504 if (!mutex_trylock(&fifo
->write_lock
))
507 if (words_to_write
> ioread32(fifo
->base_addr
+
513 /* opened in blocking mode */
515 /* wait for an interrupt (or timeout) if there isn't
516 * currently enough room in the fifo
518 mutex_lock(&fifo
->write_lock
);
519 ret
= wait_event_interruptible_timeout(fifo
->write_queue
,
520 ioread32(fifo
->base_addr
+ XLLF_TDFV_OFFSET
)
527 } else if (ret
!= -ERESTARTSYS
) {
528 dev_err(fifo
->dt_device
, "wait_event_interruptible_timeout() error in write (ret=%i)\n",
536 /* write data from an intermediate buffer into the fifo IP, refilling
537 * the buffer with userspace data as needed
540 while (words_to_write
> 0) {
541 copy
= min(words_to_write
, WRITE_BUF_SIZE
);
543 if (copy_from_user(tmp_buf
, buf
+ copied
* sizeof(u32
),
544 copy
* sizeof(u32
))) {
550 for (i
= 0; i
< copy
; i
++)
551 iowrite32(tmp_buf
[i
], fifo
->base_addr
+
555 words_to_write
-= copy
;
558 ret
= copied
* sizeof(u32
);
560 /* write packet size to fifo */
561 iowrite32(ret
, fifo
->base_addr
+ XLLF_TLR_OFFSET
);
564 mutex_unlock(&fifo
->write_lock
);
569 static irqreturn_t
axis_fifo_irq(int irq
, void *dw
)
571 struct axis_fifo
*fifo
= (struct axis_fifo
*)dw
;
572 unsigned int pending_interrupts
;
575 pending_interrupts
= ioread32(fifo
->base_addr
+
577 ioread32(fifo
->base_addr
579 if (pending_interrupts
& XLLF_INT_RC_MASK
) {
580 /* packet received */
582 /* wake the reader process if it is waiting */
583 wake_up(&fifo
->read_queue
);
585 /* clear interrupt */
586 iowrite32(XLLF_INT_RC_MASK
& XLLF_INT_ALL_MASK
,
587 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
588 } else if (pending_interrupts
& XLLF_INT_TC_MASK
) {
591 /* wake the writer process if it is waiting */
592 wake_up(&fifo
->write_queue
);
594 iowrite32(XLLF_INT_TC_MASK
& XLLF_INT_ALL_MASK
,
595 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
596 } else if (pending_interrupts
& XLLF_INT_TFPF_MASK
) {
597 /* transmit fifo programmable full */
599 iowrite32(XLLF_INT_TFPF_MASK
& XLLF_INT_ALL_MASK
,
600 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
601 } else if (pending_interrupts
& XLLF_INT_TFPE_MASK
) {
602 /* transmit fifo programmable empty */
604 iowrite32(XLLF_INT_TFPE_MASK
& XLLF_INT_ALL_MASK
,
605 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
606 } else if (pending_interrupts
& XLLF_INT_RFPF_MASK
) {
607 /* receive fifo programmable full */
609 iowrite32(XLLF_INT_RFPF_MASK
& XLLF_INT_ALL_MASK
,
610 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
611 } else if (pending_interrupts
& XLLF_INT_RFPE_MASK
) {
612 /* receive fifo programmable empty */
614 iowrite32(XLLF_INT_RFPE_MASK
& XLLF_INT_ALL_MASK
,
615 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
616 } else if (pending_interrupts
& XLLF_INT_TRC_MASK
) {
617 /* transmit reset complete interrupt */
619 iowrite32(XLLF_INT_TRC_MASK
& XLLF_INT_ALL_MASK
,
620 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
621 } else if (pending_interrupts
& XLLF_INT_RRC_MASK
) {
622 /* receive reset complete interrupt */
624 iowrite32(XLLF_INT_RRC_MASK
& XLLF_INT_ALL_MASK
,
625 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
626 } else if (pending_interrupts
& XLLF_INT_RPURE_MASK
) {
627 /* receive fifo under-read error interrupt */
628 dev_err(fifo
->dt_device
,
629 "receive under-read interrupt\n");
631 iowrite32(XLLF_INT_RPURE_MASK
& XLLF_INT_ALL_MASK
,
632 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
633 } else if (pending_interrupts
& XLLF_INT_RPORE_MASK
) {
634 /* receive over-read error interrupt */
635 dev_err(fifo
->dt_device
,
636 "receive over-read interrupt\n");
638 iowrite32(XLLF_INT_RPORE_MASK
& XLLF_INT_ALL_MASK
,
639 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
640 } else if (pending_interrupts
& XLLF_INT_RPUE_MASK
) {
641 /* receive underrun error interrupt */
642 dev_err(fifo
->dt_device
,
643 "receive underrun error interrupt\n");
645 iowrite32(XLLF_INT_RPUE_MASK
& XLLF_INT_ALL_MASK
,
646 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
647 } else if (pending_interrupts
& XLLF_INT_TPOE_MASK
) {
648 /* transmit overrun error interrupt */
649 dev_err(fifo
->dt_device
,
650 "transmit overrun error interrupt\n");
652 iowrite32(XLLF_INT_TPOE_MASK
& XLLF_INT_ALL_MASK
,
653 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
654 } else if (pending_interrupts
& XLLF_INT_TSE_MASK
) {
655 /* transmit length mismatch error interrupt */
656 dev_err(fifo
->dt_device
,
657 "transmit length mismatch error interrupt\n");
659 iowrite32(XLLF_INT_TSE_MASK
& XLLF_INT_ALL_MASK
,
660 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
661 } else if (pending_interrupts
) {
662 /* unknown interrupt type */
663 dev_err(fifo
->dt_device
,
664 "unknown interrupt(s) 0x%x\n",
667 iowrite32(XLLF_INT_ALL_MASK
,
668 fifo
->base_addr
+ XLLF_ISR_OFFSET
);
670 } while (pending_interrupts
);
675 static int axis_fifo_open(struct inode
*inod
, struct file
*f
)
677 struct axis_fifo
*fifo
= container_of(f
->private_data
,
678 struct axis_fifo
, miscdev
);
679 f
->private_data
= fifo
;
681 if (((f
->f_flags
& O_ACCMODE
) == O_WRONLY
) ||
682 ((f
->f_flags
& O_ACCMODE
) == O_RDWR
)) {
683 if (fifo
->has_tx_fifo
) {
684 fifo
->write_flags
= f
->f_flags
;
686 dev_err(fifo
->dt_device
, "tried to open device for write but the transmit fifo is disabled\n");
691 if (((f
->f_flags
& O_ACCMODE
) == O_RDONLY
) ||
692 ((f
->f_flags
& O_ACCMODE
) == O_RDWR
)) {
693 if (fifo
->has_rx_fifo
) {
694 fifo
->read_flags
= f
->f_flags
;
696 dev_err(fifo
->dt_device
, "tried to open device for read but the receive fifo is disabled\n");
704 static int axis_fifo_close(struct inode
*inod
, struct file
*f
)
706 f
->private_data
= NULL
;
711 static const struct file_operations fops
= {
712 .owner
= THIS_MODULE
,
713 .open
= axis_fifo_open
,
714 .release
= axis_fifo_close
,
715 .read
= axis_fifo_read
,
716 .write
= axis_fifo_write
719 /* read named property from the device tree */
720 static int get_dts_property(struct axis_fifo
*fifo
,
721 char *name
, unsigned int *var
)
725 rc
= of_property_read_u32(fifo
->dt_device
->of_node
, name
, var
);
727 dev_err(fifo
->dt_device
, "couldn't read IP dts property '%s'",
731 dev_dbg(fifo
->dt_device
, "dts property '%s' = %u\n",
737 static int axis_fifo_parse_dt(struct axis_fifo
*fifo
)
742 ret
= get_dts_property(fifo
, "xlnx,axi-str-rxd-tdata-width", &value
);
744 dev_err(fifo
->dt_device
, "missing xlnx,axi-str-rxd-tdata-width property\n");
746 } else if (value
!= 32) {
747 dev_err(fifo
->dt_device
, "xlnx,axi-str-rxd-tdata-width only supports 32 bits\n");
752 ret
= get_dts_property(fifo
, "xlnx,axi-str-txd-tdata-width", &value
);
754 dev_err(fifo
->dt_device
, "missing xlnx,axi-str-txd-tdata-width property\n");
756 } else if (value
!= 32) {
757 dev_err(fifo
->dt_device
, "xlnx,axi-str-txd-tdata-width only supports 32 bits\n");
762 ret
= get_dts_property(fifo
, "xlnx,rx-fifo-depth",
763 &fifo
->rx_fifo_depth
);
765 dev_err(fifo
->dt_device
, "missing xlnx,rx-fifo-depth property\n");
770 ret
= get_dts_property(fifo
, "xlnx,tx-fifo-depth",
771 &fifo
->tx_fifo_depth
);
773 dev_err(fifo
->dt_device
, "missing xlnx,tx-fifo-depth property\n");
778 /* IP sets TDFV to fifo depth - 4 so we will do the same */
779 fifo
->tx_fifo_depth
-= 4;
781 ret
= get_dts_property(fifo
, "xlnx,use-rx-data", &fifo
->has_rx_fifo
);
783 dev_err(fifo
->dt_device
, "missing xlnx,use-rx-data property\n");
788 ret
= get_dts_property(fifo
, "xlnx,use-tx-data", &fifo
->has_tx_fifo
);
790 dev_err(fifo
->dt_device
, "missing xlnx,use-tx-data property\n");
799 static int axis_fifo_probe(struct platform_device
*pdev
)
801 struct resource
*r_mem
; /* IO mem resources */
802 struct device
*dev
= &pdev
->dev
; /* OS device (from device tree) */
803 struct axis_fifo
*fifo
= NULL
;
805 int rc
= 0; /* error return value */
807 /* ----------------------------
808 * init wrapper device
809 * ----------------------------
812 device_name
= devm_kzalloc(dev
, 32, GFP_KERNEL
);
816 /* allocate device wrapper memory */
817 fifo
= devm_kzalloc(dev
, sizeof(*fifo
), GFP_KERNEL
);
821 dev_set_drvdata(dev
, fifo
);
822 fifo
->dt_device
= dev
;
824 init_waitqueue_head(&fifo
->read_queue
);
825 init_waitqueue_head(&fifo
->write_queue
);
827 mutex_init(&fifo
->read_lock
);
828 mutex_init(&fifo
->write_lock
);
830 /* ----------------------------
831 * init device memory space
832 * ----------------------------
835 /* get iospace for the device and request physical memory */
836 fifo
->base_addr
= devm_platform_get_and_ioremap_resource(pdev
, 0, &r_mem
);
837 if (IS_ERR(fifo
->base_addr
)) {
838 rc
= PTR_ERR(fifo
->base_addr
);
842 dev_dbg(fifo
->dt_device
, "remapped memory to 0x%p\n", fifo
->base_addr
);
844 /* create unique device name */
845 snprintf(device_name
, 32, "%s_%pa", DRIVER_NAME
, &r_mem
->start
);
846 dev_dbg(fifo
->dt_device
, "device name [%s]\n", device_name
);
848 /* ----------------------------
850 * ----------------------------
853 rc
= axis_fifo_parse_dt(fifo
);
859 /* ----------------------------
860 * init device interrupts
861 * ----------------------------
864 /* get IRQ resource */
865 rc
= platform_get_irq(pdev
, 0);
871 rc
= devm_request_irq(fifo
->dt_device
, fifo
->irq
, &axis_fifo_irq
, 0,
874 dev_err(fifo
->dt_device
, "couldn't allocate interrupt %i\n",
879 /* ----------------------------
881 * ----------------------------
884 /* create character device */
885 fifo
->miscdev
.fops
= &fops
;
886 fifo
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
887 fifo
->miscdev
.name
= device_name
;
888 fifo
->miscdev
.groups
= axis_fifo_attrs_groups
;
889 fifo
->miscdev
.parent
= dev
;
890 rc
= misc_register(&fifo
->miscdev
);
897 dev_set_drvdata(dev
, NULL
);
901 static void axis_fifo_remove(struct platform_device
*pdev
)
903 struct device
*dev
= &pdev
->dev
;
904 struct axis_fifo
*fifo
= dev_get_drvdata(dev
);
906 misc_deregister(&fifo
->miscdev
);
907 dev_set_drvdata(dev
, NULL
);
910 static const struct of_device_id axis_fifo_of_match
[] = {
911 { .compatible
= "xlnx,axi-fifo-mm-s-4.1", },
914 MODULE_DEVICE_TABLE(of
, axis_fifo_of_match
);
916 static struct platform_driver axis_fifo_driver
= {
919 .of_match_table
= axis_fifo_of_match
,
921 .probe
= axis_fifo_probe
,
922 .remove
= axis_fifo_remove
,
925 static int __init
axis_fifo_init(void)
927 if (read_timeout
>= 0)
928 read_timeout
= msecs_to_jiffies(read_timeout
);
930 read_timeout
= MAX_SCHEDULE_TIMEOUT
;
932 if (write_timeout
>= 0)
933 write_timeout
= msecs_to_jiffies(write_timeout
);
935 write_timeout
= MAX_SCHEDULE_TIMEOUT
;
937 pr_info("axis-fifo driver loaded with parameters read_timeout = %li, write_timeout = %li\n",
938 read_timeout
, write_timeout
);
939 return platform_driver_register(&axis_fifo_driver
);
942 module_init(axis_fifo_init
);
944 static void __exit
axis_fifo_exit(void)
946 platform_driver_unregister(&axis_fifo_driver
);
949 module_exit(axis_fifo_exit
);
951 MODULE_LICENSE("GPL");
952 MODULE_AUTHOR("Jacob Feder <jacobsfeder@gmail.com>");
953 MODULE_DESCRIPTION("Xilinx AXI-Stream FIFO v4.1 IP core driver");