99 #include <linux/kernel.h>
100 #include <linux/module.h>
101 #include <linux/poll.h>
103 #include <linux/slab.h>
110 #define SYS_IRQ_SOURCE_CTL 0x24
111 #define SYS_IRQ_OUTPUT_EN 0x28
112 #define SYS_IRQ_OUTPUT_DATA 0x2C
113 #define SYS_IRQ_INPUT_DATA 0x30
114 #define SYS_FPGA_CONFIG_STATUS 0x44
117 #define IRQ_CORL_DONE 0x10
120 #define MMAP_REG_VERSION 0x00
121 #define MMAP_REG_CORL_CONF1 0x08
122 #define MMAP_REG_CORL_CONF2 0x0C
123 #define MMAP_REG_STATUS 0x48
125 #define SYS_FPGA_BLOCK 0xF0000000
127 #define DATA_FPGA_START 0x400000
128 #define DATA_FPGA_SIZE 0x80000
130 static const char drv_name[] =
"carma-fpga";
134 #define MIN_DATA_BUFS 8
135 #define MAX_DATA_BUFS 64
196 static void fpga_device_release(
struct kref *ref)
236 static struct data_buf *data_alloc_buffer(
const size_t bytes)
238 unsigned int nr_pages;
251 INIT_LIST_HEAD(&buf->
entry);
286 list_del_init(&buf->
entry);
288 data_free_buffer(buf);
292 list_del_init(&buf->
entry);
294 data_free_buffer(buf);
327 buf = data_alloc_buffer(priv->
bufsize);
334 data_free_buffer(buf);
345 dev_err(priv->
dev,
"Unable to allocate enough data buffers\n");
346 data_free_buffers(priv);
353 "Unable to allocate %d buffers, using %d buffers instead\n",
383 return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
386 #define REG_BLOCK_SIZE (32 * 4)
398 static int data_setup_corl_table(
struct fpga_device *priv)
413 dev_err(priv->
dev,
"unable to allocate DMA table\n");
432 info = &priv->
info[
i];
451 static void fpga_write_reg(
struct fpga_device *priv,
unsigned int fpga,
476 static int data_calculate_bufsize(
struct fpga_device *priv)
478 u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
493 if ((version & 0x000000FF) >= 2) {
494 num_corl = (conf1 & 0x000000F0) >> 4;
495 num_pack = (conf1 & 0x00000F00) >> 8;
496 num_lags = (conf1 & 0x00FFF000) >> 12;
497 num_meta = (conf1 & 0x7F000000) >> 24;
498 num_qcnt = (conf2 & 0x00000FFF) >> 0;
500 num_corl = (conf1 & 0x000000F0) >> 4;
502 num_lags = (conf1 & 0x000FFF00) >> 8;
503 num_meta = (conf1 & 0x7FF00000) >> 20;
504 num_qcnt = (conf2 & 0x00000FFF) >> 0;
507 num_lag_ram = (num_corl + num_pack - 1) / num_pack;
508 blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
514 dev_dbg(priv->
dev,
"FPGA %d NUM_CORL: %d\n", i, num_corl);
515 dev_dbg(priv->
dev,
"FPGA %d NUM_PACK: %d\n", i, num_pack);
516 dev_dbg(priv->
dev,
"FPGA %d NUM_LAGS: %d\n", i, num_lags);
517 dev_dbg(priv->
dev,
"FPGA %d NUM_META: %d\n", i, num_meta);
518 dev_dbg(priv->
dev,
"FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
519 dev_dbg(priv->
dev,
"FPGA %d BLK_SIZE: %d\n", i, blk_size);
538 static void data_disable_interrupts(
struct fpga_device *priv)
553 static void data_enable_interrupts(
struct fpga_device *priv)
583 static void data_dma_cb(
void *
data)
602 data_enable_interrupts(priv);
604 spin_unlock_irqrestore(&priv->
lock, flags);
629 unsigned int dst_nents, src_nents;
635 dst_sg = buf->
vb.sglist;
636 dst_nents = buf->
vb.sglen;
648 tx = chan->
device->device_prep_dma_sg(chan,
653 dev_err(priv->
dev,
"unable to prep scatterlist DMA\n");
660 dev_err(priv->
dev,
"unable to submit scatterlist DMA\n");
667 tx = chan->
device->device_prep_dma_memcpy(chan, dst, src,
671 dev_err(priv->
dev,
"unable to prep SYS-FPGA DMA\n");
682 dev_err(priv->
dev,
"unable to submit SYS-FPGA DMA\n");
689 #define CORL_DONE 0x1
695 bool submitted =
false;
701 for (i = 0; i < 4; i++) {
704 dev_err(priv->
dev,
"spurious irq detected (FPGA)\n");
712 dev_err(priv->
dev,
"spurious irq detected (IRQ)\n");
716 spin_lock(&priv->
lock);
728 data_disable_interrupts(priv);
731 if (list_empty(&priv->
free)) {
737 list_del_init(&buf->
entry);
741 if (data_submit_dma(priv, buf)) {
742 dev_err(priv->
dev,
"Unable to setup DMA transfer\n");
743 list_move_tail(&buf->
entry, &priv->
free);
757 data_enable_interrupts(priv);
759 spin_unlock(&priv->
lock);
779 static int data_device_enable(
struct fpga_device *priv)
786 spin_lock_irq(&priv->
lock);
788 spin_unlock_irq(&priv->
lock);
794 if (!(val & (1 << 18))) {
795 dev_err(priv->
dev,
"DATA-FPGAs are not enabled\n");
800 ret = data_calculate_bufsize(priv);
802 dev_err(priv->
dev,
"unable to calculate buffer size\n");
807 ret = data_alloc_buffers(priv);
809 dev_err(priv->
dev,
"unable to allocate buffers\n");
814 ret = data_setup_corl_table(priv);
816 dev_err(priv->
dev,
"unable to setup correlation DMA table\n");
821 data_disable_interrupts(priv);
826 dev_err(priv->
dev,
"unable to request IRQ handler\n");
831 spin_lock_irq(&priv->
lock);
833 spin_unlock_irq(&priv->
lock);
836 data_enable_interrupts(priv);
843 data_free_buffers(priv);
860 static int data_device_disable(
struct fpga_device *priv)
862 spin_lock_irq(&priv->
lock);
866 spin_unlock_irq(&priv->
lock);
878 data_disable_interrupts(priv);
882 spin_unlock_irq(&priv->
lock);
884 spin_lock_irq(&priv->
lock);
887 spin_unlock_irq(&priv->
lock);
897 data_free_buffers(priv);
904 #ifdef CONFIG_DEBUG_FS
912 unsigned int ret = 0;
924 spin_lock_irq(&priv->
lock);
934 spin_unlock_irq(&priv->
lock);
945 .open = data_debug_open,
951 static int data_debugfs_init(
struct fpga_device *priv)
961 static void data_debugfs_exit(
struct fpga_device *priv)
968 static inline int data_debugfs_init(
struct fpga_device *priv)
973 static inline void data_debugfs_exit(
struct fpga_device *priv)
989 spin_lock_irq(&priv->
lock);
991 spin_unlock_irq(&priv->
lock);
997 const char *buf,
size_t count)
1005 dev_err(priv->
dev,
"unable to parse enable input\n");
1015 ret = data_device_enable(priv);
1017 ret = data_device_disable(priv);
1021 enable ?
"enable" :
"disable");
1033 static struct attribute *data_sysfs_attrs[] = {
1034 &dev_attr_enable.attr,
1039 .attrs = data_sysfs_attrs,
1059 reader = kzalloc(
sizeof(*reader),
GFP_KERNEL);
1069 dev_err(priv->
dev,
"nonseekable-open failed\n");
1078 kref_get(&priv->
ref);
1082 static int data_release(
struct inode *inode,
struct file *filp)
1088 data_free_buffer(reader->
buf);
1093 kref_put(&priv->
ref, fpga_device_release);
1097 static ssize_t data_read(
struct file *filp,
char __user *ubuf,
size_t count,
1103 bool drop_buffer =
false;
1115 spin_lock_irq(&priv->
lock);
1118 while (list_empty(used)) {
1119 spin_unlock_irq(&priv->
lock);
1128 spin_lock_irq(&priv->
lock);
1133 list_del_init(&dbuf->
entry);
1135 spin_unlock_irq(&priv->
lock);
1150 count =
min(count, avail);
1177 dev_err(priv->
dev,
"unable to remap buffer for DMA\n");
1182 spin_lock_irq(&priv->
lock);
1204 spin_unlock_irq(&priv->
lock);
1208 data_free_buffer(dbuf);
1218 unsigned int mask = 0;
1220 poll_wait(filp, &priv->
wait, tbl);
1222 if (!list_empty(&priv->
used))
1241 if (vsize > psize) {
1242 dev_err(priv->
dev,
"requested mmap mapping too large\n");
1255 .release = data_release,
1266 static bool dma_filter(
struct dma_chan *chan,
void *data)
1283 struct device *this_device;
1292 dev_err(&op->
dev,
"Unable to allocate device private data\n");
1299 kref_init(&priv->
ref);
1304 INIT_LIST_HEAD(&priv->
free);
1305 INIT_LIST_HEAD(&priv->
used);
1310 priv->
miscdev.name = drv_name;
1311 priv->
miscdev.fops = &data_fops;
1316 dev_err(&op->
dev,
"Unable to find FPGA physical address\n");
1327 dev_err(&op->
dev,
"Unable to ioremap registers\n");
1341 dev_err(&op->
dev,
"Unable to request DMA channel\n");
1343 goto out_unmap_regs;
1349 dev_err(&op->
dev,
"Unable to find IRQ line\n");
1351 goto out_release_dma;
1360 dev_err(&op->
dev,
"Unable to register miscdevice\n");
1361 goto out_irq_dispose_mapping;
1365 ret = data_debugfs_init(priv);
1367 dev_err(&op->
dev,
"Unable to create debugfs files\n");
1368 goto out_misc_deregister;
1372 this_device = priv->
miscdev.this_device;
1376 dev_err(&op->
dev,
"Unable to create sysfs files\n");
1377 goto out_data_debugfs_exit;
1380 dev_info(&op->
dev,
"CARMA FPGA Realtime Data Driver Loaded\n");
1383 out_data_debugfs_exit:
1384 data_debugfs_exit(priv);
1385 out_misc_deregister:
1387 out_irq_dispose_mapping:
1394 kref_put(&priv->
ref, fpga_device_release);
1408 data_debugfs_exit(priv);
1411 data_device_disable(priv);
1422 kref_put(&priv->
ref, fpga_device_release);
1427 { .compatible =
"carma,carma-fpga", },
1432 .probe = data_of_probe,
1433 .remove = data_of_remove,
1436 .of_match_table = data_of_match,