blob: 55e913b7eb114610aa61f6eb1b481daf7a868de9 [file] [log] [blame]
Ira Snyderc186f0e2011-02-11 13:34:29 +00001/*
2 * CARMA DATA-FPGA Access Driver
3 *
4 * Copyright (c) 2009-2011 Ira W. Snyder <iws@ovro.caltech.edu>
5 *
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.
10 */
11
12/*
13 * FPGA Memory Dump Format
14 *
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)
24 *
25 * Each correlation array consists of:
26 *
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)
30 *
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.
34 */
35
36/*
37 * Basic Description:
38 *
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
42 * remains open.
43 *
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.
47 *
48 * There are two lists and one pointer which are used to keep track of the
49 * different states of data buffers.
50 *
51 * 1) free list
52 * This list holds all empty data buffers which are ready to receive data.
53 *
54 * 2) inflight pointer
55 * This pointer holds the currently inflight data buffer. This buffer is having
56 * data copied into it by the DMA engine.
57 *
58 * 3) used list
59 * This list holds data buffers which have been filled, and are waiting to be
60 * read by userspace.
61 *
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
65 * as necessary.
66 *
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.
70 */
71
72/*
73 * Notes on the IRQ masking scheme:
74 *
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.
79 *
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.
84 *
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. :)
89 */
90
Rob Herring5af50732013-09-17 14:28:33 -050091#include <linux/of_address.h>
92#include <linux/of_irq.h>
Ira Snyderc186f0e2011-02-11 13:34:29 +000093#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>
Ira Snyderc186f0e2011-02-11 13:34:29 +0000104#include <linux/slab.h>
105#include <linux/kref.h>
106#include <linux/io.h>
107
108#include <media/videobuf-dma-sg.h>
109
110/* system controller registers */
111#define SYS_IRQ_SOURCE_CTL 0x24
112#define SYS_IRQ_OUTPUT_EN 0x28
113#define SYS_IRQ_OUTPUT_DATA 0x2C
114#define SYS_IRQ_INPUT_DATA 0x30
115#define SYS_FPGA_CONFIG_STATUS 0x44
116
117/* GPIO IRQ line assignment */
118#define IRQ_CORL_DONE 0x10
119
120/* FPGA registers */
121#define MMAP_REG_VERSION 0x00
122#define MMAP_REG_CORL_CONF1 0x08
123#define MMAP_REG_CORL_CONF2 0x0C
124#define MMAP_REG_STATUS 0x48
125
126#define SYS_FPGA_BLOCK 0xF0000000
127
128#define DATA_FPGA_START 0x400000
129#define DATA_FPGA_SIZE 0x80000
130
131static const char drv_name[] = "carma-fpga";
132
133#define NUM_FPGA 4
134
135#define MIN_DATA_BUFS 8
136#define MAX_DATA_BUFS 64
137
138struct fpga_info {
139 unsigned int num_lag_ram;
140 unsigned int blk_size;
141};
142
143struct data_buf {
144 struct list_head entry;
145 struct videobuf_dmabuf vb;
146 size_t size;
147};
148
149struct fpga_device {
150 /* character device */
151 struct miscdevice miscdev;
152 struct device *dev;
153 struct mutex mutex;
154
155 /* reference count */
156 struct kref ref;
157
158 /* FPGA registers and information */
159 struct fpga_info info[NUM_FPGA];
160 void __iomem *regs;
161 int irq;
162
163 /* FPGA Physical Address/Size Information */
164 resource_size_t phys_addr;
165 size_t phys_size;
166
167 /* DMA structures */
168 struct sg_table corl_table;
169 unsigned int corl_nents;
170 struct dma_chan *chan;
171
172 /* Protection for all members below */
173 spinlock_t lock;
174
175 /* Device enable/disable flag */
176 bool enabled;
177
178 /* Correlation data buffers */
179 wait_queue_head_t wait;
180 struct list_head free;
181 struct list_head used;
182 struct data_buf *inflight;
183
184 /* Information about data buffers */
185 unsigned int num_dropped;
186 unsigned int num_buffers;
187 size_t bufsize;
188 struct dentry *dbg_entry;
189};
190
191struct fpga_reader {
192 struct fpga_device *priv;
193 struct data_buf *buf;
194 off_t buf_start;
195};
196
197static void fpga_device_release(struct kref *ref)
198{
199 struct fpga_device *priv = container_of(ref, struct fpga_device, ref);
200
201 /* the last reader has exited, cleanup the last bits */
202 mutex_destroy(&priv->mutex);
203 kfree(priv);
204}
205
206/*
207 * Data Buffer Allocation Helpers
208 */
209
210/**
211 * data_free_buffer() - free a single data buffer and all allocated memory
212 * @buf: the buffer to free
213 *
214 * This will free all of the pages allocated to the given data buffer, and
215 * then free the structure itself
216 */
217static void data_free_buffer(struct data_buf *buf)
218{
219 /* It is ok to free a NULL buffer */
220 if (!buf)
221 return;
222
223 /* free all memory */
224 videobuf_dma_free(&buf->vb);
225 kfree(buf);
226}
227
228/**
229 * data_alloc_buffer() - allocate and fill a data buffer with pages
230 * @bytes: the number of bytes required
231 *
232 * This allocates all space needed for a data buffer. It must be mapped before
233 * use in a DMA transaction using videobuf_dma_map().
234 *
235 * Returns NULL on failure
236 */
237static struct data_buf *data_alloc_buffer(const size_t bytes)
238{
239 unsigned int nr_pages;
240 struct data_buf *buf;
241 int ret;
242
243 /* calculate the number of pages necessary */
244 nr_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
245
246 /* allocate the buffer structure */
247 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
248 if (!buf)
249 goto out_return;
250
251 /* initialize internal fields */
252 INIT_LIST_HEAD(&buf->entry);
253 buf->size = bytes;
254
255 /* allocate the videobuf */
256 videobuf_dma_init(&buf->vb);
257 ret = videobuf_dma_init_kernel(&buf->vb, DMA_FROM_DEVICE, nr_pages);
258 if (ret)
259 goto out_free_buf;
260
261 return buf;
262
263out_free_buf:
264 kfree(buf);
265out_return:
266 return NULL;
267}
268
269/**
270 * data_free_buffers() - free all allocated buffers
271 * @priv: the driver's private data structure
272 *
273 * Free all buffers allocated by the driver (except those currently in the
274 * process of being read by userspace).
275 *
276 * LOCKING: must hold dev->mutex
277 * CONTEXT: user
278 */
279static void data_free_buffers(struct fpga_device *priv)
280{
281 struct data_buf *buf, *tmp;
282
283 /* the device should be stopped, no DMA in progress */
284 BUG_ON(priv->inflight != NULL);
285
286 list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
287 list_del_init(&buf->entry);
288 videobuf_dma_unmap(priv->dev, &buf->vb);
289 data_free_buffer(buf);
290 }
291
292 list_for_each_entry_safe(buf, tmp, &priv->used, entry) {
293 list_del_init(&buf->entry);
294 videobuf_dma_unmap(priv->dev, &buf->vb);
295 data_free_buffer(buf);
296 }
297
298 priv->num_buffers = 0;
299 priv->bufsize = 0;
300}
301
302/**
303 * data_alloc_buffers() - allocate 1 seconds worth of data buffers
304 * @priv: the driver's private data structure
305 *
306 * Allocate enough buffers for a whole second worth of data
307 *
308 * This routine will attempt to degrade nicely by succeeding even if a full
309 * second worth of data buffers could not be allocated, as long as a minimum
310 * number were allocated. In this case, it will print a message to the kernel
311 * log.
312 *
313 * The device must not be modifying any lists when this is called.
314 *
315 * CONTEXT: user
316 * LOCKING: must hold dev->mutex
317 *
318 * Returns 0 on success, -ERRNO otherwise
319 */
320static int data_alloc_buffers(struct fpga_device *priv)
321{
322 struct data_buf *buf;
323 int i, ret;
324
325 for (i = 0; i < MAX_DATA_BUFS; i++) {
326
327 /* allocate a buffer */
328 buf = data_alloc_buffer(priv->bufsize);
329 if (!buf)
330 break;
331
332 /* map it for DMA */
333 ret = videobuf_dma_map(priv->dev, &buf->vb);
334 if (ret) {
335 data_free_buffer(buf);
336 break;
337 }
338
339 /* add it to the list of free buffers */
340 list_add_tail(&buf->entry, &priv->free);
341 priv->num_buffers++;
342 }
343
344 /* Make sure we allocated the minimum required number of buffers */
345 if (priv->num_buffers < MIN_DATA_BUFS) {
346 dev_err(priv->dev, "Unable to allocate enough data buffers\n");
347 data_free_buffers(priv);
348 return -ENOMEM;
349 }
350
351 /* Warn if we are running in a degraded state, but do not fail */
352 if (priv->num_buffers < MAX_DATA_BUFS) {
353 dev_warn(priv->dev,
354 "Unable to allocate %d buffers, using %d buffers instead\n",
355 MAX_DATA_BUFS, i);
356 }
357
358 return 0;
359}
360
361/*
362 * DMA Operations Helpers
363 */
364
365/**
366 * fpga_start_addr() - get the physical address a DATA-FPGA
367 * @priv: the driver's private data structure
368 * @fpga: the DATA-FPGA number (zero based)
369 */
370static dma_addr_t fpga_start_addr(struct fpga_device *priv, unsigned int fpga)
371{
372 return priv->phys_addr + 0x400000 + (0x80000 * fpga);
373}
374
375/**
376 * fpga_block_addr() - get the physical address of a correlation data block
377 * @priv: the driver's private data structure
378 * @fpga: the DATA-FPGA number (zero based)
379 * @blknum: the correlation block number (zero based)
380 */
381static dma_addr_t fpga_block_addr(struct fpga_device *priv, unsigned int fpga,
382 unsigned int blknum)
383{
384 return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
385}
386
387#define REG_BLOCK_SIZE (32 * 4)
388
389/**
390 * data_setup_corl_table() - create the scatterlist for correlation dumps
391 * @priv: the driver's private data structure
392 *
393 * Create the scatterlist for transferring a correlation dump from the
394 * DATA FPGAs. This structure will be reused for each buffer than needs
395 * to be filled with correlation data.
396 *
397 * Returns 0 on success, -ERRNO otherwise
398 */
399static int data_setup_corl_table(struct fpga_device *priv)
400{
401 struct sg_table *table = &priv->corl_table;
402 struct scatterlist *sg;
403 struct fpga_info *info;
404 int i, j, ret;
405
406 /* Calculate the number of entries needed */
407 priv->corl_nents = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
408 for (i = 0; i < NUM_FPGA; i++)
409 priv->corl_nents += priv->info[i].num_lag_ram;
410
411 /* Allocate the scatterlist table */
412 ret = sg_alloc_table(table, priv->corl_nents, GFP_KERNEL);
413 if (ret) {
414 dev_err(priv->dev, "unable to allocate DMA table\n");
415 return ret;
416 }
417
418 /* Add the DATA FPGA registers to the scatterlist */
419 sg = table->sgl;
420 for (i = 0; i < NUM_FPGA; i++) {
421 sg_dma_address(sg) = fpga_start_addr(priv, i);
422 sg_dma_len(sg) = REG_BLOCK_SIZE;
423 sg = sg_next(sg);
424 }
425
426 /* Add the SYS-FPGA registers to the scatterlist */
427 sg_dma_address(sg) = SYS_FPGA_BLOCK;
428 sg_dma_len(sg) = REG_BLOCK_SIZE;
429 sg = sg_next(sg);
430
431 /* Add the FPGA correlation data blocks to the scatterlist */
432 for (i = 0; i < NUM_FPGA; i++) {
433 info = &priv->info[i];
434 for (j = 0; j < info->num_lag_ram; j++) {
435 sg_dma_address(sg) = fpga_block_addr(priv, i, j);
436 sg_dma_len(sg) = info->blk_size;
437 sg = sg_next(sg);
438 }
439 }
440
441 /*
442 * All physical addresses and lengths are present in the structure
443 * now. It can be reused for every FPGA DATA interrupt
444 */
445 return 0;
446}
447
448/*
449 * FPGA Register Access Helpers
450 */
451
452static void fpga_write_reg(struct fpga_device *priv, unsigned int fpga,
453 unsigned int reg, u32 val)
454{
455 const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
456 iowrite32be(val, priv->regs + fpga_start + reg);
457}
458
459static u32 fpga_read_reg(struct fpga_device *priv, unsigned int fpga,
460 unsigned int reg)
461{
462 const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
463 return ioread32be(priv->regs + fpga_start + reg);
464}
465
466/**
467 * data_calculate_bufsize() - calculate the data buffer size required
468 * @priv: the driver's private data structure
469 *
470 * Calculate the total buffer size needed to hold a single block
471 * of correlation data
472 *
473 * CONTEXT: user
474 *
475 * Returns 0 on success, -ERRNO otherwise
476 */
477static int data_calculate_bufsize(struct fpga_device *priv)
478{
479 u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
480 u32 conf1, conf2, version;
481 u32 num_lag_ram, blk_size;
482 int i;
483
484 /* Each buffer starts with the 5 FPGA register areas */
485 priv->bufsize = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
486
487 /* Read and store the configuration data for each FPGA */
488 for (i = 0; i < NUM_FPGA; i++) {
489 version = fpga_read_reg(priv, i, MMAP_REG_VERSION);
490 conf1 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF1);
491 conf2 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF2);
492
493 /* minor version 2 and later */
494 if ((version & 0x000000FF) >= 2) {
495 num_corl = (conf1 & 0x000000F0) >> 4;
496 num_pack = (conf1 & 0x00000F00) >> 8;
497 num_lags = (conf1 & 0x00FFF000) >> 12;
498 num_meta = (conf1 & 0x7F000000) >> 24;
499 num_qcnt = (conf2 & 0x00000FFF) >> 0;
500 } else {
501 num_corl = (conf1 & 0x000000F0) >> 4;
502 num_pack = 1; /* implied */
503 num_lags = (conf1 & 0x000FFF00) >> 8;
504 num_meta = (conf1 & 0x7FF00000) >> 20;
505 num_qcnt = (conf2 & 0x00000FFF) >> 0;
506 }
507
508 num_lag_ram = (num_corl + num_pack - 1) / num_pack;
509 blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
510
511 priv->info[i].num_lag_ram = num_lag_ram;
512 priv->info[i].blk_size = blk_size;
513 priv->bufsize += num_lag_ram * blk_size;
514
515 dev_dbg(priv->dev, "FPGA %d NUM_CORL: %d\n", i, num_corl);
516 dev_dbg(priv->dev, "FPGA %d NUM_PACK: %d\n", i, num_pack);
517 dev_dbg(priv->dev, "FPGA %d NUM_LAGS: %d\n", i, num_lags);
518 dev_dbg(priv->dev, "FPGA %d NUM_META: %d\n", i, num_meta);
519 dev_dbg(priv->dev, "FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
520 dev_dbg(priv->dev, "FPGA %d BLK_SIZE: %d\n", i, blk_size);
521 }
522
523 dev_dbg(priv->dev, "TOTAL BUFFER SIZE: %zu bytes\n", priv->bufsize);
524 return 0;
525}
526
527/*
528 * Interrupt Handling
529 */
530
531/**
532 * data_disable_interrupts() - stop the device from generating interrupts
533 * @priv: the driver's private data structure
534 *
535 * Hide interrupts by switching to GPIO interrupt source
536 *
537 * LOCKING: must hold dev->lock
538 */
539static void data_disable_interrupts(struct fpga_device *priv)
540{
541 /* hide the interrupt by switching the IRQ driver to GPIO */
542 iowrite32be(0x2F, priv->regs + SYS_IRQ_SOURCE_CTL);
543}
544
545/**
546 * data_enable_interrupts() - allow the device to generate interrupts
547 * @priv: the driver's private data structure
548 *
549 * Unhide interrupts by switching to the FPGA interrupt source. At the
550 * same time, clear the DATA-FPGA status registers.
551 *
552 * LOCKING: must hold dev->lock
553 */
554static void data_enable_interrupts(struct fpga_device *priv)
555{
556 /* clear the actual FPGA corl_done interrupt */
557 fpga_write_reg(priv, 0, MMAP_REG_STATUS, 0x0);
558 fpga_write_reg(priv, 1, MMAP_REG_STATUS, 0x0);
559 fpga_write_reg(priv, 2, MMAP_REG_STATUS, 0x0);
560 fpga_write_reg(priv, 3, MMAP_REG_STATUS, 0x0);
561
562 /* flush the writes */
563 fpga_read_reg(priv, 0, MMAP_REG_STATUS);
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000564 fpga_read_reg(priv, 1, MMAP_REG_STATUS);
565 fpga_read_reg(priv, 2, MMAP_REG_STATUS);
566 fpga_read_reg(priv, 3, MMAP_REG_STATUS);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000567
568 /* switch back to the external interrupt source */
569 iowrite32be(0x3F, priv->regs + SYS_IRQ_SOURCE_CTL);
570}
571
572/**
573 * data_dma_cb() - DMAEngine callback for DMA completion
574 * @data: the driver's private data structure
575 *
576 * Complete a DMA transfer from the DATA-FPGA's
577 *
578 * This is called via the DMA callback mechanism, and will handle moving the
579 * completed DMA transaction to the used list, and then wake any processes
580 * waiting for new data
581 *
582 * CONTEXT: any, softirq expected
583 */
584static void data_dma_cb(void *data)
585{
586 struct fpga_device *priv = data;
587 unsigned long flags;
588
589 spin_lock_irqsave(&priv->lock, flags);
590
591 /* If there is no inflight buffer, we've got a bug */
592 BUG_ON(priv->inflight == NULL);
593
594 /* Move the inflight buffer onto the used list */
595 list_move_tail(&priv->inflight->entry, &priv->used);
596 priv->inflight = NULL;
597
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000598 /*
599 * If data dumping is still enabled, then clear the FPGA
600 * status registers and re-enable FPGA interrupts
601 */
602 if (priv->enabled)
603 data_enable_interrupts(priv);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000604
605 spin_unlock_irqrestore(&priv->lock, flags);
606
607 /*
608 * We've changed both the inflight and used lists, so we need
609 * to wake up any processes that are blocking for those events
610 */
611 wake_up(&priv->wait);
612}
613
614/**
615 * data_submit_dma() - prepare and submit the required DMA to fill a buffer
616 * @priv: the driver's private data structure
617 * @buf: the data buffer
618 *
619 * Prepare and submit the necessary DMA transactions to fill a correlation
620 * data buffer.
621 *
622 * LOCKING: must hold dev->lock
623 * CONTEXT: hardirq only
624 *
625 * Returns 0 on success, -ERRNO otherwise
626 */
627static int data_submit_dma(struct fpga_device *priv, struct data_buf *buf)
628{
629 struct scatterlist *dst_sg, *src_sg;
630 unsigned int dst_nents, src_nents;
631 struct dma_chan *chan = priv->chan;
632 struct dma_async_tx_descriptor *tx;
633 dma_cookie_t cookie;
634 dma_addr_t dst, src;
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200635 unsigned long dma_flags = 0;
Ira Snyderc186f0e2011-02-11 13:34:29 +0000636
637 dst_sg = buf->vb.sglist;
638 dst_nents = buf->vb.sglen;
639
640 src_sg = priv->corl_table.sgl;
641 src_nents = priv->corl_nents;
642
643 /*
644 * All buffers passed to this function should be ready and mapped
645 * for DMA already. Therefore, we don't need to do anything except
646 * submit it to the Freescale DMA Engine for processing
647 */
648
649 /* setup the scatterlist to scatterlist transfer */
650 tx = chan->device->device_prep_dma_sg(chan,
651 dst_sg, dst_nents,
652 src_sg, src_nents,
653 0);
654 if (!tx) {
655 dev_err(priv->dev, "unable to prep scatterlist DMA\n");
656 return -ENOMEM;
657 }
658
659 /* submit the transaction to the DMA controller */
660 cookie = tx->tx_submit(tx);
661 if (dma_submit_error(cookie)) {
662 dev_err(priv->dev, "unable to submit scatterlist DMA\n");
663 return -ENOMEM;
664 }
665
666 /* Prepare the re-read of the SYS-FPGA block */
667 dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
668 src = SYS_FPGA_BLOCK;
669 tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
670 REG_BLOCK_SIZE,
Bartlomiej Zolnierkiewiczbfc191e2012-11-05 10:00:15 +0000671 dma_flags);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000672 if (!tx) {
673 dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
674 return -ENOMEM;
675 }
676
677 /* Setup the callback */
678 tx->callback = data_dma_cb;
679 tx->callback_param = priv;
680
681 /* submit the transaction to the DMA controller */
682 cookie = tx->tx_submit(tx);
683 if (dma_submit_error(cookie)) {
684 dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
685 return -ENOMEM;
686 }
687
688 return 0;
689}
690
691#define CORL_DONE 0x1
692#define CORL_ERR 0x2
693
694static irqreturn_t data_irq(int irq, void *dev_id)
695{
696 struct fpga_device *priv = dev_id;
697 bool submitted = false;
698 struct data_buf *buf;
699 u32 status;
700 int i;
701
702 /* detect spurious interrupts via FPGA status */
703 for (i = 0; i < 4; i++) {
704 status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
705 if (!(status & (CORL_DONE | CORL_ERR))) {
706 dev_err(priv->dev, "spurious irq detected (FPGA)\n");
707 return IRQ_NONE;
708 }
709 }
710
711 /* detect spurious interrupts via raw IRQ pin readback */
712 status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
713 if (status & IRQ_CORL_DONE) {
714 dev_err(priv->dev, "spurious irq detected (IRQ)\n");
715 return IRQ_NONE;
716 }
717
718 spin_lock(&priv->lock);
719
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000720 /*
721 * This is an error case that should never happen.
722 *
723 * If this driver has a bug and manages to re-enable interrupts while
724 * a DMA is in progress, then we will hit this statement and should
725 * start paying attention immediately.
726 */
727 BUG_ON(priv->inflight != NULL);
728
Ira Snyderc186f0e2011-02-11 13:34:29 +0000729 /* hide the interrupt by switching the IRQ driver to GPIO */
730 data_disable_interrupts(priv);
731
732 /* If there are no free buffers, drop this data */
733 if (list_empty(&priv->free)) {
734 priv->num_dropped++;
735 goto out;
736 }
737
738 buf = list_first_entry(&priv->free, struct data_buf, entry);
739 list_del_init(&buf->entry);
740 BUG_ON(buf->size != priv->bufsize);
741
742 /* Submit a DMA transfer to get the correlation data */
743 if (data_submit_dma(priv, buf)) {
744 dev_err(priv->dev, "Unable to setup DMA transfer\n");
745 list_move_tail(&buf->entry, &priv->free);
746 goto out;
747 }
748
749 /* Save the buffer for the DMA callback */
750 priv->inflight = buf;
751 submitted = true;
752
753 /* Start the DMA Engine */
Bartlomiej Zolnierkiewiczb9ee8682012-11-08 09:59:54 +0000754 dma_async_issue_pending(priv->chan);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000755
756out:
757 /* If no DMA was submitted, re-enable interrupts */
758 if (!submitted)
759 data_enable_interrupts(priv);
760
761 spin_unlock(&priv->lock);
762 return IRQ_HANDLED;
763}
764
765/*
766 * Realtime Device Enable Helpers
767 */
768
769/**
770 * data_device_enable() - enable the device for buffered dumping
771 * @priv: the driver's private data structure
772 *
773 * Enable the device for buffered dumping. Allocates buffers and hooks up
774 * the interrupt handler. When this finishes, data will come pouring in.
775 *
776 * LOCKING: must hold dev->mutex
777 * CONTEXT: user context only
778 *
779 * Returns 0 on success, -ERRNO otherwise
780 */
781static int data_device_enable(struct fpga_device *priv)
782{
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000783 bool enabled;
Ira Snyderc186f0e2011-02-11 13:34:29 +0000784 u32 val;
785 int ret;
786
787 /* multiple enables are safe: they do nothing */
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000788 spin_lock_irq(&priv->lock);
789 enabled = priv->enabled;
790 spin_unlock_irq(&priv->lock);
791 if (enabled)
Ira Snyderc186f0e2011-02-11 13:34:29 +0000792 return 0;
793
794 /* check that the FPGAs are programmed */
795 val = ioread32be(priv->regs + SYS_FPGA_CONFIG_STATUS);
796 if (!(val & (1 << 18))) {
797 dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
798 return -ENODATA;
799 }
800
801 /* read the FPGAs to calculate the buffer size */
802 ret = data_calculate_bufsize(priv);
803 if (ret) {
804 dev_err(priv->dev, "unable to calculate buffer size\n");
805 goto out_error;
806 }
807
808 /* allocate the correlation data buffers */
809 ret = data_alloc_buffers(priv);
810 if (ret) {
811 dev_err(priv->dev, "unable to allocate buffers\n");
812 goto out_error;
813 }
814
815 /* setup the source scatterlist for dumping correlation data */
816 ret = data_setup_corl_table(priv);
817 if (ret) {
818 dev_err(priv->dev, "unable to setup correlation DMA table\n");
819 goto out_error;
820 }
821
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000822 /* prevent the FPGAs from generating interrupts */
823 data_disable_interrupts(priv);
824
Ira Snyderc186f0e2011-02-11 13:34:29 +0000825 /* hookup the irq handler */
826 ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
827 if (ret) {
828 dev_err(priv->dev, "unable to request IRQ handler\n");
829 goto out_error;
830 }
831
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000832 /* allow the DMA callback to re-enable FPGA interrupts */
833 spin_lock_irq(&priv->lock);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000834 priv->enabled = true;
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000835 spin_unlock_irq(&priv->lock);
836
837 /* allow the FPGAs to generate interrupts */
838 data_enable_interrupts(priv);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000839 return 0;
840
841out_error:
842 sg_free_table(&priv->corl_table);
843 priv->corl_nents = 0;
844
845 data_free_buffers(priv);
846 return ret;
847}
848
849/**
850 * data_device_disable() - disable the device for buffered dumping
851 * @priv: the driver's private data structure
852 *
853 * Disable the device for buffered dumping. Stops new DMA transactions from
854 * being generated, waits for all outstanding DMA to complete, and then frees
855 * all buffers.
856 *
857 * LOCKING: must hold dev->mutex
858 * CONTEXT: user only
859 *
860 * Returns 0 on success, -ERRNO otherwise
861 */
862static int data_device_disable(struct fpga_device *priv)
863{
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000864 spin_lock_irq(&priv->lock);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000865
866 /* allow multiple disable */
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000867 if (!priv->enabled) {
868 spin_unlock_irq(&priv->lock);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000869 return 0;
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000870 }
Ira Snyderc186f0e2011-02-11 13:34:29 +0000871
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000872 /*
873 * Mark the device disabled
874 *
875 * This stops DMA callbacks from re-enabling interrupts
876 */
877 priv->enabled = false;
878
879 /* prevent the FPGAs from generating interrupts */
Ira Snyderc186f0e2011-02-11 13:34:29 +0000880 data_disable_interrupts(priv);
881
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000882 /* wait until all ongoing DMA has finished */
883 while (priv->inflight != NULL) {
884 spin_unlock_irq(&priv->lock);
885 wait_event(priv->wait, priv->inflight == NULL);
886 spin_lock_irq(&priv->lock);
887 }
888
889 spin_unlock_irq(&priv->lock);
890
Ira Snyderc186f0e2011-02-11 13:34:29 +0000891 /* unhook the irq handler */
892 free_irq(priv->irq, priv);
893
Ira Snyderc186f0e2011-02-11 13:34:29 +0000894 /* free the correlation table */
895 sg_free_table(&priv->corl_table);
896 priv->corl_nents = 0;
897
Ira Snyderc186f0e2011-02-11 13:34:29 +0000898 /* free all buffers: the free and used lists are not being changed */
899 data_free_buffers(priv);
900 return 0;
901}
902
903/*
904 * DEBUGFS Interface
905 */
906#ifdef CONFIG_DEBUG_FS
907
908/*
909 * Count the number of entries in the given list
910 */
911static unsigned int list_num_entries(struct list_head *list)
912{
913 struct list_head *entry;
914 unsigned int ret = 0;
915
916 list_for_each(entry, list)
917 ret++;
918
919 return ret;
920}
921
922static int data_debug_show(struct seq_file *f, void *offset)
923{
924 struct fpga_device *priv = f->private;
Ira Snyderc186f0e2011-02-11 13:34:29 +0000925
926 spin_lock_irq(&priv->lock);
927
928 seq_printf(f, "enabled: %d\n", priv->enabled);
929 seq_printf(f, "bufsize: %d\n", priv->bufsize);
930 seq_printf(f, "num_buffers: %d\n", priv->num_buffers);
931 seq_printf(f, "num_free: %d\n", list_num_entries(&priv->free));
932 seq_printf(f, "inflight: %d\n", priv->inflight != NULL);
933 seq_printf(f, "num_used: %d\n", list_num_entries(&priv->used));
934 seq_printf(f, "num_dropped: %d\n", priv->num_dropped);
935
936 spin_unlock_irq(&priv->lock);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000937 return 0;
938}
939
940static int data_debug_open(struct inode *inode, struct file *file)
941{
942 return single_open(file, data_debug_show, inode->i_private);
943}
944
945static const struct file_operations data_debug_fops = {
946 .owner = THIS_MODULE,
947 .open = data_debug_open,
948 .read = seq_read,
949 .llseek = seq_lseek,
950 .release = single_release,
951};
952
953static int data_debugfs_init(struct fpga_device *priv)
954{
955 priv->dbg_entry = debugfs_create_file(drv_name, S_IRUGO, NULL, priv,
956 &data_debug_fops);
Fabian Frederickfc2f6772014-06-15 08:22:42 +0200957 return PTR_ERR_OR_ZERO(priv->dbg_entry);
Ira Snyderc186f0e2011-02-11 13:34:29 +0000958}
959
960static void data_debugfs_exit(struct fpga_device *priv)
961{
962 debugfs_remove(priv->dbg_entry);
963}
964
965#else
966
967static inline int data_debugfs_init(struct fpga_device *priv)
968{
969 return 0;
970}
971
972static inline void data_debugfs_exit(struct fpga_device *priv)
973{
974}
975
976#endif /* CONFIG_DEBUG_FS */
977
978/*
979 * SYSFS Attributes
980 */
981
982static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
983 char *buf)
984{
985 struct fpga_device *priv = dev_get_drvdata(dev);
Ira Snyder6c15d7a2012-01-26 11:00:14 +0000986 int ret;
987
988 spin_lock_irq(&priv->lock);
989 ret = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
990 spin_unlock_irq(&priv->lock);
991
992 return ret;
Ira Snyderc186f0e2011-02-11 13:34:29 +0000993}
994
995static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
996 const char *buf, size_t count)
997{
998 struct fpga_device *priv = dev_get_drvdata(dev);
999 unsigned long enable;
1000 int ret;
1001
Jingoo Hanf7b41272013-06-04 13:15:16 +09001002 ret = kstrtoul(buf, 0, &enable);
Ira Snyderc186f0e2011-02-11 13:34:29 +00001003 if (ret) {
1004 dev_err(priv->dev, "unable to parse enable input\n");
Jingoo Hanf7b41272013-06-04 13:15:16 +09001005 return ret;
Ira Snyderc186f0e2011-02-11 13:34:29 +00001006 }
1007
Ira Snyder6c15d7a2012-01-26 11:00:14 +00001008 /* protect against concurrent enable/disable */
Ira Snyderc186f0e2011-02-11 13:34:29 +00001009 ret = mutex_lock_interruptible(&priv->mutex);
1010 if (ret)
1011 return ret;
1012
1013 if (enable)
1014 ret = data_device_enable(priv);
1015 else
1016 ret = data_device_disable(priv);
1017
1018 if (ret) {
1019 dev_err(priv->dev, "device %s failed\n",
1020 enable ? "enable" : "disable");
1021 count = ret;
1022 goto out_unlock;
1023 }
1024
1025out_unlock:
1026 mutex_unlock(&priv->mutex);
1027 return count;
1028}
1029
1030static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, data_en_show, data_en_set);
1031
1032static struct attribute *data_sysfs_attrs[] = {
1033 &dev_attr_enable.attr,
1034 NULL,
1035};
1036
1037static const struct attribute_group rt_sysfs_attr_group = {
1038 .attrs = data_sysfs_attrs,
1039};
1040
1041/*
1042 * FPGA Realtime Data Character Device
1043 */
1044
1045static int data_open(struct inode *inode, struct file *filp)
1046{
1047 /*
1048 * The miscdevice layer puts our struct miscdevice into the
1049 * filp->private_data field. We use this to find our private
1050 * data and then overwrite it with our own private structure.
1051 */
1052 struct fpga_device *priv = container_of(filp->private_data,
1053 struct fpga_device, miscdev);
1054 struct fpga_reader *reader;
1055 int ret;
1056
1057 /* allocate private data */
1058 reader = kzalloc(sizeof(*reader), GFP_KERNEL);
1059 if (!reader)
1060 return -ENOMEM;
1061
1062 reader->priv = priv;
1063 reader->buf = NULL;
1064
1065 filp->private_data = reader;
1066 ret = nonseekable_open(inode, filp);
1067 if (ret) {
1068 dev_err(priv->dev, "nonseekable-open failed\n");
1069 kfree(reader);
1070 return ret;
1071 }
1072
1073 /*
1074 * success, increase the reference count of the private data structure
1075 * so that it doesn't disappear if the device is unbound
1076 */
1077 kref_get(&priv->ref);
1078 return 0;
1079}
1080
1081static int data_release(struct inode *inode, struct file *filp)
1082{
1083 struct fpga_reader *reader = filp->private_data;
1084 struct fpga_device *priv = reader->priv;
1085
1086 /* free the per-reader structure */
1087 data_free_buffer(reader->buf);
1088 kfree(reader);
1089 filp->private_data = NULL;
1090
1091 /* decrement our reference count to the private data */
1092 kref_put(&priv->ref, fpga_device_release);
1093 return 0;
1094}
1095
1096static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
1097 loff_t *f_pos)
1098{
1099 struct fpga_reader *reader = filp->private_data;
1100 struct fpga_device *priv = reader->priv;
1101 struct list_head *used = &priv->used;
Ira Snyder75ff85a2012-01-26 10:59:54 +00001102 bool drop_buffer = false;
Ira Snyderc186f0e2011-02-11 13:34:29 +00001103 struct data_buf *dbuf;
1104 size_t avail;
1105 void *data;
1106 int ret;
1107
1108 /* check if we already have a partial buffer */
1109 if (reader->buf) {
1110 dbuf = reader->buf;
1111 goto have_buffer;
1112 }
1113
1114 spin_lock_irq(&priv->lock);
1115
1116 /* Block until there is at least one buffer on the used list */
1117 while (list_empty(used)) {
1118 spin_unlock_irq(&priv->lock);
1119
1120 if (filp->f_flags & O_NONBLOCK)
1121 return -EAGAIN;
1122
1123 ret = wait_event_interruptible(priv->wait, !list_empty(used));
1124 if (ret)
1125 return ret;
1126
1127 spin_lock_irq(&priv->lock);
1128 }
1129
1130 /* Grab the first buffer off of the used list */
1131 dbuf = list_first_entry(used, struct data_buf, entry);
1132 list_del_init(&dbuf->entry);
1133
1134 spin_unlock_irq(&priv->lock);
1135
1136 /* Buffers are always mapped: unmap it */
1137 videobuf_dma_unmap(priv->dev, &dbuf->vb);
1138
1139 /* save the buffer for later */
1140 reader->buf = dbuf;
1141 reader->buf_start = 0;
1142
1143have_buffer:
1144 /* Get the number of bytes available */
1145 avail = dbuf->size - reader->buf_start;
1146 data = dbuf->vb.vaddr + reader->buf_start;
1147
1148 /* Get the number of bytes we can transfer */
1149 count = min(count, avail);
1150
1151 /* Copy the data to the userspace buffer */
1152 if (copy_to_user(ubuf, data, count))
1153 return -EFAULT;
1154
1155 /* Update the amount of available space */
1156 avail -= count;
1157
1158 /*
1159 * If there is still some data available, save the buffer for the
1160 * next userspace call to read() and return
1161 */
1162 if (avail > 0) {
1163 reader->buf_start += count;
1164 reader->buf = dbuf;
1165 return count;
1166 }
1167
1168 /*
1169 * Get the buffer ready to be reused for DMA
1170 *
1171 * If it fails, we pretend that the read never happed and return
1172 * -EFAULT to userspace. The read will be retried.
1173 */
1174 ret = videobuf_dma_map(priv->dev, &dbuf->vb);
1175 if (ret) {
1176 dev_err(priv->dev, "unable to remap buffer for DMA\n");
1177 return -EFAULT;
1178 }
1179
1180 /* Lock against concurrent enable/disable */
1181 spin_lock_irq(&priv->lock);
1182
1183 /* the reader is finished with this buffer */
1184 reader->buf = NULL;
1185
1186 /*
1187 * One of two things has happened, the device is disabled, or the
1188 * device has been reconfigured underneath us. In either case, we
1189 * should just throw away the buffer.
Ira Snyder75ff85a2012-01-26 10:59:54 +00001190 *
1191 * Lockdep complains if this is done under the spinlock, so we
1192 * handle it during the unlock path.
Ira Snyderc186f0e2011-02-11 13:34:29 +00001193 */
1194 if (!priv->enabled || dbuf->size != priv->bufsize) {
Ira Snyder75ff85a2012-01-26 10:59:54 +00001195 drop_buffer = true;
Ira Snyderc186f0e2011-02-11 13:34:29 +00001196 goto out_unlock;
1197 }
1198
1199 /* The buffer is safe to reuse, so add it back to the free list */
1200 list_add_tail(&dbuf->entry, &priv->free);
1201
1202out_unlock:
1203 spin_unlock_irq(&priv->lock);
Ira Snyder75ff85a2012-01-26 10:59:54 +00001204
1205 if (drop_buffer) {
1206 videobuf_dma_unmap(priv->dev, &dbuf->vb);
1207 data_free_buffer(dbuf);
1208 }
1209
Ira Snyderc186f0e2011-02-11 13:34:29 +00001210 return count;
1211}
1212
1213static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
1214{
1215 struct fpga_reader *reader = filp->private_data;
1216 struct fpga_device *priv = reader->priv;
1217 unsigned int mask = 0;
1218
1219 poll_wait(filp, &priv->wait, tbl);
1220
1221 if (!list_empty(&priv->used))
1222 mask |= POLLIN | POLLRDNORM;
1223
1224 return mask;
1225}
1226
1227static int data_mmap(struct file *filp, struct vm_area_struct *vma)
1228{
1229 struct fpga_reader *reader = filp->private_data;
1230 struct fpga_device *priv = reader->priv;
1231 unsigned long offset, vsize, psize, addr;
1232
1233 /* VMA properties */
1234 offset = vma->vm_pgoff << PAGE_SHIFT;
1235 vsize = vma->vm_end - vma->vm_start;
1236 psize = priv->phys_size - offset;
1237 addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
1238
1239 /* Check against the FPGA region's physical memory size */
1240 if (vsize > psize) {
1241 dev_err(priv->dev, "requested mmap mapping too large\n");
1242 return -EINVAL;
1243 }
1244
Ira Snyderc186f0e2011-02-11 13:34:29 +00001245 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1246
1247 return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
1248 vma->vm_page_prot);
1249}
1250
1251static const struct file_operations data_fops = {
1252 .owner = THIS_MODULE,
1253 .open = data_open,
1254 .release = data_release,
1255 .read = data_read,
1256 .poll = data_poll,
1257 .mmap = data_mmap,
1258 .llseek = no_llseek,
1259};
1260
1261/*
1262 * OpenFirmware Device Subsystem
1263 */
1264
1265static bool dma_filter(struct dma_chan *chan, void *data)
1266{
1267 /*
1268 * DMA Channel #0 is used for the FPGA Programmer, so ignore it
1269 *
1270 * This probably won't survive an unload/load cycle of the Freescale
1271 * DMAEngine driver, but that won't be a problem
1272 */
1273 if (chan->chan_id == 0 && chan->device->dev_id == 0)
1274 return false;
1275
1276 return true;
1277}
1278
Al Viro49334022011-11-08 19:57:05 -05001279static int data_of_probe(struct platform_device *op)
Ira Snyderc186f0e2011-02-11 13:34:29 +00001280{
1281 struct device_node *of_node = op->dev.of_node;
1282 struct device *this_device;
1283 struct fpga_device *priv;
1284 struct resource res;
1285 dma_cap_mask_t mask;
1286 int ret;
1287
1288 /* Allocate private data */
1289 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1290 if (!priv) {
1291 dev_err(&op->dev, "Unable to allocate device private data\n");
1292 ret = -ENOMEM;
1293 goto out_return;
1294 }
1295
Jingoo Han9093ca82013-05-23 19:35:23 +09001296 platform_set_drvdata(op, priv);
Ira Snyderc186f0e2011-02-11 13:34:29 +00001297 priv->dev = &op->dev;
1298 kref_init(&priv->ref);
1299 mutex_init(&priv->mutex);
1300
1301 dev_set_drvdata(priv->dev, priv);
1302 spin_lock_init(&priv->lock);
1303 INIT_LIST_HEAD(&priv->free);
1304 INIT_LIST_HEAD(&priv->used);
1305 init_waitqueue_head(&priv->wait);
1306
1307 /* Setup the misc device */
1308 priv->miscdev.minor = MISC_DYNAMIC_MINOR;
1309 priv->miscdev.name = drv_name;
1310 priv->miscdev.fops = &data_fops;
1311
1312 /* Get the physical address of the FPGA registers */
1313 ret = of_address_to_resource(of_node, 0, &res);
1314 if (ret) {
1315 dev_err(&op->dev, "Unable to find FPGA physical address\n");
1316 ret = -ENODEV;
1317 goto out_free_priv;
1318 }
1319
1320 priv->phys_addr = res.start;
1321 priv->phys_size = resource_size(&res);
1322
1323 /* ioremap the registers for use */
1324 priv->regs = of_iomap(of_node, 0);
1325 if (!priv->regs) {
1326 dev_err(&op->dev, "Unable to ioremap registers\n");
1327 ret = -ENOMEM;
1328 goto out_free_priv;
1329 }
1330
1331 dma_cap_zero(mask);
1332 dma_cap_set(DMA_MEMCPY, mask);
1333 dma_cap_set(DMA_INTERRUPT, mask);
1334 dma_cap_set(DMA_SLAVE, mask);
1335 dma_cap_set(DMA_SG, mask);
1336
1337 /* Request a DMA channel */
1338 priv->chan = dma_request_channel(mask, dma_filter, NULL);
1339 if (!priv->chan) {
1340 dev_err(&op->dev, "Unable to request DMA channel\n");
1341 ret = -ENODEV;
1342 goto out_unmap_regs;
1343 }
1344
1345 /* Find the correct IRQ number */
1346 priv->irq = irq_of_parse_and_map(of_node, 0);
1347 if (priv->irq == NO_IRQ) {
1348 dev_err(&op->dev, "Unable to find IRQ line\n");
1349 ret = -ENODEV;
1350 goto out_release_dma;
1351 }
1352
1353 /* Drive the GPIO for FPGA IRQ high (no interrupt) */
1354 iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
1355
1356 /* Register the miscdevice */
1357 ret = misc_register(&priv->miscdev);
1358 if (ret) {
1359 dev_err(&op->dev, "Unable to register miscdevice\n");
1360 goto out_irq_dispose_mapping;
1361 }
1362
1363 /* Create the debugfs files */
1364 ret = data_debugfs_init(priv);
1365 if (ret) {
1366 dev_err(&op->dev, "Unable to create debugfs files\n");
1367 goto out_misc_deregister;
1368 }
1369
1370 /* Create the sysfs files */
1371 this_device = priv->miscdev.this_device;
1372 dev_set_drvdata(this_device, priv);
1373 ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
1374 if (ret) {
1375 dev_err(&op->dev, "Unable to create sysfs files\n");
1376 goto out_data_debugfs_exit;
1377 }
1378
1379 dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
1380 return 0;
1381
1382out_data_debugfs_exit:
1383 data_debugfs_exit(priv);
1384out_misc_deregister:
1385 misc_deregister(&priv->miscdev);
1386out_irq_dispose_mapping:
1387 irq_dispose_mapping(priv->irq);
1388out_release_dma:
1389 dma_release_channel(priv->chan);
1390out_unmap_regs:
1391 iounmap(priv->regs);
1392out_free_priv:
1393 kref_put(&priv->ref, fpga_device_release);
1394out_return:
1395 return ret;
1396}
1397
1398static int data_of_remove(struct platform_device *op)
1399{
Jingoo Han9093ca82013-05-23 19:35:23 +09001400 struct fpga_device *priv = platform_get_drvdata(op);
Ira Snyderc186f0e2011-02-11 13:34:29 +00001401 struct device *this_device = priv->miscdev.this_device;
1402
1403 /* remove all sysfs files, now the device cannot be re-enabled */
1404 sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
1405
1406 /* remove all debugfs files */
1407 data_debugfs_exit(priv);
1408
1409 /* disable the device from generating data */
1410 data_device_disable(priv);
1411
1412 /* remove the character device to stop new readers from appearing */
1413 misc_deregister(&priv->miscdev);
1414
1415 /* cleanup everything not needed by readers */
1416 irq_dispose_mapping(priv->irq);
1417 dma_release_channel(priv->chan);
1418 iounmap(priv->regs);
1419
1420 /* release our reference */
1421 kref_put(&priv->ref, fpga_device_release);
1422 return 0;
1423}
1424
1425static struct of_device_id data_of_match[] = {
1426 { .compatible = "carma,carma-fpga", },
1427 {},
1428};
1429
Al Viro49334022011-11-08 19:57:05 -05001430static struct platform_driver data_of_driver = {
Ira Snyderc186f0e2011-02-11 13:34:29 +00001431 .probe = data_of_probe,
1432 .remove = data_of_remove,
1433 .driver = {
1434 .name = drv_name,
1435 .of_match_table = data_of_match,
1436 .owner = THIS_MODULE,
1437 },
1438};
1439
Axel Linb00e1262012-01-22 15:33:49 +08001440module_platform_driver(data_of_driver);
Ira Snyderc186f0e2011-02-11 13:34:29 +00001441
1442MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
1443MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
1444MODULE_LICENSE("GPL");