Merge tag 'nvme-5.13-2021-05-05' of git://git.infradead.org/nvme into block-5.13
[linux-2.6-microblaze.git] / drivers / bus / bt1-axi.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
4  *
5  * Authors:
6  *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
7  *
8  * Baikal-T1 AXI-bus driver
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/bitfield.h>
15 #include <linux/device.h>
16 #include <linux/atomic.h>
17 #include <linux/regmap.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/nmi.h>
23 #include <linux/of.h>
24 #include <linux/clk.h>
25 #include <linux/reset.h>
26 #include <linux/sysfs.h>
27
28 #define BT1_AXI_WERRL                   0x110
29 #define BT1_AXI_WERRH                   0x114
30 #define BT1_AXI_WERRH_TYPE              BIT(23)
31 #define BT1_AXI_WERRH_ADDR_FLD          24
32 #define BT1_AXI_WERRH_ADDR_MASK         GENMASK(31, BT1_AXI_WERRH_ADDR_FLD)
33
34 /*
35  * struct bt1_axi - Baikal-T1 AXI-bus private data
36  * @dev: Pointer to the device structure.
37  * @qos_regs: AXI Interconnect QoS tuning registers.
38  * @sys_regs: Baikal-T1 System Controller registers map.
39  * @irq: Errors IRQ number.
40  * @aclk: AXI reference clock.
41  * @arst: AXI Interconnect reset line.
42  * @count: Number of errors detected.
43  */
44 struct bt1_axi {
45         struct device *dev;
46
47         void __iomem *qos_regs;
48         struct regmap *sys_regs;
49         int irq;
50
51         struct clk *aclk;
52
53         struct reset_control *arst;
54
55         atomic_t count;
56 };
57
58 static irqreturn_t bt1_axi_isr(int irq, void *data)
59 {
60         struct bt1_axi *axi = data;
61         u32 low = 0, high = 0;
62
63         regmap_read(axi->sys_regs, BT1_AXI_WERRL, &low);
64         regmap_read(axi->sys_regs, BT1_AXI_WERRH, &high);
65
66         dev_crit_ratelimited(axi->dev,
67                 "AXI-bus fault %d: %s at 0x%x%08x\n",
68                 atomic_inc_return(&axi->count),
69                 high & BT1_AXI_WERRH_TYPE ? "no slave" : "slave protocol error",
70                 high, low);
71
72         /*
73          * Print backtrace on each CPU. This might be pointless if the fault
74          * has happened on the same CPU as the IRQ handler is executed or
75          * the other core proceeded further execution despite the error.
76          * But if it's not, by looking at the trace we would get straight to
77          * the cause of the problem.
78          */
79         trigger_all_cpu_backtrace();
80
81         return IRQ_HANDLED;
82 }
83
84 static void bt1_axi_clear_data(void *data)
85 {
86         struct bt1_axi *axi = data;
87         struct platform_device *pdev = to_platform_device(axi->dev);
88
89         platform_set_drvdata(pdev, NULL);
90 }
91
92 static struct bt1_axi *bt1_axi_create_data(struct platform_device *pdev)
93 {
94         struct device *dev = &pdev->dev;
95         struct bt1_axi *axi;
96         int ret;
97
98         axi = devm_kzalloc(dev, sizeof(*axi), GFP_KERNEL);
99         if (!axi)
100                 return ERR_PTR(-ENOMEM);
101
102         ret = devm_add_action(dev, bt1_axi_clear_data, axi);
103         if (ret) {
104                 dev_err(dev, "Can't add AXI EHB data clear action\n");
105                 return ERR_PTR(ret);
106         }
107
108         axi->dev = dev;
109         atomic_set(&axi->count, 0);
110         platform_set_drvdata(pdev, axi);
111
112         return axi;
113 }
114
115 static int bt1_axi_request_regs(struct bt1_axi *axi)
116 {
117         struct platform_device *pdev = to_platform_device(axi->dev);
118         struct device *dev = axi->dev;
119
120         axi->sys_regs = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
121         if (IS_ERR(axi->sys_regs)) {
122                 dev_err(dev, "Couldn't find syscon registers\n");
123                 return PTR_ERR(axi->sys_regs);
124         }
125
126         axi->qos_regs = devm_platform_ioremap_resource_byname(pdev, "qos");
127         if (IS_ERR(axi->qos_regs))
128                 dev_err(dev, "Couldn't map AXI-bus QoS registers\n");
129
130         return PTR_ERR_OR_ZERO(axi->qos_regs);
131 }
132
133 static int bt1_axi_request_rst(struct bt1_axi *axi)
134 {
135         int ret;
136
137         axi->arst = devm_reset_control_get_optional_exclusive(axi->dev, "arst");
138         if (IS_ERR(axi->arst)) {
139                 dev_warn(axi->dev, "Couldn't get reset control line\n");
140                 return PTR_ERR(axi->arst);
141         }
142
143         ret = reset_control_deassert(axi->arst);
144         if (ret)
145                 dev_err(axi->dev, "Failed to deassert the reset line\n");
146
147         return ret;
148 }
149
150 static void bt1_axi_disable_clk(void *data)
151 {
152         struct bt1_axi *axi = data;
153
154         clk_disable_unprepare(axi->aclk);
155 }
156
157 static int bt1_axi_request_clk(struct bt1_axi *axi)
158 {
159         int ret;
160
161         axi->aclk = devm_clk_get(axi->dev, "aclk");
162         if (IS_ERR(axi->aclk)) {
163                 dev_err(axi->dev, "Couldn't get AXI Interconnect clock\n");
164                 return PTR_ERR(axi->aclk);
165         }
166
167         ret = clk_prepare_enable(axi->aclk);
168         if (ret) {
169                 dev_err(axi->dev, "Couldn't enable the AXI clock\n");
170                 return ret;
171         }
172
173         ret = devm_add_action_or_reset(axi->dev, bt1_axi_disable_clk, axi);
174         if (ret)
175                 dev_err(axi->dev, "Can't add AXI clock disable action\n");
176
177         return ret;
178 }
179
180 static int bt1_axi_request_irq(struct bt1_axi *axi)
181 {
182         struct platform_device *pdev = to_platform_device(axi->dev);
183         int ret;
184
185         axi->irq = platform_get_irq(pdev, 0);
186         if (axi->irq < 0)
187                 return axi->irq;
188
189         ret = devm_request_irq(axi->dev, axi->irq, bt1_axi_isr, IRQF_SHARED,
190                                "bt1-axi", axi);
191         if (ret)
192                 dev_err(axi->dev, "Couldn't request AXI EHB IRQ\n");
193
194         return ret;
195 }
196
197 static ssize_t count_show(struct device *dev,
198                           struct device_attribute *attr, char *buf)
199 {
200         struct bt1_axi *axi = dev_get_drvdata(dev);
201
202         return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&axi->count));
203 }
204 static DEVICE_ATTR_RO(count);
205
206 static ssize_t inject_error_show(struct device *dev,
207                                  struct device_attribute *attr, char *buf)
208 {
209         return scnprintf(buf, PAGE_SIZE, "Error injection: bus unaligned\n");
210 }
211
212 static ssize_t inject_error_store(struct device *dev,
213                                   struct device_attribute *attr,
214                                   const char *data, size_t count)
215 {
216         struct bt1_axi *axi = dev_get_drvdata(dev);
217
218         /*
219          * Performing unaligned read from the memory will cause the CM2 bus
220          * error while unaligned writing - the AXI bus write error handled
221          * by this driver.
222          */
223         if (sysfs_streq(data, "bus"))
224                 readb(axi->qos_regs);
225         else if (sysfs_streq(data, "unaligned"))
226                 writeb(0, axi->qos_regs);
227         else
228                 return -EINVAL;
229
230         return count;
231 }
232 static DEVICE_ATTR_RW(inject_error);
233
234 static struct attribute *bt1_axi_sysfs_attrs[] = {
235         &dev_attr_count.attr,
236         &dev_attr_inject_error.attr,
237         NULL
238 };
239 ATTRIBUTE_GROUPS(bt1_axi_sysfs);
240
241 static void bt1_axi_remove_sysfs(void *data)
242 {
243         struct bt1_axi *axi = data;
244
245         device_remove_groups(axi->dev, bt1_axi_sysfs_groups);
246 }
247
248 static int bt1_axi_init_sysfs(struct bt1_axi *axi)
249 {
250         int ret;
251
252         ret = device_add_groups(axi->dev, bt1_axi_sysfs_groups);
253         if (ret) {
254                 dev_err(axi->dev, "Failed to add sysfs files group\n");
255                 return ret;
256         }
257
258         ret = devm_add_action_or_reset(axi->dev, bt1_axi_remove_sysfs, axi);
259         if (ret)
260                 dev_err(axi->dev, "Can't add AXI EHB sysfs remove action\n");
261
262         return ret;
263 }
264
265 static int bt1_axi_probe(struct platform_device *pdev)
266 {
267         struct bt1_axi *axi;
268         int ret;
269
270         axi = bt1_axi_create_data(pdev);
271         if (IS_ERR(axi))
272                 return PTR_ERR(axi);
273
274         ret = bt1_axi_request_regs(axi);
275         if (ret)
276                 return ret;
277
278         ret = bt1_axi_request_rst(axi);
279         if (ret)
280                 return ret;
281
282         ret = bt1_axi_request_clk(axi);
283         if (ret)
284                 return ret;
285
286         ret = bt1_axi_request_irq(axi);
287         if (ret)
288                 return ret;
289
290         ret = bt1_axi_init_sysfs(axi);
291         if (ret)
292                 return ret;
293
294         return 0;
295 }
296
297 static const struct of_device_id bt1_axi_of_match[] = {
298         { .compatible = "baikal,bt1-axi" },
299         { }
300 };
301 MODULE_DEVICE_TABLE(of, bt1_axi_of_match);
302
303 static struct platform_driver bt1_axi_driver = {
304         .probe = bt1_axi_probe,
305         .driver = {
306                 .name = "bt1-axi",
307                 .of_match_table = bt1_axi_of_match
308         }
309 };
310 module_platform_driver(bt1_axi_driver);
311
312 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
313 MODULE_DESCRIPTION("Baikal-T1 AXI-bus driver");
314 MODULE_LICENSE("GPL v2");