carma-fpga-program.c 27 KB

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