xref: /OK3568_Linux_fs/kernel/drivers/watchdog/ath79_wdt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
6*4882a593Smuzhiyun  * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
9*4882a593Smuzhiyun  *	Author: Deepak Saxena <dsaxena@plexity.net>
10*4882a593Smuzhiyun  *	Copyright 2004 (c) MontaVista, Software, Inc.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * which again was based on sa1100 driver,
13*4882a593Smuzhiyun  *	Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/bitops.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun #include <linux/errno.h>
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/io.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/miscdevice.h>
25*4882a593Smuzhiyun #include <linux/module.h>
26*4882a593Smuzhiyun #include <linux/moduleparam.h>
27*4882a593Smuzhiyun #include <linux/platform_device.h>
28*4882a593Smuzhiyun #include <linux/types.h>
29*4882a593Smuzhiyun #include <linux/watchdog.h>
30*4882a593Smuzhiyun #include <linux/clk.h>
31*4882a593Smuzhiyun #include <linux/err.h>
32*4882a593Smuzhiyun #include <linux/of.h>
33*4882a593Smuzhiyun #include <linux/of_platform.h>
34*4882a593Smuzhiyun #include <linux/uaccess.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define DRIVER_NAME	"ath79-wdt"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define WDT_TIMEOUT	15	/* seconds */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define WDOG_REG_CTRL		0x00
41*4882a593Smuzhiyun #define WDOG_REG_TIMER		0x04
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define WDOG_CTRL_LAST_RESET	BIT(31)
44*4882a593Smuzhiyun #define WDOG_CTRL_ACTION_MASK	3
45*4882a593Smuzhiyun #define WDOG_CTRL_ACTION_NONE	0	/* no action */
46*4882a593Smuzhiyun #define WDOG_CTRL_ACTION_GPI	1	/* general purpose interrupt */
47*4882a593Smuzhiyun #define WDOG_CTRL_ACTION_NMI	2	/* NMI */
48*4882a593Smuzhiyun #define WDOG_CTRL_ACTION_FCR	3	/* full chip reset */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
51*4882a593Smuzhiyun module_param(nowayout, bool, 0);
52*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
53*4882a593Smuzhiyun 			   "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static int timeout = WDT_TIMEOUT;
56*4882a593Smuzhiyun module_param(timeout, int, 0);
57*4882a593Smuzhiyun MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
58*4882a593Smuzhiyun 			  "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)");
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun static unsigned long wdt_flags;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define WDT_FLAGS_BUSY		0
63*4882a593Smuzhiyun #define WDT_FLAGS_EXPECT_CLOSE	1
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static struct clk *wdt_clk;
66*4882a593Smuzhiyun static unsigned long wdt_freq;
67*4882a593Smuzhiyun static int boot_status;
68*4882a593Smuzhiyun static int max_timeout;
69*4882a593Smuzhiyun static void __iomem *wdt_base;
70*4882a593Smuzhiyun 
ath79_wdt_wr(unsigned reg,u32 val)71*4882a593Smuzhiyun static inline void ath79_wdt_wr(unsigned reg, u32 val)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	iowrite32(val, wdt_base + reg);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
ath79_wdt_rr(unsigned reg)76*4882a593Smuzhiyun static inline u32 ath79_wdt_rr(unsigned reg)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	return ioread32(wdt_base + reg);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
ath79_wdt_keepalive(void)81*4882a593Smuzhiyun static inline void ath79_wdt_keepalive(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout);
84*4882a593Smuzhiyun 	/* flush write */
85*4882a593Smuzhiyun 	ath79_wdt_rr(WDOG_REG_TIMER);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
ath79_wdt_enable(void)88*4882a593Smuzhiyun static inline void ath79_wdt_enable(void)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	ath79_wdt_keepalive();
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/*
93*4882a593Smuzhiyun 	 * Updating the TIMER register requires a few microseconds
94*4882a593Smuzhiyun 	 * on the AR934x SoCs at least. Use a small delay to ensure
95*4882a593Smuzhiyun 	 * that the TIMER register is updated within the hardware
96*4882a593Smuzhiyun 	 * before enabling the watchdog.
97*4882a593Smuzhiyun 	 */
98*4882a593Smuzhiyun 	udelay(2);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR);
101*4882a593Smuzhiyun 	/* flush write */
102*4882a593Smuzhiyun 	ath79_wdt_rr(WDOG_REG_CTRL);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
ath79_wdt_disable(void)105*4882a593Smuzhiyun static inline void ath79_wdt_disable(void)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE);
108*4882a593Smuzhiyun 	/* flush write */
109*4882a593Smuzhiyun 	ath79_wdt_rr(WDOG_REG_CTRL);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
ath79_wdt_set_timeout(int val)112*4882a593Smuzhiyun static int ath79_wdt_set_timeout(int val)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	if (val < 1 || val > max_timeout)
115*4882a593Smuzhiyun 		return -EINVAL;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	timeout = val;
118*4882a593Smuzhiyun 	ath79_wdt_keepalive();
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return 0;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
ath79_wdt_open(struct inode * inode,struct file * file)123*4882a593Smuzhiyun static int ath79_wdt_open(struct inode *inode, struct file *file)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
126*4882a593Smuzhiyun 		return -EBUSY;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
129*4882a593Smuzhiyun 	ath79_wdt_enable();
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	return stream_open(inode, file);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
ath79_wdt_release(struct inode * inode,struct file * file)134*4882a593Smuzhiyun static int ath79_wdt_release(struct inode *inode, struct file *file)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags))
137*4882a593Smuzhiyun 		ath79_wdt_disable();
138*4882a593Smuzhiyun 	else {
139*4882a593Smuzhiyun 		pr_crit("device closed unexpectedly, watchdog timer will not stop!\n");
140*4882a593Smuzhiyun 		ath79_wdt_keepalive();
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
144*4882a593Smuzhiyun 	clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return 0;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
ath79_wdt_write(struct file * file,const char * data,size_t len,loff_t * ppos)149*4882a593Smuzhiyun static ssize_t ath79_wdt_write(struct file *file, const char *data,
150*4882a593Smuzhiyun 				size_t len, loff_t *ppos)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	if (len) {
153*4882a593Smuzhiyun 		if (!nowayout) {
154*4882a593Smuzhiyun 			size_t i;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 			clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 			for (i = 0; i != len; i++) {
159*4882a593Smuzhiyun 				char c;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 				if (get_user(c, data + i))
162*4882a593Smuzhiyun 					return -EFAULT;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 				if (c == 'V')
165*4882a593Smuzhiyun 					set_bit(WDT_FLAGS_EXPECT_CLOSE,
166*4882a593Smuzhiyun 						&wdt_flags);
167*4882a593Smuzhiyun 			}
168*4882a593Smuzhiyun 		}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		ath79_wdt_keepalive();
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return len;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun static const struct watchdog_info ath79_wdt_info = {
177*4882a593Smuzhiyun 	.options		= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
178*4882a593Smuzhiyun 				  WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
179*4882a593Smuzhiyun 	.firmware_version	= 0,
180*4882a593Smuzhiyun 	.identity		= "ATH79 watchdog",
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
ath79_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)183*4882a593Smuzhiyun static long ath79_wdt_ioctl(struct file *file, unsigned int cmd,
184*4882a593Smuzhiyun 			    unsigned long arg)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
187*4882a593Smuzhiyun 	int __user *p = argp;
188*4882a593Smuzhiyun 	int err;
189*4882a593Smuzhiyun 	int t;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	switch (cmd) {
192*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
193*4882a593Smuzhiyun 		err = copy_to_user(argp, &ath79_wdt_info,
194*4882a593Smuzhiyun 				   sizeof(ath79_wdt_info)) ? -EFAULT : 0;
195*4882a593Smuzhiyun 		break;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
198*4882a593Smuzhiyun 		err = put_user(0, p);
199*4882a593Smuzhiyun 		break;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
202*4882a593Smuzhiyun 		err = put_user(boot_status, p);
203*4882a593Smuzhiyun 		break;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
206*4882a593Smuzhiyun 		ath79_wdt_keepalive();
207*4882a593Smuzhiyun 		err = 0;
208*4882a593Smuzhiyun 		break;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
211*4882a593Smuzhiyun 		err = get_user(t, p);
212*4882a593Smuzhiyun 		if (err)
213*4882a593Smuzhiyun 			break;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 		err = ath79_wdt_set_timeout(t);
216*4882a593Smuzhiyun 		if (err)
217*4882a593Smuzhiyun 			break;
218*4882a593Smuzhiyun 		fallthrough;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
221*4882a593Smuzhiyun 		err = put_user(timeout, p);
222*4882a593Smuzhiyun 		break;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	default:
225*4882a593Smuzhiyun 		err = -ENOTTY;
226*4882a593Smuzhiyun 		break;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return err;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static const struct file_operations ath79_wdt_fops = {
233*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
234*4882a593Smuzhiyun 	.llseek		= no_llseek,
235*4882a593Smuzhiyun 	.write		= ath79_wdt_write,
236*4882a593Smuzhiyun 	.unlocked_ioctl	= ath79_wdt_ioctl,
237*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
238*4882a593Smuzhiyun 	.open		= ath79_wdt_open,
239*4882a593Smuzhiyun 	.release	= ath79_wdt_release,
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun static struct miscdevice ath79_wdt_miscdev = {
243*4882a593Smuzhiyun 	.minor = WATCHDOG_MINOR,
244*4882a593Smuzhiyun 	.name = "watchdog",
245*4882a593Smuzhiyun 	.fops = &ath79_wdt_fops,
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
ath79_wdt_probe(struct platform_device * pdev)248*4882a593Smuzhiyun static int ath79_wdt_probe(struct platform_device *pdev)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	u32 ctrl;
251*4882a593Smuzhiyun 	int err;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (wdt_base)
254*4882a593Smuzhiyun 		return -EBUSY;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	wdt_base = devm_platform_ioremap_resource(pdev, 0);
257*4882a593Smuzhiyun 	if (IS_ERR(wdt_base))
258*4882a593Smuzhiyun 		return PTR_ERR(wdt_base);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	wdt_clk = devm_clk_get(&pdev->dev, "wdt");
261*4882a593Smuzhiyun 	if (IS_ERR(wdt_clk))
262*4882a593Smuzhiyun 		return PTR_ERR(wdt_clk);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	err = clk_prepare_enable(wdt_clk);
265*4882a593Smuzhiyun 	if (err)
266*4882a593Smuzhiyun 		return err;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	wdt_freq = clk_get_rate(wdt_clk);
269*4882a593Smuzhiyun 	if (!wdt_freq) {
270*4882a593Smuzhiyun 		err = -EINVAL;
271*4882a593Smuzhiyun 		goto err_clk_disable;
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	max_timeout = (0xfffffffful / wdt_freq);
275*4882a593Smuzhiyun 	if (timeout < 1 || timeout > max_timeout) {
276*4882a593Smuzhiyun 		timeout = max_timeout;
277*4882a593Smuzhiyun 		dev_info(&pdev->dev,
278*4882a593Smuzhiyun 			"timeout value must be 0 < timeout < %d, using %d\n",
279*4882a593Smuzhiyun 			max_timeout, timeout);
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	ctrl = ath79_wdt_rr(WDOG_REG_CTRL);
283*4882a593Smuzhiyun 	boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	err = misc_register(&ath79_wdt_miscdev);
286*4882a593Smuzhiyun 	if (err) {
287*4882a593Smuzhiyun 		dev_err(&pdev->dev,
288*4882a593Smuzhiyun 			"unable to register misc device, err=%d\n", err);
289*4882a593Smuzhiyun 		goto err_clk_disable;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	return 0;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun err_clk_disable:
295*4882a593Smuzhiyun 	clk_disable_unprepare(wdt_clk);
296*4882a593Smuzhiyun 	return err;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
ath79_wdt_remove(struct platform_device * pdev)299*4882a593Smuzhiyun static int ath79_wdt_remove(struct platform_device *pdev)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	misc_deregister(&ath79_wdt_miscdev);
302*4882a593Smuzhiyun 	clk_disable_unprepare(wdt_clk);
303*4882a593Smuzhiyun 	return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
ath79_wdt_shutdown(struct platform_device * pdev)306*4882a593Smuzhiyun static void ath79_wdt_shutdown(struct platform_device *pdev)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	ath79_wdt_disable();
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun #ifdef CONFIG_OF
312*4882a593Smuzhiyun static const struct of_device_id ath79_wdt_match[] = {
313*4882a593Smuzhiyun 	{ .compatible = "qca,ar7130-wdt" },
314*4882a593Smuzhiyun 	{},
315*4882a593Smuzhiyun };
316*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ath79_wdt_match);
317*4882a593Smuzhiyun #endif
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun static struct platform_driver ath79_wdt_driver = {
320*4882a593Smuzhiyun 	.probe		= ath79_wdt_probe,
321*4882a593Smuzhiyun 	.remove		= ath79_wdt_remove,
322*4882a593Smuzhiyun 	.shutdown	= ath79_wdt_shutdown,
323*4882a593Smuzhiyun 	.driver		= {
324*4882a593Smuzhiyun 		.name	= DRIVER_NAME,
325*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(ath79_wdt_match),
326*4882a593Smuzhiyun 	},
327*4882a593Smuzhiyun };
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun module_platform_driver(ath79_wdt_driver);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver");
332*4882a593Smuzhiyun MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
333*4882a593Smuzhiyun MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
334*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
335*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRIVER_NAME);
336