blob: f674e32832ec4438668d318d0768541fb78729fd [file] [log] [blame]
Moritz Fischer37784702015-10-16 15:42:30 -07001/*
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
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>
24#include <linux/io.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>
30#include <linux/pm.h>
31#include <linux/regmap.h>
32#include <linux/string.h>
33
34/* Offsets into SLCR regmap */
35
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
40
41/* Constant Definitions */
42
43/* Control Register */
44#define CTRL_OFFSET 0x00
45/* Lock Register */
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
51/* Status Register */
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
61/* Unlock Register */
62#define UNLOCK_OFFSET 0x34
63/* Misc. Control Register */
64#define MCTRL_OFFSET 0x80
65
66/* Control Register Bit definitions */
67
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)
72/* Enable PCAP */
73#define CTRL_PCAP_MODE_MASK BIT(26)
74
75/* Miscellaneous Control Register bit definitions */
76/* Internal PCAP loopback */
77#define MCTRL_PCAP_LPBK_MASK BIT(4)
78
79/* Status register bit definitions */
80
81/* FPGA init status */
82#define STATUS_DMA_Q_F BIT(31)
83#define STATUS_PCFG_INIT_MASK BIT(4)
84
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)
90 /* FPGA programmed */
91#define IXR_PCFG_DONE_MASK BIT(2)
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -070092#define IXR_ERROR_FLAGS_MASK 0x00F0C860
Moritz Fischer37784702015-10-16 15:42:30 -070093#define IXR_ALL_MASK 0xF8F7F87F
94
95/* Miscellaneous constant values */
96
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
107
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
119
120struct zynq_fpga_priv {
Moritz Fischer37784702015-10-16 15:42:30 -0700121 int irq;
122 struct clk *clk;
123
124 void __iomem *io_base;
125 struct regmap *slcr;
126
127 struct completion dma_done;
128};
129
130static inline void zynq_fpga_write(struct zynq_fpga_priv *priv, u32 offset,
131 u32 val)
132{
133 writel(val, priv->io_base + offset);
134}
135
136static inline u32 zynq_fpga_read(const struct zynq_fpga_priv *priv,
137 u32 offset)
138{
139 return readl(priv->io_base + offset);
140}
141
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, \
144 timeout_us)
145
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700146/* Cause the specified irq mask bits to generate IRQs */
147static inline void zynq_fpga_set_irq(struct zynq_fpga_priv *priv, u32 enable)
Moritz Fischer37784702015-10-16 15:42:30 -0700148{
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700149 zynq_fpga_write(priv, INT_MASK_OFFSET, ~enable);
Moritz Fischer37784702015-10-16 15:42:30 -0700150}
151
152static irqreturn_t zynq_fpga_isr(int irq, void *data)
153{
154 struct zynq_fpga_priv *priv = data;
155
156 /* disable DMA and error IRQs */
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700157 zynq_fpga_set_irq(priv, 0);
Moritz Fischer37784702015-10-16 15:42:30 -0700158
159 complete(&priv->dma_done);
160
161 return IRQ_HANDLED;
162}
163
Alan Tull1df28652016-11-01 14:14:26 -0500164static int zynq_fpga_ops_write_init(struct fpga_manager *mgr,
165 struct fpga_image_info *info,
Moritz Fischer37784702015-10-16 15:42:30 -0700166 const char *buf, size_t count)
167{
168 struct zynq_fpga_priv *priv;
169 u32 ctrl, status;
170 int err;
171
172 priv = mgr->priv;
173
174 err = clk_enable(priv->clk);
175 if (err)
176 return err;
177
178 /* don't globally reset PL if we're doing partial reconfig */
Alan Tull1df28652016-11-01 14:14:26 -0500179 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
Moritz Fischer37784702015-10-16 15:42:30 -0700180 /* assert AXI interface resets */
181 regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
182 FPGA_RST_ALL_MASK);
183
184 /* disable all level shifters */
185 regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
186 LVL_SHFTR_DISABLE_ALL_MASK);
187 /* enable level shifters from PS to PL */
188 regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
189 LVL_SHFTR_ENABLE_PS_TO_PL);
190
191 /* create a rising edge on PCFG_INIT. PCFG_INIT follows
192 * PCFG_PROG_B, so we need to poll it after setting PCFG_PROG_B
193 * to make sure the rising edge actually happens.
194 * Note: PCFG_PROG_B is low active, sequence as described in
195 * UG585 v1.10 page 211
196 */
197 ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
198 ctrl |= CTRL_PCFG_PROG_B_MASK;
199
200 zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
201
202 err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
203 status & STATUS_PCFG_INIT_MASK,
204 INIT_POLL_DELAY,
205 INIT_POLL_TIMEOUT);
206 if (err) {
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000207 dev_err(&mgr->dev, "Timeout waiting for PCFG_INIT\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700208 goto out_err;
209 }
210
211 ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
212 ctrl &= ~CTRL_PCFG_PROG_B_MASK;
213
214 zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
215
216 err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
217 !(status & STATUS_PCFG_INIT_MASK),
218 INIT_POLL_DELAY,
219 INIT_POLL_TIMEOUT);
220 if (err) {
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000221 dev_err(&mgr->dev, "Timeout waiting for !PCFG_INIT\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700222 goto out_err;
223 }
224
225 ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
226 ctrl |= CTRL_PCFG_PROG_B_MASK;
227
228 zynq_fpga_write(priv, CTRL_OFFSET, ctrl);
229
230 err = zynq_fpga_poll_timeout(priv, STATUS_OFFSET, status,
231 status & STATUS_PCFG_INIT_MASK,
232 INIT_POLL_DELAY,
233 INIT_POLL_TIMEOUT);
234 if (err) {
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000235 dev_err(&mgr->dev, "Timeout waiting for PCFG_INIT\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700236 goto out_err;
237 }
238 }
239
240 /* set configuration register with following options:
241 * - enable PCAP interface
242 * - set throughput for maximum speed
243 * - set CPU in user mode
244 */
245 ctrl = zynq_fpga_read(priv, CTRL_OFFSET);
246 zynq_fpga_write(priv, CTRL_OFFSET,
247 (CTRL_PCAP_PR_MASK | CTRL_PCAP_MODE_MASK | ctrl));
248
249 /* check that we have room in the command queue */
250 status = zynq_fpga_read(priv, STATUS_OFFSET);
251 if (status & STATUS_DMA_Q_F) {
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000252 dev_err(&mgr->dev, "DMA command queue full\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700253 err = -EBUSY;
254 goto out_err;
255 }
256
257 /* ensure internal PCAP loopback is disabled */
258 ctrl = zynq_fpga_read(priv, MCTRL_OFFSET);
259 zynq_fpga_write(priv, MCTRL_OFFSET, (~MCTRL_PCAP_LPBK_MASK & ctrl));
260
261 clk_disable(priv->clk);
262
263 return 0;
264
265out_err:
266 clk_disable(priv->clk);
267
268 return err;
269}
270
271static int zynq_fpga_ops_write(struct fpga_manager *mgr,
272 const char *buf, size_t count)
273{
274 struct zynq_fpga_priv *priv;
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700275 const char *why;
Moritz Fischer37784702015-10-16 15:42:30 -0700276 int err;
277 char *kbuf;
Moritz Fischer4d10eaf2015-10-20 10:19:56 -0700278 size_t in_count;
Moritz Fischer37784702015-10-16 15:42:30 -0700279 dma_addr_t dma_addr;
Moritz Fischer4d10eaf2015-10-20 10:19:56 -0700280 u32 transfer_length;
Moritz Fischer37784702015-10-16 15:42:30 -0700281 u32 intr_status;
282
283 in_count = count;
284 priv = mgr->priv;
285
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000286 kbuf =
287 dma_alloc_coherent(mgr->dev.parent, count, &dma_addr, GFP_KERNEL);
Moritz Fischer37784702015-10-16 15:42:30 -0700288 if (!kbuf)
289 return -ENOMEM;
290
291 memcpy(kbuf, buf, count);
292
Moritz Fischer37784702015-10-16 15:42:30 -0700293 /* enable clock */
294 err = clk_enable(priv->clk);
295 if (err)
296 goto out_free;
297
298 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
299
300 reinit_completion(&priv->dma_done);
301
302 /* enable DMA and error IRQs */
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700303 zynq_fpga_set_irq(priv, IXR_D_P_DONE_MASK | IXR_ERROR_FLAGS_MASK);
Moritz Fischer37784702015-10-16 15:42:30 -0700304
305 /* the +1 in the src addr is used to hold off on DMA_DONE IRQ
306 * until both AXI and PCAP are done ...
307 */
308 zynq_fpga_write(priv, DMA_SRC_ADDR_OFFSET, (u32)(dma_addr) + 1);
309 zynq_fpga_write(priv, DMA_DST_ADDR_OFFSET, (u32)DMA_INVALID_ADDRESS);
310
311 /* convert #bytes to #words */
312 transfer_length = (count + 3) / 4;
313
314 zynq_fpga_write(priv, DMA_SRC_LEN_OFFSET, transfer_length);
315 zynq_fpga_write(priv, DMA_DEST_LEN_OFFSET, 0);
316
317 wait_for_completion(&priv->dma_done);
318
319 intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
320 zynq_fpga_write(priv, INT_STS_OFFSET, intr_status);
321
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700322 if (intr_status & IXR_ERROR_FLAGS_MASK) {
323 why = "DMA reported error";
324 err = -EIO;
325 goto out_report;
Moritz Fischer37784702015-10-16 15:42:30 -0700326 }
327
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700328 if (!((intr_status & IXR_D_P_DONE_MASK) == IXR_D_P_DONE_MASK)) {
329 why = "DMA did not complete";
330 err = -EIO;
331 goto out_report;
332 }
333
334 err = 0;
335 goto out_clk;
336
337out_report:
338 dev_err(&mgr->dev,
339 "%s: INT_STS:0x%x CTRL:0x%x LOCK:0x%x INT_MASK:0x%x STATUS:0x%x MCTRL:0x%x\n",
340 why,
341 intr_status,
342 zynq_fpga_read(priv, CTRL_OFFSET),
343 zynq_fpga_read(priv, LOCK_OFFSET),
344 zynq_fpga_read(priv, INT_MASK_OFFSET),
345 zynq_fpga_read(priv, STATUS_OFFSET),
346 zynq_fpga_read(priv, MCTRL_OFFSET));
347
348out_clk:
Moritz Fischer37784702015-10-16 15:42:30 -0700349 clk_disable(priv->clk);
350
351out_free:
Jason Gunthorpe80baf642016-11-21 22:26:44 +0000352 dma_free_coherent(mgr->dev.parent, count, kbuf, dma_addr);
Moritz Fischer37784702015-10-16 15:42:30 -0700353 return err;
354}
355
Alan Tull1df28652016-11-01 14:14:26 -0500356static int zynq_fpga_ops_write_complete(struct fpga_manager *mgr,
357 struct fpga_image_info *info)
Moritz Fischer37784702015-10-16 15:42:30 -0700358{
359 struct zynq_fpga_priv *priv = mgr->priv;
360 int err;
361 u32 intr_status;
362
363 err = clk_enable(priv->clk);
364 if (err)
365 return err;
366
367 err = zynq_fpga_poll_timeout(priv, INT_STS_OFFSET, intr_status,
368 intr_status & IXR_PCFG_DONE_MASK,
369 INIT_POLL_DELAY,
370 INIT_POLL_TIMEOUT);
371
372 clk_disable(priv->clk);
373
374 if (err)
375 return err;
376
377 /* for the partial reconfig case we didn't touch the level shifters */
Alan Tull1df28652016-11-01 14:14:26 -0500378 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
Moritz Fischer37784702015-10-16 15:42:30 -0700379 /* enable level shifters from PL to PS */
380 regmap_write(priv->slcr, SLCR_LVL_SHFTR_EN_OFFSET,
381 LVL_SHFTR_ENABLE_PL_TO_PS);
382
383 /* deassert AXI interface resets */
384 regmap_write(priv->slcr, SLCR_FPGA_RST_CTRL_OFFSET,
385 FPGA_RST_NONE_MASK);
386 }
387
388 return 0;
389}
390
391static enum fpga_mgr_states zynq_fpga_ops_state(struct fpga_manager *mgr)
392{
393 int err;
394 u32 intr_status;
395 struct zynq_fpga_priv *priv;
396
397 priv = mgr->priv;
398
399 err = clk_enable(priv->clk);
400 if (err)
401 return FPGA_MGR_STATE_UNKNOWN;
402
403 intr_status = zynq_fpga_read(priv, INT_STS_OFFSET);
404 clk_disable(priv->clk);
405
406 if (intr_status & IXR_PCFG_DONE_MASK)
407 return FPGA_MGR_STATE_OPERATING;
408
409 return FPGA_MGR_STATE_UNKNOWN;
410}
411
412static const struct fpga_manager_ops zynq_fpga_ops = {
413 .state = zynq_fpga_ops_state,
414 .write_init = zynq_fpga_ops_write_init,
415 .write = zynq_fpga_ops_write,
416 .write_complete = zynq_fpga_ops_write_complete,
417};
418
419static int zynq_fpga_probe(struct platform_device *pdev)
420{
421 struct device *dev = &pdev->dev;
422 struct zynq_fpga_priv *priv;
423 struct resource *res;
424 int err;
425
426 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
427 if (!priv)
428 return -ENOMEM;
429
Moritz Fischer37784702015-10-16 15:42:30 -0700430 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
431 priv->io_base = devm_ioremap_resource(dev, res);
432 if (IS_ERR(priv->io_base))
433 return PTR_ERR(priv->io_base);
434
435 priv->slcr = syscon_regmap_lookup_by_phandle(dev->of_node,
436 "syscon");
437 if (IS_ERR(priv->slcr)) {
Jason Gunthorpe1930c282016-11-21 22:26:43 +0000438 dev_err(dev, "unable to get zynq-slcr regmap\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700439 return PTR_ERR(priv->slcr);
440 }
441
442 init_completion(&priv->dma_done);
443
444 priv->irq = platform_get_irq(pdev, 0);
445 if (priv->irq < 0) {
Jason Gunthorpe1930c282016-11-21 22:26:43 +0000446 dev_err(dev, "No IRQ available\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700447 return priv->irq;
448 }
449
Moritz Fischer37784702015-10-16 15:42:30 -0700450 priv->clk = devm_clk_get(dev, "ref_clk");
451 if (IS_ERR(priv->clk)) {
Jason Gunthorpe1930c282016-11-21 22:26:43 +0000452 dev_err(dev, "input clock not found\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700453 return PTR_ERR(priv->clk);
454 }
455
456 err = clk_prepare_enable(priv->clk);
457 if (err) {
Jason Gunthorpe1930c282016-11-21 22:26:43 +0000458 dev_err(dev, "unable to enable clock\n");
Moritz Fischer37784702015-10-16 15:42:30 -0700459 return err;
460 }
461
462 /* unlock the device */
463 zynq_fpga_write(priv, UNLOCK_OFFSET, UNLOCK_MASK);
464
Jason Gunthorpe6b45e0f2017-02-01 12:48:42 -0700465 zynq_fpga_set_irq(priv, 0);
Jason Gunthorpe340c0c52016-11-21 22:26:45 +0000466 zynq_fpga_write(priv, INT_STS_OFFSET, IXR_ALL_MASK);
467 err = devm_request_irq(dev, priv->irq, zynq_fpga_isr, 0, dev_name(dev),
468 priv);
469 if (err) {
470 dev_err(dev, "unable to request IRQ\n");
471 clk_disable_unprepare(priv->clk);
472 return err;
473 }
474
Moritz Fischer37784702015-10-16 15:42:30 -0700475 clk_disable(priv->clk);
476
477 err = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
478 &zynq_fpga_ops, priv);
479 if (err) {
Jason Gunthorpe1930c282016-11-21 22:26:43 +0000480 dev_err(dev, "unable to register FPGA manager\n");
Moritz Fischer63769312015-10-19 13:35:33 -0700481 clk_unprepare(priv->clk);
Moritz Fischer37784702015-10-16 15:42:30 -0700482 return err;
483 }
484
485 return 0;
486}
487
488static int zynq_fpga_remove(struct platform_device *pdev)
489{
490 struct zynq_fpga_priv *priv;
Moritz Fischer28f98a12015-10-22 11:56:09 -0700491 struct fpga_manager *mgr;
492
493 mgr = platform_get_drvdata(pdev);
494 priv = mgr->priv;
Moritz Fischer37784702015-10-16 15:42:30 -0700495
496 fpga_mgr_unregister(&pdev->dev);
497
Moritz Fischer63769312015-10-19 13:35:33 -0700498 clk_unprepare(priv->clk);
Moritz Fischer37784702015-10-16 15:42:30 -0700499
500 return 0;
501}
502
503#ifdef CONFIG_OF
504static const struct of_device_id zynq_fpga_of_match[] = {
505 { .compatible = "xlnx,zynq-devcfg-1.0", },
506 {},
507};
508
509MODULE_DEVICE_TABLE(of, zynq_fpga_of_match);
510#endif
511
512static struct platform_driver zynq_fpga_driver = {
513 .probe = zynq_fpga_probe,
514 .remove = zynq_fpga_remove,
515 .driver = {
516 .name = "zynq_fpga_manager",
517 .of_match_table = of_match_ptr(zynq_fpga_of_match),
518 },
519};
520
521module_platform_driver(zynq_fpga_driver);
522
523MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
524MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
525MODULE_DESCRIPTION("Xilinx Zynq FPGA Manager");
526MODULE_LICENSE("GPL v2");