2 * Copyright (c) 2011-2015 Xilinx Inc.
3 * Copyright (c) 2015, National Instruments Corp.
5 * FPGA Manager Driver for Xilinx Zynq, heavily based on xdevcfg driver
6 * in their vendor tree.
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.
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.
18 #include <linux/clk.h>
19 #include <linux/completion.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/fpga/fpga-mgr.h>
23 #include <linux/interrupt.h>
25 #include <linux/iopoll.h>
26 #include <linux/module.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/of_address.h>
29 #include <linux/of_irq.h>
31 #include <linux/regmap.h>
32 #include <linux/string.h>
34 /* Offsets into SLCR regmap */
36 /* FPGA Software Reset Control */
37 #define SLCR_FPGA_RST_CTRL_OFFSET 0x240
38 /* Level Shifters Enable */
39 #define SLCR_LVL_SHFTR_EN_OFFSET 0x900
41 /* Constant Definitions */
43 /* Control Register */
44 #define CTRL_OFFSET 0x00
46 #define LOCK_OFFSET 0x04
47 /* Interrupt Status Register */
48 #define INT_STS_OFFSET 0x0c
49 /* Interrupt Mask Register */
50 #define INT_MASK_OFFSET 0x10
52 #define STATUS_OFFSET 0x14
53 /* DMA Source Address Register */
54 #define DMA_SRC_ADDR_OFFSET 0x18
55 /* DMA Destination Address Reg */
56 #define DMA_DST_ADDR_OFFSET 0x1c
57 /* DMA Source Transfer Length */
58 #define DMA_SRC_LEN_OFFSET 0x20
59 /* DMA Destination Transfer */
60 #define DMA_DEST_LEN_OFFSET 0x24
62 #define UNLOCK_OFFSET 0x34
63 /* Misc. Control Register */
64 #define MCTRL_OFFSET 0x80
66 /* Control Register Bit definitions */
68 /* Signal to reset FPGA */
69 #define CTRL_PCFG_PROG_B_MASK BIT(30)
70 /* Enable PCAP for PR */
71 #define CTRL_PCAP_PR_MASK BIT(27)
73 #define CTRL_PCAP_MODE_MASK BIT(26)
75 /* Miscellaneous Control Register bit definitions */
76 /* Internal PCAP loopback */
77 #define MCTRL_PCAP_LPBK_MASK BIT(4)
79 /* Status register bit definitions */
81 /* FPGA init status */
82 #define STATUS_DMA_Q_F BIT(31)
83 #define STATUS_PCFG_INIT_MASK BIT(4)
85 /* Interrupt Status/Mask Register Bit definitions */
86 /* DMA command done */
87 #define IXR_DMA_DONE_MASK BIT(13)
88 /* DMA and PCAP cmd done */
89 #define IXR_D_P_DONE_MASK BIT(12)
91 #define IXR_PCFG_DONE_MASK BIT(2)
92 #define IXR_ERROR_FLAGS_MASK 0x00F0F860
93 #define IXR_ALL_MASK 0xF8F7F87F
95 /* Miscellaneous constant values */
97 /* Invalid DMA addr */
98 #define DMA_INVALID_ADDRESS GENMASK(31, 0)
99 /* Used to unlock the dev */
100 #define UNLOCK_MASK 0x757bdf0d
101 /* Timeout for DMA to complete */
102 #define DMA_DONE_TIMEOUT msecs_to_jiffies(1000)
103 /* Timeout for polling reset bits */
104 #define INIT_POLL_TIMEOUT 2500000
105 /* Delay for polling reset bits */
106 #define INIT_POLL_DELAY 20
108 /* Masks for controlling stuff in SLCR */
109 /* Disable all Level shifters */
110 #define LVL_SHFTR_DISABLE_ALL_MASK 0x0
111 /* Enable Level shifters from PS to PL */
112 #define LVL_SHFTR_ENABLE_PS_TO_PL 0xa
113 /* Enable Level shifters from PL to PS */
114 #define LVL_SHFTR_ENABLE_PL_TO_PS 0xf
115 /* Enable global resets */
116 #define FPGA_RST_ALL_MASK 0xf
117 /* Disable global resets */
118 #define FPGA_RST_NONE_MASK 0x0
120 struct zynq_fpga_priv
{
125 void __iomem
*io_base
;
128 struct completion dma_done
;
131 static inline void zynq_fpga_write(struct zynq_fpga_priv
*priv
, u32 offset
,
134 writel(val
, priv
->io_base
+ offset
);
137 static inline u32
zynq_fpga_read(const struct zynq_fpga_priv
*priv
,
140 return readl(priv
->io_base
+ offset
);
143 #define zynq_fpga_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
144 readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \
147 static void zynq_fpga_mask_irqs(struct zynq_fpga_priv
*priv
)
151 intr_mask
= zynq_fpga_read(priv
, INT_MASK_OFFSET
);
152 zynq_fpga_write(priv
, INT_MASK_OFFSET
,
153 intr_mask
| IXR_DMA_DONE_MASK
| IXR_ERROR_FLAGS_MASK
);
156 static void zynq_fpga_unmask_irqs(struct zynq_fpga_priv
*priv
)
160 intr_mask
= zynq_fpga_read(priv
, INT_MASK_OFFSET
);
161 zynq_fpga_write(priv
, INT_MASK_OFFSET
,
163 & ~(IXR_D_P_DONE_MASK
| IXR_ERROR_FLAGS_MASK
));
166 static irqreturn_t
zynq_fpga_isr(int irq
, void *data
)
168 struct zynq_fpga_priv
*priv
= data
;
170 /* disable DMA and error IRQs */
171 zynq_fpga_mask_irqs(priv
);
173 complete(&priv
->dma_done
);
178 static int zynq_fpga_ops_write_init(struct fpga_manager
*mgr
, u32 flags
,
179 const char *buf
, size_t count
)
181 struct zynq_fpga_priv
*priv
;
187 err
= clk_enable(priv
->clk
);
191 /* don't globally reset PL if we're doing partial reconfig */
192 if (!(flags
& FPGA_MGR_PARTIAL_RECONFIG
)) {
193 /* assert AXI interface resets */
194 regmap_write(priv
->slcr
, SLCR_FPGA_RST_CTRL_OFFSET
,
197 /* disable all level shifters */
198 regmap_write(priv
->slcr
, SLCR_LVL_SHFTR_EN_OFFSET
,
199 LVL_SHFTR_DISABLE_ALL_MASK
);
200 /* enable level shifters from PS to PL */
201 regmap_write(priv
->slcr
, SLCR_LVL_SHFTR_EN_OFFSET
,
202 LVL_SHFTR_ENABLE_PS_TO_PL
);
204 /* create a rising edge on PCFG_INIT. PCFG_INIT follows
205 * PCFG_PROG_B, so we need to poll it after setting PCFG_PROG_B
206 * to make sure the rising edge actually happens.
207 * Note: PCFG_PROG_B is low active, sequence as described in
208 * UG585 v1.10 page 211
210 ctrl
= zynq_fpga_read(priv
, CTRL_OFFSET
);
211 ctrl
|= CTRL_PCFG_PROG_B_MASK
;
213 zynq_fpga_write(priv
, CTRL_OFFSET
, ctrl
);
215 err
= zynq_fpga_poll_timeout(priv
, STATUS_OFFSET
, status
,
216 status
& STATUS_PCFG_INIT_MASK
,
220 dev_err(priv
->dev
, "Timeout waiting for PCFG_INIT");
224 ctrl
= zynq_fpga_read(priv
, CTRL_OFFSET
);
225 ctrl
&= ~CTRL_PCFG_PROG_B_MASK
;
227 zynq_fpga_write(priv
, CTRL_OFFSET
, ctrl
);
229 err
= zynq_fpga_poll_timeout(priv
, STATUS_OFFSET
, status
,
230 !(status
& STATUS_PCFG_INIT_MASK
),
234 dev_err(priv
->dev
, "Timeout waiting for !PCFG_INIT");
238 ctrl
= zynq_fpga_read(priv
, CTRL_OFFSET
);
239 ctrl
|= CTRL_PCFG_PROG_B_MASK
;
241 zynq_fpga_write(priv
, CTRL_OFFSET
, ctrl
);
243 err
= zynq_fpga_poll_timeout(priv
, STATUS_OFFSET
, status
,
244 status
& STATUS_PCFG_INIT_MASK
,
248 dev_err(priv
->dev
, "Timeout waiting for PCFG_INIT");
253 /* set configuration register with following options:
254 * - enable PCAP interface
255 * - set throughput for maximum speed
256 * - set CPU in user mode
258 ctrl
= zynq_fpga_read(priv
, CTRL_OFFSET
);
259 zynq_fpga_write(priv
, CTRL_OFFSET
,
260 (CTRL_PCAP_PR_MASK
| CTRL_PCAP_MODE_MASK
| ctrl
));
262 /* check that we have room in the command queue */
263 status
= zynq_fpga_read(priv
, STATUS_OFFSET
);
264 if (status
& STATUS_DMA_Q_F
) {
265 dev_err(priv
->dev
, "DMA command queue full");
270 /* ensure internal PCAP loopback is disabled */
271 ctrl
= zynq_fpga_read(priv
, MCTRL_OFFSET
);
272 zynq_fpga_write(priv
, MCTRL_OFFSET
, (~MCTRL_PCAP_LPBK_MASK
& ctrl
));
274 clk_disable(priv
->clk
);
279 clk_disable(priv
->clk
);
284 static int zynq_fpga_ops_write(struct fpga_manager
*mgr
,
285 const char *buf
, size_t count
)
287 struct zynq_fpga_priv
*priv
;
298 kbuf
= dma_alloc_coherent(priv
->dev
, count
, &dma_addr
, GFP_KERNEL
);
302 memcpy(kbuf
, buf
, count
);
305 err
= clk_enable(priv
->clk
);
309 zynq_fpga_write(priv
, INT_STS_OFFSET
, IXR_ALL_MASK
);
311 reinit_completion(&priv
->dma_done
);
313 /* enable DMA and error IRQs */
314 zynq_fpga_unmask_irqs(priv
);
316 /* the +1 in the src addr is used to hold off on DMA_DONE IRQ
317 * until both AXI and PCAP are done ...
319 zynq_fpga_write(priv
, DMA_SRC_ADDR_OFFSET
, (u32
)(dma_addr
) + 1);
320 zynq_fpga_write(priv
, DMA_DST_ADDR_OFFSET
, (u32
)DMA_INVALID_ADDRESS
);
322 /* convert #bytes to #words */
323 transfer_length
= (count
+ 3) / 4;
325 zynq_fpga_write(priv
, DMA_SRC_LEN_OFFSET
, transfer_length
);
326 zynq_fpga_write(priv
, DMA_DEST_LEN_OFFSET
, 0);
328 wait_for_completion(&priv
->dma_done
);
330 intr_status
= zynq_fpga_read(priv
, INT_STS_OFFSET
);
331 zynq_fpga_write(priv
, INT_STS_OFFSET
, intr_status
);
333 if (!((intr_status
& IXR_D_P_DONE_MASK
) == IXR_D_P_DONE_MASK
)) {
334 dev_err(priv
->dev
, "Error configuring FPGA");
338 clk_disable(priv
->clk
);
341 dma_free_coherent(priv
->dev
, in_count
, kbuf
, dma_addr
);
346 static int zynq_fpga_ops_write_complete(struct fpga_manager
*mgr
, u32 flags
)
348 struct zynq_fpga_priv
*priv
= mgr
->priv
;
352 err
= clk_enable(priv
->clk
);
356 err
= zynq_fpga_poll_timeout(priv
, INT_STS_OFFSET
, intr_status
,
357 intr_status
& IXR_PCFG_DONE_MASK
,
361 clk_disable(priv
->clk
);
366 /* for the partial reconfig case we didn't touch the level shifters */
367 if (!(flags
& FPGA_MGR_PARTIAL_RECONFIG
)) {
368 /* enable level shifters from PL to PS */
369 regmap_write(priv
->slcr
, SLCR_LVL_SHFTR_EN_OFFSET
,
370 LVL_SHFTR_ENABLE_PL_TO_PS
);
372 /* deassert AXI interface resets */
373 regmap_write(priv
->slcr
, SLCR_FPGA_RST_CTRL_OFFSET
,
380 static enum fpga_mgr_states
zynq_fpga_ops_state(struct fpga_manager
*mgr
)
384 struct zynq_fpga_priv
*priv
;
388 err
= clk_enable(priv
->clk
);
390 return FPGA_MGR_STATE_UNKNOWN
;
392 intr_status
= zynq_fpga_read(priv
, INT_STS_OFFSET
);
393 clk_disable(priv
->clk
);
395 if (intr_status
& IXR_PCFG_DONE_MASK
)
396 return FPGA_MGR_STATE_OPERATING
;
398 return FPGA_MGR_STATE_UNKNOWN
;
401 static const struct fpga_manager_ops zynq_fpga_ops
= {
402 .state
= zynq_fpga_ops_state
,
403 .write_init
= zynq_fpga_ops_write_init
,
404 .write
= zynq_fpga_ops_write
,
405 .write_complete
= zynq_fpga_ops_write_complete
,
408 static int zynq_fpga_probe(struct platform_device
*pdev
)
410 struct device
*dev
= &pdev
->dev
;
411 struct zynq_fpga_priv
*priv
;
412 struct resource
*res
;
415 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
421 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
422 priv
->io_base
= devm_ioremap_resource(dev
, res
);
423 if (IS_ERR(priv
->io_base
))
424 return PTR_ERR(priv
->io_base
);
426 priv
->slcr
= syscon_regmap_lookup_by_phandle(dev
->of_node
,
428 if (IS_ERR(priv
->slcr
)) {
429 dev_err(dev
, "unable to get zynq-slcr regmap");
430 return PTR_ERR(priv
->slcr
);
433 init_completion(&priv
->dma_done
);
435 priv
->irq
= platform_get_irq(pdev
, 0);
437 dev_err(dev
, "No IRQ available");
441 err
= devm_request_irq(dev
, priv
->irq
, zynq_fpga_isr
, 0,
442 dev_name(dev
), priv
);
444 dev_err(dev
, "unable to request IRQ");
448 priv
->clk
= devm_clk_get(dev
, "ref_clk");
449 if (IS_ERR(priv
->clk
)) {
450 dev_err(dev
, "input clock not found");
451 return PTR_ERR(priv
->clk
);
454 err
= clk_prepare_enable(priv
->clk
);
456 dev_err(dev
, "unable to enable clock");
460 /* unlock the device */
461 zynq_fpga_write(priv
, UNLOCK_OFFSET
, UNLOCK_MASK
);
463 clk_disable(priv
->clk
);
465 err
= fpga_mgr_register(dev
, "Xilinx Zynq FPGA Manager",
466 &zynq_fpga_ops
, priv
);
468 dev_err(dev
, "unable to register FPGA manager");
469 clk_unprepare(priv
->clk
);
476 static int zynq_fpga_remove(struct platform_device
*pdev
)
478 struct zynq_fpga_priv
*priv
;
479 struct fpga_manager
*mgr
;
481 mgr
= platform_get_drvdata(pdev
);
484 fpga_mgr_unregister(&pdev
->dev
);
486 clk_unprepare(priv
->clk
);
492 static const struct of_device_id zynq_fpga_of_match
[] = {
493 { .compatible
= "xlnx,zynq-devcfg-1.0", },
497 MODULE_DEVICE_TABLE(of
, zynq_fpga_of_match
);
500 static struct platform_driver zynq_fpga_driver
= {
501 .probe
= zynq_fpga_probe
,
502 .remove
= zynq_fpga_remove
,
504 .name
= "zynq_fpga_manager",
505 .of_match_table
= of_match_ptr(zynq_fpga_of_match
),
509 module_platform_driver(zynq_fpga_driver
);
511 MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
512 MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
513 MODULE_DESCRIPTION("Xilinx Zynq FPGA Manager");
514 MODULE_LICENSE("GPL v2");