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