Blame view

drivers/watchdog/mv64x60_wdt.c 7.78 KB
3be10211a   James Chapman   [WATCHDOG] mv64x6...
1
2
3
4
5
6
7
8
9
10
  /*
   * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
   *
   * Author: James Chapman <jchapman@katalix.com>
   *
   * Platform-specific setup code should configure the dog to generate
   * interrupt or reset as required.  This code only enables/disables
   * and services the watchdog.
   *
   * Derived from mpc8xx_wdt.c, with the following copyright.
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
11
   *
3be10211a   James Chapman   [WATCHDOG] mv64x6...
12
13
14
15
16
   * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under
   * the terms of the GNU General Public License version 2. This program
   * is licensed "as is" without any warranty of any kind, whether express
   * or implied.
   */
3be10211a   James Chapman   [WATCHDOG] mv64x6...
17
18
19
20
21
22
  #include <linux/fs.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/miscdevice.h>
  #include <linux/module.h>
  #include <linux/watchdog.h>
d052d1bef   Russell King   Create platform_d...
23
  #include <linux/platform_device.h>
7e07a1591   Dale Farnsworth   [WATCHDOG] mv64x6...
24
  #include <linux/mv643xx.h>
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
25
26
  #include <linux/uaccess.h>
  #include <linux/io.h>
3be10211a   James Chapman   [WATCHDOG] mv64x6...
27

8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
28
  #define MV64x60_WDT_WDC_OFFSET	0
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  /*
   * 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 MV64x60_WDC_ENABLE_SHIFT	24
  #define MV64x60_WDC_SERVICE_SHIFT	26
  #define MV64x60_WDC_ENABLED_SHIFT	31
  
  #define MV64x60_WDC_ENABLED_TRUE	1
  #define MV64x60_WDC_ENABLED_FALSE	0
3be10211a   James Chapman   [WATCHDOG] mv64x6...
46
47
48
  
  /* Flags bits */
  #define MV64x60_WDOG_FLAG_OPENED	0
3be10211a   James Chapman   [WATCHDOG] mv64x6...
49
50
51
  
  static unsigned long wdt_flags;
  static int wdt_status;
8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
52
  static void __iomem *mv64x60_wdt_regs;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
53
  static int mv64x60_wdt_timeout;
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
54
  static int mv64x60_wdt_count;
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
55
  static unsigned int bus_clk;
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
56
  static char expect_close;
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
57
  static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
58

d37a5c3dd   Dale Farnsworth   [WATCHDOG] mv64x6...
59
60
  static int nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, int, 0);
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
61
62
63
  MODULE_PARM_DESC(nowayout,
  		"Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
d37a5c3dd   Dale Farnsworth   [WATCHDOG] mv64x6...
64

f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
65
  static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
66
  {
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  	u32 data;
  	u32 enabled;
  	int ret = 0;
  
  	spin_lock(&mv64x60_wdt_spinlock);
  	data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  	enabled = (data >> MV64x60_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) | mv64x60_wdt_count;
  		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  
  		data = (2 << field_shift) | mv64x60_wdt_count;
  		writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  		ret = 1;
  	}
  	spin_unlock(&mv64x60_wdt_spinlock);
  
  	return ret;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
88
89
90
91
  }
  
  static void mv64x60_wdt_service(void)
  {
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
92
93
  	mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  			       MV64x60_WDC_SERVICE_SHIFT);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
94
  }
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
95
  static void mv64x60_wdt_handler_enable(void)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
96
  {
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
97
98
99
100
101
  	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
  				   MV64x60_WDC_ENABLE_SHIFT)) {
  		mv64x60_wdt_service();
  		printk(KERN_NOTICE "mv64x60_wdt: watchdog activated
  ");
3be10211a   James Chapman   [WATCHDOG] mv64x6...
102
103
  	}
  }
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
104
  static void mv64x60_wdt_handler_disable(void)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
105
  {
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
106
107
108
109
  	if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  				   MV64x60_WDC_ENABLE_SHIFT))
  		printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated
  ");
3be10211a   James Chapman   [WATCHDOG] mv64x6...
110
  }
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
111
  static void mv64x60_wdt_set_timeout(unsigned int timeout)
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
112
113
114
115
  {
  	/* maximum bus cycle count is 0xFFFFFFFF */
  	if (timeout > 0xFFFFFFFF / bus_clk)
  		timeout = 0xFFFFFFFF / bus_clk;
f18699940   Dale Farnsworth   [WATCHDOG] mv64x6...
116
  	mv64x60_wdt_count = timeout * bus_clk >> 8;
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
117
  	mv64x60_wdt_timeout = timeout;
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
118
  }
