Blame view

drivers/watchdog/ath79_wdt.c 7.19 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
f8394f61c   Gabor Juhos   watchdog: add dri...
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
   *
   * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
   * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
   *
   * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
   *	Author: Deepak Saxena <dsaxena@plexity.net>
   *	Copyright 2004 (c) MontaVista, Software, Inc.
   *
   * which again was based on sa1100 driver,
   *	Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
f8394f61c   Gabor Juhos   watchdog: add dri...
14
   */
27c766aaa   Joe Perches   watchdog: Use pr_...
15
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
f8394f61c   Gabor Juhos   watchdog: add dri...
16
  #include <linux/bitops.h>
23afeb613   Gabor Juhos   watchdog: ath79_w...
17
  #include <linux/delay.h>
f8394f61c   Gabor Juhos   watchdog: add dri...
18
19
  #include <linux/errno.h>
  #include <linux/fs.h>
09f5100a5   Gabor Juhos   watchdog: ath79_w...
20
  #include <linux/io.h>
f8394f61c   Gabor Juhos   watchdog: add dri...
21
22
23
24
25
26
27
28
29
  #include <linux/kernel.h>
  #include <linux/miscdevice.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/platform_device.h>
  #include <linux/types.h>
  #include <linux/watchdog.h>
  #include <linux/clk.h>
  #include <linux/err.h>
15920d129   Gabor Juhos   watchdog: ath79_w...
30
31
  #include <linux/of.h>
  #include <linux/of_platform.h>
29abfbd9c   Al Viro   mips: separate ex...
32
  #include <linux/uaccess.h>
f8394f61c   Gabor Juhos   watchdog: add dri...
33

f8394f61c   Gabor Juhos   watchdog: add dri...
34
35
36
  #define DRIVER_NAME	"ath79-wdt"
  
  #define WDT_TIMEOUT	15	/* seconds */
09f5100a5   Gabor Juhos   watchdog: ath79_w...
37
38
  #define WDOG_REG_CTRL		0x00
  #define WDOG_REG_TIMER		0x04
f8394f61c   Gabor Juhos   watchdog: add dri...
39
40
41
42
43
44
  #define WDOG_CTRL_LAST_RESET	BIT(31)
  #define WDOG_CTRL_ACTION_MASK	3
  #define WDOG_CTRL_ACTION_NONE	0	/* no action */
  #define WDOG_CTRL_ACTION_GPI	1	/* general purpose interrupt */
  #define WDOG_CTRL_ACTION_NMI	2	/* NMI */
  #define WDOG_CTRL_ACTION_FCR	3	/* full chip reset */
86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
45
46
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
f8394f61c   Gabor Juhos   watchdog: add dri...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  			   "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  
  static int timeout = WDT_TIMEOUT;
  module_param(timeout, int, 0);
  MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
  			  "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
  
  static unsigned long wdt_flags;
  
  #define WDT_FLAGS_BUSY		0
  #define WDT_FLAGS_EXPECT_CLOSE	1
  
  static struct clk *wdt_clk;
  static unsigned long wdt_freq;
  static int boot_status;
  static int max_timeout;
09f5100a5   Gabor Juhos   watchdog: ath79_w...
64
65
66
67
68
69
70
71
72
73
74
  static void __iomem *wdt_base;
  
  static inline void ath79_wdt_wr(unsigned reg, u32 val)
  {
  	iowrite32(val, wdt_base + reg);
  }
  
  static inline u32 ath79_wdt_rr(unsigned reg)
  {
  	return ioread32(wdt_base + reg);
  }
f8394f61c   Gabor Juhos   watchdog: add dri...
75
76
77
  
  static inline void ath79_wdt_keepalive(void)
  {
09f5100a5   Gabor Juhos   watchdog: ath79_w...
78
  	ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout);
86955e2bc   Gabor Juhos   watchdog: ath79_w...
79
  	/* flush write */
09f5100a5   Gabor Juhos   watchdog: ath79_w...
80
  	ath79_wdt_rr(WDOG_REG_TIMER);
f8394f61c   Gabor Juhos   watchdog: add dri...
81
82
83
84
85
  }
  
  static inline void ath79_wdt_enable(void)
  {
  	ath79_wdt_keepalive();
23afeb613   Gabor Juhos   watchdog: ath79_w...
86
87
88
89
90
91
92
93
  
  	/*
  	 * Updating the TIMER register requires a few microseconds
  	 * on the AR934x SoCs at least. Use a small delay to ensure
  	 * that the TIMER register is updated within the hardware
  	 * before enabling the watchdog.
  	 */
  	udelay(2);
09f5100a5   Gabor Juhos   watchdog: ath79_w...
94
  	ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR);
