blob: e0574844c3132c01f86b2c66ed72fc67e69a85d3 [file] [log] [blame]
Alessandro Zummof52ac8f2006-03-25 03:06:37 -08001/*
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 *
H Hartley Sweetene12a6792012-03-14 10:31:50 -070011 * Copyright (c) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
12 * Convert to a platform device and use the watchdog framework API
13 *
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080014 * 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
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110029#include <linux/platform_device.h>
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080030#include <linux/module.h>
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080031#include <linux/miscdevice.h>
32#include <linux/watchdog.h>
33#include <linux/timer.h>
Ryan Mallon2653d1d2009-07-15 21:33:22 +010034#include <linux/io.h>
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080035
H Hartley Sweetene12a6792012-03-14 10:31:50 -070036#define WDT_VERSION "0.4"
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080037
38/* default timeout (secs) */
39#define WDT_TIMEOUT 30
40
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010041static bool nowayout = WATCHDOG_NOWAYOUT;
H Hartley Sweetene12a6792012-03-14 10:31:50 -070042module_param(nowayout, bool, 0);
43MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
44
Wim Van Sebroeck2ca16062012-03-22 09:37:10 +010045static unsigned int timeout = WDT_TIMEOUT;
46module_param(timeout, uint, 0);
H Hartley Sweetene12a6792012-03-14 10:31:50 -070047MODULE_PARM_DESC(timeout,
48 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
49 __MODULE_STRING(WDT_TIMEOUT) ")");
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080050
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110051static void __iomem *mmio_base;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080052static struct timer_list timer;
53static unsigned long next_heartbeat;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080054
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110055#define EP93XX_WATCHDOG 0x00
56#define EP93XX_WDSTATUS 0x04
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080057
Wim Van Sebroeck2ca16062012-03-22 09:37:10 +010058/* reset the wdt every ~200ms - the heartbeat of the device is 0.250 seconds*/
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080059#define WDT_INTERVAL (HZ/5)
60
H Hartley Sweetene12a6792012-03-14 10:31:50 -070061static void ep93xx_wdt_timer_ping(unsigned long data)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080062{
63 if (time_before(jiffies, next_heartbeat))
H Hartley Sweetene12a6792012-03-14 10:31:50 -070064 writel(0x5555, mmio_base + EP93XX_WATCHDOG);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080065
66 /* Re-set the timer interval */
67 mod_timer(&timer, jiffies + WDT_INTERVAL);
68}
69
H Hartley Sweetene12a6792012-03-14 10:31:50 -070070static int ep93xx_wdt_start(struct watchdog_device *wdd)
71{
72 next_heartbeat = jiffies + (timeout * HZ);
73
74 writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
75 mod_timer(&timer, jiffies + WDT_INTERVAL);
76
77 return 0;
78}
79
80static int ep93xx_wdt_stop(struct watchdog_device *wdd)
81{
82 del_timer_sync(&timer);
83 writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
84
85 return 0;
86}
87
88static int ep93xx_wdt_keepalive(struct watchdog_device *wdd)
89{
90 /* user land ping */
91 next_heartbeat = jiffies + (timeout * HZ);
92
93 return 0;
94}
95
96static const struct watchdog_info ep93xx_wdt_ident = {
97 .options = WDIOF_CARDRESET |
98 WDIOF_MAGICCLOSE |
99 WDIOF_KEEPALIVEPING,
100 .identity = "EP93xx Watchdog",
101};
102
103static struct watchdog_ops ep93xx_wdt_ops = {
104 .owner = THIS_MODULE,
105 .start = ep93xx_wdt_start,
106 .stop = ep93xx_wdt_stop,
107 .ping = ep93xx_wdt_keepalive,
108};
109
110static struct watchdog_device ep93xx_wdt_wdd = {
111 .info = &ep93xx_wdt_ident,
112 .ops = &ep93xx_wdt_ops,
113};
114
Bill Pemberton2d991a12012-11-19 13:21:41 -0500115static int ep93xx_wdt_probe(struct platform_device *pdev)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800116{
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100117 struct resource *res;
118 unsigned long val;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800119 int err;
120
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 if (!res)
123 return -ENXIO;
124
125 if (!devm_request_mem_region(&pdev->dev, res->start,
126 resource_size(res), pdev->name))
127 return -EBUSY;
128
129 mmio_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
130 if (!mmio_base)
131 return -ENXIO;
132
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800133 if (timeout < 1 || timeout > 3600) {
134 timeout = WDT_TIMEOUT;
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700135 dev_warn(&pdev->dev,
136 "timeout value must be 1<=x<=3600, using %d\n",
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800137 timeout);
138 }
139
Wim Van Sebroeck697b41e2012-03-13 09:06:12 +0100140 val = readl(mmio_base + EP93XX_WATCHDOG);
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700141 ep93xx_wdt_wdd.bootstatus = (val & 0x01) ? WDIOF_CARDRESET : 0;
Mika Westerberg59dcf1e2012-03-18 13:09:52 +0200142 ep93xx_wdt_wdd.timeout = timeout;
Wim Van Sebroeck697b41e2012-03-13 09:06:12 +0100143
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700144 watchdog_set_nowayout(&ep93xx_wdt_wdd, nowayout);
Wim Van Sebroeck697b41e2012-03-13 09:06:12 +0100145
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700146 setup_timer(&timer, ep93xx_wdt_timer_ping, 1);
Wim Van Sebroeck697b41e2012-03-13 09:06:12 +0100147
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700148 err = watchdog_register_device(&ep93xx_wdt_wdd);
149 if (err)
150 return err;
151
152 dev_info(&pdev->dev,
153 "EP93XX watchdog, driver version " WDT_VERSION "%s\n",
Wim Van Sebroeck697b41e2012-03-13 09:06:12 +0100154 (val & 0x08) ? " (nCS1 disable detected)" : "");
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700155
156 return 0;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800157}
158
Bill Pemberton4b12b892012-11-19 13:26:24 -0500159static int ep93xx_wdt_remove(struct platform_device *pdev)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800160{
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700161 watchdog_unregister_device(&ep93xx_wdt_wdd);
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100162 return 0;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800163}
164
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100165static struct platform_driver ep93xx_wdt_driver = {
166 .driver = {
167 .owner = THIS_MODULE,
168 .name = "ep93xx-wdt",
169 },
170 .probe = ep93xx_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500171 .remove = ep93xx_wdt_remove,
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100172};
173
174module_platform_driver(ep93xx_wdt_driver);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800175
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800176MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
H Hartley Sweetene12a6792012-03-14 10:31:50 -0700177 "Alessandro Zummo <a.zummo@towertech.it>,"
178 "H Hartley Sweeten <hsweeten@visionengravers.com>");
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800179MODULE_DESCRIPTION("EP93xx Watchdog");
180MODULE_LICENSE("GPL");
181MODULE_VERSION(WDT_VERSION);
182MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);