Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Watchdog driver for Technologic Systems TS-72xx based SBCs
4 * (TS-7200, TS-7250 and TS-7260). These boards have external
5 * glue logic CPLD chip, which includes programmable watchdog
6 * timer.
7 *
8 * Copyright (c) 2009 Mika Westerberg <mika.westerberg@iki.fi>
9 *
10 * This driver is based on ep93xx_wdt and wm831x_wdt drivers.
11 *
12 */
13
14#include <linux/platform_device.h>
15#include <linux/mod_devicetable.h>
16#include <linux/module.h>
17#include <linux/watchdog.h>
18#include <linux/io.h>
19
20#define TS72XX_WDT_DEFAULT_TIMEOUT 30
21
22static int timeout;
23module_param(timeout, int, 0);
24MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds.");
25
26static bool nowayout = WATCHDOG_NOWAYOUT;
27module_param(nowayout, bool, 0);
28MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
29
30/* priv->control_reg */
31#define TS72XX_WDT_CTRL_DISABLE 0x00
32#define TS72XX_WDT_CTRL_250MS 0x01
33#define TS72XX_WDT_CTRL_500MS 0x02
34#define TS72XX_WDT_CTRL_1SEC 0x03
35#define TS72XX_WDT_CTRL_RESERVED 0x04
36#define TS72XX_WDT_CTRL_2SEC 0x05
37#define TS72XX_WDT_CTRL_4SEC 0x06
38#define TS72XX_WDT_CTRL_8SEC 0x07
39
40/* priv->feed_reg */
41#define TS72XX_WDT_FEED_VAL 0x05
42
43struct ts72xx_wdt_priv {
44 void __iomem *control_reg;
45 void __iomem *feed_reg;
46 struct watchdog_device wdd;
47 unsigned char regval;
48};
49
50static int ts72xx_wdt_start(struct watchdog_device *wdd)
51{
52 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
53
54 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
55 writeb(priv->regval, priv->control_reg);
56
57 return 0;
58}
59
60static int ts72xx_wdt_stop(struct watchdog_device *wdd)
61{
62 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
63
64 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
65 writeb(TS72XX_WDT_CTRL_DISABLE, priv->control_reg);
66
67 return 0;
68}
69
70static int ts72xx_wdt_ping(struct watchdog_device *wdd)
71{
72 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
73
74 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
75
76 return 0;
77}
78
79static int ts72xx_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
80{
81 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
82
83 if (to == 1) {
84 priv->regval = TS72XX_WDT_CTRL_1SEC;
85 } else if (to == 2) {
86 priv->regval = TS72XX_WDT_CTRL_2SEC;
87 } else if (to <= 4) {
88 priv->regval = TS72XX_WDT_CTRL_4SEC;
89 to = 4;
90 } else {
91 priv->regval = TS72XX_WDT_CTRL_8SEC;
92 if (to <= 8)
93 to = 8;
94 }
95
96 wdd->timeout = to;
97
98 if (watchdog_active(wdd)) {
99 ts72xx_wdt_stop(wdd);
100 ts72xx_wdt_start(wdd);
101 }
102
103 return 0;
104}
105
106static const struct watchdog_info ts72xx_wdt_ident = {
107 .options = WDIOF_KEEPALIVEPING |
108 WDIOF_SETTIMEOUT |
109 WDIOF_MAGICCLOSE,
110 .firmware_version = 1,
111 .identity = "TS-72XX WDT",
112};
113
114static const struct watchdog_ops ts72xx_wdt_ops = {
115 .owner = THIS_MODULE,
116 .start = ts72xx_wdt_start,
117 .stop = ts72xx_wdt_stop,
118 .ping = ts72xx_wdt_ping,
119 .set_timeout = ts72xx_wdt_settimeout,
120};
121
122static int ts72xx_wdt_probe(struct platform_device *pdev)
123{
124 struct device *dev = &pdev->dev;
125 struct ts72xx_wdt_priv *priv;
126 struct watchdog_device *wdd;
127 int ret;
128
129 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
130 if (!priv)
131 return -ENOMEM;
132
133 priv->control_reg = devm_platform_ioremap_resource(pdev, 0);
134 if (IS_ERR(priv->control_reg))
135 return PTR_ERR(priv->control_reg);
136
137 priv->feed_reg = devm_platform_ioremap_resource(pdev, 1);
138 if (IS_ERR(priv->feed_reg))
139 return PTR_ERR(priv->feed_reg);
140
141 wdd = &priv->wdd;
142 wdd->info = &ts72xx_wdt_ident;
143 wdd->ops = &ts72xx_wdt_ops;
144 wdd->min_timeout = 1;
145 wdd->max_hw_heartbeat_ms = 8000;
146 wdd->parent = dev;
147
148 watchdog_set_nowayout(wdd, nowayout);
149
150 wdd->timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
151 watchdog_init_timeout(wdd, timeout, dev);
152
153 watchdog_set_drvdata(wdd, priv);
154
155 ret = devm_watchdog_register_device(dev, wdd);
156 if (ret)
157 return ret;
158
159 dev_info(dev, "TS-72xx Watchdog driver\n");
160
161 return 0;
162}
163
164static const struct of_device_id ts72xx_wdt_of_ids[] = {
165 { .compatible = "technologic,ts7200-wdt" },
166 { /* sentinel */ }
167};
168MODULE_DEVICE_TABLE(of, ts72xx_wdt_of_ids);
169
170static struct platform_driver ts72xx_wdt_driver = {
171 .probe = ts72xx_wdt_probe,
172 .driver = {
173 .name = "ts72xx-wdt",
174 .of_match_table = ts72xx_wdt_of_ids,
175 },
176};
177
178module_platform_driver(ts72xx_wdt_driver);
179
180MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
181MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
182MODULE_LICENSE("GPL");
183MODULE_ALIAS("platform:ts72xx-wdt");
1/*
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
16#include <linux/platform_device.h>
17#include <linux/module.h>
18#include <linux/watchdog.h>
19#include <linux/io.h>
20
21#define TS72XX_WDT_DEFAULT_TIMEOUT 30
22
23static int timeout;
24module_param(timeout, int, 0);
25MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds.");
26
27static bool nowayout = WATCHDOG_NOWAYOUT;
28module_param(nowayout, bool, 0);
29MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
30
31/* 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
40
41/* priv->feed_reg */
42#define TS72XX_WDT_FEED_VAL 0x05
43
44struct ts72xx_wdt_priv {
45 void __iomem *control_reg;
46 void __iomem *feed_reg;
47 struct watchdog_device wdd;
48 unsigned char regval;
49};
50
51static int ts72xx_wdt_start(struct watchdog_device *wdd)
52{
53 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
54
55 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
56 writeb(priv->regval, priv->control_reg);
57
58 return 0;
59}
60
61static int ts72xx_wdt_stop(struct watchdog_device *wdd)
62{
63 struct ts72xx_wdt_priv *priv = watchdog_get_drvdata(wdd);
64
65 writeb(TS72XX_WDT_FEED_VAL, priv->feed_reg);
66 writeb(TS72XX_WDT_CTRL_DISABLE, priv->control_reg);
67
68 return 0;
69}
70
71static 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 |
110 WDIOF_MAGICCLOSE,
111 .firmware_version = 1,
112 .identity = "TS-72XX WDT",
113};
114
115static const struct watchdog_ops ts72xx_wdt_ops = {
116 .owner = THIS_MODULE,
117 .start = ts72xx_wdt_start,
118 .stop = ts72xx_wdt_stop,
119 .ping = ts72xx_wdt_ping,
120 .set_timeout = ts72xx_wdt_settimeout,
121};
122
123static int ts72xx_wdt_probe(struct platform_device *pdev)
124{
125 struct ts72xx_wdt_priv *priv;
126 struct watchdog_device *wdd;
127 struct resource *res;
128 int ret;
129
130 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
131 if (!priv)
132 return -ENOMEM;
133
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135 priv->control_reg = devm_ioremap_resource(&pdev->dev, res);
136 if (IS_ERR(priv->control_reg))
137 return PTR_ERR(priv->control_reg);
138
139 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
140 priv->feed_reg = devm_ioremap_resource(&pdev->dev, res);
141 if (IS_ERR(priv->feed_reg))
142 return PTR_ERR(priv->feed_reg);
143
144 wdd = &priv->wdd;
145 wdd->info = &ts72xx_wdt_ident;
146 wdd->ops = &ts72xx_wdt_ops;
147 wdd->min_timeout = 1;
148 wdd->max_hw_heartbeat_ms = 8000;
149 wdd->parent = &pdev->dev;
150
151 watchdog_set_nowayout(wdd, nowayout);
152
153 wdd->timeout = TS72XX_WDT_DEFAULT_TIMEOUT;
154 watchdog_init_timeout(wdd, timeout, &pdev->dev);
155
156 watchdog_set_drvdata(wdd, priv);
157
158 ret = devm_watchdog_register_device(&pdev->dev, wdd);
159 if (ret)
160 return ret;
161
162 dev_info(&pdev->dev, "TS-72xx Watchdog driver\n");
163
164 return 0;
165}
166
167static struct platform_driver ts72xx_wdt_driver = {
168 .probe = ts72xx_wdt_probe,
169 .driver = {
170 .name = "ts72xx-wdt",
171 },
172};
173
174module_platform_driver(ts72xx_wdt_driver);
175
176MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
177MODULE_DESCRIPTION("TS-72xx SBC Watchdog");
178MODULE_LICENSE("GPL");
179MODULE_ALIAS("platform:ts72xx-wdt");