blob: bf918f5fa13175fca6e36154a649616d60a47330 [file] [log] [blame]
Mika Westerbergc90bf2a2009-11-29 16:58:38 +02001/*
2 * Watchdog driver for Technologic Systems TS-72xx based SBCs
3 * (TS-7200, TS-7250 and TS-7260). These boards have external
4 * glue logic CPLD chip, which includes programmable watchdog
5 * timer.
6 *
7 * Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi>
8 *
9 * This driver is based on ep93xx_wdt and wm831x_wdt drivers.
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020016#include <linux/platform_device.h>
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070017#include <linux/module.h>
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020018#include <linux/watchdog.h>
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070019#include <linux/io.h>
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020020
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070021#define TS72XX_WDT_DEFAULT_TIMEOUT 30
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020022
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070023static int timeout;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020024module_param(timeout, int, 0);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070025MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds.");
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020026
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010027static bool nowayout = WATCHDOG_NOWAYOUT;
28module_param(nowayout, bool, 0);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020029MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
30
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070031/* priv->control_reg */
32#define TS72XX_WDT_CTRL_DISABLE 0x00
33#define TS72XX_WDT_CTRL_250MS 0x01
34#define TS72XX_WDT_CTRL_500MS 0x02
35#define TS72XX_WDT_CTRL_1SEC 0x03
36#define TS72XX_WDT_CTRL_RESERVED 0x04
37#define TS72XX_WDT_CTRL_2SEC 0x05
38#define TS72XX_WDT_CTRL_4SEC 0x06
39#define TS72XX_WDT_CTRL_8SEC 0x07
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020040
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070041/* priv->feed_reg */
42#define TS72XX_WDT_FEED_VAL 0x05
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020043
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070044struct ts72xx_wdt_priv {
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020045 void __iomem *control_reg;
46 void __iomem *feed_reg;
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070047 struct watchdog_device wdd;
48 unsigned char regval;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020049};
50
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070051static int ts72xx_wdt_start(struct watchdog_device *wdd)
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020052{
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070053 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020054
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070055 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
56 writeb(priv->regval, priv->control_reg);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020057
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020058 return 0;
59}
60
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070061static int ts72xx_wdt_stop(struct watchdog_device *wdd)
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020062{
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070063 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020064
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070065 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
66 writeb(TS72XX_WDT_CTRL_DISABLE, priv->control_reg);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020067
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070068 return 0;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +020069}
70
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -070071static int ts72xx_wdt_ping(struct watchdog_device *wdd)
72{
73 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
74
75 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
76
77 return 0;
78}
79
80static int ts72xx_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
81{
82 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
83
84 if (to == 1) {
85 priv->regval = TS72XX_WDT_CTRL_1SEC;
86 } else if (to == 2) {
87 priv->regval = TS72XX_WDT_CTRL_2SEC;
88 } else if (to <= 4) {
89 priv->regval = TS72XX_WDT_CTRL_4SEC;
90 to = 4;
91 } else {
92 priv->regval = TS72XX_WDT_CTRL_8SEC;
93 if (to <= 8)
94 to = 8;
95 }
96
97 wdd->timeout = to;
98
99 if (watchdog_active(wdd)) {
100 ts72xx_wdt_stop(wdd);
101 ts72xx_wdt_start(wdd);
102 }
103
104 return 0;
105}
106
107static const struct watchdog_info ts72xx_wdt_ident = {
108 .options = WDIOF_KEEPALIVEPING |
109 WDIOF_SETTIMEOUT |
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200110 WDIOF_MAGICCLOSE,
111 .firmware_version = 1,
112 .identity = "TS-72XX WDT",
113};
114
Gustavo A. R. Silva5870ac72017-07-07 19:18:40 -0500115static const struct watchdog_ops ts72xx_wdt_ops = {
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200116 .owner = THIS_MODULE,
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700117 .start = ts72xx_wdt_start,
118 .stop = ts72xx_wdt_stop,
119 .ping = ts72xx_wdt_ping,
120 .set_timeout = ts72xx_wdt_settimeout,
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200121};
122
Bill Pemberton2d991a12012-11-19 13:21:41 -0500123static int ts72xx_wdt_probe(struct platform_device *pdev)
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200124{
Guenter Roeck16c46142019-04-10 09:27:45 -0700125 struct device *dev = &pdev->dev;
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700126 struct ts72xx_wdt_priv *priv;
127 struct watchdog_device *wdd;
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700128 int ret;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200129
Guenter Roeck16c46142019-04-10 09:27:45 -0700130 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700131 if (!priv)
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200132 return -ENOMEM;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200133
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700134 priv->control_reg = devm_platform_ioremap_resource(pdev, 0);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700135 if (IS_ERR(priv->control_reg))
136 return PTR_ERR(priv->control_reg);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200137
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700138 priv->feed_reg = devm_platform_ioremap_resource(pdev, 1);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700139 if (IS_ERR(priv->feed_reg))
140 return PTR_ERR(priv->feed_reg);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200141
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700142 wdd = &priv->wdd;
143 wdd->info = &ts72xx_wdt_ident;
144 wdd->ops = &ts72xx_wdt_ops;
145 wdd->min_timeout = 1;
146 wdd->max_hw_heartbeat_ms = 8000;
Guenter Roeck16c46142019-04-10 09:27:45 -0700147 wdd->parent = dev;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200148
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700149 watchdog_set_nowayout(wdd, nowayout);
Mika Westerberg0e901be2010-08-29 13:53:14 +0300150
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700151 wdd->timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
Guenter Roeck16c46142019-04-10 09:27:45 -0700152 watchdog_init_timeout(wdd, timeout, dev);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700153
154 watchdog_set_drvdata(wdd, priv);
155
Guenter Roeck16c46142019-04-10 09:27:45 -0700156 ret = devm_watchdog_register_device(dev, wdd);
H Hartley Sweeten8f8dc7b2017-01-31 09:33:30 -0700157 if (ret)
158 return ret;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200159
Guenter Roeck16c46142019-04-10 09:27:45 -0700160 dev_info(dev, "TS-72xx Watchdog driver\n");
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200161
162 return 0;
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200163}
164
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200165static struct platform_driver ts72xx_wdt_driver = {
166 .probe = ts72xx_wdt_probe,
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200167 .driver = {
168 .name = "ts72xx-wdt",
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200169 },
170};
171
Axel Linb8ec6112011-11-29 13:56:27 +0800172module_platform_driver(ts72xx_wdt_driver);
Mika Westerbergc90bf2a2009-11-29 16:58:38 +0200173
174MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
175MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
176MODULE_LICENSE("GPL");
177MODULE_ALIAS("platform:ts72xx-wdt");