carma-fpga-program.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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 = chan->device->device_control(chan, DMA_SLAVE_CONFIG,
  429. (unsigned long)&config);
  430. if (ret) {
  431. dev_err(priv->dev, "DMA slave configuration failed\n");
  432. goto out_dma_unmap;
  433. }
  434. ret = chan->device->device_control(chan, FSLDMA_EXTERNAL_START, 1);
  435. if (ret) {
  436. dev_err(priv->dev, "DMA external control setup failed\n");
  437. goto out_dma_unmap;
  438. }
  439. /* setup and submit the DMA transaction */
  440. tx = chan->device->device_prep_dma_sg(chan,
  441. table.sgl, num_pages,
  442. vb->sglist, vb->sglen, 0);
  443. if (!tx) {
  444. dev_err(priv->dev, "Unable to prep DMA transaction\n");
  445. ret = -ENOMEM;
  446. goto out_dma_unmap;
  447. }
  448. cookie = tx->tx_submit(tx);
  449. if (dma_submit_error(cookie)) {
  450. dev_err(priv->dev, "Unable to submit DMA transaction\n");
  451. ret = -ENOMEM;
  452. goto out_dma_unmap;
  453. }
  454. dma_async_issue_pending(chan);
  455. /* Set the total byte count */
  456. fpga_set_byte_count(priv->regs, priv->bytes);
  457. dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
  458. /* Enable the controller for DMA programming */
  459. fpga_programmer_enable(priv, true);
  460. dev_dbg(priv->dev, "enabled the controller\n");
  461. /* Wait for the interrupt handler to signal that programming finished */
  462. ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
  463. if (!ret) {
  464. dev_err(priv->dev, "Timed out waiting for completion\n");
  465. ret = -ETIMEDOUT;
  466. goto out_disable_controller;
  467. }
  468. /* Retrieve the status from the interrupt handler */
  469. ret = priv->status;
  470. out_disable_controller:
  471. fpga_programmer_disable(priv);
  472. out_dma_unmap:
  473. videobuf_dma_unmap(priv->dev, vb);
  474. out_free_table:
  475. sg_free_table(&table);
  476. out_return:
  477. return ret;
  478. }
  479. /*
  480. * Interrupt Handling
  481. */
  482. static irqreturn_t fpga_irq(int irq, void *dev_id)
  483. {
  484. struct fpga_dev *priv = dev_id;
  485. /* Save the status */
  486. priv->status = fpga_config_error(priv->regs) ? -EIO : 0;
  487. dev_dbg(priv->dev, "INTERRUPT status %d\n", priv->status);
  488. fpga_dump_registers(priv);
  489. /* Disabling the programmer clears the interrupt */
  490. fpga_programmer_disable(priv);
  491. /* Notify any waiters */
  492. complete(&priv->completion);
  493. return IRQ_HANDLED;
  494. }
  495. /*
  496. * SYSFS Helpers
  497. */
  498. /**
  499. * fpga_do_stop() - deconfigure (reset) the DATA-FPGA's
  500. * @priv: the driver's private data structure
  501. *
  502. * LOCKING: must hold priv->lock
  503. */
  504. static int fpga_do_stop(struct fpga_dev *priv)
  505. {
  506. u32 val;
  507. /* Set the led to unprogrammed */
  508. ledtrig_fpga_programmed(false);
  509. /* Pulse the config line to reset the FPGA's */
  510. val = CFG_CTL_ENABLE | CFG_CTL_RESET;
  511. iowrite32be(val, priv->regs + FPGA_CONFIG_CONTROL);
  512. iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
  513. return 0;
  514. }
  515. static noinline int fpga_do_program(struct fpga_dev *priv)
  516. {
  517. int ret;
  518. if (priv->bytes != priv->fw_size) {
  519. dev_err(priv->dev, "Incorrect bitfile size: got %zu bytes, "
  520. "should be %zu bytes\n",
  521. priv->bytes, priv->fw_size);
  522. return -EINVAL;
  523. }
  524. if (!fpga_power_enabled(priv)) {
  525. dev_err(priv->dev, "Power not enabled\n");
  526. return -EINVAL;
  527. }
  528. if (!fpga_power_good(priv)) {
  529. dev_err(priv->dev, "Power not good\n");
  530. return -EINVAL;
  531. }
  532. /* Set the LED to unprogrammed */
  533. ledtrig_fpga_programmed(false);
  534. /* Try to program the FPGA's using DMA */
  535. ret = fpga_program_dma(priv);
  536. /* If DMA failed or doesn't exist, try with CPU */
  537. if (ret) {
  538. dev_warn(priv->dev, "Falling back to CPU programming\n");
  539. ret = fpga_program_cpu(priv);
  540. }
  541. if (ret) {
  542. dev_err(priv->dev, "Unable to program FPGA's\n");
  543. return ret;
  544. }
  545. /* Drop the firmware bitfile from memory */
  546. fpga_drop_firmware_data(priv);
  547. dev_dbg(priv->dev, "FPGA programming successful\n");
  548. ledtrig_fpga_programmed(true);
  549. return 0;
  550. }
  551. /*
  552. * File Operations
  553. */
  554. static int fpga_open(struct inode *inode, struct file *filp)
  555. {
  556. /*
  557. * The miscdevice layer puts our struct miscdevice into the
  558. * filp->private_data field. We use this to find our private
  559. * data and then overwrite it with our own private structure.
  560. */
  561. struct fpga_dev *priv = container_of(filp->private_data,
  562. struct fpga_dev, miscdev);
  563. unsigned int nr_pages;
  564. int ret;
  565. /* We only allow one process at a time */
  566. ret = mutex_lock_interruptible(&priv->lock);
  567. if (ret)
  568. return ret;
  569. filp->private_data = priv;
  570. kref_get(&priv->ref);
  571. /* Truncation: drop any existing data */
  572. if (filp->f_flags & O_TRUNC)
  573. priv->bytes = 0;
  574. /* Check if we have already allocated a buffer */
  575. if (priv->vb_allocated)
  576. return 0;
  577. /* Allocate a buffer to hold enough data for the bitfile */
  578. nr_pages = DIV_ROUND_UP(priv->fw_size, PAGE_SIZE);
  579. ret = videobuf_dma_init_kernel(&priv->vb, DMA_TO_DEVICE, nr_pages);
  580. if (ret) {
  581. dev_err(priv->dev, "unable to allocate data buffer\n");
  582. mutex_unlock(&priv->lock);
  583. kref_put(&priv->ref, fpga_dev_remove);
  584. return ret;
  585. }
  586. priv->vb_allocated = true;
  587. return 0;
  588. }
  589. static int fpga_release(struct inode *inode, struct file *filp)
  590. {
  591. struct fpga_dev *priv = filp->private_data;
  592. mutex_unlock(&priv->lock);
  593. kref_put(&priv->ref, fpga_dev_remove);
  594. return 0;
  595. }
  596. static ssize_t fpga_write(struct file *filp, const char __user *buf,
  597. size_t count, loff_t *f_pos)
  598. {
  599. struct fpga_dev *priv = filp->private_data;
  600. /* FPGA bitfiles have an exact size: disallow anything else */
  601. if (priv->bytes >= priv->fw_size)
  602. return -ENOSPC;
  603. count = min_t(size_t, priv->fw_size - priv->bytes, count);
  604. if (copy_from_user(priv->vb.vaddr + priv->bytes, buf, count))
  605. return -EFAULT;
  606. priv->bytes += count;
  607. return count;
  608. }
  609. static ssize_t fpga_read(struct file *filp, char __user *buf, size_t count,
  610. loff_t *f_pos)
  611. {
  612. struct fpga_dev *priv = filp->private_data;
  613. count = min_t(size_t, priv->bytes - *f_pos, count);
  614. if (copy_to_user(buf, priv->vb.vaddr + *f_pos, count))
  615. return -EFAULT;
  616. *f_pos += count;
  617. return count;
  618. }
  619. static loff_t fpga_llseek(struct file *filp, loff_t offset, int origin)
  620. {
  621. struct fpga_dev *priv = filp->private_data;
  622. loff_t newpos;
  623. /* only read-only opens are allowed to seek */
  624. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  625. return -EINVAL;
  626. switch (origin) {
  627. case SEEK_SET: /* seek relative to the beginning of the file */
  628. newpos = offset;
  629. break;
  630. case SEEK_CUR: /* seek relative to current position in the file */
  631. newpos = filp->f_pos + offset;
  632. break;
  633. case SEEK_END: /* seek relative to the end of the file */
  634. newpos = priv->fw_size - offset;
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. /* check for sanity */
  640. if (newpos > priv->fw_size)
  641. return -EINVAL;
  642. filp->f_pos = newpos;
  643. return newpos;
  644. }
  645. static const struct file_operations fpga_fops = {
  646. .open = fpga_open,
  647. .release = fpga_release,
  648. .write = fpga_write,
  649. .read = fpga_read,
  650. .llseek = fpga_llseek,
  651. };
  652. /*
  653. * Device Attributes
  654. */
  655. static ssize_t pfail_show(struct device *dev, struct device_attribute *attr,
  656. char *buf)
  657. {
  658. struct fpga_dev *priv = dev_get_drvdata(dev);
  659. u8 val;
  660. val = ioread8(priv->regs + CTL_PWR_FAIL);
  661. return snprintf(buf, PAGE_SIZE, "0x%.2x\n", val);
  662. }
  663. static ssize_t pgood_show(struct device *dev, struct device_attribute *attr,
  664. char *buf)
  665. {
  666. struct fpga_dev *priv = dev_get_drvdata(dev);
  667. return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_good(priv));
  668. }
  669. static ssize_t penable_show(struct device *dev, struct device_attribute *attr,
  670. char *buf)
  671. {
  672. struct fpga_dev *priv = dev_get_drvdata(dev);
  673. return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_enabled(priv));
  674. }
  675. static ssize_t penable_store(struct device *dev, struct device_attribute *attr,
  676. const char *buf, size_t count)
  677. {
  678. struct fpga_dev *priv = dev_get_drvdata(dev);
  679. unsigned long val;
  680. int ret;
  681. ret = kstrtoul(buf, 0, &val);
  682. if (ret)
  683. return ret;
  684. if (val) {
  685. ret = fpga_enable_power_supplies(priv);
  686. if (ret)
  687. return ret;
  688. } else {
  689. fpga_do_stop(priv);
  690. fpga_disable_power_supplies(priv);
  691. }
  692. return count;
  693. }
  694. static ssize_t program_show(struct device *dev, struct device_attribute *attr,
  695. char *buf)
  696. {
  697. struct fpga_dev *priv = dev_get_drvdata(dev);
  698. return snprintf(buf, PAGE_SIZE, "%d\n", fpga_running(priv));
  699. }
  700. static ssize_t program_store(struct device *dev, struct device_attribute *attr,
  701. const char *buf, size_t count)
  702. {
  703. struct fpga_dev *priv = dev_get_drvdata(dev);
  704. unsigned long val;
  705. int ret;
  706. ret = kstrtoul(buf, 0, &val);
  707. if (ret)
  708. return ret;
  709. /* We can't have an image writer and be programming simultaneously */
  710. if (mutex_lock_interruptible(&priv->lock))
  711. return -ERESTARTSYS;
  712. /* Program or Reset the FPGA's */
  713. ret = val ? fpga_do_program(priv) : fpga_do_stop(priv);
  714. if (ret)
  715. goto out_unlock;
  716. /* Success */
  717. ret = count;
  718. out_unlock:
  719. mutex_unlock(&priv->lock);
  720. return ret;
  721. }
  722. static DEVICE_ATTR(power_fail, S_IRUGO, pfail_show, NULL);
  723. static DEVICE_ATTR(power_good, S_IRUGO, pgood_show, NULL);
  724. static DEVICE_ATTR(power_enable, S_IRUGO | S_IWUSR,
  725. penable_show, penable_store);
  726. static DEVICE_ATTR(program, S_IRUGO | S_IWUSR,
  727. program_show, program_store);
  728. static struct attribute *fpga_attributes[] = {
  729. &dev_attr_power_fail.attr,
  730. &dev_attr_power_good.attr,
  731. &dev_attr_power_enable.attr,
  732. &dev_attr_program.attr,
  733. NULL,
  734. };
  735. static const struct attribute_group fpga_attr_group = {
  736. .attrs = fpga_attributes,
  737. };
  738. /*
  739. * OpenFirmware Device Subsystem
  740. */
  741. #define SYS_REG_VERSION 0x00
  742. #define SYS_REG_GEOGRAPHIC 0x10
  743. static bool dma_filter(struct dma_chan *chan, void *data)
  744. {
  745. /*
  746. * DMA Channel #0 is the only acceptable device
  747. *
  748. * This probably won't survive an unload/load cycle of the Freescale
  749. * DMAEngine driver, but that won't be a problem
  750. */
  751. return chan->chan_id == 0 && chan->device->dev_id == 0;
  752. }
  753. static int fpga_of_remove(struct platform_device *op)
  754. {
  755. struct fpga_dev *priv = platform_get_drvdata(op);
  756. struct device *this_device = priv->miscdev.this_device;
  757. sysfs_remove_group(&this_device->kobj, &fpga_attr_group);
  758. misc_deregister(&priv->miscdev);
  759. free_irq(priv->irq, priv);
  760. irq_dispose_mapping(priv->irq);
  761. /* make sure the power supplies are off */
  762. fpga_disable_power_supplies(priv);
  763. /* unmap registers */
  764. iounmap(priv->immr);
  765. iounmap(priv->regs);
  766. dma_release_channel(priv->chan);
  767. /* drop our reference to the private data structure */
  768. kref_put(&priv->ref, fpga_dev_remove);
  769. return 0;
  770. }
  771. /* CTL-CPLD Version Register */
  772. #define CTL_CPLD_VERSION 0x2000
  773. static int fpga_of_probe(struct platform_device *op)
  774. {
  775. struct device_node *of_node = op->dev.of_node;
  776. struct device *this_device;
  777. struct fpga_dev *priv;
  778. dma_cap_mask_t mask;
  779. u32 ver;
  780. int ret;
  781. /* Allocate private data */
  782. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  783. if (!priv) {
  784. dev_err(&op->dev, "Unable to allocate private data\n");
  785. ret = -ENOMEM;
  786. goto out_return;
  787. }
  788. /* Setup the miscdevice */
  789. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  790. priv->miscdev.name = drv_name;
  791. priv->miscdev.fops = &fpga_fops;
  792. kref_init(&priv->ref);
  793. platform_set_drvdata(op, priv);
  794. priv->dev = &op->dev;
  795. mutex_init(&priv->lock);
  796. init_completion(&priv->completion);
  797. videobuf_dma_init(&priv->vb);
  798. dev_set_drvdata(priv->dev, priv);
  799. dma_cap_zero(mask);
  800. dma_cap_set(DMA_MEMCPY, mask);
  801. dma_cap_set(DMA_SLAVE, mask);
  802. dma_cap_set(DMA_SG, mask);
  803. /* Get control of DMA channel #0 */
  804. priv->chan = dma_request_channel(mask, dma_filter, NULL);
  805. if (!priv->chan) {
  806. dev_err(&op->dev, "Unable to acquire DMA channel #0\n");
  807. ret = -ENODEV;
  808. goto out_free_priv;
  809. }
  810. /* Remap the registers for use */
  811. priv->regs = of_iomap(of_node, 0);
  812. if (!priv->regs) {
  813. dev_err(&op->dev, "Unable to ioremap registers\n");
  814. ret = -ENOMEM;
  815. goto out_dma_release_channel;
  816. }
  817. /* Remap the IMMR for use */
  818. priv->immr = ioremap(get_immrbase(), 0x100000);
  819. if (!priv->immr) {
  820. dev_err(&op->dev, "Unable to ioremap IMMR\n");
  821. ret = -ENOMEM;
  822. goto out_unmap_regs;
  823. }
  824. /*
  825. * Check that external DMA is configured
  826. *
  827. * U-Boot does this for us, but we should check it and bail out if
  828. * there is a problem. Failing to have this register setup correctly
  829. * will cause the DMA controller to transfer a single cacheline
  830. * worth of data, then wedge itself.
  831. */
  832. if ((ioread32be(priv->immr + 0x114) & 0xE00) != 0xE00) {
  833. dev_err(&op->dev, "External DMA control not configured\n");
  834. ret = -ENODEV;
  835. goto out_unmap_immr;
  836. }
  837. /*
  838. * Check the CTL-CPLD version
  839. *
  840. * This driver uses the CTL-CPLD DATA-FPGA power sequencer, and we
  841. * don't want to run on any version of the CTL-CPLD that does not use
  842. * a compatible register layout.
  843. *
  844. * v2: changed register layout, added power sequencer
  845. * v3: added glitch filter on the i2c overcurrent/overtemp outputs
  846. */
  847. ver = ioread8(priv->regs + CTL_CPLD_VERSION);
  848. if (ver != 0x02 && ver != 0x03) {
  849. dev_err(&op->dev, "CTL-CPLD is not version 0x02 or 0x03!\n");
  850. ret = -ENODEV;
  851. goto out_unmap_immr;
  852. }
  853. /* Set the exact size that the firmware image should be */
  854. ver = ioread32be(priv->regs + SYS_REG_VERSION);
  855. priv->fw_size = (ver & (1 << 18)) ? FW_SIZE_EP2S130 : FW_SIZE_EP2S90;
  856. /* Find the correct IRQ number */
  857. priv->irq = irq_of_parse_and_map(of_node, 0);
  858. if (priv->irq == NO_IRQ) {
  859. dev_err(&op->dev, "Unable to find IRQ line\n");
  860. ret = -ENODEV;
  861. goto out_unmap_immr;
  862. }
  863. /* Request the IRQ */
  864. ret = request_irq(priv->irq, fpga_irq, IRQF_SHARED, drv_name, priv);
  865. if (ret) {
  866. dev_err(&op->dev, "Unable to request IRQ %d\n", priv->irq);
  867. ret = -ENODEV;
  868. goto out_irq_dispose_mapping;
  869. }
  870. /* Reset and stop the FPGA's, just in case */
  871. fpga_do_stop(priv);
  872. /* Register the miscdevice */
  873. ret = misc_register(&priv->miscdev);
  874. if (ret) {
  875. dev_err(&op->dev, "Unable to register miscdevice\n");
  876. goto out_free_irq;
  877. }
  878. /* Create the sysfs files */
  879. this_device = priv->miscdev.this_device;
  880. dev_set_drvdata(this_device, priv);
  881. ret = sysfs_create_group(&this_device->kobj, &fpga_attr_group);
  882. if (ret) {
  883. dev_err(&op->dev, "Unable to create sysfs files\n");
  884. goto out_misc_deregister;
  885. }
  886. dev_info(priv->dev, "CARMA FPGA Programmer: %s rev%s with %s FPGAs\n",
  887. (ver & (1 << 17)) ? "Correlator" : "Digitizer",
  888. (ver & (1 << 16)) ? "B" : "A",
  889. (ver & (1 << 18)) ? "EP2S130" : "EP2S90");
  890. return 0;
  891. out_misc_deregister:
  892. misc_deregister(&priv->miscdev);
  893. out_free_irq:
  894. free_irq(priv->irq, priv);
  895. out_irq_dispose_mapping:
  896. irq_dispose_mapping(priv->irq);
  897. out_unmap_immr:
  898. iounmap(priv->immr);
  899. out_unmap_regs:
  900. iounmap(priv->regs);
  901. out_dma_release_channel:
  902. dma_release_channel(priv->chan);
  903. out_free_priv:
  904. kref_put(&priv->ref, fpga_dev_remove);
  905. out_return:
  906. return ret;
  907. }
  908. static struct of_device_id fpga_of_match[] = {
  909. { .compatible = "carma,fpga-programmer", },
  910. {},
  911. };
  912. static struct platform_driver fpga_of_driver = {
  913. .probe = fpga_of_probe,
  914. .remove = fpga_of_remove,
  915. .driver = {
  916. .name = drv_name,
  917. .of_match_table = fpga_of_match,
  918. .owner = THIS_MODULE,
  919. },
  920. };
  921. /*
  922. * Module Init / Exit
  923. */
  924. static int __init fpga_init(void)
  925. {
  926. led_trigger_register_simple("fpga", &ledtrig_fpga);
  927. return platform_driver_register(&fpga_of_driver);
  928. }
  929. static void __exit fpga_exit(void)
  930. {
  931. platform_driver_unregister(&fpga_of_driver);
  932. led_trigger_unregister_simple(ledtrig_fpga);
  933. }
  934. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  935. MODULE_DESCRIPTION("CARMA Board DATA-FPGA Programmer");
  936. MODULE_LICENSE("GPL");
  937. module_init(fpga_init);
  938. module_exit(fpga_exit);