3be10211a   James Chapman   [WATCHDOG] mv64x6...
119
120
121
122
  static int mv64x60_wdt_open(struct inode *inode, struct file *file)
  {
  	if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
  		return -EBUSY;
d37a5c3dd   Dale Farnsworth   [WATCHDOG] mv64x6...
123
124
  	if (nowayout)
  		__module_get(THIS_MODULE);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
125
  	mv64x60_wdt_handler_enable();
861e51377   Dale Farnsworth   [WATCHDOG] mv64x6...
126
  	return nonseekable_open(inode, file);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
127
128
129
130
  }
  
  static int mv64x60_wdt_release(struct inode *inode, struct file *file)
  {
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
131
  	if (expect_close == 42)
d37a5c3dd   Dale Farnsworth   [WATCHDOG] mv64x6...
132
  		mv64x60_wdt_handler_disable();
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
133
134
135
136
137
138
139
  	else {
  		printk(KERN_CRIT
  		       "mv64x60_wdt: unexpected close, not stopping timer!
  ");
  		mv64x60_wdt_service();
  	}
  	expect_close = 0;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
140
141
142
143
144
  
  	clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
  
  	return 0;
  }
b2846dfa4   Al Viro   [PATCH] mv64x60_w...
145
  static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
146
  				 size_t len, loff_t *ppos)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
147
  {
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
148
149
150
151
152
153
154
155
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			expect_close = 0;
  
  			for (i = 0; i != len; i++) {
  				char c;
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
156
  				if (get_user(c, data + i))
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
157
158
159
160
161
  					return -EFAULT;
  				if (c == 'V')
  					expect_close = 42;
  			}
  		}
3be10211a   James Chapman   [WATCHDOG] mv64x6...
162
  		mv64x60_wdt_service();
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
163
  	}
3be10211a   James Chapman   [WATCHDOG] mv64x6...
164
165
166
  
  	return len;
  }
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
167
168
  static long mv64x60_wdt_ioctl(struct file *file,
  					unsigned int cmd, unsigned long arg)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
169
  {
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
170
  	int timeout;
85d57238d   Dale Farnsworth   [WATCHDOG] mv64x6...
171
  	int options;
b2846dfa4   Al Viro   [PATCH] mv64x60_w...
172
  	void __user *argp = (void __user *)arg;
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
173
  	static const struct watchdog_info info = {
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
174
  		.options =	WDIOF_SETTIMEOUT	|
bf2fc92ca   Dale Farnsworth   [WATCHDOG] mv64x6...
175
  				WDIOF_MAGICCLOSE	|
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
176
  				WDIOF_KEEPALIVEPING,
3be10211a   James Chapman   [WATCHDOG] mv64x6...
177
178
179
180
181
182
  		.firmware_version = 0,
  		.identity = "MV64x60 watchdog",
  	};
  
  	switch (cmd) {
  	case WDIOC_GETSUPPORT:
b2846dfa4   Al Viro   [PATCH] mv64x60_w...
183
  		if (copy_to_user(argp, &info, sizeof(info)))
3be10211a   James Chapman   [WATCHDOG] mv64x6...
184
185
186
187
188
  			return -EFAULT;
  		break;
  
  	case WDIOC_GETSTATUS:
  	case WDIOC_GETBOOTSTATUS:
b2846dfa4   Al Viro   [PATCH] mv64x60_w...
189
  		if (put_user(wdt_status, (int __user *)argp))
3be10211a   James Chapman   [WATCHDOG] mv64x6...
190
191
192
193
194
195
196
197
  			return -EFAULT;
  		wdt_status &= ~WDIOF_KEEPALIVEPING;
  		break;
  
  	case WDIOC_GETTEMP:
  		return -EOPNOTSUPP;
  
  	case WDIOC_SETOPTIONS:
85d57238d   Dale Farnsworth   [WATCHDOG] mv64x6...
198
199
200
201
202
203
204
205
206
  		if (get_user(options, (int __user *)argp))
  			return -EFAULT;
  
  		if (options & WDIOS_DISABLECARD)
  			mv64x60_wdt_handler_disable();
  
  		if (options & WDIOS_ENABLECARD)
  			mv64x60_wdt_handler_enable();
  		break;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
207
208
209
210
211
212
213
  
  	case WDIOC_KEEPALIVE:
  		mv64x60_wdt_service();
  		wdt_status |= WDIOF_KEEPALIVEPING;
  		break;
  
  	case WDIOC_SETTIMEOUT:
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
214
215
216
217
  		if (get_user(timeout, (int __user *)argp))
  			return -EFAULT;
  		mv64x60_wdt_set_timeout(timeout);
  		/* Fall through */
3be10211a   James Chapman   [WATCHDOG] mv64x6...
218
219
  
  	case WDIOC_GETTIMEOUT:
264f09915   Dale Farnsworth   [WATCHDOG] mv64x6...
220
  		if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
3be10211a   James Chapman   [WATCHDOG] mv64x6...
221
222
223
224
  			return -EFAULT;
  		break;
  
  	default:
795b89d20   Samuel Tardieu   [WATCHDOG] use EN...
225
  		return -ENOTTY;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
226
227
228
229
  	}
  
  	return 0;
  }