86955e2bc   Gabor Juhos   watchdog: ath79_w...
95
  	/* flush write */
09f5100a5   Gabor Juhos   watchdog: ath79_w...
96
  	ath79_wdt_rr(WDOG_REG_CTRL);
f8394f61c   Gabor Juhos   watchdog: add dri...
97
98
99
100
  }
  
  static inline void ath79_wdt_disable(void)
  {
09f5100a5   Gabor Juhos   watchdog: ath79_w...
101
  	ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE);
86955e2bc   Gabor Juhos   watchdog: ath79_w...
102
  	/* flush write */
09f5100a5   Gabor Juhos   watchdog: ath79_w...
103
  	ath79_wdt_rr(WDOG_REG_CTRL);
f8394f61c   Gabor Juhos   watchdog: add dri...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  }
  
  static int ath79_wdt_set_timeout(int val)
  {
  	if (val < 1 || val > max_timeout)
  		return -EINVAL;
  
  	timeout = val;
  	ath79_wdt_keepalive();
  
  	return 0;
  }
  
  static int ath79_wdt_open(struct inode *inode, struct file *file)
  {
  	if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
  		return -EBUSY;
  
  	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
  	ath79_wdt_enable();
c5bf68fe0   Kirill Smelkov   *: convert stream...
124
  	return stream_open(inode, file);
f8394f61c   Gabor Juhos   watchdog: add dri...
125
126
127
128
129
130
131
  }
  
  static int ath79_wdt_release(struct inode *inode, struct file *file)
  {
  	if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
  		ath79_wdt_disable();
  	else {
27c766aaa   Joe Perches   watchdog: Use pr_...
132
133
  		pr_crit("device closed unexpectedly, watchdog timer will not stop!
  ");
f8394f61c   Gabor Juhos   watchdog: add dri...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  		ath79_wdt_keepalive();
  	}
  
  	clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
  	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
  
  	return 0;
  }
  
  static ssize_t ath79_wdt_write(struct file *file, const char *data,
  				size_t len, loff_t *ppos)
  {
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
  
  			for (i = 0; i != len; i++) {
  				char c;
  
  				if (get_user(c, data + i))
  					return -EFAULT;
  
  				if (c == 'V')
  					set_bit(WDT_FLAGS_EXPECT_CLOSE,
  						&wdt_flags);
  			}
  		}
  
  		ath79_wdt_keepalive();
  	}
  
  	return len;
  }
  
  static const struct watchdog_info ath79_wdt_info = {
  	.options		= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  				  WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
  	.firmware_version	= 0,
  	.identity		= "ATH79 watchdog",
  };
  
  static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
  			    unsigned long arg)
  {
  	void __user *argp = (void __user *)arg;
  	int __user *p = argp;
  	int err;
  	int t;
  
  	switch (cmd) {
  	case WDIOC_GETSUPPORT:
  		err = copy_to_user(argp, &ath79_wdt_info,
  				   sizeof(ath79_wdt_info)) ? -EFAULT : 0;
  		break;
  
  	case WDIOC_GETSTATUS:
  		err = put_user(0, p);
  		break;
  
  	case WDIOC_GETBOOTSTATUS:
  		err = put_user(boot_status, p);
  		break;
  
  	case WDIOC_KEEPALIVE:
  		ath79_wdt_keepalive();
  		err = 0;
  		break;
  
  	case WDIOC_SETTIMEOUT:
  		err = get_user(t, p);
  		if (err)
  			break;
  
  		err = ath79_wdt_set_timeout(t);
  		if (err)
  			break;
  
  		/* fallthrough */
  	case WDIOC_GETTIMEOUT:
  		err = put_user(timeout, p);
  		break;
  
  	default:
  		err = -ENOTTY;
  		break;
  	}
  
  	return err;
  }
  
  static const struct file_operations ath79_wdt_fops = {
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.write		= ath79_wdt_write,
  	.unlocked_ioctl	= ath79_wdt_ioctl,
  	.open		= ath79_wdt_open,
  	.release	= ath79_wdt_release,
  };
  
  static struct miscdevice ath79_wdt_miscdev = {
  	.minor = WATCHDOG_MINOR,
  	.name = "watchdog",
  	.fops = &ath79_wdt_fops,
  };
2d991a164   Bill Pemberton   watchdog: remove ...
240
  static int ath79_wdt_probe(struct platform_device *pdev)
