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