carma-fpga-program.c 28 KB

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