blob: 339b252fcedd8db0ac844e0a5a500ded59f51680 [file] [log] [blame]
Ira Snyder0e1d7152011-02-11 13:34:30 +00001/*
2 * CARMA Board DATA-FPGA Programmer
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#include <linux/dma-mapping.h>
Rob Herring5af50732013-09-17 14:28:33 -050013#include <linux/of_address.h>
14#include <linux/of_irq.h>
Ira Snyder0e1d7152011-02-11 13:34:30 +000015#include <linux/of_platform.h>
16#include <linux/completion.h>
17#include <linux/miscdevice.h>
18#include <linux/dmaengine.h>
Vinod Koul7abfe6e2014-10-11 21:16:46 +053019#include <linux/fsldma.h>
Ira Snyder0e1d7152011-02-11 13:34:30 +000020#include <linux/interrupt.h>
21#include <linux/highmem.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/mutex.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/leds.h>
28#include <linux/slab.h>
29#include <linux/kref.h>
30#include <linux/fs.h>
31#include <linux/io.h>
32
33#include <media/videobuf-dma-sg.h>
34
35/* MPC8349EMDS specific get_immrbase() */
36#include <sysdev/fsl_soc.h>
37
38static const char drv_name[] = "carma-fpga-program";
39
40/*
41 * Firmware images are always this exact size
42 *
43 * 12849552 bytes for a CARMA Digitizer Board (EP2S90 FPGAs)
44 * 18662880 bytes for a CARMA Correlator Board (EP2S130 FPGAs)
45 */
46#define FW_SIZE_EP2S90 12849552
47#define FW_SIZE_EP2S130 18662880
48
49struct fpga_dev {
50 struct miscdevice miscdev;
51
52 /* Reference count */
53 struct kref ref;
54
55 /* Device Registers */
56 struct device *dev;
57 void __iomem *regs;
58 void __iomem *immr;
59
60 /* Freescale DMA Device */
61 struct dma_chan *chan;
62
63 /* Interrupts */
64 int irq, status;
65 struct completion completion;
66
67 /* FPGA Bitfile */
68 struct mutex lock;
69
70 struct videobuf_dmabuf vb;
71 bool vb_allocated;
72
73 /* max size and written bytes */
74 size_t fw_size;
75 size_t bytes;
76};
77
78/*
79 * FPGA Bitfile Helpers
80 */
81
82/**
83 * fpga_drop_firmware_data() - drop the bitfile image from memory
84 * @priv: the driver's private data structure
85 *
86 * LOCKING: must hold priv->lock
87 */
88static void fpga_drop_firmware_data(struct fpga_dev *priv)
89{
90 videobuf_dma_free(&priv->vb);
91 priv->vb_allocated = false;
92 priv->bytes = 0;
93}
94
95/*
96 * Private Data Reference Count
97 */
98
99static void fpga_dev_remove(struct kref *ref)
100{
101 struct fpga_dev *priv = container_of(ref, struct fpga_dev, ref);
102
103 /* free any firmware image that was not programmed */
104 fpga_drop_firmware_data(priv);
105
106 mutex_destroy(&priv->lock);
107 kfree(priv);
108}
109
110/*
111 * LED Trigger (could be a seperate module)
112 */
113
114/*
115 * NOTE: this whole thing does have the problem that whenever the led's are
116 * NOTE: first set to use the fpga trigger, they could be in the wrong state
117 */
118
119DEFINE_LED_TRIGGER(ledtrig_fpga);
120
121static void ledtrig_fpga_programmed(bool enabled)
122{
123 if (enabled)
124 led_trigger_event(ledtrig_fpga, LED_FULL);
125 else
126 led_trigger_event(ledtrig_fpga, LED_OFF);
127}
128
129/*
130 * FPGA Register Helpers
131 */
132
133/* Register Definitions */
134#define FPGA_CONFIG_CONTROL 0x40
135#define FPGA_CONFIG_STATUS 0x44
136#define FPGA_CONFIG_FIFO_SIZE 0x48
137#define FPGA_CONFIG_FIFO_USED 0x4C
138#define FPGA_CONFIG_TOTAL_BYTE_COUNT 0x50
139#define FPGA_CONFIG_CUR_BYTE_COUNT 0x54
140
141#define FPGA_FIFO_ADDRESS 0x3000
142
143static int fpga_fifo_size(void __iomem *regs)
144{
145 return ioread32be(regs + FPGA_CONFIG_FIFO_SIZE);
146}
147
148#define CFG_STATUS_ERR_MASK 0xfffe
149
150static int fpga_config_error(void __iomem *regs)
151{
152 return ioread32be(regs + FPGA_CONFIG_STATUS) & CFG_STATUS_ERR_MASK;
153}
154
155static int fpga_fifo_empty(void __iomem *regs)
156{
157 return ioread32be(regs + FPGA_CONFIG_FIFO_USED) == 0;
158}
159
160static void fpga_fifo_write(void __iomem *regs, u32 val)
161{
162 iowrite32be(val, regs + FPGA_FIFO_ADDRESS);
163}
164
165static void fpga_set_byte_count(void __iomem *regs, u32 count)
166{
167 iowrite32be(count, regs + FPGA_CONFIG_TOTAL_BYTE_COUNT);
168}
169
170#define CFG_CTL_ENABLE (1 << 0)
171#define CFG_CTL_RESET (1 << 1)
172#define CFG_CTL_DMA (1 << 2)
173
174static void fpga_programmer_enable(struct fpga_dev *priv, bool dma)
175{
176 u32 val;
177
178 val = (dma) ? (CFG_CTL_ENABLE | CFG_CTL_DMA) : CFG_CTL_ENABLE;
179 iowrite32be(val, priv->regs + FPGA_CONFIG_CONTROL);
180}
181
182static void fpga_programmer_disable(struct fpga_dev *priv)
183{
184 iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
185}
186
187static void fpga_dump_registers(struct fpga_dev *priv)
188{
189 u32 control, status, size, used, total, curr;
190
191 /* good status: do nothing */
192 if (priv->status == 0)
193 return;
194
195 /* Dump all status registers */
196 control = ioread32be(priv->regs + FPGA_CONFIG_CONTROL);
197 status = ioread32be(priv->regs + FPGA_CONFIG_STATUS);
198 size = ioread32be(priv->regs + FPGA_CONFIG_FIFO_SIZE);
199 used = ioread32be(priv->regs + FPGA_CONFIG_FIFO_USED);
200 total = ioread32be(priv->regs + FPGA_CONFIG_TOTAL_BYTE_COUNT);
201 curr = ioread32be(priv->regs + FPGA_CONFIG_CUR_BYTE_COUNT);
202
203 dev_err(priv->dev, "Configuration failed, dumping status registers\n");
204 dev_err(priv->dev, "Control: 0x%.8x\n", control);
205 dev_err(priv->dev, "Status: 0x%.8x\n", status);
206 dev_err(priv->dev, "FIFO Size: 0x%.8x\n", size);
207 dev_err(priv->dev, "FIFO Used: 0x%.8x\n", used);
208 dev_err(priv->dev, "FIFO Total: 0x%.8x\n", total);
209 dev_err(priv->dev, "FIFO Curr: 0x%.8x\n", curr);
210}
211
212/*
213 * FPGA Power Supply Code
214 */
215
216#define CTL_PWR_CONTROL 0x2006
217#define CTL_PWR_STATUS 0x200A
218#define CTL_PWR_FAIL 0x200B
219
220#define PWR_CONTROL_ENABLE 0x01
221
222#define PWR_STATUS_ERROR_MASK 0x10
223#define PWR_STATUS_GOOD 0x0f
224
225/*
226 * Determine if the FPGA power is good for all supplies
227 */
228static bool fpga_power_good(struct fpga_dev *priv)
229{
230 u8 val;
231
232 val = ioread8(priv->regs + CTL_PWR_STATUS);
233 if (val & PWR_STATUS_ERROR_MASK)
234 return false;
235
236 return val == PWR_STATUS_GOOD;
237}
238
239/*
240 * Disable the FPGA power supplies
241 */
242static void fpga_disable_power_supplies(struct fpga_dev *priv)
243{
244 unsigned long start;
245 u8 val;
246
247 iowrite8(0x0, priv->regs + CTL_PWR_CONTROL);
248
249 /*
250 * Wait 500ms for the power rails to discharge
251 *
252 * Without this delay, the CTL-CPLD state machine can get into a
253 * state where it is waiting for the power-goods to assert, but they
254 * never do. This only happens when enabling and disabling the
255 * power sequencer very rapidly.
256 *
257 * The loop below will also wait for the power goods to de-assert,
258 * but testing has shown that they are always disabled by the time
259 * the sleep completes. However, omitting the sleep and only waiting
260 * for the power-goods to de-assert was not sufficient to ensure
261 * that the power sequencer would not wedge itself.
262 */
263 msleep(500);
264
265 start = jiffies;
266 while (time_before(jiffies, start + HZ)) {
267 val = ioread8(priv->regs + CTL_PWR_STATUS);
268 if (!(val & PWR_STATUS_GOOD))
269 break;
270
271 usleep_range(5000, 10000);
272 }
273
274 val = ioread8(priv->regs + CTL_PWR_STATUS);
275 if (val & PWR_STATUS_GOOD) {
276 dev_err(priv->dev, "power disable failed: "
277 "power goods: status 0x%.2x\n", val);
278 }
279
280 if (val & PWR_STATUS_ERROR_MASK) {
281 dev_err(priv->dev, "power disable failed: "
282 "alarm bit set: status 0x%.2x\n", val);
283 }
284}
285
286/**
287 * fpga_enable_power_supplies() - enable the DATA-FPGA power supplies
288 * @priv: the driver's private data structure
289 *
290 * Enable the DATA-FPGA power supplies, waiting up to 1 second for
291 * them to enable successfully.
292 *
293 * Returns 0 on success, -ERRNO otherwise
294 */
295static int fpga_enable_power_supplies(struct fpga_dev *priv)
296{
297 unsigned long start = jiffies;
298
299 if (fpga_power_good(priv)) {
300 dev_dbg(priv->dev, "power was already good\n");
301 return 0;
302 }
303
304 iowrite8(PWR_CONTROL_ENABLE, priv->regs + CTL_PWR_CONTROL);
305 while (time_before(jiffies, start + HZ)) {
306 if (fpga_power_good(priv))
307 return 0;
308
309 usleep_range(5000, 10000);
310 }
311
312 return fpga_power_good(priv) ? 0 : -ETIMEDOUT;
313}
314
315/*
316 * Determine if the FPGA power supplies are all enabled
317 */
318static bool fpga_power_enabled(struct fpga_dev *priv)
319{
320 u8 val;
321
322 val = ioread8(priv->regs + CTL_PWR_CONTROL);
323 if (val & PWR_CONTROL_ENABLE)
324 return true;
325
326 return false;
327}
328
329/*
330 * Determine if the FPGA's are programmed and running correctly
331 */
332static bool fpga_running(struct fpga_dev *priv)
333{
334 if (!fpga_power_good(priv))
335 return false;
336
337 /* Check the config done bit */
338 return ioread32be(priv->regs + FPGA_CONFIG_STATUS) & (1 << 18);
339}
340
341/*
342 * FPGA Programming Code
343 */
344
345/**
346 * fpga_program_block() - put a block of data into the programmer's FIFO
347 * @priv: the driver's private data structure
348 * @buf: the data to program
349 * @count: the length of data to program (must be a multiple of 4 bytes)
350 *
351 * Returns 0 on success, -ERRNO otherwise
352 */
353static int fpga_program_block(struct fpga_dev *priv, void *buf, size_t count)
354{
355 u32 *data = buf;
356 int size = fpga_fifo_size(priv->regs);
357 int i, len;
358 unsigned long timeout;
359
360 /* enforce correct data length for the FIFO */
361 BUG_ON(count % 4 != 0);
362
363 while (count > 0) {
364
365 /* Get the size of the block to write (maximum is FIFO_SIZE) */
366 len = min_t(size_t, count, size);
367 timeout = jiffies + HZ / 4;
368
369 /* Write the block */
370 for (i = 0; i < len / 4; i++)
371 fpga_fifo_write(priv->regs, data[i]);
372
373 /* Update the amounts left */
374 count -= len;
375 data += len / 4;
376
377 /* Wait for the fifo to empty */
378 while (true) {
379
380 if (fpga_fifo_empty(priv->regs)) {
381 break;
382 } else {
383 dev_dbg(priv->dev, "Fifo not empty\n");
384 cpu_relax();
385 }
386
387 if (fpga_config_error(priv->regs)) {
388 dev_err(priv->dev, "Error detected\n");
389 return -EIO;
390 }
391
392 if (time_after(jiffies, timeout)) {
393 dev_err(priv->dev, "Fifo drain timeout\n");
394 return -ETIMEDOUT;
395 }
396
397 usleep_range(5000, 10000);
398 }
399 }
400
401 return 0;
402}
403
404/**
405 * fpga_program_cpu() - program the DATA-FPGA's using the CPU
406 * @priv: the driver's private data structure
407 *
408 * This is useful when the DMA programming method fails. It is possible to
409 * wedge the Freescale DMA controller such that the DMA programming method
410 * always fails. This method has always succeeded.
411 *
412 * Returns 0 on success, -ERRNO otherwise
413 */
414static noinline int fpga_program_cpu(struct fpga_dev *priv)
415{
416 int ret;
417
418 /* Disable the programmer */
419 fpga_programmer_disable(priv);
420
421 /* Set the total byte count */
422 fpga_set_byte_count(priv->regs, priv->bytes);
423 dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
424
425 /* Enable the controller for programming */
426 fpga_programmer_enable(priv, false);
427 dev_dbg(priv->dev, "enabled the controller\n");
428
429 /* Write each chunk of the FPGA bitfile to FPGA programmer */
430 ret = fpga_program_block(priv, priv->vb.vaddr, priv->bytes);
431 if (ret)
432 goto out_disable_controller;
433
434 /* Wait for the interrupt handler to signal that programming finished */
435 ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
436 if (!ret) {
437 dev_err(priv->dev, "Timed out waiting for completion\n");
438 ret = -ETIMEDOUT;
439 goto out_disable_controller;
440 }
441
442 /* Retrieve the status from the interrupt handler */
443 ret = priv->status;
444
445out_disable_controller:
446 fpga_programmer_disable(priv);
447 return ret;
448}
449
450#define FIFO_DMA_ADDRESS 0xf0003000
451#define FIFO_MAX_LEN 4096
452
453/**
454 * fpga_program_dma() - program the DATA-FPGA's using the DMA engine
455 * @priv: the driver's private data structure
456 *
457 * Program the DATA-FPGA's using the Freescale DMA engine. This requires that
458 * the engine is programmed such that the hardware DMA request lines can
459 * control the entire DMA transaction. The system controller FPGA then
460 * completely offloads the programming from the CPU.
461 *
462 * Returns 0 on success, -ERRNO otherwise
463 */
464static noinline int fpga_program_dma(struct fpga_dev *priv)
465{
466 struct videobuf_dmabuf *vb = &priv->vb;
467 struct dma_chan *chan = priv->chan;
468 struct dma_async_tx_descriptor *tx;
469 size_t num_pages, len, avail = 0;
470 struct dma_slave_config config;
471 struct scatterlist *sg;
472 struct sg_table table;
473 dma_cookie_t cookie;
474 int ret, i;
475
476 /* Disable the programmer */
477 fpga_programmer_disable(priv);
478
479 /* Allocate a scatterlist for the DMA destination */
480 num_pages = DIV_ROUND_UP(priv->bytes, FIFO_MAX_LEN);
481 ret = sg_alloc_table(&table, num_pages, GFP_KERNEL);
482 if (ret) {
483 dev_err(priv->dev, "Unable to allocate dst scatterlist\n");
484 ret = -ENOMEM;
485 goto out_return;
486 }
487
488 /*
489 * This is an ugly hack
490 *
491 * We fill in a scatterlist as if it were mapped for DMA. This is
492 * necessary because there exists no better structure for this
493 * inside the kernel code.
494 *
495 * As an added bonus, we can use the DMAEngine API for all of this,
496 * rather than inventing another extremely similar API.
497 */
498 avail = priv->bytes;
499 for_each_sg(table.sgl, sg, num_pages, i) {
500 len = min_t(size_t, avail, FIFO_MAX_LEN);
501 sg_dma_address(sg) = FIFO_DMA_ADDRESS;
502 sg_dma_len(sg) = len;
503
504 avail -= len;
505 }
506
507 /* Map the buffer for DMA */
508 ret = videobuf_dma_map(priv->dev, &priv->vb);
509 if (ret) {
510 dev_err(priv->dev, "Unable to map buffer for DMA\n");
511 goto out_free_table;
512 }
513
514 /*
515 * Configure the DMA channel to transfer FIFO_SIZE / 2 bytes per
516 * transaction, and then put it under external control
517 */
518 memset(&config, 0, sizeof(config));
Vinod Koul1d0c81e2011-10-14 10:42:56 +0530519 config.direction = DMA_MEM_TO_DEV;
Ira Snyder0e1d7152011-02-11 13:34:30 +0000520 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
521 config.dst_maxburst = fpga_fifo_size(priv->regs) / 2 / 4;
Vinod Koul4fdcf682014-10-11 21:16:45 +0530522 ret = dmaengine_slave_config(chan, &config);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000523 if (ret) {
524 dev_err(priv->dev, "DMA slave configuration failed\n");
525 goto out_dma_unmap;
526 }
527
Vinod Koul7abfe6e2014-10-11 21:16:46 +0530528 ret = fsl_dma_external_start(chan, 1)
Ira Snyder0e1d7152011-02-11 13:34:30 +0000529 if (ret) {
530 dev_err(priv->dev, "DMA external control setup failed\n");
531 goto out_dma_unmap;
532 }
533
534 /* setup and submit the DMA transaction */
Vinod Koul4fdcf682014-10-11 21:16:45 +0530535
536 tx = dmaengine_prep_dma_sg(chan, table.sgl, num_pages,
537 vb->sglist, vb->sglen, 0);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000538 if (!tx) {
539 dev_err(priv->dev, "Unable to prep DMA transaction\n");
540 ret = -ENOMEM;
541 goto out_dma_unmap;
542 }
543
544 cookie = tx->tx_submit(tx);
545 if (dma_submit_error(cookie)) {
546 dev_err(priv->dev, "Unable to submit DMA transaction\n");
547 ret = -ENOMEM;
548 goto out_dma_unmap;
549 }
550
Bartlomiej Zolnierkiewiczb9ee8682012-11-08 09:59:54 +0000551 dma_async_issue_pending(chan);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000552
553 /* Set the total byte count */
554 fpga_set_byte_count(priv->regs, priv->bytes);
555 dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
556
557 /* Enable the controller for DMA programming */
558 fpga_programmer_enable(priv, true);
559 dev_dbg(priv->dev, "enabled the controller\n");
560
561 /* Wait for the interrupt handler to signal that programming finished */
562 ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
563 if (!ret) {
564 dev_err(priv->dev, "Timed out waiting for completion\n");
565 ret = -ETIMEDOUT;
566 goto out_disable_controller;
567 }
568
569 /* Retrieve the status from the interrupt handler */
570 ret = priv->status;
571
572out_disable_controller:
573 fpga_programmer_disable(priv);
574out_dma_unmap:
575 videobuf_dma_unmap(priv->dev, vb);
576out_free_table:
577 sg_free_table(&table);
578out_return:
579 return ret;
580}
581
582/*
583 * Interrupt Handling
584 */
585
586static irqreturn_t fpga_irq(int irq, void *dev_id)
587{
588 struct fpga_dev *priv = dev_id;
589
590 /* Save the status */
591 priv->status = fpga_config_error(priv->regs) ? -EIO : 0;
592 dev_dbg(priv->dev, "INTERRUPT status %d\n", priv->status);
593 fpga_dump_registers(priv);
594
595 /* Disabling the programmer clears the interrupt */
596 fpga_programmer_disable(priv);
597
598 /* Notify any waiters */
599 complete(&priv->completion);
600
601 return IRQ_HANDLED;
602}
603
604/*
605 * SYSFS Helpers
606 */
607
608/**
609 * fpga_do_stop() - deconfigure (reset) the DATA-FPGA's
610 * @priv: the driver's private data structure
611 *
612 * LOCKING: must hold priv->lock
613 */
614static int fpga_do_stop(struct fpga_dev *priv)
615{
616 u32 val;
617
618 /* Set the led to unprogrammed */
619 ledtrig_fpga_programmed(false);
620
621 /* Pulse the config line to reset the FPGA's */
622 val = CFG_CTL_ENABLE | CFG_CTL_RESET;
623 iowrite32be(val, priv->regs + FPGA_CONFIG_CONTROL);
624 iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
625
626 return 0;
627}
628
629static noinline int fpga_do_program(struct fpga_dev *priv)
630{
631 int ret;
632
633 if (priv->bytes != priv->fw_size) {
634 dev_err(priv->dev, "Incorrect bitfile size: got %zu bytes, "
635 "should be %zu bytes\n",
636 priv->bytes, priv->fw_size);
637 return -EINVAL;
638 }
639
640 if (!fpga_power_enabled(priv)) {
641 dev_err(priv->dev, "Power not enabled\n");
642 return -EINVAL;
643 }
644
645 if (!fpga_power_good(priv)) {
646 dev_err(priv->dev, "Power not good\n");
647 return -EINVAL;
648 }
649
650 /* Set the LED to unprogrammed */
651 ledtrig_fpga_programmed(false);
652
653 /* Try to program the FPGA's using DMA */
654 ret = fpga_program_dma(priv);
655
656 /* If DMA failed or doesn't exist, try with CPU */
657 if (ret) {
658 dev_warn(priv->dev, "Falling back to CPU programming\n");
659 ret = fpga_program_cpu(priv);
660 }
661
662 if (ret) {
663 dev_err(priv->dev, "Unable to program FPGA's\n");
664 return ret;
665 }
666
667 /* Drop the firmware bitfile from memory */
668 fpga_drop_firmware_data(priv);
669
670 dev_dbg(priv->dev, "FPGA programming successful\n");
671 ledtrig_fpga_programmed(true);
672
673 return 0;
674}
675
676/*
677 * File Operations
678 */
679
680static int fpga_open(struct inode *inode, struct file *filp)
681{
682 /*
683 * The miscdevice layer puts our struct miscdevice into the
684 * filp->private_data field. We use this to find our private
685 * data and then overwrite it with our own private structure.
686 */
687 struct fpga_dev *priv = container_of(filp->private_data,
688 struct fpga_dev, miscdev);
689 unsigned int nr_pages;
690 int ret;
691
692 /* We only allow one process at a time */
693 ret = mutex_lock_interruptible(&priv->lock);
694 if (ret)
695 return ret;
696
697 filp->private_data = priv;
698 kref_get(&priv->ref);
699
700 /* Truncation: drop any existing data */
701 if (filp->f_flags & O_TRUNC)
702 priv->bytes = 0;
703
704 /* Check if we have already allocated a buffer */
705 if (priv->vb_allocated)
706 return 0;
707
708 /* Allocate a buffer to hold enough data for the bitfile */
709 nr_pages = DIV_ROUND_UP(priv->fw_size, PAGE_SIZE);
710 ret = videobuf_dma_init_kernel(&priv->vb, DMA_TO_DEVICE, nr_pages);
711 if (ret) {
712 dev_err(priv->dev, "unable to allocate data buffer\n");
713 mutex_unlock(&priv->lock);
714 kref_put(&priv->ref, fpga_dev_remove);
715 return ret;
716 }
717
718 priv->vb_allocated = true;
719 return 0;
720}
721
722static int fpga_release(struct inode *inode, struct file *filp)
723{
724 struct fpga_dev *priv = filp->private_data;
725
726 mutex_unlock(&priv->lock);
727 kref_put(&priv->ref, fpga_dev_remove);
728 return 0;
729}
730
731static ssize_t fpga_write(struct file *filp, const char __user *buf,
732 size_t count, loff_t *f_pos)
733{
734 struct fpga_dev *priv = filp->private_data;
735
736 /* FPGA bitfiles have an exact size: disallow anything else */
737 if (priv->bytes >= priv->fw_size)
738 return -ENOSPC;
739
740 count = min_t(size_t, priv->fw_size - priv->bytes, count);
741 if (copy_from_user(priv->vb.vaddr + priv->bytes, buf, count))
742 return -EFAULT;
743
744 priv->bytes += count;
745 return count;
746}
747
748static ssize_t fpga_read(struct file *filp, char __user *buf, size_t count,
749 loff_t *f_pos)
750{
751 struct fpga_dev *priv = filp->private_data;
Al Virod88c2422014-08-19 12:08:37 -0400752 return simple_read_from_buffer(buf, count, ppos,
753 priv->vb.vaddr, priv->bytes);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000754}
755
756static loff_t fpga_llseek(struct file *filp, loff_t offset, int origin)
757{
758 struct fpga_dev *priv = filp->private_data;
759 loff_t newpos;
760
761 /* only read-only opens are allowed to seek */
762 if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
763 return -EINVAL;
764
Al Viro1a37f5e2014-08-19 12:06:18 -0400765 return fixed_size_llseek(file, offset, origin, priv->fw_size);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000766}
767
768static const struct file_operations fpga_fops = {
769 .open = fpga_open,
770 .release = fpga_release,
771 .write = fpga_write,
772 .read = fpga_read,
773 .llseek = fpga_llseek,
774};
775
776/*
777 * Device Attributes
778 */
779
780static ssize_t pfail_show(struct device *dev, struct device_attribute *attr,
781 char *buf)
782{
783 struct fpga_dev *priv = dev_get_drvdata(dev);
784 u8 val;
785
786 val = ioread8(priv->regs + CTL_PWR_FAIL);
787 return snprintf(buf, PAGE_SIZE, "0x%.2x\n", val);
788}
789
790static ssize_t pgood_show(struct device *dev, struct device_attribute *attr,
791 char *buf)
792{
793 struct fpga_dev *priv = dev_get_drvdata(dev);
794 return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_good(priv));
795}
796
797static ssize_t penable_show(struct device *dev, struct device_attribute *attr,
798 char *buf)
799{
800 struct fpga_dev *priv = dev_get_drvdata(dev);
801 return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_enabled(priv));
802}
803
804static ssize_t penable_store(struct device *dev, struct device_attribute *attr,
805 const char *buf, size_t count)
806{
807 struct fpga_dev *priv = dev_get_drvdata(dev);
808 unsigned long val;
809 int ret;
810
Jingoo Hanf7b41272013-06-04 13:15:16 +0900811 ret = kstrtoul(buf, 0, &val);
812 if (ret)
813 return ret;
Ira Snyder0e1d7152011-02-11 13:34:30 +0000814
815 if (val) {
816 ret = fpga_enable_power_supplies(priv);
817 if (ret)
818 return ret;
819 } else {
820 fpga_do_stop(priv);
821 fpga_disable_power_supplies(priv);
822 }
823
824 return count;
825}
826
827static ssize_t program_show(struct device *dev, struct device_attribute *attr,
828 char *buf)
829{
830 struct fpga_dev *priv = dev_get_drvdata(dev);
831 return snprintf(buf, PAGE_SIZE, "%d\n", fpga_running(priv));
832}
833
834static ssize_t program_store(struct device *dev, struct device_attribute *attr,
835 const char *buf, size_t count)
836{
837 struct fpga_dev *priv = dev_get_drvdata(dev);
838 unsigned long val;
839 int ret;
840
Jingoo Hanf7b41272013-06-04 13:15:16 +0900841 ret = kstrtoul(buf, 0, &val);
842 if (ret)
843 return ret;
Ira Snyder0e1d7152011-02-11 13:34:30 +0000844
845 /* We can't have an image writer and be programming simultaneously */
846 if (mutex_lock_interruptible(&priv->lock))
847 return -ERESTARTSYS;
848
849 /* Program or Reset the FPGA's */
850 ret = val ? fpga_do_program(priv) : fpga_do_stop(priv);
851 if (ret)
852 goto out_unlock;
853
854 /* Success */
855 ret = count;
856
857out_unlock:
858 mutex_unlock(&priv->lock);
859 return ret;
860}
861
862static DEVICE_ATTR(power_fail, S_IRUGO, pfail_show, NULL);
863static DEVICE_ATTR(power_good, S_IRUGO, pgood_show, NULL);
864static DEVICE_ATTR(power_enable, S_IRUGO | S_IWUSR,
865 penable_show, penable_store);
866
867static DEVICE_ATTR(program, S_IRUGO | S_IWUSR,
868 program_show, program_store);
869
870static struct attribute *fpga_attributes[] = {
871 &dev_attr_power_fail.attr,
872 &dev_attr_power_good.attr,
873 &dev_attr_power_enable.attr,
874 &dev_attr_program.attr,
875 NULL,
876};
877
878static const struct attribute_group fpga_attr_group = {
879 .attrs = fpga_attributes,
880};
881
882/*
883 * OpenFirmware Device Subsystem
884 */
885
886#define SYS_REG_VERSION 0x00
887#define SYS_REG_GEOGRAPHIC 0x10
888
889static bool dma_filter(struct dma_chan *chan, void *data)
890{
891 /*
892 * DMA Channel #0 is the only acceptable device
893 *
894 * This probably won't survive an unload/load cycle of the Freescale
895 * DMAEngine driver, but that won't be a problem
896 */
897 return chan->chan_id == 0 && chan->device->dev_id == 0;
898}
899
900static int fpga_of_remove(struct platform_device *op)
901{
Jingoo Han9093ca82013-05-23 19:35:23 +0900902 struct fpga_dev *priv = platform_get_drvdata(op);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000903 struct device *this_device = priv->miscdev.this_device;
904
905 sysfs_remove_group(&this_device->kobj, &fpga_attr_group);
906 misc_deregister(&priv->miscdev);
907
908 free_irq(priv->irq, priv);
909 irq_dispose_mapping(priv->irq);
910
911 /* make sure the power supplies are off */
912 fpga_disable_power_supplies(priv);
913
914 /* unmap registers */
915 iounmap(priv->immr);
916 iounmap(priv->regs);
917
918 dma_release_channel(priv->chan);
919
920 /* drop our reference to the private data structure */
921 kref_put(&priv->ref, fpga_dev_remove);
922 return 0;
923}
924
925/* CTL-CPLD Version Register */
926#define CTL_CPLD_VERSION 0x2000
927
Al Viro49334022011-11-08 19:57:05 -0500928static int fpga_of_probe(struct platform_device *op)
Ira Snyder0e1d7152011-02-11 13:34:30 +0000929{
930 struct device_node *of_node = op->dev.of_node;
931 struct device *this_device;
932 struct fpga_dev *priv;
933 dma_cap_mask_t mask;
934 u32 ver;
935 int ret;
936
937 /* Allocate private data */
938 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
939 if (!priv) {
940 dev_err(&op->dev, "Unable to allocate private data\n");
941 ret = -ENOMEM;
942 goto out_return;
943 }
944
945 /* Setup the miscdevice */
946 priv->miscdev.minor = MISC_DYNAMIC_MINOR;
947 priv->miscdev.name = drv_name;
948 priv->miscdev.fops = &fpga_fops;
949
950 kref_init(&priv->ref);
951
Jingoo Han9093ca82013-05-23 19:35:23 +0900952 platform_set_drvdata(op, priv);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000953 priv->dev = &op->dev;
954 mutex_init(&priv->lock);
955 init_completion(&priv->completion);
956 videobuf_dma_init(&priv->vb);
957
958 dev_set_drvdata(priv->dev, priv);
959 dma_cap_zero(mask);
960 dma_cap_set(DMA_MEMCPY, mask);
Ira Snyder0e1d7152011-02-11 13:34:30 +0000961 dma_cap_set(DMA_SLAVE, mask);
962 dma_cap_set(DMA_SG, mask);
963
964 /* Get control of DMA channel #0 */
965 priv->chan = dma_request_channel(mask, dma_filter, NULL);
966 if (!priv->chan) {
967 dev_err(&op->dev, "Unable to acquire DMA channel #0\n");
968 ret = -ENODEV;
969 goto out_free_priv;
970 }
971
972 /* Remap the registers for use */
973 priv->regs = of_iomap(of_node, 0);
974 if (!priv->regs) {
975 dev_err(&op->dev, "Unable to ioremap registers\n");
976 ret = -ENOMEM;
977 goto out_dma_release_channel;
978 }
979
980 /* Remap the IMMR for use */
981 priv->immr = ioremap(get_immrbase(), 0x100000);
982 if (!priv->immr) {
983 dev_err(&op->dev, "Unable to ioremap IMMR\n");
984 ret = -ENOMEM;
985 goto out_unmap_regs;
986 }
987
988 /*
989 * Check that external DMA is configured
990 *
991 * U-Boot does this for us, but we should check it and bail out if
992 * there is a problem. Failing to have this register setup correctly
993 * will cause the DMA controller to transfer a single cacheline
994 * worth of data, then wedge itself.
995 */
996 if ((ioread32be(priv->immr + 0x114) & 0xE00) != 0xE00) {
997 dev_err(&op->dev, "External DMA control not configured\n");
998 ret = -ENODEV;
999 goto out_unmap_immr;
1000 }
1001
1002 /*
1003 * Check the CTL-CPLD version
1004 *
1005 * This driver uses the CTL-CPLD DATA-FPGA power sequencer, and we
1006 * don't want to run on any version of the CTL-CPLD that does not use
1007 * a compatible register layout.
1008 *
1009 * v2: changed register layout, added power sequencer
1010 * v3: added glitch filter on the i2c overcurrent/overtemp outputs
1011 */
1012 ver = ioread8(priv->regs + CTL_CPLD_VERSION);
1013 if (ver != 0x02 && ver != 0x03) {
1014 dev_err(&op->dev, "CTL-CPLD is not version 0x02 or 0x03!\n");
1015 ret = -ENODEV;
1016 goto out_unmap_immr;
1017 }
1018
1019 /* Set the exact size that the firmware image should be */
1020 ver = ioread32be(priv->regs + SYS_REG_VERSION);
1021 priv->fw_size = (ver & (1 << 18)) ? FW_SIZE_EP2S130 : FW_SIZE_EP2S90;
1022
1023 /* Find the correct IRQ number */
1024 priv->irq = irq_of_parse_and_map(of_node, 0);
1025 if (priv->irq == NO_IRQ) {
1026 dev_err(&op->dev, "Unable to find IRQ line\n");
1027 ret = -ENODEV;
1028 goto out_unmap_immr;
1029 }
1030
1031 /* Request the IRQ */
1032 ret = request_irq(priv->irq, fpga_irq, IRQF_SHARED, drv_name, priv);
1033 if (ret) {
1034 dev_err(&op->dev, "Unable to request IRQ %d\n", priv->irq);
1035 ret = -ENODEV;
1036 goto out_irq_dispose_mapping;
1037 }
1038
1039 /* Reset and stop the FPGA's, just in case */
1040 fpga_do_stop(priv);
1041
1042 /* Register the miscdevice */
1043 ret = misc_register(&priv->miscdev);
1044 if (ret) {
1045 dev_err(&op->dev, "Unable to register miscdevice\n");
1046 goto out_free_irq;
1047 }
1048
1049 /* Create the sysfs files */
1050 this_device = priv->miscdev.this_device;
1051 dev_set_drvdata(this_device, priv);
1052 ret = sysfs_create_group(&this_device->kobj, &fpga_attr_group);
1053 if (ret) {
1054 dev_err(&op->dev, "Unable to create sysfs files\n");
1055 goto out_misc_deregister;
1056 }
1057
1058 dev_info(priv->dev, "CARMA FPGA Programmer: %s rev%s with %s FPGAs\n",
1059 (ver & (1 << 17)) ? "Correlator" : "Digitizer",
1060 (ver & (1 << 16)) ? "B" : "A",
1061 (ver & (1 << 18)) ? "EP2S130" : "EP2S90");
1062
1063 return 0;
1064
1065out_misc_deregister:
1066 misc_deregister(&priv->miscdev);
1067out_free_irq:
1068 free_irq(priv->irq, priv);
1069out_irq_dispose_mapping:
1070 irq_dispose_mapping(priv->irq);
1071out_unmap_immr:
1072 iounmap(priv->immr);
1073out_unmap_regs:
1074 iounmap(priv->regs);
1075out_dma_release_channel:
1076 dma_release_channel(priv->chan);
1077out_free_priv:
1078 kref_put(&priv->ref, fpga_dev_remove);
1079out_return:
1080 return ret;
1081}
1082
1083static struct of_device_id fpga_of_match[] = {
1084 { .compatible = "carma,fpga-programmer", },
1085 {},
1086};
1087
Al Viro49334022011-11-08 19:57:05 -05001088static struct platform_driver fpga_of_driver = {
Ira Snyder0e1d7152011-02-11 13:34:30 +00001089 .probe = fpga_of_probe,
1090 .remove = fpga_of_remove,
1091 .driver = {
1092 .name = drv_name,
1093 .of_match_table = fpga_of_match,
1094 .owner = THIS_MODULE,
1095 },
1096};
1097
1098/*
1099 * Module Init / Exit
1100 */
1101
1102static int __init fpga_init(void)
1103{
1104 led_trigger_register_simple("fpga", &ledtrig_fpga);
Al Viro49334022011-11-08 19:57:05 -05001105 return platform_driver_register(&fpga_of_driver);
Ira Snyder0e1d7152011-02-11 13:34:30 +00001106}
1107
1108static void __exit fpga_exit(void)
1109{
Al Viro49334022011-11-08 19:57:05 -05001110 platform_driver_unregister(&fpga_of_driver);
Ira Snyder0e1d7152011-02-11 13:34:30 +00001111 led_trigger_unregister_simple(ledtrig_fpga);
1112}
1113
1114MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
1115MODULE_DESCRIPTION("CARMA Board DATA-FPGA Programmer");
1116MODULE_LICENSE("GPL");
1117
1118module_init(fpga_init);
1119module_exit(fpga_exit);