carma-fpga-program.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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/interrupt.h>
  19. #include <linux/highmem.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/leds.h>
  26. #include <linux/slab.h>
  27. #include <linux/kref.h>
  28. #include <linux/fs.h>
  29. #include <linux/io.h>
  30. #include <media/videobuf-dma-sg.h>
  31. /* MPC8349EMDS specific get_immrbase() */
  32. #include <sysdev/fsl_soc.h>
  33. static const char drv_name[] = "carma-fpga-program";
  34. /*
  35. * Firmware images are always this exact size
  36. *
  37. * 12849552 bytes for a CARMA Digitizer Board (EP2S90 FPGAs)
  38. * 18662880 bytes for a CARMA Correlator Board (EP2S130 FPGAs)
  39. */
  40. #define FW_SIZE_EP2S90 12849552
  41. #define FW_SIZE_EP2S130 18662880
  42. struct fpga_dev {
  43. struct miscdevice miscdev;
  44. /* Reference count */
  45. struct kref ref;
  46. /* Device Registers */
  47. struct device *dev;
  48. void __iomem *regs;
  49. void __iomem *immr;
  50. /* Freescale DMA Device */
  51. struct dma_chan *chan;
  52. /* Interrupts */
  53. int irq, status;
  54. struct completion completion;
  55. /* FPGA Bitfile */
  56. struct mutex lock;
  57. struct videobuf_dmabuf vb;
  58. bool vb_allocated;
  59. /* max size and written bytes */
  60. size_t fw_size;
  61. size_t bytes;
  62. };
  63. /*
  64. * FPGA Bitfile Helpers
  65. */
  66. /**
  67. * fpga_drop_firmware_data() - drop the bitfile image from memory
  68. * @priv: the driver's private data structure
  69. *
  70. * LOCKING: must hold priv->lock
  71. */
  72. static void fpga_drop_firmware_data(struct fpga_dev *priv)
  73. {
  74. videobuf_dma_free(&priv->vb);
  75. priv->vb_allocated = false;
  76. priv->bytes = 0;
  77. }
  78. /*
  79. * Private Data Reference Count
  80. */
  81. static void fpga_dev_remove(struct kref *ref)
  82. {
  83. struct fpga_dev *priv = container_of(ref, struct fpga_dev, ref);
  84. /* free any firmware image that was not programmed */
  85. fpga_drop_firmware_data(priv);
  86. mutex_destroy(&priv->lock);
  87. kfree(priv);
  88. }
  89. /*
  90. * LED Trigger (could be a seperate module)
  91. */
  92. /*
  93. * NOTE: this whole thing does have the problem that whenever the led's are
  94. * NOTE: first set to use the fpga trigger, they could be in the wrong state
  95. */
  96. DEFINE_LED_TRIGGER(ledtrig_fpga);
  97. static void ledtrig_fpga_programmed(bool enabled)
  98. {
  99. if (enabled)
  100. led_trigger_event(ledtrig_fpga, LED_FULL);
  101. else
  102. led_trigger_event(ledtrig_fpga, LED_OFF);
  103. }
  104. /*
  105. * FPGA Register Helpers
  106. */
  107. /* Register Definitions */
  108. #define FPGA_CONFIG_CONTROL 0x40
  109. #define FPGA_CONFIG_STATUS 0x44
  110. #define FPGA_CONFIG_FIFO_SIZE 0x48
  111. #define FPGA_CONFIG_FIFO_USED 0x4C
  112. #define FPGA_CONFIG_TOTAL_BYTE_COUNT 0x50
  113. #define FPGA_CONFIG_CUR_BYTE_COUNT 0x54
  114. #define FPGA_FIFO_ADDRESS 0x3000
  115. static int fpga_fifo_size(void __iomem *regs)
  116. {
  117. return ioread32be(regs + FPGA_CONFIG_FIFO_SIZE);
  118. }
  119. #define CFG_STATUS_ERR_MASK 0xfffe
  120. static int fpga_config_error(void __iomem *regs)
  121. {
  122. return ioread32be(regs + FPGA_CONFIG_STATUS) & CFG_STATUS_ERR_MASK;
  123. }
  124. static int fpga_fifo_empty(void __iomem *regs)
  125. {
  126. return ioread32be(regs + FPGA_CONFIG_FIFO_USED) == 0;
  127. }
  128. static void fpga_fifo_write(void __iomem *regs, u32 val)
  129. {
  130. iowrite32be(val, regs + FPGA_FIFO_ADDRESS);
  131. }
  132. static void fpga_set_byte_count(void __iomem *regs, u32 count)
  133. {
  134. iowrite32be(count, regs + FPGA_CONFIG_TOTAL_BYTE_COUNT);
  135. }
  136. #define CFG_CTL_ENABLE (1 << 0)
  137. #define CFG_CTL_RESET (1 << 1)
  138. #define CFG_CTL_DMA (1 << 2)
  139. static void fpga_programmer_enable(struct fpga_dev *priv, bool dma)
  140. {
  141. u32 val;
  142. val = (dma) ? (CFG_CTL_ENABLE | CFG_CTL_DMA) : CFG_CTL_ENABLE;
  143. iowrite32be(val, priv->regs + FPGA_CONFIG_CONTROL);
  144. }
  145. static void fpga_programmer_disable(struct fpga_dev *priv)
  146. {
  147. iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
  148. }
  149. static void fpga_dump_registers(struct fpga_dev *priv)
  150. {
  151. u32 control, status, size, used, total, curr;
  152. /* good status: do nothing */
  153. if (priv->status == 0)
  154. return;
  155. /* Dump all status registers */
  156. control = ioread32be(priv->regs + FPGA_CONFIG_CONTROL);
  157. status = ioread32be(priv->regs + FPGA_CONFIG_STATUS);
  158. size = ioread32be(priv->regs + FPGA_CONFIG_FIFO_SIZE);
  159. used = ioread32be(priv->regs + FPGA_CONFIG_FIFO_USED);
  160. total = ioread32be(priv->regs + FPGA_CONFIG_TOTAL_BYTE_COUNT);
  161. curr = ioread32be(priv->regs + FPGA_CONFIG_CUR_BYTE_COUNT);
  162. dev_err(priv->dev, "Configuration failed, dumping status registers\n");
  163. dev_err(priv->dev, "Control: 0x%.8x\n", control);
  164. dev_err(priv->dev, "Status: 0x%.8x\n", status);
  165. dev_err(priv->dev, "FIFO Size: 0x%.8x\n", size);
  166. dev_err(priv->dev, "FIFO Used: 0x%.8x\n", used);
  167. dev_err(priv->dev, "FIFO Total: 0x%.8x\n", total);
  168. dev_err(priv->dev, "FIFO Curr: 0x%.8x\n", curr);
  169. }
  170. /*
  171. * FPGA Power Supply Code
  172. */
  173. #define CTL_PWR_CONTROL 0x2006
  174. #define CTL_PWR_STATUS 0x200A
  175. #define CTL_PWR_FAIL 0x200B
  176. #define PWR_CONTROL_ENABLE 0x01
  177. #define PWR_STATUS_ERROR_MASK 0x10
  178. #define PWR_STATUS_GOOD 0x0f
  179. /*
  180. * Determine if the FPGA power is good for all supplies
  181. */
  182. static bool fpga_power_good(struct fpga_dev *priv)
  183. {
  184. u8 val;
  185. val = ioread8(priv->regs + CTL_PWR_STATUS);
  186. if (val & PWR_STATUS_ERROR_MASK)
  187. return false;
  188. return val == PWR_STATUS_GOOD;
  189. }
  190. /*
  191. * Disable the FPGA power supplies
  192. */
  193. static void fpga_disable_power_supplies(struct fpga_dev *priv)
  194. {
  195. unsigned long start;
  196. u8 val;
  197. iowrite8(0x0, priv->regs + CTL_PWR_CONTROL);
  198. /*
  199. * Wait 500ms for the power rails to discharge
  200. *
  201. * Without this delay, the CTL-CPLD state machine can get into a
  202. * state where it is waiting for the power-goods to assert, but they
  203. * never do. This only happens when enabling and disabling the
  204. * power sequencer very rapidly.
  205. *
  206. * The loop below will also wait for the power goods to de-assert,
  207. * but testing has shown that they are always disabled by the time
  208. * the sleep completes. However, omitting the sleep and only waiting
  209. * for the power-goods to de-assert was not sufficient to ensure
  210. * that the power sequencer would not wedge itself.
  211. */
  212. msleep(500);
  213. start = jiffies;
  214. while (time_before(jiffies, start + HZ)) {
  215. val = ioread8(priv->regs + CTL_PWR_STATUS);
  216. if (!(val & PWR_STATUS_GOOD))
  217. break;
  218. usleep_range(5000, 10000);
  219. }
  220. val = ioread8(priv->regs + CTL_PWR_STATUS);
  221. if (val & PWR_STATUS_GOOD) {
  222. dev_err(priv->dev, "power disable failed: "
  223. "power goods: status 0x%.2x\n", val);
  224. }
  225. if (val & PWR_STATUS_ERROR_MASK) {
  226. dev_err(priv->dev, "power disable failed: "
  227. "alarm bit set: status 0x%.2x\n", val);
  228. }
  229. }
  230. /**
  231. * fpga_enable_power_supplies() - enable the DATA-FPGA power supplies
  232. * @priv: the driver's private data structure
  233. *
  234. * Enable the DATA-FPGA power supplies, waiting up to 1 second for
  235. * them to enable successfully.
  236. *
  237. * Returns 0 on success, -ERRNO otherwise
  238. */
  239. static int fpga_enable_power_supplies(struct fpga_dev *priv)
  240. {
  241. unsigned long start = jiffies;
  242. if (fpga_power_good(priv)) {
  243. dev_dbg(priv->dev, "power was already good\n");
  244. return 0;
  245. }
  246. iowrite8(PWR_CONTROL_ENABLE, priv->regs + CTL_PWR_CONTROL);
  247. while (time_before(jiffies, start + HZ)) {
  248. if (fpga_power_good(priv))
  249. return 0;
  250. usleep_range(5000, 10000);
  251. }
  252. return fpga_power_good(priv) ? 0 : -ETIMEDOUT;
  253. }
  254. /*
  255. * Determine if the FPGA power supplies are all enabled
  256. */
  257. static bool fpga_power_enabled(struct fpga_dev *priv)
  258. {
  259. u8 val;
  260. val = ioread8(priv->regs + CTL_PWR_CONTROL);
  261. if (val & PWR_CONTROL_ENABLE)
  262. return true;
  263. return false;
  264. }
  265. /*
  266. * Determine if the FPGA's are programmed and running correctly
  267. */
  268. static bool fpga_running(struct fpga_dev *priv)
  269. {
  270. if (!fpga_power_good(priv))
  271. return false;
  272. /* Check the config done bit */
  273. return ioread32be(priv->regs + FPGA_CONFIG_STATUS) & (1 << 18);
  274. }
  275. /*
  276. * FPGA Programming Code
  277. */
  278. /**
  279. * fpga_program_block() - put a block of data into the programmer's FIFO
  280. * @priv: the driver's private data structure
  281. * @buf: the data to program
  282. * @count: the length of data to program (must be a multiple of 4 bytes)
  283. *
  284. * Returns 0 on success, -ERRNO otherwise
  285. */
  286. static int fpga_program_block(struct fpga_dev *priv, void *buf, size_t count)
  287. {
  288. u32 *data = buf;
  289. int size = fpga_fifo_size(priv->regs);
  290. int i, len;
  291. unsigned long timeout;
  292. /* enforce correct data length for the FIFO */
  293. BUG_ON(count % 4 != 0);
  294. while (count > 0) {
  295. /* Get the size of the block to write (maximum is FIFO_SIZE) */
  296. len = min_t(size_t, count, size);
  297. timeout = jiffies + HZ / 4;
  298. /* Write the block */
  299. for (i = 0; i < len / 4; i++)
  300. fpga_fifo_write(priv->regs, data[i]);
  301. /* Update the amounts left */
  302. count -= len;
  303. data += len / 4;
  304. /* Wait for the fifo to empty */
  305. while (true) {
  306. if (fpga_fifo_empty(priv->regs)) {
  307. break;
  308. } else {
  309. dev_dbg(priv->dev, "Fifo not empty\n");
  310. cpu_relax();
  311. }
  312. if (fpga_config_error(priv->regs)) {
  313. dev_err(priv->dev, "Error detected\n");
  314. return -EIO;
  315. }
  316. if (time_after(jiffies, timeout)) {
  317. dev_err(priv->dev, "Fifo drain timeout\n");
  318. return -ETIMEDOUT;
  319. }
  320. usleep_range(5000, 10000);
  321. }
  322. }
  323. return 0;
  324. }
  325. /**
  326. * fpga_program_cpu() - program the DATA-FPGA's using the CPU
  327. * @priv: the driver's private data structure
  328. *
  329. * This is useful when the DMA programming method fails. It is possible to
  330. * wedge the Freescale DMA controller such that the DMA programming method
  331. * always fails. This method has always succeeded.
  332. *
  333. * Returns 0 on success, -ERRNO otherwise
  334. */
  335. static noinline int fpga_program_cpu(struct fpga_dev *priv)
  336. {
  337. int ret;
  338. /* Disable the programmer */
  339. fpga_programmer_disable(priv);
  340. /* Set the total byte count */
  341. fpga_set_byte_count(priv->regs, priv->bytes);
  342. dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
  343. /* Enable the controller for programming */
  344. fpga_programmer_enable(priv, false);
  345. dev_dbg(priv->dev, "enabled the controller\n");
  346. /* Write each chunk of the FPGA bitfile to FPGA programmer */
  347. ret = fpga_program_block(priv, priv->vb.vaddr, priv->bytes);
  348. if (ret)
  349. goto out_disable_controller;
  350. /* Wait for the interrupt handler to signal that programming finished */
  351. ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
  352. if (!ret) {
  353. dev_err(priv->dev, "Timed out waiting for completion\n");
  354. ret = -ETIMEDOUT;
  355. goto out_disable_controller;
  356. }
  357. /* Retrieve the status from the interrupt handler */
  358. ret = priv->status;
  359. out_disable_controller:
  360. fpga_programmer_disable(priv);
  361. return ret;
  362. }
  363. #define FIFO_DMA_ADDRESS 0xf0003000
  364. #define FIFO_MAX_LEN 4096
  365. /**
  366. * fpga_program_dma() - program the DATA-FPGA's using the DMA engine
  367. * @priv: the driver's private data structure
  368. *
  369. * Program the DATA-FPGA's using the Freescale DMA engine. This requires that
  370. * the engine is programmed such that the hardware DMA request lines can
  371. * control the entire DMA transaction. The system controller FPGA then
  372. * completely offloads the programming from the CPU.
  373. *
  374. * Returns 0 on success, -ERRNO otherwise
  375. */
  376. static noinline int fpga_program_dma(struct fpga_dev *priv)
  377. {
  378. struct videobuf_dmabuf *vb = &priv->vb;
  379. struct dma_chan *chan = priv->chan;
  380. struct dma_async_tx_descriptor *tx;
  381. size_t num_pages, len, avail = 0;
  382. struct dma_slave_config config;
  383. struct scatterlist *sg;
  384. struct sg_table table;
  385. dma_cookie_t cookie;
  386. int ret, i;
  387. /* Disable the programmer */
  388. fpga_programmer_disable(priv);
  389. /* Allocate a scatterlist for the DMA destination */
  390. num_pages = DIV_ROUND_UP(priv->bytes, FIFO_MAX_LEN);
  391. ret = sg_alloc_table(&table, num_pages, GFP_KERNEL);
  392. if (ret) {
  393. dev_err(priv->dev, "Unable to allocate dst scatterlist\n");
  394. ret = -ENOMEM;
  395. goto out_return;
  396. }
  397. /*
  398. * This is an ugly hack
  399. *
  400. * We fill in a scatterlist as if it were mapped for DMA. This is
  401. * necessary because there exists no better structure for this
  402. * inside the kernel code.
  403. *
  404. * As an added bonus, we can use the DMAEngine API for all of this,
  405. * rather than inventing another extremely similar API.
  406. */
  407. avail = priv->bytes;
  408. for_each_sg(table.sgl, sg, num_pages, i) {
  409. len = min_t(size_t, avail, FIFO_MAX_LEN);
  410. sg_dma_address(sg) = FIFO_DMA_ADDRESS;
  411. sg_dma_len(sg) = len;
  412. avail -= len;
  413. }
  414. /* Map the buffer for DMA */
  415. ret = videobuf_dma_map(priv->dev, &priv->vb);
  416. if (ret) {
  417. dev_err(priv->dev, "Unable to map buffer for DMA\n");
  418. goto out_free_table;
  419. }
  420. /*
  421. * Configure the DMA channel to transfer FIFO_SIZE / 2 bytes per
  422. * transaction, and then put it under external control
  423. */
  424. memset(&config, 0, sizeof(config));
  425. config.direction = DMA_MEM_TO_DEV;
  426. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  427. config.dst_maxburst = fpga_fifo_size(priv->regs) / 2 / 4;
  428. ret = dmaengine_slave_config(chan, &config);
  429. if (ret) {
  430. dev_err(priv->dev, "DMA slave configuration failed\n");
  431. goto out_dma_unmap;
  432. }
  433. ret = chan->device->device_control(chan, FSLDMA_EXTERNAL_START, 1);
  434. if (ret) {
  435. dev_err(priv->dev, "DMA external control setup failed\n");
  436. goto out_dma_unmap;
  437. }
  438. /* setup and submit the DMA transaction */
  439. tx = dmaengine_prep_dma_sg(chan, 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_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. ret = kstrtoul(buf, 0, &val);
  680. if (ret)
  681. return ret;
  682. if (val) {
  683. ret = fpga_enable_power_supplies(priv);
  684. if (ret)
  685. return ret;
  686. } else {
  687. fpga_do_stop(priv);
  688. fpga_disable_power_supplies(priv);
  689. }
  690. return count;
  691. }
  692. static ssize_t program_show(struct device *dev, struct device_attribute *attr,
  693. char *buf)
  694. {
  695. struct fpga_dev *priv = dev_get_drvdata(dev);
  696. return snprintf(buf, PAGE_SIZE, "%d\n", fpga_running(priv));
  697. }
  698. static ssize_t program_store(struct device *dev, struct device_attribute *attr,
  699. const char *buf, size_t count)
  700. {
  701. struct fpga_dev *priv = dev_get_drvdata(dev);
  702. unsigned long val;
  703. int ret;
  704. ret = kstrtoul(buf, 0, &val);
  705. if (ret)
  706. return ret;
  707. /* We can't have an image writer and be programming simultaneously */
  708. if (mutex_lock_interruptible(&priv->lock))
  709. return -ERESTARTSYS;
  710. /* Program or Reset the FPGA's */
  711. ret = val ? fpga_do_program(priv) : fpga_do_stop(priv);
  712. if (ret)
  713. goto out_unlock;
  714. /* Success */
  715. ret = count;
  716. out_unlock:
  717. mutex_unlock(&priv->lock);
  718. return ret;
  719. }
  720. static DEVICE_ATTR(power_fail, S_IRUGO, pfail_show, NULL);
  721. static DEVICE_ATTR(power_good, S_IRUGO, pgood_show, NULL);
  722. static DEVICE_ATTR(power_enable, S_IRUGO | S_IWUSR,
  723. penable_show, penable_store);
  724. static DEVICE_ATTR(program, S_IRUGO | S_IWUSR,
  725. program_show, program_store);
  726. static struct attribute *fpga_attributes[] = {
  727. &dev_attr_power_fail.attr,
  728. &dev_attr_power_good.attr,
  729. &dev_attr_power_enable.attr,
  730. &dev_attr_program.attr,
  731. NULL,
  732. };
  733. static const struct attribute_group fpga_attr_group = {
  734. .attrs = fpga_attributes,
  735. };
  736. /*
  737. * OpenFirmware Device Subsystem
  738. */
  739. #define SYS_REG_VERSION 0x00
  740. #define SYS_REG_GEOGRAPHIC 0x10
  741. static bool dma_filter(struct dma_chan *chan, void *data)
  742. {
  743. /*
  744. * DMA Channel #0 is the only acceptable device
  745. *
  746. * This probably won't survive an unload/load cycle of the Freescale
  747. * DMAEngine driver, but that won't be a problem
  748. */
  749. return chan->chan_id == 0 && chan->device->dev_id == 0;
  750. }
  751. static int fpga_of_remove(struct platform_device *op)
  752. {
  753. struct fpga_dev *priv = platform_get_drvdata(op);
  754. struct device *this_device = priv->miscdev.this_device;
  755. sysfs_remove_group(&this_device->kobj, &fpga_attr_group);
  756. misc_deregister(&priv->miscdev);
  757. free_irq(priv->irq, priv);
  758. irq_dispose_mapping(priv->irq);
  759. /* make sure the power supplies are off */
  760. fpga_disable_power_supplies(priv);
  761. /* unmap registers */
  762. iounmap(priv->immr);
  763. iounmap(priv->regs);
  764. dma_release_channel(priv->chan);
  765. /* drop our reference to the private data structure */
  766. kref_put(&priv->ref, fpga_dev_remove);
  767. return 0;
  768. }
  769. /* CTL-CPLD Version Register */
  770. #define CTL_CPLD_VERSION 0x2000
  771. static int fpga_of_probe(struct platform_device *op)
  772. {
  773. struct device_node *of_node = op->dev.of_node;
  774. struct device *this_device;
  775. struct fpga_dev *priv;
  776. dma_cap_mask_t mask;
  777. u32 ver;
  778. int ret;
  779. /* Allocate private data */
  780. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  781. if (!priv) {
  782. dev_err(&op->dev, "Unable to allocate private data\n");
  783. ret = -ENOMEM;
  784. goto out_return;
  785. }
  786. /* Setup the miscdevice */
  787. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  788. priv->miscdev.name = drv_name;
  789. priv->miscdev.fops = &fpga_fops;
  790. kref_init(&priv->ref);
  791. platform_set_drvdata(op, priv);
  792. priv->dev = &op->dev;
  793. mutex_init(&priv->lock);
  794. init_completion(&priv->completion);
  795. videobuf_dma_init(&priv->vb);
  796. dev_set_drvdata(priv->dev, priv);
  797. dma_cap_zero(mask);
  798. dma_cap_set(DMA_MEMCPY, mask);
  799. dma_cap_set(DMA_SLAVE, mask);
  800. dma_cap_set(DMA_SG, mask);
  801. /* Get control of DMA channel #0 */
  802. priv->chan = dma_request_channel(mask, dma_filter, NULL);
  803. if (!priv->chan) {
  804. dev_err(&op->dev, "Unable to acquire DMA channel #0\n");
  805. ret = -ENODEV;
  806. goto out_free_priv;
  807. }
  808. /* Remap the registers for use */
  809. priv->regs = of_iomap(of_node, 0);
  810. if (!priv->regs) {
  811. dev_err(&op->dev, "Unable to ioremap registers\n");
  812. ret = -ENOMEM;
  813. goto out_dma_release_channel;
  814. }
  815. /* Remap the IMMR for use */
  816. priv->immr = ioremap(get_immrbase(), 0x100000);
  817. if (!priv->immr) {
  818. dev_err(&op->dev, "Unable to ioremap IMMR\n");
  819. ret = -ENOMEM;
  820. goto out_unmap_regs;
  821. }
  822. /*
  823. * Check that external DMA is configured
  824. *
  825. * U-Boot does this for us, but we should check it and bail out if
  826. * there is a problem. Failing to have this register setup correctly
  827. * will cause the DMA controller to transfer a single cacheline
  828. * worth of data, then wedge itself.
  829. */
  830. if ((ioread32be(priv->immr + 0x114) & 0xE00) != 0xE00) {
  831. dev_err(&op->dev, "External DMA control not configured\n");
  832. ret = -ENODEV;
  833. goto out_unmap_immr;
  834. }
  835. /*
  836. * Check the CTL-CPLD version
  837. *
  838. * This driver uses the CTL-CPLD DATA-FPGA power sequencer, and we
  839. * don't want to run on any version of the CTL-CPLD that does not use
  840. * a compatible register layout.
  841. *
  842. * v2: changed register layout, added power sequencer
  843. * v3: added glitch filter on the i2c overcurrent/overtemp outputs
  844. */
  845. ver = ioread8(priv->regs + CTL_CPLD_VERSION);
  846. if (ver != 0x02 && ver != 0x03) {
  847. dev_err(&op->dev, "CTL-CPLD is not version 0x02 or 0x03!\n");
  848. ret = -ENODEV;
  849. goto out_unmap_immr;
  850. }
  851. /* Set the exact size that the firmware image should be */
  852. ver = ioread32be(priv->regs + SYS_REG_VERSION);
  853. priv->fw_size = (ver & (1 << 18)) ? FW_SIZE_EP2S130 : FW_SIZE_EP2S90;
  854. /* Find the correct IRQ number */
  855. priv->irq = irq_of_parse_and_map(of_node, 0);
  856. if (priv->irq == NO_IRQ) {
  857. dev_err(&op->dev, "Unable to find IRQ line\n");
  858. ret = -ENODEV;
  859. goto out_unmap_immr;
  860. }
  861. /* Request the IRQ */
  862. ret = request_irq(priv->irq, fpga_irq, IRQF_SHARED, drv_name, priv);
  863. if (ret) {
  864. dev_err(&op->dev, "Unable to request IRQ %d\n", priv->irq);
  865. ret = -ENODEV;
  866. goto out_irq_dispose_mapping;
  867. }
  868. /* Reset and stop the FPGA's, just in case */
  869. fpga_do_stop(priv);
  870. /* Register the miscdevice */
  871. ret = misc_register(&priv->miscdev);
  872. if (ret) {
  873. dev_err(&op->dev, "Unable to register miscdevice\n");
  874. goto out_free_irq;
  875. }
  876. /* Create the sysfs files */
  877. this_device = priv->miscdev.this_device;
  878. dev_set_drvdata(this_device, priv);
  879. ret = sysfs_create_group(&this_device->kobj, &fpga_attr_group);
  880. if (ret) {
  881. dev_err(&op->dev, "Unable to create sysfs files\n");
  882. goto out_misc_deregister;
  883. }
  884. dev_info(priv->dev, "CARMA FPGA Programmer: %s rev%s with %s FPGAs\n",
  885. (ver & (1 << 17)) ? "Correlator" : "Digitizer",
  886. (ver & (1 << 16)) ? "B" : "A",
  887. (ver & (1 << 18)) ? "EP2S130" : "EP2S90");
  888. return 0;
  889. out_misc_deregister:
  890. misc_deregister(&priv->miscdev);
  891. out_free_irq:
  892. free_irq(priv->irq, priv);
  893. out_irq_dispose_mapping:
  894. irq_dispose_mapping(priv->irq);
  895. out_unmap_immr:
  896. iounmap(priv->immr);
  897. out_unmap_regs:
  898. iounmap(priv->regs);
  899. out_dma_release_channel:
  900. dma_release_channel(priv->chan);
  901. out_free_priv:
  902. kref_put(&priv->ref, fpga_dev_remove);
  903. out_return:
  904. return ret;
  905. }
  906. static struct of_device_id fpga_of_match[] = {
  907. { .compatible = "carma,fpga-programmer", },
  908. {},
  909. };
  910. static struct platform_driver fpga_of_driver = {
  911. .probe = fpga_of_probe,
  912. .remove = fpga_of_remove,
  913. .driver = {
  914. .name = drv_name,
  915. .of_match_table = fpga_of_match,
  916. .owner = THIS_MODULE,
  917. },
  918. };
  919. /*
  920. * Module Init / Exit
  921. */
  922. static int __init fpga_init(void)
  923. {
  924. led_trigger_register_simple("fpga", &ledtrig_fpga);
  925. return platform_driver_register(&fpga_of_driver);
  926. }
  927. static void __exit fpga_exit(void)
  928. {
  929. platform_driver_unregister(&fpga_of_driver);
  930. led_trigger_unregister_simple(ledtrig_fpga);
  931. }
  932. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  933. MODULE_DESCRIPTION("CARMA Board DATA-FPGA Programmer");
  934. MODULE_LICENSE("GPL");
  935. module_init(fpga_init);
  936. module_exit(fpga_exit);