1 // SPDX-License-Identifier: GPL-2.0+
3 * Mellanox watchdog driver
5 * Copyright (C) 2019 Mellanox Technologies
6 * Copyright (C) 2019 Michael Shych <mshych@mellanox.com>
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/platform_data/mlxreg.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/watchdog.h>
21 #define MLXREG_WDT_CLOCK_SCALE 1000
22 #define MLXREG_WDT_MAX_TIMEOUT_TYPE1 32
23 #define MLXREG_WDT_MAX_TIMEOUT_TYPE2 255
24 #define MLXREG_WDT_MAX_TIMEOUT_TYPE3 65535
25 #define MLXREG_WDT_MIN_TIMEOUT 1
26 #define MLXREG_WDT_OPTIONS_BASE (WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | \
30 * struct mlxreg_wdt - wd private data:
32 * @wdd: watchdog device;
33 * @pdata: data received from platform driver;
34 * @regmap: register map of parent device;
35 * @action_idx: index for direct access to action register;
36 * @timeout_idx:index for direct access to TO register;
37 * @tleft_idx: index for direct access to time left register;
38 * @ping_idx: index for direct access to ping register;
39 * @reset_idx: index for direct access to reset cause register;
40 * @regmap_val_sz: size of value in register map;
41 * @wdt_type: watchdog HW type;
44 struct watchdog_device wdd
;
45 struct mlxreg_core_platform_data
*pdata
;
53 enum mlxreg_wdt_type wdt_type
;
56 static void mlxreg_wdt_check_card_reset(struct mlxreg_wdt
*wdt
)
58 struct mlxreg_core_data
*reg_data
;
62 if (wdt
->reset_idx
== -EINVAL
)
65 if (!(wdt
->wdd
.info
->options
& WDIOF_CARDRESET
))
68 reg_data
= &wdt
->pdata
->data
[wdt
->reset_idx
];
69 rc
= regmap_read(wdt
->regmap
, reg_data
->reg
, ®val
);
71 if (regval
& ~reg_data
->mask
) {
72 wdt
->wdd
.bootstatus
= WDIOF_CARDRESET
;
73 dev_info(wdt
->wdd
.parent
,
74 "watchdog previously reset the CPU\n");
79 static int mlxreg_wdt_start(struct watchdog_device
*wdd
)
81 struct mlxreg_wdt
*wdt
= watchdog_get_drvdata(wdd
);
82 struct mlxreg_core_data
*reg_data
= &wdt
->pdata
->data
[wdt
->action_idx
];
84 return regmap_update_bits(wdt
->regmap
, reg_data
->reg
, ~reg_data
->mask
,
88 static int mlxreg_wdt_stop(struct watchdog_device
*wdd
)
90 struct mlxreg_wdt
*wdt
= watchdog_get_drvdata(wdd
);
91 struct mlxreg_core_data
*reg_data
= &wdt
->pdata
->data
[wdt
->action_idx
];
93 return regmap_update_bits(wdt
->regmap
, reg_data
->reg
, ~reg_data
->mask
,
97 static int mlxreg_wdt_ping(struct watchdog_device
*wdd
)
99 struct mlxreg_wdt
*wdt
= watchdog_get_drvdata(wdd
);
100 struct mlxreg_core_data
*reg_data
= &wdt
->pdata
->data
[wdt
->ping_idx
];
102 return regmap_write_bits(wdt
->regmap
, reg_data
->reg
, ~reg_data
->mask
,
106 static int mlxreg_wdt_set_timeout(struct watchdog_device
*wdd
,
107 unsigned int timeout
)
109 struct mlxreg_wdt
*wdt
= watchdog_get_drvdata(wdd
);
110 struct mlxreg_core_data
*reg_data
= &wdt
->pdata
->data
[wdt
->timeout_idx
];
111 u32 regval
, set_time
, hw_timeout
;
114 switch (wdt
->wdt_type
) {
116 rc
= regmap_read(wdt
->regmap
, reg_data
->reg
, ®val
);
120 hw_timeout
= order_base_2(timeout
* MLXREG_WDT_CLOCK_SCALE
);
121 regval
= (regval
& reg_data
->mask
) | hw_timeout
;
122 /* Rowndown to actual closest number of sec. */
123 set_time
= BIT(hw_timeout
) / MLXREG_WDT_CLOCK_SCALE
;
124 rc
= regmap_write(wdt
->regmap
, reg_data
->reg
, regval
);
128 rc
= regmap_write(wdt
->regmap
, reg_data
->reg
, timeout
);
131 /* WD_TYPE3 has 2B set time register */
133 if (wdt
->regmap_val_sz
== 1) {
134 regval
= timeout
& 0xff;
135 rc
= regmap_write(wdt
->regmap
, reg_data
->reg
, regval
);
137 regval
= (timeout
& 0xff00) >> 8;
138 rc
= regmap_write(wdt
->regmap
,
139 reg_data
->reg
+ 1, regval
);
142 rc
= regmap_write(wdt
->regmap
, reg_data
->reg
, timeout
);
149 wdd
->timeout
= set_time
;
152 * Restart watchdog with new timeout period
153 * if watchdog is already started.
155 if (watchdog_active(wdd
)) {
156 rc
= mlxreg_wdt_stop(wdd
);
158 rc
= mlxreg_wdt_start(wdd
);
165 static unsigned int mlxreg_wdt_get_timeleft(struct watchdog_device
*wdd
)
167 struct mlxreg_wdt
*wdt
= watchdog_get_drvdata(wdd
);
168 struct mlxreg_core_data
*reg_data
= &wdt
->pdata
->data
[wdt
->tleft_idx
];
169 u32 regval
, msb
, lsb
;
172 if (wdt
->wdt_type
== MLX_WDT_TYPE2
) {
173 rc
= regmap_read(wdt
->regmap
, reg_data
->reg
, ®val
);
175 /* WD_TYPE3 has 2 byte timeleft register */
176 if (wdt
->regmap_val_sz
== 1) {
177 rc
= regmap_read(wdt
->regmap
, reg_data
->reg
, &lsb
);
179 rc
= regmap_read(wdt
->regmap
,
180 reg_data
->reg
+ 1, &msb
);
181 regval
= (msb
& 0xff) << 8 | (lsb
& 0xff);
184 rc
= regmap_read(wdt
->regmap
, reg_data
->reg
, ®val
);
188 /* Return 0 timeleft in case of failure register read. */
189 return rc
== 0 ? regval
: 0;
192 static const struct watchdog_ops mlxreg_wdt_ops_type1
= {
193 .start
= mlxreg_wdt_start
,
194 .stop
= mlxreg_wdt_stop
,
195 .ping
= mlxreg_wdt_ping
,
196 .set_timeout
= mlxreg_wdt_set_timeout
,
197 .owner
= THIS_MODULE
,
200 static const struct watchdog_ops mlxreg_wdt_ops_type2
= {
201 .start
= mlxreg_wdt_start
,
202 .stop
= mlxreg_wdt_stop
,
203 .ping
= mlxreg_wdt_ping
,
204 .set_timeout
= mlxreg_wdt_set_timeout
,
205 .get_timeleft
= mlxreg_wdt_get_timeleft
,
206 .owner
= THIS_MODULE
,
209 static const struct watchdog_info mlxreg_wdt_main_info
= {
210 .options
= MLXREG_WDT_OPTIONS_BASE
212 .identity
= "mlx-wdt-main",
215 static const struct watchdog_info mlxreg_wdt_aux_info
= {
216 .options
= MLXREG_WDT_OPTIONS_BASE
218 .identity
= "mlx-wdt-aux",
221 static void mlxreg_wdt_config(struct mlxreg_wdt
*wdt
,
222 struct mlxreg_core_platform_data
*pdata
)
224 struct mlxreg_core_data
*data
= pdata
->data
;
227 wdt
->reset_idx
= -EINVAL
;
228 for (i
= 0; i
< pdata
->counter
; i
++, data
++) {
229 if (strnstr(data
->label
, "action", sizeof(data
->label
)))
231 else if (strnstr(data
->label
, "timeout", sizeof(data
->label
)))
232 wdt
->timeout_idx
= i
;
233 else if (strnstr(data
->label
, "timeleft", sizeof(data
->label
)))
235 else if (strnstr(data
->label
, "ping", sizeof(data
->label
)))
237 else if (strnstr(data
->label
, "reset", sizeof(data
->label
)))
242 if (strnstr(pdata
->identity
, mlxreg_wdt_main_info
.identity
,
243 sizeof(mlxreg_wdt_main_info
.identity
)))
244 wdt
->wdd
.info
= &mlxreg_wdt_main_info
;
246 wdt
->wdd
.info
= &mlxreg_wdt_aux_info
;
248 wdt
->wdt_type
= pdata
->version
;
249 switch (wdt
->wdt_type
) {
251 wdt
->wdd
.ops
= &mlxreg_wdt_ops_type1
;
252 wdt
->wdd
.max_timeout
= MLXREG_WDT_MAX_TIMEOUT_TYPE1
;
255 wdt
->wdd
.ops
= &mlxreg_wdt_ops_type2
;
256 wdt
->wdd
.max_timeout
= MLXREG_WDT_MAX_TIMEOUT_TYPE2
;
259 wdt
->wdd
.ops
= &mlxreg_wdt_ops_type2
;
260 wdt
->wdd
.max_timeout
= MLXREG_WDT_MAX_TIMEOUT_TYPE3
;
266 wdt
->wdd
.min_timeout
= MLXREG_WDT_MIN_TIMEOUT
;
269 static int mlxreg_wdt_init_timeout(struct mlxreg_wdt
*wdt
,
270 struct mlxreg_core_platform_data
*pdata
)
274 timeout
= pdata
->data
[wdt
->timeout_idx
].health_cntr
;
275 return mlxreg_wdt_set_timeout(&wdt
->wdd
, timeout
);
278 static int mlxreg_wdt_probe(struct platform_device
*pdev
)
280 struct device
*dev
= &pdev
->dev
;
281 struct mlxreg_core_platform_data
*pdata
;
282 struct mlxreg_wdt
*wdt
;
285 pdata
= dev_get_platdata(dev
);
287 dev_err(dev
, "Failed to get platform data.\n");
290 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
294 wdt
->wdd
.parent
= dev
;
295 wdt
->regmap
= pdata
->regmap
;
296 rc
= regmap_get_val_bytes(wdt
->regmap
);
300 wdt
->regmap_val_sz
= rc
;
301 mlxreg_wdt_config(wdt
, pdata
);
303 if ((pdata
->features
& MLXREG_CORE_WD_FEATURE_NOWAYOUT
))
304 watchdog_set_nowayout(&wdt
->wdd
, WATCHDOG_NOWAYOUT
);
305 watchdog_stop_on_reboot(&wdt
->wdd
);
306 watchdog_stop_on_unregister(&wdt
->wdd
);
307 watchdog_set_drvdata(&wdt
->wdd
, wdt
);
308 rc
= mlxreg_wdt_init_timeout(wdt
, pdata
);
312 if ((pdata
->features
& MLXREG_CORE_WD_FEATURE_START_AT_BOOT
)) {
313 rc
= mlxreg_wdt_start(&wdt
->wdd
);
316 set_bit(WDOG_HW_RUNNING
, &wdt
->wdd
.status
);
318 mlxreg_wdt_check_card_reset(wdt
);
319 rc
= devm_watchdog_register_device(dev
, &wdt
->wdd
);
323 dev_err(dev
, "Cannot register watchdog device (err=%d)\n", rc
);
327 static struct platform_driver mlxreg_wdt_driver
= {
328 .probe
= mlxreg_wdt_probe
,
334 module_platform_driver(mlxreg_wdt_driver
);
336 MODULE_AUTHOR("Michael Shych <michaelsh@mellanox.com>");
337 MODULE_DESCRIPTION("Mellanox watchdog driver");
338 MODULE_LICENSE("GPL");
339 MODULE_ALIAS("platform:mlx-wdt");