62322d255   Arjan van de Ven   [PATCH] make more...
230
  static const struct file_operations mv64x60_wdt_fops = {
3be10211a   James Chapman   [WATCHDOG] mv64x6...
231
232
233
  	.owner = THIS_MODULE,
  	.llseek = no_llseek,
  	.write = mv64x60_wdt_write,
a86b84986   Alan Cox   [WATCHDOG 29/57] ...
234
  	.unlocked_ioctl = mv64x60_wdt_ioctl,
3be10211a   James Chapman   [WATCHDOG] mv64x6...
235
236
237
238
239
240
241
242
243
  	.open = mv64x60_wdt_open,
  	.release = mv64x60_wdt_release,
  };
  
  static struct miscdevice mv64x60_wdt_miscdev = {
  	.minor = WATCHDOG_MINOR,
  	.name = "watchdog",
  	.fops = &mv64x60_wdt_fops,
  };
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
244
  static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
245
  {
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
246
  	struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
247
  	struct resource *r;
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
248
  	int timeout = 10;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
249

94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
250
  	bus_clk = 133;			/* in MHz */
3be10211a   James Chapman   [WATCHDOG] mv64x6...
251
  	if (pdata) {
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
252
  		timeout = pdata->timeout;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
253
254
  		bus_clk = pdata->bus_clk;
  	}
94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
255
256
257
258
259
260
  	/* Since bus_clk is truncated MHz, actual frequency could be
  	 * up to 1MHz higher.  Round up, since it's better to time out
  	 * too late than too soon.
  	 */
  	bus_clk++;
  	bus_clk *= 1000000;		/* convert to Hz */
8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
261
262
263
  	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  	if (!r)
  		return -ENODEV;
b782a5637   H Hartley Sweeten   [WATCHDOG] use re...
264
  	mv64x60_wdt_regs = ioremap(r->start, resource_size(r));
8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
265
266
  	if (mv64x60_wdt_regs == NULL)
  		return -ENOMEM;
3be10211a   James Chapman   [WATCHDOG] mv64x6...
267

94796f908   Dale Farnsworth   [WATCHDOG] mv64x6...
268
  	mv64x60_wdt_set_timeout(timeout);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
269

2422df5e2   Dale Farnsworth   [WATCHDOG] mv64x6...
270
  	mv64x60_wdt_handler_disable();	/* in case timer was already running */
3be10211a   James Chapman   [WATCHDOG] mv64x6...
271
272
  	return misc_register(&mv64x60_wdt_miscdev);
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
273
  static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
3be10211a   James Chapman   [WATCHDOG] mv64x6...
274
275
  {
  	misc_deregister(&mv64x60_wdt_miscdev);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
276
  	mv64x60_wdt_handler_disable();
8a5cfa648   Dale Farnsworth   [WATCHDOG] mv64x6...
277
  	iounmap(mv64x60_wdt_regs);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
278
279
  	return 0;
  }
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
280
  static struct platform_driver mv64x60_wdt_driver = {
3be10211a   James Chapman   [WATCHDOG] mv64x6...
281
282
  	.probe = mv64x60_wdt_probe,
  	.remove = __devexit_p(mv64x60_wdt_remove),
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
283
284
285
286
  	.driver = {
  		.owner = THIS_MODULE,
  		.name = MV64x60_WDT_NAME,
  	},
3be10211a   James Chapman   [WATCHDOG] mv64x6...
287
  };
3be10211a   James Chapman   [WATCHDOG] mv64x6...
288
289
  static int __init mv64x60_wdt_init(void)
  {
3be10211a   James Chapman   [WATCHDOG] mv64x6...
290
291
  	printk(KERN_INFO "MV64x60 watchdog driver
  ");
422db8d22   Dale Farnsworth   [WATCHDOG] mv64x6...
292
  	return platform_driver_register(&mv64x60_wdt_driver);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
293
294
295
296
  }
  
  static void __exit mv64x60_wdt_exit(void)
  {
3ae5eaec1   Russell King   [DRIVER MODEL] Co...
297
  	platform_driver_unregister(&mv64x60_wdt_driver);
3be10211a   James Chapman   [WATCHDOG] mv64x6...
298
299
300
301
302
303
304
305
306
  }
  
  module_init(mv64x60_wdt_init);
  module_exit(mv64x60_wdt_exit);
  
  MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  MODULE_DESCRIPTION("MV64x60 watchdog driver");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
f37d193c7   Kay Sievers   watchdog: fix pla...
307
  MODULE_ALIAS("platform:" MV64x60_WDT_NAME);