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 * @device: basic device;
34 * @pdata: data received from platform driver;
35 * @regmap: register map of parent device;
36 * @timeout: defined timeout in sec.;
37 * @action_idx: index for direct access to action register;
38 * @timeout_idx:index for direct access to TO register;
39 * @tleft_idx: index for direct access to time left register;
40 * @ping_idx: index for direct access to ping register;
41 * @reset_idx: index for direct access to reset cause register;
42 * @wd_type: watchdog HW type;
45 struct watchdog_device wdd;
46 struct mlxreg_core_platform_data *pdata;
54 enum mlxreg_wdt_type wdt_type;
57 static void mlxreg_wdt_check_card_reset(struct mlxreg_wdt *wdt)
59 struct mlxreg_core_data *reg_data;
63 if (wdt->reset_idx == -EINVAL)
66 if (!(wdt->wdd.info->options & WDIOF_CARDRESET))
69 reg_data = &wdt->pdata->data[wdt->reset_idx];
70 rc = regmap_read(wdt->regmap, reg_data->reg, ®val);
72 if (regval & ~reg_data->mask) {
73 wdt->wdd.bootstatus = WDIOF_CARDRESET;
74 dev_info(wdt->wdd.parent,
75 "watchdog previously reset the CPU\n");
80 static int mlxreg_wdt_start(struct watchdog_device *wdd)
82 struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
83 struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->action_idx];
85 return regmap_update_bits(wdt->regmap, reg_data->reg, ~reg_data->mask,
89 static int mlxreg_wdt_stop(struct watchdog_device *wdd)
91 struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
92 struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->action_idx];
94 return regmap_update_bits(wdt->regmap, reg_data->reg, ~reg_data->mask,
98 static int mlxreg_wdt_ping(struct watchdog_device *wdd)
100 struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
101 struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->ping_idx];
103 return regmap_write_bits(wdt->regmap, reg_data->reg, ~reg_data->mask,
107 static int mlxreg_wdt_set_timeout(struct watchdog_device *wdd,
108 unsigned int timeout)
110 struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
111 struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->timeout_idx];
112 u32 regval, set_time, hw_timeout;
115 switch (wdt->wdt_type) {
117 rc = regmap_read(wdt->regmap, reg_data->reg, ®val);
121 hw_timeout = order_base_2(timeout * MLXREG_WDT_CLOCK_SCALE);
122 regval = (regval & reg_data->mask) | hw_timeout;
123 /* Rowndown to actual closest number of sec. */
124 set_time = BIT(hw_timeout) / MLXREG_WDT_CLOCK_SCALE;
125 rc = regmap_write(wdt->regmap, reg_data->reg, regval);
129 rc = regmap_write(wdt->regmap, reg_data->reg, timeout);
132 /* WD_TYPE3 has 2B set time register */
134 if (wdt->regmap_val_sz == 1) {
135 regval = timeout & 0xff;
136 rc = regmap_write(wdt->regmap, reg_data->reg, regval);
138 regval = (timeout & 0xff00) >> 8;
139 rc = regmap_write(wdt->regmap,
140 reg_data->reg + 1, regval);
143 rc = regmap_write(wdt->regmap, reg_data->reg, timeout);
150 wdd->timeout = set_time;
153 * Restart watchdog with new timeout period
154 * if watchdog is already started.
156 if (watchdog_active(wdd)) {
157 rc = mlxreg_wdt_stop(wdd);
159 rc = mlxreg_wdt_start(wdd);
166 static unsigned int mlxreg_wdt_get_timeleft(struct watchdog_device *wdd)
168 struct mlxreg_wdt *wdt = watchdog_get_drvdata(wdd);
169 struct mlxreg_core_data *reg_data = &wdt->pdata->data[wdt->tleft_idx];
170 u32 regval, msb, lsb;
173 if (wdt->wdt_type == MLX_WDT_TYPE2) {
174 rc = regmap_read(wdt->regmap, reg_data->reg, ®val);
176 /* WD_TYPE3 has 2 byte timeleft register */
177 if (wdt->regmap_val_sz == 1) {
178 rc = regmap_read(wdt->regmap, reg_data->reg, &lsb);
180 rc = regmap_read(wdt->regmap,
181 reg_data->reg + 1, &msb);
182 regval = (msb & 0xff) << 8 | (lsb & 0xff);
185 rc = regmap_read(wdt->regmap, reg_data->reg, ®val);
189 /* Return 0 timeleft in case of failure register read. */
190 return rc == 0 ? regval : 0;
193 static const struct watchdog_ops mlxreg_wdt_ops_type1 = {
194 .start = mlxreg_wdt_start,
195 .stop = mlxreg_wdt_stop,
196 .ping = mlxreg_wdt_ping,
197 .set_timeout = mlxreg_wdt_set_timeout,
198 .owner = THIS_MODULE,
201 static const struct watchdog_ops mlxreg_wdt_ops_type2 = {
202 .start = mlxreg_wdt_start,
203 .stop = mlxreg_wdt_stop,
204 .ping = mlxreg_wdt_ping,
205 .set_timeout = mlxreg_wdt_set_timeout,
206 .get_timeleft = mlxreg_wdt_get_timeleft,
207 .owner = THIS_MODULE,
210 static const struct watchdog_info mlxreg_wdt_main_info = {
211 .options = MLXREG_WDT_OPTIONS_BASE
213 .identity = "mlx-wdt-main",
216 static const struct watchdog_info mlxreg_wdt_aux_info = {
217 .options = MLXREG_WDT_OPTIONS_BASE
219 .identity = "mlx-wdt-aux",
222 static void mlxreg_wdt_config(struct mlxreg_wdt *wdt,
223 struct mlxreg_core_platform_data *pdata)
225 struct mlxreg_core_data *data = pdata->data;
228 wdt->reset_idx = -EINVAL;
229 for (i = 0; i < pdata->counter; i++, data++) {
230 if (strnstr(data->label, "action", sizeof(data->label)))
232 else if (strnstr(data->label, "timeout", sizeof(data->label)))
233 wdt->timeout_idx = i;
234 else if (strnstr(data->label, "timeleft", sizeof(data->label)))
236 else if (strnstr(data->label, "ping", sizeof(data->label)))
238 else if (strnstr(data->label, "reset", sizeof(data->label)))
243 if (strnstr(pdata->identity, mlxreg_wdt_main_info.identity,
244 sizeof(mlxreg_wdt_main_info.identity)))
245 wdt->wdd.info = &mlxreg_wdt_main_info;
247 wdt->wdd.info = &mlxreg_wdt_aux_info;
249 wdt->wdt_type = pdata->version;
250 switch (wdt->wdt_type) {
252 wdt->wdd.ops = &mlxreg_wdt_ops_type1;
253 wdt->wdd.max_timeout = MLXREG_WDT_MAX_TIMEOUT_TYPE1;
256 wdt->wdd.ops = &mlxreg_wdt_ops_type2;
257 wdt->wdd.max_timeout = MLXREG_WDT_MAX_TIMEOUT_TYPE2;
260 wdt->wdd.ops = &mlxreg_wdt_ops_type2;
261 wdt->wdd.max_timeout = MLXREG_WDT_MAX_TIMEOUT_TYPE3;
267 wdt->wdd.min_timeout = MLXREG_WDT_MIN_TIMEOUT;
270 static int mlxreg_wdt_init_timeout(struct mlxreg_wdt *wdt,
271 struct mlxreg_core_platform_data *pdata)
275 timeout = pdata->data[wdt->timeout_idx].health_cntr;
276 return mlxreg_wdt_set_timeout(&wdt->wdd, timeout);
279 static int mlxreg_wdt_probe(struct platform_device *pdev)
281 struct device *dev = &pdev->dev;
282 struct mlxreg_core_platform_data *pdata;
283 struct mlxreg_wdt *wdt;
286 pdata = dev_get_platdata(dev);
288 dev_err(dev, "Failed to get platform data.\n");
291 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
295 wdt->wdd.parent = dev;
296 wdt->regmap = pdata->regmap;
297 rc = regmap_get_val_bytes(wdt->regmap);
301 wdt->regmap_val_sz = rc;
302 mlxreg_wdt_config(wdt, pdata);
304 if ((pdata->features & MLXREG_CORE_WD_FEATURE_NOWAYOUT))
305 watchdog_set_nowayout(&wdt->wdd, WATCHDOG_NOWAYOUT);
306 watchdog_stop_on_reboot(&wdt->wdd);
307 watchdog_stop_on_unregister(&wdt->wdd);
308 watchdog_set_drvdata(&wdt->wdd, wdt);
309 rc = mlxreg_wdt_init_timeout(wdt, pdata);
313 if ((pdata->features & MLXREG_CORE_WD_FEATURE_START_AT_BOOT)) {
314 rc = mlxreg_wdt_start(&wdt->wdd);
317 set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
319 mlxreg_wdt_check_card_reset(wdt);
320 rc = devm_watchdog_register_device(dev, &wdt->wdd);
324 dev_err(dev, "Cannot register watchdog device (err=%d)\n", rc);
328 static struct platform_driver mlxreg_wdt_driver = {
329 .probe = mlxreg_wdt_probe,
335 module_platform_driver(mlxreg_wdt_driver);
337 MODULE_AUTHOR("Michael Shych <michaelsh@mellanox.com>");
338 MODULE_DESCRIPTION("Mellanox watchdog driver");
339 MODULE_LICENSE("GPL");
340 MODULE_ALIAS("platform:mlx-wdt");