xref: /linux/drivers/bus/bt1-apb.c (revision b19dc1b79993a7e6a2c0aa1725f679bb9ae8ad89)
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 APB-bus driver
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/atomic.h>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/nmi.h>
20 #include <linux/of.h>
21 #include <linux/regmap.h>
22 #include <linux/clk.h>
23 #include <linux/reset.h>
24 #include <linux/time64.h>
25 #include <linux/clk.h>
26 #include <linux/sysfs.h>
27 
28 #define APB_EHB_ISR			0x00
29 #define APB_EHB_ISR_PENDING		BIT(0)
30 #define APB_EHB_ISR_MASK		BIT(1)
31 #define APB_EHB_ADDR			0x04
32 #define APB_EHB_TIMEOUT			0x08
33 
34 #define APB_EHB_TIMEOUT_MIN		0x000003FFU
35 #define APB_EHB_TIMEOUT_MAX		0xFFFFFFFFU
36 
37 /*
38  * struct bt1_apb - Baikal-T1 APB EHB private data
39  * @dev: Pointer to the device structure.
40  * @regs: APB EHB registers map.
41  * @res: No-device error injection memory region.
42  * @irq: Errors IRQ number.
43  * @rate: APB-bus reference clock rate.
44  * @pclk: APB-reference clock.
45  * @prst: APB domain reset line.
46  * @count: Number of errors detected.
47  */
48 struct bt1_apb {
49 	struct device *dev;
50 
51 	struct regmap *regs;
52 	void __iomem *res;
53 	int irq;
54 
55 	unsigned long rate;
56 	struct clk *pclk;
57 
58 	struct reset_control *prst;
59 
60 	atomic_t count;
61 };
62 
63 static const struct regmap_config bt1_apb_regmap_cfg = {
64 	.reg_bits = 32,
65 	.val_bits = 32,
66 	.reg_stride = 4,
67 	.max_register = APB_EHB_TIMEOUT,
68 	.fast_io = true
69 };
70 
71 static inline unsigned long bt1_apb_n_to_timeout_us(struct bt1_apb *apb, u32 n)
72 {
73 	u64 timeout = (u64)n * USEC_PER_SEC;
74 
75 	do_div(timeout, apb->rate);
76 
77 	return timeout;
78 
79 }
80 
81 static inline unsigned long bt1_apb_timeout_to_n_us(struct bt1_apb *apb,
82 						    unsigned long timeout)
83 {
84 	u64 n = (u64)timeout * apb->rate;
85 
86 	do_div(n, USEC_PER_SEC);
87 
88 	return n;
89 
90 }
91 
92 static irqreturn_t bt1_apb_isr(int irq, void *data)
93 {
94 	struct bt1_apb *apb = data;
95 	u32 addr = 0;
96 
97 	regmap_read(apb->regs, APB_EHB_ADDR, &addr);
98 
99 	dev_crit_ratelimited(apb->dev,
100 		"APB-bus fault %d: Slave access timeout at 0x%08x\n",
101 		atomic_inc_return(&apb->count),
102 		addr);
103 
104 	/*
105 	 * Print backtrace on each CPU. This might be pointless if the fault
106 	 * has happened on the same CPU as the IRQ handler is executed or
107 	 * the other core proceeded further execution despite the error.
108 	 * But if it's not, by looking at the trace we would get straight to
109 	 * the cause of the problem.
110 	 */
111 	trigger_all_cpu_backtrace();
112 
113 	regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING, 0);
114 
115 	return IRQ_HANDLED;
116 }
117 
118 static void bt1_apb_clear_data(void *data)
119 {
120 	struct bt1_apb *apb = data;
121 	struct platform_device *pdev = to_platform_device(apb->dev);
122 
123 	platform_set_drvdata(pdev, NULL);
124 }
125 
126 static struct bt1_apb *bt1_apb_create_data(struct platform_device *pdev)
127 {
128 	struct device *dev = &pdev->dev;
129 	struct bt1_apb *apb;
130 	int ret;
131 
132 	apb = devm_kzalloc(dev, sizeof(*apb), GFP_KERNEL);
133 	if (!apb)
134 		return ERR_PTR(-ENOMEM);
135 
136 	ret = devm_add_action(dev, bt1_apb_clear_data, apb);
137 	if (ret) {
138 		dev_err(dev, "Can't add APB EHB data clear action\n");
139 		return ERR_PTR(ret);
140 	}
141 
142 	apb->dev = dev;
143 	atomic_set(&apb->count, 0);
144 	platform_set_drvdata(pdev, apb);
145 
146 	return apb;
147 }
148 
149 static int bt1_apb_request_regs(struct bt1_apb *apb)
150 {
151 	struct platform_device *pdev = to_platform_device(apb->dev);
152 	void __iomem *regs;
153 
154 	regs = devm_platform_ioremap_resource_byname(pdev, "ehb");
155 	if (IS_ERR(regs)) {
156 		dev_err(apb->dev, "Couldn't map APB EHB registers\n");
157 		return PTR_ERR(regs);
158 	}
159 
160 	apb->regs = devm_regmap_init_mmio(apb->dev, regs, &bt1_apb_regmap_cfg);
161 	if (IS_ERR(apb->regs)) {
162 		dev_err(apb->dev, "Couldn't create APB EHB regmap\n");
163 		return PTR_ERR(apb->regs);
164 	}
165 
166 	apb->res = devm_platform_ioremap_resource_byname(pdev, "nodev");
167 	if (IS_ERR(apb->res)) {
168 		dev_err(apb->dev, "Couldn't map reserved region\n");
169 		return PTR_ERR(apb->res);
170 	}
171 
172 	return 0;
173 }
174 
175 static int bt1_apb_request_rst(struct bt1_apb *apb)
176 {
177 	int ret;
178 
179 	apb->prst = devm_reset_control_get_optional_exclusive(apb->dev, "prst");
180 	if (IS_ERR(apb->prst)) {
181 		dev_warn(apb->dev, "Couldn't get reset control line\n");
182 		return PTR_ERR(apb->prst);
183 	}
184 
185 	ret = reset_control_deassert(apb->prst);
186 	if (ret)
187 		dev_err(apb->dev, "Failed to deassert the reset line\n");
188 
189 	return ret;
190 }
191 
192 static void bt1_apb_disable_clk(void *data)
193 {
194 	struct bt1_apb *apb = data;
195 
196 	clk_disable_unprepare(apb->pclk);
197 }
198 
199 static int bt1_apb_request_clk(struct bt1_apb *apb)
200 {
201 	int ret;
202 
203 	apb->pclk = devm_clk_get(apb->dev, "pclk");
204 	if (IS_ERR(apb->pclk)) {
205 		dev_err(apb->dev, "Couldn't get APB clock descriptor\n");
206 		return PTR_ERR(apb->pclk);
207 	}
208 
209 	ret = clk_prepare_enable(apb->pclk);
210 	if (ret) {
211 		dev_err(apb->dev, "Couldn't enable the APB clock\n");
212 		return ret;
213 	}
214 
215 	ret = devm_add_action_or_reset(apb->dev, bt1_apb_disable_clk, apb);
216 	if (ret) {
217 		dev_err(apb->dev, "Can't add APB EHB clocks disable action\n");
218 		return ret;
219 	}
220 
221 	apb->rate = clk_get_rate(apb->pclk);
222 	if (!apb->rate) {
223 		dev_err(apb->dev, "Invalid clock rate\n");
224 		return -EINVAL;
225 	}
226 
227 	return 0;
228 }
229 
230 static void bt1_apb_clear_irq(void *data)
231 {
232 	struct bt1_apb *apb = data;
233 
234 	regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_MASK, 0);
235 }
236 
237 static int bt1_apb_request_irq(struct bt1_apb *apb)
238 {
239 	struct platform_device *pdev = to_platform_device(apb->dev);
240 	int ret;
241 
242 	apb->irq = platform_get_irq(pdev, 0);
243 	if (apb->irq < 0)
244 		return apb->irq;
245 
246 	ret = devm_request_irq(apb->dev, apb->irq, bt1_apb_isr, IRQF_SHARED,
247 			       "bt1-apb", apb);
248 	if (ret) {
249 		dev_err(apb->dev, "Couldn't request APB EHB IRQ\n");
250 		return ret;
251 	}
252 
253 	ret = devm_add_action(apb->dev, bt1_apb_clear_irq, apb);
254 	if (ret) {
255 		dev_err(apb->dev, "Can't add APB EHB IRQs clear action\n");
256 		return ret;
257 	}
258 
259 	/* Unmask IRQ and clear it' pending flag. */
260 	regmap_update_bits(apb->regs, APB_EHB_ISR,
261 			   APB_EHB_ISR_PENDING | APB_EHB_ISR_MASK,
262 			   APB_EHB_ISR_MASK);
263 
264 	return 0;
265 }
266 
267 static ssize_t count_show(struct device *dev, struct device_attribute *attr,
268 			  char *buf)
269 {
270 	struct bt1_apb *apb = dev_get_drvdata(dev);
271 
272 	return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&apb->count));
273 }
274 static DEVICE_ATTR_RO(count);
275 
276 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
277 			    char *buf)
278 {
279 	struct bt1_apb *apb = dev_get_drvdata(dev);
280 	unsigned long timeout;
281 	int ret;
282 	u32 n;
283 
284 	ret = regmap_read(apb->regs, APB_EHB_TIMEOUT, &n);
285 	if (ret)
286 		return ret;
287 
288 	timeout = bt1_apb_n_to_timeout_us(apb, n);
289 
290 	return scnprintf(buf, PAGE_SIZE, "%lu\n", timeout);
291 }
292 
293 static ssize_t timeout_store(struct device *dev,
294 			     struct device_attribute *attr,
295 			     const char *buf, size_t count)
296 {
297 	struct bt1_apb *apb = dev_get_drvdata(dev);
298 	unsigned long timeout;
299 	int ret;
300 	u32 n;
301 
302 	if (kstrtoul(buf, 0, &timeout) < 0)
303 		return -EINVAL;
304 
305 	n = bt1_apb_timeout_to_n_us(apb, timeout);
306 	n = clamp(n, APB_EHB_TIMEOUT_MIN, APB_EHB_TIMEOUT_MAX);
307 
308 	ret = regmap_write(apb->regs, APB_EHB_TIMEOUT, n);
309 
310 	return ret ?: count;
311 }
312 static DEVICE_ATTR_RW(timeout);
313 
314 static ssize_t inject_error_show(struct device *dev,
315 				 struct device_attribute *attr, char *buf)
316 {
317 	return scnprintf(buf, PAGE_SIZE, "Error injection: nodev irq\n");
318 }
319 
320 static ssize_t inject_error_store(struct device *dev,
321 				  struct device_attribute *attr,
322 				  const char *data, size_t count)
323 {
324 	struct bt1_apb *apb = dev_get_drvdata(dev);
325 
326 	/*
327 	 * Either dummy read from the unmapped address in the APB IO area
328 	 * or manually set the IRQ status.
329 	 */
330 	if (!strncmp(data, "nodev", 5))
331 		readl(apb->res);
332 	else if (!strncmp(data, "irq", 3))
333 		regmap_update_bits(apb->regs, APB_EHB_ISR, APB_EHB_ISR_PENDING,
334 				   APB_EHB_ISR_PENDING);
335 	else
336 		return -EINVAL;
337 
338 	return count;
339 }
340 static DEVICE_ATTR_RW(inject_error);
341 
342 static struct attribute *bt1_apb_sysfs_attrs[] = {
343 	&dev_attr_count.attr,
344 	&dev_attr_timeout.attr,
345 	&dev_attr_inject_error.attr,
346 	NULL
347 };
348 ATTRIBUTE_GROUPS(bt1_apb_sysfs);
349 
350 static void bt1_apb_remove_sysfs(void *data)
351 {
352 	struct bt1_apb *apb = data;
353 
354 	device_remove_groups(apb->dev, bt1_apb_sysfs_groups);
355 }
356 
357 static int bt1_apb_init_sysfs(struct bt1_apb *apb)
358 {
359 	int ret;
360 
361 	ret = device_add_groups(apb->dev, bt1_apb_sysfs_groups);
362 	if (ret) {
363 		dev_err(apb->dev, "Failed to create EHB APB sysfs nodes\n");
364 		return ret;
365 	}
366 
367 	ret = devm_add_action_or_reset(apb->dev, bt1_apb_remove_sysfs, apb);
368 	if (ret)
369 		dev_err(apb->dev, "Can't add APB EHB sysfs remove action\n");
370 
371 	return ret;
372 }
373 
374 static int bt1_apb_probe(struct platform_device *pdev)
375 {
376 	struct bt1_apb *apb;
377 	int ret;
378 
379 	apb = bt1_apb_create_data(pdev);
380 	if (IS_ERR(apb))
381 		return PTR_ERR(apb);
382 
383 	ret = bt1_apb_request_regs(apb);
384 	if (ret)
385 		return ret;
386 
387 	ret = bt1_apb_request_rst(apb);
388 	if (ret)
389 		return ret;
390 
391 	ret = bt1_apb_request_clk(apb);
392 	if (ret)
393 		return ret;
394 
395 	ret = bt1_apb_request_irq(apb);
396 	if (ret)
397 		return ret;
398 
399 	ret = bt1_apb_init_sysfs(apb);
400 	if (ret)
401 		return ret;
402 
403 	return 0;
404 }
405 
406 static const struct of_device_id bt1_apb_of_match[] = {
407 	{ .compatible = "baikal,bt1-apb" },
408 	{ }
409 };
410 MODULE_DEVICE_TABLE(of, bt1_apb_of_match);
411 
412 static struct platform_driver bt1_apb_driver = {
413 	.probe = bt1_apb_probe,
414 	.driver = {
415 		.name = "bt1-apb",
416 		.of_match_table = bt1_apb_of_match
417 	}
418 };
419 module_platform_driver(bt1_apb_driver);
420 
421 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
422 MODULE_DESCRIPTION("Baikal-T1 APB-bus driver");
423 MODULE_LICENSE("GPL v2");
424