2 * CARMA DATA-FPGA Access Driver
4 * Copyright (c) 2009-2011 Ira W. Snyder <iws@ovro.caltech.edu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 * FPGA Memory Dump Format
15 * FPGA #0 control registers (32 x 32-bit words)
16 * FPGA #1 control registers (32 x 32-bit words)
17 * FPGA #2 control registers (32 x 32-bit words)
18 * FPGA #3 control registers (32 x 32-bit words)
19 * SYSFPGA control registers (32 x 32-bit words)
20 * FPGA #0 correlation array (NUM_CORL0 correlation blocks)
21 * FPGA #1 correlation array (NUM_CORL1 correlation blocks)
22 * FPGA #2 correlation array (NUM_CORL2 correlation blocks)
23 * FPGA #3 correlation array (NUM_CORL3 correlation blocks)
25 * Each correlation array consists of:
27 * Correlation Data (2 x NUM_LAGSn x 32-bit words)
28 * Pipeline Metadata (2 x NUM_METAn x 32-bit words)
29 * Quantization Counters (2 x NUM_QCNTn x 32-bit words)
31 * The NUM_CORLn, NUM_LAGSn, NUM_METAn, and NUM_QCNTn values come from
32 * the FPGA configuration registers. They do not change once the FPGA's
33 * have been programmed, they only change on re-programming.
39 * This driver is used to capture correlation spectra off of the four data
40 * processing FPGAs. The FPGAs are often reprogrammed at runtime, therefore
41 * this driver supports dynamic enable/disable of capture while the device
44 * The nominal capture rate is 64Hz (every 15.625ms). To facilitate this fast
45 * capture rate, all buffers are pre-allocated to avoid any potentially long
46 * running memory allocations while capturing.
48 * There are two lists and one pointer which are used to keep track of the
49 * different states of data buffers.
52 * This list holds all empty data buffers which are ready to receive data.
55 * This pointer holds the currently inflight data buffer. This buffer is having
56 * data copied into it by the DMA engine.
59 * This list holds data buffers which have been filled, and are waiting to be
62 * All buffers start life on the free list, then move successively to the
63 * inflight pointer, and then to the used list. After they have been read by
64 * userspace, they are moved back to the free list. The cycle repeats as long
67 * It should be noted that all buffers are mapped and ready for DMA when they
68 * are on any of the three lists. They are only unmapped when they are in the
69 * process of being read by userspace.
73 * Notes on the IRQ masking scheme:
75 * The IRQ masking scheme here is different than most other hardware. The only
76 * way for the DATA-FPGAs to detect if the kernel has taken too long to copy
77 * the data is if the status registers are not cleared before the next
78 * correlation data dump is ready.
80 * The interrupt line is connected to the status registers, such that when they
81 * are cleared, the interrupt is de-asserted. Therein lies our problem. We need
82 * to schedule a long-running DMA operation and return from the interrupt
83 * handler quickly, but we cannot clear the status registers.
85 * To handle this, the system controller FPGA has the capability to connect the
86 * interrupt line to a user-controlled GPIO pin. This pin is driven high
87 * (unasserted) and left that way. To mask the interrupt, we change the
88 * interrupt source to the GPIO pin. Tada, we hid the interrupt. :)
91 #include <linux/of_address.h>
92 #include <linux/of_irq.h>
93 #include <linux/of_platform.h>
94 #include <linux/dma-mapping.h>
95 #include <linux/miscdevice.h>
96 #include <linux/interrupt.h>
97 #include <linux/dmaengine.h>
98 #include <linux/seq_file.h>
99 #include <linux/highmem.h>
100 #include <linux/debugfs.h>
101 #include <linux/kernel.h>
102 #include <linux/module.h>
103 #include <linux/poll.h>
104 #include <linux/init.h>
105 #include <linux/slab.h>
106 #include <linux/kref.h>
107 #include <linux/io.h>
109 #include <media/videobuf-dma-sg.h>
111 /* system controller registers */
112 #define SYS_IRQ_SOURCE_CTL 0x24
113 #define SYS_IRQ_OUTPUT_EN 0x28
114 #define SYS_IRQ_OUTPUT_DATA 0x2C
115 #define SYS_IRQ_INPUT_DATA 0x30
116 #define SYS_FPGA_CONFIG_STATUS 0x44
118 /* GPIO IRQ line assignment */
119 #define IRQ_CORL_DONE 0x10
122 #define MMAP_REG_VERSION 0x00
123 #define MMAP_REG_CORL_CONF1 0x08
124 #define MMAP_REG_CORL_CONF2 0x0C
125 #define MMAP_REG_STATUS 0x48
127 #define SYS_FPGA_BLOCK 0xF0000000
129 #define DATA_FPGA_START 0x400000
130 #define DATA_FPGA_SIZE 0x80000
132 static const char drv_name
[] = "carma-fpga";
136 #define MIN_DATA_BUFS 8
137 #define MAX_DATA_BUFS 64
140 unsigned int num_lag_ram
;
141 unsigned int blk_size
;
145 struct list_head entry
;
146 struct videobuf_dmabuf vb
;
151 /* character device */
152 struct miscdevice miscdev
;
156 /* reference count */
159 /* FPGA registers and information */
160 struct fpga_info info
[NUM_FPGA
];
164 /* FPGA Physical Address/Size Information */
165 resource_size_t phys_addr
;
169 struct sg_table corl_table
;
170 unsigned int corl_nents
;
171 struct dma_chan
*chan
;
173 /* Protection for all members below */
176 /* Device enable/disable flag */
179 /* Correlation data buffers */
180 wait_queue_head_t wait
;
181 struct list_head free
;
182 struct list_head used
;
183 struct data_buf
*inflight
;
185 /* Information about data buffers */
186 unsigned int num_dropped
;
187 unsigned int num_buffers
;
189 struct dentry
*dbg_entry
;
193 struct fpga_device
*priv
;
194 struct data_buf
*buf
;
198 static void fpga_device_release(struct kref
*ref
)
200 struct fpga_device
*priv
= container_of(ref
, struct fpga_device
, ref
);
202 /* the last reader has exited, cleanup the last bits */
203 mutex_destroy(&priv
->mutex
);
208 * Data Buffer Allocation Helpers
212 * data_free_buffer() - free a single data buffer and all allocated memory
213 * @buf: the buffer to free
215 * This will free all of the pages allocated to the given data buffer, and
216 * then free the structure itself
218 static void data_free_buffer(struct data_buf
*buf
)
220 /* It is ok to free a NULL buffer */
224 /* free all memory */
225 videobuf_dma_free(&buf
->vb
);
230 * data_alloc_buffer() - allocate and fill a data buffer with pages
231 * @bytes: the number of bytes required
233 * This allocates all space needed for a data buffer. It must be mapped before
234 * use in a DMA transaction using videobuf_dma_map().
236 * Returns NULL on failure
238 static struct data_buf
*data_alloc_buffer(const size_t bytes
)
240 unsigned int nr_pages
;
241 struct data_buf
*buf
;
244 /* calculate the number of pages necessary */
245 nr_pages
= DIV_ROUND_UP(bytes
, PAGE_SIZE
);
247 /* allocate the buffer structure */
248 buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
252 /* initialize internal fields */
253 INIT_LIST_HEAD(&buf
->entry
);
256 /* allocate the videobuf */
257 videobuf_dma_init(&buf
->vb
);
258 ret
= videobuf_dma_init_kernel(&buf
->vb
, DMA_FROM_DEVICE
, nr_pages
);
271 * data_free_buffers() - free all allocated buffers
272 * @priv: the driver's private data structure
274 * Free all buffers allocated by the driver (except those currently in the
275 * process of being read by userspace).
277 * LOCKING: must hold dev->mutex
280 static void data_free_buffers(struct fpga_device
*priv
)
282 struct data_buf
*buf
, *tmp
;
284 /* the device should be stopped, no DMA in progress */
285 BUG_ON(priv
->inflight
!= NULL
);
287 list_for_each_entry_safe(buf
, tmp
, &priv
->free
, entry
) {
288 list_del_init(&buf
->entry
);
289 videobuf_dma_unmap(priv
->dev
, &buf
->vb
);
290 data_free_buffer(buf
);
293 list_for_each_entry_safe(buf
, tmp
, &priv
->used
, entry
) {
294 list_del_init(&buf
->entry
);
295 videobuf_dma_unmap(priv
->dev
, &buf
->vb
);
296 data_free_buffer(buf
);
299 priv
->num_buffers
= 0;
304 * data_alloc_buffers() - allocate 1 seconds worth of data buffers
305 * @priv: the driver's private data structure
307 * Allocate enough buffers for a whole second worth of data
309 * This routine will attempt to degrade nicely by succeeding even if a full
310 * second worth of data buffers could not be allocated, as long as a minimum
311 * number were allocated. In this case, it will print a message to the kernel
314 * The device must not be modifying any lists when this is called.
317 * LOCKING: must hold dev->mutex
319 * Returns 0 on success, -ERRNO otherwise
321 static int data_alloc_buffers(struct fpga_device
*priv
)
323 struct data_buf
*buf
;
326 for (i
= 0; i
< MAX_DATA_BUFS
; i
++) {
328 /* allocate a buffer */
329 buf
= data_alloc_buffer(priv
->bufsize
);
334 ret
= videobuf_dma_map(priv
->dev
, &buf
->vb
);
336 data_free_buffer(buf
);
340 /* add it to the list of free buffers */
341 list_add_tail(&buf
->entry
, &priv
->free
);
345 /* Make sure we allocated the minimum required number of buffers */
346 if (priv
->num_buffers
< MIN_DATA_BUFS
) {
347 dev_err(priv
->dev
, "Unable to allocate enough data buffers\n");
348 data_free_buffers(priv
);
352 /* Warn if we are running in a degraded state, but do not fail */
353 if (priv
->num_buffers
< MAX_DATA_BUFS
) {
355 "Unable to allocate %d buffers, using %d buffers instead\n",
363 * DMA Operations Helpers
367 * fpga_start_addr() - get the physical address a DATA-FPGA
368 * @priv: the driver's private data structure
369 * @fpga: the DATA-FPGA number (zero based)
371 static dma_addr_t
fpga_start_addr(struct fpga_device
*priv
, unsigned int fpga
)
373 return priv
->phys_addr
+ 0x400000 + (0x80000 * fpga
);
377 * fpga_block_addr() - get the physical address of a correlation data block
378 * @priv: the driver's private data structure
379 * @fpga: the DATA-FPGA number (zero based)
380 * @blknum: the correlation block number (zero based)
382 static dma_addr_t
fpga_block_addr(struct fpga_device
*priv
, unsigned int fpga
,
385 return fpga_start_addr(priv
, fpga
) + (0x10000 * (1 + blknum
));
388 #define REG_BLOCK_SIZE (32 * 4)
391 * data_setup_corl_table() - create the scatterlist for correlation dumps
392 * @priv: the driver's private data structure
394 * Create the scatterlist for transferring a correlation dump from the
395 * DATA FPGAs. This structure will be reused for each buffer than needs
396 * to be filled with correlation data.
398 * Returns 0 on success, -ERRNO otherwise
400 static int data_setup_corl_table(struct fpga_device
*priv
)
402 struct sg_table
*table
= &priv
->corl_table
;
403 struct scatterlist
*sg
;
404 struct fpga_info
*info
;
407 /* Calculate the number of entries needed */
408 priv
->corl_nents
= (1 + NUM_FPGA
) * REG_BLOCK_SIZE
;
409 for (i
= 0; i
< NUM_FPGA
; i
++)
410 priv
->corl_nents
+= priv
->info
[i
].num_lag_ram
;
412 /* Allocate the scatterlist table */
413 ret
= sg_alloc_table(table
, priv
->corl_nents
, GFP_KERNEL
);
415 dev_err(priv
->dev
, "unable to allocate DMA table\n");
419 /* Add the DATA FPGA registers to the scatterlist */
421 for (i
= 0; i
< NUM_FPGA
; i
++) {
422 sg_dma_address(sg
) = fpga_start_addr(priv
, i
);
423 sg_dma_len(sg
) = REG_BLOCK_SIZE
;
427 /* Add the SYS-FPGA registers to the scatterlist */
428 sg_dma_address(sg
) = SYS_FPGA_BLOCK
;
429 sg_dma_len(sg
) = REG_BLOCK_SIZE
;
432 /* Add the FPGA correlation data blocks to the scatterlist */
433 for (i
= 0; i
< NUM_FPGA
; i
++) {
434 info
= &priv
->info
[i
];
435 for (j
= 0; j
< info
->num_lag_ram
; j
++) {
436 sg_dma_address(sg
) = fpga_block_addr(priv
, i
, j
);
437 sg_dma_len(sg
) = info
->blk_size
;
443 * All physical addresses and lengths are present in the structure
444 * now. It can be reused for every FPGA DATA interrupt
450 * FPGA Register Access Helpers
453 static void fpga_write_reg(struct fpga_device
*priv
, unsigned int fpga
,
454 unsigned int reg
, u32 val
)
456 const int fpga_start
= DATA_FPGA_START
+ (fpga
* DATA_FPGA_SIZE
);
457 iowrite32be(val
, priv
->regs
+ fpga_start
+ reg
);
460 static u32
fpga_read_reg(struct fpga_device
*priv
, unsigned int fpga
,
463 const int fpga_start
= DATA_FPGA_START
+ (fpga
* DATA_FPGA_SIZE
);
464 return ioread32be(priv
->regs
+ fpga_start
+ reg
);
468 * data_calculate_bufsize() - calculate the data buffer size required
469 * @priv: the driver's private data structure
471 * Calculate the total buffer size needed to hold a single block
472 * of correlation data
476 * Returns 0 on success, -ERRNO otherwise
478 static int data_calculate_bufsize(struct fpga_device
*priv
)
480 u32 num_corl
, num_lags
, num_meta
, num_qcnt
, num_pack
;
481 u32 conf1
, conf2
, version
;
482 u32 num_lag_ram
, blk_size
;
485 /* Each buffer starts with the 5 FPGA register areas */
486 priv
->bufsize
= (1 + NUM_FPGA
) * REG_BLOCK_SIZE
;
488 /* Read and store the configuration data for each FPGA */
489 for (i
= 0; i
< NUM_FPGA
; i
++) {
490 version
= fpga_read_reg(priv
, i
, MMAP_REG_VERSION
);
491 conf1
= fpga_read_reg(priv
, i
, MMAP_REG_CORL_CONF1
);
492 conf2
= fpga_read_reg(priv
, i
, MMAP_REG_CORL_CONF2
);
494 /* minor version 2 and later */
495 if ((version
& 0x000000FF) >= 2) {
496 num_corl
= (conf1
& 0x000000F0) >> 4;
497 num_pack
= (conf1
& 0x00000F00) >> 8;
498 num_lags
= (conf1
& 0x00FFF000) >> 12;
499 num_meta
= (conf1
& 0x7F000000) >> 24;
500 num_qcnt
= (conf2
& 0x00000FFF) >> 0;
502 num_corl
= (conf1
& 0x000000F0) >> 4;
503 num_pack
= 1; /* implied */
504 num_lags
= (conf1
& 0x000FFF00) >> 8;
505 num_meta
= (conf1
& 0x7FF00000) >> 20;
506 num_qcnt
= (conf2
& 0x00000FFF) >> 0;
509 num_lag_ram
= (num_corl
+ num_pack
- 1) / num_pack
;
510 blk_size
= ((num_pack
* num_lags
) + num_meta
+ num_qcnt
) * 8;
512 priv
->info
[i
].num_lag_ram
= num_lag_ram
;
513 priv
->info
[i
].blk_size
= blk_size
;
514 priv
->bufsize
+= num_lag_ram
* blk_size
;
516 dev_dbg(priv
->dev
, "FPGA %d NUM_CORL: %d\n", i
, num_corl
);
517 dev_dbg(priv
->dev
, "FPGA %d NUM_PACK: %d\n", i
, num_pack
);
518 dev_dbg(priv
->dev
, "FPGA %d NUM_LAGS: %d\n", i
, num_lags
);
519 dev_dbg(priv
->dev
, "FPGA %d NUM_META: %d\n", i
, num_meta
);
520 dev_dbg(priv
->dev
, "FPGA %d NUM_QCNT: %d\n", i
, num_qcnt
);
521 dev_dbg(priv
->dev
, "FPGA %d BLK_SIZE: %d\n", i
, blk_size
);
524 dev_dbg(priv
->dev
, "TOTAL BUFFER SIZE: %zu bytes\n", priv
->bufsize
);
533 * data_disable_interrupts() - stop the device from generating interrupts
534 * @priv: the driver's private data structure
536 * Hide interrupts by switching to GPIO interrupt source
538 * LOCKING: must hold dev->lock
540 static void data_disable_interrupts(struct fpga_device
*priv
)
542 /* hide the interrupt by switching the IRQ driver to GPIO */
543 iowrite32be(0x2F, priv
->regs
+ SYS_IRQ_SOURCE_CTL
);
547 * data_enable_interrupts() - allow the device to generate interrupts
548 * @priv: the driver's private data structure
550 * Unhide interrupts by switching to the FPGA interrupt source. At the
551 * same time, clear the DATA-FPGA status registers.
553 * LOCKING: must hold dev->lock
555 static void data_enable_interrupts(struct fpga_device
*priv
)
557 /* clear the actual FPGA corl_done interrupt */
558 fpga_write_reg(priv
, 0, MMAP_REG_STATUS
, 0x0);
559 fpga_write_reg(priv
, 1, MMAP_REG_STATUS
, 0x0);
560 fpga_write_reg(priv
, 2, MMAP_REG_STATUS
, 0x0);
561 fpga_write_reg(priv
, 3, MMAP_REG_STATUS
, 0x0);
563 /* flush the writes */
564 fpga_read_reg(priv
, 0, MMAP_REG_STATUS
);
565 fpga_read_reg(priv
, 1, MMAP_REG_STATUS
);
566 fpga_read_reg(priv
, 2, MMAP_REG_STATUS
);
567 fpga_read_reg(priv
, 3, MMAP_REG_STATUS
);
569 /* switch back to the external interrupt source */
570 iowrite32be(0x3F, priv
->regs
+ SYS_IRQ_SOURCE_CTL
);
574 * data_dma_cb() - DMAEngine callback for DMA completion
575 * @data: the driver's private data structure
577 * Complete a DMA transfer from the DATA-FPGA's
579 * This is called via the DMA callback mechanism, and will handle moving the
580 * completed DMA transaction to the used list, and then wake any processes
581 * waiting for new data
583 * CONTEXT: any, softirq expected
585 static void data_dma_cb(void *data
)
587 struct fpga_device
*priv
= data
;
590 spin_lock_irqsave(&priv
->lock
, flags
);
592 /* If there is no inflight buffer, we've got a bug */
593 BUG_ON(priv
->inflight
== NULL
);
595 /* Move the inflight buffer onto the used list */
596 list_move_tail(&priv
->inflight
->entry
, &priv
->used
);
597 priv
->inflight
= NULL
;
600 * If data dumping is still enabled, then clear the FPGA
601 * status registers and re-enable FPGA interrupts
604 data_enable_interrupts(priv
);
606 spin_unlock_irqrestore(&priv
->lock
, flags
);
609 * We've changed both the inflight and used lists, so we need
610 * to wake up any processes that are blocking for those events
612 wake_up(&priv
->wait
);
616 * data_submit_dma() - prepare and submit the required DMA to fill a buffer
617 * @priv: the driver's private data structure
618 * @buf: the data buffer
620 * Prepare and submit the necessary DMA transactions to fill a correlation
623 * LOCKING: must hold dev->lock
624 * CONTEXT: hardirq only
626 * Returns 0 on success, -ERRNO otherwise
628 static int data_submit_dma(struct fpga_device
*priv
, struct data_buf
*buf
)
630 struct scatterlist
*dst_sg
, *src_sg
;
631 unsigned int dst_nents
, src_nents
;
632 struct dma_chan
*chan
= priv
->chan
;
633 struct dma_async_tx_descriptor
*tx
;
636 unsigned long dma_flags
= 0;
638 dst_sg
= buf
->vb
.sglist
;
639 dst_nents
= buf
->vb
.sglen
;
641 src_sg
= priv
->corl_table
.sgl
;
642 src_nents
= priv
->corl_nents
;
645 * All buffers passed to this function should be ready and mapped
646 * for DMA already. Therefore, we don't need to do anything except
647 * submit it to the Freescale DMA Engine for processing
650 /* setup the scatterlist to scatterlist transfer */
651 tx
= chan
->device
->device_prep_dma_sg(chan
,
656 dev_err(priv
->dev
, "unable to prep scatterlist DMA\n");
660 /* submit the transaction to the DMA controller */
661 cookie
= tx
->tx_submit(tx
);
662 if (dma_submit_error(cookie
)) {
663 dev_err(priv
->dev
, "unable to submit scatterlist DMA\n");
667 /* Prepare the re-read of the SYS-FPGA block */
668 dst
= sg_dma_address(dst_sg
) + (NUM_FPGA
* REG_BLOCK_SIZE
);
669 src
= SYS_FPGA_BLOCK
;
670 tx
= chan
->device
->device_prep_dma_memcpy(chan
, dst
, src
,
674 dev_err(priv
->dev
, "unable to prep SYS-FPGA DMA\n");
678 /* Setup the callback */
679 tx
->callback
= data_dma_cb
;
680 tx
->callback_param
= priv
;
682 /* submit the transaction to the DMA controller */
683 cookie
= tx
->tx_submit(tx
);
684 if (dma_submit_error(cookie
)) {
685 dev_err(priv
->dev
, "unable to submit SYS-FPGA DMA\n");
692 #define CORL_DONE 0x1
695 static irqreturn_t
data_irq(int irq
, void *dev_id
)
697 struct fpga_device
*priv
= dev_id
;
698 bool submitted
= false;
699 struct data_buf
*buf
;
703 /* detect spurious interrupts via FPGA status */
704 for (i
= 0; i
< 4; i
++) {
705 status
= fpga_read_reg(priv
, i
, MMAP_REG_STATUS
);
706 if (!(status
& (CORL_DONE
| CORL_ERR
))) {
707 dev_err(priv
->dev
, "spurious irq detected (FPGA)\n");
712 /* detect spurious interrupts via raw IRQ pin readback */
713 status
= ioread32be(priv
->regs
+ SYS_IRQ_INPUT_DATA
);
714 if (status
& IRQ_CORL_DONE
) {
715 dev_err(priv
->dev
, "spurious irq detected (IRQ)\n");
719 spin_lock(&priv
->lock
);
722 * This is an error case that should never happen.
724 * If this driver has a bug and manages to re-enable interrupts while
725 * a DMA is in progress, then we will hit this statement and should
726 * start paying attention immediately.
728 BUG_ON(priv
->inflight
!= NULL
);
730 /* hide the interrupt by switching the IRQ driver to GPIO */
731 data_disable_interrupts(priv
);
733 /* If there are no free buffers, drop this data */
734 if (list_empty(&priv
->free
)) {
739 buf
= list_first_entry(&priv
->free
, struct data_buf
, entry
);
740 list_del_init(&buf
->entry
);
741 BUG_ON(buf
->size
!= priv
->bufsize
);
743 /* Submit a DMA transfer to get the correlation data */
744 if (data_submit_dma(priv
, buf
)) {
745 dev_err(priv
->dev
, "Unable to setup DMA transfer\n");
746 list_move_tail(&buf
->entry
, &priv
->free
);
750 /* Save the buffer for the DMA callback */
751 priv
->inflight
= buf
;
754 /* Start the DMA Engine */
755 dma_async_issue_pending(priv
->chan
);
758 /* If no DMA was submitted, re-enable interrupts */
760 data_enable_interrupts(priv
);
762 spin_unlock(&priv
->lock
);
767 * Realtime Device Enable Helpers
771 * data_device_enable() - enable the device for buffered dumping
772 * @priv: the driver's private data structure
774 * Enable the device for buffered dumping. Allocates buffers and hooks up
775 * the interrupt handler. When this finishes, data will come pouring in.
777 * LOCKING: must hold dev->mutex
778 * CONTEXT: user context only
780 * Returns 0 on success, -ERRNO otherwise
782 static int data_device_enable(struct fpga_device
*priv
)
788 /* multiple enables are safe: they do nothing */
789 spin_lock_irq(&priv
->lock
);
790 enabled
= priv
->enabled
;
791 spin_unlock_irq(&priv
->lock
);
795 /* check that the FPGAs are programmed */
796 val
= ioread32be(priv
->regs
+ SYS_FPGA_CONFIG_STATUS
);
797 if (!(val
& (1 << 18))) {
798 dev_err(priv
->dev
, "DATA-FPGAs are not enabled\n");
802 /* read the FPGAs to calculate the buffer size */
803 ret
= data_calculate_bufsize(priv
);
805 dev_err(priv
->dev
, "unable to calculate buffer size\n");
809 /* allocate the correlation data buffers */
810 ret
= data_alloc_buffers(priv
);
812 dev_err(priv
->dev
, "unable to allocate buffers\n");
816 /* setup the source scatterlist for dumping correlation data */
817 ret
= data_setup_corl_table(priv
);
819 dev_err(priv
->dev
, "unable to setup correlation DMA table\n");
823 /* prevent the FPGAs from generating interrupts */
824 data_disable_interrupts(priv
);
826 /* hookup the irq handler */
827 ret
= request_irq(priv
->irq
, data_irq
, IRQF_SHARED
, drv_name
, priv
);
829 dev_err(priv
->dev
, "unable to request IRQ handler\n");
833 /* allow the DMA callback to re-enable FPGA interrupts */
834 spin_lock_irq(&priv
->lock
);
835 priv
->enabled
= true;
836 spin_unlock_irq(&priv
->lock
);
838 /* allow the FPGAs to generate interrupts */
839 data_enable_interrupts(priv
);
843 sg_free_table(&priv
->corl_table
);
844 priv
->corl_nents
= 0;
846 data_free_buffers(priv
);
851 * data_device_disable() - disable the device for buffered dumping
852 * @priv: the driver's private data structure
854 * Disable the device for buffered dumping. Stops new DMA transactions from
855 * being generated, waits for all outstanding DMA to complete, and then frees
858 * LOCKING: must hold dev->mutex
861 * Returns 0 on success, -ERRNO otherwise
863 static int data_device_disable(struct fpga_device
*priv
)
865 spin_lock_irq(&priv
->lock
);
867 /* allow multiple disable */
868 if (!priv
->enabled
) {
869 spin_unlock_irq(&priv
->lock
);
874 * Mark the device disabled
876 * This stops DMA callbacks from re-enabling interrupts
878 priv
->enabled
= false;
880 /* prevent the FPGAs from generating interrupts */
881 data_disable_interrupts(priv
);
883 /* wait until all ongoing DMA has finished */
884 while (priv
->inflight
!= NULL
) {
885 spin_unlock_irq(&priv
->lock
);
886 wait_event(priv
->wait
, priv
->inflight
== NULL
);
887 spin_lock_irq(&priv
->lock
);
890 spin_unlock_irq(&priv
->lock
);
892 /* unhook the irq handler */
893 free_irq(priv
->irq
, priv
);
895 /* free the correlation table */
896 sg_free_table(&priv
->corl_table
);
897 priv
->corl_nents
= 0;
899 /* free all buffers: the free and used lists are not being changed */
900 data_free_buffers(priv
);
907 #ifdef CONFIG_DEBUG_FS
910 * Count the number of entries in the given list
912 static unsigned int list_num_entries(struct list_head
*list
)
914 struct list_head
*entry
;
915 unsigned int ret
= 0;
917 list_for_each(entry
, list
)
923 static int data_debug_show(struct seq_file
*f
, void *offset
)
925 struct fpga_device
*priv
= f
->private;
927 spin_lock_irq(&priv
->lock
);
929 seq_printf(f
, "enabled: %d\n", priv
->enabled
);
930 seq_printf(f
, "bufsize: %d\n", priv
->bufsize
);
931 seq_printf(f
, "num_buffers: %d\n", priv
->num_buffers
);
932 seq_printf(f
, "num_free: %d\n", list_num_entries(&priv
->free
));
933 seq_printf(f
, "inflight: %d\n", priv
->inflight
!= NULL
);
934 seq_printf(f
, "num_used: %d\n", list_num_entries(&priv
->used
));
935 seq_printf(f
, "num_dropped: %d\n", priv
->num_dropped
);
937 spin_unlock_irq(&priv
->lock
);
941 static int data_debug_open(struct inode
*inode
, struct file
*file
)
943 return single_open(file
, data_debug_show
, inode
->i_private
);
946 static const struct file_operations data_debug_fops
= {
947 .owner
= THIS_MODULE
,
948 .open
= data_debug_open
,
951 .release
= single_release
,
954 static int data_debugfs_init(struct fpga_device
*priv
)
956 priv
->dbg_entry
= debugfs_create_file(drv_name
, S_IRUGO
, NULL
, priv
,
958 if (IS_ERR(priv
->dbg_entry
))
959 return PTR_ERR(priv
->dbg_entry
);
964 static void data_debugfs_exit(struct fpga_device
*priv
)
966 debugfs_remove(priv
->dbg_entry
);
971 static inline int data_debugfs_init(struct fpga_device
*priv
)
976 static inline void data_debugfs_exit(struct fpga_device
*priv
)
980 #endif /* CONFIG_DEBUG_FS */
986 static ssize_t
data_en_show(struct device
*dev
, struct device_attribute
*attr
,
989 struct fpga_device
*priv
= dev_get_drvdata(dev
);
992 spin_lock_irq(&priv
->lock
);
993 ret
= snprintf(buf
, PAGE_SIZE
, "%u\n", priv
->enabled
);
994 spin_unlock_irq(&priv
->lock
);
999 static ssize_t
data_en_set(struct device
*dev
, struct device_attribute
*attr
,
1000 const char *buf
, size_t count
)
1002 struct fpga_device
*priv
= dev_get_drvdata(dev
);
1003 unsigned long enable
;
1006 ret
= kstrtoul(buf
, 0, &enable
);
1008 dev_err(priv
->dev
, "unable to parse enable input\n");
1012 /* protect against concurrent enable/disable */
1013 ret
= mutex_lock_interruptible(&priv
->mutex
);
1018 ret
= data_device_enable(priv
);
1020 ret
= data_device_disable(priv
);
1023 dev_err(priv
->dev
, "device %s failed\n",
1024 enable
? "enable" : "disable");
1030 mutex_unlock(&priv
->mutex
);
1034 static DEVICE_ATTR(enable
, S_IWUSR
| S_IRUGO
, data_en_show
, data_en_set
);
1036 static struct attribute
*data_sysfs_attrs
[] = {
1037 &dev_attr_enable
.attr
,
1041 static const struct attribute_group rt_sysfs_attr_group
= {
1042 .attrs
= data_sysfs_attrs
,
1046 * FPGA Realtime Data Character Device
1049 static int data_open(struct inode
*inode
, struct file
*filp
)
1052 * The miscdevice layer puts our struct miscdevice into the
1053 * filp->private_data field. We use this to find our private
1054 * data and then overwrite it with our own private structure.
1056 struct fpga_device
*priv
= container_of(filp
->private_data
,
1057 struct fpga_device
, miscdev
);
1058 struct fpga_reader
*reader
;
1061 /* allocate private data */
1062 reader
= kzalloc(sizeof(*reader
), GFP_KERNEL
);
1066 reader
->priv
= priv
;
1069 filp
->private_data
= reader
;
1070 ret
= nonseekable_open(inode
, filp
);
1072 dev_err(priv
->dev
, "nonseekable-open failed\n");
1078 * success, increase the reference count of the private data structure
1079 * so that it doesn't disappear if the device is unbound
1081 kref_get(&priv
->ref
);
1085 static int data_release(struct inode
*inode
, struct file
*filp
)
1087 struct fpga_reader
*reader
= filp
->private_data
;
1088 struct fpga_device
*priv
= reader
->priv
;
1090 /* free the per-reader structure */
1091 data_free_buffer(reader
->buf
);
1093 filp
->private_data
= NULL
;
1095 /* decrement our reference count to the private data */
1096 kref_put(&priv
->ref
, fpga_device_release
);
1100 static ssize_t
data_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
1103 struct fpga_reader
*reader
= filp
->private_data
;
1104 struct fpga_device
*priv
= reader
->priv
;
1105 struct list_head
*used
= &priv
->used
;
1106 bool drop_buffer
= false;
1107 struct data_buf
*dbuf
;
1112 /* check if we already have a partial buffer */
1118 spin_lock_irq(&priv
->lock
);
1120 /* Block until there is at least one buffer on the used list */
1121 while (list_empty(used
)) {
1122 spin_unlock_irq(&priv
->lock
);
1124 if (filp
->f_flags
& O_NONBLOCK
)
1127 ret
= wait_event_interruptible(priv
->wait
, !list_empty(used
));
1131 spin_lock_irq(&priv
->lock
);
1134 /* Grab the first buffer off of the used list */
1135 dbuf
= list_first_entry(used
, struct data_buf
, entry
);
1136 list_del_init(&dbuf
->entry
);
1138 spin_unlock_irq(&priv
->lock
);
1140 /* Buffers are always mapped: unmap it */
1141 videobuf_dma_unmap(priv
->dev
, &dbuf
->vb
);
1143 /* save the buffer for later */
1145 reader
->buf_start
= 0;
1148 /* Get the number of bytes available */
1149 avail
= dbuf
->size
- reader
->buf_start
;
1150 data
= dbuf
->vb
.vaddr
+ reader
->buf_start
;
1152 /* Get the number of bytes we can transfer */
1153 count
= min(count
, avail
);
1155 /* Copy the data to the userspace buffer */
1156 if (copy_to_user(ubuf
, data
, count
))
1159 /* Update the amount of available space */
1163 * If there is still some data available, save the buffer for the
1164 * next userspace call to read() and return
1167 reader
->buf_start
+= count
;
1173 * Get the buffer ready to be reused for DMA
1175 * If it fails, we pretend that the read never happed and return
1176 * -EFAULT to userspace. The read will be retried.
1178 ret
= videobuf_dma_map(priv
->dev
, &dbuf
->vb
);
1180 dev_err(priv
->dev
, "unable to remap buffer for DMA\n");
1184 /* Lock against concurrent enable/disable */
1185 spin_lock_irq(&priv
->lock
);
1187 /* the reader is finished with this buffer */
1191 * One of two things has happened, the device is disabled, or the
1192 * device has been reconfigured underneath us. In either case, we
1193 * should just throw away the buffer.
1195 * Lockdep complains if this is done under the spinlock, so we
1196 * handle it during the unlock path.
1198 if (!priv
->enabled
|| dbuf
->size
!= priv
->bufsize
) {
1203 /* The buffer is safe to reuse, so add it back to the free list */
1204 list_add_tail(&dbuf
->entry
, &priv
->free
);
1207 spin_unlock_irq(&priv
->lock
);
1210 videobuf_dma_unmap(priv
->dev
, &dbuf
->vb
);
1211 data_free_buffer(dbuf
);
1217 static unsigned int data_poll(struct file
*filp
, struct poll_table_struct
*tbl
)
1219 struct fpga_reader
*reader
= filp
->private_data
;
1220 struct fpga_device
*priv
= reader
->priv
;
1221 unsigned int mask
= 0;
1223 poll_wait(filp
, &priv
->wait
, tbl
);
1225 if (!list_empty(&priv
->used
))
1226 mask
|= POLLIN
| POLLRDNORM
;
1231 static int data_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1233 struct fpga_reader
*reader
= filp
->private_data
;
1234 struct fpga_device
*priv
= reader
->priv
;
1235 unsigned long offset
, vsize
, psize
, addr
;
1237 /* VMA properties */
1238 offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1239 vsize
= vma
->vm_end
- vma
->vm_start
;
1240 psize
= priv
->phys_size
- offset
;
1241 addr
= (priv
->phys_addr
+ offset
) >> PAGE_SHIFT
;
1243 /* Check against the FPGA region's physical memory size */
1244 if (vsize
> psize
) {
1245 dev_err(priv
->dev
, "requested mmap mapping too large\n");
1249 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1251 return io_remap_pfn_range(vma
, vma
->vm_start
, addr
, vsize
,
1255 static const struct file_operations data_fops
= {
1256 .owner
= THIS_MODULE
,
1258 .release
= data_release
,
1262 .llseek
= no_llseek
,
1266 * OpenFirmware Device Subsystem
1269 static bool dma_filter(struct dma_chan
*chan
, void *data
)
1272 * DMA Channel #0 is used for the FPGA Programmer, so ignore it
1274 * This probably won't survive an unload/load cycle of the Freescale
1275 * DMAEngine driver, but that won't be a problem
1277 if (chan
->chan_id
== 0 && chan
->device
->dev_id
== 0)
1283 static int data_of_probe(struct platform_device
*op
)
1285 struct device_node
*of_node
= op
->dev
.of_node
;
1286 struct device
*this_device
;
1287 struct fpga_device
*priv
;
1288 struct resource res
;
1289 dma_cap_mask_t mask
;
1292 /* Allocate private data */
1293 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1295 dev_err(&op
->dev
, "Unable to allocate device private data\n");
1300 platform_set_drvdata(op
, priv
);
1301 priv
->dev
= &op
->dev
;
1302 kref_init(&priv
->ref
);
1303 mutex_init(&priv
->mutex
);
1305 dev_set_drvdata(priv
->dev
, priv
);
1306 spin_lock_init(&priv
->lock
);
1307 INIT_LIST_HEAD(&priv
->free
);
1308 INIT_LIST_HEAD(&priv
->used
);
1309 init_waitqueue_head(&priv
->wait
);
1311 /* Setup the misc device */
1312 priv
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
1313 priv
->miscdev
.name
= drv_name
;
1314 priv
->miscdev
.fops
= &data_fops
;
1316 /* Get the physical address of the FPGA registers */
1317 ret
= of_address_to_resource(of_node
, 0, &res
);
1319 dev_err(&op
->dev
, "Unable to find FPGA physical address\n");
1324 priv
->phys_addr
= res
.start
;
1325 priv
->phys_size
= resource_size(&res
);
1327 /* ioremap the registers for use */
1328 priv
->regs
= of_iomap(of_node
, 0);
1330 dev_err(&op
->dev
, "Unable to ioremap registers\n");
1336 dma_cap_set(DMA_MEMCPY
, mask
);
1337 dma_cap_set(DMA_INTERRUPT
, mask
);
1338 dma_cap_set(DMA_SLAVE
, mask
);
1339 dma_cap_set(DMA_SG
, mask
);
1341 /* Request a DMA channel */
1342 priv
->chan
= dma_request_channel(mask
, dma_filter
, NULL
);
1344 dev_err(&op
->dev
, "Unable to request DMA channel\n");
1346 goto out_unmap_regs
;
1349 /* Find the correct IRQ number */
1350 priv
->irq
= irq_of_parse_and_map(of_node
, 0);
1351 if (priv
->irq
== NO_IRQ
) {
1352 dev_err(&op
->dev
, "Unable to find IRQ line\n");
1354 goto out_release_dma
;
1357 /* Drive the GPIO for FPGA IRQ high (no interrupt) */
1358 iowrite32be(IRQ_CORL_DONE
, priv
->regs
+ SYS_IRQ_OUTPUT_DATA
);
1360 /* Register the miscdevice */
1361 ret
= misc_register(&priv
->miscdev
);
1363 dev_err(&op
->dev
, "Unable to register miscdevice\n");
1364 goto out_irq_dispose_mapping
;
1367 /* Create the debugfs files */
1368 ret
= data_debugfs_init(priv
);
1370 dev_err(&op
->dev
, "Unable to create debugfs files\n");
1371 goto out_misc_deregister
;
1374 /* Create the sysfs files */
1375 this_device
= priv
->miscdev
.this_device
;
1376 dev_set_drvdata(this_device
, priv
);
1377 ret
= sysfs_create_group(&this_device
->kobj
, &rt_sysfs_attr_group
);
1379 dev_err(&op
->dev
, "Unable to create sysfs files\n");
1380 goto out_data_debugfs_exit
;
1383 dev_info(&op
->dev
, "CARMA FPGA Realtime Data Driver Loaded\n");
1386 out_data_debugfs_exit
:
1387 data_debugfs_exit(priv
);
1388 out_misc_deregister
:
1389 misc_deregister(&priv
->miscdev
);
1390 out_irq_dispose_mapping
:
1391 irq_dispose_mapping(priv
->irq
);
1393 dma_release_channel(priv
->chan
);
1395 iounmap(priv
->regs
);
1397 kref_put(&priv
->ref
, fpga_device_release
);
1402 static int data_of_remove(struct platform_device
*op
)
1404 struct fpga_device
*priv
= platform_get_drvdata(op
);
1405 struct device
*this_device
= priv
->miscdev
.this_device
;
1407 /* remove all sysfs files, now the device cannot be re-enabled */
1408 sysfs_remove_group(&this_device
->kobj
, &rt_sysfs_attr_group
);
1410 /* remove all debugfs files */
1411 data_debugfs_exit(priv
);
1413 /* disable the device from generating data */
1414 data_device_disable(priv
);
1416 /* remove the character device to stop new readers from appearing */
1417 misc_deregister(&priv
->miscdev
);
1419 /* cleanup everything not needed by readers */
1420 irq_dispose_mapping(priv
->irq
);
1421 dma_release_channel(priv
->chan
);
1422 iounmap(priv
->regs
);
1424 /* release our reference */
1425 kref_put(&priv
->ref
, fpga_device_release
);
1429 static struct of_device_id data_of_match
[] = {
1430 { .compatible
= "carma,carma-fpga", },
1434 static struct platform_driver data_of_driver
= {
1435 .probe
= data_of_probe
,
1436 .remove
= data_of_remove
,
1439 .of_match_table
= data_of_match
,
1440 .owner
= THIS_MODULE
,
1444 module_platform_driver(data_of_driver
);
1446 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
1447 MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
1448 MODULE_LICENSE("GPL");