xref: /openbmc/linux/drivers/watchdog/ep93xx_wdt.c (revision 8a10bc9d)
1 /*
2  * Watchdog driver for Cirrus Logic EP93xx family of devices.
3  *
4  * Copyright (c) 2004 Ray Lehtiniemi
5  * Copyright (c) 2006 Tower Technologies
6  * Based on ep93xx driver, bits from alim7101_wdt.c
7  *
8  * Authors: Ray Lehtiniemi <rayl@mail.com>,
9  *	Alessandro Zummo <a.zummo@towertech.it>
10  *
11  * Copyright (c) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
12  *	Convert to a platform device and use the watchdog framework API
13  *
14  * This file is licensed under the terms of the GNU General Public
15  * License version 2. This program is licensed "as is" without any
16  * warranty of any kind, whether express or implied.
17  *
18  * This watchdog fires after 250msec, which is a too short interval
19  * for us to rely on the user space daemon alone. So we ping the
20  * wdt each ~200msec and eventually stop doing it if the user space
21  * daemon dies.
22  *
23  * TODO:
24  *
25  *	- Test last reset from watchdog status
26  *	- Add a few missing ioctls
27  */
28 
29 #include <linux/platform_device.h>
30 #include <linux/module.h>
31 #include <linux/watchdog.h>
32 #include <linux/timer.h>
33 #include <linux/io.h>
34 
35 #define WDT_VERSION	"0.4"
36 
37 /* default timeout (secs) */
38 #define WDT_TIMEOUT 30
39 
40 static bool nowayout = WATCHDOG_NOWAYOUT;
41 module_param(nowayout, bool, 0);
42 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
43 
44 static unsigned int timeout = WDT_TIMEOUT;
45 module_param(timeout, uint, 0);
46 MODULE_PARM_DESC(timeout,
47 	"Watchdog timeout in seconds. (1<=timeout<=3600, default="
48 				__MODULE_STRING(WDT_TIMEOUT) ")");
49 
50 static void __iomem *mmio_base;
51 static struct timer_list timer;
52 static unsigned long next_heartbeat;
53 
54 #define EP93XX_WATCHDOG		0x00
55 #define EP93XX_WDSTATUS		0x04
56 
57 /* reset the wdt every ~200ms - the heartbeat of the device is 0.250 seconds*/
58 #define WDT_INTERVAL (HZ/5)
59 
60 static void ep93xx_wdt_timer_ping(unsigned long data)
61 {
62 	if (time_before(jiffies, next_heartbeat))
63 		writel(0x5555, mmio_base + EP93XX_WATCHDOG);
64 
65 	/* Re-set the timer interval */
66 	mod_timer(&timer, jiffies + WDT_INTERVAL);
67 }
68 
69 static int ep93xx_wdt_start(struct watchdog_device *wdd)
70 {
71 	next_heartbeat = jiffies + (timeout * HZ);
72 
73 	writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
74 	mod_timer(&timer, jiffies + WDT_INTERVAL);
75 
76 	return 0;
77 }
78 
79 static int ep93xx_wdt_stop(struct watchdog_device *wdd)
80 {
81 	del_timer_sync(&timer);
82 	writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
83 
84 	return 0;
85 }
86 
87 static int ep93xx_wdt_keepalive(struct watchdog_device *wdd)
88 {
89 	/* user land ping */
90 	next_heartbeat = jiffies + (timeout * HZ);
91 
92 	return 0;
93 }
94 
95 static const struct watchdog_info ep93xx_wdt_ident = {
96 	.options	= WDIOF_CARDRESET |
97 			  WDIOF_MAGICCLOSE |
98 			  WDIOF_KEEPALIVEPING,
99 	.identity	= "EP93xx Watchdog",
100 };
101 
102 static struct watchdog_ops ep93xx_wdt_ops = {
103 	.owner		= THIS_MODULE,
104 	.start		= ep93xx_wdt_start,
105 	.stop		= ep93xx_wdt_stop,
106 	.ping		= ep93xx_wdt_keepalive,
107 };
108 
109 static struct watchdog_device ep93xx_wdt_wdd = {
110 	.info		= &ep93xx_wdt_ident,
111 	.ops		= &ep93xx_wdt_ops,
112 };
113 
114 static int ep93xx_wdt_probe(struct platform_device *pdev)
115 {
116 	struct resource *res;
117 	unsigned long val;
118 	int err;
119 
120 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121 	if (!res)
122 		return -ENXIO;
123 
124 	if (!devm_request_mem_region(&pdev->dev, res->start,
125 				     resource_size(res), pdev->name))
126 		return -EBUSY;
127 
128 	mmio_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
129 	if (!mmio_base)
130 		return -ENXIO;
131 
132 	if (timeout < 1 || timeout > 3600) {
133 		timeout = WDT_TIMEOUT;
134 		dev_warn(&pdev->dev,
135 			"timeout value must be 1<=x<=3600, using %d\n",
136 			timeout);
137 	}
138 
139 	val = readl(mmio_base + EP93XX_WATCHDOG);
140 	ep93xx_wdt_wdd.bootstatus = (val & 0x01) ? WDIOF_CARDRESET : 0;
141 	ep93xx_wdt_wdd.timeout = timeout;
142 
143 	watchdog_set_nowayout(&ep93xx_wdt_wdd, nowayout);
144 
145 	setup_timer(&timer, ep93xx_wdt_timer_ping, 1);
146 
147 	err = watchdog_register_device(&ep93xx_wdt_wdd);
148 	if (err)
149 		return err;
150 
151 	dev_info(&pdev->dev,
152 		"EP93XX watchdog, driver version " WDT_VERSION "%s\n",
153 		(val & 0x08) ? " (nCS1 disable detected)" : "");
154 
155 	return 0;
156 }
157 
158 static int ep93xx_wdt_remove(struct platform_device *pdev)
159 {
160 	watchdog_unregister_device(&ep93xx_wdt_wdd);
161 	return 0;
162 }
163 
164 static struct platform_driver ep93xx_wdt_driver = {
165 	.driver		= {
166 		.owner	= THIS_MODULE,
167 		.name	= "ep93xx-wdt",
168 	},
169 	.probe		= ep93xx_wdt_probe,
170 	.remove		= ep93xx_wdt_remove,
171 };
172 
173 module_platform_driver(ep93xx_wdt_driver);
174 
175 MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
176 		"Alessandro Zummo <a.zummo@towertech.it>,"
177 		"H Hartley Sweeten <hsweeten@visionengravers.com>");
178 MODULE_DESCRIPTION("EP93xx Watchdog");
179 MODULE_LICENSE("GPL");
180 MODULE_VERSION(WDT_VERSION);
181