f8394f61c   Gabor Juhos   watchdog: add dri...
241
242
243
  {
  	u32 ctrl;
  	int err;
09f5100a5   Gabor Juhos   watchdog: ath79_w...
244
245
  	if (wdt_base)
  		return -EBUSY;
0f0a6a285   Guenter Roeck   watchdog: Convert...
246
  	wdt_base = devm_platform_ioremap_resource(pdev, 0);
6330c7070   Sachin Kamat   watchdog: Convert...
247
248
  	if (IS_ERR(wdt_base))
  		return PTR_ERR(wdt_base);
09f5100a5   Gabor Juhos   watchdog: ath79_w...
249

5071a8847   Gabor Juhos   watchdog: ath79_w...
250
  	wdt_clk = devm_clk_get(&pdev->dev, "wdt");
f8394f61c   Gabor Juhos   watchdog: add dri...
251
252
  	if (IS_ERR(wdt_clk))
  		return PTR_ERR(wdt_clk);
b4e62d946   Gabor Juhos   watchdog: ath79_w...
253
  	err = clk_prepare_enable(wdt_clk);
f8394f61c   Gabor Juhos   watchdog: add dri...
254
  	if (err)
5071a8847   Gabor Juhos   watchdog: ath79_w...
255
  		return err;
f8394f61c   Gabor Juhos   watchdog: add dri...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  
  	wdt_freq = clk_get_rate(wdt_clk);
  	if (!wdt_freq) {
  		err = -EINVAL;
  		goto err_clk_disable;
  	}
  
  	max_timeout = (0xfffffffful / wdt_freq);
  	if (timeout < 1 || timeout > max_timeout) {
  		timeout = max_timeout;
  		dev_info(&pdev->dev,
  			"timeout value must be 0 < timeout < %d, using %d
  ",
  			max_timeout, timeout);
  	}
09f5100a5   Gabor Juhos   watchdog: ath79_w...
271
  	ctrl = ath79_wdt_rr(WDOG_REG_CTRL);
f8394f61c   Gabor Juhos   watchdog: add dri...
272
273
274
275
276
277
278
279
280
281
282
283
284
  	boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
  
  	err = misc_register(&ath79_wdt_miscdev);
  	if (err) {
  		dev_err(&pdev->dev,
  			"unable to register misc device, err=%d
  ", err);
  		goto err_clk_disable;
  	}
  
  	return 0;
  
  err_clk_disable:
b4e62d946   Gabor Juhos   watchdog: ath79_w...
285
  	clk_disable_unprepare(wdt_clk);
f8394f61c   Gabor Juhos   watchdog: add dri...
286
287
  	return err;
  }
4b12b896c   Bill Pemberton   watchdog: remove ...
288
  static int ath79_wdt_remove(struct platform_device *pdev)
f8394f61c   Gabor Juhos   watchdog: add dri...
289
290
  {
  	misc_deregister(&ath79_wdt_miscdev);
b4e62d946   Gabor Juhos   watchdog: ath79_w...
291
  	clk_disable_unprepare(wdt_clk);
f8394f61c   Gabor Juhos   watchdog: add dri...
292
293
  	return 0;
  }
a18670f46   Christophe JAILLET   watchdog: ath79_w...
294
  static void ath79_wdt_shutdown(struct platform_device *pdev)
f8394f61c   Gabor Juhos   watchdog: add dri...
295
296
297
  {
  	ath79_wdt_disable();
  }
15920d129   Gabor Juhos   watchdog: ath79_w...
298
299
300
301
302
303
304
  #ifdef CONFIG_OF
  static const struct of_device_id ath79_wdt_match[] = {
  	{ .compatible = "qca,ar7130-wdt" },
  	{},
  };
  MODULE_DEVICE_TABLE(of, ath79_wdt_match);
  #endif
f8394f61c   Gabor Juhos   watchdog: add dri...
305
  static struct platform_driver ath79_wdt_driver = {
8157becf8   Gabor Juhos   watchdog: ath79_w...
306
  	.probe		= ath79_wdt_probe,
82268714b   Bill Pemberton   watchdog: remove ...
307
  	.remove		= ath79_wdt_remove,
a18670f46   Christophe JAILLET   watchdog: ath79_w...
308
  	.shutdown	= ath79_wdt_shutdown,
f8394f61c   Gabor Juhos   watchdog: add dri...
309
310
  	.driver		= {
  		.name	= DRIVER_NAME,
15920d129   Gabor Juhos   watchdog: ath79_w...
311
  		.of_match_table = of_match_ptr(ath79_wdt_match),
f8394f61c   Gabor Juhos   watchdog: add dri...
312
313
  	},
  };
8157becf8   Gabor Juhos   watchdog: ath79_w...
314
  module_platform_driver(ath79_wdt_driver);
f8394f61c   Gabor Juhos   watchdog: add dri...
315
316
317
318
319
320
  
  MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
  MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
  MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
  MODULE_LICENSE("GPL v2");
  MODULE_ALIAS("platform:" DRIVER_NAME);