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
{
124 void __iomem
*io_base
;
127 struct completion dma_done
;
130 static inline void zynq_fpga_write(struct zynq_fpga_priv
*priv
, u32 offset
,
133 writel(val
, priv
->io_base
+ offset
);
136 static inline u32
zynq_fpga_read(const struct zynq_fpga_priv
*priv
,
139 return readl(priv
->io_base
+ offset
);
142 #define zynq_fpga_poll_timeout(priv, addr, val, cond, sleep_us, timeout_us) \
143 readl_poll_timeout(priv->io_base + addr, val, cond, sleep_us, \
146 static void zynq_fpga_mask_irqs(struct zynq_fpga_priv
*priv
)
150 intr_mask
= zynq_fpga_read(priv
, INT_MASK_OFFSET
);
151 zynq_fpga_write(priv
, INT_MASK_OFFSET
,
152 intr_mask
| IXR_DMA_DONE_MASK
| IXR_ERROR_FLAGS_MASK
);
155 static void zynq_fpga_unmask_irqs(struct zynq_fpga_priv
*priv
)
159 intr_mask
= zynq_fpga_read(priv
, INT_MASK_OFFSET
);
160 zynq_fpga_write(priv
, INT_MASK_OFFSET
,
162 & ~(IXR_D_P_DONE_MASK
| IXR_ERROR_FLAGS_MASK
));
165 static irqreturn_t
zynq_fpga_isr(int irq
, void *data
)
167 struct zynq_fpga_priv
*priv
= data
;
169 /* disable DMA and error IRQs */
170 zynq_fpga_mask_irqs(priv
);
172 complete(&priv
->dma_done
);
177 static int zynq_fpga_ops_write_init(struct fpga_manager
*mgr
,
178 struct fpga_image_info
*info
,
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 (!(info
->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(&mgr
->dev
, "Timeout waiting for PCFG_INIT\n");
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(&mgr
->dev
, "Timeout waiting for !PCFG_INIT\n");
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(&mgr
->dev
, "Timeout waiting for PCFG_INIT\n");
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(&mgr
->dev
, "DMA command queue full\n");
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
;
299 dma_alloc_coherent(mgr
->dev
.parent
, count
, &dma_addr
, GFP_KERNEL
);
303 memcpy(kbuf
, buf
, count
);
306 err
= clk_enable(priv
->clk
);
310 zynq_fpga_write(priv
, INT_STS_OFFSET
, IXR_ALL_MASK
);
312 reinit_completion(&priv
->dma_done
);
314 /* enable DMA and error IRQs */
315 zynq_fpga_unmask_irqs(priv
);
317 /* the +1 in the src addr is used to hold off on DMA_DONE IRQ
318 * until both AXI and PCAP are done ...
320 zynq_fpga_write(priv
, DMA_SRC_ADDR_OFFSET
, (u32
)(dma_addr
) + 1);
321 zynq_fpga_write(priv
, DMA_DST_ADDR_OFFSET
, (u32
)DMA_INVALID_ADDRESS
);
323 /* convert #bytes to #words */
324 transfer_length
= (count
+ 3) / 4;
326 zynq_fpga_write(priv
, DMA_SRC_LEN_OFFSET
, transfer_length
);
327 zynq_fpga_write(priv
, DMA_DEST_LEN_OFFSET
, 0);
329 wait_for_completion(&priv
->dma_done
);
331 intr_status
= zynq_fpga_read(priv
, INT_STS_OFFSET
);
332 zynq_fpga_write(priv
, INT_STS_OFFSET
, intr_status
);
334 if (!((intr_status
& IXR_D_P_DONE_MASK
) == IXR_D_P_DONE_MASK
)) {
335 dev_err(&mgr
->dev
, "Error configuring FPGA\n");
339 clk_disable(priv
->clk
);
342 dma_free_coherent(mgr
->dev
.parent
, count
, kbuf
, dma_addr
);
346 static int zynq_fpga_ops_write_complete(struct fpga_manager
*mgr
,
347 struct fpga_image_info
*info
)
349 struct zynq_fpga_priv
*priv
= mgr
->priv
;
353 err
= clk_enable(priv
->clk
);
357 err
= zynq_fpga_poll_timeout(priv
, INT_STS_OFFSET
, intr_status
,
358 intr_status
& IXR_PCFG_DONE_MASK
,
362 clk_disable(priv
->clk
);
367 /* for the partial reconfig case we didn't touch the level shifters */
368 if (!(info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
)) {
369 /* enable level shifters from PL to PS */
370 regmap_write(priv
->slcr
, SLCR_LVL_SHFTR_EN_OFFSET
,
371 LVL_SHFTR_ENABLE_PL_TO_PS
);
373 /* deassert AXI interface resets */
374 regmap_write(priv
->slcr
, SLCR_FPGA_RST_CTRL_OFFSET
,
381 static enum fpga_mgr_states
zynq_fpga_ops_state(struct fpga_manager
*mgr
)
385 struct zynq_fpga_priv
*priv
;
389 err
= clk_enable(priv
->clk
);
391 return FPGA_MGR_STATE_UNKNOWN
;
393 intr_status
= zynq_fpga_read(priv
, INT_STS_OFFSET
);
394 clk_disable(priv
->clk
);
396 if (intr_status
& IXR_PCFG_DONE_MASK
)
397 return FPGA_MGR_STATE_OPERATING
;
399 return FPGA_MGR_STATE_UNKNOWN
;
402 static const struct fpga_manager_ops zynq_fpga_ops
= {
403 .state
= zynq_fpga_ops_state
,
404 .write_init
= zynq_fpga_ops_write_init
,
405 .write
= zynq_fpga_ops_write
,
406 .write_complete
= zynq_fpga_ops_write_complete
,
409 static int zynq_fpga_probe(struct platform_device
*pdev
)
411 struct device
*dev
= &pdev
->dev
;
412 struct zynq_fpga_priv
*priv
;
413 struct resource
*res
;
416 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
420 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
421 priv
->io_base
= devm_ioremap_resource(dev
, res
);
422 if (IS_ERR(priv
->io_base
))
423 return PTR_ERR(priv
->io_base
);
425 priv
->slcr
= syscon_regmap_lookup_by_phandle(dev
->of_node
,
427 if (IS_ERR(priv
->slcr
)) {
428 dev_err(dev
, "unable to get zynq-slcr regmap\n");
429 return PTR_ERR(priv
->slcr
);
432 init_completion(&priv
->dma_done
);
434 priv
->irq
= platform_get_irq(pdev
, 0);
436 dev_err(dev
, "No IRQ available\n");
440 priv
->clk
= devm_clk_get(dev
, "ref_clk");
441 if (IS_ERR(priv
->clk
)) {
442 dev_err(dev
, "input clock not found\n");
443 return PTR_ERR(priv
->clk
);
446 err
= clk_prepare_enable(priv
->clk
);
448 dev_err(dev
, "unable to enable clock\n");
452 /* unlock the device */
453 zynq_fpga_write(priv
, UNLOCK_OFFSET
, UNLOCK_MASK
);
455 zynq_fpga_write(priv
, INT_MASK_OFFSET
, 0xFFFFFFFF);
456 zynq_fpga_write(priv
, INT_STS_OFFSET
, IXR_ALL_MASK
);
457 err
= devm_request_irq(dev
, priv
->irq
, zynq_fpga_isr
, 0, dev_name(dev
),
460 dev_err(dev
, "unable to request IRQ\n");
461 clk_disable_unprepare(priv
->clk
);
465 clk_disable(priv
->clk
);
467 err
= fpga_mgr_register(dev
, "Xilinx Zynq FPGA Manager",
468 &zynq_fpga_ops
, priv
);
470 dev_err(dev
, "unable to register FPGA manager\n");
471 clk_unprepare(priv
->clk
);
478 static int zynq_fpga_remove(struct platform_device
*pdev
)
480 struct zynq_fpga_priv
*priv
;
481 struct fpga_manager
*mgr
;
483 mgr
= platform_get_drvdata(pdev
);
486 fpga_mgr_unregister(&pdev
->dev
);
488 clk_unprepare(priv
->clk
);
494 static const struct of_device_id zynq_fpga_of_match
[] = {
495 { .compatible
= "xlnx,zynq-devcfg-1.0", },
499 MODULE_DEVICE_TABLE(of
, zynq_fpga_of_match
);
502 static struct platform_driver zynq_fpga_driver
= {
503 .probe
= zynq_fpga_probe
,
504 .remove
= zynq_fpga_remove
,
506 .name
= "zynq_fpga_manager",
507 .of_match_table
= of_match_ptr(zynq_fpga_of_match
),
511 module_platform_driver(zynq_fpga_driver
);
513 MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
514 MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
515 MODULE_DESCRIPTION("Xilinx Zynq FPGA Manager");
516 MODULE_LICENSE("GPL v2");