zynq-fpga.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (c) 2011-2015 Xilinx Inc.
  3. * Copyright (c) 2015, National Instruments Corp.
  4. *
  5. * FPGA Manager Driver for Xilinx Zynq, heavily based on xdevcfg driver
  6. * in their vendor tree.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/fpga/fpga-mgr.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/iopoll.h>
  25. #include <linux/module.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/pm.h>
  30. #include <linux/regmap.h>
  31. #include <linux/string.h>
  32. /* Offsets into SLCR regmap */
  33. /* FPGA Software Reset Control */
  34. #define SLCR_FPGA_RST_CTRL_OFFSET 0x240
  35. /* Level Shifters Enable */
  36. #define SLCR_LVL_SHFTR_EN_OFFSET 0x900
  37. /* Constant Definitions */
  38. /* Control Register */
  39. #define CTRL_OFFSET 0x00
  40. /* Lock Register */
  41. #define LOCK_OFFSET 0x04
  42. /* Interrupt Status Register */
  43. #define INT_STS_OFFSET 0x0c
  44. /* Interrupt Mask Register */
  45. #define INT_MASK_OFFSET 0x10
  46. /* Status Register */
  47. #define STATUS_OFFSET 0x14
  48. /* DMA Source Address Register */
  49. #define DMA_SRC_ADDR_OFFSET 0x18
  50. /* DMA Destination Address Reg */
  51. #define DMA_DST_ADDR_OFFSET 0x1c
  52. /* DMA Source Transfer Length */
  53. #define DMA_SRC_LEN_OFFSET 0x20
  54. /* DMA Destination Transfer */
  55. #define DMA_DEST_LEN_OFFSET 0x24
  56. /* Unlock Register */
  57. #define UNLOCK_OFFSET 0x34
  58. /* Misc. Control Register */
  59. #define MCTRL_OFFSET 0x80
  60. /* Control Register Bit definitions */
  61. /* Signal to reset FPGA */
  62. #define CTRL_PCFG_PROG_B_MASK BIT(30)
  63. /* Enable PCAP for PR */
  64. #define CTRL_PCAP_PR_MASK BIT(27)
  65. /* Enable PCAP */
  66. #define CTRL_PCAP_MODE_MASK BIT(26)
  67. /* Miscellaneous Control Register bit definitions */
  68. /* Internal PCAP loopback */
  69. #define MCTRL_PCAP_LPBK_MASK BIT(4)
  70. /* Status register bit definitions */
  71. /* FPGA init status */
  72. #define STATUS_DMA_Q_F BIT(31)
  73. #define STATUS_PCFG_INIT_MASK BIT(4)
  74. /* Interrupt Status/Mask Register Bit definitions */
  75. /* DMA command done */
  76. #define IXR_DMA_DONE_MASK BIT(13)
  77. /* DMA and PCAP cmd done */
  78. #define IXR_D_P_DONE_MASK BIT(12)
  79. /* FPGA programmed */
  80. #define IXR_PCFG_DONE_MASK BIT(2)
  81. #define IXR_ERROR_FLAGS_MASK 0x00F0F860
  82. #define IXR_ALL_MASK 0xF8F7F87F
  83. /* Miscellaneous constant values */
  84. /* Invalid DMA addr */
  85. #define DMA_INVALID_ADDRESS GENMASK(31, 0)
  86. /* Used to unlock the dev */
  87. #define UNLOCK_MASK 0x757bdf0d
  88. /* Timeout for DMA to complete */
  89. #define DMA_DONE_TIMEOUT msecs_to_jiffies(1000)
  90. /* Timeout for polling reset bits */
  91. #define INIT_POLL_TIMEOUT 2500000
  92. /* Delay for polling reset bits */
  93. #define INIT_POLL_DELAY 20
  94. /* Masks for controlling stuff in SLCR */
  95. /* Disable all Level shifters */
  96. #define LVL_SHFTR_DISABLE_ALL_MASK 0x0
  97. /* Enable Level shifters from PS to PL */
  98. #define LVL_SHFTR_ENABLE_PS_TO_PL 0xa
  99. /* Enable Level shifters from PL to PS */
  100. #define LVL_SHFTR_ENABLE_PL_TO_PS 0xf
  101. /* Enable global resets */
  102. #define FPGA_RST_ALL_MASK 0xf
  103. /* Disable global resets */
  104. #define FPGA_RST_NONE_MASK 0x0
  105. struct zynq_fpga_priv {
  106. struct device *dev;
  107. int irq;
  108. struct clk *clk;
  109. void __iomem *io_base;
  110. struct regmap *slcr;
  111. struct completion dma_done;
  112. };
  113. static inline void zynq_fpga_write(struct zynq_fpga_priv *priv, u32 offset,
  114. u32 val)
  115. {
  116. writel(val, priv->io_base + offset);
  117. }
  118. static inline u32 zynq_fpga_read(const struct zynq_fpga_priv *priv,
  119. u32 offset)
  120. {
  121. return readl(priv->io_base + offset);
  122. }
  123. #define zynq_fpga_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
  124. readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \
  125. timeout_us)
  126. static void zynq_fpga_mask_irqs(struct zynq_fpga_priv *priv)
  127. {
  128. u32 intr_mask;
  129. intr_mask = zynq_fpga_read(priv, INT_MASK_OFFSET);
  130. zynq_fpga_write(priv, INT_MASK_OFFSET,
  131. intr_mask | IXR_DMA_DONE_MASK | IXR_ERROR_FLAGS_MASK);
  132. }
  133. static void zynq_fpga_unmask_irqs(struct zynq_fpga_priv *priv)
  134. {
  135. u32 intr_mask;
  136. intr_mask = zynq_fpga_read(priv, INT_MASK_OFFSET);
  137. zynq_fpga_write(priv, INT_MASK_OFFSET,
  138. intr_mask
  139. & ~(IXR_D_P_DONE_MASK | IXR_ERROR_FLAGS_MASK));
  140. }
  141. static irqreturn_t zynq_fpga_isr(int irq, void *data)
  142. {
  143. struct zynq_fpga_priv *priv = data;
  144. /* disable DMA and error IRQs */
  145. zynq_fpga_mask_irqs(priv);
  146. complete(&priv->dma_done);
  147. return IRQ_HANDLED;
  148. }
  149. static int zynq_fpga_ops_write_init(struct fpga_manager *mgr, u32 flags,
  150. const char *buf, size_t count)
  151. {
  152. struct zynq_fpga_priv *priv;
  153. u32 ctrl, status;
  154. int err;
  155. priv = mgr->priv;
  156. err = clk_enable(priv->clk);
  157. if (err)
  158. return err;
  159. /* don't globally reset PL if we're doing partial reconfig */
  160. if (!(flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  161. /* assert AXI interface resets */
  162. regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
  163. FPGA_RST_ALL_MASK);
  164. /* disable all level shifters */
  165. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  166. LVL_SHFTR_DISABLE_ALL_MASK);
  167. /* enable level shifters from PS to PL */
  168. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  169. LVL_SHFTR_ENABLE_PS_TO_PL);
  170. /* create a rising edge on PCFG_INIT. PCFG_INIT follows
  171. * PCFG_PROG_B, so we need to poll it after setting PCFG_PROG_B
  172. * to make sure the rising edge actually happens.
  173. * Note: PCFG_PROG_B is low active, sequence as described in
  174. * UG585 v1.10 page 211
  175. */
  176. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  177. ctrl |= CTRL_PCFG_PROG_B_MASK;
  178. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  179. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  180. status & STATUS_PCFG_INIT_MASK,
  181. INIT_POLL_DELAY,
  182. INIT_POLL_TIMEOUT);
  183. if (err) {
  184. dev_err(priv->dev, "Timeout waiting for PCFG_INIT");
  185. goto out_err;
  186. }
  187. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  188. ctrl &= ~CTRL_PCFG_PROG_B_MASK;
  189. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  190. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  191. !(status & STATUS_PCFG_INIT_MASK),
  192. INIT_POLL_DELAY,
  193. INIT_POLL_TIMEOUT);
  194. if (err) {
  195. dev_err(priv->dev, "Timeout waiting for !PCFG_INIT");
  196. goto out_err;
  197. }
  198. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  199. ctrl |= CTRL_PCFG_PROG_B_MASK;
  200. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  201. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  202. status & STATUS_PCFG_INIT_MASK,
  203. INIT_POLL_DELAY,
  204. INIT_POLL_TIMEOUT);
  205. if (err) {
  206. dev_err(priv->dev, "Timeout waiting for PCFG_INIT");
  207. goto out_err;
  208. }
  209. }
  210. /* set configuration register with following options:
  211. * - enable PCAP interface
  212. * - set throughput for maximum speed
  213. * - set CPU in user mode
  214. */
  215. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  216. zynq_fpga_write(priv, CTRL_OFFSET,
  217. (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK | ctrl));
  218. /* check that we have room in the command queue */
  219. status = zynq_fpga_read(priv, STATUS_OFFSET);
  220. if (status & STATUS_DMA_Q_F) {
  221. dev_err(priv->dev, "DMA command queue full");
  222. err = -EBUSY;
  223. goto out_err;
  224. }
  225. /* ensure internal PCAP loopback is disabled */
  226. ctrl = zynq_fpga_read(priv, MCTRL_OFFSET);
  227. zynq_fpga_write(priv, MCTRL_OFFSET, (~MCTRL_PCAP_LPBK_MASK & ctrl));
  228. clk_disable(priv->clk);
  229. return 0;
  230. out_err:
  231. clk_disable(priv->clk);
  232. return err;
  233. }
  234. static int zynq_fpga_ops_write(struct fpga_manager *mgr,
  235. const char *buf, size_t count)
  236. {
  237. struct zynq_fpga_priv *priv;
  238. int err;
  239. char *kbuf;
  240. size_t in_count;
  241. dma_addr_t dma_addr;
  242. u32 transfer_length;
  243. u32 intr_status;
  244. in_count = count;
  245. priv = mgr->priv;
  246. kbuf = dma_alloc_coherent(priv->dev, count, &dma_addr, GFP_KERNEL);
  247. if (!kbuf)
  248. return -ENOMEM;
  249. memcpy(kbuf, buf, count);
  250. /* enable clock */
  251. err = clk_enable(priv->clk);
  252. if (err)
  253. goto out_free;
  254. zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
  255. reinit_completion(&priv->dma_done);
  256. /* enable DMA and error IRQs */
  257. zynq_fpga_unmask_irqs(priv);
  258. /* the +1 in the src addr is used to hold off on DMA_DONE IRQ
  259. * until both AXI and PCAP are done ...
  260. */
  261. zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, (u32)(dma_addr) + 1);
  262. zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, (u32)DMA_INVALID_ADDRESS);
  263. /* convert #bytes to #words */
  264. transfer_length = (count + 3) / 4;
  265. zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, transfer_length);
  266. zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0);
  267. wait_for_completion(&priv->dma_done);
  268. intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
  269. zynq_fpga_write(priv, INT_STS_OFFSET, intr_status);
  270. if (!((intr_status & IXR_D_P_DONE_MASK) == IXR_D_P_DONE_MASK)) {
  271. dev_err(priv->dev, "Error configuring FPGA");
  272. err = -EFAULT;
  273. }
  274. clk_disable(priv->clk);
  275. out_free:
  276. dma_free_coherent(priv->dev, in_count, kbuf, dma_addr);
  277. return err;
  278. }
  279. static int zynq_fpga_ops_write_complete(struct fpga_manager *mgr, u32 flags)
  280. {
  281. struct zynq_fpga_priv *priv = mgr->priv;
  282. int err;
  283. u32 intr_status;
  284. err = clk_enable(priv->clk);
  285. if (err)
  286. return err;
  287. err = zynq_fpga_poll_timeout(priv, INT_STS_OFFSET, intr_status,
  288. intr_status & IXR_PCFG_DONE_MASK,
  289. INIT_POLL_DELAY,
  290. INIT_POLL_TIMEOUT);
  291. clk_disable(priv->clk);
  292. if (err)
  293. return err;
  294. /* for the partial reconfig case we didn't touch the level shifters */
  295. if (!(flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  296. /* enable level shifters from PL to PS */
  297. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  298. LVL_SHFTR_ENABLE_PL_TO_PS);
  299. /* deassert AXI interface resets */
  300. regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
  301. FPGA_RST_NONE_MASK);
  302. }
  303. return 0;
  304. }
  305. static enum fpga_mgr_states zynq_fpga_ops_state(struct fpga_manager *mgr)
  306. {
  307. int err;
  308. u32 intr_status;
  309. struct zynq_fpga_priv *priv;
  310. priv = mgr->priv;
  311. err = clk_enable(priv->clk);
  312. if (err)
  313. return FPGA_MGR_STATE_UNKNOWN;
  314. intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
  315. clk_disable(priv->clk);
  316. if (intr_status & IXR_PCFG_DONE_MASK)
  317. return FPGA_MGR_STATE_OPERATING;
  318. return FPGA_MGR_STATE_UNKNOWN;
  319. }
  320. static const struct fpga_manager_ops zynq_fpga_ops = {
  321. .state = zynq_fpga_ops_state,
  322. .write_init = zynq_fpga_ops_write_init,
  323. .write = zynq_fpga_ops_write,
  324. .write_complete = zynq_fpga_ops_write_complete,
  325. };
  326. static int zynq_fpga_probe(struct platform_device *pdev)
  327. {
  328. struct device *dev = &pdev->dev;
  329. struct zynq_fpga_priv *priv;
  330. struct resource *res;
  331. int err;
  332. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  333. if (!priv)
  334. return -ENOMEM;
  335. priv->dev = dev;
  336. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  337. priv->io_base = devm_ioremap_resource(dev, res);
  338. if (IS_ERR(priv->io_base))
  339. return PTR_ERR(priv->io_base);
  340. priv->slcr = syscon_regmap_lookup_by_phandle(dev->of_node,
  341. "syscon");
  342. if (IS_ERR(priv->slcr)) {
  343. dev_err(dev, "unable to get zynq-slcr regmap");
  344. return PTR_ERR(priv->slcr);
  345. }
  346. init_completion(&priv->dma_done);
  347. priv->irq = platform_get_irq(pdev, 0);
  348. if (priv->irq < 0) {
  349. dev_err(dev, "No IRQ available");
  350. return priv->irq;
  351. }
  352. err = devm_request_irq(dev, priv->irq, zynq_fpga_isr, 0,
  353. dev_name(dev), priv);
  354. if (err) {
  355. dev_err(dev, "unable to request IRQ");
  356. return err;
  357. }
  358. priv->clk = devm_clk_get(dev, "ref_clk");
  359. if (IS_ERR(priv->clk)) {
  360. dev_err(dev, "input clock not found");
  361. return PTR_ERR(priv->clk);
  362. }
  363. err = clk_prepare_enable(priv->clk);
  364. if (err) {
  365. dev_err(dev, "unable to enable clock");
  366. return err;
  367. }
  368. /* unlock the device */
  369. zynq_fpga_write(priv, UNLOCK_OFFSET, UNLOCK_MASK);
  370. clk_disable(priv->clk);
  371. err = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
  372. &zynq_fpga_ops, priv);
  373. if (err) {
  374. dev_err(dev, "unable to register FPGA manager");
  375. clk_unprepare(priv->clk);
  376. return err;
  377. }
  378. return 0;
  379. }
  380. static int zynq_fpga_remove(struct platform_device *pdev)
  381. {
  382. struct zynq_fpga_priv *priv;
  383. struct fpga_manager *mgr;
  384. mgr = platform_get_drvdata(pdev);
  385. priv = mgr->priv;
  386. fpga_mgr_unregister(&pdev->dev);
  387. clk_unprepare(priv->clk);
  388. return 0;
  389. }
  390. #ifdef CONFIG_OF
  391. static const struct of_device_id zynq_fpga_of_match[] = {
  392. { .compatible = "xlnx,zynq-devcfg-1.0", },
  393. {},
  394. };
  395. MODULE_DEVICE_TABLE(of, zynq_fpga_of_match);
  396. #endif
  397. static struct platform_driver zynq_fpga_driver = {
  398. .probe = zynq_fpga_probe,
  399. .remove = zynq_fpga_remove,
  400. .driver = {
  401. .name = "zynq_fpga_manager",
  402. .of_match_table = of_match_ptr(zynq_fpga_of_match),
  403. },
  404. };
  405. module_platform_driver(zynq_fpga_driver);
  406. MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
  407. MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
  408. MODULE_DESCRIPTION("Xilinx Zynq FPGA Manager");
  409. MODULE_LICENSE("GPL v2");