blob: e986ed47c4ed702866796eab197a3ed5b4776831 [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.
Nava kishore Manne30a2ac92021-02-11 10:41:48 +05304 * Copyright (c) 2017, Xilinx Inc
Moritz Fischer7e961c12017-03-24 10:33:21 -05005 *
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
Nava kishore Manne30a2ac92021-02-11 10:41:48 +053021struct xlnx_config_data {
22 const char *name;
23};
24
Moritz Fischer7e961c12017-03-24 10:33:21 -050025struct xlnx_pr_decoupler_data {
Nava kishore Manne30a2ac92021-02-11 10:41:48 +053026 const struct xlnx_config_data *ipconfig;
Moritz Fischer7e961c12017-03-24 10:33:21 -050027 void __iomem *io_base;
28 struct clk *clk;
29};
30
31static inline void xlnx_pr_decoupler_write(struct xlnx_pr_decoupler_data *d,
32 u32 offset, u32 val)
33{
34 writel(val, d->io_base + offset);
35}
36
37static inline u32 xlnx_pr_decouple_read(const struct xlnx_pr_decoupler_data *d,
38 u32 offset)
39{
40 return readl(d->io_base + offset);
41}
42
43static int xlnx_pr_decoupler_enable_set(struct fpga_bridge *bridge, bool enable)
44{
45 int err;
46 struct xlnx_pr_decoupler_data *priv = bridge->priv;
47
48 err = clk_enable(priv->clk);
49 if (err)
50 return err;
51
52 if (enable)
53 xlnx_pr_decoupler_write(priv, CTRL_OFFSET, CTRL_CMD_COUPLE);
54 else
55 xlnx_pr_decoupler_write(priv, CTRL_OFFSET, CTRL_CMD_DECOUPLE);
56
57 clk_disable(priv->clk);
58
59 return 0;
60}
61
62static int xlnx_pr_decoupler_enable_show(struct fpga_bridge *bridge)
63{
64 const struct xlnx_pr_decoupler_data *priv = bridge->priv;
65 u32 status;
66 int err;
67
68 err = clk_enable(priv->clk);
69 if (err)
70 return err;
71
72 status = readl(priv->io_base);
73
74 clk_disable(priv->clk);
75
76 return !status;
77}
78
Bhumika Goyald8d9d932017-09-21 09:52:41 -050079static const struct fpga_bridge_ops xlnx_pr_decoupler_br_ops = {
Moritz Fischer7e961c12017-03-24 10:33:21 -050080 .enable_set = xlnx_pr_decoupler_enable_set,
81 .enable_show = xlnx_pr_decoupler_enable_show,
82};
83
Moritz Fischer56ddc782021-07-01 20:54:03 -070084#ifdef CONFIG_OF
Nava kishore Manne30a2ac92021-02-11 10:41:48 +053085static const struct xlnx_config_data decoupler_config = {
86 .name = "Xilinx PR Decoupler",
87};
88
89static const struct xlnx_config_data shutdown_config = {
90 .name = "Xilinx DFX AXI Shutdown Manager",
91};
92
Moritz Fischer7e961c12017-03-24 10:33:21 -050093static const struct of_device_id xlnx_pr_decoupler_of_match[] = {
Nava kishore Manne30a2ac92021-02-11 10:41:48 +053094 { .compatible = "xlnx,pr-decoupler-1.00", .data = &decoupler_config },
95 { .compatible = "xlnx,pr-decoupler", .data = &decoupler_config },
96 { .compatible = "xlnx,dfx-axi-shutdown-manager-1.00",
97 .data = &shutdown_config },
98 { .compatible = "xlnx,dfx-axi-shutdown-manager",
99 .data = &shutdown_config },
Moritz Fischer7e961c12017-03-24 10:33:21 -0500100 {},
101};
102MODULE_DEVICE_TABLE(of, xlnx_pr_decoupler_of_match);
Moritz Fischer56ddc782021-07-01 20:54:03 -0700103#endif
Moritz Fischer7e961c12017-03-24 10:33:21 -0500104
105static int xlnx_pr_decoupler_probe(struct platform_device *pdev)
106{
Nava kishore Manne30a2ac92021-02-11 10:41:48 +0530107 struct device_node *np = pdev->dev.of_node;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500108 struct xlnx_pr_decoupler_data *priv;
Alan Tull371cd1b2018-05-16 18:49:56 -0500109 struct fpga_bridge *br;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500110 int err;
111 struct resource *res;
112
113 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
114 if (!priv)
115 return -ENOMEM;
116
Nava kishore Manne30a2ac92021-02-11 10:41:48 +0530117 if (np) {
118 const struct of_device_id *match;
119
120 match = of_match_node(xlnx_pr_decoupler_of_match, np);
121 if (match && match->data)
122 priv->ipconfig = match->data;
123 }
124
Moritz Fischer7e961c12017-03-24 10:33:21 -0500125 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
126 priv->io_base = devm_ioremap_resource(&pdev->dev, res);
127 if (IS_ERR(priv->io_base))
128 return PTR_ERR(priv->io_base);
129
130 priv->clk = devm_clk_get(&pdev->dev, "aclk");
Michal Simekb5c6ecf2021-02-04 14:36:11 +0100131 if (IS_ERR(priv->clk))
132 return dev_err_probe(&pdev->dev, PTR_ERR(priv->clk),
133 "input clock not found\n");
Moritz Fischer7e961c12017-03-24 10:33:21 -0500134
135 err = clk_prepare_enable(priv->clk);
136 if (err) {
137 dev_err(&pdev->dev, "unable to enable clock\n");
138 return err;
139 }
140
141 clk_disable(priv->clk);
142
Nava kishore Manne30a2ac92021-02-11 10:41:48 +0530143 br = devm_fpga_bridge_create(&pdev->dev, priv->ipconfig->name,
Alan Tull213befe2018-10-15 17:20:02 -0500144 &xlnx_pr_decoupler_br_ops, priv);
Alan Tull371cd1b2018-05-16 18:49:56 -0500145 if (!br) {
146 err = -ENOMEM;
147 goto err_clk;
148 }
Moritz Fischer7e961c12017-03-24 10:33:21 -0500149
Alan Tull371cd1b2018-05-16 18:49:56 -0500150 platform_set_drvdata(pdev, br);
151
152 err = fpga_bridge_register(br);
Moritz Fischer7e961c12017-03-24 10:33:21 -0500153 if (err) {
Nava kishore Manne30a2ac92021-02-11 10:41:48 +0530154 dev_err(&pdev->dev, "unable to register %s",
155 priv->ipconfig->name);
Alan Tull371cd1b2018-05-16 18:49:56 -0500156 goto err_clk;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500157 }
158
159 return 0;
Alan Tull371cd1b2018-05-16 18:49:56 -0500160
161err_clk:
162 clk_unprepare(priv->clk);
163
164 return err;
Moritz Fischer7e961c12017-03-24 10:33:21 -0500165}
166
167static int xlnx_pr_decoupler_remove(struct platform_device *pdev)
168{
169 struct fpga_bridge *bridge = platform_get_drvdata(pdev);
170 struct xlnx_pr_decoupler_data *p = bridge->priv;
171
Alan Tull371cd1b2018-05-16 18:49:56 -0500172 fpga_bridge_unregister(bridge);
Moritz Fischer7e961c12017-03-24 10:33:21 -0500173
174 clk_unprepare(p->clk);
175
176 return 0;
177}
178
179static struct platform_driver xlnx_pr_decoupler_driver = {
180 .probe = xlnx_pr_decoupler_probe,
181 .remove = xlnx_pr_decoupler_remove,
182 .driver = {
183 .name = "xlnx_pr_decoupler",
184 .of_match_table = of_match_ptr(xlnx_pr_decoupler_of_match),
185 },
186};
187
188module_platform_driver(xlnx_pr_decoupler_driver);
189
190MODULE_DESCRIPTION("Xilinx Partial Reconfiguration Decoupler");
191MODULE_AUTHOR("Moritz Fischer <mdf@kernel.org>");
192MODULE_AUTHOR("Michal Simek <michal.simek@xilinx.com>");
193MODULE_LICENSE("GPL v2");