blob: 7d69af23056773136fa4611d0dd7d178ee2e4567 [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Moritz Fischer7e961c12017-03-24 10:33:21 -05002/*
3 * Copyright (c) 2017, National Instruments Corp.
4 * Copyright (c) 2017, Xilix Inc
5 *
6 * FPGA Bridge Driver for the Xilinx LogiCORE Partial Reconfiguration
7 * Decoupler IP Core.
Moritz Fischer7e961c12017-03-24 10:33:21 -05008 */
9
10#include <linux/clk.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/of_device.h>
14#include <linux/module.h>
15#include <linux/fpga/fpga-bridge.h>
16
17#define CTRL_CMD_DECOUPLE BIT(0)
18#define CTRL_CMD_COUPLE 0
19#define CTRL_OFFSET 0
20
21struct xlnx_pr_decoupler_data {
22 void __iomem *io_base;
23 struct clk *clk;
24};
25
26static inline void xlnx_pr_decoupler_write(struct xlnx_pr_decoupler_data *d,
27 u32 offset, u32 val)
28{
29 writel(val, d->io_base + offset);
30}
31
32static inline u32 xlnx_pr_decouple_read(const struct xlnx_pr_decoupler_data *d,
33 u32 offset)
34{
35 return readl(d->io_base + offset);
36}
37
38static int xlnx_pr_decoupler_enable_set(struct fpga_bridge *bridge, bool enable)
39{
40 int err;
41 struct xlnx_pr_decoupler_data *priv = bridge->priv;
42
43 err = clk_enable(priv->clk);
44 if (err)
45 return err;
46
47 if (enable)
48 xlnx_pr_decoupler_write(priv, CTRL_OFFSET, CTRL_CMD_COUPLE);
49 else
50 xlnx_pr_decoupler_write(priv, CTRL_OFFSET, CTRL_CMD_DECOUPLE);
51
52 clk_disable(priv->clk);
53
54 return 0;
55}
56
57static int xlnx_pr_decoupler_enable_show(struct fpga_bridge *bridge)
58{
59 const struct xlnx_pr_decoupler_data *priv = bridge->priv;
60 u32 status;
61 int err;
62
63 err = clk_enable(priv->clk);
64 if (err)
65 return err;
66
67 status = readl(priv->io_base);
68
69 clk_disable(priv->clk);
70
71 return !status;
72}
73
Bhumika Goyald8d9d932017-09-21 09:52:41 -050074static const struct fpga_bridge_ops xlnx_pr_decoupler_br_ops = {
Moritz Fischer7e961c12017-03-24 10:33:21 -050075 .enable_set = xlnx_pr_decoupler_enable_set,
76 .enable_show = xlnx_pr_decoupler_enable_show,
77};
78
79static const struct of_device_id xlnx_pr_decoupler_of_match[] = {
80 { .compatible = "xlnx,pr-decoupler-1.00", },
81 { .compatible = "xlnx,pr-decoupler", },
82 {},
83};
84MODULE_DEVICE_TABLE(of, xlnx_pr_decoupler_of_match);
85
86static int xlnx_pr_decoupler_probe(struct platform_device *pdev)
87{
88 struct xlnx_pr_decoupler_data *priv;
Alan Tull371cd1b2018-05-16 18:49:56 -050089 struct fpga_bridge *br;
Moritz Fischer7e961c12017-03-24 10:33:21 -050090 int err;
91 struct resource *res;
92
93 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
94 if (!priv)
95 return -ENOMEM;
96
97 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
98 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
99 if (IS_ERR(priv->io_base))
100 return PTR_ERR(priv->io_base);
101
102 priv->clk = devm_clk_get(&pdev->dev, "aclk");
103 if (IS_ERR(priv->clk)) {
Shubhrajyoti Datta28910ce2020-01-10 12:01:13 +0530104 if (PTR_ERR(priv->clk) != -EPROBE_DEFER)
105 dev_err(&pdev->dev, "input clock not found\n");
Moritz Fischer7e961c12017-03-24 10:33:21 -0500106 return PTR_ERR(priv->clk);
107 }
108
109 err = clk_prepare_enable(priv->clk);
110 if (err) {
111 dev_err(&pdev->dev, "unable to enable clock\n");
112 return err;
113 }
114
115 clk_disable(priv->clk);
116
Alan Tull213befe2018-10-15 17:20:02 -0500117 br = devm_fpga_bridge_create(&pdev->dev, "Xilinx PR Decoupler",
118 &xlnx_pr_decoupler_br_ops, priv);
Alan Tull371cd1b2018-05-16 18:49:56 -0500119 if (!br) {
120 err = -ENOMEM;
121 goto err_clk;
122 }
Moritz Fischer7e961c12017-03-24 10:33:21 -0500123
Alan Tull371cd1b2018-05-16 18:49:56 -0500124 platform_set_drvdata(pdev, br);
125
126 err = fpga_bridge_register(br);
Moritz Fischer7e961c12017-03-24 10:33:21 -0500127 if (err) {
128 dev_err(&pdev->dev, "unable to register Xilinx PR Decoupler");
Alan Tull371cd1b2018-05-16 18:49:56 -0500129 goto err_clk;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500130 }
131
132 return 0;
Alan Tull371cd1b2018-05-16 18:49:56 -0500133
134err_clk:
135 clk_unprepare(priv->clk);
136
137 return err;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500138}
139
140static int xlnx_pr_decoupler_remove(struct platform_device *pdev)
141{
142 struct fpga_bridge *bridge = platform_get_drvdata(pdev);
143 struct xlnx_pr_decoupler_data *p = bridge->priv;
144
Alan Tull371cd1b2018-05-16 18:49:56 -0500145 fpga_bridge_unregister(bridge);
Moritz Fischer7e961c12017-03-24 10:33:21 -0500146
147 clk_unprepare(p->clk);
148
149 return 0;
150}
151
152static struct platform_driver xlnx_pr_decoupler_driver = {
153 .probe = xlnx_pr_decoupler_probe,
154 .remove = xlnx_pr_decoupler_remove,
155 .driver = {
156 .name = "xlnx_pr_decoupler",
157 .of_match_table = of_match_ptr(xlnx_pr_decoupler_of_match),
158 },
159};
160
161module_platform_driver(xlnx_pr_decoupler_driver);
162
163MODULE_DESCRIPTION("Xilinx Partial Reconfiguration Decoupler");
164MODULE_AUTHOR("Moritz Fischer <mdf@kernel.org>");
165MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
166MODULE_LICENSE("GPL v2");