Blame view

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

1c48a5c93   Grant Likely   dt: Eliminate of_...
301
  static struct platform_driver gef_wdt_driver = {
4018294b5   Grant Likely   of: Remove duplic...
302
303
  	.driver = {
  		.name = "gef_wdt",
4018294b5   Grant Likely   of: Remove duplic...
304
305
  		.of_match_table = gef_wdt_ids,
  	},
3268b5618   Martyn Welch   [WATCHDOG] Basic ...
306
  	.probe		= gef_wdt_probe,
673717656   Devendra Naga   watchdog: gef_wdt...
307
  	.remove		= gef_wdt_remove,
3268b5618   Martyn Welch   [WATCHDOG] Basic ...
308
309
310
311
  };
  
  static int __init gef_wdt_init(void)
  {
27c766aaa   Joe Perches   watchdog: Use pr_...
312
313
  	pr_info("GE watchdog driver
  ");
1c48a5c93   Grant Likely   dt: Eliminate of_...
314
  	return platform_driver_register(&gef_wdt_driver);
3268b5618   Martyn Welch   [WATCHDOG] Basic ...
315
316
317
318
  }
  
  static void __exit gef_wdt_exit(void)
  {
1c48a5c93   Grant Likely   dt: Eliminate of_...
319
  	platform_driver_unregister(&gef_wdt_driver);
3268b5618   Martyn Welch   [WATCHDOG] Basic ...
320
321
322
323
  }
  
  module_init(gef_wdt_init);
  module_exit(gef_wdt_exit);
cda61c942   Martyn Welch   [WATCHDOG] gef_wd...
324
325
  MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  MODULE_DESCRIPTION("GE watchdog driver");
3268b5618   Martyn Welch   [WATCHDOG] Basic ...
326
  MODULE_LICENSE("GPL");
ae2a00607   Axel Lin   watchdog: gef_wdt...
327
  MODULE_ALIAS("platform:gef_wdt");