carma-fpga-program.c 28 KB

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