Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Watchdog driver for the A21 VME CPU Boards
4 *
5 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
6 *
7 */
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/platform_device.h>
14#include <linux/watchdog.h>
15#include <linux/uaccess.h>
16#include <linux/gpio/consumer.h>
17#include <linux/delay.h>
18#include <linux/bitops.h>
19#include <linux/of.h>
20
21#define NUM_GPIOS 6
22
23enum a21_wdt_gpios {
24 GPIO_WD_ENAB,
25 GPIO_WD_FAST,
26 GPIO_WD_TRIG,
27 GPIO_WD_RST0,
28 GPIO_WD_RST1,
29 GPIO_WD_RST2,
30};
31
32struct a21_wdt_drv {
33 struct watchdog_device wdt;
34 struct gpio_desc *gpios[NUM_GPIOS];
35};
36
37static bool nowayout = WATCHDOG_NOWAYOUT;
38module_param(nowayout, bool, 0);
39MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
41
42static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
43{
44 int reset = 0;
45
46 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
47 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
48 reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
49
50 return reset;
51}
52
53static int a21_wdt_start(struct watchdog_device *wdt)
54{
55 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
56
57 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1);
58
59 return 0;
60}
61
62static int a21_wdt_stop(struct watchdog_device *wdt)
63{
64 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
65
66 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
67
68 return 0;
69}
70
71static int a21_wdt_ping(struct watchdog_device *wdt)
72{
73 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
74
75 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0);
76 ndelay(10);
77 gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1);
78
79 return 0;
80}
81
82static int a21_wdt_set_timeout(struct watchdog_device *wdt,
83 unsigned int timeout)
84{
85 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
86
87 if (timeout != 1 && timeout != 30) {
88 dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
89 return -EINVAL;
90 }
91
92 if (timeout == 30 && wdt->timeout == 1) {
93 dev_err(wdt->parent,
94 "Transition from fast to slow mode not allowed\n");
95 return -EINVAL;
96 }
97
98 if (timeout == 1)
99 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1);
100 else
101 gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0);
102
103 wdt->timeout = timeout;
104
105 return 0;
106}
107
108static const struct watchdog_info a21_wdt_info = {
109 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
110 .identity = "MEN A21 Watchdog",
111};
112
113static const struct watchdog_ops a21_wdt_ops = {
114 .owner = THIS_MODULE,
115 .start = a21_wdt_start,
116 .stop = a21_wdt_stop,
117 .ping = a21_wdt_ping,
118 .set_timeout = a21_wdt_set_timeout,
119};
120
121static struct watchdog_device a21_wdt = {
122 .info = &a21_wdt_info,
123 .ops = &a21_wdt_ops,
124 .min_timeout = 1,
125 .max_timeout = 30,
126};
127
128static int a21_wdt_probe(struct platform_device *pdev)
129{
130 struct device *dev = &pdev->dev;
131 struct a21_wdt_drv *drv;
132 unsigned int reset = 0;
133 int num_gpios;
134 int ret;
135 int i;
136
137 drv = devm_kzalloc(dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
138 if (!drv)
139 return -ENOMEM;
140
141 num_gpios = gpiod_count(dev, NULL);
142 if (num_gpios != NUM_GPIOS) {
143 dev_err(dev, "gpios DT property wrong, got %d want %d",
144 num_gpios, NUM_GPIOS);
145 return -ENODEV;
146 }
147
148 /* Request the used GPIOs */
149 for (i = 0; i < num_gpios; i++) {
150 enum gpiod_flags gflags;
151
152 if (i < GPIO_WD_RST0)
153 gflags = GPIOD_ASIS;
154 else
155 gflags = GPIOD_IN;
156 drv->gpios[i] = devm_gpiod_get_index(dev, NULL, i, gflags);
157 if (IS_ERR(drv->gpios[i]))
158 return PTR_ERR(drv->gpios[i]);
159
160 gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog");
161
162 /*
163 * Retrieve the initial value from the GPIOs that should be
164 * output, then set up the line as output with that value.
165 */
166 if (i < GPIO_WD_RST0) {
167 int val;
168
169 val = gpiod_get_value(drv->gpios[i]);
170 gpiod_direction_output(drv->gpios[i], val);
171 }
172 }
173
174 watchdog_init_timeout(&a21_wdt, 30, dev);
175 watchdog_set_nowayout(&a21_wdt, nowayout);
176 watchdog_set_drvdata(&a21_wdt, drv);
177 a21_wdt.parent = dev;
178
179 reset = a21_wdt_get_bootstatus(drv);
180 if (reset == 2)
181 a21_wdt.bootstatus |= WDIOF_EXTERN1;
182 else if (reset == 4)
183 a21_wdt.bootstatus |= WDIOF_CARDRESET;
184 else if (reset == 5)
185 a21_wdt.bootstatus |= WDIOF_POWERUNDER;
186 else if (reset == 7)
187 a21_wdt.bootstatus |= WDIOF_EXTERN2;
188
189 drv->wdt = a21_wdt;
190 dev_set_drvdata(dev, drv);
191
192 ret = devm_watchdog_register_device(dev, &a21_wdt);
193 if (ret)
194 return ret;
195
196 dev_info(dev, "MEN A21 watchdog timer driver enabled\n");
197
198 return 0;
199}
200
201static void a21_wdt_shutdown(struct platform_device *pdev)
202{
203 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
204
205 gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
206}
207
208static const struct of_device_id a21_wdt_ids[] = {
209 { .compatible = "men,a021-wdt" },
210 { },
211};
212MODULE_DEVICE_TABLE(of, a21_wdt_ids);
213
214static struct platform_driver a21_wdt_driver = {
215 .probe = a21_wdt_probe,
216 .shutdown = a21_wdt_shutdown,
217 .driver = {
218 .name = "a21-watchdog",
219 .of_match_table = a21_wdt_ids,
220 },
221};
222
223module_platform_driver(a21_wdt_driver);
224
225MODULE_AUTHOR("MEN Mikro Elektronik");
226MODULE_DESCRIPTION("MEN A21 Watchdog");
227MODULE_LICENSE("GPL");
228MODULE_ALIAS("platform:a21-watchdog");
1/*
2 * Watchdog driver for the A21 VME CPU Boards
3 *
4 * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation
9 */
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/watchdog.h>
17#include <linux/uaccess.h>
18#include <linux/gpio.h>
19#include <linux/of_gpio.h>
20#include <linux/delay.h>
21#include <linux/bitops.h>
22
23#define NUM_GPIOS 6
24
25enum a21_wdt_gpios {
26 GPIO_WD_ENAB,
27 GPIO_WD_FAST,
28 GPIO_WD_TRIG,
29 GPIO_WD_RST0,
30 GPIO_WD_RST1,
31 GPIO_WD_RST2,
32};
33
34struct a21_wdt_drv {
35 struct watchdog_device wdt;
36 struct mutex lock;
37 unsigned gpios[NUM_GPIOS];
38};
39
40static bool nowayout = WATCHDOG_NOWAYOUT;
41module_param(nowayout, bool, 0);
42MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
44
45static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
46{
47 int reset = 0;
48
49 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
50 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
51 reset |= gpio_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
52
53 return reset;
54}
55
56static int a21_wdt_start(struct watchdog_device *wdt)
57{
58 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
59
60 mutex_lock(&drv->lock);
61
62 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 1);
63
64 mutex_unlock(&drv->lock);
65
66 return 0;
67}
68
69static int a21_wdt_stop(struct watchdog_device *wdt)
70{
71 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
72
73 mutex_lock(&drv->lock);
74
75 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
76
77 mutex_unlock(&drv->lock);
78
79 return 0;
80}
81
82static int a21_wdt_ping(struct watchdog_device *wdt)
83{
84 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
85
86 mutex_lock(&drv->lock);
87
88 gpio_set_value(drv->gpios[GPIO_WD_TRIG], 0);
89 ndelay(10);
90 gpio_set_value(drv->gpios[GPIO_WD_TRIG], 1);
91
92 mutex_unlock(&drv->lock);
93
94 return 0;
95}
96
97static int a21_wdt_set_timeout(struct watchdog_device *wdt,
98 unsigned int timeout)
99{
100 struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
101
102 if (timeout != 1 && timeout != 30) {
103 dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
104 return -EINVAL;
105 }
106
107 if (timeout == 30 && wdt->timeout == 1) {
108 dev_err(wdt->parent,
109 "Transition from fast to slow mode not allowed\n");
110 return -EINVAL;
111 }
112
113 mutex_lock(&drv->lock);
114
115 if (timeout == 1)
116 gpio_set_value(drv->gpios[GPIO_WD_FAST], 1);
117 else
118 gpio_set_value(drv->gpios[GPIO_WD_FAST], 0);
119
120 wdt->timeout = timeout;
121
122 mutex_unlock(&drv->lock);
123
124 return 0;
125}
126
127static const struct watchdog_info a21_wdt_info = {
128 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
129 .identity = "MEN A21 Watchdog",
130};
131
132static const struct watchdog_ops a21_wdt_ops = {
133 .owner = THIS_MODULE,
134 .start = a21_wdt_start,
135 .stop = a21_wdt_stop,
136 .ping = a21_wdt_ping,
137 .set_timeout = a21_wdt_set_timeout,
138};
139
140static struct watchdog_device a21_wdt = {
141 .info = &a21_wdt_info,
142 .ops = &a21_wdt_ops,
143 .min_timeout = 1,
144 .max_timeout = 30,
145};
146
147static int a21_wdt_probe(struct platform_device *pdev)
148{
149 struct device_node *node;
150 struct a21_wdt_drv *drv;
151 unsigned int reset = 0;
152 int num_gpios;
153 int ret;
154 int i;
155
156 drv = devm_kzalloc(&pdev->dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
157 if (!drv)
158 return -ENOMEM;
159
160 /* Fill GPIO pin array */
161 node = pdev->dev.of_node;
162
163 num_gpios = of_gpio_count(node);
164 if (num_gpios != NUM_GPIOS) {
165 dev_err(&pdev->dev, "gpios DT property wrong, got %d want %d",
166 num_gpios, NUM_GPIOS);
167 return -ENODEV;
168 }
169
170 for (i = 0; i < num_gpios; i++) {
171 int val;
172
173 val = of_get_gpio(node, i);
174 if (val < 0)
175 return val;
176
177 drv->gpios[i] = val;
178 }
179
180 /* Request the used GPIOs */
181 for (i = 0; i < num_gpios; i++) {
182 ret = devm_gpio_request(&pdev->dev, drv->gpios[i],
183 "MEN A21 Watchdog");
184 if (ret)
185 return ret;
186
187 if (i < GPIO_WD_RST0)
188 ret = gpio_direction_output(drv->gpios[i],
189 gpio_get_value(drv->gpios[i]));
190 else /* GPIO_WD_RST[0..2] are inputs */
191 ret = gpio_direction_input(drv->gpios[i]);
192 if (ret)
193 return ret;
194 }
195
196 mutex_init(&drv->lock);
197 watchdog_init_timeout(&a21_wdt, 30, &pdev->dev);
198 watchdog_set_nowayout(&a21_wdt, nowayout);
199 watchdog_set_drvdata(&a21_wdt, drv);
200 a21_wdt.parent = &pdev->dev;
201
202 reset = a21_wdt_get_bootstatus(drv);
203 if (reset == 2)
204 a21_wdt.bootstatus |= WDIOF_EXTERN1;
205 else if (reset == 4)
206 a21_wdt.bootstatus |= WDIOF_CARDRESET;
207 else if (reset == 5)
208 a21_wdt.bootstatus |= WDIOF_POWERUNDER;
209 else if (reset == 7)
210 a21_wdt.bootstatus |= WDIOF_EXTERN2;
211
212 drv->wdt = a21_wdt;
213 dev_set_drvdata(&pdev->dev, drv);
214
215 ret = watchdog_register_device(&a21_wdt);
216 if (ret) {
217 dev_err(&pdev->dev, "Cannot register watchdog device\n");
218 goto err_register_wd;
219 }
220
221 dev_info(&pdev->dev, "MEN A21 watchdog timer driver enabled\n");
222
223 return 0;
224
225err_register_wd:
226 mutex_destroy(&drv->lock);
227
228 return ret;
229}
230
231static int a21_wdt_remove(struct platform_device *pdev)
232{
233 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
234
235 dev_warn(&pdev->dev,
236 "Unregistering A21 watchdog driver, board may reboot\n");
237
238 watchdog_unregister_device(&drv->wdt);
239
240 mutex_destroy(&drv->lock);
241
242 return 0;
243}
244
245static void a21_wdt_shutdown(struct platform_device *pdev)
246{
247 struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
248
249 gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
250}
251
252static const struct of_device_id a21_wdt_ids[] = {
253 { .compatible = "men,a021-wdt" },
254 { },
255};
256MODULE_DEVICE_TABLE(of, a21_wdt_ids);
257
258static struct platform_driver a21_wdt_driver = {
259 .probe = a21_wdt_probe,
260 .remove = a21_wdt_remove,
261 .shutdown = a21_wdt_shutdown,
262 .driver = {
263 .name = "a21-watchdog",
264 .of_match_table = a21_wdt_ids,
265 },
266};
267
268module_platform_driver(a21_wdt_driver);
269
270MODULE_AUTHOR("MEN Mikro Elektronik");
271MODULE_DESCRIPTION("MEN A21 Watchdog");
272MODULE_LICENSE("GPL");
273MODULE_ALIAS("platform:a21-watchdog");