Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * GE watchdog userspace interface
4 *
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 *
7 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 *
9 * Based on: mv64x60_wdt.c (MV64X60 watchdog userspace interface)
10 * Author: James Chapman <jchapman@katalix.com>
11 */
12
13/* TODO:
14 * This driver does not provide support for the hardwares capability of sending
15 * an interrupt at a programmable threshold.
16 *
17 * This driver currently can only support 1 watchdog - there are 2 in the
18 * hardware that this driver supports. Thus one could be configured as a
19 * process-based watchdog (via /dev/watchdog), the second (using the interrupt
20 * capabilities) a kernel-based watchdog.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/kernel.h>
26#include <linux/compiler.h>
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/miscdevice.h>
30#include <linux/watchdog.h>
31#include <linux/fs.h>
32#include <linux/of.h>
33#include <linux/of_address.h>
34#include <linux/of_platform.h>
35#include <linux/io.h>
36#include <linux/uaccess.h>
37
38#include <sysdev/fsl_soc.h>
39
40/*
41 * The watchdog configuration register contains a pair of 2-bit fields,
42 * 1. a reload field, bits 27-26, which triggers a reload of
43 * the countdown register, and
44 * 2. an enable field, bits 25-24, which toggles between
45 * enabling and disabling the watchdog timer.
46 * Bit 31 is a read-only field which indicates whether the
47 * watchdog timer is currently enabled.
48 *
49 * The low 24 bits contain the timer reload value.
50 */
51#define GEF_WDC_ENABLE_SHIFT 24
52#define GEF_WDC_SERVICE_SHIFT 26
53#define GEF_WDC_ENABLED_SHIFT 31
54
55#define GEF_WDC_ENABLED_TRUE 1
56#define GEF_WDC_ENABLED_FALSE 0
57
58/* Flags bits */
59#define GEF_WDOG_FLAG_OPENED 0
60
61static unsigned long wdt_flags;
62static int wdt_status;
63static void __iomem *gef_wdt_regs;
64static int gef_wdt_timeout;
65static int gef_wdt_count;
66static unsigned int bus_clk;
67static char expect_close;
68static DEFINE_SPINLOCK(gef_wdt_spinlock);
69
70static bool nowayout = WATCHDOG_NOWAYOUT;
71module_param(nowayout, bool, 0);
72MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
73 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
74
75
76static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift)
77{
78 u32 data;
79 u32 enabled;
80 int ret = 0;
81
82 spin_lock(&gef_wdt_spinlock);
83 data = ioread32be(gef_wdt_regs);
84 enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1;
85
86 /* only toggle the requested field if enabled state matches predicate */
87 if ((enabled ^ enabled_predicate) == 0) {
88 /* We write a 1, then a 2 -- to the appropriate field */
89 data = (1 << field_shift) | gef_wdt_count;
90 iowrite32be(data, gef_wdt_regs);
91
92 data = (2 << field_shift) | gef_wdt_count;
93 iowrite32be(data, gef_wdt_regs);
94 ret = 1;
95 }
96 spin_unlock(&gef_wdt_spinlock);
97
98 return ret;
99}
100
101static void gef_wdt_service(void)
102{
103 gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
104 GEF_WDC_SERVICE_SHIFT);
105}
106
107static void gef_wdt_handler_enable(void)
108{
109 if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE,
110 GEF_WDC_ENABLE_SHIFT)) {
111 gef_wdt_service();
112 pr_notice("watchdog activated\n");
113 }
114}
115
116static void gef_wdt_handler_disable(void)
117{
118 if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
119 GEF_WDC_ENABLE_SHIFT))
120 pr_notice("watchdog deactivated\n");
121}
122
123static void gef_wdt_set_timeout(unsigned int timeout)
124{
125 /* maximum bus cycle count is 0xFFFFFFFF */
126 if (timeout > 0xFFFFFFFF / bus_clk)
127 timeout = 0xFFFFFFFF / bus_clk;
128
129 /* Register only holds upper 24 bits, bit shifted into lower 24 */
130 gef_wdt_count = (timeout * bus_clk) >> 8;
131 gef_wdt_timeout = timeout;
132}
133
134
135static ssize_t gef_wdt_write(struct file *file, const char __user *data,
136 size_t len, loff_t *ppos)
137{
138 if (len) {
139 if (!nowayout) {
140 size_t i;
141
142 expect_close = 0;
143
144 for (i = 0; i != len; i++) {
145 char c;
146 if (get_user(c, data + i))
147 return -EFAULT;
148 if (c == 'V')
149 expect_close = 42;
150 }
151 }
152 gef_wdt_service();
153 }
154
155 return len;
156}
157
158static long gef_wdt_ioctl(struct file *file, unsigned int cmd,
159 unsigned long arg)
160{
161 int timeout;
162 int options;
163 void __user *argp = (void __user *)arg;
164 static const struct watchdog_info info = {
165 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
166 WDIOF_KEEPALIVEPING,
167 .firmware_version = 0,
168 .identity = "GE watchdog",
169 };
170
171 switch (cmd) {
172 case WDIOC_GETSUPPORT:
173 if (copy_to_user(argp, &info, sizeof(info)))
174 return -EFAULT;
175 break;
176
177 case WDIOC_GETSTATUS:
178 case WDIOC_GETBOOTSTATUS:
179 if (put_user(wdt_status, (int __user *)argp))
180 return -EFAULT;
181 wdt_status &= ~WDIOF_KEEPALIVEPING;
182 break;
183
184 case WDIOC_SETOPTIONS:
185 if (get_user(options, (int __user *)argp))
186 return -EFAULT;
187
188 if (options & WDIOS_DISABLECARD)
189 gef_wdt_handler_disable();
190
191 if (options & WDIOS_ENABLECARD)
192 gef_wdt_handler_enable();
193 break;
194
195 case WDIOC_KEEPALIVE:
196 gef_wdt_service();
197 wdt_status |= WDIOF_KEEPALIVEPING;
198 break;
199
200 case WDIOC_SETTIMEOUT:
201 if (get_user(timeout, (int __user *)argp))
202 return -EFAULT;
203 gef_wdt_set_timeout(timeout);
204 fallthrough;
205
206 case WDIOC_GETTIMEOUT:
207 if (put_user(gef_wdt_timeout, (int __user *)argp))
208 return -EFAULT;
209 break;
210
211 default:
212 return -ENOTTY;
213 }
214
215 return 0;
216}
217
218static int gef_wdt_open(struct inode *inode, struct file *file)
219{
220 if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags))
221 return -EBUSY;
222
223 if (nowayout)
224 __module_get(THIS_MODULE);
225
226 gef_wdt_handler_enable();
227
228 return stream_open(inode, file);
229}
230
231static int gef_wdt_release(struct inode *inode, struct file *file)
232{
233 if (expect_close == 42)
234 gef_wdt_handler_disable();
235 else {
236 pr_crit("unexpected close, not stopping timer!\n");
237 gef_wdt_service();
238 }
239 expect_close = 0;
240
241 clear_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags);
242
243 return 0;
244}
245
246static const struct file_operations gef_wdt_fops = {
247 .owner = THIS_MODULE,
248 .llseek = no_llseek,
249 .write = gef_wdt_write,
250 .unlocked_ioctl = gef_wdt_ioctl,
251 .compat_ioctl = compat_ptr_ioctl,
252 .open = gef_wdt_open,
253 .release = gef_wdt_release,
254};
255
256static struct miscdevice gef_wdt_miscdev = {
257 .minor = WATCHDOG_MINOR,
258 .name = "watchdog",
259 .fops = &gef_wdt_fops,
260};
261
262
263static int gef_wdt_probe(struct platform_device *dev)
264{
265 int timeout = 10;
266 u32 freq;
267
268 bus_clk = 133; /* in MHz */
269
270 freq = fsl_get_sys_freq();
271 if (freq != -1)
272 bus_clk = freq;
273
274 /* Map devices registers into memory */
275 gef_wdt_regs = of_iomap(dev->dev.of_node, 0);
276 if (gef_wdt_regs == NULL)
277 return -ENOMEM;
278
279 gef_wdt_set_timeout(timeout);
280
281 gef_wdt_handler_disable(); /* in case timer was already running */
282
283 return misc_register(&gef_wdt_miscdev);
284}
285
286static int gef_wdt_remove(struct platform_device *dev)
287{
288 misc_deregister(&gef_wdt_miscdev);
289
290 gef_wdt_handler_disable();
291
292 iounmap(gef_wdt_regs);
293
294 return 0;
295}
296
297static const struct of_device_id gef_wdt_ids[] = {
298 {
299 .compatible = "gef,fpga-wdt",
300 },
301 {},
302};
303MODULE_DEVICE_TABLE(of, gef_wdt_ids);
304
305static struct platform_driver gef_wdt_driver = {
306 .driver = {
307 .name = "gef_wdt",
308 .of_match_table = gef_wdt_ids,
309 },
310 .probe = gef_wdt_probe,
311 .remove = gef_wdt_remove,
312};
313
314static int __init gef_wdt_init(void)
315{
316 pr_info("GE watchdog driver\n");
317 return platform_driver_register(&gef_wdt_driver);
318}
319
320static void __exit gef_wdt_exit(void)
321{
322 platform_driver_unregister(&gef_wdt_driver);
323}
324
325module_init(gef_wdt_init);
326module_exit(gef_wdt_exit);
327
328MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
329MODULE_DESCRIPTION("GE watchdog driver");
330MODULE_LICENSE("GPL");
331MODULE_ALIAS("platform:gef_wdt");
1/*
2 * GE watchdog userspace interface
3 *
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 *
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * Based on: mv64x60_wdt.c (MV64X60 watchdog userspace interface)
14 * Author: James Chapman <jchapman@katalix.com>
15 */
16
17/* TODO:
18 * This driver does not provide support for the hardwares capability of sending
19 * an interrupt at a programmable threshold.
20 *
21 * This driver currently can only support 1 watchdog - there are 2 in the
22 * hardware that this driver supports. Thus one could be configured as a
23 * process-based watchdog (via /dev/watchdog), the second (using the interrupt
24 * capabilities) a kernel-based watchdog.
25 */
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#include <linux/kernel.h>
30#include <linux/compiler.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#include <linux/miscdevice.h>
34#include <linux/watchdog.h>
35#include <linux/fs.h>
36#include <linux/of.h>
37#include <linux/of_address.h>
38#include <linux/of_platform.h>
39#include <linux/io.h>
40#include <linux/uaccess.h>
41
42#include <sysdev/fsl_soc.h>
43
44/*
45 * The watchdog configuration register contains a pair of 2-bit fields,
46 * 1. a reload field, bits 27-26, which triggers a reload of
47 * the countdown register, and
48 * 2. an enable field, bits 25-24, which toggles between
49 * enabling and disabling the watchdog timer.
50 * Bit 31 is a read-only field which indicates whether the
51 * watchdog timer is currently enabled.
52 *
53 * The low 24 bits contain the timer reload value.
54 */
55#define GEF_WDC_ENABLE_SHIFT 24
56#define GEF_WDC_SERVICE_SHIFT 26
57#define GEF_WDC_ENABLED_SHIFT 31
58
59#define GEF_WDC_ENABLED_TRUE 1
60#define GEF_WDC_ENABLED_FALSE 0
61
62/* Flags bits */
63#define GEF_WDOG_FLAG_OPENED 0
64
65static unsigned long wdt_flags;
66static int wdt_status;
67static void __iomem *gef_wdt_regs;
68static int gef_wdt_timeout;
69static int gef_wdt_count;
70static unsigned int bus_clk;
71static char expect_close;
72static DEFINE_SPINLOCK(gef_wdt_spinlock);
73
74static bool nowayout = WATCHDOG_NOWAYOUT;
75module_param(nowayout, bool, 0);
76MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
77 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
78
79
80static int gef_wdt_toggle_wdc(int enabled_predicate, int field_shift)
81{
82 u32 data;
83 u32 enabled;
84 int ret = 0;
85
86 spin_lock(&gef_wdt_spinlock);
87 data = ioread32be(gef_wdt_regs);
88 enabled = (data >> GEF_WDC_ENABLED_SHIFT) & 1;
89
90 /* only toggle the requested field if enabled state matches predicate */
91 if ((enabled ^ enabled_predicate) == 0) {
92 /* We write a 1, then a 2 -- to the appropriate field */
93 data = (1 << field_shift) | gef_wdt_count;
94 iowrite32be(data, gef_wdt_regs);
95
96 data = (2 << field_shift) | gef_wdt_count;
97 iowrite32be(data, gef_wdt_regs);
98 ret = 1;
99 }
100 spin_unlock(&gef_wdt_spinlock);
101
102 return ret;
103}
104
105static void gef_wdt_service(void)
106{
107 gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
108 GEF_WDC_SERVICE_SHIFT);
109}
110
111static void gef_wdt_handler_enable(void)
112{
113 if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_FALSE,
114 GEF_WDC_ENABLE_SHIFT)) {
115 gef_wdt_service();
116 pr_notice("watchdog activated\n");
117 }
118}
119
120static void gef_wdt_handler_disable(void)
121{
122 if (gef_wdt_toggle_wdc(GEF_WDC_ENABLED_TRUE,
123 GEF_WDC_ENABLE_SHIFT))
124 pr_notice("watchdog deactivated\n");
125}
126
127static void gef_wdt_set_timeout(unsigned int timeout)
128{
129 /* maximum bus cycle count is 0xFFFFFFFF */
130 if (timeout > 0xFFFFFFFF / bus_clk)
131 timeout = 0xFFFFFFFF / bus_clk;
132
133 /* Register only holds upper 24 bits, bit shifted into lower 24 */
134 gef_wdt_count = (timeout * bus_clk) >> 8;
135 gef_wdt_timeout = timeout;
136}
137
138
139static ssize_t gef_wdt_write(struct file *file, const char __user *data,
140 size_t len, loff_t *ppos)
141{
142 if (len) {
143 if (!nowayout) {
144 size_t i;
145
146 expect_close = 0;
147
148 for (i = 0; i != len; i++) {
149 char c;
150 if (get_user(c, data + i))
151 return -EFAULT;
152 if (c == 'V')
153 expect_close = 42;
154 }
155 }
156 gef_wdt_service();
157 }
158
159 return len;
160}
161
162static long gef_wdt_ioctl(struct file *file, unsigned int cmd,
163 unsigned long arg)
164{
165 int timeout;
166 int options;
167 void __user *argp = (void __user *)arg;
168 static const struct watchdog_info info = {
169 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
170 WDIOF_KEEPALIVEPING,
171 .firmware_version = 0,
172 .identity = "GE watchdog",
173 };
174
175 switch (cmd) {
176 case WDIOC_GETSUPPORT:
177 if (copy_to_user(argp, &info, sizeof(info)))
178 return -EFAULT;
179 break;
180
181 case WDIOC_GETSTATUS:
182 case WDIOC_GETBOOTSTATUS:
183 if (put_user(wdt_status, (int __user *)argp))
184 return -EFAULT;
185 wdt_status &= ~WDIOF_KEEPALIVEPING;
186 break;
187
188 case WDIOC_SETOPTIONS:
189 if (get_user(options, (int __user *)argp))
190 return -EFAULT;
191
192 if (options & WDIOS_DISABLECARD)
193 gef_wdt_handler_disable();
194
195 if (options & WDIOS_ENABLECARD)
196 gef_wdt_handler_enable();
197 break;
198
199 case WDIOC_KEEPALIVE:
200 gef_wdt_service();
201 wdt_status |= WDIOF_KEEPALIVEPING;
202 break;
203
204 case WDIOC_SETTIMEOUT:
205 if (get_user(timeout, (int __user *)argp))
206 return -EFAULT;
207 gef_wdt_set_timeout(timeout);
208 /* Fall through */
209
210 case WDIOC_GETTIMEOUT:
211 if (put_user(gef_wdt_timeout, (int __user *)argp))
212 return -EFAULT;
213 break;
214
215 default:
216 return -ENOTTY;
217 }
218
219 return 0;
220}
221
222static int gef_wdt_open(struct inode *inode, struct file *file)
223{
224 if (test_and_set_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags))
225 return -EBUSY;
226
227 if (nowayout)
228 __module_get(THIS_MODULE);
229
230 gef_wdt_handler_enable();
231
232 return nonseekable_open(inode, file);
233}
234
235static int gef_wdt_release(struct inode *inode, struct file *file)
236{
237 if (expect_close == 42)
238 gef_wdt_handler_disable();
239 else {
240 pr_crit("unexpected close, not stopping timer!\n");
241 gef_wdt_service();
242 }
243 expect_close = 0;
244
245 clear_bit(GEF_WDOG_FLAG_OPENED, &wdt_flags);
246
247 return 0;
248}
249
250static const struct file_operations gef_wdt_fops = {
251 .owner = THIS_MODULE,
252 .llseek = no_llseek,
253 .write = gef_wdt_write,
254 .unlocked_ioctl = gef_wdt_ioctl,
255 .open = gef_wdt_open,
256 .release = gef_wdt_release,
257};
258
259static struct miscdevice gef_wdt_miscdev = {
260 .minor = WATCHDOG_MINOR,
261 .name = "watchdog",
262 .fops = &gef_wdt_fops,
263};
264
265
266static int gef_wdt_probe(struct platform_device *dev)
267{
268 int timeout = 10;
269 u32 freq;
270
271 bus_clk = 133; /* in MHz */
272
273 freq = fsl_get_sys_freq();
274 if (freq != -1)
275 bus_clk = freq;
276
277 /* Map devices registers into memory */
278 gef_wdt_regs = of_iomap(dev->dev.of_node, 0);
279 if (gef_wdt_regs == NULL)
280 return -ENOMEM;
281
282 gef_wdt_set_timeout(timeout);
283
284 gef_wdt_handler_disable(); /* in case timer was already running */
285
286 return misc_register(&gef_wdt_miscdev);
287}
288
289static int gef_wdt_remove(struct platform_device *dev)
290{
291 misc_deregister(&gef_wdt_miscdev);
292
293 gef_wdt_handler_disable();
294
295 iounmap(gef_wdt_regs);
296
297 return 0;
298}
299
300static const struct of_device_id gef_wdt_ids[] = {
301 {
302 .compatible = "gef,fpga-wdt",
303 },
304 {},
305};
306MODULE_DEVICE_TABLE(of, gef_wdt_ids);
307
308static struct platform_driver gef_wdt_driver = {
309 .driver = {
310 .name = "gef_wdt",
311 .of_match_table = gef_wdt_ids,
312 },
313 .probe = gef_wdt_probe,
314 .remove = gef_wdt_remove,
315};
316
317static int __init gef_wdt_init(void)
318{
319 pr_info("GE watchdog driver\n");
320 return platform_driver_register(&gef_wdt_driver);
321}
322
323static void __exit gef_wdt_exit(void)
324{
325 platform_driver_unregister(&gef_wdt_driver);
326}
327
328module_init(gef_wdt_init);
329module_exit(gef_wdt_exit);
330
331MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
332MODULE_DESCRIPTION("GE watchdog driver");
333MODULE_LICENSE("GPL");
334MODULE_ALIAS("platform:gef_wdt");