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_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_TO_DEVICE;
  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. const struct of_device_id *match)
  771. {
  772. struct device_node *of_node = op->dev.of_node;
  773. struct device *this_device;
  774. struct fpga_dev *priv;
  775. dma_cap_mask_t mask;
  776. u32 ver;
  777. int ret;
  778. /* Allocate private data */
  779. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  780. if (!priv) {
  781. dev_err(&op->dev, "Unable to allocate private data\n");
  782. ret = -ENOMEM;
  783. goto out_return;
  784. }
  785. /* Setup the miscdevice */
  786. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  787. priv->miscdev.name = drv_name;
  788. priv->miscdev.fops = &fpga_fops;
  789. kref_init(&priv->ref);
  790. dev_set_drvdata(&op->dev, priv);
  791. priv->dev = &op->dev;
  792. mutex_init(&priv->lock);
  793. init_completion(&priv->completion);
  794. videobuf_dma_init(&priv->vb);
  795. dev_set_drvdata(priv->dev, priv);
  796. dma_cap_zero(mask);
  797. dma_cap_set(DMA_MEMCPY, mask);
  798. dma_cap_set(DMA_INTERRUPT, 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 of_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 of_register_platform_driver(&fpga_of_driver);
  926. }
  927. static void __exit fpga_exit(void)
  928. {
  929. of_unregister_platform_driver(&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);