1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
8 * Baikal-T1 APB-bus driver
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>
19 #include <linux/nmi.h>
21 #include <linux/regmap.h>
22 #include <linux/clk.h>
23 #include <linux/reset.h>
24 #include <linux/time64.h>
25 #include <linux/sysfs.h>
27 #define APB_EHB_ISR 0x00
28 #define APB_EHB_ISR_PENDING BIT(0)
29 #define APB_EHB_ISR_MASK BIT(1)
30 #define APB_EHB_ADDR 0x04
31 #define APB_EHB_TIMEOUT 0x08
33 #define APB_EHB_TIMEOUT_MIN 0x000003FFU
34 #define APB_EHB_TIMEOUT_MAX 0xFFFFFFFFU
37 * struct bt1_apb - Baikal-T1 APB EHB private data
38 * @dev: Pointer to the device structure.
39 * @regs: APB EHB registers map.
40 * @res: No-device error injection memory region.
41 * @irq: Errors IRQ number.
42 * @rate: APB-bus reference clock rate.
43 * @pclk: APB-reference clock.
44 * @prst: APB domain reset line.
45 * @count: Number of errors detected.
57 struct reset_control
*prst
;
62 static const struct regmap_config bt1_apb_regmap_cfg
= {
66 .max_register
= APB_EHB_TIMEOUT
,
70 static inline unsigned long bt1_apb_n_to_timeout_us(struct bt1_apb
*apb
, u32 n
)
72 u64 timeout
= (u64
)n
* USEC_PER_SEC
;
74 do_div(timeout
, apb
->rate
);
80 static inline unsigned long bt1_apb_timeout_to_n_us(struct bt1_apb
*apb
,
81 unsigned long timeout
)
83 u64 n
= (u64
)timeout
* apb
->rate
;
85 do_div(n
, USEC_PER_SEC
);
91 static irqreturn_t
bt1_apb_isr(int irq
, void *data
)
93 struct bt1_apb
*apb
= data
;
96 regmap_read(apb
->regs
, APB_EHB_ADDR
, &addr
);
98 dev_crit_ratelimited(apb
->dev
,
99 "APB-bus fault %d: Slave access timeout at 0x%08x\n",
100 atomic_inc_return(&apb
->count
),
104 * Print backtrace on each CPU. This might be pointless if the fault
105 * has happened on the same CPU as the IRQ handler is executed or
106 * the other core proceeded further execution despite the error.
107 * But if it's not, by looking at the trace we would get straight to
108 * the cause of the problem.
110 trigger_all_cpu_backtrace();
112 regmap_update_bits(apb
->regs
, APB_EHB_ISR
, APB_EHB_ISR_PENDING
, 0);
117 static void bt1_apb_clear_data(void *data
)
119 struct bt1_apb
*apb
= data
;
120 struct platform_device
*pdev
= to_platform_device(apb
->dev
);
122 platform_set_drvdata(pdev
, NULL
);
125 static struct bt1_apb
*bt1_apb_create_data(struct platform_device
*pdev
)
127 struct device
*dev
= &pdev
->dev
;
131 apb
= devm_kzalloc(dev
, sizeof(*apb
), GFP_KERNEL
);
133 return ERR_PTR(-ENOMEM
);
135 ret
= devm_add_action(dev
, bt1_apb_clear_data
, apb
);
137 dev_err(dev
, "Can't add APB EHB data clear action\n");
142 atomic_set(&apb
->count
, 0);
143 platform_set_drvdata(pdev
, apb
);
148 static int bt1_apb_request_regs(struct bt1_apb
*apb
)
150 struct platform_device
*pdev
= to_platform_device(apb
->dev
);
153 regs
= devm_platform_ioremap_resource_byname(pdev
, "ehb");
155 dev_err(apb
->dev
, "Couldn't map APB EHB registers\n");
156 return PTR_ERR(regs
);
159 apb
->regs
= devm_regmap_init_mmio(apb
->dev
, regs
, &bt1_apb_regmap_cfg
);
160 if (IS_ERR(apb
->regs
)) {
161 dev_err(apb
->dev
, "Couldn't create APB EHB regmap\n");
162 return PTR_ERR(apb
->regs
);
165 apb
->res
= devm_platform_ioremap_resource_byname(pdev
, "nodev");
166 if (IS_ERR(apb
->res
))
167 dev_err(apb
->dev
, "Couldn't map reserved region\n");
169 return PTR_ERR_OR_ZERO(apb
->res
);
172 static int bt1_apb_request_rst(struct bt1_apb
*apb
)
176 apb
->prst
= devm_reset_control_get_optional_exclusive(apb
->dev
, "prst");
177 if (IS_ERR(apb
->prst
))
178 return dev_err_probe(apb
->dev
, PTR_ERR(apb
->prst
),
179 "Couldn't get reset control line\n");
181 ret
= reset_control_deassert(apb
->prst
);
183 dev_err(apb
->dev
, "Failed to deassert the reset line\n");
188 static int bt1_apb_request_clk(struct bt1_apb
*apb
)
190 apb
->pclk
= devm_clk_get_enabled(apb
->dev
, "pclk");
191 if (IS_ERR(apb
->pclk
))
192 return dev_err_probe(apb
->dev
, PTR_ERR(apb
->pclk
),
193 "Couldn't get APB clock descriptor\n");
195 apb
->rate
= clk_get_rate(apb
->pclk
);
197 dev_err(apb
->dev
, "Invalid clock rate\n");
204 static void bt1_apb_clear_irq(void *data
)
206 struct bt1_apb
*apb
= data
;
208 regmap_update_bits(apb
->regs
, APB_EHB_ISR
, APB_EHB_ISR_MASK
, 0);
211 static int bt1_apb_request_irq(struct bt1_apb
*apb
)
213 struct platform_device
*pdev
= to_platform_device(apb
->dev
);
216 apb
->irq
= platform_get_irq(pdev
, 0);
220 ret
= devm_request_irq(apb
->dev
, apb
->irq
, bt1_apb_isr
, IRQF_SHARED
,
223 dev_err(apb
->dev
, "Couldn't request APB EHB IRQ\n");
227 ret
= devm_add_action(apb
->dev
, bt1_apb_clear_irq
, apb
);
229 dev_err(apb
->dev
, "Can't add APB EHB IRQs clear action\n");
233 /* Unmask IRQ and clear it' pending flag. */
234 regmap_update_bits(apb
->regs
, APB_EHB_ISR
,
235 APB_EHB_ISR_PENDING
| APB_EHB_ISR_MASK
,
241 static ssize_t
count_show(struct device
*dev
, struct device_attribute
*attr
,
244 struct bt1_apb
*apb
= dev_get_drvdata(dev
);
246 return scnprintf(buf
, PAGE_SIZE
, "%d\n", atomic_read(&apb
->count
));
248 static DEVICE_ATTR_RO(count
);
250 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
253 struct bt1_apb
*apb
= dev_get_drvdata(dev
);
254 unsigned long timeout
;
258 ret
= regmap_read(apb
->regs
, APB_EHB_TIMEOUT
, &n
);
262 timeout
= bt1_apb_n_to_timeout_us(apb
, n
);
264 return scnprintf(buf
, PAGE_SIZE
, "%lu\n", timeout
);
267 static ssize_t
timeout_store(struct device
*dev
,
268 struct device_attribute
*attr
,
269 const char *buf
, size_t count
)
271 struct bt1_apb
*apb
= dev_get_drvdata(dev
);
272 unsigned long timeout
;
276 if (kstrtoul(buf
, 0, &timeout
) < 0)
279 n
= bt1_apb_timeout_to_n_us(apb
, timeout
);
280 n
= clamp(n
, APB_EHB_TIMEOUT_MIN
, APB_EHB_TIMEOUT_MAX
);
282 ret
= regmap_write(apb
->regs
, APB_EHB_TIMEOUT
, n
);
286 static DEVICE_ATTR_RW(timeout
);
288 static ssize_t
inject_error_show(struct device
*dev
,
289 struct device_attribute
*attr
, char *buf
)
291 return scnprintf(buf
, PAGE_SIZE
, "Error injection: nodev irq\n");
294 static ssize_t
inject_error_store(struct device
*dev
,
295 struct device_attribute
*attr
,
296 const char *data
, size_t count
)
298 struct bt1_apb
*apb
= dev_get_drvdata(dev
);
301 * Either dummy read from the unmapped address in the APB IO area
302 * or manually set the IRQ status.
304 if (sysfs_streq(data
, "nodev"))
306 else if (sysfs_streq(data
, "irq"))
307 regmap_update_bits(apb
->regs
, APB_EHB_ISR
, APB_EHB_ISR_PENDING
,
308 APB_EHB_ISR_PENDING
);
314 static DEVICE_ATTR_RW(inject_error
);
316 static struct attribute
*bt1_apb_sysfs_attrs
[] = {
317 &dev_attr_count
.attr
,
318 &dev_attr_timeout
.attr
,
319 &dev_attr_inject_error
.attr
,
322 ATTRIBUTE_GROUPS(bt1_apb_sysfs
);
324 static void bt1_apb_remove_sysfs(void *data
)
326 struct bt1_apb
*apb
= data
;
328 device_remove_groups(apb
->dev
, bt1_apb_sysfs_groups
);
331 static int bt1_apb_init_sysfs(struct bt1_apb
*apb
)
335 ret
= device_add_groups(apb
->dev
, bt1_apb_sysfs_groups
);
337 dev_err(apb
->dev
, "Failed to create EHB APB sysfs nodes\n");
341 ret
= devm_add_action_or_reset(apb
->dev
, bt1_apb_remove_sysfs
, apb
);
343 dev_err(apb
->dev
, "Can't add APB EHB sysfs remove action\n");
348 static int bt1_apb_probe(struct platform_device
*pdev
)
353 apb
= bt1_apb_create_data(pdev
);
357 ret
= bt1_apb_request_regs(apb
);
361 ret
= bt1_apb_request_rst(apb
);
365 ret
= bt1_apb_request_clk(apb
);
369 ret
= bt1_apb_request_irq(apb
);
373 ret
= bt1_apb_init_sysfs(apb
);
380 static const struct of_device_id bt1_apb_of_match
[] = {
381 { .compatible
= "baikal,bt1-apb" },
384 MODULE_DEVICE_TABLE(of
, bt1_apb_of_match
);
386 static struct platform_driver bt1_apb_driver
= {
387 .probe
= bt1_apb_probe
,
390 .of_match_table
= bt1_apb_of_match
393 module_platform_driver(bt1_apb_driver
);
395 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
396 MODULE_DESCRIPTION("Baikal-T1 APB-bus driver");