zynq-fpga.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #include <linux/scatterlist.h>
  33. /* Offsets into SLCR regmap */
  34. /* FPGA Software Reset Control */
  35. #define SLCR_FPGA_RST_CTRL_OFFSET 0x240
  36. /* Level Shifters Enable */
  37. #define SLCR_LVL_SHFTR_EN_OFFSET 0x900
  38. /* Constant Definitions */
  39. /* Control Register */
  40. #define CTRL_OFFSET 0x00
  41. /* Lock Register */
  42. #define LOCK_OFFSET 0x04
  43. /* Interrupt Status Register */
  44. #define INT_STS_OFFSET 0x0c
  45. /* Interrupt Mask Register */
  46. #define INT_MASK_OFFSET 0x10
  47. /* Status Register */
  48. #define STATUS_OFFSET 0x14
  49. /* DMA Source Address Register */
  50. #define DMA_SRC_ADDR_OFFSET 0x18
  51. /* DMA Destination Address Reg */
  52. #define DMA_DST_ADDR_OFFSET 0x1c
  53. /* DMA Source Transfer Length */
  54. #define DMA_SRC_LEN_OFFSET 0x20
  55. /* DMA Destination Transfer */
  56. #define DMA_DEST_LEN_OFFSET 0x24
  57. /* Unlock Register */
  58. #define UNLOCK_OFFSET 0x34
  59. /* Misc. Control Register */
  60. #define MCTRL_OFFSET 0x80
  61. /* Control Register Bit definitions */
  62. /* Signal to reset FPGA */
  63. #define CTRL_PCFG_PROG_B_MASK BIT(30)
  64. /* Enable PCAP for PR */
  65. #define CTRL_PCAP_PR_MASK BIT(27)
  66. /* Enable PCAP */
  67. #define CTRL_PCAP_MODE_MASK BIT(26)
  68. /* Lower rate to allow decrypt on the fly */
  69. #define CTRL_PCAP_RATE_EN_MASK BIT(25)
  70. /* System booted in secure mode */
  71. #define CTRL_SEC_EN_MASK BIT(7)
  72. /* Miscellaneous Control Register bit definitions */
  73. /* Internal PCAP loopback */
  74. #define MCTRL_PCAP_LPBK_MASK BIT(4)
  75. /* Status register bit definitions */
  76. /* FPGA init status */
  77. #define STATUS_DMA_Q_F BIT(31)
  78. #define STATUS_DMA_Q_E BIT(30)
  79. #define STATUS_PCFG_INIT_MASK BIT(4)
  80. /* Interrupt Status/Mask Register Bit definitions */
  81. /* DMA command done */
  82. #define IXR_DMA_DONE_MASK BIT(13)
  83. /* DMA and PCAP cmd done */
  84. #define IXR_D_P_DONE_MASK BIT(12)
  85. /* FPGA programmed */
  86. #define IXR_PCFG_DONE_MASK BIT(2)
  87. #define IXR_ERROR_FLAGS_MASK 0x00F0C860
  88. #define IXR_ALL_MASK 0xF8F7F87F
  89. /* Miscellaneous constant values */
  90. /* Invalid DMA addr */
  91. #define DMA_INVALID_ADDRESS GENMASK(31, 0)
  92. /* Used to unlock the dev */
  93. #define UNLOCK_MASK 0x757bdf0d
  94. /* Timeout for polling reset bits */
  95. #define INIT_POLL_TIMEOUT 2500000
  96. /* Delay for polling reset bits */
  97. #define INIT_POLL_DELAY 20
  98. /* Signal this is the last DMA transfer, wait for the AXI and PCAP before
  99. * interrupting
  100. */
  101. #define DMA_SRC_LAST_TRANSFER 1
  102. /* Timeout for DMA completion */
  103. #define DMA_TIMEOUT_MS 5000
  104. /* Masks for controlling stuff in SLCR */
  105. /* Disable all Level shifters */
  106. #define LVL_SHFTR_DISABLE_ALL_MASK 0x0
  107. /* Enable Level shifters from PS to PL */
  108. #define LVL_SHFTR_ENABLE_PS_TO_PL 0xa
  109. /* Enable Level shifters from PL to PS */
  110. #define LVL_SHFTR_ENABLE_PL_TO_PS 0xf
  111. /* Enable global resets */
  112. #define FPGA_RST_ALL_MASK 0xf
  113. /* Disable global resets */
  114. #define FPGA_RST_NONE_MASK 0x0
  115. struct zynq_fpga_priv {
  116. int irq;
  117. struct clk *clk;
  118. void __iomem *io_base;
  119. struct regmap *slcr;
  120. spinlock_t dma_lock;
  121. unsigned int dma_elm;
  122. unsigned int dma_nelms;
  123. struct scatterlist *cur_sg;
  124. struct completion dma_done;
  125. };
  126. static inline void zynq_fpga_write(struct zynq_fpga_priv *priv, u32 offset,
  127. u32 val)
  128. {
  129. writel(val, priv->io_base + offset);
  130. }
  131. static inline u32 zynq_fpga_read(const struct zynq_fpga_priv *priv,
  132. u32 offset)
  133. {
  134. return readl(priv->io_base + offset);
  135. }
  136. #define zynq_fpga_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
  137. readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \
  138. timeout_us)
  139. /* Cause the specified irq mask bits to generate IRQs */
  140. static inline void zynq_fpga_set_irq(struct zynq_fpga_priv *priv, u32 enable)
  141. {
  142. zynq_fpga_write(priv, INT_MASK_OFFSET, ~enable);
  143. }
  144. /* Must be called with dma_lock held */
  145. static void zynq_step_dma(struct zynq_fpga_priv *priv)
  146. {
  147. u32 addr;
  148. u32 len;
  149. bool first;
  150. first = priv->dma_elm == 0;
  151. while (priv->cur_sg) {
  152. /* Feed the DMA queue until it is full. */
  153. if (zynq_fpga_read(priv, STATUS_OFFSET) & STATUS_DMA_Q_F)
  154. break;
  155. addr = sg_dma_address(priv->cur_sg);
  156. len = sg_dma_len(priv->cur_sg);
  157. if (priv->dma_elm + 1 == priv->dma_nelms) {
  158. /* The last transfer waits for the PCAP to finish too,
  159. * notice this also changes the irq_mask to ignore
  160. * IXR_DMA_DONE_MASK which ensures we do not trigger
  161. * the completion too early.
  162. */
  163. addr |= DMA_SRC_LAST_TRANSFER;
  164. priv->cur_sg = NULL;
  165. } else {
  166. priv->cur_sg = sg_next(priv->cur_sg);
  167. priv->dma_elm++;
  168. }
  169. zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, addr);
  170. zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, DMA_INVALID_ADDRESS);
  171. zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, len / 4);
  172. zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0);
  173. }
  174. /* Once the first transfer is queued we can turn on the ISR, future
  175. * calls to zynq_step_dma will happen from the ISR context. The
  176. * dma_lock spinlock guarentees this handover is done coherently, the
  177. * ISR enable is put at the end to avoid another CPU spinning in the
  178. * ISR on this lock.
  179. */
  180. if (first && priv->cur_sg) {
  181. zynq_fpga_set_irq(priv,
  182. IXR_DMA_DONE_MASK | IXR_ERROR_FLAGS_MASK);
  183. } else if (!priv->cur_sg) {
  184. /* The last transfer changes to DMA & PCAP mode since we do
  185. * not want to continue until everything has been flushed into
  186. * the PCAP.
  187. */
  188. zynq_fpga_set_irq(priv,
  189. IXR_D_P_DONE_MASK | IXR_ERROR_FLAGS_MASK);
  190. }
  191. }
  192. static irqreturn_t zynq_fpga_isr(int irq, void *data)
  193. {
  194. struct zynq_fpga_priv *priv = data;
  195. u32 intr_status;
  196. /* If anything other than DMA completion is reported stop and hand
  197. * control back to zynq_fpga_ops_write, something went wrong,
  198. * otherwise progress the DMA.
  199. */
  200. spin_lock(&priv->dma_lock);
  201. intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
  202. if (!(intr_status & IXR_ERROR_FLAGS_MASK) &&
  203. (intr_status & IXR_DMA_DONE_MASK) && priv->cur_sg) {
  204. zynq_fpga_write(priv, INT_STS_OFFSET, IXR_DMA_DONE_MASK);
  205. zynq_step_dma(priv);
  206. spin_unlock(&priv->dma_lock);
  207. return IRQ_HANDLED;
  208. }
  209. spin_unlock(&priv->dma_lock);
  210. zynq_fpga_set_irq(priv, 0);
  211. complete(&priv->dma_done);
  212. return IRQ_HANDLED;
  213. }
  214. /* Sanity check the proposed bitstream. It must start with the sync word in
  215. * the correct byte order, and be dword aligned. The input is a Xilinx .bin
  216. * file with every 32 bit quantity swapped.
  217. */
  218. static bool zynq_fpga_has_sync(const u8 *buf, size_t count)
  219. {
  220. for (; count >= 4; buf += 4, count -= 4)
  221. if (buf[0] == 0x66 && buf[1] == 0x55 && buf[2] == 0x99 &&
  222. buf[3] == 0xaa)
  223. return true;
  224. return false;
  225. }
  226. static int zynq_fpga_ops_write_init(struct fpga_manager *mgr,
  227. struct fpga_image_info *info,
  228. const char *buf, size_t count)
  229. {
  230. struct zynq_fpga_priv *priv;
  231. u32 ctrl, status;
  232. int err;
  233. priv = mgr->priv;
  234. err = clk_enable(priv->clk);
  235. if (err)
  236. return err;
  237. /* check if bitstream is encrypted & and system's still secure */
  238. if (info->flags & FPGA_MGR_ENCRYPTED_BITSTREAM) {
  239. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  240. if (!(ctrl & CTRL_SEC_EN_MASK)) {
  241. dev_err(&mgr->dev,
  242. "System not secure, can't use crypted bitstreams\n");
  243. err = -EINVAL;
  244. goto out_err;
  245. }
  246. }
  247. /* don't globally reset PL if we're doing partial reconfig */
  248. if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  249. if (!zynq_fpga_has_sync(buf, count)) {
  250. dev_err(&mgr->dev,
  251. "Invalid bitstream, could not find a sync word. Bitstream must be a byte swapped .bin file\n");
  252. err = -EINVAL;
  253. goto out_err;
  254. }
  255. /* assert AXI interface resets */
  256. regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
  257. FPGA_RST_ALL_MASK);
  258. /* disable all level shifters */
  259. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  260. LVL_SHFTR_DISABLE_ALL_MASK);
  261. /* enable level shifters from PS to PL */
  262. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  263. LVL_SHFTR_ENABLE_PS_TO_PL);
  264. /* create a rising edge on PCFG_INIT. PCFG_INIT follows
  265. * PCFG_PROG_B, so we need to poll it after setting PCFG_PROG_B
  266. * to make sure the rising edge actually happens.
  267. * Note: PCFG_PROG_B is low active, sequence as described in
  268. * UG585 v1.10 page 211
  269. */
  270. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  271. ctrl |= CTRL_PCFG_PROG_B_MASK;
  272. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  273. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  274. status & STATUS_PCFG_INIT_MASK,
  275. INIT_POLL_DELAY,
  276. INIT_POLL_TIMEOUT);
  277. if (err) {
  278. dev_err(&mgr->dev, "Timeout waiting for PCFG_INIT\n");
  279. goto out_err;
  280. }
  281. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  282. ctrl &= ~CTRL_PCFG_PROG_B_MASK;
  283. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  284. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  285. !(status & STATUS_PCFG_INIT_MASK),
  286. INIT_POLL_DELAY,
  287. INIT_POLL_TIMEOUT);
  288. if (err) {
  289. dev_err(&mgr->dev, "Timeout waiting for !PCFG_INIT\n");
  290. goto out_err;
  291. }
  292. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  293. ctrl |= CTRL_PCFG_PROG_B_MASK;
  294. zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
  295. err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
  296. status & STATUS_PCFG_INIT_MASK,
  297. INIT_POLL_DELAY,
  298. INIT_POLL_TIMEOUT);
  299. if (err) {
  300. dev_err(&mgr->dev, "Timeout waiting for PCFG_INIT\n");
  301. goto out_err;
  302. }
  303. }
  304. /* set configuration register with following options:
  305. * - enable PCAP interface
  306. * - set throughput for maximum speed (if bistream not crypted)
  307. * - set CPU in user mode
  308. */
  309. ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
  310. if (info->flags & FPGA_MGR_ENCRYPTED_BITSTREAM)
  311. zynq_fpga_write(priv, CTRL_OFFSET,
  312. (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK
  313. | CTRL_PCAP_RATE_EN_MASK | ctrl));
  314. else
  315. zynq_fpga_write(priv, CTRL_OFFSET,
  316. (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK
  317. | ctrl));
  318. /* We expect that the command queue is empty right now. */
  319. status = zynq_fpga_read(priv, STATUS_OFFSET);
  320. if ((status & STATUS_DMA_Q_F) ||
  321. (status & STATUS_DMA_Q_E) != STATUS_DMA_Q_E) {
  322. dev_err(&mgr->dev, "DMA command queue not right\n");
  323. err = -EBUSY;
  324. goto out_err;
  325. }
  326. /* ensure internal PCAP loopback is disabled */
  327. ctrl = zynq_fpga_read(priv, MCTRL_OFFSET);
  328. zynq_fpga_write(priv, MCTRL_OFFSET, (~MCTRL_PCAP_LPBK_MASK & ctrl));
  329. clk_disable(priv->clk);
  330. return 0;
  331. out_err:
  332. clk_disable(priv->clk);
  333. return err;
  334. }
  335. static int zynq_fpga_ops_write(struct fpga_manager *mgr, struct sg_table *sgt)
  336. {
  337. struct zynq_fpga_priv *priv;
  338. const char *why;
  339. int err;
  340. u32 intr_status;
  341. unsigned long timeout;
  342. unsigned long flags;
  343. struct scatterlist *sg;
  344. int i;
  345. priv = mgr->priv;
  346. /* The hardware can only DMA multiples of 4 bytes, and it requires the
  347. * starting addresses to be aligned to 64 bits (UG585 pg 212).
  348. */
  349. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  350. if ((sg->offset % 8) || (sg->length % 4)) {
  351. dev_err(&mgr->dev,
  352. "Invalid bitstream, chunks must be aligned\n");
  353. return -EINVAL;
  354. }
  355. }
  356. priv->dma_nelms =
  357. dma_map_sg(mgr->dev.parent, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
  358. if (priv->dma_nelms == 0) {
  359. dev_err(&mgr->dev, "Unable to DMA map (TO_DEVICE)\n");
  360. return -ENOMEM;
  361. }
  362. /* enable clock */
  363. err = clk_enable(priv->clk);
  364. if (err)
  365. goto out_free;
  366. zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
  367. reinit_completion(&priv->dma_done);
  368. /* zynq_step_dma will turn on interrupts */
  369. spin_lock_irqsave(&priv->dma_lock, flags);
  370. priv->dma_elm = 0;
  371. priv->cur_sg = sgt->sgl;
  372. zynq_step_dma(priv);
  373. spin_unlock_irqrestore(&priv->dma_lock, flags);
  374. timeout = wait_for_completion_timeout(&priv->dma_done,
  375. msecs_to_jiffies(DMA_TIMEOUT_MS));
  376. spin_lock_irqsave(&priv->dma_lock, flags);
  377. zynq_fpga_set_irq(priv, 0);
  378. priv->cur_sg = NULL;
  379. spin_unlock_irqrestore(&priv->dma_lock, flags);
  380. intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
  381. zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
  382. /* There doesn't seem to be a way to force cancel any DMA, so if
  383. * something went wrong we are relying on the hardware to have halted
  384. * the DMA before we get here, if there was we could use
  385. * wait_for_completion_interruptible too.
  386. */
  387. if (intr_status & IXR_ERROR_FLAGS_MASK) {
  388. why = "DMA reported error";
  389. err = -EIO;
  390. goto out_report;
  391. }
  392. if (priv->cur_sg ||
  393. !((intr_status & IXR_D_P_DONE_MASK) == IXR_D_P_DONE_MASK)) {
  394. if (timeout == 0)
  395. why = "DMA timed out";
  396. else
  397. why = "DMA did not complete";
  398. err = -EIO;
  399. goto out_report;
  400. }
  401. err = 0;
  402. goto out_clk;
  403. out_report:
  404. dev_err(&mgr->dev,
  405. "%s: INT_STS:0x%x CTRL:0x%x LOCK:0x%x INT_MASK:0x%x STATUS:0x%x MCTRL:0x%x\n",
  406. why,
  407. intr_status,
  408. zynq_fpga_read(priv, CTRL_OFFSET),
  409. zynq_fpga_read(priv, LOCK_OFFSET),
  410. zynq_fpga_read(priv, INT_MASK_OFFSET),
  411. zynq_fpga_read(priv, STATUS_OFFSET),
  412. zynq_fpga_read(priv, MCTRL_OFFSET));
  413. out_clk:
  414. clk_disable(priv->clk);
  415. out_free:
  416. dma_unmap_sg(mgr->dev.parent, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
  417. return err;
  418. }
  419. static int zynq_fpga_ops_write_complete(struct fpga_manager *mgr,
  420. struct fpga_image_info *info)
  421. {
  422. struct zynq_fpga_priv *priv = mgr->priv;
  423. int err;
  424. u32 intr_status;
  425. err = clk_enable(priv->clk);
  426. if (err)
  427. return err;
  428. err = zynq_fpga_poll_timeout(priv, INT_STS_OFFSET, intr_status,
  429. intr_status & IXR_PCFG_DONE_MASK,
  430. INIT_POLL_DELAY,
  431. INIT_POLL_TIMEOUT);
  432. clk_disable(priv->clk);
  433. if (err)
  434. return err;
  435. /* for the partial reconfig case we didn't touch the level shifters */
  436. if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  437. /* enable level shifters from PL to PS */
  438. regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
  439. LVL_SHFTR_ENABLE_PL_TO_PS);
  440. /* deassert AXI interface resets */
  441. regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
  442. FPGA_RST_NONE_MASK);
  443. }
  444. return 0;
  445. }
  446. static enum fpga_mgr_states zynq_fpga_ops_state(struct fpga_manager *mgr)
  447. {
  448. int err;
  449. u32 intr_status;
  450. struct zynq_fpga_priv *priv;
  451. priv = mgr->priv;
  452. err = clk_enable(priv->clk);
  453. if (err)
  454. return FPGA_MGR_STATE_UNKNOWN;
  455. intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
  456. clk_disable(priv->clk);
  457. if (intr_status & IXR_PCFG_DONE_MASK)
  458. return FPGA_MGR_STATE_OPERATING;
  459. return FPGA_MGR_STATE_UNKNOWN;
  460. }
  461. static const struct fpga_manager_ops zynq_fpga_ops = {
  462. .initial_header_size = 128,
  463. .state = zynq_fpga_ops_state,
  464. .write_init = zynq_fpga_ops_write_init,
  465. .write_sg = zynq_fpga_ops_write,
  466. .write_complete = zynq_fpga_ops_write_complete,
  467. };
  468. static int zynq_fpga_probe(struct platform_device *pdev)
  469. {
  470. struct device *dev = &pdev->dev;
  471. struct zynq_fpga_priv *priv;
  472. struct resource *res;
  473. int err;
  474. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  475. if (!priv)
  476. return -ENOMEM;
  477. spin_lock_init(&priv->dma_lock);
  478. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  479. priv->io_base = devm_ioremap_resource(dev, res);
  480. if (IS_ERR(priv->io_base))
  481. return PTR_ERR(priv->io_base);
  482. priv->slcr = syscon_regmap_lookup_by_phandle(dev->of_node,
  483. "syscon");
  484. if (IS_ERR(priv->slcr)) {
  485. dev_err(dev, "unable to get zynq-slcr regmap\n");
  486. return PTR_ERR(priv->slcr);
  487. }
  488. init_completion(&priv->dma_done);
  489. priv->irq = platform_get_irq(pdev, 0);
  490. if (priv->irq < 0) {
  491. dev_err(dev, "No IRQ available\n");
  492. return priv->irq;
  493. }
  494. priv->clk = devm_clk_get(dev, "ref_clk");
  495. if (IS_ERR(priv->clk)) {
  496. dev_err(dev, "input clock not found\n");
  497. return PTR_ERR(priv->clk);
  498. }
  499. err = clk_prepare_enable(priv->clk);
  500. if (err) {
  501. dev_err(dev, "unable to enable clock\n");
  502. return err;
  503. }
  504. /* unlock the device */
  505. zynq_fpga_write(priv, UNLOCK_OFFSET, UNLOCK_MASK);
  506. zynq_fpga_set_irq(priv, 0);
  507. zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
  508. err = devm_request_irq(dev, priv->irq, zynq_fpga_isr, 0, dev_name(dev),
  509. priv);
  510. if (err) {
  511. dev_err(dev, "unable to request IRQ\n");
  512. clk_disable_unprepare(priv->clk);
  513. return err;
  514. }
  515. clk_disable(priv->clk);
  516. err = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
  517. &zynq_fpga_ops, priv);
  518. if (err) {
  519. dev_err(dev, "unable to register FPGA manager\n");
  520. clk_unprepare(priv->clk);
  521. return err;
  522. }
  523. return 0;
  524. }
  525. static int zynq_fpga_remove(struct platform_device *pdev)
  526. {
  527. struct zynq_fpga_priv *priv;
  528. struct fpga_manager *mgr;
  529. mgr = platform_get_drvdata(pdev);
  530. priv = mgr->priv;
  531. fpga_mgr_unregister(&pdev->dev);
  532. clk_unprepare(priv->clk);
  533. return 0;
  534. }
  535. #ifdef CONFIG_OF
  536. static const struct of_device_id zynq_fpga_of_match[] = {
  537. { .compatible = "xlnx,zynq-devcfg-1.0", },
  538. {},
  539. };
  540. MODULE_DEVICE_TABLE(of, zynq_fpga_of_match);
  541. #endif
  542. static struct platform_driver zynq_fpga_driver = {
  543. .probe = zynq_fpga_probe,
  544. .remove = zynq_fpga_remove,
  545. .driver = {
  546. .name = "zynq_fpga_manager",
  547. .of_match_table = of_match_ptr(zynq_fpga_of_match),
  548. },
  549. };
  550. module_platform_driver(zynq_fpga_driver);
  551. MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
  552. MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
  553. MODULE_DESCRIPTION("Xilinx Zynq FPGA Manager");
  554. MODULE_LICENSE("GPL v2");